Skip to main content

Classes for creating and wrapping file-like objects

Project description

The filelike module takes care of the groundwork for implementing and handling file-like objects that implement a rich file-like interface, including reading, writing, and iteration. It also provides a number of useful classes built on top of this functionality.

The main class is FileLikeBase, which implements the entire file-like interface (currently minus seek() and tell()) on top of primitive _read() and _write() methods. Subclasses may implement either or both of these methods to obtain all the higher-level file behaviors.

Two methods are provided for when code expects to deal with file-like objects:

  • is_filelike(obj): checks that an object is file-like

  • to_filelike(obj): wraps a variety of objects in a file-like interface

The “wrappers” subpackage contains a collection of useful classes built on top of this framework. These include:

  • TransFile: pass file contents through an arbitrary translation

    function (e.g. compression, encryption, …)

  • FixedBlockSizeFile: ensure all read/write requests are aligned with

    a given blocksize

  • DecryptFile: on-the-fly reading and writing to an encrypted file

    (using PEP272 cipher API)

As an example of the type of thing this module is designed to achieve, here’s an example of using the DecryptFile class to transparently access an encrypted file:

# Create the decryption key from Crypto.Cipher import DES cipher = DES.new(‘abcdefgh’,DES.MODE_ECB) # Open the encrypted file from filelike.wrappers import DecryptFile f = DecryptFile(file(“some_encrypted_file.bin”,”r”),cipher)

The object in <f> now behaves as a file-like object, transparently decrypting the file on-the-fly as it is read.

The “pipeline” subpackage contains facilities for composing these wrappers in the form of a unix pipeline. In this example, <f> will read the first five lines of an encrypted file:

from filelike.pipeline import DecryptFile, Head f = file(“some_encrypted_file.bin”) > DecryptFile(cipher) | Head(lines=5)

Finally, the function filelike.open() mirrors the standard file opening function but tries to be clever about accessing the file - URLs are automatically fetched using urllib2, compressed files are decompressed on-the-fly, and so-forth.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

filelike-0.2.2.tar.gz (30.2 kB view details)

Uploaded Source

Built Distribution

filelike-0.2.2-py2.4.egg (42.7 kB view details)

Uploaded Source

File details

Details for the file filelike-0.2.2.tar.gz.

File metadata

  • Download URL: filelike-0.2.2.tar.gz
  • Upload date:
  • Size: 30.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for filelike-0.2.2.tar.gz
Algorithm Hash digest
SHA256 bc8d196e8ba35a0f03b53a1b6e7eecff2f32a484b765dc8e506e6cfe56ca9fa0
MD5 0b7e9fe0629679863bc2e5e78dd1c054
BLAKE2b-256 9d3717c5030e8ba49ccc17438b90352d292e4e789be1ad88561a450d3fc5a912

See more details on using hashes here.

File details

Details for the file filelike-0.2.2-py2.4.egg.

File metadata

  • Download URL: filelike-0.2.2-py2.4.egg
  • Upload date:
  • Size: 42.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for filelike-0.2.2-py2.4.egg
Algorithm Hash digest
SHA256 eeed1a63baf35ce5acb6fadb7a7da0f5994cfe8c4b8f5f2249816a4f5616ed88
MD5 d6433b87d15a387cd1f5a366b3623c86
BLAKE2b-256 d6fa61db2a891e9dcdf28b9657396eb775e7b06eec3a4fef9596bccf21577bdf

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page