Real time use case of dataclasses in python

Deepak rkm
3 min readMay 20, 2021

I have been interviewed and was given a problem to solve..

Problem:

where in customer seeks for car information from dealer and the dealer has to return the matched details from the data they have and follow Object Oriented design pattern to solve.

I have Used the below approach to solve .Please check and let me know Your thoughts for other better ways.

Instead of real db or more simplified dictionary ,I have used data classes in python

Data classes are used to store data in runtime ,that you don’t need any real db dbs for your quick solution or prototype

now lets convert the above theory:

Factory_model.py

from dataclasses import dataclass, asdict, astuple

@dataclass
class Point:
maruthi: dict
hyundai: dict
honda: dict

ob = Point({‘price’:100000,’engine’:1.2,’variant’:’base’,’displacement’:34},
{‘price’:1200000,’engine’:1.3,’variant’:’middle’},
{‘price’:1100000,’engine’:1.2,’variant’:’top’})

db = asdict(ob)

Output:

{‘maruthi’: {‘price’: 100000, ‘engine’: 1.2, ‘variant’: ‘base’, ‘displacement’: 34}, ‘hyundai’: {‘price’: 1200000, ‘engine’: 1.3, ‘variant’: ‘middle’}, ‘honda’: {‘price’: 1100000, ‘engine’: 1.2, ‘variant’: ‘top’}}

with the help of dataclass you can add as much as data and keys

pretty part is ,all it takes only run time memory

Now creating the second Dealer class by Dealer_to_user.py

import time
from factory_model import db

class Dealer():

def __init__(self,**kwargs):
self.info = kwargs
self.price = kwargs.get(‘price’,’ensure your are sending as price as key’)
self.engine = kwargs.get(‘engine’,’ensure your are sending as price as engine’)
self.variant = kwargs.get(‘variant’,’ensure your are sending as price as variant’)

def check_model(self):
if len(self.info)>0:
list_of_cars = set()
specs_list= ‘’.join([str(each) for each in self.info.values()])
for car,specs in db.items():
if self.price in specs.values() or self.engine in specs.values() or self.variant in specs.values():
variant= specs.get(‘variant’)
list_of_cars.add(car)

if len(list_of_cars)>0:
print( f”{(‘ and ‘.join(list_of_cars))} found for the given specs { specs_list} “)
else:
print(f’No data for the given {specs_list}’)

else:
return(‘since you havent provided any specs we are fetching all information………\n’)
time.sleep(3)
for k,v in db.items():

print (f’{k}: \n \t{v}\n’)

if __name__ == ‘__main__’:

call = Dealer(price =100000)
call.check_model()
call = Dealer(price =100000232)
call.check_model()

if you run the above the result you get is

maruthi found for the given specs 100000

No data for the given 100000232

since you havent provided any specs we are fetching all information………

maruthi:
{‘price’: 100000, ‘engine’: 1.2, ‘variant’: ‘base’, ‘displacement’: 34}

hyundai:
{‘price’: 1200000, ‘engine’: 1.3, ‘variant’: ‘middle’}

honda:
{‘price’: 1100000, ‘engine’: 1.2, ‘variant’: ‘top’}

[Finished in 3.3s]

--

--

Deepak rkm

proud to be pythonist and aspiring to be sre with AI skills