0

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 @andreas-sabelfeld

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

    def __init__(self, submissions):
        if isinstance(submissions, 'dict'):
            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

Extra Bored Having Fun Update

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

yeah i like doing this

deadcow
  • 1
  • 8
  • might be relevant : https://stackoverflow.com/questions/5753597/is-it-pythonic-to-use-list-comprehensions-for-just-side-effects – JonSG Jan 31 '23 at 13:55

1 Answers1

3

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

Where info_keys_f might be defined following info_keys as:

info_keys_f = [key for key in info_keys if key.startswith("f")]
JonSG
  • 10,542
  • 2
  • 25
  • 36
  • 1
    that looks nice and neat, was even thinking creating a 3 lists of what i want. and then catching the keys. what i did was get the filings out of the way first, tons of data and then the rest will float into place. same as yours almost looks good man, I like the comprehensions, unfortunately, they are good for small data but not gb data. thanks man appreciate it – deadcow Jan 31 '23 at 21:09