<commit_before>import aerospike
from aerospike import exception as ex
from aerospike_helpers.batch import records as br
import aerospike_helpers.expressions as exp
from aerospike_helpers.operations import operations as op
import sys

config = { 'hosts': [('127.0.0.1', 3000)] }
client = aerospike.client(config).connect()

# Apply different operations to different keys
# using batch_write.
w_batch_record = br.BatchRecords(
    [
        br.Remove(
            key=(namespace, set, 1),
            policy={}
        ),
        br.Write(
            key=(namespace, set, 100),
            ops=[
                op.write("id", 100),
                op.write("balance", 100),
                op.read("id"),
                op.read("id"),
            ],
            policy={"expressions": exp.GT(exp.IntBin("balance"), 2000).compile()}
        ),
        br.Read(
            key=(namespace, set, 333),
            ops=[
                op.read("id")
            ],
            policy=None
        ),
    ]
)

try:
    # batch_write modifies its BatchRecords argument.
    # Results for each BatchRecord will be set in their result,
    # record, and in_doubt fields.

    client.batch_write(w_batch_record)

    for batch_record in w_batch_record.batch_records:
        print(batch_record.result)
        print(batch_record.record)
except ex.AerospikeError as e:
    print("Error: {0} [{1}]".format(e.msg, e.code))
    sys.exit(1)
finally:
    client.close()<commit_msg>Docs: aerospike.Client: revise batch_write example<commit_after>from aerospike_helpers.batch import records as br
from aerospike_helpers.operations import operations as op

# Keys
# Only insert two records with the first and second key
keyTuples = [
    ('test', 'demo', 'Robert'),
    ('test', 'demo', 'Daniel'),
    ('test', 'demo', 'Patrick'),
]
client.put(keyTuples[0], {'id': 100, 'balance': 400})
client.put(keyTuples[1], {'id': 101, 'balance': 200})
client.put(keyTuples[2], {'id': 102, 'balance': 300})

# Apply different operations to different keys
batchRecords = br.BatchRecords(
    [
        # Remove Robert from system
        br.Remove(
            key = keyTuples[0],
        ),
        # Modify Daniel's ID and balance 
        br.Write(
            key = keyTuples[1],
            ops = [
                op.write("id", 200),
                op.write("balance", 100),
                op.read("id"),
            ],
        ),
        # Read Patrick's ID
        br.Read(
            key = keyTuples[2],
            ops=[
                op.read("id")
            ],
            policy=None
        ),
    ]
)

client.batch_write(batchRecords)

# batch_write modifies its BatchRecords argument.
# Results for each BatchRecord will be set in the result, record, and in_doubt fields.
for batchRecord in batchRecords.batch_records:
    print(batchRecord.result)
    print(batchRecord.record)
# Note how written bins return None if their values aren't read
# 0
# (('test', 'demo', 'Robert', bytearray(b'...')), {'ttl': 4294967295, 'gen': 0}, {})
# 0
# (('test', 'demo', 'Daniel', bytearray(b'...')), {'ttl': 2592000, 'gen': 2}, {'id': 200, 'balance': None})
# 0
# (('test', 'demo', 'Patrick', bytearray(b'...')), {'ttl': 2592000, 'gen': 1}, {'id': 102})
{'fraction_non_alphanumeric': 0.1450665017012063, 'fraction_numerical': 0.029693782864212804, 'mean_word_length': 2.3970588235294117, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, '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': '2855322', 'n_tokens_mistral': 1097, 'n_tokens_neox': 974, 'n_words': 265}