sys.setrecursionlimit

Here are the examples of the python api sys.setrecursionlimit taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

59 Examples 7

Example 1

Project: gkno_launcher
Source File: isomorphvf2.py
View license
    def reset_recursion_limit(self):
        """Restores the recursion limit."""
        ### TODO:
        ###     Currently, we use recursion and set the recursion level higher.
        ###     It would be nice to restore the level, but because the
        ###     (Di)GraphMatcher classes make use of cyclic references, garbage
        ###     collection will never happen when we define __del__() to
        ###     restore the recursion level. The result is a memory leak.
        ###     So for now, we do not automatically restore the recursion level,
        ###     and instead provide a method to do this manually. Eventually,
        ###     we should turn this into a non-recursive implementation.
        sys.setrecursionlimit(self.old_recursion_limit)

Example 2

Project: dupeguru
Source File: engine_test.py
View license
    def test_dont_recurse_do_match(self):
        # with nosetests, the stack is increased. The number has to be high enough not to be failing falsely
        sys.setrecursionlimit(200)
        files = [NamedObject('foo bar') for i in range(201)]
        try:
            getmatches(files)
        except RuntimeError:
            self.fail()
        finally:
            sys.setrecursionlimit(1000)

Example 3

Project: Ragout
Source File: isomorphvf2.py
View license
    def reset_recursion_limit(self):
        """Restores the recursion limit."""
        ### TODO:
        ###     Currently, we use recursion and set the recursion level higher.
        ###     It would be nice to restore the level, but because the
        ###     (Di)GraphMatcher classes make use of cyclic references, garbage
        ###     collection will never happen when we define __del__() to
        ###     restore the recursion level. The result is a memory leak.
        ###     So for now, we do not automatically restore the recursion level,
        ###     and instead provide a method to do this manually. Eventually,
        ###     we should turn this into a non-recursive implementation.
        sys.setrecursionlimit(self.old_recursion_limit)

Example 4

Project: weboob
Source File: test.py
View license
    def run(self, result):
        """
        Call the parent run() for each backend instance.
        Skip the test if we have no backends.
        """
        # This is a hack to fix an issue with nosetests running
        # with many tests. The default is 1000.
        sys.setrecursionlimit(10000)
        try:
            if not len(self.backends):
                result.startTest(self)
                result.stopTest(self)
                raise SkipTest('No backends configured for this module.')
            TestCase.run(self, result)
        finally:
            self.weboob.deinit()

Example 5

Project: astrodendro
Source File: test_recursion.py
View license
    def test_computing_level(self):
        d = Dendrogram.compute(self.data)

        # Now pick a structure near the middle of the dendrogram:
        mid_structure = d.structure_at((0, self.size // 2))

        # Compute its level:
        sys.setrecursionlimit(100000)
        _ = mid_structure.level

        # Check that .level satisfies the recurrence relation:
        # 0 if root else parent.level + 1
        for structure in d.all_structures:
            if structure.parent is None:
                assert structure.level == 0
            else:
                assert structure.level == structure.parent.level + 1

Example 6

Project: equip
Source File: traversals.py
View license
def dfs_postorder_nodes(graph, root):
  import sys
  sys.setrecursionlimit(500)
  visited = set()

  def _dfs(node, _visited):
    _visited.add(node)
    for edge in graph.out_edges(node):
      dest_node = edge.dest
      if dest_node not in _visited:
        for child in _dfs(dest_node, _visited):
          yield child
    yield node

  return [n for n in _dfs(root, visited)]

Example 7

Project: Veil-Evasion
Source File: find_recursionlimit.py
View license
def check_limit(n, test_func_name):
    sys.setrecursionlimit(n)
    if test_func_name.startswith("test_"):
        print test_func_name[5:]
    else:
        print test_func_name
    test_func = globals()[test_func_name]
    try:
        test_func()
    # AttributeError can be raised because of the way e.g. PyDict_GetItem()
    # silences all exceptions and returns NULL, which is usually interpreted
    # as "missing attribute".
    except (RuntimeError, AttributeError):
        pass
    else:
        print "Yikes!"

Example 8

Project: slpkg
Source File: dependency.py
View license
    def binary(self, name, flag):
        """Build all dependencies of a package
        """
        if self.meta.rsl_deps in ["on", "ON"] and "--resolve-off" not in flag:
            sys.setrecursionlimit(10000)
            dependencies = []
            requires = Requires(name, self.repo).get_deps()
            if requires:
                for req in requires:
                    status(0)
                    if req and req not in self.black:
                        dependencies.append(req)
                if dependencies:
                    self.dep_results.append(dependencies)
                    for dep in dependencies:
                        self.binary(dep, flag)
            return self.dep_results
        else:
            return []

Example 9

Project: mythbox
Source File: trial.py
View license
    def opt_recursionlimit(self, arg):
        """see sys.setrecursionlimit()"""
        try:
            sys.setrecursionlimit(int(arg))
        except (TypeError, ValueError):
            raise usage.UsageError(
                "argument to recursionlimit must be an integer")

Example 10

Project: SubliminalCollaborator
Source File: trial.py
View license
    def opt_recursionlimit(self, arg):
        """
        see sys.setrecursionlimit()
        """
        try:
            sys.setrecursionlimit(int(arg))
        except (TypeError, ValueError):
            raise usage.UsageError(
                "argument to recursionlimit must be an integer")

Example 11

Project: networkx
Source File: isomorphvf2.py
View license
    def reset_recursion_limit(self):
        """Restores the recursion limit."""
        ### TODO:
        ###     Currently, we use recursion and set the recursion level higher.
        ###     It would be nice to restore the level, but because the
        ###     (Di)GraphMatcher classes make use of cyclic references, garbage
        ###     collection will never happen when we define __del__() to
        ###     restore the recursion level. The result is a memory leak.
        ###     So for now, we do not automatically restore the recursion level,
        ###     and instead provide a method to do this manually. Eventually,
        ###     we should turn this into a non-recursive implementation.
        sys.setrecursionlimit(self.old_recursion_limit)

Example 12

Project: astrodendro
Source File: test_recursion.py
View license
    def test_plot(self):
        sys.setrecursionlimit(self._oldlimit)
        ax = plt.gca()
        sys.setrecursionlimit(150)

        d = Dendrogram.compute(self.data)
        p = d.plotter()
        p.plot_tree(ax)

Example 13

View license
    def do(self, callback_name, *args):

        import sys
        sys.setrecursionlimit(10000000)

        print "generating samples"
        base_fname_part1 = self.path + '/samples-'
        base_fname_part2 = '_batch%06d'%self.main_loop.status['iterations_done']
        sampler.generate_samples(self.model, self.get_mu_sigma,
            n_samples=self.n_samples, inpaint=False, denoise_sigma=None, X_true=None,
            base_fname_part1=base_fname_part1, base_fname_part2=base_fname_part2)
        sampler.generate_samples(self.model, self.get_mu_sigma,
            n_samples=self.n_samples, inpaint=True, denoise_sigma=None, X_true=self.X,
            base_fname_part1=base_fname_part1, base_fname_part2=base_fname_part2)
        sampler.generate_samples(self.model, self.get_mu_sigma,
            n_samples=self.n_samples, inpaint=False, denoise_sigma=1, X_true=self.X,
            base_fname_part1=base_fname_part1, base_fname_part2=base_fname_part2)

Example 14

Project: pymote
Source File: npickle.py
View license
def write_pickle(obj, path, makedir=True):
    """Write object in Python pickle format."""
    # TODO: use normal pickling by implementing pickling protocol for Network
    # class http://docs.python.org/library/pickle.html#the-pickle-protocol
    # TODO: find out origin of maximum recursion depth problem, hack solution:
    sys.setrecursionlimit(6000)
    try:
        os.makedirs(os.path.split(path)[0])
    except OSError, e:
        if e.errno!=errno.EEXIST and e.filename!='':
            raise
    fh = _get_fh(str(path), mode='wb')
    pickle.dump(obj, fh, pickle.HIGHEST_PROTOCOL)
    fh.close()
    logger.info('instance of %s saved in %s' % (str(obj.__class__), path))

Example 15

View license
    def do(self, callback_name, *args):

        import sys
        sys.setrecursionlimit(10000000)

        print "plotting parameters"
        for param in self.blocks_model.parameters:
            param_name = param.name
            filename_safe_name = '-'.join(param_name.split('/')[2:]).replace(' ', '_')
            base_fname_part1 = self.path + '/params-' + filename_safe_name
            base_fname_part2 = '_batch%06d'%self.main_loop.status['iterations_done']
            viz.plot_parameter(param.get_value(), base_fname_part1, base_fname_part2,
                title=param_name, n_colors=self.model.n_colors)

Example 16

Project: TrustRouter
Source File: find_recursionlimit.py
View license
def check_limit(n, test_func_name):
    sys.setrecursionlimit(n)
    if test_func_name.startswith("test_"):
        print(test_func_name[5:])
    else:
        print(test_func_name)
    test_func = globals()[test_func_name]
    try:
        test_func()
    # AttributeError can be raised because of the way e.g. PyDict_GetItem()
    # silences all exceptions and returns NULL, which is usually interpreted
    # as "missing attribute".
    except (RuntimeError, AttributeError):
        pass
    else:
        print("Yikes!")

Example 17

Project: weboob
Source File: test.py
View license
    def run(self, result):
        """
        Call the parent run() for each backend instance.
        Skip the test if we have no backends.
        """
        # This is a hack to fix an issue with nosetests running
        # with many tests. The default is 1000.
        sys.setrecursionlimit(10000)
        try:
            if not len(self.backends):
                result.startTest(self)
                result.stopTest(self)
                raise SkipTest('No backends configured for this module.')
            TestCase.run(self, result)
        finally:
            self.weboob.deinit()

Example 18

Project: datafari
Source File: find_recursionlimit.py
View license
def check_limit(n, test_func_name):
    sys.setrecursionlimit(n)
    if test_func_name.startswith("test_"):
        print test_func_name[5:]
    else:
        print test_func_name
    test_func = globals()[test_func_name]
    try:
        test_func()
    # AttributeError can be raised because of the way e.g. PyDict_GetItem()
    # silences all exceptions and returns NULL, which is usually interpreted
    # as "missing attribute".
    except (RuntimeError, AttributeError):
        pass
    else:
        print "Yikes!"

Example 19

View license
def get_sub_tree(tree,tips_not_to_prune):
    """Get sub tree, modifying recursion limit if necessary"""

    try:
        new_tree = tree.getSubTree(tips_not_to_prune)
    except RuntimeError:
        #NOTE:  getSubTree will hit
        #maximum recursion depth on large trees
        #Try working around this issue with a large
        #recursion depth limit
        old_recursion_limit = getrecursionlimit()
        setrecursionlimit(50000)
        new_tree = tree.getSubTree(tips_not_to_prune)
        setrecursionlimit(old_recursion_limit)
    return new_tree

Example 20

Project: mythbox
Source File: trial.py
View license
    def opt_recursionlimit(self, arg):
        """see sys.setrecursionlimit()"""
        try:
            sys.setrecursionlimit(int(arg))
        except (TypeError, ValueError):
            raise usage.UsageError(
                "argument to recursionlimit must be an integer")

Example 21

Project: SubliminalCollaborator
Source File: trial.py
View license
    def opt_recursionlimit(self, arg):
        """
        see sys.setrecursionlimit()
        """
        try:
            sys.setrecursionlimit(int(arg))
        except (TypeError, ValueError):
            raise usage.UsageError(
                "argument to recursionlimit must be an integer")

Example 22

Project: networkx
Source File: isomorphvf2.py
View license
    def reset_recursion_limit(self):
        """Restores the recursion limit."""
        ### TODO:
        ###     Currently, we use recursion and set the recursion level higher.
        ###     It would be nice to restore the level, but because the
        ###     (Di)GraphMatcher classes make use of cyclic references, garbage
        ###     collection will never happen when we define __del__() to
        ###     restore the recursion level. The result is a memory leak.
        ###     So for now, we do not automatically restore the recursion level,
        ###     and instead provide a method to do this manually. Eventually,
        ###     we should turn this into a non-recursive implementation.
        sys.setrecursionlimit(self.old_recursion_limit)

Example 23

View license
    def reset_recursion_limit(self):
        """Restores the recursion limit."""
        ### TODO:
        ###     Currently, we use recursion and set the recursion level higher.
        ###     It would be nice to restore the level, but because the
        ###     (Di)GraphMatcher classes make use of cyclic references, garbage
        ###     collection will never happen when we define __del__() to
        ###     restore the recursion level. The result is a memory leak.
        ###     So for now, we do not automatically restore the recursion level,
        ###     and instead provide a method to do this manually. Eventually,
        ###     we should turn this into a non-recursive implementation.
        sys.setrecursionlimit(self.old_recursion_limit)

Example 24

Project: calibre
Source File: lrf_input.py
View license
    def convert(self, stream, options, file_ext, log,
                accelerators):
        from lxml import etree
        from calibre.ebooks.lrf.input import (MediaType, Styles, TextBlock,
                Canvas, ImageBlock, RuledLine)
        self.log = log
        self.log('Generating XML')
        from calibre.ebooks.lrf.lrfparser import LRFDocument
        d = LRFDocument(stream)
        d.parse()
        xml = d.to_xml(write_files=True)
        if options.verbose > 2:
            open(u'lrs.xml', 'wb').write(xml.encode('utf-8'))
        parser = etree.XMLParser(no_network=True, huge_tree=True)
        try:
            doc = etree.fromstring(xml, parser=parser)
        except:
            self.log.warn('Failed to parse XML. Trying to recover')
            parser = etree.XMLParser(no_network=True, huge_tree=True,
                    recover=True)
            doc = etree.fromstring(xml, parser=parser)

        char_button_map = {}
        for x in doc.xpath('//CharButton[@refobj]'):
            ro = x.get('refobj')
            jump_button = doc.xpath('//*[@objid="%s"]'%ro)
            if jump_button:
                jump_to = jump_button[0].xpath('descendant::JumpTo[@refpage and @refobj]')
                if jump_to:
                    char_button_map[ro] = '%s.xhtml#%s'%(jump_to[0].get('refpage'),
                            jump_to[0].get('refobj'))
        plot_map = {}
        for x in doc.xpath('//Plot[@refobj]'):
            ro = x.get('refobj')
            image = doc.xpath('//Image[@objid="%s" and @refstream]'%ro)
            if image:
                imgstr = doc.xpath('//ImageStream[@objid="%s" and @file]'%
                    image[0].get('refstream'))
                if imgstr:
                    plot_map[ro] = imgstr[0].get('file')

        self.log('Converting XML to HTML...')
        styledoc = etree.fromstring(P('templates/lrf.xsl', data=True))
        media_type = MediaType()
        styles = Styles()
        text_block = TextBlock(styles, char_button_map, plot_map, log)
        canvas = Canvas(doc, styles, text_block, log)
        image_block = ImageBlock(canvas)
        ruled_line = RuledLine()
        extensions = {
                ('calibre', 'media-type') : media_type,
                ('calibre', 'text-block') : text_block,
                ('calibre', 'ruled-line') : ruled_line,
                ('calibre', 'styles')     : styles,
                ('calibre', 'canvas')     : canvas,
                ('calibre', 'image-block'): image_block,
                }
        transform = etree.XSLT(styledoc, extensions=extensions)
        try:
            result = transform(doc)
        except RuntimeError:
            sys.setrecursionlimit(5000)
            result = transform(doc)

        with open('content.opf', 'wb') as f:
            f.write(result)
        styles.write()
        return os.path.abspath(u'content.opf')

Example 25

Project: slpkg
Source File: dependency.py
View license
    def sbo(self, name):
        """Build all dependencies of a package
        """
        if (self.meta.rsl_deps in ["on", "ON"] and
                "--resolve-off" not in self.flag):
            sys.setrecursionlimit(10000)
            dependencies = []
            requires = SBoGrep(name).requires()
            if requires:
                for req in requires:
                    status(0.03)
                    # toolbar_width = status(index, toolbar_width, 1)
                    # avoid to add %README% as dependency and
                    # if require in blacklist
                    if "%README%" not in req and req not in self.blacklist:
                        dependencies.append(req)
                if dependencies:
                    self.dep_results.append(dependencies)
                    for dep in dependencies:
                        self.sbo(dep)
            return self.dep_results
        else:
            return []

Example 26

Project: mtbmap
Source File: relations2lines.py
View license
def run(db_name, user, host, port):
    logger.info('Relations2lines script started')
    logger.info('  Searching RelationIDs and Lines in planet_osm_line...')
    # Create connection to DB server.
    connection = connect("dbname='{db_name}' user='{user}' password='' host='{host}' port='{port}'"
                         .format(db_name=db_name, user=user, host=host, port=port))
    relation_cursor = connection.cursor()
    auxiliary_cursor = connection.cursor()
    way_cursor = connection.cursor()

    # Find relation IDs to be parsed, ie. those with osmc:symbol or some mtb values
    # Treat lines with useful attributes as relations (osm_id >= 0)
    relation_ids = []
    relations = []
    relation_cursor.execute('''
        SELECT osm_id, CASE WHEN ("highway"='track' AND "tracktype"='grade1' AND "mtb:scale" IS NULL) THEN 'grade1'
                            ELSE "mtb:scale"
                       END AS "mtb:scale"
            , "mtb:scale:uphill", network, "osmc:symbol"
            FROM planet_osm_line
            WHERE ("osmc:symbol" IS NOT NULL OR kct_red IS NOT NULL
                OR kct_blue IS NOT NULL OR kct_green IS NOT NULL
                OR kct_yellow IS NOT NULL
                OR ("mtb:scale" IS NOT NULL
                  AND (("access"<>'private' AND "access"<>'no') OR "access" IS NULL OR ("access" IN ('private', 'no') AND bicycle='yes')))
                OR "mtb:scale:uphill" IS NOT NULL OR ("highway"='track' AND "tracktype"='grade1'))
        ''')
    while True:
        # Fetch some of the result.
        rows = relation_cursor.fetchmany(100)

        # Empty result means end of query.
        if not rows:
            break

        # relations have negative osm_id in planet_osm_line table
        # lines have positive osm_id in planet_osm_line table
        for row in rows:
            if row[0] < 0:
                # osm_id is not a primary key
                if not (row[0] in relation_ids):
                    relation_ids.append(-row[0])
            else:
                # 0: osm_id; 1: mtb:scale; 2: mtb:scale:uphill; 3: network; 4: "osmc:symbol"
                line_info = ("LINE;" + str(row[0]) + ";" + str(row[1]) + ";" + str(row[2]) + ";"
                             + str(row[3]) + ";" + str(row[4]))
                relations.append(Relation(line_info))

    logger.info('RelationIDs and Lines found.')
    logger.info('Getting Relation details from planet_osm_rels...')

    # Select important columns just for our IDs
    for r_id in relation_ids:
        relation_cursor.execute('''
            SELECT id, members, tags
                FROM planet_osm_rels
                WHERE id=%s
        ''' % r_id)
        row = relation_cursor.fetchone()
        # Make Relation object with parsed data
        relations.append(Relation(row))

    logger.info('Relations details found.')
    logger.info('Making single routes from relations with all osmc:symbols...')

    # Find final routes and append all corresponding osmcSymbols
    routes = routes_from_rels(relations)

    list_of_routes = routes.values()
    list_of_routes.sort()
    logger.info('Routes now have osmc:symbols.')
    logger.info('Finding firstNode and lastNode for each route in planet_osm_ways...')

    # Clean previous routes.
    auxiliary_cursor.execute("DROP TABLE IF EXISTS planet_osm_routes2")
    auxiliary_cursor.execute("DELETE FROM geometry_columns WHERE f_table_name = 'planet_osm_routes2'")
    auxiliary_cursor.execute("CREATE TABLE planet_osm_routes2 AS SELECT osm_id, way, highway, tracktype" +
                             " FROM planet_osm_line WHERE osm_id = 0")
    auxiliary_cursor.execute("DELETE FROM geometry_columns WHERE f_table_name = 'planet_osm_routes2'")
    auxiliary_cursor.execute("INSERT INTO geometry_columns VALUES ('', 'public', 'planet_osm_routes2'," +
                             " 'way', 2, 3857, 'LINESTRING')")

    # Add important information to each route
    for r in list_of_routes:
        auxiliary_cursor.execute('''
            SELECT way, highway, tracktype FROM planet_osm_line
              WHERE osm_id=%s AND (("access"<>'private' AND "access"<>'no') OR "access" IS NULL
                OR ("access" IN ('private', 'no') AND bicycle='yes'))
        ''' % r.id)
        row = auxiliary_cursor.fetchone()
        # Some route IDs from relations may not be present in line table, ie. out of bounding box, those are ignored
        if row is not None:
            routes[r.id].geometry = row[0]
            routes[r.id].highway = row[1]
            routes[r.id].tracktype = row[2]
            way_cursor.execute('''
                SELECT nodes[1], nodes[array_upper(nodes, 1)]
                    FROM planet_osm_ways
                    WHERE id=%s
            ''' % r.id)
            first_end_nodes = way_cursor.fetchone()
            routes[r.id].firstNode = first_end_nodes[0]
            routes[r.id].lastNode = first_end_nodes[1]
        else:
            routes.pop(r.id)
    logger.info('Found firstNodes and lastNodes.')
    logger.info('Finding route neighbours based on first and last nodes...')

    # Find end nodes and their routes
    nodes = find_nodes(routes)

    # Find previous and next route neighbours
    for r in routes:
        next_route_ids = deepcopy(nodes[routes[r].lastNode])
        next_route_ids.remove(routes[r].id)
        previous_route_ids = deepcopy(nodes[routes[r].firstNode])
        previous_route_ids.remove(routes[r].id)
        for rid in next_route_ids:
            routes[routes[r].id].nextRoutes.append(rid)
        for rid in previous_route_ids:
            routes[routes[r].id].previousRoutes.append(rid)

    # remove unconnected tracks with highway=track and tracktype=grade1 and mtb:scale is null
    logger.info('Removing disconnected tracks.')
    routes = remove_unconnected(routes, nodes)
    logger.info('Tracks removed.')

    logger.info('Finding dangerous nodes (column warning).')

    # Find nodeIDs, where track's attribute mtb:scale changes rapidly (difference >= 2),
    # create new column warning in planet_osm_lines with the difference
    danger_nodes = find_dangerous_nodes(nodes, routes)
    point_cursor = connection.cursor()
    insert_danger_nodes(danger_nodes, point_cursor)
    point_cursor.close()

    logger.info('Neighbours are found.')
    logger.info('Determining offset for each route...')

    # Find offset polarity
#    listOfRoutes = routes.values()
    list_of_routes = sorted(routes.values(), key=lambda route: route.osmcSigns[0], reverse=True)
    if len(list_of_routes) > 1000:
        setrecursionlimit(len(list_of_routes))
    for r in list_of_routes:
        set_offset(routes, r.id, "next")
        set_offset(routes, r.id, "previous")

    logger.info('Offset is found.')
    logger.info("Inserting of routes into new empty table planet_osm_routes2...")

    # Determine maximum number of different osmcSymbols at one route
    max_signs = 0
    for r in routes.values():
        if max_signs < r.numOfSigns:
            max_signs = r.numOfSigns
    if max_signs < 4:
        max_signs = 4

    # Prepare database table for data insertion
    auxiliary_cursor.execute('''
        ALTER TABLE planet_osm_routes2
          ADD "mtb:scale" text;
    ''')
    auxiliary_cursor.execute('''
        ALTER TABLE planet_osm_routes2
          ADD "mtb:scale:uphill" text;
    ''')
    auxiliary_cursor.execute('''
        ALTER TABLE planet_osm_routes2
          ADD offsetSide integer;
    ''')

    # Add columns for maximum number of osmcSymbols
    for column in range(max_signs):
        auxiliary_cursor.execute('''
            ALTER TABLE planet_osm_routes2
              ADD osmcSymbol%s text;
        ''' % (str(column)))
        auxiliary_cursor.execute('''
            ALTER TABLE planet_osm_routes2
              ADD network%s text;
        ''' % (str(column)))

    # Insert route values into the table
    for r in list_of_routes:
        if r.geometry is not None:
            row = r.get_values_row()
            auxiliary_cursor.execute('''
                INSERT INTO planet_osm_routes2
                  VALUES (%s)
            ''' % row)
    logger.info('Finished inserting routes into new table.')

    logger.info('Relations: %i' % len(relations))
    logger.info('Max Signs: %i' % max_signs)
    logger.info('Routes: %i' % len(routes))
    logger.info('Nodes: %i' % len(nodes))

    # commit the result into the database
    auxiliary_cursor.close()
    connection.commit()

    logger.info('Relations2lines finished successfully.')

Example 27

Project: calibre
Source File: lrf_input.py
View license
    def convert(self, stream, options, file_ext, log,
                accelerators):
        from lxml import etree
        from calibre.ebooks.lrf.input import (MediaType, Styles, TextBlock,
                Canvas, ImageBlock, RuledLine)
        self.log = log
        self.log('Generating XML')
        from calibre.ebooks.lrf.lrfparser import LRFDocument
        d = LRFDocument(stream)
        d.parse()
        xml = d.to_xml(write_files=True)
        if options.verbose > 2:
            open(u'lrs.xml', 'wb').write(xml.encode('utf-8'))
        parser = etree.XMLParser(no_network=True, huge_tree=True)
        try:
            doc = etree.fromstring(xml, parser=parser)
        except:
            self.log.warn('Failed to parse XML. Trying to recover')
            parser = etree.XMLParser(no_network=True, huge_tree=True,
                    recover=True)
            doc = etree.fromstring(xml, parser=parser)

        char_button_map = {}
        for x in doc.xpath('//CharButton[@refobj]'):
            ro = x.get('refobj')
            jump_button = doc.xpath('//*[@objid="%s"]'%ro)
            if jump_button:
                jump_to = jump_button[0].xpath('descendant::JumpTo[@refpage and @refobj]')
                if jump_to:
                    char_button_map[ro] = '%s.xhtml#%s'%(jump_to[0].get('refpage'),
                            jump_to[0].get('refobj'))
        plot_map = {}
        for x in doc.xpath('//Plot[@refobj]'):
            ro = x.get('refobj')
            image = doc.xpath('//Image[@objid="%s" and @refstream]'%ro)
            if image:
                imgstr = doc.xpath('//ImageStream[@objid="%s" and @file]'%
                    image[0].get('refstream'))
                if imgstr:
                    plot_map[ro] = imgstr[0].get('file')

        self.log('Converting XML to HTML...')
        styledoc = etree.fromstring(P('templates/lrf.xsl', data=True))
        media_type = MediaType()
        styles = Styles()
        text_block = TextBlock(styles, char_button_map, plot_map, log)
        canvas = Canvas(doc, styles, text_block, log)
        image_block = ImageBlock(canvas)
        ruled_line = RuledLine()
        extensions = {
                ('calibre', 'media-type') : media_type,
                ('calibre', 'text-block') : text_block,
                ('calibre', 'ruled-line') : ruled_line,
                ('calibre', 'styles')     : styles,
                ('calibre', 'canvas')     : canvas,
                ('calibre', 'image-block'): image_block,
                }
        transform = etree.XSLT(styledoc, extensions=extensions)
        try:
            result = transform(doc)
        except RuntimeError:
            sys.setrecursionlimit(5000)
            result = transform(doc)

        with open('content.opf', 'wb') as f:
            f.write(result)
        styles.write()
        return os.path.abspath(u'content.opf')

Example 28

Project: rpc-openstack
Source File: rpc-maas-tool.py
View license
    def _parse_alarm_criteria(self, alarm):
        """Use the waxeye generated parser to parse the alarm critera DSL"""
        try:
            # Waxeye requires deep recurssion, 10000 stack frames should
            # use about 5mb of memory excluding stored data.
            sys.setrecursionlimit(10000)
            p = alarmparser.Parser()
            ast = p.parse(alarm['criteria'])
            if ast.__class__.__name__ == 'AST':
                return ast
            else:
                raise ParseException(
                    "Cannot parse alarm criteria: {alarm} Error: {ast}"
                    .format(alarm=alarm['label'],
                            ast=ast), alarm=alarm)
        except RuntimeError as e:
            message = ("Failed to parse {name}: {criteria}."
                       " Message: {message}"
                       .format(name=alarm['name'],
                               criteria=alarm['criteria'],
                               message=e.message))
            raise ParseException(message, alarm=alarm)

Example 29

Project: worldengine
Source File: main.py
View license
def main():
    parser = ArgumentParser(
        usage="usage: %(prog)s [options] [" + OPERATIONS + "]")
    parser.add_argument('OPERATOR', nargs='?')
    parser.add_argument('FILE', nargs='?')
    parser.add_argument(
        '-o', '--output-dir', dest='output_dir',
        help="generate files in DIR [default = '%(default)s']",
        metavar="DIR", default='.')
    parser.add_argument(
        '-n', '--worldname', dest='world_name',
        help="set world name to STR. output is stored in a " +
             "world file with the name format 'STR.world'. If " +
             "a name is not provided, then seed_N.world, " +
             "where N=SEED",
        metavar="STR")
    parser.add_argument('--hdf5', dest='hdf5',
                        action="store_true",
                        help="Save world file using HDF5 format. " +
                             "Default = store using protobuf format",
                        default=False)
    parser.add_argument('-s', '--seed', dest='seed', type=int,
                        help="Use seed=N to initialize the pseudo-random " +
                             "generation. If not provided, one will be " +
                             "selected for you.",
                        metavar="N")
    parser.add_argument('-t', '--step', dest='step',
                        help="Use step=[" + STEPS + "] to specify how far " +
                             "to proceed in the world generation process. " +
                             "[default='%(default)s']",
                        metavar="STR", default="full")
    # TODO --step appears to be duplicate of OPERATIONS. Especially if
    # ancient_map is added to --step
    parser.add_argument('-x', '--width', dest='width', type=int,
                        help="N = width of the world to be generated " +
                             "[default=%(default)s]",
                        metavar="N",
                        default='512')
    parser.add_argument('-y', '--height', dest='height', type=int,
                        help="N = height of the world to be generated " +
                             "[default=%(default)s]",
                        metavar="N",
                        default='512')
    parser.add_argument('-q', '--number-of-plates', dest='number_of_plates',
                        type=int,
                        help="N = number of plates [default = %(default)s]",
                        metavar="N", default='10')
    parser.add_argument('--recursion_limit', dest='recursion_limit', type=int,
                        help="Set the recursion limit [default = %(default)s]",
                        metavar="N", default='2000')
    parser.add_argument('-v', '--verbose', dest='verbose', action="store_true",
                        help="Enable verbose messages", default=False)
    parser.add_argument('--version', dest='version', action="store_true",
                        help="Display version information", default=False)
    parser.add_argument('--bw', '--black-and-white', dest='black_and_white',
                        action="store_true",
                        help="generate maps in black and white",
                        default=False)

    # -----------------------------------------------------
    g_generate = parser.add_argument_group(
        "Generate Options", "These options are only useful in plate and " +
        "world modes")
    g_generate.add_argument('-r', '--rivers', dest='rivers_map',
                            action="store_true", help="generate rivers map")
    g_generate.add_argument('--gs', '--grayscale-heightmap',
                            dest='grayscale_heightmap', action="store_true",
                            help='produce a grayscale heightmap')
    g_generate.add_argument('--ocean_level', dest='ocean_level', type=float,
                            help='elevation cut off for sea level " +'
                                 '[default = %(default)s]',
                            metavar="N", default=1.0)
    g_generate.add_argument('--temps', dest='temps', 
                        help="Provide alternate ranges for temperatures. " +
                             "If not provided, the default values will be used. \n" +
                             "[default = .126/.235/.406/.561/.634/.876]",
                            metavar="#/#/#/#/#/#")
    g_generate.add_argument('--humidity', dest='humids', 
                        help="Provide alternate ranges for humidities. " +
                             "If not provided, the default values will be used. \n" +
                            "[default = .059/.222/.493/.764/.927/.986/.998]",
                            metavar="#/#/#/#/#/#/#")
    g_generate.add_argument('-gv', '--gamma-value', dest='gv', type=float,
                        help="N = Gamma value for temperature/precipitation " +
                             "gamma correction curve. [default = %(default)s]",
                        metavar="N", default='1.25')
    g_generate.add_argument('-go', '--gamma-offset', dest='go', type=float,
                        help="N = Adjustment value for temperature/precipitation " +
                             "gamma correction curve. [default = %(default)s]",
                        metavar="N", default='.2')
    g_generate.add_argument('--not-fade-borders', dest='fade_borders', action="store_false",
                               help="Not fade borders",
                               default=True)
    g_generate.add_argument('--scatter', dest='scatter_plot',
                            action="store_true", help="generate scatter plot")
    g_generate.add_argument('--sat', dest='satelite_map',
                            action="store_true", help="generate satellite map")
    g_generate.add_argument('--ice', dest='icecaps_map',
                            action="store_true", help="generate ice caps map")

    # -----------------------------------------------------
    g_ancient_map = parser.add_argument_group(
        "Ancient Map Options", "These options are only useful in " +
        "ancient_map mode")
    g_ancient_map.add_argument('-w', '--worldfile', dest='world_file',
                               help="FILE to be loaded", metavar="FILE")
    g_ancient_map.add_argument('-g', '--generatedfile', dest='generated_file',
                               help="name of the FILE", metavar="FILE")
    g_ancient_map.add_argument(
        '-f', '--resize-factor', dest='resize_factor', type=int,
        help="resize factor (only integer values). " +
             "Note this can only be used to increase " +
             "the size of the map [default=%(default)s]",
        metavar="N", default='1')
    g_ancient_map.add_argument('--sea_color', dest='sea_color',
                               help="string for color [" + SEA_COLORS + "]",
                               metavar="S", default="brown")

    g_ancient_map.add_argument('--not-draw-biome', dest='draw_biome',
                               action="store_false",
                               help="Not draw biome",
                               default=True)
    g_ancient_map.add_argument('--not-draw-mountains', dest='draw_mountains',
                               action="store_false",
                               help="Not draw mountains",
                               default=True)
    g_ancient_map.add_argument('--not-draw-rivers', dest='draw_rivers',
                               action="store_false",
                               help="Not draw rivers",
                               default=True)
    g_ancient_map.add_argument('--draw-outer-border', dest='draw_outer_border',
                               action="store_true",
                               help="Draw outer land border",
                               default=False)
    # TODO: allow for RGB specification as [r g b], ie [0.5 0.5 0.5] for gray

    # -----------------------------------------------------
    export_options = parser.add_argument_group(
        "Export Options", "You can specify the formats you wish the generated output to be in. ")
    export_options.add_argument("--export-format", dest="export_format", type=str,
                                help="Export to a specific format such as BMP or PNG. " +
                                "See http://www.gdal.org/formats_list.html for possible formats.",
                                default="PNG", metavar="STR")
    export_options.add_argument("--export-datatype", dest="export_datatype", type=str,
                                help="Type of stored data, e.g. uint16, int32, float32 etc.",
                                default="uint16", metavar="STR")

    args = parser.parse_args()

    if args.version:
        usage()

    if os.path.exists(args.output_dir):
        if not os.path.isdir(args.output_dir):
            raise Exception("Output dir exists but it is not a dir")
    else:
        print('Directory does not exist, we are creating it')
        os.makedirs(args.output_dir)

    # it needs to be increased to be able to generate very large maps
    # the limit is hit when drawing ancient maps
    sys.setrecursionlimit(args.recursion_limit)

    if args.number_of_plates < 1 or args.number_of_plates > 100:
        usage(error="Number of plates should be in [1, 100]")

    if args.hdf5 and not HDF5_AVAILABLE:
        usage(error="HDF5 requires the presence of native libraries")

    operation = "world"
    if args.OPERATOR is None:
        pass
    elif args.OPERATOR is not None and args.OPERATOR.lower() not in OPERATIONS:
        parser.print_help()
        usage("Only 1 operation allowed [" + OPERATIONS + "]")
    else:
        operation = args.OPERATOR.lower()

    if args.OPERATOR == 'info' or args.OPERATOR == 'export':
        if args.FILE is None:
            parser.print_help()
            usage("For operation info only the filename should be specified")
        if not os.path.exists(args.FILE):
            usage("The specified world file does not exist")

    maxseed = numpy.iinfo(numpy.uint16).max  # there is a hard limit somewhere so seeds outside the uint16 range are considered unsafe
    if args.seed is not None:
        seed = int(args.seed)
        assert 0 <= seed <= maxseed, "Seed has to be in the range between 0 and %s, borders included." % maxseed
    else:
        seed = numpy.random.randint(0, maxseed)  # first-time RNG initialization is done automatically
    numpy.random.seed(seed)

    if args.world_name:
        world_name = args.world_name
    else:
        world_name = "seed_%i" % seed

    step = check_step(args.step)

    world_format = 'protobuf'
    if args.hdf5:
        world_format = 'hdf5'

    generation_operation = (operation == 'world') or (operation == 'plates')

    if args.grayscale_heightmap and not generation_operation:
        usage(
            error="Grayscale heightmap can be produced only during world " +
                  "generation")

    if args.rivers_map and not generation_operation:
        usage(error="Rivers map can be produced only during world generation")

    if args.temps and not generation_operation:
        usage(error="temps can be assigned only during world generation")

    if args.temps and len(args.temps.split('/')) is not 6:
        usage(error="temps must have exactly 6 values")

    if args.go >= 1 or args.go < 0:
        usage(error="Gamma offset must be greater than or equal to 0 and less than 1")

    if args.gv <= 0:
        usage(error="Gamma value must be greater than 0")

    temps = [.874, .765, .594, .439, .366, .124]
    if args.temps:
        temps = args.temps.split('/')
        for x in range(0,len(temps)):
            temps[x] = 1 - float(temps[x])

    if args.humids and not generation_operation:
        usage(error="humidity can be assigned only during world generation")

    if args.humids and len(args.humids.split('/')) is not 7:
        usage(error="humidity must have exactly 7 values")

    humids = [.941, .778, .507, .236, 0.073, .014, .002]
    if args.humids:
        humids = args.humids.split('/')
        for x in range(0,len(humids)):
            humids[x] = 1 - float(humids[x])
    if args.scatter_plot and not generation_operation:
        usage(error="Scatter plot can be produced only during world generation")

    print('Worldengine - a world generator (v. %s)' % VERSION)
    print('-----------------------')
    if generation_operation:
        print(' operation            : %s generation' % operation)
        print(' seed                 : %i' % seed)
        print(' name                 : %s' % world_name)
        print(' width                : %i' % args.width)
        print(' height               : %i' % args.height)
        print(' number of plates     : %i' % args.number_of_plates)
        print(' world format         : %s' % world_format)
        print(' black and white maps : %s' % args.black_and_white)
        print(' step                 : %s' % step.name)
        print(' greyscale heightmap  : %s' % args.grayscale_heightmap)
        print(' icecaps heightmap    : %s' % args.icecaps_map)
        print(' rivers map           : %s' % args.rivers_map)
        print(' scatter plot         : %s' % args.scatter_plot)
        print(' satellite map        : %s' % args.satelite_map)
        print(' fade borders         : %s' % args.fade_borders)
        if args.temps:
            print(' temperature ranges   : %s' % args.temps)
        if args.humids:
            print(' humidity ranges      : %s' % args.humids)
        print(' gamma value          : %s' % args.gv)
        print(' gamma offset         : %s' % args.go)
    if operation == 'ancient_map':
        print(' operation              : %s generation' % operation)
        print(' resize factor          : %i' % args.resize_factor)
        print(' world file             : %s' % args.world_file)
        print(' sea color              : %s' % args.sea_color)
        print(' draw biome             : %s' % args.draw_biome)
        print(' draw rivers            : %s' % args.draw_rivers)
        print(' draw mountains         : %s' % args.draw_mountains)
        print(' draw land outer border : %s' % args.draw_outer_border)
        
    #Warning messages
    warnings = []
    if temps != sorted(temps, reverse=True):
        warnings.append("WARNING: Temperature array not in ascending order")
    if numpy.amin(temps) < 0:
        warnings.append("WARNING: Maximum value in temperature array greater than 1")
    if numpy.amax(temps) > 1:
        warnings.append("WARNING: Minimum value in temperature array less than 0")
    if humids != sorted(humids, reverse=True):
        warnings.append("WARNING: Humidity array not in ascending order")
    if numpy.amin(humids) < 0:
        warnings.append("WARNING: Maximum value in humidity array greater than 1")
    if numpy.amax(humids) > 1:
        warnings.append("WARNING: Minimum value in temperature array less than 0")

    if warnings:
        print("\n")
        for x in range(len(warnings)):
            print(warnings[x])

    set_verbose(args.verbose)

    if operation == 'world':
        print('')  # empty line
        print('starting (it could take a few minutes) ...')

        world = generate_world(world_name, args.width, args.height,
                               seed, args.number_of_plates, args.output_dir,
                               step, args.ocean_level, temps, humids, world_format,
                               gamma_curve=args.gv,curve_offset=args.go,
                               fade_borders=args.fade_borders,
                               verbose=args.verbose, black_and_white=args.black_and_white)
        if args.grayscale_heightmap:
            generate_grayscale_heightmap(world,
            '%s/%s_grayscale.png' % (args.output_dir, world_name))
        if args.rivers_map:
            generate_rivers_map(world,
            '%s/%s_rivers.png' % (args.output_dir, world_name))
        if args.scatter_plot:
            draw_scatter_plot(world,
            '%s/%s_scatter.png' % (args.output_dir, world_name))    
        if args.satelite_map:
            draw_satellite_map(world,
            '%s/%s_satellite.png' % (args.output_dir, world_name))
        if args.icecaps_map:
            draw_icecaps_map(world,
            '%s/%s_icecaps.png' % (args.output_dir, world_name))

    elif operation == 'plates':
        print('')  # empty line
        print('starting (it could take a few minutes) ...')

        generate_plates(seed, world_name, args.output_dir, args.width,
                        args.height, num_plates=args.number_of_plates)

    elif operation == 'ancient_map':
        print('')  # empty line
        print('starting (it could take a few minutes) ...')
        # First, some error checking
        if args.sea_color == "blue":
            sea_color = (142, 162, 179, 255)
        elif args.sea_color == "brown":
            sea_color = (212, 198, 169, 255)
        else:
            usage("Unknown sea color: " + args.sea_color +
                  " Select from [" + SEA_COLORS + "]")
        if not args.world_file:
            usage(
                "For generating an ancient map is necessary to specify the " +
                "world to be used (-w option)")
        world = load_world(args.world_file)

        print_verbose(" * world loaded")

        if not args.generated_file:
            args.generated_file = "ancient_map_%s.png" % world.name
        operation_ancient_map(world, args.generated_file,
                              args.resize_factor, sea_color,
                              args.draw_biome, args.draw_rivers,
                              args.draw_mountains, args.draw_outer_border)
    elif operation == 'info':
        world = load_world(args.FILE)
        print_world_info(world)
    elif operation == 'export':
        world = load_world(args.FILE)
        print_world_info(world)
        export(world, args.export_format, args.export_datatype,
               path = '%s/%s_elevation' % (args.output_dir, world_name))
    else:
        raise Exception(
            'Unknown operation: valid operations are %s' % OPERATIONS)

    print('...done')

Example 30

Project: worldengine
Source File: main.py
View license
def main():
    parser = ArgumentParser(
        usage="usage: %(prog)s [options] [" + OPERATIONS + "]")
    parser.add_argument('OPERATOR', nargs='?')
    parser.add_argument('FILE', nargs='?')
    parser.add_argument(
        '-o', '--output-dir', dest='output_dir',
        help="generate files in DIR [default = '%(default)s']",
        metavar="DIR", default='.')
    parser.add_argument(
        '-n', '--worldname', dest='world_name',
        help="set world name to STR. output is stored in a " +
             "world file with the name format 'STR.world'. If " +
             "a name is not provided, then seed_N.world, " +
             "where N=SEED",
        metavar="STR")
    parser.add_argument('--hdf5', dest='hdf5',
                        action="store_true",
                        help="Save world file using HDF5 format. " +
                             "Default = store using protobuf format",
                        default=False)
    parser.add_argument('-s', '--seed', dest='seed', type=int,
                        help="Use seed=N to initialize the pseudo-random " +
                             "generation. If not provided, one will be " +
                             "selected for you.",
                        metavar="N")
    parser.add_argument('-t', '--step', dest='step',
                        help="Use step=[" + STEPS + "] to specify how far " +
                             "to proceed in the world generation process. " +
                             "[default='%(default)s']",
                        metavar="STR", default="full")
    # TODO --step appears to be duplicate of OPERATIONS. Especially if
    # ancient_map is added to --step
    parser.add_argument('-x', '--width', dest='width', type=int,
                        help="N = width of the world to be generated " +
                             "[default=%(default)s]",
                        metavar="N",
                        default='512')
    parser.add_argument('-y', '--height', dest='height', type=int,
                        help="N = height of the world to be generated " +
                             "[default=%(default)s]",
                        metavar="N",
                        default='512')
    parser.add_argument('-q', '--number-of-plates', dest='number_of_plates',
                        type=int,
                        help="N = number of plates [default = %(default)s]",
                        metavar="N", default='10')
    parser.add_argument('--recursion_limit', dest='recursion_limit', type=int,
                        help="Set the recursion limit [default = %(default)s]",
                        metavar="N", default='2000')
    parser.add_argument('-v', '--verbose', dest='verbose', action="store_true",
                        help="Enable verbose messages", default=False)
    parser.add_argument('--version', dest='version', action="store_true",
                        help="Display version information", default=False)
    parser.add_argument('--bw', '--black-and-white', dest='black_and_white',
                        action="store_true",
                        help="generate maps in black and white",
                        default=False)

    # -----------------------------------------------------
    g_generate = parser.add_argument_group(
        "Generate Options", "These options are only useful in plate and " +
        "world modes")
    g_generate.add_argument('-r', '--rivers', dest='rivers_map',
                            action="store_true", help="generate rivers map")
    g_generate.add_argument('--gs', '--grayscale-heightmap',
                            dest='grayscale_heightmap', action="store_true",
                            help='produce a grayscale heightmap')
    g_generate.add_argument('--ocean_level', dest='ocean_level', type=float,
                            help='elevation cut off for sea level " +'
                                 '[default = %(default)s]',
                            metavar="N", default=1.0)
    g_generate.add_argument('--temps', dest='temps', 
                        help="Provide alternate ranges for temperatures. " +
                             "If not provided, the default values will be used. \n" +
                             "[default = .126/.235/.406/.561/.634/.876]",
                            metavar="#/#/#/#/#/#")
    g_generate.add_argument('--humidity', dest='humids', 
                        help="Provide alternate ranges for humidities. " +
                             "If not provided, the default values will be used. \n" +
                            "[default = .059/.222/.493/.764/.927/.986/.998]",
                            metavar="#/#/#/#/#/#/#")
    g_generate.add_argument('-gv', '--gamma-value', dest='gv', type=float,
                        help="N = Gamma value for temperature/precipitation " +
                             "gamma correction curve. [default = %(default)s]",
                        metavar="N", default='1.25')
    g_generate.add_argument('-go', '--gamma-offset', dest='go', type=float,
                        help="N = Adjustment value for temperature/precipitation " +
                             "gamma correction curve. [default = %(default)s]",
                        metavar="N", default='.2')
    g_generate.add_argument('--not-fade-borders', dest='fade_borders', action="store_false",
                               help="Not fade borders",
                               default=True)
    g_generate.add_argument('--scatter', dest='scatter_plot',
                            action="store_true", help="generate scatter plot")
    g_generate.add_argument('--sat', dest='satelite_map',
                            action="store_true", help="generate satellite map")
    g_generate.add_argument('--ice', dest='icecaps_map',
                            action="store_true", help="generate ice caps map")

    # -----------------------------------------------------
    g_ancient_map = parser.add_argument_group(
        "Ancient Map Options", "These options are only useful in " +
        "ancient_map mode")
    g_ancient_map.add_argument('-w', '--worldfile', dest='world_file',
                               help="FILE to be loaded", metavar="FILE")
    g_ancient_map.add_argument('-g', '--generatedfile', dest='generated_file',
                               help="name of the FILE", metavar="FILE")
    g_ancient_map.add_argument(
        '-f', '--resize-factor', dest='resize_factor', type=int,
        help="resize factor (only integer values). " +
             "Note this can only be used to increase " +
             "the size of the map [default=%(default)s]",
        metavar="N", default='1')
    g_ancient_map.add_argument('--sea_color', dest='sea_color',
                               help="string for color [" + SEA_COLORS + "]",
                               metavar="S", default="brown")

    g_ancient_map.add_argument('--not-draw-biome', dest='draw_biome',
                               action="store_false",
                               help="Not draw biome",
                               default=True)
    g_ancient_map.add_argument('--not-draw-mountains', dest='draw_mountains',
                               action="store_false",
                               help="Not draw mountains",
                               default=True)
    g_ancient_map.add_argument('--not-draw-rivers', dest='draw_rivers',
                               action="store_false",
                               help="Not draw rivers",
                               default=True)
    g_ancient_map.add_argument('--draw-outer-border', dest='draw_outer_border',
                               action="store_true",
                               help="Draw outer land border",
                               default=False)
    # TODO: allow for RGB specification as [r g b], ie [0.5 0.5 0.5] for gray

    # -----------------------------------------------------
    export_options = parser.add_argument_group(
        "Export Options", "You can specify the formats you wish the generated output to be in. ")
    export_options.add_argument("--export-format", dest="export_format", type=str,
                                help="Export to a specific format such as BMP or PNG. " +
                                "See http://www.gdal.org/formats_list.html for possible formats.",
                                default="PNG", metavar="STR")
    export_options.add_argument("--export-datatype", dest="export_datatype", type=str,
                                help="Type of stored data, e.g. uint16, int32, float32 etc.",
                                default="uint16", metavar="STR")

    args = parser.parse_args()

    if args.version:
        usage()

    if os.path.exists(args.output_dir):
        if not os.path.isdir(args.output_dir):
            raise Exception("Output dir exists but it is not a dir")
    else:
        print('Directory does not exist, we are creating it')
        os.makedirs(args.output_dir)

    # it needs to be increased to be able to generate very large maps
    # the limit is hit when drawing ancient maps
    sys.setrecursionlimit(args.recursion_limit)

    if args.number_of_plates < 1 or args.number_of_plates > 100:
        usage(error="Number of plates should be in [1, 100]")

    if args.hdf5 and not HDF5_AVAILABLE:
        usage(error="HDF5 requires the presence of native libraries")

    operation = "world"
    if args.OPERATOR is None:
        pass
    elif args.OPERATOR is not None and args.OPERATOR.lower() not in OPERATIONS:
        parser.print_help()
        usage("Only 1 operation allowed [" + OPERATIONS + "]")
    else:
        operation = args.OPERATOR.lower()

    if args.OPERATOR == 'info' or args.OPERATOR == 'export':
        if args.FILE is None:
            parser.print_help()
            usage("For operation info only the filename should be specified")
        if not os.path.exists(args.FILE):
            usage("The specified world file does not exist")

    maxseed = numpy.iinfo(numpy.uint16).max  # there is a hard limit somewhere so seeds outside the uint16 range are considered unsafe
    if args.seed is not None:
        seed = int(args.seed)
        assert 0 <= seed <= maxseed, "Seed has to be in the range between 0 and %s, borders included." % maxseed
    else:
        seed = numpy.random.randint(0, maxseed)  # first-time RNG initialization is done automatically
    numpy.random.seed(seed)

    if args.world_name:
        world_name = args.world_name
    else:
        world_name = "seed_%i" % seed

    step = check_step(args.step)

    world_format = 'protobuf'
    if args.hdf5:
        world_format = 'hdf5'

    generation_operation = (operation == 'world') or (operation == 'plates')

    if args.grayscale_heightmap and not generation_operation:
        usage(
            error="Grayscale heightmap can be produced only during world " +
                  "generation")

    if args.rivers_map and not generation_operation:
        usage(error="Rivers map can be produced only during world generation")

    if args.temps and not generation_operation:
        usage(error="temps can be assigned only during world generation")

    if args.temps and len(args.temps.split('/')) is not 6:
        usage(error="temps must have exactly 6 values")

    if args.go >= 1 or args.go < 0:
        usage(error="Gamma offset must be greater than or equal to 0 and less than 1")

    if args.gv <= 0:
        usage(error="Gamma value must be greater than 0")

    temps = [.874, .765, .594, .439, .366, .124]
    if args.temps:
        temps = args.temps.split('/')
        for x in range(0,len(temps)):
            temps[x] = 1 - float(temps[x])

    if args.humids and not generation_operation:
        usage(error="humidity can be assigned only during world generation")

    if args.humids and len(args.humids.split('/')) is not 7:
        usage(error="humidity must have exactly 7 values")

    humids = [.941, .778, .507, .236, 0.073, .014, .002]
    if args.humids:
        humids = args.humids.split('/')
        for x in range(0,len(humids)):
            humids[x] = 1 - float(humids[x])
    if args.scatter_plot and not generation_operation:
        usage(error="Scatter plot can be produced only during world generation")

    print('Worldengine - a world generator (v. %s)' % VERSION)
    print('-----------------------')
    if generation_operation:
        print(' operation            : %s generation' % operation)
        print(' seed                 : %i' % seed)
        print(' name                 : %s' % world_name)
        print(' width                : %i' % args.width)
        print(' height               : %i' % args.height)
        print(' number of plates     : %i' % args.number_of_plates)
        print(' world format         : %s' % world_format)
        print(' black and white maps : %s' % args.black_and_white)
        print(' step                 : %s' % step.name)
        print(' greyscale heightmap  : %s' % args.grayscale_heightmap)
        print(' icecaps heightmap    : %s' % args.icecaps_map)
        print(' rivers map           : %s' % args.rivers_map)
        print(' scatter plot         : %s' % args.scatter_plot)
        print(' satellite map        : %s' % args.satelite_map)
        print(' fade borders         : %s' % args.fade_borders)
        if args.temps:
            print(' temperature ranges   : %s' % args.temps)
        if args.humids:
            print(' humidity ranges      : %s' % args.humids)
        print(' gamma value          : %s' % args.gv)
        print(' gamma offset         : %s' % args.go)
    if operation == 'ancient_map':
        print(' operation              : %s generation' % operation)
        print(' resize factor          : %i' % args.resize_factor)
        print(' world file             : %s' % args.world_file)
        print(' sea color              : %s' % args.sea_color)
        print(' draw biome             : %s' % args.draw_biome)
        print(' draw rivers            : %s' % args.draw_rivers)
        print(' draw mountains         : %s' % args.draw_mountains)
        print(' draw land outer border : %s' % args.draw_outer_border)
        
    #Warning messages
    warnings = []
    if temps != sorted(temps, reverse=True):
        warnings.append("WARNING: Temperature array not in ascending order")
    if numpy.amin(temps) < 0:
        warnings.append("WARNING: Maximum value in temperature array greater than 1")
    if numpy.amax(temps) > 1:
        warnings.append("WARNING: Minimum value in temperature array less than 0")
    if humids != sorted(humids, reverse=True):
        warnings.append("WARNING: Humidity array not in ascending order")
    if numpy.amin(humids) < 0:
        warnings.append("WARNING: Maximum value in humidity array greater than 1")
    if numpy.amax(humids) > 1:
        warnings.append("WARNING: Minimum value in temperature array less than 0")

    if warnings:
        print("\n")
        for x in range(len(warnings)):
            print(warnings[x])

    set_verbose(args.verbose)

    if operation == 'world':
        print('')  # empty line
        print('starting (it could take a few minutes) ...')

        world = generate_world(world_name, args.width, args.height,
                               seed, args.number_of_plates, args.output_dir,
                               step, args.ocean_level, temps, humids, world_format,
                               gamma_curve=args.gv,curve_offset=args.go,
                               fade_borders=args.fade_borders,
                               verbose=args.verbose, black_and_white=args.black_and_white)
        if args.grayscale_heightmap:
            generate_grayscale_heightmap(world,
            '%s/%s_grayscale.png' % (args.output_dir, world_name))
        if args.rivers_map:
            generate_rivers_map(world,
            '%s/%s_rivers.png' % (args.output_dir, world_name))
        if args.scatter_plot:
            draw_scatter_plot(world,
            '%s/%s_scatter.png' % (args.output_dir, world_name))    
        if args.satelite_map:
            draw_satellite_map(world,
            '%s/%s_satellite.png' % (args.output_dir, world_name))
        if args.icecaps_map:
            draw_icecaps_map(world,
            '%s/%s_icecaps.png' % (args.output_dir, world_name))

    elif operation == 'plates':
        print('')  # empty line
        print('starting (it could take a few minutes) ...')

        generate_plates(seed, world_name, args.output_dir, args.width,
                        args.height, num_plates=args.number_of_plates)

    elif operation == 'ancient_map':
        print('')  # empty line
        print('starting (it could take a few minutes) ...')
        # First, some error checking
        if args.sea_color == "blue":
            sea_color = (142, 162, 179, 255)
        elif args.sea_color == "brown":
            sea_color = (212, 198, 169, 255)
        else:
            usage("Unknown sea color: " + args.sea_color +
                  " Select from [" + SEA_COLORS + "]")
        if not args.world_file:
            usage(
                "For generating an ancient map is necessary to specify the " +
                "world to be used (-w option)")
        world = load_world(args.world_file)

        print_verbose(" * world loaded")

        if not args.generated_file:
            args.generated_file = "ancient_map_%s.png" % world.name
        operation_ancient_map(world, args.generated_file,
                              args.resize_factor, sea_color,
                              args.draw_biome, args.draw_rivers,
                              args.draw_mountains, args.draw_outer_border)
    elif operation == 'info':
        world = load_world(args.FILE)
        print_world_info(world)
    elif operation == 'export':
        world = load_world(args.FILE)
        print_world_info(world)
        export(world, args.export_format, args.export_datatype,
               path = '%s/%s_elevation' % (args.output_dir, world_name))
    else:
        raise Exception(
            'Unknown operation: valid operations are %s' % OPERATIONS)

    print('...done')

Example 31

Project: mtbmap
Source File: relations2lines.py
View license
def run(db_name, user, host, port):
    logger.info('Relations2lines script started')
    logger.info('  Searching RelationIDs and Lines in planet_osm_line...')
    # Create connection to DB server.
    connection = connect("dbname='{db_name}' user='{user}' password='' host='{host}' port='{port}'"
                         .format(db_name=db_name, user=user, host=host, port=port))
    relation_cursor = connection.cursor()
    auxiliary_cursor = connection.cursor()
    way_cursor = connection.cursor()

    # Find relation IDs to be parsed, ie. those with osmc:symbol or some mtb values
    # Treat lines with useful attributes as relations (osm_id >= 0)
    relation_ids = []
    relations = []
    relation_cursor.execute('''
        SELECT osm_id, CASE WHEN ("highway"='track' AND "tracktype"='grade1' AND "mtb:scale" IS NULL) THEN 'grade1'
                            ELSE "mtb:scale"
                       END AS "mtb:scale"
            , "mtb:scale:uphill", network, "osmc:symbol"
            FROM planet_osm_line
            WHERE ("osmc:symbol" IS NOT NULL OR kct_red IS NOT NULL
                OR kct_blue IS NOT NULL OR kct_green IS NOT NULL
                OR kct_yellow IS NOT NULL
                OR ("mtb:scale" IS NOT NULL
                  AND (("access"<>'private' AND "access"<>'no') OR "access" IS NULL OR ("access" IN ('private', 'no') AND bicycle='yes')))
                OR "mtb:scale:uphill" IS NOT NULL OR ("highway"='track' AND "tracktype"='grade1'))
        ''')
    while True:
        # Fetch some of the result.
        rows = relation_cursor.fetchmany(100)

        # Empty result means end of query.
        if not rows:
            break

        # relations have negative osm_id in planet_osm_line table
        # lines have positive osm_id in planet_osm_line table
        for row in rows:
            if row[0] < 0:
                # osm_id is not a primary key
                if not (row[0] in relation_ids):
                    relation_ids.append(-row[0])
            else:
                # 0: osm_id; 1: mtb:scale; 2: mtb:scale:uphill; 3: network; 4: "osmc:symbol"
                line_info = ("LINE;" + str(row[0]) + ";" + str(row[1]) + ";" + str(row[2]) + ";"
                             + str(row[3]) + ";" + str(row[4]))
                relations.append(Relation(line_info))

    logger.info('RelationIDs and Lines found.')
    logger.info('Getting Relation details from planet_osm_rels...')

    # Select important columns just for our IDs
    for r_id in relation_ids:
        relation_cursor.execute('''
            SELECT id, members, tags
                FROM planet_osm_rels
                WHERE id=%s
        ''' % r_id)
        row = relation_cursor.fetchone()
        # Make Relation object with parsed data
        relations.append(Relation(row))

    logger.info('Relations details found.')
    logger.info('Making single routes from relations with all osmc:symbols...')

    # Find final routes and append all corresponding osmcSymbols
    routes = routes_from_rels(relations)

    list_of_routes = routes.values()
    list_of_routes.sort()
    logger.info('Routes now have osmc:symbols.')
    logger.info('Finding firstNode and lastNode for each route in planet_osm_ways...')

    # Clean previous routes.
    auxiliary_cursor.execute("DROP TABLE IF EXISTS planet_osm_routes2")
    auxiliary_cursor.execute("DELETE FROM geometry_columns WHERE f_table_name = 'planet_osm_routes2'")
    auxiliary_cursor.execute("CREATE TABLE planet_osm_routes2 AS SELECT osm_id, way, highway, tracktype" +
                             " FROM planet_osm_line WHERE osm_id = 0")
    auxiliary_cursor.execute("DELETE FROM geometry_columns WHERE f_table_name = 'planet_osm_routes2'")
    auxiliary_cursor.execute("INSERT INTO geometry_columns VALUES ('', 'public', 'planet_osm_routes2'," +
                             " 'way', 2, 3857, 'LINESTRING')")

    # Add important information to each route
    for r in list_of_routes:
        auxiliary_cursor.execute('''
            SELECT way, highway, tracktype FROM planet_osm_line
              WHERE osm_id=%s AND (("access"<>'private' AND "access"<>'no') OR "access" IS NULL
                OR ("access" IN ('private', 'no') AND bicycle='yes'))
        ''' % r.id)
        row = auxiliary_cursor.fetchone()
        # Some route IDs from relations may not be present in line table, ie. out of bounding box, those are ignored
        if row is not None:
            routes[r.id].geometry = row[0]
            routes[r.id].highway = row[1]
            routes[r.id].tracktype = row[2]
            way_cursor.execute('''
                SELECT nodes[1], nodes[array_upper(nodes, 1)]
                    FROM planet_osm_ways
                    WHERE id=%s
            ''' % r.id)
            first_end_nodes = way_cursor.fetchone()
            routes[r.id].firstNode = first_end_nodes[0]
            routes[r.id].lastNode = first_end_nodes[1]
        else:
            routes.pop(r.id)
    logger.info('Found firstNodes and lastNodes.')
    logger.info('Finding route neighbours based on first and last nodes...')

    # Find end nodes and their routes
    nodes = find_nodes(routes)

    # Find previous and next route neighbours
    for r in routes:
        next_route_ids = deepcopy(nodes[routes[r].lastNode])
        next_route_ids.remove(routes[r].id)
        previous_route_ids = deepcopy(nodes[routes[r].firstNode])
        previous_route_ids.remove(routes[r].id)
        for rid in next_route_ids:
            routes[routes[r].id].nextRoutes.append(rid)
        for rid in previous_route_ids:
            routes[routes[r].id].previousRoutes.append(rid)

    # remove unconnected tracks with highway=track and tracktype=grade1 and mtb:scale is null
    logger.info('Removing disconnected tracks.')
    routes = remove_unconnected(routes, nodes)
    logger.info('Tracks removed.')

    logger.info('Finding dangerous nodes (column warning).')

    # Find nodeIDs, where track's attribute mtb:scale changes rapidly (difference >= 2),
    # create new column warning in planet_osm_lines with the difference
    danger_nodes = find_dangerous_nodes(nodes, routes)
    point_cursor = connection.cursor()
    insert_danger_nodes(danger_nodes, point_cursor)
    point_cursor.close()

    logger.info('Neighbours are found.')
    logger.info('Determining offset for each route...')

    # Find offset polarity
#    listOfRoutes = routes.values()
    list_of_routes = sorted(routes.values(), key=lambda route: route.osmcSigns[0], reverse=True)
    if len(list_of_routes) > 1000:
        setrecursionlimit(len(list_of_routes))
    for r in list_of_routes:
        set_offset(routes, r.id, "next")
        set_offset(routes, r.id, "previous")

    logger.info('Offset is found.')
    logger.info("Inserting of routes into new empty table planet_osm_routes2...")

    # Determine maximum number of different osmcSymbols at one route
    max_signs = 0
    for r in routes.values():
        if max_signs < r.numOfSigns:
            max_signs = r.numOfSigns
    if max_signs < 4:
        max_signs = 4

    # Prepare database table for data insertion
    auxiliary_cursor.execute('''
        ALTER TABLE planet_osm_routes2
          ADD "mtb:scale" text;
    ''')
    auxiliary_cursor.execute('''
        ALTER TABLE planet_osm_routes2
          ADD "mtb:scale:uphill" text;
    ''')
    auxiliary_cursor.execute('''
        ALTER TABLE planet_osm_routes2
          ADD offsetSide integer;
    ''')

    # Add columns for maximum number of osmcSymbols
    for column in range(max_signs):
        auxiliary_cursor.execute('''
            ALTER TABLE planet_osm_routes2
              ADD osmcSymbol%s text;
        ''' % (str(column)))
        auxiliary_cursor.execute('''
            ALTER TABLE planet_osm_routes2
              ADD network%s text;
        ''' % (str(column)))

    # Insert route values into the table
    for r in list_of_routes:
        if r.geometry is not None:
            row = r.get_values_row()
            auxiliary_cursor.execute('''
                INSERT INTO planet_osm_routes2
                  VALUES (%s)
            ''' % row)
    logger.info('Finished inserting routes into new table.')

    logger.info('Relations: %i' % len(relations))
    logger.info('Max Signs: %i' % max_signs)
    logger.info('Routes: %i' % len(routes))
    logger.info('Nodes: %i' % len(nodes))

    # commit the result into the database
    auxiliary_cursor.close()
    connection.commit()

    logger.info('Relations2lines finished successfully.')

Example 32

Project: Ezhil-Lang
Source File: Interpreter.py
View license
    def __init__(self,lexer, debug = False,safe_mode=False,update=True,stacksize=500,env=None):
        DebugUtils.__init__(self,debug)
        self.SAFE_MODE = safe_mode
        self.debug = debug
        self.MAX_REC_DEPTH = 10000 #for Python
        self.stacksize = stacksize #for Ezhil stacksize
        self.lexer=lexer
        self.ast=None
        self.function_map = NoClobberDict()#parsed functions
        self.builtin_map = NoClobberDict() #pointers to builtin functions
        self.call_stack = list() #call stack
        if not env:
            self.env = Environment( self.call_stack, self.function_map, \
                                        self.builtin_map, self.debug, self.stacksize)
        else:
            self.env = env
        sys.setrecursionlimit( self.MAX_REC_DEPTH ) # have a large enough Python stack
        ## use the default Exprs parser.
        lang_parser = Parser(self.lexer,self.function_map, \
                             self.builtin_map, self.debug )
        self.parser = lang_parser
        
        ## run the installation code last
        self.install_builtins()
        self.install_blind_builtins()
        
        if update :
            ## update globals
            global global_interpreter
            global_interpreter = self

Example 33

Project: rez
Source File: accessibility.py
View license
def mutual_accessibility(graph):
    """
    Mutual-accessibility matrix (strongly connected components).

    @type  graph: graph, digraph
    @param graph: Graph.

    @rtype:  dictionary
    @return: Mutual-accessibility information for each node.
    """
    recursionlimit = getrecursionlimit()
    setrecursionlimit(max(len(graph.nodes())*2,recursionlimit))
    
    mutual_access = {}
    stack = []
    low = {}
        
    def visit(node):
        if node in low:
            return

        num = len(low)
        low[node] = num
        stack_pos = len(stack)
        stack.append(node)
        
        for successor in graph.neighbors(node):
            visit(successor)
            low[node] = min(low[node], low[successor])
        
        if num == low[node]:
            component = stack[stack_pos:]
            del stack[stack_pos:]
            component.sort()
            for each in component:
                mutual_access[each] = component

            for item in component:
                low[item] = len(graph)
    
    for node in graph:
        visit(node)
    
    setrecursionlimit(recursionlimit)
    return mutual_access

Example 34

Project: TADbit
Source File: normalize_hic.py
View license
def expected(hic_data, bads=None, signal_to_noise=0.05, inter_chrom=False, **kwargs):
    """
    Computes the expected values by averaging observed interactions at a given
    distance in a given HiC matrix.
    
    :param hic_data: dictionary containing the interaction data
    :param None bads: dictionary with column not to be considered
    :param 0.05 signal_to_noise: to calculate expected interaction counts,
       if not enough reads are observed at a given distance the observations
       of the distance+1 are summed. a signal to noise ratio of < 0.05
       corresponds to > 400 reads.
    
    :returns: a vector of biases (length equal to the size of the matrix)
    """
    min_n = signal_to_noise ** -2. # equals 400 when default

    size = len(hic_data)
    try:
        if not inter_chrom:
            size = max(hic_data.chromosomes.values())
    except AttributeError:
        pass

    if size > 1200:
        import sys
        sys.setrecursionlimit(size + 100)

    expc = {}
    dist = 0
    while dist < size:
        diag = []
        new_dist, val = _meandiag(hic_data, dist, diag, min_n, size, bads)
        for dist in range(dist, new_dist + 1):
            expc[dist] = val
    return expc

Example 35

Project: rez
Source File: accessibility.py
View license
def cut_edges(graph):
    """
    Return the cut-edges of the given graph.
    
    A cut edge, or bridge, is an edge of a graph whose removal increases the number of connected
    components in the graph.
    
    @type  graph: graph, hypergraph
    @param graph: Graph.
    
    @rtype:  list
    @return: List of cut-edges.
    """
    recursionlimit = getrecursionlimit()
    setrecursionlimit(max(len(graph.nodes())*2,recursionlimit))

    # Dispatch if we have a hypergraph
    if 'hypergraph' == graph.__class__.__name__:
        return _cut_hyperedges(graph)

    pre = {}    # Pre-ordering
    low = {}    # Lowest pre[] reachable from this node going down the spanning tree + one backedge
    spanning_tree = {}
    reply = []
    pre[None] = 0

    for each in graph:
        if (each not in pre):
            spanning_tree[each] = None
            _cut_dfs(graph, spanning_tree, pre, low, reply, each)
    
    setrecursionlimit(recursionlimit)
    return reply

Example 36

Project: rez
Source File: accessibility.py
View license
def accessibility(graph):
    """
    Accessibility matrix (transitive closure).

    @type  graph: graph, digraph, hypergraph
    @param graph: Graph.

    @rtype:  dictionary
    @return: Accessibility information for each node.
    """
    recursionlimit = getrecursionlimit()
    setrecursionlimit(max(len(graph.nodes())*2,recursionlimit))
    
    accessibility = {}        # Accessibility matrix

    # For each node i, mark each node j if that exists a path from i to j.
    for each in graph:
        access = {}
        # Perform DFS to explore all reachable nodes
        _dfs(graph, access, 1, each)
        accessibility[each] = list(access.keys())
    
    setrecursionlimit(recursionlimit)
    return accessibility

Example 37

Project: rez
Source File: accessibility.py
View license
def connected_components(graph):
    """
    Connected components.

    @type  graph: graph, hypergraph
    @param graph: Graph.

    @rtype:  dictionary
    @return: Pairing that associates each node to its connected component.
    """
    recursionlimit = getrecursionlimit()
    setrecursionlimit(max(len(graph.nodes())*2,recursionlimit))
    
    visited = {}
    count = 1

    # For 'each' node not found to belong to a connected component, find its connected
    # component.
    for each in graph:
        if (each not in visited):
            _dfs(graph, visited, count, each)
            count = count + 1
    
    setrecursionlimit(recursionlimit)
    return visited

Example 38

Project: orange
Source File: __init__.py
View license
    def setUp(self):
        import logging

        from PyQt4.QtGui import QApplication, QGraphicsScene, QGraphicsView, \
                                QPainter

        from PyQt4.QtCore import QTimer

        logging.basicConfig()

        self.app = QApplication([])
        self.scene = QGraphicsScene()
        self.view = QGraphicsView(self.scene)
        self.view.setRenderHints(
            QPainter.Antialiasing | \
            QPainter.SmoothPixmapTransform | \
            QPainter.TextAntialiasing
            )
        self.view.resize(500, 300)
        self.view.show()
        QTimer.singleShot(10000, self.app.exit)

        def my_excepthook(*args):
            sys.setrecursionlimit(1010)
            traceback.print_exc(limit=4)

        self._orig_excepthook = sys.excepthook
        sys.excepthook = my_excepthook
        self.singleShot = QTimer.singleShot

Example 39

Project: rez
Source File: accessibility.py
View license
def cut_nodes(graph):
    """
    Return the cut-nodes of the given graph.
    
    A cut node, or articulation point, is a node of a graph whose removal increases the number of
    connected components in the graph.
    
    @type  graph: graph, hypergraph
    @param graph: Graph.
        
    @rtype:  list
    @return: List of cut-nodes.
    """
    recursionlimit = getrecursionlimit()
    setrecursionlimit(max(len(graph.nodes())*2,recursionlimit))
    
    # Dispatch if we have a hypergraph
    if 'hypergraph' == graph.__class__.__name__:
        return _cut_hypernodes(graph)
        
    pre = {}    # Pre-ordering
    low = {}    # Lowest pre[] reachable from this node going down the spanning tree + one backedge
    reply = {}
    spanning_tree = {}
    pre[None] = 0
    
    # Create spanning trees, calculate pre[], low[]
    for each in graph:
        if (each not in pre):
            spanning_tree[each] = None
            _cut_dfs(graph, spanning_tree, pre, low, [], each)

    # Find cuts
    for each in graph:
        # If node is not a root
        if (spanning_tree[each] is not None):
            for other in graph[each]:
                # If there is no back-edge from descendent to a ancestral of each
                if (low[other] >= pre[each] and spanning_tree[other] == each):
                    reply[each] = 1
        # If node is a root
        else:
            children = 0
            for other in graph:
                if (spanning_tree[other] == each):
                    children = children + 1
            # root is cut-vertex iff it has two or more children
            if (children >= 2):
                reply[each] = 1

    setrecursionlimit(recursionlimit)
    return list(reply.keys())

Example 40

Project: rez
Source File: cycles.py
View license
def find_cycle(graph):
    """
    Find a cycle in the given graph.
    
    This function will return a list of nodes which form a cycle in the graph or an empty list if
    no cycle exists.
    
    @type graph: graph, digraph
    @param graph: Graph.
    
    @rtype: list
    @return: List of nodes. 
    """
    
    if (isinstance(graph, graph_class)):
        directed = False
    elif (isinstance(graph, digraph_class)):
        directed = True
    else:
        raise InvalidGraphType

    def find_cycle_to_ancestor(node, ancestor):
        """
        Find a cycle containing both node and ancestor.
        """
        path = []
        while (node != ancestor):
            if (node is None):
                return []
            path.append(node)
            node = spanning_tree[node]
        path.append(node)
        path.reverse()
        return path
    
    def dfs(node):
        """
        Depth-first search subfunction.
        """
        visited[node] = 1
        # Explore recursively the connected component
        for each in graph[node]:
            if (cycle):
                return
            if (each not in visited):
                spanning_tree[each] = node
                dfs(each)
            else:
                if (directed or spanning_tree[node] != each):
                    cycle.extend(find_cycle_to_ancestor(node, each))

    recursionlimit = getrecursionlimit()
    setrecursionlimit(max(len(graph.nodes())*2,recursionlimit))

    visited = {}              # List for marking visited and non-visited nodes
    spanning_tree = {}        # Spanning tree
    cycle = []

    # Algorithm outer-loop
    for each in graph:
        # Select a non-visited node
        if (each not in visited):
            spanning_tree[each] = None
            # Explore node's connected component
            dfs(each)
            if (cycle):
                setrecursionlimit(recursionlimit)
                return cycle

    setrecursionlimit(recursionlimit)
    return []

Example 41

Project: orange
Source File: __init__.py
View license
    def __exit__(self, exc_type, exc_val, exc_tb):
        sys.setrecursionlimit(self.old_limit)

Example 42

Project: rez
Source File: searching.py
View license
def depth_first_search(graph, root=None, filter=null()):
    """
    Depth-first search.

    @type  graph: graph, digraph
    @param graph: Graph.
    
    @type  root: node
    @param root: Optional root node (will explore only root's connected component)

    @rtype:  tuple
    @return: A tupple containing a dictionary and two lists:
        1. Generated spanning tree
        2. Graph's preordering
        3. Graph's postordering
    """
    
    recursionlimit = getrecursionlimit()
    setrecursionlimit(max(len(graph.nodes())*2,recursionlimit))

    def dfs(node):
        """
        Depth-first search subfunction.
        """
        visited[node] = 1
        pre.append(node)
        # Explore recursively the connected component
        for each in graph[node]:
            if (each not in visited and filter(each, node)):
                spanning_tree[each] = node
                dfs(each)
        post.append(node)

    visited = {}            # List for marking visited and non-visited nodes
    spanning_tree = {}      # Spanning tree
    pre = []                # Graph's preordering
    post = []               # Graph's postordering
    filter.configure(graph, spanning_tree)

    # DFS from one node only
    if (root is not None):
        if filter(root, None):
            spanning_tree[root] = None
            dfs(root)
        setrecursionlimit(recursionlimit)
        return spanning_tree, pre, post
    
    # Algorithm loop
    for each in graph:
        # Select a non-visited node
        if (each not in visited and filter(each, None)):
            spanning_tree[each] = None
            # Explore node's connected component
            dfs(each)

    setrecursionlimit(recursionlimit)
    
    return (spanning_tree, pre, post)

Example 43

Project: rez
Source File: accessibility.py
View license
def accessibility(graph):
    """
    Accessibility matrix (transitive closure).

    @type  graph: graph, digraph, hypergraph
    @param graph: Graph.

    @rtype:  dictionary
    @return: Accessibility information for each node.
    """
    recursionlimit = getrecursionlimit()
    setrecursionlimit(max(len(graph.nodes())*2,recursionlimit))
    
    accessibility = {}        # Accessibility matrix

    # For each node i, mark each node j if that exists a path from i to j.
    for each in graph:
        access = {}
        # Perform DFS to explore all reachable nodes
        _dfs(graph, access, 1, each)
        accessibility[each] = list(access.keys())
    
    setrecursionlimit(recursionlimit)
    return accessibility

Example 44

Project: rez
Source File: accessibility.py
View license
def mutual_accessibility(graph):
    """
    Mutual-accessibility matrix (strongly connected components).

    @type  graph: graph, digraph
    @param graph: Graph.

    @rtype:  dictionary
    @return: Mutual-accessibility information for each node.
    """
    recursionlimit = getrecursionlimit()
    setrecursionlimit(max(len(graph.nodes())*2,recursionlimit))
    
    mutual_access = {}
    stack = []
    low = {}
        
    def visit(node):
        if node in low:
            return

        num = len(low)
        low[node] = num
        stack_pos = len(stack)
        stack.append(node)
        
        for successor in graph.neighbors(node):
            visit(successor)
            low[node] = min(low[node], low[successor])
        
        if num == low[node]:
            component = stack[stack_pos:]
            del stack[stack_pos:]
            component.sort()
            for each in component:
                mutual_access[each] = component

            for item in component:
                low[item] = len(graph)
    
    for node in graph:
        visit(node)
    
    setrecursionlimit(recursionlimit)
    return mutual_access

Example 45

Project: rez
Source File: accessibility.py
View license
def connected_components(graph):
    """
    Connected components.

    @type  graph: graph, hypergraph
    @param graph: Graph.

    @rtype:  dictionary
    @return: Pairing that associates each node to its connected component.
    """
    recursionlimit = getrecursionlimit()
    setrecursionlimit(max(len(graph.nodes())*2,recursionlimit))
    
    visited = {}
    count = 1

    # For 'each' node not found to belong to a connected component, find its connected
    # component.
    for each in graph:
        if (each not in visited):
            _dfs(graph, visited, count, each)
            count = count + 1
    
    setrecursionlimit(recursionlimit)
    return visited

Example 46

Project: rez
Source File: accessibility.py
View license
def cut_edges(graph):
    """
    Return the cut-edges of the given graph.
    
    A cut edge, or bridge, is an edge of a graph whose removal increases the number of connected
    components in the graph.
    
    @type  graph: graph, hypergraph
    @param graph: Graph.
    
    @rtype:  list
    @return: List of cut-edges.
    """
    recursionlimit = getrecursionlimit()
    setrecursionlimit(max(len(graph.nodes())*2,recursionlimit))

    # Dispatch if we have a hypergraph
    if 'hypergraph' == graph.__class__.__name__:
        return _cut_hyperedges(graph)

    pre = {}    # Pre-ordering
    low = {}    # Lowest pre[] reachable from this node going down the spanning tree + one backedge
    spanning_tree = {}
    reply = []
    pre[None] = 0

    for each in graph:
        if (each not in pre):
            spanning_tree[each] = None
            _cut_dfs(graph, spanning_tree, pre, low, reply, each)
    
    setrecursionlimit(recursionlimit)
    return reply

Example 47

Project: tryalgo
Source File: biconnected_components.py
View license
def cut_nodes_edges2(graph):
    """Bi-connected components, alternative recursive implementation

    :param graph: undirected graph. in listlist format. Cannot be in listdict format.
    :assumes: graph has about 10^4 vertices at most, otherwise memory limit is reached
    :returns: a tuple with the list of cut-nodes and the list of cut-edges
    :complexity: `O(|V|+|E|)` in average, `O(|V|+|E|^2)` in worst case due to use of dictionary
    """
    N = len(graph)
    recursionlimit = getrecursionlimit()
    setrecursionlimit(max(1000, N + 42))  # 5 est la vraie constante, mais ça fait foirer les builds
    edges = set((i, j) for i in range(N) for j in graph[i] if i <= j)
    nodes = set()
    NOT = -2  # pas encore visité ; avec -1 on a un gros bug à cause de `marked[v] != prof - 1`
    FIN = -3  # déjà visité
    marked = [NOT] * N # si c'est positif ou nul, cela vaut la profondeur dans le DFS
    def DFS(n, prof=0):
        """Parcourt récursivement le graphe, met à jour la liste des arêtes et renvoie
        le premier sommet dans l'ordre de parcours auquel on peut revenir."""
        if marked[n] == FIN:  # seulement lorsqu'il y a plusieurs composantes connexes
            return
        if marked[n] != NOT:
            return marked[n]
        marked[n] = prof
        m = float('inf')
        count = 0  # utile seulement pour prof==0
        for v in graph[n]:
            if marked[v] != FIN and marked[v] != prof - 1:
                count += 1
                r = DFS(v, prof+1)
                if r <= prof:
                    edges.discard(tuple(sorted((n, v))))
                if prof and r >= prof:  # seulement si on n'est pas dans la racine
                    nodes.add(n)
                m = min(m, r)
        if prof == 0 and count >= 2:  # la racine est un point d'articulation ssi elle a plus de deux fils
            nodes.add(n)
        marked[n] = FIN
        return m
    for r in range(N):
        DFS(r)  # on pourrait compter les composantes connexes en ajoutant 1 si c'est différent de None
    setrecursionlimit(recursionlimit)
    return nodes, edges

Example 48

Project: tryalgo
Source File: biconnected_components.py
View license
def cut_nodes_edges2(graph):
    """Bi-connected components, alternative recursive implementation

    :param graph: undirected graph. in listlist format. Cannot be in listdict format.
    :assumes: graph has about 10^4 vertices at most, otherwise memory limit is reached
    :returns: a tuple with the list of cut-nodes and the list of cut-edges
    :complexity: `O(|V|+|E|)` in average, `O(|V|+|E|^2)` in worst case due to use of dictionary
    """
    N = len(graph)
    recursionlimit = getrecursionlimit()
    setrecursionlimit(max(1000, N + 42))  # 5 est la vraie constante, mais ça fait foirer les builds
    edges = set((i, j) for i in range(N) for j in graph[i] if i <= j)
    nodes = set()
    NOT = -2  # pas encore visité ; avec -1 on a un gros bug à cause de `marked[v] != prof - 1`
    FIN = -3  # déjà visité
    marked = [NOT] * N # si c'est positif ou nul, cela vaut la profondeur dans le DFS
    def DFS(n, prof=0):
        """Parcourt récursivement le graphe, met à jour la liste des arêtes et renvoie
        le premier sommet dans l'ordre de parcours auquel on peut revenir."""
        if marked[n] == FIN:  # seulement lorsqu'il y a plusieurs composantes connexes
            return
        if marked[n] != NOT:
            return marked[n]
        marked[n] = prof
        m = float('inf')
        count = 0  # utile seulement pour prof==0
        for v in graph[n]:
            if marked[v] != FIN and marked[v] != prof - 1:
                count += 1
                r = DFS(v, prof+1)
                if r <= prof:
                    edges.discard(tuple(sorted((n, v))))
                if prof and r >= prof:  # seulement si on n'est pas dans la racine
                    nodes.add(n)
                m = min(m, r)
        if prof == 0 and count >= 2:  # la racine est un point d'articulation ssi elle a plus de deux fils
            nodes.add(n)
        marked[n] = FIN
        return m
    for r in range(N):
        DFS(r)  # on pourrait compter les composantes connexes en ajoutant 1 si c'est différent de None
    setrecursionlimit(recursionlimit)
    return nodes, edges

Example 49

Project: llvm-p86
Source File: main.py
View license
def run(argv=None):
    sys.setrecursionlimit(50000)

    '''Command line options.'''

    if argv is None:
        argv = sys.argv
    else:
        sys.argv.extend(argv)

    program_name = os.path.basename(sys.argv[0])
    program_version = "v%s" % __version__
    program_build_date = str(__updated__)
    program_version_message = '%%(prog)s %s (%s)' % (program_version,
                                                     program_build_date)
    program_shortdesc = 'LLVM-based Pascal compiler'
    program_license = '''%s

  created by John Törnblom on %s.
''' % (program_shortdesc, str(__date__))

    mutation_help = '''Mutate the code using on of the mutation operators.
    sc  - statement coverage
    dcc - decision/condition coverage

    ror - relational operator replacement (<, <=, =, <>, >=, >)
    cor - conditional operator replacement (and, or)
    aor - arithmetic operator replacement (+, -, *, /, div, mod)
    sdl - statement deletion'''

    try:
        # Setup argument parser
        parser = ArgumentParser(prog=program_name, description=program_license, formatter_class=RawTextHelpFormatter)
        parser.add_argument("-t", "--syntax-tree", dest="tree", action="store_true", help="print the syntax tree to stdout")
        parser.add_argument("-S", "--emit-llvm", dest="ir_code", metavar="PATH", action="store", help="save LLVM-IR (plain text) to PATH")
        parser.add_argument("-b", "--bit-code", dest="bit_code", metavar="PATH", action="store", help="save LLVM-IR (bit code) to PATH")
        parser.add_argument("-o", "--object-code", dest="obj_code", metavar="PATH", action="store", help="save object code to PATH")
        parser.add_argument("-c", "--source-code", dest="src_code", metavar="PATH", action="store", help="save source code to PATH")
        parser.add_argument("-O", "--optimize", dest="opt", metavar="LEVEL", action="store", choices=['0', '1', '2', '3'], default='0', help="run various optimizations on the LLVM-IR code")

        parser.add_argument("-T", "--triple", dest="triple", action="store", default='', help="define the target triple, e.g. x86_64-pc-linux or i686-pc-win32")
        parser.add_argument("-mcpu", dest="cpu", default='', help='target specific cpu type')
        parser.add_argument("-mattrs", dest="attrs", default='', help='target specific attributes')

        parser.add_argument("-D", "--define", dest="defs", metavar="DEF", action="append", help="define constants for the preprocessor")
        parser.add_argument("-I", "--include", dest="incs", metavar="PATH", action="append", help="define include directories for the preprocessor")
        parser.add_argument("-e", "--execute", dest="execute", action="store_true", help="execute the main function using the LLVM JIT compiler")
        parser.add_argument("-a", "--arguments", dest="args", metavar="ARGS", action="store", default='', help="optional string with arguments when executing the main function using the JIT compiler")
        parser.add_argument("-r", "--report", dest="report", metavar="PATH", action="store", help="collect information on mutations and store it as json formatted data in the folder PATH")
        parser.add_argument("-m", "--mutation", dest="mutation", action="store", choices=['sc', 'dcc', 'ror', 'cor', 'aor', 'sdl'], help=mutation_help)
        parser.add_argument("-v", "--verbosity", dest="verbosity", action="count", default=0)
        parser.add_argument('-V', '--version', action='version', version=program_version_message)
        parser.add_argument(dest="file", metavar="file")

        # Process arguments
        args = parser.parse_args()

        if args.verbosity:
            log.set_verbosity(args.verbosity)
        else:
            log.set_verbosity(0)

        c = compiler.Compiler(args.file)

        if args.defs:
            for d in args.defs:
                c.define(d)

        if args.incs:
            for i in args.incs:
                c.include(i)

        c.analyze()

        if args.tree:
            c.print_tree()

        if args.mutation:
            c.mutate(args.mutation, args.report)

        if args.tree and args.mutation:
            c.print_tree()

        if args.src_code:
            c.save_source_code(args.src_code)

        synthesize = (args.ir_code or args.bit_code or
                      args.obj_code or args.execute)

        if synthesize:
            c.synthesize()

        if args.opt and synthesize:
            c.optimize(int(args.opt))

        if args.ir_code:
            c.save_ir(args.ir_code, args.triple)

        if args.bit_code:
            c.save_bit_code(args.bit_code, args.triple)

        if args.obj_code:
            c.save_obj_code(args.obj_code, args.triple, args.cpu, args.attrs)

        if args.execute:
            c.execute(args.args)

        return 0

    except KeyboardInterrupt:
        return 0

Example 50

Project: llvm-p86
Source File: main.py
View license
def run(argv=None):
    sys.setrecursionlimit(50000)

    '''Command line options.'''

    if argv is None:
        argv = sys.argv
    else:
        sys.argv.extend(argv)

    program_name = os.path.basename(sys.argv[0])
    program_version = "v%s" % __version__
    program_build_date = str(__updated__)
    program_version_message = '%%(prog)s %s (%s)' % (program_version,
                                                     program_build_date)
    program_shortdesc = 'LLVM-based Pascal compiler'
    program_license = '''%s

  created by John Törnblom on %s.
''' % (program_shortdesc, str(__date__))

    mutation_help = '''Mutate the code using on of the mutation operators.
    sc  - statement coverage
    dcc - decision/condition coverage

    ror - relational operator replacement (<, <=, =, <>, >=, >)
    cor - conditional operator replacement (and, or)
    aor - arithmetic operator replacement (+, -, *, /, div, mod)
    sdl - statement deletion'''

    try:
        # Setup argument parser
        parser = ArgumentParser(prog=program_name, description=program_license, formatter_class=RawTextHelpFormatter)
        parser.add_argument("-t", "--syntax-tree", dest="tree", action="store_true", help="print the syntax tree to stdout")
        parser.add_argument("-S", "--emit-llvm", dest="ir_code", metavar="PATH", action="store", help="save LLVM-IR (plain text) to PATH")
        parser.add_argument("-b", "--bit-code", dest="bit_code", metavar="PATH", action="store", help="save LLVM-IR (bit code) to PATH")
        parser.add_argument("-o", "--object-code", dest="obj_code", metavar="PATH", action="store", help="save object code to PATH")
        parser.add_argument("-c", "--source-code", dest="src_code", metavar="PATH", action="store", help="save source code to PATH")
        parser.add_argument("-O", "--optimize", dest="opt", metavar="LEVEL", action="store", choices=['0', '1', '2', '3'], default='0', help="run various optimizations on the LLVM-IR code")

        parser.add_argument("-T", "--triple", dest="triple", action="store", default='', help="define the target triple, e.g. x86_64-pc-linux or i686-pc-win32")
        parser.add_argument("-mcpu", dest="cpu", default='', help='target specific cpu type')
        parser.add_argument("-mattrs", dest="attrs", default='', help='target specific attributes')

        parser.add_argument("-D", "--define", dest="defs", metavar="DEF", action="append", help="define constants for the preprocessor")
        parser.add_argument("-I", "--include", dest="incs", metavar="PATH", action="append", help="define include directories for the preprocessor")
        parser.add_argument("-e", "--execute", dest="execute", action="store_true", help="execute the main function using the LLVM JIT compiler")
        parser.add_argument("-a", "--arguments", dest="args", metavar="ARGS", action="store", default='', help="optional string with arguments when executing the main function using the JIT compiler")
        parser.add_argument("-r", "--report", dest="report", metavar="PATH", action="store", help="collect information on mutations and store it as json formatted data in the folder PATH")
        parser.add_argument("-m", "--mutation", dest="mutation", action="store", choices=['sc', 'dcc', 'ror', 'cor', 'aor', 'sdl'], help=mutation_help)
        parser.add_argument("-v", "--verbosity", dest="verbosity", action="count", default=0)
        parser.add_argument('-V', '--version', action='version', version=program_version_message)
        parser.add_argument(dest="file", metavar="file")

        # Process arguments
        args = parser.parse_args()

        if args.verbosity:
            log.set_verbosity(args.verbosity)
        else:
            log.set_verbosity(0)

        c = compiler.Compiler(args.file)

        if args.defs:
            for d in args.defs:
                c.define(d)

        if args.incs:
            for i in args.incs:
                c.include(i)

        c.analyze()

        if args.tree:
            c.print_tree()

        if args.mutation:
            c.mutate(args.mutation, args.report)

        if args.tree and args.mutation:
            c.print_tree()

        if args.src_code:
            c.save_source_code(args.src_code)

        synthesize = (args.ir_code or args.bit_code or
                      args.obj_code or args.execute)

        if synthesize:
            c.synthesize()

        if args.opt and synthesize:
            c.optimize(int(args.opt))

        if args.ir_code:
            c.save_ir(args.ir_code, args.triple)

        if args.bit_code:
            c.save_bit_code(args.bit_code, args.triple)

        if args.obj_code:
            c.save_obj_code(args.obj_code, args.triple, args.cpu, args.attrs)

        if args.execute:
            c.execute(args.args)

        return 0

    except KeyboardInterrupt:
        return 0