Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Loop or Parse Tree of Requirments

bquad20bquad20 Posts: 17Member

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.

Thanks in advance!

Comments

  • infidelinfidel Posts: 2,900Member
    :
    : 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]

  • bquad20bquad20 Posts: 17Member
    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]
  • infidelinfidel Posts: 2,900Member
    : 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]

  • bquad20bquad20 Posts: 17Member
    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.

    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]
    :
    :

  • infidelinfidel Posts: 2,900Member
    : 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.

    Earlier we used the time module to format a time tuple as a string:

    [code]
    >>> import time
    >>> t = (2004, 9, 3, 7, 50, 0, 0, 0, 0)
    >>> time.strftime("%a %b %d %H:%M", t)
    'Mon Sep 03 07:50'
    [/code]

    The time module has the inverse function as well, allowing you to parse a string into a time tuple:

    [code]
    >>> time.strptime("Mon Sep 03 07:50", "%a %b %d %H:%M")
    (1900, 9, 3, 7, 50, 0, 0, 246, -1)
    [/code]

    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:

    [code]
    >>> str1 = 'Mon Sep 03 07:50'
    >>> str2 = 'Mon Sep 03 07:52'
    >>> t1 = (time.localtime()[0],) + time.strptime(str1, '%a %b %d %H:%M')[1:]
    >>> t2 = (time.localtime()[0],) + time.strptime(str2, '%a %b %d %H:%M')[1:]
    >>> t1
    (2004, 9, 3, 7, 50, 0, 0, 246, -1)
    >>> t2
    (2004, 9, 3, 7, 52, 0, 0, 246, -1)
    >>> def times_match(time1, time2, tolerance=0):
    ... t1 = time.mktime(time1)
    ... t2 = time.mktime(time2)
    ... if t1 >= t2 - (tolerance * 60) and t1 <= t2 + (tolerance * 60):
    ... return True
    ... return False
    ...
    >>> times_match(t1, t2)
    False
    >>> times_match(t1, t2, 2)
    True
    >>>
    [/code]

    Let me know if you can't make sense out of that.


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

    [code]
    $ select * from users where clue > 0
    no rows returned
    [/code]

  • bquad20bquad20 Posts: 17Member
    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

    : Here are some statements I hacked out in the interactive interpreter that may help:
    :
    : [code]
    : >>> str1 = 'Mon Sep 03 07:50'
    : >>> str2 = 'Mon Sep 03 07:52'
    : >>> t1 = (time.localtime()[0],) + time.strptime(str1, '%a %b %d %H:%M')[1:]
    : >>> t2 = (time.localtime()[0],) + time.strptime(str2, '%a %b %d %H:%M')[1:]
    : >>> t1
    : (2004, 9, 3, 7, 50, 0, 0, 246, -1)
    : >>> t2
    : (2004, 9, 3, 7, 52, 0, 0, 246, -1)
    : >>> def times_match(time1, time2, tolerance=0):
    : ... t1 = time.mktime(time1)
    : ... t2 = time.mktime(time2)
    : ... if t1 >= t2 - (tolerance * 60) and t1 <= t2 + (tolerance * 60):
    : ... return True
    : ... return False
    : ...
    : >>> times_match(t1, t2)
    : False
    : >>> times_match(t1, t2, 2)
    : True
    : >>>
    : [/code]
    :
    : Let me know if you can't make sense out of that.
    :
    :
    : [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]
    :
    : [code]
    : $ select * from users where clue > 0
    : no rows returned
    : [/code]
    :
    :

  • infidelinfidel Posts: 2,900Member
    : 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:

    [code]
    >>> str2 = 'Mon Sep 03 07:50 /path/to/script/shell.sh'
    >>> parts = str2.split('/', 1)
    >>> parts
    ['Mon Sep 03 07:50 ', 'path/to/script/shell.sh']
    >>> t = parts[0]
    >>> t.strip()
    'Mon Sep 03 07:50'
    [/code]

    Or a more compact (though less readable) way:

    [code]
    >>> t = str2.split('/', 1)[0].strip()
    >>> t
    'Mon Sep 03 07:50'
    [/code]


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

    [code]
    $ select * from users where clue > 0
    no rows returned
    [/code]

  • bquad20bquad20 Posts: 17Member
    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.

    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]

    : Simple, if you have too much info in your string, you need to get rid of the extraneous stuff:
    :
    : [code]
    : >>> str2 = 'Mon Sep 03 07:50 /path/to/script/shell.sh'
    : >>> parts = str2.split('/', 1)
    : >>> parts
    : ['Mon Sep 03 07:50 ', 'path/to/script/shell.sh']
    : >>> t = parts[0]
    : >>> t.strip()
    : 'Mon Sep 03 07:50'
    : [/code]
    :
    : Or a more compact (though less readable) way:
    :
    : [code]
    : >>> t = str2.split('/', 1)[0].strip()
    : >>> t
    : 'Mon Sep 03 07:50'
    : [/code]
    :
    :
    : [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]
    :
    : [code]
    : $ select * from users where clue > 0
    : no rows returned
    : [/code]
    :
    :

  • infidelinfidel Posts: 2,900Member
    : 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]

  • bquad20bquad20 Posts: 17Member
    Thanks for the information and all of your help. I am going to
    be 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]
    :
    :

Sign In or Register to comment.