Python Forum

Full Version: Is there a better way to write this case scenario?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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.
(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...
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.
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?
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 ;-)
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...
Pages: 1 2