-4
\$\begingroup\$

I know it's not that bad, but I can't help but wonder if there is a way to make this DRY-er.

s = self.start
e = self.end
if s.x > e.x:
    s.x, e.x = e.x, s.x
if s.y > e.y:
    s.y, e.y = e.y, s.y
\$\endgroup\$
2
  • 4
    \$\begingroup\$ The current question title, which states your concerns about the code, applies to too many questions on this site to be useful. The site standard is for the title to simply state the task accomplished by the code. Please see How to Ask for examples, and revise the title accordingly. \$\endgroup\$
    – Jamal
    Commented Mar 4, 2017 at 3:41
  • \$\begingroup\$ What is repeating there? \$\endgroup\$
    – t3chb0t
    Commented Mar 4, 2017 at 9:26

2 Answers 2

3
\$\begingroup\$

You basicaly want to sort your data, you may be interested in sorted then:

start = self.start
end = self.end
start.x, end.x = sorted((start.x, end.x))
start.y, end.y = sorted((start.y, end.y))
\$\endgroup\$
0
1
\$\begingroup\$

You can make use of getattr() and setattr() built-in functions, handling any potential number of attributes to swap:

start = self.start
end = self.end

attrs = ["x", "y"]
for attr in attrs:
    start_attr_value, end_attr_value = getattr(start, attr), getattr(end, attr)
    if start_attr_value > end_attr_value:
        setattr(start, attr, end_attr_value)
        setattr(end, attr, start_attr_value)

Also note the more explicit start and end variable names in place of s and e. I've abbreviated attribute as attr, but you may go for the full word if you prefer.


You may then extract this into a separate method/function for reusability and readability:

def swap_attributes(start, begin, attrs):
    """Meaningful docstring here."""
    for attr in attrs:
        start_attr_value, end_attr_value = getattr(start, attr), getattr(end, attr)
        if start_attr_value > end_attr_value:
            setattr(start, attr, end_attr_value)
            setattr(end, attr, start_attr_value)

Usage:

swap_attributes(self.start, self.begin, attrs=["x", "y"])

Or may be merge_ranges would be a better name for the function (depending on the actual problem you are solving with it).

\$\endgroup\$
4
  • \$\begingroup\$ The code is about the same size for both my way of doing and yours -- what do you think is the best way to do it? EDIT: Mine is smaller by 2 lines, actually. \$\endgroup\$
    – Matias K
    Commented Mar 4, 2017 at 1:43
  • 1
    \$\begingroup\$ @BleedingCatz it is important to understand that unless we are doing the "code golf" here, instead of trying to make the code shorter sacrificing other things, we should strive for better readability, reusability, modularity, follow principles like "DRY" (thumbs up for the question!), follow the "Zen of Python" and the "PEP8" standards making the code Pythonic. \$\endgroup\$
    – alecxe
    Commented Mar 4, 2017 at 1:47
  • \$\begingroup\$ @BleedingCatz let's though wait for other ideas, may be there is a better way. Thanks! \$\endgroup\$
    – alecxe
    Commented Mar 4, 2017 at 1:48
  • \$\begingroup\$ That's a lot more code than the original, though. \$\endgroup\$ Commented Mar 4, 2017 at 7:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.