2017-02-23 93 views
1

我试图导入一个名为从名为data_processed.py到同一目录anothe文件名为failed_hmac.py做一些百分比计算,但后一个Python文件log_entry_counts变量导入,我无法访问导入模块(文件)内的变量。这是第一个文件名data_processed.py。 进口OS 从日期时间日期时间进口,timedelta 从收藏导入柜台Python的导入文件模块到另一个

def dataCount(folderName): 
    #count = 0 
    log_entry_counts = Counter() 
    today_date = datetime.today() 
    date_ranges = [ 
        ('30 Days', today_date - timedelta(days=30)), 
        # ('3 months', today-date - timedelta(days=90)), 
        #('year', today-date - timedelta(days=365)) 
        ] 
    for path, dirs, files in os.walk(folderName): 
     for dirname in dirs: 
      log_date = (os.path.join(path, dirname)) 

     for items in files: 
      if items != ".DS_Store": 
       try: 
        log_date = datetime.strptime(path[39:47], '%m%d%Y') 
        for text, dr in date_ranges: 
         if log_date >= dr: 
          log_entry_counts[text] += 1 
       except ValueError: 
        print 'This line has a problem:', log_date 

    total = 0 

    print log_entry_counts['30 Days'] 

def main(): 

    filePath = 'file.txt' 

    hmacCount(filePath) 

if __name__ == "__main__": 

    main() 

它通过循环的文件夹并计算所有子文件夹内的文件。另一个文件名failed_hmac.py如下

import os, sys 
from datetime import datetime, timedelta 
from collections import Counter 
import data_processed 

def hmacCount(fileName): 
    # Get the last failed hmac date 
    fileHandle = open('file.txt',"r") 
    lineList = fileHandle.readlines() 
    fileHandle.close() 
    lastLine = lineList[-1] 
    lastDate = datetime.strptime(lastLine[:10], '%m/%d/%Y') 

    with open(fileName) as f_input: 

     logEntryCounts = Counter() 

     #today_date = datetime.today() - timedelta(days=14) 
       #print today_date 
     dateRanges = [ 
        ('30 Days', lastDate - timedelta(days=30)), 
        #('3 months', lastDate - timedelta(days=90)), 
        #('One year', lastDate - timedelta(days=330)) 
        ] 

     for line in f_input: 

      #Stop Processing if there are no more lines 
      if not line: 

       break 

      if "Following hmac" in line: 

       try: 
        logDate = datetime.strptime(line[:10], '%m/%d/%Y') 

        for text, dr in dateRanges: 

         if logDate >= dr: 

          logEntryCounts[text] += 1 

       except ValueError: 

        print 'This line has a problem:', logDate 

    total = 0 

    hmacData = float(logEntryCounts['30 Days']) 

    print logEntryCounts['30 Days'] 

# Call The function 
def main(): 

    filePath = 'file.txt' 

    hmacCount(filePath) 

if __name__ == "__main__": 

    main() 

的目标是导入data_processed.pyfailed_hmac.py和使用变量logEntryCountslog_entry_counts执行一些百分比计算,但我一直得到logEntryCounts没有定义错误

回答

0

data_entry_logs是可变insid e在data_processed.py中的功能。您将不得不修改您的代码以调用函数data_processed.dataCount()并返回该值,以便您可以使用该变量。

data_processed.py

def dataCount(folderName): 
    #your code 
    return log_entry_counts 

failed_hmac.py

更换

hmacData = float(logEntryCounts['30 Days']) 

hmacData = float(data_processed.dataCount(...)['30 Days']) 

另一种方法,我认为更简洁的方法是将这两个函数声明为一个类,如this question.

相关问题