<commit_before>""" The classes returned by the api """

import re
import os

import settings
import evaluate
import parsing
import keywords


class BaseOutput(object):
    _mapping = {'posixpath': 'os.path',
               'riscospath': 'os.path',
               'ntpath': 'os.path',
               'os2emxpath': 'os.path',
               'macpath': 'os.path',
               'genericpath': 'os.path',
               '_io': 'io'
               }

    _tuple_mapping = {
               ('argparse', '_ActionsContainer'): 'argparse._ActionsContainer'
               }

    def __init__(self, definition, start_pos):
        self.start_pos = start_pos
        self.definition = definition
        self.is_keyword = isinstance(definition, keywords.Keyword)

        # generate the type
        self.stripped_definition = self.definition
        if isinstance(self.definition, evaluate.InstanceElement):
            self.stripped_definition = self.definition.var
        self.type = type(self.stripped_definition).__name__

        # generate a path to the definition
        self.module_path = str(definition.get_parent_until().path)
        self.path = []
        if not isinstance(definition, keywords.Keyword):
            par = definition
            while par is not None:
                try:
                    self.path.insert(0, par.name)
                except AttributeError:
                    pass
                par = par.parent()

    @property
    def module_name(self):
        path = self.module_path
        sep = os.path.sep
        p = re.sub(r'^.*?([\w\d]+)(%s__init__)?.py$' % sep, r'\1', path)
        return p

    def in_builtin_module(self):
        return not self.module_path.endswith('.py')

    @property
    def line_nr(self):
        return self.start_pos[0]

    @property
    def column(self):
        return self.start_pos[1]

    @property
    def doc(self):
        """ Return a document string for this completion object. """
        try:
            return self.definition.doc
        except AttributeError:
            return self.raw_doc

    @property
    def raw_doc(self):
        """ Returns the raw docstring `__doc__` for any object """
        try:
            return str(self.definition.docstr)
        except AttributeError:
            return ''

    @property
    def description(self):
        raise NotImplementedError('Base Class')

    @property
    def full_name(self):
        """
        Returns the path to a certain class/function, see #61.
        """
        path = [str(p) for p in self.path]
        # TODO add further checks, the mapping should only occur on stdlib.
        try:
            path[0] = self._mapping[path[0]]
        except KeyError:
            pass
        for key, repl in self._tuple_mapping.items():
            if tuple(path[:len(key)]) == key:
                path = [repl] + path[len(key)]

        return '.'.join(path)

    def __repr__(self):
        return "<%s %s>" % (type(self).__name__, self.description)


class Completion(BaseOutput):
    """ `Completion` objects are returned from `Script.complete`. Providing
    some useful functions for IDE's. """
    def __init__(self, name, needs_dot, like_name_length, base):
        super(Completion, self).__init__(name.parent(), name.start_pos)

        self.name = name
        self.needs_dot = needs_dot
        self.like_name_length = like_name_length
        self.base = base

    @property
    def complete(self):
        """ Delievers the rest of the word, e.g. completing `isinstance`
        >>> isinstan<commit_msg>Fix type error in BaseOutput.full_name<commit_after>""" The classes returned by the api """

import re
import os

import settings
import evaluate
import parsing
import keywords


class BaseOutput(object):
    _mapping = {'posixpath': 'os.path',
               'riscospath': 'os.path',
               'ntpath': 'os.path',
               'os2emxpath': 'os.path',
               'macpath': 'os.path',
               'genericpath': 'os.path',
               '_io': 'io'
               }

    _tuple_mapping = {
               ('argparse', '_ActionsContainer'): 'argparse._ActionsContainer'
               }

    def __init__(self, definition, start_pos):
        self.start_pos = start_pos
        self.definition = definition
        self.is_keyword = isinstance(definition, keywords.Keyword)

        # generate the type
        self.stripped_definition = self.definition
        if isinstance(self.definition, evaluate.InstanceElement):
            self.stripped_definition = self.definition.var
        self.type = type(self.stripped_definition).__name__

        # generate a path to the definition
        self.module_path = str(definition.get_parent_until().path)
        self.path = []
        if not isinstance(definition, keywords.Keyword):
            par = definition
            while par is not None:
                try:
                    self.path.insert(0, par.name)
                except AttributeError:
                    pass
                par = par.parent()

    @property
    def module_name(self):
        path = self.module_path
        sep = os.path.sep
        p = re.sub(r'^.*?([\w\d]+)(%s__init__)?.py$' % sep, r'\1', path)
        return p

    def in_builtin_module(self):
        return not self.module_path.endswith('.py')

    @property
    def line_nr(self):
        return self.start_pos[0]

    @property
    def column(self):
        return self.start_pos[1]

    @property
    def doc(self):
        """ Return a document string for this completion object. """
        try:
            return self.definition.doc
        except AttributeError:
            return self.raw_doc

    @property
    def raw_doc(self):
        """ Returns the raw docstring `__doc__` for any object """
        try:
            return str(self.definition.docstr)
        except AttributeError:
            return ''

    @property
    def description(self):
        raise NotImplementedError('Base Class')

    @property
    def full_name(self):
        """
        Returns the path to a certain class/function, see #61.
        """
        path = [str(p) for p in self.path]
        # TODO add further checks, the mapping should only occur on stdlib.
        try:
            path[0] = self._mapping[path[0]]
        except KeyError:
            pass
        for key, repl in self._tuple_mapping.items():
            if tuple(path[:len(key)]) == key:
                path = [repl] + path[len(key):]

        return '.'.join(path)

    def __repr__(self):
        return "<%s %s>" % (type(self).__name__, self.description)


class Completion(BaseOutput):
    """ `Completion` objects are returned from `Script.complete`. Providing
    some useful functions for IDE's. """
    def __init__(self, name, needs_dot, like_name_length, base):
        super(Completion, self).__init__(name.parent(), name.start_pos)

        self.name = name
        self.needs_dot = needs_dot
        self.like_name_length = like_name_length
        self.base = base

    @property
    def complete(self):
        """ Delievers the rest of the word, e.g. completing `isinstance`
        >>> isinstan
{'fraction_non_alphanumeric': 0.10834962301033231, 'fraction_numerical': 0.002513264451270595, 'mean_word_length': 2.002095557418273, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '467459', 'n_tokens_mistral': 2025, 'n_tokens_neox': 1972, 'n_words': 523}