write a complex python code
- User
write a complex python code
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
write a complex python code
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Certainly! Below is a Python code example that demonstrates a variety of concepts including object-oriented programming, exception handling, file I/O, list comprehensions, and usage of built-in libraries. This script will parse a text file containing JSON data into Python objects, compute some statistics from the data, and then output these statistics to another file.
Let's say we have a list of employees represented in JSON format in a file. This program will read that file, calculate the average age of employees, count by department, and then write a summary of these statistics to an output file.
First, ensure you have a file named employees.json in the same directory as this script with content similar to the following:
[
{"name": "John Doe", "age": 30, "department": "Sales"},
{"name": "Jane Smith", "age": 25, "department": "Marketing"},
{"name": "Bob Brown", "age": 45, "department": "Sales"}
]
import json
from collections import defaultdict
class Employee:
def __init__(self, name, age, department):
self.name = name
self.age = age
self.department = department
@classmethod
def from_dict(cls, data):
return cls(**data)
class EmployeeStats:
def __init__(self, employees):
self.employees = employees
def average_age(self):
return sum(employee.age for employee in self.employees) / len(self.employees)
def count_by_department(self):
dept_counts = defaultdict(int)
for employee in self.employees:
dept_counts[employee.department] += 1
return dict(dept_counts)
def read_employees_from_file(filename):
try:
with open(filename, 'r') as file:
data = json.load(file)
return [Employee.from_dict(employee) for employee in data]
except FileNotFoundError:
print(f"Error: The file {filename} was not found.")
return []
except json.JSONDecodeError:
print("Error: There was an issue decoding the JSON data.")
return []
def write_statistics_to_file(employees, filename):
stats = EmployeeStats(employees)
summary = {
'average_age': stats.average_age(),
'count_by_department': stats.count_by_department(),
}
with open(filename, 'w') as file:
json.dump(summary, file, indent=4)
def main():
employees = read_employees_from_file('employees.json')
if employees:
write_statistics_to_file(employees, 'stats_output.json')
print("Statistics written successfully.")
else:
print("No employee data to process.")
if __name__ == '__main__':
main()
This code does the following:
Employee class to model the employee data.EmployeeStats class to calculate statistics on a list of Employee objects.employees.json and deserializes the JSON data into Employee objects.stats_output.json, in JSON format.To run this program, ensure you have Python installed and then execute the script from the terminal or command prompt. Ensure you have the employees.json file in the same directory as your script. After running, you should see a new file stats_output.json containing the calculated statistics.