Home > database >  how to create a new dictionary while deleting key from old dictionary in a comprehension?
how to create a new dictionary while deleting key from old dictionary in a comprehension?

Time:01-31

I have a dictionary that I need to separate into class attributes to format the attributes later. Was wondering, can this could be done in one comprehension?

This code works for what I want. Is there a more elegant way?

class FormatSubmissions():
    submissions = None
    sic = None
    info = None
    rest = None
    info_keys = [
        'tickers', 'exchanges' 'ein', 'formerNames', 'state',
        'addresses', 'phone',  'entityType', 'fiscal']
    
    def set_submissions(self, submissions):
        if not submissions:
            return False
        self.submissions = submissions
        self.sic =  {a: b for a, b in submissions.items() if a.startswith('sic')}
        [ submissions.pop(i) for i in self.sic.keys()]
        self.info ={c: d for c, d in submissions.items()
            for f in self.info_keys if c.startswith(f)}
        [submissions.pop(i) for i in self.info.keys()]
        self.filings = submissions.pop('filings')
        self.rest = submissions

Updated from the suggestion by the

class FormatSubmissions():
    __submissions__ = None
    info_keys = [
        'tickers', 'exchanges' 'ein', 'formerNames', 'state',
        'addresses', 'phone',  'entityType', 'fiscal']
    info = {}
    sic = {}
    filings = {}
    rest = {}

    def __init__(self, submissions):
        self.__submissions__ = submissions
        self.rest = submissions
        self._set_filings()
        self._set_sic()
        self._set_info()
 
    def _set_info(self):
        self.info = {c: d for c, d in self.rest.items()
                     for f in self.info_keys if c.startswith(f)}
        [self.rest.pop(i) for i in self.info.keys()]

    def _set_sic(self):
        self.sic = {a: b for a, b in self.rest.items()
                    if a.startswith('sic')}
        [self.rest.pop(i) for i in self.sic.keys()]
    
    def _set_filings(self):
        self.filings = self.rest.pop('filings')

    def get_filings(self):
        return self.filings
    
    def get_info(self):
        return self.info
    
    def get_rest(self):
        return self.rest

CodePudding user response:

Afaik it's good measure to make class variables private ("dunder": __) and use getter and setter methods. I would then use these methods inside the init function.

Else it looks fine.

CodePudding user response:

Elegant is in the eye of the beholder, but I personally feel that an approach that does not use a comprehension here is much easier to understand.

I think this does the same as what you are doing but I have not actually tried it...

    def set_submissions(self, submissions):
        if not submissions:
            return False

        self.submissions = {}
        self.sic = {}
        self.info = {}

        for key, value in submissions.items():
            if key == "filings":
                self.filings = value

            if key.startswith('sic'):
                self.sic[key] = value

            for info_key in self.info_keys_f:
                if key.startswith(info_key):
                    self.info[key] = value

            self.rest[key] = value
            self.submissions[key] = value
  • Related