-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharticle_examples.py
More file actions
96 lines (72 loc) · 1.81 KB
/
article_examples.py
File metadata and controls
96 lines (72 loc) · 1.81 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
from lavalamp import lavalamp
"""
Dictionary Creation
"""
my_dict = {'key1': 1, 'key2': 2}
my_dict = dict(key1=1, key2=2)
my_dict = {}
my_dict = dict()
my_dict['key'] = 123
# Define a dict with some string values and keys
my_dict = {
'my_nested_dict':
{
'a_key': 'a_value',
'another_key': 'another_value',
}
}
my_variable = my_dict['my_nested_dict']
"""
Practical Use Cases
"""
class User(object):
""" Stores info about Users """
def __init__(self, name, email, address, password, url):
self.name = name
self.email = email
...
def send_email(self):
""" Send an email to our user"""
pass
def __repr__():
"""Logic to properly format data"""
bill = User('bill @ gmail.com', '123 Acme Dr.', 'secret-password',
'http: // www.bill.com')
bill.send_email()
bill = {'email': 'bill@gmail.com',
'address': '123 Acme Dr.',
'password': 'secret-password',
'url': 'http://www.bill.com'}
def send_email(user_dict):
pass
# smtp email logic …
send_email(bill['email']) # bracket notation or …
send_email(bill.get('email')) # .get() method is handy, too
# Sample user data
json_response = [{
"id": 1,
"first_name": "Florentia",
"last_name": "Schelle",
"email": "fschelle0@nyu.edu",
"url": "https://wired.com"
}, {
"id": 2,
"first_name": "Montague",
"last_name": "McAteer",
"email": "mmcateer1@zdnet.com",
"url": "https://domainmarket.com"
}, {
"id": 3,
"first_name": "Dav",
"last_name": "Yurin",
"email": "dyurin2@e-recht24.de",
"url": "http://wufoo.com"
}]
users = []
for i in json_response:
users.append(User(
name=i['first_name'] + i['last_name'],
email = i['email'],
url=i['url'],
# ...
))