Skip to content

Add tree #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Nov 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
673ec6b
Require `asciitree`.
jakirkham Oct 27, 2017
44fdd52
Add the `tree` method to `Group`.
jakirkham Oct 27, 2017
b242d53
Test the `tree` method of `Group`.
jakirkham Oct 27, 2017
830a52d
Add TreeHierarchy object
jakirkham Oct 27, 2017
f9e8435
Return a TreeHierarchy from tree
jakirkham Oct 27, 2017
1365a80
Refresh tree tests to use repr
jakirkham Oct 27, 2017
1975711
Pick unicode/ascii agnostic tree characters
jakirkham Oct 27, 2017
f709e9e
Use custom traverser with tree
jakirkham Oct 27, 2017
0037f2f
Drop `ascii_kwargs` from `TreeHierarchy` constructor
jakirkham Oct 27, 2017
67e2407
Collect arbitrary kwargs in `update_ascii_kwargs`
jakirkham Oct 27, 2017
58486be
Add an HTML representation of TreeHierarchy
jakirkham Oct 27, 2017
0f67052
Test the HTML representation
jakirkham Oct 27, 2017
f9170cd
Include notebook demonstrating `tree`
jakirkham Oct 27, 2017
f5ce2ee
Support unicode or bytes with TreeHierarchy
jakirkham Oct 27, 2017
590f98d
Try unicode and bytes in notebook
jakirkham Oct 27, 2017
d9682de
Fix inline code quote in tree docstring
jakirkham Oct 27, 2017
7b9a6ae
Consolidate `if`s in `tree`'s HTML sublist build
jakirkham Oct 27, 2017
b7a5fd9
Drop unneeded formatting at tree's HTML list end
jakirkham Oct 27, 2017
8da154a
Change zarrTree to zarr-tree
jakirkham Oct 27, 2017
3a4c65c
Change TreeHierarchy to TreeViewer
jakirkham Oct 27, 2017
d3d06db
Drop `TreeViewer`'s `__str__`
jakirkham Oct 27, 2017
d09caa6
Merge remote-tracking branch 'alimanfoo/master' into 'jakirkham/add_t…
jakirkham Nov 14, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/api/hierarchy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Groups (``zarr.hierarchy``)
.. automethod:: visitkeys
.. automethod:: visitvalues
.. automethod:: visititems
.. automethod:: tree
.. automethod:: create_group
.. automethod:: require_group
.. automethod:: create_groups
Expand Down
204 changes: 204 additions & 0 deletions notebooks/repr_tree.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import zarr\n",
"\n",
"# Python 2/3 trick\n",
"try:\n",
" unicode\n",
"except NameError:\n",
" unicode = str"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"g1 = zarr.group()\n",
"g3 = g1.create_group('bar')\n",
"g3.create_group('baz')\n",
"g5 = g3.create_group('quux')\n",
"g5.create_dataset('baz', shape=100, chunks=10)\n",
"g7 = g3.create_group('zoo')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"/\n",
" +-- bar\n",
" +-- baz\n",
" +-- quux\n",
" | +-- baz[...]\n",
" +-- zoo\n"
]
}
],
"source": [
"print(bytes(g1.tree()).decode())"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"bar\n",
" ├── baz\n",
" ├── quux\n",
" │ └── baz[...]\n",
" └── zoo\n"
]
}
],
"source": [
"print(unicode(g3.tree()))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<style type=\"text/css\">\n",
"div.zarr-tree {\n",
" font-family: Courier, monospace;\n",
" font-size: 11pt;\n",
" font-style: normal;\n",
"}\n",
"\n",
"div.zarr-tree ul,\n",
"div.zarr-tree li,\n",
"div.zarr-tree li > div {\n",
" display: block;\n",
" position: relative;\n",
"}\n",
"\n",
"div.zarr-tree ul,\n",
"div.zarr-tree li {\n",
" list-style-type: none;\n",
"}\n",
"\n",
"div.zarr-tree li {\n",
" border-left: 2px solid #000;\n",
" margin-left: 1em;\n",
"}\n",
"\n",
"div.zarr-tree li > div {\n",
" padding-left: 1.3em;\n",
" padding-top: 0.225em;\n",
" padding-bottom: 0.225em;\n",
"}\n",
"\n",
"div.zarr-tree li > div::before {\n",
" content: '';\n",
" position: absolute;\n",
" top: 0;\n",
" left: -2px;\n",
" bottom: 50%;\n",
" width: 1.2em;\n",
" border-left: 2px solid #000;\n",
" border-bottom: 2px solid #000;\n",
"}\n",
"\n",
"div.zarr-tree > ul > li:first-child > div {\n",
" padding-left: 4%;\n",
"}\n",
"\n",
"div.zarr-tree > ul > li:first-child > div::before {\n",
" border: 0 none transparent;\n",
"}\n",
"\n",
"div.zarr-tree ul > li:last-child {\n",
" border-left: 2px solid transparent;\n",
"}\n",
"</style>\n",
"\n",
"<div class=\"zarr-tree\">\n",
"<ul>\n",
" <li><div>/</div>\n",
" <ul>\n",
" <li><div>bar</div>\n",
" <ul>\n",
" <li><div>baz</div></li>\n",
" <li><div>quux</div>\n",
" <ul>\n",
" <li><div>baz[...]</div></li>\n",
" </ul>\n",
" </li>\n",
" <li><div>zoo</div></li>\n",
" </ul>\n",
" </li>\n",
" </ul>\n",
" </li>\n",
"</ul>\n",
"</div>\n"
],
"text/plain": [
"/\n",
" └── bar\n",
" ├── baz\n",
" ├── quux\n",
" │ └── baz[...]\n",
" └── zoo"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g1.tree()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
asciitree
nose
numpy
fasteners
Expand Down
1 change: 1 addition & 0 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
appdirs==1.4.3
args==0.1.0
asciitree==0.3.3
certifi==2017.7.27.1
chardet==3.0.4
clint==0.5.1
Expand Down
1 change: 1 addition & 0 deletions requirements_rtfd.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
asciitree
setuptools
setuptools_scm
sphinx
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
'setuptools-scm>1.5.4'
],
install_requires=[
'asciitree',
'numpy>=1.7',
'fasteners',
'numcodecs>=0.2.0',
Expand Down
40 changes: 34 additions & 6 deletions zarr/hierarchy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@
from collections import MutableMapping
from itertools import islice


import numpy as np


from zarr.compat import PY2
from zarr.attrs import Attributes
from zarr.core import Array
from zarr.storage import (contains_array, contains_group, init_group, DictStore, group_meta_key,
attrs_key, listdir, rmdir)
from zarr.creation import (array, create, empty, zeros, ones, full, empty_like, zeros_like,
ones_like, full_like, normalize_store_arg)
from zarr.util import (normalize_storage_path, normalize_shape, InfoReporter,
from zarr.storage import (contains_array, contains_group, init_group,
DictStore, group_meta_key, attrs_key, listdir, rmdir)
from zarr.creation import (array, create, empty, zeros, ones, full,
empty_like, zeros_like, ones_like, full_like, normalize_store_arg)
from zarr.util import (normalize_storage_path, normalize_shape, InfoReporter, TreeViewer,
is_valid_python_name, instance_dir)
from zarr.errors import err_contains_array, err_contains_group, err_group_not_found, err_read_only
from zarr.meta import decode_group_metadata
Expand Down Expand Up @@ -62,6 +61,7 @@ class Group(MutableMapping):
visitkeys
visitvalues
visititems
tree
create_group
require_group
create_groups
Expand Down Expand Up @@ -546,6 +546,34 @@ def visititems(self, func):
base_len = len(self.name)
return self.visitvalues(lambda o: func(o.name[base_len:].lstrip("/"), o))

def tree(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a depth argument here which defaults to None (i.e., render the whole subtree) but can be an integer which would limit the tree depth - useful where you only want to view a few levels down, but whole tree might be unwieldy.

"""Provide a ``print``-able display of the hierarchy.

Examples
--------
>>> import zarr
>>> g1 = zarr.group()
>>> g2 = g1.create_group('foo')
>>> g3 = g1.create_group('bar')
>>> g4 = g3.create_group('baz')
>>> g5 = g3.create_group('quux')
>>> d1 = g5.create_dataset('baz', shape=100, chunks=10)
>>> print(g1.tree())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest just:

>>> g1.tree()

I.e., no need to call print().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a strong thought by any means, but print does force str -> repr. Other circumstances (e.g. in a Jupyter Notebook) not using print would have a different effect. Though again not a strong thought happy to change if it is preferred without print.

/
├── bar
│ ├── baz
│ └── quux
│ └── baz[...]
└── foo
>>> print(g3.tree())
bar
├── baz
└── quux
└── baz[...]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opted to try displaying the datasets like this. Please let me know what you think.

"""

return TreeViewer(self)

def _write_op(self, f, *args, **kwargs):

# guard condition
Expand Down
Loading