Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #47287 > unrolled thread
| Started by | cerr <ron.eggler@gmail.com> |
|---|---|
| First post | 2013-06-06 17:03 -0700 |
| Last post | 2013-06-07 09:35 -0700 |
| Articles | 5 — 3 participants |
Back to article view | Back to comp.lang.python
trigger at TDM/2 only cerr <ron.eggler@gmail.com> - 2013-06-06 17:03 -0700
Re: trigger at TDM/2 only MRAB <python@mrabarnett.plus.com> - 2013-06-07 01:49 +0100
Re: trigger at TDM/2 only cerr <ron.eggler@gmail.com> - 2013-06-07 09:38 -0700
Re: trigger at TDM/2 only Dave Angel <davea@davea.name> - 2013-06-06 20:43 -0400
Re: trigger at TDM/2 only cerr <ron.eggler@gmail.com> - 2013-06-07 09:35 -0700
| From | cerr <ron.eggler@gmail.com> |
|---|---|
| Date | 2013-06-06 17:03 -0700 |
| Subject | trigger at TDM/2 only |
| Message-ID | <46d8c42f-fdfd-49a5-96e8-ec53d5e599d5@googlegroups.com> |
Hi,
I have a process that I can trigger only at a certain time. Assume I have a TDM period of 10min, that means, I can only fire my trigger at the 5th minute of every 10min cycle i.e. at XX:05, XX:15, XX:25... For hat I came up with following algorithm which oly leaves the waiting while loop if minute % TDM/2 is 0 but not if minute % TDM is 0:
min = datetime.datetime.now().timetuple().tm_hour*60 + datetime.datetime.now().timetuple().tm_min
while not (min%tdm_timeslot != 0 ^ min%(int(tdm_timeslot/2)) != 0):
time.sleep(10)
logger.debug("WAIT "+str(datetime.datetime.now().timetuple().tm_hour*60 + datetime.datetime.now().timetuple().tm_min))
logger.debug(str(min%(int(tdm_timeslot/2)))+" - "+str(min%tdm_timeslot))
min = datetime.datetime.now().timetuple().tm_hour*60 + datetime.datetime.now().timetuple().tm_min
logger.debug("RUN UPDATE CHECK...")
But weird enough, the output I get is something like this:
I would expect my while to exit the loop as soon as the minute turns 1435... why is it staying in? What am I doing wrong here?
WAIT 1434
3 - 3
WAIT 1434
4 - 4
WAIT 1434
4 - 4
WAIT 1434
4 - 4
WAIT 1434
4 - 4
WAIT 1434
4 - 4
WAIT 1435
4 - 4
WAIT 1435
0 - 5
WAIT 1435
0 - 5
WAIT 1435
0 - 5
WAIT 1435
0 - 5
WAIT 1435
0 - 5
WAIT 1436
0 - 5
RUN UPDATE CHECK...
Thank you for any assistance!
Ron
[toc] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2013-06-07 01:49 +0100 |
| Message-ID | <mailman.2832.1370566146.3114.python-list@python.org> |
| In reply to | #47287 |
On 07/06/2013 01:03, cerr wrote:
> Hi,
>
> I have a process that I can trigger only at a certain time. Assume I have a TDM period of 10min, that means, I can only fire my trigger at the 5th minute of every 10min cycle i.e. at XX:05, XX:15, XX:25... For hat I came up with following algorithm which oly leaves the waiting while loop if minute % TDM/2 is 0 but not if minute % TDM is 0:
> min = datetime.datetime.now().timetuple().tm_hour*60 + datetime.datetime.now().timetuple().tm_min
> while not (min%tdm_timeslot != 0 ^ min%(int(tdm_timeslot/2)) != 0):
> time.sleep(10)
> logger.debug("WAIT "+str(datetime.datetime.now().timetuple().tm_hour*60 + datetime.datetime.now().timetuple().tm_min))
> logger.debug(str(min%(int(tdm_timeslot/2)))+" - "+str(min%tdm_timeslot))
> min = datetime.datetime.now().timetuple().tm_hour*60 + datetime.datetime.now().timetuple().tm_min
> logger.debug("RUN UPDATE CHECK...")
>
> But weird enough, the output I get is something like this:
> I would expect my while to exit the loop as soon as the minute turns 1435... why is it staying in? What am I doing wrong here?
>
> WAIT 1434
> 3 - 3
> WAIT 1434
> 4 - 4
> WAIT 1434
> 4 - 4
> WAIT 1434
> 4 - 4
> WAIT 1434
> 4 - 4
> WAIT 1434
> 4 - 4
> WAIT 1435
> 4 - 4
> WAIT 1435
> 0 - 5
> WAIT 1435
> 0 - 5
> WAIT 1435
> 0 - 5
> WAIT 1435
> 0 - 5
> WAIT 1435
> 0 - 5
> WAIT 1436
> 0 - 5
> RUN UPDATE CHECK...
>
Possibly it's due to operator precedence. The bitwise operators &, |
and ^ have a higher precedence than comparisons such as !=.
A better condition might be:
min % tdm_timeslot != tdm_timeslot // 2
or, better yet, work out how long before the next trigger time and then
sleep until then.
[toc] | [prev] | [next] | [standalone]
| From | cerr <ron.eggler@gmail.com> |
|---|---|
| Date | 2013-06-07 09:38 -0700 |
| Message-ID | <2824bb86-2013-4560-a78e-ee4cc7fa4792@googlegroups.com> |
| In reply to | #47288 |
MRAB,
Thanks for the hint! Yep, that's much easier!
Thanks! :)
Ron
On Thursday, June 6, 2013 5:49:55 PM UTC-7, MRAB wrote:
> On 07/06/2013 01:03, cerr wrote:
>
> > Hi,
>
> >
>
> > I have a process that I can trigger only at a certain time. Assume I have a TDM period of 10min, that means, I can only fire my trigger at the 5th minute of every 10min cycle i.e. at XX:05, XX:15, XX:25... For hat I came up with following algorithm which oly leaves the waiting while loop if minute % TDM/2 is 0 but not if minute % TDM is 0:
>
> > min = datetime.datetime.now().timetuple().tm_hour*60 + datetime.datetime.now().timetuple().tm_min
>
> > while not (min%tdm_timeslot != 0 ^ min%(int(tdm_timeslot/2)) != 0):
>
> > time.sleep(10)
>
> > logger.debug("WAIT "+str(datetime.datetime.now().timetuple().tm_hour*60 + datetime.datetime.now().timetuple().tm_min))
>
> > logger.debug(str(min%(int(tdm_timeslot/2)))+" - "+str(min%tdm_timeslot))
>
> > min = datetime.datetime.now().timetuple().tm_hour*60 + datetime.datetime.now().timetuple().tm_min
>
> > logger.debug("RUN UPDATE CHECK...")
>
> >
>
> > But weird enough, the output I get is something like this:
>
> > I would expect my while to exit the loop as soon as the minute turns 1435... why is it staying in? What am I doing wrong here?
>
> >
>
> > WAIT 1434
>
> > 3 - 3
>
> > WAIT 1434
>
> > 4 - 4
>
> > WAIT 1434
>
> > 4 - 4
>
> > WAIT 1434
>
> > 4 - 4
>
> > WAIT 1434
>
> > 4 - 4
>
> > WAIT 1434
>
> > 4 - 4
>
> > WAIT 1435
>
> > 4 - 4
>
> > WAIT 1435
>
> > 0 - 5
>
> > WAIT 1435
>
> > 0 - 5
>
> > WAIT 1435
>
> > 0 - 5
>
> > WAIT 1435
>
> > 0 - 5
>
> > WAIT 1435
>
> > 0 - 5
>
> > WAIT 1436
>
> > 0 - 5
>
> > RUN UPDATE CHECK...
>
> >
>
> Possibly it's due to operator precedence. The bitwise operators &, |
>
> and ^ have a higher precedence than comparisons such as !=.
>
>
>
> A better condition might be:
>
>
>
> min % tdm_timeslot != tdm_timeslot // 2
>
>
>
> or, better yet, work out how long before the next trigger time and then
>
> sleep until then.
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-06-06 20:43 -0400 |
| Message-ID | <mailman.2831.1370566116.3114.python-list@python.org> |
| In reply to | #47287 |
On 06/06/2013 08:03 PM, cerr wrote:
> Hi,
>
> I have a process that I can trigger only at a certain time. Assume I have a TDM period of 10min, that means, I can only fire my trigger at the 5th minute of every 10min cycle i.e. at XX:05, XX:15, XX:25... For hat I came up with following algorithm which oly leaves the waiting while loop if minute % TDM/2 is 0 but not if minute % TDM is 0:
> min = datetime.datetime.now().timetuple().tm_hour*60 + datetime.datetime.now().timetuple().tm_min
> while not (min%tdm_timeslot != 0 ^ min%(int(tdm_timeslot/2)) != 0):
You might have spent three minutes and simplified this for us. And in
the process discovered the problem.
(BTW, min() is a builtin function, so it's not really a good idea to be
shadowing it.)
You didn't give python version, so my sample is assuming Python 2.7
For your code it shouldn't matter.
tdm = 10
tdm2 = 5
y = min(3,4)
print y
for now in range(10,32):
print now, now%tdm, now%tdm2,
print not(now % tdm !=0 ^ now%tdm2 !=0) #bad
print not((now % tdm !=0) ^ (now%tdm2 !=0)) #good
Your problem is one of operator precedence. Notice that ^ has a higher
precedence than != operator, so you need the parentheses I added in the
following line.
What I don't understand is why you used this convoluted approach. Why not
print now%tdm != tdm2
For precedence rules, see:
http://docs.python.org/2/reference/expressions.html#operator-precedence
--
DaveA
[toc] | [prev] | [next] | [standalone]
| From | cerr <ron.eggler@gmail.com> |
|---|---|
| Date | 2013-06-07 09:35 -0700 |
| Message-ID | <4e5c610f-0242-48eb-8502-3fa00d53d137@googlegroups.com> |
| In reply to | #47289 |
DaveA, Yep, that seems to just be about it! Much easier! Thanks for the hint! Much appreciated!!!! :) Ron On Thursday, June 6, 2013 5:43:11 PM UTC-7, Dave Angel wrote: > On 06/06/2013 08:03 PM, cerr wrote: > > > Hi, > > > > > > I have a process that I can trigger only at a certain time. Assume I have a TDM period of 10min, that means, I can only fire my trigger at the 5th minute of every 10min cycle i.e. at XX:05, XX:15, XX:25... For hat I came up with following algorithm which oly leaves the waiting while loop if minute % TDM/2 is 0 but not if minute % TDM is 0: > > > min = datetime.datetime.now().timetuple().tm_hour*60 + datetime.datetime.now().timetuple().tm_min > > > while not (min%tdm_timeslot != 0 ^ min%(int(tdm_timeslot/2)) != 0): > > > > You might have spent three minutes and simplified this for us. And in > > the process discovered the problem. > > > > (BTW, min() is a builtin function, so it's not really a good idea to be > > shadowing it.) > > > > You didn't give python version, so my sample is assuming Python 2.7 > > For your code it shouldn't matter. > > > > tdm = 10 > > tdm2 = 5 > > > > y = min(3,4) > > print y > > > > for now in range(10,32): > > print now, now%tdm, now%tdm2, > > print not(now % tdm !=0 ^ now%tdm2 !=0) #bad > > print not((now % tdm !=0) ^ (now%tdm2 !=0)) #good > > > > > > Your problem is one of operator precedence. Notice that ^ has a higher > > precedence than != operator, so you need the parentheses I added in the > > following line. > > > > What I don't understand is why you used this convoluted approach. Why not > > > > print now%tdm != tdm2 > > > > For precedence rules, see: > > http://docs.python.org/2/reference/expressions.html#operator-precedence > > > > > > > > > > -- > > DaveA
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web