问:
hggit doesn't export unmerged branches
I'm using hggit to export our mercurial repository to git. I'm following the tutorial here: http://arr.gr/blog/2011/10/bitbucket-converting-hg-repositories-to-git/
This however misses all branches that were not merged into default. I guess it's beacuse of the single bookmark (hg bookmark -r default master). Do I need to bookmark each open branch separately? Can hggit somehow pick all open branches (as we have possibly many of those)?</p>

答:
It is worth noting that, whilst the hggit branch_bookmark_suffix will convert Mercurial bookmarks with that suffix into Git branches without that suffix, it does not explain that this does not apply to Mercurial named branches directly. Git branches are different from Mercurial branches, and are more akin to Mercurial bookmarks, except that they are shared in the repository, rather than just being local.

In order for hggit branch_bookmark_suffix to apply to Mercurial named branches, these need to have Mercurial bookmarks with the appropriate suffix to be converted to Git branches.

For example, in addition to adding hggit to the hgrc:

```<code>[extensions]
hgext.bookmarks =
hggit =

[git]
branch_bookmark_suffix = _bookmark
</code>```

One also needs to add a Mercurial (local) bookmark for each named branch.
For example, the following bash script can be used to do this - note this handles the Mercurial default branch separately, so that it corresponds to the Git master branch.

```<code>#!/bin/bash

BRANCHES=`hg branches`

for BRANCH in ${BRANCHES[@]}; do
    if [[ ! "$BRANCH" =~ ^([0-9]+:)|(inactive)|(closed) ]];
    then
        echo $BRANCH
        if [[ "$BRANCH" =~ "default" ]];
        then
            `hg bookmark -r "${BRANCH}" "master"`
        else
            `hg bookmark -r "${BRANCH}" "${BRANCH}_bookmark"`
        fi
    fi
done
</code>```

Title: hggit doesn't export unmerged branches

Body:
It is worth noting that, whilst the hggit branch_bookmark_suffix will convert Mercurial bookmarks with that suffix into Git branches without that suffix, it does not explain that this does not apply to Mercurial named branches directly. Git branches are different from Mercurial branches, and are more akin to Mercurial bookmarks, except that they are shared in the repository, rather than just being local.

In order for hggit branch_bookmark_suffix to apply to Mercurial named branches, these need to have Mercurial bookmarks with the appropriate suffix to be converted to Git branches.

For example, in addition to adding hggit to the hgrc:

[extensions]
hgext.bookmarks =
hggit =

[git]
branch_bookmark_suffix = _bookmark

One also needs to add a Mercurial (local) bookmark for each named branch.
For example, the following bash script can be used to do this - note this handles the Mercurial default branch separately, so that it corresponds to the Git master branch.

#!/bin/bash

BRANCHES=`hg branches`

for BRANCH in ${BRANCHES[@]}; do
    if [[ ! "$BRANCH" =~ ^([0-9]+:)|(inactive)|(closed) ]];
    then
        echo $BRANCH
        if [[ "$BRANCH" =~ "default" ]];
        then
            `hg bookmark -r "${BRANCH}" "master"`
        else
            `hg bookmark -r "${BRANCH}" "${BRANCH}_bookmark"`
        fi
    fi
done


来源: stackexchange

问:
Round DateTime to closest quarter hour
How can I round a DateTime object to the closest quarter hour?

So: 2011-05-30 09:11:00 will be rounded to 2011-05-30 09:15:00, 2011-05-30 09:47:00 will be rounded to 2011-05-30 09:45:00 and so on.

答:
You could extract the minutes divide them by 15, round them and multiply them by 15 again.
Something like</p>

```<code>// round the "minutes"
$quarter = round(date('i', $yourdate) / 15) * 15;
// get the new timestamp
$roundeddate = mktime(date("H",$yourdate), $quarter, date("s",$yourdate), date("n",$yourdate), date("j",$yourdate), date("Y",$yourdate));
</code>```

ContentLicense: CC BY-SA 3.0

Title: Round DateTime to closest quarter hour

Body:
You could extract the minutes divide them by 15, round them and multiply them by 15 again.
Something like

// round the "minutes"
$quarter = round(date('i', $yourdate) / 15) * 15;
// get the new timestamp
$roundeddate = mktime(date("H",$yourdate), $quarter, date("s",$yourdate), date("n",$yourdate), date("j",$yourdate), date("Y",$yourdate));


问:
(Java) How to store selected elements from a list into array
I have a list of elements:

```<code>private List<Track> selection;
</code>```

And I want to implement a function public Track[] selection()

which returns an array of selected tracks. How to implement this?

I was going to implement this: Swings: storing the selected values from List into an array. method, however it applies only for known number of elements.

EDIT: Sorry to ask such a newbie question, but I tried any possible ways previously and nothing worked. OOP is new for me, and where else to find answers for questions if not here? ^_^

答:
Assuming the selection is a global value

```<code>public Track[] selection()
{
    return selection.toArray(new Track[0]);
}
</code>```

Else, you can pass the selection List as an argument to the method;

```<code>public Track[] selection(List<Track> input)
{
    return input.toArray(new Track[0]);
}
</code>```

and then

```<code>Track[] result = selection( selectionList ) // List selection
</code>```

ContentLicense: CC BY-SA 4.0

Body:
I have a list of elements:

private List<Track> selection;

And I want to implement a function public Track[] selection()

which returns an array of selected tracks. How to implement this?

I was going to implement this: Swings: storing the selected values from List into an array. method, however it applies only for known number of elements.

EDIT: Sorry to ask such a newbie question, but I tried any possible ways previously and nothing worked. OOP is new for me, and where else to find answers for questions if not here? ^_^

Body:
Assuming the selection is a global value

public Track[] selection()
{
    return selection.toArray(new Track[0]);
}

Else, you can pass the selection List as an argument to the method;

public Track[] selection(List<Track> input)
{
    return input.toArray(new Track[0]);
}

and then

Track[] result = selection( selectionList ) // List selection


问:
How to associate and filter two related arrays?
I have two arrays and I want to link together when processing them.

```<code>$dat = array(
    "2020-02-01",
    "2020-02-05",
    "2020-02-10",
    "2020-02-12",
    "2020-02-15"
);
$word = array(
    "Attend To,Explore,Unaided,dull,bad"
);

//User input
$start = "2020-01-01";
$end = "2020-02-07";
</code>```

I want the input to affect the second array too, so when the first array gets its result from first 2, then the second array too should have it from the first 2.

```<code>//Filter out dates between start and end date
$result = array_filter($dat, function($data_item) use($start, $end) {
    return $data_item >= $start && $data_item <= $end;
});
</code>```

and the result is

```<code>Array
(
    [0] => 2020-02-01
    [1] => 2020-02-05
)
</code>```

I want it to be able to link $dat and $word so that the result for word too will be

```<code>Array
(
    [0] => Attend To
    [1] => Explore
)
</code>```

答:
I don't find functional programming to be very readable/attractive for this case.  Just use a simple foreach loop and conditionally grab the words related by the shared indices.

Since the two arrays share common indices, it is unnecessary work to combine the two arrays -- just reference the indices.

Code: (Demo)

```<code>$dat = ["2020-02-01", "2020-02-05", "2020-02-10", "20-02-12", "2020-02-15"];
$word = ["Attend To,Explore,Unaided,dull,bad"];
$words = explode(',', $word[0]);

//User input
$start = "2020-01-01";
$end = "2020-02-07";

$result = [];
foreach ($dat as $index => $date) {
    if ($date >= $start && $date <= $end) {
        $result[] = $words[$index];
    }
}
var_export($result);
</code>```

Output:

```<code>array (
  0 => 'Attend To',
  1 => 'Explore',
)
</code>```

Body:
I have two arrays and I want to link together when processing them.

$dat = array(
    "2020-02-01",
    "2020-02-05",
    "2020-02-10",
    "2020-02-12",
    "2020-02-15"
);
$word = array(
    "Attend To,Explore,Unaided,dull,bad"
);

//User input
$start = "2020-01-01";
$end = "2020-02-07";

I want the input to affect the second array too, so when the first array gets its result from first 2, then the second array too should have it from the first 2.

//Filter out dates between start and end date
$result = array_filter($dat, function($data_item) use($start, $end) {
    return $data_item >= $start && $data_item <= $end;
});

and the result is

Array
(
    [0] => 2020-02-01
    [1] => 2020-02-05
)

I want it to be able to link $dat and $word so that the result for word too will be

Array
(
    [0] => Attend To
    [1] => Explore
)

Body:
I don't find functional programming to be very readable/attractive for this case.  Just use a simple foreach loop and conditionally grab the words related by the shared indices.

Since the two arrays share common indices, it is unnecessary work to combine the two arrays -- just reference the indices.

Code: (Demo)

$dat = ["2020-02-01", "2020-02-05", "2020-02-10", "20-02-12", "2020-02-15"];
$word = ["Attend To,Explore,Unaided,dull,bad"];
$words = explode(',', $word[0]);

//User input
$start = "2020-01-01";
$end = "2020-02-07";

$result = [];
foreach ($dat as $index => $date) {
    if ($date >= $start && $date <= $end) {
        $result[] = $words[$index];
    }
}
var_export($result);

Output:

array (
  0 => 'Attend To',
  1 => 'Explore',
)


问:
Message 'src refspec master does not match any' when pushing commits in Git
I clone my repository with:

```<code>git clone ssh://xxxxx/xx.git
</code>```

But after I change some files and add and commit them, I want to push them to the server:

```<code>git add xxx.php
git commit -m "TEST"
git push origin master
</code>```

But the error I get back is:

error: src refspec master does not match any.
error: failed to push some refs to 'ssh://xxxxx.com/project.git'
```

答:
This could also happen if you have a typo in the name of your branch and therefore you're trying to push something that doesn't really exists! ha!

In my case, I actually created the branch with an incorrect name (by accident, obviously) and it was called fetaure/<something> and was trying to push feature/<something>

The brain will read the word just as a unit and therefore sometimes both words look the same but they're not! ha!

Just be sure you don't have a typo in the name of the branch! (or better said, check that the branch you want to push actually matches the branch you have defined locally, specially if you are manually doing the first push of a newly created branch git push origin feature/<newBranch>)

Body:
I clone my repository with:

git clone ssh://xxxxx/xx.git

But after I change some files and add and commit them, I want to push them to the server:

git add xxx.php
git commit -m "TEST"
git push origin master

But the error I get back is:

error: src refspec master does not match any.
error: failed to push some refs to 'ssh://xxxxx.com/project.git'


问:
Django-REST-Framework REST API within django app
I'm wondering if there is a way to access REST API within the Django app.

I created a REST API using the Django rest framework. In the same project, I'm having another app in which I want to fetch data using my own rest API.

答:
If you want to fetch that data on the client side you need to use and make an http request in javascript.

If you want to fetch that data on the server side you already have the data, no need to fetch from the api, if you really need to do that there is the requests module.


问:
streaming a bitmap while it is modified in C++
What I am trying to do is use some process to alter the contents of a bitmap image ( dimensions and everything else is kept constant). While I run a process to perform transformations on it, I want to concurrently stream the image to my screen as it happens. So it's basically video without a series of images, but rather the same image in different stages of a transformation.
I am trying to do this because the bitmap I want to do this with is large, so actually saving each state as its own image and then combining will not be possible due to memory constraints.

My question is regarding the "live image display" while another process modifies the image, is there any way something like this can be done with a bitmap image?

答:
If you're just trying to see the results real-time, you can use something similar to page flipping / double buffering. </p>

<p>Basically: create two images. Image 1 is a "drawing buffer" while Image 2 is a "display buffer." Perform some portion of your drawing to image 1. Then, copy its complete contents to image 2. Continue drawing to image 1, copy, draw, copy, draw... etc. Insert a delay before each copy. </p>

Body:
If you're just trying to see the results real-time, you can use something similar to page flipping / double buffering.

Basically: create two images. Image 1 is a "drawing buffer" while Image 2 is a "display buffer." Perform some portion of your drawing to image 1. Then, copy its complete contents to image 2. Continue drawing to image 1, copy, draw, copy, draw... etc. Insert a delay before each copy.


问:
Another "Retain, then Release" question
being a Cocoa/Obj-C newbie I am going through the "Cocoa Programming for Mac OS X" book by Aaron Hillegass and - leaving apart the fact that now we have also the chance to use GC to avoid all this reasoning - I am not sure I get the reason for some of those retains.

In particular in one of the examples Aaron gives as good programming practice:

```<code>- (void) setFoo:(NSCalendarDate *)x
{
    [x retain];
    [foo release];
    foo = x;
}
</code>```

I don't get the reason for retaining the x instance at the first line of the method:

```<code>[x retain];
</code>```

The scope of this instance is just the set method, right?
When exiting the method scope the x instance should be deallocated anyway no?
Besides, when assigning x to foo with:

```<code>foo = x;
</code>```

foo will be anyway pointing to x memory cells and will therefore increment the pointed object retain count, no? This should ensure the memory won't be deallocated.

So, what's the point? I am sure I am missing something, of course, but don't know what exactly.

Thanks,
Fabrizio

答:
Retain means: I will be needing this object to stay around, it must not be deallocated. If x wouldn't be retained, the following is likely to happen:

You assign x to foo, so foo now points to the address where your NSCalendarDate is. Someone releases or autoreleases this object, it's retain count eventually drops to 0 and the object is deallocated. Now your foo still points to that address, but there's no longer a valid object. Sometime later, a new object is created and by chance it's situated at the same address than your old NSCalendarDate object. Now your foo points to an entirely different object !

To prevent that, you need to retain it. You need to say, please do not deallocate the object yet, I need it. Once you're done with it, you release it which means I no longer need the object, you can clean it up now if nobody else needs it.

Now for the classical three part assignment. Consider your setFoo: would look like this:

```<code>- (void) setFoo:(NSCalendarDate *)x
{
    [foo release];
    [x retain];
    foo = x;
}
</code>```

This is a very bad idea. Consider your object is the only one who has retained the NSCalendarDate object, and consider you would then do: [self setFoo:foo];. Might sound silly, but something like this can happen. The flow would now be this:

foo would be released. Its retain count might now drop to 0 and the object will get deallocated.

Whoops, we're trying to retain and access a deallocated object.

This is why you always first retain the new object, then release the old object.

If you're coming from a Java or .NET background, it is very important to understand that a variable of type Foo * only contains the address of your object, nothing more. In Java or .NET, a variable that points to an object automatically "retains" it, if you will. Not so in Objective-C (in non-GC environments). You could consider a variable of type Foo * to be a weak reference, and you explicitly need to tell Objective-C whether you will still need that object at that address or not.

Title: Another "Retain, then Release" question

Body:
being a Cocoa/Obj-C newbie I am going through the "Cocoa Programming for Mac OS X" book by Aaron Hillegass and - leaving apart the fact that now we have also the chance to use GC to avoid all this reasoning - I am not sure I get the reason for some of those retains.

In particular in one of the examples Aaron gives as good programming practice:

- (void) setFoo:(NSCalendarDate *)x
{
    [x retain];
    [foo release];
    foo = x;
}

I don't get the reason for retaining the x instance at the first line of the method:

[x retain];

The scope of this instance is just the set method, right?
When exiting the method scope the x instance should be deallocated anyway no?
Besides, when assigning x to foo with:

foo = x;

foo will be anyway pointing to x memory cells and will therefore increment the pointed object retain count, no? This should ensure the memory won't be deallocated.

So, what's the point? I am sure I am missing something, of course, but don't know what exactly.

Thanks,
Fabrizio

Body:
Retain means: I will be needing this object to stay around, it must not be deallocated. If x wouldn't be retained, the following is likely to happen:

You assign x to foo, so foo now points to the address where your NSCalendarDate is. Someone releases or autoreleases this object, it's retain count eventually drops to 0 and the object is deallocated. Now your foo still points to that address, but there's no longer a valid object. Sometime later, a new object is created and by chance it's situated at the same address than your old NSCalendarDate object. Now your foo points to an entirely different object !

To prevent that, you need to retain it. You need to say, please do not deallocate the object yet, I need it. Once you're done with it, you release it which means I no longer need the object, you can clean it up now if nobody else needs it.

Now for the classical three part assignment. Consider your setFoo: would look like this:

- (void) setFoo:(NSCalendarDate *)x
{
    [foo release];
    [x retain];
    foo = x;
}

This is a very bad idea. Consider your object is the only one who has retained the NSCalendarDate object, and consider you would then do: [self setFoo:foo];. Might sound silly, but something like this can happen. The flow would now be this:

foo would be released. Its retain count might now drop to 0 and the object will get deallocated.

Whoops, we're trying to retain and access a deallocated object.

This is why you always first retain the new object, then release the old object.

If you're coming from a Java or .NET background, it is very important to understand that a variable of type Foo * only contains the address of your object, nothing more. In Java or .NET, a variable that points to an object automatically "retains" it, if you will. Not so in Objective-C (in non-GC environments). You could consider a variable of type Foo * to be a weak reference, and you explicitly need to tell Objective-C whether you will still need that object at that address or not.


问:
Mongo Change Streams running multiple times (kind of): Node app running multiple instances
My Node app uses Mongo change streams, and the app runs 3+ instances in production (more eventually, so this will become more of an issue as it grows). So, when a change comes in the change stream functionality runs as many times as there are processes.

How to set things up so that the change stream only runs once?

Here's what I've got:

```<code>const options = { fullDocument: "updateLookup" };

const filter = [
  {
    $match: {
      $and: [
        { "updateDescription.updatedFields.sites": { $exists: true } },
        { operationType: "update" }
      ]
    }
  }
];

const sitesStream = Client.watch(sitesFilter, options);

// Start listening to site stream
sitesStream.on("change", async change => {
  console.log("in site change stream", change);
  console.log(
    "in site change stream, update desc",
    change.updateDescription
  );

  // Do work...
  console.log("site change stream done.");
  return;
});
</code>```

答:
Doing this with strong guarantees is difficult but not impossible. I wrote about the details of one solution here: https://www.alechenninger.com/2020/05/building-kafka-like-message-queue-with.html

The examples are in Java but the important part is the algorithm.

It comes down to a few techniques:

Each process attempts to obtain a lock

Each lock (or each change) has an associated fencing token

Processing each change must be idempotent

While processing the change, the token is used to ensure ordered, effectively-once updates.

More details in the blog post.

Body:
My Node app uses Mongo change streams, and the app runs 3+ instances in production (more eventually, so this will become more of an issue as it grows). So, when a change comes in the change stream functionality runs as many times as there are processes.

How to set things up so that the change stream only runs once?

Here's what I've got:

const options = { fullDocument: "updateLookup" };

const filter = [
  {
    $match: {
      $and: [
        { "updateDescription.updatedFields.sites": { $exists: true } },
        { operationType: "update" }
      ]
    }
  }
];

const sitesStream = Client.watch(sitesFilter, options);

// Start listening to site stream
sitesStream.on("change", async change => {
  console.log("in site change stream", change);
  console.log(
    "in site change stream, update desc",
    change.updateDescription
  );

  // Do work...
  console.log("site change stream done.");
  return;
});


问:
Generate Neighbour List object for spatial lines in R
My goal is to generate an object of neighbourhood relationships between spatial lines in a road network. If my data were spatial polygons I could use spdep::poly2nb to do this, but I'm having trouble working out how to do this for spatial lines.

In the reprex below I try using igraph::as_adjacency_matrix create an adjacency matrix and then convert that to a neighbours list object using spdep::mat2listw. But is this the correct way to go?

Once I have the Neighours list I also want to label with the road_id attribute.

```<code>library(sfnetworks)
library(sf)
library(spdep)
library(igraph)

net <- as_sfnetwork(roxel, directed = FALSE) %>%
  activate("edges") %>%
  mutate(road_id = row_number()+1000)

# Make adjacency matrix
B_net <- igraph::as_adjacency_matrix(net, edges = TRUE, attr = names)

# Make neighbour list
nb_net <- mat2listw(B_net)$neighbours
# Can't use row.names in mat2listw because how do I set row.names in igraph::as_adjacency_matrix

</code>```

EDIT: New approach using methof in this sfnetworks issue https://github.com/luukvdmeer/sfnetworks/issues/10 gives a neighbour list.

```<code>net_sf <- st_as_sf(net)

net_neigh <- st_touches(net_sf)

# define ad-hoc function to translate sgbp into nb (as documented in
# https://r-spatial.github.io/spdep/articles/nb_sf.html#creating-neighbours-using-sf-objects)
as.nb.sgbp <- function(x) {
  attrs <- attributes(x)
  x <- lapply(x, function(i) { if(length(i) == 0L) 0L else i } )
  attributes(x) <- attrs
  class(x) <- "nb"
  x
}

net_nb <- as.nb.sgbp(net_neigh)
net_lw <- nb2listw(net_nb)
</code>```

答:
IMO there are a few approaches for creating a neighborhood list object for spatial lines similar to the output of spdep::poly2nb. Unfortunately there are also a few assumptions (analogous to the queen parameter of spdep::poly2nb) that I will try to explain here.

First we load some packages and data

library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(sfnetworks)
library(igraph)
#>
#> Attaching package: 'igraph'
#> The following objects are masked from 'package:stats':
#>
#>     decompose, spectrum
#> The following object is masked from 'package:base':
#>
#>     union
```

<p>The first solution is based on the <code>sf::st_touches</code> function: </p>

<pre class="lang-r prettyprint-override"><code>roxel_touches <- stplanr::geo_projected(roxel, st_touches)
roxel_touches
#> Sparse geometry binary predicate list of length 851, where the predicate was `touches'
#> first 10 elements:
#>  1: 101, 146, 493, 577, 742
#>  2: 149, 285, 294, 354, 521
#>  3: 268, 269, 270, 376, 431
#>  4: 8, 9, 276, 735
#>  5: 8, 94, 95, 108, 590
#>  6: 272, 728, 732
#>  7: 274, 275, 276, 734
#>  8: 4, 5, 95, 108, 735
#>  9: 4, 276, 281, 728
#>  10: 273, 281, 729, 733
</code>```

The output is an sgbp object that summarizes the relationships between the spatial lines according to a touches predicate. For example the first line of the output means that the first line of roxel object touches the lines 101, 146, 493, 577 and 742. The stplanr::geo_projected function is used to apply a geometric binary predicate function without reprojecting the data (otherwise we would have get some warning message).

The touches predicate is defined in the corresponding help page (?sf::st_touches) and also here. Briefly, the sf::st_touches function matches the LINESTRING geometries that share one common point when that point lies in the union of their boundaries (i.e. the first and last point of the line). For example, the following lines intersect each other but they are not touching since the common point do not lie in their boundaries.

es_1 <- st_sfc(
  st_linestring(rbind(c(0, -1), c(0, 1))),
  st_linestring(rbind(c(-1, 0), c(1, 0)))
)
par(mar = rep(1, 4))
plot(es_1, col = c("red", "blue"), lwd = 2)
legend("topright", legend = c(1, 2), col = c("red", "blue"), lty = 1, lwd = 2)
```

<p><img src="https://i.imgur.com/u2n1t9U.png" alt=""></p>

<pre class="lang-r prettyprint-override"><code>st_intersects(es_1)
#> Sparse geometry binary predicate list of length 2, where the predicate was `intersects'
#>  1: 1, 2
#>  2: 1, 2
st_touches(es_1)
#> Sparse geometry binary predicate list of length 2, where the predicate was `touches'
#>  1: (empty)
#>  2: (empty)
</code>```

Another, slightly different, solution is created using the sf::st_relate function:

roxel_relate <- stplanr::geo_projected(roxel, st_relate, pattern = "FF*FT****")
roxel_relate
#> Sparse geometry binary predicate list of length 851, where the predicate was `relate_pattern'
#> first 10 elements:
#>  1: 101, 146, 493, 577, 742
#>  2: 149, 285, 294, 354, 521
#>  3: 268, 269, 270, 376, 431
#>  4: 8, 9, 276, 735
#>  5: 8, 94, 95, 108, 590
#>  6: 272, 732
#>  7: 274, 275, 276, 734
#>  8: 4, 5, 95, 108, 735
#>  9: 4, 276, 281, 728
#>  10: 273, 281, 729, 733
```

<p>The output is (again) an <code>sgbp</code> object that summarizes the relationships between the spatial lines according to a "relate" predicate with pattern <code>"FF*FT****"</code>. That type of pattern is used to match <code>LINESTRINGS</code> if and only if they share at least one point in their boundaries, their interiors are not intersecting and there is no shared point between interiors and boundaries. For example the following two lines are touching but they are not related according to the pattern <code>"FF*FT****"</code>.</p>

<pre class="lang-r prettyprint-override"><code>es_2 <- st_sfc(
  st_linestring(rbind(c(0, -1), c(0, 1)), "red"),
  st_linestring(rbind(c(0, 0), c(1, 0)), "blue"),
  crs = 32632
)
plot(es_2, col = c("red", "blue"), lwd = 2)
legend(x = "topright", legend = c(1, 2), lty = 1, col = c("red", "blue"), lwd = 2)
</code>```

st_touches(es_2)
#> Sparse geometry binary predicate list of length 2, where the predicate was `touches'
#>  1: 2
#>  2: 1
st_relate(es_2, pattern = "FF*FT****")
#> Sparse geometry binary predicate list of length 2, where the predicate was `relate_pattern'
#>  1: (empty)
#>  2: (empty)
```

<p>You can check more details about <code>sf::st_relate</code> function and how to build patterns in the help page of <code>sf::st_relate</code>. We can understand why this distinction between <code>st_touches</code> and <code>st_relate</code> is important when we compare the output of the two predicates. </p>

<pre class="lang-r prettyprint-override"><code>roxel_touches
#> Sparse geometry binary predicate list of length 851, where the predicate was `touches'
#> first 10 elements:
#>  1: 101, 146, 493, 577, 742
#>  2: 149, 285, 294, 354, 521
#>  3: 268, 269, 270, 376, 431
#>  4: 8, 9, 276, 735
#>  5: 8, 94, 95, 108, 590
#>  6: 272, 728, 732
#>  7: 274, 275, 276, 734
#>  8: 4, 5, 95, 108, 735
#>  9: 4, 276, 281, 728
#>  10: 273, 281, 729, 733
roxel_relate
#> Sparse geometry binary predicate list of length 851, where the predicate was `relate_pattern'
#> first 10 elements:
#>  1: 101, 146, 493, 577, 742
#>  2: 149, 285, 294, 354, 521
#>  3: 268, 269, 270, 376, 431
#>  4: 8, 9, 276, 735
#>  5: 8, 94, 95, 108, 590
#>  6: 272, 732
#>  7: 274, 275, 276, 734
#>  8: 4, 5, 95, 108, 735
#>  9: 4, 276, 281, 728
#>  10: 273, 281, 729, 733
</code>```

We can see that they are almost identical but for a few cases (see, for example, the 6th row). We can understand the situation even better if we plot the corresponding lines:

plot(st_geometry(roxel[c(6, 272, 728, 732), ]), col = c("red", "orange", "blue", "darkgreen"), lwd = 2)
legend(x = "topright", legend = c(6, 272, 728, 732), lty = 1, col = c("red", "orange", "blue", "darkgreen"), lwd = 2)
```

<p><img src="https://i.imgur.com/Yj4R3B6.png" alt=""></p>

<p>We can see that the 6th line is touching all the other lines but it's not "related" to line 728 according to the pattern <code>"FF*FT****"</code>. This implies that if you create the neighbours list starting from <code>st_relate</code>, then you are missing (at least) one existing link. I explicitly chose that type of pattern since the default algorithm used by <code>sfnetworks</code> for inferring a graph structure from an <code>sf</code> object is very similar (maybe identical) to that type of pattern. We can check this fact creating the <code>line graph</code> associated to the graph created by <code>as_sfnetwork</code>:</p>

<pre class="lang-r prettyprint-override"><code>roxel_sfnetworks <- as_sfnetwork(roxel, directed = FALSE)
roxel_line_graph <- make_line_graph(roxel_sfnetworks)
roxel_adj_list <- as_adj_list(roxel_line_graph)
str(roxel_adj_list[1:10], give.attr = FALSE)
#> List of 10
#>  $ : 'igraph.vs' int [1:5] 101 146 493 577 742
#>  $ : 'igraph.vs' int [1:5] 149 285 294 354 521
#>  $ : 'igraph.vs' int [1:5] 268 269 270 376 431
#>  $ : 'igraph.vs' int [1:4] 8 9 276 735
#>  $ : 'igraph.vs' int [1:5] 8 94 95 108 590
#>  $ : 'igraph.vs' int [1:2] 272 732
#>  $ : 'igraph.vs' int [1:4] 274 275 276 734
#>  $ : 'igraph.vs' int [1:5] 4 5 95 108 735
#>  $ : 'igraph.vs' int [1:4] 4 276 281 728
#>  $ : 'igraph.vs' int [1:4] 273 281 729 733
</code>```

and test their equality:

all(unlist(mapply(function(x, y) unique(x) == unique(y), roxel_adj_list, roxel_relate)))
#> [1] TRUE
```

<p>I had to use the <code>unique</code> function since the <code>igraph</code> output may return duplicated indexes when there are multipled edges between two vertices, i.e.</p>

<pre class="lang-r prettyprint-override"><code>is.simple(roxel_sfnetworks)
#> [1] FALSE
</code>```

Anyway, we can see that the output is identical to roxel_relate, which means that we cannot always safely use st_relate or make_line_graph functions without particular considerations on the underlying street network object. I don't want to add too many details here since the examples are too complicated, but we cannot either always use the st_touches function for inferring the neighbour list object (but if you want to read a little bit more on this topic I recently wrote a paper on this topic, which is currently under review).

Anyway, the summary of that paper is that I think we can safely use st_touches or st_relate functions to generate the neighbour list only after transforming the roxel object using the stplanr::rnet_breakup_vertices function. (Actually there are still some really minor differences between the two approaches, but I still have to properly figure out how to solve them. For the moment I would use st_touches).

# 1 - Transform the input roxel object
roxel2 <- stplanr::rnet_breakup_vertices(roxel)
#> Splitting rnet object at the intersection points between nodes and internal vertexes

# 2- Apply st_touches function
roxel2_touches <- stplanr::geo_projected(roxel2, st_touches)
roxel2_touches
#> Sparse geometry binary predicate list of length 876, where the predicate was `touches'
#> first 10 elements:
#>  1: 105, 150, 504, 591, 765
#>  2: 153, 291, 300, 363, 533
#>  3: 274, 275, 276, 386, 441
#>  4: 8, 9, 282, 758
#>  5: 8, 98, 99, 112, 604
#>  6: 278, 750, 751, 755
#>  7: 280, 281, 282, 757
#>  8: 4, 5, 99, 112, 758
#>  9: 4, 282, 287, 750
#>  10: 279, 287, 752, 756
```

<p>Then, if you want, you can transform the result into an nb object: </p>

<pre class="lang-r prettyprint-override"><code>as.nb.sgbp <- function(x) {
  attrs <- attributes(x)
  x <- lapply(x, function(i) { if(length(i) == 0L) 0L else i } )
  attributes(x) <- attrs
  class(x) <- "nb"
  x
}

roxel2_nb <- as.nb.sgbp(roxel2_touches)
roxel2_lw <- spdep::nb2listw(roxel2_nb)
roxel2_lw
#> Characteristics of weights list object:
#> Neighbour list object:
#> Number of regions: 876
#> Number of nonzero links: 3318
#> Percentage nonzero weights: 0.4323826
#> Average number of links: 3.787671
#>
#> Weights style: W
#> Weights constants summary:
#>     n     nn  S0       S1       S2
#> W 876 767376 876 494.4606 3580.933
</code>```

We can also use the "line-graph" option:

str(as_adj_list(make_line_graph(as_sfnetwork(roxel2, directed = FALSE)))[1:10], give.attr = FALSE)
#> List of 10
#>  $ : 'igraph.vs' int [1:5] 105 150 504 591 765
#>  $ : 'igraph.vs' int [1:5] 153 291 300 363 533
#>  $ : 'igraph.vs' int [1:5] 274 275 276 386 441
#>  $ : 'igraph.vs' int [1:4] 8 9 282 758
#>  $ : 'igraph.vs' int [1:5] 8 98 99 112 604
#>  $ : 'igraph.vs' int [1:4] 278 750 751 755
#>  $ : 'igraph.vs' int [1:4] 280 281 282 757
#>  $ : 'igraph.vs' int [1:5] 4 5 99 112 758
#>  $ : 'igraph.vs' int [1:4] 4 282 287 750
#>  $ : 'igraph.vs' int [1:4] 279 287 752 756
```

Created on 2020-06-01 by the reprex package (v0.3.0)

I know that I wrote a lot of (maybe unnecessary) information but I think they are useful to properly give context about the suggested solution. If it's not clear please add more questions.

Body:
My goal is to generate an object of neighbourhood relationships between spatial lines in a road network. If my data were spatial polygons I could use spdep::poly2nb to do this, but I'm having trouble working out how to do this for spatial lines.

In the reprex below I try using igraph::as_adjacency_matrix create an adjacency matrix and then convert that to a neighbours list object using spdep::mat2listw. But is this the correct way to go?

Once I have the Neighours list I also want to label with the road_id attribute.

library(sfnetworks)
library(sf)
library(spdep)
library(igraph)

net <- as_sfnetwork(roxel, directed = FALSE) %>%
  activate("edges") %>%
  mutate(road_id = row_number()+1000)

# Make adjacency matrix
B_net <- igraph::as_adjacency_matrix(net, edges = TRUE, attr = names)

# Make neighbour list
nb_net <- mat2listw(B_net)$neighbours
# Can't use row.names in mat2listw because how do I set row.names in igraph::as_adjacency_matrix

EDIT: New approach using methof in this sfnetworks issue https://github.com/luukvdmeer/sfnetworks/issues/10 gives a neighbour list.

net_sf <- st_as_sf(net)

net_neigh <- st_touches(net_sf)

# define ad-hoc function to translate sgbp into nb (as documented in
# https://r-spatial.github.io/spdep/articles/nb_sf.html#creating-neighbours-using-sf-objects)
as.nb.sgbp <- function(x) {
  attrs <- attributes(x)
  x <- lapply(x, function(i) { if(length(i) == 0L) 0L else i } )
  attributes(x) <- attrs
  class(x) <- "nb"
  x
}

net_nb <- as.nb.sgbp(net_neigh)
net_lw <- nb2listw(net_nb)

Body:
IMO there are a few approaches for creating a neighborhood list object for spatial lines similar to the output of spdep::poly2nb. Unfortunately there are also a few assumptions (analogous to the queen parameter of spdep::poly2nb) that I will try to explain here.

First we load some packages and data

library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(sfnetworks)
library(igraph)
#>
#> Attaching package: 'igraph'
#> The following objects are masked from 'package:stats':
#>
#>     decompose, spectrum
#> The following object is masked from 'package:base':
#>
#>     union

The first solution is based on the sf::st_touches function:

roxel_touches <- stplanr::geo_projected(roxel, st_touches)
roxel_touches
#> Sparse geometry binary predicate list of length 851, where the predicate was `touches'
#> first 10 elements:
#>  1: 101, 146, 493, 577, 742
#>  2: 149, 285, 294, 354, 521
#>  3: 268, 269, 270, 376, 431
#>  4: 8, 9, 276, 735
#>  5: 8, 94, 95, 108, 590
#>  6: 272, 728, 732
#>  7: 274, 275, 276, 734
#>  8: 4, 5, 95, 108, 735
#>  9: 4, 276, 281, 728
#>  10: 273, 281, 729, 733

The output is an sgbp object that summarizes the relationships between the spatial lines according to a touches predicate. For example the first line of the output means that the first line of roxel object touches the lines 101, 146, 493, 577 and 742. The stplanr::geo_projected function is used to apply a geometric binary predicate function without reprojecting the data (otherwise we would have get some warning message).

The touches predicate is defined in the corresponding help page (?sf::st_touches) and also here. Briefly, the sf::st_touches function matches the LINESTRING geometries that share one common point when that point lies in the union of their boundaries (i.e. the first and last point of the line). For example, the following lines intersect each other but they are not touching since the common point do not lie in their boundaries.

es_1 <- st_sfc(
  st_linestring(rbind(c(0, -1), c(0, 1))),
  st_linestring(rbind(c(-1, 0), c(1, 0)))
)
par(mar = rep(1, 4))
plot(es_1, col = c("red", "blue"), lwd = 2)
legend("topright", legend = c(1, 2), col = c("red", "blue"), lty = 1, lwd = 2)

st_intersects(es_1)
#> Sparse geometry binary predicate list of length 2, where the predicate was `intersects'
#>  1: 1, 2
#>  2: 1, 2
st_touches(es_1)
#> Sparse geometry binary predicate list of length 2, where the predicate was `touches'
#>  1: (empty)
#>  2: (empty)

Another, slightly different, solution is created using the sf::st_relate function:

roxel_relate <- stplanr::geo_projected(roxel, st_relate, pattern = "FF*FT****")
roxel_relate
#> Sparse geometry binary predicate list of length 851, where the predicate was `relate_pattern'
#> first 10 elements:
#>  1: 101, 146, 493, 577, 742
#>  2: 149, 285, 294, 354, 521
#>  3: 268, 269, 270, 376, 431
#>  4: 8, 9, 276, 735
#>  5: 8, 94, 95, 108, 590
#>  6: 272, 732
#>  7: 274, 275, 276, 734
#>  8: 4, 5, 95, 108, 735
#>  9: 4, 276, 281, 728
#>  10: 273, 281, 729, 733

The output is (again) an sgbp object that summarizes the relationships between the spatial lines according to a "relate" predicate with pattern "FF*FT****". That type of pattern is used to match LINESTRINGS if and only if they share at least one point in their boundaries, their interiors are not intersecting and there is no shared point between interiors and boundaries. For example the following two lines are touching but they are not related according to the pattern "FF*FT****".

es_2 <- st_sfc(
  st_linestring(rbind(c(0, -1), c(0, 1)), "red"),
  st_linestring(rbind(c(0, 0), c(1, 0)), "blue"),
  crs = 32632
)
plot(es_2, col = c("red", "blue"), lwd = 2)
legend(x = "topright", legend = c(1, 2), lty = 1, col = c("red", "blue"), lwd = 2)

st_touches(es_2)
#> Sparse geometry binary predicate list of length 2, where the predicate was `touches'
#>  1: 2
#>  2: 1
st_relate(es_2, pattern = "FF*FT****")
#> Sparse geometry binary predicate list of length 2, where the predicate was `relate_pattern'
#>  1: (empty)
#>  2: (empty)

You can check more details about sf::st_relate function and how to build patterns in the help page of sf::st_relate. We can understand why this distinction between st_touches and st_relate is important when we compare the output of the two predicates.

roxel_touches
#> Sparse geometry binary predicate list of length 851, where the predicate was `touches'
#> first 10 elements:
#>  1: 101, 146, 493, 577, 742
#>  2: 149, 285, 294, 354, 521
#>  3: 268, 269, 270, 376, 431
#>  4: 8, 9, 276, 735
#>  5: 8, 94, 95, 108, 590
#>  6: 272, 728, 732
#>  7: 274, 275, 276, 734
#>  8: 4, 5, 95, 108, 735
#>  9: 4, 276, 281, 728
#>  10: 273, 281, 729, 733
roxel_relate
#> Sparse geometry binary predicate list of length 851, where the predicate was `relate_pattern'
#> first 10 elements:
#>  1: 101, 146, 493, 577, 742
#>  2: 149, 285, 294, 354, 521
#>  3: 268, 269, 270, 376, 431
#>  4: 8, 9, 276, 735
#>  5: 8, 94, 95, 108, 590
#>  6: 272, 732
#>  7: 274, 275, 276, 734
#>  8: 4, 5, 95, 108, 735
#>  9: 4, 276, 281, 728
#>  10: 273, 281, 729, 733

We can see that they are almost identical but for a few cases (see, for example, the 6th row). We can understand the situation even better if we plot the corresponding lines:

plot(st_geometry(roxel[c(6, 272, 728, 732), ]), col = c("red", "orange", "blue", "darkgreen"), lwd = 2)
legend(x = "topright", legend = c(6, 272, 728, 732), lty = 1, col = c("red", "orange", "blue", "darkgreen"), lwd = 2)

We can see that the 6th line is touching all the other lines but it's not "related" to line 728 according to the pattern "FF*FT****". This implies that if you create the neighbours list starting from st_relate, then you are missing (at least) one existing link. I explicitly chose that type of pattern since the default algorithm used by sfnetworks for inferring a graph structure from an sf object is very similar (maybe identical) to that type of pattern. We can check this fact creating the line graph associated to the graph created by as_sfnetwork:

roxel_sfnetworks <- as_sfnetwork(roxel, directed = FALSE)
roxel_line_graph <- make_line_graph(roxel_sfnetworks)
roxel_adj_list <- as_adj_list(roxel_line_graph)
str(roxel_adj_list[1:10], give.attr = FALSE)
#> List of 10
#>  $ : 'igraph.vs' int [1:5] 101 146 493 577 742
#>  $ : 'igraph.vs' int [1:5] 149 285 294 354 521
#>  $ : 'igraph.vs' int [1:5] 268 269 270 376 431
#>  $ : 'igraph.vs' int [1:4] 8 9 276 735
#>  $ : 'igraph.vs' int [1:5] 8 94 95 108 590
#>  $ : 'igraph.vs' int [1:2] 272 732
#>  $ : 'igraph.vs' int [1:4] 274 275 276 734
#>  $ : 'igraph.vs' int [1:5] 4 5 95 108 735
#>  $ : 'igraph.vs' int [1:4] 4 276 281 728
#>  $ : 'igraph.vs' int [1:4] 273 281 729 733

and test their equality:

all(unlist(mapply(function(x, y) unique(x) == unique(y), roxel_adj_list, roxel_relate)))
#> [1] TRUE

I had to use the unique function since the igraph output may return duplicated indexes when there are multipled edges between two vertices, i.e.

is.simple(roxel_sfnetworks)
#> [1] FALSE

Anyway, we can see that the output is identical to roxel_relate, which means that we cannot always safely use st_relate or make_line_graph functions without particular considerations on the underlying street network object. I don't want to add too many details here since the examples are too complicated, but we cannot either always use the st_touches function for inferring the neighbour list object (but if you want to read a little bit more on this topic I recently wrote a paper on this topic, which is currently under review).

Anyway, the summary of that paper is that I think we can safely use st_touches or st_relate functions to generate the neighbour list only after transforming the roxel object using the stplanr::rnet_breakup_vertices function. (Actually there are still some really minor differences between the two approaches, but I still have to properly figure out how to solve them. For the moment I would use st_touches).

# 1 - Transform the input roxel object
roxel2 <- stplanr::rnet_breakup_vertices(roxel)
#> Splitting rnet object at the intersection points between nodes and internal vertexes

# 2- Apply st_touches function
roxel2_touches <- stplanr::geo_projected(roxel2, st_touches)
roxel2_touches
#> Sparse geometry binary predicate list of length 876, where the predicate was `touches'
#> first 10 elements:
#>  1: 105, 150, 504, 591, 765
#>  2: 153, 291, 300, 363, 533
#>  3: 274, 275, 276, 386, 441
#>  4: 8, 9, 282, 758
#>  5: 8, 98, 99, 112, 604
#>  6: 278, 750, 751, 755
#>  7: 280, 281, 282, 757
#>  8: 4, 5, 99, 112, 758
#>  9: 4, 282, 287, 750
#>  10: 279, 287, 752, 756

Then, if you want, you can transform the result into an nb object:

as.nb.sgbp <- function(x) {
  attrs <- attributes(x)
  x <- lapply(x, function(i) { if(length(i) == 0L) 0L else i } )
  attributes(x) <- attrs
  class(x) <- "nb"
  x
}

roxel2_nb <- as.nb.sgbp(roxel2_touches)
roxel2_lw <- spdep::nb2listw(roxel2_nb)
roxel2_lw
#> Characteristics of weights list object:
#> Neighbour list object:
#> Number of regions: 876
#> Number of nonzero links: 3318
#> Percentage nonzero weights: 0.4323826
#> Average number of links: 3.787671
#>
#> Weights style: W
#> Weights constants summary:
#>     n     nn  S0       S1       S2
#> W 876 767376 876 494.4606 3580.933

We can also use the "line-graph" option:

str(as_adj_list(make_line_graph(as_sfnetwork(roxel2, directed = FALSE)))[1:10], give.attr = FALSE)
#> List of 10
#>  $ : 'igraph.vs' int [1:5] 105 150 504 591 765
#>  $ : 'igraph.vs' int [1:5] 153 291 300 363 533
#>  $ : 'igraph.vs' int [1:5] 274 275 276 386 441
#>  $ : 'igraph.vs' int [1:4] 8 9 282 758
#>  $ : 'igraph.vs' int [1:5] 8 98 99 112 604
#>  $ : 'igraph.vs' int [1:4] 278 750 751 755
#>  $ : 'igraph.vs' int [1:4] 280 281 282 757
#>  $ : 'igraph.vs' int [1:5] 4 5 99 112 758
#>  $ : 'igraph.vs' int [1:4] 4 282 287 750
#>  $ : 'igraph.vs' int [1:4] 279 287 752 756

Created on 2020-06-01 by the reprex package (v0.3.0)

I know that I wrote a lot of (maybe unnecessary) information but I think they are useful to properly give context about the suggested solution. If it's not clear please add more questions.


问:
Creating a Column Link in an Interactive Report -Oracle Apex
I am using Apex 20.1.0.00.13, trying to create link on a column of a Classic Report.

I have followed the step to create the link for a column and redirect it to a page in the same application as the steps below :

To create a link to another page, in the Link Builder - Target dialog:
  Type - Select Page in this Application.
  Page - Specify the target page number.
  Set Items - Select a Name and Value to specify session state for an item.
  Clear Session State, Clear Cache - Specify the page numbers on which to clear cache. To specify multiple page, enter a comma-delimited list of page numbers.
  Rest Pagination - Select Yes to reset pagination for this page.v
  Advanced, Request - Specify the request to be used.
  Click OK.

But when I am setting the item and values to specify session state for an item, the Set Items : Name doesn't populate the columns, it appears blank, Could someone let me know where I am going wrong.

I am trying to achieve a link from page 1 say a row with dept no - 2, to be directed to page 2 with details of dept 2 . Is there any other way to do it. Since its a column and not an item I am unable to pass it as a parameter to the next page.

Thanks in advance !

答: Probably you could change and merge these two pages into one Master-Detail page. This way you could use the built in lookup function.</p>


问:
Filter list items within a Django JSONField
Here is a simplified version of a Django model I'm working on.

```<code>class Course(models.Model):
    infos = JSONField()
</code>```

The infos JSONField looks like this :

```<code>infos = {
    category: "Maths",
    students: [
        {
            name: "Alice",
            result: 8
        },
        {
            name: "Bob",
            result: 12
        }
    ]
}

# (students can have 0 -> n items)
</code>```

I'm trying to get a list of all students that got at least 10 on any course (result >= 10).
However I'm having trouble figuring out a way to filter the QuerySet accordingly.

I'm trying to do something like this :

```<code>(Course.objects.filter(students__result__gte=10)
               .values_list('students', flat=True))
</code>```

But since students is a list, I can't access directly to the result property of each item. Besides, I don't think it would exclude the "Alice" object from the results.

I'd like to get results like this :

```<code>items = [
    {
        name: "Bob",
        result: 12
    }
]
</code>```

Bonus points if there is a way to link filtered students and course category :

```<code>items = [
    {
        category: "Maths",
        students: [
            {
                name: "Bob",
                result: 12
            }
        ]
    }
 ]
</code>```

How can I achieve the desired result ?

答:
In case you're using a PostgreSQL database, you can switch to an HStoreField ([docs here][1]). The HStoreField will allow you to use exactly the lookup you mention (students__result__gte). Otherwise the most Django-esque way would be to create a Student model like Arakkal suggests in the comments under your question.

EDIT: This is the simplest way to be able to filter given all the maps/dicts are the same (if you really want the same structure as your JSON):

```<code>class Course(models.Model):
    pass

class Info(models.Model):
    course = models.ForeignKey(Course, related_name='infos')
    category = models.CharField(max_length=128)
    # students = models.HStoreField()  # available in postgresql

class Student(models.Model):
    """If not using PostgreSQL, also more Django-esque."""
   info = models.ForeignKey(Info, related_name='students')

   name = models.CharField(max_length=128)
   result = models.IntegerField()
</code>```

I'm trying to get a list of all students that got at least 10 on any course (result >= 10)

This would be my prefered structure given your goal and my intuition about the logical relationships:

```<code>class Course(models.Model):
    students = models.ManyToManyField(Student, through=Result)
    category = models.CharField(max_length=128)

class Student(models.Model):
    name = models.CharField(max_length=128)

class Result(models.Model):
    course = models.ForeignKey(Course, on_delete=models.deletion.CASCADE)
    student = models.ForeignKey(Student, on_delete=models.deletion.CASCADE)
    grade = models.IntegerField()
</code>```

That allows for: Result.objects.filter(grade__gte=10).values_list('student__name', flat=True)

Body:
Here is a simplified version of a Django model I'm working on.

class Course(models.Model):
    infos = JSONField()

The infos JSONField looks like this :

infos = {
    category: "Maths",
    students: [
        {
            name: "Alice",
            result: 8
        },
        {
            name: "Bob",
            result: 12
        }
    ]
}

# (students can have 0 -> n items)

I'm trying to get a list of all students that got at least 10 on any course (result >= 10).
However I'm having trouble figuring out a way to filter the QuerySet accordingly.

I'm trying to do something like this :

(Course.objects.filter(students__result__gte=10)
               .values_list('students', flat=True))

But since students is a list, I can't access directly to the result property of each item. Besides, I don't think it would exclude the "Alice" object from the results.

I'd like to get results like this :

items = [
    {
        name: "Bob",
        result: 12
    }
]

Bonus points if there is a way to link filtered students and course category :

items = [
    {
        category: "Maths",
        students: [
            {
                name: "Bob",
                result: 12
            }
        ]
    }
 ]

How can I achieve the desired result ?

Body:
In case you're using a PostgreSQL database, you can switch to an HStoreField ([docs here][1]). The HStoreField will allow you to use exactly the lookup you mention (students__result__gte). Otherwise the most Django-esque way would be to create a Student model like Arakkal suggests in the comments under your question.

EDIT: This is the simplest way to be able to filter given all the maps/dicts are the same (if you really want the same structure as your JSON):

class Course(models.Model):
    pass

class Info(models.Model):
    course = models.ForeignKey(Course, related_name='infos')
    category = models.CharField(max_length=128)
    # students = models.HStoreField()  # available in postgresql

class Student(models.Model):
    """If not using PostgreSQL, also more Django-esque."""
   info = models.ForeignKey(Info, related_name='students')

   name = models.CharField(max_length=128)
   result = models.IntegerField()

I'm trying to get a list of all students that got at least 10 on any course (result >= 10)

This would be my prefered structure given your goal and my intuition about the logical relationships:

class Course(models.Model):
    students = models.ManyToManyField(Student, through=Result)
    category = models.CharField(max_length=128)

class Student(models.Model):
    name = models.CharField(max_length=128)

class Result(models.Model):
    course = models.ForeignKey(Course, on_delete=models.deletion.CASCADE)
    student = models.ForeignKey(Student, on_delete=models.deletion.CASCADE)
    grade = models.IntegerField()

That allows for: Result.objects.filter(grade__gte=10).values_list('student__name', flat=True)


问:
Printing all ASCII characters with index in Javascript
I'm trying to figure out how to print out all ASCII characters with index in Javascript. So far, I've been able to print out the actual characters, but not the index.
The code I've been using is:

var s="";

for(var i = 32; i < 127; i++) {

console.log(String.fromCharCode(i));

}

I need it to look like below

32:

33: !

34: "

35: #

36: $

37: %

38: &

39: '

...etc

Thanks!

答: If you want to print the index, just do console.log(i).


问:
Mixing Java `ExecutorService` with Spring `TaskExecutor`
I have a piece of Java code that I previously used without Spring that looks like this:

// `Callable` instead of `Runnable` because we need to throw exceptions
public MyTask extends Callable<Void> {
  @Override
  public Void call() throws Exception { ... }
}

public class MyTasksRunner {

  private final ExecutorService executorService;

  ...

  public void run() throws Exception {
    List<MyTask> tasks = ...;
    var futures = executorService.invokeAll(tasks);
    for (var future : futures) {
      // Rethrow any exceptions happened in the threads.
      future.get();
    }
  }
}
```

Now I'm merging this code into a larger Spring Boot application that has async enabled. It configures a TaskExecutor, which doesn't have the same interface as ExecutorService. A TaskExecutor can only run Runnables, not Callables.

I can probably have a TaskExecutor bean for async Spring, and another ExecutorService bean for the MyTasksRunner code at the same time. But I wonder what options I have if I want to merge those:

Can I tell Spring to use an ExecutorService for its async stuff?

Can I convert my Callable code to use Runnables instead, while still being able to propagate exceptions from the tasks?

I also thought about just making MyTask a Spring component and annotating it with @Async, but I don't really like that it makes the MyTask* code tied to Spring.

答:
Yes, you can convert your Callable task to Runnable as I see you don't expect any return value. But with one condition - you cant throw Checked Exception however you may continue throwing Runtime Exception.

Also, yes you can define Executor bean as below to inject ExecutorService

```<code>@Bean
public Executor taskExecutor() {

    ExecutorService executor = Executors.newFixedThreadPool(2);

    return executor;
}
</code>```

If you dont define an Executor bean, Spring creates SimpleAsyncTaskExecutor and uses that.

Body:
I have a piece of Java code that I previously used without Spring that looks like this:

// `Callable` instead of `Runnable` because we need to throw exceptions
public MyTask extends Callable<Void> {
  @Override
  public Void call() throws Exception { ... }
}

public class MyTasksRunner {

  private final ExecutorService executorService;

  ...

  public void run() throws Exception {
    List<MyTask> tasks = ...;
    var futures = executorService.invokeAll(tasks);
    for (var future : futures) {
      // Rethrow any exceptions happened in the threads.
      future.get();
    }
  }
}

Now I'm merging this code into a larger Spring Boot application that has async enabled. It configures a TaskExecutor, which doesn't have the same interface as ExecutorService. A TaskExecutor can only run Runnables, not Callables.

I can probably have a TaskExecutor bean for async Spring, and another ExecutorService bean for the MyTasksRunner code at the same time. But I wonder what options I have if I want to merge those:

Can I tell Spring to use an ExecutorService for its async stuff?

Can I convert my Callable code to use Runnables instead, while still being able to propagate exceptions from the tasks?

I also thought about just making MyTask a Spring component and annotating it with @Async, but I don't really like that it makes the MyTask* code tied to Spring.

Body:
Yes, you can convert your Callable task to Runnable as I see you don't expect any return value. But with one condition - you cant throw Checked Exception however you may continue throwing Runtime Exception.

Also, yes you can define Executor bean as below to inject ExecutorService

@Bean
public Executor taskExecutor() {

    ExecutorService executor = Executors.newFixedThreadPool(2);

    return executor;
}

If you dont define an Executor bean, Spring creates SimpleAsyncTaskExecutor and uses that.


问:
How can I make sure TCL doesn't mess up the order of array elements?
The problem is that the order in which I "insert" elements in an array changes throughout the execution of the script.

Here is a quick reproduction of the problem:

```<code>#!/bin/bash
# : \
exec /home/binops/afse/eer/eer_SPI-7.3.1/tclsh "$0" "$@"

proc myProc { theArray } {
  upvar $theArray theArrayInside
  parray theArrayInside
  puts "------"
  foreach { key value } [array get theArrayInside] {
    puts "$key => $value"
  }
}

# MAIN
set myArray(AQHI) AQHI
set myArray(O3) 1
set myArray(NO2) 2
set myArray(PM2.5) 3

parray myArray
puts "------"
myProc myArray
</code>```

Output is:

```<code>myArray(AQHI)  = AQHI
myArray(NO2)   = 2
myArray(O3)    = 1
myArray(PM2.5) = 3
------
theArrayInside(AQHI)  = AQHI
theArrayInside(NO2)   = 2
theArrayInside(O3)    = 1
theArrayInside(PM2.5) = 3
------
PM2.5 => 3
O3 => 1
NO2 => 2
AQHI => AQHI
</code>```

Notice I didn't use generic keys like A, B, C and generic values like 1, 2, 3, as you may have expected. This is because the order isn't messed up with these generic keys/values. Maybe this can help identify the problem.

Also notice that the initial order (AQHI, O3, NO2, PM2.5) is lost even at the first call to parray (order is now AQHI, NO2, O3, PM2.5; alphabetically sorted?). It is then changed again upon calling array get ... (inversed?)

So anyways, the question is: How can I make sure the initial order is kept?

答:
Tcl is flexible enough that one can devise many schemes to handle what you want.  Here's an idea that stores the order of your keys within the array itself, assuming the empty string is not a valid key in your data:</p>

```<code>proc array_add {ary_name key value} {
    upvar 1 $ary_name ary
    set ary($key) $value
    lappend ary() $key
}

proc array_foreach {var_name ary_name script} {
    upvar 1 $var_name var
    upvar 1 $ary_name ary
    foreach var $ary() {
        uplevel 1 $script
    }
}

array_add a foo bar
array_add a baz qux
array_add a abc def
array_add a ghi jkl

array_foreach key a {puts "$key -> $a($key)"}
# foo -> bar
# baz -> qux
# abc -> def
# ghi -> jkl

array names a
# ghi {} foo baz abc

array get a
# ghi jkl {} {foo baz abc ghi} foo bar baz qux abc def

parray a
# a()    = foo baz abc ghi
# a(abc) = def
# a(baz) = qux
# a(foo) = bar
# a(ghi) = jkl
</code>```

Body:
The problem is that the order in which I "insert" elements in an array changes throughout the execution of the script.

Here is a quick reproduction of the problem:

#!/bin/bash
# : \
exec /home/binops/afse/eer/eer_SPI-7.3.1/tclsh "$0" "$@"

proc myProc { theArray } {
  upvar $theArray theArrayInside
  parray theArrayInside
  puts "------"
  foreach { key value } [array get theArrayInside] {
    puts "$key => $value"
  }
}

# MAIN
set myArray(AQHI) AQHI
set myArray(O3) 1
set myArray(NO2) 2
set myArray(PM2.5) 3

parray myArray
puts "------"
myProc myArray

Output is:

myArray(AQHI)  = AQHI
myArray(NO2)   = 2
myArray(O3)    = 1
myArray(PM2.5) = 3
------
theArrayInside(AQHI)  = AQHI
theArrayInside(NO2)   = 2
theArrayInside(O3)    = 1
theArrayInside(PM2.5) = 3
------
PM2.5 => 3
O3 => 1
NO2 => 2
AQHI => AQHI

Notice I didn't use generic keys like A, B, C and generic values like 1, 2, 3, as you may have expected. This is because the order isn't messed up with these generic keys/values. Maybe this can help identify the problem.

Also notice that the initial order (AQHI, O3, NO2, PM2.5) is lost even at the first call to parray (order is now AQHI, NO2, O3, PM2.5; alphabetically sorted?). It is then changed again upon calling array get ... (inversed?)

So anyways, the question is: How can I make sure the initial order is kept?

Body:
Tcl is flexible enough that one can devise many schemes to handle what you want.  Here's an idea that stores the order of your keys within the array itself, assuming the empty string is not a valid key in your data:

proc array_add {ary_name key value} {
    upvar 1 $ary_name ary
    set ary($key) $value
    lappend ary() $key
}

proc array_foreach {var_name ary_name script} {
    upvar 1 $var_name var
    upvar 1 $ary_name ary
    foreach var $ary() {
        uplevel 1 $script
    }
}

array_add a foo bar
array_add a baz qux
array_add a abc def
array_add a ghi jkl

array_foreach key a {puts "$key -> $a($key)"}
# foo -> bar
# baz -> qux
# abc -> def
# ghi -> jkl

array names a
# ghi {} foo baz abc

array get a
# ghi jkl {} {foo baz abc ghi} foo bar baz qux abc def

parray a
# a()    = foo baz abc ghi
# a(abc) = def
# a(baz) = qux
# a(foo) = bar
# a(ghi) = jkl


问:
WordPress custom theme errors with showing content
Wordpress index.php errors

<?php
get_header();
get_content();
get_footer();
?>
```

The footer.php is not showing, and when I remove the get_content() footer is working normally.

答:
with this while is not working.

when i try to put this

```<code>   <?php get_header(); ?>
<?php get_header('content'); ?>
<?php get_footer('footer'); ?>
</code>```

with created header-content.php file.The footer is gona,is not showing,and content is showing only first 5 lines

Body:
Wordpress index.php errors

<?php
get_header();
get_content();
get_footer();
?>

The footer.php is not showing, and when I remove the get_content() footer is working normally.

Body:
with this while is not working.

when i try to put this

   <?php get_header(); ?>
<?php get_header('content'); ?>
<?php get_footer('footer'); ?>

with created header-content.php file.The footer is gona,is not showing,and content is showing only first 5 lines


问:
Join table with null in all group
im new in stack. I cant found problem like this.

I have two tables.

One table x with transactions

```<code>Hour (approximation to full hour)   id_seler     expenses
1:00                                    1             3,04
2:00                                    2             4,04
3:00                                    3             5,03
4:00                                    4             6,04
3:00                                    4             7,04
</code>```

Second table y with limit seler id:

```<code>seler_id
   1
   2
   4
   5
</code>```

I try to join table y to table x to get score like this

```<code>id_seler      hour            sum expensex
   1        1:00              3,04
   1        2:00              null
   1        3:00              null
   1        4:00              null
   2        1:00              null
   2        2:00              4,04
   2        3:00              null
   2        4:00              null
   4        1:00              null
   4        2:00              null
   4       3:00               7,04
   4        4:00              6,04
   5        1:00              null
   5        2:00              null
   5       3:00               null
   5       4:00               null
</code>```

I want that query back me all id_seler for all hours with expenses.

I tried to use left join

```<code>from y left join x on x.id_seller=y.id_seller
group by hour
</code>```

but it back me null only for this id_seler which is dosent exist in table x next of last hour - In this exaple for id_seler=5

答:
Use a cross join to generate the rows and then a left join to bring in existing values:

```<code>select h.hour, y.id_seler, x.expenses
       row_number() over (
from (select distinct hour from x) h cross join
     y left join
     x
     on x.hour = h.hour and x.id_seler = y.id_seler;
</code>```

Your column name suggests that you want aggregation, but aggregation does not seem necessary based on the data and the rest of the question.  However, you can add group by h.hour, y.id_seler if you need aggregation.

Title: Join table with null in all group

Body:
im new in stack. I cant found problem like this.

I have two tables.

One table x with transactions

Hour (approximation to full hour)   id_seler     expenses
1:00                                    1             3,04
2:00                                    2             4,04
3:00                                    3             5,03
4:00                                    4             6,04
3:00                                    4             7,04

Second table y with limit seler id:

seler_id
   1
   2
   4
   5

I try to join table y to table x to get score like this

id_seler      hour            sum expensex
   1        1:00              3,04
   1        2:00              null
   1        3:00              null
   1        4:00              null
   2        1:00              null
   2        2:00              4,04
   2        3:00              null
   2        4:00              null
   4        1:00              null
   4        2:00              null
   4       3:00               7,04
   4        4:00              6,04
   5        1:00              null
   5        2:00              null
   5       3:00               null
   5       4:00               null

I want that query back me all id_seler for all hours with expenses.

I tried to use left join

from y left join x on x.id_seller=y.id_seller
group by hour

but it back me null only for this id_seler which is dosent exist in table x next of last hour - In this exaple for id_seler=5

Body:
Use a cross join to generate the rows and then a left join to bring in existing values:

select h.hour, y.id_seler, x.expenses
       row_number() over (
from (select distinct hour from x) h cross join
     y left join
     x
     on x.hour = h.hour and x.id_seler = y.id_seler;

Your column name suggests that you want aggregation, but aggregation does not seem necessary based on the data and the rest of the question.  However, you can add group by h.hour, y.id_seler if you need aggregation.


问:
MaterialDesignXamlToolkit with WPF class library?
Sorry for the duplicate, but my low reputation does not allow me to comment on posts.

i am trying to use MaterialDesignXamlToolkit with WPF with class library, exactly as in this post: How to include MaterialDesignXamlToolkit to WPF class library?

-so i installed Material Design nuGet and added ResourceDictionary named MaterialDesign.xaml, where I copied and paste this code:

```<code><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
        <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
        <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml" />
        <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />
    </ResourceDictionary.MergedDictionaries>
</code>```

then I created a new WPF page where I added Resource, so my xaml looks like this:

```<code><Page x:Class="test.Page1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:local="clr-namespace:test"
  mc:Ignorable="d"
  d:DesignHeight="450" d:DesignWidth="800"
  Title="Page1"

  xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
  TextElement.Foreground="{DynamicResource MaterialDesignBody}"
  TextElement.FontWeight="Regular"
  TextElement.FontSize="13"
  TextOptions.TextFormattingMode="Ideal"
  TextOptions.TextRenderingMode="Auto"
  Background="{DynamicResource MaterialDesignPaper}"
  FontFamily="{DynamicResource MaterialDesignFont}">
<Page.Resources>
    <ResourceDictionary Source="/MyAsembly;component/ResourceDictionary/MaterialDesign.xaml" />
</Page.Resources>

<Grid>
    <materialDesign:Card Padding="32" Margin="16">
        <TextBlock Style="{DynamicResource MaterialDesignTitleTextBlock}">My First Material Design App</TextBlock>
    </materialDesign:Card>
</Grid>
</code>```

Of course I got an error: The resource {MaterialDesignBody, MaterialDesignPaper, MaterialDesignFont} could not be resolved

as @Marija Rakic mentions in the post, I dried to add dummy lines to my Page1.xaml.cs class

```<code>using MaterialDesignColors;
using MaterialDesignThemes.Wpf;
using System.Windows.Controls;

namespace test
{
    /// <summary>
    /// Interaction logic for Page1.xaml
    /// </summary>
    public partial class Page1 : Page
    {
        public Page1()
        {
            ColorZoneAssist.SetMode(new GroupBox(), ColorZoneMode.Accent);
            Hue hue = new Hue("name", System.Windows.Media.Color.FromArgb(1, 2, 3, 4), System.Windows.Media.Color.FromArgb(1, 5, 6, 7));
            InitializeComponent();
        }
    }
}
</code>```

but it didn't help. The error was still there. So I tried @Trygve solution and created one more class named MaterialDesign.xaml.cs where I added an assembly

```<code>using System.IO;
using System.Reflection;
using System.Windows;

namespace test
{
    partial class MaterialDesign: ResourceDictionary
    {
        public MaterialDesign() {
            Assembly.LoadFrom(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "MaterialDesignThemes.Wpf.dll"));
            Assembly.LoadFrom(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "MaterialDesignColors.dll"));
            InitializeComponent();
        }
    }
}
</code>```

and added a reference to MaterialDesign.xaml:

```<code>x:Class="test.MaterialDesign"
</code>```

but it also didn't work and mentioned error is still there.I don't know where I'm making a mistake ..

答:
Try to merge the resource dictinary before the XAML markup is parsed:</p>

```<code>public partial class Page1 : Page
{
    public Page1()
    {
        Resources.MergedDictionaries.Add(new ResourceDictionary()
        {
            Source = new Uri("pack://application:,,,/YourAssembly;component/MaterialDesign.xaml")
        });
        InitializeComponent();
    }
}
</code>```

Body:
Sorry for the duplicate, but my low reputation does not allow me to comment on posts.

i am trying to use MaterialDesignXamlToolkit with WPF with class library, exactly as in this post: How to include MaterialDesignXamlToolkit to WPF class library?

-so i installed Material Design nuGet and added ResourceDictionary named MaterialDesign.xaml, where I copied and paste this code:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
        <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
        <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml" />
        <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />
    </ResourceDictionary.MergedDictionaries>

then I created a new WPF page where I added Resource, so my xaml looks like this:

<Page x:Class="test.Page1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:local="clr-namespace:test"
  mc:Ignorable="d"
  d:DesignHeight="450" d:DesignWidth="800"
  Title="Page1"

  xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
  TextElement.Foreground="{DynamicResource MaterialDesignBody}"
  TextElement.FontWeight="Regular"
  TextElement.FontSize="13"
  TextOptions.TextFormattingMode="Ideal"
  TextOptions.TextRenderingMode="Auto"
  Background="{DynamicResource MaterialDesignPaper}"
  FontFamily="{DynamicResource MaterialDesignFont}">
<Page.Resources>
    <ResourceDictionary Source="/MyAsembly;component/ResourceDictionary/MaterialDesign.xaml" />
</Page.Resources>

<Grid>
    <materialDesign:Card Padding="32" Margin="16">
        <TextBlock Style="{DynamicResource MaterialDesignTitleTextBlock}">My First Material Design App</TextBlock>
    </materialDesign:Card>
</Grid>

Of course I got an error: The resource {MaterialDesignBody, MaterialDesignPaper, MaterialDesignFont} could not be resolved

as @Marija Rakic mentions in the post, I dried to add dummy lines to my Page1.xaml.cs class

using MaterialDesignColors;
using MaterialDesignThemes.Wpf;
using System.Windows.Controls;

namespace test
{
    ///
    /// Interaction logic for Page1.xaml
    ///
    public partial class Page1 : Page
    {
        public Page1()
        {
            ColorZoneAssist.SetMode(new GroupBox(), ColorZoneMode.Accent);
            Hue hue = new Hue("name", System.Windows.Media.Color.FromArgb(1, 2, 3, 4), System.Windows.Media.Color.FromArgb(1, 5, 6, 7));
            InitializeComponent();
        }
    }
}

but it didn't help. The error was still there. So I tried @Trygve solution and created one more class named MaterialDesign.xaml.cs where I added an assembly

using System.IO;
using System.Reflection;
using System.Windows;

namespace test
{
    partial class MaterialDesign: ResourceDictionary
    {
        public MaterialDesign() {
            Assembly.LoadFrom(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "MaterialDesignThemes.Wpf.dll"));
            Assembly.LoadFrom(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "MaterialDesignColors.dll"));
            InitializeComponent();
        }
    }
}

and added a reference to MaterialDesign.xaml:

x:Class="test.MaterialDesign"

but it also didn't work and mentioned error is still there.I don't know where I'm making a mistake ..

Body:
Try to merge the resource dictinary before the XAML markup is parsed:

public partial class Page1 : Page
{
    public Page1()
    {
        Resources.MergedDictionaries.Add(new ResourceDictionary()
        {
            Source = new Uri("pack://application:,,,/YourAssembly;component/MaterialDesign.xaml")
        });
        InitializeComponent();
    }
}


问:
How can I quickly encrypt a 128 bit value into another equal length value in NodeJS 14
My server will be sending many IDs to the browser/user, and, for that session, the user might operate using those IDs.  Between users, and between multiple sessions of a single user, I need IDs to be encrypted, so they cannot be traced except within the context of a single session.  Due to the number of IDs that will be used in each session, it is not reasonable to dereference or hash them and store lookup tables for each session.

The IDs are effectively UUIDs, unique 128 bit values.  The server will encrypt them in the context of a session, and when the user queries using them, and the server can decrypt those values within the context of that same session.  I would like for the encrypted output to also be 128 bits in length (for example, so they could be rendered as UUIDs even in their encrypted state).  What is the best way for me to achieve this?

This is my sample code, demonstrating that I can encrypt 16 bytes (the size of a block), but the cipher extends it to 2 blocks, doubling the size to 32 bytes, when I finalize it.  I think because it is OK for a value to be encrypted the same way twice in the context of a single session, it is acceptable to reuse the same IV for each item; so the server stores a key and IV for the session, and can encrypt and decrypt all of the IDs with those.

```<code>async function sampleCrypt() {
    const algorithm = 'aes-128-cbc';
    crypto.scrypt("samplePassword", "salty", 16, (err, key) => {
        const iv: Buffer = crypto.randomBytes(16);
        const cipher: crypto.Cipher = crypto.createCipheriv(algorithm, key, iv);
        const inbuffer = Buffer.allocUnsafe(16);
        inbuffer.writeUInt32BE(1960); // just some sample data
        console.log(cipher.update(inbuffer, undefined, 'hex')); // loads the whole buffer in
        console.log(cipher.final('hex'));
    });
}

sampleCrypt();

/* Sample output:
83134f7dc2f9b175bd70a7dd0512eaf7
9495e0cfceab0439fddc92f3fffa48c2
*/
</code>```

Please advise if I have made any incorrect assumptions here as well. Thanks!

答:
Block ciphers such as AES require plaintexts whose lengths are an integer multiple of the blocksize (16 bytes for AES). If this isn't the case, padding must be used. NodeJS applies PKCS7 padding by default. Here a complete padding block is appended if the plaintext length is already an integer multiple of the blocksize. This is the reason why in your case a 16 bytes plaintext results in a 32 bytes ciphertext. But since the plaintext is always exactly one block long, there is actually no need for padding. In NodeJS the padding can be disabled with cipher.setAutoPadding(false), so in your case plaintext and ciphertext are both 16 bytes long.

A block cipher only encrypts one block. To encrypt longer plaintexts, an operation mode must be used, e.g. CBC as in the posted code. Generally, these operation modes use an initialization vector (IV) whose size is equal to the blocksize (16 bytes for AES). The IV must meet certain conditions, e.g. a key/IV pair may only be used once. Since the IV isn't secret it's usually placed before the ciphertext. In your case, this would result in a 32 bytes result (IV + ciphertext). The condition mentioned also means that the concept you use (one key/IV pair for all encryptions) is inherently insecure.

An operating mode that doesn't require an IV is ECB. ECB generates the same ciphertext for the same plaintext, which generallay allows conclusions from the ciphertext to the plaintext. This problem doesn't exist for a mode with an IV. Therefore ECB is more insecure compared to a mode with an IV. However, the severity of this insecurity ultimately depends on the characteristics of the plaintext and the particular application, and the respective requirements determine whether this disadvantage is tolerable or not. 1-block plaintexts containing a GUID are less vulnerable in this respect than multi-block plaintexts with some message content, so ECB may be an option here.

With disabled padding and ECB mode a 16 bytes plaintext results in a 16 bytes ciphertext, as the following TypeScript code demonstrates:

```<code>import * as crypto from "crypto";

const algorithm:string = 'aes-128-ecb';
const key:Buffer = crypto.randomBytes(16);

// Encryption
const plaintextEnc:Buffer = Buffer.from('0123456789012345');
const cipherEnc:crypto.Cipher = crypto.createCipheriv(algorithm, key, null);
cipherEnc.setAutoPadding(false);
const ciphertext:Buffer = Buffer.concat([cipherEnc.update(plaintextEnc), cipherEnc.final()]);
console.log(ciphertext.toString('hex'));

// Decryption
const cipherDec:crypto.Decipher = crypto.createDecipheriv(algorithm, key, null);
cipherDec.setAutoPadding(false);
const plaintextDec:Buffer = Buffer.concat([cipherDec.update(ciphertext), cipherDec.final()]);
console.log(plaintextDec.toString('hex'));
</code>```

If the limitation to 16 bytes is dropped, GCM would be a recommendable mode that provides besides confidentiality also authenticity and integrity. GCM uses a 12 bytes IV (nonce) and generates a tag (typically 16 bytes) that is used for authentication. In your case, the result (IV + ciphertext + tag) would have a length of 44 bytes. Note that if a key/IV pair is used more than once for GCM, security is lost.

Body:
My server will be sending many IDs to the browser/user, and, for that session, the user might operate using those IDs.  Between users, and between multiple sessions of a single user, I need IDs to be encrypted, so they cannot be traced except within the context of a single session.  Due to the number of IDs that will be used in each session, it is not reasonable to dereference or hash them and store lookup tables for each session.

The IDs are effectively UUIDs, unique 128 bit values.  The server will encrypt them in the context of a session, and when the user queries using them, and the server can decrypt those values within the context of that same session.  I would like for the encrypted output to also be 128 bits in length (for example, so they could be rendered as UUIDs even in their encrypted state).  What is the best way for me to achieve this?

This is my sample code, demonstrating that I can encrypt 16 bytes (the size of a block), but the cipher extends it to 2 blocks, doubling the size to 32 bytes, when I finalize it.  I think because it is OK for a value to be encrypted the same way twice in the context of a single session, it is acceptable to reuse the same IV for each item; so the server stores a key and IV for the session, and can encrypt and decrypt all of the IDs with those.

async function sampleCrypt() {
    const algorithm = 'aes-128-cbc';
    crypto.scrypt("samplePassword", "salty", 16, (err, key) => {
        const iv: Buffer = crypto.randomBytes(16);
        const cipher: crypto.Cipher = crypto.createCipheriv(algorithm, key, iv);
        const inbuffer = Buffer.allocUnsafe(16);
        inbuffer.writeUInt32BE(1960); // just some sample data
        console.log(cipher.update(inbuffer, undefined, 'hex')); // loads the whole buffer in
        console.log(cipher.final('hex'));
    });
}

sampleCrypt();

/* Sample output:
83134f7dc2f9b175bd70a7dd0512eaf7
9495e0cfceab0439fddc92f3fffa48c2
*/

Please advise if I have made any incorrect assumptions here as well. Thanks!

Body:
Block ciphers such as AES require plaintexts whose lengths are an integer multiple of the blocksize (16 bytes for AES). If this isn't the case, padding must be used. NodeJS applies PKCS7 padding by default. Here a complete padding block is appended if the plaintext length is already an integer multiple of the blocksize. This is the reason why in your case a 16 bytes plaintext results in a 32 bytes ciphertext. But since the plaintext is always exactly one block long, there is actually no need for padding. In NodeJS the padding can be disabled with cipher.setAutoPadding(false), so in your case plaintext and ciphertext are both 16 bytes long.

A block cipher only encrypts one block. To encrypt longer plaintexts, an operation mode must be used, e.g. CBC as in the posted code. Generally, these operation modes use an initialization vector (IV) whose size is equal to the blocksize (16 bytes for AES). The IV must meet certain conditions, e.g. a key/IV pair may only be used once. Since the IV isn't secret it's usually placed before the ciphertext. In your case, this would result in a 32 bytes result (IV + ciphertext). The condition mentioned also means that the concept you use (one key/IV pair for all encryptions) is inherently insecure.

An operating mode that doesn't require an IV is ECB. ECB generates the same ciphertext for the same plaintext, which generallay allows conclusions from the ciphertext to the plaintext. This problem doesn't exist for a mode with an IV. Therefore ECB is more insecure compared to a mode with an IV. However, the severity of this insecurity ultimately depends on the characteristics of the plaintext and the particular application, and the respective requirements determine whether this disadvantage is tolerable or not. 1-block plaintexts containing a GUID are less vulnerable in this respect than multi-block plaintexts with some message content, so ECB may be an option here.

With disabled padding and ECB mode a 16 bytes plaintext results in a 16 bytes ciphertext, as the following TypeScript code demonstrates:

import * as crypto from "crypto";

const algorithm:string = 'aes-128-ecb';
const key:Buffer = crypto.randomBytes(16);

// Encryption
const plaintextEnc:Buffer = Buffer.from('0123456789012345');
const cipherEnc:crypto.Cipher = crypto.createCipheriv(algorithm, key, null);
cipherEnc.setAutoPadding(false);
const ciphertext:Buffer = Buffer.concat([cipherEnc.update(plaintextEnc), cipherEnc.final()]);
console.log(ciphertext.toString('hex'));

// Decryption
const cipherDec:crypto.Decipher = crypto.createDecipheriv(algorithm, key, null);
cipherDec.setAutoPadding(false);
const plaintextDec:Buffer = Buffer.concat([cipherDec.update(ciphertext), cipherDec.final()]);
console.log(plaintextDec.toString('hex'));

If the limitation to 16 bytes is dropped, GCM would be a recommendable mode that provides besides confidentiality also authenticity and integrity. GCM uses a 12 bytes IV (nonce) and generates a tag (typically 16 bytes) that is used for authentication. In your case, the result (IV + ciphertext + tag) would have a length of 44 bytes. Note that if a key/IV pair is used more than once for GCM, security is lost.


问:
Not splitting a string properly in scala
I have a string:

```<code>var string = "PRODUCTIDENTIFIER|UTLEVEL10|UTLEVEL10DESCRIPTION|UTLEVEL15|UTLEVEL15DESCRIPTION|UTLEVEL17|UTLEVEL17DESCRIPTION|UTLEVEL20|UTLEVEL20DESCRIPTION|UTLEVEL30|UTLEVEL30DESCRIPTION|ACCT_YR|UTCODE"
</code>```

When I try to do string.split("|"), it ends up giving me all the letters of all words in an array like:

```<code>scala> string.split("|")
res0: Array[String] = Array(|, P, R, O, D, U, C, T, I, D, E, N, T, I, F, I, E, R, |, U, T, L, E, V, E, L, 1, 0, |, U, T, L, E, V, E, L, 1, 0, D, E, S, C, R, I, P, T, I, O, N, |, U, T, L, E, V, E, L, 1, 5, |, U, T, L, E, V, E, L, 1, 5, D, E, S, C, R, I, P, T, I, O, N, |, U, T, L, E, V, E, L, 1, 7, |, U, T, L, E, V, E, L, 1, 7, D, E, S, C, R, I, P, T, I, O, N, |, U, T, L, E, V, E, L, 2, 0, |, U, T, L, E, V, E, L, 2, 0, D, E, S, C, R, I, P, T, I, O, N, |, U, T, L, E, V, E, L, 3, 0, |, U, T, L, E, V, E, L, 3, 0, D, E, S, C, R, I, P, T, I, O, N, |, A, C, C, T, _, Y, R, |, U, T, C, O, D, E)
</code>```

What I actually expected was the string will be split based on the | mark. Any idea as to why this does not work?

答:
You can also use the split that takes a single character.

```<code>string.split('|')
// val res: Array[String] = Array(
//   PRODUCTIDENTIFIER,
//   UTLEVEL10,
//   UTLEVEL10DESCRIPTION,
//   UTLEVEL15,
//   UTLEVEL15DESCRIPTION,
//   UTLEVEL17,
//   UTLEVEL17DESCRIPTION,
//   UTLEVEL20,
//   UTLEVEL20DESCRIPTION,
//   UTLEVEL30,
//   UTLEVEL30DESCRIPTION,
//   ACCT_YR,
//   UTCODE
// )
</code>```

Anyways, next time take a look to the docs.
Or Google more hard, this exact question was asked the last week.

Body:
I have a string:

var string = "PRODUCTIDENTIFIER|UTLEVEL10|UTLEVEL10DESCRIPTION|UTLEVEL15|UTLEVEL15DESCRIPTION|UTLEVEL17|UTLEVEL17DESCRIPTION|UTLEVEL20|UTLEVEL20DESCRIPTION|UTLEVEL30|UTLEVEL30DESCRIPTION|ACCT_YR|UTCODE"

When I try to do string.split("|"), it ends up giving me all the letters of all words in an array like:

scala> string.split("|")
res0: Array[String] = Array(|, P, R, O, D, U, C, T, I, D, E, N, T, I, F, I, E, R, |, U, T, L, E, V, E, L, 1, 0, |, U, T, L, E, V, E, L, 1, 0, D, E, S, C, R, I, P, T, I, O, N, |, U, T, L, E, V, E, L, 1, 5, |, U, T, L, E, V, E, L, 1, 5, D, E, S, C, R, I, P, T, I, O, N, |, U, T, L, E, V, E, L, 1, 7, |, U, T, L, E, V, E, L, 1, 7, D, E, S, C, R, I, P, T, I, O, N, |, U, T, L, E, V, E, L, 2, 0, |, U, T, L, E, V, E, L, 2, 0, D, E, S, C, R, I, P, T, I, O, N, |, U, T, L, E, V, E, L, 3, 0, |, U, T, L, E, V, E, L, 3, 0, D, E, S, C, R, I, P, T, I, O, N, |, A, C, C, T, _, Y, R, |, U, T, C, O, D, E)

What I actually expected was the string will be split based on the | mark. Any idea as to why this does not work?

Body:
You can also use the split that takes a single character.

string.split('|')
// val res: Array[String] = Array(
//   PRODUCTIDENTIFIER,
//   UTLEVEL10,
//   UTLEVEL10DESCRIPTION,
//   UTLEVEL15,
//   UTLEVEL15DESCRIPTION,
//   UTLEVEL17,
//   UTLEVEL17DESCRIPTION,
//   UTLEVEL20,
//   UTLEVEL20DESCRIPTION,
//   UTLEVEL30,
//   UTLEVEL30DESCRIPTION,
//   ACCT_YR,
//   UTCODE
// )

Anyways, next time take a look to the docs.
Or Google more hard, this exact question was asked the last week.


问:
< & <= not working in Python but when debug it opens some other files from root folder
< & <= not working in Python (Pycharm IDE) but when I debug it opens some other files from the JetBrains root folder. I'm a newbie, please help

This is my code:

```<code>j=1
x=1
for j<=100:
   print(x)
</code>```

And this shows when I debug:

```<code>#We must redefine it in Py3k if it's not already there
def execfile(file, glob=None, loc=None):
if glob is None:
    import sys
    glob = sys._getframe().f_back.f_globals
if loc is None:
    loc = glob

# It seems that the best way is using tokenize.open(): http://code.activestate.com/lists/python-dev/131251/
import tokenize
stream = tokenize.open(file)  # @UndefinedVariable
try:
    contents = stream.read()
finally:
    stream.close()

#execute the script (note: it's important to compile first to have the filename set in debug mode)
exec(compile(contents+"\n", file, 'exec'), glob, loc)
</code>```

答:
Usually, when it does something like this on PyCharm or IntelliJ, it is because I am click execution before previously selecting file...

Find file in IDE, right click, and then retry to run.

Body:
< & <= not working in Python (Pycharm IDE) but when I debug it opens some other files from the JetBrains root folder. I'm a newbie, please help

This is my code:

j=1
x=1
for j<=100:
   print(x)

And this shows when I debug:

#We must redefine it in Py3k if it's not already there
def execfile(file, glob=None, loc=None):
if glob is None:
    import sys
    glob = sys._getframe().f_back.f_globals
if loc is None:
    loc = glob

# It seems that the best way is using tokenize.open(): http://code.activestate.com/lists/python-dev/131251/
import tokenize
stream = tokenize.open(file)  # @UndefinedVariable
try:
    contents = stream.read()
finally:
    stream.close()

#execute the script (note: it's important to compile first to have the filename set in debug mode)
exec(compile(contents+"\n", file, 'exec'), glob, loc)


问:
Alternative to supervisord for docker
Supervisord is really great tool even for docker environment. It helps a lot with stderr redirection and signals forwarding. But it has a couple of disadvantages:

It doesn't support delayed startup. It could be useful to delay some agent startup until main app is initializing. Priority doesn't solve this issue.

If some app enters FATAL state supervisord just logs it but continue to work. So you can't see it until look at logs of container. It could much more friendly if supervisord just stops because in that case you see the problem with docker ps -a

So what is the best alternative to supervisord?

答:
Any new visitor:

If you are a beginner to containerization it is highly recommended to use one process per container it makes everyone's life easy.

Without any hack, all other tools will work perfectly, it will improve Observability as well.

Don't treat containers like VMs.

Title: Alternative to supervisord for docker


问:
Programmatically get Account Id from lambda context arn
I have access to

```<code>com.amazonaws.services.lambda.runtime.Context;
</code>```

object and by extension the invoked function Arn. The arn contains the account Id where the lambda resides.

My question is simple, I want the cleanest way to extract the account Id from that.

I was taking a look

com.amazon.arn.ARN;

It has a whole bunch of stuff, but no account ID (which i presume is due to the fact that not all arns have account ids ?)

I want to cleanly extract the account Id, without resorting to parsing the string.

答:
You no longer need to parse the arn anymore, sts library has introduced get_caller_identity for this purpose.
Its an overkill, but works!.

Excerpts from aws docs.

python

```<code>import boto3

client = boto3.client('sts')
response = client.get_caller_identity()['Account']
</code>```

js

```<code>/* This example shows a request and response made with the credentials for a user named Alice in the AWS account 123456789012. */

 var params = {
 };
 sts.getCallerIdentity(params, function(err, data) {
   if (err) console.log(err, err.stack); // an error occurred
   else     console.log(data);           // successful response
   /*
   data = {
    Account: "123456789012",
    Arn: "arn:aws:iam::123456789012:user/Alice",
    UserId: "AKIAI44QH8DHBEXAMPLE"
   }
   */
 });
</code>```

More details here & here

Body:
I have access to

com.amazonaws.services.lambda.runtime.Context;

object and by extension the invoked function Arn. The arn contains the account Id where the lambda resides.

My question is simple, I want the cleanest way to extract the account Id from that.

I was taking a look

com.amazon.arn.ARN;

It has a whole bunch of stuff, but no account ID (which i presume is due to the fact that not all arns have account ids ?)

I want to cleanly extract the account Id, without resorting to parsing the string.

Body:
You no longer need to parse the arn anymore, sts library has introduced get_caller_identity for this purpose.
Its an overkill, but works!.

Excerpts from aws docs.

python

import boto3

client = boto3.client('sts')
response = client.get_caller_identity()['Account']

js

/* This example shows a request and response made with the credentials for a user named Alice in the AWS account 123456789012. */

 var params = {
 };
 sts.getCallerIdentity(params, function(err, data) {
   if (err) console.log(err, err.stack); // an error occurred
   else     console.log(data);           // successful response
   /*
   data = {
    Account: "123456789012",
    Arn: "arn:aws:iam::123456789012:user/Alice",
    UserId: "AKIAI44QH8DHBEXAMPLE"
   }
   */
 });

More details here & here


问:
How can I get a Discord bot to use lines from a .txt file?
So, I was commissioned for making a bot for a music-themed server, and I'm gonna make commands for providing YouTube and Spotify links to some songs and also commands for sending an embed with lyrics of songs. The problem with this is that I have to do it for around 400 songs. I think there is a way to use a .txt file with all the links and make the bot send them one by one, and that would save me a lot of time. Please if you know how to do it, tell me and explain how it works.</p>

答:
Try putting all of your links inside of a .json file instead.

You could make it look like this :

```<code>{
    "links": [
              "link1",
              "link2",
              "and so on"
             ]
}
</code>```

Then require it inside your code for further use
(const json = require('path/to/yourFile.json');) :)

Body:
Try putting all of your links inside of a .json file instead.

You could make it look like this :

{
    "links": [
              "link1",
              "link2",
              "and so on"
             ]
}

Then require it inside your code for further use
(const json = require('path/to/yourFile.json');) :)