Skip to content

Instantly share code, notes, and snippets.

@pwwang
Last active October 15, 2023 21:12
Show Gist options
  • Select an option

  • Save pwwang/879966128b0408c2459eb0a0b413fa69 to your computer and use it in GitHub Desktop.

Select an option

Save pwwang/879966128b0408c2459eb0a0b413fa69 to your computer and use it in GitHub Desktop.

Revisions

  1. pwwang revised this gist Oct 15, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion whichpy.fish
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # ~/.config/fish/functions/whichpy.fish
    function whichpy --description 'Print python package information'
    set -l whichpy_script_url "https://gist.github.com/pwwang/879966128b0408c2459eb0a0b413fa69/raw/a28dbaeb9c3541fb669ac2ed1a96f691238c2fa5/whichpy.py"
    set -l whichpy_script_url "https://gist.github.com/pwwang/879966128b0408c2459eb0a0b413fa69/raw/01fdd16e690a015aaa1015edf8874a6986bd33af/whichpy.py"
    set -l whichpy_script /tmp/whichpy.py
    if not test -f "$whichpy_script"
    echo "Downloading: $whichpy_script_url"
  2. pwwang revised this gist Oct 15, 2023. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion whichpy.py
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,12 @@

    if mdnames:

    libs = {mdname: __import__(mdname) for mdname in mdnames}
    libs = {}
    for mdname in mdnames:
    try:
    libs[mdname] = __import__(mdname)
    except Exception:
    pass

    print()
    print('Python:')
  3. pwwang revised this gist Aug 30, 2023. No changes.
  4. pwwang revised this gist Aug 30, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion whichpy.fish
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # ~/.config/fish/functions/whichpy.fish
    function whichpy --description 'Print python package information'
    set -l whichpy_script_url "https://gist.github.com/pwwang/879966128b0408c2459eb0a0b413fa69/raw/94d69badf8b14801e9ca0fc89fb1d349432575a6/whichpy.py"
    set -l whichpy_script_url "https://gist.github.com/pwwang/879966128b0408c2459eb0a0b413fa69/raw/a28dbaeb9c3541fb669ac2ed1a96f691238c2fa5/whichpy.py"
    set -l whichpy_script /tmp/whichpy.py
    if not test -f "$whichpy_script"
    echo "Downloading: $whichpy_script_url"
  5. pwwang revised this gist Aug 30, 2023. 1 changed file with 7 additions and 3 deletions.
    10 changes: 7 additions & 3 deletions whichpy.py
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,11 @@
    import sys, json
    import sys
    import json
    from urllib.request import urlopen
    from datetime import datetime

    try:
    im = __import__('importlib.metadata', fromlist=['metadata'])
    from importlib.metadata import packages_distributions
    from importlib.metadata import distribution, packages_distributions # noqa
    except ImportError:
    im = __import__('importlib_metadata')

    @@ -19,6 +20,9 @@
    if pkg in package:
    mdnames.append(module)

    if distribution(pkg) and pkg not in mdnames:
    mdnames.append(pkg)

    if mdnames:

    libs = {mdname: __import__(mdname) for mdname in mdnames}
    @@ -68,7 +72,7 @@ def get_key(item):
    return datetime.fromisoformat(
    item[1][0]['upload_time_iso_8601'].split('.')[0]
    )
    except:
    except Exception:
    return datetime.fromisoformat('1970-01-01T00:00:00')

    data = json.loads(req.read().decode())
  6. pwwang revised this gist Jul 13, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion whichpy.fish
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # ~/.config/fish/functions/whichpy.fish
    function whichpy --description 'Print python package information'
    set -l whichpy_script_url "https://gist.github.com/pwwang/879966128b0408c2459eb0a0b413fa69/raw/4e3d29ffc580f62182f45f50d4feee3b58c6b633/whichpy.py"
    set -l whichpy_script_url "https://gist.github.com/pwwang/879966128b0408c2459eb0a0b413fa69/raw/94d69badf8b14801e9ca0fc89fb1d349432575a6/whichpy.py"
    set -l whichpy_script /tmp/whichpy.py
    if not test -f "$whichpy_script"
    echo "Downloading: $whichpy_script_url"
  7. pwwang revised this gist Jul 13, 2023. 1 changed file with 25 additions and 27 deletions.
    52 changes: 25 additions & 27 deletions whichpy.py
    Original file line number Diff line number Diff line change
    @@ -19,37 +19,35 @@
    if pkg in package:
    mdnames.append(module)

    if not mdnames:
    print('Package %s not found' % pkg)
    sys.exit(1)
    if mdnames:

    libs = {mdname: __import__(mdname) for mdname in mdnames}
    libs = {mdname: __import__(mdname) for mdname in mdnames}

    print()
    print('Python:')
    print('------')
    print(sys.executable)
    print(sys.version)
    print()
    print('Entry files:')
    print('--------------')
    for name, lib in libs.items():
    print(name, ":", lib.__file__)
    print()
    print('Version (importlib.metadata):')
    print('----------------------------')
    print(im.version(pkg))
    print()
    print('Version (__version__/version):')
    print('-----------------------------')
    for name, lib in libs.items():
    try:
    print(name, ":", lib.__version__)
    except AttributeError:
    print()
    print('Python:')
    print('------')
    print(sys.executable)
    print(sys.version)
    print()
    print('Entry files:')
    print('--------------')
    for name, lib in libs.items():
    print(name, ":", lib.__file__)
    print()
    print('Version (importlib.metadata):')
    print('----------------------------')
    print(im.version(pkg))
    print()
    print('Version (__version__/version):')
    print('-----------------------------')
    for name, lib in libs.items():
    try:
    print(name, ":", lib.version)
    print(name, ":", lib.__version__)
    except AttributeError:
    print(name, ":", 'N/A')
    try:
    print(name, ":", lib.version)
    except AttributeError:
    print(name, ":", 'N/A')

    print()
    print('Latest (PYPI):')
  8. pwwang revised this gist Apr 14, 2023. 1 changed file with 20 additions and 0 deletions.
    20 changes: 20 additions & 0 deletions whichpy.fish
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    # ~/.config/fish/functions/whichpy.fish
    function whichpy --description 'Print python package information'
    set -l whichpy_script_url "https://gist.github.com/pwwang/879966128b0408c2459eb0a0b413fa69/raw/4e3d29ffc580f62182f45f50d4feee3b58c6b633/whichpy.py"
    set -l whichpy_script /tmp/whichpy.py
    if not test -f "$whichpy_script"
    echo "Downloading: $whichpy_script_url"
    command curl -sS "$whichpy_script_url" -o "$whichpy_script"
    end
    if test (count $argv) -eq 0
    echo "Usage: whichpy <package> [python]"
    return 1
    end
    if test (count $argv) -gt 1
    set python $argv[2]
    else
    set python "python"
    end
    echo "Running: $python $whichpy_script $argv[1]"
    command $python $whichpy_script $argv[1]
    end
  9. pwwang created this gist Apr 14, 2023.
    86 changes: 86 additions & 0 deletions whichpy.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,86 @@
    import sys, json
    from urllib.request import urlopen
    from datetime import datetime

    try:
    im = __import__('importlib.metadata', fromlist=['metadata'])
    from importlib.metadata import packages_distributions
    except ImportError:
    im = __import__('importlib_metadata')

    if len(sys.argv) < 2:
    print('Usage: %s <package>' % sys.argv[0])
    sys.exit(1)

    pkg = sys.argv[1]
    mdnames = []

    for module, package in im.packages_distributions().items():
    if pkg in package:
    mdnames.append(module)

    if not mdnames:
    print('Package %s not found' % pkg)
    sys.exit(1)

    libs = {mdname: __import__(mdname) for mdname in mdnames}

    print()
    print('Python:')
    print('------')
    print(sys.executable)
    print(sys.version)
    print()
    print('Entry files:')
    print('--------------')
    for name, lib in libs.items():
    print(name, ":", lib.__file__)
    print()
    print('Version (importlib.metadata):')
    print('----------------------------')
    print(im.version(pkg))
    print()
    print('Version (__version__/version):')
    print('-----------------------------')
    for name, lib in libs.items():
    try:
    print(name, ":", lib.__version__)
    except AttributeError:
    try:
    print(name, ":", lib.version)
    except AttributeError:
    print(name, ":", 'N/A')

    print()
    print('Latest (PYPI):')
    print('-------------')

    url = f'https://pypi.python.org/pypi/{pkg}/json'
    with urlopen(url) as req:
    try:
    status = req.status
    except AttributeError:
    status = req.code

    if status != 200:
    print('Failed to fetch.')
    else:
    def get_key(item):
    try:
    return datetime.fromisoformat(
    item[1][0]['upload_time_iso_8601'].split('.')[0]
    )
    except:
    return datetime.fromisoformat('1970-01-01T00:00:00')

    data = json.loads(req.read().decode())
    releases = data.get('releases', {})
    releases = sorted(
    releases.items(),
    reverse=True,
    key=get_key,
    )
    if not releases:
    print('N/A')
    else:
    print(releases[0][0])