forked from thefriedbee/video-source-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat_print_examples.py
More file actions
138 lines (80 loc) · 3.7 KB
/
Copy pathformat_print_examples.py
File metadata and controls
138 lines (80 loc) · 3.7 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
123
124
125
126
127
128
129
130
131
132
133
134
# examples of print methods
# Tom, born in 2000, is a freshman student with GPA 3.1415926
# Bob, born in 1995, is a graduate student with GPA 4
p1 = ['Tom', 'freshman', {'gpa': 3.1415926, 'dob': 2000}]
p2 = ['Bob', 'graduate', {'gpa': 4, 'dob': 1995}]
# bad example 1
my_str = p1[0] + ', born in ' + str(p1[2]['dob']) + ', is a ' + p1[1] + ' student with GPA ' + str(p1[2]['gpa'])
print(my_str)
# bad example 2
print(p1[0], ', born in ', p1[2]['dob'], ', is a ', p1[1], ' student with GPA ', p1[2]['gpa'], sep="")
# format string
my_str = '{}, born in {}, is a {} student with GPA {}'.format(p1[0], p1[2]['dob'], p1[1], p1[2]['gpa'])
print(my_str)
my_str = '{0}, born in {2[dob]}, is a {1} student with GPA {2[gpa]}'.format(p1[0], p1[1], p1[2])
print(my_str)
my_str = '{0[0]}, born in {0[2][dob]}, is a {0[1]} student with GPA {0[2][gpa]}'.format(p1)
print(my_str)
# can also access attributes in this way
class Person():
def __init__(self, name, role, dob, gpa):
self.name = name
self.role = role
self.dob = dob
self.gpa = gpa
p1 = Person('Tom', 'freshman', '2000', '3.1415936')
print('{0.name}, born in {0.dob}, is a {0.role} student with GPA {0.gpa}'.format(p1))
# locate by keyword arguments
print('{name} is born in {dob}'.format(name='Tom', dob='2000'))
# usage: unload a dictionary or list
mini_p1 = {'name': 'Tom', 'dob':'2000', 'useless':'blablabla' }
print('{name} is born in {dob}'.format(**mini_p1))
mini_p1 = ['Tom', 'freshman', {'gpa':3.1415926, 'dob': 2000}]
print('{0} is born in {2[dob]}'.format(*mini_p1))
# old-school format
# s: str;
# d: decimal integer;
# f: float
print('%s is born in %d' % ('Tom', 2000.2))
print('%s is born in %f' % ('Tom', 2000))
print('%s is born in %.2f' % ('Tom', 2000))
# formatting using colon symbol
mini_p1 = ['Tom', 'freshman', {'gpa':3.1415926, 'dob': 2000}]
mini_p2 = ['Bob', 'graduate', {'gpa':4, 'dob': 1995}]
print('{0} is born in {2[dob]:05}'.format(*mini_p1))
print('{0} is born in {2[dob]:05.2f}'.format(*mini_p1))
print('{0} is born in {2[dob]:.2f}'.format(*mini_p1))
print('{0} is born in {2[dob]:.2f}'.format(*mini_p2))
mini_p1 = ['Tom', 'freshman', {'gpa':3.1415926, 'dob': 2000, 'salary': 12345678}]
mini_p2 = ['Bob', 'graduate', {'gpa':4, 'dob': 1995, 'salary': 12345}]
print('{0} has salary ${2[salary]:>20,.2f}/year'.format(*mini_p1))
print('{0} has salary ${2[salary]:>20,.2f}/year'.format(*mini_p2))
# format-specification-language
# https://docs.python.org/3/library/string.html#format-specification-mini-language
# format also works for datetime, like functions strftime(), strptime()
from datetime import datetime
mini_p1 = ['Tom', 'freshman', {'gpa':3.1415926, 'dob': '2000-01-01', 'salary': 12345678}]
mini_p1[2]['dob'] = datetime.strptime(mini_p1[2]['dob'], '%Y-%m-%d')
print('{0} was born in {2[dob]:%B, %d, %Y}, on a quiet {2[dob]: %A}'.format(*mini_p1))
# last but not the least. duck typing
from datetime import datetime
class Person():
def __init__(self, name, role, dob, gpa, salary):
self.name = name
self.role = role
self.dob = datetime.strptime(dob, '%Y-%m-%d')
self.gpa = gpa
self.salary = salary
def __str__(self):
# summary sentense
return '{0.name}, born in {0.dob}, is a {0.role} student with GPA {0.gpa}'.format(self)
def __format__(self, format_settings='summary'):
if format_settings == 'salary':
return '{0.name} is has salary ${0.salary:>20,.2f}/year'.format(self)
else:
return self.__str__()
p1 = Person('Tom', 'freshman', '2000-01-01', 3.1415926, 12345678)
p2 = Person('Bob', 'graduate', '1995-12-31', 4, 12345)
print(p1)
print('{:salary}'.format(p2))
print('{:summary}'.format(p2))