- Python Data Persistence - Discussion
- Python Data Persistence - Useful Resources
- Python Data Persistence - Quick Guide
- Data Persistence - Openpyxl Module
- Data Persistence - ZODB
- Python Data Persistence - Cassandra Driver
- Python Data Persistence - PyMongo module
- Python Data Persistence - SQLAlchemy
- Python Data Persistence - Sqlite3 Module
- Python Data Persistence - Plistlib Module
- Python Data Persistence - XML Parsers
- Python Data Persistence - JSON Module
- Python Data Persistence - CSV Module
- Python Data Persistence - dbm Package
- Python Data Persistence - Shelve Module
- Python Data Persistence - Marshal Module
- Python Data Persistence - Pickle Module
- Python Data Persistence - Object Serialization
- File Handling with os Module
- Python Data Persistence - File API
- Python Data Persistence - Introduction
- Python Data Persistence - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Python Data Persistence - Marshal Module
Object seriapzation features of marshal module in Python’s standard pbrary are similar to pickle module. However, this module is not used for general purpose data. On the other hand, it is used by Python itself for Python’s internal object seriapzation to support read/write operations on compiled versions of Python modules (.pyc files).
The data format used by marshal module is not compatible across Python versions. Therefore, a compiled Python script (.pyc file) of one version most probably won’t execute on another.
Just as pickle module, marshal module also defined load() and dump() functions for reading and writing marshalled objects from / to file.
dump()
This function writes byte representation of supported Python object to a file. The file itself be a binary file with write permission
load()
This function reads the byte data from a binary file and converts it to Python object.
Following example demonstrates use of dump() and load() functions to handle code objects of Python, which are used to store precompiled Python modules.
The code uses built-in compile() function to build a code object out of a source string which embeds Python instructions.
compile(source, file, mode)
The file parameter should be the file from which the code was read. If it wasn’t read from a file pass any arbitrary string.
The mode parameter is ‘exec’ if the source contains sequence of statements, ‘eval’ if there is a single expression or ‘single’ if it contains a single interactive statement.
The compile code object is then stored in a .pyc file using dump() function.
import marshal script = """ a=10 b=20 print ( addition= ,a+b) """ code = compile(script, "script", "exec") f=open("a.pyc","wb") marshal.dump(code, f) f.close()
To deseriapze, the object from .pyc file use load() function. Since, it returns a code object, it can be run using exec(), another built-in function.
import marshal f=open("a.pyc","rb") data=marshal.load(f) exec (data)Advertisements