Python Forum
Is there a better way to write this case scenario?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is there a better way to write this case scenario?
#3
I was thinking further and realised that the function (and your if/else construct to that matter) does not cover all possibilities. Here is more 'universal" one that takes into account when height and width is not a standard one, e.g. cases like height 297 (standard) and width=100, which is not a standard one.

def page_info(height, width):
    if height == width:
        p_size, p_orientation = 'Unknown', 'Square'
    else:
        p_dimensions = tuple(sorted([height, width], reverse=True))
        page_sizes = {(1189, 841):'A0', (841, 594):'A1', (594, 420):'A2',
                      (420, 297):'A3', (297, 210):'A4'}
        page_orientations = {True:'Portrait', False:'Landscape'}
        p_size = page_sizes.get(p_dimensions, 'Unknown')
        p_orientation = page_orientations[height>width]
    return {'size':p_size, 'orientation':p_orientation}

for height, width in ((1189, 841), (210, 297), (200, 100), (300, 300)):
    p_info = page_info(height, width)
    print("Page size is {size}, orientation is {orientation}".format(**p_info))
Output:
Page size is A0, orientation is Portrait Page size is A4, orientation is Landscape Page size is Unknown, orientation is Portrait Page size is Unknown, orientation is Square
Reply


Messages In This Thread
RE: Is there a better way to write this case scenario? - by buran - Sep-24-2017, 06:37 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Switch case or match case? Frankduc 9 7,734 Jan-20-2022, 01:56 PM
Last Post: Frankduc
  Help: write 'case' with Python ICanIBB 2 2,578 Jan-27-2021, 09:39 PM
Last Post: Larz60+
  How to write switch case statement in Python pyhelp 8 13,133 Nov-11-2018, 08:53 PM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020