Skip to content

Adding Sagar #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
# software_engineering_essentials
# Software Engineering Principles
## Session #0
1. Launch codespaces
2. Install libraries using requirements.txt - **Dependency Management**
3. Add json in “users” dir and serve locally
and create a new PR (checkout, add, commit, push) - **Git**
6 changes: 6 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Flask==2.3.3
snakeviz==2.2.0
pytest==7.4.2
pytest-cov==4.1.0
yapf==0.40.2
names==0.3.0
77 changes: 77 additions & 0 deletions server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
from flask import Flask
import json, os
from typing import Dict, Any

directory_path = "./users"

app = Flask(__name__)

styles = """
<style>
table {
border-collapse: collapse;
}
tr, td{
border: 1px solid black;
padding: 5px;
text-align: center;
}
body{
align: center
}
</style>
"""

def read_json_files_from_directory(directory_path: str):
""" Read JSON files from provided path and return dict in format of {user_id:user_info}"""
json_data: Dict[str, Any] = {}

try:
# Iterate through the files in the directory
for filename in os.listdir(directory_path):
if filename.endswith('.json'):
file_path = os.path.join(directory_path, filename)
with open(file_path, 'r') as file:
# Read and parse the JSON data
data = json.load(file)
for user_info in data:
json_data[user_info["user_id"]] = user_info
except FileNotFoundError:
print(f"Directory not found: {directory_path}")
return None
except json.JSONDecodeError as e:
print(f"Error decoding JSON: {e}")
return None

return json_data


@app.route('/')
def get_users():
"""Renders a table of all users."""
user_objs = read_json_files_from_directory(directory_path)
response = ""
for person_id in user_objs:
response += f"""<tr><td><a href="/get_user/{person_id}">{person_id}</a></td></tr>"""
response = f"""{styles}<table>{response}</table>"""
return response, 200, {'Content-Type': 'text/html'}


@app.route('/get_user/<string:person_id>', methods=['GET'])
def get_user(person_id):
"""Renders fields of a person with specified person_id."""
response = ""
user_objs = read_json_files_from_directory(directory_path)

if person_id in user_objs:
person_obj = user_objs[person_id]
response += f"<tr><td>User ID</td><td>{person_obj['user_id']}</td></tr>"
response += f"<tr><td>First Name</td><td>{person_obj['first_name']}</td></tr>"
response += f"<tr><td>Last Name</td><td>{person_obj['last_name']}</td></tr>"
response += f"""<tr><td>Friends</td><td>{",".join([f"<a href='/get_user/{p}'>{p}</a>" for p in person_obj['friends']])}</td></tr>"""
response = f"""{styles}<table>{response}</table>"""
return response, 200, {'Content-Type': 'text/html'}
else:
return f"User {person_id} not found."
if __name__ == '__main__':
app.run()
8 changes: 8 additions & 0 deletions users/21bec102.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"user_id": "21bec102",
"first_name": "sagar",
"last_name": "ramani",
"friends": ["21bec085", "21bec100"]
}
]
8 changes: 8 additions & 0 deletions users/ENROLLMENT_ID.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"user_id": "ENROLLMENT_ID",
"first_name": "User_First_Name",
"last_name": "User_Last_Name",
"friends": ["FRIEND_1_ENROLLMENT_ID", "FRIEND_2_ENROLLMENT_ID"]
}
]