equal#

class equal(obj, obj2, *args, method=None, detailed=False, equal_nan=True, leaf=False, verbose=None, die=False, **kwargs)[source]#

Compare equality between two arbitrary objects

There is no universal way to check equality between objects in Python. Some objects define their own equals method which may not evaluate to true/false (e.g., Numpy arrays and pandas dataframes). For others it may be undefined. For this reasons, different ways of checking equality may give different results in edge cases. The available methods are:

  • 'eq': uses the objects’ built-in __eq__() methods (most accurate, but most likely to fail)

  • 'pickle': converts the object to a binary pickle (most robust)

  • 'json': converts the object to a JSON via jsonpickle (gives most detailed object structure, but can be lossy)

  • 'str': converts the object to its string representation (least amount of detail)

By default, ‘eq’ is tried first, and if that raises an exception, ‘pickle’ is tried.

Parameters:
  • obj (any) – the first object to compare

  • obj2 (any) – the second object to compare

  • args (list) – additional objects to compare

  • method (str) – see above

  • detailed (bool) – whether to compute a detailed comparison of the objects, and return a dataframe of the results

  • equal_nan (bool) – whether matching np.nan should compare as true (default True; NB, False not guaranteed to work with method='pickle' or 'str', which includes the default; True not guaranteed to work with method='json')

  • leaf (bool) – if True, only compare the object’s leaf nodes (those with no children); otherwise, compare everything

  • verbose (bool) – level of detail to print

  • die (bool) – whether to raise an exception if an error is encountered (else return False)

  • kwargs (dict) – passed to sc.iterobj()

Examples:

o1 = dict(
    a = [1,2,3],
    b = np.array([4,5,6]),
    c = dict(
        df = sc.dataframe(q=[sc.date('2022-02-02'), sc.date('2023-02-02')])
    )
)

# Identical object
o2 = sc.dcp(o1)

# Non-identical object
o3 = sc.dcp(o1)
o3['b'][2] = 8

sc.equal(o1, o2) # Returns True
sc.equal(o1, o3) # Returns False
e = sc.Equal(o1, o2, o3, detailed=True) # Create an object
e.df.disp() # Show results as a dataframe

New in version 3.1.0.