jrnl.plugins.util.oxford_list()   A
last analyzed

Complexity

Conditions 4

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nop 1
dl 0
loc 11
rs 9.95
c 0
b 0
f 0
1
#!/usr/bin/env python
2
# encoding: utf-8
3
4
5
def get_tags_count(journal):
6
    """Returns a set of tuples (count, tag) for all tags present in the journal."""
7
    # Astute reader: should the following line leave you as puzzled as me the first time
8
    # I came across this construction, worry not and embrace the ensuing moment of enlightment.
9
    tags = [tag for entry in journal.entries for tag in set(entry.tags)]
10
    # To be read: [for entry in journal.entries: for tag in set(entry.tags): tag]
11
    tag_counts = {(tags.count(tag), tag) for tag in tags}
12
    return tag_counts
13
14
15
def oxford_list(lst):
16
    """Return Human-readable list of things obeying the object comma)"""
17
    lst = sorted(lst)
18
    if not lst:
19
        return "(nothing)"
20
    elif len(lst) == 1:
21
        return lst[0]
22
    elif len(lst) == 2:
23
        return lst[0] + " or " + lst[1]
24
    else:
25
        return ", ".join(lst[:-1]) + ", or " + lst[-1]
26