man-group / PyBloqs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ID_PRECISION set at load time so cannot be reconfigured

gityoav opened this issue · comments

It is easy to
import pybloqs
pybloqs.config.user_config['tmp_html_dir']='my temp directory'

conversely, since
pybloqs.block imports ID_PRECISIONat load time, it is tricky to change this. It would be better if instead, e.g. .show() would use self._id[:pybloqs.config.ID_PRECISION]

For my understanding, what's the motivation for wanting to change this value? Reports with high number of elements getting id collisions?

Moving it into the user_config is trivial and would achieve the desired effect.

There are actually a few things about file savings which may be good for other but don't follow my use pattern.

The _id method is using uuid but this is uninformative to the user, I have changed it in my own branch to self._id = datetime.datetime.now().utcnow().strftime('%Y%m%d%H%M%S%f')
At this point ID_PRECISION does need to be set a little higher, but we do get meaningful sort on filename. Indeed, when I send an email, by knowing when the email was sent, I can then find the file and ''similar versions I didn't send but looked at'' in the temp dir really easily. Admittedly, sorting by date does the same thing, so I am perhaps just being silly, but it does make my life a little easier.

The other annoying thing is the save method... The tempdir is set in stone and cannot be overwritten.
tempdir = tempfile.gettempdir() #line 141 of block/base.py

Strangely, I would have thought both .show() and .save() and indeed .email() should really follow a similar pattern and write to similar locations. In my own usage pattern, all generate 'throw aways temp files' but what I like to do is to keep all the data in one place and then delete judiciously

Finally, and completely unrelated, the convenience Block method isn't quite as flexible as I need it, throwing exceptions e.g. for dicts or pd.Series. Here is my own, more convenient method :-)

def block(contents=None, title=None, title_level=3, title_wrap=False, inherit_cfg=True, **kwargs):
"""
Constructs a composable layout element that will be rendered automatically by
IPython Notebooks. It can also be saved in HTML, PDF, PNG or JPEG formats.
Content is handled as follows:
- Lists are written out into a grid layout, with a single column
- tuples are written out into a grid layout, with a single row
- DataFrames are written out as an interactive HTML table.
- Strings are written out in a raw format, preserving any HTML content in them.
- Nested blocks are simply wrapped, unless there is more than one in which case
the same logic applies as for lists, tuples and sets.
:param contents: Contents to put in a block.
:param title: Optional title of the block.
:param title_level: Optional title level (adjusts the size and TOC level), 1 being the
biggest and 9 being the smallest.
:param title_wrap: Toggles whitespace wrapping of the title. (Default: False).
:param inherit_cfg: Optional. Set to False to ensure that the block does not inherit
any parameters from parents.
:param cascade_cfg: Set to True to enable parmater cascading from this block. A value
of False means that child blocks do not inherit parameters from
this block.
:param kwargs: Optional styling arguments. The style keyword argument has special
meaning in that it allows styling to be grouped as one argument.
It is also useful in case a styling parameter name clashes with a standard
block parameter.
:return: A block instance.
"""
if isinstance(contents, pybloqs.BaseBlock):
return contents
elif isinstance(contents, list):
return pybloqs.block.layout.Grid([block(c) for c in contents],
title=title, title_level=title_level, title_wrap=title_wrap, inherit_cfg=inherit_cfg, **kwargs)
elif isinstance(contents, tuple): # new but works like a treat...
return pybloqs.block.layout.HStack([block(c) for c in contents],
title=title, title_level=title_level, title_wrap=title_wrap, inherit_cfg=inherit_cfg, **kwargs)
elif isinstance(contents, dict):
return pybloqs.block.layout.Grid([block(tuple(item)) for item in contents.items()],
title=title, title_level=title_level, title_wrap=title_wrap, inherit_cfg=inherit_cfg, **kwargs)
elif isinstance(contents, pd.Series):
if len(contents)>0 and is_date(contents.index[0]):
return block(go.Scatter(x = contents.index, y = contents.values),
title=title, title_level=title_level, title_wrap=title_wrap, inherit_cfg=inherit_cfg, **kwargs)
else:
return block(dict(contents),
title=title, title_level=title_level, title_wrap=title_wrap, inherit_cfg=inherit_cfg, **kwargs)
elif isinstance(contents, Figure): #plotly figure
return pybloqs.block.image.PlotlyPlotBlock(contents,
title=title, title_level=title_level, title_wrap=title_wrap, inherit_cfg=inherit_cfg, **kwargs)
elif isinstance(contents, BaseTraceHierarchyType): #like plotly.graph_objects.Scatter
return block(go.Figure(contents),
title=title, title_level=title_level, title_wrap=title_wrap, inherit_cfg=inherit_cfg, **kwargs)
elif isinstance(contents, pd.DataFrame):
if len(contents)>0 and is_date(contents.index[0]): #TimeSeries, not really a table
return pybloqs.block.image.PlotlyPlotBlock(df_to_fig(contents),
title=title, title_level=title_level, title_wrap=title_wrap, inherit_cfg=inherit_cfg, **kwargs)
else:
return pybloqs.block.table.HTMLJinjaTableBlock(contents,
title=title, title_level=title_level, title_wrap=title_wrap, inherit_cfg=inherit_cfg, **kwargs)
elif isinstance(contents, Artist):
return pybloqs.block.image.PlotBlock(contents,
title=title, title_level=title_level, title_wrap=title_wrap, inherit_cfg=inherit_cfg, **kwargs)
else:
return Raw(str(contents),
title=title, title_level=title_level, title_wrap=title_wrap, inherit_cfg=inherit_cfg, **kwargs)

Dom, btw, why just pip and not conda? Doesn't this restrict target audience?

Added id_precision and tmp_dir to user_config as a drive-by of #68.

Re. conda: There was no demand for this yet. This would make a great contribution to the project as this is quite a standalone task.