-2
\$\begingroup\$
#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)
\$\endgroup\$
3
  • 3
    \$\begingroup\$ Why not a single if statement with if channel_sk in (4053, 4061, 4062, 4066): ? \$\endgroup\$ – ChatterOne Feb 6 '17 at 15:36
  • \$\begingroup\$ @ChatterOne Judging by the 'beginner' tag, my guess is this may not be something the OP is aware of being possible. Note that I stole your suggestion as part of my answer, only to build upon it with additional suggestions that would avoid the OP having to constantly change the conditional itself and pull in the "list" of items that if 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
  • 2
    \$\begingroup\$ While I agree with @Thomas that we appear to be missing some code, adding this now (while the question is answered) goes against the policy of Code Review. Consider posting a follow-up question once you've incorporated the current feedback into your code. \$\endgroup\$ – Mast Feb 6 '17 at 16:22
3
\$\begingroup\$

It doesn't look like you use any of these intermediate values... by removing them,

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"

Can be reduced to:

start_date_time = event[2].strftime("%Y%m%dT%H%M%SZ")

Note: strftime() returns a string, there is no need to explicitly str() it.

\$\endgroup\$
4
\$\begingroup\$

This suggestion is originally from @ChatterOne, and was initially posted in a comment, and then built upon by me.

You can combine the multiple if statements into one conditional, like follows:

if channel_sk in (4053, 4061, 4062, 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]

Building upon the above suggestion from ChatterOne in comments, I would expand on this, and approach this slightly differently. If you expect to have to add more events in the future, you may wish to make it a single variable so you don't have to go code-hunting. Define a variable that is outside the loops and is either global to the entire code or specific to the function itself. Only use one of these two items, based on which approach you want to take:

# If globally accessible at the beginning of your code file, use this:
CHANNELS_TO_HANDLE = (4053, 4061, 4062, 4066) 

# If defining within your functions but outside the loop:
channels = (4053, 4061, 4062, 4066)

Then, your if statement's conditional becomes this. Only use one of these two items, based on which approach you want to take from the variable approach:

# If using the global variable approach:
if channel_sk in CHANNELS_TO_HANDLE:
    ...

# If using the local variable approach:
if channel_sk in channels:
    ....

Either way, you won't always have to change your if statement, you can just change a variable. And if you can pull the list of special-handled channels out of the database or something, or import it as a variable or something via another Python file such as a channel_config.py in the same directory as your current Python code which would have something like this:

CHANNELS_TO_HANDLE = (4053, 4061, 4062, 4066)

... and then a corresponding import at the beginning of your code file that you posted:

import channel_config

.... then you can define the variable as a configuration file item, and not have to keep tweaking the conditional in your code, just change it in that config file. (Then, the if statement just needs to use the CHANNELS_TO_HANDLE approach above.) You can do the same with a database SELECT, and either do it in the config file, or do it early in your code and store the data, and use that in the conditional, if you can store the channel list of which channels need special case handling in the database somewhere.

Regardless of the approach you take, this can help with the code reuse problem.

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.