-
Notifications
You must be signed in to change notification settings - Fork 1
/
prep_n_load.py
79 lines (67 loc) · 2.1 KB
/
prep_n_load.py
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
"""Load libraries and data, prepare data, expose data, functions, and libraries."""
__all__ = [
# Dataframes:
"parcels",
"island_sizes",
# JSONs:
"parcels_json",
# Functions:
"show_leaderboard",
"counter_to_string",
# Some libraries to be used in the notebooks:
"sns",
"plt",
"pd",
]
from typing import List
from collections import Counter
from datetime import datetime
import requests
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from IPython.core.display import display, HTML
# Generic setup:
sns.set_style("whitegrid")
PARCELS_URL = "https://www.cryptovoxels.com/api/parcels.json"
response = requests.request("GET", PARCELS_URL)
parcels_json = response.json()["parcels"]
parcels = pd.DataFrame.from_records(parcels_json)
parcels["voxvolume"] = parcels.area * parcels.height * 8
print(datetime.utcnow().strftime("%c"), " UTC")
print("{} parcels loaded.".format(parcels.shape[0]))
island_sizes = (
parcels.groupby("island")
.agg({"id": "count", "area": "sum", "voxvolume": "sum"})
.rename(columns={"id": "parcels"})
)
island_sizes["voxperparcel"] = island_sizes.voxvolume / island_sizes.parcels
# Functions:
def show_leaderboard(
address_counts: List[Counter], name: str, howmany: int = 10
) -> None:
"""Show list of Counters as a leaderboard. Counter keys are expected to be ETH
addresses and get linked to Cryptovoxels.
"""
i = 1
for (address, count) in sorted(
address_counts.items(), key=lambda x: x[1], reverse=True
)[:howmany]:
display(
HTML(
f"{i:2d}. "
f'<a href="https://www.cryptovoxels.com/avatar/{address}">{address}</a>'
f" with {count:3d} {name}"
)
)
i += 1
def counter_to_string(counter: Counter, howmany: int = 10) -> str:
"""Turn counter object into a string showing the top howmany items."""
return "\n".join(
[
f"{cnt:5d}: {key}"
for key, cnt in sorted(counter.items(), key=lambda x: x[1], reverse=True)[
:howmany
]
]
)