问:
R webscraping setting keywords
I am web scraping in R from the WFP of the UN website in order to build up a dataset including the updates on food security (http://www.fao.org/countryprofiles/en/).
The packages are: rvest, stringr, tidyr, data.table, plyr, xml2, jsonlite.
Yet, I have imported into my dataset Country names, URL of the articles, Title of the articles and text of the articles.
Now, for the sake of parsing more easily, I would set up a new variable including some keyword I am interested in (e.g. "food security", "drought", etc.) and note the occurrencies of the aforementioned keywords.
That variable would simplify enormously my research.
Any suggestion?

Here'a the basic code I am using.

```<code>## 01. Creating a function in order to scrape data from a website (in this case, FAO's)

WFP_get_news <- function(countries) {                                                      GET(
url = "http://www1.wfp.org/countries/common/allnews/en/",
query = list(countries=countries)
 ) -> res

 warn_for_status(res)

 if (status_code(res) > 399) return(NULL)

 out <- content(res, as="text", encoding="UTF-8")
 out <- jsonlite::fromJSON(out)
 out$countries <- countries

 tbl_df(out)
 }

## 02. Setting all the Country urls in order for them to be automatically scraped

pb <- progress_estimated(length(countrycode_data$countries[]))
map_df(countrycode_data$countries[], ~{
pb$tick()$print()
Sys.sleep(5)
wfp_get_news(.x)
}) -> xdf

## 03. Setting keywords (of course, this process is arbitrary: one can    decide any keywor s/he prefers)
keywords <- c("drought", "food security")

keyword_regex <- sprintf("(%s)", paste0(keywords, collapse="|"))

## 04. Setting the keywords search
bind_cols(
 xdf,
 stri_match_all_regex(tolower(xdf$bodytext), keyword_regex) %>%
 map(~.x[,2]) %>%
 map_df(~{
 res <- table(.x, useNA="always")
 nm <- names(res)
 nm <- ifelse(is.na(nm), "NONE", stri_replace_all_regex(nm, "[ -]", "_"))
 as.list(set_names(as.numeric(res), nm))
 })
 ) %>%
 select(-NONE) -> xdf_with_keyword_counts
</code>```

The result I get from point 04. is

```<code>Error in overscope_eval_next(overscope, expr) :
 object "NONE" not found
Furthermore: Warning message:
Unknown or uninitialised column: 'data.frame'.
</code>```

Any clue?

答:
First, a general rule to keep is if you cut/paste > 2-3x it's time for a function.

We'll need some help:

```<code>library(countrycode)
library(httr)
library(rvest)
library(stringi)
library(tidyverse)
</code>```

Here's a function that you can call with an iso3 code to get the data [frame] you want:

```<code>fao_get_news <- function(iso3) {

  GET(
    url = "http://www.fao.org/countryprofiles/common/allnews/en/",
    query = list(iso3=iso3)
  ) -> res

  warn_for_status(res)

  if (status_code(res) > 399) return(NULL)

  out <- content(res, as="text", encoding="UTF-8")
  out <- jsonlite::fromJSON(out)
  out$iso3 <- iso3

  tbl_df(out)

}
</code>```

Now, we iterate over all possile iso3 codes using a helper data frame from the countrycode package.

NOTE: remove the [1:20] in the next 2 lines for production to get all  I don't need all the data, so I'm not burning time/bandwidth. If you care abt ethics at all, not getting banned nor hammering the server(s) of a non-profit organization then keep the Sys.sleep(5) in there. If you're selfish, then still keep it in there b/c your needs aren't > the FAO's.

```<code>pb <- progress_estimated(length(countrycode_data$iso3c[1:20]))
map_df(countrycode_data$iso3c[1:20], ~{
  pb$tick()$print()
  Sys.sleep(5) # no crawl delay specified in site robots.txt so this is the ethical default
  fao_get_news(.x)
}) -> xdf
</code>```

That gives us a nice, tidy data frame with all the news (with an iso3 field):

```<code>glimpse(xdf)
## Observations: 736
## Variables: 10
## $ uid         <chr> "1069933", "1069560", "1045264", "1044139", "103833...
## $ table       <chr> "news", "news", "news", "news", "news", "news", "ne...
## $ title       <chr> "FAO Calls for Stronger Collaboration on Transbound...
## $ date        <chr> "1511823600", "1511737200", "1508191200", "15081048...
## $ bodytext    <chr> "28 November 2017- Chief Veterinary Officers and ex...
## $ date_format <chr> "28/11/2017", "27/11/2017", "17/10/2017", "16/10/20...
## $ image       <chr> "http://www.fao.org/fileadmin/user_upload/rne/img/I...
## $ pid         <chr> "50840", "16275", "70992", "16275", "2330", "40990"...
## $ detail_pid  <chr> "/neareast/news/view/en/c/1069933/", "/asiapacific/...
## $ iso3        <chr> "AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "A...
</code>```

Put all your keywords (lower case) here:

```<code>keywords <- c("drought", "food security")
</code>```

We'll build a regex out of them

```<code>keyword_regex <- sprintf("(%s)", paste0(keywords, collapse="|"))
</code>```

Now, we add keyword columns to our existing data frame. The outline of that the stri_match…++ is doing is that for each news blob:

lower case the news blob

find all occurrences of each keyword (if any) in a news blob

capture a count of the # of occurrences along with the keyword

(lightly) ensure the keyword can be a valid column name

make a new column for each found keyword

blobs without any keywords will just have an NA for the found count value

We could have made the keyword results data frame count() and spread() but it is is not a fast operation. Using table() means a bit more typing but much faster results.

```<code>bind_cols(
  xdf,
  stri_match_all_regex(tolower(xdf$bodytext), keyword_regex) %>%
    map(~.x[,2]) %>%
    map_df(~{
      res <- table(.x, useNA="always")
      nm <- names(res)
      nm <- ifelse(is.na(nm), "NONE", stri_replace_all_regex(nm, "[ -]", "_"))
      as.list(set_names(as.numeric(res), nm))
    })
) %>%
  select(-NONE) -> xdf_with_keyword_counts
</code>```

Here's the result:

```<code>glimpse(xdf_with_keyword_counts)
## Observations: 736
## Variables: 12
## $ uid           <chr> "1069933", "1069560", "1045264", "1044139", "1038339", "...
## $ table         <chr> "news", "news", "news", "news", "news", "news", "news", ...
## $ title         <chr> "FAO Calls for Stronger Collaboration on Transboundary A...
## $ date          <chr> "1511823600", "1511737200", "1508191200", "1508104800", ...
## $ bodytext      <chr> "28 November 2017- Chief Veterinary Officers and experts...
## $ date_format   <chr> "28/11/2017", "27/11/2017", "17/10/2017", "16/10/2017", ...
## $ image         <chr> "http://www.fao.org/fileadmin/user_upload/rne/img/IMG_64...
## $ pid           <chr> "50840", "16275", "70992", "16275", "2330", "40990", "40...
## $ detail_pid    <chr> "/neareast/news/view/en/c/1069933/", "/asiapacific/news/...
## $ iso3          <chr> "AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "ALA", ...
## $ food_security <int> NA, NA, 2, 1, NA, 1, NA, NA, NA, 1, NA, NA, NA, NA, 1, N...
## $ drought       <int> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, ...
</code>```

Title: R webscraping setting keywords

Body:
I am web scraping in R from the WFP of the UN website in order to build up a dataset including the updates on food security (http://www.fao.org/countryprofiles/en/).
The packages are: rvest, stringr, tidyr, data.table, plyr, xml2, jsonlite.
Yet, I have imported into my dataset Country names, URL of the articles, Title of the articles and text of the articles.
Now, for the sake of parsing more easily, I would set up a new variable including some keyword I am interested in (e.g. "food security", "drought", etc.) and note the occurrencies of the aforementioned keywords.
That variable would simplify enormously my research.
Any suggestion?

Here'a the basic code I am using.

## 01. Creating a function in order to scrape data from a website (in this case, FAO's)

WFP_get_news <- function(countries) {                                                      GET(
url = "http://www1.wfp.org/countries/common/allnews/en/",
query = list(countries=countries)
 ) -> res

 warn_for_status(res)

 if (status_code(res) > 399) return(NULL)

 out <- content(res, as="text", encoding="UTF-8")
 out <- jsonlite::fromJSON(out)
 out$countries <- countries

 tbl_df(out)
 }

## 02. Setting all the Country urls in order for them to be automatically scraped

pb <- progress_estimated(length(countrycode_data$countries[]))
map_df(countrycode_data$countries[], ~{
pb$tick()$print()
Sys.sleep(5)
wfp_get_news(.x)
}) -> xdf

## 03. Setting keywords (of course, this process is arbitrary: one can    decide any keywor s/he prefers)
keywords <- c("drought", "food security")

keyword_regex <- sprintf("(%s)", paste0(keywords, collapse="|"))

## 04. Setting the keywords search
bind_cols(
 xdf,
 stri_match_all_regex(tolower(xdf$bodytext), keyword_regex) %>%
 map(~.x[,2]) %>%
 map_df(~{
 res <- table(.x, useNA="always")
 nm <- names(res)
 nm <- ifelse(is.na(nm), "NONE", stri_replace_all_regex(nm, "[ -]", "_"))
 as.list(set_names(as.numeric(res), nm))
 })
 ) %>%
 select(-NONE) -> xdf_with_keyword_counts

The result I get from point 04. is

Error in overscope_eval_next(overscope, expr) :
 object "NONE" not found
Furthermore: Warning message:
Unknown or uninitialised column: 'data.frame'.

Any clue?

Body:
First, a general rule to keep is if you cut/paste > 2-3x it's time for a function.

We'll need some help:

library(countrycode)
library(httr)
library(rvest)
library(stringi)
library(tidyverse)

Here's a function that you can call with an iso3 code to get the data [frame] you want:

fao_get_news <- function(iso3) {

  GET(
    url = "http://www.fao.org/countryprofiles/common/allnews/en/",
    query = list(iso3=iso3)
  ) -> res

  warn_for_status(res)

  if (status_code(res) > 399) return(NULL)

  out <- content(res, as="text", encoding="UTF-8")
  out <- jsonlite::fromJSON(out)
  out$iso3 <- iso3

  tbl_df(out)

}

Now, we iterate over all possile iso3 codes using a helper data frame from the countrycode package.

NOTE: remove the [1:20] in the next 2 lines for production to get all  I don't need all the data, so I'm not burning time/bandwidth. If you care abt ethics at all, not getting banned nor hammering the server(s) of a non-profit organization then keep the Sys.sleep(5) in there. If you're selfish, then still keep it in there b/c your needs aren't > the FAO's.

pb <- progress_estimated(length(countrycode_data$iso3c[1:20]))
map_df(countrycode_data$iso3c[1:20], ~{
  pb$tick()$print()
  Sys.sleep(5) # no crawl delay specified in site robots.txt so this is the ethical default
  fao_get_news(.x)
}) -> xdf

That gives us a nice, tidy data frame with all the news (with an iso3 field):

glimpse(xdf)
## Observations: 736
## Variables: 10
## $ uid         <chr> "1069933", "1069560", "1045264", "1044139", "103833...
## $ table       <chr> "news", "news", "news", "news", "news", "news", "ne...
## $ title       <chr> "FAO Calls for Stronger Collaboration on Transbound...
## $ date        <chr> "1511823600", "1511737200", "1508191200", "15081048...
## $ bodytext    <chr> "28 November 2017- Chief Veterinary Officers and ex...
## $ date_format <chr> "28/11/2017", "27/11/2017", "17/10/2017", "16/10/20...
## $ image       <chr> "http://www.fao.org/fileadmin/user_upload/rne/img/I...
## $ pid         <chr> "50840", "16275", "70992", "16275", "2330", "40990"...
## $ detail_pid  <chr> "/neareast/news/view/en/c/1069933/", "/asiapacific/...
## $ iso3        <chr> "AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "A...

Put all your keywords (lower case) here:

keywords <- c("drought", "food security")

We'll build a regex out of them

keyword_regex <- sprintf("(%s)", paste0(keywords, collapse="|"))

Now, we add keyword columns to our existing data frame. The outline of that the stri_match…++ is doing is that for each news blob:

lower case the news blob

find all occurrences of each keyword (if any) in a news blob

capture a count of the # of occurrences along with the keyword

(lightly) ensure the keyword can be a valid column name

make a new column for each found keyword

blobs without any keywords will just have an NA for the found count value

We could have made the keyword results data frame count() and spread() but it is is not a fast operation. Using table() means a bit more typing but much faster results.

bind_cols(
  xdf,
  stri_match_all_regex(tolower(xdf$bodytext), keyword_regex) %>%
    map(~.x[,2]) %>%
    map_df(~{
      res <- table(.x, useNA="always")
      nm <- names(res)
      nm <- ifelse(is.na(nm), "NONE", stri_replace_all_regex(nm, "[ -]", "_"))
      as.list(set_names(as.numeric(res), nm))
    })
) %>%
  select(-NONE) -> xdf_with_keyword_counts

Here's the result:

glimpse(xdf_with_keyword_counts)
## Observations: 736
## Variables: 12
## $ uid           <chr> "1069933", "1069560", "1045264", "1044139", "1038339", "...
## $ table         <chr> "news", "news", "news", "news", "news", "news", "news", ...
## $ title         <chr> "FAO Calls for Stronger Collaboration on Transboundary A...
## $ date          <chr> "1511823600", "1511737200", "1508191200", "1508104800", ...
## $ bodytext      <chr> "28 November 2017- Chief Veterinary Officers and experts...
## $ date_format   <chr> "28/11/2017", "27/11/2017", "17/10/2017", "16/10/2017", ...
## $ image         <chr> "http://www.fao.org/fileadmin/user_upload/rne/img/IMG_64...
## $ pid           <chr> "50840", "16275", "70992", "16275", "2330", "40990", "40...
## $ detail_pid    <chr> "/neareast/news/view/en/c/1069933/", "/asiapacific/news/...
## $ iso3          <chr> "AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "AFG", "ALA", ...
## $ food_security <int> NA, NA, 2, 1, NA, 1, NA, NA, NA, 1, NA, NA, NA, NA, 1, N...
## $ drought       <int> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, ...


来源: stackexchange

问:
Hive query for row number
I am working on pyspark, need to write a query which reads data from hive table and returns a pyspark dataframe containing all the columns and row number.

This is what I tried :

```<code>SELECT *, ROW_NUMBER() OVER () as rcd_num FROM schema_name.table_name
</code>```

This query works fine in hive, but when I run it from a pyspark script it throws the following error:

```<code>Window function row_number() requires window to be ordered, please add ORDER BY clause. For example SELECT row_number()(value_expr) OVER (PARTITION BY window_partition ORDER BY window_ordering) from table;
</code>```

Please suggest some solution.

Note: I do not wish to order the rows in any particular order, I just need row numbers for all the rows present in the table without any sorting or ordering.

Using spark 2.1

答:
ROW_NUMBER()might be required ordering so you can used monotonicallyIncreasingId function which gives you row numbers for all the rows present in the table.

```<code>from pyspark.sql.functions import monotonicallyIncreasingId

df.withColumn("rcd_num ", monotonicallyIncreasingId())
</code>```

OR

SELECT *, ROW_NUMBER() OVER (Order by (select NULL)) as rcd_num FROM schema_name.table_name

you can set order by select NULL

ContentLicense: CC BY-SA 3.0

Title: Hive query for row number

Body:
I am working on pyspark, need to write a query which reads data from hive table and returns a pyspark dataframe containing all the columns and row number.

This is what I tried :

SELECT *, ROW_NUMBER() OVER () as rcd_num FROM schema_name.table_name

This query works fine in hive, but when I run it from a pyspark script it throws the following error:

Window function row_number() requires window to be ordered, please add ORDER BY clause. For example SELECT row_number()(value_expr) OVER (PARTITION BY window_partition ORDER BY window_ordering) from table;

Please suggest some solution.

Note: I do not wish to order the rows in any particular order, I just need row numbers for all the rows present in the table without any sorting or ordering.

Using spark 2.1

Body:
ROW_NUMBER()might be required ordering so you can used monotonicallyIncreasingId function which gives you row numbers for all the rows present in the table.

from pyspark.sql.functions import monotonicallyIncreasingId

df.withColumn("rcd_num ", monotonicallyIncreasingId())

OR

SELECT *, ROW_NUMBER() OVER (Order by (select NULL)) as rcd_num FROM schema_name.table_name

you can set order by select NULL


问:
AWS : Elastic Beanstalk single profile with multiple applications
I am trying to figure out how can i set up two or more applications in single repository.

Lets say: I wanted to keep separate Eb Applications for Production(P) and Development(D) with one environment each(P > env-p & D > env-d). So how can i config my repository that master branch points to P > env-p and development branch points to D > env-d.

Note: I have setup Elastic Beanstalk in single AWS account with different applications. I have tried searching but could not anything related to this.

If any body have done this please share your views.

Thanks in advance.

答:
If I understand you correctly you want to run an application with two different environments, for two different branches.

You can do that with a config file in:
.elasticbeanstalk/config.yml

```<code>branch-defaults:
  master:
    environment: master
    default_region: ap-southeast-2
    default_ec2_keyname: master_key
    profile: master_profile
  staging:
    environment: staging
  develop:
    environment: develop
global:
  application_name: my_application
  default_ec2_keyname: develop_key
  default_platform: Python 2.7
  default_region: ap-southeast-1
  profile: develop_profile
  sc: git
</code>```

It will create this file when you do eb init for each branch, but I have seen it sometimes needs some manual edit.

Here is another answer for a similar question with more discussion.

AWS Elastic Beanstalk deploy to multiple applications

Related documentation:

http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/branch.html

http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/environment-configuration-methods-before.html

http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/command-reference-branch-environment.html

ContentLicense: CC BY-SA 4.0

Body:
If I understand you correctly you want to run an application with two different environments, for two different branches.

You can do that with a config file in:
.elasticbeanstalk/config.yml

branch-defaults:
  master:
    environment: master
    default_region: ap-southeast-2
    default_ec2_keyname: master_key
    profile: master_profile
  staging:
    environment: staging
  develop:
    environment: develop
global:
  application_name: my_application
  default_ec2_keyname: develop_key
  default_platform: Python 2.7
  default_region: ap-southeast-1
  profile: develop_profile
  sc: git

It will create this file when you do eb init for each branch, but I have seen it sometimes needs some manual edit.

Here is another answer for a similar question with more discussion.

AWS Elastic Beanstalk deploy to multiple applications

Related documentation:

http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/branch.html

http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/environment-configuration-methods-before.html

http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/command-reference-branch-environment.html


问:
Setting up subscription on Microsoft Graph give authentication error
I am trying to setup a subscription using Microsoft Graph but I keep getting the following error:

```<code>{
    "error": {
        "code": "InvalidAuthenticationToken",
        "message": "CompactToken validation failed with reason code: 80049228.",
        "innerError": {
            "request-id": "18c0ce19-cedc-4184-a46f-319e1a350901",
            "date": "2017-12-05T15:59:06"
        }
    }
}
</code>```

This is the code that I'm using to do it:

HttpWebRequest qboApiRequest = (HttpWebRequest) WebRequest.Create("https://graph.microsoft.com/v1.0/subscriptions");
qboApiRequest.Method = "POST";
qboApiRequest.Headers.Add(string.Format("Authorization: Bearer {0}", token));
qboApiRequest.ContentType = "application/json;charset=UTF-8";
qboApiRequest.Accept = "*/*";

string postData = @"{{""changeType"": ""created,updated"", ""notificationUrl"": ""http://www.somesite.com/response.ashx"", ""resource"": ""me/events"", ""expirationDateTime"": ""2017-12-20T18:23:45.9356913Z"", ""clientState"": ""subscription-identifier""}}";
var encoding = new UTF8Encoding();
var bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(postData);
qboApiRequest.ContentLength = bytes.Length;
using(var writeStream = qboApiRequest.GetRequestStream())
{
    writeStream.Write(bytes, 0, bytes.Length);
}

// get the response
string responseText;
HttpWebResponse qboApiResponse = (HttpWebResponse) qboApiRequest.GetResponse();
using(var qboApiReader = new StreamReader(qboApiResponse.GetResponseStream()))
responseText = qboApiReader.ReadToEnd();
```

I know that the token is okay because it works okay in other stuff so I'm at a loss as to why I'm getting the error

答:
What do you means that it work fine in other stuff?
Access to Microsoft Graph need to subscribe a webhook you must have calendard.read.
Try checking the token using jwt.io. How you register the app. I used app level auth registering it with adminconsent. And use this peace of code to generate the token: </p>

```<code> public static async Task<GenericToken> GetAppToken(string Dom)
        {
            const string Scope = "https://graph.microsoft.com/.default";
            string request_content = $"grant_type=client_credentials&scope={Scope}&client_id={ClientId}&client_secret={ClientSecret}";
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, $"https://login.microsoftonline.com/{Dom}/oauth2/v2.0/token");
            request.Content = new StringContent(request_content, Encoding.UTF8, "application/x-www-form-urlencoded");
            //HttpClient client = new HttpClient();
            HttpResponseMessage response = await staticClient.SendAsync(request);
            return  await response.Content.ReadAsAsync<GenericToken>();

        }
</code>```

Body:
I am trying to setup a subscription using Microsoft Graph but I keep getting the following error:

{
    "error": {
        "code": "InvalidAuthenticationToken",
        "message": "CompactToken validation failed with reason code: 80049228.",
        "innerError": {
            "request-id": "18c0ce19-cedc-4184-a46f-319e1a350901",
            "date": "2017-12-05T15:59:06"
        }
    }
}

This is the code that I'm using to do it:

HttpWebRequest qboApiRequest = (HttpWebRequest) WebRequest.Create("https://graph.microsoft.com/v1.0/subscriptions");
qboApiRequest.Method = "POST";
qboApiRequest.Headers.Add(string.Format("Authorization: Bearer {0}", token));
qboApiRequest.ContentType = "application/json;charset=UTF-8";
qboApiRequest.Accept = "*/*";

string postData = @"{{""changeType"": ""created,updated"", ""notificationUrl"": ""http://www.somesite.com/response.ashx"", ""resource"": ""me/events"", ""expirationDateTime"": ""2017-12-20T18:23:45.9356913Z"", ""clientState"": ""subscription-identifier""}}";
var encoding = new UTF8Encoding();
var bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(postData);
qboApiRequest.ContentLength = bytes.Length;
using(var writeStream = qboApiRequest.GetRequestStream())
{
    writeStream.Write(bytes, 0, bytes.Length);
}

// get the response
string responseText;
HttpWebResponse qboApiResponse = (HttpWebResponse) qboApiRequest.GetResponse();
using(var qboApiReader = new StreamReader(qboApiResponse.GetResponseStream()))
responseText = qboApiReader.ReadToEnd();

I know that the token is okay because it works okay in other stuff so I'm at a loss as to why I'm getting the error

Body:
What do you means that it work fine in other stuff?
Access to Microsoft Graph need to subscribe a webhook you must have calendard.read.
Try checking the token using jwt.io. How you register the app. I used app level auth registering it with adminconsent. And use this peace of code to generate the token:

 public static async Task<GenericToken> GetAppToken(string Dom)
        {
            const string Scope = "https://graph.microsoft.com/.default";
            string request_content = $"grant_type=client_credentials&scope={Scope}&client_id={ClientId}&client_secret={ClientSecret}";
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, $"https://login.microsoftonline.com/{Dom}/oauth2/v2.0/token");
            request.Content = new StringContent(request_content, Encoding.UTF8, "application/x-www-form-urlencoded");
            //HttpClient client = new HttpClient();
            HttpResponseMessage response = await staticClient.SendAsync(request);
            return  await response.Content.ReadAsAsync<GenericToken>();

        }


问:
PHP: white space between function name and parenthesis
I'm wondering if this is legal:

```<code><?php

is_null
(
    $var
);
</code>```

Or perhaps this:

```<code><?php
$items = array
(
    "Details" => array('controller' => 'event', 'action' => 'readall', 'tab' => 'details'),
    "Calendar" => array('controller' => 'event', 'action' => 'readall', 'tab' => 'calendar')
);
</code>```

Or will some interpreters choke?

I know the popular mixed style of classes and functions have the opening curly brace on a new line and control structures, such as "if", on the same line. However, I like all one or the other. Newline or no newline. It would be nice to extend the concept to function calls and their parenthesis as well. This is the reason for my question.

答: Whitespace doesn't matter. The only time I'm aware that the interpreter cares about whitespace is when exiting HEREDOC syntax. Other than that, you can have as many spaces or newlines as you want.</p>

ContentLicense: CC BY-SA 2.5

Body:
I'm wondering if this is legal:

<?php

is_null
(
    $var
);

Or perhaps this:

<?php
$items = array
(
    "Details" => array('controller' => 'event', 'action' => 'readall', 'tab' => 'details'),
    "Calendar" => array('controller' => 'event', 'action' => 'readall', 'tab' => 'calendar')
);

Or will some interpreters choke?

I know the popular mixed style of classes and functions have the opening curly brace on a new line and control structures, such as "if", on the same line. However, I like all one or the other. Newline or no newline. It would be nice to extend the concept to function calls and their parenthesis as well. This is the reason for my question.


问:
How do I use ScrollEffect on a ListView to prevent overscrolling?
I am using the same way to prevwnt overscrolling of a TextView like that:

```<code>ScrollView:
    id: scrlv
    size_hint: (1, 1)
    effect_cls: ScrollEffect #avoid overscrolling
    TextInput:
        id: ro_log
        size_hint: (1, None)
        height: max(self.minimum_height, scrlv.height) #required to enable scrolling when list starts to grow
        readonly: True
        background_color: 0,0,0,0
        foreground_color: 1,1,1,1
</code>```

But for a ListView, which contains a ScrollView, the kv spec below has no effect:

```<code>ListView:
    id: commandListView
    effect_cls: ScrollEffect #no effect !
    adapter: ListAdapter(data=[], cls=main.CommandListButton, args_converter=lambda row_index,rec: {'text':rec, 'on_press':root.commandSelected})
    height: '0dp'
    size_hint_y: None
</code>```

答:
Here's the solution, as SP SP gave it in his comment above:

```<code>ListView:
    id: commandListView
    adapter: ListAdapter(data=[], cls=main.CommandListButton, args_converter=lambda row_index,rec: {'text':rec, 'on_press':root.historyItemSelected})
    height: '0dp'
    size_hint_y: None
    on_parent: self.container.parent.effect_cls = ScrollEffect #prevents overscrolling
</code>```

This requires of course

```<code>#: import ScrollEffect kivy.effects.scroll.ScrollEffect
</code>```

on top of the kv file.

Body:
I am using the same way to prevwnt overscrolling of a TextView like that:

ScrollView:
    id: scrlv
    size_hint: (1, 1)
    effect_cls: ScrollEffect #avoid overscrolling
    TextInput:
        id: ro_log
        size_hint: (1, None)
        height: max(self.minimum_height, scrlv.height) #required to enable scrolling when list starts to grow
        readonly: True
        background_color: 0,0,0,0
        foreground_color: 1,1,1,1

But for a ListView, which contains a ScrollView, the kv spec below has no effect:

ListView:
    id: commandListView
    effect_cls: ScrollEffect #no effect !
    adapter: ListAdapter(data=[], cls=main.CommandListButton, args_converter=lambda row_index,rec: {'text':rec, 'on_press':root.commandSelected})
    height: '0dp'
    size_hint_y: None

Body:
Here's the solution, as SP SP gave it in his comment above:

ListView:
    id: commandListView
    adapter: ListAdapter(data=[], cls=main.CommandListButton, args_converter=lambda row_index,rec: {'text':rec, 'on_press':root.historyItemSelected})
    height: '0dp'
    size_hint_y: None
    on_parent: self.container.parent.effect_cls = ScrollEffect #prevents overscrolling

This requires of course

#: import ScrollEffect kivy.effects.scroll.ScrollEffect

on top of the kv file.


问:
Corrupt Graphics on Pixel XL and Xiaomi Mi 6
I have a minimal sample project that produces corrupt graphics on a Google Pixel XL and a Xiaomi Mi6, but works correctly on a Samsung Galaxy S6.

Setting android:hardwareAccelerated=false fixes the corrupt graphics but leads to issues with animations and scrolling leaving trails.

The sample project uses MvvmCross, however I have been able to reproduce the behaviour without MvvmCross.

The project code can be found at https://github.com/stevedcc/Xamarin_Broken_Graphics.git

View:

```<code><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:local="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
<ImageView
    android:id="@+id/mainLogo"
    android:src="@drawable/company_logo_1"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:adjustViewBounds="true" />
<ImageView
    android:id="@+id/main_image"
    android:layout_width="wrap_content"
    android:layout_height="350dp"
    android:layout_below="@drawable/main_image1"
    android:adjustViewBounds="true"
    android:src="?attr/main_image"/>
<TextView
    android:id="@+id/mainScanText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/main_image"
    android:gravity="center"
    android:textSize="24sp"
    android:text="Title" />
</RelativeLayout>
</code>```

Styles:

```<code><?xml version="1.0" encoding="utf-8" ?>
<resources>
  <attr name="company_logo" format="reference" />
  <attr name="main_image" format="reference"/>
  <attr name="company_color" format="reference" />
  <attr name="splash_background" fromat="reference" />
  <color name="company_color_1">#7f7f7f</color>
  <style name="CompanyTheme"
          parent="@android:style/Theme.Material.NoActionBar">
    <item name="company_logo">@drawable/company_logo_1</item>
    <item name="main_image">@drawable/main_image1</item>
    <item name="android:colorBackground">#000000</item>
    <item name="company_color">@color/company_color_1</item>
  </style>
</resources>
</code>```

Expected Result as displayed by a Galaxy S6

Actual Result as displayed by a Pixel XL

Can anyone tell me how to fix the Corrupt graphics?

答:
I found the answer myself:

```<code><item name="android:colorBackground">#000000</item>
</code>```

leads to the issue, if replaced by

```<code><item name="android:colorBackground">@android:color/black</item>
</code>```

then the project works.

Body:
I have a minimal sample project that produces corrupt graphics on a Google Pixel XL and a Xiaomi Mi6, but works correctly on a Samsung Galaxy S6.

Setting android:hardwareAccelerated=false fixes the corrupt graphics but leads to issues with animations and scrolling leaving trails.

The sample project uses MvvmCross, however I have been able to reproduce the behaviour without MvvmCross.

The project code can be found at https://github.com/stevedcc/Xamarin_Broken_Graphics.git

View:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:local="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
<ImageView
    android:id="@+id/mainLogo"
    android:src="@drawable/company_logo_1"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:adjustViewBounds="true" />
<ImageView
    android:id="@+id/main_image"
    android:layout_width="wrap_content"
    android:layout_height="350dp"
    android:layout_below="@drawable/main_image1"
    android:adjustViewBounds="true"
    android:src="?attr/main_image"/>
<TextView
    android:id="@+id/mainScanText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/main_image"
    android:gravity="center"
    android:textSize="24sp"
    android:text="Title" />
</RelativeLayout>

Styles:

<?xml version="1.0" encoding="utf-8" ?>
<resources>
  <attr name="company_logo" format="reference" />
  <attr name="main_image" format="reference"/>
  <attr name="company_color" format="reference" />
  <attr name="splash_background" fromat="reference" />
  <color name="company_color_1">#7f7f7f</color>

</resources>

Expected Result as displayed by a Galaxy S6

Actual Result as displayed by a Pixel XL

Can anyone tell me how to fix the Corrupt graphics?

Body:
I found the answer myself:

<item name="android:colorBackground">#000000</item>

leads to the issue, if replaced by

<item name="android:colorBackground">@android:color/black</item>

then the project works.


问:
gsub not working while implementing in a loop
here i have the following dataframe df in R.

```<code>kyid    industry    amount
112     Apparel     345436
234     APPEARELS   234567
213     apparels    345678
345     Airlines    235678
123     IT          456789
124     IT          897685
</code>```

i want to replace in industry which incorrectly written Apparel, or APPEARLS to Apparels .

i tried using creating a list and run it through a loop.

```<code>l<-c('Apparel ','APPEARELS','apparels')

for(i in range(1:3)){
    df$industry<-gsub(pattern=l[i],"Apparels",df$industry)
}
</code>```

it is not working.only one element changes.

But, when i take the statement individually it is not creating an error and its working.

```<code>df$industry<-gsub(pattern=","Apparels",df$industry)
</code>```

but this is a large dataset so i nned this to work in R please help.

答:
sub without loop using | :

```<code>l <- c("Apparel" , "APPEARELS", "apparels")
# Using OPs data
sub(paste(l, collapse = "|"), "Apparels", df$industry)
# [1] "Apparels" "Apparels" "Apparels" "Airlines" "IT" "IT"
</code>```

I'm using sub instead of gsub as there's only one occurrence of pattern in a string (at least in example).

Body:
here i have the following dataframe df in R.

kyid    industry    amount
112     Apparel     345436
234     APPEARELS   234567
213     apparels    345678
345     Airlines    235678
123     IT          456789
124     IT          897685

i want to replace in industry which incorrectly written Apparel, or APPEARLS to Apparels .

i tried using creating a list and run it through a loop.

l<-c('Apparel ','APPEARELS','apparels')

for(i in range(1:3)){
    df$industry<-gsub(pattern=l[i],"Apparels",df$industry)
}

it is not working.only one element changes.

But, when i take the statement individually it is not creating an error and its working.

df$industry<-gsub(pattern=","Apparels",df$industry)

but this is a large dataset so i nned this to work in R please help.

Body:
sub without loop using | :

l <- c("Apparel" , "APPEARELS", "apparels")
# Using OPs data
sub(paste(l, collapse = "|"), "Apparels", df$industry)
# [1] "Apparels" "Apparels" "Apparels" "Airlines" "IT" "IT"

I'm using sub instead of gsub as there's only one occurrence of pattern in a string (at least in example).


问:
Return a 0 when no data present
I have looked at a few questions similar and tried to apply my own knowledge to get the query to work correctly but still cant get the outcome i desire.
I have 5 Call lines i need to report on from our DB. The problem is some of them can go a full day without a call. Instead of reporting 0, it just doesn't pull the line through. Unfortunately for Reporting purposes I need it to put 0.

```<code>SELECT CASE [Campaign]
WHEN '1' THEN 'Line A'
WHEN '2' THEN 'Line B'
WHEN '3' THEN 'Line C'
WHEN '4' THEN 'Line D'
WHEN '5' THEN 'Line E'
ELSE 'Outbound Dialling' END AS [Line_name],
COUNT(UniqueID) AS [Total Calls]
FROM Telephone_Table
WHERE [CallTime] BETWEEN '2017-12-01 00:00:00' AND '2017-12-01 23:59:59'
AND [Campaign] IN ('402','403','404','405','406')
</code>```

Result:

```<code>Line_name   | Campaign  |  Total Calls
Line A      |  402      |       13
Line B      |  403      |       11
Line C      |  405      |       26
Line E      |  406      |       178
</code>```

Quite often Line D doesn't get a call. Instead of listing it as 0 it will miss it completely.

I have tried the following but none have worked:
COUNT(UniqueID)+1

CASE WHEN COUNT(UniqueID) = '0' THEN 1 ELSE COUNT(UniqueID)

I tried creating another table in the DB with just these lines and a name in there to join against instead of using a "Case When" in case that was the issue but that didn't work.

Any one have any suggestions? Thank you.

答:
Here's a suggestion: add a dummy for each group you want displayed, then subtract 1 from the count:

```<code>SELECT CASE [Campaign]
    WHEN '402' THEN 'Line A'
    WHEN '403' THEN 'Line B'
    WHEN '404' THEN 'Line C'
    WHEN '405' THEN 'Line D'
    WHEN '406' THEN 'Line E'
    ELSE 'Outbound Dialling' END AS [Line_name]
    ,[Campaign]
    ,COUNT(UniqueID)-1 AS [Total Calls]
FROM
    (SELECT [Campaign], [UniqueID] FROM Telephone_Table
    WHERE [CallTime] BETWEEN '2017-12-01 00:00:00' AND '2017-12-01 23:59:59'
        AND [Campaign] IN ('402','403','404','405','406')
    UNION SELECT '402' AS [Campaign], 'DUMMY1' AS UniqueID
    UNION SELECT '403' AS [Campaign], 'DUMMY2' AS UniqueID
    UNION SELECT '404' AS [Campaign], 'DUMMY3' AS UniqueID
    UNION SELECT '405' AS [Campaign], 'DUMMY4' AS UniqueID
    UNION SELECT '406' AS [Campaign], 'DUMMY5' AS UniqueID) AS a
GROUP BY [Campaign]
</code>```

I may have mixed up my syntax, I don't have sql-server handy at the moment to check, but this general idea should work.

Title: Return a 0 when no data present

Body:
I have looked at a few questions similar and tried to apply my own knowledge to get the query to work correctly but still cant get the outcome i desire.
I have 5 Call lines i need to report on from our DB. The problem is some of them can go a full day without a call. Instead of reporting 0, it just doesn't pull the line through. Unfortunately for Reporting purposes I need it to put 0.

SELECT CASE [Campaign]
WHEN '1' THEN 'Line A'
WHEN '2' THEN 'Line B'
WHEN '3' THEN 'Line C'
WHEN '4' THEN 'Line D'
WHEN '5' THEN 'Line E'
ELSE 'Outbound Dialling' END AS [Line_name],
COUNT(UniqueID) AS [Total Calls]
FROM Telephone_Table
WHERE [CallTime] BETWEEN '2017-12-01 00:00:00' AND '2017-12-01 23:59:59'
AND [Campaign] IN ('402','403','404','405','406')

Result:

Line_name   | Campaign  |  Total Calls
Line A      |  402      |       13
Line B      |  403      |       11
Line C      |  405      |       26
Line E      |  406      |       178

Quite often Line D doesn't get a call. Instead of listing it as 0 it will miss it completely.

I have tried the following but none have worked:
COUNT(UniqueID)+1

CASE WHEN COUNT(UniqueID) = '0' THEN 1 ELSE COUNT(UniqueID)

I tried creating another table in the DB with just these lines and a name in there to join against instead of using a "Case When" in case that was the issue but that didn't work.

Any one have any suggestions? Thank you.

Body:
Here's a suggestion: add a dummy for each group you want displayed, then subtract 1 from the count:

SELECT CASE [Campaign]
    WHEN '402' THEN 'Line A'
    WHEN '403' THEN 'Line B'
    WHEN '404' THEN 'Line C'
    WHEN '405' THEN 'Line D'
    WHEN '406' THEN 'Line E'
    ELSE 'Outbound Dialling' END AS [Line_name]
    ,[Campaign]
    ,COUNT(UniqueID)-1 AS [Total Calls]
FROM
    (SELECT [Campaign], [UniqueID] FROM Telephone_Table
    WHERE [CallTime] BETWEEN '2017-12-01 00:00:00' AND '2017-12-01 23:59:59'
        AND [Campaign] IN ('402','403','404','405','406')
    UNION SELECT '402' AS [Campaign], 'DUMMY1' AS UniqueID
    UNION SELECT '403' AS [Campaign], 'DUMMY2' AS UniqueID
    UNION SELECT '404' AS [Campaign], 'DUMMY3' AS UniqueID
    UNION SELECT '405' AS [Campaign], 'DUMMY4' AS UniqueID
    UNION SELECT '406' AS [Campaign], 'DUMMY5' AS UniqueID) AS a
GROUP BY [Campaign]

I may have mixed up my syntax, I don't have sql-server handy at the moment to check, but this general idea should work.


问:
Whats the easiest way to determine if a user is online? (PHP/MYSQL)
Is there a way I can piggy back sessions to know if the user is online?

I.e: use logs on, I set a $_SESSION variable, user times out- cookie Garbage collector updates the database to update their status as offline.

EDIT: I want a solution that does not involve times or dates.  I want something to ride on sessions or something similar.  Guessing if someone is online is not good enough for what I need.

答:
Here's a way on how to get the difference between two dates from a database in minutes, then check for the difference and set the online/offline status. </p>

```<code>$query = 'SELECT * FROM Users';
$result = mysqli_query($mysqli, $query);

foreach($result as $user){
    // date from the database
    $dbLastActivity = date("d-m-Y h:i:s a", strtotime($user['lastOnline']));
    // date now
    $now = date("d-m-Y h:i:s a", $date);

    // calculate the difference
    $difference = strtotime($now) - strtotime($dbLastActivity);
    $difference_in_minutes = $difference / 60;

    // check if difference is greater than five minutes
    if($difference_in_minutes < 5){
        // set online status
        $updateStatus = 'UPDATE Users SET Status="online" WHERE lastOnline="'.$user['lastOnline'].'"';
    } else {
        // set offline status
        $updateStatus = 'UPDATE Users SET Status="offline" WHERE lastOnline="'.$user['lastOnline'].'"';
    }

    // check if mysqli query was successful
    if (!$mysqli->query($updateStatus)){
        printf("Error Message %s\n", $mysqli->error);
    }
}
</code>```

Body:
Here's a way on how to get the difference between two dates from a database in minutes, then check for the difference and set the online/offline status.

$query = 'SELECT * FROM Users';
$result = mysqli_query($mysqli, $query);

foreach($result as $user){
    // date from the database
    $dbLastActivity = date("d-m-Y h:i:s a", strtotime($user['lastOnline']));
    // date now
    $now = date("d-m-Y h:i:s a", $date);

    // calculate the difference
    $difference = strtotime($now) - strtotime($dbLastActivity);
    $difference_in_minutes = $difference / 60;

    // check if difference is greater than five minutes
    if($difference_in_minutes < 5){
        // set online status
        $updateStatus = 'UPDATE Users SET Status="online" WHERE lastOnline="'.$user['lastOnline'].'"';
    } else {
        // set offline status
        $updateStatus = 'UPDATE Users SET Status="offline" WHERE lastOnline="'.$user['lastOnline'].'"';
    }

    // check if mysqli query was successful
    if (!$mysqli->query($updateStatus)){
        printf("Error Message %s\n", $mysqli->error);
    }
}


问:
redirecting some but not all domains to https
I'm trying to convince mod-rewrite to redirect http://example.com to https://example.com but not redirect http://subdomain.example.com

I've added the following to the .htaccess file in the sites root,

```<code>RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</code>```

and that (as I thought it would) redirects everything, so then I tried

```<code>RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^http://example(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}
</code>```

but that also appears to redirect everything?

答:
RewriteRule only matches on the part of the URL after the host and port (in VirtualHost context) or on the relative filesystem path (in Directory/htaccess context); so trying to match a hostname in a RewriteRule will not work.

However %{HTTP:Host} will give you the Host HTTP header, so that RewriteCond can match against it:

```<code>RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:Host} =example.com
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R]
</code>```

The drawback is that mod_rewrite will notice that you rewrite conditionally depending on an HTTP header, and will add a Vary: Host header. If you don't want that, you may store it to a variable first, and do the RewriteCond on that variable:

```<code>RewriteRule . - [E=HTTP_HOST_NO_VARY:%{HTTP:Host}]

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{ENV:HTTP_HOST_NO_VARY} =example.com
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R]
</code>```

Body:
I'm trying to convince mod-rewrite to redirect http://example.com to https://example.com but not redirect http://subdomain.example.com

I've added the following to the .htaccess file in the sites root,

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

and that (as I thought it would) redirects everything, so then I tried

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^http://example(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}

but that also appears to redirect everything?

Body:
RewriteRule only matches on the part of the URL after the host and port (in VirtualHost context) or on the relative filesystem path (in Directory/htaccess context); so trying to match a hostname in a RewriteRule will not work.

However %{HTTP:Host} will give you the Host HTTP header, so that RewriteCond can match against it:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:Host} =example.com
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R]

The drawback is that mod_rewrite will notice that you rewrite conditionally depending on an HTTP header, and will add a Vary: Host header. If you don't want that, you may store it to a variable first, and do the RewriteCond on that variable:

RewriteRule . - [E=HTTP_HOST_NO_VARY:%{HTTP:Host}]

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{ENV:HTTP_HOST_NO_VARY} =example.com
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R]


问:
How do I remove a duplicate from a SELECT DISTINCT over multiple columns?
I have the following select statement:

```<code>SELECT DISTINCT a.month,
                a.date
           FROM a
       ORDER BY a.bar DESC NULLS LAST;
</code>```

month is a char, date is a date. I need to use the date column because that's the thing i'm ordering the results by. I'm getting this result:

```<code>foo | bar

 x---|---20-nov-15 00:00:00
 y---|---31-oct-15 00:00:00
 y---|---30-oct-15 00:00:00
 z---|---28-sept-15 00:00:00
</code>```

I am in need of a WHERE statement that would eliminate that duplicate Y from the result, I've been looking everywhere and couldn't find a solution

答:
Use group by to solve this issue</p>

```<code>SELECT a.month, a.date
FROM a
GROUP BY a.month
ORDER BY a.bar DESC NULLS LAST;
</code>```

Body:
I have the following select statement:

SELECT DISTINCT a.month,
                a.date
           FROM a
       ORDER BY a.bar DESC NULLS LAST;

month is a char, date is a date. I need to use the date column because that's the thing i'm ordering the results by. I'm getting this result:

foo | bar

 x---|---20-nov-15 00:00:00
 y---|---31-oct-15 00:00:00
 y---|---30-oct-15 00:00:00
 z---|---28-sept-15 00:00:00

I am in need of a WHERE statement that would eliminate that duplicate Y from the result, I've been looking everywhere and couldn't find a solution

Body:
Use group by to solve this issue

SELECT a.month, a.date
FROM a
GROUP BY a.month
ORDER BY a.bar DESC NULLS LAST;


问:
Python deep learning with theano the basic program of mnist
I am a newbie to the deep learning, and I am just doing my baby steps in it. I have a MacBook Pro 2017 model, and I am trying to run the MNIST dataset with Logistic Regression. When I run the sample code which is mentioned in the official deep learning website, I am receiving the following error..

```<code><i>Downloading data from http://www.iro.umontreal.ca/~lisa/deep/data/mnist/mnist.pkl.gz
Traceback (most recent call last):
File "/Users/abi/PycharmProjects/LogisticRgr_test/logist_test.py", line 475, in <module>
sgd_optimization_mnist()
File "/Users/abi/PycharmProjects/LogisticRgr_test/logist_test.py", line 277, in sgd_optimization_mnist
datasets = load_data(dataset)
File "/Users/abi/PycharmProjects/LogisticRgr_test/logist_test.py", line 205, in load_data
urllib.request.urlretrieve(origin, dataset)
File "/Users/abi/miniconda2/lib/python2.7/urllib.py", line 98, in urlretrieve
return opener.retrieve(url, filename, reporthook, data)
File "/Users/abi/miniconda2/lib/python2.7/urllib.py", line 249, in retrieve
tfp = open(filename, 'wb')
IOError: [Errno 2] No such file or directory: '/Users/abi/PycharmProjects/LogisticRgr_test/../data/mnist.pkl.gz'

Process finished with exit code 1</i>
</code>```

答:
As mentioned in file logistic_sgd.py http://deeplearning.net/tutorial/code/logistic_sgd.py

first you can download this file manually on http://www.iro.umontreal.ca/~lisa/deep/data/mnist/mnist.pkl.gz
an add file 'mnist.pkl.gz' to your current Directory, because the function checks first, if the mnist file is in the data Directory. If you do this, so check if installed numpy and everything works fine.

```<code>def load_data(dataset):
''' Loads the dataset

:type dataset: string
:param dataset: the path to the dataset (here MNIST)
'''

#############
# LOAD DATA #
#############

# Download the MNIST dataset if it is not present
data_dir, data_file = os.path.split(dataset)
if data_dir == "" and not os.path.isfile(dataset):
    # Check if dataset is in the data directory.
    new_path = os.path.join(
        os.path.split(__file__)[0],
        "..",
        "data",
        dataset
    )
    if os.path.isfile(new_path) or data_file == 'mnist.pkl.gz':
        dataset = new_path

if (not os.path.isfile(dataset)) and data_file == 'mnist.pkl.gz':
    from six.moves import urllib
    origin = (
        'http://www.iro.umontreal.ca/~lisa/deep/data/mnist/mnist.pkl.gz'
    )
    print('Downloading data from %s' % origin)
    urllib.request.urlretrieve(origin, dataset)

print('... loading data')

# Load the dataset
with gzip.open(dataset, 'rb') as f:
    try:
        train_set, valid_set, test_set = pickle.load(f, encoding='latin1')
    except:
        train_set, valid_set, test_set = pickle.load(f)
# train_set, valid_set, test_set format: tuple(input, target)
# input is a numpy.ndarray of 2 dimensions (a matrix)
# where each row corresponds to an example. target is a
# numpy.ndarray of 1 dimension (vector) that has the same length as
# the number of rows in the input. It should give the target
# to the example with the same index in the input.
</code>```

the line with
urllib.request.urlretrieve(origin, dataset) did not work for me either, but you just have to download the file so i did some changes

```<code>from urllib import request

def get(url):
    with request.urlopen(url) as r:
       return r.read()

def download(url, file=None):
    if not file:
        file = url.split('/')[-1]
    with open(file, 'wb') as f:
        f.write(get(url))
</code>```

and call the download function

```<code>url = "http://www.iro.umontreal.ca/~lisa/deep/data/mnist/mnist.pkl.gz"
download(url)
</code>```

After these changes, the function load_data works fine for me

I did some more Investigation:

Replace this statement:

```<code>urllib.request.urlretrieve(origin, dataset)
</code>```

with the following Statement:

```<code>dataset, header = urllib.request.urlretrieve(origin, 'mnist.pkl.gz')
</code>```

you get a tuple back from urlretrieve and with dataset from the tuple you have to work with in the gzip.open statement!

Body:
I am a newbie to the deep learning, and I am just doing my baby steps in it. I have a MacBook Pro 2017 model, and I am trying to run the MNIST dataset with Logistic Regression. When I run the sample code which is mentioned in the official deep learning website, I am receiving the following error..

Downloading data from http://www.iro.umontreal.ca/~lisa/deep/data/mnist/mnist.pkl.gz
Traceback (most recent call last):
File "/Users/abi/PycharmProjects/LogisticRgr_test/logist_test.py", line 475, in <module>
sgd_optimization_mnist()
File "/Users/abi/PycharmProjects/LogisticRgr_test/logist_test.py", line 277, in sgd_optimization_mnist
datasets = load_data(dataset)
File "/Users/abi/PycharmProjects/LogisticRgr_test/logist_test.py", line 205, in load_data
urllib.request.urlretrieve(origin, dataset)
File "/Users/abi/miniconda2/lib/python2.7/urllib.py", line 98, in urlretrieve
return opener.retrieve(url, filename, reporthook, data)
File "/Users/abi/miniconda2/lib/python2.7/urllib.py", line 249, in retrieve
tfp = open(filename, 'wb')
IOError: [Errno 2] No such file or directory: '/Users/abi/PycharmProjects/LogisticRgr_test/../data/mnist.pkl.gz'

Process finished with exit code 1

Body:
As mentioned in file logistic_sgd.py http://deeplearning.net/tutorial/code/logistic_sgd.py

first you can download this file manually on http://www.iro.umontreal.ca/~lisa/deep/data/mnist/mnist.pkl.gz
an add file 'mnist.pkl.gz' to your current Directory, because the function checks first, if the mnist file is in the data Directory. If you do this, so check if installed numpy and everything works fine.

def load_data(dataset):
''' Loads the dataset

:type dataset: string
:param dataset: the path to the dataset (here MNIST)
'''

#############
# LOAD DATA #
#############

# Download the MNIST dataset if it is not present
data_dir, data_file = os.path.split(dataset)
if data_dir == "" and not os.path.isfile(dataset):
    # Check if dataset is in the data directory.
    new_path = os.path.join(
        os.path.split(__file__)[0],
        "..",
        "data",
        dataset
    )
    if os.path.isfile(new_path) or data_file == 'mnist.pkl.gz':
        dataset = new_path

if (not os.path.isfile(dataset)) and data_file == 'mnist.pkl.gz':
    from six.moves import urllib
    origin = (
        'http://www.iro.umontreal.ca/~lisa/deep/data/mnist/mnist.pkl.gz'
    )
    print('Downloading data from %s' % origin)
    urllib.request.urlretrieve(origin, dataset)

print('... loading data')

# Load the dataset
with gzip.open(dataset, 'rb') as f:
    try:
        train_set, valid_set, test_set = pickle.load(f, encoding='latin1')
    except:
        train_set, valid_set, test_set = pickle.load(f)
# train_set, valid_set, test_set format: tuple(input, target)
# input is a numpy.ndarray of 2 dimensions (a matrix)
# where each row corresponds to an example. target is a
# numpy.ndarray of 1 dimension (vector) that has the same length as
# the number of rows in the input. It should give the target
# to the example with the same index in the input.

the line with
urllib.request.urlretrieve(origin, dataset) did not work for me either, but you just have to download the file so i did some changes

from urllib import request

def get(url):
    with request.urlopen(url) as r:
       return r.read()

def download(url, file=None):
    if not file:
        file = url.split('/')[-1]
    with open(file, 'wb') as f:
        f.write(get(url))

and call the download function

url = "http://www.iro.umontreal.ca/~lisa/deep/data/mnist/mnist.pkl.gz"
download(url)

After these changes, the function load_data works fine for me

I did some more Investigation:

Replace this statement:

urllib.request.urlretrieve(origin, dataset)

with the following Statement:

dataset, header = urllib.request.urlretrieve(origin, 'mnist.pkl.gz')

you get a tuple back from urlretrieve and with dataset from the tuple you have to work with in the gzip.open statement!


问:
double and single quotes in wrapping awk codes
I'm trying to extract a string from a group of strings stored in a variable, say foo, in bash:

```<code>foo="I foobar you"
</code>```

using awk, but I got different results when I wrapped awk with single and double quotes:

```<code>$ echo $foo | awk '{print $2}'
$ foobar

$ echo $foo | awk "{print $2}"
$ I foobar you
</code>```

Could anyone tell me why ' and " cause different results when wrapping awk?

答:
It is because when using double-quotes the the shell tries to expand it as a positional parameter or a variable before passing it to awk. In your case it tries to expand $1 but finds no value for it, and so the expansion results to nothing and that's why you get the default print action in awk to print the whole line.

The reason why we single quote the awk commands is that we want a plain string and not modified/tampered by the shell by any means before processed by the Awk command itself which understands $1 as the first field (with default space delimiter)

Body:
I'm trying to extract a string from a group of strings stored in a variable, say foo, in bash:

foo="I foobar you"

using awk, but I got different results when I wrapped awk with single and double quotes:

$ echo $foo | awk '{print $2}'
$ foobar

$ echo $foo | awk "{print $2}"
$ I foobar you

Could anyone tell me why ' and " cause different results when wrapping awk?


答: It's legal. the interpreter should just ignore any white space it finds.</p>

Title: PHP: white space between function name and parenthesis

Body:
I'm wondering if this is legal:

<?php

is_null
(
    $var
);

Or perhaps this:

<?php
$items = array
(
    "Details" => array('controller' => 'event', 'action' => 'readall', 'tab' => 'details'),
    "Calendar" => array('controller' => 'event', 'action' => 'readall', 'tab' => 'calendar')
);

Or will some interpreters choke?

I know the popular mixed style of classes and functions have the opening curly brace on a new line and control structures, such as "if", on the same line. However, I like all one or the other. Newline or no newline. It would be nice to extend the concept to function calls and their parenthesis as well. This is the reason for my question.


问:
Name of a standard for time representation in milliseconds
I'm looking for the name of the standard for time, represented in milliseconds since midnight January 1, 1970 UTC. We can get one calling System.currentTimeMillis() in Java. For example when we are talking about the same time, represented in seconds - we use term Unix time. It would be great if you provide a link to standard in the answer.

答:
In the GNU Linux documentation it is called:

seconds since the epoch

Extrapolating you could call it milliseconds since the epoch.


问:
Dump new array into .caffemodel file
I have a customized layer with certain parameters like kernel size,pad info etc.. among this I have array parameter which is of type bool/int. I need to add this array parameter in .caffemodel file after processing. How to link/Dump an array into caffemodel file?

I have included array parameter in .proto file like below.

```<code>{
 ... //other parameters
 repeated bool/int <varibale_name> [packed = true]
}
</code>```

Is it possible to create blob of type bool?

答:
You can do it through py-caffe net surgery -

```<code>net.params[learnable_layer_name].add_blob()
new_blob_index = len(net.params[learnable_layer_name]) - 1
net.params[learnable_layer_name][new_blob_index].reshape(desired_shape)
net.params[learnable_layer_name][new_blob_index].data[:] = new_data_to_insert
</code>```

HTH.

Title: Dump new array into .caffemodel file

Body:
I have a customized layer with certain parameters like kernel size,pad info etc.. among this I have array parameter which is of type bool/int. I need to add this array parameter in .caffemodel file after processing. How to link/Dump an array into caffemodel file?

I have included array parameter in .proto file like below.

{
 ... //other parameters
 repeated bool/int <varibale_name> [packed = true]
}

Is it possible to create blob of type bool?

Body:
You can do it through py-caffe net surgery -

net.params[learnable_layer_name].add_blob()
new_blob_index = len(net.params[learnable_layer_name]) - 1
net.params[learnable_layer_name][new_blob_index].reshape(desired_shape)
net.params[learnable_layer_name][new_blob_index].data[:] = new_data_to_insert

HTH.


问:
can run project on local host because error creating bean with name
I just worked in a new office, and I want to setup all the project of my office to my localhost.

When i want to run the project on my localhoost, I have this error :

Error creating bean with name 'pageController': Injection of autowired
  dependencies failed; nested exception is
  org.springframework.beans.factory.BeanCreationException: Could not
  autowire field: private
  com.ptdam.ecomm.gateway.service.ServiceSecurity
  com.ptdam.ecomm.gateway.controller.PageController.serviceSecurity;
  nested exception is java.lang.NoClassDefFoundError:
  org/bouncycastle/jce/provider/BouncyCastleProvider

My project was based in Java and using Maven. Can some one help me ? If my information on this question unclear, you can directly ask me on the comment. Thank you.

答:
It cant find class BouncyCastleProvider. It is inorg.bouncycastle.bcprov-jdkXXPlease check dependencies in yourpom.xml, make sureorg.bouncycastle.bcprov-jdkXX` is included in it. For example:

```<code><dependency>
    <groupId>org.bouncycastle</groupId>
    <artifactId>bcprov-jdk16</artifactId>
    <version>1.45</version>
</dependency>
</code>```

Body:
It cant find class BouncyCastleProvider. It is inorg.bouncycastle.bcprov-jdkXXPlease check dependencies in yourpom.xml, make sureorg.bouncycastle.bcprov-jdkXX` is included in it. For example:

<dependency>
    <groupId>org.bouncycastle</groupId>
    <artifactId>bcprov-jdk16</artifactId>
    <version>1.45</version>
</dependency>


问:
Invariant Violation: Native component for "RNSVGSvgView" does not exist
After days of research on the internet I still haven't solved my problem yet.
I am using react-native-svg and I am having the same issues:
No component found for view with the name "RNSVGPath"

Or this one:
Invariant Violation: Native component for "RNSVGSvgView" does not exist

I tried a lot of things, including:

```<code>react-native unlink react-native-svg
npm uninstall react-native-svg
npm install react-native-svg
react-native link react-native-svg
react-native run-ios
</code>```

Also:

```<code>pod 'RNSVG', :path => '../node_modules/react-native-svg'
rm -rf node_modules
npm install
react-native run-ios
</code>```

But still nothing...
Here's the versions I am using:

```<code>"dependencies": {
    "react": "16.1.1",
    "react-native": "0.50.3",
    "react-native-svg": "^6.0.1-rc.1"
}
</code>```

I tried to build it from XCode directly and still nothing...

Thanks for your help guys !

答:
Svg have problem with version

first, unlink svg then remove npm of svg then  install new one

```<code>"dependencies": {
    "react": "16.1.1",
    "react-native": "0.44.0",
    "react-native-svg": "5.1.8"
}
</code>```

Body:
After days of research on the internet I still haven't solved my problem yet.
I am using react-native-svg and I am having the same issues:
No component found for view with the name "RNSVGPath"

Or this one:
Invariant Violation: Native component for "RNSVGSvgView" does not exist

I tried a lot of things, including:

react-native unlink react-native-svg
npm uninstall react-native-svg
npm install react-native-svg
react-native link react-native-svg
react-native run-ios

Also:

pod 'RNSVG', :path => '../node_modules/react-native-svg'
rm -rf node_modules
npm install
react-native run-ios

But still nothing...
Here's the versions I am using:

"dependencies": {
    "react": "16.1.1",
    "react-native": "0.50.3",
    "react-native-svg": "^6.0.1-rc.1"
}

I tried to build it from XCode directly and still nothing...

Thanks for your help guys !

Body:
Svg have problem with version

first, unlink svg then remove npm of svg then  install new one

"dependencies": {
    "react": "16.1.1",
    "react-native": "0.44.0",
    "react-native-svg": "5.1.8"
}


问:
Dynamic table design (common lookup table), need a nice query to get the values
sql2005

This is my simplified example:
(in reality there are 40+ tables in here, I only showed 2)

I got a table called tb_modules, with 3 columns (id, description, tablename as varchar):

```<code> 1, UserType, tb_usertype
 2, Religion, tb_religion
</code>```

(Last column is actually the name of a different table)

I got an other table that looks like this:
tb_value (columns:id, tb_modules_ID, usertype_OR_religion_ID)
values:

```<code> 1111, 1, 45
 1112, 1, 55
 1113, 2, 123
 1114, 2, 234
</code>```

so, I mean 45, 55, 123, 234 are usertype OR religion ID's
(45, 55 usertype, 123, 234 religion ID`s)

Don't judge, I didn't design the database

Question
How can I make a select, showing * from tb_value, plus one column
That one column would be TITLE from the tb_usertype or RELIGIONNAME from the tb_religion table

I would like to make a general thing.
Was thinking initially about maybe a SQL function that returns a string, but I think I would need dynamic SQL, which is not ok in a function.

Anyone a better idea ?

答:
First take the undesigner out the back and put them out of their misery.  They are hurting people.

Due to their incompetence, every time you add a row to Module, you have to modify every query that uses it.  Good for www.dailywtf.com.

You do not have Referential Integrity either, because you cannot define an FK on the this_or_that column.  Your data is exposed, probably to "code" written by the same undesigner.  No doubt you are aware that this is where the deadlocks are created.

That it is a "judgement", that is so that you understand the gravity of the undesign, and you can justify replacing it, to your managers.

SQL was designed for Relational Databases, that means Normalised.  It is not good for mangled files.  Sure, some queries may be better than others (just look at the answers), but there is no way to get around the undesign, any SQL query will be hamstrung, and need change whenever a Module row is added.

"Dynamic" is reserved for Databases, not possible for flat flies.

Two answers.  One to stop the continuing idiocy of changing the existing queries every time a Module row is added (you're welcome); the second to answer your question.

Safe Future Queries
```CREATE VIEW UserReligion_vw AS
    SELECT  [XxxxId]     = id,    -- replace Xxxx
            [ReligionId] = usertype_OR_religion_ID
        FROM  tb_value
        WHERE tb_modules_ID = 1</p>

<p>CREATE VIEW UserReligion_vw AS
    SELECT  [XxxxId]     = id,
            [ReligionId] = usertype_OR_religion_ID
        FROM  tb_value
        WHERE tb_modules_ID = 2</code>```
From now on, make sure the all queries currently using the undesign, are modified to use the correct View instead.  Do not use the Views for Update/Delete/Insert.

Answer

Ok, now for the main question. I can think of other approaches, but this one is the best.  You have stated, you want the third column to also be an unnormalised piece of chicken excreta and the supply Title for [EITHER_Religion_OR_UserType_OR_This_OR_That]. Right, so you are teaching the user to be confused as well; when the no of modules grow, they will have great fun figuring out what the column contains.  Yes a problem does always compound itself.```
    SELECT  [XxxxId]   = id,
            [Whatever] = CASE tb_modules_ID
                WHEN 1 THEN ( SELECT name      -- title, whatever
                    FROM  tb_religion
                    WHERE id = V.usertype_OR_religion_ID
                    )
                WHEN 2 THEN ( SELECT name      -- title, whatever
                    FROM  tb_usertype
                    WHERE id = V.usertype_OR_religion_ID
                    )
                ELSE "(UnknownModule)"         -- do not remove the brackets
                END
        FROM  tb_value V
        WHERE conditions...                    -- you need something here
</code>```
This is called a Correlated Scalar Subquery.

It works on any version of Sybase since 4.9.2 with no limitations. And SQL 2005 (last time I looked, anyway, Aug 2009).  But on MS you will get a StackTrace if the volume of tb_value is large, so make sure the WHERE clause has some conditions on it.

But MS have broken the server with their "new" 2008 codeline, so it does not work in all circumstances (the worse your mangled files, the less likely it will work; the better your database design, the more likely it will work). That is why some MS people pray every day for the next Service pack, and others never attend church.

Body:
sql2005

This is my simplified example:
(in reality there are 40+ tables in here, I only showed 2)

I got a table called tb_modules, with 3 columns (id, description, tablename as varchar):

 1, UserType, tb_usertype
 2, Religion, tb_religion

(Last column is actually the name of a different table)

I got an other table that looks like this:
tb_value (columns:id, tb_modules_ID, usertype_OR_religion_ID)
values:

 1111, 1, 45
 1112, 1, 55
 1113, 2, 123
 1114, 2, 234

so, I mean 45, 55, 123, 234 are usertype OR religion ID's
(45, 55 usertype, 123, 234 religion ID`s)

Don't judge, I didn't design the database

Question
How can I make a select, showing * from tb_value, plus one column
That one column would be TITLE from the tb_usertype or RELIGIONNAME from the tb_religion table

I would like to make a general thing.
Was thinking initially about maybe a SQL function that returns a string, but I think I would need dynamic SQL, which is not ok in a function.

Anyone a better idea ?

Body:
First take the undesigner out the back and put them out of their misery.  They are hurting people.

Due to their incompetence, every time you add a row to Module, you have to modify every query that uses it.  Good for www.dailywtf.com.

You do not have Referential Integrity either, because you cannot define an FK on the this_or_that column.  Your data is exposed, probably to "code" written by the same undesigner.  No doubt you are aware that this is where the deadlocks are created.

That it is a "judgement", that is so that you understand the gravity of the undesign, and you can justify replacing it, to your managers.

SQL was designed for Relational Databases, that means Normalised.  It is not good for mangled files.  Sure, some queries may be better than others (just look at the answers), but there is no way to get around the undesign, any SQL query will be hamstrung, and need change whenever a Module row is added.

"Dynamic" is reserved for Databases, not possible for flat flies.

Two answers.  One to stop the continuing idiocy of changing the existing queries every time a Module row is added (you're welcome); the second to answer your question.

Safe Future Queries

CREATE VIEW UserReligion_vw AS
    SELECT  [XxxxId]     = id,    -- replace Xxxx
            [ReligionId] = usertype_OR_religion_ID
        FROM  tb_value
        WHERE tb_modules_ID = 1

CREATE VIEW UserReligion_vw AS
    SELECT  [XxxxId]     = id,
            [ReligionId] = usertype_OR_religion_ID
        FROM  tb_value
        WHERE tb_modules_ID = 2

From now on, make sure the all queries currently using the undesign, are modified to use the correct View instead.  Do not use the Views for Update/Delete/Insert.

Answer

Ok, now for the main question. I can think of other approaches, but this one is the best.  You have stated, you want the third column to also be an unnormalised piece of chicken excreta and the supply Title for [EITHER_Religion_OR_UserType_OR_This_OR_That]. Right, so you are teaching the user to be confused as well; when the no of modules grow, they will have great fun figuring out what the column contains.  Yes a problem does always compound itself.

    SELECT  [XxxxId]   = id,
            [Whatever] = CASE tb_modules_ID
                WHEN 1 THEN ( SELECT name      -- title, whatever
                    FROM  tb_religion
                    WHERE id = V.usertype_OR_religion_ID
                    )
                WHEN 2 THEN ( SELECT name      -- title, whatever
                    FROM  tb_usertype
                    WHERE id = V.usertype_OR_religion_ID
                    )
                ELSE "(UnknownModule)"         -- do not remove the brackets
                END
        FROM  tb_value V
        WHERE conditions...                    -- you need something here

This is called a Correlated Scalar Subquery.

It works on any version of Sybase since 4.9.2 with no limitations. And SQL 2005 (last time I looked, anyway, Aug 2009).  But on MS you will get a StackTrace if the volume of tb_value is large, so make sure the WHERE clause has some conditions on it.

But MS have broken the server with their "new" 2008 codeline, so it does not work in all circumstances (the worse your mangled files, the less likely it will work; the better your database design, the more likely it will work). That is why some MS people pray every day for the next Service pack, and others never attend church.


问:
Read first column as header in matrix for unsquare matrix
I am trying to read in an adjacency matrix to create a network in graph
I can read the data in with this:

```<code>Matrix_One <- read.csv("Network Matrix.csv", header=TRUE)
Matrix <- as.matrix(Matrix_One)
first_network <- graph.adjacency(Matrix, mode= "directed", weighted=NULL)
</code>```

But this doesn't acknowledge the first column are headers as I'm getting this warning message:

Error in graph.adjacency.dense(adjmatrix, mode = mode, weighted = weighted,  :
    At structure_generators.c:273 : Non-square matrix, Non-square matrix

Any idea how to get R to read column1 as headers?

答:
You'd like to drop the first column of your Matrix like this:

Matrix <- as.matrix(Matrix_One)[,-1]

If the values of your adjacency matrix are numeric, it might be recommendable to use data.matrix() instead of as.matrix() to get numeric values instead of strings in your matrix. Often the values in the adjacency matrix are weights corresponding to each edge-weight given as a numeric value.

To get R to read your data as a usable adjacency matrix, consider this:

```<code> # Assuming your csv file is like this...
csv <- "X,A,B,C,B,E
        A,0,0,1,0,1
        B,1,0,0,0,0
        C,1,1,0,0,0
        D,1,0,0,0,0
        E,0,0,0,0,0"
# ... with first row and column indicating node name in your network.

# To keep names, we could keep the header and use it as a list of nodes:
Matrix_One <- read.csv2("Network Matrix.csv", sep=",", header=TRUE)
Nodelist <- names(Matrix_One)[-1]

# The matrix should include the first row (which is data),
# but not the first column (which too contains node-names) of the df:
Matrix <- data.matrix(Matrix_One)[,-1]
# As the matrix is now of the size N by N, row- and colnames makes for a neat matrix:
rownames(Matrix) <- colnames(Matrix) <- Nodelist

# Look at it
Matrix

# Use igraph to make a graph-object and visualize it
library(igraph)
g <- graph_from_adjacency_matrix(Matrix, mode="directed", weighted=NULL)
plot(g)
</code>```

The package graph is outdated (and removed from CRAN I believe). The above example uses igrpah instead, which is a comprehensive network data management package with some nice visualization. The result from the code above will be something like this:

If you choose to stick to graph, your like first_network <- graph.adjacency(Matrix, mode= "directed", weighted=NULL) takes the square Matrix too.

Body:
I am trying to read in an adjacency matrix to create a network in graph
I can read the data in with this:

Matrix_One <- read.csv("Network Matrix.csv", header=TRUE)
Matrix <- as.matrix(Matrix_One)
first_network <- graph.adjacency(Matrix, mode= "directed", weighted=NULL)

But this doesn't acknowledge the first column are headers as I'm getting this warning message:

Error in graph.adjacency.dense(adjmatrix, mode = mode, weighted = weighted,  :
    At structure_generators.c:273 : Non-square matrix, Non-square matrix

Any idea how to get R to read column1 as headers?

Body:
You'd like to drop the first column of your Matrix like this:

Matrix <- as.matrix(Matrix_One)[,-1]

If the values of your adjacency matrix are numeric, it might be recommendable to use data.matrix() instead of as.matrix() to get numeric values instead of strings in your matrix. Often the values in the adjacency matrix are weights corresponding to each edge-weight given as a numeric value.

To get R to read your data as a usable adjacency matrix, consider this:

 # Assuming your csv file is like this...
csv <- "X,A,B,C,B,E
        A,0,0,1,0,1
        B,1,0,0,0,0
        C,1,1,0,0,0
        D,1,0,0,0,0
        E,0,0,0,0,0"
# ... with first row and column indicating node name in your network.

# To keep names, we could keep the header and use it as a list of nodes:
Matrix_One <- read.csv2("Network Matrix.csv", sep=",", header=TRUE)
Nodelist <- names(Matrix_One)[-1]

# The matrix should include the first row (which is data),
# but not the first column (which too contains node-names) of the df:
Matrix <- data.matrix(Matrix_One)[,-1]
# As the matrix is now of the size N by N, row- and colnames makes for a neat matrix:
rownames(Matrix) <- colnames(Matrix) <- Nodelist

# Look at it
Matrix

# Use igraph to make a graph-object and visualize it
library(igraph)
g <- graph_from_adjacency_matrix(Matrix, mode="directed", weighted=NULL)
plot(g)

The package graph is outdated (and removed from CRAN I believe). The above example uses igrpah instead, which is a comprehensive network data management package with some nice visualization. The result from the code above will be something like this:

If you choose to stick to graph, your like first_network <- graph.adjacency(Matrix, mode= "directed", weighted=NULL) takes the square Matrix too.


问:
jql query in jira only returning two fields
I am trying to do a query that will return the contents of the summary field in Jira.  Jira doesn't allow the use of "AS" in jql and not finding anything useful in the documentation.

```<code>            response = jira.search_issues("project=projectName and type=Test and status=Open as fields:summary")
</code>```

If anyone has experience with Jira rest services please advise on how to approach this.

答:
Try changing your request to:

```<code>response = jira.search_issues("project=projectName and type=Test and status=Open", fields="summary")
</code>```

For further information about the search_issues function look at:
http://jira.readthedocs.io/en/master/api.html#jira.JIRA.search_issues

Body:
I am trying to do a query that will return the contents of the summary field in Jira.  Jira doesn't allow the use of "AS" in jql and not finding anything useful in the documentation.

            response = jira.search_issues("project=projectName and type=Test and status=Open as fields:summary")

If anyone has experience with Jira rest services please advise on how to approach this.

Body:
Try changing your request to:

response = jira.search_issues("project=projectName and type=Test and status=Open", fields="summary")

For further information about the search_issues function look at:
http://jira.readthedocs.io/en/master/api.html#jira.JIRA.search_issues


问:
LINQ operation creating cast exception
While reading data from data-table which is populated by from an excel file, getting "Unable to cast object of type 'System.Double' to type 'System.String'." exception when column datatype is double. My code looks like below

```<code> var importedProducts = dtImportedData.AsEnumerable()
                        .Where(dtProduct => !string.IsNullOrWhiteSpace(dtProduct.Field<string>("product_id")))
                        .Select(dtProduct => new
                        {
                            product_id = dtProduct.Field<string>("product_id").Trim(),
                            product_qty = Convert.ToInt32(dtProduct.Field<double>("product_qty "))
                        }).ToList();
</code>```

When product_id column has text datatype, all is OK. But when there is double datatype, it is throwing exception.

I have already tried solution from here

答:
Well, if product_id is a double and not a string, why you cast it to string at all?

```<code>.Where(row => !string.IsNullOrWhiteSpace(row.Field<double>("product_id").ToString()))
.Select(row => new
 {
    product_id = row.Field<double>("product_id"),
    product_qty = (int)row.Field<double>("product_qty")
 })
</code>```

I don't like the !string.IsNullOrWhiteSpace approach with a field that contains a double or double?. I'm pretty sure that you could use (to be tested):

```<code>.Where(row => row.Field<double?>("product_id").HasValue)
.Select(row => new
 {
   product_id = row.Field<double?>("product_id").Value,
   product_qty = (int)row.Field<double>("product_qty")
 })
</code>```

If you don't know the type of a DataColumn in a DataTable you can execute this in the debugger:

```<code>dtImportedData.Columns["product_id"].DataType;
</code>```

This is what you have to use in Field<T>.

If the type is really changing as commented this is the safest:

```<code>.Where(row => !string.IsNullOrWhiteSpace(row["product_id"].ToString()))
.Select(row => new
 {
    product_id = row["product_id"].ToString().Trim(),
    product_qty = int.Parse(row["product_qty"].ToString().Trim())
 })
</code>```

Title: LINQ operation creating cast exception

Body:
While reading data from data-table which is populated by from an excel file, getting "Unable to cast object of type 'System.Double' to type 'System.String'." exception when column datatype is double. My code looks like below

 var importedProducts = dtImportedData.AsEnumerable()
                        .Where(dtProduct => !string.IsNullOrWhiteSpace(dtProduct.Field<string>("product_id")))
                        .Select(dtProduct => new
                        {
                            product_id = dtProduct.Field<string>("product_id").Trim(),
                            product_qty = Convert.ToInt32(dtProduct.Field<double>("product_qty "))
                        }).ToList();

When product_id column has text datatype, all is OK. But when there is double datatype, it is throwing exception.

I have already tried solution from here

Body:
Well, if product_id is a double and not a string, why you cast it to string at all?

.Where(row => !string.IsNullOrWhiteSpace(row.Field<double>("product_id").ToString()))
.Select(row => new
 {
    product_id = row.Field<double>("product_id"),
    product_qty = (int)row.Field<double>("product_qty")
 })

I don't like the !string.IsNullOrWhiteSpace approach with a field that contains a double or double?. I'm pretty sure that you could use (to be tested):

.Where(row => row.Field<double?>("product_id").HasValue)
.Select(row => new
 {
   product_id = row.Field<double?>("product_id").Value,
   product_qty = (int)row.Field<double>("product_qty")
 })

If you don't know the type of a DataColumn in a DataTable you can execute this in the debugger:

dtImportedData.Columns["product_id"].DataType;

This is what you have to use in Field<T>.

If the type is really changing as commented this is the safest:

.Where(row => !string.IsNullOrWhiteSpace(row["product_id"].ToString()))
.Select(row => new
 {
    product_id = row["product_id"].ToString().Trim(),
    product_qty = int.Parse(row["product_qty"].ToString().Trim())
 })


问:
Java InvocationHandler and a Singleton
I have a singleton object "MySingle" which all through my classes I retrevie as MySingle.createOrGetInstance();

Now the problem I am having is that I would like to do some Method Logging and other stuff before a method in MySingle is called, so I looked into the Java InvocationHandler class Source Link

However the problem is now how would I call the singleton method, because when I use the same code and instead of doing

```<code>DebugProxy.newInstance(new FooImpl());
</code>```

I do this

DebugProxy.newInstance(MySingle.createOrGetInstance()); But when I do this, I get a class Cast expression when I end up assigning the result to a variable `(MySingle sing).

Im developing on Android but I doubt that makes any difference :).

Thanks,
Faisal abid

答:
For a normal java application (I don't know android) it will fail, because the java.lang.reflect.Proxy will only work for interfaces!

A dynamic proxy class (simply referred to as a proxy class below) is a class that implements a list of interfaces specified at runtime when the class is created, with behavior as described below.

@see: Proxy Java Doc

In your case, the DebugProxy from the java reflection guid (http://download.oracle.com/javase/1.3/docs/guide/reflection/proxy.html), uses java.lang.reflect.Proxy!

In your code MySingle is a class, but a java.lang.reflect.Proxy only implement the Interfaces of this class. So casting a java.lang.reflect.Proxy, created for the class MySingle, to the class must MySingle fail with an class cast exception, because the Proxy is not an subclass of MySingle.

One workarround that would work: would be intoducing an Interface MySingle and implemement a class MySingleImpl (with the getInstance method), and then use everywhere (except creation) the interface.

Title: Java InvocationHandler and a Singleton

Body:
I have a singleton object "MySingle" which all through my classes I retrevie as MySingle.createOrGetInstance();

Now the problem I am having is that I would like to do some Method Logging and other stuff before a method in MySingle is called, so I looked into the Java InvocationHandler class Source Link

However the problem is now how would I call the singleton method, because when I use the same code and instead of doing

DebugProxy.newInstance(new FooImpl());

I do this

DebugProxy.newInstance(MySingle.createOrGetInstance()); But when I do this, I get a class Cast expression when I end up assigning the result to a variable `(MySingle sing).

Im developing on Android but I doubt that makes any difference :).

Thanks,
Faisal abid


问:
numpy boolean indexing selecting and setting
I'm not very familiar with python. I reading the book 'Python for Data Analysis' recently, and I'm a bit confused about the numpy boolean indexing and setting.
The book said:

Selecting data from an array by boolean indexing always creates a copy of the data, even if the returned array is unchanged.

Setting values with boolean arrays works in a common-sense way.

And I have tried it as the follow code:

First:

```<code>data = np.random.randn(7, 4)
data[data < 0] = 0 # this could change the `data`
</code>```

Second:

```<code>data = np.random.randn(7, 4)
copied = data[data < 0]
copied[1] = 1  # this couldn't change the `data`
</code>```

I do not quite understand here, anyone can explain it. In my understanding, copied should be pointer to the data[data < 0] slices.

答:
As a rule of thumb numpy creates a view where possible and a copy where necessary.

When is a view possible? When the data can be addressed using strides, i.e. for example for a 2d array A each A[i, j] sits in memory at address base + i*stride[0] + j*stride[1]. If you create a subarray using just slices this will always be the case which is why you will get a view.

For logical and advanced indexing it will typically not be possible to find a base and strides which happen to address the right elements. Therefore these operations return a new array with data copied.

Body:
I'm not very familiar with python. I reading the book 'Python for Data Analysis' recently, and I'm a bit confused about the numpy boolean indexing and setting.
The book said:

Selecting data from an array by boolean indexing always creates a copy of the data, even if the returned array is unchanged.

Setting values with boolean arrays works in a common-sense way.

And I have tried it as the follow code:

First:

data = np.random.randn(7, 4)
data[data < 0] = 0 # this could change the `data`

Second:

data = np.random.randn(7, 4)
copied = data[data < 0]
copied[1] = 1  # this couldn't change the `data`

I do not quite understand here, anyone can explain it. In my understanding, copied should be pointer to the data[data < 0] slices.


问:
Create chat message layout
I have a problem with my chat message Layout in Android.

I have 3 custom textview, but the Time of textView should be set on Bottom and Right align.

Here is my screenshot:

And here is my code:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="60dp"
    android:background="@drawable/msg_in"
    android:orientation="vertical">

    <com.fearefull.chatstory.Widgets.MyTextView
        android:id="@+id/nickname_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:autoLink="none"
        android:layout_alignParentLeft="true"
        android:paddingBottom="7dp"
        android:paddingLeft="16dp"
        android:paddingRight="12dp"
        android:paddingTop="5dp"
        android:text="adawd awdaw"
        android:textStyle="bold"
        android:textColor="@android:color/holo_green_light"
        android:textSize="18sp"
        tools:ignore="ObsoleteLayoutParam,RtlHardcoded" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <com.fearefull.chatstory.Widgets.MyTextView
            android:id="@+id/textview_message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:autoLink="none"
            android:paddingBottom="7dp"
            android:paddingLeft="16dp"
            android:paddingRight="12dp"
            android:paddingTop="5dp"
            android:text="dlsadfafawfafawawfawf a fawfawfafafkawmflaknfalfntigtggt"
            android:textColor="@android:color/black"
            android:textSize="17sp"
            tools:ignore="ObsoleteLayoutParam,RtlHardcoded" />

        <com.fearefull.chatstory.Widgets.MyTextView
            android:id="@+id/textview_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="11:15 PM"
            android:textColor="#a6abad"
            android:textSize="12sp"
            android:layout_alignBottom="@id/textview_message"
            android:layout_alignParentRight="true"
            android:padding="5dp"
            tools:ignore="RtlHardcoded" />
    </RelativeLayout>

</LinearLayout>
```

I used any Alignment for my Time of textView, but it didn't work.

答:
Your layout should be like this. You should get ride of the RelativeLayout and simply change your view's layout gravity</p>

```<code><LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="60dp"
    android:background="@drawable/msg_in"
    android:orientation="vertical">

    <com.fearefull.chatstory.Widgets.MyTextView
        android:id="@+id/nickname_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:autoLink="none"
        android:layout_alignParentLeft="true"
        android:paddingBottom="7dp"
        android:paddingLeft="16dp"
        android:paddingRight="12dp"
        android:paddingTop="5dp"
        android:text="adawd awdaw"
        android:textStyle="bold"
        android:textColor="@android:color/holo_green_light"
        android:textSize="18sp"
        tools:ignore="ObsoleteLayoutParam,RtlHardcoded" />

        <com.fearefull.chatstory.Widgets.MyTextView
            android:id="@+id/textview_message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:autoLink="none"
            android:paddingBottom="7dp"
            android:paddingLeft="16dp"
            android:paddingRight="12dp"
            android:paddingTop="5dp"
            android:text="dlsadfafawfafawawfawf a fawfawfafafkawmflaknfalfntigtggt"
            android:textColor="@android:color/black"
            android:textSize="17sp"
            tools:ignore="ObsoleteLayoutParam,RtlHardcoded" />

        <com.fearefull.chatstory.Widgets.MyTextView
            android:id="@+id/textview_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="11:15 PM"
            android:textColor="#a6abad"
            android:textSize="12sp"
            android:layout_gravity="right"
            android:padding="5dp"
            tools:ignore="RtlHardcoded" />

</LinearLayout>
</code>```

Title: Create chat message layout

Body:
I have a problem with my chat message Layout in Android.

I have 3 custom textview, but the Time of textView should be set on Bottom and Right align.

Here is my screenshot:

And here is my code:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="60dp"
    android:background="@drawable/msg_in"
    android:orientation="vertical">

    <com.fearefull.chatstory.Widgets.MyTextView
        android:id="@+id/nickname_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:autoLink="none"
        android:layout_alignParentLeft="true"
        android:paddingBottom="7dp"
        android:paddingLeft="16dp"
        android:paddingRight="12dp"
        android:paddingTop="5dp"
        android:text="adawd awdaw"
        android:textStyle="bold"
        android:textColor="@android:color/holo_green_light"
        android:textSize="18sp"
        tools:ignore="ObsoleteLayoutParam,RtlHardcoded" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <com.fearefull.chatstory.Widgets.MyTextView
            android:id="@+id/textview_message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:autoLink="none"
            android:paddingBottom="7dp"
            android:paddingLeft="16dp"
            android:paddingRight="12dp"
            android:paddingTop="5dp"
            android:text="dlsadfafawfafawawfawf a fawfawfafafkawmflaknfalfntigtggt"
            android:textColor="@android:color/black"
            android:textSize="17sp"
            tools:ignore="ObsoleteLayoutParam,RtlHardcoded" />

        <com.fearefull.chatstory.Widgets.MyTextView
            android:id="@+id/textview_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="11:15 PM"
            android:textColor="#a6abad"
            android:textSize="12sp"
            android:layout_alignBottom="@id/textview_message"
            android:layout_alignParentRight="true"
            android:padding="5dp"
            tools:ignore="RtlHardcoded" />
    </RelativeLayout>

</LinearLayout>

I used any Alignment for my Time of textView, but it didn't work.

Body:
Your layout should be like this. You should get ride of the RelativeLayout and simply change your view's layout gravity

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="60dp"
    android:background="@drawable/msg_in"
    android:orientation="vertical">

    <com.fearefull.chatstory.Widgets.MyTextView
        android:id="@+id/nickname_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:autoLink="none"
        android:layout_alignParentLeft="true"
        android:paddingBottom="7dp"
        android:paddingLeft="16dp"
        android:paddingRight="12dp"
        android:paddingTop="5dp"
        android:text="adawd awdaw"
        android:textStyle="bold"
        android:textColor="@android:color/holo_green_light"
        android:textSize="18sp"
        tools:ignore="ObsoleteLayoutParam,RtlHardcoded" />

        <com.fearefull.chatstory.Widgets.MyTextView
            android:id="@+id/textview_message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:autoLink="none"
            android:paddingBottom="7dp"
            android:paddingLeft="16dp"
            android:paddingRight="12dp"
            android:paddingTop="5dp"
            android:text="dlsadfafawfafawawfawf a fawfawfafafkawmflaknfalfntigtggt"
            android:textColor="@android:color/black"
            android:textSize="17sp"
            tools:ignore="ObsoleteLayoutParam,RtlHardcoded" />

        <com.fearefull.chatstory.Widgets.MyTextView
            android:id="@+id/textview_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="11:15 PM"
            android:textColor="#a6abad"
            android:textSize="12sp"
            android:layout_gravity="right"
            android:padding="5dp"
            tools:ignore="RtlHardcoded" />

</LinearLayout>


问:
Visual Studio 2017 intellisense includes full namespace
Assuming I have two classes called Job and Person with a property called Jobs that is a List<Job>. Inside the constructor when I create a new List<Job> using intellisense it automatically adds the full namespace this.Jobs = new List<Demo.Namespace.Job>(). I just want it to be like this: this.Jobs = new List<Job>(). Any ideas how to solve this?

```<code>public class Job
{

}

public class Person
{

    public Person()
    {
       this.Jobs = new List<Demo.Namespace.Job>() // this should be ... new List<Job>();
    }

    public List<Job> Jobs { get; set; }

 }
</code>```

答:
2 ways of doing that:

Add Manuallu an using

using Demo.Namespace;
on top of the class

Use CTRL+. autocompletion

First put the cursor on the object Job and press CTRL+., then pur an using statement with the first menu options (it's the same as point 1).

That will avoid you to have to specify a full namespace every time.

IF YOU ALREADY KNOW THIS, then you have namespace ambiguity.

Try to just put List as you want to do and check what it says in the error window, it will tell you whatever other class is creating ambiguity, then you will have to adapt the namespaces to be different

Body:
Assuming I have two classes called Job and Person with a property called Jobs that is a List<Job>. Inside the constructor when I create a new List<Job> using intellisense it automatically adds the full namespace this.Jobs = new List<Demo.Namespace.Job>(). I just want it to be like this: this.Jobs = new List<Job>(). Any ideas how to solve this?

public class Job
{

}

public class Person
{

    public Person()
    {
       this.Jobs = new List<Demo.Namespace.Job>() // this should be ... new List<Job>();
    }

    public List<Job> Jobs { get; set; }

 }