Hi,
I am trying to make use of some code in the PythonCaller to work out the number of working days.
I found this code that had been posted.
import fme
import fmeobjects
from datetime import timedelta
from datetime import datetime
(MON, TUE, WED, THU, FRI, SAT, SUN) = range(7)
weekends=(SAT,SUN)
def networkdays(start_date, end_date, holidays=[]):
delta_days = (end_date - start_date).days + 1
full_weeks, extra_days = divmod(delta_days, 7)
# num_workdays = how many days/week you work * total # of weeks
num_workdays = (full_weeks + 1) * (7 - len(weekends))
# subtract out any working days that fall in the 'shortened week'
for d in range(1, 8 - extra_days):
if (end_date + timedelta(d)).weekday() not in weekends:
num_workdays -= 1
# skip holidays that fall on weekends
holidays = [x for x in holidays if x.weekday() not in weekends]
# subtract out any holidays
for d in holidays:
if start_date <= d <= end_date:
num_workdays -= 1
return num_workdays
def processFeature(feature):
start_date = datetime.strptime(str(feature.getAttribute("pyStartDate")), "%Y%m%d").date()
end_date = datetime.strptime(str(feature.getAttribute("pyEndDate")), "%Y%m%d").date()
holidays = [datetime.strptime(date, "%Y%m%d").date() for date in feature.getAttribute("pyHolidays").split("_")]
feature.setAttribute("py_networkdays", networkdays(start_date, end_date, holidays))
I converted this to python 3 as was unable to run as 2.7 but got this error once converted and could run the code.
PythonFactory failed to load python symbol `FeatureProcessor'
Factory proxy not initialized
PythonCaller (PythonFactory): PythonFactory failed to process feature
A fatal error has occurred. Check the logfile above for details
After searching I found when getting that error it advised replacing processFeature with FeatureProcessor
So I now run this code after the convention and FeatureProcessor change.
import fme
import fmeobjects
from datetime import timedelta
from datetime import datetime
(MON, TUE, WED, THU, FRI, SAT, SUN) = list(range(7))
weekends=(SAT,SUN)
def networkdays(start_date, end_date, holidays=[]):
delta_days = (end_date - start_date).days + 1
full_weeks, extra_days = divmod(delta_days, 7)
# num_workdays = how many days/week you work * total # of weeks
num_workdays = (full_weeks + 1) * (7 - len(weekends))
# subtract out any working days that fall in the 'shortened week'
for d in range(1, 8 - extra_days):
if (end_date + timedelta(d)).weekday() not in weekends:
num_workdays -= 1
# skip holidays that fall on weekends
holidays = [x for x in holidays if x.weekday() not in weekends]
# subtract out any holidays
for d in holidays:
if start_date <= d <= end_date:
num_workdays -= 1
return num_workdays
def FeatureProcessor(feature):
start_date = datetime.strptime(str(feature.getAttribute("pyStartDate")), "%Y%m%d").date()
end_date = datetime.strptime(str(feature.getAttribute("pyEndDate")), "%Y%m%d").date()
holidays = [datetime.strptime(date, "%Y%m%d").date() for date in feature.getAttribute("pyHolidays").split("_")]
feature.setAttribute("py_networkdays", networkdays(start_date, end_date, holidays))
I now get this error.
Python Exception <AttributeError>: 'NoneType' object has no attribute 'split'
Error encountered while calling function `FeatureProcessor'
PythonCaller (PythonFactory): PythonFactory failed to process feature
A fatal error has occurred. Check the logfile above for details
Bit out of my depth with this. Sorry posted so much giving the start to the end of what I have done in case I have messed up along the way.
Any idea on how I can fix this error or if there is a better way to do the working days calculations?
I have also tried the WorkingDaysCalculator feature I found but this doesn't allow for holiday dates and also won't allow the end date before the start (The start date for me is a target date in my case so this happens)
Thanks
Kevin