问:
using javascript array method to print the value of x
Could someone explain following code to me and what happens in x.join = x.shift;?

x = [5, 10, 15];

x.join = x.shift;

if (x == 5 && x == 10 && x == 15)
  console.log('good');```

答:
First, the array function shift removes the first element of an array and returns it. So, for example:

```<code>x = [5, 10, 15]
a = x.shift() #now a = 5 and x = [10, 15]
</code>```

Next, the line x.join = x.shift in your code basically reassigns your array's (x's) join method to call shift instead of join. And as join is (apparently ?) used in ==, then at each comparison, x's shift() method is called and returns the first available element.

So your code is equivalent to:

x = [5, 10, 15];

if (x.shift() == 5 && x.shift() == 10 && x.shift() == 15)
  console.log('good');```

Which is just an overkill way to check the array's first, second and third values against the values 5, 10, and 15.

Body:
First, the array function shift removes the first element of an array and returns it. So, for example:

x = [5, 10, 15]
a = x.shift() #now a = 5 and x = [10, 15]

Next, the line x.join = x.shift in your code basically reassigns your array's (x's) join method to call shift instead of join. And as join is (apparently ?) used in ==, then at each comparison, x's shift() method is called and returns the first available element.

So your code is equivalent to:

x = [5, 10, 15];

if (x.shift() == 5 && x.shift() == 10 && x.shift() == 15)
  console.log('good');

Which is just an overkill way to check the array's first, second and third values against the values 5, 10, and 15.


来源: stackexchange

问:
Python and performance of list comprehensions
Suppose you have got a list comprehension in python, like

```<code>Values = [ f(x) for x in range( 0, 1000 ) ]
</code>```

with f being just a function without side effects. So all the entries can be computed independently.

Is Python able to increase the performance of this list comprehension compared with the "obvious" implementation; e.g. by shared-memory-parallelization on multicore CPUs?

答:
Try if the following can be faster:

```<code>Values = map(f,range(0,1000))
</code>```

That's a functionnal manner to code

Another idea is to replace all occurences of Values in the code by the generator expression

```<code>imap(f,range(0,1000))  # Python < 3

map(f,range(0,1000))  # Python 3
</code>```

ContentLicense: CC BY-SA 3.0

Body:
Suppose you have got a list comprehension in python, like

Values = [ f(x) for x in range( 0, 1000 ) ]

with f being just a function without side effects. So all the entries can be computed independently.

Is Python able to increase the performance of this list comprehension compared with the "obvious" implementation; e.g. by shared-memory-parallelization on multicore CPUs?

Body:
Try if the following can be faster:

Values = map(f,range(0,1000))

That's a functionnal manner to code

Another idea is to replace all occurences of Values in the code by the generator expression

imap(f,range(0,1000))  # Python < 3

map(f,range(0,1000))  # Python 3


问:
Slice or downsize (resize) image for neural networks
Given a dataset of images with very big resolution, 8000x6000, which is the best method to train a neural network? For image segmentation and not only.

As the title says, should I slice them for 800x600 each patch of image or should I resize the whole image to 800x600?

In the first case I'd lose the content aware and in second case I'd lose a lot of details but it will be content aware of the full image.

Thank you!

答: Resizing is a better option. Often your neural network does not require all the details from a super high resolution, and a smaller resolution suffices. The content of the image, on the other hand, is important and it may become problematic depending on what part of the image you crop it.</p>

ContentLicense: CC BY-SA 4.0


问:
create an alert using css, jquery/javascript arrays or switch
I'm doing some experimenting. So this may look bad to javascript experts here. But I'm trying to learn.

I have a header div that will show a larger image when the user mouses over each thumbnail image. I used jQuery to create this effect. The header div contains a button. Once the button is clicked, an alert will pop up to tell the user the artist's name of image. The button is wired to an ID of a paragraph. Problem is, my code is not working. I don't want to use "onclick" inline javascript (which is what I am using to call the showArtistname() function). I want unobtrusive javascript like jQuery is. I'm not sure how to do this. I don't know if I should use an array or if I'm even approaching this correctly. Well, it's not correct, because it's not working...

Here is my jQuery code:

```<code>$(function(){
$("a:has(img.small)").mouseover(function(){
var bigImage= $(this).attr("href");
$("#heading").attr({src: bigImage});
return false;
});

});
</code>```

Here is my javascript code:

```<code>function showArtistname(){

var a = document.getElementById   ("bluesails", "purplemountains", "bigsky", "nightlights", "fireysunset", "brilliantsunrise").innerHTML;

switch(a) {
case "bluesails":
    alert("Arthur MacKenzie")
    break
case "purplemountains":
    alert("Maggie Laing")
    break

case "bigsky":
    alert("Arthur MacKenzie")
    break

case "nightlights":
    alert("Aria Soriano")
    break

case "fireysunset":
    alert("Felix Buckley")
    break

case "brilliantsunrise":
    alert("Felix Buckley")

}
}
</code>```

Here is my HTML:

```<code><div class="container_12" id="_container">

<div class="grid_12" id="12_header" >

    <div class="hc_left_pic"> <img src="images/01_md.jpg" id="heading" alt="Big Image." />
 <div id="showImage" onclick="showArtistname()"></div>
  </a></div>
</div>

<div class="clear"></div>

<div class="grid_4" id="artist_container1">
<a href="images/01_md.jpg"><img src="images/01_sm.jpg" width="100" height="100" class="small" alt="Small image Blue Sails" /></a>
    <p class="text" id="bluesails">Blue Sails</p>
</div>

<div class="grid_4" id="artist_container2">
<a href="images/02_md.jpg"><img src="images/02_sm.jpg" width="100" height="100" class="small" alt="Small Image Purple Mountains"/></a>
    <p class="text" id="purplemountains">Purple Mountains</p>
</div>

<div class="grid_4" id="artist_container3">
<a href="images/03_md.jpg"><img src="images/03_sm.jpg" width="100" height="100" class="small" alt="Small Image Big Sky"/></a>
    <p class="text" id="bigsky">Big Sky</p>
</div>

<div class="clear"></div>

<div class="grid_4" id="artist_container4">
<a href="images/04_md.jpg"><img src="images/04_sm.jpg" width="100" height="100" class="small" alt="Small Image Night Lights" /></a>
    <p class="text" id="nightlights">Night Lights</p>
</div>

 <div class="grid_4" id="artist_container5">
<a href="images/05_md.jpg"><img src="images/05_sm.jpg" width="100" height="100" class="small"  alt="Small Product Image Firey Sunset"/></a>
    <p class="text" id="fireysunset">Firey Sunset</p>
</div>

<div class="grid_4" id="artist_container6">
<a href="images/06_md.jpg"><img src="images/06_sm.jpg" width="100" height="100" class="small"  alt="Small Product Image Brilliant Sunrise"/></a>
    <p class="text" id="brilliantsunrise">Brilliant Sunrise</p>
</div>
</code>```

Here is my CSS:

```<code>.container_12 .grid_4 {
  width: 274px;
  height: 370px;
  background-color:#ccc;
  border: 3px solid #999;
  padding-left:10px;
  padding-right:10px;
  padding-bottom:10px;
 }

#showImage{
margin: -170px 20px 80px 700px;
width: 176px;
height: 48px;
background:url(../images/showimage.jpg);
position:relative;
z-index:100;
}

 .grid_4 img {
position:relative;
left:100px;
top:20px;
padding:0 0 60px 0;
border:none;

}
.grid_4 p {
position:relative;
text-align:center;
}

p.text {
font-family:Arial, Helvetica, sans-serif;
font-size:.75em;
color:#000;
line-height:1.25em;
font-weight:bold;
}

#12_header {
display: inline;
background-color:#e5e5e5;
border: 3px solid #bfbfbf;
height:225px;
font-family:Arial, Helvetica, sans-serif;
font-size:;
color:#000;
font-weight:bold;
line-height:1.2em;

}

.hc_left_pic {
float:left;
margin-top:15px;
background-color:#e5e5e5;
border: 3px solid #bfbfbf;
width:935px;
height:250px;
}
</code>```

I hope I've given enough information to get a solution to this.

Thanks

答:
It is probably not exactly what you are after but I have made a demo which simply clones the image being hovered over into the larger area above.

The alert() is taking the title attribute of the existing  element which allows you to keep the artist name as part of the HTML, rather than having to maintain HTML and JavaScript. It also feels more semantically correct, gives your page greater content and is a much simpler solution.

If you need to display a different image rather than just a clone, you could add the larger images as hidden elements either inside the top
 and use jQuery show() to un-hide the image.

Edit: Updated demo that moves the alert into a click event. This could easily be a click event on the top  itself its or parent
 if necessary by just changing the selector to $('#topImage img').click(function() {...}); or $('#topImage').click(function() {...});

Body:
I'm doing some experimenting. So this may look bad to javascript experts here. But I'm trying to learn.

I have a header div that will show a larger image when the user mouses over each thumbnail image. I used jQuery to create this effect. The header div contains a button. Once the button is clicked, an alert will pop up to tell the user the artist's name of image. The button is wired to an ID of a paragraph. Problem is, my code is not working. I don't want to use "onclick" inline javascript (which is what I am using to call the showArtistname() function). I want unobtrusive javascript like jQuery is. I'm not sure how to do this. I don't know if I should use an array or if I'm even approaching this correctly. Well, it's not correct, because it's not working...

Here is my jQuery code:

$(function(){
$("a:has(img.small)").mouseover(function(){
var bigImage= $(this).attr("href");
$("#heading").attr({src: bigImage});
return false;
});

});

Here is my javascript code:

function showArtistname(){

var a = document.getElementById   ("bluesails", "purplemountains", "bigsky", "nightlights", "fireysunset", "brilliantsunrise").innerHTML;

switch(a) {
case "bluesails":
    alert("Arthur MacKenzie")
    break
case "purplemountains":
    alert("Maggie Laing")
    break

case "bigsky":
    alert("Arthur MacKenzie")
    break

case "nightlights":
    alert("Aria Soriano")
    break

case "fireysunset":
    alert("Felix Buckley")
    break

case "brilliantsunrise":
    alert("Felix Buckley")

}
}

Here is my HTML:

Blue Sails

Purple Mountains

Big Sky

Night Lights

Firey Sunset

Brilliant Sunrise

Here is my CSS:

.container_12 .grid_4 {
  width: 274px;
  height: 370px;
  background-color:#ccc;
  border: 3px solid #999;
  padding-left:10px;
  padding-right:10px;
  padding-bottom:10px;
 }

#showImage{
margin: -170px 20px 80px 700px;
width: 176px;
height: 48px;
background:url(../images/showimage.jpg);
position:relative;
z-index:100;
}

 .grid_4 img {
position:relative;
left:100px;
top:20px;
padding:0 0 60px 0;
border:none;

}
.grid_4 p {
position:relative;
text-align:center;
}

p.text {
font-family:Arial, Helvetica, sans-serif;
font-size:.75em;
color:#000;
line-height:1.25em;
font-weight:bold;
}

#12_header {
display: inline;
background-color:#e5e5e5;
border: 3px solid #bfbfbf;
height:225px;
font-family:Arial, Helvetica, sans-serif;
font-size:;
color:#000;
font-weight:bold;
line-height:1.2em;

}

.hc_left_pic {
float:left;
margin-top:15px;
background-color:#e5e5e5;
border: 3px solid #bfbfbf;
width:935px;
height:250px;
}

I hope I've given enough information to get a solution to this.

Thanks

Body:
It is probably not exactly what you are after but I have made a demo which simply clones the image being hovered over into the larger area above.

The alert() is taking the title attribute of the existing <img> element which allows you to keep the artist name as part of the HTML, rather than having to maintain HTML and JavaScript. It also feels more semantically correct, gives your page greater content and is a much simpler solution.

If you need to display a different image rather than just a clone, you could add the larger images as hidden elements either inside the top <div> and use jQuery show() to un-hide the image.

Edit: Updated demo that moves the alert into a click event. This could easily be a click event on the top <img> itself its or parent <div> if necessary by just changing the selector to $('#topImage img').click(function() {...}); or $('#topImage').click(function() {...});


问:
Golang stops navigation on downloading
Whenever I make a large download from my website using Go, it prevents me from navigating or doing anything else in my website in the same browser at the same time the download is happening. This happens with Firefox, chrome and Safari which makes me think it is a conf issue.

Go env

```<code>GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build062753082=/tmp/go-build"
</code>```

Using the default GO Web server net/http for this.

EDIT:

Sorry forgot about the code for the download

```<code>func FunctionName(res http.ResponseWriter, req *http.Request, p httprouter.Params) {
    defer req.Body.Close()
    setSecurityHeaders(res)
    req.ParseForm()
    id := p.ByName("id")

    incletter, err := GetIncLetterById(bson.ObjectIdHex(id))
    if err != nil {
        jsonResponse(res, map[string]string{"status": "error", "message": "."})
        return
    }

    bytes, filename := incletter.GetFileBytes()

    res.Header().Set("Content-Disposition", "attachment; filename=\""+filename+"\"")
    res.Header().Set("Content-type", "application/pdf")
    res.Header().Set("Content-Length", strconv.Itoa(len(bytes)))
    res.Write(bytes)
}
</code>```

答:
Your headers look good (Though I think the proper content type notation is proper case ("Content-Type"). If this is only happening for large files, I suspect this means the entire file is being loaded into memory before being streamed to the client. To get around this, you can use the following:

```<code>byteSlice, filename := incletter.GetFileBytes()
byteStream := bytes.NewReader(byteSlice)

res.Header().Set("Content-Disposition", "attachment; filename=\""+filename+"\"")
res.Header().Set("Content-type", "application/pdf")
res.Header().Set("Content-Length", strconv.Itoa(len(bytes)))

io.Copy(res, byteStream)
</code>```

This will essentially stream from a reader interface to a writer directly to the client.

Title: Golang stops navigation on downloading

Body:
Whenever I make a large download from my website using Go, it prevents me from navigating or doing anything else in my website in the same browser at the same time the download is happening. This happens with Firefox, chrome and Safari which makes me think it is a conf issue.

Go env

GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build062753082=/tmp/go-build"

Using the default GO Web server net/http for this.

EDIT:

Sorry forgot about the code for the download

func FunctionName(res http.ResponseWriter, req *http.Request, p httprouter.Params) {
    defer req.Body.Close()
    setSecurityHeaders(res)
    req.ParseForm()
    id := p.ByName("id")

    incletter, err := GetIncLetterById(bson.ObjectIdHex(id))
    if err != nil {
        jsonResponse(res, map[string]string{"status": "error", "message": "."})
        return
    }

    bytes, filename := incletter.GetFileBytes()

    res.Header().Set("Content-Disposition", "attachment; filename=\""+filename+"\"")
    res.Header().Set("Content-type", "application/pdf")
    res.Header().Set("Content-Length", strconv.Itoa(len(bytes)))
    res.Write(bytes)
}

Body:
Your headers look good (Though I think the proper content type notation is proper case ("Content-Type"). If this is only happening for large files, I suspect this means the entire file is being loaded into memory before being streamed to the client. To get around this, you can use the following:

byteSlice, filename := incletter.GetFileBytes()
byteStream := bytes.NewReader(byteSlice)

res.Header().Set("Content-Disposition", "attachment; filename=\""+filename+"\"")
res.Header().Set("Content-type", "application/pdf")
res.Header().Set("Content-Length", strconv.Itoa(len(bytes)))

io.Copy(res, byteStream)

This will essentially stream from a reader interface to a writer directly to the client.


问:
What's the difference between non-unique key lookup and index range scan?
I don't know what's the difference between non-unique key lookup and index range scan because I think both of them return rows which follow a condition like the operator "=" o "<".</p>

答:
I think for the most part it is semantics.  The two are very similar.

An index range scan starts with a lookup.  In some cases, that is sufficient.  For instance;

To find a maximum or minimum value.

If the index is known to be unique and the comparison is equality (or is  null).

More generally, the engine continues scanning the index either until the key value changes (for an = comparison) or until the end of the range.


问:
How to store a const char* in std :: string?
```<code>    char       *buffer1 = "abc";

    const char *buffer2 = (const char*) buffer;

    std :: string str (buffer2);
</code>```

This works, but I want to declare the std::string object i.e. str, once and use it many times to store different const char*.

What's the way out?

答:
str is actually copying the characters from buffer2, so it is not connected in any way.

If you want it to have another value, you just assign a new one

```<code>str = "Hello";
</code>```

Body:
    char       *buffer1 = "abc";

    const char *buffer2 = (const char*) buffer;

    std :: string str (buffer2);

This works, but I want to declare the std::string object i.e. str, once and use it many times to store different const char*.

What's the way out?

Body:
str is actually copying the characters from buffer2, so it is not connected in any way.

If you want it to have another value, you just assign a new one

str = "Hello";


问:
ggplot2 fill/grouping causes unwanted lines to connect in geom_ribbon
I'm trying to plot a stock price and some color above it as an indicator. The problem is that the color should not always be present which is indicated by having missing values, but using geom_ribbon causes the missing values to appear. The code is as follows:

```<code>library(dplyr)
library(ggplot2)
library(lubridate)
library(padr)

# Does not work correctly
ggplot(x, aes(x = Date, y = Stock)) +
  geom_ribbon(data = a, inherit.aes = FALSE, aes(
    x = Date2,
    ymin = Price * 1.1,
    ymax = Price,
    y = Price,
    fill = Group
  ), alpha = 0.9) +
  geom_line()

# Add missing values as NAs
b <- pad(a)

# Works but does not show colors
ggplot(x, aes(x = Date, y = Stock)) +
  geom_ribbon(data = b,
              inherit.aes = FALSE,
              aes(x = Date2,
                  ymin = Price * 1.1,
                  ymax = Price,
                  y = Price#,
                  #fill = Group
              ),
              alpha = 0.9) +
  geom_line()

# Does not work correctly because of the grouping caused by the fill
ggplot(x, aes(x = Date, y = Stock)) +
  geom_ribbon(data = b,
              inherit.aes = FALSE,
              aes(x = Date2,
                  ymin = Price * 1.1,
                  ymax = Price,
                  y = Price,
                  fill = Group),
              alpha = 0.9) +
  geom_line()

</code>```

It should look like this but without the missing values being filled in:

Padding the missing values with NAs allows to not show the missing values. Only the following points should have a ribbon above them, but it seems impossible to color them:

If the ribbons are colored after padding, then the data get grouped and the missing values get interpolated, which should not be the case:

Data:

```<code>x <- structure(list(Date = structure(c(18213, 18214, 18215, 18218,
                                       18219, 18220, 18221, 18222, 18225, 18226, 18227, 18229, 18232,
                                       18233, 18234, 18235, 18236, 18239, 18240, 18241, 18242, 18243,
                                       18246, 18247, 18248, 18249, 18250, 18253, 18254, 18256, 18257,
                                       18260, 18261, 18263, 18264, 18267, 18268, 18269, 18270, 18271,
                                       18274, 18275, 18276, 18277, 18278, 18282, 18283, 18284, 18285,
                                       18288, 18289, 18290, 18291, 18292, 18295, 18296, 18297, 18298,
                                       18299, 18302, 18303, 18304, 18305, 18306, 18310, 18311, 18312,
                                       18313, 18316, 18317, 18318, 18319, 18320, 18324, 18325, 18326,
                                       18327, 18328, 18331, 18332, 18333, 18334, 18335, 18338, 18339,
                                       18340, 18341, 18342, 18345, 18346, 18347, 18348, 18349, 18352,
                                       18353, 18354, 18355, 18356, 18359, 18360, 18361, 18362, 18363,
                                       18366, 18367, 18368, 18369, 18373, 18374, 18375, 18376, 18377,
                                       18380, 18381, 18382, 18383, 18384, 18387, 18388, 18389, 18390,
                                       18391, 18394, 18395, 18396, 18397, 18398, 18401, 18402, 18403,
                                       18404, 18405, 18409, 18410, 18411, 18412, 18415, 18416, 18417,
                                       18418, 18419, 18422, 18423, 18424, 18425, 18426, 18429, 18430,
                                       18431, 18432, 18433, 18436, 18437, 18438, 18439, 18440, 18443,
                                       18444, 18445, 18446, 18450, 18451, 18452, 18453, 18454, 18457,
                                       18458, 18459, 18460, 18461, 18464, 18465, 18466, 18467, 18468,
                                       18471, 18472, 18473, 18474, 18475, 18478, 18479, 18480, 18481,
                                       18482, 18485, 18486, 18487, 18488, 18489, 18492, 18493, 18494,
                                       18495, 18496, 18499, 18500, 18501, 18502, 18503, 18507, 18508,
                                       18509, 18510, 18513, 18514, 18515, 18516, 18517, 18520, 18521,
                                       18522, 18523, 18524, 18527, 18528, 18529, 18530, 18531, 18534,
                                       18535, 18536, 18537, 18538, 18541, 18542, 18543, 18544, 18545,
                                       18548, 18549, 18550, 18551, 18552, 18555, 18556, 18557, 18558,
                                       18559, 18562, 18563, 18564, 18565, 18566, 18569, 18570, 18571,
                                       18572, 18573, 18576, 18577, 18578, 18579, 18580, 18583, 18584,
                                       18585, 18586, 18587, 18590, 18591, 18592, 18594, 18597, 18598,
                                       18599, 18600, 18601, 18604, 18605, 18606, 18607, 18608, 18611,
                                       18612, 18613, 18614, 18615, 18618, 18619, 18620, 18622, 18625,
                                       18626, 18627), class = "Date"),
                    Stock = c(32.302406, 32.783638,
                              32.753559, 33.084404, 32.723484, 32.377602, 32.663322, 32.994171,
                              32.422722, 32.332485, 32.106918, 32.00164, 31.445225, 31.294838,
                              31.249725, 30.738419, 31.580574, 31.881338, 31.941496, 31.460262,
                              31.370037, 31.430185, 31.685837, 31.730949, 32.531425, 32.305305,
                              32.320374, 32.893227, 32.817848, 32.516354, 32.697247, 32.531425,
                              33.013821, 31.883207, 31.702311, 31.084244, 31.219913, 31.129463,
                              30.963646, 31.536489, 31.023943, 31.38574, 31.476185, 31.219913,
                              31.250069, 31.280209, 31.415892, 30.692297, 30.119457, 30.07423,
                              29.938557, 29.305418, 29.606909, 29.395864, 28.265259, 29.124521,
                              29.471237, 30.05916, 30.104383, 29.938557, 30.330505, 30.360651,
                              30.119457, 30.360651, 29.998857, 28.958696, 29.486313, 29.712437,
                              29.727509, 29.245119, 28.732574, 28.476297, 27.767788, 25.627169,
                              26.727627, 26.622101, 26.079411, 25.883436, 25.868364, 25.20507,
                              24.858349, 23.652367, 24.662378, 25.551794, 26.667328, 25.868364,
                              25.159845, 25.597015, 25.597015, 26.652252, 26.652252, 26.727627,
                              26.546728, 27.828083, 28.024061, 27.767788, 27.390923, 27.481363,
                              26.893454, 27.044199, 27.285393, 26.953745, 27.225088, 26.365831,
                              25.627169, 26.184935, 27.601965, 26.652252, 26.411057, 26.395979,
                              25.702539, 24.978949, 26.184935, 26.124634, 26.139708, 26.139708,
                              25.732691, 25.883436, 26.380905, 27.737633, 27.782866, 27.586891,
                              27.933609, 28.325556, 28.702429, 28.370781, 28.642126, 29.395864,
                              29.682287, 30.013933, 29.727509, 30.044086, 29.410936, 29.471237,
                              29.712437, 29.214966, 29.426016, 29.772738, 29.848104, 29.953634,
                              30.345573, 30.436029, 30.541553, 30.782749, 30.390808, 30.601849,
                              30.194832, 30.209909, 30.963646, 30.707371, 30.586777, 30.888273,
                              31.491264, 31.226501, 30.954172, 31.075211, 30.606213, 30.454916,
                              31.271893, 31.302149, 31.196245, 31.211376, 31.710632, 31.150858,
                              31.105465, 30.863405, 30.575953, 30.969309, 28.89662, 29.501785,
                              28.836103, 29.123556, 28.972263, 29.093298, 28.639423, 28.20068,
                              27.610645, 28.351974, 27.882971, 27.928352, 28.125036, 27.232416,
                              26.521349, 26.808798, 27.550123, 27.958612, 27.746809, 28.246065,
                              28.397356, 28.291456, 28.881491, 29.138687, 29.108423, 29.516909,
                              28.972263, 29.486652, 29.380754, 29.229462, 28.321712, 27.686293,
                              27.338318, 29.00252, 28.715067, 29.380754, 29.00252, 28.730202,
                              29.032782, 28.20068, 28.185551, 27.535, 27.988873, 27.580385,
                              27.429092, 27.277803, 26.823933, 27.247543, 27.988873, 27.262672,
                              27.171898, 26.415443, 26.127991, 25.764894, 25.94644, 25.94644,
                              26.521349, 25.825409, 26.11286, 25.47744, 25.053823, 25.295889,
                              25.250502, 25.462311, 24.887403, 25.386665, 25.053823, 25.295889,
                              24.660463, 24.509178, 24.963047, 24.645338, 25.038694, 24.766371,
                              25.961571, 25.537954, 25.159723, 25.038694, 24.811756, 23.782978,
                              24.176332, 24.645338, 24.478916, 24.61508, 23.767849, 23.979656,
                              23.934263, 24.040173, 24.085558, 24.5243, 24.947918, 25.038694,
                              24.312494, 23.979656, 22.618032, 22.723938, 21.649773, 22.118773,
                              21.558996, 21.649773, 20.409182, 20.424313, 19.985567, 18.835754,
                              18.260849, 16.173027, 16.309193, 18.383097, 17.293257, 19.008156,
                              19.745405, 19.376781, 19.825541, 20.130056, 18.206802, 19.55308,
                              18.383097)),
               class = "data.frame", row.names = c(NA, -285L))

a <- structure(list(Price = c(16.173027, 16.309193, 25.627169, 26.079411,
                              25.883436, 25.868364, 25.20507, 24.858349, 23.652367, 24.662378,
                              25.551794, 25.868364, 25.159845, 25.597015, 25.597015, 26.365831,
                              25.627169, 26.184935, 26.411057, 26.395979, 25.702539, 24.978949,
                              26.184935, 26.124634, 26.139708, 26.139708, 25.732691, 25.883436,
                              26.380905, 26.415443, 26.127991, 25.764894, 25.94644, 25.94644,
                              25.825409, 26.11286, 25.47744, 25.053823, 25.295889, 25.250502,
                              25.462311, 24.887403, 25.386665, 25.053823, 25.295889, 24.660463,
                              24.509178, 24.963047, 24.645338, 25.038694, 24.766371, 25.961571,
                              25.537954, 25.159723, 25.038694, 24.811756, 23.782978, 24.176332,
                              24.645338, 24.478916, 24.61508, 23.767849, 23.979656, 23.934263,
                              24.040173, 24.085558, 24.5243, 24.947918, 25.038694, 24.312494,
                              23.979656, 22.618032, 22.723938, 21.649773, 22.118773, 21.558996,
                              21.649773, 20.409182, 20.424313, 19.985567, 18.835754, 18.260849,
                              18.383097, 17.293257, 19.008156, 19.745405, 19.376781, 19.825541,
                              20.130056, 18.206802, 19.55308, 18.383097),
                    Date2 = structure(c(18611,
                                        18612, 18324, 18327, 18328, 18331, 18332, 18333, 18334, 18335,
                                        18338, 18340, 18341, 18342, 18345, 18366, 18367, 18368, 18374,
                                        18375, 18376, 18377, 18380, 18381, 18382, 18383, 18384, 18387,
                                        18388, 18534, 18535, 18536, 18537, 18538, 18542, 18543, 18544,
                                        18545, 18548, 18549, 18550, 18551, 18552, 18555, 18556, 18557,
                                        18558, 18559, 18562, 18563, 18564, 18565, 18566, 18569, 18570,
                                        18571, 18572, 18573, 18576, 18577, 18578, 18579, 18580, 18583,
                                        18584, 18585, 18586, 18587, 18590, 18591, 18592, 18594, 18597,
                                        18598, 18599, 18600, 18601, 18604, 18605, 18606, 18607, 18608,
                                        18613, 18614, 18615, 18618, 18619, 18620, 18622, 18625, 18626,
                                        18627), class = "Date"),
                    Group = c("b", "b", "a", "a", "a", "a",
                              "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
                              "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
                              "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
                              "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
                              "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
                              "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
                              "a", "a", "a", "a", "a", "a", "a", "a")),
               class = "data.frame", row.names = c(NA,
                                                   -92L))
</code>```

答:
You can set the group independently from the fill. The only downside is that you have to calculate these groups yourself. You can do this with run length encoding.

```<code># Calculate Rle
rle <- rle(b$Group)
# Replace non-NA runs with run IDs
rle <- within(unclass(rle), values[!is.na(values)] <- seq_along(values[!is.na(values)]))

# Add Rle to data
b$run <- inverse.rle(rle)
# Discard unecessary data
b <- b[!is.na(b$Group),]

ggplot(x, aes(x = Date, y = Stock)) +
  geom_ribbon(data = b, inherit.aes = FALSE, aes(
    x = Date2,
    ymin = Price * 1.1,
    ymax = Price,
    y = Price,
    fill = Group,
    group = run
  ), alpha = 0.9) +
  geom_line()
</code>```

Body:
I'm trying to plot a stock price and some color above it as an indicator. The problem is that the color should not always be present which is indicated by having missing values, but using geom_ribbon causes the missing values to appear. The code is as follows:

library(dplyr)
library(ggplot2)
library(lubridate)
library(padr)

# Does not work correctly
ggplot(x, aes(x = Date, y = Stock)) +
  geom_ribbon(data = a, inherit.aes = FALSE, aes(
    x = Date2,
    ymin = Price * 1.1,
    ymax = Price,
    y = Price,
    fill = Group
  ), alpha = 0.9) +
  geom_line()

# Add missing values as NAs
b <- pad(a)

# Works but does not show colors
ggplot(x, aes(x = Date, y = Stock)) +
  geom_ribbon(data = b,
              inherit.aes = FALSE,
              aes(x = Date2,
                  ymin = Price * 1.1,
                  ymax = Price,
                  y = Price#,
                  #fill = Group
              ),
              alpha = 0.9) +
  geom_line()

# Does not work correctly because of the grouping caused by the fill
ggplot(x, aes(x = Date, y = Stock)) +
  geom_ribbon(data = b,
              inherit.aes = FALSE,
              aes(x = Date2,
                  ymin = Price * 1.1,
                  ymax = Price,
                  y = Price,
                  fill = Group),
              alpha = 0.9) +
  geom_line()

It should look like this but without the missing values being filled in:

Padding the missing values with NAs allows to not show the missing values. Only the following points should have a ribbon above them, but it seems impossible to color them:

If the ribbons are colored after padding, then the data get grouped and the missing values get interpolated, which should not be the case:

Data:

x <- structure(list(Date = structure(c(18213, 18214, 18215, 18218,
                                       18219, 18220, 18221, 18222, 18225, 18226, 18227, 18229, 18232,
                                       18233, 18234, 18235, 18236, 18239, 18240, 18241, 18242, 18243,
                                       18246, 18247, 18248, 18249, 18250, 18253, 18254, 18256, 18257,
                                       18260, 18261, 18263, 18264, 18267, 18268, 18269, 18270, 18271,
                                       18274, 18275, 18276, 18277, 18278, 18282, 18283, 18284, 18285,
                                       18288, 18289, 18290, 18291, 18292, 18295, 18296, 18297, 18298,
                                       18299, 18302, 18303, 18304, 18305, 18306, 18310, 18311, 18312,
                                       18313, 18316, 18317, 18318, 18319, 18320, 18324, 18325, 18326,
                                       18327, 18328, 18331, 18332, 18333, 18334, 18335, 18338, 18339,
                                       18340, 18341, 18342, 18345, 18346, 18347, 18348, 18349, 18352,
                                       18353, 18354, 18355, 18356, 18359, 18360, 18361, 18362, 18363,
                                       18366, 18367, 18368, 18369, 18373, 18374, 18375, 18376, 18377,
                                       18380, 18381, 18382, 18383, 18384, 18387, 18388, 18389, 18390,
                                       18391, 18394, 18395, 18396, 18397, 18398, 18401, 18402, 18403,
                                       18404, 18405, 18409, 18410, 18411, 18412, 18415, 18416, 18417,
                                       18418, 18419, 18422, 18423, 18424, 18425, 18426, 18429, 18430,
                                       18431, 18432, 18433, 18436, 18437, 18438, 18439, 18440, 18443,
                                       18444, 18445, 18446, 18450, 18451, 18452, 18453, 18454, 18457,
                                       18458, 18459, 18460, 18461, 18464, 18465, 18466, 18467, 18468,
                                       18471, 18472, 18473, 18474, 18475, 18478, 18479, 18480, 18481,
                                       18482, 18485, 18486, 18487, 18488, 18489, 18492, 18493, 18494,
                                       18495, 18496, 18499, 18500, 18501, 18502, 18503, 18507, 18508,
                                       18509, 18510, 18513, 18514, 18515, 18516, 18517, 18520, 18521,
                                       18522, 18523, 18524, 18527, 18528, 18529, 18530, 18531, 18534,
                                       18535, 18536, 18537, 18538, 18541, 18542, 18543, 18544, 18545,
                                       18548, 18549, 18550, 18551, 18552, 18555, 18556, 18557, 18558,
                                       18559, 18562, 18563, 18564, 18565, 18566, 18569, 18570, 18571,
                                       18572, 18573, 18576, 18577, 18578, 18579, 18580, 18583, 18584,
                                       18585, 18586, 18587, 18590, 18591, 18592, 18594, 18597, 18598,
                                       18599, 18600, 18601, 18604, 18605, 18606, 18607, 18608, 18611,
                                       18612, 18613, 18614, 18615, 18618, 18619, 18620, 18622, 18625,
                                       18626, 18627), class = "Date"),
                    Stock = c(32.302406, 32.783638,
                              32.753559, 33.084404, 32.723484, 32.377602, 32.663322, 32.994171,
                              32.422722, 32.332485, 32.106918, 32.00164, 31.445225, 31.294838,
                              31.249725, 30.738419, 31.580574, 31.881338, 31.941496, 31.460262,
                              31.370037, 31.430185, 31.685837, 31.730949, 32.531425, 32.305305,
                              32.320374, 32.893227, 32.817848, 32.516354, 32.697247, 32.531425,
                              33.013821, 31.883207, 31.702311, 31.084244, 31.219913, 31.129463,
                              30.963646, 31.536489, 31.023943, 31.38574, 31.476185, 31.219913,
                              31.250069, 31.280209, 31.415892, 30.692297, 30.119457, 30.07423,
                              29.938557, 29.305418, 29.606909, 29.395864, 28.265259, 29.124521,
                              29.471237, 30.05916, 30.104383, 29.938557, 30.330505, 30.360651,
                              30.119457, 30.360651, 29.998857, 28.958696, 29.486313, 29.712437,
                              29.727509, 29.245119, 28.732574, 28.476297, 27.767788, 25.627169,
                              26.727627, 26.622101, 26.079411, 25.883436, 25.868364, 25.20507,
                              24.858349, 23.652367, 24.662378, 25.551794, 26.667328, 25.868364,
                              25.159845, 25.597015, 25.597015, 26.652252, 26.652252, 26.727627,
                              26.546728, 27.828083, 28.024061, 27.767788, 27.390923, 27.481363,
                              26.893454, 27.044199, 27.285393, 26.953745, 27.225088, 26.365831,
                              25.627169, 26.184935, 27.601965, 26.652252, 26.411057, 26.395979,
                              25.702539, 24.978949, 26.184935, 26.124634, 26.139708, 26.139708,
                              25.732691, 25.883436, 26.380905, 27.737633, 27.782866, 27.586891,
                              27.933609, 28.325556, 28.702429, 28.370781, 28.642126, 29.395864,
                              29.682287, 30.013933, 29.727509, 30.044086, 29.410936, 29.471237,
                              29.712437, 29.214966, 29.426016, 29.772738, 29.848104, 29.953634,
                              30.345573, 30.436029, 30.541553, 30.782749, 30.390808, 30.601849,
                              30.194832, 30.209909, 30.963646, 30.707371, 30.586777, 30.888273,
                              31.491264, 31.226501, 30.954172, 31.075211, 30.606213, 30.454916,
                              31.271893, 31.302149, 31.196245, 31.211376, 31.710632, 31.150858,
                              31.105465, 30.863405, 30.575953, 30.969309, 28.89662, 29.501785,
                              28.836103, 29.123556, 28.972263, 29.093298, 28.639423, 28.20068,
                              27.610645, 28.351974, 27.882971, 27.928352, 28.125036, 27.232416,
                              26.521349, 26.808798, 27.550123, 27.958612, 27.746809, 28.246065,
                              28.397356, 28.291456, 28.881491, 29.138687, 29.108423, 29.516909,
                              28.972263, 29.486652, 29.380754, 29.229462, 28.321712, 27.686293,
                              27.338318, 29.00252, 28.715067, 29.380754, 29.00252, 28.730202,
                              29.032782, 28.20068, 28.185551, 27.535, 27.988873, 27.580385,
                              27.429092, 27.277803, 26.823933, 27.247543, 27.988873, 27.262672,
                              27.171898, 26.415443, 26.127991, 25.764894, 25.94644, 25.94644,
                              26.521349, 25.825409, 26.11286, 25.47744, 25.053823, 25.295889,
                              25.250502, 25.462311, 24.887403, 25.386665, 25.053823, 25.295889,
                              24.660463, 24.509178, 24.963047, 24.645338, 25.038694, 24.766371,
                              25.961571, 25.537954, 25.159723, 25.038694, 24.811756, 23.782978,
                              24.176332, 24.645338, 24.478916, 24.61508, 23.767849, 23.979656,
                              23.934263, 24.040173, 24.085558, 24.5243, 24.947918, 25.038694,
                              24.312494, 23.979656, 22.618032, 22.723938, 21.649773, 22.118773,
                              21.558996, 21.649773, 20.409182, 20.424313, 19.985567, 18.835754,
                              18.260849, 16.173027, 16.309193, 18.383097, 17.293257, 19.008156,
                              19.745405, 19.376781, 19.825541, 20.130056, 18.206802, 19.55308,
                              18.383097)),
               class = "data.frame", row.names = c(NA, -285L))

a <- structure(list(Price = c(16.173027, 16.309193, 25.627169, 26.079411,
                              25.883436, 25.868364, 25.20507, 24.858349, 23.652367, 24.662378,
                              25.551794, 25.868364, 25.159845, 25.597015, 25.597015, 26.365831,
                              25.627169, 26.184935, 26.411057, 26.395979, 25.702539, 24.978949,
                              26.184935, 26.124634, 26.139708, 26.139708, 25.732691, 25.883436,
                              26.380905, 26.415443, 26.127991, 25.764894, 25.94644, 25.94644,
                              25.825409, 26.11286, 25.47744, 25.053823, 25.295889, 25.250502,
                              25.462311, 24.887403, 25.386665, 25.053823, 25.295889, 24.660463,
                              24.509178, 24.963047, 24.645338, 25.038694, 24.766371, 25.961571,
                              25.537954, 25.159723, 25.038694, 24.811756, 23.782978, 24.176332,
                              24.645338, 24.478916, 24.61508, 23.767849, 23.979656, 23.934263,
                              24.040173, 24.085558, 24.5243, 24.947918, 25.038694, 24.312494,
                              23.979656, 22.618032, 22.723938, 21.649773, 22.118773, 21.558996,
                              21.649773, 20.409182, 20.424313, 19.985567, 18.835754, 18.260849,
                              18.383097, 17.293257, 19.008156, 19.745405, 19.376781, 19.825541,
                              20.130056, 18.206802, 19.55308, 18.383097),
                    Date2 = structure(c(18611,
                                        18612, 18324, 18327, 18328, 18331, 18332, 18333, 18334, 18335,
                                        18338, 18340, 18341, 18342, 18345, 18366, 18367, 18368, 18374,
                                        18375, 18376, 18377, 18380, 18381, 18382, 18383, 18384, 18387,
                                        18388, 18534, 18535, 18536, 18537, 18538, 18542, 18543, 18544,
                                        18545, 18548, 18549, 18550, 18551, 18552, 18555, 18556, 18557,
                                        18558, 18559, 18562, 18563, 18564, 18565, 18566, 18569, 18570,
                                        18571, 18572, 18573, 18576, 18577, 18578, 18579, 18580, 18583,
                                        18584, 18585, 18586, 18587, 18590, 18591, 18592, 18594, 18597,
                                        18598, 18599, 18600, 18601, 18604, 18605, 18606, 18607, 18608,
                                        18613, 18614, 18615, 18618, 18619, 18620, 18622, 18625, 18626,
                                        18627), class = "Date"),
                    Group = c("b", "b", "a", "a", "a", "a",
                              "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
                              "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
                              "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
                              "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
                              "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
                              "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
                              "a", "a", "a", "a", "a", "a", "a", "a")),
               class = "data.frame", row.names = c(NA,
                                                   -92L))

Body:
You can set the group independently from the fill. The only downside is that you have to calculate these groups yourself. You can do this with run length encoding.

# Calculate Rle
rle <- rle(b$Group)
# Replace non-NA runs with run IDs
rle <- within(unclass(rle), values[!is.na(values)] <- seq_along(values[!is.na(values)]))

# Add Rle to data
b$run <- inverse.rle(rle)
# Discard unecessary data
b <- b[!is.na(b$Group),]

ggplot(x, aes(x = Date, y = Stock)) +
  geom_ribbon(data = b, inherit.aes = FALSE, aes(
    x = Date2,
    ymin = Price * 1.1,
    ymax = Price,
    y = Price,
    fill = Group,
    group = run
  ), alpha = 0.9) +
  geom_line()


问:
How to access variable in a different file
In my sanic async project, I have a main file called app.py in which I have defined the app instance as such:

```<code>from sanic import Sanic
app=Sanic(name='myapp')
</code>```

Now, I have a different file utility.py in which I want to access the config details for which I will need this app instance. So, how to make the app instance globally available?

Thanks in advance

答:
This has nothing to do with asyncio.  It's just basic Python.  When the interpreter reaches the line in app.py:

```<code>app=Sanic(name='myapp')
</code>```

it instantiates an instance of Sanic and assigns to a variable named "app" that is global to the module app.py.

In utilities.py you put a line something like this:

```<code>from app import app
</code>```

This imports the variable named "app" from the module app.py.  You may have to modify from ___ import app to reflect how you have laid out your modules and packages. But that's the general idea.

For this to work, your top-level script must import app before it imports utilities.

Body:
In my sanic async project, I have a main file called app.py in which I have defined the app instance as such:

from sanic import Sanic
app=Sanic(name='myapp')

Now, I have a different file utility.py in which I want to access the config details for which I will need this app instance. So, how to make the app instance globally available?

Thanks in advance

Body:
This has nothing to do with asyncio.  It's just basic Python.  When the interpreter reaches the line in app.py:

app=Sanic(name='myapp')

it instantiates an instance of Sanic and assigns to a variable named "app" that is global to the module app.py.

In utilities.py you put a line something like this:

from app import app

This imports the variable named "app" from the module app.py.  You may have to modify from ___ import app to reflect how you have laid out your modules and packages. But that's the general idea.

For this to work, your top-level script must import app before it imports utilities.


Title: Ndarray of lists with mix of floats and integers?

Body:
I have an array of lists (corr: N-Dimensional array)

s_cluster_data
Out[410]:
array([[ 0.9607611 ,  0.19538569,  0.        ],
       [ 1.03990463,  0.22274072,  0.        ],
       [ 1.09430461,  0.22603228,  0.        ],
       ...,
       [ 1.10802461, -0.54190659,  2.        ],
       [ 0.9288097 , -0.49195368,  2.        ],
       [ 0.81606986, -0.47141286,  2.        ]])

I would like to make the third column an integer. I've tried to assign dtype as such

dtype=[('A','f8'),('B','f8'),('C','i4')]

s_cluster_data = np.array(s_cluster_data, dtype=dtype)
s_cluster_data

Out[414]:
array([[( 0.9607611 ,  0.9607611 , 0), ( 0.19538569,  0.19538569, 0),
        ( 0.        ,  0.        , 0)],
       [( 1.03990463,  1.03990463, 1), ( 0.22274072,  0.22274072, 0),
        ( 0.        ,  0.        , 0)],
       [( 1.09430461,  1.09430461, 1), ( 0.22603228,  0.22603228, 0),
        ( 0.        ,  0.        , 0)],
       ...,
       dtype=[('A', '<f8'), ('B', '<f8'), ('C', '<i4')])

Which creates an array of lists of tuples (corr: array with dtype), with each index in lists becoming a separate tuple.

I've also tried to take apart the array, read it in as array of tuples, but return back to original state.

list_cluster = s_cluster_data.tolist() # py list
tuple_cluster = [tuple(l) for l in list_cluster] # list of tuples

dtype=[('A','f8'),('B','f8'),('C','i4')]
sd_cluster_data = np.array(tuple_cluster, dtype=dtype) # array of tuples with dtype
sd_cluster_data

Out:   ...,
       (1.0020371 , -0.56034073, 2), (1.18264038, -0.55773913, 2),
       (1.00550194, -0.55359672, 2), (1.10802461, -0.54190659, 2),
       (0.9288097 , -0.49195368, 2), (0.81606986, -0.47141286, 2)],
      dtype=[('A', '<f8'), ('B', '<f8'), ('C', '<i4')])

So ideally the above output is what I would like to see, but with array of lists, not array of tuples.
I tried to take the array apart and merge it back as lists

x_val_arr = np.array([x[0] for x in sd_cluster_data])
y_val_arr = np.array([x[1] for x in sd_cluster_data])
cluster_id_arr = np.array([x[2] for x in sd_cluster_data])

coordinates_arr = np.stack((x_val_arr,y_val_arr,cluster_id_arr),axis=1)

But once again I get floats in the third column

coordinates_arr
Out[416]:
array([[ 0.9607611 ,  0.19538569,  0.        ],
       [ 1.03990463,  0.22274072,  0.        ],
       [ 1.09430461,  0.22603228,  0.        ],
       ...,
       [ 1.10802461, -0.54190659,  2.        ],
       [ 0.9288097 , -0.49195368,  2.        ],
       [ 0.81606986, -0.47141286,  2.        ]])

So this is probably a question due to my lack of domain knowledge, but do ndarrays not support mixed data types if it consists of lists, not tuples?

Body:
In [87]: import numpy.lib.recfunctions as rf
In [88]: arr = np.array([[ 0.9607611 ,  0.19538569,  0.        ],
    ...:        [ 1.03990463,  0.22274072,  0.        ],
    ...:        [ 1.09430461,  0.22603228,  0.        ],
    ...:        [ 1.10802461, -0.54190659,  2.        ],
    ...:        [ 0.9288097 , -0.49195368,  2.        ],
    ...:        [ 0.81606986, -0.47141286,  2.        ]])
In [89]: arr
Out[89]:
array([[ 0.9607611 ,  0.19538569,  0.        ],
       [ 1.03990463,  0.22274072,  0.        ],
       [ 1.09430461,  0.22603228,  0.        ],
       [ 1.10802461, -0.54190659,  2.        ],
       [ 0.9288097 , -0.49195368,  2.        ],
       [ 0.81606986, -0.47141286,  2.        ]])

There are various ways of constructing a structured array from 2d array like this.  Recent versions provide a convenient unstructured_to_structured function:

In [90]: dt = np.dtype([('A','f8'),('B','f8'),('C','i4')])
In [92]: rf.unstructured_to_structured(arr, dt)
Out[92]:
array([(0.9607611 ,  0.19538569, 0), (1.03990463,  0.22274072, 0),
       (1.09430461,  0.22603228, 0), (1.10802461, -0.54190659, 2),
       (0.9288097 , -0.49195368, 2), (0.81606986, -0.47141286, 2)],
      dtype=[('A', '<f8'), ('B', '<f8'), ('C', '<i4')])

Each row of arr has been turned into  a structured record, displayed as a tuple.

A functionally equivalent approach is to create a 'blank' array, and assign field values by name:

In [93]: res = np.zeros(arr.shape[0], dt)
In [94]: res
Out[94]:
array([(0., 0., 0), (0., 0., 0), (0., 0., 0), (0., 0., 0), (0., 0., 0),
       (0., 0., 0)], dtype=[('A', '<f8'), ('B', '<f8'), ('C', '<i4')])
In [95]: res['A'] = arr[:,0]
In [96]: res['B'] = arr[:,1]
In [97]: res['C'] = arr[:,2]
In [98]: res
Out[98]:
array([(0.9607611 ,  0.19538569, 0), (1.03990463,  0.22274072, 0),
       (1.09430461,  0.22603228, 0), (1.10802461, -0.54190659, 2),
       (0.9288097 , -0.49195368, 2), (0.81606986, -0.47141286, 2)],
      dtype=[('A', '<f8'), ('B', '<f8'), ('C', '<i4')])

and to belabor the point, we could also make the structured array from a list of tuples:

In [104]: np.array([tuple(row) for row in arr.tolist()], dt)
Out[104]:
array([(0.9607611 ,  0.19538569, 0), (1.03990463,  0.22274072, 0),
       (1.09430461,  0.22603228, 0), (1.10802461, -0.54190659, 2),
       (0.9288097 , -0.49195368, 2), (0.81606986, -0.47141286, 2)],
      dtype=[('A', '<f8'), ('B', '<f8'), ('C', '<i4')])


问:
AWS Elastic Beanstalk set root volume through .ebextensions
The AWS documentation is so large and confusing, I can't seem to get this working.

I have a docker image that I am deploying onto elasticbeanstalk t2 instance. I want to set the volume to 50 gigs.

It creates the Docker running on 64bit Amazon Linux 2/3.0.1 Platform.

Instance created with:

```<code>eb create --single --tier webserver --instance_type t2.nano
</code>```

I have tried a few solutions suggested and none of them seem to work when i redeploy or create the instance.

Attempt 1:

Inside .ebextensions/options.config

```<code>option_settings:
  aws:autoscaling:launchconfiguration:
    RootVolumeType: gp2
    RootVolumeSize: "50"
</code>```

Attempt 2:

```<code>Resources:
  AWSEBAutoScalingLaunchConfiguration:
    Type: AWS::AutoScaling::LaunchConfiguration
    Properties:
      BlockDeviceMappings:
        - DeviceName: /dev/xvda
          Ebs:
            VolumeType:
              'gp2'
            VolumeSize:
              '50'
</code>```

followed by:

eb deploy

Neither of these change the size of the volume, it's staying put at default.

What am I missing?

答:
I figured out my problem, my .gitignore was setup to ignore the config files. </p>

<p>Both seem to be valid options, not sure which is better. </p>

Body:
The AWS documentation is so large and confusing, I can't seem to get this working.

I have a docker image that I am deploying onto elasticbeanstalk t2 instance. I want to set the volume to 50 gigs.

It creates the Docker running on 64bit Amazon Linux 2/3.0.1 Platform.

Instance created with:

eb create --single --tier webserver --instance_type t2.nano

I have tried a few solutions suggested and none of them seem to work when i redeploy or create the instance.

Attempt 1:

Inside .ebextensions/options.config

option_settings:
  aws:autoscaling:launchconfiguration:
    RootVolumeType: gp2
    RootVolumeSize: "50"

Attempt 2:

Resources:
  AWSEBAutoScalingLaunchConfiguration:
    Type: AWS::AutoScaling::LaunchConfiguration
    Properties:
      BlockDeviceMappings:
        - DeviceName: /dev/xvda
          Ebs:
            VolumeType:
              'gp2'
            VolumeSize:
              '50'

followed by:

eb deploy

Neither of these change the size of the volume, it's staying put at default.

What am I missing?

Body:
I figured out my problem, my .gitignore was setup to ignore the config files.

Both seem to be valid options, not sure which is better.


问:
Nullable type as a generic parameter possible?
I want to do something like this :

```<code>myYear = record.GetValueOrNull<int?>("myYear"),
</code>```

Notice the nullable type as the generic parameter.

Since the GetValueOrNull function could return null my first attempt was this:

```<code>public static T GetValueOrNull<T>(this DbDataRecord reader, string columnName)
  where T : class
{
    object columnValue = reader[columnName];

    if (!(columnValue is DBNull))
    {
        return (T)columnValue;
    }
    return null;
}
</code>```

But the error I'm getting now is:

The type 'int?' must be a reference type in order to use it as parameter 'T' in the generic type or method

Right! Nullable<int> is a struct! So I tried changing the class constraint to a struct constraint (and as a side effect can't return null any more):

```<code>public static T GetValueOrNull<T>(this DbDataRecord reader, string columnName)
  where T : struct
</code>```

Now the assignment:

```<code>myYear = record.GetValueOrNull<int?>("myYear");
</code>```

Gives the following error:

The type 'int?' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method

Is specifying a nullable type as a generic parameter at all possible?

答:
The shorter way :

```<code>public static T ValueOrDefault<T>(this DataRow reader, string columnName) =>
        reader.IsNull(columnName) ? default : (T) reader[columnName];
</code>```

return 0 for int, and null for int?

OwnerDisplayName: Tom Pester

Body:
I want to do something like this :

myYear = record.GetValueOrNull<int?>("myYear"),

Notice the nullable type as the generic parameter.

Since the GetValueOrNull function could return null my first attempt was this:

public static T GetValueOrNull<T>(this DbDataRecord reader, string columnName)
  where T : class
{
    object columnValue = reader[columnName];

    if (!(columnValue is DBNull))
    {
        return (T)columnValue;
    }
    return null;
}

But the error I'm getting now is:

The type 'int?' must be a reference type in order to use it as parameter 'T' in the generic type or method

Right! Nullable<int> is a struct! So I tried changing the class constraint to a struct constraint (and as a side effect can't return null any more):

public static T GetValueOrNull<T>(this DbDataRecord reader, string columnName)
  where T : struct

Now the assignment:

myYear = record.GetValueOrNull<int?>("myYear");

Gives the following error:

The type 'int?' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method

Is specifying a nullable type as a generic parameter at all possible?

LastEditorDisplayName: Gulzar

Body:
The shorter way :

public static T ValueOrDefault<T>(this DataRow reader, string columnName) =>
        reader.IsNull(columnName) ? default : (T) reader[columnName];

return 0 for int, and null for int?


问:
Adding +1 to a Firebase database, keeps running until it is at maximum capacity - how to get it to only add one on run?
I am wanting to make a function that just adds +1 onto the existing database value - it is numeric so resevered + 1

Here is me trialing it inside a function, then just running it from console.

```<code>function AddaMember() {
  playersRef.on("value", function(data, prevChildKey) {
    var newPlayer = data.val();
    var members = firebase.database().ref("locations/hello");
    var age = newPlayer.resevered;
    const newmember = age + 1;
    console.log(age);
    members.update({
      "resevered" : newmember,
    })
  });
}
</code>```

I just want it to add one and then stop, rather it runs until the document is full, what is the work around for this? Should I be using a loop?

Thanks, Max

答:
Your code is using on() to set up a persistent listener the location of playersRef.  That listener will receive a callback every time anything changes at or under the location.  Since your listener is also writing back to a child at that location, it ends up triggering the callback again.

If you don't want a persistent listener, and instead just want to query a single time, use once() instead of on().

```<code>playersRef.once("value").then(snapshot => {
    // work with the data here
})
</code>```

Body:
I am wanting to make a function that just adds +1 onto the existing database value - it is numeric so resevered + 1

Here is me trialing it inside a function, then just running it from console.

function AddaMember() {
  playersRef.on("value", function(data, prevChildKey) {
    var newPlayer = data.val();
    var members = firebase.database().ref("locations/hello");
    var age = newPlayer.resevered;
    const newmember = age + 1;
    console.log(age);
    members.update({
      "resevered" : newmember,
    })
  });
}

I just want it to add one and then stop, rather it runs until the document is full, what is the work around for this? Should I be using a loop?

Thanks, Max

Body:
Your code is using on() to set up a persistent listener the location of playersRef.  That listener will receive a callback every time anything changes at or under the location.  Since your listener is also writing back to a child at that location, it ends up triggering the callback again.

If you don't want a persistent listener, and instead just want to query a single time, use once() instead of on().

playersRef.once("value").then(snapshot => {
    // work with the data here
})


问:
Can jaeger be used to trace an end-to-end transaction with polling & callback used by the underlying components fulfilling steps of the transaction
I'm a newbie on Jaeger and would like to know if I could trace an end-to-end transaction with a parent span & child ones like the one described below with polling from child components (no direct invocation from parent to child) and callback from childs to the parent component.

Context:

First let's describe a simplified view of what I'd like to do.
A solution made of several components exposes a REST API to submit transactions.
It synchronously returns a transaction Id after invocation and will callback the invoker upon completion or failure of the transaction. So what I want to trace is the overall transaction from 1 to 3:

invoker calls solution REST API with transaction input parameters & a callback url

invoker receives an Id from REST API response

solution call the invoker callback url with that Id

Under the hood:

the component (C1) exposing the REST API to submit transaction will perform some actions and then place the transaction in a queue.This is the parent component I was referring to above.

a second component (C2) will poll C1 using an internal REST API to fetch the transaction from the queue and then invoke the REST API exposed by a third component (C3) to further execute actions related to the transaction. In the message C1 propagates a callback url which is then propagated by C2 to C3.

the third component (C3) will inform C1 that it starts to process the transaction by invoking the propagated callback to C1 with IN-PROGRESS

and then C3 orchestrates many actions invoking synchronously (request<->response) or asynchronously (request<->response & then <-callback) different other components

When all actions are completed successfully or when one fails with a un-recoverable error, then component C3 will callback back again C1 to indicate SUCCESS or FAILURE.

When C1 is invoked by C3, it then performs some closing actions and invokes the callback of the transaction invoker.

I'm assuming that it will be possible to trace the entire transaction with Jaeger, but here are my questions:
Question:

Q1: Am I right ? :-)

Q2: How to convey/propagate the parent span created in C1 to C2 ? (between step 1 & 2). I'm thinking at adding the SpanContext in a map as a new attribute of the transaction ?

Q3: Assuming C2 got the span it can propagate it to C3 and as C3 will invoke several times the callback to C1 (steps 3 & 5) how to correlate these callbacks invocation in C1 with the parent span ?

Any hints & tips will be welcome.
Thx.

答:
so I give it a try and my answer to the question above are:

Q1) yes it is possible (congrats to the jaeger team, package pretty easy to grasp with a good documentation)

Q2) I did struggle a bit with this one and thanks to https://github.com/CHOMNANP/jaeger-js-text-map-demo I implemented a solution by adding a "textCarrier" with a ref. to the span context formatted as "FORMAT_TEXT_MAP" to the message Component 1 was publishing towards Component 2.

Code snipper in C1 on the first API invocation

```<code>server.post("/api/vms", (req, res) => {
  console.log('Enter /api/vms');
  const span = tracer.startSpan(req.path);
  // Use the log api to capture a log
  span.log({ event: 'request_received' })
  txSpan = span;
  //console.log("req",span);
  // Use the setTag api to capture standard span tags for http traces
  span.setTag(opentracing.Tags.HTTP_METHOD, req.method)
  span.setTag(opentracing.Tags.SPAN_KIND, opentracing.Tags.SPAN_KIND_RPC_SERVER)
  span.setTag(opentracing.Tags.HTTP_URL, req.path)
</code>```

followed by this part when sending the msg on redis:

```<code>   const textCarrier = getTextCarrierBySpanObject(span);
   tracer.inject(span.context(), opentracing.FORMAT_TEXT_MAP, textCarrier)

   var vm = req.body;
   console.log('Creating a new vm: ', vm);
   // Publish a message by specifying a channel name.
   try {
      Object.assign(vm, { textCarrier });
      pub.publish('tasks-queue', JSON.stringify(vm));
   } catch(e) {
        console.log("App1 Error when publishing task to App2", e);
   }
</code>```

The getTextCarrierBySpanObject function is coming from https://github.com/CHOMNANP/jaeger-js-text-map-demo

```<code>function getTextCarrierBySpanObject(_span) {

    const spanContext = _span.context();
    const traceId = spanContext._traceId.toString('hex');
    const spanId = spanContext._spanId.toString('hex');
    let parentSpanId = spanContext._parentId;
    const flag = _.get(spanContext, '_flags', 1);

    if (parentSpanId) {
        parentSpanId = parentSpanId.toString('hex');
    } else {
        parentSpanId = 0;
    }

    const uberTraceId = `${traceId}:${spanId}:${parentSpanId}:${flag}`;
    console.log("uberTraceId===> ", uberTraceId)

    let textCarrier = {
        "uber-trace-id": uberTraceId
    };

    return textCarrier
}
</code>```

Code snippet in C2 receiving the msg from redis

```<code>sub.on('message', function(channel, message) {
  // message is json string in our case so we are going to parse it.
  try {
    var json = JSON.parse(message)
    console.log("Task received", message);

    const tracer = opentracing.globalTracer();
    // Extracting the span context from the message
    var parentSpan = tracer.extract(opentracing.FORMAT_TEXT_MAP, JSON.parse(message).textCarrier);
    console.log("textCarrier=",JSON.parse(message).textCarrier);
    const span = tracer.startSpan("/msg", { childOf: parentSpan });
    // Use the log api to capture a log
    span.log({ event: 'msg_received' })
</code>```

I tested with version 1.13

```<code>docker run -d --name jaeger   -e COLLECTOR_ZIPKIN_HTTP_PORT=9411   -p 5775:5775/udp   -p 6831:6831/udp   -p 6832:6832/udp   -p 5778:5778   -p 16686:16686   -p 14268:14268   -p 9411:9411   jaegertracing/all-in-one:1.13
</code>```

Q3) This one is pretty straight forward using FORMAT_HTTP_HEADERS to convey the span from the component C4 invoking a callback on C3 and the from the Component C3 invoking a callback on C1. The only "problem" I found was more a "trace readability issue" as, in fact, the Spans appear on the Jaeger UI in "the progagation" order and not in the "timing order" which can be a bit confusing... but the "trace graph" experimental feature allowed to actually see the trace in the right order of appearance, so all good.

So all in all a pretty convincing prototyping exercise with Jaeger, will most probably pilot it now on a real project before trying it in production.

Body:
so I give it a try and my answer to the question above are:

Q1) yes it is possible (congrats to the jaeger team, package pretty easy to grasp with a good documentation)

Q2) I did struggle a bit with this one and thanks to https://github.com/CHOMNANP/jaeger-js-text-map-demo I implemented a solution by adding a "textCarrier" with a ref. to the span context formatted as "FORMAT_TEXT_MAP" to the message Component 1 was publishing towards Component 2.

Code snipper in C1 on the first API invocation

server.post("/api/vms", (req, res) => {
  console.log('Enter /api/vms');
  const span = tracer.startSpan(req.path);
  // Use the log api to capture a log
  span.log({ event: 'request_received' })
  txSpan = span;
  //console.log("req",span);
  // Use the setTag api to capture standard span tags for http traces
  span.setTag(opentracing.Tags.HTTP_METHOD, req.method)
  span.setTag(opentracing.Tags.SPAN_KIND, opentracing.Tags.SPAN_KIND_RPC_SERVER)
  span.setTag(opentracing.Tags.HTTP_URL, req.path)

followed by this part when sending the msg on redis:

   const textCarrier = getTextCarrierBySpanObject(span);
   tracer.inject(span.context(), opentracing.FORMAT_TEXT_MAP, textCarrier)

   var vm = req.body;
   console.log('Creating a new vm: ', vm);
   // Publish a message by specifying a channel name.
   try {
      Object.assign(vm, { textCarrier });
      pub.publish('tasks-queue', JSON.stringify(vm));
   } catch(e) {
        console.log("App1 Error when publishing task to App2", e);
   }

The getTextCarrierBySpanObject function is coming from https://github.com/CHOMNANP/jaeger-js-text-map-demo

function getTextCarrierBySpanObject(_span) {

    const spanContext = _span.context();
    const traceId = spanContext._traceId.toString('hex');
    const spanId = spanContext._spanId.toString('hex');
    let parentSpanId = spanContext._parentId;
    const flag = _.get(spanContext, '_flags', 1);

    if (parentSpanId) {
        parentSpanId = parentSpanId.toString('hex');
    } else {
        parentSpanId = 0;
    }

    const uberTraceId = `${traceId}:${spanId}:${parentSpanId}:${flag}`;
    console.log("uberTraceId===> ", uberTraceId)

    let textCarrier = {
        "uber-trace-id": uberTraceId
    };

    return textCarrier
}

Code snippet in C2 receiving the msg from redis

sub.on('message', function(channel, message) {
  // message is json string in our case so we are going to parse it.
  try {
    var json = JSON.parse(message)
    console.log("Task received", message);

    const tracer = opentracing.globalTracer();
    // Extracting the span context from the message
    var parentSpan = tracer.extract(opentracing.FORMAT_TEXT_MAP, JSON.parse(message).textCarrier);
    console.log("textCarrier=",JSON.parse(message).textCarrier);
    const span = tracer.startSpan("/msg", { childOf: parentSpan });
    // Use the log api to capture a log
    span.log({ event: 'msg_received' })

I tested with version 1.13

docker run -d --name jaeger   -e COLLECTOR_ZIPKIN_HTTP_PORT=9411   -p 5775:5775/udp   -p 6831:6831/udp   -p 6832:6832/udp   -p 5778:5778   -p 16686:16686   -p 14268:14268   -p 9411:9411   jaegertracing/all-in-one:1.13

Q3) This one is pretty straight forward using FORMAT_HTTP_HEADERS to convey the span from the component C4 invoking a callback on C3 and the from the Component C3 invoking a callback on C1. The only "problem" I found was more a "trace readability issue" as, in fact, the Spans appear on the Jaeger UI in "the progagation" order and not in the "timing order" which can be a bit confusing... but the "trace graph" experimental feature allowed to actually see the trace in the right order of appearance, so all good.

So all in all a pretty convincing prototyping exercise with Jaeger, will most probably pilot it now on a real project before trying it in production.


问:
Simplify Rails Method and nested variable
I have 2 methods as seen below:

```<code>def post(type, params=nil)
    if params
    @animalCategory.post(type, params:params[:params], header:{...})
    else
      @animalCategory.post(type, header:{...})
    end
  end

  def put(route, params=nil)
    if params
      @animalCategory.put(type, params:params[:params], header:{...})
    else
      @animalCategory.put(type, header:{...})
    end
  end
</code>```

It gets called like this:

```<code> animal_category.put '/cat', params: data
</code>```

I have a wrapper for two functions above. They basically do the same thing except one calls a post and the other calls a put. I'm wondering if there's a way to merge/simplify it. Also, is there a suggested way to simplify the extraction of params:params[:params]?

答:
```<code>def put_or_post(http_method, type, params=nil)
  if params
    @animalCategory.send(http_method, type, params:params[:params], header:{...})
  else
    @animalCategory.send(http_method, type, header:{...})
  end
end
</code>```

Use Object#send to call methods from variables

You can then refactor the other methods to call this one:

```<code>def put(*args)
  put_or_post :put, *args
end

def post(*args)
  put_or_post :post, *args
end
</code>```

and leave your original call signature (animal_category.put) the same

Body:
I have 2 methods as seen below:

def post(type, params=nil)
    if params
    @animalCategory.post(type, params:params[:params], header:{...})
    else
      @animalCategory.post(type, header:{...})
    end
  end

  def put(route, params=nil)
    if params
      @animalCategory.put(type, params:params[:params], header:{...})
    else
      @animalCategory.put(type, header:{...})
    end
  end

It gets called like this:

 animal_category.put '/cat', params: data

I have a wrapper for two functions above. They basically do the same thing except one calls a post and the other calls a put. I'm wondering if there's a way to merge/simplify it. Also, is there a suggested way to simplify the extraction of params:params[:params]?

Body:
def put_or_post(http_method, type, params=nil)
  if params
    @animalCategory.send(http_method, type, params:params[:params], header:{...})
  else
    @animalCategory.send(http_method, type, header:{...})
  end
end

Use Object#send to call methods from variables

You can then refactor the other methods to call this one:

def put(*args)
  put_or_post :put, *args
end

def post(*args)
  put_or_post :post, *args
end

and leave your original call signature (animal_category.put) the same


问:
How to move the file pointer to next character through "ifstream" without "getting" any character, just like "fseek" does?
seekg uses ios as the second argument, and ios can be set to end or beg or some other values as shown here: http://www.cplusplus.com/reference/iostream/ios/

I just want the pointer to move to the next character, how is that to be accomplished through ifstream?

EDIT
Well, the problem is that I want a function in ifstream similar to fseek, which moves the pointer without reading anything.

答:
I guess peek() and unget() could be useful too.

Use peek() to peek next character so that getline will work as you have wanted.

Use unget to put the character back to your buffer in case you have used get() method.


问:
How to scrape link title over many pages and through specified tab
I am having trouble figuring out how to use BeautifulSoup to scrape all 100 link titles on the page since it is under "a href = ....." . I have tried the below code but it returns a blank.

```<code>from bs4 import BeautifulSoup
from urllib.request import urlopen
import bs4

url = 'https://www150.statcan.gc.ca/n1/en/type/data?count=100'
page = urlopen(url)
soup = bs4.BeautifulSoup(page,'html.parser')
title = soup.find_all('a')
</code>```

Additionally, is there a way to ensure I am scraping everything under the "Tables (8898)" tabs? Thanks in advance!

```<code>Link:

https://www150.statcan.gc.ca/n1/en/type/data?count=100
</code>```

答:
The link you provided is loading it's contents with async javascript requests. So when you exec page = urlopen(url) it is only fetching the empty HTML and javascript blocks.

You need to use a browser to execute js to load page contents. You can checkout this link to learn how to do it: https://towardsdatascience.com/web-scraping-using-selenium-python-8a60f4cf40ab

Body:
I am having trouble figuring out how to use BeautifulSoup to scrape all 100 link titles on the page since it is under "a href = ....." . I have tried the below code but it returns a blank.

from bs4 import BeautifulSoup
from urllib.request import urlopen
import bs4

url = 'https://www150.statcan.gc.ca/n1/en/type/data?count=100'
page = urlopen(url)
soup = bs4.BeautifulSoup(page,'html.parser')
title = soup.find_all('a')

Additionally, is there a way to ensure I am scraping everything under the "Tables (8898)" tabs? Thanks in advance!

Link:

https://www150.statcan.gc.ca/n1/en/type/data?count=100