chevron_left chevron_right
Login Register invert_colors photo_library


Stay updated and chat with others! - Join the Discord!
Thread Rating:
  • 0 Vote(s) - 0 Average


The SYS module - Version_info & hexversion filter_list
Author
Message
The SYS module - Version_info & hexversion #1
If you've read some of my Python programming threads, you've most likely seen me use something like this;
Code:
if sys.version_info[0] > 2:
    import urllib.request as urlreq
else:
    import urllib2 as urlreq

This is a really simple and easy-to-use function in Python that can help you in programming.

Scenario; You have a module that has been imported but changes between versions. How can you check the module and import it differently if you need to do so?

Use version info.

The only modules required for this to work is sys. That's it.

How does this work? Basically you can see all the information affiliated with your Python version.

Code:
print(sys.version_info)

If you print the version info, you will see something like this;

Spoiler:
sys.version_info(major=3, minor=3, micro=3, releaselevel='final', serial=0)

(That was my version info, yours may vary but will always have major, minor, micro, releaselevel, and serial information provided)

Let's break this down.

We see the Python version is broken up into three parts.

For example; my version is 3.3.3.

3 | 3 | 3 = major | minor | micro

So when your version is, for example, 2.7.6, your major is 2, your minor is 7, and your micro is 6.

2(Major) | 7(Minor) | 6(Micro)

So how does any of this help us?

If we setup our program, whatever it may be, to have sys.version_info defined as something, such as "ver", we can use this to detect the user's Python version and therefore import modules version-based.

I use this function with urllib.request/urllib2 urlopen functions.

Code:
if sys.version_info[0] > 2:
    import urllib.request as urlreq
else:
    import urllib2 as urlreq

In that snipet of code, we grab the major version value. If it is greater than 2, we import urllib.request as urlreq. If not, we import urllib2 as urlreq instead.

sys.version_info[0] = Major
sys.version_info[1] = Minor
sys.version_info[2] = Micro

Code:
ver = sys.version_info

print('Major: ' + ver[0])
print('Minor: ' + ver[1])
print('Micro: ' + ver[2])
print('Python '+ ver[0] +'.'+ ver[1] +'.'+ ver[2])

Next, the release level just tells us what release type we are running.

For example, if we run a final version of Python, it will show as follows:
Spoiler:
releaselevel='final'

As of today, March 3, 2014, the only product versions of the Python language is version 2.7.6 and 3.3.5.

Here is a list of releases;
Spoiler:
Python 3.3.5 - Released on March 9, 2014
Python 3.2.5 - Released on May 15, 2013
Python 3.1.5 - Released on April 10, 2012
Python 3.0.1 - Released on February 13, 2009
Python 2.7.6 - Released on November 10, 2013
Python 2.6.9 - Released on October 26, 2013
Python 2.5.6 - Released on May 26, 2011
Python 2.4.6 - Released on December 19, 2008
Python 2.3.7 - Released on March 11, 2008
Python 2.2.3 - Released on May 30, 2003
Python 2.1.3 - Released on April 8, 2002
Python 2.0.1 - Released in June of 2001
Python 1.6.1 - Released in September of 2000
Python 1.5.2 - Released in April of 1999

Any other versions released before that are in binaries and the source itself.

This is binaries 1.1 - 1.5, and, of course, the source releases.

The only testing version of Python is 3.4.0, released on February 23, 2014.

Next we have our serials. These basically tell us what release we are on.

Any serial that is equal to 0 is a final release of that version.

Now we move onto sys.hexversion. This is the counter-part to sys.version_info, which is more human-friendly because of how it displays.

This is something along the lines of what it will look like;
Spoiler:
50529264

Here are the bits in the big endian order;
Spoiler:
1-8 = Major version (Ex; 2.1.0a3)
9-16 = Minor version (Ex; 2.1.0a3)
17-24 = Micro version (Ex; 2.1.0a3)

Next we have our release level in the bits in the big endian order, I didn't put this into the table above so I could make it have its' own table.

Bits 25 - 28 is the release level. (Ex; 2.1.0a3)
Spoiler:
0xA = Alpha
0xB = Beta
0xC = Candidate
0xF = Final

The last bits in the big endian order for reading the hexversion is bits 29-32. This is the release serial, and, of course 0 being a final release. This is the last character in the version. (Ex; 2.1.0a3)

So with all of this in mind, we can conclude how to convert HEX's over to actual versions that we can read.

By the way, for those who want to know, version 2.1.0a3 is 0x020100a3 in hex format.

Hope this was of use. ~Duubz

Reply






Users browsing this thread: 1 Guest(s)