问:
super slow query times for laravel 5 / php-fpm / nginx in dockerized web app
I have a Laravel 5.5/PHP-fpm (PHP 7) API sitting behind nginx, both inside separate Docker containers. My API call which simply returns a json object takes about 2 seconds, which is very long for such a simple query (Even when I query and return some rows from a dockerized mysql database, it takes 3+ seconds). It should be less than a hundred milliseconds. I'm not sure how to debug this problem. Any ideas why it's so slow?

Using Docker for Mac

docker-compose version 1.16.1, build 6d1ac21

Docker version 17.09.0-ce, build afdb6d4

PHP API

```<code>Route::get('/api/names', function () {
  return array(
    1 => "Honey",
    2 => "Nut",
    3 => "Cheerios"
  );
});
</code>```

Nginx dockerfile

```<code>FROM nginx

RUN rm /etc/nginx/conf.d/*

COPY nginx.conf /etc/nginx/conf.d/
</code>```

nginx.conf

```<code>upstream phpie {
  server php-fpm:9000 weight=10 max_fails=3 fail_timeout=30s;
}

server {
  listen 80;
  index index.php index.html;
  root /var/www/api/public;

  resolver            127.0.0.11 valid=5s ipv6=off;

  location / {
    try_files $uri $uri/ /index.php$is_args$args;
  }

  location ~ \.php$ {
      try_files $uri /index.php =404;
      fastcgi_pass phpie:9000;
      fastcgi_index index.php;
      fastcgi_buffers 16 16k;
      fastcgi_buffer_size 32k;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      include fastcgi_params;
  }
}
</code>```

PHP dockerfile

```<code>FROM php:7.1-fpm

RUN apt-get update && apt-get install -y libmcrypt-dev \
    mysql-client libmagickwand-dev --no-install-recommends \
    && pecl install imagick \
    && docker-php-ext-enable imagick \
    && docker-php-ext-install mcrypt pdo_mysql
</code>```

docker-compose.yml

```<code>  php-fpm:
    build:
      context: ./api
    working_dir: /var/www/api
    volumes:
      - ./api:/var/www/api
    ports:
      - "9000"

  nginx:
    restart: always
    build:
      context: ./nginx
    volumes:
      - ./api:/var/www/api
    depends_on:
      - php-fpm
    ports:
      - "80:80"
    command: /bin/bash -c "nginx -g 'daemon off;'"
</code>```

答:
PHP relies on file caching to load quickly on each request. Each time php runs it has to load all required files again. The mount from your Mac to the Docker VM messes with that file cache and loading files becomes slow, so every request becomes slow. Especially anything that loads a framework with lots of files, like Laravel.

Run the app without the Mac -> VM volume mount, with the api/ files copied into both each image in the Dockerfile to confirm.

One solution I've used to this is to create a content volume, that is mounted into each container at run time. For development this content volume is synced by an external tool with your local dev changes.

Content Dockerfile - Dockerfile.content

```<code>FROM scratch
WORKDIR /var/www/api/
COPY . /var/www/api/
</code>```

Compose

```<code>  content:
    image: user3226932/api-content
    build:
      context: ./api
      dockerfile: Dockerfile.content
    volumes:
      - 'content:/var/www/api'

  php-fpm:
    build:
      context: ./api
    working_dir: /var/www/api
    volumes:
      - 'content:/var/www/api'
    depends_on:
      - content
    ports:
      - "9000"

  nginx:
    restart: always
    build:
      context: ./nginx
    volumes:
      - 'content:/var/www/api:ro'
    depends_on:
      - php-fpm
    ports:
      - "80:80"
    command: /bin/bash -c "nginx -g 'daemon off;'"

volumes:
  - content
</code>```

Try something like docker-sync (use the rsync strategy) to keep you local dev environment in sync with the volume. It mostly works but can get a bit buggy from time to time. The other option is doing a straight fswatch & rsync to the local volume directory. docker volume ls

Body:
I have a Laravel 5.5/PHP-fpm (PHP 7) API sitting behind nginx, both inside separate Docker containers. My API call which simply returns a json object takes about 2 seconds, which is very long for such a simple query (Even when I query and return some rows from a dockerized mysql database, it takes 3+ seconds). It should be less than a hundred milliseconds. I'm not sure how to debug this problem. Any ideas why it's so slow?

Using Docker for Mac

docker-compose version 1.16.1, build 6d1ac21

Docker version 17.09.0-ce, build afdb6d4

PHP API

Route::get('/api/names', function () {
  return array(
    1 => "Honey",
    2 => "Nut",
    3 => "Cheerios"
  );
});

Nginx dockerfile

FROM nginx

RUN rm /etc/nginx/conf.d/*

COPY nginx.conf /etc/nginx/conf.d/

nginx.conf

upstream phpie {
  server php-fpm:9000 weight=10 max_fails=3 fail_timeout=30s;
}

server {
  listen 80;
  index index.php index.html;
  root /var/www/api/public;

  resolver            127.0.0.11 valid=5s ipv6=off;

  location / {
    try_files $uri $uri/ /index.php$is_args$args;
  }

  location ~ \.php$ {
      try_files $uri /index.php =404;
      fastcgi_pass phpie:9000;
      fastcgi_index index.php;
      fastcgi_buffers 16 16k;
      fastcgi_buffer_size 32k;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      include fastcgi_params;
  }
}

PHP dockerfile

FROM php:7.1-fpm

RUN apt-get update && apt-get install -y libmcrypt-dev \
    mysql-client libmagickwand-dev --no-install-recommends \
    && pecl install imagick \
    && docker-php-ext-enable imagick \
    && docker-php-ext-install mcrypt pdo_mysql

docker-compose.yml

  php-fpm:
    build:
      context: ./api
    working_dir: /var/www/api
    volumes:
      - ./api:/var/www/api
    ports:
      - "9000"

  nginx:
    restart: always
    build:
      context: ./nginx
    volumes:
      - ./api:/var/www/api
    depends_on:
      - php-fpm
    ports:
      - "80:80"
    command: /bin/bash -c "nginx -g 'daemon off;'"

Body:
PHP relies on file caching to load quickly on each request. Each time php runs it has to load all required files again. The mount from your Mac to the Docker VM messes with that file cache and loading files becomes slow, so every request becomes slow. Especially anything that loads a framework with lots of files, like Laravel.

Run the app without the Mac -> VM volume mount, with the api/ files copied into both each image in the Dockerfile to confirm.

One solution I've used to this is to create a content volume, that is mounted into each container at run time. For development this content volume is synced by an external tool with your local dev changes.

Content Dockerfile - Dockerfile.content

FROM scratch
WORKDIR /var/www/api/
COPY . /var/www/api/

Compose

  content:
    image: user3226932/api-content
    build:
      context: ./api
      dockerfile: Dockerfile.content
    volumes:
      - 'content:/var/www/api'

  php-fpm:
    build:
      context: ./api
    working_dir: /var/www/api
    volumes:
      - 'content:/var/www/api'
    depends_on:
      - content
    ports:
      - "9000"

  nginx:
    restart: always
    build:
      context: ./nginx
    volumes:
      - 'content:/var/www/api:ro'
    depends_on:
      - php-fpm
    ports:
      - "80:80"
    command: /bin/bash -c "nginx -g 'daemon off;'"

volumes:
  - content

Try something like docker-sync (use the rsync strategy) to keep you local dev environment in sync with the volume. It mostly works but can get a bit buggy from time to time. The other option is doing a straight fswatch & rsync to the local volume directory. docker volume ls


来源: stackexchange

问:
Jenkins gerrit trigger not reporting verified voting if there are any compilation errors
When I do some code changes and push the code to Gerrit. Jenkins build gets triggered and we usually get verified voting +1 or -1 depending on the result on to the Gerrit UI for that change.

Currently, we observed that for any of the compilation errors it is not reporting on to the Gerrit. And this happens only for compilation errors, not for failing tests, failing git checkouts or other reasons that make a build fail.

Below I've mentioned the version details. Any suggestions will be highly appreciated.

Gerrit Version: 2.14.2

Jenkins Version: 1.642.3

Gerrit-Trigger plugin: 2.18.3

Project: Java project

Build Tool: Maven

Regards

Sharieff.

答:
That's intended for an "Unstable build" (i.e. compile/build errors but not a test error) in the default Gerrit-trigger configuration.

Take a look at your "Gerrit Reporting Values" for the project/default configuration.
If you want to use the "verify" label for unstable (i.e. compile errors) and failed (test errors) builds, configure the verify values like seen below.

(iirc the default configuration uses code-review = -1 for unstable, for this the used gerrit account needs the permission to submit a code-review label)

ContentLicense: CC BY-SA 3.0


问:
Concatting a list of strings in Prolog
I'm writing a Lisp to C translator and I have a problem with handling strings. This is a code that transforms an unary Lisp function to a C equivalent:

```<code>define(F) --> fun_unary(F), !.

fun_unary(F) --> "(define (", label(Fun), spaces, label(Arg1), ")", spaces, expr(Body), ")",
  {swritef(F, "data *%t(data *%t) { return(%t); }", [Fun, Arg1, Body])}, !.

funs([F])  --> define(F), !.
funs([F|Fs]) --> define(F), spaces, funs(Fs), !.
</code>```

Now I want to read any number of functions and return them as a single string. The above funs is the best I could come up with, but it works like this:

```<code>?- funs(F, "(define (carzero l) (= (car l) 0)) (define (zero n) (= 0 n))", []).
F = ["data *carzero(data *l) { return(eq(car(l), make_atom_int(0))); }", "data *zero(data *n) { return(eq(make_atom_int(0), n)); }"].
</code>```

While I want something like this:

```<code>F = "data *carzero(data *l) { return(eq(car(l), make_atom_int(0))); }\n\ndata *zero(data *n) { return(eq(make_atom_int(0), n)); }".
</code>```

so that I can nicely swritef is into a complete program, between #includes and main(). An alternative solution is to modify the highest level translator to handle the list. It curently looks like this:

```<code>program(P) --> define(F), {swritef(P, "#include \"lisp2c.h\" \n\n%t \nint main() { return 0; }", [F])}, !.
</code>```

How would I do any of these two? I'm using SWI Prolog.

答:
Setting aside for now the purpose for which it's needed, let's write a Prolog predicate that concatenates a list of strings into one string, placing a double newline between each consecutive pair of strings (but not at the end of the output string, judging by the example that Jerry posted).

SWI-Prolog Manual:  Normally I'd post "deep" links to  the documentation, but the SWI-Prolog site uses a style of URL that triggers cross-site scripting (XSS) warnings with many browser/plugin combinations.  So instead I'll refer than link to the appropriate section.

Section 4.22 Representing text in strings says (in part), "String objects by default have no lexical representation and thus can only be created using the predicates below or through the foreign language interface."  This can be a little confusing, as SWI-Prolog writes strings as double-quoted text, but reads double-quoted text (by default) as lists of character codes.

Here's code for a predicate that concatenates the strings in a list, inserting another string Separator in between consecutive string pairs:

```<code>strSepCat([ ],_,Empty) :-
    string_to_list(Empty,[ ]).
strSepCat([H|T],Separator,StrCat) :-
    strSepCat(T,Separator,H,StrCat).

strSepCat([ ],_,StrCat,StrCat).
strSepCat([H|T],Sep,Str,Cat) :-
    string_concat(Sep,H,SepH),
    string_concat(Str,SepH,StrSepH),
    strSepCat(T,Sep,StrSepH,Cat).
</code>```

Note that we've defined two predicates, strSepCat/3 and strSepCat/4.  The former is defined in terms of the latter, a typical design pattern in Prolog that introduces an extra argument as an accumulator that binds to an output when recursion is complete.  Such a technique is often helpful in getting a  tail recursive definition.

To use the predicate strSepCat/3, we'd generally need to construct the separator string with (the escape sequence for) two newlines:

```<code>?- funs(Fs,Lisp,[ ]), string_to_list(Sep,"\n\n"), strSepCat(Fs,Sep,CProg).
</code>```

ContentLicense: CC BY-SA 2.5

Title: Concatting a list of strings in Prolog

Body:
I'm writing a Lisp to C translator and I have a problem with handling strings. This is a code that transforms an unary Lisp function to a C equivalent:

define(F) --> fun_unary(F), !.

fun_unary(F) --> "(define (", label(Fun), spaces, label(Arg1), ")", spaces, expr(Body), ")",
  {swritef(F, "data *%t(data *%t) { return(%t); }", [Fun, Arg1, Body])}, !.

funs([F])  --> define(F), !.
funs([F|Fs]) --> define(F), spaces, funs(Fs), !.

Now I want to read any number of functions and return them as a single string. The above funs is the best I could come up with, but it works like this:

?- funs(F, "(define (carzero l) (= (car l) 0)) (define (zero n) (= 0 n))", []).
F = ["data *carzero(data *l) { return(eq(car(l), make_atom_int(0))); }", "data *zero(data *n) { return(eq(make_atom_int(0), n)); }"].

While I want something like this:

F = "data *carzero(data *l) { return(eq(car(l), make_atom_int(0))); }\n\ndata *zero(data *n) { return(eq(make_atom_int(0), n)); }".

so that I can nicely swritef is into a complete program, between #includes and main(). An alternative solution is to modify the highest level translator to handle the list. It curently looks like this:

program(P) --> define(F), {swritef(P, "#include \"lisp2c.h\" \n\n%t \nint main() { return 0; }", [F])}, !.

How would I do any of these two? I'm using SWI Prolog.

Body:
Setting aside for now the purpose for which it's needed, let's write a Prolog predicate that concatenates a list of strings into one string, placing a double newline between each consecutive pair of strings (but not at the end of the output string, judging by the example that Jerry posted).

SWI-Prolog Manual:  Normally I'd post "deep" links to  the documentation, but the SWI-Prolog site uses a style of URL that triggers cross-site scripting (XSS) warnings with many browser/plugin combinations.  So instead I'll refer than link to the appropriate section.

Section 4.22 Representing text in strings says (in part), "String objects by default have no lexical representation and thus can only be created using the predicates below or through the foreign language interface."  This can be a little confusing, as SWI-Prolog writes strings as double-quoted text, but reads double-quoted text (by default) as lists of character codes.

Here's code for a predicate that concatenates the strings in a list, inserting another string Separator in between consecutive string pairs:

strSepCat([ ],_,Empty) :-
    string_to_list(Empty,[ ]).
strSepCat([H|T],Separator,StrCat) :-
    strSepCat(T,Separator,H,StrCat).

strSepCat([ ],_,StrCat,StrCat).
strSepCat([H|T],Sep,Str,Cat) :-
    string_concat(Sep,H,SepH),
    string_concat(Str,SepH,StrSepH),
    strSepCat(T,Sep,StrSepH,Cat).

Note that we've defined two predicates, strSepCat/3 and strSepCat/4.  The former is defined in terms of the latter, a typical design pattern in Prolog that introduces an extra argument as an accumulator that binds to an output when recursion is complete.  Such a technique is often helpful in getting a  tail recursive definition.

To use the predicate strSepCat/3, we'd generally need to construct the separator string with (the escape sequence for) two newlines:

?- funs(Fs,Lisp,[ ]), string_to_list(Sep,"\n\n"), strSepCat(Fs,Sep,CProg).


问:
jwplayer v7.xx _.getVideoData is not a function issue
From today I have the following issue on youtube hosted videos when I want to change dynamically the video on my site.

_.getVideoData is not a function

```<code>Uncaught TypeError: _.getVideoData is not a function
at M (ssl.p.jwpcdn.com/player/v/7.12.11/provider.youtube.js:1)
at load (ssl.p.jwpcdn.com/player/v/7.12.11/provider.youtube.js:1)
at l.a (ssl.p.jwpcdn.com/player/v/7.12.11/provider.youtube.js:1)
at l.<anonymous>
(ssl.p.jwpcdn.com/player/v/7.12.11/provider.youtube.js:1)
at P (ssl.p.jwpcdn.com/player/v/7.12.11/provider.youtube.js:1)
at K.f.I (s.ytimg.com/yts/jsbin/www-widgetapi-vflnzpyZ4/www-
widgetapi.js:66)
at W.f.l (s.ytimg.com/yts/jsbin/www-widgetapi-vflnzpyZ4/www-
widgetapi.js:114)
at W.f.J (s.ytimg.com/yts/jsbin/www-widgetapi-vflnzpyZ4/www-
widgetapi.js:127)
at S.g (s.ytimg.com/yts/jsbin/www-widgetapi-vflnzpyZ4/www-
widgetapi.js:143)
at g (s.ytimg.com/yts/jsbin/www-widgetapi-vflnzpyZ4/www-
widgetapi.js:95)
</code>```

Until today it worked fine. (The jwplayer 8 version show the same issue)

答:
JW Player 7.12.x uses the youtube-iframe-api. The getVideoData method was removed without warning, and we would like Google to put it back to restore functionality to integrations that use it.

The source to the JW Player Youtube provider can be found and forked here:

https://github.com/jwplayer/jwplayer/blob/v7.12.x/src/js/providers/youtube.js

And this issue is being tracked in the project here:

https://github.com/jwplayer/jwplayer/issues/2525

Body:
From today I have the following issue on youtube hosted videos when I want to change dynamically the video on my site.

_.getVideoData is not a function

Uncaught TypeError: _.getVideoData is not a function
at M (ssl.p.jwpcdn.com/player/v/7.12.11/provider.youtube.js:1)
at load (ssl.p.jwpcdn.com/player/v/7.12.11/provider.youtube.js:1)
at l.a (ssl.p.jwpcdn.com/player/v/7.12.11/provider.youtube.js:1)
at l.<anonymous>
(ssl.p.jwpcdn.com/player/v/7.12.11/provider.youtube.js:1)
at P (ssl.p.jwpcdn.com/player/v/7.12.11/provider.youtube.js:1)
at K.f.I (s.ytimg.com/yts/jsbin/www-widgetapi-vflnzpyZ4/www-
widgetapi.js:66)
at W.f.l (s.ytimg.com/yts/jsbin/www-widgetapi-vflnzpyZ4/www-
widgetapi.js:114)
at W.f.J (s.ytimg.com/yts/jsbin/www-widgetapi-vflnzpyZ4/www-
widgetapi.js:127)
at S.g (s.ytimg.com/yts/jsbin/www-widgetapi-vflnzpyZ4/www-
widgetapi.js:143)
at g (s.ytimg.com/yts/jsbin/www-widgetapi-vflnzpyZ4/www-
widgetapi.js:95)

Until today it worked fine. (The jwplayer 8 version show the same issue)


问:
Python tkinter find which button is clicked
I am trying to implement a game called "Five In a Row".
And I create a 15×15 list to put the buttons. (I used range(16) because I also want a row and a column to display the row number and column number)

I hope my implementation will be like when a button is clicked, it becomes a label.
But I don't know which button the user clicks.

How am I able to implement that? Thanks!

```<code>from tkinter import *
root=Tk()
root.wm_title("Five In a Row")
buttonlst=[ list(range(16)) for i in range(16)]

chess=Label(root,width=2,height=2,text='0')

def p(button):
    gi=button.grid_info()
    x=gi['row']
    y=gi['column']
    button.grid_forget()
    chess.grid(row=x,column=y)
    buttonlst[x][y]=chess

for i in range(16):
    for j in range(16):
        if i==0:
            obj=Label(root,width=2,text=hex(j)[-1].upper())
        elif j==0:
            obj=Label(root,width=2,text=hex(i)[-1].upper())
        else:
            obj=Button(root,relief=FLAT,width=2,command=p(obj))
        obj.grid(row=i,column=j)
        buttonlst[i][j]=obj

root.mainloop()
</code>```

There is a similar question How to determine which button is pressed out of Button grid in Python TKinter?. But I don't quite get that.

答:
When you use command = p(obj) you are actually calling the function p. If you want to pass a function with parameters you should create a lambda function. Therefore, the command assignment should be something like:

```<code>command = lambda: p(obj)
</code>```

That will pass the object properly into p function.

Body:
I am trying to implement a game called "Five In a Row".
And I create a 15×15 list to put the buttons. (I used range(16) because I also want a row and a column to display the row number and column number)

I hope my implementation will be like when a button is clicked, it becomes a label.
But I don't know which button the user clicks.

How am I able to implement that? Thanks!

from tkinter import *
root=Tk()
root.wm_title("Five In a Row")
buttonlst=[ list(range(16)) for i in range(16)]

chess=Label(root,width=2,height=2,text='0')

def p(button):
    gi=button.grid_info()
    x=gi['row']
    y=gi['column']
    button.grid_forget()
    chess.grid(row=x,column=y)
    buttonlst[x][y]=chess

for i in range(16):
    for j in range(16):
        if i==0:
            obj=Label(root,width=2,text=hex(j)[-1].upper())
        elif j==0:
            obj=Label(root,width=2,text=hex(i)[-1].upper())
        else:
            obj=Button(root,relief=FLAT,width=2,command=p(obj))
        obj.grid(row=i,column=j)
        buttonlst[i][j]=obj

root.mainloop()

There is a similar question How to determine which button is pressed out of Button grid in Python TKinter?. But I don't quite get that.

Body:
When you use command = p(obj) you are actually calling the function p. If you want to pass a function with parameters you should create a lambda function. Therefore, the command assignment should be something like:

command = lambda: p(obj)

That will pass the object properly into p function.


问:
Optimize Postgis Query and understanding performance
I have a database on a Digital Ocean Server and it seems a bit slow to me (more than one second sometimes). Postgresql with postgis is running there.

Here are some statistics about the database houses, that actually just stores some apartments:

Houses: 190000

```<code>SELECT count(*) from houses;
</code>```

Houses that have been online for the last 24 h: 58000

```<code>SELECT count(*) FROM houses
JOIN (select max(last_seen) as last_ts from houses) as dt
ON last_seen >= dt.last_ts - interval '24 hour';
</code>```

Houses that are in a specific region and active: 3086

```<code> select count(*) from houses
 where ST_DWithin(geom, ST_MakePoint(52.5277411, 13.4)::geography,30000)
                 (active IS NULL OR active = TRUE)
</code>```

Here is the actual SQL query that is a bit slow. Slow means it takes sometimes more then one second for one query:

```<code>SELECT
      *,
      ST_DistanceSphere(geom, ST_MakePoint(52.5277411, 13.4)) as distace
      FROM houses
      JOIN (select max(last_seen) as last_ts from houses) as dt
      ON last_seen >= dt.last_ts - interval '24 hour'
      WHERE
        ST_DWithin(geom, ST_MakePoint(52.5277411, 13.4)::geography,30000)
        AND (active IS NULL OR active = TRUE)
</code>```

What I have tried so far. Remove the join since it is a bit of redundant. Introduce indices.

Here is a query explanation:

Any Idea how that can be improved? Thanks a lot!

PS: If some data is missing please let me know and I will provide.

Here the same with explain analyze:

Database Indices:

答:
since a lot of people tried to help and gave really good advice I want to post my final solution:
As mentioned in the comments, you should always measure, optimize, repeat. Table size and indices are the key points.

Since I'm not an expert on this topic visualization helped a lot with http://tatiyants.com

```<code> Explain (ANALYZE, COSTS, VERBOSE, BUFFERS, FORMAT JSON)
 select
  *,
  ST_DistanceSphere(geom, ST_MakePoint(52.5277411, 13.4)) as distace
  FROM houses
  JOIN (select max(last_seen) as last_ts from houses) as dt
  ON last_seen >= dt.last_ts - interval '24 hour'
  WHERE
    ST_DWithin(geom, ST_MakePoint(52.5277411, 13.4)::geography,30000)
    AND (active IS NULL OR active = TRUE);
</code>```

query visualisation

That helped for the basic understanding. Since I was already using indices, there was not that much optimization possible. In my case it was ok to get a bit delayed results. I introduce materialized view which stores a part of the query:

```<code>CREATE MATERIALIZED VIEW mathouses
 select
  *,
  FROM houses
  JOIN (select max(last_seen) as last_ts from houses) as dt
  ON last_seen >= dt.last_ts - interval '24 hour'
  WHERE (active IS NULL OR active = TRUE);
</code>```

Then added an Index on that view. And added a simple shell script that is called every hour by cron:

```<code>#!/bin/sh
sudo -u <myuser>-Hi -- psql -d <db> -c 'refresh materialized view mathouses;'
</code>```

My final result:

```<code> Explain (ANALYZE, COSTS, VERBOSE, BUFFERS, FORMAT JSON)
 select
  *,
  ST_DistanceSphere(geom, ST_MakePoint(52.5277411, 13.4)) as distace
  FROM mathouses
  WHERE ST_DWithin(geom, ST_MakePoint(52.5277411, 13.4)::geography,30000);
</code>```

query visualisation

Pretty happy with the solution. It's a factor 3x or even more faster right now. To go further the next logical step would be to look at the hardware or optimize the postgresql settings.

Body:
I have a database on a Digital Ocean Server and it seems a bit slow to me (more than one second sometimes). Postgresql with postgis is running there.

Here are some statistics about the database houses, that actually just stores some apartments:

Houses: 190000

SELECT count(*) from houses;

Houses that have been online for the last 24 h: 58000

SELECT count(*) FROM houses
JOIN (select max(last_seen) as last_ts from houses) as dt
ON last_seen >= dt.last_ts - interval '24 hour';

Houses that are in a specific region and active: 3086

 select count(*) from houses
 where ST_DWithin(geom, ST_MakePoint(52.5277411, 13.4)::geography,30000)
                 (active IS NULL OR active = TRUE)

Here is the actual SQL query that is a bit slow. Slow means it takes sometimes more then one second for one query:

SELECT
      *,
      ST_DistanceSphere(geom, ST_MakePoint(52.5277411, 13.4)) as distace
      FROM houses
      JOIN (select max(last_seen) as last_ts from houses) as dt
      ON last_seen >= dt.last_ts - interval '24 hour'
      WHERE
        ST_DWithin(geom, ST_MakePoint(52.5277411, 13.4)::geography,30000)
        AND (active IS NULL OR active = TRUE)

What I have tried so far. Remove the join since it is a bit of redundant. Introduce indices.

Here is a query explanation:

Any Idea how that can be improved? Thanks a lot!

PS: If some data is missing please let me know and I will provide.

Here the same with explain analyze:

Database Indices:

Body:
since a lot of people tried to help and gave really good advice I want to post my final solution:
As mentioned in the comments, you should always measure, optimize, repeat. Table size and indices are the key points.

Since I'm not an expert on this topic visualization helped a lot with http://tatiyants.com

 Explain (ANALYZE, COSTS, VERBOSE, BUFFERS, FORMAT JSON)
 select
  *,
  ST_DistanceSphere(geom, ST_MakePoint(52.5277411, 13.4)) as distace
  FROM houses
  JOIN (select max(last_seen) as last_ts from houses) as dt
  ON last_seen >= dt.last_ts - interval '24 hour'
  WHERE
    ST_DWithin(geom, ST_MakePoint(52.5277411, 13.4)::geography,30000)
    AND (active IS NULL OR active = TRUE);

query visualisation

That helped for the basic understanding. Since I was already using indices, there was not that much optimization possible. In my case it was ok to get a bit delayed results. I introduce materialized view which stores a part of the query:

CREATE MATERIALIZED VIEW mathouses
 select
  *,
  FROM houses
  JOIN (select max(last_seen) as last_ts from houses) as dt
  ON last_seen >= dt.last_ts - interval '24 hour'
  WHERE (active IS NULL OR active = TRUE);

Then added an Index on that view. And added a simple shell script that is called every hour by cron:

#!/bin/sh
sudo -u <myuser>-Hi -- psql -d <db> -c 'refresh materialized view mathouses;'

My final result:

 Explain (ANALYZE, COSTS, VERBOSE, BUFFERS, FORMAT JSON)
 select
  *,
  ST_DistanceSphere(geom, ST_MakePoint(52.5277411, 13.4)) as distace
  FROM mathouses
  WHERE ST_DWithin(geom, ST_MakePoint(52.5277411, 13.4)::geography,30000);

query visualisation

Pretty happy with the solution. It's a factor 3x or even more faster right now. To go further the next logical step would be to look at the hardware or optimize the postgresql settings.


问:
composing a dateframe with an index of a datetime
I have two lists but one is a datetime. How can I combine to form a date frame with index of this datetime and the values of lista2?</p>

```<code>lista1 = [datetime.datetime(2017, 11, 11, 0, 0), datetime.datetime(2017, 11, 12, 0, 0), datetime.datetime(2017, 11, 13, 0, 0)]
lista2 = [31488, 14335, 89]
</code>```

Body:
I have two lists but one is a datetime. How can I combine to form a date frame with index of this datetime and the values of lista2?

lista1 = [datetime.datetime(2017, 11, 11, 0, 0), datetime.datetime(2017, 11, 12, 0, 0), datetime.datetime(2017, 11, 13, 0, 0)]
lista2 = [31488, 14335, 89]

Body:
past two list to a list of tuple

pd.DataFrame(list(zip(lista1,lista2))).set_index(0)
Out[646]:
                1
0
2017-11-11  31488
2017-11-12  14335
2017-11-13     89


问:
Can't declare a 2D dynamic array in a struct
So I have 2 questions.

I'm trying to learn how to dynamically allocate memory for 2D arrays.
Here is a working code, I first want to know if it's fine, it works but I really don't know if I have memory leaks or some errors I don't see.

```<code>typedef struct Map Map;

struct Map
{
    int width, height;
    int** cases; // Not technically a 2D array but I use it like it in my code
};

int getMapValue(Map map, int x, int y);
void setMapValue(Map* map, int value, int x, int y);

void mallocMap(Map* map, int width, int height);
void freeMap(Map* map);

int main()
{
    int l,h,i,j;
    Map map;

    printf("Width : ");
    scanf("%d", &l);
    printf("Height : ");
    scanf("%d", &h);

    map.width = l;
    map.height = h;

    mallocMap(&map, l, h); // allocate memory for the map

    for(j = 0; j < map.height; j++)
        for(i = 0; i < map.width; i++)
            setMapValue(&map, i*j, i, j); // set some values

    for(j = 0; j < map.height; j++)
        for(i = 0; i < map.width; i++)
            printf("%d ", getMapValue(map, j, i)); // read some values, works fine

    freeMap(&map); // free memory

    return 0;
}

void mallocMap(Map* map, int width, int height)
{
    map->cases = malloc(sizeof(int) * width * height);

    if (map->cases == NULL)
    {
        printf("Error\n");
        exit(0);
    }
}

void freeMap(Map* map)
{
    free(map->cases);
}

int getMapValue(Map map, int x, int y)
{
    return *(map.cases + y*map.height + x);
}

void setMapValue(Map* map, int value, int x, int y)
{
    *(map->cases + y*map->height + x) = value;
}
</code>```

Then I have a problem.
I want to add a struct Player which has two Map elements in it like so :

```<code>struct Player
{
    Map map[2];
};
</code>```

But this results in an error array has incomplete element type. Apparently it's because of the size of the array that isn't set correctly, how should I make this work ?

Update : I needed to write the Map structure before the Player structure.

答:
I really don't know if I have memory leaks or some errors I don't see.

Yes.  You have some memory problems during the allocation that has been pointed out by @StephanLechner.

Also, you have an arithmetic error that indexes the wrong elements and indexes outside the bounds of your array.  Your x value ranges from 0 to width-1 and your y value ranges from 0 to height-1.  Each time you increment y you are actually moving width elements in the array.  So:

```<code>return *(map.cases + y*map.height + x);
</code>```

Should be:

```<code>return *(map.cases + y*map.width + x);
</code>```

Body:
So I have 2 questions.

I'm trying to learn how to dynamically allocate memory for 2D arrays.
Here is a working code, I first want to know if it's fine, it works but I really don't know if I have memory leaks or some errors I don't see.

typedef struct Map Map;

struct Map
{
    int width, height;
    int** cases; // Not technically a 2D array but I use it like it in my code
};

int getMapValue(Map map, int x, int y);
void setMapValue(Map* map, int value, int x, int y);

void mallocMap(Map* map, int width, int height);
void freeMap(Map* map);

int main()
{
    int l,h,i,j;
    Map map;

    printf("Width : ");
    scanf("%d", &l);
    printf("Height : ");
    scanf("%d", &h);

    map.width = l;
    map.height = h;

    mallocMap(&map, l, h); // allocate memory for the map

    for(j = 0; j < map.height; j++)
        for(i = 0; i < map.width; i++)
            setMapValue(&map, i*j, i, j); // set some values

    for(j = 0; j < map.height; j++)
        for(i = 0; i < map.width; i++)
            printf("%d ", getMapValue(map, j, i)); // read some values, works fine

    freeMap(&map); // free memory

    return 0;
}

void mallocMap(Map* map, int width, int height)
{
    map->cases = malloc(sizeof(int) * width * height);

    if (map->cases == NULL)
    {
        printf("Error\n");
        exit(0);
    }
}

void freeMap(Map* map)
{
    free(map->cases);
}

int getMapValue(Map map, int x, int y)
{
    return *(map.cases + y*map.height + x);
}

void setMapValue(Map* map, int value, int x, int y)
{
    *(map->cases + y*map->height + x) = value;
}

Then I have a problem.
I want to add a struct Player which has two Map elements in it like so :

struct Player
{
    Map map[2];
};

But this results in an error array has incomplete element type. Apparently it's because of the size of the array that isn't set correctly, how should I make this work ?

Update : I needed to write the Map structure before the Player structure.

Body:
I really don't know if I have memory leaks or some errors I don't see.

Yes.  You have some memory problems during the allocation that has been pointed out by @StephanLechner.

Also, you have an arithmetic error that indexes the wrong elements and indexes outside the bounds of your array.  Your x value ranges from 0 to width-1 and your y value ranges from 0 to height-1.  Each time you increment y you are actually moving width elements in the array.  So:

return *(map.cases + y*map.height + x);

Should be:

return *(map.cases + y*map.width + x);


问:
How to pass already stored data in the database each time the website is run in ASP.NET MVC?
How to pass already stored data in the database each time the website is run in ASP.net MVC??
I have a controller with methods, i want it to be executed for the already stored data each time i run the application.
Also is there any way to change in such a way that this stored data get passed into the controller in a timely manner. Say i want this stored data to be passed into the methods in controller every five minutes once.</p>

答:
What you need is something like a cache singleton.

e.g.

```<code>public interface IMyStuff{
 string VeryImportantStringHere {get;}
}

public MyStuff :IMyStuff {

public string VeryImportantStringHere => {
    var cached = HttpRuntime.Cache.Get(nameof(VeryImportantStringHere));
    if(cached != null) // might need to store something else if you could have nulls in db
    {
         return cached ;
    }

    cached = .... // retrieved from DB however you do it - ADO.net, Linq2Sql, EF etc...

    Cache.Insert(nameof(VeryImportantStringHere), cached , null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(5));

    }
}
</code>```

Then in your controller, using any DI:

```<code>public controller MyController : Controller{

private IMyStuff _ms;
public MyController(IMyStuff ms)
{
    _ms = ms;
}

[HttpGet]
public ActionResult Whatever(){
    var cached = _ms.VeryImportantStringHere; // cached now has the value.
    return View();
    }
}
</code>```

Body:
What you need is something like a cache singleton.

e.g.

public interface IMyStuff{
 string VeryImportantStringHere {get;}
}

public MyStuff :IMyStuff {

public string VeryImportantStringHere => {
    var cached = HttpRuntime.Cache.Get(nameof(VeryImportantStringHere));
    if(cached != null) // might need to store something else if you could have nulls in db
    {
         return cached ;
    }

    cached = .... // retrieved from DB however you do it - ADO.net, Linq2Sql, EF etc...

    Cache.Insert(nameof(VeryImportantStringHere), cached , null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(5));

    }
}

Then in your controller, using any DI:

public controller MyController : Controller{

private IMyStuff _ms;
public MyController(IMyStuff ms)
{
    _ms = ms;
}

[HttpGet]
public ActionResult Whatever(){
    var cached = _ms.VeryImportantStringHere; // cached now has the value.
    return View();
    }
}


问:
cast problem from class to interface
```<code>Assembly assembly = Assembly.LoadFrom("Logic\\bin\\Debug\\Logic.dll");
            Type queryManagerType = assembly.GetType("Logic." + HttpContext.Current.Session["lang_name"] + "SearchQueryManager");
            var queryManager = (ISearchQueryManager)Activator.CreateInstance(queryManagerType);

public interface ISearchQueryManager
    {
        IList<Advertisements> ApplyQueries(string searchQuery, int page, int pageSize, string orderBy, out int count);
    }

public class SlovenianSearchQueryManager : ISearchQueryManager
    {
...
}
</code>```

but i get

Unable to cast object of type 'Logic.SlovenianSearchQueryManager' to type 'Logic.ISearchQueryManager'.

EDIT: whole stacktrace

System.InvalidCastException was
  unhandled by user code

  Message="Unable to cast object of type
  'Logic.SlovenianSearchQueryManager' to
  type 'Logic.ISearchQueryManager'."

  Source="ViaMura.Web.Module"

  StackTrace:
         at ViaMura.Web.Module.WebController.GetAdvertismentsByRawQuery(String
  rawQuery, Int32 page, Int32 pageSize,
  String orderBy, Int32& count) in
  D:\PROJEKTI\crawler\WebCrawlerSuite\ViaMura.Web.Module\WebController.cs:line
  32
         at ViaMura.Web.Module.Views.SearchResultsPresenter.OnResultsLoad(Int32
  page, Int32 pageSize, String orderBy)
  in
  D:\PROJEKTI\crawler\WebCrawlerSuite\ViaMura.Web.Module\Views\SearchResultsPresenter.cs:line
  43
         at ViaMura.Web.SearchResults.SearchAdvertisments()
  in
  D:\PROJEKTI\crawler\WebCrawlerSuite\ViaMura.Web\SearchResults.aspx.cs:line
  155
         at ViaMura.Web.SearchResults.Page_Load(Object
  sender, EventArgs e) in
  D:\PROJEKTI\crawler\WebCrawlerSuite\ViaMura.Web\SearchResults.aspx.cs:line
  149
         at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr
  fp, Object o, Object t, EventArgs e)
         at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object
  sender, EventArgs e)
         at System.Web.UI.Control.OnLoad(EventArgs
  e)
         at ViaMura.Web.App_Code.PageControllers.BasePage.OnLoad(EventArgs
  e) in
  D:\PROJEKTI\crawler\WebCrawlerSuite\ViaMura.Web\App_Code\PageControllers\BasePage.cs:line 89
         at System.Web.UI.Control.LoadRecursive()
         at System.Web.UI.Page.ProcessRequestMain(Boolean
  includeStagesBeforeAsyncPoint, Boolean
  includeStagesAfterAsyncPoint)

  InnerException:

EDIT2:

```<code>string a1 = typeof (ISearchQueryManager).Assembly.Location;
string a2 = typeof(SlovenianSearchQueryManager).Assembly.Location
</code>```

give me the same result:

C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary
  ASP.NET
  Files\root\5438a399\53975f83\assembly\dl3\0f9540b5\15407fe2_5db7cb01\Logic.DLL

but

```<code>string a3 = queryManagerType.Assembly.Location;
</code>```

gives me different path:

D:\PROJEKTI\crawler\WebCrawlerSuite\WebCrawler.Logic\bin\Debug\WebCrawler.Logic.dll

答: One possible reason for this happening is that you have this ISearchQueryManager interface defined in two different assemblies which in fact doesn't represent the same type. I see that you are playing with dynamic assembly loading. So the interface you are statically casting to is not the interface implemented by the SlovenianSearchQueryManager class even if it has the same name.

Title: cast problem from class to interface

Body:
Assembly assembly = Assembly.LoadFrom("Logic\\bin\\Debug\\Logic.dll");
            Type queryManagerType = assembly.GetType("Logic." + HttpContext.Current.Session["lang_name"] + "SearchQueryManager");
            var queryManager = (ISearchQueryManager)Activator.CreateInstance(queryManagerType);

public interface ISearchQueryManager
    {
        IList<Advertisements> ApplyQueries(string searchQuery, int page, int pageSize, string orderBy, out int count);
    }

public class SlovenianSearchQueryManager : ISearchQueryManager
    {
...
}

but i get

Unable to cast object of type 'Logic.SlovenianSearchQueryManager' to type 'Logic.ISearchQueryManager'.

EDIT: whole stacktrace

System.InvalidCastException was
  unhandled by user code

  Message="Unable to cast object of type
  'Logic.SlovenianSearchQueryManager' to
  type 'Logic.ISearchQueryManager'."

  Source="ViaMura.Web.Module"

  StackTrace:
         at ViaMura.Web.Module.WebController.GetAdvertismentsByRawQuery(String
  rawQuery, Int32 page, Int32 pageSize,
  String orderBy, Int32& count) in
  D:\PROJEKTI\crawler\WebCrawlerSuite\ViaMura.Web.Module\WebController.cs:line
  32
         at ViaMura.Web.Module.Views.SearchResultsPresenter.OnResultsLoad(Int32
  page, Int32 pageSize, String orderBy)
  in
  D:\PROJEKTI\crawler\WebCrawlerSuite\ViaMura.Web.Module\Views\SearchResultsPresenter.cs:line
  43
         at ViaMura.Web.SearchResults.SearchAdvertisments()
  in
  D:\PROJEKTI\crawler\WebCrawlerSuite\ViaMura.Web\SearchResults.aspx.cs:line
  155
         at ViaMura.Web.SearchResults.Page_Load(Object
  sender, EventArgs e) in
  D:\PROJEKTI\crawler\WebCrawlerSuite\ViaMura.Web\SearchResults.aspx.cs:line
  149
         at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr
  fp, Object o, Object t, EventArgs e)
         at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object
  sender, EventArgs e)
         at System.Web.UI.Control.OnLoad(EventArgs
  e)
         at ViaMura.Web.App_Code.PageControllers.BasePage.OnLoad(EventArgs
  e) in
  D:\PROJEKTI\crawler\WebCrawlerSuite\ViaMura.Web\App_Code\PageControllers\BasePage.cs:line 89
         at System.Web.UI.Control.LoadRecursive()
         at System.Web.UI.Page.ProcessRequestMain(Boolean
  includeStagesBeforeAsyncPoint, Boolean
  includeStagesAfterAsyncPoint)

  InnerException:

EDIT2:

string a1 = typeof (ISearchQueryManager).Assembly.Location;
string a2 = typeof(SlovenianSearchQueryManager).Assembly.Location

give me the same result:

C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary
  ASP.NET
  Files\root\5438a399\53975f83\assembly\dl3\0f9540b5\15407fe2_5db7cb01\Logic.DLL

but

string a3 = queryManagerType.Assembly.Location;

gives me different path:

D:\PROJEKTI\crawler\WebCrawlerSuite\WebCrawler.Logic\bin\Debug\WebCrawler.Logic.dll


问:
what is this syntax called? (int)(value) casts the same as int(value)
I saw this in an exercise problem and had never seen this syntax used in Python before. Haven't had any luck googling it</p>

答:
The (int)(value) is the same as int(value). The syntax has no (other) special name. The parentheses are superfluous in this case as (int) is the same as int:

```<code>>>> (int)
<type 'int'>
>>> (int)('2')
2
</code>```

You should stick with int(value) and avoid ambiguity.

Body:
The (int)(value) is the same as int(value). The syntax has no (other) special name. The parentheses are superfluous in this case as (int) is the same as int:

>>> (int)
<type 'int'>
>>> (int)('2')
2

You should stick with int(value) and avoid ambiguity.


问:
RegEx match sequence of three strings along with text inbetween
I'm trying to get a regular expression to match something inbetween two strings that includes a third. I'm having trouble getting the lazy quantifier to cooperate, as there are multiple instances of these strings in the input and the RegEx matches something that is not useful, i.e.:

Start...End...Start...End...Start...Middle...End

Whet I'm actually looking for (only one instance of Start and End for each match):

Start...Middle...End or Start...Center...End

I'm pretty sure I need to use lookahead/lookbehind, but while I do conceptually understand them, putting them into practice is really difficult. Here's where I'm at:

/<Start[\s\S]*?(Middle|Center)[\s\S]*?End>/gm

答:
Make use of the tempered greedy token:

```<code>Start(?:(?!Start|End)[\s\S])*?(Middle|Center)[\s\S]*?End
     ^^^^^^^^^^^^^^^^^^^^^^^^^
</code>```

See the regex demo

Details

Start - a literal string

(?:(?!Start|End)[\s\S])*? - any char, 0+ repetitions, as few as possible, that is not a starting point of Start or End sequence

(Middle|Center) - Group 1: Middle or Center

[\s\S]*? - any 0+ chars, as few as possible

End  - a literal string

Body:
Make use of the tempered greedy token:

Start(?:(?!Start|End)[\s\S])*?(Middle|Center)[\s\S]*?End
     ^^^^^^^^^^^^^^^^^^^^^^^^^

See the regex demo

Details

Start - a literal string

(?:(?!Start|End)[\s\S])*? - any char, 0+ repetitions, as few as possible, that is not a starting point of Start or End sequence

(Middle|Center) - Group 1: Middle or Center

[\s\S]*? - any 0+ chars, as few as possible

End  - a literal string


答:
Parentheses can be used arbitrarily for precedence purposes.

(int) evaluates to int, which then gets called with value as the argument, exactly the same as int(value). It's abusing the extra parens to look like a C-style cast, but it's distinctly unPythonic, style-wise.

A scenario in which this might make sense might include parsing a str as int or float based on whether a . occurs in the str, e.g.:

```<code>(float if '.' in value else int)(value)
</code>```

Note: Having written it, still ugly, but it illustrates a use for the parens.

Title: what is this syntax called? (int)(value) casts the same as int(value)

Body: I saw this in an exercise problem and had never seen this syntax used in Python before. Haven't had any luck googling it

Body:
Parentheses can be used arbitrarily for precedence purposes.

(int) evaluates to int, which then gets called with value as the argument, exactly the same as int(value). It's abusing the extra parens to look like a C-style cast, but it's distinctly unPythonic, style-wise.

A scenario in which this might make sense might include parsing a str as int or float based on whether a . occurs in the str, e.g.:

(float if '.' in value else int)(value)

Note: Having written it, still ugly, but it illustrates a use for the parens.


答: Seems like something mistakenly copied from another language such as C. It's not actually invalid syntax in Python but the parentheses around int literally do nothing and are ignored.


问:
bootstrap-datepicker: Show DIV when selecting specific date
Using the bootstrap-datepicker plugin with the embedded markup...

How to show/reveal a DIV element when selecting a specific date AND hide it again when another date has been selected?

```<code><head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.7.1/css/bootstrap-datepicker.min.css" />
  <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.7.1/js/bootstrap-datepicker.min.js"></script>

  <style>
    #example {
      display: none;
      color: green;
      font-weight: bold;
    }

  </style>
</head>

<body>
  <div id="example">Yay my favorate day!</div>
  Please select <b>11/07/2017</b> to show an example:
  <br />
  <div id="myCalendar"></div>
  <input type="hidden" id="hiddenInput">

  <script>
    $(function() {
      $('#myCalendar').datepicker({
        format: 'mm-dd-yyyy',
        weekStart: 1,
        maxViewMode: 2,
        todayBtn: "linked",
        daysOfWeekHighlighted: "0,6",
        todayHighlight: true
      }).on('changeDate', function() {
        $('#hiddenInput').val(
          $('#myCalendar').datepicker('getFormattedDate')
        );
      }).change(function() {
        $("#example").toggle($(this).val() === "11-07-2017");
      });
    });

  </script>
</body>
</code>```

Demo on JSFiddle

答:
There are several ways for different frameworks (and vanilla js). For example, an easy one, if you are using angularjs (not much javascript code is required):</p>

```<code><input data-provide="datepicker" ng-model='yourdate'>
<div id='divExample' ng-show='yourdate==somevalue'></div>
</code>```

Body:
Using the bootstrap-datepicker plugin with the embedded markup...

How to show/reveal a DIV element when selecting a specific date AND hide it again when another date has been selected?

  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.7.1/css/bootstrap-datepicker.min.css" />

Yay my favorate day!

  Please select 11/07/2017 to show an example:

  <input type="hidden" id="hiddenInput">

Demo on JSFiddle

Body:
There are several ways for different frameworks (and vanilla js). For example, an easy one, if you are using angularjs (not much javascript code is required):

<input data-provide="datepicker" ng-model='yourdate'>
<div id='divExample' ng-show='yourdate==somevalue'></div>


问:
How can I remove all occurrences of one number in a list in Python
Just joined to ask this question as I cannot find it for the life of me on Google.

Say I have a list x = [1,1,1,2,3,3,4,5,5,5,6,7]

How can I remove all instances of the number 1 without deleting the index one by one?

tried del & remove but cannot seem to fathom it out.

Essentially what I am doing is counting all the occurrences of one number then removing it from my list.

ps. Cannot use: Remove all occurrences of a value from a list? as lambda isn't covered in our course.

I am not looking for code review so ignore some of my methods, just want to see if theres a way I can do this.

Edit: Thanks all, managed to get what I was after and complete my code. I've taken my code out just in case I am not allowed to post it on here.

Body:
Just use list comprehension:

x = [1,1,1,2,3,3,4,5,5,5,6,7]
x = [i for i in x if i != 1]

Output:

[2, 3, 3, 4, 5, 5, 5, 6, 7]


答:
In a nutshell - this syntax is called redundancy.

Lets think about what brackets do first. Here is an example using arithmetic.

```<code>(3) + (2)
</code>```

I'm sure you're aware that brackets are a higher precedence than + - so this expression evaluates to the following;

```<code>3 + 2
</code>```

If there were something more in the brackets then that would happen first - but as there isn't there is very little point having them.

Now for your example.

```<code>(int) ("5")
</code>```

The brackets on the left are redundant - and the ones on the right indicate a function call. But first, it would evaluate the brackets that are only acting as brackets.

```<code>int("5")
</code>```

The two syntax's you point out are identical in behaviour - but there is no reason to use the one with extra brackets as they are redundant.

It is possible that you are currently getting C syntax confused where you can cast types using (type)object. Please note that there is no such syntax in python - the code you wrote works by coincidence due to the above.

Body:
In a nutshell - this syntax is called redundancy.

Lets think about what brackets do first. Here is an example using arithmetic.

(3) + (2)

I'm sure you're aware that brackets are a higher precedence than + - so this expression evaluates to the following;

3 + 2

If there were something more in the brackets then that would happen first - but as there isn't there is very little point having them.

Now for your example.

(int) ("5")

The brackets on the left are redundant - and the ones on the right indicate a function call. But first, it would evaluate the brackets that are only acting as brackets.

int("5")

The two syntax's you point out are identical in behaviour - but there is no reason to use the one with extra brackets as they are redundant.

It is possible that you are currently getting C syntax confused where you can cast types using (type)object. Please note that there is no such syntax in python - the code you wrote works by coincidence due to the above.


问:
How to create Asynchronous Call using Retrofit and Observables in Kotlin?
I want to make API calls using Retrofit2 library, returns generic type observable.

I am getting an error: android.os.NetworkOnMainThreadException, while making calls.

答:
Looks really easy to solve, two cases to consider. :1)If you are not using RXJava or 2) if you are using it

1) If you are NOT using RXJava

You should use the method enqueue when you make the call. The error you get is because you are calling the response on the same Thread(the MainThread)

Here is an example took from the web that uses Enqueue with Kotlin that possibly you can adapt to your case

```<code>override fun loadPokemonList(pokemonListListener: PokemonListListener) {

    call = pokemonService.getPokedex();
    call.enqueue(object : Callback<PokeDex> {
        override fun onResponse(call: Call<PokeDex>?, response: Response<PokeDex>?) {

            if (response != null && response.isSuccessful) {
                pokemonListListener.onSuccess(response.body())
            } else {
                pokemonListListener.onFailure(appContext.getString(R.string.error_fetching_data))
            }

        }

        override fun onFailure(call: Call<PokeDex>?, t: Throwable?) {
            pokemonListListener.onFailure(appContext.getString(R.string.error_fetching_data))
        }
    })

 }
</code>```

2) If you are using RXJava(usually version 2, be aware you will find several tutorials for the first version online)

But please consider that most of the developers today solve this asynchronous call using RXJava2 that is the second case I was mentioning at the beginning of he post. It will take you several hours to understand the basics of it but once you will do, you will have a great skill to bring with you and manage this kind of calls( even multiple ones) will be really simple. In fact RXJava allows you really easy to direct the operation in a concurrent Thread and observe the result on the Main Thread. Here a good tutorial that explains how to do that
with the easy and well known pattern (see the notations)

```<code>Observable<List<TrendsResponse>>
    .subscribeOn(Schedulers.io())//Run the call on another Thread
    .observeOn(AndroidSchedulers.mainThread())//observe on the Main Thread
    .subscribe();
</code>```

Differently by what people new to RXJava think:
 on default RXJava does not call a concurrent Thread, is up to you to do that with the subcribeOn that does all the "dirty" work and then to observe the result on the Main Thread with ObserveOn.

For principiants is easy to grasp that comparing both of them to the two famous methods of AsyncTask: doInBackground and onPostExecute

EDIT: Please look also this post if you or future users have problems.

Body:
Looks really easy to solve, two cases to consider. :1)If you are not using RXJava or 2) if you are using it

1) If you are NOT using RXJava

You should use the method enqueue when you make the call. The error you get is because you are calling the response on the same Thread(the MainThread)

Here is an example took from the web that uses Enqueue with Kotlin that possibly you can adapt to your case

override fun loadPokemonList(pokemonListListener: PokemonListListener) {

    call = pokemonService.getPokedex();
    call.enqueue(object : Callback<PokeDex> {
        override fun onResponse(call: Call<PokeDex>?, response: Response<PokeDex>?) {

            if (response != null && response.isSuccessful) {
                pokemonListListener.onSuccess(response.body())
            } else {
                pokemonListListener.onFailure(appContext.getString(R.string.error_fetching_data))
            }

        }

        override fun onFailure(call: Call<PokeDex>?, t: Throwable?) {
            pokemonListListener.onFailure(appContext.getString(R.string.error_fetching_data))
        }
    })

 }

2) If you are using RXJava(usually version 2, be aware you will find several tutorials for the first version online)

But please consider that most of the developers today solve this asynchronous call using RXJava2 that is the second case I was mentioning at the beginning of he post. It will take you several hours to understand the basics of it but once you will do, you will have a great skill to bring with you and manage this kind of calls( even multiple ones) will be really simple. In fact RXJava allows you really easy to direct the operation in a concurrent Thread and observe the result on the Main Thread. Here a good tutorial that explains how to do that
with the easy and well known pattern (see the notations)

Observable<List<TrendsResponse>>
    .subscribeOn(Schedulers.io())//Run the call on another Thread
    .observeOn(AndroidSchedulers.mainThread())//observe on the Main Thread
    .subscribe();

Differently by what people new to RXJava think:
 on default RXJava does not call a concurrent Thread, is up to you to do that with the subcribeOn that does all the "dirty" work and then to observe the result on the Main Thread with ObserveOn.

For principiants is easy to grasp that comparing both of them to the two famous methods of AsyncTask: doInBackground and onPostExecute

EDIT: Please look also this post if you or future users have problems.


问:
Contents from text file are rearranged when using hashmap
I have to print out the top 5 contents from a txt file using a HashMap.
Here is a sample of what is in the text file.

A01 Web app

A02 Engagement

A03 Embedding

A04 Impressions

A05 Influencer

A06 Mention

A07 Microblogging

A08 Organic

A09 Reach

A10 Social graph

A11 User-generated content

A12 Affiliate marketing

A13 Bounce rate

A14 Call to Action

A15 Click through rate

etc....

Here is what i have so far:

```<code> final int assumedLineLength = 16;
       File file = new File("src/hw7p1/Acronyms.txt");
     try {
    Scanner scan = new Scanner(file);
     HashMap<String, String> hashMap = new HashMap<String, String>((int)(file.length() / assumedLineLength) * 2);

    while(scan.hasNextLine()) {
        String[] columns = scan.nextLine().split(" ");
        hashMap.put(columns[0], columns[1]);

    }

    for (String key : hashMap.keySet()) {

    System.out.println(key +" " + hashMap.get(key));}

} catch (IOException e) {
    e.printStackTrace();

}
     }}
</code>```

This is what I am getting for my output:

```<code> A87 Big
 A88 Data
 A01 Web
 A89 Database
 A02 Engagement
 A03 Embedding
 A04 Impressions
 A05 Influencer
 A06 Mention
 A07 Microblogging
 A08 Organic
 A09 Reach
 A90 Data
 A91 Data
 A92 Relational
 A93 Hybrid
 A94 IDE
 A95 Native
 A96 NFC
 A97 Responsive
 A10 Social
 A98 SDK
 A11 User-generated
 A99 Content
</code>```

For some reason it is mixing some of the out put around when it is suppose to look the example above. Any help would be greatly appreciated. Thank you

答:
Try:</p>

```<code>Map<String, String> hashMap = new LinkedHashMap<String, String>(
                    (int) (file.length() / assumedLineLength) * 2);
</code>```

Body:
I have to print out the top 5 contents from a txt file using a HashMap.
Here is a sample of what is in the text file.

A01 Web app

A02 Engagement

A03 Embedding

A04 Impressions

A05 Influencer

A06 Mention

A07 Microblogging

A08 Organic

A09 Reach

A10 Social graph

A11 User-generated content

A12 Affiliate marketing

A13 Bounce rate

A14 Call to Action

A15 Click through rate

etc....

Here is what i have so far:

 final int assumedLineLength = 16;
       File file = new File("src/hw7p1/Acronyms.txt");
     try {
    Scanner scan = new Scanner(file);
     HashMap<String, String> hashMap = new HashMap<String, String>((int)(file.length() / assumedLineLength) * 2);

    while(scan.hasNextLine()) {
        String[] columns = scan.nextLine().split(" ");
        hashMap.put(columns[0], columns[1]);

    }

    for (String key : hashMap.keySet()) {

    System.out.println(key +" " + hashMap.get(key));}

} catch (IOException e) {
    e.printStackTrace();

}
     }}

This is what I am getting for my output:

 A87 Big
 A88 Data
 A01 Web
 A89 Database
 A02 Engagement
 A03 Embedding
 A04 Impressions
 A05 Influencer
 A06 Mention
 A07 Microblogging
 A08 Organic
 A09 Reach
 A90 Data
 A91 Data
 A92 Relational
 A93 Hybrid
 A94 IDE
 A95 Native
 A96 NFC
 A97 Responsive
 A10 Social
 A98 SDK
 A11 User-generated
 A99 Content

For some reason it is mixing some of the out put around when it is suppose to look the example above. Any help would be greatly appreciated. Thank you

Body:
Try:

Map<String, String> hashMap = new LinkedHashMap<String, String>(
                    (int) (file.length() / assumedLineLength) * 2);


问:
'git checkout' is not creating local branch to match remote tracking branch
On one machine, calling git checkout will not create a local branch.

For example, if I have a remote tracking branch origin/master, I can call git checkout origin/master, and it will work fine, putting me in a headless state.

But if I try to call git checkout master, I get the error error: pathspec 'master' did not match any file(s) known to git.

I tried this on another machine, and it worked just fine.

They have the exact same version of git installed (2.15.0.windows.1).  git config --list shows one difference.  The machine where it worked properly had the following line in its configuration:

```<code>remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
</code>```

What would put that in the local configuration, why might it be missing from one of the systems, and what is the proper thing to do to fix the problem?

答:
Try

git remote -v

you should see the correct fetch and  push branches .
Else u can force and set it using below command

git remote set-url origin <URL>

Make sure URL is exacltly same url you used to clone the repo .

Body:
On one machine, calling git checkout will not create a local branch.

For example, if I have a remote tracking branch origin/master, I can call git checkout origin/master, and it will work fine, putting me in a headless state.

But if I try to call git checkout master, I get the error error: pathspec 'master' did not match any file(s) known to git.

I tried this on another machine, and it worked just fine.

They have the exact same version of git installed (2.15.0.windows.1).  git config --list shows one difference.  The machine where it worked properly had the following line in its configuration:

remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*

What would put that in the local configuration, why might it be missing from one of the systems, and what is the proper thing to do to fix the problem?


问:
SwiftCrypto AES 265
I don't why I cannot make a AES encryption using the code below.</p>

```<code>    let key="12345678900987654321564738290123".bytes;//32
    let iv="passwordpasswordwordpasswordpass".bytes;//32
    let message="Hello text fcrypt Hello text for enfor r encryptg ";
    print(message.count) //50
    let data = Padding.pkcs7.add(to: message.bytes, blockSize: AES.blockSize)
    do {
        let aes = try AES(key: key, blockMode: .CBC(iv: iv), padding: .pkcs7)
        let ciphertext = try aes.encrypt(data)
        print(ciphertext)
    } catch {
        print(error)//dataPaddingRequired
    }
</code>```

答:
Your IV should be 16 bytes, not 32. AES always has a block size of 128 bits, which divided by 8 is of course 16 bytes. CBC mode uses an IV of the same size as the block size as most (but not all) modes of encryption do.

The authenticated GCM mode is a bit of an odd one out: it uses a nonce / IV of 12 bytes by default and is only available for ciphers with a block size of 128 bits.

Title: SwiftCrypto AES 265

Body:
I don't why I cannot make a AES encryption using the code below.

    let key="12345678900987654321564738290123".bytes;//32
    let iv="passwordpasswordwordpasswordpass".bytes;//32
    let message="Hello text fcrypt Hello text for enfor r encryptg ";
    print(message.count) //50
    let data = Padding.pkcs7.add(to: message.bytes, blockSize: AES.blockSize)
    do {
        let aes = try AES(key: key, blockMode: .CBC(iv: iv), padding: .pkcs7)
        let ciphertext = try aes.encrypt(data)
        print(ciphertext)
    } catch {
        print(error)//dataPaddingRequired
    }


问:
High Sierra Chat.db Timestamp
I'm going to chat.db to get messages on a application (Mac OS).

Before Sierra in my SQL SELECT I was using datetime(date, 'unixepoch', 'localtime') to convert the timestamps.

Now on High Sierra it's not working, I get nothing.

It's seems the timestamps change... more digit like this: 531651854330806016

Does anybody know how to convert this?

答:
Looks like Date fields are now nanosecond fields as per https://apple.stackexchange.com/questions/114168/dates-format-in-messages-chat-db

So you can try this:
datetime(date/1000000000, 'unixepoch', 'localtime')

Title: High Sierra Chat.db Timestamp


问:
Using an array as needles in strpos
How do you use the strpos for an array of needles when searching a string? For example:

```<code>$find_letters = array('a', 'c', 'd');
$string = 'abcdefg';

if(strpos($string, $find_letters) !== false)
{
    echo 'All the letters are found in the string!';
}
</code>```

Because when using this, it wouldn't work, it would be good if there was something like this

答:
Reply to @binyamin and @Timo..  (not enough points to add a comment..) but the result doesn't contain the position..

The code below will return the actual position of the first element which is what strpos is intended to do.  This is useful if you're expecting to find exactly 1 match..  If you're expecting to find multiple matches, then position of first found may be meaningless.

```<code>function strposa($haystack, $needle, $offset=0) {
    if(!is_array($needle)) $needle = array($needle);
    foreach($needle as $query) {
      $res=strpos($haystack, $query, $offset);
      if($res !== false) return $res; // stop on first true result
    }
    return false;
}
</code>```

Title: Using an array as needles in strpos

Body:
How do you use the strpos for an array of needles when searching a string? For example:

$find_letters = array('a', 'c', 'd');
$string = 'abcdefg';

if(strpos($string, $find_letters) !== false)
{
    echo 'All the letters are found in the string!';
}

Because when using this, it wouldn't work, it would be good if there was something like this

Body:
Reply to @binyamin and @Timo..  (not enough points to add a comment..) but the result doesn't contain the position..

The code below will return the actual position of the first element which is what strpos is intended to do.  This is useful if you're expecting to find exactly 1 match..  If you're expecting to find multiple matches, then position of first found may be meaningless.

function strposa($haystack, $needle, $offset=0) {
    if(!is_array($needle)) $needle = array($needle);
    foreach($needle as $query) {
      $res=strpos($haystack, $query, $offset);
      if($res !== false) return $res; // stop on first true result
    }
    return false;
}


问:
Node.js serving over https
I'm testing Node.js application over https connection where I created certificates for localhost,

Certificate creation,

```<code>$ openssl genrsa -out localhost.key 2048
$ openssl req -new -x509 -key localhost.key -out localhost.cert -days 3650 -subj /CN=localhost
</code>```

Use this in server,

```<code>var options = {
  key: fs.readFileSync('./localhost.key'),
  cert: fs.readFileSync('./localhost.cert'),
};
var http2 = require('http2');
var app = express();
const server = http2.createSecureServer( options, app);
server.listen({ host: app_host, port: port});
</code>```

Start the node.js server as,

```<code>$ node server.js

Tested using simple curl command as,
$ curl -k https://localhost:9000/getcpuinfo
 {"hw": ...}
</code>```

"-k" option is to ignote certificate validation step.

But if I try to use pythons 'requests' module as shown below the request fails,
    $ python
    import requests

requests.get("https://localhost:9000/getcpuinfo")
            requests.exceptions.SSLError: ("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')],)",)

So I used 'verify' option to make the request, it still fails.

requests.get("https://localhost:9000/getcpuinfo", verify=False)
          requests.exceptions.SSLError: ("bad handshake: SysCallError(-1, 'Unexpected EOF')",)

What am I doing wrong? How do I workaround this issue using 'requests' module'? Shouldn't 'verify' prevent the check?

答:
The Python requests module does not connect to HTTP/2 servers, it only supports up to HTTP/1.1:

Requests allows you to send organic, grass-fed HTTP/1.1 requests, without the need for manual labor. There's no need to manually add query strings to your URLs, or to form-encode your POST data. Keep-alive and HTTP connection pooling are 100% automatic, thanks to urllib3.

If you compile curl with HTTP/2 support, then it will work. The curl packages pre-installed on most Linux distros and MacOS aren't and probably won't work.

Since HTTP/2 support in Node is experimental and client support is pretty bad outside of modern web browsers, I would not suggest you use it at this time unless you're specifically targeting web browsers or want to use a HTTP/2-capable web server that can support both HTTP/2 and HTTPS.

If you do need to connect to HTTP/2 servers from Python, there is the (also unstable) hyper module that does connect to a node.js HTTP/2 server. It currently doesn't allow you to disable certificate verification, so it will not be a drop-in replacement for requests.

Title: Node.js serving over https

Body:
I'm testing Node.js application over https connection where I created certificates for localhost,

Certificate creation,

$ openssl genrsa -out localhost.key 2048
$ openssl req -new -x509 -key localhost.key -out localhost.cert -days 3650 -subj /CN=localhost

Use this in server,

var options = {
  key: fs.readFileSync('./localhost.key'),
  cert: fs.readFileSync('./localhost.cert'),
};
var http2 = require('http2');
var app = express();
const server = http2.createSecureServer( options, app);
server.listen({ host: app_host, port: port});

Start the node.js server as,

$ node server.js

Tested using simple curl command as,
$ curl -k https://localhost:9000/getcpuinfo
 {"hw": ...}

"-k" option is to ignote certificate validation step.

But if I try to use pythons 'requests' module as shown below the request fails,
    $ python
    import requests

requests.get("https://localhost:9000/getcpuinfo")
            requests.exceptions.SSLError: ("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')],)",)

So I used 'verify' option to make the request, it still fails.

requests.get("https://localhost:9000/getcpuinfo", verify=False)
          requests.exceptions.SSLError: ("bad handshake: SysCallError(-1, 'Unexpected EOF')",)

What am I doing wrong? How do I workaround this issue using 'requests' module'? Shouldn't 'verify' prevent the check?


问:
Simple UML tools
I am looking for a simple UML-tool that will let me do for example a sequence diagram quicker and better than Word from scratch (Without spending to much time on learning the tool, and with the tool more supporting me than constraining me)

Primary choice is Visio.

答: VS 2010 Ultimate has support for diagrams.  You can even generate sequence diagrams.  Here is a blog post about doing so (no affiliation).</p>

Title: Simple UML tools


问:
why is javascript node.js not on google app engine
Google created the V8 JavaScript engine: V8 compiles JavaScript source code directly into machine code when it is first executed.

Node.js is built on V8 - why is Google not offering any Node.js servers like Microsoft Azure?

Google App Engine would be a natural place to put Node.js.

Do you know why Google is not doing just that?

答:
Background: App Engine Standard vs App Engine Flexible

There seems to be some confusion on this topic because there are two versions of App Engine: Standard and Flexible. NodeJS is supported on App Engine Flexible but NOT on App Engine Standard. (See here for a more complete explanation of the differences).

App Engine Standard scales in seconds (as opposed to minutes for the Flexible environment), and has a free tier, so you can develop and demo without spending a dime. These benefits come at the cost of flexibility. App Engine Standard only supports certain languages and libraries, doesn't allow writing to disk nor SSH. In other words, the environment is standardized.

Answer: NodeJS Support on App Engine Standard

If you are interested in NodeJS support on App Engine Standard, please star this issue: https://issuetracker.google.com/issues/67711509.

Google addresses features that get attention from the community (see here). The best way to give your attention to this feature, is to star it on the Google Issue tracker linked to above.

ContentLicense: CC BY-SA 4.0


问:
Cloud Firestore: Update fields in nested objects with dynamic key
following the official documentation of firestore :

```<code>{
    name: "Frank",
    favorites: { food: "Pizza", color: "Blue", subject: "recess" },
    age: 12
}

// To update favorite color:
db.collection("users").doc("frank").update({
    "favorites.color": "Red"
})
</code>```

I would like to use a dynamic key instead of color.

```<code>db.collection("users").doc("frank").update({
    "favorites[" + KEY + "].color": true
});
</code>```

this is of course not possible and will throw an error.

I've been trying to do this :

```<code>db.collection("users").doc("frank").update({
    favorites: {
        [key]: {
            color": true
        }
    }
});
</code>```

It is actually updating with the right key but unfortunately, it is overwriting the other keys (they are being deleted).

答:
I found the solution inspired by a firebase solution (replacing "/" by "."). </p>

```<code>var usersUpdate = {};
usersUpdate[`favorites.${key}.color`] = true;

db.collection("users").doc("frank").update(usersUpdate);
</code>```

Body:
following the official documentation of firestore :

{
    name: "Frank",
    favorites: { food: "Pizza", color: "Blue", subject: "recess" },
    age: 12
}

// To update favorite color:
db.collection("users").doc("frank").update({
    "favorites.color": "Red"
})

I would like to use a dynamic key instead of color.

db.collection("users").doc("frank").update({
    "favorites[" + KEY + "].color": true
});

this is of course not possible and will throw an error.

I've been trying to do this :

db.collection("users").doc("frank").update({
    favorites: {
        [key]: {
            color": true
        }
    }
});

It is actually updating with the right key but unfortunately, it is overwriting the other keys (they are being deleted).

Body:
I found the solution inspired by a firebase solution (replacing "/" by ".").

var usersUpdate = {};
usersUpdate[`favorites.${key}.color`] = true;

db.collection("users").doc("frank").update(usersUpdate);


问:
Tyring to pivot based on Date field
I have 2 fields that I want to pull into a query, plus a deal_balance (Decimal), and AsOfDate (Date).  I tried two different ways; neither worked but I think both are close.  The only catch is that the AsOfDate is the last working day of each month.  I'm not sure how to convert that into an Integer (1,2,etc) or an abbreviated month's name (Jan,Feb,etc.)

```<code>select Contact_ID, TB_Line,
  sum(case when [AsOfDate] = 1 then [DEAL_BALANCE] else 0 end) MonthJan,
  sum(case when [AsOfDate] = 2 then [DEAL_BALANCE] else 0 end) MonthFeb,
  sum(case when [AsOfDate] = 3 then [DEAL_BALANCE] else 0 end) MonthMar,
  sum(case when [AsOfDate] = 4 then [DEAL_BALANCE] else 0 end) MonthApr,
  sum(case when [AsOfDate] = 5 then [DEAL_BALANCE] else 0 end) MonthMay,
  sum(case when [AsOfDate] = 6 then [DEAL_BALANCE] else 0 end) MonthJun,
  sum(case when [AsOfDate] = 7 then [DEAL_BALANCE] else 0 end) MonthJul,
  sum(case when [AsOfDate] = 8 then [DEAL_BALANCE] else 0 end) MonthAug,
  sum(case when [AsOfDate] = 9 then [DEAL_BALANCE] else 0 end) MonthSep,
  sum(case when [AsOfDate] = 10 then [DEAL_BALANCE] else 0 end) MonthOct,
  sum(case when [AsOfDate] = 11 then [DEAL_BALANCE] else 0 end) MonthNov,
  sum(case when [AsOfDate] = 12 then [DEAL_BALANCE] else 0 end) MonthDec
from [TBL_HIST]
group by Contact_ID, TB_Line;

SELECT *
FROM
(
  SELECT Contact_ID, TB_Line, AsOfDate, Deal_Balance
  FROM [TBL_HIST]
) src
pivot
(
  SUM(DEAL_BALANCE)
  for AsOfDate in (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec)
) piv;
</code>```

I am using SQL Server 2008.

答:
Just use the month() function:

```<code>select Contact_ID, TB_Line,
       sum(case when month([AsOfDate]) = 1 then [DEAL_BALANCE] else 0 end) MonthJan,
       sum(case when month([AsOfDate]0 = 2 then [DEAL_BALANCE] else 0 end) MonthFeb,
       . . .
</code>```

Title: Tyring to pivot based on Date field

Body:
I have 2 fields that I want to pull into a query, plus a deal_balance (Decimal), and AsOfDate (Date).  I tried two different ways; neither worked but I think both are close.  The only catch is that the AsOfDate is the last working day of each month.  I'm not sure how to convert that into an Integer (1,2,etc) or an abbreviated month's name (Jan,Feb,etc.)

select Contact_ID, TB_Line,
  sum(case when [AsOfDate] = 1 then [DEAL_BALANCE] else 0 end) MonthJan,
  sum(case when [AsOfDate] = 2 then [DEAL_BALANCE] else 0 end) MonthFeb,
  sum(case when [AsOfDate] = 3 then [DEAL_BALANCE] else 0 end) MonthMar,
  sum(case when [AsOfDate] = 4 then [DEAL_BALANCE] else 0 end) MonthApr,
  sum(case when [AsOfDate] = 5 then [DEAL_BALANCE] else 0 end) MonthMay,
  sum(case when [AsOfDate] = 6 then [DEAL_BALANCE] else 0 end) MonthJun,
  sum(case when [AsOfDate] = 7 then [DEAL_BALANCE] else 0 end) MonthJul,
  sum(case when [AsOfDate] = 8 then [DEAL_BALANCE] else 0 end) MonthAug,
  sum(case when [AsOfDate] = 9 then [DEAL_BALANCE] else 0 end) MonthSep,
  sum(case when [AsOfDate] = 10 then [DEAL_BALANCE] else 0 end) MonthOct,
  sum(case when [AsOfDate] = 11 then [DEAL_BALANCE] else 0 end) MonthNov,
  sum(case when [AsOfDate] = 12 then [DEAL_BALANCE] else 0 end) MonthDec
from [TBL_HIST]
group by Contact_ID, TB_Line;

SELECT *
FROM
(
  SELECT Contact_ID, TB_Line, AsOfDate, Deal_Balance
  FROM [TBL_HIST]
) src
pivot
(
  SUM(DEAL_BALANCE)
  for AsOfDate in (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec)
) piv;

I am using SQL Server 2008.

Body:
Just use the month() function:

select Contact_ID, TB_Line,
       sum(case when month([AsOfDate]) = 1 then [DEAL_BALANCE] else 0 end) MonthJan,
       sum(case when month([AsOfDate]0 = 2 then [DEAL_BALANCE] else 0 end) MonthFeb,
       . . .


问:
Converting NAs to 0 and non-NA's to 1s in R
I am having trouble converting a table of NAs and different values to 0 for NAs and 1s for the all the values.

Table looks like this:

```<code>x    y    z
NA   8    NA
2    NA   NA
9    9    8
...
</code>```

I was able to convert the NAs to 0 using:

```<code>data[is.na(data)] <- 0
</code>```

But I couldn't manage to convert the other values for all the columns at once.

答:
You can do this with a simple ifelse (one-liner):

```<code>data <- data.frame(ifelse(is.na(data),0,1))
</code>```

Or even easier:

```<code>+!is.na(data)
</code>```

Both will give you:

```<code>##   x y z
## 1 0 1 0
## 2 1 0 0
## 3 1 1 1
</code>```

Data:

```<code>data <- structure(list(x = c(NA, 2L, 9L), y = c(8L, NA, 9L), z = c(NA,
     NA, 8L)), .Names = c("x", "y", "z"), row.names = c(NA, 3L), class = "data.frame")
</code>```

Body:
I am having trouble converting a table of NAs and different values to 0 for NAs and 1s for the all the values.

Table looks like this:

x    y    z
NA   8    NA
2    NA   NA
9    9    8
...

I was able to convert the NAs to 0 using:

data[is.na(data)] <- 0

But I couldn't manage to convert the other values for all the columns at once.

Body:
You can do this with a simple ifelse (one-liner):

data <- data.frame(ifelse(is.na(data),0,1))

Or even easier:

+!is.na(data)

Both will give you:

##   x y z
## 1 0 1 0
## 2 1 0 0
## 3 1 1 1

Data:

data <- structure(list(x = c(NA, 2L, 9L), y = c(8L, NA, 9L), z = c(NA,
     NA, 8L)), .Names = c("x", "y", "z"), row.names = c(NA, 3L), class = "data.frame")


问:
C# LINQ to Entities One-To-Many relationship: Problem displaying last log entry data
I have a web application with two tables to track user shares.  Shares must be approved and so they go through a series of status changes.  Each status change is recorded in the log and timestamped. The current status is always the last entry in the log for that particular share.

Here are the entities (with brevity):

Shares:
ShareID
UserName

ShareLogs:
LogID
ShareID
CreatedDate
Status

I'm trying to get a list of Shares with their current status based on the last log entry.
Here is the SQL query that returns the correct result, but I'd like to do this using LINQ.

```<code>SELECT s.ShareID, s.UserName, sl.Code, sl.CreatedDate
FROM Shares s
INNER JOIN
(
SELECT s.ShareID, s.Code, s.CreatedDate
    FROM ShareLogs sl
    INNER JOIN (SELECT ShareID, MAX(CreatedDate) logDate FROM ShareLogs GROUP BY ShareID) sl2
        ON sl.ShareID = sl2.ShareID AND sl.CreatedDate = sl2.logDate) psl1
ON s.ShareID = sl1.ShareID
ORDER BY s.ShareID
</code>```

I have written one LINQ query that seems to work, but I feel like there should be a better way, as this query looks pretty terrible.

```<code>var query = from list in
(from s in context.Shares
join sl in context.ShareLogs on s.ShareID equals sl.ShareID
where sl.CreatedDate == s.ShareLogs.Max(e => e.CreatedDate)
select new
{
 share = s
 status = sl.Code,
 processDate = sl.CreatedDate
}).Where(e => e.status == 2 || e.status == 3)
select list;
</code>```

答:
I prefer method syntax, so here's my answer.  Note I'm also doing a Group By but not a Join.

```<code> context.ShareLogs.GroupBy(log => log.ShareID)
     .Select(g => g.Logs.Single(log =>
         log.CreatedDate == g.Logs.Max(g => g.CreatedDate)))
     .Select(l => new { Share = l.Share, Status = l.Code, Date = l.CreatedDate });
</code>```

You should be able to group the ShareLogs to determine the correct ShareLog object (which contains the Code, Date and ShareID) - and then retrieve the Share by using the navigation property log.Share.

No need for a join, as far as I can see, because as it usually does LINQ has hidden that away via navigation properties.

Body:
I have a web application with two tables to track user shares.  Shares must be approved and so they go through a series of status changes.  Each status change is recorded in the log and timestamped. The current status is always the last entry in the log for that particular share.

Here are the entities (with brevity):

Shares:
ShareID
UserName

ShareLogs:
LogID
ShareID
CreatedDate
Status

I'm trying to get a list of Shares with their current status based on the last log entry.
Here is the SQL query that returns the correct result, but I'd like to do this using LINQ.

SELECT s.ShareID, s.UserName, sl.Code, sl.CreatedDate
FROM Shares s
INNER JOIN
(
SELECT s.ShareID, s.Code, s.CreatedDate
    FROM ShareLogs sl
    INNER JOIN (SELECT ShareID, MAX(CreatedDate) logDate FROM ShareLogs GROUP BY ShareID) sl2
        ON sl.ShareID = sl2.ShareID AND sl.CreatedDate = sl2.logDate) psl1
ON s.ShareID = sl1.ShareID
ORDER BY s.ShareID

I have written one LINQ query that seems to work, but I feel like there should be a better way, as this query looks pretty terrible.

var query = from list in
(from s in context.Shares
join sl in context.ShareLogs on s.ShareID equals sl.ShareID
where sl.CreatedDate == s.ShareLogs.Max(e => e.CreatedDate)
select new
{
 share = s
 status = sl.Code,
 processDate = sl.CreatedDate
}).Where(e => e.status == 2 || e.status == 3)
select list;

Body:
I prefer method syntax, so here's my answer.  Note I'm also doing a Group By but not a Join.

 context.ShareLogs.GroupBy(log => log.ShareID)
     .Select(g => g.Logs.Single(log =>
         log.CreatedDate == g.Logs.Max(g => g.CreatedDate)))
     .Select(l => new { Share = l.Share, Status = l.Code, Date = l.CreatedDate });

You should be able to group the ShareLogs to determine the correct ShareLog object (which contains the Code, Date and ShareID) - and then retrieve the Share by using the navigation property log.Share.

No need for a join, as far as I can see, because as it usually does LINQ has hidden that away via navigation properties.


问:
Flask - Global Variable is not persisting on page reload
I am trying to use a global variable within my flask app:

```<code>from flask import Flask, render_template, request, jsonify

app = Flask(__name__)

varGlobal = None
@app.route('/')
def home():
    return render_template('home.html')

@app.route('/api/prepare', methods=['POST'])
def prepare():

    if varGlobal is None:
       varGlobal = "some_val"
    return varGlobal

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8989, debug=True)
</code>```

So when my page loads and I hit api/prepare I get:

UnboundLocalError: local variable 'varGlobal' referenced before assignment

I want this varGlobal to persist as long as my app is running. Even if I reload the page. How can I do this?

答:
I won't mention again the reason of your traceback, that's mainly because you're not using the global keyword so the global variable can't be bound properly from your method as other already clarified in the comments/answers.

Now, I'd strongly recommend you to avoid global variables, specially in multithread environments, they are evil. Your code will fail miserably when different threads are being spawned (ie: multiple users).

Instead, i'd suggest you get familiar with some of the flask utils created for this very specific task, like this one http://flask.pocoo.org/docs/0.12/api/#flask.Flask.app_ctx_globals_class.

Body:
I am trying to use a global variable within my flask app:

from flask import Flask, render_template, request, jsonify

app = Flask(__name__)

varGlobal = None
@app.route('/')
def home():
    return render_template('home.html')

@app.route('/api/prepare', methods=['POST'])
def prepare():

    if varGlobal is None:
       varGlobal = "some_val"
    return varGlobal

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8989, debug=True)

So when my page loads and I hit api/prepare I get:

UnboundLocalError: local variable 'varGlobal' referenced before assignment

I want this varGlobal to persist as long as my app is running. Even if I reload the page. How can I do this?


问:
what is this syntax called? (int)(value) casts the same as int(value)
I saw this in an exercise problem and had never seen this syntax used in Python before. Haven't had any luck googling it</p>

答:
Both does the same. In OOP, we call it type casting and concept is similar. Both the syntax does the same thing as below:-</p>

```<code>a = (int)(34.65)
print(a)
> 34
a = int(34.65)
print(a)
> 34
</code>```

Body:
Both does the same. In OOP, we call it type casting and concept is similar. Both the syntax does the same thing as below:-

a = (int)(34.65)
print(a)
> 34
a = int(34.65)
print(a)
> 34


问:
VBA Loop - Only 1 Result
I am using this tutorial to write a VBA Loop to search for a value in a column and pulls all rows with the criteria value.

https://www.youtube.com/watch?v=QOxhRSCfHaw

It is running but it takes about 5 minutes to run and in the end I only get 1 result(row) when I should be getting thousands.

```<code>Sub finddata()

'1.Declare Variables
'2.Find Records that match criteria and paste them into new worksheet

Dim customcode As String
Dim finalrow As Long
Dim i As Long

customcode = Sheets("Sheet2").Range("A1").Value
finalrow = Sheets("Raw Data").Range("A252800").End(xlUp).Row

For i = 1 To finalrow

If Cells(i, 46) = customcode Then
Range(Cells(i, 1), Cells(i, 102)).Copy
Worksheets("Sheet1").Range("A1").PasteSpecial
End If

Next i

End Sub
</code>```

Any help is greatly appreciated.

答:
Try an array.</p>

```<code>Sub finddate()

Dim dataRng As Range
Dim origData, newData
Dim i As Long, j As Long, k As Long
Dim customcode As String

customcode = Sheets("Sheet2").Range("A1").Value

With ThisWorkbook.Worksheets("Raw Data")
    Set dataRng = .Range(.Cells(1, 1), .Cells(.Rows.Count, 102).End(xlUp))
End With

origData = dataRng.Value
ReDim newData(1 To UBound(origData, 1), 1 To UBound(origData, 2))

j = 1
For i = 1 To UBound(origData, 1)
    If origData(i, 46) = customcode Then
        For k = 1 To UBound(origData, 2)
            newData(j, k) = origData(i, k)
        Next
        j = j + 1
    End If
Next

With ThisWorkbook.Worksheets("Sheet1")
    .Range(.Cells(1, 1), .Cells(j, 102)) = newData
End With

End Sub
</code>```

Title: VBA Loop - Only 1 Result

Body:
I am using this tutorial to write a VBA Loop to search for a value in a column and pulls all rows with the criteria value.

https://www.youtube.com/watch?v=QOxhRSCfHaw

It is running but it takes about 5 minutes to run and in the end I only get 1 result(row) when I should be getting thousands.

Sub finddata()

'1.Declare Variables
'2.Find Records that match criteria and paste them into new worksheet

Dim customcode As String
Dim finalrow As Long
Dim i As Long

customcode = Sheets("Sheet2").Range("A1").Value
finalrow = Sheets("Raw Data").Range("A252800").End(xlUp).Row

For i = 1 To finalrow

If Cells(i, 46) = customcode Then
Range(Cells(i, 1), Cells(i, 102)).Copy
Worksheets("Sheet1").Range("A1").PasteSpecial
End If

Next i

End Sub

Any help is greatly appreciated.

Body:
Try an array.

Sub finddate()

Dim dataRng As Range
Dim origData, newData
Dim i As Long, j As Long, k As Long
Dim customcode As String

customcode = Sheets("Sheet2").Range("A1").Value

With ThisWorkbook.Worksheets("Raw Data")
    Set dataRng = .Range(.Cells(1, 1), .Cells(.Rows.Count, 102).End(xlUp))
End With

origData = dataRng.Value
ReDim newData(1 To UBound(origData, 1), 1 To UBound(origData, 2))

j = 1
For i = 1 To UBound(origData, 1)
    If origData(i, 46) = customcode Then
        For k = 1 To UBound(origData, 2)
            newData(j, k) = origData(i, k)
        Next
        j = j + 1
    End If
Next

With ThisWorkbook.Worksheets("Sheet1")
    .Range(.Cells(1, 1), .Cells(j, 102)) = newData
End With

End Sub


问:
Angular 4 call function in another component
I have a search bar in a header component, that are supposed to search in a list in another component. How do i get the headerComponent to call a function in the BooksComponent?</p>

```<code>export class HeaderComponent {
    search(){
        searchBooks($(".header-search-input").val());
    }
}

export class BooksComponent {

  searchBooks(searchWord){
    ...
}
</code>```

答:
A solution would be to create a service and there use event emitter.

Book.service.ts

```<code>export class BookService {
    search = new EventEmitter<string>();
}
</code>```

Header Component

```<code>export class headerComponent() {
   constructor(private bookService:BookService) {}

   search() {
       this.bookService.search.emit($(".header-search-input").val());
   }
}
</code>```

Book Component

```<code>export class BookComponent {
    constructor(private bookService:BookService) {
        //Catch the event to make the search
        this.bookService.search.subscribe(value => {
             this.searchBooks(searchWord);
        });
    }
}
</code>```

Body:
I have a search bar in a header component, that are supposed to search in a list in another component. How do i get the headerComponent to call a function in the BooksComponent?

export class HeaderComponent {
    search(){
        searchBooks($(".header-search-input").val());
    }
}

export class BooksComponent {

  searchBooks(searchWord){
    ...
}

Body:
A solution would be to create a service and there use event emitter.

Book.service.ts

export class BookService {
    search = new EventEmitter<string>();
}

Header Component

export class headerComponent() {
   constructor(private bookService:BookService) {}

   search() {
       this.bookService.search.emit($(".header-search-input").val());
   }
}

Book Component

export class BookComponent {
    constructor(private bookService:BookService) {
        //Catch the event to make the search
        this.bookService.search.subscribe(value => {
             this.searchBooks(searchWord);
        });
    }
}


问:
How can I remove all occurrences of one number in a list in Python
Just joined to ask this question as I cannot find it for the life of me on Google.

Say I have a list x = [1,1,1,2,3,3,4,5,5,5,6,7]

How can I remove all instances of the number 1 without deleting the index one by one?

tried del & remove but cannot seem to fathom it out.

Essentially what I am doing is counting all the occurrences of one number then removing it from my list.

ps. Cannot use: Remove all occurrences of a value from a list? as lambda isn't covered in our course.

I am not looking for code review so ignore some of my methods, just want to see if theres a way I can do this.

Edit: Thanks all, managed to get what I was after and complete my code. I've taken my code out just in case I am not allowed to post it on here.

答:
List Comprehension to the rescue.</p>

```<code>x = [v for v in x if v != 1]
</code>```

Body:
List Comprehension to the rescue.

x = [v for v in x if v != 1]


问:
Forgot Apache OFBIZ admin password
I've forgotten my password for the apache of biz admin account , I've asked for an email to be sent however I don't know which account is associated with the ofbiz server so I can't view the email which changed the password , does anyone have any tips? </p>

<p>Thanks </p>

答:
if you can access database, you can make the change directly on the UserLogin table.

Otherwise if you have a remote access to the server, you can use the ant or gradle target - depending of your OFBiz revision - to create another admin acount :

```<code>gradlew loadAdminUserLogin -PuserLoginId=myadmin
</code>```

or

```<code>ant create-admin-user-login
</code>```

Title: Forgot Apache OFBIZ admin password

Body:
I've forgotten my password for the apache of biz admin account , I've asked for an email to be sent however I don't know which account is associated with the ofbiz server so I can't view the email which changed the password , does anyone have any tips?

Thanks

Body:
if you can access database, you can make the change directly on the UserLogin table.

Otherwise if you have a remote access to the server, you can use the ant or gradle target - depending of your OFBiz revision - to create another admin acount :

gradlew loadAdminUserLogin -PuserLoginId=myadmin

or

ant create-admin-user-login


问:
Single Quote Params vs No Quote Params?
This is something that's been mind boggling to me for a while, some times I see people writing javascript objects like so with single quote params:

```<code>{
    'name': 'Teddy',
    'last': 'Monster'
}
</code>```

But then I also see the more common, no quote params:

```<code>{
    name: 'Teddy',
    last: 'Monster'
}
</code>```

Is there a reason one would use single quote params?
Is it faster to parse?

From what I can see, there is no speed difference, rather just cluttering the file with unnecessary quotes and increasing file size.

I'll change my mind if I can get a straight answer :)

答: A quick Google search finds this answer on this very website. Essentially there's no functional difference if you're talking about object literals, but the properties can't be reserved words like "class" or "namespace". Wrapping the property in quotation marks allows those words to be used</p>

Title: Single Quote Params vs No Quote Params?

Body:
This is something that's been mind boggling to me for a while, some times I see people writing javascript objects like so with single quote params:

{
    'name': 'Teddy',
    'last': 'Monster'
}

But then I also see the more common, no quote params:

{
    name: 'Teddy',
    last: 'Monster'
}

Is there a reason one would use single quote params?
Is it faster to parse?

From what I can see, there is no speed difference, rather just cluttering the file with unnecessary quotes and increasing file size.

I'll change my mind if I can get a straight answer :)


问:
Using functional for loop with non-Array iterator
Say we have this generator function (generator fn returns an iterator):

```<code>function* foo(){
  yield 1;
  yield 2;
  yield 3;
}

for (let i of foo()) {
  console.log(i);
}
</code>```

we can iterate over the iterator using a for..of loop.

My question is: is there a way to create a functional for-loop to iterate over an iterator like with an Array?

```<code>[1,2,3].forEach(function(i){
   // we have our own block scope for each item in the iterable
});
</code>```

is our best bet this technique?

```<code> for (let i of foo()) {
      (function(i){

      })(i);
 }
</code>```

seem like there must be an easier way

答:
You can use the second argument of Array.from, which is a map-like callback function:

```<code>Array.from(foo(), value => console.log(value));
</code>```

function* foo(){
  yield 1;
  yield 2;
  yield 3;
}
Array.from(foo(), value => console.log(value));```

Body:
Say we have this generator function (generator fn returns an iterator):

function* foo(){
  yield 1;
  yield 2;
  yield 3;
}

for (let i of foo()) {
  console.log(i);
}

we can iterate over the iterator using a for..of loop.

My question is: is there a way to create a functional for-loop to iterate over an iterator like with an Array?

[1,2,3].forEach(function(i){
   // we have our own block scope for each item in the iterable
});

is our best bet this technique?

 for (let i of foo()) {
      (function(i){

      })(i);
 }

seem like there must be an easier way

Body:
You can use the second argument of Array.from, which is a map-like callback function:

Array.from(foo(), value => console.log(value));

function* foo(){
  yield 1;
  yield 2;
  yield 3;
}
Array.from(foo(), value => console.log(value));