Boolen check if datetime.now() is between the values of any tuple in a list of tuples - boolean-logic

I have a list of tuples looking like this:
import datetime as dt
hours = [(dt.datetime(2019,3,9,23,0), dt.datetime(2019,3,10,22,0)),
(dt.datetime(2019,3,10,23,0), d.datetime(2019,3,11,22,0))]
The list has a variable length and I just need a boolean if datetime.now() is between the first and second element of any tuple in the list.
In NumPy I would do:
((start <= now) & (end >= now)).any()
what is the most efficient way to do this in a pythonic way? Sorry about the beginners question.
this works but I don't like the len():
from itertools import takewhile
len(list(takewhile(lambda x: x[0] <= now and now <= x[1], hours ))) > 0
any better suggestions?

any(map(lambda d: d[0] <= now <= d[1], hours))
any: Logical OR across all elements
map: runs a function on every element of the list
As #steff pointed out map is redundant, because we cause list enumeration directly.
any(d[0] <= now <= d[1] for d in hours)
It would be way better if we can avoid indexing into tuple and use tuple unpacking somehow (this was the reason I started with map)

A more verbose alternative. (But more readable in my eyes)
import datetime as dt
def in_time_ranges(ranges):
now = dt.datetime.now()
return any([r for r in ranges if now <= r[0] and r[1] >= now])
ranges1 = [(dt.datetime(2019, 3, 9, 23, 0), dt.datetime(2019, 3, 10, 22, 0)),
(dt.datetime(2019, 3, 10, 23, 0), dt.datetime(2019, 3, 11, 22, 0)),
(dt.datetime(2019, 4, 10, 23, 0), dt.datetime(2019, 5, 11, 22, 0))]
print(in_time_ranges(ranges1))
ranges2 = [(dt.datetime(2017, 3, 9, 14, 0), dt.datetime(2018, 3, 10, 22, 0)),
(dt.datetime(2018, 3, 10, 23, 0), dt.datetime(2018, 3, 11, 22, 0)),
(dt.datetime(2018, 4, 10, 23, 0), dt.datetime(2018, 5, 11, 22, 0))]
print(in_time_ranges(ranges2))
Output
True
False

Related

Musician wants to know the programming term for this function

My apologies that I don't use always use programming terms in my description - I am a musician who has only dabbled in programming.
Suppose I have a list of numbers named a:
a = (0, 2, 4, 5, 7, 9, 11, 12)
and from the list a the following list of numbers is randomly generated to create list b:
b = (4, 7, 5, 7, 9, 11, 9)
Then I want to have "transpositions" (this is a musical term) of list b, such as those shown in lists c, d, and e:
c = (5, 9, 7, 9, 11, 12, 11) or d = (2, 5, 4, 5, 7, 9, 11, 9) or e = (0, 4, 2, 4, 5, 7, 9, 7)
What do you call this "transposition" of this list of numbers in programming terms, and what bit of programming code would accomplish this task? My best guess is that it has to do with the indexing of the numbers in list a, and when list b is created the indexes of list b are put in a list such as list f:
f = (2, 4, 3, 4, 5, 6, 5)
so that the "transposition" is accomplished by adding or subtracting a specific number from each number in list f. For example, the numbers in list c are generated by adding 1 to each of the numbers in list f:
(3, 5, 4, 5, 6, 7, 6)
the numbers in list d are generated by subtracting 1 to each of the numbers in list f:
(1, 3, 2, 3, 4, 5, 4)
and the numbers in list e are generated by subtracting 2 to each of the index numbers taken from list f:
(0, 2, 1, 2, 3, 4, 3)
Or if anyone has a better idea, please let me know.
An operation like "add 1 to every member of this list, to generate a new list" is usually called a map.

Make a tuple of arbitrary size functionally in Julia

An ordinary way to make a tuple in Julia is like this:
n = 5
t2 = (n,n) # t2 = (5,5)
t3 = (n,n,n)# t3 = (5,5,5)
I want to make a tuple of arbitrary size functionally.
n = 5
someFunction(n,size) = ???
t10 = someFunction(n,10) # t10 = (5,5,5,5,5,5,5,5,5,5)
How can I realize this?
Any information would be appreciated.
Maybe what you are looking for is ntuple ?
julia> ntuple(_ -> 5, 10)
(5, 5, 5, 5, 5, 5, 5, 5, 5, 5)
Note that, you can also use tuple or Tuple:
julia> tuple((5 for _ in 1:10)...)
(5, 5, 5, 5, 5, 5, 5, 5, 5, 5)
julia> Tuple(5 for _ in 1:10)
(5, 5, 5, 5, 5, 5, 5, 5, 5, 5)

Query for array elements inside Jsonb type postgreSQL

I have a psql table where one of the jsonb data is extracted over it.
{
"SrcRcs": [4, 0, 1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 158],
"reason": "",
"result": "Success",
"InitTech": 1
}
This column is named Data and is of type jsonb.
I am extracting the SrcRcs data from the jsonb:
select Data->'SrcRcs' from table_name;
Output:
[4, 0, 1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 158]
But which is in unsorted order as from the jsonb.
I want it in the sorted order like this:
[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,158]
Can someone please help me out?
I have tried the psql sort() but wasn't able to achieve the desired result.
You need to unnest the array elements and then aggregate them back in a sorted way:
SELECT (select jsonb_agg(i::int order by i::int)
from jsonb_array_elements(data -> 'SrcRcs') as t(i))
from the_table
If you want, you can create a function for this to make the SQL queries easier.

AWK: filtering of the data based on TWO column information

I am working on post-processing of multi-column CSV arranged in multi-column format:
ID, POP, dG
1, 10, -5.6200
2, 4, -5.4900
3, 1, -5.3000
4, 4, -5.1600
5, 4, -4.8800
6, 3, -4.7600
7, 2, -4.4900
8, 5, -4.4500
9, 2, -4.4400
10, 8, -4.1400
11, 1, -4.1200
12, 2, -4.0900
13, 5, -4.0100
14, 1, -3.9500
15, 3, -3.9200
16, 10, -3.8800
17, 1, -3.8700
18, 3, -3.8300
19, 1, -3.8200
20, 3, -3.8000
Previously I have used the following AWK sollution to process the inout log two times, detect pop(MAX) and save linnes which matched $2 > (.8 * max)':
awk -F ', ' 'NR == 1 {next} FNR==NR {if (max < $2) {max=$2; n=FNR+1} next} FNR <= 2 || (FNR == n && $2 > (.4*max)) || $2 > (.8 * max)' input.csv{,} > output.csv
that could reduce the input log keeping just two linnes with highest POP:
ID, POP, dG
1, 10, -5.6200
16, 10, -3.8800
Now I need to change the search algorithm taking into account the both 2nd (POP) and 3rd(dG) columns: i) always taking the first line as the reference, which always has most negative number in the 3rd column (dG); ii) finding the line which has biggest number in the second column, pop(MAX);
iii) taking all linnes between (i) and (ii) that will match the following rule applied for the BOTH columns:
a) line should have (negative) number in 3rd column, matching following the rule: $1 > (.5 * $1(min))', where $1(min) is the number (dG) of the first line (always most negative)
b) additionally line should match the old rule for the second column with decreased threshold : $2 = or > (.5 * max)', where max is the pop(MAX)
So an expecting output should be
ID, POP, dG
1, 10, -5.6200. # this is the first line with most negative dG
8, 5, -4.4500 # this has POP (5) and dG (-4.4500) matching the both rules
10, 8, -4.1400. # this has POP (8) and dG (-4.1400) matching the both rules
16, 10, -3.8800 # this is pop max, with higher POP
ADDED 8-04:
For the case if the first line has with very low POP (which does not match the rule $2 >= (.5 * maxPop)
ID, POP, dG
1, 5, -5.5600
2, 7, -5.3300
3, 7, -5.1900
4, 1, -4.6800
5, 1, -4.5800
6, 5, -4.5600
7, 3, -4.4700
8, 4, -4.4300
9, 9, -4.4200
10, 4, -4.4200
11, 2, -4.3800
12, 4, -4.3400
13, 25, -4.3000
14, 6, -4.2900
15, 8, -4.2600
16, 3, -4.2300
17, 1, -4.1800
18, 3, -4.1300
19, 1, -4.1300
20, 1, -4.1200
21, 27, -4.0800
22, 2, -4.0300
the output should not contain the first line either while still using its value from dG column as the reference for the second condition ($3 <= (.5 * minD), which should be applied for the selection of other linnes in the output:
13, 25, -4.3000
21, 27, -4.0800
You may use this awk solution:
awk -F ', ' 'NR == 1 {next} FNR==NR {if (maxP < $2) maxP=$2; if (minD=="" || minD > $3) minD=$3; next} FNR <= 2 || ($2 >= (.5 * maxP) && $3 <= (.5 * minD))' file{,}
ID, POP, dG
1, 10, -5.6200
8, 5, -4.4500
10, 8, -4.1400
13, 5, -4.0100
16, 10, -3.8800
To make it more readable:
awk -F ', ' '
NR == 1 {next} # skip 1st record 1st time
FNR == NR {
if (maxP < $2) # compute max(POP)
maxP = $2
if (minD == "" || minD > $3) # compute min(dG)
minD = $3
next
}
# print if 1st 2 lines OR "$2 >= .5 * max(POP) && $3 <= .5 * min(dG)"
FNR <= 2 || ($2 >= (.5 * maxP) && $3 <= (.5 * minD))
' file{,}

Extract the minimum positive value of each row in a matrix?

I have a matrix like this
A =
[0, 0, 0, 1, 3, 7, NA;
0, 0, 3, 5, 7, NA, NA;
0, 2, 3, 4, 5, 6, NA;
0, 0, 4, 5, 6, 7, NA;]
I want to extract the minimum values in each row of the matrix A that is greater than 0 into a vector B:
B = [1;3;2;4]
Any suggestion?
Thank you very much.
A(A<=0)=NA;
B=min(A, [], 2)
As suggested by Matt I'll explain this a little bit. Because you don't want results <=0 I set them to NA. You already have some in your data and the "min" operation will ignore them.
In the second step we search the minimum in the rows (2. dimension).