Skip to content

Commit

Permalink
feat: Updated dspy/primitives/example.py
Browse files Browse the repository at this point in the history
  • Loading branch information
sweep-ai[bot] committed Dec 22, 2023
1 parent c4a1535 commit 6e8b6dd
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions dspy/primitives/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def __init__(self, base=None, **kwargs):
If a dict is provided, it is copied. Defaults to None.
**kwargs: Additional key-value pairs to add to the internal storage.
"""
"""
# Internal storage and other attributes
self._store = {}
self._demos = []
Expand All @@ -32,7 +33,7 @@ def __init__(self, base=None, **kwargs):
def __getattr__(self, key):
"""
Get an attribute of the Example instance.
Attempt to access the value of an instance attribute that is not part of the standard properties of the object but is expected to be in the _store dictionary.

Args:
key (str): The name of the attribute.
Expand All @@ -51,7 +52,7 @@ def __getattr__(self, key):
def __setattr__(self, key, value):
"""
Set an attribute of the Example instance.
Set a new attribute of the object. If the key doesn't start with an underscore and isn't a built-in property/method name, the (key, value) pair is stored in the _store dictionary instead of setting an object attribute.

Args:
key (str): The name of the attribute.
Expand Down Expand Up @@ -82,6 +83,9 @@ def __setitem__(self, key, value):
key (str): The key of the item.
value (Any): The value to set the item to.
"""
"""
Add or update an item in the Example instance's internal store using dictionary-style key assignment.
"""
self._store[key] = value

def __delitem__(self, key):
Expand Down Expand Up @@ -109,6 +113,9 @@ def __len__(self):
return len([k for k in self._store if not k.startswith('dspy_')])

def __repr__(self):
"""
Provide a string representation of the Example instance excluding private properties prefixed with 'dspy_'.
"""
# return f"Example({self._store})" + f" (input_keys={self._input_keys}, demos={self._demos})"
d = {k: v for k, v in self._store.items() if not k.startswith('dspy_')}
return f"Example({d})" + f" (input_keys={self._input_keys})"
Expand All @@ -132,6 +139,16 @@ def items(self, include_dspy=False):
return [(k, v) for k, v in self._store.items() if not k.startswith('dspy_') or include_dspy]

def get(self, key, default=None):
"""
Retrieve the value associated with the given key from the instance's store, or return the default if the key is not found.
Args:
key (str): The key to retrieve the value for.
default (any, optional): The value to return if the key is not found. Default is None.
Returns:
The value associated with the key, or the default value.
"""
return self._store.get(key, default)

def with_inputs(self, *keys):
Expand All @@ -144,6 +161,15 @@ def inputs(self):
raise ValueError("Inputs have not been set for this example. Use `example.with_inputs()` to set them.")

# return items that are in input_keys
"""
Retrieve a new Example instance containing only the key-value pairs where the keys are specified as inputs.
Raises:
ValueError: If the input keys have not been set prior to invocation of this method.
Returns:
Example: A new Example instance containing only key-value pairs specified as inputs.
"""
d = {key: self._store[key] for key in self._store if key in self._input_keys}
return type(self)(d)

Expand All @@ -157,6 +183,15 @@ def __iter__(self):
return iter(dict(self._store))

def copy(self, **kwargs):
"""
Create a deep copy of the Example instance, optionally updated with new or changed key-value pairs.
Args:
**kwargs: Key-value pairs to add to or update in the copy of the instance's store.
Returns:
Example: A new Example instance that is a copy of this instance with the specified updates.
"""
return type(self)(base=self, **kwargs)

def without(self, *keys):
Expand Down

0 comments on commit 6e8b6dd

Please sign in to comment.