Loop or Parse Tree of Requirments

I have a question on stepping through a loop or a treeof requirements. Currently I have 3 lists built that Ineed to get info from:

scriptsThatShouldRun []

# list built from a DBscriptsThatDidRun []

# every minute of that particular dayminutesList []

This next part is where I need some help because it getsa little tricky.

I have to check each element of the "scriptsThatShouldRun"list against the "minutesList" list and break down each bit of info to compare in that list: day of week, month,day of month, hour and minute.

If the minutesList shows: Mon Aug 31 00:00, then I need tosee if the scriptsThatShouldRun matches this exactly so Icheck: the day of week, month, date, hour and minute

If it does MATCH, then I will then compare against the listscriptsThatDidRun and look if it has the same value, if it does append to a success list or if its not in there append to a failure list.

If it does NOT MATCH, then I ignore it and move onto the nextelement of the list because that item "scriptsThatShouldRun"is NOT eligible to run @ that time of day, so nothing shouldhappen.

In basic words:

The whole point is to check the scriptsThatShouldRun list against the scriptsThatDidRun all the while looping throughthe minutesList list (every minute of the day). The basicmeaning of this is to go minute by minute and check if aa script is eligible to run, if it is, compare and see ifit really did run and report on that.

Basically, if the time in minutesList matches the time inscriptsThatShouldRun, I need to check the scriptsThatDidRunand append to success or failure.

I hope this all makes sense? Any code examples or pointersyou may have would be very helpful.

Thanks in advance!

Comments

: : I have a question on stepping through a loop or a tree: of requirements. Currently I have 3 lists built that I: need to get info from:: : scriptsThatShouldRun [] : : # list built from a DB: scriptsThatDidRun []: : # every minute of that particular day: minutesList []: : : This next part is where I need some help because it gets: a little tricky.: : I have to check each element of the "scriptsThatShouldRun": list against the "minutesList" list and break down each : bit of info to compare in that list: day of week, month,: day of month, hour and minute.: : If the minutesList shows: Mon Aug 31 00:00, then I need to: see if the scriptsThatShouldRun matches this exactly so I: check: the day of week, month, date, hour and minute: : If it does MATCH, then I will then compare against the list: scriptsThatDidRun and look if it has the same value, if it : does append to a success list or if its not in there append : to a failure list.: : If it does NOT MATCH, then I ignore it and move onto the next: element of the list because that item "scriptsThatShouldRun": is NOT eligible to run @ that time of day, so nothing should: happen.: : In basic words:: : The whole point is to check the scriptsThatShouldRun list : against the scriptsThatDidRun all the while looping through: the minutesList list (every minute of the day). The basic: meaning of this is to go minute by minute and check if a: a script is eligible to run, if it is, compare and see if: it really did run and report on that.: : Basically, if the time in minutesList matches the time in: scriptsThatShouldRun, I need to check the scriptsThatDidRun: and append to success or failure.: : I hope this all makes sense? Any code examples or pointers: you may have would be very helpful.

Ok, but I still think you're doing way too much work with the list of minutes.

[code]for minute in minutesList: for script in scriptsThatShouldRun: if script.startswith(minute): if script in scriptsThatDidRun: successes.append(script) else: failures.append(script)[/code]

This is the most simplified version I could think of. The startswith() method of strings looks to see if the string starts with the exact string passed to it. This will of course only work if the date/time format is exactly the same for both lists.

I will try and implement your code snippet and see how it goes.I think I may need to do an equal's as well as a startswith() because the server may start the application a minute after for beforethe scriptsThatShouldRun list says. Do you see a different way of writing my script without each loop?

Thanks for all your help.

: : I have a question on stepping through a loop or a tree: : of requirements. Currently I have 3 lists built that I: : need to get info from:: : : : scriptsThatShouldRun [] : : : : # list built from a DB: : scriptsThatDidRun []: : : : # every minute of that particular day: : minutesList []: : : : : : This next part is where I need some help because it gets: : a little tricky.: : : : I have to check each element of the "scriptsThatShouldRun": : list against the "minutesList" list and break down each : : bit of info to compare in that list: day of week, month,: : day of month, hour and minute.: : : : If the minutesList shows: Mon Aug 31 00:00, then I need to: : see if the scriptsThatShouldRun matches this exactly so I: : check: the day of week, month, date, hour and minute: : : : If it does MATCH, then I will then compare against the list: : scriptsThatDidRun and look if it has the same value, if it : : does append to a success list or if its not in there append : : to a failure list.: : : : If it does NOT MATCH, then I ignore it and move onto the next: : element of the list because that item "scriptsThatShouldRun": : is NOT eligible to run @ that time of day, so nothing should: : happen.: : : : In basic words:: : : : The whole point is to check the scriptsThatShouldRun list : : against the scriptsThatDidRun all the while looping through: : the minutesList list (every minute of the day). The basic: : meaning of this is to go minute by minute and check if a: : a script is eligible to run, if it is, compare and see if: : it really did run and report on that.: : : : Basically, if the time in minutesList matches the time in: : scriptsThatShouldRun, I need to check the scriptsThatDidRun: : and append to success or failure.: : : : I hope this all makes sense? Any code examples or pointers: : you may have would be very helpful.: : Ok, but I still think you're doing way too much work with the list of minutes.: : [code]: for minute in minutesList:: for script in scriptsThatShouldRun:: if script.startswith(minute):: if script in scriptsThatDidRun:: successes.append(script): else:: failures.append(script): [/code]: : This is the most simplified version I could think of. The startswith() method of strings looks to see if the string starts with the exact string passed to it. This will of course only work if the date/time format is exactly the same for both lists.: : : [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]: : [code]: $ select * from users where clue > 0: no rows returned: [/code]

: I will try and implement your code snippet and see how it goes.: I think I may need to do an equal's as well as a startswith() : because the server may start the application a minute after for : beforethe scriptsThatShouldRun list says. Do you see a different : way of writing my script without each loop?

Well now you've introduced a whole new variable into the equation. If there's a tolerance of a minute or two in either direction then you'll need to convert the timestamps into numbers that you can compare. If you allow that, what if you a job that should run once and then again two minutes later. And what if the server only ran that job once halfway between the two times it should've? Then you've got one "did run" item that is a minute away from two different "should run" items. Which one does it match?

I will have to build my code to allow a time tolerance. I am guesing on a 2-4 minute range. I ran through the scriptsThatShouldRun [] listand there is nothing that will overlap, everything is pretty spread out through the day.

As you mentioned, I will not be able to use the startswith() methodand will have to figure out way around this. I am working on some codelike this:[CODE]# loop each thru each minutefor minute in minutesList:if day of week in scriptsThatShouldRun == day of week in minutesListif month in scriptsThatShouldRun == month in minutesListif day of month in scriptsThatShouldRun == day of month in minutesListif hour in scriptsThatShouldRun == hour in minutesList if minute in scriptsThatShouldRun == minute in minutesList

# its eligible to run based on the same time stamp, check if it did:

for x in scriptsThatShouldRunif x not in scriptsThatDidRun:shouldHaveRan.append(x)elsedidntRun.append(x)

# need to build in a 2-4 minute tolerance either way on the time[/CODE]

I am really not sure if this is the right way to start going through each requirement or not. I just know I have to meet each requirement, then check if it ran or not, and append to a list. I know this may sound like a long way of doing it, but I am really new to the python world.

Any help or ideas or code would be very appreciated.

Thanks

: : I will try and implement your code snippet and see how it goes.: : I think I may need to do an equal's as well as a startswith() : : because the server may start the application a minute after for : : beforethe scriptsThatShouldRun list says. Do you see a different : : way of writing my script without each loop?: : Well now you've introduced a whole new variable into the equation. If there's a tolerance of a minute or two in either direction then you'll need to convert the timestamps into numbers that you can compare. If you allow that, what if you a job that should run once and then again two minutes later. And what if the server only ran that job once halfway between the two times it should've? Then you've got one "did run" item that is a minute away from two different "should run" items. Which one does it match?: : : [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]: : [code]: $ select * from users where clue > 0: no rows returned: [/code]: :

: I will have to build my code to allow a time tolerance. I am guesing on a 2-4 minute range. I ran through the scriptsThatShouldRun [] list: and there is nothing that will overlap, everything is pretty spread out through the day.: : As you mentioned, I will not be able to use the startswith() method: and will have to figure out way around this. I am working on some code: like this:: [CODE]: # loop each thru each minute: for minute in minutesList:: if day of week in scriptsThatShouldRun == day of week in minutesList: if month in scriptsThatShouldRun == month in minutesList: if day of month in scriptsThatShouldRun == day of month in minutesList: if hour in scriptsThatShouldRun == hour in minutesList : if minute in scriptsThatShouldRun == minute in minutesList: : # its eligible to run based on the same time stamp, check if it did:: : for x in scriptsThatShouldRun: if x not in scriptsThatDidRun:: shouldHaveRan.append(x): else: didntRun.append(x): : # need to build in a 2-4 minute tolerance either way on the time: [/CODE] : : I am really not sure if this is the right way to start going through each requirement or not. I just know I have to meet each requirement, then check if it ran or not, and append to a list. I know this may sound like a long way of doing it, but I am really new to the python world.: : Any help or ideas or code would be very appreciated.

Well, if you're looking for the easiest approach, I would say loading all of your data into a database and using SQL statements would be the simplest, but there's certainly ways of doing it in python too, so here are some ideas.

You'll notice that the time format your strings use doesn't include a year component so the module defaults the value to 1900. The 246 and -1 values in the tuple returned by strptime are the "day of the year" and the "daylight savings" flag, respectively. You can pretty much ignore them for your purposes.

Here are some statements I hacked out in the interactive interpreter that may help:

I have gone through this and it makes sense to me. I am trying to implement this right now and my second list has an additional element of a path to shell script besides the date itself, so I am trying to figure out how I can make this work. If I can get this:

: I have gone through this and it makes sense to me. I am trying to implement this right now and my second list has an additional element of a path to shell script besides the date itself, so I am trying to figure out how I can make this work. If I can get this:: : [CODE]: str1 = 'Mon Sep 03 07:50': str2 = 'Mon Sep 03 07:50 /path/to/script/shell.sh': : # works fine: t1 = (time.localtime()[0],) + time.strptime(str1, '%a %b %d %H:%M')[1:]: : [B]# doesnt like this one cuz of the path to .sh[/B]: t2 = (time.localtime()[0],) + time.strptime(str2, '%a %b %d %H:%M')[1:]: [/CODE]: : to work, I think I am set because I can use this for each rule I need to loop through, then find the differences.: : I really appreciate your help! I am soo close..Thanks

Simple, if you have too much info in your string, you need to get rid of the extraneous stuff:

: This works great, thank you!: : I am working through an issue that I hope is simple:: : [CODE]: parts = job.split('/', 1) : [/CODE]: is it possible to add: blank space ' ' AND $ because: some of my shell scripts start with those instead of: the /. basically i have a mix of starting points that: i need to split on ('/', '$', ' ') and split() only : allows 2 arguments to be given.

You can split on any single substring, but with a space you'll have to be careful. Here's an idea that might be helpful:[code]s = 'Mon Sep 03 07:50 /path/to/script/shell.sh'weekday, month, day, time, path = s.split(' ')[/code]

: i am still going over the tolerance code snippet you: gave me so i can completely understand it, but it seems: to work fine.: [CODE]: if t1 >= t2 - (tolerance * 60) and t1 <= t2 + (tolerance * 60):: [/CODE]

The tolerance argument is just the number of minutes in either direction of the target time that you will allow as a "match". tolerance * 60 is then the number of seconds in either direction you allow as a "match". Note that t1 and t2 are not strings like 'Fri Sep 03 07:50' but are numbers, representing a datetime in terms of seconds since some point in history.

Thanks for the information and all of your help. I am going tobe working on this later in the week. Thanks again!

: : This works great, thank you!: : : : I am working through an issue that I hope is simple:: : : : [CODE]: : parts = job.split('/', 1) : : [/CODE]: : is it possible to add: blank space ' ' AND $ because: : some of my shell scripts start with those instead of: : the /. basically i have a mix of starting points that: : i need to split on ('/', '$', ' ') and split() only : : allows 2 arguments to be given.: : You can split on any single substring, but with a space you'll have to be careful. Here's an idea that might be helpful:: [code]: s = 'Mon Sep 03 07:50 /path/to/script/shell.sh': weekday, month, day, time, path = s.split(' '): [/code]: : : i am still going over the tolerance code snippet you: : gave me so i can completely understand it, but it seems: : to work fine.: : [CODE]: : if t1 >= t2 - (tolerance * 60) and t1 <= t2 + (tolerance * 60):: : [/CODE]: : The tolerance argument is just the number of minutes in either direction of the target time that you will allow as a "match". tolerance * 60 is then the number of seconds in either direction you allow as a "match". Note that t1 and t2 are not strings like 'Fri Sep 03 07:50' but are numbers, representing a datetime in terms of seconds since some point in history.: : : [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]: : [code]: $ select * from users where clue > 0: no rows returned: [/code]: :