Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.idea/
*.png
*.jpg
*.gif
*.txt
*.csv
*.pdf
**/__pycache__/
55 changes: 55 additions & 0 deletions READMe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Package Measurement Conversion

## Overview
This Measurement Conversion application enables users to input a string of characters, and receive the output in a JSON format. The inputted string will be transformed into a list of the total values of measured inflows. The application can be accessed through the RESTful API endpoint. The application is constructed with scalability and extensibility in focus, allowing for modifications and expansions.

## Features
- **Sequence Input Handling:** Accepts a sequence of characters and underscores as input for conversion.
- **Efficient Conversion Algorithms:** Implements efficient algorithms for converting the input sequence into a list of measurements.
- **Clear and adaptable:** Easily modified and extended to include additional functionalities.

## Installation
- Clone the following url in your command prompt: https://github.com/sarah-alshukaily/PackageMeasurementConversionAPI.git

### Prerequisites
- Python 3.x
- CherryPy

### Setup
1. Clone the repository (see the Installation section above).
2. Install required Python packages from requirements.txt:
```pip install -r requirements.txt```
## Usage
- Get a specific string:

Default: http://localhost:8080/convert?user_input=<enter_string_here>
Specific port: http://localhost:<enter_port>/convert?user_input=<enter_string_here>

- Get history of all conversions:

Default: http://localhost:8080/get_history
Specific port: http://localhost:<enter_port>/get_history

### Running the Program
- **Script**:

Default: ```python main_app.py```

Specific port: ```python main_app.py <enter_port>```
- **Access URL**:

Default: http://localhost:8080/

Specific port: http://localhost:<enter_port>/

## Contributing
Contributions to this project are prohibited due to the course restrictions.

## License
This project is licensed under the MIT License.

## Contact
For any queries, please contact [email protected]

## Acknowledgements
Project by Sara Al Shukaili
Empty file added controller/__init__.py
Empty file.
95 changes: 95 additions & 0 deletions controller/conversion_controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import json
import cherrypy
from services.conversion_service import Conversion
from models.database_initializer import init_db
from utilis.database_operations import ConversionDatabaseOperations
from models.sequence_output import SequenceOutput


class ConversionController:

class MeasurementConversion:

def __init__(self):
self.converter = Conversion() # Instantiate the Conversion class

def convert_input(self, user_input):
"""
A function to convert the user input to the list of values
:param user_input: User input string
:return: Converted result string or list
"""
converted_result = self.converter.converted_string(user_input) # Use the converted_string method
return converted_result

def __init__(self):
self.controller = ConversionController.MeasurementConversion()
self.conversion = Conversion()
self.dbops = ConversionDatabaseOperations()

@staticmethod
def get_db():
"""
A function that retrieves the database connection object, and initializing it if it is not present
:return: A database connection object
"""
if not hasattr(cherrypy.thread_data, 'conn'):
cherrypy.thread_data.conn = init_db()
return cherrypy.thread_data.conn

@cherrypy.expose
@cherrypy.tools.json_out()
def convert(self, user_input=None):
"""
A function that exposes an endpoint to convert user's input to a converted list of values as JSON
:param user_input: A sequence of characters that is None by default
:return: A JSON response containing the conversion result, status, and error message
"""
res_msg = {"status": "SUCCESS", "err_msg": "", "result": ""}
m = SequenceOutput(user_input, None, None, None)
try:
if not user_input:
user_input = cherrypy.request.params.get('user_input')
if user_input is not None:
converted_result = self.conversion.converted_string(user_input)
m.user_input = user_input
m.converted_result = str(converted_result)
m.status = "SUCCESS"
m.error_message = ""
self.dbops.save_to_db(m, converted_result)
res_msg["result"] = converted_result
else:
res_msg["status"] = "FAIL"
res_msg["result"] = "Missing user_input parameter"
except Exception as e:
m.user_input = user_input
m.converted_result = ""
m.status = "ERROR"
m.error_message = "ERROR"
self.dbops.save_to_db(m, converted_result="")
res_msg["status"] = "ERROR"
res_msg["result"] = str(e)

return res_msg

@cherrypy.expose
@cherrypy.tools.json_out()
def get_history(self):
"""
A function that exposes an endpoint to retrieve conversions history
:return: A JSON response containing conversion history
"""
res_msg = {"status": "SUCCESS", "err_msg": "", "history": []}
try:
with self.get_db() as conn:
cursor = conn.cursor()
# Retrieve conversion history from SQLite
cursor.execute('''SELECT * FROM conversions''')
history = cursor.fetchall()
res_msg["history"] = [
{"user_input": row[1], "converted_result": row[2], "status": row[3], "error_message": row[4]} for
row in history]
except Exception as e:
res_msg["status"] = "ERROR"
res_msg["err_msg"] = str(e)
return res_msg
Loading