Return grouped, sorted namedtuples in either format: flat, sections.
188449
def get_desc2nts_fnc(self, hdrgo_prt=True, section_prt=None,
                         top_n=None, use_sections=True):
        """"""
        # RETURN: flat list of namedtuples
        nts_flat = self.get_nts_flat(hdrgo_prt, use_sections)
        if nts_flat:
            flds = nts_flat[0]._fields
            if not use_sections:
                return {'sortobj':self, 'flat' : nts_flat, 'hdrgo_prt':hdrgo_prt, 'flds':flds,
                        'num_items':len(nts_flat), 'num_sections':1}
            else:
                return {'sortobj':self,
                        'sections' : [(self.grprobj.hdrobj.secdflt, nts_flat)],
                        'hdrgo_prt':hdrgo_prt,
                        'flds':flds,
                        'num_items':len(nts_flat), 'num_sections':1}
        # print('FFFF Sorter:get_desc2nts_fnc: nts_flat is None')
        # RETURN: 2-D list [(section_name0, namedtuples0), (section_name1, namedtuples1), ...
        #     kws: top_n hdrgo_prt section_sortby
        # Over-ride hdrgo_prt depending on top_n value
        assert top_n is not True and top_n is not False, \
            "top_n({T}) MUST BE None OR AN int".format(T=top_n)
        assert self.sectobj is not None, "SECTIONS OBJECT DOES NOT EXIST"
        sec_sb = self.sectobj.section_sortby
        # Override hdrgo_prt, if sorting by sections or returning a subset of GO IDs in section
        hdrgo_prt_curr = hdrgo_prt is True
        if sec_sb is True or (sec_sb is not False and sec_sb is not None) or top_n is not None:
            hdrgo_prt_curr = False
        # print('GGGG Sorter:get_desc2nts_fnc: hdrgo_prt_curr({}) sec_sb({}) top_n({})'.format(
        #     hdrgo_prt_curr, sec_sb, top_n))
        nts_section = self.sectobj.get_sorted_nts_keep_section(hdrgo_prt_curr)
        # print('HHHH Sorter:get_desc2nts_fnc: nts_section')
        # Take top_n in each section, if requested
        if top_n is not None:
            nts_section = [(s, nts[:top_n]) for s, nts in nts_section]
            if section_prt is None:
                nts_flat = self.get_sections_flattened(nts_section)
                flds = nts_flat[0]._fields if nts_flat else []
                return {'sortobj':self, 'flat' : nts_flat, 'hdrgo_prt':hdrgo_prt_curr, 'flds':flds,
                        'num_items':len(nts_flat), 'num_sections':1}
        # Send flat list of sections nts back, as requested
        if section_prt is False:
            nts_flat = self.get_sections_flattened(nts_section)
            flds = nts_flat[0]._fields if nts_flat else []
            return {'sortobj':self, 'flat' : nts_flat, 'hdrgo_prt':hdrgo_prt_curr, 'flds':flds,
                    'num_items':len(nts_flat),
                    'num_sections':len(nts_section)}
        # Send 2-D sections nts back
        # print('IIII Sorter:get_desc2nts_fnc: nts_section')
        flds = nts_section[0][1][0]._fields if nts_section else []
        return {'sortobj':self, 'sections' : nts_section, 'hdrgo_prt':hdrgo_prt_curr, 'flds':flds,
                'num_items':sum(len(nts) for _, nts in nts_section),
                'num_sections':len(nts_section)}
1268898
def _get_all_ns_packages(self):
        """"""
        pkgs = self.distribution.namespace_packages or []
        return sorted(flatten(map(self._pkg_names, pkgs)))
165671
def get_sections_2d(self):
        """"""
        sections_hdrgos_act = []
        hdrgos_act_all = self.get_hdrgos()  # Header GOs actually used to group
        hdrgos_act_secs = set()
        if self.hdrobj.sections:
            for section_name, hdrgos_all_lst in self.hdrobj.sections:
                # print("GGGGGGGGGGGGGGGGG {N:3} {NAME}".format(N=len(hdrgos_all_lst), NAME=section_name))
                hdrgos_all_set = set(hdrgos_all_lst)
                hdrgos_act_set = hdrgos_all_set.intersection(hdrgos_act_all)
                if hdrgos_act_set:
                    hdrgos_act_secs |= hdrgos_act_set
                    # Use original order of header GOs found in sections
                    hdrgos_act_lst = []
                    hdrgos_act_ctr = cx.Counter()
                    for hdrgo_p in hdrgos_all_lst: # Header GO that may or may not be used.
                        if hdrgo_p in hdrgos_act_set and hdrgos_act_ctr[hdrgo_p] == 0:
                            hdrgos_act_lst.append(hdrgo_p)
                        hdrgos_act_ctr[hdrgo_p] += 1
                    sections_hdrgos_act.append((section_name, hdrgos_act_lst))
            # print(">>>>>>>>>>>>>>> hdrgos_act_all {N:3}".format(N=len(hdrgos_act_all)))
            # print(">>>>>>>>>>>>>>> hdrgos_act_secs {N:3}".format(N=len(hdrgos_act_secs)))
            hdrgos_act_rem = hdrgos_act_all.difference(hdrgos_act_secs)
            if hdrgos_act_rem:
                # print("RRRRRRRRRRR {N:3}".format(N=len(hdrgos_act_rem)))
                sections_hdrgos_act.append((self.hdrobj.secdflt, hdrgos_act_rem))
        else:
            sections_hdrgos_act.append((self.hdrobj.secdflt, hdrgos_act_all))
        return sections_hdrgos_act
607392
def section(self, section, skip=['type', 'order']):
        
        return [(key, val) for key, val in self.parser.items(section)
                if key not in skip]
393649
def get_fields(desc2nts):
        """"""
        if 'flat' in desc2nts:
            nts_flat = desc2nts.get('flat')
            if nts_flat:
                return nts_flat[0]._fields
        if 'sections' in desc2nts:
            nts_sections = desc2nts.get('sections')
            if nts_sections:
                return nts_sections[0][1][0]._fields
1221270
def sort_return_tuples(response, **options):
    
    if not response or not options.get('groups'):
        return response
    n = options['groups']
    return list(izip(*[response[i::n] for i in range(n)]))
351548
def get_goids_sections(sections):
        """"""
        goids_all = set()
        for _, goids_sec in sections:
            goids_all |= set(goids_sec)
        return goids_all
1374102
def zip_namedtuple(nt_list):
    
    if not nt_list:
        return dict()
    if not isinstance(nt_list, list):
        nt_list = [nt_list]
    for nt in nt_list:
        assert type(nt) == type(nt_list[0])
    ret = {k : [v] for k, v in nt_list[0]._asdict().items()}
    for nt in nt_list[1:]:
        for k, v in nt._asdict().items():
            ret[k].append(v)
    return ret
474107
def _grouped(iterable, n):
        
        args = [iter(iterable)] * n
        return [[e for e in t if not e is None] for t in zip_longest(*args, fillvalue=None)]
340430
def getAsTuple(self, section):
        
        keys = self.getKeys(section)
        value_dict = self.getValues(section)
        return namedtuple(section, keys)(**value_dict)
1284105
def flatten(list_of_lists):
    """"""
    flat_list = []
    for sublist in list_of_lists:
        if isinstance(sublist, string_types) or isinstance(sublist, int):
            flat_list.append(sublist)
        elif sublist is None:
            continue
        elif not isinstance(sublist, string_types) and len(sublist) == 1:
            flat_list.append(sublist[0])
        else:
            flat_list.append(tuple(sublist))
    return flat_list
642289
def combine_nt_lists(lists, flds, dflt_null=""):
    """"""
    combined_nt_list = []
    # Check that all lists are the same length
    lens = [len(lst) for lst in lists]
    assert len(set(lens)) == 1, \
        "LIST LENGTHS MUST BE EQUAL: {Ls}".format(Ls=" ".join(str(l) for l in lens))
    # 1. Instantiate namedtuple object
    ntobj = cx.namedtuple("Nt", " ".join(flds))
    # 2. Loop through zipped list
    for lst0_lstn in zip(*lists):
        # 2a. Combine various namedtuples into a single namedtuple
        combined_nt_list.append(ntobj._make(_combine_nt_vals(lst0_lstn, flds, dflt_null)))
    return combined_nt_list
956964
def get_flat_groups(self, field=None):
        '''
        
        '''
        field = field if field else self._determine_group_field(field)
        temp_groups = self.data['grouped'][field]['groups']
        return [y for x in temp_groups for y in x['doclist']['docs']]
1263515
def flat_list(lst):
    
    if isinstance(lst, list):
        for item in lst:
            for i in flat_list(item):
                yield i
    else:
        yield lst
571719
def get_desc2nts(self, **kws_usr):
        """"""
        # desc2nts contains: (sections hdrgo_prt sortobj) or (flat hdrgo_prt sortobj)
        # keys_nts: hdrgo_prt section_prt top_n use_sections
        kws_nts = {k:v for k, v in kws_usr.items() if k in self.keys_nts}
        return self.get_desc2nts_fnc(**kws_nts)
1114420
def generate_flattened_dict(headers, results):
    ''''''
    for name, values in results.items():
        row = []
        for header in headers:
            if header == 'name':
                row.append(name)
            elif ':' in header:
                neurite_type, metric = header.split(':')
                row.append(values[neurite_type][metric])
            else:
                row.append(values[header])
        yield row
268059
def subgroup(self, t, i):
        """"""

        current = []

        # (?flags)
        flags = self.get_flags(i)
        if flags:
            self.flags(flags[2:-1])
            return [flags]

        # (?#comment)
        comments = self.get_comments(i)
        if comments:
            return [comments]

        verbose = self.verbose
        unicode_flag = self.unicode

        # (?flags:pattern)
        flags = self.get_flags(i, True)
        if flags:  # pragma: no cover
            t = flags
            self.flags(flags[2:-1], scoped=True)

        current = []
        try:
            while t != ')':
                if not current:
                    current.append(t)
                else:
                    current.extend(self.normal(t, i))

                t = next(i)
        except StopIteration:
            pass

        # Restore flags after group
        self.verbose = verbose
        self.unicode = unicode_flag

        if t == ")":
            current.append(t)
        return current