-
Notifications
You must be signed in to change notification settings - Fork 2
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
[pending] Documentation for pyDKB.common.json_utils. #169
base: pyDKB-master-merge
Are you sure you want to change the base?
Conversation
Conflicts: Utils/Dataflow/091_datasetsRucio/datasets_processing.py
After (or with) every change in docs I would add new version of HTML and PDF documentation; after all, it is the destination of any changes in docstrings ;) |
Utils to work with JSON (dict) objects. | ||
Utils to work with JSON objects. | ||
|
||
In context of python, JSON [#]_ objects may be considered as structures |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Python?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to my knowledge, JSON is not tied to python in any way - python is just another language which can work with this format. Other languages may not even have things such as 'dictionary' and use different terms to define the same object. Therefore, I believe that saying "you see, JSON file is usually just a big dictionary which may contain other dictionaries, lists, ..." is wrong without indicating that you are talking in terms of python (even considering the fact that this is mostly python project).
Side note - thanks, due to this comment I've found that I forgot to mention some other possible objects such as True/False.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huh, bold symbols are not that bold when you don`t know they are here...
I meant "shouldn`t Python be capitalized?"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ha, so that's what this was about! Sure, will do.
STRING key -- dot-separated list of nested keys. | ||
If a key contains dot itself, the key must be put between | ||
quotation marks. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I`d suggest to follow common style in docstrings.
I started to use this notation:
def func(arg1, agr2):
""" Do this and that.
Detailed description, warnings, notes and ToDo.
:param arg1: first argument meaning
:type arg1: str
:param arg2: second argument meaning
:type arg2: int, float, NoneType
:return: this or that (None if failed)
:rtype: dict, NoneType
"""
It produces pretty predictable view after building (please see e.g. pyDKB.common.custom_readline()
in the compiled documentation -- it is not perfect, yet uses stuff I`m talking about).
If it is OK then let`s follow this style; if not, let`s find (or invent) another and use it everywhere.
Google also says that there`s a keyword :raises
, yet I never tried it yet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I am going to add a “howto” to the repo, so that we had a common guide for this work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure thing, I'll take a look at custom_readline().
**Examples:** | ||
Transform STRING ``\'1.2.3'`` into LIST ``[\'1', \'2', \'3']``. | ||
|
||
Transform STRING ``\'1.\"2.3".4'`` into |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Talking about examples — I would suggest example in form of Python code, something like:
>>> nestedKeys('1.2.3')
['1', '2', '3']
Having less words it looks more illustrative for me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense. However, per our discussion yesterday - tests will be used as examples, so I'll remove examples I've made here.
STRING key -- dot-separated list of nested keys. | ||
If a key contains dot itself, the key must be put between | ||
quotation marks. | ||
If a key contains dot itself, the key must be put |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please do not forget that originally it was written with no concern to Sphinx, reStructuredText and building documentation from this comment. Thus newline in the comment does not mean newline or new paragraph in the built version — check how it looks now. Is it what you expected to get there?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, didn`t say that I was talking about PDF version.
HTML looks fine -- couldn`t check it from tablet ;)
""" | ||
|
||
|
||
def valueByKey(json_data, key): | ||
""" Return value by a chain (list) of nested keys. | ||
|
||
Parameters: | ||
It is common for JSON objects to contain many layers of dictionaries | ||
nested in other dictionaries - this function extracts the data from |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Single hyphen produces short dash, double — long one. In this case I guess you meant to use the long one, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right, thanks. Will fix.
Updated the docs according to comments. @mgolosova, due to software reasons known to you I cannot make PDF version at the moment. Should I make and upload HTML version or leave this issue to you? |
@Evildoor Yet I have a couple of comment already.
It is a bit too much for such a laconic log message ;) I see two ways:
I do remember that the last change (examples) was later reverted, so it is up to you — leave it and revert or simply remove — but the general idea is kind of this. |
@Evildoor |
- Extended module description - Extended valueByKey() description - Reformatted the "parameters" sections - Added examples
It was decided that tests should be used as examples.
The following changes were also made to match the new style: - Rephrased "key" description in both functions because saying "list of keys" about a variable which is explicitly defined as "str" in the next line is confusing. - Removed type highlights from nestedKeys() description.
230963c
to
a2ad012
Compare
@mgolosova |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now almost everything looks fine, thank you!
@@ -57,6 +57,8 @@ def nestedKeys(key): | |||
|
|||
String should contain keys separated by dot. If a key contains | |||
dot itself, the key must be put between matching quotation marks. | |||
Quotation marks inside the keys (not preceding or following a dot) | |||
are ignored. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By "ignored" you mean "treated as ordinary symbols", right?
If I read the description without knowing how the function works (or supposed to), for a moment I might think that "ignored" means "removed from the output", like:
>>> nested_keys('ab.c"d.e')
['ab', 'cd', 'e']
are ignored. | ||
|
||
:param key: nested keys | ||
:type key: str |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:type key: str, list
If passed key
is of type list
, it is returned without changes.
:param json_data: to search in | ||
:type json_data: dict | ||
:param key: nested keys | ||
:type key: str |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:type key: str, list
If passed key
is of type list
, it is treated as "already parsed" nested keys.
Nested keys can be list instead of str in both functions - it is treated as already processed str and used as-is.
Done. |
STRING key -- dot-separated list of nested keys | ||
It is common for JSON objects to contain many layers of dictionaries | ||
nested in other dictionaries -- this function extracts the data from | ||
such constructions according to given string or list with keys. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The description does not explain how keys should be passed. If usage of list version can be guessed, string is not clear at all.
What about something like this?
...according to a list of keys.
<...>
:param keys: sequence of nested keys (dot-separated string or list object)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, will do.
Everything seems to be fine; now waiting for the |
132b720
to
fb7e780
Compare
f415e34
to
abd340a
Compare
Basic documentation for pyDKB.common.json_utils.
Waits for #165