Search⌘ K
AI Features

Solution: Create a Customer Relationship Management System

Explore how to build a Customer Relationship Management system with Python by creating and refactoring Customer and Interaction classes. Understand the use of namedtuples, dataclasses, defaultdicts, queues, priority queues, dictionaries, and sets to manage and analyze customer data effectively.

Create the original structure

Generate the Customer class incorporating id, name, and email attributes. Moreover, add another class named Interaction as well, containing customer_id, interaction_type, and timestamp attributes.

Python 3.10.4
class Customer:
id: int
name: str
email: str
class Interaction:
customer_id: int
interaction_type: str
timestamp: str

Refactor original classes

Now refactor the Customer class with named tuples and the Interaction class with dataclasses that help us to provide a convenient way to define classes with automatically generated methods. Moreover, refactor a dictionary to a defaultdict and use the counter to track the customer interactions.

Python 3.10.4
from typing import NamedTuple
from dataclasses import dataclass
from collections import defaultdict, Counter
# Refactor classes with named tuples
class Customer(NamedTuple):
id: int
name: str
email: str
# Refactor classes with dataclasses
@dataclass
class Interaction:
customer_id: int
interaction_type: str
timestamp: str
# Refactor a dictionary to a defaultdict
customer_data = defaultdict(list)
customer_data[1].append(Interaction(1, "Call", "2022-01-01"))
customer_data[1].append(Interaction(1, "Email", "2022-01-02"))
# Use Counter to track interaction frequency
interaction_counter = Counter([interaction.interaction_type for interactions in customer_data.values() for interaction in interactions])

Code explanation

Here’s the explanation of the code written above:

...