We have some customer records in a text file (customers.txt) -- one customer per line, JSON lines formatted. We want to invite any customer within 100km of our Dublin office for some food and drinks on us. Write a program that will read the full list of customers and output the names and user ids of matching customers (within 100km), sorted by User ID (ascending).
I have built a configurable solution. Here by simply changing the values in the config.json file, we can modify the behaviour of the code.
Instead of hard-coding the parameters, the main.py
script initializes the Invite
object with instance attributes from the config file and the Invite
object takes it from there.
This structure be amazingly helpful, flexible and extensible if we need to run multiple programs. For example, we can configure two programs, one where we invite customers within 100km and the other where we invite a different set of customers within a distance of 50km.
In the root of the project there are the following files
- main.py - This is the main python script used to run the program.
- customer.py - This file contains the customer model.
- invite.py - This file contains a class called Invite which contains the bulk of running knowledge.
- readers.py - This file contains the Reader interface and implementations: HttpReader and FileReader.
- utils.py - This file contains utility functions Eg: writing to file, calculating distance.
- config.json - This is the configuration file. It contains properties of the invite class like the intercom coordinates, distance threshold and i/o file paths.
The test folder contains python scripts performing tests on the various components of the program.
unittest
has been used to perform these tests.
This folder contains the input file customers.txt along with some files for the tests and output files.
We can find the output files
- inviteList.txt - text file with json of each customer invited.
- inviteList.csv - CSV file with customer information as rows.
in the resources/out
folder.
This problem has been solved using the language Python. If you are using a Mac or Linux, it should already be pre-installed. You can check this by entering:
python --version
on the command line. This code was written using Python 3, so please make sure the version runnning on your computer is 3 or above if you intend to test it. There is a good article here on how to do so.
There are no additional packages required to run this script.
To run the program :
python main.py config.json
To run tests on the program:
python -m unittest discover test