-4

Can anyone help me with the following problem? I have a previous function that returns a dictionary. Now I need this function to make a new one, but I need to do something with the dictionary. To show you what I'm trying to do, dictionary(fasta_filename) is my previous function;

def test(fasta_filename, minMW, maxMW):
    interval = minMW, maxMW
    for key, value in dictionary(fasta_filename).items():
        if value in interval:
            print(key)
        else:
            print('intervals do not match')

This is just starting to try what I want to do, but the main problem I have is that the things you can do with a dictionary, like the .items(), are not working if you try to do it on the function. The error says: 'NoneType' object has no attribute 'items'. I understand this error, because when you do function.items() you assume the function IS the dictionary, but it's not. I'm still very new to all this, perhaps you can tell, but I would appreciate it if someone could give me a hint. :)

Georgy
  • 9,972
  • 7
  • 57
  • 66
  • There are some problems here: (a) by `value in interval`, you do not check a value against an interval, but check if a value is either `minMW` or `maxMW`. Furthermore this function does *not* provide output, it `print`s things, which is I/O. You `return` things with the `return` keyword. – Willem Van Onsem Dec 30 '17 at 21:17
  • Your `dictionary` function is returning `None` at least some times, in which case `dictionary(fasta_filename).items()` will not work, but it *would* work if your function in fact returned a `dict` object. Without seeing the `dictionary` function, I can only speculate. – juanpa.arrivillaga Dec 30 '17 at 21:19
  • Note, you aren't doing `function.items()`, which would be `dictionary.items()`, that error would be slightly different, it would say `AttributeError: 'function' object has no attribute 'items'`, but in your case, it is `"'NoneType' object has no attribute 'items'"`. Since you correctly *call* the function, i.e. `dictionary(...)` – juanpa.arrivillaga Dec 30 '17 at 21:23

1 Answers1

0

You could fix your testing like this, but it still will only print stuff out.

Modify the return statement of the def dictionary(fn) function to check the different outputs for a None-dict and a dict that contains data and is checked.

def dictionary(fn):
    # you do stuff here with your file, make sure to NOT return None but 
    # always retunr at least an empty dict if your actions on your file 
    # do not result in a filled dictionary
    return {"dummy1" : 55, "dummy2" : 97, "dummy3" : 12345}
    # return None # uncomment this and comment the one above for a None-Dict

def test(fasta_filename, minMW, maxMW):
    interval = range(minMW, maxMW+1) # this is the interval [minMW..maxMW] including both
    dic = dictionary(fasta_filename)
    if not dic:
        print('dictionary is None')
        return None # leave test method

    for key, value in dic.items():
        if value in interval:
            print(key)
        else:
            print('intervals do not match for', key)

test("does not matter, dummy data provided internally",60,120)

Output (python 3.6):

intervals do not match for dummy1
dummy2
intervals do not match for dummy3

or

dictionary is None

depending on what def dictionary(fn) returns.

Patrick Artner
  • 48,339
  • 8
  • 43
  • 63
  • Thank you, I understand the difference between return and print better now! I how have a function, and it works, but only for the not matching intervals. I thought it was because the output of the dictionary, which is like this: {'seq_7009': (6236.9764, 6367.049999999999), 'seq_418': (3716.3642000000004, 3796.4124000000006), ...)} did not work for the interval because of the floating point numbers. I tried adding a step in the range like 'interval = range(minMW, maxMW+1, 0.00000000001)' but then it says: 'TypeError: 'float' object cannot be interpreted as an integer'. – Jozefien Demuynck Jan 02 '18 at 12:50