Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I am building TiKZ files, one pdf for each image I have. The purpose is to add text to each separately. The images are legion, so I created a script to generate the text files instead of generating each TeX using copy/paste.

So I created a tree mapping the section to the images.

Map

This map is in the form of a YAML file I read using python.

chapters:
- ConfigurationAndOperation:
        sections:
            - StartupParameters:
                subsections:
                    - GeneralSettings:
                        subsubsections:
                            - SystemSettings: 
                                images: [90]
                            - FacilitySettings: 
                                images: [93]

And I iterate it and assemble the required information for each part in the following manner:

import ConversionLibrary.ConfigurationHandling as config
import collections

TexNode = collections.namedtuple('TexNode', "level name")
class TexElement(TexNode):
    __slots__ = ()
    def __str__(self):
        return str(self.level)+ " " + str(self.name)

class StubHandler(config.ConfigurationHandler):
    """ Handle the config file that indexes the images
    """

    def __init__(self, config_file, filesystem_hint = None, filesystem_folder = None):
        super(StubHandler, self).__init__(config_file, filesystem_hint, filesystem_folder)
        self.manual_levels = { 1 : 'chapters',
                               2 : 'sections',
                               3 : 'subsections',
                               4 : 'subsubsections',
                               5 : 'paragraphs' }
        self.images = 'images'
        self.elements = list()
        self.image_map = list()


    def BuildTexSkeletons(self):
        self.__iterate_tree(self.config, 1, None);

    def __iterate_tree(self, tree, level = 1, method_delegate = None):
        document_part = self.manual_levels[level]
        if document_part in tree:
            for element in tree[document_part]:
                element_name = element.iterkeys().next()
                element_values = element.itervalues().next()
                self.__process_element_parents(level, element_name)
                self.__process_level(element_name, element_values, level, method_delegate)

    def __process_level(self, current_name, element, level, method_delegate = None):
        if element:
            if self.images in element:
                if method_delegate is None:
                    print [self.elements, element[self.images]]
                else:
                    method_delegate([self.elements, element[self.images]])
            if level != 5:
                self.__iterate_tree(element, level+1, method_delegate)

    def __process_element_parents(self, level, current_element):
        if not self.elements:
            self.elements.append(TexElement(level, current_element))
        else:
            if level == self.elements[-1].level:
                del self.elements[-1]
            elif level < self.elements[-1].level:
                new_trim = (self.elements[-1].level - level) + 1
                del self.elements[-new_trim:]
            self.elements.append(TexElement(level, current_element))

if __name__ == "__main__":
TREST = StubHandler("SHELL.yml")
TREST.BuildTexSkeletons()

As you can see I am using a method delegate, this method delegate gets passed down to the __process_level method.

The line that calls the delegate is:

method_delegate([self.elements, element[self.images]])

This is the thing I will be using to assemble my TeX files.

What I would like to improve

I put a stop to this because I want to keep moving forward. But something is bothering me.

Is there a way to implement this with a generator and that the iteration returns the same [self.elements, element[self.images]]?

I think with a generator it would be cleaner, since I could use the StubHandler in another higher level that has the Output methods.

share|improve this question
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.