DeaD_EyE icon

get latest python version string maybe....

DeaD_EyE | PRO | 07/26/22 08:22:31 PM UTC | 0 ⭐ | 1730 👁️ | Never ⏰ | []
Python |

1.05 KB

|

None

|

0 👍

/

0 👎

# This example is not good for production use
 
from urllib.request import urlopen, URLError
from html.parser import HTMLParser
from functools import cache
 
 
class Parser(HTMLParser):
    def __init__(self):
        super().__init__()
        self.versions = []
        self.feed(urlopen("https://www.python.org/ftp/python").read().decode())
        self.versions.sort(key=self._by_version)
        
    def handle_starttag(self, tag, content):
        content = dict(content)        
        match tag:
            case "a" if "href" in content and content["href"][0].isdigit():
                self.versions.append(content["href"].rstrip("/"))
 
    @staticmethod
    def _by_version(value):
        return list(map(int, value.split(".")))
 
 
@cache
def get_latest_python_version():
    try:
        parser = Parser()
    except URLError:
        raise RuntimeError("Could not request python.org")
    # removing latest version, which should be always
    # a development version
    return parser.versions[-2]
 
 
 
get_latest_python_version()

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎