nicolargo / glances

Glances an Eye on your system. A top/htop alternative for GNU/Linux, BSD, Mac OS and Windows operating systems.
http://nicolargo.github.io/glances/
Other
26.77k stars 1.53k forks source link

better way to check python version #329

Closed simonlopez closed 10 years ago

simonlopez commented 10 years ago

for instance https://github.com/nicolargo/glances/blob/develop/glances/core/glances_server.py#L34 uses exceptions in the hope of discovering python version. a better way to do this is:

import sys
if sys.version_info.major == 2:
    from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler
    from SimpleXMLRPCServer import SimpleXMLRPCServer
    from xmlrpclib import ServerProxy, ProtocolError
else:
    from xmlrpc.server import SimpleXMLRPCRequestHandler
    from xmlrpc.server import SimpleXMLRPCServer
    from xmlrpc.client import ServerProxy, ProtocolError
asergi commented 10 years ago

When writing code to support both Python 2 and 3, the common idiom is to import from the Python 3 locations and if that fails you import from the Python 2 locations (or viceversa). The python version check is redundant.

Some links as reference points: http://python3porting.com/stdlib.html http://python3porting.com/noconv.html (Import errors section)