0
 if not all(key in payload for key in payloads[template]):
        raise InvalidPayloadException

    if 'order_date' in payload:
        payload['order_date'] = self._get_formatted_date(payload['order_date'])

    if 'payment_date' in payload:
        payload['payment_date'] = self._get_formatted_date(payload['payment_date'])

    if 'shipped_date' in payload:
        payload['shipped_date'] = self._get_formatted_date(payload['shipped_date'])

I have some code that triggers a PDF generation. It accepts a python dict that contains the payload for the PDF.

There are a good few number of dates that need to be displayed in the PDF but not all documents contain all PDFs. I need to format the dates before sending it to the PDF. At the moment my code is a lot of different IF statements to catch all the possible dates and format them in the dict.

Is there a more pythonic way to do it?

user7692855
  • 1,582
  • 5
  • 19
  • 39

4 Answers4

4

Use a loop.

for date_key in ('order_date', 'payment_date', 'shipped_date'):
    if date_key in payload:
        payload[date_key] = self._get_formatted_date(payload[date_key])
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
1

You don't need any of the if statements.

A common methodology in Python is EAFP (Easier to Ask for Forgiveness than Permission) rather than LBYL (Look Before You Leap). Hence you should prepare a tuple or list of keys you expect to be in the payload dict, and use try-except in case one of the keys is missing.

for key in ('order_date', 'payment_date', 'shipped_date'):
    try:
        payload[key] = self._get_formatted_date(payload[key])
    except KeyError:
        print('{} not in payload dict'.format(key))
DeepSpace
  • 78,697
  • 11
  • 109
  • 154
  • 1
    Exception handling is not a conditional so checking if something exists is cleaner. See [here](https://stackoverflow.com/questions/4512557/python-if-key-in-dict-vs-try-except) for more explanation – Ivonet Dec 11 '17 at 11:03
  • Yet is less Pythonic, and OP ask for a Pythonic approach. It also depends what is expected to happen more, an existing key or a missing key. – DeepSpace Dec 11 '17 at 11:05
  • 1
    EAFP is fine when you know exceptions will be, well, exceptional (or when you can have a race condition, which is not the point here). And __NO__, blindly using potentially expensive try/excepts just because someone once said it was "pythonic" when there's a way cheaper solution and no possible race condition is __definitly NOT__ pythonic. – bruno desthuilliers Dec 11 '17 at 11:06
  • EAFP is fine, don't know why people have such a big issue with it. This answer is great since it shows another perspective to the problem. – RoadRunner Dec 11 '17 at 11:36
0

You could created a tuple of date keys and use a for loop.

date_keys = ("order_date", "payment_date", "shipped_date")

for date_key in date_keys:
    if date_key in payload:
        payload[date_key] = self._get_formatted_date(payload[date_key])
ramchauhan
  • 228
  • 2
  • 6
-1

You could create a list of all possible dates to format and iterate over them

dates = [
    "order_date",
    "payment_date",
    "shipped_date"
]

for d in dates:
    if d in payload:
       payload[d] = self._get_formatted_date(payload[d])
Ivonet
  • 2,492
  • 2
  • 15
  • 28