简体   繁体   English

逻辑测试所有值,而不是一个python

[英]Logic tests all values rather than one python

So I have my function which essentially check if a specific filename exists in both sets of data. 因此,我有一个函数,该函数实质上检查两组数据中是否都存在特定的文件名。 If it does , then it will carry out some calculations on the filesize and output on the terminal the results. 如果这样做,则它将对文件大小进行一些计算,并在终端上输出结果。 Since I am passing one file name to test , it begins to go through each filename in my list until it finishes. 由于我要传递一个文件名来测试,所以它将开始遍历列表中的每个文件名,直到完成为止。 I just want to test it for filename 'a.json' as a test. 我只想测试文件名'a.json'作为测试。 Then i can test in isolation 'b.json' and 'c.json'. 然后,我可以隔离测试“ b.json”和“ c.json”。 The output i currently get is : 我目前得到的输出是:

a.json
()
(1000, 1000)
ok
b.json
()
(1000, 1000)
ok
c.json
()
(1000, 1000)
ok

So the pseudocode will be : 因此,伪代码为:

For a.json in file_names
if a.json exists in jsonDatacurrFile
 if  a.json exist in both jsonDataprevFile and jsonDatacurrFile
  use compare function with the filesize from jsonDatacurrFile and jsonDataprevFile for a.json and output whatever condition it meets

So an example output would be : 因此,示例输出为:

a.json - ok

The files are as follows : 这些文件如下:

jsonDataprevFile is equal to : jsonDataprevFile等于:

{"File Name": "a.json", "File Size": 1000}
{"File Name": "b.json", "File Size": 1000}
{"File Name": "c.json", "File Size": 1000}

jsonDatacurrFile

{"File Name": "a.json", "File Size": 1000}
{"File Name": "b.json", "File Size": 1000}
{"File Name": "c.json", "File Size": 1000}    

My current logic is as follows : 我当前的逻辑如下:

def compare(previous,current):
  # temporary for debug
  print()
  print(previous,current)

  tolerance = 0.4

  if previous is None and current is None:
      return "both missing"

  if previous is None:
      return "new"

  if current is None:
      return "missing"

  size_ratio = float(current)/previous

  if size_ratio >= 1 + tolerance:
      return "not ok %d%% bigger" % round(((size_ratio - 1) * 100),0)

  if size_ratio <= 1 - tolerance:
      return "not ok %d%% smaller" % round(((1 - size_ratio) * 100),0)

  return "ok"



def readFileIntoDict(pathOfFile):
  fo = open(pathOfFile, "rw+")
  linesOfFiles = fo.readlines()
  dataInFile = {}
  for line in linesOfFiles:
      jsonD = json.loads(line)
      dataInFile[jsonD['File Name']] = jsonD['File Size']
  return dataInFile

  jsonDataprevFile = readFileIntoDict('dates/2018-01-01.json')
  jsonDatacurrFile = readFileIntoDict('dates/2018-01-02.json')


file_names = ['a.json', 'b.json', 'c.json']
for fileNames in file_names:
    if fileNames in jsonDatacurrFile:
        if jsonDataprevFile[fileNames] == jsonDatacurrFile[fileNames]:
         print fileNames
         print(compare(jsonDataprevFile.get('a.json') , jsonDatacurrFile.get('a.json')))

A couple of points to help out. 有两点可以帮助您。 You are probably getting weird answers now because in this line: 您现在可能会得到奇怪的答案,因为在此行中:

if jsonDataprevFile[fileNames] == jsonDatacurrFile[fileNames]:

you are comparing the value in the dictionary (size), not the names, so you are only proceeding into the 'if' block when both are same size. 您正在比较字典中的值(大小),而不是名称中的值,因此,当两个大小相同时,您仅进入“ if”块。 Additionally, you are using the literal name 'a.json' in your call to the compare where you should be using the fileName variable. 此外,在对compare的调用中使用了文字名称“ a.json”,您应该使用fileName变量。

A cleaner way to do this if you have a particular set of names you are looking for is to use the dict.keys() method to get set of keys for each and use set intersection on them to get the common ones... 如果您要查找的是一组特定的名称,一种更清洁的方法是使用dict.keys()方法获取每个名称的键集,并在它们上使用设置交集以获取通用的键...

names_of_interest = {'a.file', 'b.file'}
names_in_both = json_file_a.keys() & json_file_b.keys()
# find names of interest that are in both files...
names = names_of_interest & names_in_both
# now you can just iterate through that set and go to work....
for name in names:
  compare(json_file_a[name], json_file_b[name])

Note that if you just want to work on all common names, you can dump the names of interest list and just work on the intersection of the key sets 请注意,如果您只想使用所有通用名称,则可以转储感兴趣列表的名称,而只使用键集的交集

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用writer.writerows(reader)在python3中一一而不是一次写入csv行 - write csv rows one by one rather than all at once in python3 using writer.writerows(reader) 如何将DataFrame中列的所有值相乘而不是仅根据位置相乘? - How to multiply all values of column in DataFrame rather than just one based on location? 使用函数而不是一个衬里 Python - Using Functions rather than one liners Python 如何让python在列表中搜索一个单词而不是列表中所有单词的文本? - How do I get python to search text for one word in a list rather than all the words in a list? Tkinter 一次显示所有页面,而不是一页一页地显示 - Tkinter shows all pages at once rather than one by one if 语句输出多个值,而不是一个并中断 - If statement outputting multiple values, rather than one and breaking 如何使用 displot 在 python 中制作 seaborn plot ,其中我们计算一个字段中的唯一值而不是总行数? - How can I make a seaborn plot in python with displot where we count unique values in one field rather than the total number of rows? Python嵌套字典更新所有字典而不是指定 - Python nested dictionary updating all dicts rather than specified Python pandas groupby 返回所有条目而不是分组条目 - Python pandas groupby returning all entries rather than grouped entries 使用值而不是索引从Python列表中选择 - Selecting from Python lists using values rather than indices
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM