Skip to main content

Syntactic cached property generator for classes.

Project description

Syntactic Cache

Tests codecov PyPI version

Syntactic Cache is a Python library that allows you to automatically create cached properties for methods in your classes, using the syntax of the methods to determine which methods should be cached.

I.e, methods with the get_ prefix will have a cached property created for them using the method name without the prefix, this allows for easy refactoring of existing code adding cached properties of functions 'for free'. The cache is a simple memory cache linked to each instance of the class.

Installation

You can install Syntactic Cache using pip.

pip install syntactic-cache

Usage

Here's a simple example of how to use Syntactic Cache, curently the library support 'get_' and 'is_' prefixes for methods.

from syntactic_cache import do_not_make_cached_property, make_cached_properties

@make_cached_properties
class MyClass:
    def __init__(self, value):
        self.value = value

    def get_power(self, exponent):
        return self.value ** exponent

    def get_square(self):
        return self.get_power(2)

    @do_not_make_cached_property
    def get_cube(self):
        return self.get_power(3)

    def is_even(self):
        return self.value % 2 == 0

instance = MyClass(3)

print(instance.get_square())  # 9, does not use the cache,
print(instance.get_cube())    # 27, does not use the cache.

print(instance.square)  # 9, sets and uses the cache.
print(instance.cube)    # Raises AttributeError, as making cached properties is disabled.
print(instance.power)  # Raises AttributeError, as will not make cached properties for methods with arguments.

print(instance.is_even())  # False, does not use the cache.
print(instance.even)  # False, sets and uses the cache.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

syntactic_cache-1.0.0.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

syntactic_cache-1.0.0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file syntactic_cache-1.0.0.tar.gz.

File metadata

  • Download URL: syntactic_cache-1.0.0.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.7.19

File hashes

Hashes for syntactic_cache-1.0.0.tar.gz
Algorithm Hash digest
SHA256 decdce3ca90a29b22846014b10b110c0797d189fb6ebbedec8c831bb311affbf
MD5 1d5a25bbb181f8600292195bdc08fbd2
BLAKE2b-256 9ddacb4ac9e2339b8ab3cc37bff0e40ed95661c09f8f2d821b26b85c73fb4266

See more details on using hashes here.

File details

Details for the file syntactic_cache-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for syntactic_cache-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ed2b8be356cdb02d67d6c39f44e426867c86f9d54ac0c6e9d26c7b40b6e4e8a1
MD5 aec7a771a5f3882c41dfbda05eec6a51
BLAKE2b-256 8679c6cc885764e813d38fc26490527c3fff698faa7aff195280a3558e148b28

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page