Every Minute, Compare Values - Programmers Heaven

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.

Every Minute, Compare Values

bquad20bquad20 Posts: 17Member
I have 2 lists (list a, list b) and I need to compare them
against each other to find the differences. The lists hold
a timestamp and information about certain shell scripts that
should run (list a) and what did run (list b).

Example:
[CODE]
list a (scripts should have ran)
'Thu Jun 17 11:30 /path/to/shell/script.sh'
'Thu Jun 17 5:30 /path/to/shell/aScript.sh'

list b (scripts that did run)
'Thu Jun 17 11:30 /path/to/shell/script.sh'

[B]didnt run (was not in list b)
Thu Jun 17 5:30 /path/to/shell/aScript.sh[/B]
[/CODE]
To compare them, I have to loop through each minute of the
day to see if the script is eligible to run at that time
(ex: list a is eligible to run at 11:30). If it is eligible run,
(its 11:30 in this case, so script can run) then I need to compare the lists to see
if list a is equal to list b (basically, list b has a matching value).

Can some help/point me in the right direction? I have the two
lists built (a & b), but I am unsure on how to start looping
through each minute of the day & then compare if the 2 values
to see if they are equal or the same. If they are the same,
I want to append that info to a new list (list c) OR if they
are not the same, I want to append that info to a new list (list d).

Any help would be greatly appreciated.

Thanks In Advance!

Comments

  • infidelinfidel Posts: 2,900Member
    [b][red]This message was edited by Moderator at 2004-8-24 7:48:44[/red][/b][hr]
    : I have 2 lists (list a, list b) and I need to compare them
    : against each other to find the differences. The lists hold
    : a timestamp and information about certain shell scripts that
    : should run (list a) and what did run (list b).
    :
    : Example:
    : [CODE]
    : list a (scripts should have ran)
    : 'Thu Jun 17 11:30 /path/to/shell/script.sh'
    : 'Thu Jun 17 5:30 /path/to/shell/aScript.sh'
    :
    : list b (scripts that did run)
    : 'Thu Jun 17 11:30 /path/to/shell/script.sh'
    :
    : [B]didnt run (was not in list b)
    : Thu Jun 17 5:30 /path/to/shell/aScript.sh[/B]
    : [/CODE]
    : To compare them, I have to loop through each minute of the
    : day to see if the script is eligible to run at that time
    : (ex: list a is eligible to run at 11:30). If it is eligible run,
    : (its 11:30 in this case, so script can run) then I need to compare the lists to see
    : if list a is equal to list b (basically, list b has a matching value).
    :
    : Can some help/point me in the right direction? I have the two
    : lists built (a & b), but I am unsure on how to start looping
    : through each minute of the day & then compare if the 2 values
    : to see if they are equal or the same. If they are the same,
    : I want to append that info to a new list (list c) OR if they
    : are not the same, I want to append that info to a new list (list d).

    It's pretty easy to see if an object is in a list:

    [code]
    scripts_to_run = ['Thu Jun 17 11:30 foo.sh', 'Thu Jun 17 17:30 bar.sh']
    scripts_that_ran = ['Thu Jun 17 17:30 bar.sh']
    scripts_not_run = []

    for script in scripts_to_run:
    if script not in scripts_that_ran:
    scripts_not_run.append(script)
    [/code]

    You can also use "list comprehensions" which are a kind of shortcut in python which are a little tricky to understand at first but very handy:

    [code]
    scripts_not_run = [script for script in scripts_to_run if script not in scripts_that_ran]
    [/code]

    Running a script every minute of the day is a task probably best handled by the operating system using "cron" or whatever scheduling system your machine has. If you leave it up to your program to handle the scheduling you have to worry about that process staying up all day and synchronizing it to the system clock etc which doesn't sound like fun to me. Better, IMO, to write a simple script that performs your needed logic and tell the scheduler to run it once a minute.


    [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 looks like something I will be able to use, thank you!

    I think I may have not been clear on my script running part.
    I have to have a "pre-defined" list or something that has all
    the minutes of the day [b]already[/b] in it. I am NOT trying
    to run this script every minute, I am simply trying to check
    my lists against every minute of the day somehow.

    So, this script executes @ 12:05 am and will already have the
    lists(2) built and I need to have a 3rd list(?) or something
    that I can check against that already has every minute of the
    day in it.

    : [b][red]This message was edited by Moderator at 2004-8-24 7:48:44[/red][/b][hr]
    : : I have 2 lists (list a, list b) and I need to compare them
    : : against each other to find the differences. The lists hold
    : : a timestamp and information about certain shell scripts that
    : : should run (list a) and what did run (list b).
    : :
    : : Example:
    : : [CODE]
    : : list a (scripts should have ran)
    : : 'Thu Jun 17 11:30 /path/to/shell/script.sh'
    : : 'Thu Jun 17 5:30 /path/to/shell/aScript.sh'
    : :
    : : list b (scripts that did run)
    : : 'Thu Jun 17 11:30 /path/to/shell/script.sh'
    : :
    : : [B]didnt run (was not in list b)
    : : Thu Jun 17 5:30 /path/to/shell/aScript.sh[/B]
    : : [/CODE]
    : : To compare them, I have to loop through each minute of the
    : : day to see if the script is eligible to run at that time
    : : (ex: list a is eligible to run at 11:30). If it is eligible run,
    : : (its 11:30 in this case, so script can run) then I need to compare the lists to see
    : : if list a is equal to list b (basically, list b has a matching value).
    : :
    : : Can some help/point me in the right direction? I have the two
    : : lists built (a & b), but I am unsure on how to start looping
    : : through each minute of the day & then compare if the 2 values
    : : to see if they are equal or the same. If they are the same,
    : : I want to append that info to a new list (list c) OR if they
    : : are not the same, I want to append that info to a new list (list d).
    :
    : It's pretty easy to see if an object is in a list:
    :
    : [code]
    : scripts_to_run = ['Thu Jun 17 11:30 foo.sh', 'Thu Jun 17 17:30 bar.sh']
    : scripts_that_ran = ['Thu Jun 17 17:30 bar.sh']
    : scripts_not_run = []
    :
    : for script in scripts_to_run:
    : if script not in scripts_that_ran:
    : scripts_not_run.append(script)
    : [/code]
    :
    : You can also use "list comprehensions" which are a kind of shortcut in python which are a little tricky to understand at first but very handy:
    :
    : [code]
    : scripts_not_run = [script for script in scripts_to_run if script not in scripts_that_ran]
    : [/code]
    :
    : Running a script every minute of the day is a task probably best handled by the operating system using "cron" or whatever scheduling system your machine has. If you leave it up to your program to handle the scheduling you have to worry about that process staying up all day and synchronizing it to the system clock etc which doesn't sound like fun to me. Better, IMO, to write a simple script that performs your needed logic and tell the scheduler to run it once a minute.
    :
    :
    : [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
    [b][red]This message was edited by Moderator at 2004-8-25 10:28:21[/red][/b][hr]
    : This looks like something I will be able to use, thank you!
    :
    : I think I may have not been clear on my script running part.
    : I have to have a "pre-defined" list or something that has all
    : the minutes of the day [b]already[/b] in it. I am NOT trying
    : to run this script every minute, I am simply trying to check
    : my lists against every minute of the day somehow.
    :
    : So, this script executes @ 12:05 am and will already have the
    : lists(2) built and I need to have a 3rd list(?) or something
    : that I can check against that already has every minute of the
    : day in it.

    Oh, ok. I wonder why, though. If the first list has all of the jobs that [italic]should[/italic] run including the time they should run, and the second list has all of the jobs that [italic]did[/italic] run including the time they ran I don't think you need a separate list that includes all of the times. If an item is in the first list but not the second then you know it didn't run. If an item is in the second list but not the first then you know something ran that shouldn't have.

    Maybe I'm still not clear on the problem you're trying to solve.

    If you're still determined to loop over every minute of the day, here's something that may help. This function is called a generator because it uses the "yield" keyword. Yielding a value is like returning a value, except that it returns multiple values, one per loop iteration. This allows you to loop over a long sequence, such as every minute of a day, without having to store that entire sequence in memory all at once.

    [code]
    def todays_minutes():
    year, month, day = time.localtime()[0:3]
    for hour in xrange(0, 24):
    for minute in xrange(0, 60):
    t = (year, month, day, hour, minute, 0, 0, 0, 0)
    yield time.strftime("%a %b %d %H:%M", t)
    [/code]

    Here is how you would use this function:

    [code]
    >>> for min in todays_minutes():
    ... print min
    [/code]

    Here is what the output looks like:

    [code]
    Mon Aug 25 00:00
    Mon Aug 25 00:01
    Mon Aug 25 00:02
    Mon Aug 25 00:03
    Mon Aug 25 00:04
    Mon Aug 25 00:05
    Mon Aug 25 00:06
    Mon Aug 25 00:07
    Mon Aug 25 00:08
    Mon Aug 25 00:09
    Mon Aug 25 00:10
    Mon Aug 25 00:11
    Mon Aug 25 00:12
    Mon Aug 25 00:13
    Mon Aug 25 00:14
    Mon Aug 25 00:15
    Mon Aug 25 00:16
    Mon Aug 25 00:17
    Mon Aug 25 00:18
    Mon Aug 25 00:19
    Mon Aug 25 00:20
    Mon Aug 25 00:21
    Mon Aug 25 00:22
    Mon Aug 25 00:23
    Mon Aug 25 00:24
    Mon Aug 25 00:25
    Mon Aug 25 00:26
    Mon Aug 25 00:27
    Mon Aug 25 00:28
    Mon Aug 25 00:29
    Mon Aug 25 00:30
    Mon Aug 25 00:31
    Mon Aug 25 00:32
    Mon Aug 25 00:33
    Mon Aug 25 00:34
    Mon Aug 25 00:35
    Mon Aug 25 00:36
    Mon Aug 25 00:37
    Mon Aug 25 00:38
    Mon Aug 25 00:39
    Mon Aug 25 00:40
    Mon Aug 25 00:41
    Mon Aug 25 00:42
    Mon Aug 25 00:43
    Mon Aug 25 00:44
    Mon Aug 25 00:45
    Mon Aug 25 00:46
    Mon Aug 25 00:47
    Mon Aug 25 00:48
    Mon Aug 25 00:49
    Mon Aug 25 00:50
    Mon Aug 25 00:51
    Mon Aug 25 00:52
    Mon Aug 25 00:53
    Mon Aug 25 00:54
    Mon Aug 25 00:55
    Mon Aug 25 00:56
    Mon Aug 25 00:57
    Mon Aug 25 00:58
    Mon Aug 25 00:59
    Mon Aug 25 01:00
    Mon Aug 25 01:01
    Mon Aug 25 01:02
    ... {snip very long list} ...
    Mon Aug 25 23:56
    Mon Aug 25 23:57
    Mon Aug 25 23:58
    Mon Aug 25 23:59
    [/code]

    Depending on whether you have control over the representation of time in your strings, the time.strftime function may not be flexible enough for you. I just chose to use the time module's features for simplicity's sake. The main point I was trying to get across was using a generator.

    Note that you can yield anything from the generator fuction, I just chose to use a string representation close to what your example data used.

    If you do decide you need the whole list available at once instead of via a for loop, you can do this:

    minlist = list(todays_minutes())

    The list() function (really the constructor for the list class) will run the generator internally and return a full list.

    [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 read through your reply and spent some time trying to implement
    it in a few ways and it works great. Thank you for your help!

    : [b][red]This message was edited by Moderator at 2004-8-25 10:28:21[/red][/b][hr]
    : : This looks like something I will be able to use, thank you!
    : :
    : : I think I may have not been clear on my script running part.
    : : I have to have a "pre-defined" list or something that has all
    : : the minutes of the day [b]already[/b] in it. I am NOT trying
    : : to run this script every minute, I am simply trying to check
    : : my lists against every minute of the day somehow.
    : :
    : : So, this script executes @ 12:05 am and will already have the
    : : lists(2) built and I need to have a 3rd list(?) or something
    : : that I can check against that already has every minute of the
    : : day in it.
    :
    : Oh, ok. I wonder why, though. If the first list has all of the jobs that [italic]should[/italic] run including the time they should run, and the second list has all of the jobs that [italic]did[/italic] run including the time they ran I don't think you need a separate list that includes all of the times. If an item is in the first list but not the second then you know it didn't run. If an item is in the second list but not the first then you know something ran that shouldn't have.
    :
    : Maybe I'm still not clear on the problem you're trying to solve.
    :
    : If you're still determined to loop over every minute of the day, here's something that may help. This function is called a generator because it uses the "yield" keyword. Yielding a value is like returning a value, except that it returns multiple values, one per loop iteration. This allows you to loop over a long sequence, such as every minute of a day, without having to store that entire sequence in memory all at once.
    :
    : [code]
    : def todays_minutes():
    : year, month, day = time.localtime()[0:3]
    : for hour in xrange(0, 24):
    : for minute in xrange(0, 60):
    : t = (year, month, day, hour, minute, 0, 0, 0, 0)
    : yield time.strftime("%a %b %d %H:%M", t)
    : [/code]
    :
    : Here is how you would use this function:
    :
    : [code]
    : >>> for min in todays_minutes():
    : ... print min
    : [/code]
    :
    : Here is what the output looks like:
    :
    : [code]
    : Mon Aug 25 00:00
    : Mon Aug 25 00:01
    : Mon Aug 25 00:02
    : Mon Aug 25 00:03
    : Mon Aug 25 00:04
    : Mon Aug 25 00:05
    : Mon Aug 25 00:06
    : Mon Aug 25 00:07
    : Mon Aug 25 00:08
    : Mon Aug 25 00:09
    : Mon Aug 25 00:10
    : Mon Aug 25 00:11
    : Mon Aug 25 00:12
    : Mon Aug 25 00:13
    : Mon Aug 25 00:14
    : Mon Aug 25 00:15
    : Mon Aug 25 00:16
    : Mon Aug 25 00:17
    : Mon Aug 25 00:18
    : Mon Aug 25 00:19
    : Mon Aug 25 00:20
    : Mon Aug 25 00:21
    : Mon Aug 25 00:22
    : Mon Aug 25 00:23
    : Mon Aug 25 00:24
    : Mon Aug 25 00:25
    : Mon Aug 25 00:26
    : Mon Aug 25 00:27
    : Mon Aug 25 00:28
    : Mon Aug 25 00:29
    : Mon Aug 25 00:30
    : Mon Aug 25 00:31
    : Mon Aug 25 00:32
    : Mon Aug 25 00:33
    : Mon Aug 25 00:34
    : Mon Aug 25 00:35
    : Mon Aug 25 00:36
    : Mon Aug 25 00:37
    : Mon Aug 25 00:38
    : Mon Aug 25 00:39
    : Mon Aug 25 00:40
    : Mon Aug 25 00:41
    : Mon Aug 25 00:42
    : Mon Aug 25 00:43
    : Mon Aug 25 00:44
    : Mon Aug 25 00:45
    : Mon Aug 25 00:46
    : Mon Aug 25 00:47
    : Mon Aug 25 00:48
    : Mon Aug 25 00:49
    : Mon Aug 25 00:50
    : Mon Aug 25 00:51
    : Mon Aug 25 00:52
    : Mon Aug 25 00:53
    : Mon Aug 25 00:54
    : Mon Aug 25 00:55
    : Mon Aug 25 00:56
    : Mon Aug 25 00:57
    : Mon Aug 25 00:58
    : Mon Aug 25 00:59
    : Mon Aug 25 01:00
    : Mon Aug 25 01:01
    : Mon Aug 25 01:02
    : ... {snip very long list} ...
    : Mon Aug 25 23:56
    : Mon Aug 25 23:57
    : Mon Aug 25 23:58
    : Mon Aug 25 23:59
    : [/code]
    :
    : Depending on whether you have control over the representation of time in your strings, the time.strftime function may not be flexible enough for you. I just chose to use the time module's features for simplicity's sake. The main point I was trying to get across was using a generator.
    :
    : Note that you can yield anything from the generator fuction, I just chose to use a string representation close to what your example data used.
    :
    : If you do decide you need the whole list available at once instead of via a for loop, you can do this:
    :
    : minlist = list(todays_minutes())
    :
    : The list() function (really the constructor for the list class) will run the generator internally and return a full list.
    :
    : [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.