Home > Back-end >  How I can find out how many vectors are processed in Python?
How I can find out how many vectors are processed in Python?

Time:11-11

I have a very long dictionary containing more than 1 million keys.

df['file'] = files
df['features'] = df.apply(lambda row: getVector(model, row['file']), axis=1) 

Below is getVector function:

vector=model.predict(inp.reshape(1, 100, 100, 1))
print(file  " is added.")
return vector

But, it shows blahblah.jpg is added but it has no use as I do not know how many files have been processed. My question is that how I get the count of files that has been processed?

For example,

1200 out of 1,000,000 is processed.

or even just

1200

I do not want to have a neat and clean output. I just want to know how many files have been processed.

Any idea would be appreciated.

CodePudding user response:

How about creating a column in the initial dataframe with incremental numbers? You would use this column as an input of your getVector function and display the value. Something like this:

df['features'].apply(lambda row: getVector(model, row['file'], row['count']), axis=1)

CodePudding user response:

You can use a decorator to count it:

import functools
def count(func):
    @functools.wraps(func)
    def wrappercount(*args):
        func(*args)
        func.counter =1
        print(func.counter,"of 1,000,000 is processed.")
    func.counter=0
    return wrappercount

@count
def getVector(*args):
    #write code and add arguments

Learn more about decorators here.

If you're having a class of which you want to count a method's usage, you can use the following:

class getVector:
     counter=0
     def __init__(self):#add your arguments here
         #write code here
         getVector.counter  =1
         print(getVector.counter,"of 1,000,000 is processed.")
  • Related