<commit_before># ============================================================================
# GeoJSON. Copyright (C) 2007 <NAME>
#
# See ../LICENSE.txt
# 
# Contact: <NAME>, <EMAIL>
# ============================================================================


import simplejson
from geojson.feature import SimpleWebFeature


# Equal access to object attributes or mapping items

class Fly(object):
    
    def __init__(self, ob):
        self._ob = ob

    def get(self, attr, default=None):
        try:
            return self._ob[attr]
        except (TypeError, KeyError):
            return getattr(self._ob, attr, default)

    def has(self, attr):
        try:
            return bool(self._ob[attr])
        except (TypeError, KeyError):
            return bool(getattr(self._ob, attr, None))

    def __iter__(self):
        return iter(self._ob)

    def __len__(self):
        return len(self._ob)


class PyGFPEncoder(simplejson.JSONEncoder):
    
    """
    Extends JSONEncoder, serializes objects which satisfy the Python
    GIS feature protocol (PyGFP). See
    http://trac.gispython.org/projects/PCL/wiki/PythonFeatureProtocol.
    """

    def crs_default(self, o):
        return {
            'type': o.get('type'),
            'properties': o.get('properties'),
            }

    def geom_default(self, o):
        if o.get('type') == 'GeometryCollection':
            value = {
                'type': 'GeometryCollection',
                'members': 
                    [self.geom_default(Fly(g)) for g in o.get('members')],
                }
            if o.has('crs'):
                value['crs'] = self.crs_default(Fly(o.get('crs')))
            return value
        else:
            value = {
                'type': o.get('type'),
                'coordinates': tuple(o.get('coordinates')),
                }
            if o.has('crs'):
                value['crs'] = self.crs_default(Fly(o.get('crs')))
            return value

    def feature_default(self, o):
        value = {
            'type': 'Feature',
            'id': o.get('id'),
            'properties': o.get('properties'),
            'geometry': self.geom_default(Fly(o.get('geometry'))),
            }
        if o.has('crs'):
            value['crs'] = self.crs_default(Fly(o.get('crs')))
        return value

    def default(self, o):
        if o.has('coordinates'):
            return self.geom_default(o)
        elif o.has('geometry'):
            return self.feature_default(o)
        elif len(o) > 0:
            value = {
                'type': 'FeatureCollection',
                'members': [self.feature_default(Fly(m)) for m in iter(o)]
                }
            if o.has('crs'):
                value['crs'] = self.crs_default(o.get('crs'))
            return value
        else:
            return simplejson.JSONEncoder.default()


# Wrap the functions from simplejson, providing encoder, decoders, and
# object creation hooks

def dump(obj, fp):
    return simplejson.dump(Fly(obj), fp, cls=PyGFPEncoder)

def dumps(obj):
    return simplejson.dumps(Fly(obj), cls=PyGFPEncoder)

def load(fp, object_hook=None):<commit_msg>Look first for __geo_interface__ attribute, detect feature collections by their 'members' property<commit_after># ============================================================================
# GeoJSON. Copyright (C) 2007 <NAME>
#
# See ../LICENSE.txt
# 
# Contact: <NAME>, <EMAIL>
# ============================================================================


import simplejson
from geojson.feature import SimpleWebFeature


# Equal access to object attributes or mapping items

class Fly(object):
    
    def __init__(self, ob):
        if hasattr(ob, '__geo_interface__'):
            self._ob = ob.__geo_interface__
        else:
            self._ob = ob

    def get(self, attr, default=None):
        try:
            return self._ob[attr]
        except (TypeError, KeyError):
            return getattr(self._ob, attr, default)

    def has(self, attr):
        try:
            return bool(self._ob[attr])
        except (TypeError, KeyError):
            return bool(getattr(self._ob, attr, None))

    def __iter__(self):
        return iter(self._ob)

    def __len__(self):
        return len(self._ob)


class PyGFPEncoder(simplejson.JSONEncoder):
    
    """
    Extends JSONEncoder, serializes objects which satisfy the Python
    GIS feature protocol (PyGFP). See
    http://trac.gispython.org/projects/PCL/wiki/PythonFeatureProtocol.
    """

    def crs_default(self, o):
        return {
            'type': o.get('type'),
            'properties': o.get('properties'),
            }

    def geom_default(self, o):
        if o.get('type') == 'GeometryCollection':
            value = {
                'type': 'GeometryCollection',
                'members': 
                    [self.geom_default(Fly(g)) for g in o.get('members')],
                }
            if o.has('crs'):
                value['crs'] = self.crs_default(Fly(o.get('crs')))
            return value
        else:
            value = {
                'type': o.get('type'),
                'coordinates': tuple(o.get('coordinates')),
                }
            if o.has('crs'):
                value['crs'] = self.crs_default(Fly(o.get('crs')))
            return value

    def feature_default(self, o):
        value = {
            'type': 'Feature',
            'id': o.get('id'),
            'properties': o.get('properties'),
            'geometry': self.geom_default(Fly(o.get('geometry'))),
            }
        if o.has('crs'):
            value['crs'] = self.crs_default(Fly(o.get('crs')))
        return value

    def default(self, o):
        if o.has('coordinates'):
            return self.geom_default(o)
        elif o.has('geometry'):
            return self.feature_default(o)
        elif o.has('members'):
            value = {
                'members': [self.feature_default(Fly(m)) for m in iter(o)]
                }
            if o.has('crs'):
                value['crs'] = self.crs_default(o.get('crs'))
            return value
        else:
            return simplejson.JSONEncoder.default()


# Wrap the functions from simplejson, providing encoder, decoders, and
# object creation hooks

def dump(obj, fp):
    return simplejson.dump(Fly(obj), fp, cls=PyGFPEncoder)

def dumps(obj):
    return simplejson.dumps(Fly(obj), cls=PyGFPEncoder)

def load(fp, object_hook=None):
{'fraction_non_alphanumeric': 0.17519715478583578, 'fraction_numerical': 0.0013916808411937528, 'mean_word_length': 2.077069457659372, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, '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': '3346024', 'n_tokens_mistral': 1938, 'n_tokens_neox': 1837, 'n_words': 415}