将pandas read_csv调用传递给具有其命名参数的另一个函数

时间:2019-06-24 10:19:38

标签: python

由于函数在Python中是first-class citizens,所以我应该能够重构它:

<script type="text/javascript">
var track_page = 1; 
var loading  = false;
var idarticle = <?php echo $content['idartikel']?>;
var idkanal = <?php echo $content['idkanal']?>; 

// create html element to hold the idarticle and idkanal values
$('#more_article').append('<input type="hidden" id="idarticle" value="'+idarticle+'" /><input type="hidden" id="idkanal" value="'+idkanal+'" />');    

$(window).scroll(function() {
    if($(window).scrollTop() == $(document).height() - $(window).height()) {
        track_page++;
        idarticle = $('#idarticle').val();
        idkanal = $('#idkanal').val();
        load_contents(track_page,idarticle,idkanal);
    }
});

function load_contents(track_page,idarticle,idkanal){
if(loading == false){
    loading = true;  
    $('.loading-info').show();  
    $.post( "http://localhost/staging-casa/article/more/",
    {page:track_page,idkanal:idkanal,idartikel:idarticle},
    function(data){
        loading = false;
        if(data.article_view.trim().length == 0 || track_page >= 10){
        $('.loading-info').html("");
        return;
    }
    $('.loading-info').hide(); 
    $("#more_article").append(data.article_view);
    $('#idarticle').val(data.article_data.idartikel);
    $('#idkanal').val(data.article_data.idkanal);
    //var url = 'https://' + window.location.hostname + ;
    //window.history.pushState("object or string", "Title", url); 

    }).fail(function(xhr, ajaxOptions, thrownError) { 
    alert(thrownError); 
    })
}
}
</script>

对于这样的事情:

def get_events():
    csv_path = os.path.join(INPUT_CSV_PATH, DATA_NAME + '.csv')
    print(f'Starting reading events at {datetime.now()}')
    start_time = datetime.now()
    events = pd.read_csv(csv_path, dtype=DTYPES)
    end_time = datetime.now()
    print(f'Finished reading events at {end_time} ({end_time - start_time})')
    return events

即将pandas read_csv函数及其命名的参数传递给一个辅助函数。 (注:我不确定在传递函数时如何传递命名参数,this answer帮助。)

但是重构后,出现以下错误:

  

ValueError:无效的文件路径或缓冲区对象类型:

关于如何将函数及其命名参数传递给另一个Python函数进行评估,我缺少什么?

1 个答案:

答案 0 :(得分:2)

您可能希望将其重构为:

def get_events():
    csv_path = os.path.join(INPUT_CSV_PATH, DATA_NAME + '.csv')
    events = _time_function_call('reading events', pd.read_csv, filepath_or_buffer=csv_path, dtype=DTYPES)
    return events

def _time_function_call(message, func, *args, **kwds):
    start_time = datetime.now()
    print(f'Starting {message} at {start_time}')
    result = func(*args, **kwds)
    end_time = datetime.now()
    duration = end_time - start_time
    print(f'Finished {message} at {end_time} ({duration})')
    return result

那样,Python可以处理handling arbitrary argument lists

我建议使用context managerslogging module,因为这样的代码可以很容易地组成,例如:

from time import perf_counter
import logging

logger = logging.getLogger(__name__)

class log_timer:
    def __init__(self, message):
        self.message = message

    def __enter__(self):
        logger.info(f"{self.message} started")
        # call to perf_counter() should be the last statement in method
        self.start_time = perf_counter()

    def __exit__(self, exc_type, exc_value, traceback):
        # perf_counter() call should be first statement
        secs = perf_counter() - self.start_time
        state = 'finished' if exc_value is None else 'failed'
        logger.info(f"{self.message} {state} after {secs * 1000:.2f}ms")

可以这样使用:

from time import sleep

logging.basicConfig(
    format='%(asctime)s %(levelname)s %(message)s',
    level=0,
)

with log_timer("sleep"):
    sleep(1)

那样,您不必担心将任意代码放入函数中以及它们之间的线程状态。

另外,由于您以前使用datetime不太适合测量少量代码的运行时间,因此time模块提供了perf_counter并将其移交给更合适的OS / CPU (高分辨率)计时器。