Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dict-wrapper #40

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions vflow/subkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ def __init__(self, value, origin: str, output_matching: bool = False):
output_matching: bool (optional), default False
inherited from the Vset where the Subkey is created
"""
self.meta = {}
self.meta['value'] = value
self.meta['origin'] = origin
self.meta['inputs'] = None
self.meta['params'] = None
self.meta['vfunc'] = None
self.value = value
self.origin = origin
self.output_matching = output_matching
Expand Down Expand Up @@ -66,6 +72,10 @@ def mismatches(self, other: object):
cond2 = self.origin == other.origin and self.value != other.value
return (cond0 or cond1) and cond2
return True

def __copy__(self):
"""Return a copy of this Subkey
"""

def __eq__(self, other: object):
"""Mainly used for testing purposes.
Expand Down
31 changes: 31 additions & 0 deletions vflow/vdict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""Vdict key tuple wrapper class
"""
from typing import Tuple, Union

class Vdict:

def __init__(self, _dict: dict = {}):
self._dict = _dict

def to_pandas(self, copy=False):
"""Return a pandas.DataFrame representation of the Vdict.
"""

def __getitem__(self, *subkeys: Union[str, Tuple[str, str]], copy=False):
"""Return a new Vdict with a subset of the items in self by filtering keys
based on subkey values. If copy=True, then make a deep copy of Vkeys and values.

Examples:
preds[`preproc_0`, `RF`] => Vdict with all items that have subkey
with value `preproc_0` and another with `RF`
`preproc_0` in preds => bool
(`model`, `RF`) in preds => bool
"""
out = Vdict()
for vkey, value in self._dict.items():
if subkeys in vkey:
if copy:
out[vkey.__deepcopy__()] = value
else:
out[vkey] = value
return out
77 changes: 77 additions & 0 deletions vflow/vkey.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
"""Vdict key tuple wrapper class
"""
from typing import Tuple, Union

from vflow.utils import combine_keys

class Vkey:

def __init__(self, subkeys, origin: str, method: str):
"""
Parameters
----------
subkeys: tuple
Tuple of Subkey objects to associate with this Vkey.
origin: str
String attribute that identifies the Vset that created this Vkey.
method: str
String attribute that identifies the Vset method that was called to create
this Vkey.
"""
self._subkeys = subkeys
self.origin = origin
self.method = method

def subkeys(self):
"""Return a tuple of the Subkey objects in this Vkey.
"""
return self._subkeys

def get_origins(self):
"""Return a tuple of strings with the origins of the Subkeys in this Vkey.
"""
return (sk.origin for sk in self.subkeys())

def get_values(self):
"""Return a tuple of strings with the values of the Subkeys in this Vkey.
"""
return (sk.value for sk in self.subkeys())

def __contains__(self, *subkeys: Union[str, Tuple[str, str]]):
"""Returns True if subkeys that are strings overlap with self.get_values()
and if subkeys that are string tuples like (`origin`, `value`) have
corresponding matches in both self.get_origins() and self.get_values().

Examples:
`preproc_0` in vkey => bool
(`model`, `RF`) in vkey => bool
"""
_values = self.get_values()
_sktuples = zip(self.get_origins(), _values)
for subkey in subkeys:
if isinstance(subkey, str) and subkey in _values:
return True
if isinstance(subkey, tuple) and subkey in _sktuples:
return True
return False

def __add__(self, other: 'Vkey'):
"""Return a new Vkey by combining this Vkey with other, following Subkey
matching rules. Returns an empty Vkey if there are any Subkey mismatches.
"""
return Vkey(combine_keys(self.subkeys(), other.subkeys()), self.origin, self.method)

def __copy__(self):
"""Return a copy of this Vkey (but not its Subkeys).
"""
return Vkey(self.subkeys(), self.origin, self.method)

def __deepcopy__(self, memo):
"""Return a copy of this Vkey and its Subkeys.
"""
return Vkey((sk.__copy__() for sk in self.subkeys()), self.origin, self.method)

def __len__(self):
"""Return the number of Subkeys in this Vkey.
"""
return len(self.subkeys())