Source code for libvhc

# Virus Health Check: a validation tool for HETDEX/VIRUS data
# Copyright (C) 2015, 2016, 2018  "The HETDEX collaboration"
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
"""
"""
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

import pkg_resources
__version__ = pkg_resources.get_distribution('vhc').version


[docs]class VCheck(object): """Store the information about the current check. The attributes can be accessed in the standard way, e.g. ``instance.attribute``, or as in a dictionary, e.g. ``instance["attribute"]``. Parameters ---------- recipe : string name of the recipe check : string name of the check Examples -------- >>> recipe = "flat" >>> checks = ["common:n_pixels", "common:saturation"] >>> vcheck = VCheck(recipe) >>> for c in checks: ... vcheck.check = c ... # call the correct check function ... print(vcheck) recipe: flat, check: common:n_pixels recipe: flat, check: common:saturation >>> print(vcheck.recipe) flat >>> print(vcheck["recipe"]) flat >>> print(vcheck.check) common:saturation >>> print(vcheck["check"]) common:saturation >>> for k in vcheck: ... print(k, vcheck[k]) recipe flat check common:saturation >>> repr(vcheck) "VCheck('flat', 'common:saturation')" >>> vcheck['attribute'] Traceback (most recent call last): ... KeyError: attribute is not a valid key >>> # you can also change the recipe name >>> vcheck.recipe = 'new_recipe' """ def __init__(self, recipe="", check=""): self._recipe = recipe self._check = check # list of attributes accessible as dictionary self._keys = ["recipe", "check"] @property def recipe(self): return self._recipe @recipe.setter def recipe(self, recipe): self._recipe = recipe @property def check(self): return self._check @check.setter def check(self, check): self._check = check def as_dict(self): """returns a dictionary of the keys-value accessible as :class:``VCheck`` where a dictionary""" return {k: self[k] for k in self._keys} def __str__(self): descr = "recipe: {}, check: {}".format(self.recipe, self.check) return descr def __repr__(self): descr = "{name}('{recipe}', '{check}')" return descr.format(name=self.__class__.__name__, recipe=self.recipe, check=self.check) def __getitem__(self, key): if key in self._keys: return getattr(self, key) else: raise KeyError("{} is not a valid key".format(key)) def __iter__(self): return iter(['recipe', 'check'])