问:
Color every point in a polygon depending on another dataset of points, in R
Problem:

1.) I have a shapefile that looks like this:

Extreme values for coordinates are: xmin = 300,000, xmax = 620,000, ymin = 31,000 and ymax = 190,000.

2.) I have a dataset of approx. 2mio points (every point is inside the given polygon) - each one is in one of a 5 different categories.

Now, for every point inside the border (distance between points has to be 10, so that would give us 580,800,000 points) I want to determine color, depending on a category of the nearest point in a dataset.

In the end I would like to draw a ggplot, where the color of every point is dependent on its category (so I'll use 5 different colors).

What I have so far:

My ideas for solution are not optimized and it takes R forever to determine categories for every point inside the polygon.

1.) I created a new dataset with points in a shape of a rectangle with extreme values of coordinates, with 10 units between points. From a new dataset I selected points that have fallen inside the border of polygons (with a function pnt.in.poly from package SDMTools). Then I wanted to find nearest points (from dataset) of  every point in a polygon and determined category, but I never manage to get a subset from 580,800,000 points (obviously).

2.) I tried to take 2mio points and color an area around them, dependent on their category, but that did not work right.

I know that it is not possible to plot so many points and see the difference between plot with 200,000,000 points and plot with 1,000,000 points, but I would like to have an accurate coloring when zooming (drawing) only one little spot in a polygon (size of 100 x 100 for example).

Question: Is there any better a way of coloring so many points in a polygon (with creating a new shapefile or grouping points)?

Thank you for your ideas!

答:
It’s really helpful if you include some data with your question, even (especially) if it’s a toy data set. As you don’t, I’ve made a toy example. First, I define a simple shape data frame and a data frame of synthetic data that includes x, y, and grp (i.e., a categorical variable with 5 levels). I crop the latter to the former and plot the results,

# Dummy shape function
df_shape <- data.frame(x = c(0, 0.5, 1, 0.5, 0),
                    y = c(0, 0.2, 1, 0.8, 0))

# Load library
library(ggplot2)
library(sgeostat) # For in.polygon function

# Data frame of synthetic data: random [x, y] and category (grp)
df_synth <- data.frame(x = runif(500),
                       y = runif(500),
                       grp = factor(sample(1:5, 500, replace = TRUE)))

# Remove points outside polygon
df_synth <- df_synth[in.polygon(df_synth$x, df_synth$y, df_shape$x, df_shape$y), ]

# Plot shape and synthetic data
g <- ggplot(df_shape, aes(x = x, y = y)) + geom_path(colour = "#FF3300", size = 1.5)
g <- g + ggthemes::theme_clean()
g <- g + geom_point(data = df_synth, aes(x = x, y = y, colour = grp))
g
```

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

<p>Next, I create a regular grid and crop that using the polygon.</p>

<pre class="lang-r prettyprint-override"><code># Create a grid
df_grid <- expand.grid(x = seq(0, 1, length.out = 50),
                       y = seq(0, 1, length.out = 50))

# Check if grid points are in polygon
df_grid <- df_grid[in.polygon(df_grid$x, df_grid$y, df_shape$x, df_shape$y), ]

# Plot shape and show points are inside
g <- ggplot(df_shape, aes(x = x, y = y)) + geom_path(colour = "#FF3300", size = 1.5)
g <- g + ggthemes::theme_clean()
g <- g + geom_point(data = df_grid, aes(x = x, y = y))
g
</code>```

To classify each point on this grid by the nearest point in the synthetic data set, I use knn or k-nearest-neighbours with k = 1. That gives something like this.

# Classify grid points according to synthetic data set using k-nearest neighbour
df_grid$grp <- class::knn(df_synth[, 1:2], df_grid, df_synth[, 3])

# Show categorised points
g <- ggplot()
g <- g + ggthemes::theme_clean()
g <- g + geom_point(data = df_grid, aes(x = x, y = y, colour = grp))
g
```

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

<p>So, that's how I'd address that part of your question about classifying points on a grid. </p>

<p>The other part of your question seems to be about resolution. If I understand correctly, you want the same resolution even if you're zoomed in. Also, you don't want to plot so many points when zoomed out, as you can't even see them. Here, I create a plotting function that lets you specify the resolution. First, I plot all the points in the shape with 50 points in each direction. Then, I plot a subregion (i.e., zoom), but keep the same number of points in each direction the same so that it looks pretty much the same as the previous plot in terms of numbers of dots.</p>

<pre class="lang-r prettyprint-override"><code>res_plot <- function(xlim, xn, ylim, yn, df_data, df_sh){
  # Create a grid
  df_gr <- expand.grid(x = seq(xlim[1], xlim[2], length.out = xn),
                         y = seq(ylim[1], ylim[2], length.out = yn))

  # Check if grid points are in polygon
  df_gr <- df_gr[in.polygon(df_gr$x, df_gr$y, df_sh$x, df_sh$y), ]

  # Classify grid points according to synthetic data set using k-nearest neighbour
  df_gr$grp <- class::knn(df_data[, 1:2], df_gr, df_data[, 3])

  g <- ggplot()
  g <- g + ggthemes::theme_clean()
  g <- g + geom_point(data = df_gr, aes(x = x, y = y, colour = grp))
  g <- g + xlim(xlim) + ylim(ylim)
  g
}

# Example plot
res_plot(c(0, 1), 50, c(0, 1), 50, df_synth, df_shape)
</code>```

# Same resolution, but different limits
res_plot(c(0.25, 0.75), 50, c(0, 1), 50, df_synth, df_shape)
```

Created on 2019-05-31 by the reprex package (v0.3.0)

Hopefully, that addresses your question.

Body:
It’s really helpful if you include some data with your question, even (especially) if it’s a toy data set. As you don’t, I’ve made a toy example. First, I define a simple shape data frame and a data frame of synthetic data that includes x, y, and grp (i.e., a categorical variable with 5 levels). I crop the latter to the former and plot the results,

# Dummy shape function
df_shape <- data.frame(x = c(0, 0.5, 1, 0.5, 0),
                    y = c(0, 0.2, 1, 0.8, 0))

# Load library
library(ggplot2)
library(sgeostat) # For in.polygon function

# Data frame of synthetic data: random [x, y] and category (grp)
df_synth <- data.frame(x = runif(500),
                       y = runif(500),
                       grp = factor(sample(1:5, 500, replace = TRUE)))

# Remove points outside polygon
df_synth <- df_synth[in.polygon(df_synth$x, df_synth$y, df_shape$x, df_shape$y), ]

# Plot shape and synthetic data
g <- ggplot(df_shape, aes(x = x, y = y)) + geom_path(colour = "#FF3300", size = 1.5)
g <- g + ggthemes::theme_clean()
g <- g + geom_point(data = df_synth, aes(x = x, y = y, colour = grp))
g

Next, I create a regular grid and crop that using the polygon.

# Create a grid
df_grid <- expand.grid(x = seq(0, 1, length.out = 50),
                       y = seq(0, 1, length.out = 50))

# Check if grid points are in polygon
df_grid <- df_grid[in.polygon(df_grid$x, df_grid$y, df_shape$x, df_shape$y), ]

# Plot shape and show points are inside
g <- ggplot(df_shape, aes(x = x, y = y)) + geom_path(colour = "#FF3300", size = 1.5)
g <- g + ggthemes::theme_clean()
g <- g + geom_point(data = df_grid, aes(x = x, y = y))
g

To classify each point on this grid by the nearest point in the synthetic data set, I use knn or k-nearest-neighbours with k = 1. That gives something like this.

# Classify grid points according to synthetic data set using k-nearest neighbour
df_grid$grp <- class::knn(df_synth[, 1:2], df_grid, df_synth[, 3])

# Show categorised points
g <- ggplot()
g <- g + ggthemes::theme_clean()
g <- g + geom_point(data = df_grid, aes(x = x, y = y, colour = grp))
g

So, that's how I'd address that part of your question about classifying points on a grid.

The other part of your question seems to be about resolution. If I understand correctly, you want the same resolution even if you're zoomed in. Also, you don't want to plot so many points when zoomed out, as you can't even see them. Here, I create a plotting function that lets you specify the resolution. First, I plot all the points in the shape with 50 points in each direction. Then, I plot a subregion (i.e., zoom), but keep the same number of points in each direction the same so that it looks pretty much the same as the previous plot in terms of numbers of dots.

res_plot <- function(xlim, xn, ylim, yn, df_data, df_sh){
  # Create a grid
  df_gr <- expand.grid(x = seq(xlim[1], xlim[2], length.out = xn),
                         y = seq(ylim[1], ylim[2], length.out = yn))

  # Check if grid points are in polygon
  df_gr <- df_gr[in.polygon(df_gr$x, df_gr$y, df_sh$x, df_sh$y), ]

  # Classify grid points according to synthetic data set using k-nearest neighbour
  df_gr$grp <- class::knn(df_data[, 1:2], df_gr, df_data[, 3])

  g <- ggplot()
  g <- g + ggthemes::theme_clean()
  g <- g + geom_point(data = df_gr, aes(x = x, y = y, colour = grp))
  g <- g + xlim(xlim) + ylim(ylim)
  g
}

# Example plot
res_plot(c(0, 1), 50, c(0, 1), 50, df_synth, df_shape)

# Same resolution, but different limits
res_plot(c(0.25, 0.75), 50, c(0, 1), 50, df_synth, df_shape)

Created on 2019-05-31 by the reprex package (v0.3.0)

Hopefully, that addresses your question.


来源: stackexchange

问:
How can I better handle commits polluting a "closed" branch in git?
We're new to git at my company, coming to git from Subversion, and over the weekend we ran in to a situation with a branch in our repository where commits were made to the public version of a branch that we didn't want there. We had:

```<code>A -> B
</code>```

And then got the bad commits to put the branch at:

```<code>A -> B -> C -> D
</code>```

C and D should never have been on that branch. The trouble is this branch was "closed" -- this was a released version of our software and there shouldn't have been any new commits to this branch.

In Subversion the only way out of this sort of situation was to commit !D and !C so you end up with:

```<code>A -> B -> C -> D -> !D -> !C
</code>```

Which gets me back to B but keeps me moving forward on the timeline for the branch so anyone with a remote of the branch who syncs with the master repository would get C and D and then have them undone to end up at a logically similar version of B (but not B -- call it B').

I came across this solution for reverting commits in git which seemed ideal: it would put our public repository back to A -> B. But it meant that any clone of this branch out on anyone's working machine would be very incorrect and everyone would need to re-clone. My fix amounted to:

```<code>git checkout thebranch
git reset --hard <<commit # associated with commit B>>
git push --force
</code>```

I ended up going the route of the above link and it caused quite the stir that:

a) You can throw away commit history at the public repository with git like this, literally re-writing commit reality;

b) Everyone had to re-clone so they wouldn't risk re-injecting C -> D on to the branch (or the new branch of that branch that we wanted to create).

I think I should have done:

```<code>git revert HEAD~2
git commit
git push
</code>```

But this would have left the branch as A -> B -> C -> D -> E and it really shouldn't have C -> D -> E on it because it's supposed to be closed.

I've got three questions:

How could I have handled the clean up better? Use revert instead of reset? What's the best practice here for branch pollution?

Did the push --force of the reverted branch really destroy the history at the public repository? Or did git roll back to B but keep a record of C -> D and that a revert was done back to B at some point by me? It definitely doesn't show the revert in the commit log, but maybe a record of my action is kept some place else?

How do you handle "closed" branches in git such that these changes couldn't have gotten on there in the first place? We did have a tag applied to the repository at commit B and people are supposed to use the branch + tag to get the source for the release, but this is still a scary thing to have changes show up on a branch line that should not have changes on it after commit B. And someone branching from the branch for a patch release could have easily missed the tag and pulled C -> D in to their new branch as well.

答:
Regarding point 2: No; resetting a branch (locally or on the server) will never destroy history. In git, a branch is akin to a label that is just pointing to a specific commit (and the label automatically moves forward when the branch is checked out and a new commit is made). Resetting a branch just means that the label is moved backward; the commits themselves remain, but become "invisible" if there are no other branches pointing to them. In this case, they can be recovered using git reflog, which will show you the hashes of all commits, even those without a branch. You will then see that the "deleted" commits are still there. The only thing that can destroy them is git gc (which is also run sporadically by git itself), which removes unreferenced commits.

When you reset a branch to an earlier point and force-push it, other developers can reset their branch by using git fetch -f origin branchname:branchname; there is no need to re-clone (however, they should first checkout a different branch in order to prevent the index from getting messed up by the force-fetch). However, this will cause them to "lose" commits (again, they can be found with git reflog) if they have made commits to the branch past the ones you've accidentally pushed. This is probably not a problem for you, since the branch was supposed to be closed anyway, but if it happens, they can create a new branch before force-fetching; then the new branch will still point to their newest commits, and git rebase can transplant the commits onto the proper branch.

ContentLicense: CC BY-SA 3.0

Body:
We're new to git at my company, coming to git from Subversion, and over the weekend we ran in to a situation with a branch in our repository where commits were made to the public version of a branch that we didn't want there. We had:

A -> B

And then got the bad commits to put the branch at:

A -> B -> C -> D

C and D should never have been on that branch. The trouble is this branch was "closed" -- this was a released version of our software and there shouldn't have been any new commits to this branch.

In Subversion the only way out of this sort of situation was to commit !D and !C so you end up with:

A -> B -> C -> D -> !D -> !C

Which gets me back to B but keeps me moving forward on the timeline for the branch so anyone with a remote of the branch who syncs with the master repository would get C and D and then have them undone to end up at a logically similar version of B (but not B -- call it B').

I came across this solution for reverting commits in git which seemed ideal: it would put our public repository back to A -> B. But it meant that any clone of this branch out on anyone's working machine would be very incorrect and everyone would need to re-clone. My fix amounted to:

git checkout thebranch
git reset --hard <<commit # associated with commit B>>
git push --force

I ended up going the route of the above link and it caused quite the stir that:

a) You can throw away commit history at the public repository with git like this, literally re-writing commit reality;

b) Everyone had to re-clone so they wouldn't risk re-injecting C -> D on to the branch (or the new branch of that branch that we wanted to create).

I think I should have done:

git revert HEAD~2
git commit
git push

But this would have left the branch as A -> B -> C -> D -> E and it really shouldn't have C -> D -> E on it because it's supposed to be closed.

I've got three questions:

How could I have handled the clean up better? Use revert instead of reset? What's the best practice here for branch pollution?

Did the push --force of the reverted branch really destroy the history at the public repository? Or did git roll back to B but keep a record of C -> D and that a revert was done back to B at some point by me? It definitely doesn't show the revert in the commit log, but maybe a record of my action is kept some place else?

How do you handle "closed" branches in git such that these changes couldn't have gotten on there in the first place? We did have a tag applied to the repository at commit B and people are supposed to use the branch + tag to get the source for the release, but this is still a scary thing to have changes show up on a branch line that should not have changes on it after commit B. And someone branching from the branch for a patch release could have easily missed the tag and pulled C -> D in to their new branch as well.


问:
Add extra row to dataframe based on values of other rows
I have a dataframe with two rows of data. I would like to add a third row with values representing the difference between the previous two.

The data is in the following format:

```<code>Month   A   B   C   D   E   F
Jan     1   2   4   8   4   1
Feb     1   1   4   5   2   0
</code>```

I would like to add an extra row with a calculation to give the change across the two months:

```<code>Month   A   B   C   D   E   F
Jan     1   2   4   8   4   1
Feb     1   1   4   5   2   0
Change  0   1   0   3   2   1
</code>```

I've been looking at various functions to add additional rows including rbind and mutate but I'm struggling to perform the calculation in the newly created row.

答:
An option with tidyverse

```<code>library(tidyverse)
df1 %>%
    summarise_if(is.numeric, diff) %>%
    abs %>%
    bind_rows(df1, .) %>%
    mutate(Month = replace_na(Month, "Change"))
#   Month A B C D E F
#1    Jan 1 2 4 8 4 1
#2    Feb 1 1 4 5 2 0
#3 Change 0 1 0 3 2 1
</code>```

data

```<code>df1 <- structure(list(Month = c("Jan", "Feb"), A = c(1L, 1L), B = 2:1,
    C = c(4L, 4L), D = c(8L, 5L), E = c(4L, 2L), F = 1:0),
    class = "data.frame", row.names = c(NA,
-2L))
</code>```

ContentLicense: CC BY-SA 4.0

Body:
I have a dataframe with two rows of data. I would like to add a third row with values representing the difference between the previous two.

The data is in the following format:

Month   A   B   C   D   E   F
Jan     1   2   4   8   4   1
Feb     1   1   4   5   2   0

I would like to add an extra row with a calculation to give the change across the two months:

Month   A   B   C   D   E   F
Jan     1   2   4   8   4   1
Feb     1   1   4   5   2   0
Change  0   1   0   3   2   1

I've been looking at various functions to add additional rows including rbind and mutate but I'm struggling to perform the calculation in the newly created row.

Body:
An option with tidyverse

library(tidyverse)
df1 %>%
    summarise_if(is.numeric, diff) %>%
    abs %>%
    bind_rows(df1, .) %>%
    mutate(Month = replace_na(Month, "Change"))
#   Month A B C D E F
#1    Jan 1 2 4 8 4 1
#2    Feb 1 1 4 5 2 0
#3 Change 0 1 0 3 2 1

data

df1 <- structure(list(Month = c("Jan", "Feb"), A = c(1L, 1L), B = 2:1,
    C = c(4L, 4L), D = c(8L, 5L), E = c(4L, 2L), F = 1:0),
    class = "data.frame", row.names = c(NA,
-2L))


问:
What could prevent establishing a connection between two web api apps in Azure?
I have two web API apps (App Service resources) in Azure: app1 and app2.  I'm trying to send https requests from app1 to app2 but connections fail with this error:

"An error occurred while sending the request. Unable to read data
  from the transport connection: An existing connection was forcibly
  closed by the remote host"

When I run app1 on my local dev machine (e.g. from VS 2017) I can connect to app2 in Azure with no problems.

I use the following code to POST a request from app1 to app2:

```<code>using (HttpClient client = new HttpClient())
{
    HttpResponseMessage response = await client.PostAsync(uri, stringContent);
}
</code>```

答:
I had the same situation (App Service A calling App Service B) and finally found the problem.

In SSL Settings of App Service B, I had to lower Minimum TLS Version to 1.0.

Of course it would be better to make App Service A use a higher TLS version, but I am unsure how to set this.

Body:
I have two web API apps (App Service resources) in Azure: app1 and app2.  I'm trying to send https requests from app1 to app2 but connections fail with this error:

"An error occurred while sending the request. Unable to read data
  from the transport connection: An existing connection was forcibly
  closed by the remote host"

When I run app1 on my local dev machine (e.g. from VS 2017) I can connect to app2 in Azure with no problems.

I use the following code to POST a request from app1 to app2:

using (HttpClient client = new HttpClient())
{
    HttpResponseMessage response = await client.PostAsync(uri, stringContent);
}


问:
do_compile fails when building an image using the yocto-project "no such file or directory"
I tried to append a layer from git (https://github.com/sigysmund/meta-lora-net) to my Image and whenever I try to bitbake one of the recipes (the other worked), I get this error message:

```<code>ERROR: lora-pkt-fwd-4.0.1-r0 do_configure: oe_runmake failed
ERROR: lora-pkt-fwd-4.0.1-r0 do_configure: Function failed: do_configure (log file is located at /home/kilian/mlinux-4.x/build/tmp/work/arm926ejste-mlinux-linux-gnueabi/lora-pkt-fwd/4.0.1-r0/temp/log.do_configure.23440)
ERROR: Logfile of failure stored in: /home/kilian/mlinux-4.x/build/tmp/work/arm926ejste-mlinux-linux-gnueabi/lora-pkt-fwd/4.0.1-r0/temp/log.do_configure.23440
</code>```

(see last part for full message)

I have already tried to edit the bb file.
I changed the DEPENDS to "lora-gateway pkgconfig" instead of just the name since I read on another thread, that this can produce Issues but it didn't help.
This is the bb file of the recipe:

```<code>SUMMARY = "LoRa network packet forwarder project"
SECTION = "libs/network"
LICENSE = "MIT"
LIC_FILES_CHKSUM =                "file://LICENSE;md5=22af7693d7b76ef0fc76161c4be76c45"

SRC_URI = "https://github.com/Lora-net/packet_forwarder/archive/v${PV}.tar.gz"

SRC_URI[md5sum] = "a1f942e0cc7b02d604b11c8cb5f2a029"
SRC_URI[sha256sum] = "e68fadf6f1d2e5e7b601e504d5efb48b0a8f374c2c29c0476ab2fe9db68d33ae"

#old: DEPENDS += "lora-gateway"
# since new release of yocto you should add pkg-config
DEPENDS += "lora-gateway pkgconfig"

S = "${WORKDIR}/packet_forwarder-${PV}"

CFLAGS_append = "-I ${includedir}/libloragw -I ${S}/lora_pkt_fwd/inc -I ${S}/util_tx_test/inc "

do_configure_prepend() {
export LGW_PATH="${STAGING_LIBDIR}/libloragw"
}

do_compile_prepend() {
    export LGW_PATH="${STAGING_LIBDIR}/libloragw"
}

do_install() {
install -d ${D}${bindir}
install -d ${D}${docdir}/lora-pkt-fwd/conf

install ${B}/lora_pkt_fwd/lora_pkt_fwd ${D}${bindir}
install ${B}/util_*/util_* ${D}${bindir}

install -D -m 0644 ${S}/PROTOCOL.TXT ${D}${docdir}/lora-pkt-fwd/PROTOCOL.TXT
install -D -m 0644 ${S}/lora_pkt_fwd/readme.md ${D}${docdir}/lora-pkt-fwd/readme.md
install -D -m 0644 ${S}/lora_pkt_fwd/global_conf.json ${D}${docdir}/lora-pkt-fwd/global_conf.json
install -D -m 0644 ${S}/lora_pkt_fwd/local_conf.json ${D}${docdir}/lora-pkt-fwd/local_conf.json
install -D -m 0755 ${S}/lora_pkt_fwd/update_gwid.sh ${D}${docdir}/lora-pkt-fwd
install -D -m 0644 ${S}/lora_pkt_fwd/cfg/*.json.* ${D}${docdir}/lora-pkt-fwd/conf

rm -f ${D}${bindir}/util_tx_test
rm -f ${D}${bindir}/.debug/util_tx_test
}

PACKAGE_DEBUG_SPLIT_STYLE = "debug-without-src"

PACKAGES = "${PN}-dbg ${PN} ${PN}-doc"

# Avoid QA Issue: No GNU_HASH in the elf binary
INSANE_SKIP_${PN} = "ldflags"

FILES_${PN}-dbg = " \
    ${bindir}/.debug \
"
FILES_${PN} = " \
    ${bindir}/* \
"
FILES_${PN}-doc = " \
    ${docdir} \
"
</code>```

the full error is:

```<code>ERROR: lora-pkt-fwd-4.0.1-r0 do_configure: oe_runmake failed
ERROR: lora-pkt-fwd-4.0.1-r0 do_configure: Function failed: do_configure (log file is located at /home/kilian/mlinux-4.x/build/tmp/work/arm926ejste-mlinux-linux-gnueabi/lora-pkt-fwd/4.0.1-r0/temp/log.do_configure.23440)
ERROR: Logfile of failure stored in: /home/kilian/mlinux-4.x/build/tmp/work/arm926ejste-mlinux-linux-gnueabi/lora-pkt-fwd/4.0.1-r0/temp/log.do_configure.23440

Log data follows:
| DEBUG: Executing python function sysroot_cleansstate
| DEBUG: Python function sysroot_cleansstate finished
| DEBUG: Executing shell function do_configure
| NOTE: make clean
| ERROR: oe_runmake failed
| make clean -e -C lora_pkt_fwd
| make[1]: Entering directory `/home/kilian/mlinux-4.x/build/tmp/work/arm926ejste-mlinux-linux-gnueabi/lora-pkt-fwd/4.0.1-r0/packet_forwarder-4.0.1/lora_pkt_fwd'
| Makefile:17: /home/kilian/mlinux-4.x/build/tmp/sysroots/mtcdt/usr/lib/libloragw/library.cfg: No such file or directory
| make[1]: *** No rule to make target `/home/kilian/mlinux-4.x/build/tmp/sysroots/mtcdt/usr/lib/libloragw/library.cfg'.  Stop.
| make[1]: Leaving directory `/home/kilian/mlinux-4.x/build/tmp/work/arm926ejste-mlinux-linux-gnueabi/lora-pkt-fwd/4.0.1-r0/packet_forwarder-4.0.1/lora_pkt_fwd'
| make: *** [clean] Error 2
| WARNING: exit code 1 from a shell command.
| ERROR: Function failed: do_configure (log file is located at /home/kilian/mlinux-4.x/build/tmp/work/arm926ejste-mlinux-linux-gnueabi/lora-pkt-fwd/4.0.1-r0/temp/log.do_configure.23440)
ERROR: Task (/home/kilian/mlinux-4.x/layers/meta-custom/recipes-wireless/lora-pkt-fwd/lora-pkt-fwd_4.0.1.bb:do_configure) failed with exit code '1'
NOTE: Tasks Summary: Attempted 1590 tasks of which 1580 didn't need to be rerun and 1 failed.

Summary: 1 task failed:
  /home/kilian/mlinux-4.x/layers/meta-custom/recipes-wireless/lora-pkt-fwd/lora-pkt-fwd_4.0.1.bb:do_configure
Summary: There was 1 WARNING message shown.
Summary: There were 2 ERROR messages shown, returning a non-zero exit code.
</code>```

I know that is has got something to do with the libloragw/library.cfg file but since that is imported and I don't know enough about these scripts, I can't find the error.

Any hint is really appreciated!

P.s I didn't include the logfiles since everything in there is already in the Log data follows part of the error. If you need it I can upload it.

Edit:
Thank  you jku, appending CLEANBROKEN = "1" fixed this error.
Sadly I am getting  a new one:

```<code>ERROR: lora-pkt-fwd-4.0.1-r0 do_compile: oe_runmake failed
ERROR: lora-pkt-fwd-4.0.1-r0 do_compile: Function failed: do_compile (log file is located at /home/kilian/mlinux-4.x/build/tmp/work/arm926ejste-mlinux-linux-gnueabi/lora-pkt-fwd/4.0.1-r0/temp/log.do_compile.24486)
ERROR: Logfile of failure stored in: /home/kilian/mlinux-4.x/build/tmp/work/arm926ejste-mlinux-linux-gnueabi/lora-pkt-fwd/4.0.1-r0/temp/log.do_compile.24486
Log data follows:
| DEBUG: Executing shell function do_compile
| NOTE: make -j 2
| ERROR: oe_runmake failed
| make all -e -C lora_pkt_fwd
| make[1]: Entering directory `/home/kilian/mlinux-4.x/build/tmp/work/arm926ejste-mlinux-linux-gnueabi/lora-pkt-fwd/4.0.1-r0/packet_forwarder-4.0.1/lora_pkt_fwd'
| Makefile:17: /home/kilian/mlinux-4.x/build/tmp/sysroots/mtcdt/usr/lib/libloragw/library.cfg: No such file or directory
| make[1]: *** No rule to make target `/home/kilian/mlinux-4.x/build/tmp/sysroots/mtcdt/usr/lib/libloragw/library.cfg'.  Stop.
| make[1]: Leaving directory `/home/kilian/mlinux-4.x/build/tmp/work/arm926ejste-mlinux-linux-gnueabi/lora-pkt-fwd/4.0.1-r0/packet_forwarder-4.0.1/lora_pkt_fwd'
| make: *** [all] Error 2
| WARNING: exit code 1 from a shell command.
| ERROR: Function failed: do_compile (log file is located at /home/kilian/mlinux-4.x/build/tmp/work/arm926ejste-mlinux-linux-gnueabi/lora-pkt-fwd/4.0.1-r0/temp/log.do_compile.24486)
</code>```

So there is still a problem with the libloragw/library.cfg file

I did check and there is such a file in the recipe given by the DEPENDS variable so I guess that including that and the corresponding commands don't work

```<code>CFLAGS_append = "-I ${includedir}/libloragw -I ${S}/lora_pkt_fwd/inc -I ${S}/util_tx_test/inc "

do_configure_prepend() {
export LGW_PATH="${STAGING_LIBDIR}/libloragw"
}

do_compile_prepend() {
    export LGW_PATH="${STAGING_LIBDIR}/libloragw"
}
</code>```

But I still don't know what to edit so any help is precious.

答:
```<code>| DEBUG: Executing shell function do_configure
| NOTE: make clean
| ERROR: oe_runmake failed
| make clean -e -C lora_pkt_fwd
</code>```

Yocto/OpenEmbedded tries to run "make clean" during configure to ensure a reproducible build but this particular project fails to do so cleanly. You can either fix the project (patching the Makefile) or add

```<code>CLEANBROKEN = "1"
</code>```

in the recipe: this way OpenEmbedded won't run make clean on configure.

Body:
I tried to append a layer from git (https://github.com/sigysmund/meta-lora-net) to my Image and whenever I try to bitbake one of the recipes (the other worked), I get this error message:

ERROR: lora-pkt-fwd-4.0.1-r0 do_configure: oe_runmake failed
ERROR: lora-pkt-fwd-4.0.1-r0 do_configure: Function failed: do_configure (log file is located at /home/kilian/mlinux-4.x/build/tmp/work/arm926ejste-mlinux-linux-gnueabi/lora-pkt-fwd/4.0.1-r0/temp/log.do_configure.23440)
ERROR: Logfile of failure stored in: /home/kilian/mlinux-4.x/build/tmp/work/arm926ejste-mlinux-linux-gnueabi/lora-pkt-fwd/4.0.1-r0/temp/log.do_configure.23440

(see last part for full message)

I have already tried to edit the bb file.
I changed the DEPENDS to "lora-gateway pkgconfig" instead of just the name since I read on another thread, that this can produce Issues but it didn't help.
This is the bb file of the recipe:

SUMMARY = "LoRa network packet forwarder project"
SECTION = "libs/network"
LICENSE = "MIT"
LIC_FILES_CHKSUM =                "file://LICENSE;md5=22af7693d7b76ef0fc76161c4be76c45"

SRC_URI = "https://github.com/Lora-net/packet_forwarder/archive/v${PV}.tar.gz"

SRC_URI[md5sum] = "a1f942e0cc7b02d604b11c8cb5f2a029"
SRC_URI[sha256sum] = "e68fadf6f1d2e5e7b601e504d5efb48b0a8f374c2c29c0476ab2fe9db68d33ae"

#old: DEPENDS += "lora-gateway"
# since new release of yocto you should add pkg-config
DEPENDS += "lora-gateway pkgconfig"

S = "${WORKDIR}/packet_forwarder-${PV}"

CFLAGS_append = "-I ${includedir}/libloragw -I ${S}/lora_pkt_fwd/inc -I ${S}/util_tx_test/inc "

do_configure_prepend() {
export LGW_PATH="${STAGING_LIBDIR}/libloragw"
}

do_compile_prepend() {
    export LGW_PATH="${STAGING_LIBDIR}/libloragw"
}

do_install() {
install -d ${D}${bindir}
install -d ${D}${docdir}/lora-pkt-fwd/conf

install ${B}/lora_pkt_fwd/lora_pkt_fwd ${D}${bindir}
install ${B}/util_*/util_* ${D}${bindir}

install -D -m 0644 ${S}/PROTOCOL.TXT ${D}${docdir}/lora-pkt-fwd/PROTOCOL.TXT
install -D -m 0644 ${S}/lora_pkt_fwd/readme.md ${D}${docdir}/lora-pkt-fwd/readme.md
install -D -m 0644 ${S}/lora_pkt_fwd/global_conf.json ${D}${docdir}/lora-pkt-fwd/global_conf.json
install -D -m 0644 ${S}/lora_pkt_fwd/local_conf.json ${D}${docdir}/lora-pkt-fwd/local_conf.json
install -D -m 0755 ${S}/lora_pkt_fwd/update_gwid.sh ${D}${docdir}/lora-pkt-fwd
install -D -m 0644 ${S}/lora_pkt_fwd/cfg/*.json.* ${D}${docdir}/lora-pkt-fwd/conf

rm -f ${D}${bindir}/util_tx_test
rm -f ${D}${bindir}/.debug/util_tx_test
}

PACKAGE_DEBUG_SPLIT_STYLE = "debug-without-src"

PACKAGES = "${PN}-dbg ${PN} ${PN}-doc"

# Avoid QA Issue: No GNU_HASH in the elf binary
INSANE_SKIP_${PN} = "ldflags"

FILES_${PN}-dbg = " \
    ${bindir}/.debug \
"
FILES_${PN} = " \
    ${bindir}/* \
"
FILES_${PN}-doc = " \
    ${docdir} \
"

the full error is:

ERROR: lora-pkt-fwd-4.0.1-r0 do_configure: oe_runmake failed
ERROR: lora-pkt-fwd-4.0.1-r0 do_configure: Function failed: do_configure (log file is located at /home/kilian/mlinux-4.x/build/tmp/work/arm926ejste-mlinux-linux-gnueabi/lora-pkt-fwd/4.0.1-r0/temp/log.do_configure.23440)
ERROR: Logfile of failure stored in: /home/kilian/mlinux-4.x/build/tmp/work/arm926ejste-mlinux-linux-gnueabi/lora-pkt-fwd/4.0.1-r0/temp/log.do_configure.23440

Log data follows:
| DEBUG: Executing python function sysroot_cleansstate
| DEBUG: Python function sysroot_cleansstate finished
| DEBUG: Executing shell function do_configure
| NOTE: make clean
| ERROR: oe_runmake failed
| make clean -e -C lora_pkt_fwd
| make[1]: Entering directory `/home/kilian/mlinux-4.x/build/tmp/work/arm926ejste-mlinux-linux-gnueabi/lora-pkt-fwd/4.0.1-r0/packet_forwarder-4.0.1/lora_pkt_fwd'
| Makefile:17: /home/kilian/mlinux-4.x/build/tmp/sysroots/mtcdt/usr/lib/libloragw/library.cfg: No such file or directory
| make[1]: *** No rule to make target `/home/kilian/mlinux-4.x/build/tmp/sysroots/mtcdt/usr/lib/libloragw/library.cfg'.  Stop.
| make[1]: Leaving directory `/home/kilian/mlinux-4.x/build/tmp/work/arm926ejste-mlinux-linux-gnueabi/lora-pkt-fwd/4.0.1-r0/packet_forwarder-4.0.1/lora_pkt_fwd'
| make: *** [clean] Error 2
| WARNING: exit code 1 from a shell command.
| ERROR: Function failed: do_configure (log file is located at /home/kilian/mlinux-4.x/build/tmp/work/arm926ejste-mlinux-linux-gnueabi/lora-pkt-fwd/4.0.1-r0/temp/log.do_configure.23440)
ERROR: Task (/home/kilian/mlinux-4.x/layers/meta-custom/recipes-wireless/lora-pkt-fwd/lora-pkt-fwd_4.0.1.bb:do_configure) failed with exit code '1'
NOTE: Tasks Summary: Attempted 1590 tasks of which 1580 didn't need to be rerun and 1 failed.

Summary: 1 task failed:
  /home/kilian/mlinux-4.x/layers/meta-custom/recipes-wireless/lora-pkt-fwd/lora-pkt-fwd_4.0.1.bb:do_configure
Summary: There was 1 WARNING message shown.
Summary: There were 2 ERROR messages shown, returning a non-zero exit code.

I know that is has got something to do with the libloragw/library.cfg file but since that is imported and I don't know enough about these scripts, I can't find the error.

Any hint is really appreciated!

P.s I didn't include the logfiles since everything in there is already in the Log data follows part of the error. If you need it I can upload it.

Edit:
Thank  you jku, appending CLEANBROKEN = "1" fixed this error.
Sadly I am getting  a new one:

ERROR: lora-pkt-fwd-4.0.1-r0 do_compile: oe_runmake failed
ERROR: lora-pkt-fwd-4.0.1-r0 do_compile: Function failed: do_compile (log file is located at /home/kilian/mlinux-4.x/build/tmp/work/arm926ejste-mlinux-linux-gnueabi/lora-pkt-fwd/4.0.1-r0/temp/log.do_compile.24486)
ERROR: Logfile of failure stored in: /home/kilian/mlinux-4.x/build/tmp/work/arm926ejste-mlinux-linux-gnueabi/lora-pkt-fwd/4.0.1-r0/temp/log.do_compile.24486
Log data follows:
| DEBUG: Executing shell function do_compile
| NOTE: make -j 2
| ERROR: oe_runmake failed
| make all -e -C lora_pkt_fwd
| make[1]: Entering directory `/home/kilian/mlinux-4.x/build/tmp/work/arm926ejste-mlinux-linux-gnueabi/lora-pkt-fwd/4.0.1-r0/packet_forwarder-4.0.1/lora_pkt_fwd'
| Makefile:17: /home/kilian/mlinux-4.x/build/tmp/sysroots/mtcdt/usr/lib/libloragw/library.cfg: No such file or directory
| make[1]: *** No rule to make target `/home/kilian/mlinux-4.x/build/tmp/sysroots/mtcdt/usr/lib/libloragw/library.cfg'.  Stop.
| make[1]: Leaving directory `/home/kilian/mlinux-4.x/build/tmp/work/arm926ejste-mlinux-linux-gnueabi/lora-pkt-fwd/4.0.1-r0/packet_forwarder-4.0.1/lora_pkt_fwd'
| make: *** [all] Error 2
| WARNING: exit code 1 from a shell command.
| ERROR: Function failed: do_compile (log file is located at /home/kilian/mlinux-4.x/build/tmp/work/arm926ejste-mlinux-linux-gnueabi/lora-pkt-fwd/4.0.1-r0/temp/log.do_compile.24486)

So there is still a problem with the libloragw/library.cfg file

I did check and there is such a file in the recipe given by the DEPENDS variable so I guess that including that and the corresponding commands don't work

CFLAGS_append = "-I ${includedir}/libloragw -I ${S}/lora_pkt_fwd/inc -I ${S}/util_tx_test/inc "

do_configure_prepend() {
export LGW_PATH="${STAGING_LIBDIR}/libloragw"
}

do_compile_prepend() {
    export LGW_PATH="${STAGING_LIBDIR}/libloragw"
}

But I still don't know what to edit so any help is precious.

Body:
| DEBUG: Executing shell function do_configure
| NOTE: make clean
| ERROR: oe_runmake failed
| make clean -e -C lora_pkt_fwd

Yocto/OpenEmbedded tries to run "make clean" during configure to ensure a reproducible build but this particular project fails to do so cleanly. You can either fix the project (patching the Makefile) or add

CLEANBROKEN = "1"

in the recipe: this way OpenEmbedded won't run make clean on configure.


问:
Does a moving average preserve resolution over sum / count quotient in a binary system?
There are multiple methods for find the average of a set of numbers.

First, the sum / count quotient. Add all values and divide them by the number of values.

Second, the moving average. The function I found in another Stack answer is:

New average = old average * (n-1)/n + new value /n

This works as long as each value is added to the average one value at a time.

My concern is that the second method is more calculatively complex for my processor to execute, but I also fear that the first method will result in a loss of resolution for data sets that result in large sums. In a 32 bit system, for example, the resolution of a float value stored is reduced automatically as the magnitude of the number grows.

Does a moving average preserve resolution?

答:
"moving average" does not calculate average over large interval.

It smooth data in such way that newer measurements give larger impact, and older measurements weight becomes smaller and smaller.

If you bother about large sums and want to preserve all possible data "bits", consider special methods like Kahan summation algorithm


问:
Why the code doesn't recognize the assigned variable?
I am trying to write a program in Python 3 that takes an int year larger than 1970 as an input and returns the increase in temperature from 1970 until that year. The formulas and constants are given. Python throws me an error:

```<code>File "4.5.py", line 28, in <module>
    int_years = int_year - 1970
NameError: name 'int_year' is not defined
</code>```

I am a beginner in Python so I have browsed possible solutions, but could not find any working ones.

```<code>def user_input():
    while True:
        int_year = int(input("Please enter a year greater than 1970 " ))
        try:
            if int_year > 1970:
                break
            else:
                print("Please enter a year greater than 1970")
        except ValueError:
            print ("It is not a valid year. Try again. ")
    return int_year

"""CO2 level of January 1970"""
c0 = 325.03

"""Current levels of CO2"""
c1 = 411.97

"""Difference in CO2 levels between 1970 and now"""
differenceCO = c1-c0

"""The average CO2 increase per year since 1970"""
per_year_changedCO = ((differenceCO)/(2019-1970))

"""Diffrence in years between 1970 and user input year"""
int_years = int_year - 1970

"""A projected CO2 level in user input year"""
int_year_changedCO = c0+((int_years)*(per_year_changedCO))

"""A projected RF in any year"""
RF = 5.35*(math.log((int_year_changedCO)/(c0)))

"""Increase in temperature from 1970 to user input year"""
def predict_increase():
  temp_int_year = 0.5 * RF
  return temp_int_year

print(temp_int_year)
</code>```

I expect the program to recognize the variables I use in functions. Overall, I will appreciate any comments on the code.

答:
you dont call your functions anywhere, variables temp_int_year and int_year are Local variables of functions and they can't be accessed from outside, when the function call has finished

you need to call function and save return value into  temp_int_year and int_year:

```<code>int_year = user_input()

temp_int_year = predict_increase()
print(temp_int_year)
</code>```

NOTE: you have the same mistake with temp_int_year

full code:

```<code>import math
def user_input():
    while True:
        int_year = int(input("Please enter a year greater than 1970 " ))
        try:
            if int_year > 1970:
                break
            else:
                print("Please enter a year greater than 1970")
        except ValueError:
            print ("It is not a valid year. Try again. ")
    return int_year

int_year = user_input()

"""CO2 level of January 1970"""
c0 = 325.03

"""Current levels of CO2"""
c1 = 411.97

"""Difference in CO2 levels between 1970 and now"""
differenceCO = c1-c0

"""The average CO2 increase per year since 1970"""
per_year_changedCO = ((differenceCO)/(2019-1970))

"""Diffrence in years between 1970 and user input year"""
int_years = int_year - 1970

"""A projected CO2 level in user input year"""
int_year_changedCO = c0+((int_years)*(per_year_changedCO))

"""A projected RF in any year"""
RF = 5.35*(math.log((int_year_changedCO)/(c0)))

"""Increase in temperature from 1970 to user input year"""
def predict_increase():
  temp_int_year = 0.5 * RF
  return temp_int_year

temp_int_year = predict_increase()
print(temp_int_year)
</code>```

Body:
I am trying to write a program in Python 3 that takes an int year larger than 1970 as an input and returns the increase in temperature from 1970 until that year. The formulas and constants are given. Python throws me an error:

File "4.5.py", line 28, in <module>
    int_years = int_year - 1970
NameError: name 'int_year' is not defined

I am a beginner in Python so I have browsed possible solutions, but could not find any working ones.

def user_input():
    while True:
        int_year = int(input("Please enter a year greater than 1970 " ))
        try:
            if int_year > 1970:
                break
            else:
                print("Please enter a year greater than 1970")
        except ValueError:
            print ("It is not a valid year. Try again. ")
    return int_year

"""CO2 level of January 1970"""
c0 = 325.03

"""Current levels of CO2"""
c1 = 411.97

"""Difference in CO2 levels between 1970 and now"""
differenceCO = c1-c0

"""The average CO2 increase per year since 1970"""
per_year_changedCO = ((differenceCO)/(2019-1970))

"""Diffrence in years between 1970 and user input year"""
int_years = int_year - 1970

"""A projected CO2 level in user input year"""
int_year_changedCO = c0+((int_years)*(per_year_changedCO))

"""A projected RF in any year"""
RF = 5.35*(math.log((int_year_changedCO)/(c0)))

"""Increase in temperature from 1970 to user input year"""
def predict_increase():
  temp_int_year = 0.5 * RF
  return temp_int_year

print(temp_int_year)

I expect the program to recognize the variables I use in functions. Overall, I will appreciate any comments on the code.

Body:
you dont call your functions anywhere, variables temp_int_year and int_year are Local variables of functions and they can't be accessed from outside, when the function call has finished

you need to call function and save return value into  temp_int_year and int_year:

int_year = user_input()

temp_int_year = predict_increase()
print(temp_int_year)

NOTE: you have the same mistake with temp_int_year

full code:

import math
def user_input():
    while True:
        int_year = int(input("Please enter a year greater than 1970 " ))
        try:
            if int_year > 1970:
                break
            else:
                print("Please enter a year greater than 1970")
        except ValueError:
            print ("It is not a valid year. Try again. ")
    return int_year

int_year = user_input()

"""CO2 level of January 1970"""
c0 = 325.03

"""Current levels of CO2"""
c1 = 411.97

"""Difference in CO2 levels between 1970 and now"""
differenceCO = c1-c0

"""The average CO2 increase per year since 1970"""
per_year_changedCO = ((differenceCO)/(2019-1970))

"""Diffrence in years between 1970 and user input year"""
int_years = int_year - 1970

"""A projected CO2 level in user input year"""
int_year_changedCO = c0+((int_years)*(per_year_changedCO))

"""A projected RF in any year"""
RF = 5.35*(math.log((int_year_changedCO)/(c0)))

"""Increase in temperature from 1970 to user input year"""
def predict_increase():
  temp_int_year = 0.5 * RF
  return temp_int_year

temp_int_year = predict_increase()
print(temp_int_year)


问:
JerseyTest and JUnit throws NullPointerException
I have some problems with the jersey test framework. If i use the @Before and @After annotations, then the target method throws a NullPointerException.
I thought JerseyTest works with JUnit? Where is my problem?

Jersey: 2.12

JUnit: 4.11

Code that fails:

```<code>public class MyResourceTest extends JerseyTest {
    @Before
    public void setUp() { }

    @After
    public void tearDown() { }

    @Override
    protected Application configure() {
        return new ResourceConfig(MyResource.class);
    }

    @Test
    public void SHOULD_RETURN_BAD_REQUEST() throws IOException {
        System.out.println(target("myPath"));
        assertEquals(1, 1);
    }
}
</code>```

Result:

java.lang.NullPointerException    at
  org.glassfish.jersey.test.JerseyTest.target(JerseyTest.java:566)  at
  org.glassfish.jersey.test.JerseyTest.target(JerseyTest.java:580)  at
  foo.bar.MyResourceTest.SHOULD_RETURN_BAD_REQUEST(MyResourceTest.java:43)

Code that works:

```<code>public class MyResourceTest extends JerseyTest {
    @Override
    protected Application configure() {
        return new ResourceConfig(MyResource.class);
    }

    @Test
    public void SHOULD_RETURN_BAD_REQUEST() throws IOException {
        System.out.println(target("myPath"));
        assertEquals(1, 1);
    }
}
</code>```

Result:

```<code>JerseyWebTarget { http://localhost:9998/myPath }
</code>```

答:
I came here because I was using JUnit 5 and it seems that it wasn't seeing the @Before and @After annotations on the JerseyTest setup/tearDown methods. I had to override them and use the new JUnit 5 annotations</p>

```<code>public class MyResourceTest extends JerseyTest {
    @BeforeEach
    @Override
    public void setUp() throws Exception {
        super.setUp();
    }

    @AfterEach
    @Override
    public void tearDown() throws Exception {
        super.tearDown();
    }

    @Override
    protected Application configure() {
        return new ResourceConfig(MyResource.class);
    }

    @Test
    public void SHOULD_RETURN_BAD_REQUEST() throws IOException {
        System.out.println(target("myPath"));
        assertEquals(1, 1);
    }
}
</code>```

Body:
I have some problems with the jersey test framework. If i use the @Before and @After annotations, then the target method throws a NullPointerException.
I thought JerseyTest works with JUnit? Where is my problem?

Jersey: 2.12

JUnit: 4.11

Code that fails:

public class MyResourceTest extends JerseyTest {
    @Before
    public void setUp() { }

    @After
    public void tearDown() { }

    @Override
    protected Application configure() {
        return new ResourceConfig(MyResource.class);
    }

    @Test
    public void SHOULD_RETURN_BAD_REQUEST() throws IOException {
        System.out.println(target("myPath"));
        assertEquals(1, 1);
    }
}

Result:

java.lang.NullPointerException    at
  org.glassfish.jersey.test.JerseyTest.target(JerseyTest.java:566)  at
  org.glassfish.jersey.test.JerseyTest.target(JerseyTest.java:580)  at
  foo.bar.MyResourceTest.SHOULD_RETURN_BAD_REQUEST(MyResourceTest.java:43)

Code that works:

public class MyResourceTest extends JerseyTest {
    @Override
    protected Application configure() {
        return new ResourceConfig(MyResource.class);
    }

    @Test
    public void SHOULD_RETURN_BAD_REQUEST() throws IOException {
        System.out.println(target("myPath"));
        assertEquals(1, 1);
    }
}

Result:

JerseyWebTarget { http://localhost:9998/myPath }

Body:
I came here because I was using JUnit 5 and it seems that it wasn't seeing the @Before and @After annotations on the JerseyTest setup/tearDown methods. I had to override them and use the new JUnit 5 annotations

public class MyResourceTest extends JerseyTest {
    @BeforeEach
    @Override
    public void setUp() throws Exception {
        super.setUp();
    }

    @AfterEach
    @Override
    public void tearDown() throws Exception {
        super.tearDown();
    }

    @Override
    protected Application configure() {
        return new ResourceConfig(MyResource.class);
    }

    @Test
    public void SHOULD_RETURN_BAD_REQUEST() throws IOException {
        System.out.println(target("myPath"));
        assertEquals(1, 1);
    }
}


问:
How to get float values from PostgreSQL table as float only instead of Decimal in psycopg2?
I am using psycopg2 for querying PostgreSQL database.

```<code>import psycopg2
import psycopg2.extras

DB_CONNECTION = {
  'host': os.getenv('PG_HOST'),
  'database': os.getenv('PG_DB_NAME'),
  'user': os.getenv('PG_USER'),
  'password': os.getenv('PG_PASSWORD')
}

connection = psycopg2.connect(**DB_CONNECTION)
connection.autocommit = True
cursor = connection.cursor(cursor_factory = psycopg2.extras.RealDictCursor)

# amount column type is numeric in table
query = 'SELECT userid, amount FROM some_table WHERE userid = %(userid)s'
cursor.execute(query, {'userid': 1234})
rows = cursor.fetchall()
print(rows[0]['amount']) # This is value is object of decimal.Decimal
</code>```

rows[0]['amount'] is object of decimal.Decimal class. Is there any simple and clean way to get this value as float? I do not want cast this into float by float(rows[0]['amount']) manually.

答:
Summary - you can register a custom type conversion function based on the OID of the type returned in the query. The psycopg2 docs give this exact example:

```<code>DEC2FLOAT = psycopg2.extensions.new_type(
    psycopg2.extensions.DECIMAL.values,
    'DEC2FLOAT',
    lambda value, curs: float(value) if value is not None else None)
psycopg2.extensions.register_type(DEC2FLOAT)
</code>```

See -

http://initd.org/psycopg/docs/advanced.html#type-casting-of-sql-types-into-python-objects

http://initd.org/psycopg/docs/faq.html#problems-with-type-conversions

You can also cast in the SQL itself - select userid, amount::float from some_table ..., but that seems to violate your requirement for avoiding specific casts.

Body:
I am using psycopg2 for querying PostgreSQL database.

import psycopg2
import psycopg2.extras

DB_CONNECTION = {
  'host': os.getenv('PG_HOST'),
  'database': os.getenv('PG_DB_NAME'),
  'user': os.getenv('PG_USER'),
  'password': os.getenv('PG_PASSWORD')
}

connection = psycopg2.connect(**DB_CONNECTION)
connection.autocommit = True
cursor = connection.cursor(cursor_factory = psycopg2.extras.RealDictCursor)

# amount column type is numeric in table
query = 'SELECT userid, amount FROM some_table WHERE userid = %(userid)s'
cursor.execute(query, {'userid': 1234})
rows = cursor.fetchall()
print(rows[0]['amount']) # This is value is object of decimal.Decimal

rows[0]['amount'] is object of decimal.Decimal class. Is there any simple and clean way to get this value as float? I do not want cast this into float by float(rows[0]['amount']) manually.

Body:
Summary - you can register a custom type conversion function based on the OID of the type returned in the query. The psycopg2 docs give this exact example:

DEC2FLOAT = psycopg2.extensions.new_type(
    psycopg2.extensions.DECIMAL.values,
    'DEC2FLOAT',
    lambda value, curs: float(value) if value is not None else None)
psycopg2.extensions.register_type(DEC2FLOAT)

See -

http://initd.org/psycopg/docs/advanced.html#type-casting-of-sql-types-into-python-objects

http://initd.org/psycopg/docs/faq.html#problems-with-type-conversions

You can also cast in the SQL itself - select userid, amount::float from some_table ..., but that seems to violate your requirement for avoiding specific casts.


问:
Why is typescript object in false branch possibly undefined?
I have the following typescript code, in which obj is definitely not undefined within the outer "if" (and thus also within the inner "if"), and yet the typescript 3.4.5 compiler gives me a warning:

let obj: {'x': number} | undefined
obj = Math.random() > 0.5 ? {x: 1} : undefined
if (obj) {
  if (false) {
    obj['x'] += 1 # "obj is possibly undefined" here: huh?
  }
}
```

If I replace the inner "if" with if (true) it doesn't give a warning. Is this a typescript bug or something I'm doing wrong?

答:
I'd recommend letting the compiler in on the secret that false is actually coming from a global boolean option... by writing if (globalOption) and not if (false).  But maybe there's some kind of pre-build step which replaces your global option with the false literal or something?  Not sure I see the use case here.

Anyway, if you want to force the compiler to entertain that false might actually be true, you can use a type assertion to widen false to the boolean type:

```<code>let obj: {'x': number} | undefined
obj = Math.random() > 0.5 ? {x: 1} : undefined
if (obj) {
  if (false as boolean) { // assertion widening
    obj['x'] += 1  // no error
  }
}
</code>```

Link to code

That will stop the compiler from giving up on the control flow anaylsis and prevents the error. Okay, hope that helps.  Good luck!

Body:
I have the following typescript code, in which obj is definitely not undefined within the outer "if" (and thus also within the inner "if"), and yet the typescript 3.4.5 compiler gives me a warning:

let obj: {'x': number} | undefined
obj = Math.random() > 0.5 ? {x: 1} : undefined
if (obj) {
  if (false) {
    obj['x'] += 1 # "obj is possibly undefined" here: huh?
  }
}

If I replace the inner "if" with if (true) it doesn't give a warning. Is this a typescript bug or something I'm doing wrong?

Body:
I'd recommend letting the compiler in on the secret that false is actually coming from a global boolean option... by writing if (globalOption) and not if (false).  But maybe there's some kind of pre-build step which replaces your global option with the false literal or something?  Not sure I see the use case here.

Anyway, if you want to force the compiler to entertain that false might actually be true, you can use a type assertion to widen false to the boolean type:

let obj: {'x': number} | undefined
obj = Math.random() > 0.5 ? {x: 1} : undefined
if (obj) {
  if (false as boolean) { // assertion widening
    obj['x'] += 1  // no error
  }
}

Link to code

That will stop the compiler from giving up on the control flow anaylsis and prevents the error. Okay, hope that helps.  Good luck!


问:
Difference between (int ret = 0; !ret && ret !=0)
What the difference between:

```<code>1) !ret
2) ret != 0
</code>```

?

```<code>if (ret != 0 || stack[fd] == NULL || stack[fd][0] == '\0')
{
  if (!ret && *line)
    *line = NULL;
  return (ret);
}
</code>```

答:
They are exactly equivalent. (I assume you meant == instead of != because otherwise they are each others opposite and definitely not equivalent in any way.) I would recommend the first one when ret should be treated as a boolean and the second if you're comparing quantities.

However, their counterpart is not equivalent. ret is obviously not the same as ret != 1, since any value except for 0 is treated as "true". This may seem like a nobrainer, but it can actually cause problems if you're using regular ints as booleans together with #define true 1.

Body:
What the difference between:

1) !ret
2) ret != 0

?

if (ret != 0 || stack[fd] == NULL || stack[fd][0] == '\0')
{
  if (!ret && *line)
    *line = NULL;
  return (ret);
}


问:
What are the differences between extern, abstract, and partial for methods in an abstract class?
I am writing an abstract class because I want to provide a few commonly used methods, require a few methods that will be too specific, and allow some methods to "extended".  After bumping into a compiler error I am wondering if anybody can explain the differences between the extern, abstract, and partial keywords.  What do they mean and when/where should I use them?

答:
Extern: http://msdn.microsoft.com/en-us/library/e59b22c5%28v=vs.80%29.aspx

It is an error to use the abstract (C# Reference) and extern modifiers together to modify the same member. Using the extern modifier means that the method is implemented outside the C# code, while using the abstract modifier means that the method implementation is not provided in the class.

Abstract: http://msdn.microsoft.com/en-us/library/sf985hc5%28v=vs.80%29.aspx

Use the abstract modifier in a class declaration to indicate that a class is intended only to be a base class of other classes. Members marked as abstract, or included in an abstract class, must be implemented by classes that derive from the abstract class.

partial: http://msdn.microsoft.com/en-us/library/wbx7zzdd%28v=vs.80%29.aspx

Partial type definitions allow the definition of a class, struct or interface to be split into multiple files.


问:
running a phpUnit test within a docker container
i want to run a specific version of phpUnit WITHIN  a docker container. This container will use a specific version of php. i.e

php:5.6-apache

Its a Laravel application. i have install phpunit via composer on the hostfiles and then used the volume command to transfer this to the container.

my composer.json file has following entry:

"require-dev": {
          "phpunit/phpunit": "^5.0"
      },

This is my docker run  command to run the test on my testdev container:

```<code>docker run --rm -it -v ~/Users/mow/Documents/devFolder/testdev:/app testdev_php "php ./vendor/bin/phpunit"
</code>```

This returns the error:

```<code>exec: fatal: unable to exec php ./vendor/bin/phpunit: No such file or directory
</code>```

i am unclear why it says this because te vendor directory is at the root of my site directory.

this is my dockerfile

```<code>FROM php:5.6-apache

ENV S6_OVERLAY_VERSION 1.11.0.1

RUN apt-get update && apt-get install -y \
    libldap2-dev \
    git \
    --no-install-recommends \
    && rm -r /var/lib/apt/lists/* \
    && docker-php-ext-configure ldap --with-libdir=lib/x86_64-linux-gnu/ \
    && docker-php-ext-install ldap \
    && docker-php-ext-install mysqli pdo pdo_mysql

#install xdebug
RUN git clone https://github.com/xdebug/xdebug.git \
    && cd xdebug \
    && git checkout tags/XDEBUG_2_5_5 \
    && phpize \
    && ./configure --enable-xdebug \
    && make \
    && make install

RUN a2enmod rewrite

COPY ./docker/rootfs /
COPY . /app

WORKDIR /app

ENTRYPOINT ["/init"]
</code>```

I guess the real question is this: what is the correct way to run a phpUnit test within a docker container so that its subject to the php version within that container.

答: In the Entrypoint, you need to ran composer install to install the requisite packages that will be available in the docker container

Body:
i want to run a specific version of phpUnit WITHIN  a docker container. This container will use a specific version of php. i.e

php:5.6-apache

Its a Laravel application. i have install phpunit via composer on the hostfiles and then used the volume command to transfer this to the container.

my composer.json file has following entry:

"require-dev": {
          "phpunit/phpunit": "^5.0"
      },

This is my docker run  command to run the test on my testdev container:

docker run --rm -it -v ~/Users/mow/Documents/devFolder/testdev:/app testdev_php "php ./vendor/bin/phpunit"

This returns the error:

exec: fatal: unable to exec php ./vendor/bin/phpunit: No such file or directory

i am unclear why it says this because te vendor directory is at the root of my site directory.

this is my dockerfile

FROM php:5.6-apache

ENV S6_OVERLAY_VERSION 1.11.0.1

RUN apt-get update && apt-get install -y \
    libldap2-dev \
    git \
    --no-install-recommends \
    && rm -r /var/lib/apt/lists/* \
    && docker-php-ext-configure ldap --with-libdir=lib/x86_64-linux-gnu/ \
    && docker-php-ext-install ldap \
    && docker-php-ext-install mysqli pdo pdo_mysql

#install xdebug
RUN git clone https://github.com/xdebug/xdebug.git \
    && cd xdebug \
    && git checkout tags/XDEBUG_2_5_5 \
    && phpize \
    && ./configure --enable-xdebug \
    && make \
    && make install

RUN a2enmod rewrite

COPY ./docker/rootfs /
COPY . /app

WORKDIR /app

ENTRYPOINT ["/init"]

I guess the real question is this: what is the correct way to run a phpUnit test within a docker container so that its subject to the php version within that container.


问:
NLP - How to add more features?
I want to use a sklearn classifier to train a model to classify data entries (yes,no) using a text feature (content), a numerical feature (population) and a categorical feature (location).

The model below is using only the text data to classify each entry. The text is converted with TF-IDF into a sparse matrix before being imported into the classifier.

Is there a way to add/use also the other features? These features are not in sparse matrix format so not sure how to combine them with the text sparse matrix.

    #import libraries
    import string, re, nltk
    import pandas as pd
    from pandas import Series, DataFrame
    from nltk.corpus import stopwords
    from nltk.stem.porter import PorterStemmer
    from sklearn.feature_extraction.text import CountVectorizer
    from sklearn.feature_extraction.text import TfidfTransformer
    from sklearn.model_selection import train_test_split
    from sklearn.metrics import classification_report
    from sklearn.pipeline import Pipeline

    # read data and remove empty lines
    dataset = pd.read_csv('sample_data.txt',
                           sep='\t',
                           names=['content','location','population','target'])
                           .dropna(how='all')
                           .dropna(subset=['target'])

    df = dataset[1:]

    #reset dataframe index
    df.reset_index(inplace = True)

    #add an extra column which is the length of text
    df['length'] = df['content'].apply(len)

    #create a dataframe that contains only two columns the text and the target class
    df_cont = df.copy()
    df_cont = df_cont.drop(
        ['location','population','length'],axis = 1)

    # function that takes in a string of text, removes all punctuation, stopwords and returns a list of cleaned text

    def text_process(mess):
        # lower case for string
        mess = mess.lower()

        # check characters and removes URLs
       nourl = re.sub(r'http\S+', ' ', mess)

        # check characters and removes punctuation
        nopunc = [char for char in nourl if char not in string.punctuation]

        # join the characters again to form the string and removes numbers
        nopunc = ''.join([i for i in nopunc if not i.isdigit()])

        # remove stopwords
        return [ps.stem(word) for word in nopunc.split() if word not in set(stopwords.words('english'))]

    #split the data in train and test set and train/test the model

    cont_train, cont_test, target_train, target_test = train_test_split(df_cont['content'],df_cont['target'],test_size = 0.2,shuffle = True, random_state = 1)

    pipeline = Pipeline([('bag_of_words',CountVectorizer(analyzer=text_process)),
                         ('tfidf',TfidfTransformer()),
                         ('classifier',MultinomialNB())])

    pipeline.fit(cont_train,target_train)
    predictions = pipeline.predict(cont_test)

    print(classification_report(predictions,target_test))

```

The model is expected to return the following: accuracy, precision, recall ,f1-score

答:
You can convert your sparse matrix to numpy array using toarray method.

You will get a vector for each text entry which you can concat with other features.

Title: NLP - How to add more features?

Body:
I want to use a sklearn classifier to train a model to classify data entries (yes,no) using a text feature (content), a numerical feature (population) and a categorical feature (location).

The model below is using only the text data to classify each entry. The text is converted with TF-IDF into a sparse matrix before being imported into the classifier.

Is there a way to add/use also the other features? These features are not in sparse matrix format so not sure how to combine them with the text sparse matrix.

    #import libraries
    import string, re, nltk
    import pandas as pd
    from pandas import Series, DataFrame
    from nltk.corpus import stopwords
    from nltk.stem.porter import PorterStemmer
    from sklearn.feature_extraction.text import CountVectorizer
    from sklearn.feature_extraction.text import TfidfTransformer
    from sklearn.model_selection import train_test_split
    from sklearn.metrics import classification_report
    from sklearn.pipeline import Pipeline

    # read data and remove empty lines
    dataset = pd.read_csv('sample_data.txt',
                           sep='\t',
                           names=['content','location','population','target'])
                           .dropna(how='all')
                           .dropna(subset=['target'])

    df = dataset[1:]

    #reset dataframe index
    df.reset_index(inplace = True)

    #add an extra column which is the length of text
    df['length'] = df['content'].apply(len)

    #create a dataframe that contains only two columns the text and the target class
    df_cont = df.copy()
    df_cont = df_cont.drop(
        ['location','population','length'],axis = 1)

    # function that takes in a string of text, removes all punctuation, stopwords and returns a list of cleaned text

    def text_process(mess):
        # lower case for string
        mess = mess.lower()

        # check characters and removes URLs
       nourl = re.sub(r'http\S+', ' ', mess)

        # check characters and removes punctuation
        nopunc = [char for char in nourl if char not in string.punctuation]

        # join the characters again to form the string and removes numbers
        nopunc = ''.join([i for i in nopunc if not i.isdigit()])

        # remove stopwords
        return [ps.stem(word) for word in nopunc.split() if word not in set(stopwords.words('english'))]

    #split the data in train and test set and train/test the model

    cont_train, cont_test, target_train, target_test = train_test_split(df_cont['content'],df_cont['target'],test_size = 0.2,shuffle = True, random_state = 1)

    pipeline = Pipeline([('bag_of_words',CountVectorizer(analyzer=text_process)),
                         ('tfidf',TfidfTransformer()),
                         ('classifier',MultinomialNB())])

    pipeline.fit(cont_train,target_train)
    predictions = pipeline.predict(cont_test)

    print(classification_report(predictions,target_test))

The model is expected to return the following: accuracy, precision, recall ,f1-score


问:
Dynamically generate an accordion based on array data
I am reading mysql fine to pull a set of data given a particular ID, this returns an array to my script.  Thats the easy bit.  Example of which is here an is stored in an array called $history

```<code>Array ( [serviceID] => 1 [VesselTag] => 1000001 [component] => Engine 1 [item] => Caterpillar [serial] => 123456 [comment] => Something in here like this [parts] => Oil [lat] => 50.38313740 [longitude] => -4.03461360 [engineer] => 27 [date] => 2019-05-30 19:25:56 )
Array ( [serviceID] => 2 [VesselTag] => 1000001 [component] => Engine 2 [item] => Caterpillar [serial] => 677889 [comment] => Did a full overhaul of top section replaced everything [parts] => everything [lat] => 50.38309180 [longitude] => -4.03468820 [engineer] => 27 [date] => 2019-05-30 19:27:29 )
Array ( [serviceID] => 3 [VesselTag] => 1000001 [component] => Engine 1 [item] => Caterpillar [serial] => 123456 [comment] => This seems quite usable [parts] => Oil [lat] => 50.38345892 [longitude] => -4.03475649 [engineer] => 27 [date] => 2019-05-30 19:29:23 )
</code>```

What I would like to do is now generate an accordion 'card-body' grouping the cards based on [component]s from the list.  Sometime the array may have more or less, so there is going to be anywhere from 0 to 7 cards [components] showing.

```<code><div class="card">
  <div class="card-header">
    <a class="card-link" data-toggle="collapse" href="#collapseOne">
        $array[Component]  **<--- Guessing this would be from a ```foreach``` or ```while``` loop???**
    </a>
  </div>
  <div id="collapseOne" class="collapse" data-parent="#accordion">
    <div class="card-body">
      <?php **THIS IS WHERE I WILL DISPLAY THE ROWS OF DATA ASSOCIATED TO THE [component] IN QUESTION ?>**
    </div>
  </div>
</div>
</code>```

I am just really struggling getting my head around which loop to use, and to be able to drive it by the [components].

Its doing my head in......

答:
After quite some time looking at different articles I seem to have come up with something that works.....
I think it is relatively efficient, but happy for someone to tell me otherwise....</p>

```<code><div id="accordion">
    <?php $newArray=array();
            foreach($history as $val){
                $key=$val['component'];
                $grouped[$key][]=$val;
            }
            foreach($grouped as $group){?>
                <div class="card">
                    <div class="card-header">
                        <a class="card-link" data-toggle="collapse" href="#<?php echo str_replace(' ', '',$group[0]['component']); ?>">
                            <?php echo $group[0]['component']; ?>
                        </a>
                    </div>
                    <div id="<?php echo str_replace(' ', '',$group[0]['component']); ?>" class="collapse" data-parent="#accordion">
                        <div class="card-body">
                            <?php foreach ($group as $occurance){
                                echo $occurance['date'] . ' - ' . $occurance['comment'] . ' - ' . $occurance['parts'] . ' - ' . $occurance['engineer'];
                                echo '<br>';
                                } ?>
                        </div>
                    </div>
                </div>
        <?php }?>
</div>
</code>```

Body:
I am reading mysql fine to pull a set of data given a particular ID, this returns an array to my script.  Thats the easy bit.  Example of which is here an is stored in an array called $history

Array ( [serviceID] => 1 [VesselTag] => 1000001 [component] => Engine 1 [item] => Caterpillar [serial] => 123456 [comment] => Something in here like this [parts] => Oil [lat] => 50.38313740 [longitude] => -4.03461360 [engineer] => 27 [date] => 2019-05-30 19:25:56 )
Array ( [serviceID] => 2 [VesselTag] => 1000001 [component] => Engine 2 [item] => Caterpillar [serial] => 677889 [comment] => Did a full overhaul of top section replaced everything [parts] => everything [lat] => 50.38309180 [longitude] => -4.03468820 [engineer] => 27 [date] => 2019-05-30 19:27:29 )
Array ( [serviceID] => 3 [VesselTag] => 1000001 [component] => Engine 1 [item] => Caterpillar [serial] => 123456 [comment] => This seems quite usable [parts] => Oil [lat] => 50.38345892 [longitude] => -4.03475649 [engineer] => 27 [date] => 2019-05-30 19:29:23 )

What I would like to do is now generate an accordion 'card-body' grouping the cards based on [component]s from the list.  Sometime the array may have more or less, so there is going to be anywhere from 0 to 7 cards [components] showing.

        $array[Component]  **<--- Guessing this would be from a ```foreach``` or ```while``` loop???**

      <?php **THIS IS WHERE I WILL DISPLAY THE ROWS OF DATA ASSOCIATED TO THE [component] IN QUESTION ?>**

I am just really struggling getting my head around which loop to use, and to be able to drive it by the [components].

Its doing my head in......

Body:
After quite some time looking at different articles I seem to have come up with something that works.....
I think it is relatively efficient, but happy for someone to tell me otherwise....

<div id="accordion">
    <?php $newArray=array();
            foreach($history as $val){
                $key=$val['component'];
                $grouped[$key][]=$val;
            }
            foreach($grouped as $group){?>
                <div class="card">
                    <div class="card-header">
                        <a class="card-link" data-toggle="collapse" href="#<?php echo str_replace(' ', '',$group[0]['component']); ?>">
                            <?php echo $group[0]['component']; ?>
                        </a>
                    </div>
                    <div id="<?php echo str_replace(' ', '',$group[0]['component']); ?>" class="collapse" data-parent="#accordion">
                        <div class="card-body">
                            <?php foreach ($group as $occurance){
                                echo $occurance['date'] . ' - ' . $occurance['comment'] . ' - ' . $occurance['parts'] . ' - ' . $occurance['engineer'];
                                echo '<br>';
                                } ?>
                        </div>
                    </div>
                </div>
        <?php }?>
</div>


问:
Trigger with Multiple Rows
I have written a trigger to log changes to a table and I of course didn't realize until after that this only works on one record at a time.  Now I am trying to update it to allow for bulk updates and I cannot figure out how to do this.

```<code>CREATE TRIGGER [DT].[trg_LogChanges]
ON [DT].[NewDetails]
FOR UPDATE
AS

DECLARE
    @TableName VARCHAR(100) ,
    @UpdatedDate smalldatetime ,
    @UpdatedBy uniqueidentifier

SELECT @TableName = 'DT.NewDetails'

IF EXISTS (SELECT 1 FROM INSERTED i INNER JOIN DELETED d on i.Guid = d.Guid)

    IF(SELECT ModifiedDate FROM INSERTED) Is Null
        SET @UpdatedDate = getdate()
    ELSE
        SET @UpdatedDate = (SELECT ModifiedDate FROM INSERTED)

    IF(SELECT ModifiedBy FROM INSERTED) Is Null
        SET @UpdatedBy = '11111111-1111-1111-1111-111111111111'
    ELSE
        SET @UpdatedBy = (SELECT ModifiedBy FROM INSERTED)

IF UPDATE (StatusID)
    BEGIN
        INSERT INTO DT.LogChanges
        (
            ChangeType, TableName, RecordGuid, FieldName
            , OldValue, NewValue, UpdatedBy, UpdatedDate
        )
        SELECT
            'U', @TableName, d.Guid, 'StatusID'
            , d.StatusID, i.StatusID, @UpdatedBy, @UpdatedDate
        FROM INSERTED i INNER JOIN DELETED d on i.Guid = d.Guid
        WHERE
            (d.StatusID IS NULL AND i.StatusID IS NOT NULL)
            OR (d.StatusID IS NOT NULL AND i.StatusID IS NULL)
            OR (d.StatusID <> i.StatusID)
    END
</code>```

Can anyone offer up any help on how to fix this to work with multiple rows?  I attempted the following by adding the SELECT 1 FROM INSERTED but I still get a subquery error message.

```<code>CREATE TRIGGER [DT].[trg_LogChanges]
   ON [DT].[NewDetails]
   FOR UPDATE
AS

DECLARE
    @TableName VARCHAR(100) ,
    @UpdatedDate smalldatetime ,
    @UpdatedBy uniqueidentifier

SELECT @TableName = 'DT.NewDetails'

IF EXISTS (SELECT 1 FROM INSERTED i INNER JOIN DELETED d on i.Guid = d.Guid)

    IF(SELECT ModifiedDate FROM INSERTED) Is Null
        SET @UpdatedDate = getdate()
    ELSE
        SET @UpdatedDate = (SELECT ModifiedDate FROM INSERTED)

    IF(SELECT ModifiedBy FROM INSERTED) Is Null
        SET @UpdatedBy = '11111111-1111-1111-1111-111111111111'
    ELSE
        SET @UpdatedBy = (SELECT ModifiedBy FROM INSERTED)

IF UPDATE (StatusID)
    BEGIN
        IF EXISTS (SELECT 1 FROM INSERTED i
                    INNER JOIN DELETED d
                        on i.Guid = d.Guid
                    WHERE
                        (d.StatusID IS NULL AND i.StatusID IS NOT NULL)
                        OR (d.StatusID IS NOT NULL AND i.StatusID IS NULL)
                        OR (d.StatusID <> i.StatusID))
        BEGIN
            INSERT INTO DT.LogChanges
            (
                ChangeType, TableName, RecordGuid, FieldName
                , OldValue, NewValue, UpdatedBy, UpdatedDate
            )
            SELECT
                'U', @TableName, d.Guid, 'StatusID'
                , d.StatusID, i.StatusID, @UpdatedBy, @UpdatedDate
            FROM INSERTED i INNER JOIN DELETED d on i.Guid = d.Guid
            WHERE
                (d.StatusID IS NULL AND i.StatusID IS NOT NULL)
                OR (d.StatusID IS NOT NULL AND i.StatusID IS NULL)
                OR (d.StatusID <> i.StatusID)
        END
    END
</code>```

I have searched online but obviously I am still missing the correct way to do this. Any help would be greatly appreciated.

EDIT
I spoke with the business owners of this process and they want to ignore the updates on multiple rows.  Is there a way to add an IF around the entire trigger to ignore it if the records are more than 0?

Thanks

答:
I believe your conditional checks can be minimized to CASE statements. It seems like you could minimize the entire trigger to

```<code>INSERT INTO DT.LogChanges (ChangeType, TableName, RecordGuid, FieldName, OldValue, NewValue, UpdatedBy, UpdatedDate)
  SELECT 'U', 'DT.NewDetails', d.Guid, 'StatusID', d.StatusID, i.StatusID,
CASE WHEN i.ModifiedBy IS NULL THEN GETDATE() ELSE i.ModifiedBy AS ModifiedBy,
CASE WHEN i.ModifiedDate IS NULL THEN '11111111-1111-1111-1111-111111111111' ELSE i.ModifiedDate AS ModifiedDate
FROM INSERTED i INNER JOIN DELETED d on i.Guid = d.Guid
WHERE      (d.StatusID IS NULL AND i.StatusID IS NOT NULL)
    OR (d.StatusID IS NOT NULL AND i.StatusID IS NULL)
    OR (d.StatusID <> i.StatusID)
</code>```

I didn't have time to make sure the syntax is perfect, but if you have some problems with it I can assist.

In response to your EDIT, that's done pretty easily with something like

```<code>IF (SELECT COUNT(*) FROM INSERTED i INNER JOIN DELETED d on i.Guid = d.Guid) = 1
BEGIN
PRINT 'Only one update record'
END
</code>```

Title: Trigger with Multiple Rows

Body:
I have written a trigger to log changes to a table and I of course didn't realize until after that this only works on one record at a time.  Now I am trying to update it to allow for bulk updates and I cannot figure out how to do this.

CREATE TRIGGER [DT].[trg_LogChanges]
ON [DT].[NewDetails]
FOR UPDATE
AS

DECLARE
    @TableName VARCHAR(100) ,
    @UpdatedDate smalldatetime ,
    @UpdatedBy uniqueidentifier

SELECT @TableName = 'DT.NewDetails'

IF EXISTS (SELECT 1 FROM INSERTED i INNER JOIN DELETED d on i.Guid = d.Guid)

    IF(SELECT ModifiedDate FROM INSERTED) Is Null
        SET @UpdatedDate = getdate()
    ELSE
        SET @UpdatedDate = (SELECT ModifiedDate FROM INSERTED)

    IF(SELECT ModifiedBy FROM INSERTED) Is Null
        SET @UpdatedBy = '11111111-1111-1111-1111-111111111111'
    ELSE
        SET @UpdatedBy = (SELECT ModifiedBy FROM INSERTED)

IF UPDATE (StatusID)
    BEGIN
        INSERT INTO DT.LogChanges
        (
            ChangeType, TableName, RecordGuid, FieldName
            , OldValue, NewValue, UpdatedBy, UpdatedDate
        )
        SELECT
            'U', @TableName, d.Guid, 'StatusID'
            , d.StatusID, i.StatusID, @UpdatedBy, @UpdatedDate
        FROM INSERTED i INNER JOIN DELETED d on i.Guid = d.Guid
        WHERE
            (d.StatusID IS NULL AND i.StatusID IS NOT NULL)
            OR (d.StatusID IS NOT NULL AND i.StatusID IS NULL)
            OR (d.StatusID <> i.StatusID)
    END

Can anyone offer up any help on how to fix this to work with multiple rows?  I attempted the following by adding the SELECT 1 FROM INSERTED but I still get a subquery error message.

CREATE TRIGGER [DT].[trg_LogChanges]
   ON [DT].[NewDetails]
   FOR UPDATE
AS

DECLARE
    @TableName VARCHAR(100) ,
    @UpdatedDate smalldatetime ,
    @UpdatedBy uniqueidentifier

SELECT @TableName = 'DT.NewDetails'

IF EXISTS (SELECT 1 FROM INSERTED i INNER JOIN DELETED d on i.Guid = d.Guid)

    IF(SELECT ModifiedDate FROM INSERTED) Is Null
        SET @UpdatedDate = getdate()
    ELSE
        SET @UpdatedDate = (SELECT ModifiedDate FROM INSERTED)

    IF(SELECT ModifiedBy FROM INSERTED) Is Null
        SET @UpdatedBy = '11111111-1111-1111-1111-111111111111'
    ELSE
        SET @UpdatedBy = (SELECT ModifiedBy FROM INSERTED)

IF UPDATE (StatusID)
    BEGIN
        IF EXISTS (SELECT 1 FROM INSERTED i
                    INNER JOIN DELETED d
                        on i.Guid = d.Guid
                    WHERE
                        (d.StatusID IS NULL AND i.StatusID IS NOT NULL)
                        OR (d.StatusID IS NOT NULL AND i.StatusID IS NULL)
                        OR (d.StatusID <> i.StatusID))
        BEGIN
            INSERT INTO DT.LogChanges
            (
                ChangeType, TableName, RecordGuid, FieldName
                , OldValue, NewValue, UpdatedBy, UpdatedDate
            )
            SELECT
                'U', @TableName, d.Guid, 'StatusID'
                , d.StatusID, i.StatusID, @UpdatedBy, @UpdatedDate
            FROM INSERTED i INNER JOIN DELETED d on i.Guid = d.Guid
            WHERE
                (d.StatusID IS NULL AND i.StatusID IS NOT NULL)
                OR (d.StatusID IS NOT NULL AND i.StatusID IS NULL)
                OR (d.StatusID <> i.StatusID)
        END
    END

I have searched online but obviously I am still missing the correct way to do this. Any help would be greatly appreciated.

EDIT
I spoke with the business owners of this process and they want to ignore the updates on multiple rows.  Is there a way to add an IF around the entire trigger to ignore it if the records are more than 0?

Thanks

Body:
I believe your conditional checks can be minimized to CASE statements. It seems like you could minimize the entire trigger to

INSERT INTO DT.LogChanges (ChangeType, TableName, RecordGuid, FieldName, OldValue, NewValue, UpdatedBy, UpdatedDate)
  SELECT 'U', 'DT.NewDetails', d.Guid, 'StatusID', d.StatusID, i.StatusID,
CASE WHEN i.ModifiedBy IS NULL THEN GETDATE() ELSE i.ModifiedBy AS ModifiedBy,
CASE WHEN i.ModifiedDate IS NULL THEN '11111111-1111-1111-1111-111111111111' ELSE i.ModifiedDate AS ModifiedDate
FROM INSERTED i INNER JOIN DELETED d on i.Guid = d.Guid
WHERE      (d.StatusID IS NULL AND i.StatusID IS NOT NULL)
    OR (d.StatusID IS NOT NULL AND i.StatusID IS NULL)
    OR (d.StatusID <> i.StatusID)

I didn't have time to make sure the syntax is perfect, but if you have some problems with it I can assist.

In response to your EDIT, that's done pretty easily with something like

IF (SELECT COUNT(*) FROM INSERTED i INNER JOIN DELETED d on i.Guid = d.Guid) = 1
BEGIN
PRINT 'Only one update record'
END


问:
Boost asio udp socket sending to different ip address
I have one udp server receiving messages from multiple remote clients. When it receives one message, I copy the endpoint and reply to the client at the same IP address on port 5000 where each client is listening.

I have tried multiple debbuging strategies, and printing the endpoint right before I send the reply message gives me the correct IP address and port.

The sender:

```<code>    std::cout << udp_remote_endpoint.address().to_string();
    std::string str(packet.begin(), packet.end());
    std::cout << str << std::endl;
    io_service.post(
        [this, packet]()
        {
            udp_socket.async_send_to(
                boost::asio::buffer(packet),
                udp_remote_endpoint,
                boost::bind(
                    &uds::handle_write,
                    this,
                    boost::asio::placeholders::error,
                    boost::asio::placeholders::bytes_transferred
                )
            );
        }
            );
</code>```

On the receiver, I get the udp_remote_endpoint and before sending, I set the socket endpoint:

```<code>    new_addr.endpoint = socket.get_udp_remote_endpoint();
    new_addr.endpoint.port(5000);
    socket.set_udp_remote_endpoint(new_addr.endpoint);
</code>```

For example, this output:

```<code>192.168.1.131K-131-1559147491761155
</code>```

Is actually sending to the IP 192.168.1.130. The message contents are correct "K-131-1559147491761155"

答:
Solved!

I removed the io_service.post and it worked!

```<code>`std::cout << udp_remote_endpoint.address().to_string();
std::string str(packet.begin(), packet.end());
std::cout << str << std::endl;
//io_service.post(
//    [this, packet]()
//    {
        udp_socket.async_send_to(
            boost::asio::buffer(packet),
            udp_remote_endpoint,
            boost::bind(
                &uds::handle_write,
                this,
                boost::asio::placeholders::error,
                boost::asio::placeholders::bytes_transferred
            )
        );
//    }
//);`
</code>```

Body:
I have one udp server receiving messages from multiple remote clients. When it receives one message, I copy the endpoint and reply to the client at the same IP address on port 5000 where each client is listening.

I have tried multiple debbuging strategies, and printing the endpoint right before I send the reply message gives me the correct IP address and port.

The sender:

    std::cout << udp_remote_endpoint.address().to_string();
    std::string str(packet.begin(), packet.end());
    std::cout << str << std::endl;
    io_service.post(
        [this, packet]()
        {
            udp_socket.async_send_to(
                boost::asio::buffer(packet),
                udp_remote_endpoint,
                boost::bind(
                    &uds::handle_write,
                    this,
                    boost::asio::placeholders::error,
                    boost::asio::placeholders::bytes_transferred
                )
            );
        }
            );

On the receiver, I get the udp_remote_endpoint and before sending, I set the socket endpoint:

    new_addr.endpoint = socket.get_udp_remote_endpoint();
    new_addr.endpoint.port(5000);
    socket.set_udp_remote_endpoint(new_addr.endpoint);

For example, this output:

192.168.1.131K-131-1559147491761155

Is actually sending to the IP 192.168.1.130. The message contents are correct "K-131-1559147491761155"

Body:
Solved!

I removed the io_service.post and it worked!

`std::cout << udp_remote_endpoint.address().to_string();
std::string str(packet.begin(), packet.end());
std::cout << str << std::endl;
//io_service.post(
//    [this, packet]()
//    {
        udp_socket.async_send_to(
            boost::asio::buffer(packet),
            udp_remote_endpoint,
            boost::bind(
                &uds::handle_write,
                this,
                boost::asio::placeholders::error,
                boost::asio::placeholders::bytes_transferred
            )
        );
//    }
//);`


问:
Buffio scanner stop reading halfway of line from mysqldump stdout
am trying to write a program in go that able to export mysql database(using mysqldump) into .sql file, but somehow scanner.Text() only read halfway of the output from mysqldump. Following main package:

```<code>func main() {
    // Just to get list of database name from
    // []string{dbA, dbB}
    databases := pkg.GetDatabases()

    // Just to get mysql connection config
    dbConfig := pkg.NewConnection()

    var wg sync.WaitGroup

    // How many goroutines need to be waited
    wg.Add(len(databases.Name))

    // databases.Name is a type []string hold database's name
    for _, name := range databases.Name {
        go dumping(name, dbConfig, &wg)
    }

    wg.Wait()

    fmt.Print("Main program exit!")
}

func dumping (name string, c *pkg.Connection, wg *sync.WaitGroup) {
    defer wg.Done()
    args := []string{
        fmt.Sprintf("--port=%s", c.Port),
        fmt.Sprintf("--host=%s", c.Host),
        fmt.Sprintf("--password=%s", ""),
    }
    args = append(args, name)
    log.Printf("exec mysqldump with %v", args)

    cmd := exec.Command("mysqldump", args...)

    stdout, err := cmd.StdoutPipe()
    if err != nil {
        log.Fatalf("Error to execute mysqlump command: ", err)
        os.Exit(1)
    }
    scanner := bufio.NewScanner(stdout)
    go func() {
        for scanner.Scan() {
            fmt.Printf("%s\n", scanner.Text())
        }
    }()

    err = cmd.Start()
    if err != nil {
        fmt.Fprintln(os.Stderr, "Error starting Cmd", err)
        os.Exit(1)
    }

    err = cmd.Wait()
    if err != nil {
        fmt.Fprintln(os.Stderr, "Error waiting for Cmd", err.Error())
        os.Exit(1)
    }
}
</code>```

Output from fmt.Printf("%s\n", scanner.Text()):

```<code>2019/05/31 15:43:03 exec mysqldump with [--port=3306 --host=127.0.0.1 --password= dbA]
-- MySQL dump 10.16  Distrib 10.3.9-MariaDB, for osx10.13 (x86_64)
--
-- Host: 127.0.0.1    Database: dbA
-- -----------------------------------------------------
-- Server version       10.3.9-MariaDB

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
Error waiting for Cmd exit status 2
exit status 1
</code>```

If i run directly this command mysqldump -P 3306 -h 127.0.0.1 -u root --password="" dbA from a terminal, got a lot of sql statements that still left behind. Is there anything i missed?

Thanks.

答:
Found the culprit. The real problem is i didn't provided --user=%s argument to the exec command. Should be:

```<code>args := []string{
      fmt.Sprintf("--port=%s", c.Port),
      fmt.Sprintf("--host=%s", c.Host),
      fmt.Sprintf("--user=%s", c.User), <-- here
      fmt.Sprintf("--password=%s", ""),
}
</code>```

How i found that? The error actually goes to cmd.Stderr:

```<code>var out bytes.Buffer
cmd.Stderr = &out
if err != nil {
    log.Fatalf("Error to execute mysqlump command: ", err)
    os.Exit(1)
}
fmt.Printf("%q\n", out.String())
</code>```

Which output the real error like following:

```<code>"mysqldump: Got error: 1044: \"Access denied for user ''@'localhost' to database 'dbA'\" when selecting the database\n"
</code>```

Body:
am trying to write a program in go that able to export mysql database(using mysqldump) into .sql file, but somehow scanner.Text() only read halfway of the output from mysqldump. Following main package:

func main() {
    // Just to get list of database name from
    // []string{dbA, dbB}
    databases := pkg.GetDatabases()

    // Just to get mysql connection config
    dbConfig := pkg.NewConnection()

    var wg sync.WaitGroup

    // How many goroutines need to be waited
    wg.Add(len(databases.Name))

    // databases.Name is a type []string hold database's name
    for _, name := range databases.Name {
        go dumping(name, dbConfig, &wg)
    }

    wg.Wait()

    fmt.Print("Main program exit!")
}

func dumping (name string, c *pkg.Connection, wg *sync.WaitGroup) {
    defer wg.Done()
    args := []string{
        fmt.Sprintf("--port=%s", c.Port),
        fmt.Sprintf("--host=%s", c.Host),
        fmt.Sprintf("--password=%s", ""),
    }
    args = append(args, name)
    log.Printf("exec mysqldump with %v", args)

    cmd := exec.Command("mysqldump", args...)

    stdout, err := cmd.StdoutPipe()
    if err != nil {
        log.Fatalf("Error to execute mysqlump command: ", err)
        os.Exit(1)
    }
    scanner := bufio.NewScanner(stdout)
    go func() {
        for scanner.Scan() {
            fmt.Printf("%s\n", scanner.Text())
        }
    }()

    err = cmd.Start()
    if err != nil {
        fmt.Fprintln(os.Stderr, "Error starting Cmd", err)
        os.Exit(1)
    }

    err = cmd.Wait()
    if err != nil {
        fmt.Fprintln(os.Stderr, "Error waiting for Cmd", err.Error())
        os.Exit(1)
    }
}

Output from fmt.Printf("%s\n", scanner.Text()):

2019/05/31 15:43:03 exec mysqldump with [--port=3306 --host=127.0.0.1 --password= dbA]
-- MySQL dump 10.16  Distrib 10.3.9-MariaDB, for osx10.13 (x86_64)
--
-- Host: 127.0.0.1    Database: dbA
-- -----------------------------------------------------
-- Server version       10.3.9-MariaDB

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
Error waiting for Cmd exit status 2
exit status 1

If i run directly this command mysqldump -P 3306 -h 127.0.0.1 -u root --password="" dbA from a terminal, got a lot of sql statements that still left behind. Is there anything i missed?

Thanks.

Body:
Found the culprit. The real problem is i didn't provided --user=%s argument to the exec command. Should be:

args := []string{
      fmt.Sprintf("--port=%s", c.Port),
      fmt.Sprintf("--host=%s", c.Host),
      fmt.Sprintf("--user=%s", c.User), <-- here
      fmt.Sprintf("--password=%s", ""),
}

How i found that? The error actually goes to cmd.Stderr:

var out bytes.Buffer
cmd.Stderr = &out
if err != nil {
    log.Fatalf("Error to execute mysqlump command: ", err)
    os.Exit(1)
}
fmt.Printf("%q\n", out.String())

Which output the real error like following:

"mysqldump: Got error: 1044: \"Access denied for user ''@'localhost' to database 'dbA'\" when selecting the database\n"