问:
Is auto increment id possible for UseInMemoryDatabase?
I use UseInMemoryDatabase in my .net core application. I need auto increment functionality.  Is that possible? For example my class is;

```<code>public class City
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }
</code>```

...

```<code>public DbSet<Statistic> Statistics { get; set; }
</code>```

...

I want to add a new object as follows;

```<code>context.Cities.Add(new City{Name = "Amsterdam"});
</code>```

And i don't want to add as follows;

```<code>context.Cities.Add(new City{Id = 1,Name = "Amsterdam"});
</code>```

Because I don't want to check existing ids every time.

答:
Yes this is possible and even the default behaviour as long as you don't set the Id (so it is inserted with Id 0) which is the same behaviour as in DB-backed providers.

```<code>using System;
using Microsoft.EntityFrameworkCore;

namespace InMemoryIdSample
{
    class Program
    {
        static void Main(string[] args)
        {
            var options = new DbContextOptionsBuilder<CityContext>().UseInMemoryDatabase("Cities").Options;
            using (var context = new CityContext(options))
            {
                context.Add(new City { Name = "Vienna" });
                context.Add(new City { Name = "Munich" });
                context.SaveChanges();
            }

            using (var context = new CityContext(options))
            {
                foreach(var city in context.Cities)
                {
                    Console.WriteLine($"City {city.Id}: {city.Name}");
                }
            }
        }
    }

    public class City
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public class CityContext : DbContext
    {
        public CityContext(DbContextOptions<CityContext> options) : base(options) { }

        public DbSet<City> Cities { get; set; }
    }
}
</code>```

Prints:

```<code>$ dotnet run
City 1: Vienna
City 2: Munich
</code>```

Body:
I use UseInMemoryDatabase in my .net core application. I need auto increment functionality.  Is that possible? For example my class is;

public class City
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }

...

public DbSet<Statistic> Statistics { get; set; }

...

I want to add a new object as follows;

context.Cities.Add(new City{Name = "Amsterdam"});

And i don't want to add as follows;

context.Cities.Add(new City{Id = 1,Name = "Amsterdam"});

Because I don't want to check existing ids every time.

Body:
Yes this is possible and even the default behaviour as long as you don't set the Id (so it is inserted with Id 0) which is the same behaviour as in DB-backed providers.

using System;
using Microsoft.EntityFrameworkCore;

namespace InMemoryIdSample
{
    class Program
    {
        static void Main(string[] args)
        {
            var options = new DbContextOptionsBuilder<CityContext>().UseInMemoryDatabase("Cities").Options;
            using (var context = new CityContext(options))
            {
                context.Add(new City { Name = "Vienna" });
                context.Add(new City { Name = "Munich" });
                context.SaveChanges();
            }

            using (var context = new CityContext(options))
            {
                foreach(var city in context.Cities)
                {
                    Console.WriteLine($"City {city.Id}: {city.Name}");
                }
            }
        }
    }

    public class City
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public class CityContext : DbContext
    {
        public CityContext(DbContextOptions<CityContext> options) : base(options) { }

        public DbSet<City> Cities { get; set; }
    }
}

Prints:

$ dotnet run
City 1: Vienna
City 2: Munich


来源: stackexchange

问:
ArrayList "cannot be resolved" when trying to access an ArrayList in a method, Java
I've created two classes (Team and MarchMadness) to create a MarchMadness bracket in Java. In the MarchMadness class, I've created an ArrayList of type Team for each round and added all the teams for round1. However, I keep getting a "cannot be resolved" error when trying to pull the ArrayList's size and elements in the randomize method.

I've tried moving the ArrayList's outside of the main method, but then I get an error when trying to add teams to the ArrayList. I've also searched online, but haven't found anything.

Here is my code with only the teams for round1 East.

```<code>
public class Team extends MarchMadness {

    private String name;
    private double rating;
    private int seed;

    public Team(String name, double rating, int seed) {
        this.name = name;
        this.rating = rating;
        this.seed = seed;
    }
}

import java.util.ArrayList;
import java.util.List;

public class MarchMadness {

    public static void main(String[] args) {
        ArrayList<Team> round1East = new ArrayList<Team>();

        round1East.add(new Team("Duke", 0.670, 1));
        round1East.add(new Team("North Dakota St", 0.495, 16));
        round1East.add(new Team("VCU", 0.609, 8));
        round1East.add(new Team("UCF", 0.606, 9));
        round1East.add(new Team("Mississippi St", 0.609, 5));
        round1East.add(new Team("Liberty", 0.549, 12));
        round1East.add(new Team("Virginia Tech", 0.605, 4));
        round1East.add(new Team("Saint Louis", 0.547, 13));
        round1East.add(new Team("Maryland", 0.602, 6));
        round1East.add(new Team("Belmont", 0.579, 11));
        round1East.add(new Team("LSU", 0.626, 3));
        round1East.add(new Team("Yale", 0.572, 14));
        round1East.add(new Team("Louisville", 0.594, 7));
        round1East.add(new Team("Minnesota", 0.578, 10));
        round1East.add(new Team("Michigan St", 0.641, 2));
        round1East.add(new Team("Bradley", 0.504, 15));

    }

    private void randomize() {
        if(round2East.isEmpty()) {
            for(int i = 0; i < round1East.size(); i += 2) {
                int x = (int)(Math.random() * 2);
                if(x == 0) {
                    round2East.add(round1East.get(i));
                } else {
                    round2East.add(round1East.get(i++));
                }
            }
     }

}
</code>```

答:
This is because when you put the ArrayList outside of the main method, you declare it as an "instance variable". Since the main method is static, you cannot reference a "non-static (instance)" variable in a static method.

The fix would be to just declare it outside of the main method, but make the variable static as well.

```<code>static ArrayList<Team> round1East = new ArrayList<Team>();
</code>```

ContentLicense: CC BY-SA 4.0

Body:
I've created two classes (Team and MarchMadness) to create a MarchMadness bracket in Java. In the MarchMadness class, I've created an ArrayList of type Team for each round and added all the teams for round1. However, I keep getting a "cannot be resolved" error when trying to pull the ArrayList's size and elements in the randomize method.

I've tried moving the ArrayList's outside of the main method, but then I get an error when trying to add teams to the ArrayList. I've also searched online, but haven't found anything.

Here is my code with only the teams for round1 East.

public class Team extends MarchMadness {

    private String name;
    private double rating;
    private int seed;

    public Team(String name, double rating, int seed) {
        this.name = name;
        this.rating = rating;
        this.seed = seed;
    }
}

import java.util.ArrayList;
import java.util.List;

public class MarchMadness {

    public static void main(String[] args) {
        ArrayList<Team> round1East = new ArrayList<Team>();

        round1East.add(new Team("Duke", 0.670, 1));
        round1East.add(new Team("North Dakota St", 0.495, 16));
        round1East.add(new Team("VCU", 0.609, 8));
        round1East.add(new Team("UCF", 0.606, 9));
        round1East.add(new Team("Mississippi St", 0.609, 5));
        round1East.add(new Team("Liberty", 0.549, 12));
        round1East.add(new Team("Virginia Tech", 0.605, 4));
        round1East.add(new Team("Saint Louis", 0.547, 13));
        round1East.add(new Team("Maryland", 0.602, 6));
        round1East.add(new Team("Belmont", 0.579, 11));
        round1East.add(new Team("LSU", 0.626, 3));
        round1East.add(new Team("Yale", 0.572, 14));
        round1East.add(new Team("Louisville", 0.594, 7));
        round1East.add(new Team("Minnesota", 0.578, 10));
        round1East.add(new Team("Michigan St", 0.641, 2));
        round1East.add(new Team("Bradley", 0.504, 15));

    }

    private void randomize() {
        if(round2East.isEmpty()) {
            for(int i = 0; i < round1East.size(); i += 2) {
                int x = (int)(Math.random() * 2);
                if(x == 0) {
                    round2East.add(round1East.get(i));
                } else {
                    round2East.add(round1East.get(i++));
                }
            }
     }

}

Body:
This is because when you put the ArrayList outside of the main method, you declare it as an "instance variable". Since the main method is static, you cannot reference a "non-static (instance)" variable in a static method.

The fix would be to just declare it outside of the main method, but make the variable static as well.

static ArrayList<Team> round1East = new ArrayList<Team>();


问:
Is there a way to "allow" a perl RegEx to ignore 1 character at a time when trying to match?
Basically, I have several files, each one with several lines of text, and I'm interested in finding a specific sequence of 7 letters and count how many time they appear per file using some basic regular expressions on Perl(v5.24.1).

So far no problem, but the "tricky" part is that if one of those seven letters doesn't match my pattern, I would like to count it as well (as long as it's just one).

Patterns I'm looking for:
'CCCAGGT', 'CCCAGTT', 'CCCAGCT', and 'CCCAGAT'.

Examples of non-matching text that I would like to count:

```<code>line1 - AGGCTCAGGAG'CCCATGT'GGGCGGACCCAT --> Count as 'CCCAGGT'
line2 - CGGCTCAGGAG'CCCGGGT'GGGCGGTCCCAT --> Count as 'CCCAGGT'
</code>```

I'm including a piece code (at the bottom) to further explain what I'm searching and what I've thought so far, but it has to be a better way of doing this.

So, do you know if it's possible to "tell" Perl that I can tolerate 1 mismatch in the sequence when using the =~ m/ operator? OR another function to use?

Thanks a lot for your help!

  if ($elements[0] =~ m/CCCAGGT/){
    $mutg = $mutg + $elements[1];
  }
  elsif ($elements[0] =~ m/CCCAGTT/){
    $mutt = $mutt + $elements[1];
  }
  elsif ($elements[0] =~ m/CCCAGAT/){
    $muta = $muta + $elements[1];
  }
  elsif ($elements[0] =~ m/CCCAGCT/){
    $mutc = $mutc + $elements[1];
  }
  else {
    if ($elements[0] =~ m/.CCAGGT/){
      $mutg = $mutg + $elements[1];
    }
    elsif ($elements[0] =~ m/.CCAGTT/){
    $mutt = $mutt + $elements[1];
    }
    elsif ($elements[0] =~ m/.CCAGAT/){
      $muta = $muta + $elements[1];
    }
    elsif ($elements[0] =~ m/.CCAGCT/){
      $mutc = $mutc + $elements[1];
    }
    else {
      [[AGAIN BUT WITH THE "." IN THE SECOND POSITION AND SO ON]]
    }
  }
```

答:
It may be possible with regex but it will be overly complicated, regex is not designed for fuzzy matching. You may consider Text::Fuzzy. The normal interface would require first somehow turning your lines into possible sequences to consider.

```<code>use strict;
use warnings;
use Text::Fuzzy;
my $fuzzy = Text::Fuzzy->new('CCCAGGT', max => 1);
my @matches = $fuzzy->nearestv(\@possible);
</code>```

The fuzzy_index function may also be useful for searching a larger text string similar to regex, but only returns the closest match within the string.

Body:
Basically, I have several files, each one with several lines of text, and I'm interested in finding a specific sequence of 7 letters and count how many time they appear per file using some basic regular expressions on Perl(v5.24.1).

So far no problem, but the "tricky" part is that if one of those seven letters doesn't match my pattern, I would like to count it as well (as long as it's just one).

Patterns I'm looking for:
'CCCAGGT', 'CCCAGTT', 'CCCAGCT', and 'CCCAGAT'.

Examples of non-matching text that I would like to count:

line1 - AGGCTCAGGAG'CCCATGT'GGGCGGACCCAT --> Count as 'CCCAGGT'
line2 - CGGCTCAGGAG'CCCGGGT'GGGCGGTCCCAT --> Count as 'CCCAGGT'

I'm including a piece code (at the bottom) to further explain what I'm searching and what I've thought so far, but it has to be a better way of doing this.

So, do you know if it's possible to "tell" Perl that I can tolerate 1 mismatch in the sequence when using the =~ m/ operator? OR another function to use?

Thanks a lot for your help!

  if ($elements[0] =~ m/CCCAGGT/){
    $mutg = $mutg + $elements[1];
  }
  elsif ($elements[0] =~ m/CCCAGTT/){
    $mutt = $mutt + $elements[1];
  }
  elsif ($elements[0] =~ m/CCCAGAT/){
    $muta = $muta + $elements[1];
  }
  elsif ($elements[0] =~ m/CCCAGCT/){
    $mutc = $mutc + $elements[1];
  }
  else {
    if ($elements[0] =~ m/.CCAGGT/){
      $mutg = $mutg + $elements[1];
    }
    elsif ($elements[0] =~ m/.CCAGTT/){
    $mutt = $mutt + $elements[1];
    }
    elsif ($elements[0] =~ m/.CCAGAT/){
      $muta = $muta + $elements[1];
    }
    elsif ($elements[0] =~ m/.CCAGCT/){
      $mutc = $mutc + $elements[1];
    }
    else {
      [[AGAIN BUT WITH THE "." IN THE SECOND POSITION AND SO ON]]
    }
  }

Body:
It may be possible with regex but it will be overly complicated, regex is not designed for fuzzy matching. You may consider Text::Fuzzy. The normal interface would require first somehow turning your lines into possible sequences to consider.

use strict;
use warnings;
use Text::Fuzzy;
my $fuzzy = Text::Fuzzy->new('CCCAGGT', max => 1);
my @matches = $fuzzy->nearestv(\@possible);

The fuzzy_index function may also be useful for searching a larger text string similar to regex, but only returns the closest match within the string.


问:
Google Datastore returns incomplete data via official client library for nodejs
Here some information about context of the problem I facing:

we have a semi-structured (JSON from node.js backend) data in datastore.

after saving an entity,

and getting a list of entities about them soon and even a while later,

returned data does not have one indexed property

I can find the entity by that property value.

I use Google Datastore via node.js client library. @google-cloud/datastore: "^2.0.0".

How it can be possible? I understood when due to eventual consistency some updates can be incompletely written etc. But when I getting same inconsistency - lack of whole property of entity saved e. g. hour ago?
I gone through scenario multiple times for same kind multiple times.
I do not have such issues with other kinds or other properties of that kind.

How I can avoid this type of issues with Google Datastore?

答:
Answer for anyone who may encounter with such issue.
We mostly do not use any DTO (data-transfer objects) or any other wrappers for most of our kinds in this project, but for this one a DTO has been used, mostly to be sure the result objects have default values for properties omitted/absent in entity which usually happens for entities created by older version of code.

After reviewing my own code more carefully, I found a piece of code which is out of sync with other related pieces of code - there was no a line to copy this property from entity to the DTO object.

Side note: Actually all this situation remind me a story or meme about a guy who claimed he found a bug in compiler just because he was not able to find a mistake he made in his code.


问:
Using Cython for game development?
How practical would it be to use Cython as the primary programming language for a game?

I am a experienced Python programmer and I absolutely love it, but I'm admittedly a novice when it comes to game programming specifically. I know that typically Python is considered too slow to do any serious game programming, which is why Cython is interesting to me. With Cython I can use a Python-like language with the speed of C.

I understand that I'll probably need to learn a bit of C/C++ anyway, but it seems like Cython would speed up development time quite a bit in comparison.

So, is it practical? And would I still be able to use C/C++ libraries like OpenGL, OpenAL, and Bullet Physics?

答:
at this date (12th of April 2011) unixmab83 is wrong.

Cython doesn't forbid the use of threads, you just needs to use the no_gil special statements.

Beside the bindins of c++ is now functional in cython.

We do use it for something which is close to gamedev. So while I cannot be final on this, cython is a valid candidate.

ContentLicense: CC BY-SA 2.5

Title: Using Cython for game development?

ContentLicense: CC BY-SA 3.0


问:
horizontal scrollbar on top and bottom of table
I've a very large table on my page. So I decided to put a horizontal scrollbar on the bottom of the table. But I would like this scrollbar to be also on top on the table.

What I have in the template is this:

```<code><div style="overflow:auto; width:100%; height:130%">
<table id="data" style="width:100%">...</table>
</div>
</code>```

Is this possible to do in HTML and CSS only?

答:
StanleyH's answer was excellent, but it had one unfortunate bug: clicking the shaded area of the scrollbar no longer jumps to the selection you click.  Instead, what you get is a very small and somewhat annoying increment in the position of the scrollbar.

Tested: 4 versions of Firefox (100% affected), 4 versions of Chrome (50% affected).

Here's my jsfiddle. You can get around this with by having an on/off (true/false) var that allows only one onScroll() event to trigger at a time:

```<code>var scrolling = false;
$(".wrapper1").scroll(function(){
    if(scrolling) {
      scrolling = false;
      return true;
    }
    scrolling = true;
    $(".wrapper2")
        .scrollLeft($(".wrapper1").scrollLeft());
});
$(".wrapper2").scroll(function(){
    if(scrolling) {
      scrolling = false;
      return true;
    }
      scrolling = true;
    $(".wrapper1")
        .scrollLeft($(".wrapper2").scrollLeft());
});
</code>```

Problem Behavior With Accepted Answer :

Actually Desired Behavior :

So, just why does this happen? If you run through the code, you'll see that wrapper1 calls wrapper2's scrollLeft, and wrapper2 calls wrapper1's scrollLeft, and repeat this infinitely, so, we have an infinite loop problem.  Or, rather: the continued scrolling of the user conflicts with wrapperx's call of the scrolling, an event conflict occurs, and the end result is no jumping in the scrollbars.

Hope this helps someone else out!

Body:
I've a very large table on my page. So I decided to put a horizontal scrollbar on the bottom of the table. But I would like this scrollbar to be also on top on the table.

What I have in the template is this:

...

Is this possible to do in HTML and CSS only?

Body:
StanleyH's answer was excellent, but it had one unfortunate bug: clicking the shaded area of the scrollbar no longer jumps to the selection you click.  Instead, what you get is a very small and somewhat annoying increment in the position of the scrollbar.

Tested: 4 versions of Firefox (100% affected), 4 versions of Chrome (50% affected).

Here's my jsfiddle. You can get around this with by having an on/off (true/false) var that allows only one onScroll() event to trigger at a time:

var scrolling = false;
$(".wrapper1").scroll(function(){
    if(scrolling) {
      scrolling = false;
      return true;
    }
    scrolling = true;
    $(".wrapper2")
        .scrollLeft($(".wrapper1").scrollLeft());
});
$(".wrapper2").scroll(function(){
    if(scrolling) {
      scrolling = false;
      return true;
    }
      scrolling = true;
    $(".wrapper1")
        .scrollLeft($(".wrapper2").scrollLeft());
});

Problem Behavior With Accepted Answer :

Actually Desired Behavior :

So, just why does this happen? If you run through the code, you'll see that wrapper1 calls wrapper2's scrollLeft, and wrapper2 calls wrapper1's scrollLeft, and repeat this infinitely, so, we have an infinite loop problem.  Or, rather: the continued scrolling of the user conflicts with wrapperx's call of the scrolling, an event conflict occurs, and the end result is no jumping in the scrollbars.

Hope this helps someone else out!


问:
Webpack cannot find module mini-css-extract-plugin
I'm using Webpack ^4.26.0. I've used "extract-text-webpack-plugin" with Webpack 3 before to use for CSS. But I've read that that plugin isn't working anymore on Webpack 4. And "extract-text-webpack-plugin" suggest to use the "mini-css-extract-plugin"-plugin.

I've installed the plugin via the command:

npm install --save-dev mini-css-extract-plugin

and required the plugin in webpackconfig, also added it to my rules and plugins :

```<code>// webpack.config.js
const webpack = require("webpack");
const path = require("path");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

const config = {
  context: path.resolve(__dirname),
  entry: "./index.js",
  devServer: {
    contentBase: "./dist"
  },
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "bundle.js"
  },
  resolve: {
    // Add '.ts' and '.tsx' as resolvable extensions.
    extensions: [".ts", ".tsx", ".js", ".json"]
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        include: path.resolve(__dirname),
        use: [
          {
            loader: "babel-loader",
            options: {
              presets: [["@babel/env", { modules: false }], "@babel/react"]
            }
          }
        ]
      },
      {
        test: /\.tsx?$/,
        loader: "awesome-typescript-loader"
      },
      {
         test: /\.css$/,
         use: [MiniCssExtractPlugin.loader, "css-loader"]
       }
    ],
    noParse: [/aws-sdk/]
  },
  plugins: [
    new webpack.DefinePlugin({
      "process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV),
      "process.env.STATIC_PORT": JSON.stringify(process.env.STATIC_PORT),
      VERSION: JSON.stringify(require("./package.json").version)
    }),
    new MiniCssExtractPlugin({
      filename: 'bundle.css'
    }),
    new CopyWebpackPlugin([{ from: "./cb_icons", to: "cb_icons" }])
  ],
  node: { fs: "empty" },
  externals: [{ "./cptable": "var cptable" }, { "./jszip": "jszip" }]
};

module.exports = config;
</code>```

However I'm getting the following error:

It is installed in my node_modules:

components/searchkit/node_modules/mini-css-extract-plugin

My folder structure:

答:
By default, Heroku will install all dependencies listed in package.json under dependencies and devDependencies. After running the installation and build steps Heroku will strip out the packages declared under devDependencies before deploying the application.

Source: https://devcenter.heroku.com/articles/nodejs-support

If you need the devDependencies during runtime, you can either uninstall and install it as a runtime dependency OR set heroku environment variables to withold from pruning your devDependencies.

In command line, it would look like this:

```<code>  heroku config:set NPM_CONFIG_PRODUCTION=false
</code>```

Body:
I'm using Webpack ^4.26.0. I've used "extract-text-webpack-plugin" with Webpack 3 before to use for CSS. But I've read that that plugin isn't working anymore on Webpack 4. And "extract-text-webpack-plugin" suggest to use the "mini-css-extract-plugin"-plugin.

I've installed the plugin via the command:

npm install --save-dev mini-css-extract-plugin

and required the plugin in webpackconfig, also added it to my rules and plugins :

// webpack.config.js
const webpack = require("webpack");
const path = require("path");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

const config = {
  context: path.resolve(__dirname),
  entry: "./index.js",
  devServer: {
    contentBase: "./dist"
  },
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "bundle.js"
  },
  resolve: {
    // Add '.ts' and '.tsx' as resolvable extensions.
    extensions: [".ts", ".tsx", ".js", ".json"]
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        include: path.resolve(__dirname),
        use: [
          {
            loader: "babel-loader",
            options: {
              presets: [["@babel/env", { modules: false }], "@babel/react"]
            }
          }
        ]
      },
      {
        test: /\.tsx?$/,
        loader: "awesome-typescript-loader"
      },
      {
         test: /\.css$/,
         use: [MiniCssExtractPlugin.loader, "css-loader"]
       }
    ],
    noParse: [/aws-sdk/]
  },
  plugins: [
    new webpack.DefinePlugin({
      "process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV),
      "process.env.STATIC_PORT": JSON.stringify(process.env.STATIC_PORT),
      VERSION: JSON.stringify(require("./package.json").version)
    }),
    new MiniCssExtractPlugin({
      filename: 'bundle.css'
    }),
    new CopyWebpackPlugin([{ from: "./cb_icons", to: "cb_icons" }])
  ],
  node: { fs: "empty" },
  externals: [{ "./cptable": "var cptable" }, { "./jszip": "jszip" }]
};

module.exports = config;

However I'm getting the following error:

It is installed in my node_modules:

components/searchkit/node_modules/mini-css-extract-plugin

My folder structure:

Body:
By default, Heroku will install all dependencies listed in package.json under dependencies and devDependencies. After running the installation and build steps Heroku will strip out the packages declared under devDependencies before deploying the application.

Source: https://devcenter.heroku.com/articles/nodejs-support

If you need the devDependencies during runtime, you can either uninstall and install it as a runtime dependency OR set heroku environment variables to withold from pruning your devDependencies.

In command line, it would look like this:

  heroku config:set NPM_CONFIG_PRODUCTION=false


问:
How does toArray() function make sure that a list of long type can't be converted to array of Integer
```<code>    public <T> T[] toArray(T[] a) {
            int size = size();
            if (a.length < size)
                return Arrays.copyOf(this.a, size,
                                     (Class<? extends T[]>) a.getClass());
            System.arraycopy(this.a, 0, a, 0, size);
            if (a.length > size)
                a[size] = null;
            return a;
    }

    public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
        @SuppressWarnings("unchecked")
        T[] copy = ((Object)newType == (Object)Object[].class)
            ? (T[]) new Object[newLength]
            : (T[]) Array.newInstance(newType.getComponentType(), newLength);
        System.arraycopy(original, 0, copy, 0,
                         Math.min(original.length, newLength));
        return copy;
    }
</code>```

Not getting why the third parameter type for function copyOf() is Class<? extends T[]> and not simply Class<T[]> . What make's the difference between them here. Is this constraint making sure that one type of list can't be converted to other type of array.

答:
The wildcard allows you to pass in non-literal classes.

For example:

```<code>copyOf(new String[0], 0, String[].class);
copyOf(new String[0], 0, new String[0].getClass());
</code>```

Both of these are valid when the third parameter type is Class<? extends T[]>; but only the first is valid for Class<T[]>.

Ideone demo

Practically, this means that you are able to invoke copyOf for types you don't know at compile time; instead, you are obtaining the class from an existing array.

Body:
    public <T> T[] toArray(T[] a) {
            int size = size();
            if (a.length < size)
                return Arrays.copyOf(this.a, size,
                                     (Class<? extends T[]>) a.getClass());
            System.arraycopy(this.a, 0, a, 0, size);
            if (a.length > size)
                a[size] = null;
            return a;
    }

    public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
        @SuppressWarnings("unchecked")
        T[] copy = ((Object)newType == (Object)Object[].class)
            ? (T[]) new Object[newLength]
            : (T[]) Array.newInstance(newType.getComponentType(), newLength);
        System.arraycopy(original, 0, copy, 0,
                         Math.min(original.length, newLength));
        return copy;
    }

Not getting why the third parameter type for function copyOf() is Class<? extends T[]> and not simply Class<T[]> . What make's the difference between them here. Is this constraint making sure that one type of list can't be converted to other type of array.

Body:
The wildcard allows you to pass in non-literal classes.

For example:

copyOf(new String[0], 0, String[].class);
copyOf(new String[0], 0, new String[0].getClass());

Both of these are valid when the third parameter type is Class<? extends T[]>; but only the first is valid for Class<T[]>.

Ideone demo

Practically, this means that you are able to invoke copyOf for types you don't know at compile time; instead, you are obtaining the class from an existing array.


问:
Find most significant bit (left-most) that is set in a bit array
I have a bit array implementation where the 0th index is the MSB of the first byte in an array, the 8th index is the MSB of the second byte, etc...

What's a fast way to find the first bit that is set in this bit array? All the related solutions I have looked up find the first least significant bit, but I need the first most significant one. So, given 0x00A1, I want 8 (since it's the 9th bit from the left).

Body:
For java I use this:

static public final int msb(int n) {
    n |= n >>> 1;
    n |= n >>> 2;
    n |= n >>> 4;
    n |= n >>> 8;
    n |= n >>> 16;
    n >>>= 1;
    n += 1;
    return n;
}

And:

static public final int msb_index(int n) {

    final int[] multiply_de_bruijn_bit_position = {
        0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
        31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
    };
    return multiply_de_bruijn_bit_position[(msb(n) * 0x077CB531) >>> 27];
}


问:
Confused about junction table implementation
First things first, I'm not a programmer and this is the first time I've worked with database creation and this implementation is part of a need that was identified during the internship that I'm currently doing.

I'm currently developing an MS Access database that will be used to manage work contracts with sports trainers. The implementation aims to migrate a series of data that's always been stored on flat Excel databases.

Right now I'm having an issue with how to implement a functionality that is needed in order to do some searches.

Table A has about 200 trainers, each is identified with their own ID number and their name, which is used to manage the relationships with other tables (things like social security, phone numbers, adressess, etc). ID is the key. It's structured with three fields as the example shows.

```<code>[Trainer_ID] [Name]  [Last_Name]
1            Pedro   Pérez
2            María   Gómez
3            Hollman Vivas
</code>```

Table B is a list of the (currently 20) sports we handle contracts for. It has an autonumber field as the key and the name of every sport as short text (using lowercase characters for example only). The list can be updated with more sports depending on demand and trainer enrolment.

```<code>[Sport_ID] [Sport_Name]
a          Soccer
b          Basketball
c          Tennis
</code>```

Finally, Table C stores the specific certifications every trainer has, with the Trainer ID as the key. In the Excel version it has four fields.

```<code>[Trainer_ID] [Sport1] [Sport2]   [Sport3] [Sport4)
1            Soccer
2            Tennis   Soccer
3            Tennis   Basketball Soccer
</code>```

All the data in Table C is scrambled like that.

As you can see, every trainer has one or more certifications that drive to almost endless combinations (like 1abc 2c 3ac). Not to mention, eventually we can have a trainer with five or more certifications and the database isn't designed with that in mind.

I need to find a way to make sense of that data in the environment of the MS Access Database but can't really think of the best way to do it,
considering that due to the sheer amount of information that we handle, we need that the data can be easily updated through mass uploads from .csv files but also updateable through the forms that are used for manual verifications.

It needs to be used in queries to search for specific competences when required.

The first thing I tried was leaving the data as it was (with the four sports fields) but it proved to be a mess to handle.

Then multivalued fields, which were scraped after 10 minutes because my research on the subject shows it's a non standard implementation and can't be updated through mass uploads to the base.

Further research drove me to junction tables, but I still haven't figured out how to structure the thing considering most examples on the web are based on two tables, not three.

I was thinking about having the certifications table (Table C) without key fields and just store fragmented information with repeated use of [Trainer_ID], but I'm afraid this lack of normalization can lead to problems in the future.

As I described before, the database should be able to store, display and update the qualifications of every trainer in a consistent way no matter how many certifications they have.

答:
There is nothing wrong with your idea of using the Certifications table with repeating Trainer ID's. It isn't redundant data because you would have a Certification_ID in the table as well, to act as the primary key. Your two other fields would be foreign keys from the two other table.

Intermediary(Junction-tables) tables are VERY common in large scale databases that run Multi-Billion dollar corporations, so there should be no worry.

Body:
First things first, I'm not a programmer and this is the first time I've worked with database creation and this implementation is part of a need that was identified during the internship that I'm currently doing.

I'm currently developing an MS Access database that will be used to manage work contracts with sports trainers. The implementation aims to migrate a series of data that's always been stored on flat Excel databases.

Right now I'm having an issue with how to implement a functionality that is needed in order to do some searches.

Table A has about 200 trainers, each is identified with their own ID number and their name, which is used to manage the relationships with other tables (things like social security, phone numbers, adressess, etc). ID is the key. It's structured with three fields as the example shows.

[Trainer_ID] [Name]  [Last_Name]
1            Pedro   Pérez
2            María   Gómez
3            Hollman Vivas

Table B is a list of the (currently 20) sports we handle contracts for. It has an autonumber field as the key and the name of every sport as short text (using lowercase characters for example only). The list can be updated with more sports depending on demand and trainer enrolment.

[Sport_ID] [Sport_Name]
a          Soccer
b          Basketball
c          Tennis

Finally, Table C stores the specific certifications every trainer has, with the Trainer ID as the key. In the Excel version it has four fields.

[Trainer_ID] [Sport1] [Sport2]   [Sport3] [Sport4)
1            Soccer
2            Tennis   Soccer
3            Tennis   Basketball Soccer

All the data in Table C is scrambled like that.

As you can see, every trainer has one or more certifications that drive to almost endless combinations (like 1abc 2c 3ac). Not to mention, eventually we can have a trainer with five or more certifications and the database isn't designed with that in mind.

I need to find a way to make sense of that data in the environment of the MS Access Database but can't really think of the best way to do it,
considering that due to the sheer amount of information that we handle, we need that the data can be easily updated through mass uploads from .csv files but also updateable through the forms that are used for manual verifications.

It needs to be used in queries to search for specific competences when required.

The first thing I tried was leaving the data as it was (with the four sports fields) but it proved to be a mess to handle.

Then multivalued fields, which were scraped after 10 minutes because my research on the subject shows it's a non standard implementation and can't be updated through mass uploads to the base.

Further research drove me to junction tables, but I still haven't figured out how to structure the thing considering most examples on the web are based on two tables, not three.

I was thinking about having the certifications table (Table C) without key fields and just store fragmented information with repeated use of [Trainer_ID], but I'm afraid this lack of normalization can lead to problems in the future.

As I described before, the database should be able to store, display and update the qualifications of every trainer in a consistent way no matter how many certifications they have.


问:
jquery/javascript wait for a function to complete
I have script that performs from DOM manipulation to change the way a div looks. The way the application is, when the page loads, the div is rendered in its original state, the function manipulates the DIV. The problem is it renders the original div before rendering the changed div.

I tried:

```<code>//Make div invisible

//Call function to change the DIV

//Make div visible
</code>```

This does not work

答: If you don't want it showing right away, you should just hide it with CSS (rather than JS). Then you run your change function and display it.</p>

OwnerDisplayName: Bob Smith

Body:
I have script that performs from DOM manipulation to change the way a div looks. The way the application is, when the page loads, the div is rendered in its original state, the function manipulates the DIV. The problem is it renders the original div before rendering the changed div.

I tried:

//Make div invisible

//Call function to change the DIV

//Make div visible

This does not work


问:
Apache stopped working on LAN when on reboot
I set up an Apache server on an old laptop last night (apache2) just to play around on. It worked fine on my laptop, and I could get web pages from it from other computers on my local network. After rebooting the computer, however, the Apache server is not visible on the local network.

nmap TCP connect(), Syn, and ping scans showed that the host was down. An nmap -PN scan, however, showed that the host is up and all 1000 scanned ports are filtered.

PHP, MySQL, and PhpMyAdmin are all still working perfectly on localhost, the only problem I can find is that nothing works on local network.

Thanks in advance for the help.

答:
You havent said on your OS, but, there could be a local firewall, or, a change of IP (it could be DHCP) or DNS hasnt caught up properly with DHCP..

a netstat -a should tell you what ports are being listened on. Check its not just listening on 127.0.0.1


问:
Controlling display of background map in ggmap for custom set of shapefiles
I'm working with a data set corresponding to the extract below:

```<code># Data sourcing -----------------------------------------------------------

# Download an read US state shapefiles
tmp_shps <- tempfile(); tmp_dir <- tempdir()
download.file("http://www2.census.gov/geo/tiger/GENZ2014/shp/cb_2014_us_state_20m.zip",
              tmp_shps)
unzip(tmp_shps, exdir = tmp_dir)

# Libs
require(rgdal); require(ggplot2)

# Read
us_shps <- readOGR(dsn = tmp_dir, layer = "cb_2014_us_state_20m")
# Prepare data set for ggplot2
us_shps_frt <- fortify(us_shps, region = "NAME")
</code>```

From within that data set I subset a custom selection of shapefiles, like in the example below:

```<code>map_shps <- ggplot(data = us_shps_frt[grep("South", us_shps_frt$id),]) +
  geom_polygon(aes(x = long, y = lat, group = group,
                                fill = id)) +
  coord_equal() +
  ggtitle("Odd Map") +
  theme_map()
</code>```

Given the odd selection of shapefiles above I would like to fit a background map that would neatly overlay with the maximum extent of the polygons. Initially I tried to achieve this in a following manner:

```<code># Get box for the map
bbox <- make_bbox(lon = long, lat = lat,
                  data = us_shps_frt[grep("South", us_shps_frt$id),],
                  f = 0.5)
map_backgr  <- get_map(bbox, maptype = "roadmap", zoom = 5,
                       source = "google", scale = 2, messaging = TRUE)
map_backgr <- ggmap(map_backgr, extent = "normal", maprange = FALSE)
</code>```

and then generating the map:

```<code>map_backgr +
  geom_polygon(data = us_shps_frt[grep("South", us_shps_frt$id),],
               aes(x = long, y = lat, group = group,
                   fill = id)) +
  coord_equal() +
  ggtitle("Odd Map with Background") +
  theme_map()
</code>```

The produced graphic does not fit the polygons well:

I'm interested in adjusting the background map so it matches the polygons exactly. In particular I would like to crop/shorten the background by the red lines marked in the picture below:

答:
Idea 1: Go with Stamen, they support oddly-shaped maps:

```<code>dat <- us_shps_frt[grep("South", us_shps_frt$id),]

ggmap(get_stamenmap(c(min(dat$long), min(dat$lat),
                      max(dat$long), max(dat$lat)),
                    zoom = 6)) +
geom_polygon(data = dat,
             aes(x = long, y = lat, group = group,
                 fill = id)) +
ggtitle("Odd Map with Background")
</code>```

Idea 2: Cut your Google Maps map:

```<code>dat <- us_shps_frt[grep("South", us_shps_frt$id),]

ggmap(get_map(c(min(dat$long), min(dat$lat), max(dat$long), max(dat$lat)))) +
geom_polygon(data = dat,
             aes(x = long, y = lat, group = group,
                 fill = id)) +
ggtitle("Odd Map with Background") +
coord_map(xlim = c(min(dat$long), max(dat$long)),
          ylim = c(min(dat$lat), max(dat$lat)))
</code>```

But this one is still a bit off and I assume you need to play with the projection = "..." and parameters = "..." of coord_map().

Body:
I'm working with a data set corresponding to the extract below:

# Data sourcing -----------------------------------------------------------

# Download an read US state shapefiles
tmp_shps <- tempfile(); tmp_dir <- tempdir()
download.file("http://www2.census.gov/geo/tiger/GENZ2014/shp/cb_2014_us_state_20m.zip",
              tmp_shps)
unzip(tmp_shps, exdir = tmp_dir)

# Libs
require(rgdal); require(ggplot2)

# Read
us_shps <- readOGR(dsn = tmp_dir, layer = "cb_2014_us_state_20m")
# Prepare data set for ggplot2
us_shps_frt <- fortify(us_shps, region = "NAME")

From within that data set I subset a custom selection of shapefiles, like in the example below:

map_shps <- ggplot(data = us_shps_frt[grep("South", us_shps_frt$id),]) +
  geom_polygon(aes(x = long, y = lat, group = group,
                                fill = id)) +
  coord_equal() +
  ggtitle("Odd Map") +
  theme_map()

Given the odd selection of shapefiles above I would like to fit a background map that would neatly overlay with the maximum extent of the polygons. Initially I tried to achieve this in a following manner:

# Get box for the map
bbox <- make_bbox(lon = long, lat = lat,
                  data = us_shps_frt[grep("South", us_shps_frt$id),],
                  f = 0.5)
map_backgr  <- get_map(bbox, maptype = "roadmap", zoom = 5,
                       source = "google", scale = 2, messaging = TRUE)
map_backgr <- ggmap(map_backgr, extent = "normal", maprange = FALSE)

and then generating the map:

map_backgr +
  geom_polygon(data = us_shps_frt[grep("South", us_shps_frt$id),],
               aes(x = long, y = lat, group = group,
                   fill = id)) +
  coord_equal() +
  ggtitle("Odd Map with Background") +
  theme_map()

The produced graphic does not fit the polygons well:

I'm interested in adjusting the background map so it matches the polygons exactly. In particular I would like to crop/shorten the background by the red lines marked in the picture below:

Body:
Idea 1: Go with Stamen, they support oddly-shaped maps:

dat <- us_shps_frt[grep("South", us_shps_frt$id),]

ggmap(get_stamenmap(c(min(dat$long), min(dat$lat),
                      max(dat$long), max(dat$lat)),
                    zoom = 6)) +
geom_polygon(data = dat,
             aes(x = long, y = lat, group = group,
                 fill = id)) +
ggtitle("Odd Map with Background")

Idea 2: Cut your Google Maps map:

dat <- us_shps_frt[grep("South", us_shps_frt$id),]

ggmap(get_map(c(min(dat$long), min(dat$lat), max(dat$long), max(dat$lat)))) +
geom_polygon(data = dat,
             aes(x = long, y = lat, group = group,
                 fill = id)) +
ggtitle("Odd Map with Background") +
coord_map(xlim = c(min(dat$long), max(dat$long)),
          ylim = c(min(dat$lat), max(dat$lat)))

But this one is still a bit off and I assume you need to play with the projection = "..." and parameters = "..." of coord_map().


问:
Joining to tables/columns specified by query results
Maybe I am not searching the correct terms, because I found no other questions/answers re: this.

Is it possible to join to a table/column designated in the results from a query? I have a query with objects that are designated in other tables, and I can create a table of all tables/columns.

Example

Table_1

```<code>        Col1        Col2        Col3
        999         HTML        001
        998         HTML        002
        997         CSS         001
        996         CSS         002
        995         CSS         003
        994         C#          001
</code>```

Table_2 - Table of Tables/Columns

```<code>        Col1        Col2        Col3
        HTML        T_HTML      C_HTML_ID
        CSS         T_CSS       C_CSS_ID
        C#          T_C#        C_C#_ID
</code>```

Table_3 - T_HTML (but could be T_CSS, T_C#, etc.)

```<code>        C_HTML_ID        Col2
        001              blah
        002              blah
        003              blah

</code>```

Is it then possible to somehow do something like this?

```<code>SELECT
    *
FROM
    table_1
    JOIN table_2
        ON table_2.Col1 = table_1.Col2
    JOIN <<table_2.Col2>>
        ON <<table_2.Col2>>.<<Col3>> = table_1.Col3
</code>```

答:
you can join the table_1 and table_3 direct.</p>

```<code>select *
from tab1 t1
join tab3 t2 on t1.Col3 = t2.C_HTML_ID
where t1.Col2 = 'HTML';
</code>```

Body:
Maybe I am not searching the correct terms, because I found no other questions/answers re: this.

Is it possible to join to a table/column designated in the results from a query? I have a query with objects that are designated in other tables, and I can create a table of all tables/columns.

Example

Table_1

        Col1        Col2        Col3
        999         HTML        001
        998         HTML        002
        997         CSS         001
        996         CSS         002
        995         CSS         003
        994         C#          001

Table_2 - Table of Tables/Columns

        Col1        Col2        Col3
        HTML        T_HTML      C_HTML_ID
        CSS         T_CSS       C_CSS_ID
        C#          T_C#        C_C#_ID

Table_3 - T_HTML (but could be T_CSS, T_C#, etc.)

        C_HTML_ID        Col2
        001              blah
        002              blah
        003              blah

Is it then possible to somehow do something like this?

SELECT
    *
FROM
    table_1
    JOIN table_2
        ON table_2.Col1 = table_1.Col2
    JOIN <<table_2.Col2>>
        ON <<table_2.Col2>>.<<Col3>> = table_1.Col3

Body:
you can join the table_1 and table_3 direct.

select *
from tab1 t1
join tab3 t2 on t1.Col3 = t2.C_HTML_ID
where t1.Col2 = 'HTML';


问:
Binding and Using ngModel with Multi-nested JSON Object
I'm trying to bind and use ngModel with a JSON object that is double nested. This is what I'm trying to use: (Calendar from PrimeNG)

```<code><div *ngFor="let field of fields;">
    <div *ngFor="let player of field.players; let i = index;">
        PlayerID: {{player.playerid}}
        <div *ngFor="let goal of player.goals; let j = index;">
            {{goal.timestamp}} <p-calendar [showTime]="true" dateFormat="yy-mm-dd" timeFormat="hh:mm" required [(ngModel)]="field.players[i].goals[j].timestamp"></p-calendar></span><br>
        </div>
    </div>
</div>
</code>```

However, only the first date value in the array is binded correctly, the others do not work at all.

答:
Try to use index for the fields as well.

```<code><div *ngFor="let field of fields; let fi = index;">
    <div *ngFor="let player of field.players; let i = index;">
        PlayerID: {{player.playerid}}
        <div *ngFor="let goal of player.goals; let j = index;">
            {{goal.timestamp}} <p-calendar [showTime]="true" dateFormat="yy-mm-dd" timeFormat="hh:mm" required [(ngModel)]="fields[fi].players[i].goals[j].timestamp"></p-calendar><br>
        </div>
    </div>
</div>
</code>```

Body:
I'm trying to bind and use ngModel with a JSON object that is double nested. This is what I'm trying to use: (Calendar from PrimeNG)

        PlayerID: {{player.playerid}}

            {{goal.timestamp}}

However, only the first date value in the array is binded correctly, the others do not work at all.

Body:
Try to use index for the fields as well.

<div *ngFor="let field of fields; let fi = index;">
    <div *ngFor="let player of field.players; let i = index;">
        PlayerID: {{player.playerid}}
        <div *ngFor="let goal of player.goals; let j = index;">
            {{goal.timestamp}} <p-calendar [showTime]="true" dateFormat="yy-mm-dd" timeFormat="hh:mm" required [(ngModel)]="fields[fi].players[i].goals[j].timestamp"></p-calendar><br>
        </div>
    </div>
</div>


问:
How to create alert when two fields contain data
I have two fields (A and B) on an application that cannot both contain data at the same time.  I would like for when someone has filled A, and then starts to fill B (and/or vice versa), an error message is thrown that tells them both fields cannot contain data.

I tried using javascript to throw an error onchange if both fields do not equal null, but haven't had success. I know this is an unsophisticated way to meet my intentions as well.

```<code>  <script>
//set IDs of selectors to use
var HighSchoolID = "ctl00$mainContent$CreateAccountUserControl$CreateUserControl$ProspectForm$datatel_highschoolacademichistoryd4d7c0cb_3dfc_451f_b497_62f37d538e48datatel_highschoolid$criteriaSchoolName"
var UnlistedSchoolID = "ctl00$mainContent$CreateAccountUserControl$CreateUserControl$ProspectForm$datatel_highschoolacademichistoryd4d7c0cb_3dfc_451f_b497_62f37d538e48datatel_unlistedschoolinfo$datatel_unlistedschoolinfo"
//attach OnChange event listener to fields
var HighSchool = document.getElemntByID(HighSchoolID);
var UnlistedSchool = document.getElementByID(UnlistedSchoolID);

  confirmHighSchool.addEventListener("click",CheckUnlisted);

  function CheckUnlisted(){
      if(HighSchool != null && UnlistedSchool != null){
          alert(ERROR);
      }
  }
  </script>
</code>```

I would like for an error message to pop up when someone begins (or finishes, either work) filling out the second field.

Edit* Apologies, newbie on stackoverflow, more code included.  The two fields come delivered as part of our CRM, and it is required that one or the other is filled out.

答:
There are several mistakes.
getElemntByID is not a function,
neither is getElementByID

The correct function is getElementById

Despite this, you need to check the value of your inputs
not the references of the inputs.

As you stated you're new to javascript, I recommend you this link too:
  w3schools examples about forms/inputs and validation

checkout this fiddle:

var highSchool = document.getElementById("highSchool");
var unlistedSchool = document.getElementById("unlistedSchool");
var confirmHighSchool = document.getElementById('confirmHighSchool');

confirmHighSchool.addEventListener("click", checkUnlisted);

function checkUnlisted() {
      if(highSchool.value && unlistedSchool.value){
          alert("ERROR MESSAGE TO BE ADDED");
      }
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code>
<label for"highSchool" >HighSchool</label>
<input type="text" id="highSchool">
<br>
<label for"unlistedSchool" >UnlistedSchool</label>
<input type="text" id="unlistedSchool">
<br>
<button id="confirmHighSchool">Confirm Highschool</button>
 </code>```

Body:
I have two fields (A and B) on an application that cannot both contain data at the same time.  I would like for when someone has filled A, and then starts to fill B (and/or vice versa), an error message is thrown that tells them both fields cannot contain data.

I tried using javascript to throw an error onchange if both fields do not equal null, but haven't had success. I know this is an unsophisticated way to meet my intentions as well.

I would like for an error message to pop up when someone begins (or finishes, either work) filling out the second field.

Edit* Apologies, newbie on stackoverflow, more code included.  The two fields come delivered as part of our CRM, and it is required that one or the other is filled out.

Body:
There are several mistakes.
getElemntByID is not a function,
neither is getElementByID

The correct function is getElementById

Despite this, you need to check the value of your inputs
not the references of the inputs.

As you stated you're new to javascript, I recommend you this link too:
  w3schools examples about forms/inputs and validation

checkout this fiddle:

var highSchool = document.getElementById("highSchool");
var unlistedSchool = document.getElementById("unlistedSchool");
var confirmHighSchool = document.getElementById('confirmHighSchool');

confirmHighSchool.addEventListener("click", checkUnlisted);

function checkUnlisted() {
      if(highSchool.value && unlistedSchool.value){
          alert("ERROR MESSAGE TO BE ADDED");
      }
}

<label for"highSchool" >HighSchool</label>
<input type="text" id="highSchool">
<br>
<label for"unlistedSchool" >UnlistedSchool</label>
<input type="text" id="unlistedSchool">
<br>
<button id="confirmHighSchool">Confirm Highschool</button>


问:
Understanding a shuffling function
I found on this thread the following function to shuffle any kind of data types:

```<code>#define NELEMS(x)  (sizeof(x) / sizeof(x[0]))

/* arrange the N elements of ARRAY in random order.
 * Only effective if N is much smaller than RAND_MAX;
 * if this may not be the case, use a better random
 * number generator. */
static void shuffle(void *array, size_t n, size_t size) {
    char tmp[size];
    char *arr = array;
    size_t stride = size * sizeof(char);

    if (n > 1) {
        size_t i;
        for (i = 0; i < n - 1; ++i) {
            size_t rnd = (size_t) rand();
            size_t j = i + rnd / (RAND_MAX / (n - i) + 1);

            memcpy(tmp, arr + j * stride, size);
            memcpy(arr + j * stride, arr + i * stride, size);
            memcpy(arr + i * stride, tmp, size);
        }
    }
}
</code>```

I have been testing and it seems to work fine but I'm having a hard time understanding how and why it works.

How and why doesn't it swap an element of the array directly on array

How come it is possible to assign array to a char*: char *arr = array;

The offset i * stride or j * stride in memcpy functions is bigger than the total size of the array (sizeof(array)). How come the pointer arithmetic works here?

答:
array is of type void*. It is just a pointer with no interpretation, so we don't know what data type it points to. The number of elements is given int n and the size of each in size. It is done this way so the function can be used with arrays of any type.

char *arr = array; is interpreting the void pointer as pointing to a sequence of bytes.

sizeof(array) gives the size of the pointer, not the size of the actual array. The actual size in bytes is i * size.

The shuffle algorithm appears to be the Fisher-Yates Shuffle.

I'll also note that * sizeof(char) is pointless, since sizeof(char) is 1 by definition.

Title: Understanding a shuffling function

Body:
I found on this thread the following function to shuffle any kind of data types:

#define NELEMS(x)  (sizeof(x) / sizeof(x[0]))

/* arrange the N elements of ARRAY in random order.
 * Only effective if N is much smaller than RAND_MAX;
 * if this may not be the case, use a better random
 * number generator. */
static void shuffle(void *array, size_t n, size_t size) {
    char tmp[size];
    char *arr = array;
    size_t stride = size * sizeof(char);

    if (n > 1) {
        size_t i;
        for (i = 0; i < n - 1; ++i) {
            size_t rnd = (size_t) rand();
            size_t j = i + rnd / (RAND_MAX / (n - i) + 1);

            memcpy(tmp, arr + j * stride, size);
            memcpy(arr + j * stride, arr + i * stride, size);
            memcpy(arr + i * stride, tmp, size);
        }
    }
}

I have been testing and it seems to work fine but I'm having a hard time understanding how and why it works.

How and why doesn't it swap an element of the array directly on array

How come it is possible to assign array to a char*: char *arr = array;

The offset i * stride or j * stride in memcpy functions is bigger than the total size of the array (sizeof(array)). How come the pointer arithmetic works here?


问:
Working with GitHub and Cloud
I can't find a way to work with github and a cloud server that satisfies me.

The cloud server has an 'app' which is my project and inside this app there are different deployments (git branches).

I also have a github repository with my project.

We are 4 guys working on this project. Each of us has its own branch (and deployment in the cloud).

Every time I want to try some new code, I need to commit, push into the cloud and tell the cloud to deploy it. So at the end of the day, I have a lot of commits in my git log. I would like to push to the github too, so everyone can see my branch update, but I don't want everyone to see all my meaningless commits.

I can solve that problem by doing a rebase to squash them, but next time i push into the cloud i would get in troubles and I would have to force the push. It works, but it feels wrong.

Is squashing the commits, forcing the push into de cloud, and pushing into github the only way to work?

Any ideas?

Thank you a lot. :)

答: If you want to squash your work in progress commits then I see two options. Either do not push your commits to any remote repository while you work, then squash, then push. Or, use two branches. Work all day on the topic branch. Then create a new branch and git merge --squash into the new branch. It works best if you have your own personal fork of your team's github repo. Only merge the  cleaned up, squashed branches into your team's repository and keep the messier stuff in your personal one.</p>

Title: Working with GitHub and Cloud


问:
PDF file showing in iframe on localhost but now showing in web page online in asp.net
I am trying to show two different pdf files in two iframes in same webpage side by side. These pdf files are on web server. I can view these files in iframes during local run on localhost. But when i upload my application online, these PDF files are not visible in iframes.

Two panels with iframes in one div.

```<code>    <div>
        <asp:Panel runat="server" ID="pnlPDF_BKS" Style="height: 90%; width: 48%; float: left; margin-left: 3px; margin-top: 3px;">
            <iframe id="ifmPDF_BKS" runat="server" style="height: 90%; width: 48%; position: absolute; margin-top: 1px; margin-left: 1%; border-style: solid; border-color: #ace1d8; min-width: 600px;"></iframe>
        </asp:Panel>
        <asp:Panel runat="server" ID="pnlPDF_HIN" Style="height: 90%; width: 48%; float: right; margin-right: 2%; margin-top: 3px;">
            <iframe id="ifmPDF_HIN" runat="server" style="height: 90%; width: 48%; position: absolute; margin-top: 1px; margin-left: 1%; border-style: solid; border-color: #ace1d8; min-width: 600px;"></iframe>
        </asp:Panel>
    </div>
</code>```

C# code..

```<code>    string strURL = http://www.somewebsite.com/folder/filename;

    protected void btnBKS_Click(object sender, EventArgs e)
    { pnlPDF_BKS.Visible = true; ifmPDF_BKS.Src = strURL + ".pdf"; }

    protected void btnADD_Click(object sender, EventArgs e)
    { pnlPDF_HIN.Visible = true; ifmPDF_HIN.Src = strURL + "_ADD.pdf"; }
</code>```

I am looking for solution to view PDF files in iframe when running my application online.

答: Maybe it's a security problem. If your online web site is using SSL certificate (HTTPS) then you won't be able to embed an iframe from a source without SSL certificate (HTTP). In that case you should upload those PDF files to a site which is using SSL. For example GoogleDocs.</p>

Body:
I am trying to show two different pdf files in two iframes in same webpage side by side. These pdf files are on web server. I can view these files in iframes during local run on localhost. But when i upload my application online, these PDF files are not visible in iframes.

Two panels with iframes in one div.

        <asp:Panel runat="server" ID="pnlPDF_BKS" Style="height: 90%; width: 48%; float: left; margin-left: 3px; margin-top: 3px;">

        </asp:Panel>
        <asp:Panel runat="server" ID="pnlPDF_HIN" Style="height: 90%; width: 48%; float: right; margin-right: 2%; margin-top: 3px;">

        </asp:Panel>

C# code..

    string strURL = http://www.somewebsite.com/folder/filename;

    protected void btnBKS_Click(object sender, EventArgs e)
    { pnlPDF_BKS.Visible = true; ifmPDF_BKS.Src = strURL + ".pdf"; }

    protected void btnADD_Click(object sender, EventArgs e)
    { pnlPDF_HIN.Visible = true; ifmPDF_HIN.Src = strURL + "_ADD.pdf"; }

I am looking for solution to view PDF files in iframe when running my application online.


问:
Is there a rate limit for the Google OAuth 2.0 API?
I'm considering to use https://www.googleapis.com/auth/userinfo.email to get hold of the user's email address and I've seen numerous examples on how to do this in both Google's own documentation and other guides on the Internet.

However I have not been able to find any information on what the rate limits are, or if there even are any rate limits for this. The Google APIs Console lists courtesy limits for many different APIs, but nothing is mentioned about the OAuth 2.0 API.

Does anyone know if there are any limits, and if so what they are?

答:
This is an old question, so things have likely changed since the original reply, but Google now has "quota restrictions based on risk level" (whatever that means):

To protect users and Google systems from abuse, applications that use
  OAuth and Google Identity have certain quota restrictions based on the
  risk level of the OAuth scopes an app uses. These limits include the
  following:

A new user authorization rate limit that limits how quickly your
  application can get new users.

A total new user cap. To learn more, see the Unverified apps  page.

When an application exceeds the rate
  limit, Error 403: rate_limit_exceeded is displayed to users


问:
OutOfMemoryError in StringBuilder.append() even with more than enough memory
I get an OutOfMemoryError from java.lang.StringBuilder.append even when I set the memory size to 32 GB and reading only a 500 MB file into the builder.</p>

```<code>java.lang.OutOfMemoryError: null
    at java.base/java.lang.AbstractStringBuilder.hugeCapacity(AbstractStringBuilder.java:214) ~[na:na]
    at java.base/java.lang.AbstractStringBuilder.newCapacity(AbstractStringBuilder.java:206) ~[na:na]
    at java.base/java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:173) ~[na:na]
    at java.base/java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:538) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:174) ~[na:na]
    at com.github.loa.indexer.service.index.base64.Base64Encoder.encode(Base64Encoder.java:31) ~[classes/:na]
</code>```

答: This happens because the byte array that the StringBuilder want to allocate would require a larger array size than allowed by the JVM (more than 2147483647 aka Integer.MAX_VALUE elements).</p>

Body:
I get an OutOfMemoryError from java.lang.StringBuilder.append even when I set the memory size to 32 GB and reading only a 500 MB file into the builder.

java.lang.OutOfMemoryError: null
    at java.base/java.lang.AbstractStringBuilder.hugeCapacity(AbstractStringBuilder.java:214) ~[na:na]
    at java.base/java.lang.AbstractStringBuilder.newCapacity(AbstractStringBuilder.java:206) ~[na:na]
    at java.base/java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:173) ~[na:na]
    at java.base/java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:538) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:174) ~[na:na]
    at com.github.loa.indexer.service.index.base64.Base64Encoder.encode(Base64Encoder.java:31) ~[classes/:na]


问:
Retrieve log files from EMR nodes after unknown termination failure?
We have an EMR cluster that failed for an unknown reason.
The last event was Amazon EMR Cluster ... has terminated with errors with a reason of INSTANCE_FAILURE

I am currently trying to diagnose it. I would like to retrieve log files from the EMR nodes (Master, Core, Task) but they are all terminated.

Unfortunately we do not configure EMR to push logs to S3.

Is there any way for me to manually restart the nodes just so that I can retrieve the log files?

Or is it possible to retrieve the EC2 disk without starting up the instance?

答: I don't think there is. The instances are terminated and disks are not saved. Enabling log pushing to S3 is the best method to get the logs for EMR.</p>


问:
How do I query for the size of the newest island in SQL Server?
I would like to query for the size of the last island in a 'gaps and islands' problem in SQL Server (Azure).

Say I have the following table:

```<code>ID date status
500 2019-05-15 1
528 2019-05-16 0
538 2019-05-17 0
550 2019-05-18 1
580 2019-05-19 0
590 2019-05-20 0
</code>```

ID is an identity column
date is a date column
status is 0 or 1.

I want to find out how many days that a status has been set to 0. In this case today is 2019-05-20.

How do I write a query to pull the count of the last status columns being 0 (ie 2)?

答:
You can find the most recent record with status = 1 and use it to count the recent rows with status = 0.

```<code>select
    count(*) as cnt
from yourTable
where [date] > (select max([date]) from yourTable where [status] = 1)
</code>```

Body:
I would like to query for the size of the last island in a 'gaps and islands' problem in SQL Server (Azure).

Say I have the following table:

ID date status
500 2019-05-15 1
528 2019-05-16 0
538 2019-05-17 0
550 2019-05-18 1
580 2019-05-19 0
590 2019-05-20 0

ID is an identity column
date is a date column
status is 0 or 1.

I want to find out how many days that a status has been set to 0. In this case today is 2019-05-20.

How do I write a query to pull the count of the last status columns being 0 (ie 2)?

Body:
You can find the most recent record with status = 1 and use it to count the recent rows with status = 0.

select
    count(*) as cnt
from yourTable
where [date] > (select max([date]) from yourTable where [status] = 1)


问:
Unexpected exception being raised when trying to convert an input string to float
I want to ask the user to input a value and then print its length, if the input is a string. If the user's input is an integer or a decimal, I want to print "Sorry, integers don't have length" or  "Sorry, floats don't have length", respectively.

I'm making use of exception while trying to convert the input into float or integer.

Here is my code:

```<code>c=input("enter a string: ")

def length(c):
    return len(c)

try:
    float(c)
    if float(c)==int(c):
        print("Sorry integers don't have length")
    else:
        print("Sorry floats don't have length")
except:
    print(length(c))
</code>```

The output results are as follows:

> enter a string: sfkelkrelte
11
> enter a string: 21
Sorry integers don't have length
> enter a string: 21.1
4
```

When I input an integer, the error message is displayed correctly, as the conversion float() is possible. But, in case of a floating point number, the interpreter goes to except block, though it should have executed the try block.

Why does this happen?

答:
Also, why not apply the EAFP principle (What is the EAFP principle in Python?) to the second condition, too?

```<code>s = input("Input: ")
try:
    float(s)
    try:
        int(s)
        print("Sorry, you have entered an integer.")
    except:
        print("Sorry, you have entered a float.")
except:
    print(len(s))
</code>```

I have omitted to check for ValueError as the exception, because you didn't check that in your code, too. However, you should have a look at How to properly ignore exceptions

Body:
I want to ask the user to input a value and then print its length, if the input is a string. If the user's input is an integer or a decimal, I want to print "Sorry, integers don't have length" or  "Sorry, floats don't have length", respectively.

I'm making use of exception while trying to convert the input into float or integer.

Here is my code:

c=input("enter a string: ")

def length(c):
    return len(c)

try:
    float(c)
    if float(c)==int(c):
        print("Sorry integers don't have length")
    else:
        print("Sorry floats don't have length")
except:
    print(length(c))

The output results are as follows:

> enter a string: sfkelkrelte
11
> enter a string: 21
Sorry integers don't have length
> enter a string: 21.1
4

When I input an integer, the error message is displayed correctly, as the conversion float() is possible. But, in case of a floating point number, the interpreter goes to except block, though it should have executed the try block.

Why does this happen?

Body:
Also, why not apply the EAFP principle (What is the EAFP principle in Python?) to the second condition, too?

s = input("Input: ")
try:
    float(s)
    try:
        int(s)
        print("Sorry, you have entered an integer.")
    except:
        print("Sorry, you have entered a float.")
except:
    print(len(s))

I have omitted to check for ValueError as the exception, because you didn't check that in your code, too. However, you should have a look at How to properly ignore exceptions


问:
Plupload: how to remove yellow highlight on delete event
I'm using plupload plugin to create drag and drop form. I changed css to make plupload form look like form from my design. When i click on "delete" btn - thumbnail gets highlighted with yellow color. This is standart behavior of plupload plugin. How can i change this color?

It seems that yellow color is not created by css class. Its created with a js function (if i understand correctly). I tried to look inside plugins code to change this behavior, but can find right spot.

答:
Plupload uses JS to apply inline styles directly on the .plupload_file element upon delete. From my observation, it applies the inline styles background-image:none; background-color: rgb(xxx, yyy, zzz); opacity: 0.xxx; (where x,y,z values change dynamically until the element is removed from the DOM)

To override these styles, the simplest option is to utilize the CSS !important rule. For example, given the following HTML:

```<code><div class="myDiv" style="background-color: red;">
    The inline styles for this div should make it red.
</div>
</code>```

We can fight that with this:

```<code>.myDiv {
   background-color: yellow !important;
}
</code>```

https://css-tricks.com/override-inline-styles-with-css/

The less graceful option would be combing through the source code to find where the yellow highlight is being applied, and modifying it.

Body:
Plupload uses JS to apply inline styles directly on the .plupload_file element upon delete. From my observation, it applies the inline styles background-image:none; background-color: rgb(xxx, yyy, zzz); opacity: 0.xxx; (where x,y,z values change dynamically until the element is removed from the DOM)

To override these styles, the simplest option is to utilize the CSS !important rule. For example, given the following HTML:

<div class="myDiv" style="background-color: red;">
    The inline styles for this div should make it red.
</div>

We can fight that with this:

.myDiv {
   background-color: yellow !important;
}

https://css-tricks.com/override-inline-styles-with-css/

The less graceful option would be combing through the source code to find where the yellow highlight is being applied, and modifying it.


问:
Difference between JIT machine specific operations and static binary
One of the "wins" listed by proponents of JIT is that it can generate machine-specific assembly instructions, based on the CPU of the machine on which the code is running.

How is this different than statically compiling a file? If a statically compiled file is going to run on a given machine, it must at some point generate/use machine code for the CPU of that machine. Why is the JIT version better or faster?

答:
Because what the JIT makes is different for each system.

If we provide a statically compiled binary, it will only work for systems compatible with that system. Java is trying to be platform independent.

So instead of you providing the statically compiled binary (which only works for one) you provide bytecode which will work in any JVM, and allow the JIT to turn it into compiled code if it feels the need to.


问:
How to integrate Flask & Scrapy?
I'm using scrapy to get data and I want to use flask web framework to show the results in webpage. But I don't know how to call the spiders in the flask app. I've tried to use CrawlerProcess to call my spiders,but I got the error like this :

```<code>ValueError
ValueError: signal only works in main thread

Traceback (most recent call last)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/Library/Python/2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/Library/Python/2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/Library/Python/2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/Rabbit/PycharmProjects/Flask_template/FlaskTemplate.py", line 102, in index
process = CrawlerProcess()
File "/Library/Python/2.7/site-packages/scrapy/crawler.py", line 210, in __init__
install_shutdown_handlers(self._signal_shutdown)
File "/Library/Python/2.7/site-packages/scrapy/utils/ossignal.py", line 21, in install_shutdown_handlers
reactor._handleSignals()
File "/Library/Python/2.7/site-packages/twisted/internet/posixbase.py", line 295, in _handleSignals
_SignalReactorMixin._handleSignals(self)
File "/Library/Python/2.7/site-packages/twisted/internet/base.py", line 1154, in _handleSignals
signal.signal(signal.SIGINT, self.sigInt)
ValueError: signal only works in main thread
</code>```

My scrapy code like this:

```<code>class EPGD(Item):

genID = Field()
genID_url = Field()
taxID = Field()
taxID_url = Field()
familyID = Field()
familyID_url = Field()
chromosome = Field()
symbol = Field()
description = Field()

class EPGD_spider(Spider):
    name = "EPGD"
    allowed_domains = ["epgd.biosino.org"]
    term = "man"
    start_urls = ["http://epgd.biosino.org/EPGD/search/textsearch.jsp?textquery="+term+"&submit=Feeling+Lucky"]

db = DB_Con()
collection = db.getcollection(name, term)

def parse(self, response):
    sel = Selector(response)
    sites = sel.xpath('//tr[@class="odd"]|//tr[@class="even"]')
    url_list = []
    base_url = "http://epgd.biosino.org/EPGD"

    for site in sites:
        item = EPGD()
        item['genID'] = map(unicode.strip, site.xpath('td[1]/a/text()').extract())
        item['genID_url'] = base_url+map(unicode.strip, site.xpath('td[1]/a/@href').extract())[0][2:]
        item['taxID'] = map(unicode.strip, site.xpath('td[2]/a/text()').extract())
        item['taxID_url'] = map(unicode.strip, site.xpath('td[2]/a/@href').extract())
        item['familyID'] = map(unicode.strip, site.xpath('td[3]/a/text()').extract())
        item['familyID_url'] = base_url+map(unicode.strip, site.xpath('td[3]/a/@href').extract())[0][2:]
        item['chromosome'] = map(unicode.strip, site.xpath('td[4]/text()').extract())
        item['symbol'] = map(unicode.strip, site.xpath('td[5]/text()').extract())
        item['description'] = map(unicode.strip, site.xpath('td[6]/text()').extract())
        self.collection.update({"genID":item['genID']}, dict(item),  upsert=True)
        yield item

    sel_tmp = Selector(response)
    link = sel_tmp.xpath('//span[@id="quickPage"]')

    for site in link:
        url_list.append(site.xpath('a/@href').extract())

    for i in range(len(url_list[0])):
        if cmp(url_list[0][i], "#") == 0:
            if i+1 < len(url_list[0]):
                print url_list[0][i+1]
                actual_url = "http://epgd.biosino.org/EPGD/search/" + url_list[0][i+1]
                yield Request(actual_url, callback=self.parse)
                break
            else:
                print "The index is out of range!"
</code>```

My flask code like this:

```<code>@app.route('/', methods=['GET', 'POST'])
def index():
    process = CrawlerProcess()
    process.crawl(EPGD_spider)
    return redirect(url_for('details'))

@app.route('/details', methods = ['GET'])
def epgd():
    if request.method == 'GET':
        results = db['EPGD_test'].find()
        json_results= []
        for result in results:
            json_results.append(result)
        return toJson(json_results)
</code>```

How can I call my scrapy spiders when using flask web framework?

答:
There is at least one more way to do it which was not presented here yet, namely using the crochet library. To demonstrate, we create a minimal flask app returning JSON output plus a modified version of a basic example spider.

flask_app.py:

```<code>import crochet
crochet.setup()  # initialize crochet before further imports

from flask import Flask, jsonify
from scrapy import signals
from scrapy.crawler import CrawlerRunner
from scrapy.signalmanager import dispatcher

from myproject.spiders import example

app = Flask(__name__)
output_data = []
crawl_runner = CrawlerRunner()
# crawl_runner = CrawlerRunner(get_project_settings()) if you want to apply settings.py

@app.route("/scrape")
def scrape():
    # run crawler in twisted reactor synchronously
    scrape_with_crochet()

    return jsonify(output_data)

@crochet.wait_for(timeout=60.0)
def scrape_with_crochet():
    # signal fires when single item is processed
    # and calls _crawler_result to append that item
    dispatcher.connect(_crawler_result, signal=signals.item_scraped)
    eventual = crawl_runner.crawl(
        example.ToScrapeSpiderXPath)
    return eventual  # returns a twisted.internet.defer.Deferred

def _crawler_result(item, response, spider):
    """
    We're using dict() to decode the items.
    Ideally this should be done using a proper export pipeline.
    """
    output_data.append(dict(item))

if __name__=='__main__':
    app.run('0.0.0.0', 8080)

</code>```

spiders/example.py:

```<code>import scrapy

class MyItem(scrapy.Item):
    text = scrapy.Field()
    author = scrapy.Field()

class ToScrapeSpiderXPath(scrapy.Spider):
    name = 'toscrape-xpath'
    start_urls = [
        'http://quotes.toscrape.com/',
    ]

    def parse(self, response):
        for quote in response.xpath('//div[@class="quote"]'):
            return MyItem(
                text=quote.xpath('./span[@class="text"]/text()').extract_first(),
                author=quote.xpath('.//small[@class="author"]/text()').extract_first())

        next_page_url = response.xpath('//li[@class="next"]/a/@href').extract_first()
        if next_page_url is not None:
            return scrapy.Request(response.urljoin(next_page_url))

</code>```

This whole setup is done in a synchronously way, that means /scrape won't return anything until the crawling process is done. Here's some additional information from the crochet documentation:

Setup: Crochet does a number of things for you as part of setup. Most significantly, it runs Twisted’s reactor in a thread it manages.

@wait_for: Blocking calls into Twisted (...) When the decorated function is called, the code will not run in the calling thread, but rather in the reactor thread.

The function blocks until a result is available from the code running in the Twisted thread.

This solution is inspired by the following 2 posts:

Execute Scrapy spiders in a Flask web application

Get Scrapy crawler output/results in script file function

Note that this is a very prototype like approach as for example output_data will keep it's state after a request. If you're just looking for a way to start this might be do fine.

Title: How to integrate Flask & Scrapy?

Body:
I'm using scrapy to get data and I want to use flask web framework to show the results in webpage. But I don't know how to call the spiders in the flask app. I've tried to use CrawlerProcess to call my spiders,but I got the error like this :

ValueError
ValueError: signal only works in main thread

Traceback (most recent call last)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/Library/Python/2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/Library/Python/2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/Library/Python/2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/Rabbit/PycharmProjects/Flask_template/FlaskTemplate.py", line 102, in index
process = CrawlerProcess()
File "/Library/Python/2.7/site-packages/scrapy/crawler.py", line 210, in __init__
install_shutdown_handlers(self._signal_shutdown)
File "/Library/Python/2.7/site-packages/scrapy/utils/ossignal.py", line 21, in install_shutdown_handlers
reactor._handleSignals()
File "/Library/Python/2.7/site-packages/twisted/internet/posixbase.py", line 295, in _handleSignals
_SignalReactorMixin._handleSignals(self)
File "/Library/Python/2.7/site-packages/twisted/internet/base.py", line 1154, in _handleSignals
signal.signal(signal.SIGINT, self.sigInt)
ValueError: signal only works in main thread

My scrapy code like this:

class EPGD(Item):

genID = Field()
genID_url = Field()
taxID = Field()
taxID_url = Field()
familyID = Field()
familyID_url = Field()
chromosome = Field()
symbol = Field()
description = Field()

class EPGD_spider(Spider):
    name = "EPGD"
    allowed_domains = ["epgd.biosino.org"]
    term = "man"
    start_urls = ["http://epgd.biosino.org/EPGD/search/textsearch.jsp?textquery="+term+"&submit=Feeling+Lucky"]

db = DB_Con()
collection = db.getcollection(name, term)

def parse(self, response):
    sel = Selector(response)
    sites = sel.xpath('//tr[@class="odd"]|//tr[@class="even"]')
    url_list = []
    base_url = "http://epgd.biosino.org/EPGD"

    for site in sites:
        item = EPGD()
        item['genID'] = map(unicode.strip, site.xpath('td[1]/a/text()').extract())
        item['genID_url'] = base_url+map(unicode.strip, site.xpath('td[1]/a/@href').extract())[0][2:]
        item['taxID'] = map(unicode.strip, site.xpath('td[2]/a/text()').extract())
        item['taxID_url'] = map(unicode.strip, site.xpath('td[2]/a/@href').extract())
        item['familyID'] = map(unicode.strip, site.xpath('td[3]/a/text()').extract())
        item['familyID_url'] = base_url+map(unicode.strip, site.xpath('td[3]/a/@href').extract())[0][2:]
        item['chromosome'] = map(unicode.strip, site.xpath('td[4]/text()').extract())
        item['symbol'] = map(unicode.strip, site.xpath('td[5]/text()').extract())
        item['description'] = map(unicode.strip, site.xpath('td[6]/text()').extract())
        self.collection.update({"genID":item['genID']}, dict(item),  upsert=True)
        yield item

    sel_tmp = Selector(response)
    link = sel_tmp.xpath('//span[@id="quickPage"]')

    for site in link:
        url_list.append(site.xpath('a/@href').extract())

    for i in range(len(url_list[0])):
        if cmp(url_list[0][i], "#") == 0:
            if i+1 < len(url_list[0]):
                print url_list[0][i+1]
                actual_url = "http://epgd.biosino.org/EPGD/search/" + url_list[0][i+1]
                yield Request(actual_url, callback=self.parse)
                break
            else:
                print "The index is out of range!"

My flask code like this:

@app.route('/', methods=['GET', 'POST'])
def index():
    process = CrawlerProcess()
    process.crawl(EPGD_spider)
    return redirect(url_for('details'))

@app.route('/details', methods = ['GET'])
def epgd():
    if request.method == 'GET':
        results = db['EPGD_test'].find()
        json_results= []
        for result in results:
            json_results.append(result)
        return toJson(json_results)

How can I call my scrapy spiders when using flask web framework?

Body:
There is at least one more way to do it which was not presented here yet, namely using the crochet library. To demonstrate, we create a minimal flask app returning JSON output plus a modified version of a basic example spider.

flask_app.py:

import crochet
crochet.setup()  # initialize crochet before further imports

from flask import Flask, jsonify
from scrapy import signals
from scrapy.crawler import CrawlerRunner
from scrapy.signalmanager import dispatcher

from myproject.spiders import example

app = Flask(__name__)
output_data = []
crawl_runner = CrawlerRunner()
# crawl_runner = CrawlerRunner(get_project_settings()) if you want to apply settings.py

@app.route("/scrape")
def scrape():
    # run crawler in twisted reactor synchronously
    scrape_with_crochet()

    return jsonify(output_data)

@crochet.wait_for(timeout=60.0)
def scrape_with_crochet():
    # signal fires when single item is processed
    # and calls _crawler_result to append that item
    dispatcher.connect(_crawler_result, signal=signals.item_scraped)
    eventual = crawl_runner.crawl(
        example.ToScrapeSpiderXPath)
    return eventual  # returns a twisted.internet.defer.Deferred

def _crawler_result(item, response, spider):
    """
    We're using dict() to decode the items.
    Ideally this should be done using a proper export pipeline.
    """
    output_data.append(dict(item))

if __name__=='__main__':
    app.run('0.0.0.0', 8080)

spiders/example.py:

import scrapy

class MyItem(scrapy.Item):
    text = scrapy.Field()
    author = scrapy.Field()

class ToScrapeSpiderXPath(scrapy.Spider):
    name = 'toscrape-xpath'
    start_urls = [
        'http://quotes.toscrape.com/',
    ]

    def parse(self, response):
        for quote in response.xpath('//div[@class="quote"]'):
            return MyItem(
                text=quote.xpath('./span[@class="text"]/text()').extract_first(),
                author=quote.xpath('.//small[@class="author"]/text()').extract_first())

        next_page_url = response.xpath('//li[@class="next"]/a/@href').extract_first()
        if next_page_url is not None:
            return scrapy.Request(response.urljoin(next_page_url))

This whole setup is done in a synchronously way, that means /scrape won't return anything until the crawling process is done. Here's some additional information from the crochet documentation:

Setup: Crochet does a number of things for you as part of setup. Most significantly, it runs Twisted’s reactor in a thread it manages.

@wait_for: Blocking calls into Twisted (...) When the decorated function is called, the code will not run in the calling thread, but rather in the reactor thread.

The function blocks until a result is available from the code running in the Twisted thread.

This solution is inspired by the following 2 posts:

Execute Scrapy spiders in a Flask web application

Get Scrapy crawler output/results in script file function

Note that this is a very prototype like approach as for example output_data will keep it's state after a request. If you're just looking for a way to start this might be do fine.


问:
need to calculate #quarters for which product was not bought continuously

Please refer to the image.

I have records of the buying trends of customers across 20 quarters in terms of revenue. I want to calculate the gaps for each customer i.e. #quarters for which revenue was 0 continuously. Also, I need to calculate the maximum size of the gaps for each customer.

For this specific record, there are 2 gaps in revenues. One b/w 2015Q1 and 2016Q1 and another between 2016Q4 and 2017Q3. I need a code/logic to be able to calculate these 2 gaps of 3 quarters and 2 quarters respectively and then the max of those gaps.

I tried using match index, but it seemed to not help achieve the desired outcome. Can you please help me with this?

答:
So I'm unsure of how you want the results formatted but I'll assume it will be 2 cells to the right of the info you have above, 1 for the # of gaps and 1 to show the maximum GAP. The below code should be able to assist but changes will have to be made based on the amount of rows/columns being checked. </p>

```<code>   Sub Finder()

Dim X As Integer
Dim Y As Integer
Dim GapCounter As Integer
Dim MaxCounter As Integer

'This For statement should be change according to how many rows need to be done
For c = 2 To 3

GapCounter = 0
MaxCounter = 0

'This For statement should be changed according to how many columns are being viewed.
For Y = 1 To 11

'This will check to see if the next column is a 0, if so a gap is beginning and the gap counter will increase
If Cells(c, Y).Value <> 0 And Cells(c, Y + 1).Value = 0 And IsEmpty(Cells(c, Y + 1)) = False Then GapCounter = GapCounter + 1

'This is checking value,if value is 0 need to add it to the current Gap amount
If Cells(c, Y).Value = 0 Then MaxCounter = MaxCounter + 1

'Checks to see if the next value is not a 0 and is not empty, if these are met we can conclude that's all the gap that needs to be counted and compare to the previous Max gap #, if it's larger than we replace
'Then we have to reset the max counter
If Cells(c, Y + 1).Value <> 0 And IsEmpty(Cells(c, Y + 1)) = False And MaxCounter > Cells(c, 14).Value Then
'This is the cell where the max time a gap has lasted will appear.
Cells(c, 14).Value = MaxCounter
MaxCounter = 0
End If

Next
'This is the cell where the # of gaps will appear
Cells(c, 13).Value = GapCounter

Next

End Sub
</code>```

Body:
So I'm unsure of how you want the results formatted but I'll assume it will be 2 cells to the right of the info you have above, 1 for the # of gaps and 1 to show the maximum GAP. The below code should be able to assist but changes will have to be made based on the amount of rows/columns being checked.

   Sub Finder()

Dim X As Integer
Dim Y As Integer
Dim GapCounter As Integer
Dim MaxCounter As Integer

'This For statement should be change according to how many rows need to be done
For c = 2 To 3

GapCounter = 0
MaxCounter = 0

'This For statement should be changed according to how many columns are being viewed.
For Y = 1 To 11

'This will check to see if the next column is a 0, if so a gap is beginning and the gap counter will increase
If Cells(c, Y).Value <> 0 And Cells(c, Y + 1).Value = 0 And IsEmpty(Cells(c, Y + 1)) = False Then GapCounter = GapCounter + 1

'This is checking value,if value is 0 need to add it to the current Gap amount
If Cells(c, Y).Value = 0 Then MaxCounter = MaxCounter + 1

'Checks to see if the next value is not a 0 and is not empty, if these are met we can conclude that's all the gap that needs to be counted and compare to the previous Max gap #, if it's larger than we replace
'Then we have to reset the max counter
If Cells(c, Y + 1).Value <> 0 And IsEmpty(Cells(c, Y + 1)) = False And MaxCounter > Cells(c, 14).Value Then
'This is the cell where the max time a gap has lasted will appear.
Cells(c, 14).Value = MaxCounter
MaxCounter = 0
End If

Next
'This is the cell where the # of gaps will appear
Cells(c, 13).Value = GapCounter

Next

End Sub


问:
media folder images are not displaying in Template Page
I'm new to Django. Medial folder images are not displaying in Template file. please help me in that.

models.py

```<code>class Images(models.Model):
    image = models. ImageField(upload_to='images', blank=True, null=True)
</code>```

settings.py

```<code>STATIC_URL = '/static/'

STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

MEDIA_URL = '/media/'
</code>```

urls.py

```<code>urlpatterns = [
......
]
if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
</code>```

views.py

```<code>def image_display(request):
    image_obj = Images.objects.all()
    return render(request, 'index.html', {'image_obj': image_obj})
</code>```

index.html

```<code><body>

    {% for item in myoffers_objs %}
    <div>
        <img src="{{ item.image.url }}" alt="Images"/>
    </div>
    {% endfor %}

</body>
</code>```

Project details where i did mistake. why its displaying like this.
Image displaying like this

答:
The for loop in the template file is incorrect.
Try this

    {% for item in image_obj %}

    {% endfor %}

```

Body:
I'm new to Django. Medial folder images are not displaying in Template file. please help me in that.

models.py

class Images(models.Model):
    image = models. ImageField(upload_to='images', blank=True, null=True)

settings.py

STATIC_URL = '/static/'

STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

MEDIA_URL = '/media/'

urls.py

urlpatterns = [
......
]
if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

views.py

def image_display(request):
    image_obj = Images.objects.all()
    return render(request, 'index.html', {'image_obj': image_obj})

index.html

    {% for item in myoffers_objs %}

    {% endfor %}

Project details where i did mistake. why its displaying like this.
Image displaying like this

Body:
The for loop in the template file is incorrect.
Try this

<body>

    {% for item in image_obj %}
    <div>
        <img src="{{ item.image.url }}" alt="Images"/>
    </div>
    {% endfor %}

</body>


问:
Is there simplest possible reduce/fold method in java?
Other technologies I know (.Net, JS) contain the simplest fold/reduce operation:

```<code>TResult reduce(TResult result, (TResult prevResult, TValue value) -> TResult)
</code>```

One method I found requires TValue and TResult to be the same type.
Other one requires providing BinaryOperation that combines 2 TResults.
None of those constraints matches my context.
For now I ended up with code like:

```<code>Accumulator acc = someInitialValue;
for(Element element: list) {
    accumulator = reducer(accumulator, element);
}
</code>```

but I believe so the basic method should be contained in stream API.

I have also taken a look for collectors, but I haven't found anything helpful.

答:
What you're looking for is the Java Stream framework added in Java 8.

The Stream class has 3 reduce methods:

Optional<T> reduce(BinaryOperator<T> accumulator)

T reduce(T identity, BinaryOperator<T> accumulator)

 U reduce(U identity, BiFunction accumulator, BinaryOperator combiner)

The second reduce method matches your example, but requires same result type. For different result type, you need to use the third reduce method, which requires an extra combiner method, since it needs to support parallel processing.

It also has 2 collect methods:

<R,A> R collect(Collector<? super T,A,R> collector)

<R> R collect(Supplier<R> supplier, BiConsumer<R,? super T> accumulator, BiConsumer<R,R> combiner)

The first collect method is the most commonly used one, when used in combination with the Collectors factory class.

Body:
Other technologies I know (.Net, JS) contain the simplest fold/reduce operation:

TResult reduce(TResult result, (TResult prevResult, TValue value) -> TResult)

One method I found requires TValue and TResult to be the same type.
Other one requires providing BinaryOperation that combines 2 TResults.
None of those constraints matches my context.
For now I ended up with code like:

Accumulator acc = someInitialValue;
for(Element element: list) {
    accumulator = reducer(accumulator, element);
}

but I believe so the basic method should be contained in stream API.

I have also taken a look for collectors, but I haven't found anything helpful.

Body:
What you're looking for is the Java Stream framework added in Java 8.

The Stream class has 3 reduce methods:

Optional<T> reduce(BinaryOperator<T> accumulator)

T reduce(T identity, BinaryOperator<T> accumulator)

<U> U reduce(U identity, BiFunction<U,? super T,U> accumulator, BinaryOperator<U> combiner)

The second reduce method matches your example, but requires same result type. For different result type, you need to use the third reduce method, which requires an extra combiner method, since it needs to support parallel processing.

It also has 2 collect methods:

<R,A> R collect(Collector<? super T,A,R> collector)

<R> R collect(Supplier<R> supplier, BiConsumer<R,? super T> accumulator, BiConsumer<R,R> combiner)

The first collect method is the most commonly used one, when used in combination with the Collectors factory class.