Python duplicate code removal -


according codeclimate, 2 methods in simplified class below duplicates of 1 mass of 40. what's best way refactor remove duplication? equivalent dictionary comprehension has marginally lower mass, otherwise identical.

 class dataadaptor:      def __init__(self):          self._feeds = {'field1': 'temperature', 'field2': 'humidity'}       def parse_data(self, data):          content = {}          field, feed in self._feeds.items():              if feed in data:                  content[field] = data[feed]          return content       def parse_content(self, content):          data = {}          field, feed in self._feeds.items():              if field in content:                  data[feed] = content[field]          return data 

clarification: version dictionary comprehensions has same duplication mass think it's less clear.

 class dataadaptor:      def __init__(self):          self._feeds = {'field1': 'temperature', 'field2': 'humidity'}       def parse_data(self, data):          return {field: data[feed] field, feed in self._feeds.items() if feed in data}       def parse_content(self, content):          return {feed: content[field] field, feed in self._feeds.items() if field in content} 

this green field development, we're free anything.

i have read first 3 answers , offer different. don't re-write functions. they're fine are. short, easy understand , easy read. why mess them? i've never used codeclimate , have no idea mass of 40 means, think it's mistake regard static code-checking tool absolute final authority on how piece of software should written. bet have bigger things worry partial duplication in simple little function, said written single dictionary comprehension.

i have 1 suggestion, however: change names of 2 functions select_by_matching_values , select_by_matching_keys. gives visibility , how differ each other.


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -