NIPY logo

Site Navigation

NIPY Community

Table Of Contents

Previous topic

io.imageformats.filename_parser

This Page

io.imageformats.filetuples

Module: io.imageformats.filetuples

Inheritance diagram for nipy.io.imageformats.filetuples:

Classes for containing filename pairs, triplets etc, with expected extensions

Classes

FileTuples

class nipy.io.imageformats.filetuples.FileTuples(types=(), default_type=None, ignored_suffixes=(), enforce_extensions=True)

Bases: object

Class to accept and check filenames for compatibility with standard

Methods

add_type
get_file_of_type
get_filenames
set_file_of_type
set_filenames
__init__(types=(), default_type=None, ignored_suffixes=(), enforce_extensions=True)

Initialize FileTuples object

Parameters :

types : sequence of sequences, iptional

sequence of (name, extension) sequences defining types

default_type : None or string, optional

string identifying name (above) of default type, when type not specified. If None, use the name of the first type

ignored_suffixes : sequence of strings, optional

suffixes that should be ignored when looking for extensions - e.g (‘.gz’, ‘.bz2’)

enforce_extensions : {True, False}, optional

If True, raise an error when attempting to set value to type which has the wrong extension

Examples

>>> fn = FileTuples((('t1', '.ext1'), ('t2', '.ext2')))
>>> fn.types
(('t1', '.ext1'), ('t2', '.ext2'))
>>> fn.get_filenames()
(None, None)
>>> fn.default_type
't1'
>>> fn = FileTuples((('t1', '.ext1'), ('t2', '.ext2')), default_type='t2')
>>> fn.default_type
't2'
>>> fn = FileTuples((('t1', '.ext1'), ('t2', '.ext2')), ignored_suffixes=('.bz2', '.gz'))
add_type(name, extension=None)

Add a type to the type list

Parameters :

name : string

name for type

extension : string

default extension for this type

Returns :

None :

Examples

>>> fn = FileTuples()
>>> fn.types
()
>>> fn.get_file_of_type('all')
()
>>> fn.default_type
>>> fn.add_type('test', '.tst')
>>> fn.types
(('test', '.tst'),)
>>> fn.get_file_of_type('all')
(None,)
>>> fn.default_type
'test'
>>> fn.add_type('test2', '.tst2')
>>> fn.default_type
'test'
get_file_of_type(typespec='all')

Get files of specified type

Parameters :

typespec : string

either name in types, or “all”

Returns :

files : file, or tuple

If typespec == “all”, return all files Otherwise return file matching name in typespec

Examples

>>> fn = FileTuples(types=(('t1',),('t2',)))
>>> fn.get_file_of_type()
(None, None)
>>> fn.get_file_of_type('all')
(None, None)
>>> fn.get_file_of_type('t1')
>>> fn.set_file_of_type('t1', 'test_file')
>>> fn.get_file_of_type('all')
('test_file', None)
>>> fn.get_file_of_type('t1')
'test_file'
get_filenames()

Get filenames from object

Parameters :

None :

Returns :

filenames : tuple of string

tuple of filenames, one element per type

set_file_of_type(name, fileobj)

Set value of file for given type name name

Parameters :

name : string

name identifying type of file to return

fileobj : string or file-like object

value to add for this name

Examples

>>> fn = FileTuples((('t1', 'ext1'),('t2', 'ext2')))
>>> fn.enforce_extensions = True
>>> fn.set_file_of_type('t1', 'test_name.ext1')
>>> fn.get_file_of_type('t1')
'test_name.ext1'
>>> fn.ignored_suffixes = (('.gz',))
>>> fn.set_file_of_type('t1', 'test_name.ext1.gz')
>>> fn.get_file_of_type('t1')
'test_name.ext1.gz'
set_filenames(input_filename)

Set filename(s) from example filename

Parameters :

filename : string

Example filename. If self.enforce_extensions is True, then filename must have one of the defined extensions from the types list. If self.enforce_extensions is False, then the other filenames are guessed at by adding extensions to the base filename. Ignored suffixes (from self.ignored_suffixes) append themselves to the end of all the filenames set here.

Returns :

None :

Examples

>>> fn = FileTuples(types=(('t1','.ext1'),('t2', '.ext2')))
>>> fn.enforce_extensions = True
>>> fn.set_filenames('/path/test.ext1')
>>> fn.get_filenames()
('/path/test.ext1', '/path/test.ext2')
>>> fn.set_filenames('/path/test.ext2')
>>> fn.get_filenames()
('/path/test.ext1', '/path/test.ext2')
>>> # bare file roots without extensions get them added
>>> fn.set_filenames('/path/test')
>>> fn.get_filenames()
('/path/test.ext1', '/path/test.ext2')
>>> fn.enforce_extensions = False
>>> fn.set_filenames('/path/test.funny')
>>> fn.get_filenames()
('/path/test.funny', '/path/test.ext2')

FileTuplesError

class nipy.io.imageformats.filetuples.FileTuplesError

Bases: exceptions.Exception

__init__()

x.__init__(...) initializes x; see help(type(x)) for signature

args
message