Defining Versatile Python Functions: Eliminate Repetitive Code

If you find yourself writing a lot of repetitive Python code, it is an excellent exercise to consolidate with open-ended functions

Tom Sullivan
Código Ecuador
3 min readNov 19, 2019

--

A female project manager leading meeting, points to wall of post-its. Two women and two men sit at a table with laptops.
Photo by You X Ventures on Unsplash

A month ago, I began working with another person on the team at my job putting together a Middle East news roundup for Jadaliyya. Another person using the news generator meant that I should make it easier to learn and understand. Thus, I decided to shorten the codebase and reduce repetition. This post will outline some of the decisions I made to reduce repetitive code, in the hope it can be illuminating for your projects as they reach a higher stage of maturity.

When we are working on an application by ourselves, we can write it however we want as long as it will run. The reason we have programming conventions is, I think because other programmers will be looking at the code we write. Sometimes, that other programmer ends up being you. The roundup generator was the first major project I undertook while programming, and revising it required a great deal of care.

Repetitive code might cause confusion

If I am going to be working on something with someone else, it should be easy to update. The roundup generator app’s codebase was very repetitive. Every single action that was taken to scrape an article, update a field, something or save it to the database involved its function. If I wanted to update an article’s author or publisher, it would use different functions to perform operations that were mostly the same except for the field that they were targeting.

Take a look at this example. The first function updates an article’s name, while the second updates a description. Notice anything different? I don’t, except for the actual field.

def update_article_name(article_id, new_article_name):
u = update(table).where(table.c.articleID == article_id)
u = u.values(name=new_article_name)
result = connection.execute(u)
print(result.rowcount)
def update_article_description(article_id, new_description):
u = update(table).where(table.c.articleID == article_id)
u = u.values(description=new_description)
result = connection.execute(u)
print(result.rowcount)

One need not waste space in writing code

I looked at each function carefully, determined to write a piece of code no more times than was necessary. The changes would be feature-agnostic, meaning that the user would notice no apparent differences with the code. The function calls must also be able to distinguish between the different types of queries. So I created a new single update function that combines all of these various fields we can update into a single command.

def update_article(article_id, new_value, update_type=None):
u = update(dal.table).where(dal.table.c.articleID== article_id)
if update_type == None:
raise Exception('Update type not specified')
elif update_type == 'name':
u = u.values(name=new_value)
elif update_type == 'description':
u = u.values(description=new_value)
elif update_type == 'author':
u = u.values(author=new_value)
elif update_type == 'publication':
u = u.values(publication = new_value)
elif update_type == 'category_id':
u = u.values(categoryID=new_value)
elif update_type == 'date':
u = u.values(date=new_value)
else:
print('Invalid update type')
return
result = dal.connection.execute(u)
print(result.rowcount)

I replaced all of the functions that update articles and categories with a single command that updates everything on the database side. The update_type parameter identifies the type of update. Then every kind of update has a conditional if statement. The great thing about this function is that it supports updating multiple fields at the same time.

The fundamental principle for consolidating functions is to examine the purpose of the function and the parameters. If you find yourself writing a lot of repetitive Python code, it is an excellent exercise to consolidate with open-ended functions.

You can read more about the Python news generator here

--

--