-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathassumption_coverage.py
More file actions
57 lines (48 loc) · 1.77 KB
/
assumption_coverage.py
File metadata and controls
57 lines (48 loc) · 1.77 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
import random
import requests
from helpers.crate_data import CrateVersion
from helpers.logger import get_crate_names
from solver import memoized_crate_analysis
from collections import Counter
from helpers.assumption import Assumption
# Function to get the latest version of a crate
def get_latest_version(crate_name):
url = f'https://crates.io/api/v1/crates/{crate_name}'
response = requests.get(url)
crate_info = response.json()['crate']
return crate_info['newest_version']
def get_random_crates(count: int):
# Get all crate names from a random page
all_crate_names = []
per_page = 20
visited = []
while len(all_crate_names) < count:
while True:
random_page = random.randint(200, 3000)
if random_page in visited:
continue
visited.append(random_page)
break
crates = get_crate_names(random_page, per_page)
if not crates:
continue
all_crate_names.extend([crate['id'] for crate in crates])
# Randomly select 1000 crate names
random_crates = random.sample(all_crate_names, count)
# Get the latest version of each selected crate
return [CrateVersion(crate_name, get_latest_version(crate_name)) for crate_name in random_crates]
def get_assumptions_made(crate: CrateVersion) -> list[Assumption]:
"""
Returns a list of assumptions made for a given crate.
"""
summary = memoized_crate_analysis(crate)
return summary.assumptions_made
def main():
random_crates = get_random_crates(30)
for crate in random_crates:
print(f"{crate} assumptions:")
assumptions_made = get_assumptions_made(crate)
for assumption in assumptions_made:
print(assumption.name)
if __name__ == "__main__":
main()