...

/

Solution: Create a Customer Relationship Management System

Solution: Create a Customer Relationship Management System

Learn how to implement the coded solution of the customer relationship management system using Python data structures.

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.

Press + to interact
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.

Press + to interact
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:

...