<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Project: Create a Proxy Class
#
# In this assignment, create a proxy class (one is started for you
# below).  You should be able to initialize the proxy object with any
# object.  Any attributes called on the proxy object should be forwarded
# to the target object.  As each attribute call is sent, the proxy should
# record the name of the attribute sent.
#
# The proxy class is started for you.  You will need to add a method
# missing handler and any other supporting methods.  The specification
# of the Proxy class is given in the AboutProxyObjectProject koan.

# Note: This is a bit trickier that its Ruby Koans counterpart, but you
# can do it!

from runner.koan import *


class Proxy(object):
    def __init__(self, target_object):
        # WRITE CODE HERE

        #initialize '_obj' attribute last. Trust me on this!
        self._obj = target_object

    # WRITE CODE HERE


# The proxy object should pass the following Koan:
#
class AboutProxyObjectProject(Koan):
    def test_proxy_method_returns_wrapped_object(self):
        # NOTE: The Television class is defined below
        tv = Proxy(Television())

        self.assertTrue(isinstance(tv, Proxy))

    def test_tv_methods_still_perform_their_function(self):
        tv = Proxy(Television())

        tv.channel = 10
        tv.power()

        self.assertEqual(10, tv.channel)
        self.assertTrue(tv.is_on())

    def test_proxy_records_messages_sent_to_tv(self):
        tv = Proxy(Television())
<commit_msg>Thinking AboutProxyObjectProject: DONE<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Project: Create a Proxy Class
#
# In this assignment, create a proxy class (one is started for you
# below).  You should be able to initialize the proxy object with any
# object.  Any attributes called on the proxy object should be forwarded
# to the target object.  As each attribute call is sent, the proxy should
# record the name of the attribute sent.
#
# The proxy class is started for you.  You will need to add a method
# missing handler and any other supporting methods.  The specification
# of the Proxy class is given in the AboutProxyObjectProject koan.

# Note: This is a bit trickier that its Ruby Koans counterpart, but you
# can do it!

from runner.koan import *


class Proxy(object):
    def __init__(self, target_object):
        # WRITE CODE HERE
        self._messages = []
        self._num_calls = {}

        #initialize '_obj' attribute last. Trust me on this!
        self._obj = target_object

    # WRITE CODE HERE
    def __getattr__(self, attr_name):
        self._update_call(attr_name)
        return self._obj.__getattribute__(attr_name)

    def __setattr__(self, attr_name, value):
        if (attr_name.startswith('_')):
            super(Proxy, self).__setattr__(attr_name, value)
        else:
            self._update_call(attr_name)
            self._obj.__setattr__(attr_name, value)

    def messages(self):
        return self._messages

    def was_called(self, attr_name):
        return attr_name in self._messages

    def number_of_times_called(self, attr_name):
        return self._num_calls.get(attr_name, 0)

    def _update_call(self, attr_name):
        self._messages.append(attr_name)
        self._num_calls.setdefault(attr_name, 0)
        self._num_calls[attr_name] += 1


# The proxy object should pass the following Koan:
#
class AboutProxyObjectProject(Koan):
    def test_proxy_method_returns_wrapped_object(self):
        # NOTE: The Television class is defined below
        tv = Proxy(Television())

        self.assertTrue(isinstance(tv, Proxy))

    def test_tv_methods_still_perform_their_function(self):
        tv = Proxy(Television())

        tv.channel = 10
        tv.power()

        self.assertEqual(10, tv.channel)
        self.assertTrue(tv.is_on())

    def test_proxy_records_messages_sent_to_tv(self):
        tv = Proxy(Television())

{'fraction_non_alphanumeric': 0.08041341063776153, 'fraction_numerical': 0.0032770355432316612, 'mean_word_length': 3.1506276150627617, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7649090', 'n_tokens_mistral': 1184, 'n_tokens_neox': 1131, 'n_words': 396}