Exemple #1
0
 def load_files(self):
     """Load nodes and elements from all network files and return them in
     a |Selections| instance.  Each single network file defines a separate
     |Selection| instance.  Additionally, all |Element| and |Node| objects
     are bundled in a selection named `complete`.
     """
     selections = selectiontools.Selections()
     for (filename, path) in zip(self.filenames, self.filepaths):
         # Ensure both `Node` and `Element`start with a `fresh` memory.
         devicetools.Node.gather_new_nodes()
         devicetools.Element.gather_new_elements()
         try:
             info = runpy.run_path(path)
         except BaseException:
             objecttools.augment_excmessage(
                 'While trying to load the network file `%s`' % path)
         try:
             selections += selectiontools.Selection(
                 filename.split('.')[0], info['Node'].gather_new_nodes(),
                 info['Element'].gather_new_elements())
         except KeyError as exc:
             raise KeyError(
                 'The class `%s` cannot be loaded from the network '
                 'file `%s`.  Please refer to the HydPy documentation '
                 'on how to prepare network files properly.' %
                 (exc.args[0], filename))
     selections += selectiontools.Selection(
         'complete', info['Node'].registered_nodes(),
         info['Element'].registered_elements())
     return selections
Exemple #2
0
    def load(self):
        """Load nodes and elements from all network files and return them in
        a :class:`~hydpy.selectiontools.Selections` instance.  Each single
        network file defines a seperate
        :class:`~hydpy.selectiontools.Selection` instance.  Additionally, all
        elements and nodes are bundled in a selection named `complete`.
        """
        selections = selectiontools.Selections()
        for (filename, path) in zip(self.filenames, self.filepaths):
            # Ensure both `Node` and `Element`start with a `fresh` memory.
            devicetools.Node.gathernewnodes()
            devicetools.Element.gathernewelements()
            info = {}
            try:
                with open(path) as file_:
                    code = compile(file_.read(), path, 'exec')
                    exec(code, {}, info)
            except Exception:
                prefix = 'While trying to load the network file `%s`' % path
                objecttools.augmentexcmessage(prefix)
            try:
                selections += selectiontools.Selection(
                    filename.split('.')[0], info['Node'].gathernewnodes(),
                    info['Element'].gathernewelements())

            except KeyError as exc:
                KeyError('The class `%s` cannot be loaded from the network '
                         'file `%s`.  Please refer to the HydPy documentation '
                         'on how to prepare network files properly.' %
                         (exc.args[0], filename))
        selections += selectiontools.Selection(
            'complete', info['Node'].registerednodes(),
            info['Element'].registeredelements())
        return selections
Exemple #3
0
 def distinct_networks(self):
     sels1 = selectiontools.Selections()
     sels2 = selectiontools.Selections()
     complete = selectiontools.Selection('complete',
                                         self.nodes, self.elements)
     for (name, node) in self.endnodes:
         sel = complete.copy(name).select_upstream(node)
         sels1 += sel
         sels2 += sel.copy(name)
     for (name1, sel1) in sels1:
         for (name2, sel2) in sels2:
             if name1 != name2:
                 sel1 -= sel2
     for name in sels1.names:
         if not sels1[name].elements:
             del sels1[name]
     return sels1
Exemple #4
0
 def numberofnetworks(self):
     """The number of distinct networks defined by the|Node| and
     |Element| objects currently handled by the |HydPy| object."""
     sels1 = selectiontools.Selections()
     sels2 = selectiontools.Selections()
     complete = selectiontools.Selection('complete', self.nodes,
                                         self.elements)
     for node in self.endnodes:
         sel = complete.copy(node.name).select_upstream(node)
         sels1 += sel
         sels2 += sel.copy(node.name)
     for sel1 in sels1:
         for sel2 in sels2:
             if sel1.name != sel2.name:
                 sel1 -= sel2
     for name in list(sels1.names):
         if not sels1[name].elements:
             del sels1[name]
     return sels1
Exemple #5
0
 def save_files(self, selections):
     """Save the nodes and elements from each |Selection| object contained
     within the given |Selections| instance to a separate network file of
     the same name.
     """
     try:
         currentpath = self.currentpath
         selections = selectiontools.Selections(selections)
         for selection in selections:
             if selection.name == 'complete':
                 continue
             path = os.path.join(currentpath, selection.name + '.py')
             selection.save(path=path, write_nodes=True)
     except BaseException:
         objecttools.augment_excmessage(
             'While trying to save selections `%s` into network files' %
             selections)
Exemple #6
0
 def save(self, selections, overwrite=False):
     """Save the nodes and elements from each
     :class:`~hydpy.selectiontools.Selection` object contained within the
     given :class:`~hydpy.selectiontools.Selections` instance to a seperate
     network file of the same name.  Set `overwrite` to `True`, if you
     want to overwrite already existing network files.
     """
     selections = selectiontools.Selections(selections)
     for (name, selection) in selections:
         if name == 'complete':
             continue
         path = os.path.join(self.dirpath, name + '.py')
         if os.path.exists(path) and not overwrite:
             warnings.warn('The path `%s` does already exist, selection '
                           '`%s` cannot be saved.  Please select another '
                           'network directory or set the `overwrite` flag '
                           'to `True`' % (path, name))
         else:
             with open(path, 'w') as file_:
                 file_.write('from hydpy import *\n\n')
                 file_.write(repr(selection.elements))
Exemple #7
0
 def prepare_network(self):
     """Load all network files as |Selections| (stored in module |pub|)
     and assign the "complete" selection to the |HydPy| object."""
     pub.selections = selectiontools.Selections()
     pub.selections += pub.networkmanager.load_files()
     self.update_devices(pub.selections.complete)
Exemple #8
0
 def preparenetwork(self):
     pub.selections = selectiontools.Selections()
     pub.selections += pub.networkmanager.load()
     self.updatedevices(pub.selections.complete)