TempfileManagerClass

(class from pyomo.common.tempfiles)

class pyomo.common.tempfiles.TempfileManagerClass[source]

Bases: object

A class for managing tempfile contexts

Pyomo declares a global instance of this class as TempfileManager:

>>> from pyomo.common.tempfiles import TempfileManager

This class provides an interface for managing TempfileContext contexts. It implements a basic stack, where users can push() a new context (causing it to become the current “active” context) and pop() contexts off (optionally deleting all files associated with the context). In general usage, users will either use this class to create new tempfile contexts and use them explicitly (i.e., through a context manager):

>>> import os
>>> with TempfileManager.new_context() as tempfile:
...     fd, fname = tempfile.mkstemp()
...     dname = tempfile.mkdtemp()
...     os.path.isfile(fname)
...     os.path.isdir(dname)
True
True
>>> os.path.exists(fname)
False
>>> os.path.exists(dname)
False

or through an implicit active context accessed through the manager class:

>>> TempfileManager.push()
<pyomo.common.tempfiles.TempfileContext object ...>
>>> fname = TempfileManager.create_tempfile()
>>> dname = TempfileManager.create_tempdir()
>>> os.path.isfile(fname)
True
>>> os.path.isdir(dname)
True

>>> TempfileManager.pop()
<pyomo.common.tempfiles.TempfileContext object ...>
>>> os.path.exists(fname)
False
>>> os.path.exists(dname)
False
__init__()[source]

Methods

__init__()

add_tempfile(filename[, exists])

Call TempfileContext.add_tempfile() on the active context

clear_tempfiles([remove])

Delete all temporary files and remove all contexts.

context()

Return the current active TempfileContext.

create_tempdir([suffix, prefix, dir])

Call TempfileContext.create_tempdir() on the active context

create_tempfile([suffix, prefix, text, dir])

Call TempfileContext.create_tempfile() on the active context

new_context()

Create and return an new tempfile context

pop([remove])

Remove and release the active context

push()

Create a new tempfile context and set it as the active context.

sequential_files([ctr])

DEPRECATED.

shutdown([remove])

unique_files()

Member Documentation

add_tempfile(filename, exists=True)[source]

Call TempfileContext.add_tempfile() on the active context

clear_tempfiles(remove=True)[source]

Delete all temporary files and remove all contexts.

context()[source]

Return the current active TempfileContext.

Raises:

TempfileContextError if there is not a current context.

create_tempdir(suffix=None, prefix=None, dir=None)[source]

Call TempfileContext.create_tempdir() on the active context

create_tempfile(suffix=None, prefix=None, text=False, dir=None)[source]

Call TempfileContext.create_tempfile() on the active context

new_context()[source]

Create and return an new tempfile context

Returns:

the newly-created tempfile context

Return type:

TempfileContext

pop(remove=True)[source]

Remove and release the active context

Parameters:

remove (bool) – If True, delete all managed files / directories

push()[source]

Create a new tempfile context and set it as the active context.

Returns:

the newly-created tempfile context

Return type:

TempfileContext

sequential_files(ctr=0)[source]

DEPRECATED.

Deprecated since version 6.2: The TempfileManager.sequential_files() method has been removed. All temporary files are created with guaranteed unique names. Users wishing sequentially numbered files should create a temporary (empty) directory using mkdtemp / create_tempdir and place the sequential files within it.