Index Module Index Search Page

qnd.generic module

Generic file or file family open.

class qnd.generic.MultiFile(pattern, existing, future, mode, **kwargs)[source]

Bases: object

A binary file or family of binary files.

callbacks(flusher, initializer)[source]

set callback function that flushes file metadata

close()[source]

flush and close the current file

current_file()[source]

Index of current file in family, argument to open method.

declared(addr, dtype, nitems)[source]

declare that array has been declared, maybe update next_address

filename(n=None)[source]

current or n-th existing filename in family

flush()[source]

flush metadata and ordinary file buffers

next_address(both=False, newfile=False)[source]

next unused multi-file address, or None if newfile cannot create

open(n)[source]

open n-th file of family

seek(addr)[source]

seek to multi-file address, opening alternate file if needed

split_address(addr)[source]

return file index, address for a multifile address

tell()[source]

return current multi-file address

zero_address(n=None)[source]

multifile address of first byte in current or n-th file

qnd.generic.opener(filename, mode, **kwargs)[source]

Generic file or file family opener.

Parameters:
  • filename (str) – Name of file to open. See notes below for family conventions.
  • mode (str) – One of ‘r’ (default, read-only), ‘r+’ (read-write, must exist), ‘a’ (read-write, create if does not exist), ‘w’ (create, clobber if exists), ‘w-’ (create, fail if exists).
  • **kwargs – Other keywords. This opener consumes one item from kwargs:
  • nextaddr_mode (int) – Affects setting of nextaddr for families opened with ‘a’ or ‘r+’ mode. 0 (default) sets nextaddr to the end of the final existing file, 1 sets nextaddr to 0 (beginning of first file), and 2 sets nextaddr to the beginning of the next file after all existing files.
Returns:

  • handle

    A file handle implementing the generic interface, consisting of:

    handle.callbacks(flusher, initializer)
    addr = handle.next_address()  # next unused address
    f = handle.seek(addr)  # return ordinary file handle at addr
    f = handle.open(n)  # open nth file, calling initializer(f)
    handle.flush()  # make file readable, calling flusher(f)
    # flush() restores next_address to value on entry
    handle.close()  # flush if necessary, then close
    
  • nexisting (int) – Number of existing paths matching filename.

Notes

The filename may be an iterable, one string per file in order. The sequence may extend beyond the files which actually exist for ‘r+’, ‘a’, ‘w’, or ‘w-’ modes.

Alternatively filename specifies a family if it contains shell globbing wildcard characters. Existing matching files are sorted first by length, then alphabetically (ensuring that ‘file100’ comes after ‘file99’, for example). If there is only a single wildcard group, it also serves to define a sequence of future family names beyond those currently existing for ‘r+’, ‘a’, ‘w’, or ‘w-’ modes. A ‘?’ pattern is treated the same as a ‘[0-9]’ pattern if all its matches are digits or if the pattern matches no existing files. Similarly, a ‘*’ acts like the minimum number of all-digit matches, or three digits if there are no matches.

A single filename may also contain a %d or %0nd print format directive, which will be converted to the corresponding number of [0-9] glob patterns.