forked from ls1248659692/python_guide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdifferent_method.py
More file actions
122 lines (90 loc) · 2.76 KB
/
different_method.py
File metadata and controls
122 lines (90 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/python
# coding=utf8
import math
import time
import pytest
__author__ = 'Jam'
__date__ = '2019/5/30 9:24'
class Circle(object):
def __init__(self, radius, name):
self.radius = radius
self.__name = name
@property
def area(self):
return math.pi * self.radius ** 2
@property
def perimeter(self):
return 2 * math.pi * self.radius
@property
def name(self):
return self.__name
@name.setter
def name(self, value):
if not isinstance(value, str):
raise TypeError('%s must be str' % value)
self.__NAME = value
@name.deleter
def name(self):
raise TypeError('Can not delete')
@classmethod
def compute_volume(cls, height, radius):
circle = cls(radius, 'circle')
return height * circle.area
class Date:
def __init__(self, year, month, day):
self.year = year
self.month = month
self.day = day
@staticmethod
def now():
t = time.localtime()
return Date(t.tm_year, t.tm_mon, t.tm_mday)
@staticmethod
def tomorrow():
t = time.localtime(time.time() + 86400)
return Date(t.tm_year, t.tm_mon, t.tm_mday)
class GetDate(object):
def __init__(self, year=0, month=0, day=0):
self.day = day
self.month = month
self.year = year
def out_date(self):
print "year :{}, month :{}, day :{}".format(self.year, self.month, self.day)
@classmethod
def get_date(cls, data_as_string):
year, month, day = map(int, data_as_string.split('-'))
date = cls(year, month, day)
return date
@staticmethod
def is_date_valid(date_as_string):
year, month, day = map(int, date_as_string.split('-'))
return day <= 31 and month <= 12 and year <= 3999
def unit_test():
c = Circle(10, 'circle')
print c.area, c.name
print Circle.compute_volume(10, 10)
with pytest.raises(Exception) as excinfo:
# 此时的特性area和perimeter不能被赋值
c.area = 3
assert "AttributeError" in str(excinfo.typename)
assert "can't set attribute" in str(excinfo.value)
with pytest.raises(Exception) as excinfo:
# 此时的特性name不能删除
del c.name
assert "TypeError" in str(excinfo.typename)
assert "Can not delete" in str(excinfo.value)
a = Date(1897, 11, 27)
b = Date.now()
c = Date.tomorrow()
print(a.year, a.month, a.day)
print(b.year, b.month, b.day)
print(c.year, c.month, c.day)
string_date = '2016-8-6'
s = GetDate(*string_date.split('-'))
s.out_date()
r = GetDate.get_date("2016-8-6")
r.out_date()
is_valid = GetDate.is_date_valid('2016-8-6')
print is_valid
if __name__ == '__main__':
unit_test()