Skip to content

Frequently used code snippets library for extracting and manipulating WDC data

License

Notifications You must be signed in to change notification settings

livingearth-system/code-snippets

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1e6aab4a-acb9-403c-abd8-6a242ba420e4",
   "metadata": {},
   "source": [
    "# WDC Code Snippet Library\n",
    "\n",
    "This notebook creates a code snippet library that you can use to quickly add commonly used features and functions to the notebooks you are developing. It needs to be setup before you start using in and populated with the shared code snippets in the library folder.\n",
    "\n",
    "**To add the snippets from this library, select 'Run' on the top menu, and then select 'Run All Cells'**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "09afe4a1-a5f8-4ce0-b71f-4030cb5366d3",
   "metadata": {
    "jupyter": {
     "source_hidden": true
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import json\n",
    "import glob\n",
    "from datetime import datetime\n",
    "\n",
    "config_file_path = \"/home/jovyan/.jupyter/lab/user-settings/jupyterlab-code-snippets/snippets.jupyterlab-settings\"\n",
    "\n",
    "def extract_id(json):\n",
    "    try:\n",
    "        return json['id']\n",
    "    except KeyError:\n",
    "        return 0\n",
    "\n",
    "def build_library():\n",
    "    snippets = []\n",
    "    user_snippets = []\n",
    "    counter = 0\n",
    "    config = {}\n",
    "    \n",
    "    for filepath in glob.glob(os.path.join('library', '*.json')):\n",
    "        with open(filepath) as f:\n",
    "            content = json.load(f)\n",
    "            if not('tags' in content):\n",
    "                content['tags'] = []\n",
    "            if not('wdc_library' in content['tags']):\n",
    "                content['tags'].append('wdc_library')\n",
    "            snippets.append(content)\n",
    "            \n",
    "    snippets.sort(key=extract_id)\n",
    "        \n",
    "    # check is the user already has a snippet library and be careful not to obliterate it; instead\n",
    "    # remove all snippets with 'wdc_library' tag and merge it with the global library of snippets\n",
    "    if os.path.exists(config_file_path):\n",
    "        with open(config_file_path) as f:\n",
    "            content = \"\"\n",
    "            while line := f.readline():\n",
    "                if not line.lstrip().startswith(\"//\"):\n",
    "                    content = content + line\n",
    "            try:\n",
    "                user_config = json.loads(content)\n",
    "            except:\n",
    "                print(\"ERROR: Failed to parse your existing configuration file. Continuing may delete existing snippets so exiting without making changes\")\n",
    "                return\n",
    "            \n",
    "        # create a backup\n",
    "        with open(f'{config_file_path}-backup-{datetime.now().strftime(\"%Y-%m-%d\")}', 'w', encoding='utf-8') as f:\n",
    "            json.dump(user_config, f, ensure_ascii=False, indent=4)\n",
    "        \n",
    "        user_snippets = []\n",
    "        if \"snippets\" in user_config:\n",
    "            for snip in user_config[\"snippets\"]:\n",
    "                if not(\"wdc_library\" in snip[\"tags\"]):\n",
    "                    user_snippets.append(snip)\n",
    "                       \n",
    "        user_snippets.sort(key=extract_id)\n",
    "        \n",
    "    # merge the global list with the user list\n",
    "    config_snippets = snippets + user_snippets\n",
    "    for snip in config_snippets:\n",
    "        snip[\"id\"] = counter\n",
    "        counter+=1\n",
    "         \n",
    "    config['snippets'] = config_snippets\n",
    "    with open(config_file_path, 'w', encoding='utf-8') as f:\n",
    "        json.dump(config, f, ensure_ascii=False, indent=4)\n",
    "     \n",
    "    print(f\"Snippet library updated; {len(snippets)} global snippets and {len(user_snippets)} user snippets\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6363c94f-b47f-4d05-91f3-dbaa4d4c66ee",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "build_library()"
   ]
  }
 ],
 "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.11.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}

About

Frequently used code snippets library for extracting and manipulating WDC data

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published