|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "42b7f162-35b4-4958-b57d-de78a20118ff", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "In order to always fetch the latest version of the data, such data is fetched from author's repository, then converted into a Python statement and finally executed.\n", |
| 9 | + "\n", |
| 10 | + "> Note about `exec`, it is discouraged to execute arbirtary code as is done in the following block. A malicious user could post harmful\n", |
| 11 | + "> code in such GitHub's file and then such code would be executed." |
| 12 | + ] |
| 13 | + }, |
| 14 | + { |
| 15 | + "cell_type": "code", |
| 16 | + "execution_count": 14, |
| 17 | + "id": "abae6b96-65db-49fe-ab49-b0f955418d6d", |
| 18 | + "metadata": {}, |
| 19 | + "outputs": [ |
| 20 | + { |
| 21 | + "name": "stdout", |
| 22 | + "output_type": "stream", |
| 23 | + "text": [ |
| 24 | + "[{'name': 'photo1.jpg', 'tags': {'food', 'coffee', 'drink', 'cup', 'breakfast', 'table', 'tableware'}}, {'name': 'photo2.jpg', 'tags': {'food', 'dish', 'vegetable', 'dinner', 'meal', 'meat', 'tableware'}}, {'name': 'photo3.jpg', 'tags': {'city', 'skyline', 'skyscraper', 'architecture', 'travel', 'building', 'cityscape'}}, {'name': 'photo4.jpg', 'tags': {'glass', 'drink', 'meal', 'grapes', 'fruit', 'juice', 'food'}}]\n" |
| 25 | + ] |
| 26 | + } |
| 27 | + ], |
| 28 | + "source": [ |
| 29 | + "import requests\n", |
| 30 | + "\n", |
| 31 | + "LIST_OF_DICTIONARIES_JSON_URL = \"https://raw.githubusercontent.com/pythondatabook/sources/main/ch2/list_of_dicts.txt\"\n", |
| 32 | + "\n", |
| 33 | + "response = requests.get(LIST_OF_DICTIONARIES_JSON_URL)\n", |
| 34 | + "response_text = response.text\n", |
| 35 | + "response_text = 'data = ' + response_text\n", |
| 36 | + "\n", |
| 37 | + "exec(response_text)\n", |
| 38 | + "print(data)" |
| 39 | + ] |
| 40 | + }, |
| 41 | + { |
| 42 | + "cell_type": "markdown", |
| 43 | + "id": "9309d683-2707-4857-aee4-a2ef09806dad", |
| 44 | + "metadata": {}, |
| 45 | + "source": [ |
| 46 | + "Group photos with intersecting tags, saving the results in `photo_groups`." |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | + "cell_type": "code", |
| 51 | + "execution_count": 35, |
| 52 | + "id": "86df43c8-d757-406c-86b9-b466111f7adb", |
| 53 | + "metadata": {}, |
| 54 | + "outputs": [ |
| 55 | + { |
| 56 | + "name": "stdout", |
| 57 | + "output_type": "stream", |
| 58 | + "text": [ |
| 59 | + "{\n", |
| 60 | + " \"tableware_food\": [\n", |
| 61 | + " \"photo1.jpg\",\n", |
| 62 | + " \"photo2.jpg\"\n", |
| 63 | + " ],\n", |
| 64 | + " \"drink_food\": [\n", |
| 65 | + " \"photo1.jpg\",\n", |
| 66 | + " \"photo4.jpg\"\n", |
| 67 | + " ],\n", |
| 68 | + " \"meal_food\": [\n", |
| 69 | + " \"photo2.jpg\",\n", |
| 70 | + " \"photo4.jpg\"\n", |
| 71 | + " ]\n", |
| 72 | + "}\n" |
| 73 | + ] |
| 74 | + } |
| 75 | + ], |
| 76 | + "source": [ |
| 77 | + "import json\n", |
| 78 | + "\n", |
| 79 | + "photo_groups = {}\n", |
| 80 | + "\n", |
| 81 | + "for photo_x in range(0, len(data)):\n", |
| 82 | + " for photo_y in range(photo_x+1, len(data)):\n", |
| 83 | + " intersection = data[photo_x]['tags'].intersection(data[photo_y]['tags'])\n", |
| 84 | + "\n", |
| 85 | + " if len(intersection) >= 2:\n", |
| 86 | + " intersection = list(intersection)\n", |
| 87 | + " key = '_'.join(intersection)\n", |
| 88 | + " photo_groups.setdefault(key, [data[photo_x]['name'], data[photo_y]['name']])\n", |
| 89 | + "\n", |
| 90 | + "print(json.dumps(photo_groups, indent=4))" |
| 91 | + ] |
| 92 | + } |
| 93 | + ], |
| 94 | + "metadata": { |
| 95 | + "kernelspec": { |
| 96 | + "display_name": "Python 3 (ipykernel)", |
| 97 | + "language": "python", |
| 98 | + "name": "python3" |
| 99 | + }, |
| 100 | + "language_info": { |
| 101 | + "codemirror_mode": { |
| 102 | + "name": "ipython", |
| 103 | + "version": 3 |
| 104 | + }, |
| 105 | + "file_extension": ".py", |
| 106 | + "mimetype": "text/x-python", |
| 107 | + "name": "python", |
| 108 | + "nbconvert_exporter": "python", |
| 109 | + "pygments_lexer": "ipython3", |
| 110 | + "version": "3.9.17" |
| 111 | + } |
| 112 | + }, |
| 113 | + "nbformat": 4, |
| 114 | + "nbformat_minor": 5 |
| 115 | +} |
0 commit comments