Fileseq Python API¶
fileseq - A simple python library for parsing file sequence strings commonly used in VFX and Animation applications.
The MIT License (MIT)
Copyright (c) 2015 Matthew Chambers
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
fileseq.exceptions module¶
exceptions - Exception subclasses relevant to fileseq operations.
-
exception
fileseq.exceptions.
FileSeqException
[source]¶ Bases:
exceptions.ValueError
Thrown for general exceptions handled by FileSeq.
-
exception
fileseq.exceptions.
MaxSizeException
[source]¶ Bases:
exceptions.ValueError
Thrown when a range exceeds allowable size.
-
exception
fileseq.exceptions.
ParseException
[source]¶ Bases:
fileseq.exceptions.FileSeqException
Thrown after a frame range or file sequence parse error.
fileseq.filesequence module¶
filesequence - A parsing object representing sequential files for fileseq.
-
class
fileseq.filesequence.
FileSequence
(sequence)[source]¶ Bases:
future.types.newobject.newobject
FileSequence
represents an ordered sequence of files.Parameters: sequence (str) – (ie: dir/path.1-100#.ext)
Returns: Return type: Raises: fileseq.exceptions.MaxSizeException
– If frame size exceedsfileseq.constants.MAX_FRAME_SIZE
-
__getitem__
(idx)[source]¶ Allows indexing and slicing into the underlying
FrameSet
When indexing, a string filepath is returns for the frame.
When slicing, a new
FileSequence
is returned. Slicing outside the range of the sequence results in an IndexErrorParameters: idx (int or slice) – the desired index Returns: Return type: str or FileSequence
Raises: IndexError
– If slice is outside the range of the sequence
-
__iter__
()[source]¶ Allow iteration over the path or paths this
FileSequence
represents.Yields: FileSequence
-
__len__
()[source]¶ The length (number of files) represented by this
FileSequence
.Returns: Return type: int
-
classmethod
conformPadding
(chars)[source]¶ Ensure alternate input padding formats are conformed to formats defined in PAD_MAP
If chars is already a format defined in PAD_MAP, then it is returned unmodified.
- Example::
- ‘#’ -> ‘#’ ‘@@@@’ -> ‘@@@@’ ‘%04d’ -> ‘#’
Parameters: chars (str) – input padding chars Returns: conformed padding chars Return type: str Raises: ValueError
– If chars contains invalid padding characters
-
copy
()[source]¶ Create a deep copy of this sequence
Returns: Return type: FileSequence
-
end
()[source]¶ Returns the end frame of the sequences
FrameSet
. Will return 0 if the sequence has no frame pattern.Returns: Return type: int
-
extension
()[source]¶ Return the file extension of the sequence, including leading period.
Returns: Return type: str
-
classmethod
findSequenceOnDisk
(pattern, strictPadding=False)[source]¶ Search for a specific sequence on disk.
The padding characters used in the pattern are used to filter the frame values of the files on disk (if strictPadding is True).
Examples
Find sequence matching basename and extension, and a wildcard for any frame. returns bar.1.exr bar.10.exr, bar.100.exr, bar.1000.exr, inclusive
>>> findSequenceOnDisk("seq/bar@@@@.exr")
Find exactly 4-padded sequence, i.e. seq/bar1-100#.exr returns only frames bar1000.exr through bar9999.exr
>>> findSequenceOnDisk("seq/bar#.exr", strictPadding=True)
Parameters: - pattern (str) – the sequence pattern being searched for
- strictPadding (bool) – if True, ignore files with padding length different from pattern
Returns: Return type: str
Raises: FileSeqException
– if no sequence is found on disk
-
static
findSequencesInList
(paths)[source]¶ Returns the list of discrete sequences within paths. This does not try to determine if the files actually exist on disk, it assumes you already know that.
Parameters: paths (list[str]) – a list of paths Returns: Return type: list
-
classmethod
findSequencesOnDisk
(pattern, include_hidden=False, strictPadding=False)[source]¶ Yield the sequences found in the given directory.
Examples
>>> findSequencesOnDisk('/path/to/files')
- The pattern can also specify glob-like shell wildcards including the following:
?
- 1 wildcard character*
- 1 or more wildcard character{foo,bar}
- either ‘foo’ or ‘bar’
Exact frame ranges are not considered, and padding characters are converted to wildcards (
#
or@
)Examples
>>> findSequencesOnDisk('/path/to/files/image_stereo_{left,right}.#.jpg') >>> findSequencesOnDisk('/path/to/files/imag?_*_{left,right}.@@@.jpg', strictPadding=True)
Parameters: - pattern (str) – directory to scan, or pattern to filter in directory
- include_hidden (bool) – if true, show .hidden files as well
- strictPadding (bool) – if True, ignore files with padding length different from pattern
Returns: Return type: list
-
format
(template='{basename}{range}{padding}{extension}')[source]¶ Return the file sequence as a formatted string according to the given template.
- Utilizes the python string format syntax. Available keys include:
- basename - the basename of the sequence.
- extension - the file extension of the sequence.
- start - the start frame.
- end - the end frame.
- length - the length of the frame range.
- padding - the detecting amount of padding.
- inverted - the inverted frame range. (returns “” if none)
- dirname - the directory name.
If asking for the inverted range value, and the new inverted range exceeded
fileseq.constants.MAX_FRAME_SIZE
, aMaxSizeException
will be raised.Parameters: template (str) –
Returns: Return type: str
Raises: fileseq.exceptions.MaxSizeException
– If frame size exceedsfileseq.constants.MAX_FRAME_SIZE
-
frame
(frame)[source]¶ Return a path go the given frame in the sequence. Integer or string digits are treated as a frame number and padding is applied, all other values are passed though.
Examples
>>> seq.frame(1) /foo/bar.0001.exr >>> seq.frame("#") /foo/bar.#.exr
Parameters: frame (int or str) – the desired frame number or a char to pass through (ie. #) Returns: Return type: str
-
frameRange
()[source]¶ Returns the string formatted frame range of the sequence. Will return an empty string if the sequence has no frame pattern.
Returns: Return type: str
-
frameSet
()[source]¶ Return the
FrameSet
of the sequence if specified, otherwise None.Returns: Return type: FrameSet
or None
-
static
getPaddingChars
(num)[source]¶ Given a particular amount of padding, return the proper padding characters.
Parameters: num (int) – Returns: Return type: str
-
static
getPaddingNum
(chars)[source]¶ Given a supported group of padding characters, return the amount of padding.
Parameters: chars (str) – a supported group of padding characters Returns: Return type: int Raises: ValueError
– if unsupported padding character is detected
-
index
(idx)[source]¶ Return the path to the file at the given index.
Parameters: idx (int) – the desired index Returns: Return type: str
-
invertedFrameRange
()[source]¶ Returns the inverse string formatted frame range of the sequence. Will return an empty string if the sequence has no frame pattern.
Returns: Return type: str Raises: fileseq.exceptions.MaxSizeException
– If new inverted range exceededfileseq.constants.MAX_FRAME_SIZE
-
padding
()[source]¶ Return the the padding characters in the sequence.
Returns: sequence padding Return type: str
-
setBasename
(base)[source]¶ Set a new basename for the sequence.
Parameters: base (str) – the new base name
-
setDirname
(dirname)[source]¶ Set a new directory name for the sequence.
Parameters: dirname (str) – the new directory name
-
setExtension
(ext)[source]¶ Set a new file extension for the sequence.
Note
A leading period will be added if none is provided.
Parameters: ext (str) – the new file extension
-
setExtention
(ext)[source]¶ Deprecated: use
setExtension()
.Parameters: ext (str) –
-
setFrameRange
(frange)[source]¶ Set a new frame range for the sequence.
Parameters: frange (str) – a properly formatted frame range, as per FrameSet
-
setFrameSet
(frameSet)[source]¶ Set a new
FrameSet
for the sequence.Parameters: frameSet ( FrameSet
) – the newFrameSet
object
-
setPadding
(padding)[source]¶ Set new padding characters for the sequence. i.e. “#” or “@@@” or ‘%04d’, or an empty string to disable range formatting.
Parameters: padding (str) – sequence padding to set
-
split
()[source]¶ Split the
FileSequence
into contiguous pieces and return them as a list ofFileSequence
instances.Returns: Return type: list[ FileSequence
]
-
start
()[source]¶ Returns the start frame of the sequence’s
FrameSet
. Will return 0 if the sequence has no frame pattern.Returns: Return type: int
-
static
yield_sequences_in_list
(paths)[source]¶ Yield the discrete sequences within paths. This does not try to determine if the files actually exist on disk, it assumes you already know that.
Parameters: paths (list[str]) – a list of paths Yields: FileSequence
fileseq.frameset module¶
frameset - A set-like object representing a frame range for fileseq.
-
class
fileseq.frameset.
FrameSet
(frange)[source]¶ Bases:
_abcoll.Set
A
FrameSet
is an immutable representation of the ordered, unique set of frames in a given frame range.- The frame range can be expressed in the following ways:
- 1-5
- 1-5,10-20
- 1-100x5 (every fifth frame)
- 1-100y5 (opposite of above, fills in missing frames)
- 1-100:4 (same as 1-100x4,1-100x3,1-100x2,1-100)
A
FrameSet
is effectively an ordered frozenset, with FrameSet-returning versions of frozenset methods:>>> FrameSet('1-5').union(FrameSet('5-10')) FrameSet('1-10') >>> FrameSet('1-5').intersection(FrameSet('5-10')) FrameSet('5')
Because a FrameSet is hashable, it can be used as the key to a dictionary:
>>> {FrameSet('1-20'): 'good'}
- Caveats:
- Because the internal storage of a
FrameSet
contains the discreet values of the entire range, an exception will be thrown if the range exceeds a large reasonable limit, which could lead to huge memory allocations or memory failures. Seefileseq.constants.MAX_FRAME_SIZE
. - All frozenset operations return a normalized
FrameSet
: internal frames are in numerically increasing order. - Equality is based on the contents and order, NOT the frame range string (there are a finite, but potentially extremely large, number of strings that can represent any given range, only a “best guess” can be made).
- Human-created frame ranges (ie 1-100x5) will be reduced to the actual internal frames (ie 1-96x5).
- The “null”
Frameset
(FrameSet('')
) is now a valid thing to create, it is required by set operations, but may cause confusion as both its start and end methods will raise IndexError. Theis_null()
property has been added to allow you to guard against this.
- Because the internal storage of a
Parameters: frange (str or FrameSet or collections.Iterable) – the frame range as a string (ie “1-100x5”)
Raises: ParseException
– if the frame range (or a portion of it) could not be parsed.fileseq.exceptions.MaxSizeException
– if the range exceeds fileseq.constants.MAX_FRAME_SIZE
-
__and__
(other)[source]¶ Overloads the
&
operator. Returns a newFrameSet
that holds only the frames self and other have in common.Note
The order of operations is irrelevant:
(self & other) == (other & self)
Parameters: other ( FrameSet
) –Returns: NotImplemented
: if other fails to convert to aFrameSet
Return type: FrameSet
-
__contains__
(item)[source]¶ Check if item is a member of this
FrameSet
.Parameters: item (int) – the frame number to check for Returns: Return type: bool
-
__eq__
(other)[source]¶ Check if self == other via a comparison of the hash of their contents. If other is not a
FrameSet
, but is a set, frozenset, or is iterable, it will be cast to aFrameSet
.Parameters: other ( FrameSet
) – Also accepts an object that can be cast to aFrameSet
Returns: NotImplemented
: if other fails to convert to aFrameSet
Return type: bool
-
__ge__
(other)[source]¶ Check if self >= other via a comparison of the contents. If other is not a
FrameSet
, but is a set, frozenset, or is iterable, it will be cast to aFrameSet
.Parameters: other ( FrameSet
) – Also accepts an object that can be cast to aFrameSet
Returns: NotImplemented
: if other fails to convert to aFrameSet
Return type: bool
-
__getitem__
(index)[source]¶ Allows indexing into the ordered frames of this
FrameSet
.Parameters: index (int) – the index to retrieve Returns: Return type: int Raises: IndexError
– if index is out of bounds
-
__getstate__
()[source]¶ Allows for serialization to a pickled
FrameSet
.Returns: (frame range string, ) Return type: tuple
-
__gt__
(other)[source]¶ Check if self > other via a comparison of the contents. If other is not a
FrameSet
, but is a set, frozenset, or is iterable, it will be cast to aFrameSet
.Note
A
FrameSet
is greater than other if the set of its contents are greater, OR if the contents are equal but the order is greater.>>> FrameSet("1-5") > FrameSet("5-1") False
Parameters: other ( FrameSet
) – Also accepts an object that can be cast to aFrameSet
Returns: NotImplemented
: if other fails to convert to aFrameSet
Return type: bool
-
__hash__
()[source]¶ Builds the hash of this
FrameSet
for equality checking and to allow use as a dictionary key.Returns: Return type: int
-
__iter__
()[source]¶ Allows for iteration over the ordered frames of this
FrameSet
.Returns: Return type: generator
-
__le__
(other)[source]¶ Check if self <= other via a comparison of the contents. If other is not a
FrameSet
, but is a set, frozenset, or is iterable, it will be cast to aFrameSet
.Parameters: other ( FrameSet
) – Also accepts an object that can be cast to aFrameSet
Returns: NotImplemented
: if other fails to convert to aFrameSet
Return type: bool
-
__len__
()[source]¶ Returns the length of the ordered frames of this
FrameSet
.Returns: Return type: int
-
__lt__
(other)[source]¶ Check if self < other via a comparison of the contents. If other is not a
FrameSet
, but is a set, frozenset, or is iterable, it will be cast to aFrameSet
.Note
A
FrameSet
is less than other if the set of its contents are less, OR if the contents are equal but the order of the items is less.>>> FrameSet("1-5") < FrameSet("5-1") True
Parameters: other ( FrameSet
) – Can also be an object that can be cast to aFrameSet
Returns: NotImplemented
: if other fails to convert to aFrameSet
Return type: bool
-
__ne__
(other)[source]¶ Check if self != other via a comparison of the hash of their contents. If other is not a
FrameSet
, but is a set, frozenset, or is iterable, it will be cast to aFrameSet
.Parameters: other ( FrameSet
) – Also accepts an object that can be cast to aFrameSet
Returns: NotImplemented
: if other fails to convert to aFrameSet
Return type: bool
-
__or__
(other)[source]¶ Overloads the
|
operator. Returns a newFrameSet
that holds all the frames in self, other, or both.Note
The order of operations is irrelevant:
(self | other) == (other | self)
Parameters: other ( FrameSet
) –Returns: NotImplemented
: if other fails to convert to aFrameSet
Return type: FrameSet
-
__rand__
(other)¶ Overloads the
&
operator. Returns a newFrameSet
that holds only the frames self and other have in common.Note
The order of operations is irrelevant:
(self & other) == (other & self)
Parameters: other ( FrameSet
) –Returns: NotImplemented
: if other fails to convert to aFrameSet
Return type: FrameSet
-
__reversed__
()[source]¶ Allows for reversed iteration over the ordered frames of this
FrameSet
.Returns: Return type: generator
-
__ror__
(other)¶ Overloads the
|
operator. Returns a newFrameSet
that holds all the frames in self, other, or both.Note
The order of operations is irrelevant:
(self | other) == (other | self)
Parameters: other ( FrameSet
) –Returns: NotImplemented
: if other fails to convert to aFrameSet
Return type: FrameSet
-
__rsub__
(other)[source]¶ Overloads the
-
operator. Returns a newFrameSet
that holds only the frames of other that are not in self.Note
This is for right-hand subtraction (
other - self
).Parameters: other ( FrameSet
) –Returns: NotImplemented
: if other fails to convert to aFrameSet
Return type: FrameSet
-
__rxor__
(other)¶ Overloads the
^
operator. Returns a newFrameSet
that holds all the frames in self or other but not both.Note
The order of operations is irrelevant:
(self ^ other) == (other ^ self)
Parameters: other ( FrameSet
) –Returns: NotImplemented
: if other fails to convert to aFrameSet
Return type: FrameSet
-
__setstate__
(state)[source]¶ Allows for de-serialization from a pickled
FrameSet
.Parameters: state (tuple or str or dict) – A string/dict can be used for backwards compatibility Raises: ValueError
– if state is not an appropriate type
-
__sub__
(other)[source]¶ Overloads the
-
operator. Returns a newFrameSet
that holds only the frames of self that are not in other.Note
This is for left-hand subtraction (
self - other
).Parameters: other ( FrameSet
) –Returns: NotImplemented
: if other fails to convert to aFrameSet
Return type: FrameSet
-
__xor__
(other)[source]¶ Overloads the
^
operator. Returns a newFrameSet
that holds all the frames in self or other but not both.Note
The order of operations is irrelevant:
(self ^ other) == (other ^ self)
Parameters: other ( FrameSet
) –Returns: NotImplemented
: if other fails to convert to aFrameSet
Return type: FrameSet
-
difference
(*other)[source]¶ Returns a new
FrameSet
with elements in self but not in other.Parameters: other ( FrameSet
) – or objects that can cast toFrameSet
Returns: Return type: FrameSet
-
end
()[source]¶ The last frame in the
FrameSet
.Returns: Return type: int Raises: IndexError
– (with the emptyFrameSet
)
-
frame
(index)[source]¶ Return the frame at the given index.
Parameters: index (int) – the index to find the frame for Returns: Return type: int Raises: IndexError
– if index is out of bounds
-
frameRange
(zfill=0)[source]¶ Return the frame range used to create this
FrameSet
, padded if desired.Examples
>>> FrameSet('1-100').frameRange() '1-100' >>> FrameSet('1-100').frameRange(5) '00001-00100'
Parameters: zfill (int) – the width to use to zero-pad the frame range string Returns: Return type: str
-
static
framesToFrameRange
(frames, sort=True, zfill=0, compress=False)[source]¶ Converts an iterator of frames into a frame range string.
Parameters: - frames (collections.Iterable) – sequence of frames to process
- sort (bool) – sort the sequence before processing
- zfill (int) – width for zero padding
- compress (bool) – remove any duplicates before processing
Returns: Return type: str
-
static
framesToFrameRanges
(frames, zfill=0)[source]¶ Converts a sequence of frames to a series of padded frame range strings.
Parameters: - frames (collections.Iterable) – sequence of frames to process
- zfill (int) – width for zero padding
Yields: str
-
frange
¶ Read-only access to the frame range used to create this
FrameSet
.Returns: Return type: frozenset
-
classmethod
from_iterable
(frames, sort=False)[source]¶ Build a
FrameSet
from an iterable of frames.Parameters: - frames (collections.Iterable) – an iterable object containing frames as integers
- sort (bool) – True to sort frames before creation, default is False
Returns: Return type:
-
hasFrame
(frame)[source]¶ Check if the
FrameSet
contains the frame.Parameters: frame (int) – the frame number to search for Returns: Return type: bool
-
index
(frame)[source]¶ Return the index of the given frame number within the
FrameSet
.Parameters: frame (int) – the frame number to find the index for Returns: Return type: int Raises: ValueError
– if frame is not in self
-
intersection
(*other)[source]¶ Returns a new
FrameSet
with the elements common to self and other.Parameters: other ( FrameSet
) – or objects that can cast toFrameSet
Returns: Return type: FrameSet
-
invertedFrameRange
(zfill=0)[source]¶ Return the inverse of the
FrameSet
‘s frame range, padded if desired. The inverse is every frame within the full extent of the range.Examples
>>> FrameSet('1-100x2').invertedFrameRange() '2-98x2' >>> FrameSet('1-100x2').invertedFrameRange(5) '00002-00098x2'
If the inverted frame size exceeds fileseq.constants.MAX_FRAME_SIZE, a
MaxSizeException
will be raised.Parameters: zfill (int) – the width to use to zero-pad the frame range string Returns: Return type: str Raises: fileseq.exceptions.MaxSizeException
-
isConsecutive
()[source]¶ Return whether the frame range represents consecutive integers, as opposed to having a stepping >= 2
Examples
>>> FrameSet('1-100').isConsecutive() True >>> FrameSet('1-100x2').isConsecutive() False >>> FrameSet('1-50,60-100').isConsecutive() False
Returns: Return type: bool
-
static
isFrameRange
(frange)[source]¶ Return True if the given string is a frame range. Any padding characters, such as ‘#’ and ‘@’ are ignored.
Parameters: frange (str) – a frame range to test Returns: Return type: bool
-
is_null
¶ Read-only access to determine if the
FrameSet
is the null or emptyFrameSet
.Returns: Return type: bool
-
isdisjoint
(other)[source]¶ Check if the contents of :class:self has no common intersection with the contents of :class:other.
Parameters: other ( FrameSet
) –Returns: NotImplemented
: if other fails to convert to aFrameSet
Return type: bool
-
issubset
(other)[source]¶ Check if the contents of self is a subset of the contents of other.
Parameters: other ( FrameSet
) –Returns: NotImplemented
: if other fails to convert to aFrameSet
Return type: bool
-
issuperset
(other)[source]¶ Check if the contents of self is a superset of the contents of other.
Parameters: other ( FrameSet
) –Returns: NotImplemented
: if other fails to convert to aFrameSet
Return type: bool
-
items
¶ Read-only access to the unique frames that form this
FrameSet
.Returns: Return type: frozenset
-
normalize
()[source]¶ Returns a new normalized (sorted and compacted)
FrameSet
.Returns: Return type: FrameSet
-
static
padFrameRange
(frange, zfill)[source]¶ Return the zero-padded version of the frame range string.
Parameters: - frange (str) – a frame range to test
- zfill (int) –
Returns: Return type: str
-
start
()[source]¶ The first frame in the
FrameSet
.Returns: Return type: int Raises: IndexError
– (with the emptyFrameSet
)