Skip to content

Instantly share code, notes, and snippets.

@apage43
Created December 5, 2023 03:11
Show Gist options
  • Save apage43/2efbf384da91fe56bc1a8cd803ca7be1 to your computer and use it in GitHub Desktop.
Save apage43/2efbf384da91fe56bc1a8cd803ca7be1 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import json"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"with open('tagsource.json' ,'r') as tf:\n",
" tagsrc_db = json.load(tf)\n",
"with open('tagdest.json', 'r') as tf:\n",
" tagdest_db = json.load(tf)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tbls = {t['tableName']: t['rows'] for t in tagsrc_db['data']['data']}\n",
"dsttbls = {t['tableName']: t['rows'] for t in tagdest_db['data']['data']}\n",
"tags_by_id = {tag['id']: tag for tag in tbls['tags']}\n",
"def tag_id_to_name(id):\n",
" return tags_by_id[id]['name']\n",
"\n",
"dst_tag_ids = set(tag['id'] for tag in dsttbls['tags'])\n",
"\n",
"for (tag_id, tag) in tags_by_id.items():\n",
" if tag_id not in dst_tag_ids:\n",
" print(f'Transferring tag definition {tag_id_to_name(tag_id)} ({tag_id})')\n",
" dsttbls['tags'].append(tag)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"imgs_by_relative_path = {i['relativePath']: i for i in tbls['files']}\n",
"dst_imgs_by_relative_path = {i['relativePath']: i for i in dsttbls['files']}\n",
"\n",
"for (imgrp, img) in imgs_by_relative_path.items():\n",
" dsti = dst_imgs_by_relative_path.get(imgrp)\n",
" if dsti is None:\n",
" print(f'no match for {imgrp}')\n",
" continue\n",
" dst_tags = set(dsti['tags'])\n",
" src_tags = set(img['tags'])\n",
" add_tags = src_tags - dst_tags\n",
" if add_tags:\n",
" print(f'adding tags to {imgrp}: {add_tags}')\n",
" for tag in add_tags:\n",
" dsti['tags'].append(tag)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"with open('combined.json', 'w') as tf:\n",
" json.dump(tagdest_db, tf)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment