{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 读取算法参数，写入redis\n",
    "\n",
    "import pandas as pd\n",
    "import time\n",
    "from sqlalchemy import create_engine\n",
    "import os\n",
    "import configparser\n",
    "import pymysql\n",
    "import traceback\n",
    "import datetime\n",
    "from LIB.BACKEND import DBManager,Log # 以相对路径的方式从LIB开始引入！！！！！\n",
    "from LIB.MIDDLE.AlgoDemo.Demo.V_1_0_0.CoreAlgo import core # 以相对路径的方式从LIB开始引入！！！！！\n",
    "from LIB.BACKEND.OPENAPI import OpenApi\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    \n",
    "    \n",
    "    # 环境变量配置（通过环境变量确定当前程序运行在开发、测试、生产环境）\n",
    "    env_dist = os.environ\n",
    "    cur_env = env_dist.get(\"CURENV\", 'dev') # 默认为开发环境\n",
    "    \n",
    "    # 读取配置文件 (该部分请不要修改)\n",
    "    cf = configparser.ConfigParser()\n",
    "    if cur_env == 'dev':\n",
    "        cf.read(\"config-dev.ini\")\n",
    "    elif cur_env == 'test':\n",
    "        cf.read(\"config-test.ini\")\n",
    "    elif cur_env == 'pro':\n",
    "        cf.read(\"config-pro.ini\")\n",
    "    \n",
    "    # 解析配置文件 （该部分请按照实际需求修改）\n",
    "    host1 = cf.get(\"Mysql-1\", 'host')\n",
    "    port1 = int(cf.get(\"Mysql-1\", 'port'))\n",
    "    db1 = cf.get(\"Mysql-1\", 'db')\n",
    "    user1 = cf.get(\"Mysql-1\", 'user')\n",
    "    password1 = cf.get(\"Mysql-1\", 'password')\n",
    "    \n",
    "    \n",
    "    # 日志配置（按照该配置，每次运行时可自动生成运行日期的文件夹, 会在与main.py同级的， 该部分可按照实际需求修改）\n",
    "    now_str = time.strftime(\"%Y-%m-%d %H:%M:%S\", time.localtime()).replace(\":\",\"_\")\n",
    "    log_path = 'log/' + now_str\n",
    "    if not os.path.exists(log_path):\n",
    "        os.makedirs(log_path)\n",
    "    log = Log.Mylog(log_name='cal_sor', log_level = 'info')\n",
    "    log.set_file_hl(file_name='{}/info.log'.format(log_path), log_level='info', size=1024* 1024 * 100)\n",
    "    log.set_file_hl(file_name='{}/error.log'.format(log_path), log_level='error', size=1024* 1024 * 100)\n",
    "    log.set_stream_hl(\"error\") # 打印错误日志\n",
    "    logger = log.get_logger()\n",
    "    logger.info(\"{}, 算法开始\".format(str(os.getpid())))\n",
    "    \n",
    "    try:    \n",
    "        # 连接数据库的两种方式\n",
    "        # 方式一：新dataframe写入数据库时，采用该方式可以不需要写sql语句；\n",
    "        #    该方式无法对数据库进行修改；\n",
    "        \n",
    "        db_engine_1 = create_engine(\n",
    "            \"mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8\".format(\n",
    "                user1, password1, host1, port1, db1\n",
    "            ))    \n",
    "    except Exception as e :\n",
    "        logger.error(traceback.format_exc)\n",
    "        logger.error(str(e))\n",
    "        logger.error(u\"任务运行错误2\\n\", exc_info=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "5ac93a79b26608c768d42fbd754dd4f69161017bfc4d38cb4b18d60e5198dbca"
  },
  "kernelspec": {
   "display_name": "Python 3.6.2 ('ana_py36')",
   "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.8.12"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
