{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 产生目录"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import os.path"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "文件夹名:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "folders = ['01-python-tools', \n",
    "           '02-python-essentials',\n",
    "           '03-numpy',\n",
    "           '04-scipy',\n",
    "           '05-advanced-python',\n",
    "           '06-matplotlib',\n",
    "           '07-interfacing-with-other-languages',\n",
    "           '08-object-oriented-programming',\n",
    "           '09-theano',\n",
    "           '10-something-interesting',\n",
    "           '11-useful-tools',\n",
    "           '12-pandas'\n",
    "          ]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "对应的中文名:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "chinese = ['01. **Python 工具**', \n",
    "           '02. **Python 基础**', \n",
    "           '03. **Numpy**',\n",
    "           '04. **Scipy**',\n",
    "           '05. **Python 进阶**',\n",
    "           '06. **Matplotlib**',\n",
    "           '07. **使用其他语言进行扩展**',\n",
    "           '08. **面向对象编程**',\n",
    "           '09. **Theano 基础**',\n",
    "           '10. **有趣的第三方模块**',\n",
    "           '11. **有用的工具**',\n",
    "           '12. **Pandas**'\n",
    "          ]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "一一对应到字典中:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "folders_to_chinese = dict(zip(folders, chinese))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "产生目录文件:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "index_file = open('index.md', 'w')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "遍历产生中文目录:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "01. Python 工具\n",
      "    01.01 Python 简介\n",
      "    01.02 Ipython 解释器\n",
      "    01.03 Ipython notebook\n",
      "    01.04 使用 Anaconda\n",
      "02. Python 基础\n",
      "    02.01 Python 入门演示\n",
      "    02.02 Python 数据类型\n",
      "    02.03 数字\n",
      "    02.04 字符串\n",
      "    02.05 索引和分片\n",
      "    02.06 列表\n",
      "    02.07 可变和不可变类型\n",
      "    02.08 元组\n",
      "    02.09 列表与元组的速度比较\n",
      "    02.10 字典\n",
      "    02.11 集合\n",
      "    02.12 不可变集合\n",
      "    02.13 Python 赋值机制\n",
      "    02.14 判断语句\n",
      "    02.15 循环\n",
      "    02.16 列表推导式\n",
      "    02.17 函数\n",
      "    02.18 模块和包\n",
      "    02.19 异常\n",
      "    02.20 警告\n",
      "    02.21 文件读写\n",
      "03. Numpy\n",
      "    03.01 Numpy 简介\n",
      "    03.02 Matplotlib 基础\n",
      "    03.03 Numpy 数组及其索引\n",
      "    03.04 数组类型\n",
      "    03.05 数组方法\n",
      "    03.06 数组排序\n",
      "    03.07 数组形状\n",
      "    03.08 对角线\n",
      "    03.09 数组与字符串的转换\n",
      "    03.10 数组属性方法总结\n",
      "    03.11 生成数组的函数\n",
      "    03.12 矩阵\n",
      "    03.13 一般函数\n",
      "    03.14 向量化函数\n",
      "    03.15 二元运算\n",
      "    03.16 ufunc 对象\n",
      "    03.17 choose 函数实现条件筛选\n",
      "    03.18 数组广播机制\n",
      "    03.19 数组读写\n",
      "    03.20 结构化数组\n",
      "    03.21 记录数组\n",
      "    03.22 内存映射\n",
      "    03.23 从 Matlab 到 Numpy\n",
      "04. Scipy\n",
      "    04.01 SCIentific PYthon 简介\n",
      "    04.02 插值\n",
      "    04.03 概率统计方法\n",
      "    04.04 曲线拟合\n",
      "    04.05 最小化函数\n",
      "    04.06 积分\n",
      "    04.07 解微分方程\n",
      "    04.08 稀疏矩阵\n",
      "    04.09 线性代数\n",
      "    04.10 稀疏矩阵的线性代数\n",
      "05. Python 进阶\n",
      "    05.01 sys 模块简介\n",
      "    05.02 与操作系统进行交互:os 模块\n",
      "    05.03 CSV 文件和 csv 模块\n",
      "    05.04 正则表达式和 re 模块\n",
      "    05.05 datetime 模块\n",
      "    05.06 SQL 数据库\n",
      "    05.07 对象关系映射\n",
      "    05.08 函数进阶:参数传递,高阶函数,lambda 匿名函数,global 变量,递归\n",
      "    05.09 迭代器\n",
      "    05.10 生成器\n",
      "    05.11 with 语句和上下文管理器\n",
      "    05.12 修饰符\n",
      "    05.13 修饰符的使用\n",
      "    05.14 operator, functools, itertools, toolz, fn, funcy 模块\n",
      "    05.15 作用域\n",
      "    05.16 动态编译\n",
      "06. Matplotlib\n",
      "    06.01 Pyplot 教程\n",
      "    06.02 使用 style 来配置 pyplot 风格\n",
      "    06.03 处理文本(基础)\n",
      "    06.04 处理文本(数学表达式)\n",
      "    06.05 图像基础\n",
      "    06.06 注释\n",
      "    06.07 标签\n",
      "    06.08 figures, subplots, axes 和 ticks 对象\n",
      "    06.09 不要迷信默认设置\n",
      "    06.10 各种绘图实例\n",
      "07. 使用其他语言进行扩展\n",
      "    07.01 简介\n",
      "    07.02 Python 扩展模块\n",
      "    07.03 Cython:Cython 基础,将源代码转换成扩展模块\n",
      "    07.04 Cython:Cython 语法,调用其他C库\n",
      "    07.05 Cython:class 和 cdef class,使用 C++\n",
      "    07.06 Cython:Typed memoryviews\n",
      "    07.07 生成编译注释\n",
      "    07.08 ctypes\n",
      "08. 面向对象编程\n",
      "    08.01 简介\n",
      "    08.02 使用 OOP 对森林火灾建模\n",
      "    08.03 什么是对象?\n",
      "    08.04 定义 class\n",
      "    08.05 特殊方法\n",
      "    08.06 属性\n",
      "    08.07 森林火灾模拟\n",
      "    08.08 继承\n",
      "    08.09 super() 函数\n",
      "    08.10 重定义森林火灾模拟\n",
      "    08.11 接口\n",
      "    08.12 共有,私有和特殊方法和属性\n",
      "    08.13 多重继承\n",
      "09. Theano 基础\n",
      "    09.01 Theano 简介及其安装\n",
      "    09.02 Theano 基础\n",
      "    09.03 Theano 在 Windows 上的配置\n",
      "    09.04 Theano 符号图结构\n",
      "    09.05 Theano 配置和编译模式\n",
      "    09.06 Theano 条件语句\n",
      "    09.07 Theano 循环:scan(详解)\n",
      "    09.08 Theano 实例:线性回归\n",
      "    09.09 Theano 实例:Logistic 回归\n",
      "    09.10 Theano 实例:Softmax 回归\n",
      "    09.11 Theano 实例:人工神经网络\n",
      "    09.12 Theano 随机数流变量\n",
      "    09.13 Theano 实例:更复杂的网络\n",
      "    09.14 Theano 实例:卷积神经网络\n",
      "    09.15 Theano tensor 模块:基础\n",
      "    09.16 Theano tensor 模块:索引\n",
      "    09.17 Theano tensor 模块:操作符和逐元素操作\n",
      "    09.18 Theano tensor 模块:nnet 子模块\n",
      "    09.19 Theano tensor 模块:conv 子模块\n",
      "10. 有趣的第三方模块\n",
      "    10.01 使用 basemap 画地图\n",
      "    10.02 使用 cartopy 画地图\n",
      "    10.03 探索 NBA 数据\n",
      "    10.04 金庸的武侠世界\n",
      "11. 有用的工具\n",
      "    11.01 pprint 模块:打印 Python 对象\n",
      "    11.02 pickle, cPickle 模块:序列化 Python 对象\n",
      "    11.03 json 模块:处理 JSON 数据\n",
      "    11.04 glob 模块:文件模式匹配\n",
      "    11.05 shutil 模块:高级文件操作\n",
      "    11.06 gzip, zipfile, tarfile 模块:处理压缩文件\n",
      "    11.07 logging 模块:记录日志\n",
      "    11.08 string 模块:字符串处理\n",
      "    11.09 collections 模块:更多数据结构\n",
      "    11.10 requests 模块:HTTP for Human\n",
      "12. Pandas\n",
      "    12.01 十分钟上手 Pandas\n",
      "    12.02 一维数据结构:Series\n",
      "    12.03 二维数据结构:DataFrame\n"
     ]
    }
   ],
   "source": [
    "import nbformat\n",
    "import glob\n",
    "\n",
    "for folder in folders:\n",
    "    index_file.write('- [' + folders_to_chinese[folder] \n",
    "                     +'](' + folder + ')\\n')\n",
    "    print folders_to_chinese[folder].replace('*', '')\n",
    "    files = sorted(os.listdir(folder))\n",
    "    for file_name in files:\n",
    "        if file_name.endswith('.ipynb'):\n",
    "            with open(os.path.join(folder, file_name)) as fp:\n",
    "                nb = nbformat.read(fp, nbformat.NO_CONVERT)\n",
    "            name = nb['cells'][0]['source'][1:].strip()\n",
    "            print '    ' + file_name[:5], name\n",
    "            index_file.write('\\t - [' + file_name[:5] + ' ')\n",
    "            index_file.write(name.encode('utf-8'))\n",
    "            index_file.write('](' + folder + '/' + file_name +')\\n')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "关闭目录文件:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "index_file.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}