<commit_before>"""
publish Python objects as various API formats
"""

import datetime
import simplejson as json
import web

API_PREFIX = "http://watchdog.net/about/api#"

def _listify(v):
    if v is None:
        return []
    elif not isinstance(v, list):
        return [v]
    else:
        return v

def _getitems(obj, listify=True):
    out = []
    for k in obj.columns:
        c = obj.columns[k]
        if hasattr(c, 'export') and not c.export:
            pass
        else:
            v = getattr(obj, k)
            if listify: v = _listify(v)
            out.append((k, c, v))
    out.sort(lambda x, y: cmp(x[0], y[0]))
    return out

class SmartJSONEncoder(json.JSONEncoder):
    DATE_FORMAT = "%Y-%m-%d"
    TIME_FORMAT = "%H:%M:%S"
    def _default(self, obj):
        if isinstance(obj, datetime.datetime):
            return obj.strftime("%sT%sZ" % (self.DATE_FORMAT, self.TIME_FORMAT))
        elif isinstance(obj, datetime.date):
            return obj.strftime(self.DATE_FORMAT)
        elif isinstance(obj, datetime.time):
            return obj.strftime(self.TIME_FORMAT)
        else:
            try:
                return super(SmartJSONEncoder, self).default(obj)
            except TypeError:
                try:
                    return json.dumps(obj)
                except:
                    return obj._uri_
    def default(self, obj):
        return getattr(obj, 'tojson', lambda: self._default(obj))()

def tojson(x):
    return json.dumps(x, cls=SmartJSONEncoder)

def publishjson(lst):
    out = ['[']
    for obj in lst:
        out.append('  {')
        out.append('    "_type": "%s",' % obj.__class__.__name__)
        out.append('    "uri": "%s",' % obj._uri_)
        for k, c, v in _getitems(obj, listify=False):
            out.append('    "%s": %s,' % (k, tojson(v)))
        out[-1] = out[-1][:-1]
        out.append('  }')
    out.append(']\n')
    return '\n'.join(out)

n3_basic_indent = '  '

def _n3ify(obj, indent):
    if isinstance(obj, dict):
        return n3ify_dict(obj, indent)
    elif isinstance(obj, list):
        return ', '.join([n3ify(item, indent + n3_basic_indent)
                          for item in obj])
    elif isinstance(obj, bool):
        return str(obj).lower()
    elif isinstance(obj, (int, float)):
        return obj
    else:
        return '"%s"' % str(obj).replace('"', r'\"')

def n3ify(obj, indent, c=None):
    if hasattr(obj, 'ton3'):
        return obj.ton3(indent)
    elif hasattr(c, 'ton3'):<commit_msg>fix bug with unicode n3<commit_after>"""
publish Python objects as various API formats
"""

import datetime
import simplejson as json
import web

API_PREFIX = "http://watchdog.net/about/api#"

def _listify(v):
    if v is None:
        return []
    elif not isinstance(v, list):
        return [v]
    else:
        return v

def _getitems(obj, listify=True):
    out = []
    for k in obj.columns:
        c = obj.columns[k]
        if hasattr(c, 'export') and not c.export:
            pass
        else:
            v = getattr(obj, k)
            if listify: v = _listify(v)
            out.append((k, c, v))
    out.sort(lambda x, y: cmp(x[0], y[0]))
    return out

class SmartJSONEncoder(json.JSONEncoder):
    DATE_FORMAT = "%Y-%m-%d"
    TIME_FORMAT = "%H:%M:%S"
    def _default(self, obj):
        if isinstance(obj, datetime.datetime):
            return obj.strftime("%sT%sZ" % (self.DATE_FORMAT, self.TIME_FORMAT))
        elif isinstance(obj, datetime.date):
            return obj.strftime(self.DATE_FORMAT)
        elif isinstance(obj, datetime.time):
            return obj.strftime(self.TIME_FORMAT)
        else:
            try:
                return super(SmartJSONEncoder, self).default(obj)
            except TypeError:
                try:
                    return json.dumps(obj)
                except:
                    return obj._uri_
    def default(self, obj):
        return getattr(obj, 'tojson', lambda: self._default(obj))()

def tojson(x):
    return json.dumps(x, cls=SmartJSONEncoder)

def publishjson(lst):
    out = ['[']
    for obj in lst:
        out.append('  {')
        out.append('    "_type": "%s",' % obj.__class__.__name__)
        out.append('    "uri": "%s",' % obj._uri_)
        for k, c, v in _getitems(obj, listify=False):
            out.append('    "%s": %s,' % (k, tojson(v)))
        out[-1] = out[-1][:-1]
        out.append('  }')
    out.append(']\n')
    return '\n'.join(out)

n3_basic_indent = '  '

def _n3ify(obj, indent):
    if isinstance(obj, dict):
        return n3ify_dict(obj, indent)
    elif isinstance(obj, list):
        return ', '.join([n3ify(item, indent + n3_basic_indent)
                          for item in obj])
    elif isinstance(obj, bool):
        return str(obj).lower()
    elif isinstance(obj, (int, float)):
        return obj
    else:
        return '"%s"' % unicode(obj).replace('"', r'\"')

def n3ify(obj, indent, c=None):
    if hasattr(obj, 'ton3'):
        return obj.ton3(indent)
    elif hasattr(c, 'ton3'):
{'fraction_non_alphanumeric': 0.14564265817747712, 'fraction_numerical': 0.0057699960206923995, 'mean_word_length': 2.1011721159777914, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '1636703', 'n_tokens_mistral': 1660, 'n_tokens_neox': 1637, 'n_words': 411}