Friday, September 27, 2024

DAG Hamilton Graph Presented as SVG in Blogger

Through the kindness of the DAG Hamilton project team, I was able to secure an official svg version of the DAG Hamilton logo. It looks significantly better than the one I had generated with an online image to svg converter and is much smaller and easy to work with (4 kilobytes versus 200 kb). The DAG Hamilton graphviz graph now shows up in Blogger; it is unlikely to show up on the planet(python) feed. Blogger is not liking the code and svg I have included (complaints of malformed html). In the interest of preserving the rendering of the graph(s), I am constraining the text here to a few paragraphs

The first graph has the code provided. This graph is from a previous post.

The second graph represents the DAG Hamilton workflow for the production of the first graph. This is in keeping with the "Eat your own dogfood" mantra. I happen to like the DAG Hamilton dogfood as I've mentioned in previous posts. It allows me to visualize my workflows and track complexity and areas for improvement in the code.

The third one I did with a scaled down version of the code presented (no logos). I hand pasted the DAG Hamilton official logo into the third one. It is not subtle (the logo is huge), but it provides an idea of what one can do creatively with the logo or any svg element. Also, it shows the DAG Hamilton workflow for a graph.

All the code is a work in progress. Ideally I would like to keep reducing this to the most simple svg implementation possible to get it to show up or "work." Realistically, I'm afraid to sneeze for fear Blogger will protest. For now, I'm leaving good enough alone. Links and thoughts on svg (there is at least one python library (orsinium-labs/svg.py) out there that is way more elegant in its treatment of the medium than my rough regular expressions / text processing) will have to wait for another post.

Thanks for stopping by.

Toy Web Scraping Script Run Diagram Web Scraping Functions Highlighted Legend datafile str commodity_word_counts dict info_dict_merged dict colloquial_company_word_counts dict data_with_wikipedia dict data_with_company dict parsed_data dict wikipedia_report str info_output str input function

run.py code


"""
Hamilton wrapper.
"""

# run.py

import sys

import pprint

from hamilton import driver

import dag_hamilton_to_blogger as dhtb

dr = driver.Builder().with_modules(dhtb).build()

dr.display_all_functions('dhtb.svg',
                         deduplicate_inputs=True,
                         keep_dot=True,
                         orient='BR')

results = dr.execute(['defluffed_lines',
                      'scale_and_translation',
                      'logo_positions',
                      'captured_values',
                      'scaled_elements',
                      'translated_elements',
                      'hamilton_logo_data',
                      'scale_and_translation_hamilton_logo',
                      'fauxcompany_logo_data',
                      'scale_and_translation_fauxcompany_logo',
                      'svg_ready_doc',
                      'written_svg'],
                      inputs={'svg_file':'web_scraping_functions_highlighted.svg',
                              'outputfile':'test_output.svg',
                              'hamiltonlogofile':'hamilton_official_stripped.svg',
                              'hamiltonlogo_coords':{'min_x':-0.001,
                                                     'max_x':4353.846,
                                                     'min_y':-0.0006,
                                                     'max_y':4177.257},
                              'fauxcompanylogofile':'fauxcompanylogo_stripped_down.svg',
                              'fauxcompanylogo_coords':{'min_x':11.542786063261742,
                                                        'max_x':705.10684,
                                                        'min_y':4.9643821,
                                                        'max_y':74.47416391682819}})

Main DAG Hamilton functions (dag_hamilton_to_blogger.py)


# python 3.12

"""
Make DAG Hamilton graph show up in Blogger.
"""

import re
import sys
import pprint
import math
import copy

import reusedfunctions as rf

VIEWBOX_PAT = (r'[ ]viewBox[=]["][-]?[0-9]+[.]?[0-9]*[ ][-]?[0-9]+[.]?[0-9]*[ ]'
               r'([0-9]+[.]?[0-9]*)[ ]([0-9]+[.]?[0-9]*)')

# 5 coordinates.
POLYGON_PAT = (r'[<]polygon'
               r'.*([ ]points[=]["])([-]?[0-9]+[.]?[0-9]*)[,]'
                                  r'([-]?[0-9]+[.]?[0-9]*)[ ]'
                                  r'([-]?[0-9]+[.]?[0-9]*)[,]'
                                  r'([-]?[0-9]+[.]?[0-9]*)[ ]'
                                  r'([-]?[0-9]+[.]?[0-9]*)[,]'
                                  r'([-]?[0-9]+[.]?[0-9]*)[ ]'
                                  r'([-]?[0-9]+[.]?[0-9]*)[,]'
                                  r'([-]?[0-9]+[.]?[0-9]*)[ ]'
                                  r'([-]?[0-9]+[.]?[0-9]*)[,]'
                                  r'([-]?[0-9]+[.]?[0-9]*)["]')

# 4 coordinates instead of 5.
POLYGON_PAT_4 = (r'[<]polygon'
                 r'.*([ ]points[=]["])([-]?[0-9]+[.]?[0-9]*)[,]'
                                    r'([-]?[0-9]+[.]?[0-9]*)[ ]'
                                    r'([-]?[0-9]+[.]?[0-9]*)[,]'
                                    r'([-]?[0-9]+[.]?[0-9]*)[ ]'
                                    r'([-]?[0-9]+[.]?[0-9]*)[,]'
                                    r'([-]?[0-9]+[.]?[0-9]*)[ ]'
                                    r'([-]?[0-9]+[.]?[0-9]*)[,]'
                                    r'([-]?[0-9]+[.]?[0-9]*)["]')

# x, y
TEXTPAT = (r'')

IMAGE_FLAG = '')
# 4 coords (arrow head).
POLYGON_STR_4 = (r' points="{0:.3f},{1:.3f} {2:.3f},{3:.3f} '
                         r'{4:.3f},{5:.3f} {6:.3f},{7:.3f}"/>')
PATH_START_STR = r' d="M{0:.3f},{1:.3f}C'
PATH_STR_SEGMENT = ' {0:.3f},{1:.3f}'
PATH_STR = r' {0:s}"/>'
TEXT_STR = r' x="{0:.3f}" y="{1:.3f}"'
TEXT_STR_FONT = r' font-size="{0:.3f}"'

HAMILTON_LOGO_DIMENSIONS_PAT = (r'.*width[=]["]([0-9]+[.]?[0-9]*)px["][ ]'
                                  r'height[=]["]([0-9]+[.]?[0-9]*)px["][>]')

FAUXCOMPANY_LOGO_DIMENSIONS_PAT = (r'[ ]width[=]["]([0-9]+[.]?[0-9]*)["][ ]'
                                      r'height[=]["]([0-9]+[.]?[0-9]*)["][ ][>]')

# The official Hamilton logo splits the path into multiple
# lines with the last one having the absolute location
# ("C") of a bezier curve.
HAMILTON_CHANGE_LINE_PAT = r'.*C[-]?[0-9]+[.]?[0-9]*'

HAMILTON_TRANSFORM_FMT = (' transform="scale({scale:f}) '
                           'translate({translate_x:f},{translate_y:f})" />')

# One line of paths in Inkscape generated file.
FAUXCOMPANY_CHANGE_LINE_PAT = r'.*d[=]["]m[ ]'

# Inkscape put the closing tag /> on the following line.
FAUXCOMPANY_TRANSFORM_FMT = (' transform="scale({scale:f}) '
                              'translate({translate_x:f},{translate_y:f})"')

# * - get rid of first 6 lines.

# * - get rid of any line starting with:
#
#   "