...
/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: intname: stremail: strclass Interaction:customer_id: intinteraction_type: strtimestamp: 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 NamedTuplefrom dataclasses import dataclassfrom collections import defaultdict, Counter# Refactor classes with named tuplesclass Customer(NamedTuple):id: intname: stremail: str# Refactor classes with dataclasses@dataclassclass Interaction:customer_id: intinteraction_type: strtimestamp: str# Refactor a dictionary to a defaultdictcustomer_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 frequencyinteraction_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: