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?
#11
the input is height and width. if height>width the orientation is portrait, and if width>height - orientation is Landscape. No matter if user calls page_info(height=297, width=210) or page_info(height=210, width = 297) it is A4 size. So, I take height and width and create tuple with first element the bigger of the height/width and second element - the other one. that is p-dimensions that I use as key. Of course I don't order page_size - I don't need to sorted dict. As you know dict is unordered by design up untill 3.6.
I know my dict is not exhaustive for all possible page sizes - it's up the OP to implement full nomenclature if s/he wants to do so. but for page sizes A0-A4 it's OK and if at least one of the dimensions is not standard one it will return unknown size. I have provided an example for 4 possible combinations and all work fine.
Reply
#12
(Sep-25-2017, 06:46 PM)DeaD_EyE Wrote: I can't find anything about height and width which belongs to A0 - A10.
it's the first column of the table in your link...
Reply
#13
page_info(1189, 1)
Output:
{'orientation': 'Portrait', 'size': 'A0'}
I haven't seen that you have as key only an integer. Never trust user input.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#14
That would be the output from the code in post #2 and we were discussing the snippet in #3 that uses tuple as key? Do you care to post also the output from it?
Reply
#15
In snippet #3 you're using tuples. I haven't seen it.

Output:
In [2]: page_info(1189, 1) Out[2]: {'orientation': 'Portrait', 'size': 'Unknown'}
Then the output is right.

What I meant with my first post, is that you have to pay attention of the order in page_sizes.
Quote:With the last example you've to pay attention about the order of the definition for page_sizes.

Your example from #3 with exchanged height and width in page_sizes:
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 = {(841, 1189):'A0', (841, 594):'A1', (594, 420):'A2',
                      (420, 297):'A3', (210, 297):'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 Unknown, orientation is Portrait Page size is Unknown, orientation is Landscape Page size is Unknown, orientation is Portrait Page size is Unknown, orientation is Square
Instead of writing 10 replies, I'll post next time directly how to break your code example ;-)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#16
well, obviously that is not correct - you have changed my page_sizes dict!!!! I really don't understand what you want to prove. And you know very well that I have used tuple in my second snippet, because you said user should keep the order - always highest first. And from there we started this pointless discussion...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Switch case or match case? Frankduc 9 4,518 Jan-20-2022, 01:56 PM
Last Post: Frankduc
  Help: write 'case' with Python ICanIBB 2 1,872 Jan-27-2021, 09:39 PM
Last Post: Larz60+
  How to write switch case statement in Python pyhelp 9 9,215 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