159 lines
5.3 KiB
Plaintext
159 lines
5.3 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"id": "090c7992",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\n",
|
|
" im still struggling:\n",
|
|
" </p>\n",
|
|
" <p>sudo pacman -S archlinux-keyring</p>\n",
|
|
" <p>melange:</p>\n",
|
|
" <ul>\n",
|
|
" <li><a href=\"/page2/line/lineplot.html\">machine learning with failed hyperparams</a></li>\n",
|
|
" <li><a href=\"/page2/dblp/bar/index.html\">the dblp dataset in bar plot</a></li>\n",
|
|
" <li><a href=\"/page2/dblp/edge/index.html\">the dblp dataset with an example of community</a></li>\n",
|
|
" <li><a href=\"/page2/dblp/full/index.html\">the dblp dataset of 5 years as a graph</a></li>\n",
|
|
" <li><a href=\"/page2/dblp/time/index.html\">the generated links as a graph</a></li>\n",
|
|
"\n",
|
|
" </ul>\n",
|
|
"\n",
|
|
"Post 3 updated successfully.\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"import datetime\n",
|
|
"\n",
|
|
"def get_entry(file_path, id):\n",
|
|
"\n",
|
|
" # 1. Import the current list to get the latest ID\n",
|
|
"\n",
|
|
" # We use a namespace to avoid path issues\n",
|
|
"\n",
|
|
" namespace = {}\n",
|
|
"\n",
|
|
" with open(file_path, 'r') as f:\n",
|
|
"\n",
|
|
" exec(f.read(), namespace)\n",
|
|
"\n",
|
|
" posts = namespace.get('BLOG_POSTS', [])\n",
|
|
"\n",
|
|
" for items in posts:\n",
|
|
"\n",
|
|
" if items.get(\"id\") == id:\n",
|
|
"\n",
|
|
" content = items.get('content')\n",
|
|
"\n",
|
|
" print(content)\n",
|
|
"\n",
|
|
" return items \n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"def update_entry(file_path, post_id, new_content):\n",
|
|
" # 1. Load the existing data into memory\n",
|
|
" namespace = {}\n",
|
|
" with open(file_path, 'r') as f:\n",
|
|
" exec(f.read(), namespace)\n",
|
|
" \n",
|
|
" posts = namespace.get('BLOG_POSTS', [])\n",
|
|
" \n",
|
|
" # 2. Find and update the specific post\n",
|
|
" found = False\n",
|
|
" for post in posts:\n",
|
|
" if post.get(\"id\") == post_id:\n",
|
|
" post['content'] = new_content\n",
|
|
" # Optional: Update the date or add an \"updated\" field\n",
|
|
" # post['subtitle'] += \" (Updated)\" \n",
|
|
" found = True\n",
|
|
" break\n",
|
|
" \n",
|
|
" if not found:\n",
|
|
" print(f\"Error: Post {post_id} not found.\")\n",
|
|
" return\n",
|
|
"\n",
|
|
" # 3. Write the entire list back to the file\n",
|
|
" # Using repr() or a loop to format it cleanly\n",
|
|
" with open(file_path, 'w') as f:\n",
|
|
" f.write(\"BLOG_POSTS = [\\n\")\n",
|
|
" for p in posts:\n",
|
|
" # Use .get() to provide a default value if the key is missing\n",
|
|
" display_val = p.get('displayall', False) \n",
|
|
" data_val = p.get('date', \"?\") \n",
|
|
" subtitle_val = p.get('subtitle', \"\") \n",
|
|
" \n",
|
|
" entry = f\"\"\" {{\n",
|
|
" 'id': {p['id']},\n",
|
|
" 'title': \"{p['title']}\",\n",
|
|
" 'subtitle': \"{subtitle_val}\",\n",
|
|
" 'date': \"{data_val}\",\n",
|
|
" 'content': \\\"\\\"\\\"{p['content']}\\\"\\\"\\\",\n",
|
|
" \"displayall\": {display_val}\n",
|
|
" }},\\n\"\"\"\n",
|
|
" f.write(entry)\n",
|
|
" f.write(\"]\\n\")\n",
|
|
" \n",
|
|
" print(f\"Post {post_id} updated successfully.\")\n",
|
|
"\n",
|
|
"# --- Usage Example ---\n",
|
|
"if __name__ == \"__main__\":\n",
|
|
" file = '../content/posts.py'\n",
|
|
" target_id = 3\n",
|
|
" \n",
|
|
" # Get the old one first to see it\n",
|
|
" old_post = get_entry(file, target_id)\n",
|
|
" \n",
|
|
" if old_post:\n",
|
|
" pass\n",
|
|
"\n",
|
|
" new_text=\"\"\"\n",
|
|
" <p>sudo pacman -S archlinux-keyring</p>\n",
|
|
" <p>sudo pacman -Sc</p>\n",
|
|
" <p>melange:</p>\n",
|
|
" <ul>\n",
|
|
" <li><a href=\"/page2/line/lineplot.html\">machine learning with failed hyperparams</a></li>\n",
|
|
" <li><a href=\"/page2/dblp/bar/index.html\">the dblp dataset in bar plot</a></li>\n",
|
|
" <li><a href=\"/page2/dblp/edge/index.html\">the dblp dataset with an example of community</a></li>\n",
|
|
" <li><a href=\"/page2/dblp/full/index.html\">the dblp dataset of 5 years as a graph</a></li>\n",
|
|
" <li><a href=\"/page2/dblp/time/index.html\">the generated links as a graph</a></li>\n",
|
|
"\n",
|
|
" </ul>\n",
|
|
"\n",
|
|
"\n",
|
|
" \"\"\"\n",
|
|
" \n",
|
|
" \n",
|
|
" update_entry(file, target_id, new_text)\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "base",
|
|
"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.11.7"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|