<commit_before>from __future__ import unicode_literals, print_function
import uuid, json, six
from collections import OrderedDict as _OrderedDict
from collections import Mapping as _Mapping
from mpcontribs.config import mp_level01_titles, replacements

def render_dict(dct, webapp=False):
    """use JsonHuman library to render a dictionairy"""
    json_str, uuid_str = json.dumps(dct).replace('\\n', ' '), str(uuid.uuid4())
    html = ["<div id='{}' style='width:100%;'></div>".format(uuid_str)]
    html.append("<script>")
    if webapp:
        html.append("requirejs(['main'], function() {")
    html.append("require(['json.human'], function(JsonHuman) {")
    html.append("'use strict';")
    html.append("var data = JSON.parse('{}');".format(json_str))
    html.append("var node = JsonHuman.format(data);")
    html.append("document.getElementById('{}').appendChild(node);".format(uuid_str))
    html.append("});")
    if webapp:
        html.append("});")
    html.append("</script>")
    return ' '.join(html) # TODO use \n?

class RecursiveDict(_OrderedDict):
    """extension of dict for internal representation of MPFile"""

    def rec_update(self, other=None, overwrite=False, replace_newlines=True):
        """https://gist.github.com/Xjs/114831"""
        # overwrite=False: don't overwrite existing unnested key
        if other is None: # mode to force RecursiveDicts to be used
            other = self
            overwrite = True
        for key,value in other.items():<commit_msg>recdict.rec_update: replacements if necessary<commit_after>from __future__ import unicode_literals, print_function
import uuid, json, six
from collections import OrderedDict as _OrderedDict
from collections import Mapping as _Mapping
from mpcontribs.config import mp_level01_titles, replacements

def render_dict(dct, webapp=False):
    """use JsonHuman library to render a dictionairy"""
    json_str, uuid_str = json.dumps(dct).replace('\\n', ' '), str(uuid.uuid4())
    html = ["<div id='{}' style='width:100%;'></div>".format(uuid_str)]
    html.append("<script>")
    if webapp:
        html.append("requirejs(['main'], function() {")
    html.append("require(['json.human'], function(JsonHuman) {")
    html.append("'use strict';")
    html.append("var data = JSON.parse('{}');".format(json_str))
    html.append("var node = JsonHuman.format(data);")
    html.append("document.getElementById('{}').appendChild(node);".format(uuid_str))
    html.append("});")
    if webapp:
        html.append("});")
    html.append("</script>")
    return ' '.join(html) # TODO use \n?

class RecursiveDict(_OrderedDict):
    """extension of dict for internal representation of MPFile"""

    def rec_update(self, other=None, overwrite=False, replace_newlines=True):
        """https://gist.github.com/Xjs/114831"""
        # overwrite=False: don't overwrite existing unnested key
        if other is None: # mode to force RecursiveDicts to be used
            other = self
            overwrite = True
        for key,value in other.items():
            if isinstance(key, six.string_types):
                key = ''.join([replacements.get(c, c) for c in key])
{'fraction_non_alphanumeric': 0.15318744053282587, 'fraction_numerical': 0.007611798287345386, 'mean_word_length': 3.9051321928460343, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3925096', 'n_tokens_mistral': 974, 'n_tokens_neox': 946, 'n_words': 234}