Skip to content
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions petsctools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from .exceptions import PetscToolsException # noqa: F401
from .options import flatten_parameters # noqa: F401
from .utils import PETSC4PY_INSTALLED
from .appctx import AppContext

# Now conditionally import the functions that depend on petsc4py. If petsc4py
# is not available then attempting to access these attributes will raise an
Expand Down
28 changes: 28 additions & 0 deletions petsctools/appctx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import itertools


class AppContext:
def __init__(self):
self._count = itertools.count()
self._data = {}
self._missing_key = next(self._count)

@property
def missing_key(self):
return self._missing_key

def insert(self, val):
key = next(self._count)
self._data[key] = val
return key

def __getitem__(self, key):
return self._data[key]

def get(self, key, default=None):
if key == self.missing_key:
return default
return self._data.get(key, default=default)

def values(self):
return self._data.values()
Loading