Jinja2 indentation on every line of multiline substitute - jinja2

Say I have a template:
<tag>
{{ x }}
</tag>
and I set x equal to:
1
2
3
4
5
I get:
<tag>
1
2
3
4
5
</tag>
How should I change the template to get:
<tag>
1
2
3
4
5
</tag>
?
Thank you.

Related

Identify the empty line in text file and loop over with that list in tcl

I have a file which has the following kind of data
A 1 2 3
B 2 2 2
c 2 4 5
d 4 5 6
From the above file I want to execute a loop like ,
three iteration where first iteration will have A,B elements 2nd iteration with c elements and 3rd with d. so that my html table will look like
Week1 | week2 | week3
----------------------------
A 1 2 3 | c 2 4 5 | d 4 5 6
B 2 2 2
I found this in SO catch multiple empty lines in file in tcl but I'm not getting what I exactly want.
I would suggest using arrays:
# Counter
set week 1
# Create file channel
set file [open filename.txt r]
# Read file contents line by line and store the line in the varialbe called $line
while {[gets $file line] != -1} {
if {$line != ""} {
# if line not empty, add line to current array with counter $week
lappend Week($week) $line
} else {
# else, increment week number
incr week
}
}
# close file channel
close $file
# print Week array
parray Week
# Week(1) = {A 1 2 3} {B 2 2 2}
# Week(2) = {c 2 4 5}
# Week(3) = {d 4 5 6}
ideone demo

What is the color palette used by igraph?

My reproducible example is the following:
get.vertex.attribute(g)
$name
[1] "LV" "Ve" "Ca" "Ai" "BN" "EN" "Or" "So" "SG" "Bo" "AX" "Sa" "To" "Pe" "Da" "He" "VI" "Ke" "Va" "At" "Ac" "Mi"
[23] "Cr" "Le" "Pu" "Re" "Te" "C." "N." "Y." "M." "D." "F." "L." "P." "S." "B." "J." "I." "A." "H." "R." "E." "O."
$color
[1] 1 1 1 1 1 2 3 1 1 3 1 3 3 3 1 4 3 5 3 1 1 6 2 6 1 3 3 1 1 1 1 3 1 2 3 1 5 1 2 3 3 4 3 6
In my case, the following code:
library("igraph")
vertices<-data.frame("name" = unique(unlist(relations)))
g = graph.data.frame(relations, directed=F, vertices=vertices)
vertices$group = edge.betweenness.community(g)$membership
V(g)$color <- vertices$group
plot(g,layout=layout.auto,vertex.size=6, vertex.label.cex = 0.8)
gives this graph:
where the color 1 seems to be orange, 2 is light blue, etc...
yet
palette()
[1] "black" "red" "green3" "blue" "cyan" "magenta" "yellow" "gray"
>
So what is the color palette used by igraph?
I am curious because I would like to use it in another package that only takes names of colors as input and doesn't seem to recognize the V(g)$color vector as a candidate for input (ie outputs only black).
The short answer is categorical_pal(8).
Full Story
If you look at the help page ?igraph.plotting and search on palette you will find.
palette
The color palette to use for vertex color. The default is
categorical_pal, which is a color-blind friendly categorical palette.
See its manual page for details and other palettes.
The help page ?categorical_pal says:
This is a color blind friendly palette from
http://jfly.iam.u-tokyo.ac.jp/color. It has 8 colors.
We can make a quick demonstration of this.
library(igraph)
x = 1:8
y = rep(1,8)
plot(x,y, pch=20, cex=10, col=categorical_pal(8), xlim=c(0.5,8.5))

Julia: Detect and remove duplicate rows from array?

What is the best method of detecting and dropping duplicate rows from an array in Julia?
x = Integer.(round.(10 .* rand(1000,4)))
# In R I would apply the duplicated function.
x = x[duplicated(x),:]
unique is what you are looking for: (this does not answer the question for the detection part.)
julia> x = Integer.(round.(10 .* rand(1000,4)))
1000×4 Array{Int64,2}:
7 3 10 1
7 4 8 9
7 7 3 0
3 4 8 2
⋮
julia> unique(x, 1)
973×4 Array{Int64,2}:
7 3 10 1
7 4 8 9
7 7 3 0
3 4 8 2
⋮
As for the detection part, a dirty fix would be editing this line:
#nref $N A d->d == dim ? sort!(uniquerows) : (indices(A, d))
to:
(#nref $N A d->d == dim ? sort!(uniquerows) : (indices(A, d))), uniquerows
Alternatively, you could define your own unique2 with abovementioned changes:
using Base.Cartesian
import Base.Prehashed
#generated function unique2(A::AbstractArray{T,N}, dim::Int) where {T,N}
......
end
julia> y, idx = unique2(x, 1)
julia> y
960×4 Array{Int64,2}:
8 3 1 5
8 3 1 6
1 1 0 1
8 10 1 10
9 1 8 7
⋮
julia> setdiff(1:1000, idx)
40-element Array{Int64,1}:
99
120
132
140
216
227
⋮
The benchmark on my machine is:
x = rand(1:10,1000,4) # 48 dups
#btime unique2($x, 1);
124.342 μs (31 allocations: 145.97 KiB)
#btime duplicated($x);
407.809 μs (9325 allocations: 394.78 KiB)
x = rand(1:4,1000,4) # 751 dups
#btime unique2($x, 1);
66.062 μs (25 allocations: 50.30 KiB)
#btime duplicated($x);
222.337 μs (4851 allocations: 237.88 KiB)
The result shows that the convoluted-metaprogramming-hashtable way in Base benefits a lot from lower memory allocation.
Julia v1.4 and above you would need to type unique(a, dims=1) where a is your N by 2 Array
julia> a=[2 2 ; 2 2; 1 2; 3 1]
4×2 Array{Int64,2}:
2 2
2 2
1 2
3 1
julia> unique(a,dims=1)
3×2 Array{Int64,2}:
2 2
1 2
3 1
You can also go with:
duplicated(x) = foldl(
(d,y)->(x[y,:] in d[1] ? (d[1],push!(d[2],y)) : (push!(d[1],x[y,:]),d[2])),
(Set(), Vector{Int}()),
1:size(x,1))[2]
This collects a set of seen rows, and outputs the indices of those already seen. This is essentially the minimal effort needed to get the result, so it should be fast.
julia> x = rand(1:2,5,2)
5×2 Array{Int64,2}:
2 1
1 2
1 2
1 1
1 1
julia> duplicated(x)
2-element Array{Int64,1}:
3
5
julia> x[duplicated(x),:]
2×2 Array{Int64,2}:
1 2
1 1

Iterating through CSV reader to slice data frame

I have a data frame that contains 508383 rows. I am only showing the first 10 row.
0 1 2
0 chr3R 4174822 4174922
1 chr3R 4175400 4175500
2 chr3R 4175466 4175566
3 chr3R 4175521 4175621
4 chr3R 4175603 4175703
5 chr3R 4175619 4175719
6 chr3R 4175692 4175792
7 chr3R 4175889 4175989
8 chr3R 4175966 4176066
9 chr3R 4176044 4176144
I want to iterate through each row and check the value of column #2 of the first row to the value of the next row. I want to check if the difference between these values is less than 5000. If the difference is greater than 5000 then I want to slice the data frame from the first row to the previous row and have this be a subset data frame.
I then want to repeat this process and create a second subset data frame. I've only manage to get this done by using CSV reader in combination with Pandas.
Here is my code:
#!/usr/bin/env python
import pandas as pd
data = pd.read_csv('sort_cov_emb_sg.bed', sep='\t', header=None, index_col=None)
import csv
file = open('sort_cov_emb_sg.bed')
readCSV = csv.reader(file, delimiter="\t")
first_row = readCSV.next()
print first_row
count_1 = 0
while count_1 < 100000:
next_row = readCSV.next()
value_1 = int(next_row[1]) - int(first_row[1])
count_1 = count_1 + 1
if value_1 < 5000:
continue
else:
break
print next_row
print count_1
print value_1
window_1 = data[0:63]
print window_1
first_row = readCSV.next()
print first_row
count_2 = 0
while count_2 < 100000:
next_row = readCSV.next()
value_2 = int(next_row[1]) - int(first_row[1])
count_2 = count_2 + 1
if value_2 < 5000:
continue
else:
break
print next_row
print count_2
print value_2
window_2 = data[0:74]
print window_2
I wanted to know if there is a better way to do this process )without repeating the code every time) and get all the subset data frames I need.
Thanks.
Rodrigo
This is yet another example of the compare-cumsum-groupby pattern. Using only rows you showed (and so changing the diff to 100 instead of 5000):
jumps = df[2] > df[2].shift() + 100
grouped = df.groupby(jumps.cumsum())
for k, group in grouped:
print(k)
print(group)
produces
0
0 1 2
0 chr3R 4174822 4174922
1
0 1 2
1 chr3R 4175400 4175500
2 chr3R 4175466 4175566
3 chr3R 4175521 4175621
4 chr3R 4175603 4175703
5 chr3R 4175619 4175719
6 chr3R 4175692 4175792
2
0 1 2
7 chr3R 4175889 4175989
8 chr3R 4175966 4176066
9 chr3R 4176044 4176144
This works because the comparison gives us a new True every time a new group starts, and when we take the cumulative sum of that, we get what is effectively a group id, which we can group on:
>>> jumps
0 False
1 True
2 False
3 False
4 False
5 False
6 False
7 True
8 False
9 False
Name: 2, dtype: bool
>>> jumps.cumsum()
0 0
1 1
2 1
3 1
4 1
5 1
6 1
7 2
8 2
9 2
Name: 2, dtype: int32

what are the elements to feed join command?

I am confused at "join" command, here is my code inside tclsh:
% lappend aaa 1
1
% lappend aaa 2
1 2
% lappend aaa {3 4}
1 2 {3 4}
% join $aaa
1 2 3 4
so the list aaa should have 3 elements: 1, 2 and {3 4}. Because "join" command should just concat all the elements together to form a string, then the return should be "1 2 {3 4}" because the list has just 3 elements. Why does join command break the 3rd element?
It doesn't. The 3rd element is a string 3 4 -- the braces you see are not actually part of the string. You're joining with a space, so the space in the string is not visually different. An example:
% lappend aaa 1
1
% lappend aaa 2
1 2
% lappend aaa {3 4}
1 2 {3 4}
% lappend aaa 5
1 2 {3 4} 5
% join $aaa :
1:2:3 4:5