Search code examples
pythonpython-3.xgspread

How Can I Make this Python Code Better/More Elegant?


I wrote a script in Python to query the RESTful API of my company's Jira instance for certain information to be uploaded to a Google Doc. Admittedly, I'm not a professional programmer and am still an amateur at best. How can I clean this code up and make it more Pythonic and elegant?

cell = 2

for issue in issues:
    title = issue.fields.customfield_xxxx
    first_name = issue.fields.customfield_xxxx
    last_name = issue.fields.customfield_xxxx
    email = issue.fields.customfield_xxxx
    username = first_name[0] + last_name
    wks.update_acell('A{}'.format(cell), '{}'.format(first_name))
    wks.update_acell('B{}'.format(cell), '{}'.format(last_name))
    wks.update_acell('C{}'.format(cell), '{}'.format(title))
    wks.update_acell('I{}'.format(cell), '{}'.format(email))
    wks.update_acell('E{}'.format(cell), '{}'.format(
       username + "@company.com"))
    wks.update_acell('F{}'.format(cell), '{}'.format(username))
    wks.update_acell('H{}'.format(cell), '{}'.format(
       first_name + " " + last_name))
   wks.update_acell('G{}'.format(cell), '{}'.format(
       first_name + " " + last_name))
   wks.update_acell('J{}'.format(cell), '{}'.format(x))

cell += 1

Solution

  • For starters you could use a for loop to clean up your code and save some typing

        cellfields = [['a',first_name],['b',last_name]['c',title]] ... etc
        for fields in cellfields:
            wks.update_acell(fields[0] + str(cell), fields[1])
    

    sorry for the poor variable naming. :/