6

This is about this answer: https://stackoverflow.com/a/58327293/1092820

It was originally a very distinct answer:

You could subclass Formatter and then it is fairly easy:

import string
class PartialFormatter(string.Formatter):
    def __init__(self, missing='~~', bad_fmt='!!'):
        self.missing, self.bad_fmt=missing, bad_fmt

    def get_field(self, field_name, args, kwargs):
        # Handle a key not found
        try:
            val=super(PartialFormatter, self).get_field(field_name, args, kwargs)
            # Python 3, 'super().get_field(field_name, args, kwargs)' works
        except (KeyError, AttributeError):
            val=None,field_name 
        return val 

    def format_field(self, value, spec):
        # handle an invalid format
        if value==None: return self.missing
        try:
            return super(PartialFormatter, self).format_field(value, spec)
        except ValueError:
            if self.bad_fmt is not None: return self.bad_fmt   
            else: raise

d1={'A':'x', 'B':'y', 'C':'{A}_foo', 'D':'bar_{B}'}
d2={}

fmt=PartialFormatter()
for k,v in d1.items():
    if '{' in v:
        d2[k]=fmt.format(v,**d1)
    else:
        d2[k]=v    

>>> d2
{'A': 'x', 'B': 'y', 'C': 'x_foo', 'D': 'bar_y'}

Then I posted my own answer a minute later:

With a list comprehension on d.items, we can apply val.format(**d) on each value to interpolate the values with the dict itself:

>>> d = {'A':'x', 'B':'y', 'C':'{A}_foo', 'D':'bar_{B}'}
>>> o = dict([ (key, val.format(**d)) for key,val in d.items() ])
>>> print (o)
{'A': 'x', 'B': 'y', 'C': 'x_foo', 'D': 'bar_y'}

Then 7 minutes afterwards, the author of the first answer edited their answer to substantially change it into a near exact, but unintentional, duplicate of my answer:

Just use Python string formatting and expand the d1 dict as the arguments:

d1={'A':'x', 'B':'y', 'C':'{A}_foo', 'D':'bar_{B}'}
d2={k:v.format(v,**d1) for k,v in d1.items()}

>>> d2
{'A': 'x', 'B': 'y', 'C': 'x_foo', 'D': 'bar_y'}

Since the author actually decreased the level of contribution their answer made (from being a distinct answer to being noise), is the correct course of action here to roll back the edit?

I do think it was unintentional so I don't know if flagging is the right answer here.

1
  • This questions looks as if it could occur quite often. It's likely that it was asked and answered already, but I cannot find a suitable duplicate right now. I personally think there is no reason to keep exactly identical answers (intentional or unintentional), but if they differ even only by a tiny bit, the case might be less clear. A comment below the duplicate answer and ultimately a custom mod flag should be the preferred course of action. An edit is not necessary. Commented Oct 11, 2019 at 9:44

2 Answers 2

4

Thank you for bringing this up on meta versus just unilaterally rolling it back a third time. Thank you also for giving the benefit of the doubt and calling this unintentional. Honestly, I did not read nor consider answers posted after my own answer and it was unintentional.

In the very first version of that answer, I quickly repurposed an older piece of code that I wrote 7 years ago.

Note (in the v1 of the answer) the loop and assignment to a second d2 dict:

fmt=PartialFormatter()
for k,v in d1.items():
    if '{' in v:
        d2[k]=fmt.format(v,**d1)
    else:
        d2[k]=v    

As I tried to simplify that loop and while testing I tried:

d2={k:v.format(v,**d1) if ('{' in v) else (k:v) for k,v in d1.items()}

Which is a syntax error. I then realized I could do:

d2={k:v.format(v,**d1) for k,v in d1.items()}

and eliminate the entire subclassing (which is somewhat too advanced for this particular question anyway.)

I could argue that Ruzihm's answer was actually a simplification yet duplicate of my v1 answer. The same core loop and the same fmt.format(val,**dict) functionality exists in all three answers. That is the 'core' of answering this question. In an intellectual property dispute, that view would likely prevail. My v1 version has more graceful error handling and would also likely be faster, but at the expense of being well beyond a simple answer to this question.

However - I also give him the benefit of the doubt. I think he answered in good faith at the same time in a similar fashion to my v2, simplified version. I doubt the v1 version could be read and simplified in the 1,2 minute spread between the answer v1 and his answer.

I think the 'answer' to situations like this is:

  1. Have a sense of humor about it;
  2. Assume good faith; and
  3. Let the 'market' in votes decide.

Have a great day.

2
  • 1
    That's fair. I didn't even read far enough into your original answer to see its core. Sorry for reverting your answer. I still think your original answer is worthy of an additional answer to the question or an addendum at the bottom of your answer if asker/a visitor does want error checking.
    – Ruzihm
    Commented Oct 10, 2019 at 18:09
  • 1
    Your suggested addition added. Thanks!
    – dawg
    Commented Oct 10, 2019 at 18:31
2

I know similar situations quite well. It happened several times that I wrote an answer and recognized it was a duplicate of another answer that popped seconds earlier.

Even if it was effort and it doesn't feel fair, I usually just delete my answer and upvote the other because it was a great idea - I had the same :)

In your case it was even an edit. From the point of visitors imho there is not much sense in having two almost ident code pieces.

While I'm writing this, there is another edit that ads further information. If an answer has duplicate code but elaborates it is a good reason not to remove it.

To answer the question: If I was in a situation where I write an answer and while writing the answer I got another great clue and edit to a different answer and suddenly see that someone else got that nice idea already and I'd be embarrassed leaving the previous code, I'd not role back. I would remove it.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .