<commit_before>import uuid

class NowObject(object):
    def __init__(self, parent=None, **kwargs):
        self.children = {}

        self.parent = parent

        if not self.parent:
            self.root = self
            self.pathchain = []
        else:
            self.pathchain = kwargs.get('pathchain', [])
            name = None
            if not self.pathchain:
                name = kwargs.get('name', uuid.uuid4().hex)
                self.pathchain = [name]

            self.root = self.parent.root
            if name:
                self.parent.register(self)
    
    def register(self, other):
        self.children[ other.pathchain[0] ] = other

    def __getattr__(self, key):
        if key.startswith('handle_'):
            return None
        pathchain = list(self.pathchain)
        pathchain.append(key)
        return NowObject(parent=self, pathchain=pathchain)

    def __repr__(self):
        return '<' + self.__class__.__name__ + ' ' + '.'.join(self.pathchain) + '>'
    
    def __call__(self, *args, **kwargs):
        wrapped_args = args
        wrapped_kwargs = kwargs
        return self.root.call(self.pathchain, *wrapped_args, **wrapped_kwargs)

    def call(self, pathchain, *args, **kwargs):
        pathchain = list(pathchain)

        next = pathchain.pop(0)

        child = self.children.get(next, None)
        if child:
            return child.call(pathchain, *args, **kwargs)

        handler = getattr(self, 'handle_' + next)
        if handler:
            return handler(*args, **kwargs)
        
        raise AttributeError(next)

class ChatRoom(NowObject):
    def send_message(self):
        return 'lala'

class AuthService(NowObject):
    def handle_login(self, username, password):
        print 'LOGIN'

        return ChatRoom(parent=self)

def main():    
    # Root Object the client interfaces with
    now = NowObject()

    default = NowObject(now, name='default')
    auth = AuthService(default, name='auth')

    room = now.default.auth.login(username='enki', password='<PASSWORD>') 

    print 'room', room
    print 'SENDMESSAGE', room.send_message()

if __name__ == '__main__':
    main()<commit_msg>start resolving<commit_after>import uuid
import weakref

class NowObject(object):
    def __init__(self, parent=None, **kwargs):
        self.children = weakref.WeakValueDictionary()

        self.is_original = False

        self.parent = parent

        if not self.parent:
            self.root = self
            self.pathchain = []
        else:
            self.pathchain = kwargs.get('pathchain', [])
            name = None
            if not self.pathchain:
                name = kwargs.get('name', uuid.uuid4().hex)
                self.pathchain = [name]
                self.is_original = True

            self.root = self.parent.root
            if name:
                self.parent.register(self)
    
    def register(self, other):
        print 'REGISTER SELF', self, 'HAS CHILD', other
        self.children[ other.pathchain[0] ] = other

    def __getattr__(self, key):
        if key.startswith('handle_'):
            return None
        pathchain = list(self.pathchain)
        pathchain.append(key)
        return NowObject(parent=self, pathchain=pathchain)

    def fullpath(self):
        x = []
        y = self
        while y:
            print 'ADDING', y.pathchain
            x.insert(0, '[' + '.'.join(y.pathchain) + ']' )
            y = y.parent
        
        fp = '.'.join(x)
        return fp

    def __repr__(self):
        return '<' + self.__class__.__name__ + ' PathHistory: ' + self.fullpath() + '>'
    
    def __call__(self, *args, **kwargs):
        wrapped_args = args
        wrapped_kwargs = kwargs
        return self.root.call(self.pathchain, *wrapped_args, **wrapped_kwargs)

    def call(self, pathchain, *args, **kwargs):
        pathchain = list(pathchain)

        if not pathchain:
            print 'END OF PATHCHAIN', self
            return self

        next = pathchain.pop(0)

        child = self.children.get(next, None)
        if child:
            return child.call(pathchain, *args, **kwargs)

        handler = getattr(self, 'handle_' + next)
        if handler:
            return handler(*args, **kwargs)
        
        raise AttributeError(next)
    
class ChatRoom(NowObject):
    def send_message(self):
        return 'lala'

class AuthService(NowObject):
    def handle_login(self, username, password):
        print 'LOGIN'

        return ChatRoom(self.root)

def main():    
    # Root Object the client interfaces with
    now = NowObject()
    default = NowObject(now, name='default')
    auth = AuthService(default, name='auth')

    room = now.default.auth.login(username='enki', password='<PASSWORD>')
    print now.default.children.items()
    print 'room', room
    print 'SENDMESSAGE', room.send_message()
    
    print getattr(now, room.pathchain[0])()

if __name__ == '__main__':
    main()
{'fraction_non_alphanumeric': 0.11925866236905722, 'fraction_numerical': 0.0016116035455278, 'mean_word_length': 2.108954289292423, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '6760360', 'n_tokens_mistral': 1480, 'n_tokens_neox': 1452, 'n_words': 338}