ternaris / marv

THIS REPOSITORY HAS BEEN MERGE INTO
https://github.com/ternaris/marv-robotics
GNU Affero General Public License v3.0
3 stars 4 forks source link

Extend FORMATTER_MAP #32

Closed beetleskin closed 6 years ago

beetleskin commented 6 years ago

The FORMATTER_MAP storing the formatters for the _listingcolumns is very limited and misses basic atomic types. E.g. I just want to display a float and accumulate that float in _listingsummary.

All I can do right now is use the string formatter in the _listingcolumns. When I then accumulate that row, it displays "0" in the _listingsummary when I set the _listingsummary formatter to float. It concatenates the string values of that column if I set it to string, resulting in the summary: "0.334.235.3437.54.4334.45".

I don't get why especially a "float" formatter is missing for _listingcolumns (and filters?), it's one of the most common things to work with. Also, it is not straight forward, why there are different formatters for _listingcolumns and _listingsummary.


Update: Related: When I use the format S-Expression, I get an error on marv scan:

Traceback (most recent call last):
  File "/usr/local/bin/marv", line 11, in <module>
    sys.exit(cli())
  File "/usr/local/lib/python2.7/dist-packages/marv_cli/__init__.py", line 131, in cli
    marv(auto_envvar_prefix='MARV')
  File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 1066, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/marv/cli.py", line 424, in marvcli_scan
    create_app().site.scan(dry_run)
  File "/usr/local/lib/python2.7/dist-packages/marv/site.py", line 375, in scan
    collection.scan(scanroot, dry_run)
  File "/usr/local/lib/python2.7/dist-packages/marv/collection.py", line 500, in scan
    self._add_batch(log, batch)
  File "/usr/local/lib/python2.7/dist-packages/marv/collection.py", line 565, in _add_batch
    row, fields, relfields = self.render_listing(dataset)
  File "/usr/local/lib/python2.7/dist-packages/marv/collection.py", line 656, in render_listing
    value = calltree(functree, funcs)
  File "/usr/local/lib/python2.7/dist-packages/marv/config.py", line 92, in calltree
    return func(*args)
  File "/usr/local/lib/python2.7/dist-packages/marv/config.py", line 49, in <lambda>
    'format': lambda fmt, *args: fmt.format(*args),
ValueError: Unknown format code 'f' for object of type 'unicode'

config:

listing_columns =
distance   | Distance | string   | (format "{:.2f}" (get "distance_node.value")) # "value" is a Float64

Running marv scan again returns no error .. and the formatter in the listing works correctly.

chaoflow commented 6 years ago

@beetleskin the string formatter in combination with actual floats is the correct choice. However, the floats were erroneously cast to strings in the backend already - fixed in 3.1

A dedicated float formatter would require a format string and so far formatters cannot receive parameters. If you want more control you can add your own formatters, see https://ternaris.com/marv-robotics/docs/widgets.html#custom

beetleskin commented 6 years ago

@beetleskin the string formatter in combination with actual floats is the correct choice. However, the floats were erroneously cast to strings in the backend already - fixed in 3.1

With version 3.1 and the config line as posted initially, I still get the same error. And still: Running marv scan again returns no error .. and the formatter in the listing works correctly.


Despite that, I still don't know how to summarize this kind of float-valued-but-string-formatted listing columns.


In the doc, you list the formatters float and int, but they're nowhere to be found in the FORMATTER_MAP.

chaoflow commented 6 years ago

@beetleskin The format function you are using returns a string, which cannot be summarized. In 3.1 you have the following options:

  1. use string formatter (which processes int, float, and string) without the format function and hand it a float value
  2. add 'float': float, to the FORMATTER_MAP it is already available in the frontend. In 3.2 it will be added to the FORMATTER_MAP. The float formatter renders two decimal places.

Regarding the error you are describing, a minimal working example would be helpful. To this end we created https://github.com/ternaris/marv-site#reporting-issues--minimal-working-example