<commit_before># Copyright (c) Metakernel Development Team.
# Distributed under the terms of the Modified BSD License.

from metakernel import Magic, option
import traceback
try:
    import jedi
    from jedi import Interpreter
    from jedi.api.helpers import completion_parts
    from jedi.parser.user_context import UserContext
except ImportError:
    jedi = None


class PythonMagic(Magic):

    def __init__(self, kernel):
        super(PythonMagic, self).__init__(kernel)
        self.env = globals()['__builtins__'].copy()
        self.retval = None

    def line_python(self, *args):
        """
        %python CODE - evaluate code as Python

        This line magic will evaluate the CODE (either expression or
        statement) as Python code.

        Note that the version of Python is that of the notebook server. 

        Examples:
            %python x = 42
            %python import math
            %python x + math.pi

        """
        code = " ".join(args)
        self.retval = self.eval(code)

    def eval(self, code):
        if "__builtins__" not in self.env:
            ## __builtins__ get generated after an eval:
            eval("1", self.env)
            ## make kernel available in imports:
            self.env["__builtins__"]["kernel"] = self.kernel
        try:
            return eval(code.strip(), self.env)
        except:
            try:
                exec(code.strip(), self.env)
            except Exception as exc:
                self.kernel.Error(traceback.format_exc())
                return None
        if "retval" in self.env:
            return self.env["retval"]

    @option(
        "-e", "--eval_output", action="store_true", default=False,
        help="Use the retval value from the Python cell as code in the kernel language."
    )
    def cell_python(self, eval_output=False):
        """<commit_msg>Add input and/or raw_input to python3/python2<commit_after># Copyright (c) Metakernel Development Team.
# Distributed under the terms of the Modified BSD License.

from metakernel import Magic, option
import traceback
import sys
try:
    import jedi
    from jedi import Interpreter
    from jedi.api.helpers import completion_parts
    from jedi.parser.user_context import UserContext
except ImportError:
    jedi = None

PY3 = sys.version_info[0] == 3

class PythonMagic(Magic):

    def __init__(self, kernel):
        super(PythonMagic, self).__init__(kernel)
        self.env = globals()['__builtins__'].copy()
        self.retval = None

    def line_python(self, *args):
        """
        %python CODE - evaluate code as Python

        This line magic will evaluate the CODE (either expression or
        statement) as Python code.

        Note that the version of Python is that of the notebook server. 

        Examples:
            %python x = 42
            %python import math
            %python x + math.pi

        """
        code = " ".join(args)
        self.retval = self.eval(code)

    def eval(self, code):
        if "__builtins__" not in self.env:
            ## __builtins__ get generated after an eval:
            eval("1", self.env)
            ## make 'kernel' and 'input' available:
            self.env["__builtins__"]["kernel"] = self.kernel
            if PY3:
                self.env["__builtins__"]["input"] = self.kernel.raw_input
                self.env["input"] = self.kernel.raw_input
            else:
                def input(*args, **kwargs):
                    return eval(self.kernel.raw_input(*args, **kwargs))
                self.env["__builtins__"]["input"] = input
                self.env["input"] = input
                self.env["__builtins__"]["raw_input"] = self.kernel.raw_input = self.kernel.raw_input
                self.env["raw_input"] = self.kernel.raw_input = self.kernel.raw_input
        try:
            return eval(code.strip(), self.env)
        except:
            pass
        try:
            exec(code.strip(), self.env)
        except Exception as exc:
            self.kernel.Error(traceback.format_exc())
            return None
        if "retval" in self.env:
            return self.env["retval"]

    @option(
        "-e", "--eval_output", action="store_true", default=False,
        help="Use the retval value from the Python cell as code in the kernel language."
    )
    def cell_python(self, eval_output=False):
        """
{'fraction_non_alphanumeric': 0.10013686131386862, 'fraction_numerical': 0.002737226277372263, 'mean_word_length': 2.0750350631136043, 'pattern_counts': {'":': 0, '<': 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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '226331', 'n_tokens_mistral': 1224, 'n_tokens_neox': 1188, 'n_words': 368}