How to set a value of table attribute into variable in python?

Hi guys,

I’m new with ERPnext and python, i was confuse on how to set a value of table attribute into variable in python?

here is the snippet which i got from monthly_attendance_sheet.py file from erpnext folder:

def get_attendance_list(conditions, filters):
    attendance_list = frappe.db.sql("""select employee, day(att_date) as day_of_month,
        status, time_in , time_out from tabAttendance where docstatus = 1 %s order by employee, att_date""" %
        conditions, filters, as_dict=1)
        
    att_map = {}
    for d in attendance_list:
        att_map.setdefault(d.employee, frappe._dict()).setdefault(d.day_of_month, "")
        att_map[d.employee][d.day_of_month] = d.status
        
    return att_map

as you can see I just add time_in and time_out custom fields to the table, and now trying to get this value from the table and set this value to an attribute, because im going to have the average time in and time out of each employee in each month.

If you need more snippets or even the file, i can email you.
NEED HELP :disappointed_relieved: :disappointed_relieved: :disappointed_relieved:

Regards,

Hi @Luned_Hernando

Where are you stuck?

i want to have a variable that has the value of (time in) attribute and (time out) attribute
example:

x = #value of (time in) such as 13:00:00
y = #value of (time out) such as 15:00:00

this (time in) and (time out) is an attribute of “tabAttendance” table.
looks like the example i gave, erpnext do set their employee attribute value , month and status so that they can print or use it. but i dont understand that code. can you help me?

Monthly Attendance Sheet is a report. The dict att_map seems like it is mapping the table data to Employee and Date for display.

i hv already solve the problem. maybe i can show the solution for the others who needs help:

def get_attendance_list(conditions, filters):
attendance_list = frappe.db.sql(“”“select employee, day(att_date) as day_of_month,
status,time_in, time_out from tabAttendance where docstatus = 1 %s order by employee, att_date”“” %
conditions, filters, as_dict=1)

att_map = {}
for d in attendance_list:
    att_map.setdefault(d.employee, frappe._dict()).setdefault(d.day_of_month, "")
    att_map[d.employee][d.day_of_month] = d.status
    att_map.setdefault(d.employee, frappe._dict()).setdefault("time", frappe._dict()).setdefault(d.day_of_month, "")
    att_map[d.employee]["time"][d.day_of_month] = d.time_out - d.time_in
return att_map

Thanks for replying btw :smile:

2 Likes