# Buggy code
def toItem(self, chunkSize=65535):
    """
    :rtype: dict
    """
    item = {}
    serializedAndEncodedJob = base64.b64encode(bz2.compress(cPickle.dumps(self)))
    jobChunks = [
        serializedAndEncodedJob[i : i + chunkSize]
        for i in range(0, len(serializedAndEncodedJob), chunkSize)
    ]
    for attributeOrder, chunk in enumerate(jobChunks):
        item["_" + str(attributeOrder).zfill(3)] = chunk
    return item


# Traceback (WindowsAzureError)
WARNING:toil.leader:4f782575_20d9_48bb_bc1f_94cc8b207479:   Traceback (most recent call last):
WARNING:toil.leader:4f782575_20d9_48bb_bc1f_94cc8b207479:     File "/usr/local/lib/python2.7/dist-packages/toil/worker.py", line 282, in main
WARNING:toil.leader:4f782575_20d9_48bb_bc1f_94cc8b207479:       fileStore=fileStore)
WARNING:toil.leader:4f782575_20d9_48bb_bc1f_94cc8b207479:     File "/usr/local/lib/python2.7/dist-packages/toil/job.py", line 1282, in _execute
WARNING:toil.leader:4f782575_20d9_48bb_bc1f_94cc8b207479:       self._serialiseJobGraph(jobWrapper, jobStore, returnValues, False)
WARNING:toil.leader:4f782575_20d9_48bb_bc1f_94cc8b207479:     File "/usr/local/lib/python2.7/dist-packages/toil/job.py", line 1233, in _serialiseJobGraph
WARNING:toil.leader:4f782575_20d9_48bb_bc1f_94cc8b207479:       job._serialiseJob(jobStore, jobsToJobWrappers, jobWrapper)
WARNING:toil.leader:4f782575_20d9_48bb_bc1f_94cc8b207479:     File "/usr/local/lib/python2.7/dist-packages/toil/job.py", line 1196, in _serialiseJob
WARNING:toil.leader:4f782575_20d9_48bb_bc1f_94cc8b207479:       jobStore.update(jobsToJobWrappers[self])
WARNING:toil.leader:4f782575_20d9_48bb_bc1f_94cc8b207479:     File "/usr/local/lib/python2.7/dist-packages/toil/jobStores/azureJobStore.py", line 150, in update
WARNING:toil.leader:4f782575_20d9_48bb_bc1f_94cc8b207479:       entity=job.toItem(chunkSize=self.jobChunkSize))
WARNING:toil.leader:4f782575_20d9_48bb_bc1f_94cc8b207479:     File "/usr/local/lib/python2.7/dist-packages/toil/jobStores/azureJobStore.py", line 517, in f
WARNING:toil.leader:4f782575_20d9_48bb_bc1f_94cc8b207479:       return function(**kwargs)
WARNING:toil.leader:4f782575_20d9_48bb_bc1f_94cc8b207479:     File "/usr/local/lib/python2.7/dist-packages/azure/storage/tableservice.py", line 476, in update_entity
WARNING:toil.leader:4f782575_20d9_48bb_bc1f_94cc8b207479:       response = self._perform_request(request)
WARNING:toil.leader:4f782575_20d9_48bb_bc1f_94cc8b207479:     File "/usr/local/lib/python2.7/dist-packages/azure/storage/storageclient.py", line 179, in _perform_request
WARNING:toil.leader:4f782575_20d9_48bb_bc1f_94cc8b207479:       _storage_error_handler(ex)
WARNING:toil.leader:4f782575_20d9_48bb_bc1f_94cc8b207479:     File "/usr/local/lib/python2.7/dist-packages/azure/storage/__init__.py", line 1174, in _storage_error_handler
WARNING:toil.leader:4f782575_20d9_48bb_bc1f_94cc8b207479:       return _general_error_handler(http_error)
WARNING:toil.leader:4f782575_20d9_48bb_bc1f_94cc8b207479:     File "/usr/local/lib/python2.7/dist-packages/azure/__init__.py", line 551, in _general_error_handler
WARNING:toil.leader:4f782575_20d9_48bb_bc1f_94cc8b207479:       http_error.respbody.decode('utf-8-sig'))
WARNING:toil.leader:4f782575_20d9_48bb_bc1f_94cc8b207479:   WindowsAzureError: Unknown error (Bad Request)
WARNING:toil.leader:4f782575_20d9_48bb_bc1f_94cc8b207479:   <?xml version="1.0" encoding="utf-8"?><error xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"><code>PropertyValueTooLarge</code><message xml:lang="en-US">The property value exceeds the maximum allowed size (64KB). If the property value is a string, it is UTF-16 encoded and the maximum number of characters should be 32K or less.
WARNING:toil.leader:4f782575_20d9_48bb_bc1f_94cc8b207479:   RequestId:bc75b149-0002-0131-4274-260c9f000000
WARNING:toil.leader:4f782575_20d9_48bb_bc1f_94cc8b207479:   Time:2015-11-24T04:54:04.2292362Z</message></error>
WARNING:toil.leader:4f782575_20d9_48bb_bc1f_94cc8b207479:   Exiting the worker because of a failed jobWrapper on host c1agent34
WARNING:toil.leader:4f782575_20d9_48bb_bc1f_94cc8b207479:   ERROR:toil.worker:Exiting the worker because of a failed jobWrapper on host c1agent34
WARNING:toil.leader:4f782575_20d9_48bb_bc1f_94cc8b207479:   WARNING:toil.jobWrapper:Due to failure we are reducing the remaining retry count of job 4f782575_20d9_48bb_bc1f_94cc8b207479 to 0

# Fixed code
def toItem(self, chunkSize=maxAzureTablePropertySize):
    """
    :param chunkSize: the size of a chunk for splitting up the serialized job into chunks
    that each fit into a property value of the an Azure table entity
    :rtype: dict
    """
    assert chunkSize <= maxAzureTablePropertySize
    item = {}
    serializedAndEncodedJob = bz2.compress(cPickle.dumps(self))
    jobChunks = [
        serializedAndEncodedJob[i : i + chunkSize]
        for i in range(0, len(serializedAndEncodedJob), chunkSize)
    ]
    for attributeOrder, chunk in enumerate(jobChunks):
        item["_" + str(attributeOrder).zfill(3)] = EntityProperty("Edm.Binary", chunk)
    return item