<commit_before># https://pythonhosted.org/setuptools/setuptools.html#namespace-packages
__import__('pkg_resources').declare_namespace(__name__)

import sys
import os
import subprocess
import tempfile
import geojson
import logging

class entempified:

    def __init__(self, f):

        # note: we clean up after ourselves in the destructor
        # below (20150827/thisisaaronland)

        fh = tempfile.NamedTemporaryFile(delete=False)
        geojson.dump(f, fh)
        fh.close()

        logging.debug("entempified %s" % fh.name)
        self.fh = fh

    def __del__(self):

        path = self.path()

        if os.path.exists(path):
            logging.debug("unlink entempified %s" % path)
            os.unlink(path)

    def path(self):
        return self.fh.name

class source:

    def __init__(self, **kwargs):

        if kwargs.get('path', None):
            path = kwargs['path']

        elif kwargs.get('feature', None):
            feature = kwargs['feature']
            self._tmp = entempified(feature)
            path = self._tmp.path()

            # note the part where we're assigning _tmp to the object
            # so that it does not fall out of scope prematurely and
            # delete the underlying tempfile before we try to do something
            # with it (20150827/thisisaaronland)

        else:
            raise Exception, "Missing path or feature to centroidify"
        
        if not os.path.exists(path):
            raise Exception, "Invalid path for source (%s)" % path

        self._path = path

    def path(self):
        return self._path

class cli:

    def __init__(self, ms):

        if not os.path.exists(ms):
            err = "path to mapshaper binary (%s) does not exist" % ms
            raise Exception, err

        logging.debug("instantiating mapshaper cli instance with %s" % ms)
        self.ms = ms
        
    def centroidify(self, **kwargs):

        src = source(**kwargs)
        path = src.path()

        args = [
            "-i", path,
            "-points", "inner",
            "-o", "-"
        ]

        out = self.mapshaperify(args)

        featurecol = geojson.loads(out)
        feature = featurecol['features'][0]

        geom = feature['geometry']
        return geom

    def mapshaperify(self, args):

        args.insert(0, self.ms)
        cmd = " ".join(args)

        logging.debug(cmd)
        out = subprocess.check_output(args)

        logging.debug(out)
        return out<commit_msg>notes<commit_after># https://pythonhosted.org/setuptools/setuptools.html#namespace-packages
__import__('pkg_resources').declare_namespace(__name__)

import sys
import os
import subprocess
import tempfile
import geojson
import logging

class entempified:

    def __init__(self, f):

        # note: we clean up after ourselves in the destructor
        # below (20150827/thisisaaronland)

        fh = tempfile.NamedTemporaryFile(delete=False)
        geojson.dump(f, fh)
        fh.close()

        logging.debug("entempified %s" % fh.name)
        self.fh = fh

    def __del__(self):

        path = self.path()

        if os.path.exists(path):
            logging.debug("unlink entempified %s" % path)
            os.unlink(path)

    def path(self):
        return self.fh.name

class source:

    def __init__(self, **kwargs):

        if kwargs.get('path', None):
            path = kwargs['path']

        elif kwargs.get('feature', None):
            feature = kwargs['feature']
            self._tmp = entempified(feature)
            path = self._tmp.path()

            # note the part where we're assigning _tmp to the object
            # so that it does not fall out of scope prematurely and
            # delete the underlying tempfile before we try to do something
            # with it (20150827/thisisaaronland)

        else:
            raise Exception, "Missing path or feature to centroidify"
        
        if not os.path.exists(path):
            raise Exception, "Invalid path for source (%s)" % path

        self._path = path

    def path(self):
        return self._path

class cli:

    def __init__(self, ms):

        if not os.path.exists(ms):
            err = "path to mapshaper binary (%s) does not exist" % ms
            raise Exception, err

        logging.debug("instantiating mapshaper cli instance with %s" % ms)
        self.ms = ms
        
    # because this:
    # https://github.com/mbloch/mapshaper/issues/63
    
    def centroidify(self, **kwargs):

        src = source(**kwargs)
        path = src.path()

        args = [
            "-i", path,
            "-points", "inner",
            "-o", "-"
        ]

        out = self.mapshaperify(args)

        featurecol = geojson.loads(out)
        feature = featurecol['features'][0]

        geom = feature['geometry']
        return geom

    def mapshaperify(self, args):

        args.insert(0, self.ms)
        cmd = " ".join(args)

        logging.debug(cmd)
        out = subprocess.check_output(args)

        logging.debug(out)
        return out
{'fraction_non_alphanumeric': 0.10460747478742337, 'fraction_numerical': 0.007514336563179751, 'mean_word_length': 1.9648300117233295, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 3, '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': '4656198', 'n_tokens_mistral': 1534, 'n_tokens_neox': 1442, 'n_words': 404}