问:
DQN not working Properly
I am trying to write my own DQN in Python, using keras. I think my logic is correct. I am trying it on the CartPole environment, but the rewards are not increasing after 50,000 episodes. Any help will be appreciated. Currently I am not looking to the dueling or the Double DQN part. </p>

```<code>class ReplayBuffer:
        def __init__(self, size=100000):
            self.buffer=deque(maxlen=size)

        def sample(self, sample_size):
            return random.sample(self.buffer, sample_size)

        def add_to_buffer(self, experience):
            self.buffer.append(experience)

    def generator(number):
        return(i for i in range(number))

    def epsilon_greedy_policy(q_values, epsilon):
        number_of_actions =len(q_values)
        action_probabilites = np.ones(number_of_actions, dtype=float)*epsilon/number_of_actions
        best_action = np.argmax(q_values)
        action_probabilites[best_action]+= (1-epsilon)
        return np.random.choice(number_of_actions, p=action_probabilites)

    class DQNAgent:
        def __init__(self, env, model, gamma):
            self.env=env
            self.model=model
            self.replay_buffer=ReplayBuffer()
            self.gamma=gamma
            self.state_dim=env.observation_space.shape[0]

        def train_model(self, training_data, training_label):
            self.model.fit(training_data, training_label, batch_size=32, verbose=0)

        def predict_one(self, state):
            return self.model.predict(state.reshape(1, self.state_dim)).flatten()

        def experience_replay(self, experiences):
            import pdb; pdb.set_trace()
            states, actions, rewards, next_states=zip(*[[experience[0], experience[1], experience[2], experience[3]] for experience in experiences])
            states=np.asarray(states)
            place_holder_state=np.zeros(self.state_dim)
            next_states_ = np.asarray([(place_holder_state if next_state is None else next_state) for next_state in next_states])
            q_values_for_states=self.model.predict(states)
            q_values_for_next_states=self.model.predict(next_states_)
            for x in generator(len(experiences)):
                y_true=rewards[x]
                if next_states[x].any():
                    y_true +=self.gamma*(np.amax(q_values_for_next_states[x]))
                q_values_for_states[x][actions[x]]=y_true
            self.train_model(states, q_values_for_states)

        def fit(self, number_of_epsiodes, batch_size):
            for _ in generator(number_of_epsiodes):
                total_reward=0
                state=env.reset()
                while True:
                    #self.env.render()
                    q_values_for_state=self.predict_one(state)
                    action=epsilon_greedy_policy(q_values_for_state, 0.1)
                    next_state, reward, done, _=env.step(action)
                    self.replay_buffer.add_to_buffer([state, action, reward, next_state])
                    state = next_state
                    total_reward += reward
                    if len(self.replay_buffer.buffer) > 50:
                        experience=self.replay_buffer.sample(batch_size)
                        self.experience_replay(experience)
                    if done:
                       break
                print("Total reward:", total_reward)

    env = gym.make('CartPole-v0')
    model=create_model(env.observation_space.shape[0], env.action_space.n)
    agent=DQNAgent(env, model, 0.99)
    agent.fit(100000, 32)'
</code>```

答:
In the fit function I had to add </p>

```<code>if done:
   next_state = None
</code>```

Title: DQN not working Properly

Body:
I am trying to write my own DQN in Python, using keras. I think my logic is correct. I am trying it on the CartPole environment, but the rewards are not increasing after 50,000 episodes. Any help will be appreciated. Currently I am not looking to the dueling or the Double DQN part.

class ReplayBuffer:
        def __init__(self, size=100000):
            self.buffer=deque(maxlen=size)

        def sample(self, sample_size):
            return random.sample(self.buffer, sample_size)

        def add_to_buffer(self, experience):
            self.buffer.append(experience)

    def generator(number):
        return(i for i in range(number))

    def epsilon_greedy_policy(q_values, epsilon):
        number_of_actions =len(q_values)
        action_probabilites = np.ones(number_of_actions, dtype=float)*epsilon/number_of_actions
        best_action = np.argmax(q_values)
        action_probabilites[best_action]+= (1-epsilon)
        return np.random.choice(number_of_actions, p=action_probabilites)

    class DQNAgent:
        def __init__(self, env, model, gamma):
            self.env=env
            self.model=model
            self.replay_buffer=ReplayBuffer()
            self.gamma=gamma
            self.state_dim=env.observation_space.shape[0]

        def train_model(self, training_data, training_label):
            self.model.fit(training_data, training_label, batch_size=32, verbose=0)

        def predict_one(self, state):
            return self.model.predict(state.reshape(1, self.state_dim)).flatten()

        def experience_replay(self, experiences):
            import pdb; pdb.set_trace()
            states, actions, rewards, next_states=zip(*[[experience[0], experience[1], experience[2], experience[3]] for experience in experiences])
            states=np.asarray(states)
            place_holder_state=np.zeros(self.state_dim)
            next_states_ = np.asarray([(place_holder_state if next_state is None else next_state) for next_state in next_states])
            q_values_for_states=self.model.predict(states)
            q_values_for_next_states=self.model.predict(next_states_)
            for x in generator(len(experiences)):
                y_true=rewards[x]
                if next_states[x].any():
                    y_true +=self.gamma*(np.amax(q_values_for_next_states[x]))
                q_values_for_states[x][actions[x]]=y_true
            self.train_model(states, q_values_for_states)

        def fit(self, number_of_epsiodes, batch_size):
            for _ in generator(number_of_epsiodes):
                total_reward=0
                state=env.reset()
                while True:
                    #self.env.render()
                    q_values_for_state=self.predict_one(state)
                    action=epsilon_greedy_policy(q_values_for_state, 0.1)
                    next_state, reward, done, _=env.step(action)
                    self.replay_buffer.add_to_buffer([state, action, reward, next_state])
                    state = next_state
                    total_reward += reward
                    if len(self.replay_buffer.buffer) > 50:
                        experience=self.replay_buffer.sample(batch_size)
                        self.experience_replay(experience)
                    if done:
                       break
                print("Total reward:", total_reward)

    env = gym.make('CartPole-v0')
    model=create_model(env.observation_space.shape[0], env.action_space.n)
    agent=DQNAgent(env, model, 0.99)
    agent.fit(100000, 32)'

Body:
In the fit function I had to add

if done:
   next_state = None


来源: stackexchange

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

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

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

If any body have done this please share your views.

Thanks in advance.

答:
I had a similar problem with filebeat configs in the same situation.

Basically, you need to use environment variables to differentiate the environment, and then run different commands in the container_commands section depending on which environment variables you have set.

You can see my worked example here:
https://www.dativa.com/simplifying-filebeat-logging-with-elastic-beanstalk/

ContentLicense: CC BY-SA 4.0

ContentLicense: CC BY-SA 3.0


问:
Symfony: Allow Basic Auth on sub route - multiple firewalls
I have a main firewall for my Symfony 4 application. It is configured to load users from the DB and to use a login form etc, all standard stuff. And it works fine.

Here is my current security.yaml file:

```<code>security:
encoders:
    App\Entity\User:
        algorithm: bcrypt
providers:
    user_provider:
        entity:
            class: App\Entity\User
firewalls:
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false
    main:
        pattern: ^/
        provider: user_provider
        anonymous: ~
        form_login:
            login_path: /login
            check_path: /login
        logout:
            path: /logout
            target: /login
access_control:
    - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/, roles: ROLE_ADMIN }
</code>```

Now I need to add in basic authentication on a particular route that has dynamic information in it, which is where im struggling. An example URL would be:

http://myapp.com/repositories/%%username%%/directory/file.json

So up to http://myapp.com/ is secured and authenticated by the standard login form.

But if the request is to ^/repositories/%%username%% I need basic auth to be used.

%%username%% being dynamic and as you can see its a route to a file that will be the response in JSON. The basic auth doesn't actually have to be authenticated for symfony, it just needs to be available and if it fails return the correct Exception/Response:

array('fatal: Authentication failed', 'remote error: Invalid username or password.')

Any help would be much appreciated.

Thanks

** EDIT**

Ok guys so I have managed to figure out how to add the Basic Auth alongside my form based auth for a sub route.

Now i just need to figure out how to enable basic auth for a dynamic route.

Currently the following security.yaml covers http://myapp.com/repositories and anyone that logs in successfully will have access to all of the routes underneath.

```<code>security:
encoders:
    App\Entity\User:
        algorithm: bcrypt
providers:
    user_provider:
        entity:
            class: App\Entity\User
firewalls:
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false
    repos:
        pattern: ^/repositories/
        anonymous: ~
        stateless: true
        http_basic:
            realm: "Toran Proxy Access"
    main:
        pattern: ^/
        provider: user_provider
        anonymous: ~
        form_login:
            login_path: /login
            check_path: /login
        logout:
            path: /logout
            target: /login
access_control:
    - { path: ^/repositories/, roles: ROLE_ADMIN}
    - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/, roles: ROLE_ADMIN }
</code>```

But what i now need to do is restrict /repositories/%%username%% using basic auth and only giving access to this directory for certain credentials.

Thanks

答:
So in the end I created my own Authorization Provider following http://symfony.com/doc/current/security/custom_authentication_provider.html

the in the provider I done a check to see if the current user should have permission to the sub directory or not.

Body:
I have a main firewall for my Symfony 4 application. It is configured to load users from the DB and to use a login form etc, all standard stuff. And it works fine.

Here is my current security.yaml file:

security:
encoders:
    App\Entity\User:
        algorithm: bcrypt
providers:
    user_provider:
        entity:
            class: App\Entity\User
firewalls:
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false
    main:
        pattern: ^/
        provider: user_provider
        anonymous: ~
        form_login:
            login_path: /login
            check_path: /login
        logout:
            path: /logout
            target: /login
access_control:
    - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/, roles: ROLE_ADMIN }

Now I need to add in basic authentication on a particular route that has dynamic information in it, which is where im struggling. An example URL would be:

http://myapp.com/repositories/%%username%%/directory/file.json

So up to http://myapp.com/ is secured and authenticated by the standard login form.

But if the request is to ^/repositories/%%username%% I need basic auth to be used.

%%username%% being dynamic and as you can see its a route to a file that will be the response in JSON. The basic auth doesn't actually have to be authenticated for symfony, it just needs to be available and if it fails return the correct Exception/Response:

array('fatal: Authentication failed', 'remote error: Invalid username or password.')

Any help would be much appreciated.

Thanks

** EDIT**

Ok guys so I have managed to figure out how to add the Basic Auth alongside my form based auth for a sub route.

Now i just need to figure out how to enable basic auth for a dynamic route.

Currently the following security.yaml covers http://myapp.com/repositories and anyone that logs in successfully will have access to all of the routes underneath.

security:
encoders:
    App\Entity\User:
        algorithm: bcrypt
providers:
    user_provider:
        entity:
            class: App\Entity\User
firewalls:
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false
    repos:
        pattern: ^/repositories/
        anonymous: ~
        stateless: true
        http_basic:
            realm: "Toran Proxy Access"
    main:
        pattern: ^/
        provider: user_provider
        anonymous: ~
        form_login:
            login_path: /login
            check_path: /login
        logout:
            path: /logout
            target: /login
access_control:
    - { path: ^/repositories/, roles: ROLE_ADMIN}
    - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/, roles: ROLE_ADMIN }

But what i now need to do is restrict /repositories/%%username%% using basic auth and only giving access to this directory for certain credentials.

Thanks


问:
Receiving getExecuting FilePath error in TypeScript
I am using WebStorm for a node.js application developed using transpiled TypeScript. Whenever I run the tsconfig.json by selecting "compile all" from the TypeScript tab, I get the following error:

Error:Initialization error (typescript). Cannot read property 'getExecutingFilePath' of undefined.

However, if I execute one file at a time I receive "Service is not started".

Here is the tsconfig.json file:

```<code>{
"compilerOptions": {
  "module": "commonjs",
  "target": "ES6",
  "moduleResolution": "node",
  "experimentalDecorators": true,
  "emitDecoratorMetadata": true,
  "types": ["reflect-metadata"],
  "lib": ["ES6"],
  "sourceMap": true,
  "inlineSources": true,
  "pretty": true,
  "outDir": "dist",
  "rootDir": "src",
  "noLib": false,
  "declaration": true
},
"compileOnSave": true,
"include": [
  "src/**/*"
],
"exclude": [
  "node_modules"
]
}
</code>```

Also, I notice that I do not see the TypeScript words at the bottom right of the screen:

I noticed that if I do a tsc --version from the console, I am getting Version 2.0.2, whereas, if I Preferences -> Languages & Frameworks -> TypeScript, I am getting 2.6.2 for WebStorm. If I run tsc from the command line it seems to work but not from within WebStorm. What could be the cause of this issue?

答:
I noticed that if I do a tsc --version from the console, I am getting
  Version 2.0.2, whereas, if I Preferences -> Languages & Frameworks ->
  TypeScript, I am getting 2.6.2 for WebStorm

You have TypeScript 2.0.2 installed globally, and 2.6.2 is your local project version. By default, WebStorm uses TypeScript installed in project node_modules.

Most probably, your WebStorm version doesn't support TypeScript 2.6.x (this release includes breaking API changes, so tsserver integration might stop working). Please change TypeScript version to Bundled in Preferences -> Languages & Frameworks -> TypeScript - does it help?

Body:
I am using WebStorm for a node.js application developed using transpiled TypeScript. Whenever I run the tsconfig.json by selecting "compile all" from the TypeScript tab, I get the following error:

Error:Initialization error (typescript). Cannot read property 'getExecutingFilePath' of undefined.

However, if I execute one file at a time I receive "Service is not started".

Here is the tsconfig.json file:

{
"compilerOptions": {
  "module": "commonjs",
  "target": "ES6",
  "moduleResolution": "node",
  "experimentalDecorators": true,
  "emitDecoratorMetadata": true,
  "types": ["reflect-metadata"],
  "lib": ["ES6"],
  "sourceMap": true,
  "inlineSources": true,
  "pretty": true,
  "outDir": "dist",
  "rootDir": "src",
  "noLib": false,
  "declaration": true
},
"compileOnSave": true,
"include": [
  "src/**/*"
],
"exclude": [
  "node_modules"
]
}

Also, I notice that I do not see the TypeScript words at the bottom right of the screen:

I noticed that if I do a tsc --version from the console, I am getting Version 2.0.2, whereas, if I Preferences -> Languages & Frameworks -> TypeScript, I am getting 2.6.2 for WebStorm. If I run tsc from the command line it seems to work but not from within WebStorm. What could be the cause of this issue?


问:
How to send a fake udp package in golang
Im trying to send a fake udp (a random mac address, let's say 01:ff:ff:ff:ff:ff) package to be handled by the ServeDHCP on the server side, Im running the following dhcpv4 github repository github.com/krolaw/dhcp4.

The goal of send a discover package, is to check if the dhcp is alive.

In fact I created a new func called check

```<code>func (h *DHCPHandler) check () {
    con, err = net.Dial("udp", "127.0.0.1:67")
    for {
            //fake udp package???
            time.Sleep(10 * time.Minute)
    }
</code>```

}

in the main of the function, I have the following call go handler.check()

And in the ServeDHCP I should pass these parameters:
 func (h *DHCPHandler) ServeDHCP(p dhcp.Packet, msgType dhcp.MessageType, options dhcp.Options)

How could I send a fake upd package from the func check?

答:
At the end I managed to make works this check to send in periods of 10 minuts an upd package with an mac address I know is gonna never be reached as following.

```<code>func (h *DHCPHandler) check() {
    //Fetch parameters from config file
    config := getConfig() // here is a mac saved on a json file 01:ff:ff:ff:ff:ff
    macFake, err := net.ParseMAC(config.MacFake)

    if err != nil {
            fmt.Println("error with the fake mac provided on the json file", err)
    }

    // create connection
    conn, err := net.Dial("udp4", "127.0.0.1:67")
    if err != nil {
            fmt.Println("error with the connection", err)
    }

    //stay alive
    for {
            fmt.Fprintf(conn, "GET / HTTP/1.0\r\n\r\n")
            conn.Write(dhcp.RequestPacket(dhcp.Discover, macFake, h.ip, []byte{0, 1, 2, 3}, true, nil))
            time.Sleep(10 * time.Minute)
    }
</code>```

}

On the main function, I just need to call this check goroutine and add on the server side (ServeDHCP) this code:

```<code>    mac := p.CHAddr().String()
    //Fetch parameters from config file
    config := getConfig()
    if mac == config.MacFake { //udp package received and a mac saved on a json file 01:ff:ff:ff:ff:ff
            // send notification to restart if failure on a systemd
            daemon.SdNotify(false, "WATCHDOG=1")
            return
    }
</code>```

The last part needed is to add a systemd check, in my case I send the watchdog every 10 min, so, the systemd check will be setted it to 11min

```<code>[Service]
ExecStartPre=//something
WatchdogSec=11min
Restart=on-failure
</code>```

Body:
Im trying to send a fake udp (a random mac address, let's say 01:ff:ff:ff:ff:ff) package to be handled by the ServeDHCP on the server side, Im running the following dhcpv4 github repository github.com/krolaw/dhcp4.

The goal of send a discover package, is to check if the dhcp is alive.

In fact I created a new func called check

func (h *DHCPHandler) check () {
    con, err = net.Dial("udp", "127.0.0.1:67")
    for {
            //fake udp package???
            time.Sleep(10 * time.Minute)
    }

}

in the main of the function, I have the following call go handler.check()

And in the ServeDHCP I should pass these parameters:
 func (h *DHCPHandler) ServeDHCP(p dhcp.Packet, msgType dhcp.MessageType, options dhcp.Options)

How could I send a fake upd package from the func check?

Body:
At the end I managed to make works this check to send in periods of 10 minuts an upd package with an mac address I know is gonna never be reached as following.

func (h *DHCPHandler) check() {
    //Fetch parameters from config file
    config := getConfig() // here is a mac saved on a json file 01:ff:ff:ff:ff:ff
    macFake, err := net.ParseMAC(config.MacFake)

    if err != nil {
            fmt.Println("error with the fake mac provided on the json file", err)
    }

    // create connection
    conn, err := net.Dial("udp4", "127.0.0.1:67")
    if err != nil {
            fmt.Println("error with the connection", err)
    }

    //stay alive
    for {
            fmt.Fprintf(conn, "GET / HTTP/1.0\r\n\r\n")
            conn.Write(dhcp.RequestPacket(dhcp.Discover, macFake, h.ip, []byte{0, 1, 2, 3}, true, nil))
            time.Sleep(10 * time.Minute)
    }

}

On the main function, I just need to call this check goroutine and add on the server side (ServeDHCP) this code:

    mac := p.CHAddr().String()
    //Fetch parameters from config file
    config := getConfig()
    if mac == config.MacFake { //udp package received and a mac saved on a json file 01:ff:ff:ff:ff:ff
            // send notification to restart if failure on a systemd
            daemon.SdNotify(false, "WATCHDOG=1")
            return
    }

The last part needed is to add a systemd check, in my case I send the watchdog every 10 min, so, the systemd check will be setted it to 11min

[Service]
ExecStartPre=//something
WatchdogSec=11min
Restart=on-failure


问:
Public final in enum's instance variables
i would like to know if it's a common accepted practice to give public visibility to final data fields in 'enum' instances, and name them using the CONSTANT_CASE.
I know that it's not strictly correct, but often I use enum instead of final Classes to simplify the code.

An example, I use:

```<code>public enum PropertyType {

   STRING("string", String.class, "generico"),
   BOOLEAN("boolean", Boolean.class, "vero/falso"),
   NUMBER("number", Double.class, "numerico"),
   INTEGER("integer", Long.class, "intero");

   public final String JSON_TYPE;
   public final Class JAVA_TYPE;
   public final String DESCRIPTION;

   private PropertyType(String jsonType, Class javaType, String description)
   {
       if (jsonType == null)
           throw new IllegalArgumentException("json type can't be null");

       if (javaType == null)
           throw new IllegalArgumentException("java type can't be null");

       this.JSON_TYPE = jsonType;
       this.JAVA_TYPE = javaType;
       this.DESCRIPTION = description;
   }
}
</code>```

instead of

```<code>public class PropertyType {
    public static final class STRING {
        public static final String JSON_TYPE="string";
        public static final Class JAVA_TYPE=String.class;
        public static final String DESCRIPTION="generico";
    }

    ...
    ...

    private PropertyType() {}
}
</code>```

Both the options let you use syntax like 'PropertyType.STRING.DESCRIPTION', but the first is easier to code and less redundant, plus it's faster to expand while the latter it's long to code with a lot of copy and paste...
Since the purpose of the two it's the same, why require a different naming convention?

答:
While this question is primarily opinion-based, I would say the following:

upper case field names are adopted to be used only for static constants

for this particular case more logical make these object variables private and add conventional getters for them.

Body:
i would like to know if it's a common accepted practice to give public visibility to final data fields in 'enum' instances, and name them using the CONSTANT_CASE.
I know that it's not strictly correct, but often I use enum instead of final Classes to simplify the code.

An example, I use:

public enum PropertyType {

   STRING("string", String.class, "generico"),
   BOOLEAN("boolean", Boolean.class, "vero/falso"),
   NUMBER("number", Double.class, "numerico"),
   INTEGER("integer", Long.class, "intero");

   public final String JSON_TYPE;
   public final Class JAVA_TYPE;
   public final String DESCRIPTION;

   private PropertyType(String jsonType, Class javaType, String description)
   {
       if (jsonType == null)
           throw new IllegalArgumentException("json type can't be null");

       if (javaType == null)
           throw new IllegalArgumentException("java type can't be null");

       this.JSON_TYPE = jsonType;
       this.JAVA_TYPE = javaType;
       this.DESCRIPTION = description;
   }
}

instead of

public class PropertyType {
    public static final class STRING {
        public static final String JSON_TYPE="string";
        public static final Class JAVA_TYPE=String.class;
        public static final String DESCRIPTION="generico";
    }

    ...
    ...

    private PropertyType() {}
}

Both the options let you use syntax like 'PropertyType.STRING.DESCRIPTION', but the first is easier to code and less redundant, plus it's faster to expand while the latter it's long to code with a lot of copy and paste...
Since the purpose of the two it's the same, why require a different naming convention?


问:
python and javascript with html generation
On a website that I manage, we have JSON files which contain page data. We then create the page using this JSON.

The data looks roughly like this (except a lot more complex).

```<code>[
  {"title": "Hello world", "content": "World, hello to you!"},
  {"title": "Hello world Part II", "content": "The sequel to hello world."},
  ...
]
</code>```

This data is then parsed into HTML. Now, here lies the issue: we need two versions of the HTML.

One needs to be static, outputted in the format of file-0.html which would be formatted with a title of Hello World and content of World, hello to you! and file-1.html (title=Hello World Part II, content=The sequel to hello world).

The second needs to be just a plain page file-all.html which includes a JavaScript that pulls the JSON via AJAX when its needed and creates a container for each page which includes subpages that have the content/titles for everything in the JSON.

Right now, we use Python to generate the HTML for the file-0.html static pages, and then JavaScript for the AJAX pages. While this works, it means there is a lot of code duplication for a pretty small project—every time we want to change the class of the
 title is wrapped in, we have to change two places with slightly different syntax.

Is there any good way of fixing this issue, so that all the code for generating page (or as much as possible) is in one language? (This would probably have to be JavaScript, since bandwidth is an issue—we'd like to avoid transferring HTML via AJAX if possible.)

答:
You have two good options:

Write the page generation logic in a template language like Mustache (http://mustache.github.com/).  Then you can compile those templates down to Python (for the server side) and Javascript (for the client).  The data consumed by both versions is the same and you only have to maintain one template definition.

Write everything in Javascript and execute that JS on the server.  There are at least two good server-side JS engines: V8 and Apache Rhino.

ContentLicense: CC BY-SA 2.5

Body:
On a website that I manage, we have JSON files which contain page data. We then create the page using this JSON.

The data looks roughly like this (except a lot more complex).

[
  {"title": "Hello world", "content": "World, hello to you!"},
  {"title": "Hello world Part II", "content": "The sequel to hello world."},
  ...
]

This data is then parsed into HTML. Now, here lies the issue: we need two versions of the HTML.

One needs to be static, outputted in the format of file-0.html which would be formatted with a title of Hello World and content of World, hello to you! and file-1.html (title=Hello World Part II, content=The sequel to hello world).

The second needs to be just a plain page file-all.html which includes a JavaScript that pulls the JSON via AJAX when its needed and creates a container for each page which includes subpages that have the content/titles for everything in the JSON.

Right now, we use Python to generate the HTML for the file-0.html static pages, and then JavaScript for the AJAX pages. While this works, it means there is a lot of code duplication for a pretty small project—every time we want to change the class of the
 title is wrapped in, we have to change two places with slightly different syntax.

Is there any good way of fixing this issue, so that all the code for generating page (or as much as possible) is in one language? (This would probably have to be JavaScript, since bandwidth is an issue—we'd like to avoid transferring HTML via AJAX if possible.)


问:
How can I route to a child route in Zend 3?
I use Zend Framework Version 3. I would like to redirect the request to a new route, and in more precisely to a childroute?

If it was only a "parent" route I would to this

```<code>return $this->redirect()->toRoute('name-of-route', []);
</code>```

however I have a childroute I would like to redirect to. How can this be done

答:
Assuming this works the same as in ZF2, you use a / to denote the heirarchy:

```<code>return $this->redirect()->toRoute('name-of-route/name-of-child-route', []);
</code>```

Body:
I use Zend Framework Version 3. I would like to redirect the request to a new route, and in more precisely to a childroute?

If it was only a "parent" route I would to this

return $this->redirect()->toRoute('name-of-route', []);

however I have a childroute I would like to redirect to. How can this be done

Body:
Assuming this works the same as in ZF2, you use a / to denote the heirarchy:

return $this->redirect()->toRoute('name-of-route/name-of-child-route', []);


问:
how can reduce video size at time of recording, Android?
ffmpeg, mediacodec, and mp4parser codecs are taking time to compress, so is there any other way to reduce size of video at time of recording?

I am using MediaRecorder for recording videos but it creates a lot of data, and I want to reduce it as I'm not able to upload it.

答:
I got my answer with the use of media Recording Documentation and added this lines in code:

```<code>mediaRecorder.setVideoEncodingBitRate(1000000);
mediaRecorder.setVideoSize(1280, 720);
mediaRecorder.setOutputFile(folder.getAbsolutePath()+"/"+mediaFileName+".mp4");
mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
mediaRecorder.setVideoFrameRate(18);
mediaRecorder.setOrientationHint(mOrientation);
</code>```

After this, I get a lower size of video with good quality.

Body:
I got my answer with the use of media Recording Documentation and added this lines in code:

mediaRecorder.setVideoEncodingBitRate(1000000);
mediaRecorder.setVideoSize(1280, 720);
mediaRecorder.setOutputFile(folder.getAbsolutePath()+"/"+mediaFileName+".mp4");
mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
mediaRecorder.setVideoFrameRate(18);
mediaRecorder.setOrientationHint(mOrientation);

After this, I get a lower size of video with good quality.


问:
How can I use Tensorflow with react-native?
I've looked around the web and cant seem to find a way to use React Native with TensorFlow.

I don't think TF supports react-native (at least not officially) integration but i hope someone in the community has found a way.

How can one use TensorFlow in a React Native project?

Thanks.

CONTRIBUTOR UPDATE: Now is possible

答:
Came across the following Platform Adapter for React Native.

This package provides a TensorFlow.js platform adapter for react
  native. It provides GPU accelerated execution of TensorFlow.js
  supporting all major modes of tfjs usage

There is also a library react-native-tensorflow bridging tensorflow with react-native.

A TensorFlow inference library for react native.

They have recently added support for iOS, so it's available for both Android and iOS platforms.

To get started:

```<code>$ npm install react-native-tensorflow --save
</code>```

Perform library link:

```<code>$ react-native link react-native-tensorflow
</code>```

For iOS you will also need to use CocoaPods with content:

```<code>target '<ProjectName>'
       pod 'TensorFlow-experimental'
</code>```

There are performance limitations due to the nature of react-native bridge when transferring large data between native side and JS. For other known issues and usage check out the library's documentation.

Body:
Came across the following Platform Adapter for React Native.

This package provides a TensorFlow.js platform adapter for react
  native. It provides GPU accelerated execution of TensorFlow.js
  supporting all major modes of tfjs usage

There is also a library react-native-tensorflow bridging tensorflow with react-native.

A TensorFlow inference library for react native.

They have recently added support for iOS, so it's available for both Android and iOS platforms.

To get started:

$ npm install react-native-tensorflow --save

Perform library link:

$ react-native link react-native-tensorflow

For iOS you will also need to use CocoaPods with content:

target '<ProjectName>'
       pod 'TensorFlow-experimental'

There are performance limitations due to the nature of react-native bridge when transferring large data between native side and JS. For other known issues and usage check out the library's documentation.


问:
Cannot get property 'compileSdkVersion' on extra properties extension as it does not exist Open File
I imported a project downloaded from GitHub into my Android Studio project as module.
The "Import module..." wizard worked fine, but when the Adroid Studio tried to rebuild the project, it returned me this error:

```<code>Cannot get property 'compileSdkVersion' on extra properties extension as it does not exist Open File
</code>```

The error is related to this line in the "build.gradle" file of the imported module:

```<code>compileSdkVersion rootProject.compileSdkVersion
</code>```

I tried to add "ext" section in the project "build.gradle" like this:

```<code>ext {
    compileSdkVersion 26
}
</code>```

But in this way I receive a new error:

```<code>Gradle DSL method not found: 'compileSdkVersion()' Possible causes: ...
</code>```

答:
In your top-level file use:

```<code>ext {
    compileSdkVersion = 26
}
</code>```

In your module/build.gradle file use:

```<code>android {
  compileSdkVersion rootProject.ext.compileSdkVersion
  ...
}
</code>```

Body:
I imported a project downloaded from GitHub into my Android Studio project as module.
The "Import module..." wizard worked fine, but when the Adroid Studio tried to rebuild the project, it returned me this error:

Cannot get property 'compileSdkVersion' on extra properties extension as it does not exist Open File

The error is related to this line in the "build.gradle" file of the imported module:

compileSdkVersion rootProject.compileSdkVersion

I tried to add "ext" section in the project "build.gradle" like this:

ext {
    compileSdkVersion 26
}

But in this way I receive a new error:

Gradle DSL method not found: 'compileSdkVersion()' Possible causes: ...

Body:
In your top-level file use:

ext {
    compileSdkVersion = 26
}

In your module/build.gradle file use:

android {
  compileSdkVersion rootProject.ext.compileSdkVersion
  ...
}


问:
Horizontal Gradient with a rounded rect path?
I have a custom UIView I'm drawing using CoreGraphics. Using CoreGraphics, how would I set a horizontal gradient and clip it to my rounded rect path while still using a shadow? I wrote as much as the path and I can fill it with a color using setFill, but the gradient (a horizontal one, too) isn't abiding by the path...

EDIT: Since the time of posting (not so long ago), I figured out how to do a complex horizontal gradient, so now my only problem is drawing it within my CGContext's path.

答:
Well it seems I've found my own answer:

```<code>CGContextAddPath(context, path);
CGContextClosePath(context);
CGContextClip(context);
CGContextDrawLinearGradient(context, gradient, startPt, endPt, 0);
</code>```

Those lines of code perfectly clip it to the path of a rounded rect, or any path desired.

Body:
Well it seems I've found my own answer:

CGContextAddPath(context, path);
CGContextClosePath(context);
CGContextClip(context);
CGContextDrawLinearGradient(context, gradient, startPt, endPt, 0);

Those lines of code perfectly clip it to the path of a rounded rect, or any path desired.


问:
Spring Data JPA repository with specification, pagination and criteria fetch-join
I am implementing search/filtering service for list of entities, using Spring Data JPA repository with specifications and pagination features. I am trying to reduce number of queries (n+1 problem) and fetch nested data using criteria fetch mechanism.

I have two entity classes:

```<code>@Entity
@Table(name = "delegations")
public class Delegation {

    @Id
    @GeneratedValue(strategy = IDENTITY)
    private Long id;

    @ManyToOne
    private Customer customer;

    // more fields, getters, setters, business logic...

}
</code>```

and

```<code>@Entity
@Table(name = "customers")
public class Customer {

    @Id
    @GeneratedValue(strategy = IDENTITY)
    private Long id;

    // more fields, getters, setters, business logic...
}
</code>```

DTO filter class:

```<code>public class DelegationFilter {

    private String customerName;

    // more filters, getters, setters...
}
</code>```

And search / filtering service:

```<code>public class DelegationService {
    public Page<Delegation> findAll(DelegationFilter filter, Pageable page) {
        Specifications<Delegation> spec = Specifications.where(
                customerLike(filter.getCustomerName())
        );
        return delegationRepository.findAll(spec, page);
    }

    public List<Delegation> findAll(DelegationFilter filter) {
        Specifications<Delegation> spec = Specifications.where(
                customerLike(filter.getCustomerName())
        );
        return delegationRepository.findAll(spec);
    }

    private Specification<Delegation> customerLike(String customerName) {
        return (root, query, cb) -> {
            Join<Delegation,Customer> join = (Join) root.fetch(Delegation_.customer);
            return cb.like(cb.lower(join.get(Customer_.name)), addWildCards(customerName.toLowerCase()));
        };
    }

    private static String addWildCards(String param) {
        return '%' + param + '%';
    }
}
</code>```

Problem:

When I call findAll(DelegationFilter filter, Pageable page) I am getting exception:

```<code>org.springframework.dao.InvalidDataAccessApiUsageException:
org.hibernate.QueryException: query specified join fetching, but the owner
of the fetched association was not present in the select list
</code>```

Is there a way to solve this problem?

findAll(DelegationFilter filter) (method without pagination) works like charm... Using join only (without fetch) also works fine (even with pagination)

I know that there is solution for JPQL:
Spring-Data FETCH JOIN with Paging is not working
But I want to stick with criteria api...

I am using Spring Boot 1.4 (spring 4.3.2, spring-data-jpa 1.10.2) and Hibernate 5.0.9

答:
I was facing the same problem, and I found a workaround (source).

You can check the query's return type at runtime, so that if it is Long (the type the count query returns) you join and otherwise you fetch. In your code it will look like this:

```<code>...
private Specification<Delegation> customerLike(String customerName) {
    return (root, query, cb) -> {
        if (query.getResultType() != Long.class && query.getResultType() != long.class) {
          Join<Delegation,Customer> join = (Join) root.fetch(Delegation_.customer);
        } else {
          Join<Delegation,Customer> join = root.join(Delegation_.customer);
        }
        return cb.like(cb.lower(join.get(Customer_.name)), addWildCards(customerName.toLowerCase()));
    };
}
...
</code>```

I know it's not very clean, but it's the only solution I've ofund ATM.

Body:
I am implementing search/filtering service for list of entities, using Spring Data JPA repository with specifications and pagination features. I am trying to reduce number of queries (n+1 problem) and fetch nested data using criteria fetch mechanism.

I have two entity classes:

@Entity
@Table(name = "delegations")
public class Delegation {

    @Id
    @GeneratedValue(strategy = IDENTITY)
    private Long id;

    @ManyToOne
    private Customer customer;

    // more fields, getters, setters, business logic...

}

and

@Entity
@Table(name = "customers")
public class Customer {

    @Id
    @GeneratedValue(strategy = IDENTITY)
    private Long id;

    // more fields, getters, setters, business logic...
}

DTO filter class:

public class DelegationFilter {

    private String customerName;

    // more filters, getters, setters...
}

And search / filtering service:

public class DelegationService {
    public Page<Delegation> findAll(DelegationFilter filter, Pageable page) {
        Specifications<Delegation> spec = Specifications.where(
                customerLike(filter.getCustomerName())
        );
        return delegationRepository.findAll(spec, page);
    }

    public List<Delegation> findAll(DelegationFilter filter) {
        Specifications<Delegation> spec = Specifications.where(
                customerLike(filter.getCustomerName())
        );
        return delegationRepository.findAll(spec);
    }

    private Specification<Delegation> customerLike(String customerName) {
        return (root, query, cb) -> {
            Join<Delegation,Customer> join = (Join) root.fetch(Delegation_.customer);
            return cb.like(cb.lower(join.get(Customer_.name)), addWildCards(customerName.toLowerCase()));
        };
    }

    private static String addWildCards(String param) {
        return '%' + param + '%';
    }
}

Problem:

When I call findAll(DelegationFilter filter, Pageable page) I am getting exception:

org.springframework.dao.InvalidDataAccessApiUsageException:
org.hibernate.QueryException: query specified join fetching, but the owner
of the fetched association was not present in the select list

Is there a way to solve this problem?

findAll(DelegationFilter filter) (method without pagination) works like charm... Using join only (without fetch) also works fine (even with pagination)

I know that there is solution for JPQL:
Spring-Data FETCH JOIN with Paging is not working
But I want to stick with criteria api...

I am using Spring Boot 1.4 (spring 4.3.2, spring-data-jpa 1.10.2) and Hibernate 5.0.9

Body:
I was facing the same problem, and I found a workaround (source).

You can check the query's return type at runtime, so that if it is Long (the type the count query returns) you join and otherwise you fetch. In your code it will look like this:

...
private Specification<Delegation> customerLike(String customerName) {
    return (root, query, cb) -> {
        if (query.getResultType() != Long.class && query.getResultType() != long.class) {
          Join<Delegation,Customer> join = (Join) root.fetch(Delegation_.customer);
        } else {
          Join<Delegation,Customer> join = root.join(Delegation_.customer);
        }
        return cb.like(cb.lower(join.get(Customer_.name)), addWildCards(customerName.toLowerCase()));
    };
}
...

I know it's not very clean, but it's the only solution I've ofund ATM.


问:
Heap Sort Algorithm number of comparisons

I'm trying to count the number of comparisons in this heap sort algorithm:

```<code>import random
import time

#HeapSort Algorithm
def heapify(arr, n, i):
    count = 0
    largest = i
    l = 2 * i + 1
    r = 2 * i + 2
    if l < n and arr[i] < arr[l]:
        largest = l
    if r < n and arr[largest] < arr[r]:
        largest = r
    if largest != i:
        count += 1
        arr[i],arr[largest] = arr[largest],arr[i]
        heapify(arr, n, largest)
    return count

def heapSort(arr):

    n = len(arr)
    count = 0
    for i in range(n, -1, -1):
        heapify(arr, n, i)
        count += heapify(arr, i, 0)
    for i in range(n-1, 0, -1):
        arr[i], arr[0] = arr[0], arr[i]
        heapify(arr, i, 0)

    return count

print("For n = 1000:")

print("a) Random Generation:")

arr = [x for x in range(1000)]
random.shuffle(arr)

print("Before Sort:")
print (arr)

print("After Sort:")
start_time = time.time()
heapSort(arr)
time = time.time() - start_time
print(arr)

print("Comparisions")
print(heapSort(arr))
print("Time:")
print(time)
</code>```

I expect the result when n = 1000 integers to be 8421 and when n = 10000 to be 117681

However, each time it either shows 0 or 2001 when I try to count += 1 around the loops and not comparisons.

答:
You seem to be forgetting to take into account the comparisons your recursive solution makes while solving the smaller subproblems. In other words, you are only finding the comparisons made in the topmost level of your solution. Instead, you should update the count variable in the relevant scope whenever you make a call to your heapify function. Notice the updates below where I increased local count variables by the return value of calls to heapify.

```<code>def heapify(arr, n, i):
    count = 0
    largest = i
    l = 2 * i + 1
    r = 2 * i + 2
    if l < n and arr[i] < arr[l]:
        largest = l
    if r < n and arr[largest] < arr[r]:
        largest = r
    if largest != i:
        count += 1
        arr[i],arr[largest] = arr[largest],arr[i]
        count += heapify(arr, n, largest)
    return count

def heapSort(arr):
    n = len(arr)
    count = 0
    for i in range(n, -1, -1):
        heapify(arr, n, i)
        count += heapify(arr, i, 0)
    for i in range(n-1, 0, -1):
        arr[i], arr[0] = arr[0], arr[i]
        count += heapify(arr, i, 0)
    return count
</code>```

Here is a working example of your code including the fix given above. I understand that the output is still slightly different than the exact number of comparisons you are expecting, but it is in the ballpark. The relatively small distance is due to the fact that you are randomizing the initial state of the array.

Body:
I'm trying to count the number of comparisons in this heap sort algorithm:

import random
import time

#HeapSort Algorithm
def heapify(arr, n, i):
    count = 0
    largest = i
    l = 2 * i + 1
    r = 2 * i + 2
    if l < n and arr[i] < arr[l]:
        largest = l
    if r < n and arr[largest] < arr[r]:
        largest = r
    if largest != i:
        count += 1
        arr[i],arr[largest] = arr[largest],arr[i]
        heapify(arr, n, largest)
    return count

def heapSort(arr):

    n = len(arr)
    count = 0
    for i in range(n, -1, -1):
        heapify(arr, n, i)
        count += heapify(arr, i, 0)
    for i in range(n-1, 0, -1):
        arr[i], arr[0] = arr[0], arr[i]
        heapify(arr, i, 0)

    return count

print("For n = 1000:")

print("a) Random Generation:")

arr = [x for x in range(1000)]
random.shuffle(arr)

print("Before Sort:")
print (arr)

print("After Sort:")
start_time = time.time()
heapSort(arr)
time = time.time() - start_time
print(arr)

print("Comparisions")
print(heapSort(arr))
print("Time:")
print(time)

I expect the result when n = 1000 integers to be 8421 and when n = 10000 to be 117681

However, each time it either shows 0 or 2001 when I try to count += 1 around the loops and not comparisons.

Body:
You seem to be forgetting to take into account the comparisons your recursive solution makes while solving the smaller subproblems. In other words, you are only finding the comparisons made in the topmost level of your solution. Instead, you should update the count variable in the relevant scope whenever you make a call to your heapify function. Notice the updates below where I increased local count variables by the return value of calls to heapify.

def heapify(arr, n, i):
    count = 0
    largest = i
    l = 2 * i + 1
    r = 2 * i + 2
    if l < n and arr[i] < arr[l]:
        largest = l
    if r < n and arr[largest] < arr[r]:
        largest = r
    if largest != i:
        count += 1
        arr[i],arr[largest] = arr[largest],arr[i]
        count += heapify(arr, n, largest)
    return count

def heapSort(arr):
    n = len(arr)
    count = 0
    for i in range(n, -1, -1):
        heapify(arr, n, i)
        count += heapify(arr, i, 0)
    for i in range(n-1, 0, -1):
        arr[i], arr[0] = arr[0], arr[i]
        count += heapify(arr, i, 0)
    return count

Here is a working example of your code including the fix given above. I understand that the output is still slightly different than the exact number of comparisons you are expecting, but it is in the ballpark. The relatively small distance is due to the fact that you are randomizing the initial state of the array.


问:
JavaFX Directory Chooser - How to get the path from directory?
I would like to get the path from an Directory with the DirectoryChooser. My code looks like that:

```<code>DirectoryChooser dc = new DirectoryChooser();
dc.showDialog(stage);
File f = dc.getInitialDirectory();
String s = f.getAbsolutePath();
System.out.println(s);
</code>```

But that doesn't work, can somebody help me? The method getInitialDirectory() is always null

答:
It Should look something like this:

```<code>DirectoryChooser directoryChooser = new DirectoryChooser();
File selectedDirectory = directoryChooser.showDialog(stage);

if(selectedDirectory == null){
     //No Directory selected
}else{
     System.out.println(selectedDirectory.getAbsolutePath());
}
</code>```

more here: example of DirectoryChooser

Body:
I would like to get the path from an Directory with the DirectoryChooser. My code looks like that:

DirectoryChooser dc = new DirectoryChooser();
dc.showDialog(stage);
File f = dc.getInitialDirectory();
String s = f.getAbsolutePath();
System.out.println(s);

But that doesn't work, can somebody help me? The method getInitialDirectory() is always null

Body:
It Should look something like this:

DirectoryChooser directoryChooser = new DirectoryChooser();
File selectedDirectory = directoryChooser.showDialog(stage);

if(selectedDirectory == null){
     //No Directory selected
}else{
     System.out.println(selectedDirectory.getAbsolutePath());
}

more here: example of DirectoryChooser


问:
How to get ownership of a moved value back from a closure?
Consider the following program:

```<code>fn primes_up_to(n: usize) -> Vec<usize> {
    let mut ans = Vec::with_capacity(n);

    if n < 2 {
        return ans;
    }

    ans.push(2);

    // https://doc.rust-lang.org/1.22.0/book/first-edition/closures.html#closures-and-their-environment
    // The closure needs ownership of vec to access elements
    let is_prime = |n: usize| -> bool {
        for x in ans {
            if x * x > n {
                break;
            }

            if n % x == 0 {
                return false;
            }
        }

        true
    };

    let mut i = 3;
    while i <= n {
        if is_prime(i) {
            ans.push(i);
        }

        i += 2;
    }

    ans
}

fn main() {
    println!("{:?}", primes_up_to(23));
}
</code>```

(playground)

Compiling the code above gives these compilation errors:

error[E0382]: use of moved value: `is_prime`
  --> src/main.rs:28:12
   |
28 |         if is_prime(i) {
   |            ^^^^^^^^ value moved here in previous iteration of loop
   |
note: closure cannot be invoked more than once because it moves the variable `ans` out of its environment
  --> src/main.rs:13:18
   |
13 |         for x in ans {
   |                  ^^^

error[E0382]: use of moved value: `ans`
  --> src/main.rs:29:13
   |
12 |     let is_prime = |n: usize| -> bool {
   |                    ------------------ value moved (into closure) here
...
29 |             ans.push(i);
   |             ^^^ value used here after move
   |
   = note: move occurs because `ans` has type `std::vec::Vec<usize>`, which does not implement the `Copy` trait

error[E0382]: use of moved value: `ans`
  --> src/main.rs:35:5
   |
12 |     let is_prime = |n: usize| -> bool {
   |                    ------------------ value moved (into closure) here
...
35 |     ans
   |     ^^^ value used here after move
   |
   = note: move occurs because `ans` has type `std::vec::Vec<usize>`, which does not implement the `Copy` trait
```

I know a potential solution would be to pass an immutable reference to ans (&ans) to is_prime. What is the idiomatic way for ownership of ans to pass into is_prime and come back out when is_prime exits every time I call is_prime?

答:
How to get ownership of a moved value back from a closure

You don't.

Once ownership is transferred, it's gone. The only way to "get it back" is to have the thing that now owns it transfer it back. Some types have an into_inner method for this exact reason. Other types have equivalent methods.

A closure's only interface, by definition, is the ability to invoke it. There are no other possibilities for methods.

You could return the captured value when you are done, but that doesn't help in your case because you need to call the closure multiple times — you couldn't return it because it might be needed in the future.

Body:
Consider the following program:

fn primes_up_to(n: usize) -> Vec<usize> {
    let mut ans = Vec::with_capacity(n);

    if n < 2 {
        return ans;
    }

    ans.push(2);

    // https://doc.rust-lang.org/1.22.0/book/first-edition/closures.html#closures-and-their-environment
    // The closure needs ownership of vec to access elements
    let is_prime = |n: usize| -> bool {
        for x in ans {
            if x * x > n {
                break;
            }

            if n % x == 0 {
                return false;
            }
        }

        true
    };

    let mut i = 3;
    while i <= n {
        if is_prime(i) {
            ans.push(i);
        }

        i += 2;
    }

    ans
}

fn main() {
    println!("{:?}", primes_up_to(23));
}

(playground)

Compiling the code above gives these compilation errors:

error[E0382]: use of moved value: `is_prime`
  --> src/main.rs:28:12
   |
28 |         if is_prime(i) {
   |            ^^^^^^^^ value moved here in previous iteration of loop
   |
note: closure cannot be invoked more than once because it moves the variable `ans` out of its environment
  --> src/main.rs:13:18
   |
13 |         for x in ans {
   |                  ^^^

error[E0382]: use of moved value: `ans`
  --> src/main.rs:29:13
   |
12 |     let is_prime = |n: usize| -> bool {
   |                    ------------------ value moved (into closure) here
...
29 |             ans.push(i);
   |             ^^^ value used here after move
   |
   = note: move occurs because `ans` has type `std::vec::Vec<usize>`, which does not implement the `Copy` trait

error[E0382]: use of moved value: `ans`
  --> src/main.rs:35:5
   |
12 |     let is_prime = |n: usize| -> bool {
   |                    ------------------ value moved (into closure) here
...
35 |     ans
   |     ^^^ value used here after move
   |
   = note: move occurs because `ans` has type `std::vec::Vec<usize>`, which does not implement the `Copy` trait

I know a potential solution would be to pass an immutable reference to ans (&ans) to is_prime. What is the idiomatic way for ownership of ans to pass into is_prime and come back out when is_prime exits every time I call is_prime?


问:
Loops and map references
I have code that loops over an array of Task and creates a Map with string keys and Task values:

```<code>package main

import (
    "fmt"
)

type Task struct {
    Name    string
    Project string
}

func main() {
    taskMap := map[string]*Task{}
    taskList := []Task{
        {
            Name:    "name1",
            Project: "project1",
        },
        {
            Name:    "name2",
            Project: "project2",
        },
        {
            Name:    "name3",
            Project: "project3",
        },
    }
    for _, task := range taskList {
        taskMap[task.Name] = &task
    }

    // Print results
    for k, v := range taskMap {
        fmt.Println(k, v)
    }
}
</code>```

The output for this code is the following:

```<code>name1 &{name3 project3}
name2 &{name3 project3}
name3 &{name3 project3}
</code>```

This is not what I expected, since it is printing the same every time. I have a slight clue of what is going on here, because if I replace the loop with the following, it works as expected:

```<code>for _, task := range taskList {
        taskMap[task.Name] = &Task{
            Name:        task.Name,
            Project: task.Project,
        }
}
</code>```

So somehow the reference to the last visited task is the one that ends up stored in every key of taskMap, apparently.

Is there a way to do this without resorting to copying each field manually?

答:
You get the same value for all keys cause you are referencing the same task variable in your for loop. Use following snippet to get references to items in your slice.

```<code>for idx, task := range taskList {
    taskMap[task.Name] = &taskList[idx]
}
</code>```

BTW to (shallow) copy a variable you can assign it to a new variable like

```<code>for _, task := range taskList {
    x := task
    taskMap[task.Name] = &x
}
</code>```

a new x variable will be created in each iteration and a pointer to that will be used inside the map.

Title: Loops and map references

Body:
I have code that loops over an array of Task and creates a Map with string keys and Task values:

package main

import (
    "fmt"
)

type Task struct {
    Name    string
    Project string
}

func main() {
    taskMap := map[string]*Task{}
    taskList := []Task{
        {
            Name:    "name1",
            Project: "project1",
        },
        {
            Name:    "name2",
            Project: "project2",
        },
        {
            Name:    "name3",
            Project: "project3",
        },
    }
    for _, task := range taskList {
        taskMap[task.Name] = &task
    }

    // Print results
    for k, v := range taskMap {
        fmt.Println(k, v)
    }
}

The output for this code is the following:

name1 &{name3 project3}
name2 &{name3 project3}
name3 &{name3 project3}

This is not what I expected, since it is printing the same every time. I have a slight clue of what is going on here, because if I replace the loop with the following, it works as expected:

for _, task := range taskList {
        taskMap[task.Name] = &Task{
            Name:        task.Name,
            Project: task.Project,
        }
}

So somehow the reference to the last visited task is the one that ends up stored in every key of taskMap, apparently.

Is there a way to do this without resorting to copying each field manually?

Body:
You get the same value for all keys cause you are referencing the same task variable in your for loop. Use following snippet to get references to items in your slice.

for idx, task := range taskList {
    taskMap[task.Name] = &taskList[idx]
}

BTW to (shallow) copy a variable you can assign it to a new variable like

for _, task := range taskList {
    x := task
    taskMap[task.Name] = &x
}

a new x variable will be created in each iteration and a pointer to that will be used inside the map.


问:
How to split noise and text from the image for preprocessing of OCR
I am applying OCR against subtitle in TV footage. (I am using Tesseact 3.x w/ C++) I am trying to split text and background part as a preprocessing of OCR.

Here's the original image:

And, preprocessed image:

The OCR result is: Sicemn clone

As the above preprocessed image shown, there're some "fog" remained around the letter which prevents OCR module to do their job properly.

Is there any way to recognize those "fog" programatically to remove, or do some image processing to remove/reduce it from the preprocessed image?

Since preprocessed logic is heavily optimized to handle different images, I rather want to find a way to "clean" the preprocessed image, than modifying preprocessed logic (since optimizing to this pics can affecting to other pics)

Any suggestion is very welcome.

Update

Apparently, sixela's answer is great, and will work with most of the case.
The case it does not work is background also include similar color of text

Example of not working case:

Example of result:

Seemingly, Gaussian filter seems to cause a problem in this types of footage.
This implies, different footage may requires different approach.

答:
I managed to have a clearer (not perfect) image by using morphological operations and thresholding.

Here is how:

I started by converting the original image in greyscale

Applied a gaussian Blur (9x9 kernel) to denoise the greyscale image

Top Hat Morphological operation (3x3 kernel)to get the white text

Otsu thresholding method

dilation

Inverted binary threshold to get the white text in black

I finally obtained the following image

Which gives, as OCR results, this text: "Since vou don'k"

PS: This result can of course be improved by tweaking the parameters (kernel size for example) but i hope it can guide you. I used OpenCv in Python to quickly try out those methods.

```<code>import cv2

image = cv2.imread('./inputImg.png', 0)
imgBlur = cv2.GaussianBlur(image, (9, 9), 0)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
imgTH = cv2.morphologyEx(imgBlur, cv2.MORPH_TOPHAT, kernel)
_, imgBin = cv2.threshold(imgTH, 0, 250, cv2.THRESH_OTSU)

imgdil = cv2.dilate(imgBin, kernel)
_, imgBin_Inv = cv2.threshold(imgdil, 0, 250, cv2.THRESH_BINARY_INV)

cv2.imshow('original', image)
cv2.imshow('bin', imgBin)
cv2.imshow('dil', imgdil)
cv2.imshow('inv', imgBin_Inv)

cv2.imwrite('./output.png', imgBin_Inv)
cv2.waitKey(0)
</code>```

After this i tried the output image on Tesseract with this command:

```<code>tesseract output.png stdout
</code>```

Body:
I managed to have a clearer (not perfect) image by using morphological operations and thresholding.

Here is how:

I started by converting the original image in greyscale

Applied a gaussian Blur (9x9 kernel) to denoise the greyscale image

Top Hat Morphological operation (3x3 kernel)to get the white text

Otsu thresholding method

dilation

Inverted binary threshold to get the white text in black

I finally obtained the following image

Which gives, as OCR results, this text: "Since vou don'k"

PS: This result can of course be improved by tweaking the parameters (kernel size for example) but i hope it can guide you. I used OpenCv in Python to quickly try out those methods.

import cv2

image = cv2.imread('./inputImg.png', 0)
imgBlur = cv2.GaussianBlur(image, (9, 9), 0)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
imgTH = cv2.morphologyEx(imgBlur, cv2.MORPH_TOPHAT, kernel)
_, imgBin = cv2.threshold(imgTH, 0, 250, cv2.THRESH_OTSU)

imgdil = cv2.dilate(imgBin, kernel)
_, imgBin_Inv = cv2.threshold(imgdil, 0, 250, cv2.THRESH_BINARY_INV)

cv2.imshow('original', image)
cv2.imshow('bin', imgBin)
cv2.imshow('dil', imgdil)
cv2.imshow('inv', imgBin_Inv)

cv2.imwrite('./output.png', imgBin_Inv)
cv2.waitKey(0)

After this i tried the output image on Tesseract with this command:

tesseract output.png stdout


问:
How to populate lookup tables in Rails tests
I am using Cucumber, RSpec, and Factory Girl for the testing of my Rails application. But I have several lookup tables that contain mostly static data. So I'm trying to figure out the best way to populate these when testing. Doing them individually in FactoryGirl seems tedious and I'd like to stay away from fixtures. For development and production, I populate them in my seeds.rb file.

Thanks!

答:
Maybe something like

```<code>rake RAILS_ENV=test db:seed
</code>```

in your test helper file?

Body:
Maybe something like

rake RAILS_ENV=test db:seed

in your test helper file?


问:
can't find references when building c# project in jenkins
After that I import a c# project from TFS to a Jenkins workspace, I try to compile him without installing Visual Studio, but i get the error bellow :

CS0234: The type or namespace name 'Office' does not exist in the namespace 'Microsoft'

could someone give me a solution please ?

Thank you in advance,

答:
You probably are missing an assembly-reference from the Office-SDK.

Check the project for references to any Microsoft.Office.*.dll, which will most likely be referenced from GAC.

Installing the appropiate Office-SDK on the Build-Agent might solve the issue.

Or, consider packaging the Office SDK as nuget-package. This would involve provisioning a private nuget-feed for consumption of these packages from your dev and your build-machine. Also, have a look at this question, which covers this scenario in a broader way.


问:
AWS Lambda can't define config
I am currently running some code which works using the amazon sdk, but i am using my Key to authenticate to Refer:

```<code>AWS.config.update({
  accessKeyId: "ID",
  secretAccessKey: "secretAccessKey",
});
</code>```

When i upload it to the lambda, i dont want it to be needed to be used with any AWS code, is it possible?
on every GET that i use AWS to do a function, i get a

Execution failed due to configuration error: Malformed Lambda proxy response

Method completed with status: 502

And again on my client it works i just took off the AWS.config, because it is on the AWS platform i think it needs to without without it..
Am i mistaken? thanks.

答: After digging alot, I found out u dont need to configure the api, u just need to specify correct permission to the role u currently use with the Lambda function.</p>

Title: AWS Lambda can't define config

Body:
I am currently running some code which works using the amazon sdk, but i am using my Key to authenticate to Refer:

AWS.config.update({
  accessKeyId: "ID",
  secretAccessKey: "secretAccessKey",
});

When i upload it to the lambda, i dont want it to be needed to be used with any AWS code, is it possible?
on every GET that i use AWS to do a function, i get a

Execution failed due to configuration error: Malformed Lambda proxy response

Method completed with status: 502

And again on my client it works i just took off the AWS.config, because it is on the AWS platform i think it needs to without without it..
Am i mistaken? thanks.


问:
Connect multiple redux sub-states to a React component
In my react-redux-typescript app, I have multiple sub-states that form the application state. Application state is as follows.

```<code>interface AppState {
    dialogs: DialogState,
    messages: MessageState,
    notifications: NotificationsState,
    errors: ErrorState
    loading: LoadingState,
    status: StatusState;
}
</code>```

I use connect method for getting one of the sub-states available in a React component as props. But now I have a case where I need two sub-states properties available in a component as its props.

Specifically these parts:

```<code>interface LoadingState {
    size: number
    available: boolean
}

interface StatusState {
    name: string
}
</code>```

Usually, I use the connect method in component Loading as follows:

```<code>export default connect(
(state: AppState) => state.loading, actionCreators)(Loading);
</code>```

The header of the Loading component is:

```<code>type LoadingProps = LoadingState & typeof actionCreators;
class Loading extends React.Component<LoadingProps, {}>
</code>```

If I do that I have properties of the LoadingState interface available as props. But what should I do if I want also properties from the StatusState available as props in the same component?

答:
First your props need to also expect StatusState

type LoadingProps = LoadingState & StatusState & typeof actionCreators

Then your mapStateToProps function would just need to return an object containing all those properties mapped, easiest way is to use a spread (...) from the state values

```<code>export default connect(
  (state: AppState) => ({...state.loading, ...state.status }),
  actionCreators
)(Loading)
</code>```

Body:
In my react-redux-typescript app, I have multiple sub-states that form the application state. Application state is as follows.

interface AppState {
    dialogs: DialogState,
    messages: MessageState,
    notifications: NotificationsState,
    errors: ErrorState
    loading: LoadingState,
    status: StatusState;
}

I use connect method for getting one of the sub-states available in a React component as props. But now I have a case where I need two sub-states properties available in a component as its props.

Specifically these parts:

interface LoadingState {
    size: number
    available: boolean
}

interface StatusState {
    name: string
}

Usually, I use the connect method in component Loading as follows:

export default connect(
(state: AppState) => state.loading, actionCreators)(Loading);

The header of the Loading component is:

type LoadingProps = LoadingState & typeof actionCreators;
class Loading extends React.Component<LoadingProps, {}>

If I do that I have properties of the LoadingState interface available as props. But what should I do if I want also properties from the StatusState available as props in the same component?

Body:
First your props need to also expect StatusState

type LoadingProps = LoadingState & StatusState & typeof actionCreators

Then your mapStateToProps function would just need to return an object containing all those properties mapped, easiest way is to use a spread (...) from the state values

export default connect(
  (state: AppState) => ({...state.loading, ...state.status }),
  actionCreators
)(Loading)


问:
Get results in an Earth Engine python script
I'm trying to get NDVI mean in every polygon in a feature collection with earth engine python API.
I think that I succeeded getting the result (a feature collection in a feature collection), but then I don't know how to get data from it.
The data I want is IDs from features and ndvi mean in each feature.</p>

```<code>import datetime
import ee
ee.Initialize()

#Feature collection
fc = ee.FeatureCollection("ft:1s57dkY_Sg_E_COTe3sy1tIR_U-5Gw-BQNwHh4Xel");
fc_filtered = fc.filter(ee.Filter.equals('NUM_DECS', 1))
#Image collection
Sentinel_collection1 = (ee.ImageCollection('COPERNICUS/S2')).filterBounds(fc_filtered)
Sentinel_collection2 = Sentinel_collection1.filterDate(datetime.datetime(2017, 1, 1),datetime.datetime(2017, 8, 1))

# NDVI function to use with ee map
def NDVIcalc (image):
  red = image.select('B4')
  nir = image.select('B8')
  ndvi = nir.subtract(red).divide(nir.add(red)).rename('NDVI')

  #NDVI mean calculation with reduceRegions
  MeansFeatures = ndvi.reduceRegions(reducer= ee.Reducer.mean(),collection= fc_filtered,scale= 10)

  return (MeansFeatures)

#Result that I don't know to get the information: Features ID and NDVI mean
result = Sentinel_collection2.map(NDVIcalc)
</code>```

答:
If the result is small, you pull them into python using result.getInfo().  That will give you a python dictionary containing a list of FeatureCollection (which are more dictionaries).  However, if the results are large or the polygons cover large regions, you'll have to Export the collection instead.

That said, there are probably some other things you'll want to do first:

1) You might want to flatten() the collection, so it's not nested collections.  It'll be easier to handle that way.

2) You might want to add a date to each result so you know what time the result came from.  You can do that with a map on the result, inside your NDVIcalc function

```<code>return MeansFeatures.map(lambda f : f.set('date', image.date().format())
</code>```

3) If what you really want is a time-series of NDVI over time for each polygon (most common), then restructuring your code to map over polygons first will be easier:

```<code>Sentinel_collection = (ee.ImageCollection('COPERNICUS/S2')
    .filterBounds(fc_filtered)
    .filterDate(ee.Date('2017-01-01'),ee.Date('2017-08-01')))

def GetSeries(feature):
  def NDVIcalc(img):
    red = img.select('B4')
    nir = img.select('B8')
    ndvi = nir.subtract(red).divide(nir.add(red)).rename(['NDVI'])
    return (feature
            .set(ndvi.reduceRegion(ee.Reducer.mean(), feature.geometry(), 10))
            .set('date', img.date().format("YYYYMMdd")))

  series = Sentinel_collection.map(NDVIcalc)
  // Get the time-series of values as two lists.
  list = series.reduceColumns(ee.Reducer.toList(2), ['date', 'NDVI']).get('list')
  return feature.set(ee.Dictionary(ee.List(list).flatten()))

result = fc_filtered.map(GetSeries)
print(result.getInfo())
</code>```

4) And finally, if you're going to try to Export the result, you're likely to run into an issue where the columns of the exported table are selected from whatever columns the first feature has, so it's good to provide a "header" feature that has all columns (times), that you can merge() with the result as the first feature:

```<code># Get all possible dates.
dates = ee.List(Sentinel_collection.map(function(img) {
      return ee.Feature(null, {'date': img.date().format("YYYYMMdd") })
}).aggregate_array('date'))

# Make a default value for every date.
header = ee.Feature(null, ee.Dictionary(dates, ee.List.repeat(-1, dates.size())))
output = header.merge(result)
ee.batch.Export.table.toDrive(...)
</code>```

Body:
I'm trying to get NDVI mean in every polygon in a feature collection with earth engine python API.
I think that I succeeded getting the result (a feature collection in a feature collection), but then I don't know how to get data from it.
The data I want is IDs from features and ndvi mean in each feature.

import datetime
import ee
ee.Initialize()

#Feature collection
fc = ee.FeatureCollection("ft:1s57dkY_Sg_E_COTe3sy1tIR_U-5Gw-BQNwHh4Xel");
fc_filtered = fc.filter(ee.Filter.equals('NUM_DECS', 1))
#Image collection
Sentinel_collection1 = (ee.ImageCollection('COPERNICUS/S2')).filterBounds(fc_filtered)
Sentinel_collection2 = Sentinel_collection1.filterDate(datetime.datetime(2017, 1, 1),datetime.datetime(2017, 8, 1))

# NDVI function to use with ee map
def NDVIcalc (image):
  red = image.select('B4')
  nir = image.select('B8')
  ndvi = nir.subtract(red).divide(nir.add(red)).rename('NDVI')

  #NDVI mean calculation with reduceRegions
  MeansFeatures = ndvi.reduceRegions(reducer= ee.Reducer.mean(),collection= fc_filtered,scale= 10)

  return (MeansFeatures)

#Result that I don't know to get the information: Features ID and NDVI mean
result = Sentinel_collection2.map(NDVIcalc)

Body:
If the result is small, you pull them into python using result.getInfo().  That will give you a python dictionary containing a list of FeatureCollection (which are more dictionaries).  However, if the results are large or the polygons cover large regions, you'll have to Export the collection instead.

That said, there are probably some other things you'll want to do first:

1) You might want to flatten() the collection, so it's not nested collections.  It'll be easier to handle that way.

2) You might want to add a date to each result so you know what time the result came from.  You can do that with a map on the result, inside your NDVIcalc function

return MeansFeatures.map(lambda f : f.set('date', image.date().format())

3) If what you really want is a time-series of NDVI over time for each polygon (most common), then restructuring your code to map over polygons first will be easier:

Sentinel_collection = (ee.ImageCollection('COPERNICUS/S2')
    .filterBounds(fc_filtered)
    .filterDate(ee.Date('2017-01-01'),ee.Date('2017-08-01')))

def GetSeries(feature):
  def NDVIcalc(img):
    red = img.select('B4')
    nir = img.select('B8')
    ndvi = nir.subtract(red).divide(nir.add(red)).rename(['NDVI'])
    return (feature
            .set(ndvi.reduceRegion(ee.Reducer.mean(), feature.geometry(), 10))
            .set('date', img.date().format("YYYYMMdd")))

  series = Sentinel_collection.map(NDVIcalc)
  // Get the time-series of values as two lists.
  list = series.reduceColumns(ee.Reducer.toList(2), ['date', 'NDVI']).get('list')
  return feature.set(ee.Dictionary(ee.List(list).flatten()))

result = fc_filtered.map(GetSeries)
print(result.getInfo())

4) And finally, if you're going to try to Export the result, you're likely to run into an issue where the columns of the exported table are selected from whatever columns the first feature has, so it's good to provide a "header" feature that has all columns (times), that you can merge() with the result as the first feature:

# Get all possible dates.
dates = ee.List(Sentinel_collection.map(function(img) {
      return ee.Feature(null, {'date': img.date().format("YYYYMMdd") })
}).aggregate_array('date'))

# Make a default value for every date.
header = ee.Feature(null, ee.Dictionary(dates, ee.List.repeat(-1, dates.size())))
output = header.merge(result)
ee.batch.Export.table.toDrive(...)


问:
AWS Lex Android SDK start voice interaction without InteractiveVoiceView
I work with AWS Lex Android SDK 2.4.2

Is there any way to start voice interaction with Lex bot WITHOUT pushing InteractiveVoiceView component? For example after pushing custom button, changing fragment of other action.

答:
I found this solution:</p>

```<code>voiceView = (InteractiveVoiceView) mRootView.findViewById(R.id.voiceInterface_weight);
voiceView.performClick(); // simulating clicking on the InteractiveVoiceView
</code>```

Body:
I found this solution:

voiceView = (InteractiveVoiceView) mRootView.findViewById(R.id.voiceInterface_weight);
voiceView.performClick(); // simulating clicking on the InteractiveVoiceView


答:
You're storing the address of task rather than the value.

You can instead switch taskList to a slice of pointers: taskList := []*Task{ and use taskMap[task.Name] = task.

Title: Loops and map references


问:
repeated measures anova in Matlab, F-value interpretation
I am using this package for repeated measures anova for MATLAB.

However, I am not sure about the interpretation, and the code is not entirely documented. Say I have one group of people, and measurements from three timepoints (conditions) of those, hence a repeated measures ANOVA with 3 factors. Now I want to see, if there is a significant effect of condition - which F-value corresponds to this question? I would have said the one corresponding to the row "time", but for an $F_2,24$ the F-value on the row "Subject" which is around 12 should be what the F-table lists?

答: the F for time should be the effect of interest, since you are not interested in differences between subjects. F = 20.82 then. You have 3 time points and 2 degrees of freedom. In order to interpret the effect you should see which time point is different than the others.</p>


问:
"From" not expected on AS400 Update using a join
I am trying to update a column to today's date only if it matches data from a result set of a sub query.

I have it worked up in sql, but I am trying to convert to DB2 (Operating system: i5/OS Version: V5R4M0)

```<code>UPDATE TST.INV
SET  DLI = ?
FROM (SELECT Inv.PART, Inv.DLI
FROM TST.INV Inv left join TST.LOCS L ON Inv.PART = L.LPART
WHERE L.LPART IS NULL AND (IC != 'C') AND (I.PRICE < 1000)
) i
WHERE  PARTNO = i.PARTN
</code>```

This works fine using sql server, but AS400 gives me a, "From" not expected error.

答:
DB2 for i does not support updating from a sub-select. You can see on the manual page for the UPDATE statement that you can only specify a table or view name.

I think you could re-write it to do something like this, but obviously I have nothing to test with. :)

UPDATE TST.INV I
   SET DLI = ?
WHERE I.PRICE < 1000
  AND IC <> 'C'
  AND NOT EXISTS (
    SELECT 1
    FROM TST.LOCS L
    WHERE I.PART = L.LPART
  )
```

Body:
I am trying to update a column to today's date only if it matches data from a result set of a sub query.

I have it worked up in sql, but I am trying to convert to DB2 (Operating system: i5/OS Version: V5R4M0)

UPDATE TST.INV
SET  DLI = ?
FROM (SELECT Inv.PART, Inv.DLI
FROM TST.INV Inv left join TST.LOCS L ON Inv.PART = L.LPART
WHERE L.LPART IS NULL AND (IC != 'C') AND (I.PRICE < 1000)
) i
WHERE  PARTNO = i.PARTN

This works fine using sql server, but AS400 gives me a, "From" not expected error.


问:
Searching for string in the whole schema
I have Oracle database with many schemas. I'd like to find xyz string in all the tables in one specific schema. I've tried to do it as in accepted answer here (Search All Fields In All Tables For A Specific Value (Oracle)). Unfortunately, I get error:

3 ORA-00903: invalid table name
ORA-06512: at line 8 SQL.sql 30 2
```

<p>where line 8 is </p>

```SELECT COUNT(*) FROM ' || t.owner || '.' || t.table_name ||
```

How can I do this full search? I've also tried other googled solutions but none of them worked for me.

答:
You can adapt the original code to cope with tables (and schemas, and columns) created with case-sensitive or otherwise invalid object identifiers, by treating everything as a quoted identifier.

```<code>EXECUTE IMMEDIATE
  'SELECT COUNT(*) FROM "' || t.owner || '"."' || t.table_name || '"' ||
  ' WHERE "'||t.column_name||'" = :1'
  INTO match_count
  USING '1/22/2008P09RR8';
</code>```

but using whatever string you're actually looking for, of course.

In the dynamic SQL that generates, the owner, table name and column name are now all enclosed in double quotes - which is what @CaiusJard is doing, but this still executes separate queries inside an anonymous block.

Body:
I have Oracle database with many schemas. I'd like to find xyz string in all the tables in one specific schema. I've tried to do it as in accepted answer here (Search All Fields In All Tables For A Specific Value (Oracle)). Unfortunately, I get error:

3 ORA-00903: invalid table name
ORA-06512: at line 8 SQL.sql 30 2

where line 8 is

SELECT COUNT(*) FROM ' || t.owner || '.' || t.table_name ||

How can I do this full search? I've also tried other googled solutions but none of them worked for me.

Body:
You can adapt the original code to cope with tables (and schemas, and columns) created with case-sensitive or otherwise invalid object identifiers, by treating everything as a quoted identifier.

EXECUTE IMMEDIATE
  'SELECT COUNT(*) FROM "' || t.owner || '"."' || t.table_name || '"' ||
  ' WHERE "'||t.column_name||'" = :1'
  INTO match_count
  USING '1/22/2008P09RR8';

but using whatever string you're actually looking for, of course.

In the dynamic SQL that generates, the owner, table name and column name are now all enclosed in double quotes - which is what @CaiusJard is doing, but this still executes separate queries inside an anonymous block.


问:
Django getting csrf error
I've made a register form, usually it works, but sometimes it gives me the csrf error, after i go back to the register page, it works again:

```<code>Forbidden (403)
CSRF verification failed. Request aborted.
</code>```

This is the register form i've made:

views.py

```<code>def register(request):
    data = dict()
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            email = cd['email']
            password = cd['password1']
            new_user = CustomUser.objects.create_user(
                email=email,
                password=password,
            )
            new_user.valid_email = False
            new_user.save()
            current_site = get_current_site(request)
            subject = 'Activate your account.'
            message = render_to_string('registration/account_activation_email.html', {
                'user': new_user,
                'domain': current_site.domain,
                'uid': urlsafe_base64_encode(force_bytes(new_user.pk)),
                'token': account_activation_token.make_token(new_user),
            })
            new_user.email_user(subject, message)
            return redirect('account:account_activation_sent')
    else:
        form = RegistrationForm()
    data['form'] = form
    return render(request, 'registration/register.html', data)
</code>```

In register.html i am using {% csrf_token %}:

register.html

```<code><form method="POST" class="m-login__form m-form" action="">
    {% csrf_token %}
    {{ user_form.non_field_errors }}
    {{ user_form.email.errors }}
    <div class="form-group m-form__group">
        <input type="text" placeholder="E-mail*" name="email" autocomplete="off">
    </div>
    {{ user_form.password1.errors }}
    <div class="form-group m-form__group">
        <input type="password" placeholder="Parola*" name="password1">
    </div>
    {{ user_form.password2.errors }}
    <div class="form-group m-form__group">
        <input type="password" placeholder="Confirmati parola*" name="password2">
    </div>
    <div class="m-login__form-action">
        <button id="m_login_signup_submit" class="btn m-btn m-btn--pill m-btn--custom m-btn--air m-login__btn m-login__btn--primary">
            Sign Up
        </button>
    </div>
</form>
</code>```

What could be the problem?

答:
As you are using csrf middlewares, you need to add csrf permissions to you API, use below given decorators for your API,

```<code>from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def register(request):
data = dict()
if request.method == 'POST':
    form = RegistrationForm(request.POST)
    if form.is_valid():
        cd = form.cleaned_data
        email = cd['email']
        password = cd['password1']
        new_user = CustomUser.objects.create_user(
            email=email,
            password=password,
        )
        new_user.valid_email = False
        new_user.save()
        current_site = get_current_site(request)
        subject = 'Activate your account.'
        message = render_to_string('registration/account_activation_email.html', {
            'user': new_user,
            'domain': current_site.domain,
            'uid': urlsafe_base64_encode(force_bytes(new_user.pk)),
            'token': account_activation_token.make_token(new_user),
        })
        new_user.email_user(subject, message)
        return redirect('account:account_activation_sent')
else:
    form = RegistrationForm()
data['form'] = form
return render(request, 'registration/register.html', data)
</code>```

Hope, this will solve the problem.

Title: Django getting csrf error

Body:
I've made a register form, usually it works, but sometimes it gives me the csrf error, after i go back to the register page, it works again:

Forbidden (403)
CSRF verification failed. Request aborted.

This is the register form i've made:

views.py

def register(request):
    data = dict()
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            email = cd['email']
            password = cd['password1']
            new_user = CustomUser.objects.create_user(
                email=email,
                password=password,
            )
            new_user.valid_email = False
            new_user.save()
            current_site = get_current_site(request)
            subject = 'Activate your account.'
            message = render_to_string('registration/account_activation_email.html', {
                'user': new_user,
                'domain': current_site.domain,
                'uid': urlsafe_base64_encode(force_bytes(new_user.pk)),
                'token': account_activation_token.make_token(new_user),
            })
            new_user.email_user(subject, message)
            return redirect('account:account_activation_sent')
    else:
        form = RegistrationForm()
    data['form'] = form
    return render(request, 'registration/register.html', data)

In register.html i am using {% csrf_token %}:

register.html

<form method="POST" class="m-login__form m-form" action="">
    {% csrf_token %}
    {{ user_form.non_field_errors }}
    {{ user_form.email.errors }}

        <input type="text" placeholder="E-mail*" name="email" autocomplete="off">

    {{ user_form.password1.errors }}

        <input type="password" placeholder="Parola*" name="password1">

    {{ user_form.password2.errors }}

        <input type="password" placeholder="Confirmati parola*" name="password2">

        <button id="m_login_signup_submit" class="btn m-btn m-btn--pill m-btn--custom m-btn--air m-login__btn m-login__btn--primary">
            Sign Up
        </button>

</form>

What could be the problem?

Body:
As you are using csrf middlewares, you need to add csrf permissions to you API, use below given decorators for your API,

from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def register(request):
data = dict()
if request.method == 'POST':
    form = RegistrationForm(request.POST)
    if form.is_valid():
        cd = form.cleaned_data
        email = cd['email']
        password = cd['password1']
        new_user = CustomUser.objects.create_user(
            email=email,
            password=password,
        )
        new_user.valid_email = False
        new_user.save()
        current_site = get_current_site(request)
        subject = 'Activate your account.'
        message = render_to_string('registration/account_activation_email.html', {
            'user': new_user,
            'domain': current_site.domain,
            'uid': urlsafe_base64_encode(force_bytes(new_user.pk)),
            'token': account_activation_token.make_token(new_user),
        })
        new_user.email_user(subject, message)
        return redirect('account:account_activation_sent')
else:
    form = RegistrationForm()
data['form'] = form
return render(request, 'registration/register.html', data)

Hope, this will solve the problem.


问:
Remove duplicates from dataframe, based on two columns A,B, keeping row with max value in another column C
I have a pandas dataframe which contains duplicates values according to two columns (A and B):

```<code>A B C
1 2 1
1 2 4
2 7 1
3 4 0
3 4 8
</code>```

I want to remove duplicates keeping the row with max value in column C. This would lead to:

```<code>A B C
1 2 4
2 7 1
3 4 8
</code>```

I cannot figure out how to do that. Should I use drop_duplicates(), something else?

答:
You can do it with drop_duplicates as you wanted

```<code># initialisation
d = pd.DataFrame({'A' : [1,1,2,3,3], 'B' : [2,2,7,4,4],  'C' : [1,4,1,0,8]})

d = d.sort_values("C", ascending=False)
d = d.drop_duplicates(["A","B"])
</code>```

If it's important to get the same order

```<code>d = d.sort_index()
</code>```

Body:
I have a pandas dataframe which contains duplicates values according to two columns (A and B):

A B C
1 2 1
1 2 4
2 7 1
3 4 0
3 4 8

I want to remove duplicates keeping the row with max value in column C. This would lead to:

A B C
1 2 4
2 7 1
3 4 8

I cannot figure out how to do that. Should I use drop_duplicates(), something else?

Body:
You can do it with drop_duplicates as you wanted

# initialisation
d = pd.DataFrame({'A' : [1,1,2,3,3], 'B' : [2,2,7,4,4],  'C' : [1,4,1,0,8]})

d = d.sort_values("C", ascending=False)
d = d.drop_duplicates(["A","B"])

If it's important to get the same order

d = d.sort_index()


问:
Scrapy Retrieve Symblos in json if content in arabic
```<code>import scrapy

class QuotesSpider(scrapy.Spider):
    name = "quotes"
    start_urls = [
        'http://quotes.toscrape.com/page/1/',
        'http://quotes.toscrape.com/page/2/',
    ]

    def parse(self, response):
        for test in response.css('div.news-content'):
            yield {
                'author': test.css('h1::text').extract_first(),
            }
</code>```

and run this command line:
scrapy runspider quotes.py -o quotes.json

if this title contain Arabic content it retrieve this in json:

```<code>[
    {
        "author": "\u0643\u062a\u0628 \u0631\u0627\u0645\u0649 \u0646\u0627\u062c\u0649"
    }
]
</code>```

how can i get this json in arabic ?

答:
Try encoding the JSON as UTF-8 byte:

```<code>import json

x = [ { "author": "\u0643\u062a\u0628 \u0631\u0627\u0645\u0649 \u0646\u0627\u062c\u0649" } ]

with open('f.json','wb') as f:
    f.write(json.dumps(x,ensure_ascii=False).encode('utf8'))
</code>```

Output file:

Body:
import scrapy

class QuotesSpider(scrapy.Spider):
    name = "quotes"
    start_urls = [
        'http://quotes.toscrape.com/page/1/',
        'http://quotes.toscrape.com/page/2/',
    ]

    def parse(self, response):
        for test in response.css('div.news-content'):
            yield {
                'author': test.css('h1::text').extract_first(),
            }

and run this command line:
scrapy runspider quotes.py -o quotes.json

if this title contain Arabic content it retrieve this in json:

[
    {
        "author": "\u0643\u062a\u0628 \u0631\u0627\u0645\u0649 \u0646\u0627\u062c\u0649"
    }
]

how can i get this json in arabic ?

Body:
Try encoding the JSON as UTF-8 byte:

import json

x = [ { "author": "\u0643\u062a\u0628 \u0631\u0627\u0645\u0649 \u0646\u0627\u062c\u0649" } ]

with open('f.json','wb') as f:
    f.write(json.dumps(x,ensure_ascii=False).encode('utf8'))

Output file:


问:
Python syntax error_ maybe indentation issue
Hi~  I have this syntax error in Python:

```<code>while true:
    print("Please type your name.")
    name = input()
    if name == "your name":
        break
print("Thanks!")
</code>```

But looks like the code "print(Thanks!)" has syntax error~  I don't know why~ Thanks!

答:
Use the code below for Python 2.7

And never ask your syntax problems.

```<code>while True:
    print("Please type your name.")
    name = raw_input()
    if name == "your name":
        break
print("Thanks!")
</code>```

Body:
Hi~  I have this syntax error in Python:

while true:
    print("Please type your name.")
    name = input()
    if name == "your name":
        break
print("Thanks!")

But looks like the code "print(Thanks!)" has syntax error~  I don't know why~ Thanks!

Body:
Use the code below for Python 2.7

And never ask your syntax problems.

while True:
    print("Please type your name.")
    name = raw_input()
    if name == "your name":
        break
print("Thanks!")


问:
How to check if value is greater than another using javascript
I'm using javascript to check if the value entered by the user is greater than the value there. But when I click update it just redirect to another page without giving the error message.

Javascript

```<code><script type="text/javascript">

$(function onUpdateClick () {
    var initVal = $('#quantity').val();

    $('#quantity').change(function () {
        if ($('#quantityI').val() > initVal)
            console.log('Quantity issued should be less than quantity requested');
        else {

            location.href = '@Url.Action("Index","Issue")';
        }
    });
</code>```

View

```<code>   <div class="form-group">
                        @Html.LabelFor(model => model.item.quantity, htmlAttributes: new { @class = "control-label col-md-2" })
                        @Html.EditorFor(model => model.item.quantity, new { htmlAttributes = new { @class = "form-control" , @readonly="readonly", @id=quantity} })
                        @Html.ValidationMessageFor(model => model.item.quantity, "", new { @class = "text-danger" })
                    </div>

                <div class="form-group">
                    @Html.LabelFor(model => model.item.quantityI, htmlAttributes: new { @class = "control-label col-md-2" })
                    @Html.EditorFor(model => model.item.quantityI, new { htmlAttributes = new { @class = "form-control", @id=quantityI } })
                    @Html.ValidationMessageFor(model => model.item.quantityI, "", new { @class = "text-danger" })
                </div>
</code>```

Model
    [Required]

```<code>    [Range(0.5,double.MaxValue, ErrorMessage = "You must enter an amount greater than Zero")]
    [Display(Name ="Quantity Requested")]
    public double quantity { get; set; }

    [Required]
    [Range(0.5, double.MaxValue, ErrorMessage = "You must enter an amount greater than Zero")]
    [Display(Name = "Quantity Issued")]
    public double quantityI { get; set; }
</code>```

答:
I found issue with you code , you need to make use of parseInt() function when fetching value.

Following is working

```<code><input type="Text" id="quantity"  />
<input type="Text" id="quantityI" />

$(function()
{
   $('#quantity').change(function ()
    {
      var quantityI =  parseInt($('#quantityI').val());
      var quantity = parseInt($('#quantity').val());
      alert(quantityI);alert(quantity);
     if( quantityI > quantity)
      {
        alert('Quantity issued should be less than quantity requested');
      }
     else {
        alert('Else called');
       }
    });
 });
</code>```

Working Demo :  jsfiddle

Body:
I found issue with you code , you need to make use of parseInt() function when fetching value.

Following is working

<input type="Text" id="quantity"  />
<input type="Text" id="quantityI" />

$(function()
{
   $('#quantity').change(function ()
    {
      var quantityI =  parseInt($('#quantityI').val());
      var quantity = parseInt($('#quantity').val());
      alert(quantityI);alert(quantity);
     if( quantityI > quantity)
      {
        alert('Quantity issued should be less than quantity requested');
      }
     else {
        alert('Else called');
       }
    });
 });

Working Demo :  jsfiddle


问:
limiting versionControl.GetAllTeamProjects to specific folders
I am not a .net person.  I use a tool whose c# code I took right off the internet to scan tfs for specific strings in files whose name patterns I can enter in a string list variable.  Examples of file name patterns are ".dtsx",".rdl" etc.  The critical lines of code are shown below.  A friend of mine who has been to more depts in the company than I and who probably has permissions on more tfs directories than I complained that the tool runs longer for her than the rest of us.  Presumably because more tfs directories are being searched.  Does someone know how I might limit these lines of code to search only the directory patterns entered in a list?

var allProjs = versionControl.GetAllTeamProjects(true);enter code here
foreach (var teamProj in allProjs)
{

答: yes, it looks like qualifying the file pattern list with directory nodes did the trick.  My friend's search time went from 20 minutes to about 20 seconds looking for one string.  </p>