#Get the service key and event id
for channel_sk in channel_sk_list:
auto_cur.execute(
"SELECT service_key, event_id, start_date_time, duration, prog_name, series_id "
"FROM autodb.ssr_events "
"WHERE (ca_details='FTA' OR ca_details='SUB') "
"AND service_key={} "
"AND recordable_event='1' "
"AND start_date_time <= CURRENT_TIMESTAMP() ORDER BY start_date_time "
"DESC LIMIT 1 ".format(channel_sk)
)
ssr_events = auto_cur.fetchall()
if channel_sk == 4053:
for event in ssr_events:
print event
service_key = event[0]
event_id = event[1]
get_start_date_time = event[2]
get_date = get_start_date_time.strftime('%Y%m%d')
get_time = get_start_date_time.strftime('%H%M%S')
start_date_time = str(get_date)+"T"+str(get_time)+"Z"
duration = '0'+str(event[3])
program_name = event[4]
series_id = event[5]
if channel_sk == 4066:
for event in ssr_events:
print event
service_key = event[0]
event_id = event[1]
get_start_date_time = event[2]
get_date = get_start_date_time.strftime('%Y%m%d')
get_time = get_start_date_time.strftime('%H%M%S')
start_date_time = str(get_date)+"T"+str(get_time)+"Z"
duration = '0'+str(event[3])
program_name = event[4]
series_id = event[5]
if channel_sk == 4062:
for event in ssr_events:
print event
service_key = event[0]
event_id = event[1]
get_start_date_time = event[2]
get_date = get_start_date_time.strftime('%Y%m%d')
get_time = get_start_date_time.strftime('%H%M%S')
start_date_time = str(get_date)+"T"+str(get_time)+"Z"
duration = '0'+str(event[3])
program_name = event[4]
series_id = event[5]
if channel_sk == 4061:
for event in ssr_events:
print event
service_key = event[0]
event_id = event[1]
get_start_date_time = event[2]
get_date = get_start_date_time.strftime('%Y%m%d')
get_time = get_start_date_time.strftime('%H%M%S')
start_date_time = str(get_date)+"T"+str(get_time)+"Z"
duration = '0'+str(event[3])
program_name = event[4]
series_id = event[5]
I am trying to improve my code quality and not to repeat the same code. How can I achive this in a pythonic way? For example, I am writing this code for many channels:
for event in ssr_events:
print event
service_key = event[0]
event_id = event[1]
get_start_date_time = event[2]
get_date = get_start_date_time.strftime('%Y%m%d')
get_time = get_start_date_time.strftime('%H%M%S')
start_date_time = str(get_date)+"T"+str(get_time)+"Z"
duration = '0'+str(event[3])
program_name = event[4]
series_id = event[5]
So, I do not want to repeat this code block many times.
for ip in ip_address:
print ip
# Step 4 record SKY atlantic and wait 1 minute
filename = full_path+"\\"+"log"+str(ip)+".txt"
arg_list = []
action ="SetRecording"
arg_list.append(upnp_path)
arg_list.append(' --action=')
arg_list.append(action)
arg_list.append(' --ip=')
arg_list.append(ip)
arg_list.append(' --serviceKey=4066')
# arg_list.append(service_key)
arg_list.append(' --eventId=')
arg_list.append(event_id)
arg_list.append(' --startDateTime=')
arg_list.append(start_date_time)
arg_list.append(' --duration=')
arg_list.append(duration)
arg_list.append(' --seriesId=')
arg_list.append(series_id)
if
statement withif channel_sk in (4053, 4061, 4062, 4066):
? \$\endgroup\$ – ChatterOne Feb 6 '17 at 15:36if channel_sk in
would check against from either a variable or an external config file variable definition. (I did credit you for that part I stole in my answer, though) \$\endgroup\$ – Thomas Ward Feb 6 '17 at 15:56