问:
Filter Python list of dictionaries based on key/value criteria
I have a python list of dictionaries that I need to filter. Specifically, I need all elements from the original list retained but filtered for a specific key/value criteria. Consider the dictionary below. I need all objects retained that have the radius_mean key value >= 13. Thanks!

my_list = [{'id': '842302', 'diagnosis': 'M', 'radius_mean': '17.99', 'texture_mean': '10.38', 'perimeter_mean': '122.8', 'area_mean': '1001', 'smoothness_mean': '0.1184', 'compactness_mean': '0.2776', 'concavity_mean': '0.3001', 'concave points_mean': '0.1471', 'symmetry_mean': '0.2419', 'fractal_dimension_mean': '0.07871', 'radius_se': '1.095', 'texture_se': '0.9053', 'perimeter_se': '8.589', 'area_se': '153.4', 'smoothness_se': '0.006399', 'compactness_se': '0.04904', 'concavity_se': '0.05373', 'concave points_se': '0.01587', 'symmetry_se': '0.03003', 'fractal_dimension_se': '0.006193', 'radius_worst': '25.38', 'texture_worst': '17.33', 'perimeter_worst': '184.6', 'area_worst': '2019', 'smoothness_worst': '0.1622', 'compactness_worst': '0.6656', 'concavity_worst': '0.7119', 'concave points_worst': '0.2654', 'symmetry_worst': '0.4601', 'fractal_dimension_worst': '0.1189'}, {'id': '842517', 'diagnosis': 'M', 'radius_mean': '20.57', 'texture_mean': '17.77', 'perimeter_mean': '132.9', 'area_mean': '1326', 'smoothness_mean': '0.08474', 'compactness_mean': '0.07864', 'concavity_mean': '0.0869', 'concave points_mean': '0.07017', 'symmetry_mean': '0.1812', 'fractal_dimension_mean': '0.05667', 'radius_se': '0.5435', 'texture_se': '0.7339', 'perimeter_se': '3.398', 'area_se': '74.08', 'smoothness_se': '0.005225', 'compactness_se': '0.01308', 'concavity_se': '0.0186', 'concave points_se': '0.0134', 'symmetry_se': '0.01389', 'fractal_dimension_se': '0.003532', 'radius_worst': '24.99', 'texture_worst': '23.41', 'perimeter_worst': '158.8', 'area_worst': '1956', 'smoothness_worst': '0.1238', 'compactness_worst': '0.1866', 'concavity_worst': '0.2416', 'concave points_worst': '0.186', 'symmetry_worst': '0.275', 'fractal_dimension_worst': '0.08902'}, {'id': '84300903', 'diagnosis': 'M', 'radius_mean': '19.69', 'texture_mean': '21.25', 'perimeter_mean': '130', 'area_mean': '1203', 'smoothness_mean': '0.1096', 'compactness_mean': '0.1599', 'concavity_mean': '0.1974', 'concave points_mean': '0.1279', 'symmetry_mean': '0.2069', 'fractal_dimension_mean': '0.05999', 'radius_se': '0.7456', 'texture_se': '0.7869', 'perimeter_se': '4.585', 'area_se': '94.03', 'smoothness_se': '0.00615', 'compactness_se': '0.04006', 'concavity_se': '0.03832', 'concave points_se': '0.02058', 'symmetry_se': '0.0225', 'fractal_dimension_se': '0.004571', 'radius_worst': '23.57', 'texture_worst': '25.53', 'perimeter_worst': '152.5', 'area_worst': '1709', 'smoothness_worst': '0.1444', 'compactness_worst': '0.4245', 'concavity_worst': '0.4504', 'concave points_worst': '0.243', 'symmetry_worst': '0.3613', 'fractal_dimension_worst': '0.08758'}, {'id': '84348301', 'diagnosis': 'M', 'radius_mean': '11.42', 'texture_mean': '20.38', 'perimeter_mean': '77.58', 'area_mean': '386.1', 'smoothness_mean': '0.1425', 'compactness_mean': '0.2839', 'concavity_mean': '0.2414', 'concave points_mean': '0.1052', 'symmetry_mean': '0.2597', 'fractal_dimension_mean': '0.09744', 'radius_se': '0.4956', 'texture_se': '1.156', 'perimeter_se': '3.445', 'area_se': '27.23', 'smoothness_se': '0.00911', 'compactness_se': '0.07458', 'concavity_se': '0.05661', 'concave points_se': '0.01867', 'symmetry_se': '0.05963', 'fractal_dimension_se': '0.009208', 'radius_worst': '14.91', 'texture_worst': '26.5', 'perimeter_worst': '98.87', 'area_worst': '567.7', 'smoothness_worst': '0.2098', 'compactness_worst': '0.8663', 'concavity_worst': '0.6869', 'concave points_worst': '0.2575', 'symmetry_worst': '0.6638', 'fractal_dimension_worst': '0.173'}, {'id': '84358402', 'diagnosis': 'M', 'radius_mean': '20.29', 'texture_mean': '14.34', 'perimeter_mean': '135.1', 'area_mean': '1297', 'smoothness_mean': '0.1003', 'compactness_mean': '0.1328', 'concavity_mean': '0.198', 'concave points_mean': '0.1043', 'symmetry_mean': '0.1809', 'fractal_dimension_mean': '0.05883', 'radius_se': '0.7572', 'texture_se': '0.7813', 'perimeter_se': '5.438', 'area_se': '94.44', 'smoothness_se': '0.01149', 'compactness_se': '0.02461', 'concavity_se': '0.05688', 'concave points_se': '0.01885', 'symmetry_se': '0.01756', 'fractal_dimension_se': '0.005115', 'radius_worst': '22.54', 'texture_worst': '16.67', 'perimeter_worst': '152.2', 'area_worst': '1575', 'smoothness_worst': '0.1374', 'compactness_worst': '0.205', 'concavity_worst': '0.4', 'concave points_worst': '0.1625', 'symmetry_worst': '0.2364', 'fractal_dimension_worst': '0.07678'}]

答:
You can use </p>

```<code>rad_gt_13 = [x for x in my_list if float(x["radius_mean"]) >= 13 ]
</code>```

Body:
You can use

rad_gt_13 = [x for x in my_list if float(x["radius_mean"]) >= 13 ]


来源: stackexchange

答:
One way to filter is via a comprehension:</p>

```<code>filtered_list = [d for d in my_list if float(d['radius_mean']) >= 13.0]
</code>```

ContentLicense: CC BY-SA 4.0

Title: Filter Python list of dictionaries based on key/value criteria

Body:
I have a python list of dictionaries that I need to filter. Specifically, I need all elements from the original list retained but filtered for a specific key/value criteria. Consider the dictionary below. I need all objects retained that have the radius_mean key value >= 13. Thanks!

my_list = [{'id': '842302', 'diagnosis': 'M', 'radius_mean': '17.99', 'texture_mean': '10.38', 'perimeter_mean': '122.8', 'area_mean': '1001', 'smoothness_mean': '0.1184', 'compactness_mean': '0.2776', 'concavity_mean': '0.3001', 'concave points_mean': '0.1471', 'symmetry_mean': '0.2419', 'fractal_dimension_mean': '0.07871', 'radius_se': '1.095', 'texture_se': '0.9053', 'perimeter_se': '8.589', 'area_se': '153.4', 'smoothness_se': '0.006399', 'compactness_se': '0.04904', 'concavity_se': '0.05373', 'concave points_se': '0.01587', 'symmetry_se': '0.03003', 'fractal_dimension_se': '0.006193', 'radius_worst': '25.38', 'texture_worst': '17.33', 'perimeter_worst': '184.6', 'area_worst': '2019', 'smoothness_worst': '0.1622', 'compactness_worst': '0.6656', 'concavity_worst': '0.7119', 'concave points_worst': '0.2654', 'symmetry_worst': '0.4601', 'fractal_dimension_worst': '0.1189'}, {'id': '842517', 'diagnosis': 'M', 'radius_mean': '20.57', 'texture_mean': '17.77', 'perimeter_mean': '132.9', 'area_mean': '1326', 'smoothness_mean': '0.08474', 'compactness_mean': '0.07864', 'concavity_mean': '0.0869', 'concave points_mean': '0.07017', 'symmetry_mean': '0.1812', 'fractal_dimension_mean': '0.05667', 'radius_se': '0.5435', 'texture_se': '0.7339', 'perimeter_se': '3.398', 'area_se': '74.08', 'smoothness_se': '0.005225', 'compactness_se': '0.01308', 'concavity_se': '0.0186', 'concave points_se': '0.0134', 'symmetry_se': '0.01389', 'fractal_dimension_se': '0.003532', 'radius_worst': '24.99', 'texture_worst': '23.41', 'perimeter_worst': '158.8', 'area_worst': '1956', 'smoothness_worst': '0.1238', 'compactness_worst': '0.1866', 'concavity_worst': '0.2416', 'concave points_worst': '0.186', 'symmetry_worst': '0.275', 'fractal_dimension_worst': '0.08902'}, {'id': '84300903', 'diagnosis': 'M', 'radius_mean': '19.69', 'texture_mean': '21.25', 'perimeter_mean': '130', 'area_mean': '1203', 'smoothness_mean': '0.1096', 'compactness_mean': '0.1599', 'concavity_mean': '0.1974', 'concave points_mean': '0.1279', 'symmetry_mean': '0.2069', 'fractal_dimension_mean': '0.05999', 'radius_se': '0.7456', 'texture_se': '0.7869', 'perimeter_se': '4.585', 'area_se': '94.03', 'smoothness_se': '0.00615', 'compactness_se': '0.04006', 'concavity_se': '0.03832', 'concave points_se': '0.02058', 'symmetry_se': '0.0225', 'fractal_dimension_se': '0.004571', 'radius_worst': '23.57', 'texture_worst': '25.53', 'perimeter_worst': '152.5', 'area_worst': '1709', 'smoothness_worst': '0.1444', 'compactness_worst': '0.4245', 'concavity_worst': '0.4504', 'concave points_worst': '0.243', 'symmetry_worst': '0.3613', 'fractal_dimension_worst': '0.08758'}, {'id': '84348301', 'diagnosis': 'M', 'radius_mean': '11.42', 'texture_mean': '20.38', 'perimeter_mean': '77.58', 'area_mean': '386.1', 'smoothness_mean': '0.1425', 'compactness_mean': '0.2839', 'concavity_mean': '0.2414', 'concave points_mean': '0.1052', 'symmetry_mean': '0.2597', 'fractal_dimension_mean': '0.09744', 'radius_se': '0.4956', 'texture_se': '1.156', 'perimeter_se': '3.445', 'area_se': '27.23', 'smoothness_se': '0.00911', 'compactness_se': '0.07458', 'concavity_se': '0.05661', 'concave points_se': '0.01867', 'symmetry_se': '0.05963', 'fractal_dimension_se': '0.009208', 'radius_worst': '14.91', 'texture_worst': '26.5', 'perimeter_worst': '98.87', 'area_worst': '567.7', 'smoothness_worst': '0.2098', 'compactness_worst': '0.8663', 'concavity_worst': '0.6869', 'concave points_worst': '0.2575', 'symmetry_worst': '0.6638', 'fractal_dimension_worst': '0.173'}, {'id': '84358402', 'diagnosis': 'M', 'radius_mean': '20.29', 'texture_mean': '14.34', 'perimeter_mean': '135.1', 'area_mean': '1297', 'smoothness_mean': '0.1003', 'compactness_mean': '0.1328', 'concavity_mean': '0.198', 'concave points_mean': '0.1043', 'symmetry_mean': '0.1809', 'fractal_dimension_mean': '0.05883', 'radius_se': '0.7572', 'texture_se': '0.7813', 'perimeter_se': '5.438', 'area_se': '94.44', 'smoothness_se': '0.01149', 'compactness_se': '0.02461', 'concavity_se': '0.05688', 'concave points_se': '0.01885', 'symmetry_se': '0.01756', 'fractal_dimension_se': '0.005115', 'radius_worst': '22.54', 'texture_worst': '16.67', 'perimeter_worst': '152.2', 'area_worst': '1575', 'smoothness_worst': '0.1374', 'compactness_worst': '0.205', 'concavity_worst': '0.4', 'concave points_worst': '0.1625', 'symmetry_worst': '0.2364', 'fractal_dimension_worst': '0.07678'}]

Body:
One way to filter is via a comprehension:

filtered_list = [d for d in my_list if float(d['radius_mean']) >= 13.0]


问:
Why do methods on @RestControllerAdvice class return HTML instead of JSON?
I have the following exception handler:

```<code>@RestControllerAdvice
@RequiredArgsConstructor
public class ControllerExceptionHandler {

    @ExceptionHandler(FeignException.class)
    @ResponseBody
    public String afterThrowing(FeignException ex, HttpServletResponse response) {
        response.setStatus(ex.status());
        return ex.contentUTF8();
    }

}
</code>```

I would expect when the FeignException propagates to one of my REST controllers that

The afterThrowing method would be called

The response returned to an HTTP client would be JSON

The method is called but the content type returned to the client is HTML instead of JSON. How can I have JSON returned instead of HTML?

答:
You should wrap your response with something(class or map).

Wrapper class :

```<code>public class ApiError {
    private HttpStatus status;
    private String response;

    public ApiError(String response, HttpStatus status) {
       this.response = s;
       this.status = status;
    }

    // getter setter
}
</code>```

And exception handler :

```<code>  @ExceptionHandler(FeignException.class)
  protected ResponseEntity<Object> handleFeignException(FeignException ex) {
    ApiError apiError = new ApiError(ex.contentUTF8(), NOT_ACCEPTABLE);
    return new ResponseEntity<>(apiError, apiError.getStatus());
  }
</code>```

For further reading you can check this question : How to return simple String as JSON in Rest Controller

Edit:
Since your ex.contentUTF8() call returns valid JSON you don't need to wrap it. Simply return String with ResponseEntity.

```<code>  @ExceptionHandler(FeignException.class)
  protected ResponseEntity<String> handleFeignException(FeignException ex) {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    return new ResponseEntity<>(ex.contentUTF8(), headers, BAD_REQUEST);
  }
</code>```

Body:
I have the following exception handler:

@RestControllerAdvice
@RequiredArgsConstructor
public class ControllerExceptionHandler {

    @ExceptionHandler(FeignException.class)
    @ResponseBody
    public String afterThrowing(FeignException ex, HttpServletResponse response) {
        response.setStatus(ex.status());
        return ex.contentUTF8();
    }

}

I would expect when the FeignException propagates to one of my REST controllers that

The afterThrowing method would be called

The response returned to an HTTP client would be JSON

The method is called but the content type returned to the client is HTML instead of JSON. How can I have JSON returned instead of HTML?

Body:
You should wrap your response with something(class or map).

Wrapper class :

public class ApiError {
    private HttpStatus status;
    private String response;

    public ApiError(String response, HttpStatus status) {
       this.response = s;
       this.status = status;
    }

    // getter setter
}

And exception handler :

  @ExceptionHandler(FeignException.class)
  protected ResponseEntity<Object> handleFeignException(FeignException ex) {
    ApiError apiError = new ApiError(ex.contentUTF8(), NOT_ACCEPTABLE);
    return new ResponseEntity<>(apiError, apiError.getStatus());
  }

For further reading you can check this question : How to return simple String as JSON in Rest Controller

Edit:
Since your ex.contentUTF8() call returns valid JSON you don't need to wrap it. Simply return String with ResponseEntity.

  @ExceptionHandler(FeignException.class)
  protected ResponseEntity<String> handleFeignException(FeignException ex) {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    return new ResponseEntity<>(ex.contentUTF8(), headers, BAD_REQUEST);
  }


问:
Creating Binary Tree
Most of the questions I've searched for regarding binary trees shows the implementation of binary search trees, but not binary trees. The terms of a complete binary tree are:

Either an empty tree or it has 1 node with 2 children, where each
child is another Binary Tree.

All levels are full (except for possibly the last level)

All leaves on the bottom-most level are
as far left as possible.

I've come up with a concept but it doesn't seem to running through the recursion properly -- Does anyone know what I'm doing wrong?

```<code>class Node():
    def __init__(self, key):
        self.key = key
        self.left = None
        self.right = None

    def add(self, key):
        if self.key:
            if self.left is None:
                self.left = Node(key)
            else:
                self.left.add(key)

                if self.right is None:
                    self.right = Node(key)
                else:
                    self.right.add(key)
        else:
            self.key = key
        return (self.key)
</code>```

答: You can use the sklearn Decision trees, as they are able to be set up as binary decision trees as well. link to the documentation here.</p>

Title: Creating Binary Tree

Body:
Most of the questions I've searched for regarding binary trees shows the implementation of binary search trees, but not binary trees. The terms of a complete binary tree are:

Either an empty tree or it has 1 node with 2 children, where each
child is another Binary Tree.

All levels are full (except for possibly the last level)

All leaves on the bottom-most level are
as far left as possible.

I've come up with a concept but it doesn't seem to running through the recursion properly -- Does anyone know what I'm doing wrong?

class Node():
    def __init__(self, key):
        self.key = key
        self.left = None
        self.right = None

    def add(self, key):
        if self.key:
            if self.left is None:
                self.left = Node(key)
            else:
                self.left.add(key)

                if self.right is None:
                    self.right = Node(key)
                else:
                    self.right.add(key)
        else:
            self.key = key
        return (self.key)


问:
COVID-19 Data visualization with R
I wanted to use this time to improve my skills with R. I chose CoVID-19 as my topic and would like to visualize some data and maybe analyze it. I would be interested in how globalization is connected to the pandemic (maybe with a regression analysis). But first I would like to visualize some data. Do you have any tips which package is helpful for my purposes?
I've already tried a little bit and I'm not really getting anywhere.
My idea was a simple time series plot with the cumulated data of the Ecdc, which can be found in almost every newspaper nowdays.
As Data I used:

```<code>data<- read.csv(file= "https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/ecdc/total_cases_per_million.csv")
</code>```

I have already looked at some tutorials and researched here in Stackoverflow. Until now I could not output a logical plot. My Goal is to recreate the two following pictures in R:

答:
Since the question seems to be mostly about how to get started with visualising this data, here is how one would plot a simple timeseries graph with some countries highlighted with the ggplot2 package.

Libraries and package import

library(tidyverse)

data<- read.csv(file= "https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/ecdc/total_cases_per_million.csv")
```

<p>The data is in what is called a 'wide' format, where columns are a date or region. ggplot2 works better with long data, where each observation is a row. You could convert these as follows:</p>

```long <- pivot_longer(data, World:Zimbabwe)
```

<p>Next, we need to clean the data just a little bit by converting the date column to a <code>Date</code> class and throwing out <code>NA</code> observations (the latter is not needed but recommended, if you get <code>NA</code> warnings now it is probably a human error rather than data error).</p>

```long$date <- as.Date(long$date)
long <- long[!is.na(long$value),]
```

<p>We could pick a few countries that we would like to highlight.</p>

```highlight_countries <- c("Russia", "San.Marino", "United.States")
```

<p>Then we can make a lineplot out of this. There are a lot of tutorials about how to use ggplot, so you could search for those to customise the plot to your specific needs.</p>

```ggplot(long, aes(x = date, y = value)) +
  geom_line(aes(group = name,
                colour = ifelse(name %in% highlight_countries, name, NA))) +
  scale_colour_discrete(name = "Regions", labels = c(highlight_countries, "Other"))
```

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

Title: COVID-19 Data visualization with R

Body:
I wanted to use this time to improve my skills with R. I chose CoVID-19 as my topic and would like to visualize some data and maybe analyze it. I would be interested in how globalization is connected to the pandemic (maybe with a regression analysis). But first I would like to visualize some data. Do you have any tips which package is helpful for my purposes?
I've already tried a little bit and I'm not really getting anywhere.
My idea was a simple time series plot with the cumulated data of the Ecdc, which can be found in almost every newspaper nowdays.
As Data I used:

data<- read.csv(file= "https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/ecdc/total_cases_per_million.csv")

I have already looked at some tutorials and researched here in Stackoverflow. Until now I could not output a logical plot. My Goal is to recreate the two following pictures in R:

Body:
Since the question seems to be mostly about how to get started with visualising this data, here is how one would plot a simple timeseries graph with some countries highlighted with the ggplot2 package.

Libraries and package import

library(tidyverse)

data<- read.csv(file= "https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/ecdc/total_cases_per_million.csv")

The data is in what is called a 'wide' format, where columns are a date or region. ggplot2 works better with long data, where each observation is a row. You could convert these as follows:

long <- pivot_longer(data, World:Zimbabwe)

Next, we need to clean the data just a little bit by converting the date column to a Date class and throwing out NA observations (the latter is not needed but recommended, if you get NA warnings now it is probably a human error rather than data error).

long$date <- as.Date(long$date)
long <- long[!is.na(long$value),]

We could pick a few countries that we would like to highlight.

highlight_countries <- c("Russia", "San.Marino", "United.States")

Then we can make a lineplot out of this. There are a lot of tutorials about how to use ggplot, so you could search for those to customise the plot to your specific needs.

ggplot(long, aes(x = date, y = value)) +
  geom_line(aes(group = name,
                colour = ifelse(name %in% highlight_countries, name, NA))) +
  scale_colour_discrete(name = "Regions", labels = c(highlight_countries, "Other"))

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


问:
How can I upload an inline picture from a chat message as a new bitmap file?
I'm trying to upload the content bytes of an inline picture from a Teams chat message as a new bitmap file. I'm getting the content bytes of the inline picture with this method GET https://graph.microsoft.com/beta/teams/{teamId}/channels/{channelId}/messages/{messageId}/hostedContents/{hostedContentId}/$value.

The response gives me content bytes which I try sending directly as the request body in this call PUT https://graph.microsoft.com/beta/drives/{channelDriveId}/items/{currentPhaseInBilderFolderId}:/picture_123.jpg:/content. To this request I have included "image/jpeg" as Content-Type in the header. The file gets created alright, but it's not created as an image. When trying to view the picture from the newly created file I'm prompted with this error: "This is not a valid bitmap file, or its format is not currently supported".

How can I grab an inline picture in a chat message and create a bitmap file out of it using Microsoft graph?

答:
Can you share the complete PUT request along with the file which got created in the drive folder?
Without much information on the file I will not be able to help further

Edit-1:

Try specifying the image name with file format (xyz.bmp or xyz.jpg) when uploading the contents through PUT request.

Could you also post the PUT reponse here

like the example in graph api docs

```<code>HTTP/1.1 201 Created
Content-Type: application/json
        {
          "id": "0123456789abc",
          "name": "FileB.txt",
          "size": 35,
          "file": { }
        }
</code>```

Body:
Can you share the complete PUT request along with the file which got created in the drive folder?
Without much information on the file I will not be able to help further

Edit-1:

Try specifying the image name with file format (xyz.bmp or xyz.jpg) when uploading the contents through PUT request.

Could you also post the PUT reponse here

like the example in graph api docs

HTTP/1.1 201 Created
Content-Type: application/json
        {
          "id": "0123456789abc",
          "name": "FileB.txt",
          "size": 35,
          "file": { }
        }


问:
Docker - multiple base images? NetCore runtime with Python in single image
I have a console .Net Core application which also uses Python, nVidia CUDA runtime, etc.

I know about building patterns, but so far I just directly use build output of Visual Studio to create image, so my Dockerfile is simple:

```<code>FROM mcr.microsoft.com/dotnet/core/runtime:3.1.4-buster-slim AS base
FROM base AS final
WORKDIR /app
COPY . .
ENTRYPOINT ["dotnet", "MyApp.dll"]
</code>```

In the same way as with the NetCore runtime, there're Python and nVidia CUDA images available. But if I just run another container with Python - my app is unable to use Python from this container,  because containers are isolated and they may only interact using TCP. My thought was that I might use multiple images as base image for image with my app, but I don't see a way to use multiple FROM statements to combine one single image from them.

What's the proper way of using Python, CUDA and other images with environments from container with my running app, which only has NetCore runtime by default?

答:
In the case where there's no container image that includes everything you want, you can write your own container image to build such a thing.

```<code>FROM mcr.microsoft.com/dotnet/core/runtime:3.1.4-buster-slim AS base
</code>```

This container is based on Debian buster. That's a Linux distribution that has Python. It probably doesn't have CUDA, since Debian's all about Freedom and CUDA is non-free, to the best of my knowledge.

In this case, you can do 2 things:

Install languages/libraries you need that are available in the distribution using the package manager

Some languages/runtimes - including CUDA provide packages for some distributions. Look at their install instructions and use those in your dockerfile.

Manually install things that are not available via the package manger.

So you probably want something like this:

```<code>FROM mcr.microsoft.com/dotnet/core/runtime:3.1.4-buster-slim AS base
RUN apt-get install -y python3
RUN wget http://developer.download.nvidia.com/compute/cuda/10.2/Prod/cluster_management/cuda_cluster_pkgs_10.2.89_440.33.01_ubuntu1804.tar.gz
RUN true # extract/install the tar file from above, I dont use CUDA so I dont know
WORKDIR /app
COPY . .
ENTRYPOINT ["dotnet", "MyApp.dll"]
</code>```

This dockerfile starts with a container image that include .NET Core. Then it installs python in it. Then it installs CUDA in it. Then it runs your application.

Body:
I have a console .Net Core application which also uses Python, nVidia CUDA runtime, etc.

I know about building patterns, but so far I just directly use build output of Visual Studio to create image, so my Dockerfile is simple:

FROM mcr.microsoft.com/dotnet/core/runtime:3.1.4-buster-slim AS base
FROM base AS final
WORKDIR /app
COPY . .
ENTRYPOINT ["dotnet", "MyApp.dll"]

In the same way as with the NetCore runtime, there're Python and nVidia CUDA images available. But if I just run another container with Python - my app is unable to use Python from this container,  because containers are isolated and they may only interact using TCP. My thought was that I might use multiple images as base image for image with my app, but I don't see a way to use multiple FROM statements to combine one single image from them.

What's the proper way of using Python, CUDA and other images with environments from container with my running app, which only has NetCore runtime by default?

Body:
In the case where there's no container image that includes everything you want, you can write your own container image to build such a thing.

FROM mcr.microsoft.com/dotnet/core/runtime:3.1.4-buster-slim AS base

This container is based on Debian buster. That's a Linux distribution that has Python. It probably doesn't have CUDA, since Debian's all about Freedom and CUDA is non-free, to the best of my knowledge.

In this case, you can do 2 things:

Install languages/libraries you need that are available in the distribution using the package manager

Some languages/runtimes - including CUDA provide packages for some distributions. Look at their install instructions and use those in your dockerfile.

Manually install things that are not available via the package manger.

So you probably want something like this:

FROM mcr.microsoft.com/dotnet/core/runtime:3.1.4-buster-slim AS base
RUN apt-get install -y python3
RUN wget http://developer.download.nvidia.com/compute/cuda/10.2/Prod/cluster_management/cuda_cluster_pkgs_10.2.89_440.33.01_ubuntu1804.tar.gz
RUN true # extract/install the tar file from above, I dont use CUDA so I dont know
WORKDIR /app
COPY . .
ENTRYPOINT ["dotnet", "MyApp.dll"]

This dockerfile starts with a container image that include .NET Core. Then it installs python in it. Then it installs CUDA in it. Then it runs your application.


问:
Java 8: Lambda with variable arguments
I am looking for a way to invoke multiple argument methods but using a lambda construct. In the documentation it is said that lambda is only usable if it can map to a functional interface.

I want to do something like:

```<code>test((arg0, arg1) -> me.call(arg0, arg1));
test((arg0, arg1, arg2) -> me.call(arg0, arg1, arg2));
...
</code>```

Is there any way one can do this elegantly without defining 10 interfaces, one for each argument count?

Update

I use multiple interfaces extending from a non-method interface and I overload the method.

Example for two arguments:

```<code>interface Invoker {}
interface Invoker2 extends Invoker { void invoke(Object arg0, Object arg1);}
void test(Invoker2 invoker, Object ... arguments) {
    test((Invoker)invoker, Object ... arguments);
}

void test(Invoker invoker, Object ... arguments) {
    //Use Reflection or whatever to access the provided invoker
}
</code>```

I hope for a possibility to replace the 10 invoker interfaces and the 10 overloaded methods with a single solution.

I have a reasonable use case and please do not ask questions like 'Why would you do such a thing?' and 'What is the problem you are trying to solve?' or anything like that. Just know that I have thought this through and this is a legitimate problem I'm try to solve.

Sorry to add confusion calling it invoker but it is actually what it is called in my current use case (testing constructor contracts).

Basically, as stated above, think about a method that works with a different number of attributes within the lambda.

答:
I was curious if a lambda for a variable number of arguments would work. Indeed it seems to. See:

```<code>public class Example {
    public interface Action<T> {
        public void accept(T... ts);
    }

    public static void main(String args[]) {
        // Action<String> a = (String... x) -> { also works
        Action<String> a = (x) -> {
            for(String s : x) {
                System.out.println(s);
            }
        };

        a.accept("Hello", "World");
    }
}
</code>```

I know the question has been answered. This is mainly for others who are curious and come across this post.

ContentLicense: CC BY-SA 3.0

Title: Java 8: Lambda with variable arguments

Body:
I am looking for a way to invoke multiple argument methods but using a lambda construct. In the documentation it is said that lambda is only usable if it can map to a functional interface.

I want to do something like:

test((arg0, arg1) -> me.call(arg0, arg1));
test((arg0, arg1, arg2) -> me.call(arg0, arg1, arg2));
...

Is there any way one can do this elegantly without defining 10 interfaces, one for each argument count?

Update

I use multiple interfaces extending from a non-method interface and I overload the method.

Example for two arguments:

interface Invoker {}
interface Invoker2 extends Invoker { void invoke(Object arg0, Object arg1);}
void test(Invoker2 invoker, Object ... arguments) {
    test((Invoker)invoker, Object ... arguments);
}

void test(Invoker invoker, Object ... arguments) {
    //Use Reflection or whatever to access the provided invoker
}

I hope for a possibility to replace the 10 invoker interfaces and the 10 overloaded methods with a single solution.

I have a reasonable use case and please do not ask questions like 'Why would you do such a thing?' and 'What is the problem you are trying to solve?' or anything like that. Just know that I have thought this through and this is a legitimate problem I'm try to solve.

Sorry to add confusion calling it invoker but it is actually what it is called in my current use case (testing constructor contracts).

Basically, as stated above, think about a method that works with a different number of attributes within the lambda.

Body:
I was curious if a lambda for a variable number of arguments would work. Indeed it seems to. See:

public class Example {
    public interface Action<T> {
        public void accept(T... ts);
    }

    public static void main(String args[]) {
        // Action<String> a = (String... x) -> { also works
        Action<String> a = (x) -> {
            for(String s : x) {
                System.out.println(s);
            }
        };

        a.accept("Hello", "World");
    }
}

I know the question has been answered. This is mainly for others who are curious and come across this post.


问:
Stop Python script with ctrl + c
I have a script which uses threads, but it is unable to catch Ctrl + C.

Here it is the sample code to reproduce this error:

```<code>import threading
import time
import signal

class DummyThread(threading.Thread):

    def __init__(self):
        threading.Thread.__init__(self)
        self._running = True
        signal.signal(signal.SIGINT, self.stop)
        signal.signal(signal.SIGTERM, self.stop)

    def stop(self, signum=None, frame=None):
        self._running = False

    def run(self):
        while self._running:
            time.sleep(1)
            print("Running")

if __name__ == "__main__":
    try:
        t = DummyThread()
        t.start()
        while True:
            print("Main thread running")
            time.sleep(0.5)
    except KeyboardInterrupt:
        print("This never gets printed")
        t.stop()
    finally:
        print("Exit")
</code>```

When I run python3 script.py it starts running, but it does not catch ctrl+c. I have googled it but I have not found a solution. I have to kill the script with SIGTERM, but I want DummyThread to stop gracefully.

答:
```<code>class DummyThread(threading.Thread):

    def __init__(self):
        threading.Thread.__init__(self)
        self._running = True
        signal.signal(signal.SIGINT, self.stop)
        signal.signal(signal.SIGTERM, self.stop)
</code>```

The program actually does not work as expected because of those last two lines and would work without them.

The reason is that, if you press Ctrl-C, the SIGINT signal is handled by the signal handler that is set up by signal.signal and self.stop is called. So the thread should actually stop.

But in the main thread, the while True loop is still running. Since the signal has already been handled, there will be no KeyboardInterrupt exception raised by the Python runtime. Therefore you never get to the except part.

```<code>if __name__ == "__main__":
    try:
        t = DummyThread()
        t.start()
        while True:            # you are stuck in this loop
            print("Main thread running")
            time.sleep(0.5)
    except KeyboardInterrupt:  # this never happens
        print("This never gets printed")
        t.stop()
</code>```

Only one signal handler should be set up to call the stop method. So there are two options to solve the problem:

Handle the signal implicitly by catching the KeyboardInterrupt exception. This is achieved by simply removing the two signal.signal(...) lines.

Set up an explicit signal handler (as you did by using signal.signal in DummyThread.__init__), but remove the while True: loop from the main thread and do not try to handle KeyboardInterrupt. Instead, just wait for the DummyThread to finish on its own by using its join method:

```<code>if __name__ == "__main__":
    t = DummyThread()
    t.start()
    t.join()
    print("Exit")
</code>```

Title: Stop Python script with ctrl + c

Body:
I have a script which uses threads, but it is unable to catch Ctrl + C.

Here it is the sample code to reproduce this error:

import threading
import time
import signal

class DummyThread(threading.Thread):

    def __init__(self):
        threading.Thread.__init__(self)
        self._running = True
        signal.signal(signal.SIGINT, self.stop)
        signal.signal(signal.SIGTERM, self.stop)

    def stop(self, signum=None, frame=None):
        self._running = False

    def run(self):
        while self._running:
            time.sleep(1)
            print("Running")

if __name__ == "__main__":
    try:
        t = DummyThread()
        t.start()
        while True:
            print("Main thread running")
            time.sleep(0.5)
    except KeyboardInterrupt:
        print("This never gets printed")
        t.stop()
    finally:
        print("Exit")

When I run python3 script.py it starts running, but it does not catch ctrl+c. I have googled it but I have not found a solution. I have to kill the script with SIGTERM, but I want DummyThread to stop gracefully.

Body:
class DummyThread(threading.Thread):

    def __init__(self):
        threading.Thread.__init__(self)
        self._running = True
        signal.signal(signal.SIGINT, self.stop)
        signal.signal(signal.SIGTERM, self.stop)

The program actually does not work as expected because of those last two lines and would work without them.

The reason is that, if you press Ctrl-C, the SIGINT signal is handled by the signal handler that is set up by signal.signal and self.stop is called. So the thread should actually stop.

But in the main thread, the while True loop is still running. Since the signal has already been handled, there will be no KeyboardInterrupt exception raised by the Python runtime. Therefore you never get to the except part.

if __name__ == "__main__":
    try:
        t = DummyThread()
        t.start()
        while True:            # you are stuck in this loop
            print("Main thread running")
            time.sleep(0.5)
    except KeyboardInterrupt:  # this never happens
        print("This never gets printed")
        t.stop()

Only one signal handler should be set up to call the stop method. So there are two options to solve the problem:

Handle the signal implicitly by catching the KeyboardInterrupt exception. This is achieved by simply removing the two signal.signal(...) lines.

Set up an explicit signal handler (as you did by using signal.signal in DummyThread.__init__), but remove the while True: loop from the main thread and do not try to handle KeyboardInterrupt. Instead, just wait for the DummyThread to finish on its own by using its join method:

if __name__ == "__main__":
    t = DummyThread()
    t.start()
    t.join()
    print("Exit")


问:
Mock a typescript interface with jest
Is it possible to mock a typescript interface with jest?

For example:

```<code>import { IMultiplier } from "./IMultiplier";

export class Math {
  multiplier: IMultiplier;

  public multiply (a: number, b: number) {
    return this.multiplier.multiply(a, b);
  }
}
</code>```

Then in a test:

```<code>import { Math } from "../src/Math";
import { IMultiplier } from "../src/IMultiplier";

describe("Math", () => {

    it("can multiply", () => {
        let mathlib = new Math();
        mathlib.multiplier = // <--- assign this property a mock
        let result = mathlib.multiply(10, 2);
        expect(result).toEqual(20);
    });
});
</code>```

I've tried to create a mock object to satisfy this a number of ways, but none work. For example assigning it this mock:

```<code>let multiplierMock = jest.fn(() => ({ multiply: jest.fn() }));
</code>```

Will produce something along the lines of:

```<code>Error - Type 'Mock<{ multiply: Mock<{}>; }>' is not assignable to type 'IMultiplier'.
</code>```

答:
try out moq.ts library.

import {Mock} from "moq.ts";

const multiplier = new Mock<IMultiplier>()
   .setup(instance => instance.multiply(3, 4))
   .returns(12)
   .object();

let mathlib = new Math();
mathlib.multiplier = multiplier;
```

Title: Mock a typescript interface with jest

Body:
Is it possible to mock a typescript interface with jest?

For example:

import { IMultiplier } from "./IMultiplier";

export class Math {
  multiplier: IMultiplier;

  public multiply (a: number, b: number) {
    return this.multiplier.multiply(a, b);
  }
}

Then in a test:

import { Math } from "../src/Math";
import { IMultiplier } from "../src/IMultiplier";

describe("Math", () => {

    it("can multiply", () => {
        let mathlib = new Math();
        mathlib.multiplier = // <--- assign this property a mock
        let result = mathlib.multiply(10, 2);
        expect(result).toEqual(20);
    });
});

I've tried to create a mock object to satisfy this a number of ways, but none work. For example assigning it this mock:

let multiplierMock = jest.fn(() => ({ multiply: jest.fn() }));

Will produce something along the lines of:

Error - Type 'Mock<{ multiply: Mock<{}>; }>' is not assignable to type 'IMultiplier'.


问:
Are there advanced troubleshooting techniques I can try to debug a JavaFX process that hangs on close?
So this one is a bit of a noodle scratcher, and I was wondering if anyone out there has ideas on things I might be able to try to get to the bottom of this.

JRE/JDK is 1.8.0_251, running on Windows 10. Application is a JavaFX application launched using launch4j.

Sometimes when I exit the application, the JVM will not shut down.  Instead, I have a process stuck in Task Manager until I explicitly close it.  It used to be intermittent, now it's almost constant.

I suspect I've got a user thread stuck which is blocking the application from closing.  However, I have not been able to collect a useful thread dump to see if I can identify where the thread is being created.

When I run into this situation, I identify the process ID via tasklist / wmic (JMC doesn't see the process), then I try to run jstack against it:

"jstack -F" works, but removes all helpful information like thread names or type of threads (daemon / user). None of the threads contain any references to my code.

"jstack -l" used to work, and I saw two affected threads named "pool-#-thread-#" that were not daemon threads but then I had to install the _251 JDK after a JRE update was pushed to my machine and since then, it just hangs forever. (I've left it to run for hours, no change)

If I could get the ability to run jstack -l back, it'd go a long way towards helping me identify the Executor pool causing this.  The application spawns a few different pools, and the few I know of all explicitly set the daemon flag on threads they generate.

Interestingly enough, I have a similar problem when running the application from Eclipse. None of the other people on my team can reproduce this problem, and this problem only started after I had received a replacement laptop.  You'd think if there was a sticky user thread, other people would see the same problem. This makes me think there might be something environmental/hardware involved or that I might be triggering a JRE bug.

Has anyone out there successfully troubleshooted an issue like this where jstack is hanging?  Any tricks for getting a thread dump when the application fails to shut down?

答:
So, after poking around at this again for some time, I've at least made some progress and have a good idea what is causing the problem, though not necessarily how to solve it just yet.

The thread dump acquired via jstack -F contained a tell-tale line that I had missed having been focused on the fact that there were no thread names / daemon status in the output received:

```<code>Thread 22: (state = IN_NATIVE)
- com.sun.prism.d3d.D3DPipeline.nDispose() u/bci=0 (Interpreted frame)
- com.sun.prism.d3d.D3DPipeline.dispose() u/bci=49, line=164 (Interpreted frame)
- com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.cleanup() u/bci=9, line=118 (Interpreted frame)
- com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.run() u/bci=14, line=127 (Interpreted frame)
- java.lang.Thread.run() u/bci=11 (Interpreted frame)
</code>```

Having done Java support for years, I know that the hardware accelerated Java UI can have troubles with certain graphics cards / drivers. So I tried disabling the d3d renderer using the -Dsun.java2d.d3d=falseflag on the JVM.  Wasn't able to reproduce the hang.

Of course, just disabling hardware acceleration is not the ideal solution for me, so I've been delving deeper into troubleshooting the issue.  The call stack above led me to learn about the -Dprism.verbose flag which gave me more information about what card was being used. (Internal Intel GPU rather than my external GPU)

I've since tried upgrading the drivers, and will look into how I might be able to switch prism from using the intel chipset to my dedicated GPU and see if that resolves the problem.  But at the very least, I do have the option to disable the d3d acceleration completely.

Body:
So, after poking around at this again for some time, I've at least made some progress and have a good idea what is causing the problem, though not necessarily how to solve it just yet.

The thread dump acquired via jstack -F contained a tell-tale line that I had missed having been focused on the fact that there were no thread names / daemon status in the output received:

Thread 22: (state = IN_NATIVE)
- com.sun.prism.d3d.D3DPipeline.nDispose() u/bci=0 (Interpreted frame)
- com.sun.prism.d3d.D3DPipeline.dispose() u/bci=49, line=164 (Interpreted frame)
- com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.cleanup() u/bci=9, line=118 (Interpreted frame)
- com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.run() u/bci=14, line=127 (Interpreted frame)
- java.lang.Thread.run() u/bci=11 (Interpreted frame)

Having done Java support for years, I know that the hardware accelerated Java UI can have troubles with certain graphics cards / drivers. So I tried disabling the d3d renderer using the -Dsun.java2d.d3d=falseflag on the JVM.  Wasn't able to reproduce the hang.

Of course, just disabling hardware acceleration is not the ideal solution for me, so I've been delving deeper into troubleshooting the issue.  The call stack above led me to learn about the -Dprism.verbose flag which gave me more information about what card was being used. (Internal Intel GPU rather than my external GPU)

I've since tried upgrading the drivers, and will look into how I might be able to switch prism from using the intel chipset to my dedicated GPU and see if that resolves the problem.  But at the very least, I do have the option to disable the d3d acceleration completely.


问:
Fixing Karger's min cut algorithm with union-find data structure
I was trying to implement Karger's min cut algorithm in the same way it is explained here but I don't like the fact that at each step of the while loop we can pick an edge with it's two endpoints already in a supernode. More specifically, this part

```<code>// If two corners belong to same subset,
// then no point considering this edge
       if (subset1 == subset2)
         continue;
</code>```

Is there a quick fix for avoiding this problem?

答:
It might help to back up and think about why there’s a union-find structure here at all and why it’s worth improving on the continue statement.

Conceptually, each contraction performed changes the graph in the following way:

The nodes contracted get replaced with a single node.

The edges incident to either node get replaced with an edge to the new joint node.

The edges running between the two earlier nodes get removed.

The question, then, is how to actually do this to the graph. The code you’ve found does this lazily. It doesn’t actually change the graph when the contraction is done. Instead, it uses the union-find structure to show which nodes are now equivalent to one another. When it samples a random edge, it then has to check whether that edge is one of the ones that would have been deleted in step (3). If so, it skips it and moves on. This has the effect that early contractions are really fast (the likelihood of picking two edges that are part of contracted nodes is very low when few edges are contracted), but later contractions might be a lot slower (once edges have started being contracted, lots of edges may have been deleted).

Here’s a simple modification you can use to speed this step up. Whenever you pick an edge to contract and find that its endpoints are already connected, discard that edge, and remove it from the list of edges so that it never gets picked again. You can do this by swapping that edge to the end of the list of edges, then removing the last element of the list. This has the effect that every edge processed will never be seen again, so across all iterations of the algorithm every edge will be processed at most once. That gives a runtime of one randomized contraction phase as O(m + nα(n)), where m is the number of edges and n is the number of nodes. The factor of α(n) comes from the use of the union-find structure.

If you truly want to remove all semblance of that continue statement, an alternative approach would be to directly simulate the contraction. After each contraction, iterate over all m edges and adjust each one by seeing whether it needs to remain unchanged, point to the new contracted node, or be removed altogether. This will take time O(m) per contraction for a net cost of O(mn) for the overall min cut calculation.

There are ways to speed things up beyond this. Karger’s original paper suggests generating a random permutation of the edges and using binary search over that array with a clever use of BFS or DFS to find the cut produced in time O(m), which is slightly faster than the O(m + nα(n)) approach for large graphs. The basic idea is the following:

Probe the middle element of the list of edges.

Run a BFS on the graph formed by only using those edges and see if there are exactly two connected components.

If so, great! Those two CCs are the ones you want.

If there is only one CC, discard the back half of the array of edges and try again.

If there is more than one CC, contract each CC into a single node and update a global table indicating which CC each node belongs to. Then discard the first half of the array and try again.

The cost of each BFS is O(m), where m is the number of edges in the graph, and this gives the recurrence T(m) = T(m/2) + O(m) because at each stage we’re throwing away half of the edges. That solves to O(m) total time, though as you can see, it’s a much trickier way of coding this algorithm up!

To summarize:

With a very small modification to the provided code, you can keep the continue statement in but still have a very fast implementation of the randomized contraction algorithm.

To eliminate that continue without sacrificing the runtime of the algorithm, you need to do some major surgery and change approaches to something only marginally asymptotically faster than keeping the continue in.

Hope this helps!

Body:
I was trying to implement Karger's min cut algorithm in the same way it is explained here but I don't like the fact that at each step of the while loop we can pick an edge with it's two endpoints already in a supernode. More specifically, this part

// If two corners belong to same subset,
// then no point considering this edge
       if (subset1 == subset2)
         continue;

Is there a quick fix for avoiding this problem?


问:
C#: how to handle floats properly
In my code i write the following line:

```<code>return ((value / 120) * 1440).ToString();
</code>```

Now this returns me 0 when the value in value is less than Zero because it treats the result of (value / 120) as an integer where as i need C# to treat this result as float is there any way to achieve this?

Example:
If Value = 80

Then,
This function returns me 0 whereas i want this function to return me 960

答:
return ((value / 120.0) * 1440).ToString();

or you can define value as float type.

Title: C#: how to handle floats properly

Body:
In my code i write the following line:

return ((value / 120) * 1440).ToString();

Now this returns me 0 when the value in value is less than Zero because it treats the result of (value / 120) as an integer where as i need C# to treat this result as float is there any way to achieve this?

Example:
If Value = 80

Then,
This function returns me 0 whereas i want this function to return me 960


问:
Combine two NumPy arrays into one structured array for appending to a PyTables table
I have two unstructured NumPy arrays a and b with shapes (N,) and (N, 256, 2) respectively and dtype np.float. I wish to combine these into a single structured array with shape (N,) and dtype [('field1', np.float), ('field2', np.float, (256, 2))].

The documentation on this is surprisingly lacking. I've found methods like np.lib.recfunctions.merge_arrays but have not been able to find the precise combination of features required to do this.

For the sake of avoiding the XY problem, I'll state my wider aims.

I have a PyTables table with layout {"field1": tables.FloatCol(), "field2": tables.FloatCol(shape = (256, 2))}. The two NumPy arrays represent N new rows to be appended to each of these fields. N is large, so I wish to do this with a single efficient table.append(rows) call, rather than the slow process of looping through table.row['field'] = ....

The table.append documentation says

The rows argument may be any object which can be converted to a structured array compliant with the table structure (otherwise, a ValueError is raised). This includes NumPy structured arrays, lists of tuples or array records, and a string or Python buffer.

Converting my arrays to an appropriate structured array seems to be what I should be doing here. I'm looking for speed, and I anticipate the other options being slower.

答:
This answer builds on @hpualj's answer. His first method creates the obj argument as a structured array and his second creates a record array. (This array would be the rows argument when you append.) I like both of these methods to create or append to tables when I already have my data in a structured (or record) array. However, you don't have to do this if your data is in separate arrays (as stated under "avoiding the X-Y problem'). As noted in the PyTables doc for table.append():

The rows argument may be any object which can be converted to a
  structured array compliant with the table structure.... This includes
  NumPy structured arrays, lists of tuples or array records...

In other words, you can append with lists referencing your arrays, so long they match the table structure created with description=dt in the example. (I think you are limited to structured arrays at creation.) This might simplify your code.

I wrote an example that builds on @hpaulj's code. It creates 2 identical HDF5 files with different methods.

For the first file (_1.h5) I create the table using the structured array method. I then add 3 rows of data to the table using table.append([list of arrays])

For the second file (_2.h5) I create the table referencing the
structured array dtype using description=dt, but do not add data with obj=arr. I then add the first 3 rows of data to the table using table.append([list of arrays]) and repeat to add 3 more rows.

Example below:

```<code>import numpy as np
import tables as tb

dt = np.dtype([('field1', np.float), ('field2', np.float, (4, 2))])
arr = np.zeros(3, dt)     # float display is prettier
arr['field1'] = np.arange(3)
arr['field2'] = np.arange(24).reshape(3,4,2)

with tb.File('SO_62104084_1.h5','w') as h5f1:
    test_tb = h5f1.create_table('/','test',obj=arr)
    arr1 = np.arange(13.,16.,1.)
    arr2 = np.arange(124.,148.,1.).reshape(3,4,2)
# add rows of data referencing list of arrays:
    test_tb.append([arr1,arr2])

with tb.File('SO_62104084_2.h5','w') as h5f2:
    test_tb=h5f2.create_table('/','test', description=dt)
    # add data rows 0-2:
    arr1 = np.arange(3)
    arr2 = np.arange(24).reshape(3,4,2)
    test_tb.append([arr1,arr2])
# add data rows 3-5:
    arr1 = np.arange(13.,16.,1.)
    arr2 = np.arange(124.,148.,1.).reshape(3,4,2)
    test_tb.append([arr1,arr2])
</code>```

Body:
This answer builds on @hpualj's answer. His first method creates the obj argument as a structured array and his second creates a record array. (This array would be the rows argument when you append.) I like both of these methods to create or append to tables when I already have my data in a structured (or record) array. However, you don't have to do this if your data is in separate arrays (as stated under "avoiding the X-Y problem'). As noted in the PyTables doc for table.append():

The rows argument may be any object which can be converted to a
  structured array compliant with the table structure.... This includes
  NumPy structured arrays, lists of tuples or array records...

In other words, you can append with lists referencing your arrays, so long they match the table structure created with description=dt in the example. (I think you are limited to structured arrays at creation.) This might simplify your code.

I wrote an example that builds on @hpaulj's code. It creates 2 identical HDF5 files with different methods.

For the first file (_1.h5) I create the table using the structured array method. I then add 3 rows of data to the table using table.append([list of arrays])

For the second file (_2.h5) I create the table referencing the
structured array dtype using description=dt, but do not add data with obj=arr. I then add the first 3 rows of data to the table using table.append([list of arrays]) and repeat to add 3 more rows.

Example below:

import numpy as np
import tables as tb

dt = np.dtype([('field1', np.float), ('field2', np.float, (4, 2))])
arr = np.zeros(3, dt)     # float display is prettier
arr['field1'] = np.arange(3)
arr['field2'] = np.arange(24).reshape(3,4,2)

with tb.File('SO_62104084_1.h5','w') as h5f1:
    test_tb = h5f1.create_table('/','test',obj=arr)
    arr1 = np.arange(13.,16.,1.)
    arr2 = np.arange(124.,148.,1.).reshape(3,4,2)
# add rows of data referencing list of arrays:
    test_tb.append([arr1,arr2])

with tb.File('SO_62104084_2.h5','w') as h5f2:
    test_tb=h5f2.create_table('/','test', description=dt)
    # add data rows 0-2:
    arr1 = np.arange(3)
    arr2 = np.arange(24).reshape(3,4,2)
    test_tb.append([arr1,arr2])
# add data rows 3-5:
    arr1 = np.arange(13.,16.,1.)
    arr2 = np.arange(124.,148.,1.).reshape(3,4,2)
    test_tb.append([arr1,arr2])


问:
Firebase: child of child extract value

how can I extract all the "name" values contained in DATA?

I tried this to extract a single name but it doesn't work:

```<code>DatabaseReference myRef = FirebaseDatabase.getInstance().getReference("ALL");
Query query = myRef.orderByChild("name").equalTo("pluto");
</code>```

答:
Thanks

I edited it so the log is working
Log.i("Firebase", snapshot.child("DATA/name").getValue(String.class));

If I want to print all the names, how should I do it?

Title: Firebase: child of child extract value

Body:
how can I extract all the "name" values contained in DATA?

I tried this to extract a single name but it doesn't work:

DatabaseReference myRef = FirebaseDatabase.getInstance().getReference("ALL");
Query query = myRef.orderByChild("name").equalTo("pluto");


问:
how do I replicate matplotlib normalisation functionality imshow
I am implementing neural style transfer. My objective is to create a timelapse of the training process. However, the generated image that the algorithm outputs falls out of the range [0, 1]. I tried subtracting the min and dividing by the max but the resulting image turns out whit-ish but I also noticed that when using Matplotlib to show the image the following warning come out:

Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).

and the resulting image comes fantastic, so what does it do exactly?

here is my code:
https://colab.research.google.com/drive/1AzGGpAtGi-kMIGl32y9U8PCg7ERh1cik?usp=sharing

答:
imshow() does just a linear transformation, mapping the range [vmin, vmax] to [0, 1].  It's a function like f(x) = (x - vmin) / (vmax - vmin). When vmin or vmax aren't provided, they are calculated as the minimum and maximum of the given data.

More elaborate mappings are also possible, as explained here.


问:
How to pass in a body of array in React axios and accept a body of array data in Nodejs server
I have a post request in the server that needs to accept an array of body to use InsertMany in mongoose.

The model is called order and is meant to store an array of products name per order. I currently have in the server a sample

```<code>const data = [
                {name: 'Phone'},
                {name: 'Guitar'},
             ]

let order = await Order.insertMany(data)
</code>```

So now, I'm looking to find how it'll be in the client with axios

答:
dont know if I got your question, but if the problem is in your axios request, just add it on your default axios package payload:</p>

```<code>import axios from 'axios';

let payload = {
  data = [
      {name: 'Phone'},
      {name: 'Guitar'},
  ]
};

axios({
  url: 'your_url',
  method: 'post',
  data: payload
})
.then(function (response) {
    // your action after success
    console.log(response);
})
.catch(function (error) {
   // your action on error success
    console.log(error);
});
</code>```

Body:
I have a post request in the server that needs to accept an array of body to use InsertMany in mongoose.

The model is called order and is meant to store an array of products name per order. I currently have in the server a sample

const data = [
                {name: 'Phone'},
                {name: 'Guitar'},
             ]

let order = await Order.insertMany(data)

So now, I'm looking to find how it'll be in the client with axios

Body:
dont know if I got your question, but if the problem is in your axios request, just add it on your default axios package payload:

import axios from 'axios';

let payload = {
  data = [
      {name: 'Phone'},
      {name: 'Guitar'},
  ]
};

axios({
  url: 'your_url',
  method: 'post',
  data: payload
})
.then(function (response) {
    // your action after success
    console.log(response);
})
.catch(function (error) {
   // your action on error success
    console.log(error);
});


问:
dynamics 365 API 9.1 associate contact to opportunity
We are using https://github.com/dangoscomb/pydynamics in Python to connect to our Dynamics 365 org.

We can create, select, update our entity sets.

```<code>import pydynamics
from pydynamics.client import Client
from pydynamics.querybuilder import QueryBuilder
token = tokenres.json()['access_token']
pdc = Client(token, endpoint=CREDENTIALS['resource'] + CREDENTIALS['api_base_url'] )

#======update contact (works fine) =========
q = QueryBuilder('contacts').guid('2bcd88c7-c295-ea11-a811-000d3a6546e3').\
    data({'firstname': 'ChangedFirstName'})
pdc.update(q)
</code>```

For associating entities we followed:
https://learn.microsoft.com/en-us/powerapps/developer/common-data-service/webapi/associate-disassociate-entities-using-web-api

```<code>POST [Organization URI]/api/data/v9.0/accounts(00000000-0000-0000-0000-000000000002)/opportunity_customer_accounts/$ref HTTP/1.1
Content-Type: application/json
Accept: application/json
OData-MaxVersion: 4.0
OData-Version: 4.0

{
"@odata.id":"[Organization URI]/api/data/v9.0/opportunities(00000000-0000-0000-0000-000000000001)"
}
</code>```

If we do it accordingly, we get errors that the json is invalid!

```<code>print(js)
{'@odata.id': '<ORG>/api/data/v9.1/opportunities(4585df90-4aa3-ea11-a812-000d3a6546e3)'}

import requests
query = 'contacts(ab906e8c-4aa3-ea11-a812-000d3a65473c)/opportunity_customer_contacts/$ref'
resp=requests.post(endpoint + query, headers=headers, data=js)
resp.json()
</code>```

```<code>{'error': {'code': '0x0',
  'message': 'An error occurred while validating input parameters: Microsoft.OData.ODataException: Invalid JSON. A token was not recognized in the JSON content.\r\n
</code>```

Might there be a problem with this "@odata.id" ?

答:
The solution was simple. Microsofts documentation is not very precise. They write

```<code>{
 "customerid_account@odata.bind":"[Organization URI]/api/data/v9.0/accounts(00000000-0000-0000-0000-000000000002)"
}
</code>```

It does NOT need the "[Organization URI]/api/data/v9.0" Part.

Solution:

```<code>q = QueryBuilder('opportunities').guid(opport_guid).\
    data({  "customerid_contact@odata.bind":  f"/contacts({contact_guid})"   })
DynamicsClient.update(q)
</code>```

Body:
We are using https://github.com/dangoscomb/pydynamics in Python to connect to our Dynamics 365 org.

We can create, select, update our entity sets.

import pydynamics
from pydynamics.client import Client
from pydynamics.querybuilder import QueryBuilder
token = tokenres.json()['access_token']
pdc = Client(token, endpoint=CREDENTIALS['resource'] + CREDENTIALS['api_base_url'] )

#======update contact (works fine) =========
q = QueryBuilder('contacts').guid('2bcd88c7-c295-ea11-a811-000d3a6546e3').\
    data({'firstname': 'ChangedFirstName'})
pdc.update(q)

For associating entities we followed:
https://learn.microsoft.com/en-us/powerapps/developer/common-data-service/webapi/associate-disassociate-entities-using-web-api

POST [Organization URI]/api/data/v9.0/accounts(00000000-0000-0000-0000-000000000002)/opportunity_customer_accounts/$ref HTTP/1.1
Content-Type: application/json
Accept: application/json
OData-MaxVersion: 4.0
OData-Version: 4.0

{
"@odata.id":"[Organization URI]/api/data/v9.0/opportunities(00000000-0000-0000-0000-000000000001)"
}

If we do it accordingly, we get errors that the json is invalid!

print(js)
{'@odata.id': '<ORG>/api/data/v9.1/opportunities(4585df90-4aa3-ea11-a812-000d3a6546e3)'}

import requests
query = 'contacts(ab906e8c-4aa3-ea11-a812-000d3a65473c)/opportunity_customer_contacts/$ref'
resp=requests.post(endpoint + query, headers=headers, data=js)
resp.json()

{'error': {'code': '0x0',
  'message': 'An error occurred while validating input parameters: Microsoft.OData.ODataException: Invalid JSON. A token was not recognized in the JSON content.\r\n

Might there be a problem with this "@odata.id" ?

Body:
The solution was simple. Microsofts documentation is not very precise. They write

{
 "customerid_account@odata.bind":"[Organization URI]/api/data/v9.0/accounts(00000000-0000-0000-0000-000000000002)"
}

It does NOT need the "[Organization URI]/api/data/v9.0" Part.

Solution:

q = QueryBuilder('opportunities').guid(opport_guid).\
    data({  "customerid_contact@odata.bind":  f"/contacts({contact_guid})"   })
DynamicsClient.update(q)


问:
jQuery $ function syntax
What is the difference between the following function definitions?

1:

$(function () {
    //stuff here
});
```

<p>2:</p>

<pre class="lang-js prettyprint-override"><code>function($){
    //stuff here
}
</code>```

答:
In #1, your function will be called by jQuery when the DOM is ready; passing a function into $() is a shortcut for $(document).ready(function() { ... }); (details here).

In #2, you're defining a function but neither calling it nor asking jQuery to call it. (And in fact, as shown, it's a syntax error — you'd need to be assigning it to something or calling it for it to be valid without a name.) Nothing you've quoted will execute the function, you'd have to call it yourself.

On #2, the idiom you've probably seen there is:

```<code>(function($) {
    // ...code using `$` for jQuery here...
})(jQuery);
</code>```

That's fairly standard thing you'd do when you want the code to run immediately (not wait for DOM ready), and you want to use $ within the function to refer to jQuery, but you want to be compatible with noConflict mode. You see this a lot in plug-ins. It defines a function accepting a $ argument, which shadows any $ defined globally, and then immediately calls that function passing in jQuery as the argument. So even if $ doesn't map to jQuery globally, it does within that function. You can do the same thing like this:

```<code>(function() {
    var $ = jQuery;
    // ...code using `$` for jQuery here...
})();
</code>```

...but for some reason the earlier example is more common, even if a bit less clear. (It's a couple of characters shorter, I suppose.)

Title: jQuery $ function syntax

Body:
What is the difference between the following function definitions?

1:

$(function () {
    //stuff here
});

2:

function($){
    //stuff here
}

Body:
In #1, your function will be called by jQuery when the DOM is ready; passing a function into $() is a shortcut for $(document).ready(function() { ... }); (details here).

In #2, you're defining a function but neither calling it nor asking jQuery to call it. (And in fact, as shown, it's a syntax error — you'd need to be assigning it to something or calling it for it to be valid without a name.) Nothing you've quoted will execute the function, you'd have to call it yourself.

On #2, the idiom you've probably seen there is:

(function($) {
    // ...code using `$` for jQuery here...
})(jQuery);

That's fairly standard thing you'd do when you want the code to run immediately (not wait for DOM ready), and you want to use $ within the function to refer to jQuery, but you want to be compatible with noConflict mode. You see this a lot in plug-ins. It defines a function accepting a $ argument, which shadows any $ defined globally, and then immediately calls that function passing in jQuery as the argument. So even if $ doesn't map to jQuery globally, it does within that function. You can do the same thing like this:

(function() {
    var $ = jQuery;
    // ...code using `$` for jQuery here...
})();

...but for some reason the earlier example is more common, even if a bit less clear. (It's a couple of characters shorter, I suppose.)


问:
Is there a way to add a column of type dictionary to a spark dataframe in pyspark?
This is how I create a dataframe with primitive data types in pyspark:

```<code>from pyspark.sql.types import StructType, StructField, DoubleType, StringType, IntegerType
fields = [StructField('column1', IntegerType(), True), StructField('column2', IntegerType(), True)]
schema = StructType(fields)
df = spark.createDataFrame([], schema)
values = [tuple([i]) +
          tuple([i])
          for i in range(3)]
df = spark.createDataFrame(values, schema)
</code>```

Now, if I want to have a third column with dictionary data, eg: {"1": 1.0, "2": 2.0, "3": 3.0}, what should I do?
I want to create this data frame:

```<code>+--------------------+-----------------+------------------------------+
|column1             |column2          |column3                       |
+--------------------+-----------------+------------------------------+
|1                   |1                |{"1": 1.0, "2": 1.0, "3": 1.0}|
+--------------------+-----------------+------------------------------+
|2                   |2                |{"1": 2.0, "2": 2.0, "3": 2.0}|
+--------------------+-----------------+------------------------------+
|3                   |3                |{"1": 3.0, "2": 3.0, "3": 3.0}|
+--------------------+-----------------+------------------------------+
</code>```

There is a MapType that seems to be helpful, but I can't figure out how to use it?

And assuming the data frame is created, how to filter it based on the third column, given a dict to select the rows of the data frame that have that dict value?

答:
Example how to create:

```<code>from pyspark.sql.types import MapType, IntegerType, DoubleType, StringType, StructType, StructField
import pyspark.sql.functions as f

schema = StructType([
            StructField('column1', IntegerType()),
            StructField('column2', IntegerType()),
            StructField('column3', MapType(StringType(), DoubleType()))])

data = [(1, 2, {'a':3.5, 'b':4.2}), (4, 8, {'b':3.7, 'e':4.9})]
df = spark.createDataFrame(data, schema=schema)
df.show()
</code>```

Output:

```<code>+-------+-------+--------------------+
|column1|column2|             column3|
+-------+-------+--------------------+
|      1|      2|[a -> 3.5, b -> 4.2]|
|      4|      8|[e -> 4.9, b -> 3.7]|
+-------+-------+--------------------+
</code>```

Example on how to filter DataFrame only leaving elements which have a certain key (assuming you don't have null values in the map and your Spark version is 2.4+ cause early versions don't have element_at):

```<code>filtered_df = df.where(f.element_at(df.column3, 'a').isNotNull())
</code>```

Output:

```<code>+-------+-------+--------------------+
|column1|column2|             column3|
+-------+-------+--------------------+
|      1|      2|[a -> 3.5, b -> 4.2]|
+-------+-------+--------------------+
</code>```

I might have misunderstood your question - if your intention is to only leave rows where map column equal to a specific dictionary you have it is a little bit more tricky. As far as I know Spark doesn't have comparison operation on dictionary types (it is somewhat unusual operation). There is a way to implement it using udf, which will be not very efficient. The code for that might look like this:

```<code>from pyspark.sql.types import MapType, IntegerType, DoubleType, StringType, StructType, StructField, BooleanType
my_dict = {'b':2.7, 'e':4.9}

from pyspark.sql.functions import udf
def map_equality_comparer(my_dict):
    @udf(BooleanType())
    def comparer(m):
        if len(m) != len(my_dict): return False
        for k, v in m.items():
            if my_dict.get(k) != v: return False
        return True
    return comparer

filtered_df = df.where(map_equality_comparer(my_dict)(df.column3))
filtered_df.show()
</code>```

If this is too slow for you you might consider creating a canonical representation of your Dictionaries and comparing those (e.g. converting dictionaries to sorted arrays of key value pairs and filtering based on equality of these arrays).

Body:
This is how I create a dataframe with primitive data types in pyspark:

from pyspark.sql.types import StructType, StructField, DoubleType, StringType, IntegerType
fields = [StructField('column1', IntegerType(), True), StructField('column2', IntegerType(), True)]
schema = StructType(fields)
df = spark.createDataFrame([], schema)
values = [tuple([i]) +
          tuple([i])
          for i in range(3)]
df = spark.createDataFrame(values, schema)

Now, if I want to have a third column with dictionary data, eg: {"1": 1.0, "2": 2.0, "3": 3.0}, what should I do?
I want to create this data frame:

+--------------------+-----------------+------------------------------+
|column1             |column2          |column3                       |
+--------------------+-----------------+------------------------------+
|1                   |1                |{"1": 1.0, "2": 1.0, "3": 1.0}|
+--------------------+-----------------+------------------------------+
|2                   |2                |{"1": 2.0, "2": 2.0, "3": 2.0}|
+--------------------+-----------------+------------------------------+
|3                   |3                |{"1": 3.0, "2": 3.0, "3": 3.0}|
+--------------------+-----------------+------------------------------+

There is a MapType that seems to be helpful, but I can't figure out how to use it?

And assuming the data frame is created, how to filter it based on the third column, given a dict to select the rows of the data frame that have that dict value?

Body:
Example how to create:

from pyspark.sql.types import MapType, IntegerType, DoubleType, StringType, StructType, StructField
import pyspark.sql.functions as f

schema = StructType([
            StructField('column1', IntegerType()),
            StructField('column2', IntegerType()),
            StructField('column3', MapType(StringType(), DoubleType()))])

data = [(1, 2, {'a':3.5, 'b':4.2}), (4, 8, {'b':3.7, 'e':4.9})]
df = spark.createDataFrame(data, schema=schema)
df.show()

Output:

+-------+-------+--------------------+
|column1|column2|             column3|
+-------+-------+--------------------+
|      1|      2|[a -> 3.5, b -> 4.2]|
|      4|      8|[e -> 4.9, b -> 3.7]|
+-------+-------+--------------------+

Example on how to filter DataFrame only leaving elements which have a certain key (assuming you don't have null values in the map and your Spark version is 2.4+ cause early versions don't have element_at):

filtered_df = df.where(f.element_at(df.column3, 'a').isNotNull())

Output:

+-------+-------+--------------------+
|column1|column2|             column3|
+-------+-------+--------------------+
|      1|      2|[a -> 3.5, b -> 4.2]|
+-------+-------+--------------------+

I might have misunderstood your question - if your intention is to only leave rows where map column equal to a specific dictionary you have it is a little bit more tricky. As far as I know Spark doesn't have comparison operation on dictionary types (it is somewhat unusual operation). There is a way to implement it using udf, which will be not very efficient. The code for that might look like this:

from pyspark.sql.types import MapType, IntegerType, DoubleType, StringType, StructType, StructField, BooleanType
my_dict = {'b':2.7, 'e':4.9}

from pyspark.sql.functions import udf
def map_equality_comparer(my_dict):
    @udf(BooleanType())
    def comparer(m):
        if len(m) != len(my_dict): return False
        for k, v in m.items():
            if my_dict.get(k) != v: return False
        return True
    return comparer

filtered_df = df.where(map_equality_comparer(my_dict)(df.column3))
filtered_df.show()

If this is too slow for you you might consider creating a canonical representation of your Dictionaries and comparing those (e.g. converting dictionaries to sorted arrays of key value pairs and filtering based on equality of these arrays).


问:
TfidfVectorizer in scikit-learn : ValueError: np.nan is an invalid document
I'm using TfidfVectorizer from scikit-learn to do some feature extraction from text data. I have a CSV file with a Score (can be +1 or -1) and a Review (text). I pulled this data into a DataFrame so I can run the Vectorizer.

This is my code:

```<code>import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer

df = pd.read_csv("train_new.csv",
             names = ['Score', 'Review'], sep=',')

# x = df['Review'] == np.nan
#
# print x.to_csv(path='FindNaN.csv', sep=',', na_rep = 'string', index=True)
#
# print df.isnull().values.any()

v = TfidfVectorizer(decode_error='replace', encoding='utf-8')
x = v.fit_transform(df['Review'])
</code>```

This is the traceback for the error I get:

```<code>Traceback (most recent call last):
  File "/home/PycharmProjects/Review/src/feature_extraction.py", line 16, in <module>
x = v.fit_transform(df['Review'])
 File "/home/b/hw1/local/lib/python2.7/site-   packages/sklearn/feature_extraction/text.py", line 1305, in fit_transform
   X = super(TfidfVectorizer, self).fit_transform(raw_documents)
 File "/home/b/work/local/lib/python2.7/site-packages/sklearn/feature_extraction/text.py", line 817, in fit_transform
self.fixed_vocabulary_)
 File "/home/b/work/local/lib/python2.7/site- packages/sklearn/feature_extraction/text.py", line 752, in _count_vocab
   for feature in analyze(doc):
 File "/home/b/work/local/lib/python2.7/site-packages/sklearn/feature_extraction/text.py", line 238, in <lambda>
tokenize(preprocess(self.decode(doc))), stop_words)
 File "/home/b/work/local/lib/python2.7/site-packages/sklearn/feature_extraction/text.py", line 118, in decode
 raise ValueError("np.nan is an invalid document, expected byte or "
 ValueError: np.nan is an invalid document, expected byte or unicode string.
</code>```

I checked the CSV file and DataFrame for anything that's being read as NaN but I can't find anything. There are 18000 rows, none of which return isnan as True.

This is what df['Review'].head() looks like:

```<code>  0    This book is such a life saver.  It has been s...
  1    I bought this a few times for my older son and...
  2    This is great for basics, but I wish the space...
  3    This book is perfect!  I'm a first time new mo...
  4    During your postpartum stay at the hospital th...
  Name: Review, dtype: object
</code>```

答:
I was getting MemoryError even after using .values.astype('U') for the reviews in my dataset.

So i tried .astype('U').values and it worked.

This is a answer from: Python: how to avoid MemoryError when transform text data into Unicode using astype('U')

Body:
I'm using TfidfVectorizer from scikit-learn to do some feature extraction from text data. I have a CSV file with a Score (can be +1 or -1) and a Review (text). I pulled this data into a DataFrame so I can run the Vectorizer.

This is my code:

import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer

df = pd.read_csv("train_new.csv",
             names = ['Score', 'Review'], sep=',')

# x = df['Review'] == np.nan
#
# print x.to_csv(path='FindNaN.csv', sep=',', na_rep = 'string', index=True)
#
# print df.isnull().values.any()

v = TfidfVectorizer(decode_error='replace', encoding='utf-8')
x = v.fit_transform(df['Review'])

This is the traceback for the error I get:

Traceback (most recent call last):
  File "/home/PycharmProjects/Review/src/feature_extraction.py", line 16, in <module>
x = v.fit_transform(df['Review'])
 File "/home/b/hw1/local/lib/python2.7/site-   packages/sklearn/feature_extraction/text.py", line 1305, in fit_transform
   X = super(TfidfVectorizer, self).fit_transform(raw_documents)
 File "/home/b/work/local/lib/python2.7/site-packages/sklearn/feature_extraction/text.py", line 817, in fit_transform
self.fixed_vocabulary_)
 File "/home/b/work/local/lib/python2.7/site- packages/sklearn/feature_extraction/text.py", line 752, in _count_vocab
   for feature in analyze(doc):
 File "/home/b/work/local/lib/python2.7/site-packages/sklearn/feature_extraction/text.py", line 238, in <lambda>
tokenize(preprocess(self.decode(doc))), stop_words)
 File "/home/b/work/local/lib/python2.7/site-packages/sklearn/feature_extraction/text.py", line 118, in decode
 raise ValueError("np.nan is an invalid document, expected byte or "
 ValueError: np.nan is an invalid document, expected byte or unicode string.

I checked the CSV file and DataFrame for anything that's being read as NaN but I can't find anything. There are 18000 rows, none of which return isnan as True.

This is what df['Review'].head() looks like:

  0    This book is such a life saver.  It has been s...
  1    I bought this a few times for my older son and...
  2    This is great for basics, but I wish the space...
  3    This book is perfect!  I'm a first time new mo...
  4    During your postpartum stay at the hospital th...
  Name: Review, dtype: object


问:
How to resolve `Raw use of parameterized class 'Comparable'` warning?
Got to implement the method below for an assignment which it's subject is "WildCards", but don't know where to use wildcards in order to resolve the warning.

```<code>static <T extends Comparable> T findMax(T ... items)
{
    T max = items[0];
    for (T item : items)
        if (item.compareTo(max) > 0)
            max = item;
    return max;
}
</code>```

Any ideas ?

答:
Comparable is a generic interface, so to use it safely you must always specify the generic type to use. In your case, something like:

<T extends Comparable<T>>
```

is likely what you're looking for. Otherwise, the compiler is unable to help you verify that the types are actually compatible in all scenarii.

Body:
Got to implement the method below for an assignment which it's subject is "WildCards", but don't know where to use wildcards in order to resolve the warning.

static <T extends Comparable> T findMax(T ... items)
{
    T max = items[0];
    for (T item : items)
        if (item.compareTo(max) > 0)
            max = item;
    return max;
}

Any ideas ?

Body:
Comparable is a generic interface, so to use it safely you must always specify the generic type to use. In your case, something like:

<T extends Comparable<T>>

is likely what you're looking for. Otherwise, the compiler is unable to help you verify that the types are actually compatible in all scenarii.


问:
the best way to display this jquery data
What could be the best way to display this jquery data. I know I can possibly do $('.Counter').html(count);

```<code><!doctype html>
  <html>
   <head>
    <title></title>
    <script src="jquery.min.js"></script>

<style>

.Counter { color: white;background: red; padding:6px; }
</style>
<script>

$(document).ready(function(){

alert('okay');
//var Counter = [];
$user = 'nancy';
count = 1;

$('#' + user + ' label.Counter').html(count);
$('#' + user + ' label.Counter').show();

});

</script>
  </head>
  <body>

<label class="Counter"></label>

  </body>
</html>
</code>```

答:
Here's a working example of your code. I've added display:none; to the CSS of the class Counter because otherwise the show() method you're using would be unnecessary.

$(document).ready(function() {

  alert('okay');
  $user = 'nancy';
  count = 1;

  $('#' + $user + ' label.Counter').html(count);
  $('#' + $user + ' label.Counter').show();

});```
<pre class="snippet-code-css lang-css prettyprint-override"><code>.Counter {
  color: white;
  background: red;
  padding: 6px;
  display:none;
}</code>```

```

Body:
What could be the best way to display this jquery data. I know I can possibly do $('.Counter').html(count);

<!doctype html>

    <title></title>

Body:
Here's a working example of your code. I've added display:none; to the CSS of the class Counter because otherwise the show() method you're using would be unnecessary.

$(document).ready(function() {

  alert('okay');
  $user = 'nancy';
  count = 1;

  $('#' + $user + ' label.Counter').html(count);
  $('#' + $user + ' label.Counter').show();

});

.Counter {
  color: white;
  background: red;
  padding: 6px;
  display:none;
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="nancy">
  <label class="Counter"></label>
</div>


问:
How can I create a text box for a note in markdown?
I am writing a document in markdown. I am using the wonderful pandoc to create docx and tex files from the markdown source. I would like to have a textbox for tips and notes to readers the way programming books often do. I cannot figure out how to do this in markdown. Can you help?</p>

答:
The following methods work on GitHub, on GitLab... and on Stackoverflow, which now uses CommonMark!

> One-Line Box made with Blockquote

One-Line Box made with Blockquote

`One-Line Box made with Backticks`

One-Line Box made with Backticks

```<br>
Box made with Triple Backticks<br>
```

```<code>Box made with Triple Backticks
</code>```

~ ~ ~

Box made with Triple Tildes (remove the spaces between the tildes to make this work)

~ ~ ~

```<code>Box made with Triple Tildes
</code>```

Box made with Four Spaces at the start of each line:

```<code>    “Sometimes we must let go of our pride and do what is requested of us.”
    Padmé Amidala

</code>```

... or use horizontal lines?

Three dashes (---) make a horizontal line:

Note: “ Your focus determines your reality.” – Qui-Gon Jinn.

For more configurations, I strongly advise the excellent GitLab Markdown Guide.

You can also check the less detailed GitHub basic formatting syntax.

You can compare Markdown implementations using Babelmark.

Useful hints :

to force a newline, put two spaces at the end of the line;

to escape special characters, use \.

Body:
The following methods work on GitHub, on GitLab... and on Stackoverflow, which now uses CommonMark!

> One-Line Box made with Blockquote

One-Line Box made with Blockquote

`One-Line Box made with Backticks`

One-Line Box made with Backticks

```<br>
Box made with Triple Backticks<br>
```

Box made with Triple Backticks

~ ~ ~

Box made with Triple Tildes (remove the spaces between the tildes to make this work)

~ ~ ~

Box made with Triple Tildes

Box made with Four Spaces at the start of each line:

    “Sometimes we must let go of our pride and do what is requested of us.”
    Padmé Amidala

... or use horizontal lines?

Three dashes (---) make a horizontal line:

Note: “ Your focus determines your reality.” – Qui-Gon Jinn.

For more configurations, I strongly advise the excellent GitLab Markdown Guide.

You can also check the less detailed GitHub basic formatting syntax.

You can compare Markdown implementations using Babelmark.

Useful hints :

to force a newline, put two spaces at the end of the line;

to escape special characters, use \.


问:
How Scalable is SQLite?
I recently read this Question about SQLite vs MySQL and the answer pointed out that SQLite doesn't scale well and the official website sort-of confirms this, however.

How scalable is SQLite and what are its upper most limits?

答:
Sqlite is scalable in terms of single-user, I have multi-gigabyte database that performs very well and I haven't had much problems with it.

But it is single-user, so it depends on what kind of scaling you're talking about.

In response to comments. Note that there is nothing that prevents using an Sqlite database in a multi-user environment, but every transaction (in effect, every SQL statement that modifies the database) takes a lock on the file, which will prevent other users from accessing the database at all.

So if you have lots of modifications done to the database, you're essentially going to hit scaling problems very quick. If, on the other hand, you have lots of read access compared to write access, it might not be so bad.

But Sqlite will of course function in a multi-user environment, but it won't perform well.

OwnerDisplayName: GateKiller

ContentLicense: CC BY-SA 2.5

Title: How Scalable is SQLite?

OwnerDisplayName: lassevk


问:
How do you make websites with Java?
This might be a really trivial question, but I've been writing Java programs at my school and I just found out that I can create websites with Java as well.

How can I do that? Any good books/tutorials for that?

Which is better for Web development, Java or PHP?

Also, when using PHP, MySQL comes into picture and while writing Java programs for desktop, we just use File I/O, so what is better for web dev, File I/O or MySQL?

答:
Read the tutorial on Java Web applications.

Basically Web applications are a part of the Java EE standard.  A lot of people only use the Web (servlets) part with additional frameworks thrown in, most notably Spring but also Struts, Seam and others.

All you need is an IDE like IntelliJ, Eclipse or Netbeans, the JDK, the Java EE download and a servlet container like Tomcat (or a full-blown application server like Glassfish or JBoss).

Here is a Tomcat tutorial.

OwnerDisplayName: GVRV

Title: How do you make websites with Java?

OwnerDisplayName: cletus


问:
How can access particular String from Resource file with using Resource.getString(id) in Android
I want to know that I have the string.xml Resource file which contains status messages. Now I want to process them according their id. I have written the following code but it fails on Message = res.getString(Msgid); and logs Resource not found Exception.

Can any one help me?

```<code>public void FileStatusMsg(int Msgid)

        {
                String Message = null;
                Resources res = this.getResources();

                Message = res.getString(Msgid);

                //Display Status Messages..
                AlertDialog.Builder builder = new AlertDialog.Builder(OfficeLinQ.this);
                builder.setMessage(Message).setCancelable(false).setPositiveButton(
                        "OK", new DialogInterface.OnClickListener()
                        {
                            public void onClick(DialogInterface dialog, int id)
                            {
                                dialog.cancel();
                            }
                        });
                AlertDialog alert = builder.create();
                alert.show();
            }
</code>```

答:
you can access it by using this method : </p>

```<code>YourActivity.this.getResources().getString(R.string.myString);
</code>```

Body:
I want to know that I have the string.xml Resource file which contains status messages. Now I want to process them according their id. I have written the following code but it fails on Message = res.getString(Msgid); and logs Resource not found Exception.

Can any one help me?

public void FileStatusMsg(int Msgid)

        {
                String Message = null;
                Resources res = this.getResources();

                Message = res.getString(Msgid);

                //Display Status Messages..
                AlertDialog.Builder builder = new AlertDialog.Builder(OfficeLinQ.this);
                builder.setMessage(Message).setCancelable(false).setPositiveButton(
                        "OK", new DialogInterface.OnClickListener()
                        {
                            public void onClick(DialogInterface dialog, int id)
                            {
                                dialog.cancel();
                            }
                        });
                AlertDialog alert = builder.create();
                alert.show();
            }

Body:
you can access it by using this method :

YourActivity.this.getResources().getString(R.string.myString);


问:
How to create a N-dimentional feature vector in python
I have 20 pixel values for an image, and i would like to store them in a 20D feature vector, not a 20 length feature vector. I'm new to Python, so i don't know if in Python a regular array is considered a n-dimentional vector, or i need to vectorize a single array somehow. These are the pixels value:

```<code>[245, 247, 199, 199, 210, 213, 216, 196, 225,
    199, 189, 189, 195, 221, 225, 201, 221, 201, 216, 222]
</code>```

I wish to turn them into a 20d dimentional vector, how would i proceed to do that?

答:
If I understand you correctly, the shape of your solution array should be (20,1). To do this, np.reshape is the quickest and easiest way. The code to do this below:</p>

```<code>np.reshape(your_array,(20,1))
</code>```

Body:
I have 20 pixel values for an image, and i would like to store them in a 20D feature vector, not a 20 length feature vector. I'm new to Python, so i don't know if in Python a regular array is considered a n-dimentional vector, or i need to vectorize a single array somehow. These are the pixels value:

[245, 247, 199, 199, 210, 213, 216, 196, 225,
    199, 189, 189, 195, 221, 225, 201, 221, 201, 216, 222]

I wish to turn them into a 20d dimentional vector, how would i proceed to do that?

Body:
If I understand you correctly, the shape of your solution array should be (20,1). To do this, np.reshape is the quickest and easiest way. The code to do this below:

np.reshape(your_array,(20,1))


问:
Set time zone in PHP for Pakistan
I am writing a code for time. I write the following code but want to set my default time zone is Pakistan +5.

```<code><?php
date_default_timezone_set("America/New_York");
echo "The time is " . date("h:i:sa");
?>
</code>```

How can I do this?

答:
Use this coding

```<code><?php
date_default_timezone_set("Asia/Karachi");
echo "The time is " . date('h:i:s a');
?>
</code>```

You can get more information about timezone in Asia at:  https://www.php.net/manual/en/timezones.asia.php

Title: Set time zone in PHP for Pakistan

Body:
I am writing a code for time. I write the following code but want to set my default time zone is Pakistan +5.

<?php
date_default_timezone_set("America/New_York");
echo "The time is " . date("h:i:sa");
?>

How can I do this?

Body:
Use this coding

<?php
date_default_timezone_set("Asia/Karachi");
echo "The time is " . date('h:i:s a');
?>

You can get more information about timezone in Asia at:  https://www.php.net/manual/en/timezones.asia.php


问:
Installing libxml2 and libxslt in python 3.6
I am having some problems installing lxml 3.6.4 in python 3.6 64bit using Pycharm and PIP.

```<code>pip install lxml==3.6.4

ERROR: b"'xslt-config' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n"
** make sure the development packages of libxml2 and libxslt are installed **
</code>```

When installing libxml

pip install libxml2-dev

I get the following error:

```<code>Collecting libxml2-dev
  Could not find a version that satisfies the requirement libxml2-dev (from versions: )
No matching distribution found for libxml2-dev
</code>```

When running libxslt

pip install libxslt

I get this error

```<code>Collecting libxslt
  Could not find a version that satisfies the requirement libxslt (from versions: )
No matching distribution found for libxslt
</code>```

The ultimate goal is to install pyiso on Pycharm.

My specs - Windows 7 - 64
Python 3.6

答:
On Linux, with the typo corrected, @pguardiario's comment above fixed this for me.

```<code>sudo apt-get install libxml2-dev libxslt-dev
</code>```

Then:

```<code>conda install lxml
</code>```

Or:

```<code>pip install lxml
</code>```

Body:
I am having some problems installing lxml 3.6.4 in python 3.6 64bit using Pycharm and PIP.

pip install lxml==3.6.4

ERROR: b"'xslt-config' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n"
** make sure the development packages of libxml2 and libxslt are installed **

When installing libxml

pip install libxml2-dev

I get the following error:

Collecting libxml2-dev
  Could not find a version that satisfies the requirement libxml2-dev (from versions: )
No matching distribution found for libxml2-dev

When running libxslt

pip install libxslt

I get this error

Collecting libxslt
  Could not find a version that satisfies the requirement libxslt (from versions: )
No matching distribution found for libxslt

The ultimate goal is to install pyiso on Pycharm.

My specs - Windows 7 - 64
Python 3.6

Body:
On Linux, with the typo corrected, @pguardiario's comment above fixed this for me.

sudo apt-get install libxml2-dev libxslt-dev

Then:

conda install lxml

Or:

pip install lxml


问:
Issue running query in Google Collab + Bigquery
I've followed the step by step here and inserted this snippet:

https://colab.research.google.com/notebook#snippetFileIds=%2Fv2%2Fexternal%2Fnotebooks%2Fsnippets%2Fbigquery.ipynb&snippetQuery=Using%20BigQuery%20with%20Pandas%20API

however, i can run the query, but then appears an error :

TypeError                                 Traceback (most recent call last)
<ipython-input-22-b9e37aa67e26> in <module>()
      9     COUNT(*) as total
     10   FROM `bigquery-public-data.samples.gsod`
---> 11 ''', project_id=project_id).total[0]
     12
     13 df = pd.io.gbq.read_gbq(f'''

8 frames
/usr/local/lib/python3.6/dist-packages/pyarrow/table.pxi in pyarrow.lib.RecordBatch.from_arrays()

TypeError: from_arrays() takes at least 2 positional arguments (1 given)```

I have tried with several database, with no success.

Any Idea?

答:
I fixed this issue by updating to the latest version of arrow
!pip install pyarrow==0.17.1

Body:
I've followed the step by step here and inserted this snippet:

https://colab.research.google.com/notebook#snippetFileIds=%2Fv2%2Fexternal%2Fnotebooks%2Fsnippets%2Fbigquery.ipynb&snippetQuery=Using%20BigQuery%20with%20Pandas%20API

however, i can run the query, but then appears an error :

TypeError                                 Traceback (most recent call last)
<ipython-input-22-b9e37aa67e26> in <module>()
      9     COUNT(*) as total
     10   FROM `bigquery-public-data.samples.gsod`
---> 11 ''', project_id=project_id).total[0]
     12
     13 df = pd.io.gbq.read_gbq(f'''

8 frames
/usr/local/lib/python3.6/dist-packages/pyarrow/table.pxi in pyarrow.lib.RecordBatch.from_arrays()

TypeError: from_arrays() takes at least 2 positional arguments (1 given)

I have tried with several database, with no success.

Any Idea?