How can I refactor this code to be more concise?

tdkr

I'm using Python to extract some data from the Google Analytics Core Reporting API. I've managed to use functions to make different calls to the API (below are just two examples) but I'm wondering how can I refactor this to make it even shorter? There's quite a lot of duplicate code still.

def get_pvs(service, profile_id, start_date, end_date, type, data):
    if type == "url":
        return service.data().ga().get(
            ids = 'ga:' + profile_id,
            start_date = start_date,
            end_date = end_date, 
            metrics = 'ga:pageviews', 
            dimensions = 'ga:pagePath',   
            filters = 'ga:pageviews!=0',
            sort = '-ga:pageviews',
            max_results = '10000').execute()
    elif type == "author":
        return service.data().ga().get(
            ids = 'ga:' + profile_id,
            start_date = start_date,
            end_date = end_date, 
            metrics = 'ga:pageviews', 
            # Post Author
            dimensions = 'ga:dimension2',
            sort = '-ga:pageviews',
            max_results = '100').execute()   
Martijn Pieters

Create a dictionary with type-specific arguments, then apply that with **kw:

def get_pvs(service, profile_id, start_date, end_date, type, data):
    if type == 'url':
        kwargs = {
            'dimensions': 'ga:pagePath',
            'filters': 'ga:pageviews!=0',
            'maxresults': 100
        }
    elif type == 'author':
        kwargs = {
            'dimensions': 'ga:dimension2',
            'max_results': '100'
        }
    return service.data().ga().get(
        ids = 'ga:' + profile_id,
        start_date = start_date,
        end_date = end_date, 
        metrics = 'ga:pageviews', 
        sort = '-ga:pageviews',
        **kwargs).execute()   

I left the common arguments in place. if type can have more values, then you probably need to add a else: return None too.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I refactor my kotlin code and make it more clear

From Dev

How can I refactor / make this code more pythonic?

From Dev

How can I make this method more concise?

From Dev

How can i refactor these lines of code?

From Dev

Parsing: how can I generate a more concise AST?

From Dev

How can I make a for-loop pyramid more concise in Python?

From Dev

How can I make this match expression more concise?

From Dev

how can I build this method in a more concise way?

From Dev

How can I refactor these simple functions and make them more DRY?

From Dev

How can I refactor a simple class member to a more complex type?

From Dev

How can I refactor these simple functions and make them more DRY?

From Dev

How can I refactor this method using anonymous projection to be more generic?

From Dev

Can this Scala code snippet be made more concise?

From Dev

How can I use ESLint to refactor/restyle code in webstorm

From Dev

How can I refactor this code that finds the end of Unix time?

From Dev

How can I use the Factory pattern to refactor my Java code?

From Dev

How can I store key presses into variables to refactor this code?

From Dev

How to Refactor this code to not nest more than 3 "if"

From Dev

How can I swap this JavaScript nested For Loop for something more clear and concise?

From Dev

Can I refactor this code in any way in javascript?

From Dev

How can I write this code more elegantly?

From Dev

How can I make this code more elegant?

From Dev

How can I add more classes for this code?

From Dev

Can I make this if statement with lots of ORs more concise?

From Dev

How could one improve this code so that it is more concise and efficient?

From Dev

Writing more concise code in go

From Dev

Code reusability: how to refactor checks to be more readable and reusable, separating responsabilities?

From Dev

Code reusability: how to refactor checks to be more readable and reusable, separating responsabilities?

From Dev

How can I refactor a set of ugly if statements?

Related Related

  1. 1

    How can I refactor my kotlin code and make it more clear

  2. 2

    How can I refactor / make this code more pythonic?

  3. 3

    How can I make this method more concise?

  4. 4

    How can i refactor these lines of code?

  5. 5

    Parsing: how can I generate a more concise AST?

  6. 6

    How can I make a for-loop pyramid more concise in Python?

  7. 7

    How can I make this match expression more concise?

  8. 8

    how can I build this method in a more concise way?

  9. 9

    How can I refactor these simple functions and make them more DRY?

  10. 10

    How can I refactor a simple class member to a more complex type?

  11. 11

    How can I refactor these simple functions and make them more DRY?

  12. 12

    How can I refactor this method using anonymous projection to be more generic?

  13. 13

    Can this Scala code snippet be made more concise?

  14. 14

    How can I use ESLint to refactor/restyle code in webstorm

  15. 15

    How can I refactor this code that finds the end of Unix time?

  16. 16

    How can I use the Factory pattern to refactor my Java code?

  17. 17

    How can I store key presses into variables to refactor this code?

  18. 18

    How to Refactor this code to not nest more than 3 "if"

  19. 19

    How can I swap this JavaScript nested For Loop for something more clear and concise?

  20. 20

    Can I refactor this code in any way in javascript?

  21. 21

    How can I write this code more elegantly?

  22. 22

    How can I make this code more elegant?

  23. 23

    How can I add more classes for this code?

  24. 24

    Can I make this if statement with lots of ORs more concise?

  25. 25

    How could one improve this code so that it is more concise and efficient?

  26. 26

    Writing more concise code in go

  27. 27

    Code reusability: how to refactor checks to be more readable and reusable, separating responsabilities?

  28. 28

    Code reusability: how to refactor checks to be more readable and reusable, separating responsabilities?

  29. 29

    How can I refactor a set of ugly if statements?

HotTag

Archive