37. pzffile
— A multifunctional file format for saving pyFormex geometry or projects.¶
This module defines the PzfFile class which is the new implementation of the PZF file format.
37.1. Classes defined in module pzffile¶
-
class
pzffile.
Config
[source]¶ A very simple config parser.
This class contains two static functions: ‘dumps’ to dump a dict to a string, and ‘loads’ to load back the dict from the string.
The string format is such that it can easily be read and edited. Each of the items in the dict is stored on a line of the form ‘key = repr(value)’. On loading back, each line is split on the first appearance of a ‘=’. The first part is stripped and used as key, the second part is eval’ed and used as value.
-
class
pzffile.
PzfFile
(filename)[source]¶ An archive file in PZF format.
PZF stands for ‘pyFormex zip format’. A complete description of the format and API is given in pyFormex file formats.
This is the implementation of version 2.0 of the PZF file format. The format has minor changes from the (unpublished) 1.0 version and is able to read (but not write) the older format.
A PZF file is actually a ZIP archive, written with the standard Python ZipFile module. Thus, its contents are individual files. In the current format 2.0, the PzfFile writer creates only three types of files, marked by their suffix:
.npy: a file containing a single NumPy array in Numpy’s .npy format;
.txt: a file containing text in a utf-8 encoding;
no suffix: an empty file: the info is in the file name.
The filename carry important information though. Usually they follow the scheme name__class__attr, where name is the object name, class the object’s class name (to be used on loading) and attr is the name of the attribute that has its data in the file. Files without suffix have their information in the filename.
Saving objects to a PZF file is as simple as:
PzfFile(filename).save(**kargs)
Each of the keyword arguments provided specifies an object to be saved with the keyword as its name.
To load the objects from a PZF file, do:
dic = PzfFile(filename).load()
This returns a dict containing the pyFormex objects with their names as keys.
Limitations: currently, only objects of the following classes can be stored: str, dict, numpy.ndarray, Coords, Formex, Mesh, TriSurface, PolyLine, BezierSpline, CoordSys, Camera, Canvas settings. Using the API (see pyFormex file formats) this can however easily be extended to any other class of objects.
- Parameters
filename (path_like) – Name of the file from which to load the objects. It is normally a file with extension ‘.pzf’.
Notes
See also the example SaveLoad.
37.2. Functions defined in module pzffile¶
-
pzffile.
dict2str
(d, fmt)[source]¶ Nicely format a dict so it can be imported again
Examples
>>> d = {'a': 0, 'b': (0,1), 'c': 'string'} >>> print(dict2str(d, 'c')) a = 0 b = (0, 1) c = 'string' >>> print(dict2str(d, 'j')) {"a": 0, "b": [0, 1], "c": "string"} >>> print(dict2str(d, 'r')) {'a': 0, 'b': (0, 1), 'c': 'string'}
-
pzffile.
register
(clas, name=None)[source]¶ Register a class in the pzf i/o module
A registered class can be exported to a PZF file.
- Returns
class – The provided class is returned, so that this method can be used as a decorator. Normally though, one uses the
utils.pzf_register()
as decorator.
-
pzffile.
str2dict
(s, fmt)[source]¶ Read a dict from a string representation
Examples
>>> s = "{'a': 0, 'b': (0,1), 'c': 'string'}"
-
pzffile.
zipfile_write_array
(zipf, fname, val, datetime=None, compress=True)[source]¶ Write a numpy array to an open ZipFile
- Parameters
zipf (ZipFile) – A ZipFIle that is open for writing.
fname (str) – The filename as it will be set in the zip archive.
val (ndarray) – The data to be written into the file. It should be a numpy.ndarray or data that can be converted to one.
datetime (tuple, optional) – The date and time mark to be set on the file. It should be a tuple of 6 ints: (year, month, day, hour, min, sec). If not provided, the current date/time is used.
compress (bool, optional) – If True, the data will be compressed with the zipfile.ZIP_DEFLATED method.