<commit_before>"""f90nml.namelist
   ===============

   Tools for creating Fortran namelist files from Python ``dict``s.

   :copyright: Copyright 2014 <NAME>, see AUTHORS for details.
   :license: Apache License, Version 2.0, see LICENSE for details.
"""
from __future__ import print_function

import os
try:
    from collections import OrderedDict
except ImportError:
    from ordereddict import OrderedDict


class Namelist(OrderedDict):
    """Case-insensitive Python dict"""

    def __init__(self, *args, **kwds):

        # If using (unordered) dict, then resort the keys for reproducibility
        s_args = list(args)
        if (args and not isinstance(args[0], OrderedDict) and
                isinstance(args[0], dict)):
            s_args[0] = sorted(args[0].items())

        super(Namelist, self).__init__(*s_args, **kwds)

        # Convert any internal dicts to Namelists
        for key, val in self.items():
            if isinstance(val, dict):
                self[key] = Namelist(val)

        # Formatting properties
        self._colwidth = 72
        self._indent = 4 * ' '
        self._end_comma = False
        self._uppercase = False
        self._floatformat = ''
        self._logical_repr = {False: '.false.', True: '.true.'}

        # Representatation functions
        self.f90str = {
            bool:
                lambda x: self.logical_repr[x],
            int:
                lambda x: str(x),
            float:
                lambda x: '{0:{fmt}}'.format(x, fmt=self.floatformat),
            complex:
                lambda x: '({0}, {1})'.format(x.real, x.imag),
            str:
                lambda x: repr(x).replace("\\'", "''").replace('\\"', '""'),
            type(None):
                lambda x: ''
        }

    def __contains__(self, key):
        return super(Namelist, self).__contains__(key.lower())
<commit_msg>In encoding the values of character string variables for Fortran (i.e., in the definition of Namelist.f90str), accommodate the backslash character, which Python escapes but Fortran does not. Strictly speaking, the definition should also accommodate still other characters that Python escapes, including \a, \b, \f, \n, \r, \t, and \v, but I'm not sufficiently familiar with fortran namelists to know how to correctly encode these.<commit_after>"""f90nml.namelist
   ===============

   Tools for creating Fortran namelist files from Python ``dict``s.

   :copyright: Copyright 2014 <NAME>, see AUTHORS for details.
   :license: Apache License, Version 2.0, see LICENSE for details.
"""
from __future__ import print_function

import os
try:
    from collections import OrderedDict
except ImportError:
    from ordereddict import OrderedDict


class Namelist(OrderedDict):
    """Case-insensitive Python dict"""

    def __init__(self, *args, **kwds):

        # If using (unordered) dict, then resort the keys for reproducibility
        s_args = list(args)
        if (args and not isinstance(args[0], OrderedDict) and
                isinstance(args[0], dict)):
            s_args[0] = sorted(args[0].items())

        super(Namelist, self).__init__(*s_args, **kwds)

        # Convert any internal dicts to Namelists
        for key, val in self.items():
            if isinstance(val, dict):
                self[key] = Namelist(val)

        # Formatting properties
        self._colwidth = 72
        self._indent = 4 * ' '
        self._end_comma = False
        self._uppercase = False
        self._floatformat = ''
        self._logical_repr = {False: '.false.', True: '.true.'}

        # Representatation functions
        self.f90str = {
            bool:
                lambda x: self.logical_repr[x],
            int:
                lambda x: str(x),
            float:
                lambda x: '{0:{fmt}}'.format(x, fmt=self.floatformat),
            complex:
                lambda x: '({0}, {1})'.format(x.real, x.imag),
            str:
                lambda x: repr(x).replace("\\'", "''").replace('\\"', '""').replace('\\\\', '\\'),
            type(None):
                lambda x: ''
        }

    def __contains__(self, key):
        return super(Namelist, self).__contains__(key.lower())

{'fraction_non_alphanumeric': 0.12800953516090585, 'fraction_numerical': 0.0100119189511323, 'mean_word_length': 2.2227342549923197, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5479306', 'n_tokens_mistral': 1259, 'n_tokens_neox': 1170, 'n_words': 358}