Retrieve a specific employee by their unique identifier. The response includes all commuting periods associated with the employee, allowing you to see their complete commuting history and CO2e calculations.
from datetime import datedef get_annual_emissions(employee_id, year): """Calculate employee's emissions for a specific year""" response = requests.get( f"https://api.dcycle.io/v1/employees/{employee_id}", headers=headers ) employee = response.json() total_co2e = 0 for period in employee.get('periods', []): # Check if period overlaps with the year start = date.fromisoformat(period['start_date']) end = date.fromisoformat(period['end_date']) year_start = date(year, 1, 1) year_end = date(year, 12, 31) if start <= year_end and end >= year_start: # Period overlaps with the year total_co2e += period['co2e'] return total_co2eemissions_2024 = get_annual_emissions( "550e8400-e29b-41d4-a716-446655440000", 2024)print(f"2024 Emissions: {emissions_2024} kg CO2e")
Understand which transport modes an employee uses:
Copy
def analyze_transport_modes(employee_id): """Analyze transport modes used by employee""" response = requests.get( f"https://api.dcycle.io/v1/employees/{employee_id}", headers=headers ) employee = response.json() transport_summary = {} for period in employee.get('periods', []): transport = period['transport_type'] if transport not in transport_summary: transport_summary[transport] = { 'periods': 0, 'total_co2e': 0 } transport_summary[transport]['periods'] += 1 transport_summary[transport]['total_co2e'] += period['co2e'] print(f"Transport analysis for {employee['name']}:") for transport, data in transport_summary.items(): print(f" {transport}: {data['periods']} periods, {data['total_co2e']:.1f} kg CO2e") return transport_summaryanalyze_transport_modes("550e8400-e29b-41d4-a716-446655440000")