text: {
   "cells": [
    {
     "cell_type": "code",
     "execution_count": 1,
     "metadata": {},
     "outputs": [],
     "source": [
      "class salad():\n",
      "\n",
      "    import glob\n",
      "    import numpy\n",
      "    import pandas\n",
      "    import os\n",
      "    import re\n",
      "    \n",
      "    # Initialize class\n",
      "    def __init__(self):\n",
      "        self.path = ''\n",
      "        self.salad_items = []\n",
      "        self.n_items = []\n",
      "    \n",
      "    # Define function to write files\n",
      "    # Set path to read and write files\n",
      "    def write(self, path, salad_items, n_items):\n",
      "        self.path = path\n",
      "        print(self.path)\n",
      "\n",
      "        # Check whether both lists are of the same length, otherwise return an error\n",
      "        assert len(salad_items) == len(n_items), 'Not matching!'\n",
      "        \n",
      "        # Make a new folder only if it doesn't already exist in that path\n",
      "        os.makedirs(self.path, exist_ok = True)\n",
      "        \n",
      "        # Write files\n",
      "        # Go ingredient by ingredient\n",
      "        for k in range(len(salad_items)):\n",
      "            print(salad_items[k],n_items[k])\n",
      "            \n",
      "            # Go amount by amount\n",
      "            for j in range(n_items[k]):\n",
      "                # Create file with specific format\n",
      "                file_name = salad_items[k] + '{:0>2}'.format(j) + '.salad'\n",
      "                # Write file\n",
      "                f = open(os.path.join(self.path, file_name), \"w+\")\n",
      "                f.close()\n",
      "        return\n",
      "\n",
      "    # Define function to read files\n",
      "    def read(self, path):\n",
      "        \n",
      "        # Create a list with all file names ending with .salad\n",
      "        flist = glob.glob(os.path.join(path, '*.salad'))\n",
      "        \n",
      "        # Initialize empty result lists\n",
      "        ingredient = []\n",
      "        ingredient_n = []\n",
      "        \n",
      "        # Go file by file, parsing ingredient and file number\n",
      "        for file in flist:\n",
      "            \n",
      "            # Create regex of useful parts of the string\n",
      "            pattern = r\"(\\w*)(\\d\\d)\"\n",
      "            \n",
      "            # Search file name\n",
      "            x = re.search(pattern, file)\n",
      "            \n",
      "            # Parse file name and add to corresponding result list\n",
      "            ingredient.append(x.group(1))\n",
      "            ingredient_number.append(x.group(2))\n",
      "            \n",
      "            # Create dictionary\n",
      "            res_zip = zip(ingredient, ingredient_number)\n",
      "            res_dict = {}\n",
      "            for k,v in res_dict:\n",
      "                res_dict.setdefault(k, []).append(v)\n",
      "\n",
      "        return res_dict"
     ]
    }
   ],
   "metadata": {
    "kernelspec": {
     "display_name": "Python 3.7.0 64-bit (conda)",
     "language": "python",
     "name": "python37064bitcondad190390a1b0247f1982120b783568fbd"
    },
    "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.7.0"
    }
   },
   "nbformat": 4,
   "nbformat_minor": 2
  }
  