BlockFile — Block File Bases class

This is the base class used for (large) files consisting of blocks within the pylam framework.

As a Block file we consider a file which can be contains of one ore more repeating logical sections, like bfile.txt:

Example for a block file.

[data]
00 01 02 03 04
10 11 12 13 14
20 21 22 23 24
[end data]

Lorem ipsum dolor sit amet, consetetur sadipscing elitr,
sed diam nonumy eirmod tempor invidunt ut labore et dolore.

[data]
20 21 22 23 24
30 31 32 33 34
40 41 42 43 44
50 51 52 53 54
[end data]

Et vero eos et accusam et justo duo dolores et ea rebum.

[data]
40 41 42 43 44
50 51 52 53 54
60 61 62 63 64
70 71 72 73 74
80 81 82 83 84
[end data]

*eof*

The base.BlockFile class is derived from base.IndexedFile. Therefore we can easily make use of the base.IndexedFile._parseLine() method to implement a parsing. That way a Block file object can be populated with base.Block objects by using base.BlockFile.addBlock().

For the above shown example bfile.txt, a simple implementation could look like:

>>> import pylam.base
>>>
>>> class MyBlockFile(pylam.base.BlockFile):
>>>
>>>     def __init__(self, filename):
>>>         self.tmp = None
>>>         super(MyBlockFile, self).__init__(filename)
>>>
>>>     def _parseLine(self, line, no):
>>>         if "[data]" in line:
>>>            self.tmp = no + 1
>>>         if "[end data]" in line:
>>>           self.addBlock(fline=self.tmp,
>>>                         lline=no-1,
>>>                         header_line=str('Block {0:d}'.format(len(self))))
>>>
>>>
>>> blockfile = MyBlockFile('bfile.txt')
>>> print len(blockfile)   # returns the number of blocks
3

Note

len() returns the number of blocks, not number of lines!

One can iterate over the blocks within a block file object, and has itemized access:

>>> for block in blockfile:
>>>     print block.header_line
Block 0
Block 1
Block 2
>>> print blockfile[0].data
00 01 02 03 04
10 11 12 13 14
20 21 22 23 24

class pylam.base.BlockFile(filename)[source]

Bases: pylam.base.IndexedFile

Generic class for files containing different blocks.

Parameters:filename (str) – file name
Returns:block file object
Return type:BlockFile
blockClass

Class of the Block object which is attached by addBlock().

alias of Block

__len__()[source]

Returns the number of blocks,

__getitem__(index)[source]

Support for read only itemized access (self[index]) to the Blocks.

Parameters:index – block index within the file
Returns:block object
Return type:Block
getBlock(index)[source]

Returns the Block object.

Deprecated since version 0.1.dev1: Use itemized access instead!

Parameters:
  • index – block index within the file
  • index – int
Returns:

block object

Return type:

Block

addBlock(fline=0, lline=0, header_line='N/A')[source]

Adds a Block object of type blockClass to the BlockFile.

Parameters:
  • fline (int) – index of the first line of the block within the BlockFile
  • lline (int) – index of the last line of the block within the BlockFile
  • header_line (str) – an optional header

Block — Block Bases class

This is the base class for a block (a logical subunit) within a file.

class pylam.base.Block(blockFile, fline=0, lline=0, header_line='N/A')[source]

Generic class for a block within an indexed block file.

Parameters:
  • blockFile (pylam.base.BlockFile) – block file object
  • fline (int) – first line index of the data block
  • lline (int) – last line index of the data block
  • header_line (str) – header of the block
__getitem__(index)[source]

Support for read only itemized access (self[index]) to the lines of the block.

Parameters:index (int) – block line index
Returns:data
Return type:str
__len__()[source]

Returns the number of lines of the block.

next()[source]

Returns the next line from the block as a string.

getLines(startLineIndex, endLineIndex)[source]

Returns a part of the block as a string.

Parameters:
  • startLineIndex (int) – index of first line
  • endLineIndex (int) – index of last line (included!)
Returns:

multiple lines

Return type:

str

getLine(lineIndex)[source]

Returns a line of the block as a string.

Parameters:lineIndex (int) – index of data row
Returns:line
Return type:str
writeOTF(filename)[source]

Writes the whole block as it is and on the fly to a separate file. Hence, not all the data needs to be stored in memory in between!

Parameters:filename (str) – output file name
info()[source]

Prints debug info to screen, e.g.:

header    : This is a header line.
size      : 100
first line: 5
last line : 104
data

The whole block as a string.