0

I am trying to run this code:

from crontab import CronTab
import pandas as pd
cron = CronTab(user='bla')

df = pd.read_csv('dummy.txt')

def func_assign_job(row):
    '''A function executed on each row of the df dataframe.
    I want for each row the job string to be unique'''    
    'job_'+str(row.name)+str(row.time) = cron.new(command='python crontab_example1.py')
    'job_'+str(row.name)+str(row.time).setall('55 16 * * *')

df.apply(func_assign_job, axis=1)

At the line

'job_'+str(row.name)+str(row.time) = cron.new(command='python crontab_example1.py')

I get the error-message "SyntaxError: can't assign to operator". I understand what's the problem - I am trying to assign something to a string I am creating in-place at the same time. I do this, because I need to have the actions in the function assigned to different strings, depending on the input of the function, e.g. assigned to job_John_08:00, job_Mila_11:00 a.s.o. For example, this code would work:

job_name = cron.new(command='python crontab_example1.py')
job_name.setall('55 16 * * *')

but I don't want to use job_name, because I want this string to be different for each row.

So, how can I achieve this goal of mine? What workarounds are there? tnx

NeStack
  • 1,739
  • 1
  • 20
  • 40

1 Answers1

1

Maybe you can use dict to manage your functions with custom names? Something like that.

from crontab import CronTab
import pandas as pd
cron = CronTab(user='bla')

df = pd.read_csv('dummy.txt')

tasks={}


def func_assign_job(row):
    '''A function executed on each row of the df dataframe.
    I want for each row the job string to be unique'''    
    tasks['job_{0}{1}'.format(str(row.name),str(row.time))] = cron.new(command='python crontab_example1.py')

df.apply(func_assign_job, axis=1)
Michał M
  • 61
  • 6