<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2017~2999 - cologler <<EMAIL>>
# ----------
#
# ----------

import functools
from .descriptor_store import IStore, FieldStore, NOVALUE

def get_descriptor_name(descriptor):
    if isinstance(descriptor, property):
        funcs = [descriptor.fget, descriptor.fset, descriptor.fdel]
        funcs = [f.__name__ for f in funcs if f is not None]
        return funcs[0]

    if isinstance(descriptor, getonly_property):
        return descriptor.fget.__name__

    return getattr(descriptor, '__name__', None)


class ICacheDescriptor:
    '''the base type for `CacheDescriptor`.'''
    pass


def cache(descriptor=None, *, store: IStore = None):
    '''
    usage:

    ``` py
    @cache
    @property
    def name(self): pass
    ```
    '''

    if descriptor is None:
        return functools.partial(cache, store=store)

    hasattrs = {
        'get': hasattr(descriptor, '__get__'),
        'set': hasattr(descriptor, '__set__'),
        'del': hasattr(descriptor, '__delete__')
    }

    descriptor_name = get_descriptor_name(descriptor)

    # pylint: disable=R0903,C0111
    class CacheDescriptor(ICacheDescriptor):
        def __init__(self):
            if descriptor_name is not None:
                self.__name__ = descriptor_name

        def __getattr__(self, name):
            print(name)
            raise NotImplementedError

        def __getattribute__(self, name):
            raise NotImplementedError

    cache_descriptor = CacheDescriptor()

    if store is None:
        field = descriptor_name or cache_descriptor
        store = FieldStore(field)
    elif not isinstance(store, IStore):
        raise TypeError(f'store must be a {IStore}.')
<commit_msg>use descriptor as key to save cached value<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2017~2999 - cologler <<EMAIL>>
# ----------
#
# ----------

import functools
from .descriptor_store import IStore, FieldStore, NOVALUE

def get_descriptor_name(descriptor):
    if isinstance(descriptor, property):
        funcs = [descriptor.fget, descriptor.fset, descriptor.fdel]
        funcs = [f.__name__ for f in funcs if f is not None]
        return funcs[0]

    if isinstance(descriptor, getonly_property):
        return descriptor.fget.__name__

    return getattr(descriptor, '__name__', None)


class ICacheDescriptor:
    '''the base type for `CacheDescriptor`.'''
    pass


def cache(descriptor=None, *, store: IStore = None):
    '''
    usage:

    ``` py
    @cache
    @property
    def name(self): pass
    ```
    '''

    if descriptor is None:
        return functools.partial(cache, store=store)

    hasattrs = {
        'get': hasattr(descriptor, '__get__'),
        'set': hasattr(descriptor, '__set__'),
        'del': hasattr(descriptor, '__delete__')
    }

    descriptor_name = get_descriptor_name(descriptor)

    # pylint: disable=R0903,C0111
    class CacheDescriptor(ICacheDescriptor):
        def __init__(self):
            if descriptor_name is not None:
                self.__name__ = descriptor_name

        def __getattr__(self, name):
            print(name)
            raise NotImplementedError

        def __getattribute__(self, name):
            raise NotImplementedError

    cache_descriptor = CacheDescriptor()

    if store is None:
        store = FieldStore(cache_descriptor)
    elif not isinstance(store, IStore):
        raise TypeError(f'store must be a {IStore}.')

{'fraction_non_alphanumeric': 0.11538461538461539, 'fraction_numerical': 0.010256410256410256, 'mean_word_length': 2.6611053180396245, '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': '3666609', 'n_tokens_mistral': 1059, 'n_tokens_neox': 1014, 'n_words': 269}