csv

csv_sample.py

#! /usr/bin/env python
"""{fname}
# sample code for csv
Usage:
"""

__author__ = 'Memoru'
__version__ = '0.0.1'
__date__ = '2018-06-26'

import sys
import os
__doc__ = __doc__.format(fname=os.path.basename(__file__))

import csv
from pathlib import Path

import logging.config
import configbase as base
CONF = base.CONF
ENV = base.ENV



#  Debug ---------------------------------------  #

def process(args={'csv_file' : 'sample.csv'}):
    try:
        logger = logging.getLogger(__name__)
        logger.debug('◇◇--Begin sample -----------------◇◇')
        logger.debug(__doc__)
        logger.debug(f'CONF.MEDIA_ROOT={CONF.MEDIA_ROOT}')
        csv_root = CONF.MEDIA_ROOT
        p = Path(csv_root)
        csvf = args['csv_file']
        built_p = p / csvf
        logger.debug(built_p)
        
        if built_p.exists() :
            with open(built_p, 'r', encoding='shift_jis') as f:
                reader = csv.reader(f)
                head = next(reader)
                logger.debug(head)
                if(len(head)==2):
                    pass
                else:
                    raise ValueError('header error')
            
        
        logger.debug('◆◆--End   sample -----------------◆◆')
    except Exception as ex:
        logger.exception(ex)
        raise ex
        

if __name__ == '__main__':
    logging.config.fileConfig('logging_debug.conf')
    #process()
    args = {'csv_file' : 'sample2.csv'}
    process(args)

configbase.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-

"""
Base Config setting
 - 共通のconfig定数をconfigs.default.pyに設定する。
   CONFに読み込まれる
 - マシン環境(本番、デバック, etc)によって異なる定数の場合
   configs.config_<env.EXECUTION_MODE>.pyに、値を設定する。
   ENVに読み込まれる。
"""
__version__ = '0.0.2'

import importlib
from os import environ

# module statement ------------------------------
import configs.default as CONF  ## Common Config

class Config():
    """ configs配下のconfig_xxx.pyよりConfigを読み込むClass"""
    def __init__(self,env=None):
        if env is None:
            if environ.get("EXECUTION_MODE") is None:
                suffix = ""
            else:
                suffix = "_" + environ.get("EXECUTION_MODE")
        else:
            suffix = "_" + env

        self.ENV = _get_conf('configs.config' + suffix) ## Environment Config
        self.LOG_CONF = 'logging' + suffix +".conf"     ## Logging Config

def _get_conf(config_name):
    conf = importlib.import_module(config_name)
    return conf

ENV = Config().ENV
LOG_CONF = Config().LOG_CONF


###### debug ---------------------------------###
# configs/config_debug.pyのMODULE_FILEを確認
def process(env):
    print(" 1  : get config from {}".format(Config(env).ENV.MODULE_FILE))
    print(" 11 : get config from {}".format(Config(env).LOG_CONF))
    print(" 2  : get config from {}".format(Config().ENV.MODULE_FILE))
    print(" 22 : get config from {}".format(Config().LOG_CONF))
    print(" 3  : get config from '{}' in default".format(CONF.PROJECT_NAME))

if __name__ == '__main__' :
    process("debug")

logging_debug.conf

[loggers]
keys=root,functions

[handlers]
keys=fileHandler,stderrHandler

[formatters]
keys=simpleFormatter

[logger_root]
level=DEBUG
handlers=fileHandler,stderrHandler

[logger_functions]
level=INFO
handlers=fileHandler,stderrHandler
qualname=functions
propagate=0

[handler_fileHandler]
class=handlers.TimedRotatingFileHandler
formatter=simpleFormatter
args=('apps.log','D')

[handler_stderrHandler]
class=StreamHandler
formatter=simpleFormatter
args=(sys.stderr,)

[formatter_simpleFormatter]
format=[%(asctime)s][%(levelname)s](%(filename)s:%(lineno)s) %(message)s
datefmt=%Y/%m/%d %H:%M:%S