Skip to content

Commit e107581

Browse files
committed
Added range mode for identifying Python versions
1 parent 57fdac5 commit e107581

File tree

1 file changed

+36
-9
lines changed

1 file changed

+36
-9
lines changed

versions.py

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python
22

33
import argparse
4+
import json
45
import re
56
from urllib import request
67

@@ -15,18 +16,36 @@
1516
}
1617

1718

18-
def get_min_python_version():
19+
def get_all_python_versions():
1920
"""
20-
Find the minimum version of Python supported (i.e., not end-of-life)
21+
Retrieve all supported Python versions (i.e., not end-of-life)
2122
"""
22-
min_python = (
23+
python_versions = (
2324
pd.read_html(
2425
"https://devguide.python.org/versions/",
2526
storage_options=HEADERS,
2627
)[0]
27-
.iloc[-1]
28-
.Branch
28+
.query('Branch != "main"')
29+
.Branch.to_list()
2930
)
31+
return python_versions
32+
33+
34+
def get_all_python_versions_in_range(start, stop):
35+
"""
36+
Find all Python minor versions within some start/stop range (inclusive)
37+
"""
38+
python_versions = get_all_python_versions()[::-1] # Reverse order
39+
start_idx = python_versions.index(start)
40+
stop_idx = python_versions.index(stop)
41+
print(json.dumps(python_versions[start_idx : stop_idx + 1]))
42+
43+
44+
def get_min_python_version():
45+
"""
46+
Find the minimum version of Python supported (i.e., not end-of-life)
47+
"""
48+
min_python = get_all_python_versions()[-1]
3049
return min_python
3150

3251

@@ -425,24 +444,32 @@ def get_all_min_versions(MIN_PYTHON):
425444
if __name__ == "__main__":
426445
parser = argparse.ArgumentParser()
427446
parser.add_argument(
428-
"-mode", type=str, default="min", help='Options: ["min", "max"]'
447+
"-mode", type=str, default="min", help='Options: ["min", "max", "range"]'
429448
)
430-
parser.add_argument("python_version", nargs="?", default=None)
449+
parser.add_argument("python_version", nargs="*", default=None)
431450
args = parser.parse_args()
432451
# Example
433452
# ./versions.py
434453
# ./versions.py 3.11
435454
# ./versions.py -mode max
436-
437-
print(f"mode: {args.mode}")
455+
# /versions.py -mode range 3.10 3.14
438456

439457
if args.mode == "min":
458+
if len(args.python_version) == 0:
459+
args.python_version = None
460+
elif len(args.python_version) == 1:
461+
args.python_version = args.python_version[0]
462+
440463
if args.python_version is not None:
441464
MIN_PYTHON = str(args.python_version)
442465
else:
443466
MIN_PYTHON = get_min_python_version()
444467
get_all_min_versions(MIN_PYTHON)
445468
elif args.mode == "max":
446469
get_all_max_versions()
470+
elif args.mode == "range":
471+
start = args.python_version[0]
472+
stop = args.python_version[1]
473+
get_all_python_versions_in_range(start, stop)
447474
else:
448475
raise ValueError(f'Unrecognized mode: "{args.mode}"')

0 commit comments

Comments
 (0)