Hone logo
Hone
Problems

Implementing Soft Deletes in a Python Class

Soft deletes are a common technique in database management to avoid permanently deleting data. Instead of physically removing a record, you mark it as deleted, typically by adding a flag. This challenge asks you to implement soft deletes within a Python class representing a data record, allowing for marking records as deleted without actually removing them from the system. This is useful for auditing, data recovery, and maintaining historical data.

Problem Description

You are tasked with creating a Record class that supports soft deletes. The class should have the following attributes: id, data, and deleted. Initially, deleted should be False. The class should provide methods to:

  1. mark_deleted(): Sets the deleted attribute to True.
  2. undelete(): Sets the deleted attribute to False.
  3. is_deleted(): Returns True if the record is marked as deleted, False otherwise.
  4. __str__(): Returns a string representation of the record, including its id, data, and deleted status. The string should be formatted as "Record(id={id}, data={data}, deleted={deleted})".

The class should ensure that the deleted attribute can only be set to True or False. It should also handle the case where a record is already deleted when undelete() is called.

Examples

Example 1:

Input: record = Record(id=1, data="Initial Data")
record.is_deleted()
record.mark_deleted()
record.is_deleted()
record.data = "Updated Data"
record.undelete()
record.is_deleted()
print(record)

Output:

False
True
True
False
Record(id=1, data=Updated Data, deleted=False)

Explanation: The record is initially created with deleted=False. It's marked as deleted, then the data is updated, and finally, it's undeleted. The final print statement shows the record's state.

Example 2:

Input: record = Record(id=2, data="Some Data")
record.mark_deleted()
record.mark_deleted() # Calling mark_deleted again on a deleted record
record.is_deleted()
print(record)

Output:

True
True
Record(id=2, data=Some Data, deleted=True)

Explanation: The record is marked as deleted, and then mark_deleted() is called again. The deleted attribute remains True.

Example 3: (Edge Case)

Input: record = Record(id=3, data="Edge Case Data")
record.undelete() # Calling undelete on a non-deleted record
record.is_deleted()
print(record)

Output:

False
False
Record(id=3, data=Edge Case Data, deleted=False)

Explanation: The record is initially not deleted. Calling undelete() has no effect, and the record remains undeleted.

Constraints

  • The id attribute should be an integer.
  • The data attribute can be any string.
  • The deleted attribute must be a boolean.
  • The mark_deleted() and undelete() methods should not raise exceptions if called multiple times.
  • The deleted attribute should only ever be True or False.

Notes

Consider using properties or other Pythonic ways to control access and modification of the deleted attribute if you want to enforce stricter validation. Think about how to handle the case where undelete() is called on a record that is already undeleted. Focus on creating a clean and well-documented class.

Loading editor...
python