<commit_before>===============
Using Cassandra
===============

Make sure you have a Cassandra server available on localhost at port 9042.

Install the library with the 'cassandra' option.

::

    pip install eventsourcing[cassandra]


Infrastructure
--------------

Setup the connection and the database tables, using the library classes for Cassandra.

.. code:: python

    from eventsourcing.infrastructure.cassandra.datastore import CassandraSettings, CassandraDatastore
    from eventsourcing.infrastructure.cassandra.activerecords import IntegerSequencedItemRecord

    cassandra_datastore = CassandraDatastore(
        settings=CassandraSettings(),
        tables=(IntegerSequencedItemRecord,),
    )

    cassandra_datastore.setup_connection()
    cassandra_datastore.setup_tables()


Application object
------------------

Define a factory that uses library classes for Cassandra to construct an application
object. Investigate the library class ``CassandraSettings`` for information about
configuring away from default settings.

.. code:: python

    from eventsourcing.example.application import ExampleApplication
    from eventsourcing.infrastructure.cassandra.activerecords import CassandraActiveRecordStrategy

    def construct_application():
        active_record_strategy = CassandraActiveRecordStrategy(
            active_record_class=IntegerSequencedItemRecord,
        )
        app = ExampleApplication(
            integer_sequenced_active_record_strategy=active_record_strategy,
        )
        return app


Run the code
------------

The example application can be used in the same way as before.

.. code:: python

    with construct_application() as app:

        entity = app.create_new_example(foo='bar1')

        assert entity.id in app.example_repository

        assert app.example_repository[entity.id].foo == 'bar1'

        entity.foo = 'bar2'

        assert app.example_repository[entity.id].foo == 'bar2'

        # Discard the entity.
        entity.discard()
        assert entity.id not in app.example_repository

        try:
            app.example_repository[entity.id]
        except KeyError:
            pass
        else:
            raise Exception('KeyError was not raised')
<commit_msg>Moved sentence, was in wrong place.<commit_after>===============
Using Cassandra
===============

Make sure you have a Cassandra server available on localhost at port 9042.

Install the library with the 'cassandra' option.

::

    pip install eventsourcing[cassandra]


Infrastructure
--------------

Setup the connection and the database tables, using the library classes for Cassandra.

Investigate the library class ``CassandraSettings`` for information about
configuring away from default settings.

.. code:: python

    from eventsourcing.infrastructure.cassandra.datastore import CassandraSettings, CassandraDatastore
    from eventsourcing.infrastructure.cassandra.activerecords import IntegerSequencedItemRecord

    cassandra_datastore = CassandraDatastore(
        settings=CassandraSettings(),
        tables=(IntegerSequencedItemRecord,),
    )

    cassandra_datastore.setup_connection()
    cassandra_datastore.setup_tables()


Application object
------------------

Define a factory that uses library classes for Cassandra to construct an application
object.

.. code:: python

    from eventsourcing.example.application import ExampleApplication
    from eventsourcing.infrastructure.cassandra.activerecords import CassandraActiveRecordStrategy

    def construct_application():
        active_record_strategy = CassandraActiveRecordStrategy(
            active_record_class=IntegerSequencedItemRecord,
        )
        app = ExampleApplication(
            integer_sequenced_active_record_strategy=active_record_strategy,
        )
        return app


Run the code
------------

The example application can be used in the same way as before.

.. code:: python

    with construct_application() as app:

        entity = app.create_new_example(foo='bar1')

        assert entity.id in app.example_repository

        assert app.example_repository[entity.id].foo == 'bar1'

        entity.foo = 'bar2'

        assert app.example_repository[entity.id].foo == 'bar2'

        # Discard the entity.
        entity.discard()
        assert entity.id not in app.example_repository

        try:
            app.example_repository[entity.id]
        except KeyError:
            pass
        else:
            raise Exception('KeyError was not raised')

{'fraction_non_alphanumeric': 0.08955885612946132, 'fraction_numerical': 0.0035468853912657946, 'mean_word_length': 3.131868131868132, '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': '3612390', 'n_tokens_mistral': 1175, 'n_tokens_neox': 1103, 'n_words': 327}