{ "cells": [ { "cell_type": "code", "execution_count": 15, "id": "eb57d79f-c9cd-45a6-bc29-8a4cbd9dc87e", "metadata": {}, "outputs": [], "source": [ "def to_int(s):\n", " try:\n", " n = int(s)\n", " except ValueError:\n", " n = int(s[0]) * 10 + ord(s[1])\n", " return n\n", "\n", "def get_id(s):\n", " return s[s.index('#ch_')+1:]" ] }, { "cell_type": "code", "execution_count": 24, "id": "2ec7ea76-776f-48f7-b84b-d172d41cb004", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/4q /13 ch_ifaces_prot_abc\n", "/4r /16 ch_op_overload\n", "/4s /17 ch_generators\n", "/4t /12 ch_seq_methods\n", "/53 /24 ch_class_metaprog\n", "/54 /15 ch_more_types\n", "/55 /23 ch_descriptors\n", "/56 /14 ch_inheritance\n", "/57 /21 ch_async\n", "/5c /9 ch_closure_decorator\n", "/5d /10 ch_design_patterns\n", "{'/4q': '/13',\n", " '/4r': '/16',\n", " '/4s': '/17',\n", " '/4t': '/12',\n", " '/53': '/24',\n", " '/54': '/15',\n", " '/55': '/23',\n", " '/56': '/14',\n", " '/57': '/21',\n", " '/5c': '/9',\n", " '/5d': '/10'}\n" ] } ], "source": [ "with open('links/FPY.LI.htaccess') as fp:\n", " lines = fp.readlines()\n", "\n", "PRE = 'https://pythonfluente.com/2/#ch_'\n", "\n", "id2n = {}\n", "subs = {}\n", "\n", "for line in lines:\n", " try:\n", " _, short, long = line.strip().split()\n", " except ValueError:\n", " continue\n", " if not long.startswith(PRE):\n", " continue\n", " n = to_int(short[1:])\n", " id = get_id(long)\n", " if n in range(1, 25):\n", " id2n[id] = n\n", " else:\n", " print(f'{short} /{id2n[id]} {id}')\n", " subs[short] = f'/{id2n[id]}'\n", "\n", "from pprint import pprint\n", "pprint(subs)" ] }, { "cell_type": "code", "execution_count": 31, "id": "d4efc774-3fae-4dd2-91c9-284985f3bddc", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "vol1/cap03.adoc\n", "vol1/Copyright-pb.adoc\n", "vol1/cap02.adoc\n", "vol1/cap05.adoc\n", "vol1/cap04.adoc\n", "vol1/cap08.adoc\n", "vol1/Copyright-cor.adoc\n", "vol1/cap07.adoc\n", "vol1/cap06.adoc\n", "vol1/titulos-vol1.adoc\n", "vol1/vol1-pb.adoc\n", "vol1/vol1-cor.adoc\n", "vol1/cap01.adoc\n", "vol1/Prefacio.adoc\n" ] } ], "source": [ "from glob import glob\n", "\n", "link = 'fpy.li{}&'\n", "\n", "for name in glob('vol1/*.adoc'):\n", " print(name)\n", " with open(name) as fp:\n", " adoc = fp.read()\n", " for old, new in subs.items():\n", " old, new = link.format(old), link.format(new)\n", " adoc = adoc.replace(old, new)\n", " with open(name, 'w', encoding='utf8') as fp:\n", " fp.write(adoc)\n", "\n", " " ] }, { "cell_type": "code", "execution_count": null, "id": "e801a363-bb8c-4db9-ae1b-216fc1dbb27c", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.14.0rc1" } }, "nbformat": 4, "nbformat_minor": 5 }