leforestier / yattag

Python library to generate HTML or XML in a readable, concise and pythonic way.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AttributeError: 'numpy.float64' object has no attribute 'replace'

scls19fr opened this issue · comments

Hello,

text raises an error when something else than a string is given

Traceback (most recent call last):
  File "google_earth.py", line 143, in <module>
    main()
  File "google_earth.py", line 47, in main
    task_to_kml_with_yattag(df_task, outdir, filename_base+"_yattag", disp)
  File "google_earth.py", line 69, in task_to_kml_with_yattag
    text(lon)
  File "//anaconda/lib/python2.7/site-packages/yattag/simpledoc.py", line 102, in text
    self._append(html_escape(strg))
  File "//anaconda/lib/python2.7/site-packages/yattag/simpledoc.py", line 210, in html_escape
    return s.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
AttributeError: 'numpy.float64' object has no attribute 'replace'

(it could also raises AttributeError: 'float' object has no attribute 'replace')

maybe you should do:

try:
    return s.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
except:
    return(str(s))

You might also provide a string formatting method so when a number (float) is given we could given a number of decimals.

Kind regards

By experience, I know that if you accept everything inside the templates, sooner or later you get None values, python dictionaries, or <object at 0x5f14df2e6180> all over the place in the html/xml and you spend a lot of time wondering what happened. Better be explicit and make sure the user understands what he's doing. It doesn't seem like a lot of work to me to add str(...) calls around values that have to be converted to strings.

Maybe you should add int_to_text(number) and float_to_text(number, format) with default format="%.3f" for example

No, it's easier to just use the str() function, or whatever exists in the corresponding library to convert stuff to strings.