From 6e8b6ddbbca3323616b5850174f73a4bcb3e3c22 Mon Sep 17 00:00:00 2001 From: "sweep-ai[bot]" <128439645+sweep-ai[bot]@users.noreply.github.com> Date: Fri, 22 Dec 2023 06:36:50 +0000 Subject: [PATCH] feat: Updated dspy/primitives/example.py --- dspy/primitives/example.py | 39 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/dspy/primitives/example.py b/dspy/primitives/example.py index 305f64ce0..2d0948654 100644 --- a/dspy/primitives/example.py +++ b/dspy/primitives/example.py @@ -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 = [] @@ -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. @@ -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. @@ -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): @@ -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})" @@ -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): @@ -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) @@ -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):