Skip to content

Commit dbda852

Browse files
committed
Fix liniting errors
1 parent cbfda8b commit dbda852

4 files changed

Lines changed: 55 additions & 69 deletions

File tree

data/boston/script/table2csv.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
"""Convert raw Boston club membership data into CSV."""
2-
32
import csv
43
import string
54
import sys
65

76

87
def namify_member(name):
98
"""Convert data formatted name into human readable form."""
10-
[last_name, first_name] = map(lambda s: s.capitalize(), name.split("."))
11-
return " ".join([first_name, last_name])
9+
[last_name, first_name] = map(lambda s: s.capitalize(), name.split('.'))
10+
return ' '.join([first_name, last_name])
1211

1312

1413
def namify_club(name):
1514
"""Convert data formmated club name into human readable form."""
16-
1715
def split_on(s, splitters):
1816
if not s:
1917
return []
@@ -24,46 +22,46 @@ def split_on(s, splitters):
2422

2523
return [s]
2624

27-
return " ".join(split_on(name, string.ascii_uppercase))
25+
return ' '.join(split_on(name, string.ascii_uppercase))
2826

2927

30-
if __name__ == "__main__":
28+
if __name__ == '__main__':
3129
# Read in the raw data.
3230
raw_data = list(map(dict, csv.DictReader(sys.stdin)))
3331

3432
# Extract the club and member names.
3533
club_names = [k for k in raw_data[0] if k]
36-
member_names = [row[""] for row in raw_data]
34+
member_names = [row[''] for row in raw_data]
3735

3836
# Construct tables to a unique ID for each entity.
3937
ids = {name: idx for (idx, name) in enumerate(club_names + member_names)}
4038

4139
# Create member and club tables.
4240
members = [
43-
{"_key": ids[name], "name": namify_member(name)} for name in member_names
41+
{'_key': ids[name], 'name': namify_member(name)} for name in member_names
4442
]
45-
clubs = [{"_key": ids[name], "name": namify_club(name)} for name in club_names]
43+
clubs = [{'_key': ids[name], 'name': namify_club(name)} for name in club_names]
4644

4745
# Create the membership table.
4846
membership = []
4947
for row in raw_data:
50-
member_name = row[""]
51-
club_names = [club_name for club_name in row if row[club_name] == "1"]
48+
member_name = row['']
49+
club_names = [club_name for club_name in row if row[club_name] == '1']
5250

5351
for club_name in club_names:
5452
membership.append(
5553
{
56-
"_from": f"members/{ids[member_name]}",
57-
"_to": f"clubs/{ids[club_name]}",
54+
'_from': f'members/{ids[member_name]}',
55+
'_to': f'clubs/{ids[club_name]}',
5856
}
5957
)
6058

6159
# Dump the tables to disk as csv files.
62-
for table_name in ["clubs", "members", "membership"]:
63-
csvname = f"{table_name}.csv"
60+
for table_name in ['clubs', 'members', 'membership']:
61+
csvname = f'{table_name}.csv'
6462
table = eval(table_name)
6563

66-
with open(csvname, "w") as csvfile:
64+
with open(csvname, 'w') as csvfile:
6765
writer = csv.DictWriter(csvfile, fieldnames=list(table[0]))
6866

6967
writer.writeheader()
Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,33 @@
11
"""Download Les Miserables data from the web and process it into Multinet CSV files."""
2-
32
import csv
43
import json
54
import sys
65

76

87
def add_key(rec, idx):
98
"""Add a key value to the character records."""
9+
rec['_key'] = rec['id']
10+
rec['influential'] = 'false' if rec['influential'] == 'False' else 'true'
11+
rec['original'] = 'false' if rec['original'] == 'False' else 'true'
1012

11-
rec["_key"] = rec["id"]
12-
rec["influential"] = "false" if rec["influential"] == "False" else "true"
13-
rec["original"] = "false" if rec["original"] == "False" else "true"
14-
15-
del rec["utc_offset"]
16-
del rec["id"]
13+
del rec['utc_offset']
14+
del rec['id']
1715

1816
return rec
1917

2018

2119
def convert_link(link, idx):
2220
"""Convert the D3 JSON link data into a Multinet-style record."""
23-
2421
return {
25-
"_key": str(idx),
26-
"_from": f"""people/{link["source"]}""",
27-
"_to": f"""people/{link["target"]}""",
22+
'_key': str(idx),
23+
'_from': f'''people/{link['source']}''',
24+
'_to': f'''people/{link['target']}''',
2825
}
2926

3027

3128
def write_csv(data, fields, filename):
3229
"""Write a CSV file from data and field names."""
33-
34-
with open(filename, "w") as f:
30+
with open(filename, 'w') as f:
3531
writer = csv.DictWriter(f, fieldnames=fields)
3632

3733
writer.writeheader()
@@ -41,17 +37,16 @@ def write_csv(data, fields, filename):
4137

4238
def main():
4339
"""Run main function."""
44-
4540
data = json.loads(sys.stdin.read())
4641

4742
# Prepare the node data by adjoining a key value equal to each record's
4843
# index in the original data.
49-
nodes = [add_key(record, index) for (index, record) in enumerate(data["nodes"])]
44+
nodes = [add_key(record, index) for (index, record) in enumerate(data['nodes'])]
5045

5146
# Convert the link data to Multinet form. Note that the D3 JSON format uses
5247
# node list indices to refer to the source and target nodes; these can be
5348
# used unchanged because of how the key value for the nodes was set above.
54-
links = [convert_link(link, index) for (index, link) in enumerate(data["links"])]
49+
links = [convert_link(link, index) for (index, link) in enumerate(data['links'])]
5550

5651
# Reduce the total number of nodes by truncating
5752
nodes = [nodes[i] for i in range(0, 100)]
@@ -61,8 +56,8 @@ def main():
6156
link
6257
for link in links
6358
if (
64-
any(f"people/{node['_key']}" == link["_from"] for node in nodes)
65-
and any(f"people/{node['_key']}" == link["_to"] for node in nodes)
59+
any(f'''people/{node['_key']}''' == link['_from'] for node in nodes)
60+
and any(f'''people/{node['_key']}''' == link['_to'] for node in nodes)
6661
)
6762
]
6863
links = [link for (index, link) in enumerate(links) if index % 10 == 0]
@@ -71,25 +66,25 @@ def main():
7166
write_csv(
7267
nodes,
7368
[
74-
"_key",
75-
"followers_count",
76-
"query_tweet_count",
77-
"friends_count",
78-
"statuses_count",
79-
"listed_count",
80-
"favourites_count",
81-
"count_followers_in_query",
82-
"screen_name",
83-
"profile_image_url",
84-
"influential",
85-
"original",
69+
'_key',
70+
'followers_count',
71+
'query_tweet_count',
72+
'friends_count',
73+
'statuses_count',
74+
'listed_count',
75+
'favourites_count',
76+
'count_followers_in_query',
77+
'screen_name',
78+
'profile_image_url',
79+
'influential',
80+
'original',
8681
],
87-
"people.csv",
82+
'people.csv',
8883
)
89-
write_csv(links, ["_key", "_from", "_to"], "connections.csv")
84+
write_csv(links, ['_key', '_from', '_to'], 'connections.csv')
9085

9186
return 0
9287

9388

94-
if __name__ == "__main__":
89+
if __name__ == '__main__':
9590
sys.exit(main())

data/miserables/scripts/process.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,27 @@
11
"""Download Les Miserables data from the web and process it into Multinet CSV files."""
2-
32
import csv
43
import json
54
import sys
65

76

87
def add_key(rec, idx):
98
"""Add a key value to the character records."""
10-
11-
rec["_key"] = idx
9+
rec['_key'] = idx
1210
return rec
1311

1412

1513
def convert_link(link):
1614
"""Convert the D3 JSON link data into a Multinet-style record."""
17-
1815
return {
19-
"_from": f"""characters/{link["source"]}""",
20-
"_to": f"""characters/{link["target"]}""",
21-
"value": link["value"],
16+
'_from': f'''characters/{link['source']}''',
17+
'_to': f'''characters/{link['target']}''',
18+
'value': link['value'],
2219
}
2320

2421

2522
def write_csv(data, fields, filename):
2623
"""Write a CSV file from data and field names."""
27-
28-
with open(filename, "w") as f:
24+
with open(filename, 'w') as f:
2925
writer = csv.DictWriter(f, fieldnames=fields)
3026

3127
writer.writeheader()
@@ -35,7 +31,6 @@ def write_csv(data, fields, filename):
3531

3632
def main():
3733
"""Run main function."""
38-
3934
data = json.loads(sys.stdin.read())
4035

4136
# Prepare the node data by adjoining a key value equal to each record's
@@ -48,11 +43,11 @@ def main():
4843
links = [convert_link(link) for link in data["links"]]
4944

5045
# Write out both the node and link data to CSV files.
51-
write_csv(nodes, ["_key", "name", "group"], "characters.csv")
52-
write_csv(links, ["_from", "_to", "value"], "relationships.csv")
46+
write_csv(nodes, ['_key', 'name', 'group'], 'characters.csv')
47+
write_csv(links, ['_from', '_to', 'value'], 'relationships.csv')
5348

5449
return 0
5550

5651

57-
if __name__ == "__main__":
52+
if __name__ == '__main__':
5853
sys.exit(main())

data/openflights/script/process.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
11
"""Repair the stock airport flights data so it works properly with Multinet."""
2-
32
import csv
43
import sys
54

65

76
def main():
87
"""Run main function."""
9-
108
with open(sys.argv[1]) as nodes:
119
reader = csv.DictReader(nodes)
12-
ids = {n["_key"] for n in reader}
10+
ids = {n['_key'] for n in reader}
1311

1412
reader = csv.DictReader(sys.stdin)
1513
writer = csv.DictWriter(sys.stdout, reader.fieldnames)
1614

1715
writer.writeheader()
1816
for row in reader:
1917
# Filter out flights to or from undeclared airports.
20-
if row["_from"] in ids and row["_to"] in ids:
18+
if row['_from'] in ids and row['_to'] in ids:
2119
# Prepend the presumed node table name to the from/to columns.
22-
row["_from"] = f'airports/{row["_from"]}'
23-
row["_to"] = f'airports/{row["_to"]}'
20+
row['_from'] = f'''airports/{row['_from']}'''
21+
row['_to'] = f'''airports/{row['_to']}'''
2422

2523
writer.writerow(row)
2624

2725
return 0
2826

2927

30-
if __name__ == "__main__":
28+
if __name__ == '__main__':
3129
sys.exit(main())

0 commit comments

Comments
 (0)