1

I am using a code that (among other things) generate a svg file of the geometry that I create.

Given an input script my-geometry.py, the structure of the code is the following:

python pre.py my-geometry

In pre.py t is called another module:

from render import *
[...] some other code
execfile(script, globals()) #script=my-geometry

render.py itself import another module

from svg_render import *
[...]
svg = SvgEnvironment(x,y,title='a',desc='b')
svg.text(x_mid, y_mid, blk.label, anchor="middle")
[...]

where SvgEnvironment is a class defined in svg_render. The text method of the class is defined as:

def text(self, x, y, textString, angle=0.0, fontSize=10,
         anchor="start", colour="black",
         fontFamily="sanserif")

My question is: how is it possible to modify/override the default value of fontSize,from my-geometry.py? (What i want to do, is to modify the fontsize for all text that it is written to the svg without modyfing the source code)

I had a look to this question , but it seems not to fit to my case.

Community
  • 1
  • 1
Pierpaolo
  • 1,721
  • 4
  • 20
  • 34
  • You can use monkeypatch, replace existing function with your new code. If you want to do with monkeypatch, then I can give solution. – Nilesh Nov 18 '14 at 04:59

1 Answers1

1

You can use Monkey Patch or override function.

Monkey Patch

#File: render.py

from svg_render import SvgEnvironment

def modified_text(self, x, y, textString, angle=0.0, fontSize=<change size>,
         anchor="start", colour="black",
         fontFamily="sanserif")


SvgEnvironment.text = modified_text

This will replace whole function and when ever it used it will get new default value.

Override

#File: render.py

from svg_render import SvgEnvironment

class MySvgEnvironment(SvgEnvironment):
    def text(self, x, y, textString, angle=0.0, fontSize=<change size>,
         anchor="start", colour="black",
         fontFamily="sanserif")



...
...
svg = MySvgEnvironment(x,y,title='a',desc='b')
svg.text(x_mid, y_mid, blk.label, anchor="middle")
...
...
Community
  • 1
  • 1
Nilesh
  • 20,521
  • 16
  • 92
  • 148
  • Your answer implies to modify the file render.py, that' s not what I want, because I want to override the function at the script level (my-geometry.py) (at least, if it is possible) – Pierpaolo Nov 18 '14 at 05:13