Python: Best practice for including version numbers in an application?

I have a PyQt application that reads and writes data files. A "version number" is written to each file. This is a prime number similar to: 1.2 or something (major and minor versions).

I do this to change the format of these data files in future versions, and then still parse them correctly, just by checking that the version is inside the file.

My question is what is the best way to store this number stored inside the application itself. That is, I just adjust the version number of the application in the class that is responsible for reading and writing files? Or should I have some kind of object / variable stored at the top level of the application, and somehow access it from the class responsible for reading and writing these files. If the latter, how to save it and how do I access it?

Thanks.

+4
source share
3 answers

Many open source projects (such as numpy, fabric) have a module called "version" that contains version information in top-level variables. See fabric version file .

+1
source

First, enter the data format separately from the application, if not already done. Secondly, there are separate classes for newer versions of the format. If the format is fully backward compatible, you can opt out of classes for older versions. As for the file structure, you may have something like:

  • Df
    • __ __ INIT. RU
    • dfbase.py
    • v1_1
      • __ __ INIT. RU
      • format.py
    • v2_0
      • __ __ INIT. RU
      • format.py

where "df" stands for the name of the data format. Package initialization files import the appropriate packages and define a structure or function to make them available. The top level __init__.py should also define a factory function to create data format objects, so you don't need to.

.

DF / __ __ INIT ru:

from df.dfbase import DFBase from v1_1 import DF as DF1 from v2_0 import DF as DF2 versions = { '1.0': DF1, # let say minor versions are fully backwards compatible '1.1': DF1, '2.0': DF2 } def create(file): """Factory function. Loads data from 'file', returns a DF of the appropriate version. """ ... 

DF / v1_1 / __ __ INIT ru :.

 __version__ = '1.1' # previous versions that this one is backwards compatible with previous = ['1.0'] from format import DF 

DF / v1_1 / format.py:

 from df import DFBase class DF(DFBase): ... 

DF / v2_0 / __ __ INIT ru :.

 __version__ = '2.0' from format import DF 

With a bit more encoding in DF / __ init__.py, you can automate the import and registration of format versions.

 import glob, sys from ndf.ndfbase import NDFBase formats={} for ver in glob.iglob('v*_*'): pkg = '{0}.{1}'.format(__package__, ver) __import__(pkg) formats[sys.modules[pkg].__version__] = sys.modules[pkg] if hasattr(sys.modules[pkg], 'previous'): for prev in sys.modules[pkg].previous: formats[prev] = sys.modules[pkg] def create(file): """Factory function. Loads data from 'file', returns a DF of the appropriate version. """ ... 
+1
source

Personally, I would not do it manually. I would save my code in Subversion and let it save version numbers for me.

0
source

Source: https://habr.com/ru/post/1333431/


All Articles