HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Checking which pypi packages are Py3k-only

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
pypiarecheckingonlywhichpackagespy3k

Problem

I looked at the pypi classifiers and some guide to help me write the following script, and here's me wondering if it can be improved:

import xmlrpc.client

# pypi language version classifiers
PY3 =  ["Programming Language :: Python :: 3"]
PY2 =  ["Programming Language :: Python :: 2"]
PY27 = ["Programming Language :: Python :: 2.7"]
PY26 = ["Programming Language :: Python :: 2.6"]
PY25 = ["Programming Language :: Python :: 2.5"]
PY24 = ["Programming Language :: Python :: 2.4"]
PY23 = ["Programming Language :: Python :: 2.3"]

def main():
    client = xmlrpc.client.ServerProxy('http://pypi.python.org/pypi')

    # get module metadata
    py3names = [name[0] for name in client.browse(PY3)]
    py2names = [name[0] for name in client.browse(PY2)]
    py27names = [name[0] for name in client.browse(PY27)]
    py26names = [name[0] for name in client.browse(PY26)]
    py25names = [name[0] for name in client.browse(PY25)]
    py24names = [name[0] for name in client.browse(PY24)]
    py23names = [name[0] for name in client.browse(PY23)]

    cnt = 0
    for py3name in py3names:
        if py3name not in py27names \
           and py3name not in py26names \
           and py3name not in py25names \
           and py3name not in py24names \
           and py3name not in py23names \
           and py3name not in py2names:
            cnt += 1

    print("Python3-only packages: {}".format(cnt))

main()


Sidenote:

$ time python3 py3k-only.py 
Python3-only packages: 259

real    0m17.312s
user    0m0.324s
sys 0m0.012s


In addition, can you spot any functionality bugs in there? Will it give accurate results, assuming that pypi has correct info?

Solution

import xmlrpc.client

# pypi language version classifiers
PY3 =  ["Programming Language :: Python :: 3"]


Why do you have a single string in a list?

PY2 =  ["Programming Language :: Python :: 2"]
PY27 = ["Programming Language :: Python :: 2.7"]
PY26 = ["Programming Language :: Python :: 2.6"]
PY25 = ["Programming Language :: Python :: 2.5"]
PY24 = ["Programming Language :: Python :: 2.4"]
PY23 = ["Programming Language :: Python :: 2.3"]


You should put all these strings in one list.

def main():
    client = xmlrpc.client.ServerProxy('http://pypi.python.org/pypi')

    # get module metadata
    py3names = [name[0] for name in client.browse(PY3)]
    py2names = [name[0] for name in client.browse(PY2)]
    py27names = [name[0] for name in client.browse(PY27)]
    py26names = [name[0] for name in client.browse(PY26)]
    py25names = [name[0] for name in client.browse(PY25)]
    py24names = [name[0] for name in client.browse(PY24)]
    py23names = [name[0] for name in client.browse(PY23)]


If you put the python 2.x versions in a one list, you should be able to fetch all this data
into one big list rather then all of these lists.

cnt = 0


Don't uselessly abbreviate, spell out counter

for py3name in py3names:
        if py3name not in py27names \
           and py3name not in py26names \
           and py3name not in py25names \
           and py3name not in py24names \
           and py3name not in py23names \
           and py3name not in py2names:
            cnt += 1


I'd do something like: python3_only = [name for name in py3name if py3name not in py2names]. Then I'd get the number of packages as a len of that.

print("Python3-only packages: {}".format(cnt))

main()


Usually practice is to the call to main inside if __name__ == '__main__': so that it only gets run if this script is the main script.

Code Snippets

import xmlrpc.client

# pypi language version classifiers
PY3 =  ["Programming Language :: Python :: 3"]
PY2 =  ["Programming Language :: Python :: 2"]
PY27 = ["Programming Language :: Python :: 2.7"]
PY26 = ["Programming Language :: Python :: 2.6"]
PY25 = ["Programming Language :: Python :: 2.5"]
PY24 = ["Programming Language :: Python :: 2.4"]
PY23 = ["Programming Language :: Python :: 2.3"]
def main():
    client = xmlrpc.client.ServerProxy('http://pypi.python.org/pypi')

    # get module metadata
    py3names = [name[0] for name in client.browse(PY3)]
    py2names = [name[0] for name in client.browse(PY2)]
    py27names = [name[0] for name in client.browse(PY27)]
    py26names = [name[0] for name in client.browse(PY26)]
    py25names = [name[0] for name in client.browse(PY25)]
    py24names = [name[0] for name in client.browse(PY24)]
    py23names = [name[0] for name in client.browse(PY23)]
for py3name in py3names:
        if py3name not in py27names \
           and py3name not in py26names \
           and py3name not in py25names \
           and py3name not in py24names \
           and py3name not in py23names \
           and py3name not in py2names:
            cnt += 1
print("Python3-only packages: {}".format(cnt))

main()

Context

StackExchange Code Review Q#8259, answer score: 3

Revisions (0)

No revisions yet.