0

Does it make sense to organize screen resolutions?

I write a lot of selenium tests, often which I need to choose a resolution for the browser. I ended up creating a dictionary with some of the more common resolutions I need to test, but I've never been happy with it.

    DIMENSIONS = {
        # 4:3
        "1024": (1024, 768),
        "1280": (1280, 960),
        "1600": (1600, 1200),
        "1920": (1920, 1440),
        # 16:9
        "720": (1280, 720),
        "1080": (1920, 1080),
        "1440": (2560, 1440),
        "2160": (3840, 2160),  # 4k
        "4320": (7680, 4320),  # 8k
    }

Issues I have with this:

  • references to a specific size requires using a string since variables in python cannot start with a number.
  • dictionary lookups don't provide nice code traceability.

I figure I could use a letter in front of keys (e.g. R_1080) and then create something like an Enum or a dataclass but for some reason that didn't sit right with me either.

I can't decide if I'm overthinking things or if there is clearly another approach.

Is there a better way to organize resolutions? Should I bother?

6
  • 1
    No idea if there is a better way, but I can't see a downside with Enums for each resolution name. That would also enable you to safely and easily integrate over them. Commented Sep 6, 2023 at 17:02
  • 2
    How many places in your code are you referencing the dictionary keys? If this is limited then I wouldn't worry about this problem at all. Commented Sep 6, 2023 at 17:09
  • @PeterM I had the same thought. My trepidation is what to name a bunch of resolutions if not by the numbers. And if the numbers the the only obvious way to reference resolutions, should I even bother? In some way, this post is a check against that assertion; to see if perhaps there is some way to catalog them without losing the the "meta" info. Commented Sep 7, 2023 at 20:06
  • @MarcelWilson Naming the enums after the resolutions means your IDE of choice will know when you screw up a name (EG you type R_1025 instead of R_1024) and the mistake will be found at compile time. Whereas if you use a string your dictionary will simply not return a value (EG you type "1025" instead of "1024") and that will only be found at run time. Commented Sep 7, 2023 at 20:12
  • @GregBurghardt it's more than a couple, but just barely. Plus they aren't all in the same area of the code. I originally justified the use of the dictionary because it was pretty limited usage. My concern is that limited usage will likely grow a bit as more devices and screen sizes are added. This is indeed one of those items that I could leave well enough alone and be fine with it. But I figured it was worth investigating a little. Commented Sep 7, 2023 at 20:12

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.