A nice long string of key-value pairs!
Here's the Python code to convert it to a dictionary:
import ast
# Your input string
input_str = "'photo_ids: [None]', 'photo_types: [None]', 'filepaths: [None]', \"task_types: ['FACE_MATCHING']\", \"partner_ids: ['50f6cabd-2ff0-430c-8388-b0dd6a894a3e']\", \"event_ids: ['b3e03d6d-d6f0-4243-b769-210d253182bf']\", \"subsystem_ids: ['partner-registry']\", \"subsystem_entity_ids: ['171188e0-e9e0-4225-8e8a-6fde46a34478']\", 'task_statuses: [<TaskStatus.SUCCESS: 1>]', \"face_photo_ids: ['62708f3d-9e85-4da6-bf6d-6265a8a00bad.jpg']\", \"face_photo_types: ['FACE']\", \"face_filepaths: ['selfie-photos-sec/62708f3d-9e85-4da6-bf6d-6265a8a00bad.jpg']\", \"doc_photo_ids: ['7d69268c-7a92-45db-9b96-b5f993bccddf.jpg']\", \"doc_photo_types: ['PASSPORT']\", \"doc_filepaths: ['passport-photos-sec/7d69268c-7a92-45db-9b96-b5f993bccddf.jpg']\", 'ocr_payloads: [None]'"
# Split the string into individual key-value pairs
pairs = [pair.strip().strip("'").strip('"') for pair in input_str.split(', ')]
# Create an empty dictionary
result_dict = {}
# Iterate over each key-value pair and parse it
for pair in pairs:
key, value = pair.split(': ')
# Use ast.literal_eval to safely evaluate the value as a Python literal
try:
value = ast.literal_eval(value)
except (ValueError, SyntaxError):
# If the value is not a valid Python literal (e.g., <TaskStatus.SUCCESS: 1>), keep it as a string
pass
result_dict[key] = value
print(result_dict)
Here's the resulting dictionary:
{
'photo_ids': [None],
'photo_types': [None],
'filepaths': [None],
'task_types': ['FACE_MATCHING'],
'partner_ids': ['50f6cabd-2ff0-430c-8388-b0dd6a894a3e'],
'event_ids': ['b3e03d6d-d6f0-4243-b769-210d253182bf'],
'subsystem_ids': ['partner-registry'],
'subsystem_entity_ids': ['171188e0-e9e0-4225-8e8a-6fde46a34478'],
'task_statuses': ['<TaskStatus.SUCCESS: 1>'], # Note: this value is kept as a string
'face_photo_ids': ['62708f3d-9e85-4da6-bf6d-6265a8a00bad.jpg'],
'face_photo_types': ['FACE'],
'face_filepaths': ['selfie-photos-sec/62708f3d-9e85-4da6-bf6d-6265a8a00bad.jpg'],
'doc_photo_ids': ['7d69268c-7a92-45db-9b96-b5f993bccddf.jpg'],
'doc_photo_types': ['PASSPORT'],
'doc_filepaths': ['passport-photos-sec/7d69268c-7a92-45db-9b96-b5f993bccddf.jpg'],
'ocr_payloads': [None]
}
Note that the value for 'task_statuses' is kept as a string ('<TaskStatus.SUCCESS: 1>') because ast.literal_eval cannot evaluate it as a valid Python literal (it's an enum value, I assume). If you have the TaskStatus enum defined somewhere in your code, you'll need to parse this value manually.
Example:
from enum import Enum
class TaskStatus(Enum):
SUCCESS = 1
# ...
if key == 'task_statuses':
value = [TaskStatus(int(val.split(':')[-1].strip('>'))) for val in value]
This will convert the 'task_statuses' value to [TaskStatus.SUCCESS]. Adjust according to your actual enum definition.