问:
JPA exception: Object: ... is not a known entity type
I'm new to JPA and I'm having problems with the autogeneration of primary key values.

I have the following entity:

```<code>package jpatest.entities;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class MyEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    private String someProperty;

    public String getSomeProperty() {
        return someProperty;
    }

    public void setSomeProperty(String someProperty) {
        this.someProperty = someProperty;
    }

    public MyEntity() {
    }

    public MyEntity(String someProperty) {
        this.someProperty = someProperty;
    }

    @Override
    public String toString() {
        return "jpatest.entities.MyEntity[id=" + id + "]";
    }
}
</code>```

and the following main method in other class:

```<code>public static void main(String[] args) {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPATestPU");
    EntityManager em = emf.createEntityManager();

    em.getTransaction().begin();

    MyEntity e = new MyEntity("some value");
    em.persist(e); /* (exception thrown here) */

    em.getTransaction().commit();

    em.close();
    emf.close();
}
</code>```

This is my persistence unit:

```<code><?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
  <persistence-unit name="JPATestPU" transaction-type="RESOURCE_LOCAL">
    <provider>oracle.toplink.essentials.PersistenceProvider</provider>
    <class>jpatest.entities.MyEntity</class>
    <properties>
      <property name="toplink.jdbc.user" value="..."/>
      <property name="toplink.jdbc.password" value="..."/>
      <property name="toplink.jdbc.url" value="jdbc:mysql://localhost:3306/jpatest"/>
      <property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
      <property name="toplink.ddl-generation" value="create-tables"/>
    </properties>
  </persistence-unit>
</persistence>
</code>```

When I execute the program I get the following exception in the line marked with the proper comment:

```<code>Exception in thread "main" java.lang.IllegalArgumentException: Object: jpatest.entities.MyEntity[id=null] is not a known entity type.
        at oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:3212)
        at oracle.toplink.essentials.internal.ejb.cmp3.base.EntityManagerImpl.persist(EntityManagerImpl.java:205)
        at jpatest.Main.main(Main.java:...)
</code>```

What am I missing?

答:
The combination of deployment from within NetBeans 8.2 on Glassfish 4.1 on a Maven project with the "Debug" function of a project can cause an outdated version to be re-deployed (unclear where the fault lies).

Stop GlassFish, delete [glassfish base]/glassfish/domains/[domain name]/generated/, restart and redeploy.

Body:
I'm new to JPA and I'm having problems with the autogeneration of primary key values.

I have the following entity:

package jpatest.entities;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class MyEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    private String someProperty;

    public String getSomeProperty() {
        return someProperty;
    }

    public void setSomeProperty(String someProperty) {
        this.someProperty = someProperty;
    }

    public MyEntity() {
    }

    public MyEntity(String someProperty) {
        this.someProperty = someProperty;
    }

    @Override
    public String toString() {
        return "jpatest.entities.MyEntity[id=" + id + "]";
    }
}

and the following main method in other class:

public static void main(String[] args) {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPATestPU");
    EntityManager em = emf.createEntityManager();

    em.getTransaction().begin();

    MyEntity e = new MyEntity("some value");
    em.persist(e); /* (exception thrown here) */

    em.getTransaction().commit();

    em.close();
    emf.close();
}

This is my persistence unit:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
  <persistence-unit name="JPATestPU" transaction-type="RESOURCE_LOCAL">
    <provider>oracle.toplink.essentials.PersistenceProvider</provider>
    <class>jpatest.entities.MyEntity</class>
    <properties>
      <property name="toplink.jdbc.user" value="..."/>
      <property name="toplink.jdbc.password" value="..."/>
      <property name="toplink.jdbc.url" value="jdbc:mysql://localhost:3306/jpatest"/>
      <property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
      <property name="toplink.ddl-generation" value="create-tables"/>
    </properties>
  </persistence-unit>
</persistence>

When I execute the program I get the following exception in the line marked with the proper comment:

Exception in thread "main" java.lang.IllegalArgumentException: Object: jpatest.entities.MyEntity[id=null] is not a known entity type.
        at oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:3212)
        at oracle.toplink.essentials.internal.ejb.cmp3.base.EntityManagerImpl.persist(EntityManagerImpl.java:205)
        at jpatest.Main.main(Main.java:...)

What am I missing?


来源: stackexchange

问:
Tensorflow: Linear regression with non-negative constraints
I am trying to implement a linear regression model in Tensorflow, with additional constraints (coming from the domain) that the W and b terms must be non-negative.

I believe there are a couple of ways to do this.

We can modify the cost function to penalize negative weights [Lagrangian approach] [See:TensorFlow - best way to implement weight constraints

We can compute the gradients ourselves and project them on [0, infinity] [Projected gradient approach]

Approach 1: Lagrangian

When I tried the first approach, I would often end up with negative b.

I had modified the cost function from:

```<code>cost = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples)
</code>```

to:

```<code>cost = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples)
nn_w = tf.reduce_sum(tf.abs(W) - W)
nn_b = tf.reduce_sum(tf.abs(b) - b)
constraint = 100.0*nn_w + 100*nn_b
cost_with_constraint = cost + constraint
</code>```

Keeping the coefficient of nn_b and nn_w to be very high leads to instability and very high cost.

Here is the complete code.

```<code>import numpy as np
import tensorflow as tf

n_samples = 50
train_X = np.linspace(1, 50, n_samples)
train_Y = 10*train_X + 6 +40*np.random.randn(50)

X = tf.placeholder("float")
Y = tf.placeholder("float")

# Set model weights
W = tf.Variable(np.random.randn(), name="weight")
b = tf.Variable(np.random.randn(), name="bias")

# Construct a linear model
pred = tf.add(tf.multiply(X, W), b)

# Gradient descent
learning_rate=0.0001
# Initializing the variables
init = tf.global_variables_initializer()

# Mean squared error
cost = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples)
nn_w = tf.reduce_sum(tf.abs(W) - W)
nn_b = tf.reduce_sum(tf.abs(b) - b)
constraint = 1.0*nn_w + 100*nn_b
cost_with_constraint = cost + constraint
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost_with_constraint)

training_epochs=200
with tf.Session() as sess:
    sess.run(init)

    # Fit all training data
    cost_array = np.zeros(training_epochs)
    W_array = np.zeros(training_epochs)
    b_array = np.zeros(training_epochs)

    for epoch in range(training_epochs):
        for (x, y) in zip(train_X, train_Y):
            sess.run(optimizer, feed_dict={X: x, Y: y})
            W_array[epoch] = sess.run(W)
            b_array[epoch] = sess.run(b)
            cost_array[epoch] = sess.run(cost, feed_dict={X: train_X, Y: train_Y})
</code>```

The following is the mean of b across 10 different runs.

```<code>0   -1.101268
1    0.169225
2    0.158363
3    0.706270
4   -0.371205
5    0.244424
6    1.312516
7   -0.069609
8   -1.032187
9   -1.711668
</code>```

Clearly, the first approach is not optimal. Further, there is a lot of art involved in choosing the coefficient of penalty terms.

Approach 2: Projected gradient

I then thought to use the second approach, which is more guaranteed to work.

```<code>gr = tf.gradients(cost, [W, b])
</code>```

We manually compute the gradients and update the W and b.

```<code> with tf.Session() as sess:
    sess.run(init)

    for epoch in range(training_epochs):
        for (x, y) in zip(train_X, train_Y):
            W_del, b_del = sess.run(gr, feed_dict={X: x, Y: y})
            W = max(0, (W - W_del)*learning_rate) #Project the gradient on [0, infinity]
            b = max(0, (b - b_del)*learning_rate) # Project the gradient on [0, infinity]
</code>```

This approach seems to be very slow.

I am wondering if there is a better way to run the second approach, or guarantee the results with the first approach. Can we somehow allow the optimizer to ensure that the learnt weights are non-negative?

Edit: How to do this in Autograd

https://github.com/HIPS/autograd/issues/207

答:
I actually was not able to reproduce your problem of getting negative bs with your first approach.
But I do agree that this is not optimal for your use case and can result in negative values.

You should be able to constrain your parameters to non-negative values like so:

```<code>W *= tf.cast(W > 0., tf.float32)
b *= tf.cast(b > 0., tf.float32)
</code>```

(exchange > with >= if necessary, the cast is necessary as the comparison operators will produce boolean values.
You then would optimize for the "standard cost" without the additional constraints.
However, this does not work in every case. For example, it should be avoided to initialize W or b with negative values in the beginning.

Your second (and probably better) approach can be accelerated by defining the update logic in the general computational graph, i.e. after the definition of cost

```<code>params = [W, b]
grads = tf.gradients(cost, params)
optimizer = [tf.assign(param, tf.maximum(0., param - grad*learning_rate))
             for param, grad in zip(params, grads)]
</code>```

I think your solution is slow because it creates new computation nodes every time which is probably very costly and repeated a lot inside the loops.

update using tensorflow optimizer

In my solution above not the gradients are clipped but rather the resulting update values.
Along the lines of this answer you could clip the gradients to be at most the value of the updated parameter like so:

```<code>params = [W, b]
opt = tf.train.GradientDescentOptimizer(learning_rate)
grads_and_vars = opt.compute_gradients(cost, params)
clipped_grads_vars = [(tf.clip_by_value(grad, -np.inf, var), var) for grad, var in grads_and_vars]
optimizer = opt.apply_gradients(clipped_grads_vars)
</code>```

This way an update will never decrease a parameter to a value below 0.
However, I think this will not work in the case the updated variable is already negative.
Also, if the optimizing algorithm somehow multiplies the clipped gradient by a value greater than 1.
The latter might actually never happen, but I'm not 100% sure.

ContentLicense: CC BY-SA 3.0

Body:
I am trying to implement a linear regression model in Tensorflow, with additional constraints (coming from the domain) that the W and b terms must be non-negative.

I believe there are a couple of ways to do this.

We can modify the cost function to penalize negative weights [Lagrangian approach] [See:TensorFlow - best way to implement weight constraints

We can compute the gradients ourselves and project them on [0, infinity] [Projected gradient approach]

Approach 1: Lagrangian

When I tried the first approach, I would often end up with negative b.

I had modified the cost function from:

cost = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples)

to:

cost = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples)
nn_w = tf.reduce_sum(tf.abs(W) - W)
nn_b = tf.reduce_sum(tf.abs(b) - b)
constraint = 100.0*nn_w + 100*nn_b
cost_with_constraint = cost + constraint

Keeping the coefficient of nn_b and nn_w to be very high leads to instability and very high cost.

Here is the complete code.

import numpy as np
import tensorflow as tf

n_samples = 50
train_X = np.linspace(1, 50, n_samples)
train_Y = 10*train_X + 6 +40*np.random.randn(50)

X = tf.placeholder("float")
Y = tf.placeholder("float")

# Set model weights
W = tf.Variable(np.random.randn(), name="weight")
b = tf.Variable(np.random.randn(), name="bias")

# Construct a linear model
pred = tf.add(tf.multiply(X, W), b)

# Gradient descent
learning_rate=0.0001
# Initializing the variables
init = tf.global_variables_initializer()

# Mean squared error
cost = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples)
nn_w = tf.reduce_sum(tf.abs(W) - W)
nn_b = tf.reduce_sum(tf.abs(b) - b)
constraint = 1.0*nn_w + 100*nn_b
cost_with_constraint = cost + constraint
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost_with_constraint)

training_epochs=200
with tf.Session() as sess:
    sess.run(init)

    # Fit all training data
    cost_array = np.zeros(training_epochs)
    W_array = np.zeros(training_epochs)
    b_array = np.zeros(training_epochs)

    for epoch in range(training_epochs):
        for (x, y) in zip(train_X, train_Y):
            sess.run(optimizer, feed_dict={X: x, Y: y})
            W_array[epoch] = sess.run(W)
            b_array[epoch] = sess.run(b)
            cost_array[epoch] = sess.run(cost, feed_dict={X: train_X, Y: train_Y})

The following is the mean of b across 10 different runs.

0   -1.101268
1    0.169225
2    0.158363
3    0.706270
4   -0.371205
5    0.244424
6    1.312516
7   -0.069609
8   -1.032187
9   -1.711668

Clearly, the first approach is not optimal. Further, there is a lot of art involved in choosing the coefficient of penalty terms.

Approach 2: Projected gradient

I then thought to use the second approach, which is more guaranteed to work.

gr = tf.gradients(cost, [W, b])

We manually compute the gradients and update the W and b.

 with tf.Session() as sess:
    sess.run(init)

    for epoch in range(training_epochs):
        for (x, y) in zip(train_X, train_Y):
            W_del, b_del = sess.run(gr, feed_dict={X: x, Y: y})
            W = max(0, (W - W_del)*learning_rate) #Project the gradient on [0, infinity]
            b = max(0, (b - b_del)*learning_rate) # Project the gradient on [0, infinity]

This approach seems to be very slow.

I am wondering if there is a better way to run the second approach, or guarantee the results with the first approach. Can we somehow allow the optimizer to ensure that the learnt weights are non-negative?

Edit: How to do this in Autograd

https://github.com/HIPS/autograd/issues/207

Body:
I actually was not able to reproduce your problem of getting negative bs with your first approach.
But I do agree that this is not optimal for your use case and can result in negative values.

You should be able to constrain your parameters to non-negative values like so:

W *= tf.cast(W > 0., tf.float32)
b *= tf.cast(b > 0., tf.float32)

(exchange > with >= if necessary, the cast is necessary as the comparison operators will produce boolean values.
You then would optimize for the "standard cost" without the additional constraints.
However, this does not work in every case. For example, it should be avoided to initialize W or b with negative values in the beginning.

Your second (and probably better) approach can be accelerated by defining the update logic in the general computational graph, i.e. after the definition of cost

params = [W, b]
grads = tf.gradients(cost, params)
optimizer = [tf.assign(param, tf.maximum(0., param - grad*learning_rate))
             for param, grad in zip(params, grads)]

I think your solution is slow because it creates new computation nodes every time which is probably very costly and repeated a lot inside the loops.

update using tensorflow optimizer

In my solution above not the gradients are clipped but rather the resulting update values.
Along the lines of this answer you could clip the gradients to be at most the value of the updated parameter like so:

params = [W, b]
opt = tf.train.GradientDescentOptimizer(learning_rate)
grads_and_vars = opt.compute_gradients(cost, params)
clipped_grads_vars = [(tf.clip_by_value(grad, -np.inf, var), var) for grad, var in grads_and_vars]
optimizer = opt.apply_gradients(clipped_grads_vars)

This way an update will never decrease a parameter to a value below 0.
However, I think this will not work in the case the updated variable is already negative.
Also, if the optimizing algorithm somehow multiplies the clipped gradient by a value greater than 1.
The latter might actually never happen, but I'm not 100% sure.


问:
Why do managed attributes just work for class attributes and not for instance attributes in python?
To illustrate the question check the following code:

```<code>class MyDescriptor(object):
  def __get__(self, obj, type=None):
    print "get", self, obj, type
    return self._v
  def __set__(self, obj, value):
    self._v = value
    print "set", self, obj, value
    return None

class SomeClass1(object):
  m = MyDescriptor()

class SomeClass2(object):
  def __init__(self):
    self.m = MyDescriptor()

x1 = SomeClass1()
x2 = SomeClass2()

x1.m = 1000
# ->  set <__main__.MyDescriptor object at 0xb787c7ec> <__main__.SomeClass1 object at 0xb787cc8c> 10000
x2.m = 1000 # I guess that this overwrites the function. But why?
# ->
print x1.m
# -> get <__main__.MyDescriptor object at 0xb787c7ec> <__main__.SomeClass1 object at 0xb787cc8c> <class '__main__.SomeClass1'> 10000
print x2.m
# -> 10000
</code>```

Why doesn't x2.m = 1000 not call the __set__-function? It seems that this overwrites the function. But why?

Where is _v in x1? It is not in x1._v

答:
I found _v of x1: It is in SomeClass1.__dict__['m']._v

For the version suggested by S.Lott within the other answer: _v is in x1._v

OwnerDisplayName: Philipp der Rautenberg

ContentLicense: CC BY-SA 2.5

Body:
To illustrate the question check the following code:

class MyDescriptor(object):
  def __get__(self, obj, type=None):
    print "get", self, obj, type
    return self._v
  def __set__(self, obj, value):
    self._v = value
    print "set", self, obj, value
    return None

class SomeClass1(object):
  m = MyDescriptor()

class SomeClass2(object):
  def __init__(self):
    self.m = MyDescriptor()

x1 = SomeClass1()
x2 = SomeClass2()

x1.m = 1000
# ->  set <__main__.MyDescriptor object at 0xb787c7ec> <__main__.SomeClass1 object at 0xb787cc8c> 10000
x2.m = 1000 # I guess that this overwrites the function. But why?
# ->
print x1.m
# -> get <__main__.MyDescriptor object at 0xb787c7ec> <__main__.SomeClass1 object at 0xb787cc8c> <class '__main__.SomeClass1'> 10000
print x2.m
# -> 10000

Why doesn't x2.m = 1000 not call the __set__-function? It seems that this overwrites the function. But why?

Where is _v in x1? It is not in x1._v


问:
Wordpress - comment html code
Is it possible to comment html code?
I want to comment a img html tag or a video frame but everytime it gets filtered out. Is there any way that it doesn't get displayed? The html code should be executed that the image gets displayed itself and not the code</p>

答:
Q: Is it possible to comment html code?

A: Yes.

Since you tagged yor question with wordpress, I'll show you how to comment in PHP codes too.

HTML:

```<code><HTML><!--This is a comment. Comments are not displayed in the browser--></HTML>
</code>```

PHP:

```<code><?php echo 'Print this'; //This is a comment. ?>
</code>```

Title: Wordpress - comment html code

Body:
Q: Is it possible to comment html code?

A: Yes.

Since you tagged yor question with wordpress, I'll show you how to comment in PHP codes too.

HTML:

<HTML><!--This is a comment. Comments are not displayed in the browser--></HTML>

PHP:

<?php echo 'Print this'; //This is a comment. ?>


问:
Check if one of the values from both lists are present in the string in ruby
I have two arrays with logins and file extensions:
logins = ['bob', 'mark', 'joe']
extensions = ['.doc', '.xls']

I need to check if one of the values from both lists are present in the string (string is like str = "aaa bob test.txt test text"), and if yes do some work.

How to correctly perform this checking in Ruby.

Now I'm perform this with several loop and if statements.

答:
```<code>[logins, extensions].all? do |list|
  list.any? { |match| str.include? match }
end
</code>```

You have two lists, logins and extensions. You want to make sure that all? of them do something and that 'something' is that the string includes any? of their elements.

The answer using regex is better performing, though, even if a little less simple to write.

Body:
[logins, extensions].all? do |list|
  list.any? { |match| str.include? match }
end

You have two lists, logins and extensions. You want to make sure that all? of them do something and that 'something' is that the string includes any? of their elements.

The answer using regex is better performing, though, even if a little less simple to write.


问:
Remote debugging Azure website a Visual Studio 2017 Project
I recently installed Visual Studio 2017 and my problems began with remote debugging.

So, this is the message error.

And on Azure, they are lagging behind the official release of VS Remote Debugging for this VS  version.

Any idea on how to make this work?

答: If you use Cloud Explorer within Visual Studio 2017 to initiate the Remote Debugging it'll set the VS Version on the Web App correctly. Currently, the portal doesn't include UI to allow you to set 2017 as the version, but using the Cloud Explorer to initiate remote debugging will do it behind the scenes. We just need to wait for the Azure Portal to be updated and get caught up with VS.</p>


问:
in java, When "DatatypeConverter.printBase64Binary" use this function, the expected result is different
The following code , It is Java code that is encoded as sha1 and then encoded as base64.

```<code>System.out.println("key : "+DatatypeConverter
                                .printBase64Binary(MessageDigest.getInstance("SHA-1").digest(("wZCYQre/AdRckTa9/hwEHg==258EAFA5-E914-47DA-95CA-C5AB0DC85B11")
                                        .getBytes("UTF-8"))));
</code>```

above code result is..

```<code>key : 1jBpw9uqcRx3Cl/AajHQmeDYw/c=
</code>```

but The results from sha1-encoded websites and base64-encoded websites is

```<code>key : ZDYzMDY5YzNkYmFhNzExYzc3MGE1ZmMwNmEzMWQwOTllMGQ4YzNmNw==
</code>```

Why are the results of the two different?

答: When you use the websites, you are base-64–encoding the hash after encoding in hexadecimal characters. This is strange and probably not what you want. Your Java code is directly base-64–encoding the hash itself.</p>

Body:
The following code , It is Java code that is encoded as sha1 and then encoded as base64.

System.out.println("key : "+DatatypeConverter
                                .printBase64Binary(MessageDigest.getInstance("SHA-1").digest(("wZCYQre/AdRckTa9/hwEHg==258EAFA5-E914-47DA-95CA-C5AB0DC85B11")
                                        .getBytes("UTF-8"))));

above code result is..

key : 1jBpw9uqcRx3Cl/AajHQmeDYw/c=

but The results from sha1-encoded websites and base64-encoded websites is

key : ZDYzMDY5YzNkYmFhNzExYzc3MGE1ZmMwNmEzMWQwOTllMGQ4YzNmNw==

Why are the results of the two different?


问:
jenkins complicated buildflow, is it possible?

I would like to have a Jenkins build flow that looks like this.

After the build is triggered all slaves run the same job in parallel (a setup job).

If any slaves fail this job they should not continue on.

For the all the slaves that to pass that job, they should grab a job out of a pool of jobs that need to be completed. And once a slave completes a job they should go back to complete another job in the pool.

I have only started working with Jenkins a few weeks ago and they way I have it setup now is as each job is picked up by a slave they have to run the setup job first. This really slows down build times because I have about 30 jobs and the setup takes ~2 minutes.

I am using Jenkins as an automated testing platform and all the jobs in the job pool can run independently of each other. I have 5 slaves currently and ~30 jobs.

答:
It seems like you want separate stages in the same job. This is made much easier in jenkins 2's pipelines. There are some pictures here:
https://wiki.jenkins-ci.org/display/JENKINS/Pipeline+Stage+View+Plugin

the [groovy] code ends up looking like this:

```<code>node {
  stage 'Checkout'
  svn 'https://svn.mycorp/trunk/'
  stage 'Build'
  sh 'make all'
  stage 'Test'
  sh 'make test'
}
</code>```

Body:
It seems like you want separate stages in the same job. This is made much easier in jenkins 2's pipelines. There are some pictures here:
https://wiki.jenkins-ci.org/display/JENKINS/Pipeline+Stage+View+Plugin

the [groovy] code ends up looking like this:

node {
  stage 'Checkout'
  svn 'https://svn.mycorp/trunk/'
  stage 'Build'
  sh 'make all'
  stage 'Test'
  sh 'make test'
}


问:
how to know encoding of a file using command line?
Is there any command to know the encoding of a file in windows?

like for a file A.txt encoding is UTF-16

答:
In Windows command prompt (cmd), there is no command I know of, that is capable of determining how a text file is encoded.

Nevertheless, I wrote a small batch file that is able to check a few conditions and thus, determine whether a given text file is ASCII-/ANSI-encoded or Unicode-encoded (UTF-8 or UTF-16, Little Endian or Big Endian). At first, it checks whether or not the first (non-empty) line contains zero-bytes, which is an indication that the file is not ASCII-/ANSI-encoded. Next, it checks the first few bytes whether they constitute the Byte Order Mark (BOM) for UTF-8/UTF-16. Since the BOM is optional for Unicode-encoded files, its absence is not a clear sign for an ASCI-/ANSI-encoded file.

So here is the code, featuring a lot of explanatory remarks (rem) -- I hope, it helps:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "_FILE=%~1" & rem // (provide file via the first command line argument)

rem // Check whether a dedicated file is given (so no wild-cards):
2> nul >&2 (< "%_FILE%" set /P ="" & ver) || (
    rem // The file does not exist:
    >&2 echo The file could not be found, hence there is no encoding!
    exit /B 255
)

rem // Determine the file size:
set "SIZE=" & for %%F in ("%_FILE%") do set "SIZE=%%~zF"
if not defined SIZE (
    rem // The file does not exist:
    >&2 echo The file could not be found, hence there is no encoding!
    exit /B 255
)
if %SIZE% EQU 0 (
    rem // The file is empty:
    >&2 echo The file is empty, hence encoding cannot be determined!
    exit /B 1
)

rem // Store current code page to be able to restore it finally:
for /F "tokens=2 delims=:" %%C in ('chcp') do set /A "$CP=%%C"
rem /* Change to code page 437 (original IBM PC or DOS code page) temporarily;
rem    this is necessary for extended characters not to be converted: */
> nul chcp 437

rem // Attempt to read first line from file; this fails if zero-bytes occur:
(
    rem /* The loop does not iterate over an empty file or one with empty lines only;
    rem    therefore, the behaviour is the same as when zero-bytes occur: */
    for /F usebackq^ delims^=^ eol^= %%L in ("%_FILE%") do (
        rem // Abort reading file after first non-empty line:
        goto :NEXT
    )
) || (
    rem /* The `for /F` loop returns a non-zero exit code in case the file is empty,
    rem    contains empty lines only or the first non-empty line contains zero-bytes;
    rem    to determine whether there are zero-bytes, let `find` process the file,
    rem    which removes zero-bytes or converts them to line-breaks, so `for /F` can
    rem    read the file;
    rem    however, `find` would read the whole file, hence do that only for small
    rem    ones and skip that for large ones, such contains zero-bytes most likely: */
    if %SIZE% LEQ 8192 (
        (
            rem // In case the file contains line-breaks only, the loop does not iterate:
            for /F delims^=^ eol^= %%L in ('^< "%_FILE%" find /V ""') do (
                rem // Abort reading file after first non-empty line:
                goto :ZERO
            )
        ) || (
            rem /* The loop did not iterate, so the file contains line-breaks only;
            rem    restore the initial code page prior to termination: */
            > nul chcp %$CP%
            >&2 echo The file holds only empty lines, hence encoding cannot be determined!
            exit /B 1
        )
    )
)

rem // This point is reached in case the file contains zero-bytes:
:ZERO
rem // Restore the initial code page prior to termination:
> nul chcp %$CP%
>&2 echo NULL-bytes detected in first line, so file is non-ASCII/ANSI!
exit /B 2

rem // This point is reached in case the file does not contain any zero-bytes:
:NEXT
rem /* Build Byte Order Marks (BOMs) for UTF-16-encoded text (Little Endian and Big Endian)
rem    and for UTF-8-encoded text: */
for /F "tokens=1-3" %%A in ('
    forfiles /P "%~dp0." /M "%~nx0" /C "cmd /C echo 0xFF0xFE 0xFE0xFF 0xEF0xBB0xBF"
') do set "$LE=%%A" & set "$BE=%%B" & set "$U8=%%C"

rem /* Reset line string variable, then store first line string (1023 bytes at most);
rem    in contrast to `for /F`, this does not skip over blank lines: */
< "%_FILE%" (set "LINE=" & set /P LINE="")
rem // Check whether the first line of the file begins with any of the BOMs:
if not "%LINE:~,2%"=="%$LE%" if not "%LINE:~,2%"=="%$BE%" if not "%LINE:~,3%"=="%$U8%" goto :CONT
rem /* One of the BOMs has been encountered, hence the file is Unicode-encoded;
rem    restore the initial code page prior to termination: */
> nul chcp %$CP%
>&2 echo BOM encountered in first line, so file is non-ASCII/ANSI!
exit /B 4

rem // This point is reached in case the file does not appear as Unicode-encoded:
:CONT
rem // Restore the initial code page prior to termination:
> nul chcp %$CP%
echo The file appears to be an ASCII-/ANSI-encoded text.

endlocal
exit /B 0
```

ContentLicense: CC BY-SA 4.0


问:
Makefile from one source Directory trying to access a cpp file from other Directory
I've an issue with object creation for a cpp files residing in 2 different directories as show below:

```<code>- Project-xyz
  - Hello
    - Hello.cpp
  - World
    - Main.cpp
    - World.cpp
    - Makefile
</code>```

For better clarity here is how the code looks (just a dummy code).

hello/Hello.h

```<code>#ifndef HELLO_H
#define HELLO_H

void HelloPrint();

#endif // HELLO_H
</code>```

hello/Hell.cpp

```<code>#include <iostream>
#include "Hello.h"

void HelloPrint()
{
  std::cout <<"Hello" << std::endl;
}
</code>```

world/World.h

```<code> #ifndef WORLD_H
 #define WORLD_H

 void WorldPrint();

 #endif // WORLD_H
</code>```

world/World.cpp

```<code>#include <iostream>
#include "World.h"

void WorldPrint()
{
 std::cout <<"World!" << std::endl;
}
</code>```

world/Makefile
Using a makefile given in here. Which is working fine. But it doesn't create any obj files.

```<code># set non-optional compiler flags here
CXXFLAGS += -std=c++11 -Wall -Wextra -pedantic-errors

# set non-optional preprocessor flags here
# eg. project specific include directories
CPPFLAGS +=

# find cpp files in subdirectories
SOURCES := $(shell find . -name '*.cpp')
SOURCES += $(shell find ../hello -name '*.cpp')

# find headers
HEADERS := $(shell find . -name '*.h')

OUTPUT := HelloWorld

# Everything depends on the output
all: $(OUTPUT)

# The output depends on sources and headers
$(OUTPUT): $(SOURCES) $(HEADERS)
    $(CXX) $(CXXFLAGS) $(CPPFLAGS) -o $(OUTPUT) $(SOURCES)

clean:
     $(RM) $(OUTPUT)
</code>```

I'm  looking at a solution which uses the makefile which generates the obj files in specific directory as shown here.

Now I'm trying to generate the obj files for hello/hello.cpp file in world/objdir/hello.o using the Below make file. Which is not working.
How to get it working?

world/Makefile

```<code>GCC=g++
CPPFLAGS=-c -Wall
LDFLAGS=
OBJDIR=objdir
OBJ=$(addprefix $(OBJDIR)/, $(patsubst %.cpp, %.o, $(wildcard *.cpp)))
TARGET=HelloWorld

.PHONY: all clean

all: $(OBJDIR) $(TARGET)

$(OBJDIR):
    mkdir $(OBJDIR)

$(OBJDIR)/%.o: %.cpp
    $(GCC) $(CPPFLAGS) -c $< -o $@
$(TARGET): $(OBJ)
    $(GCC) $(LDFLAGS) -o $@ $^
clean:
    @rm -f $(TARGET) $(wildcard *.o)
    @rm -rf $(OBJDIR)
</code>```

When i try to run the make it throws the following error.

```<code>g++ -c -Wall -c main.cpp ../hello/Hello.cpp -o objdir/main.o
g++: fatal error: cannot specify -o with -c, -S or -E with multiple files
compilation terminated.
make: *** [objdir/main.o] Error 4
</code>```

It is not able to generate the obj files for the hello/Hello.cpp file and hence throwing this error.

Any help from the makefile Masters is appreciated! :)

答:
Adding vpath %.cpp ../hello along with @gaoithe's solution has solved this issue.

```<code>vpath %.cpp ../hello
GCC=g++
CPPFLAGS=-c -Wall
LDFLAGS=
OBJDIR=objdir
OBJ=$(addprefix $(OBJDIR)/, $(patsubst %.cpp, %.o, $(wildcard *.cpp)))
OBJ+=$(addprefix $(OBJDIR)/, $(patsubst %.cpp, %.o, $(notdir $(wildcard ../hello/*.cpp))))
TARGET=HelloWorld

.PHONY: all clean

all: $(OBJDIR) $(TARGET)

$(OBJDIR):
    mkdir $(OBJDIR)

$(OBJDIR)/%.o: %.cpp
    $(GCC) $(CPPFLAGS) -c $< -o $@
$(TARGET): $(OBJ)
    $(GCC) $(LDFLAGS) -o $@ $^
clean:
    @rm -f $(TARGET) $(wildcard *.o)
    @rm -rf $(OBJDIR)
</code>```

now

```<code>$ ls objdir/
 Hello.o  main.o   World.o
</code>```

Experts need to evaluate the new makefile and confirm if this is a scale able solution?

Any inputs on optimising/enhancing the current makefile and help in reducing the build time is highly appreciated.

Body:
I've an issue with object creation for a cpp files residing in 2 different directories as show below:

- Project-xyz
  - Hello
    - Hello.cpp
  - World
    - Main.cpp
    - World.cpp
    - Makefile

For better clarity here is how the code looks (just a dummy code).

hello/Hello.h

#ifndef HELLO_H
#define HELLO_H

void HelloPrint();

#endif // HELLO_H

hello/Hell.cpp

#include <iostream>
#include "Hello.h"

void HelloPrint()
{
  std::cout <<"Hello" << std::endl;
}

world/World.h

 #ifndef WORLD_H
 #define WORLD_H

 void WorldPrint();

 #endif // WORLD_H

world/World.cpp

#include <iostream>
#include "World.h"

void WorldPrint()
{
 std::cout <<"World!" << std::endl;
}

world/Makefile
Using a makefile given in here. Which is working fine. But it doesn't create any obj files.

# set non-optional compiler flags here
CXXFLAGS += -std=c++11 -Wall -Wextra -pedantic-errors

# set non-optional preprocessor flags here
# eg. project specific include directories
CPPFLAGS +=

# find cpp files in subdirectories
SOURCES := $(shell find . -name '*.cpp')
SOURCES += $(shell find ../hello -name '*.cpp')

# find headers
HEADERS := $(shell find . -name '*.h')

OUTPUT := HelloWorld

# Everything depends on the output
all: $(OUTPUT)

# The output depends on sources and headers
$(OUTPUT): $(SOURCES) $(HEADERS)
    $(CXX) $(CXXFLAGS) $(CPPFLAGS) -o $(OUTPUT) $(SOURCES)

clean:
     $(RM) $(OUTPUT)

I'm  looking at a solution which uses the makefile which generates the obj files in specific directory as shown here.

Now I'm trying to generate the obj files for hello/hello.cpp file in world/objdir/hello.o using the Below make file. Which is not working.
How to get it working?

world/Makefile

GCC=g++
CPPFLAGS=-c -Wall
LDFLAGS=
OBJDIR=objdir
OBJ=$(addprefix $(OBJDIR)/, $(patsubst %.cpp, %.o, $(wildcard *.cpp)))
TARGET=HelloWorld

.PHONY: all clean

all: $(OBJDIR) $(TARGET)

$(OBJDIR):
    mkdir $(OBJDIR)

$(OBJDIR)/%.o: %.cpp
    $(GCC) $(CPPFLAGS) -c $< -o $@
$(TARGET): $(OBJ)
    $(GCC) $(LDFLAGS) -o $@ $^
clean:
    @rm -f $(TARGET) $(wildcard *.o)
    @rm -rf $(OBJDIR)

When i try to run the make it throws the following error.

g++ -c -Wall -c main.cpp ../hello/Hello.cpp -o objdir/main.o
g++: fatal error: cannot specify -o with -c, -S or -E with multiple files
compilation terminated.
make: *** [objdir/main.o] Error 4

It is not able to generate the obj files for the hello/Hello.cpp file and hence throwing this error.

Any help from the makefile Masters is appreciated! :)

Body:
Adding vpath %.cpp ../hello along with @gaoithe's solution has solved this issue.

vpath %.cpp ../hello
GCC=g++
CPPFLAGS=-c -Wall
LDFLAGS=
OBJDIR=objdir
OBJ=$(addprefix $(OBJDIR)/, $(patsubst %.cpp, %.o, $(wildcard *.cpp)))
OBJ+=$(addprefix $(OBJDIR)/, $(patsubst %.cpp, %.o, $(notdir $(wildcard ../hello/*.cpp))))
TARGET=HelloWorld

.PHONY: all clean

all: $(OBJDIR) $(TARGET)

$(OBJDIR):
    mkdir $(OBJDIR)

$(OBJDIR)/%.o: %.cpp
    $(GCC) $(CPPFLAGS) -c $< -o $@
$(TARGET): $(OBJ)
    $(GCC) $(LDFLAGS) -o $@ $^
clean:
    @rm -f $(TARGET) $(wildcard *.o)
    @rm -rf $(OBJDIR)

now

$ ls objdir/
 Hello.o  main.o   World.o

Experts need to evaluate the new makefile and confirm if this is a scale able solution?

Any inputs on optimising/enhancing the current makefile and help in reducing the build time is highly appreciated.


问:
How do I set a color other than black to image created with makeTransparent()
I am using the MakeTransparent() function call on the c# Bitmap object to convert images to transparent images. When this method is called, it will convert a background color to be transparent by setting the alpha channel, but it then converts the background color to black.

I need to find a fast way of converting this background color back to white or whatever the original color was, as occasionally I need to flatten the image to a non-alpha channel enabled format.

Make transparent doesn't seem to have any flags or overloads that allows you to tell it to leave the background color alone, and altering the image pixel by pixel is way to inefficient.  Anyone have any suggestions or GDI tricks to solve this problem?

答: There doesn't seem to be a fast way to do this using the managed code interface. Using individual pixel manipulation, or using unmanaged code to update the pixels seem to be the only real options.</p>


问:
STL std::map, pass by ref to const and the necessity of const_casting
I have a simple question regarding const_cast and best practices regarding STL containers. Consider the following where class Foo has a private STL std::map from Widget* to int:

Declaration:

```<code>#include <map>
using std::map;

class Widget;

class Foo {
public:
     Foo(int n);
     virtual ~Foo();

     bool hasWidget(const Widget&);

private:
     map<Widget*,int> widget_map;
};
</code>```

Definition:

```<code>#include <map>
#include "Foo.h"
#include "Widget.h"

using std::map;

Foo::Foo(int n)
{
     for (int i = 0; i < n; i++) {
          widget_map[new Widget()] = 1;
     }
}

Foo::~Foo()
{
     map<Widget*, int>::iterator it;
     for (it = widget_map.begin(); it != widget_map.end(); it++) {
          delete it->first;
     }
}

bool Foo::hasWidget(const Widget& w)
{
     map<Widget*, int>::iterator it;
     it = this->widget_map.find(const_cast<Widget*>(&w));
     return ( ! ( it == widget_map.end() ) );
}
</code>```

Given that hasWidget takes a reference to const as its parameter, the constness needs to be cast away when calling map::find (wiget_map being from Wiget* to int). As far as I can tell, this approach is both sensible and desirable -- but I'm reluctant to accept it as such without feedback from more experienced C++ programmers.

It seems to me that this is one of the few cases of using const_cast appropriately given that we're passing the result of the cast to an STL method. Am I correct?

I realise that other permutations of this question have been posed already (for example, const_cast for vector with object) but none seem to directly address the above.

Thanks in advance.

答:
This looks clunky to me. Identifying objects by their physical addresses is quite "special", admittedly it's unique, but it's weird too.

I would strongly consider reversing the map:

```<code>std::map<Widget::Id, Widget*>
</code>```

where Widget::Id could simply be an int or similar.

There would not be any issue with the const-ness then.

To delve deeper, you could also have a look at the Boost Pointer Container library:

```<code>boost::ptr_map<Widget::Id, Widget>
</code>```

which would alleviate the memory management issues.

Body:
I have a simple question regarding const_cast and best practices regarding STL containers. Consider the following where class Foo has a private STL std::map from Widget* to int:

Declaration:

#include <map>
using std::map;

class Widget;

class Foo {
public:
     Foo(int n);
     virtual ~Foo();

     bool hasWidget(const Widget&);

private:
     map<Widget*,int> widget_map;
};

Definition:

#include <map>
#include "Foo.h"
#include "Widget.h"

using std::map;

Foo::Foo(int n)
{
     for (int i = 0; i < n; i++) {
          widget_map[new Widget()] = 1;
     }
}

Foo::~Foo()
{
     map<Widget*, int>::iterator it;
     for (it = widget_map.begin(); it != widget_map.end(); it++) {
          delete it->first;
     }
}

bool Foo::hasWidget(const Widget& w)
{
     map<Widget*, int>::iterator it;
     it = this->widget_map.find(const_cast<Widget*>(&w));
     return ( ! ( it == widget_map.end() ) );
}

Given that hasWidget takes a reference to const as its parameter, the constness needs to be cast away when calling map::find (wiget_map being from Wiget* to int). As far as I can tell, this approach is both sensible and desirable -- but I'm reluctant to accept it as such without feedback from more experienced C++ programmers.

It seems to me that this is one of the few cases of using const_cast appropriately given that we're passing the result of the cast to an STL method. Am I correct?

I realise that other permutations of this question have been posed already (for example, const_cast for vector with object) but none seem to directly address the above.

Thanks in advance.

Body:
This looks clunky to me. Identifying objects by their physical addresses is quite "special", admittedly it's unique, but it's weird too.

I would strongly consider reversing the map:

std::map<Widget::Id, Widget*>

where Widget::Id could simply be an int or similar.

There would not be any issue with the const-ness then.

To delve deeper, you could also have a look at the Boost Pointer Container library:

boost::ptr_map<Widget::Id, Widget>

which would alleviate the memory management issues.


问:
I'm trying to understand this c# code how would it work in python
I'm trying to understand this c# code:

```<code>sum += (myName[i]) * Math.Pow(i+5,i+1);
</code>```

how would this work in python?

答:
As long as the types are correct, the python syntax would be:

```<code>sum += (myName[i]) * ((i + 5) ** (i + 1))
</code>```

The ** is the exponentiation operator in python.

Body:
I'm trying to understand this c# code:

sum += (myName[i]) * Math.Pow(i+5,i+1);

how would this work in python?

Body:
As long as the types are correct, the python syntax would be:

sum += (myName[i]) * ((i + 5) ** (i + 1))

The ** is the exponentiation operator in python.


问:
how to write java 8 Comparator for Map values inside Map.Entry
Here is what I have, I want to sort in descending a the value in the entry.

```<code>Map<String, Outer> outer = Input
                          .entrySet()
                          .stream
                          .collect(toMap(Map.Entry::getKey, entry->{
                           List<Outer> inner = entry
                                    .getValue()
                                    .stream()
                                    .sorted(Comparator.comparingDouble(???))
                              })) ... (more lines of code.)
</code>```

How to write a Comparator with sorted.
If want to do the following inside sorted

```<code>if (o1.getSomething() > o2.getSomething())
     return -1;
if (o1.getSomething() < o2.getSomething())
     return 1;
return 0;
</code>```

After getting the List and sorting it works for me.

```<code>inner.sort((Outer o1, Outer o2) -> {
if (o1.getSomething() > o2.getSomething())
         return -1;
    if (o1.getSomething() < o2.getSomething())
         return 1;
    return 0;
});
</code>```

But is there a way to use stream.sorted( " use the same comparator logic here") inside entry.

Entry is an ArrayList, with bunch of values.

```<code>entry:[{w:1},{w:2},{w:3},{w:4}];
</code>```

So I want to reverse sort this, to be as follows:

```<code>entry:[{w:4},{w:3},{w:2},{w:1}];
</code>```

so the final list I get is a sorted in reverse order one.

sry for the confusion.

答:
If I'm guessing correctly, you want to order all your values (which are lists) according to a given comparator.

If this is the case, you don't need to create a stream at all. Using Map's replaceAll method should suffice:

```<code>// Orders Outer elements according to Outer's getSomething method
// in descendant order (assumes getSomething returns a double value)
Comparator<Outer> yourComparator =
    Comparator.comparingDouble(Outer::getSomething).reversed();

yourMap.replaceAll((k, v) -> {
        Collections.sort(v, yourComparator);
        return v;
    });
</code>```

This utilizes the Collections.sort method, which takes a list and a comparator.

Body:
Here is what I have, I want to sort in descending a the value in the entry.

Map<String, Outer> outer = Input
                          .entrySet()
                          .stream
                          .collect(toMap(Map.Entry::getKey, entry->{
                           List<Outer> inner = entry
                                    .getValue()
                                    .stream()
                                    .sorted(Comparator.comparingDouble(???))
                              })) ... (more lines of code.)

How to write a Comparator with sorted.
If want to do the following inside sorted

if (o1.getSomething() > o2.getSomething())
     return -1;
if (o1.getSomething() < o2.getSomething())
     return 1;
return 0;

After getting the List and sorting it works for me.

inner.sort((Outer o1, Outer o2) -> {
if (o1.getSomething() > o2.getSomething())
         return -1;
    if (o1.getSomething() < o2.getSomething())
         return 1;
    return 0;
});

But is there a way to use stream.sorted( " use the same comparator logic here") inside entry.

Entry is an ArrayList, with bunch of values.

entry:[{w:1},{w:2},{w:3},{w:4}];

So I want to reverse sort this, to be as follows:

entry:[{w:4},{w:3},{w:2},{w:1}];

so the final list I get is a sorted in reverse order one.

sry for the confusion.

Body:
If I'm guessing correctly, you want to order all your values (which are lists) according to a given comparator.

If this is the case, you don't need to create a stream at all. Using Map's replaceAll method should suffice:

// Orders Outer elements according to Outer's getSomething method
// in descendant order (assumes getSomething returns a double value)
Comparator<Outer> yourComparator =
    Comparator.comparingDouble(Outer::getSomething).reversed();

yourMap.replaceAll((k, v) -> {
        Collections.sort(v, yourComparator);
        return v;
    });

This utilizes the Collections.sort method, which takes a list and a comparator.


问:
How to output only the first n lines based on occurrences of a column string
I have a large file that contains ID numbers in the first column, followed by additional unique information in subsequent columns. Each ID number occurs multiple times in the file:

```<code>000112 3489 A-8 40984
000112 4743 A-7 94587
000112 5894 A-1 45795
000177 8347 A-2 54575
000177 5843 B-5 94342
000177 5684 A-4 76544
000177 6586 C-2 65834
000226 5679 C-2 85795
000226 5456 C-1 45876
000226 9899 A-2 56834
</code>```

I would like to output a file containing lines for only the first two occurrences of each ID number:

```<code>000112 3489 A-8 40984
000112 4743 A-7 94587
000177 8347 A-2 54575
000177 5843 B-5 94342
000226 5679 C-2 85795
000226 5456 C-1 45876
</code>```

Note that this data represents only a small portion of the input file, so a command that requires entering specific strings (ID numbers) is not what I'm looking for. Thanks!

答:
This isn't pretty but it yields the desired output:

Step 1:

```<code>awk '!seen[$1]++' input.file > output1
</code>```

Step 2:

```<code>grep -v -F -f output1 input.file | awk '!seen[$1]++' > output2
</code>```

Step 3:

```<code>cat output1 output2 | sort -k 1 > desired.output
</code>```

Body:
I have a large file that contains ID numbers in the first column, followed by additional unique information in subsequent columns. Each ID number occurs multiple times in the file:

000112 3489 A-8 40984
000112 4743 A-7 94587
000112 5894 A-1 45795
000177 8347 A-2 54575
000177 5843 B-5 94342
000177 5684 A-4 76544
000177 6586 C-2 65834
000226 5679 C-2 85795
000226 5456 C-1 45876
000226 9899 A-2 56834

I would like to output a file containing lines for only the first two occurrences of each ID number:

000112 3489 A-8 40984
000112 4743 A-7 94587
000177 8347 A-2 54575
000177 5843 B-5 94342
000226 5679 C-2 85795
000226 5456 C-1 45876

Note that this data represents only a small portion of the input file, so a command that requires entering specific strings (ID numbers) is not what I'm looking for. Thanks!

Body:
This isn't pretty but it yields the desired output:

Step 1:

awk '!seen[$1]++' input.file > output1

Step 2:

grep -v -F -f output1 input.file | awk '!seen[$1]++' > output2

Step 3:

cat output1 output2 | sort -k 1 > desired.output


问:
Python3: What is an efficient way to compare a bunch of variables, if one of the comparison fails, do X otherwise do Y?
Say you have 10 v_t1, v_t2, v_t3, etc, threshold variables and 10 variables for current values v_c1, v_c2, v_c3, etc,, all threshold variables contain integers, all current values contain an integer or a string of "Not found". I need to compare the thresholds against the current values. If any of the current value variables are greater than the threshold, I want to run function_a otherwise run function_b

I could write out 10 different if statements and checking their return values, with a check to see if the current value contains "Not found", but that seems wrong and inefficient. Is there a better way? Maybe compare two lists together?

答:
Assume the list of variables is v_list = [v_c1, v_c2, ...], and the list of thresholds is t_list = [v_t1, v_t2, ...], you can use

```<code>if any(v != "Not found" and v > t for (v, t) in zip(v_list, t_list)):
    function_a()
else:
    function_b()
</code>```

Body:
Assume the list of variables is v_list = [v_c1, v_c2, ...], and the list of thresholds is t_list = [v_t1, v_t2, ...], you can use

if any(v != "Not found" and v > t for (v, t) in zip(v_list, t_list)):
    function_a()
else:
    function_b()


问:
How does Azure ML handle categorical columns during training a linear or logistic regression model?
How does Azure ML handle categorical columns during training a linear regression model? A linear regression model takes continuous values. However, even though I haven't changed anything of those categorical columns, Azure ML trains linear and logistic regression without error. So I would like to know how Azure ML manages to process categorical columns behind the scene. Thanks!</p>

答:
The linear regression module will only take numeric independent variables. Are you sure you had this working with categoricals in the linear regression?

https://msdn.microsoft.com/en-us/library/azure/dn905978.aspx


问:
URL regex validation
i made that:

```<code> /^(http[s]?://){0,1}(www.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1}
</code>```

and checked it with a validator but on my page it is not working:

```<code>var re = /^(http[s]?://){0,1}(www.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1};
if (!re.test(url)) {
    alert("url error");
    return false;
}
</code>```

i get this error

```<code>Webpage error details

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)
Timestamp: Tue, 30 Nov 2010 14:23:10 UTC

Message: Expected ')' in regular expression
Line: 781
Char: 23
Code: 0
URI: http://*************************
</code>```

答:
You have to escape your special characters (/ and that . after www in this case) and att the missing trailing /, like this:

```<code>var re = /^(http[s]?:\/\/){0,1}(www\.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1}/;
if (!re.test(url)) {
    alert("url error");
    return false;
}
</code>```

Title: URL regex validation

Body:
i made that:

 /^(http[s]?://){0,1}(www.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1}

and checked it with a validator but on my page it is not working:

var re = /^(http[s]?://){0,1}(www.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1};
if (!re.test(url)) {
    alert("url error");
    return false;
}

i get this error

Webpage error details

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)
Timestamp: Tue, 30 Nov 2010 14:23:10 UTC

Message: Expected ')' in regular expression
Line: 781
Char: 23
Code: 0
URI: http://*************************

Body:
You have to escape your special characters (/ and that . after www in this case) and att the missing trailing /, like this:

var re = /^(http[s]?:\/\/){0,1}(www\.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1}/;
if (!re.test(url)) {
    alert("url error");
    return false;
}


问:
Referer sub array?
I have an array orig[].
Now I want to create another array ref[] that refer to only specific elements in orig[].
By implying it is referrer I mean any change in element of ref[] will change the corresponding element of orig[].

F.e. pseudo code :

```<code>orig = [ 1,2,3,4,5 ]
ref[0] ==> orig[2]
ref[1] ==> orig[4]
</code>```

now if I do :

```<code> ref[0] = 11
 ref[1] = 22
</code>```

orig will become :

```<code>[ 1,2,11,4,22 ]
</code>```

also when I ca do : for x in ref : ....

答:
You could extend the list class, and then implement all the methods that should behave differently than in a standard list.

Here is an implementation, but if you really would go ahead with this, you'd have to define a lot more methods:

```<code>class Referrer(list):
    def __init__(self, referredTo):
        self.__referredTo = referredTo

    def add_refs(self, *indexes):
        self.extend(indexes)

    # Redefine some of the methods that are invoked in certain syntax:
    def __getitem__(self, index):
        return self.__referredTo[list.__getitem__(self, index)]

    def __setitem__(self, index, value):
        self.__referredTo[list.__getitem__(self, index)] = value

    def __iter__(self):
        for i in range(0, len(self)):
            yield self[i]

    def __repr__(self):
        return str([x for x in self])

# Example use:
orig = [1, 2, 3, 4, 5]
ref = Referrer(orig)
ref.add_refs(2, 4)
ref[0] = 11
ref[1] = 22
print (orig) # [1, 2, 11, 4, 22]

for x in ref:
    print (x) # 11 and 22
</code>```

See it run on repl.it

Title: Referer sub array?

Body:
I have an array orig[].
Now I want to create another array ref[] that refer to only specific elements in orig[].
By implying it is referrer I mean any change in element of ref[] will change the corresponding element of orig[].

F.e. pseudo code :

orig = [ 1,2,3,4,5 ]
ref[0] ==> orig[2]
ref[1] ==> orig[4]

now if I do :

 ref[0] = 11
 ref[1] = 22

orig will become :

[ 1,2,11,4,22 ]

also when I ca do : for x in ref : ....

Body:
You could extend the list class, and then implement all the methods that should behave differently than in a standard list.

Here is an implementation, but if you really would go ahead with this, you'd have to define a lot more methods:

class Referrer(list):
    def __init__(self, referredTo):
        self.__referredTo = referredTo

    def add_refs(self, *indexes):
        self.extend(indexes)

    # Redefine some of the methods that are invoked in certain syntax:
    def __getitem__(self, index):
        return self.__referredTo[list.__getitem__(self, index)]

    def __setitem__(self, index, value):
        self.__referredTo[list.__getitem__(self, index)] = value

    def __iter__(self):
        for i in range(0, len(self)):
            yield self[i]

    def __repr__(self):
        return str([x for x in self])

# Example use:
orig = [1, 2, 3, 4, 5]
ref = Referrer(orig)
ref.add_refs(2, 4)
ref[0] = 11
ref[1] = 22
print (orig) # [1, 2, 11, 4, 22]

for x in ref:
    print (x) # 11 and 22

See it run on repl.it


问:
libcurl implementation on public-private key authentication on SSH

Created public and private key

ssh-keygen -t rsa <-- performed the operation in the SSH client

ssh-copy-id @
Note: no passphrase on the creation of the key

Can someone please help me on how to implement libcurl PutFile method using
- username
- private key
- public key
- sftp

Tried to browse on libcurl examples, but sadly this is the most similar example https://curl.haxx.se/libcurl/c/usercertinmem.html

答:
Forget about usercertinmem.c, just take fileupload.c and add SSH options as needed.

Something like this:

```<code>    curl_easy_setopt(curl, CURLOPT_URL, "sftp://username@host/path/file");

    curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
    curl_easy_setopt(curl, CURLOPT_READDATA, fd);
    curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_size);

    curl_easy_setopt(curl, CURLOPT_SSH_AUTH_TYPES, CURLSSH_AUTH_PUBLICKEY);
    curl_easy_setopt(curl, CURLOPT_SSH_PUBLIC_KEYFILE, pubkey_filename);
    curl_easy_setopt(curl, CURLOPT_SSH_PRIVATE_KEYFILE, privkey_filename);
    curl_easy_setopt(curl, CURLOPT_KEYPASSWD, "");
    curl_easy_setopt(curl, CURLOPT_SSH_HOST_PUBLIC_KEY_MD5, known_host_sign);

    res = curl_easy_perform(curl);
</code>```

You can read about these SSH-specific and other options in the options documentation.

Instead of CURLOPT_SSH_HOST_PUBLIC_KEY_MD5 you can also use CURLOPT_SSH_KNOWNHOSTS or CURLOPT_SSH_KEYFUNCTION.

Body:
Forget about usercertinmem.c, just take fileupload.c and add SSH options as needed.

Something like this:

    curl_easy_setopt(curl, CURLOPT_URL, "sftp://username@host/path/file");

    curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
    curl_easy_setopt(curl, CURLOPT_READDATA, fd);
    curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_size);

    curl_easy_setopt(curl, CURLOPT_SSH_AUTH_TYPES, CURLSSH_AUTH_PUBLICKEY);
    curl_easy_setopt(curl, CURLOPT_SSH_PUBLIC_KEYFILE, pubkey_filename);
    curl_easy_setopt(curl, CURLOPT_SSH_PRIVATE_KEYFILE, privkey_filename);
    curl_easy_setopt(curl, CURLOPT_KEYPASSWD, "");
    curl_easy_setopt(curl, CURLOPT_SSH_HOST_PUBLIC_KEY_MD5, known_host_sign);

    res = curl_easy_perform(curl);

You can read about these SSH-specific and other options in the options documentation.

Instead of CURLOPT_SSH_HOST_PUBLIC_KEY_MD5 you can also use CURLOPT_SSH_KNOWNHOSTS or CURLOPT_SSH_KEYFUNCTION.


问:
Hashmap to multiple graphs
I have a hashmap that is setup like this:

```<code>{ 192.168.2.10 = [192.167.2.10, 192.168.3.10] }
</code>```

The key is the first router(source) and the following IP's are the destination.

I need the hashmap to be converted into a graph so that I can find the shortest path.

The end goal is that this is going to be a network simulator with packets. Each router must hold 50 packets. I must send a packet to the final destination  but I must find the shortest path to that destination.

答:
To access sequentially key-value pairs in hashmap you need to use LinkedHashMap which stores keys in insertion order. You can access keys in both directions

https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html

Title: Hashmap to multiple graphs

Body:
I have a hashmap that is setup like this:

{ 192.168.2.10 = [192.167.2.10, 192.168.3.10] }

The key is the first router(source) and the following IP's are the destination.

I need the hashmap to be converted into a graph so that I can find the shortest path.

The end goal is that this is going to be a network simulator with packets. Each router must hold 50 packets. I must send a packet to the final destination  but I must find the shortest path to that destination.


问:
SQL Server - Sorting by category
I have a sorting question. I am using SQL Server 2012. I have the following dataset. The empty lines are for display purposes.

```<code>Country        CountryUser        State        StateUser
---------------------------------------------------------
Canada         User1              null         null
Canada         User2              null         null
USA            User2              null         null
USA            User3              null         null

Canada         User1              BC           User4
Canada         User1              BC           User5
Canada         User1              ON           User4
Canada         User1              ON           User8

Canada         User2              BC           User4
Canada         User2              BC           User5
Canada         User2              ON           User4
Canada         User2              ON           User8
total rows: 12
</code>```

I want to sort the rows such that all the rows that contain a state are shown below their country(row with null as state). Example,

```<code>Country        CountryUser        State        StateUser
---------------------------------------------------------
Canada         User1              null         null
Canada         User1              BC           User4
Canada         User1              BC           User5
Canada         User1              ON           User4
Canada         User1              ON           User8

Canada         User2              null         null
Canada         User2              BC           User4
Canada         User2              BC           User5
Canada         User2              ON           User4
Canada         User2              ON           User8

USA            User2              null         null
USA            User3              null         null
total rows: 12
</code>```

The first sort category is Country. The row with an empty state is shown first, then all the states are non null are shown afterwards. These rows have a secondary sort of state

答:
Try adding an order by clause with four columns:

```<code>select 'Canada'  as Country   ,    'User1' as CountryUser,null as State,null as StateUser
    union all select 'Canada', 'User2', null, null
    union all select 'USA'   , 'User2', null, null
    union all select 'USA'   , 'User3', null, null
    union all select 'Canada', 'User1', 'BC', 'User4'
    union all select 'Canada', 'User1', 'BC', 'User5'
    union all select 'Canada', 'User1', 'ON', 'User4'
    union all select 'Canada', 'User1', 'ON', 'User8'
    union all select 'Canada', 'User2', 'BC', 'User4'
    union all select 'Canada', 'User2', 'BC', 'User5'
    union all select 'Canada', 'User2', 'ON', 'User4'
    union all select 'Canada', 'User2', 'ON', 'User8'
order by Country, CountryUser, State, StateUser
</code>```

these are the results:

Edit:

This works because Sql Server 2012 considers null values as the lowest possible. See MSDN for reference, here is an excerpt:

ASC | DESC

  Specifies that the values in the specified column should be
  sorted in ascending or descending order. ASC sorts from the lowest
  value to highest value. DESC sorts from highest value to lowest value.
  ASC is the default sort order. Null values are treated as the lowest
  possible values.

Title: SQL Server - Sorting by category

Body:
I have a sorting question. I am using SQL Server 2012. I have the following dataset. The empty lines are for display purposes.

Country        CountryUser        State        StateUser
---------------------------------------------------------
Canada         User1              null         null
Canada         User2              null         null
USA            User2              null         null
USA            User3              null         null

Canada         User1              BC           User4
Canada         User1              BC           User5
Canada         User1              ON           User4
Canada         User1              ON           User8

Canada         User2              BC           User4
Canada         User2              BC           User5
Canada         User2              ON           User4
Canada         User2              ON           User8
total rows: 12

I want to sort the rows such that all the rows that contain a state are shown below their country(row with null as state). Example,

Country        CountryUser        State        StateUser
---------------------------------------------------------
Canada         User1              null         null
Canada         User1              BC           User4
Canada         User1              BC           User5
Canada         User1              ON           User4
Canada         User1              ON           User8

Canada         User2              null         null
Canada         User2              BC           User4
Canada         User2              BC           User5
Canada         User2              ON           User4
Canada         User2              ON           User8

USA            User2              null         null
USA            User3              null         null
total rows: 12

The first sort category is Country. The row with an empty state is shown first, then all the states are non null are shown afterwards. These rows have a secondary sort of state

Body:
Try adding an order by clause with four columns:

select 'Canada'  as Country   ,    'User1' as CountryUser,null as State,null as StateUser
    union all select 'Canada', 'User2', null, null
    union all select 'USA'   , 'User2', null, null
    union all select 'USA'   , 'User3', null, null
    union all select 'Canada', 'User1', 'BC', 'User4'
    union all select 'Canada', 'User1', 'BC', 'User5'
    union all select 'Canada', 'User1', 'ON', 'User4'
    union all select 'Canada', 'User1', 'ON', 'User8'
    union all select 'Canada', 'User2', 'BC', 'User4'
    union all select 'Canada', 'User2', 'BC', 'User5'
    union all select 'Canada', 'User2', 'ON', 'User4'
    union all select 'Canada', 'User2', 'ON', 'User8'
order by Country, CountryUser, State, StateUser

these are the results:

Edit:

This works because Sql Server 2012 considers null values as the lowest possible. See MSDN for reference, here is an excerpt:

ASC | DESC

  Specifies that the values in the specified column should be
  sorted in ascending or descending order. ASC sorts from the lowest
  value to highest value. DESC sorts from highest value to lowest value.
  ASC is the default sort order. Null values are treated as the lowest
  possible values.


答: Select * from table order  by table_atr desc</p>

Title: SQL Server - Sorting by category


问:
Lines and drawables draw order in Android Google Maps
sorry for my english ;-P!

I'm writing an app that shows in google maps pipes and valves(lines and points). For that i've two classes.

One extends from ItemizedOverlay, and i add to map overlay one for each category of valve, and each one can have more than one item.

The second extends from Overlay, and in the draw method y draw the line with the method drawline of the canvas.

The problem i've is that the lines are drawn over the ItemizedOverlay items, no matter the order i add them to the mapView.getOverlays().

I want the lines on the bottom of all overlays. is there a way to do that.

Thanks very much.

答:
I've solved it! I didn't know that when you populate the itemizedovelay items, they automatically are added to the map, so i was populating them before adding the lineoverlays and after that i were adding them again.

The order you add them to getOverlays() affects correctly!!

Thks


问:
Restricted Google API Key not giving back result?
So I am consuming the Google Link Shortener API. I have created an API Key for access in the Developer Console. When the API has no restrictions, then I am able to call it and get the result, but when I restrict the key with my android package name and SHA-1 then it gives an error The request did not specify any Android package name or signing-certificate fingerprint. Please ensure that the client is sending them or use the API Console to update your key restrictions. Although I am sending the request from the Android app. I am using retrofit for networking.

Do i have to explicitly specify the package name? and so then how?

答:
The problem was I didn't had the SHA-1 of the keystore in my Google API Console.

Just to clear up guys. When you generate a signed apk, it is signed with a different SHA-1 key than your debug SHA-1 key, so be sure to also add the SHA-1 from the keystore you have signed with.

You can get the SHA-1 from the keystore by this command : keytool -list -v -keystore keystorename.jks -alias myaliasname


问:
Nesting For Loops Example
I have a problem where I have to Modify function multiplyAll so that it multiplies the product variable by each number in the sub-arrays of arr. Here is the code snippet:

```<code>function multiplyAll(arr) {
  var product = 1;
  // Only change code below this line
  //Here I have written this but its not working!
      <!--
       for(product = 1; product < arr.length; product++){
       return arr[product];
      }
      -->
  // Only change code above this line
  return product;
}

// Modify values below to test your code
multiplyAll([[1,2],[3,4],[5,6,7]]);
</code>```

Its is displaying [3,4] in the answer.

What am I doing wrong?

答:
replace your for loop with :

```<code>for(var i=0; i<arr.length; i++)
{
    for(var j=0; j<arr[i].length; j++)
    {
        product *= arr[i][j] ;
    }
}
</code>```

Title: Nesting For Loops Example

Body:
I have a problem where I have to Modify function multiplyAll so that it multiplies the product variable by each number in the sub-arrays of arr. Here is the code snippet:

function multiplyAll(arr) {
  var product = 1;
  // Only change code below this line
  //Here I have written this but its not working!

  // Only change code above this line
  return product;
}

// Modify values below to test your code
multiplyAll([[1,2],[3,4],[5,6,7]]);

Its is displaying [3,4] in the answer.

What am I doing wrong?

Body:
replace your for loop with :

for(var i=0; i<arr.length; i++)
{
    for(var j=0; j<arr[i].length; j++)
    {
        product *= arr[i][j] ;
    }
}


问:
how can I mock a service to throw an exception a method that returns a List?
I am facing this little issue. I have a service this way

```<code>public class Myservice {

   MyRestService myRestService;

    public List<String> getNames() throws RestClientException {
        return myRestService.getNames();
    }

....
</code>```

and a controller that is like this:

```<code>@RequestMapping(value = URL, method = GET)
    public ModelAndView display(final ModelMap model) {
        ....
        try{
            List<String> listOfNames = myService.getNames();
        }catch(RestClientException e){
            LOG.error("Error when invoking Names service", e);
        }
        model.addAttribute("names", listOfNames);
        return new ModelAndView(VIEW, model);
    }....
</code>```

So far works so good, the unit testing for the case the service actually
 returns a list of Strings works fine.

But since the service calls another one that is basically a rest client that could throw an exception, I want to mock that case.

If I have myService calling myRestClientService where myRestClientService throws an exception should I hadd to the method signature "throws Exception" ?

```<code> final RestClientException myException =  mockery.mock(RestClientException.class);
        mockery.checking(new Expectations() {
            {
                oneOf(myService).getNames();
                will(returnValue(myException));
...
</code>```

But I get an error that I cannot throw an exception from a method that only return List is there anyway to fix this? How could I test it?

答:
According to the documentation Throwing Exceptions from Mocked Methods you should use throwException rather than returnValue. It means code should be something like

```<code> will(throwException(myException));
</code>```

Body:
I am facing this little issue. I have a service this way

public class Myservice {

   MyRestService myRestService;

    public List<String> getNames() throws RestClientException {
        return myRestService.getNames();
    }

....

and a controller that is like this:

@RequestMapping(value = URL, method = GET)
    public ModelAndView display(final ModelMap model) {
        ....
        try{
            List<String> listOfNames = myService.getNames();
        }catch(RestClientException e){
            LOG.error("Error when invoking Names service", e);
        }
        model.addAttribute("names", listOfNames);
        return new ModelAndView(VIEW, model);
    }....

So far works so good, the unit testing for the case the service actually
 returns a list of Strings works fine.

But since the service calls another one that is basically a rest client that could throw an exception, I want to mock that case.

If I have myService calling myRestClientService where myRestClientService throws an exception should I hadd to the method signature "throws Exception" ?

 final RestClientException myException =  mockery.mock(RestClientException.class);
        mockery.checking(new Expectations() {
            {
                oneOf(myService).getNames();
                will(returnValue(myException));
...

But I get an error that I cannot throw an exception from a method that only return List is there anyway to fix this? How could I test it?

Body:
According to the documentation Throwing Exceptions from Mocked Methods you should use throwException rather than returnValue. It means code should be something like

 will(throwException(myException));


问:
dc.js pie chart is empty
I'm trying to link a pie chart to a map so that when you click a state the pie chart updates with the popular vote for that state.

Currently my piechart is displaying empty.

Csv is formatted like so:

```<code>state, party, votes
Alabama,dem,725704
Alabama,rep,1314431
Alabama,lib,44211
Alabama,gre,20276
Alabama,con,9341
Alaska,dem,116454
Alaska,rep,163387
Alaska,lib,18725
Alaska,gre,5735
Alaska,con,3866
Alaska,other,10441
</code>```

My code:

```<code>d3.csv("elecVotes.csv", function (data) {
    d3.json("us.json", function (json){

       // set up crossfilter on the data.
        var ndx = crossfilter(data);

    // set up the dimensions
        var stateDim = ndx.dimension(function (d) { return d.state; });
        var party = partyDim.group().reduceSum(function(d) { return d.votes;});
        var votesDim = ndx.dimension(function(d) { return d.votes; });

    // set up the groups/values
        var state = stateDim.group();
        var party = partyDim.group();
        var votes = votesDim.group();

    // the 4 different charts - options are set below for each one.
        var pie = dc.pieChart('#chart-pie');
        var usmap = dc.geoChoroplethChart("#usmap");

    //create pie from to show popular vote for each state
        pie
        .width(180)
        .height(180)
        .radius(80)
        .dimension(partyDim)
        .group(votes)
        .renderLabel(true)
        .innerRadius(10)
        .transitionDuration(500)
        .colorAccessor(function (d, i) { return d.value; });

    //display US map
        usmap
        .width(900)
        .height(500)
        .dimension(stateDim)
        .group(state)
        .colors(["rgb(20,202,255)","rgb(144,211,035)"])
        .overlayGeoJson(json.features, "name", function (d) { return d.properties.name; })

        // at the end this needs to be called to actually go through and generate all the graphs on the page.
        dc.renderAll();
    });
    });
</code>```

I'm not sure what i'm doing wrong

答:
I don't think you want a votesDim - that would group by the number of votes, so you would probably end up with a different bin for each count, since they are likely to be unique.

I'm guessing you probably want to count the number of votes for each party, so:

```<code>var partyGroup = partyDim.group().reduceSum(function(d) { return d.votes; });
</code>```

Remember that a dimension specifies what you want to filter on, and a group is where data gets aggregated and read.

You also need to convert any numbers explicitly before you get started, since d3.csv will read every field as a string. So:

```<code>data.forEach(function(r) {
  r.votes = +r.votes;
});
var ndx = crossfilter(data);
</code>```

Not sure if this helps with your problem. Note that this really has nothing to do with the map; the pie chart should be able to draw itself independent of what the map is doing.

Edit

I bet the problem is those spaces in the column names. You could easily end up with fields named " party" and " votes" that way...

Title: dc.js pie chart is empty

Body:
I'm trying to link a pie chart to a map so that when you click a state the pie chart updates with the popular vote for that state.

Currently my piechart is displaying empty.

Csv is formatted like so:

state, party, votes
Alabama,dem,725704
Alabama,rep,1314431
Alabama,lib,44211
Alabama,gre,20276
Alabama,con,9341
Alaska,dem,116454
Alaska,rep,163387
Alaska,lib,18725
Alaska,gre,5735
Alaska,con,3866
Alaska,other,10441

My code:

d3.csv("elecVotes.csv", function (data) {
    d3.json("us.json", function (json){

       // set up crossfilter on the data.
        var ndx = crossfilter(data);

    // set up the dimensions
        var stateDim = ndx.dimension(function (d) { return d.state; });
        var party = partyDim.group().reduceSum(function(d) { return d.votes;});
        var votesDim = ndx.dimension(function(d) { return d.votes; });

    // set up the groups/values
        var state = stateDim.group();
        var party = partyDim.group();
        var votes = votesDim.group();

    // the 4 different charts - options are set below for each one.
        var pie = dc.pieChart('#chart-pie');
        var usmap = dc.geoChoroplethChart("#usmap");

    //create pie from to show popular vote for each state
        pie
        .width(180)
        .height(180)
        .radius(80)
        .dimension(partyDim)
        .group(votes)
        .renderLabel(true)
        .innerRadius(10)
        .transitionDuration(500)
        .colorAccessor(function (d, i) { return d.value; });

    //display US map
        usmap
        .width(900)
        .height(500)
        .dimension(stateDim)
        .group(state)
        .colors(["rgb(20,202,255)","rgb(144,211,035)"])
        .overlayGeoJson(json.features, "name", function (d) { return d.properties.name; })

        // at the end this needs to be called to actually go through and generate all the graphs on the page.
        dc.renderAll();
    });
    });

I'm not sure what i'm doing wrong

Body:
I don't think you want a votesDim - that would group by the number of votes, so you would probably end up with a different bin for each count, since they are likely to be unique.

I'm guessing you probably want to count the number of votes for each party, so:

var partyGroup = partyDim.group().reduceSum(function(d) { return d.votes; });

Remember that a dimension specifies what you want to filter on, and a group is where data gets aggregated and read.

You also need to convert any numbers explicitly before you get started, since d3.csv will read every field as a string. So:

data.forEach(function(r) {
  r.votes = +r.votes;
});
var ndx = crossfilter(data);

Not sure if this helps with your problem. Note that this really has nothing to do with the map; the pie chart should be able to draw itself independent of what the map is doing.

Edit

I bet the problem is those spaces in the column names. You could easily end up with fields named " party" and " votes" that way...


问:
how to configure kotlin-logging logger
For logging purposes in my Kotlin project I am using kotlin-logging which is really nice to use however I am missing a very central point: How can I configure the log level of the logger?

Per default it is set to info and I would like to set it to debug. As there is nothing about that on the Github page nor is there any method to set the level programmatically I had a look into slf4j as kotlin-logging is a wrapper around that.

Apparently I have to set a System Property like this:

```<code>-Dorg.slf4j.simpleLogger.defaultLogLevel=DEBUG
</code>```

However I have no idea how to do this in Kotlin.

Anyone can help me out?

答:
We do not have a way to change log level from slf4j api and we need to rely on implementation

By looking at the Logger interface of slf4j, you can see that it has isLevelEnabled() for all the levels, but not a setter. Therefore, setting the level is implementation specific and it is based on the underlying logging platform you use.

From your question it looks like you use slf4j SimpleLogger as the layer behind slf4j. For SimpleLogger, you can only change the log level through properties like you've already done.
See this question for more information.

```<code>System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "TRACE");
</code>```

Please note that once the logger is created the log level can't be changed. If you need to dynamically change the logging level you might want to use log4j with SLF4J.

Title: how to configure kotlin-logging logger

Body:
For logging purposes in my Kotlin project I am using kotlin-logging which is really nice to use however I am missing a very central point: How can I configure the log level of the logger?

Per default it is set to info and I would like to set it to debug. As there is nothing about that on the Github page nor is there any method to set the level programmatically I had a look into slf4j as kotlin-logging is a wrapper around that.

Apparently I have to set a System Property like this:

-Dorg.slf4j.simpleLogger.defaultLogLevel=DEBUG

However I have no idea how to do this in Kotlin.

Anyone can help me out?

Body:
We do not have a way to change log level from slf4j api and we need to rely on implementation

By looking at the Logger interface of slf4j, you can see that it has isLevelEnabled() for all the levels, but not a setter. Therefore, setting the level is implementation specific and it is based on the underlying logging platform you use.

From your question it looks like you use slf4j SimpleLogger as the layer behind slf4j. For SimpleLogger, you can only change the log level through properties like you've already done.
See this question for more information.

System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "TRACE");

Please note that once the logger is created the log level can't be changed. If you need to dynamically change the logging level you might want to use log4j with SLF4J.


问:
Why does the ternary operator work differently at compile time compared to the run time?
```<code>template <uint_fast64_t MAX_RAND>
    struct RandomLimit
    {
        static const uint_fast64_t steps =
            RAND_MAX >= MAX_RAND ?
            1 :
            RandomLimit<MAX_RAND / (RAND_MAX + 1)>::steps + 1;
    };
</code>```

The code above yields and error; steps is undefined. At run time, if a function call would be in place of the last expression, this function would not be called and no side - effects would happen. Why isn't this code eliminated when the condition holds true?

Note that RAND_MAX is the cstdlib constant and I'm using a Visual Studio 2015 setup.

答:
Probably yours is a linker error, not a compiler error.

That is, the steps member is properly declared in a header file, so the code compiles fine, but then, if the condition is always true, then the compiler optimizes out the else expression and no reference to the steps variable is emitted. However, if the condition is not constant, then both branches are compiled, and you get an undefined reference error.

The solution, obviously, is to add the proper library to the linker command.

Body:
template <uint_fast64_t MAX_RAND>
    struct RandomLimit
    {
        static const uint_fast64_t steps =
            RAND_MAX >= MAX_RAND ?
            1 :
            RandomLimit<MAX_RAND / (RAND_MAX + 1)>::steps + 1;
    };

The code above yields and error; steps is undefined. At run time, if a function call would be in place of the last expression, this function would not be called and no side - effects would happen. Why isn't this code eliminated when the condition holds true?

Note that RAND_MAX is the cstdlib constant and I'm using a Visual Studio 2015 setup.


问:
IDEA: Grails SDK is not selected
How do I set up Grails with IntelliJ IDEA? I have the ultimate edition. sdkman installed Grails in /home/user/.sdkman/candidates/grails/current/bin/grails
 but if I choose that folder as the SDK, IDEA says: The selected directory is not a valid home for Grails SDK

How do I set up an IDEA project with Grails?

grails -version from the terminal provides me the following:

```<code>| Grails Version: 3.2.8
| Groovy Version: 2.4.10
| JVM Version: 1.8.0_121
</code>```

So it's definitely there..

答:
There is no need to select a grails SDK with your version. Import a project from existing sources -> double click the build.gradle file - it will import and setup everything correctly - Assuming IntelliJ 2016.x or 2017.x

Video of process here: http://guides.grails.org/grails-quickcasts-developing-grails-3-applications-with-intellij-idea/guide/index.html

Edit: I see what your saying now, you can't select current in sdkman - use /home/user/.sdkman/candidates/grails/3.2.8

Title: IDEA: Grails SDK is not selected

Body:
How do I set up Grails with IntelliJ IDEA? I have the ultimate edition. sdkman installed Grails in /home/user/.sdkman/candidates/grails/current/bin/grails
 but if I choose that folder as the SDK, IDEA says: The selected directory is not a valid home for Grails SDK

How do I set up an IDEA project with Grails?

grails -version from the terminal provides me the following:

| Grails Version: 3.2.8
| Groovy Version: 2.4.10
| JVM Version: 1.8.0_121

So it's definitely there..


问:
merging columns when column name is an integer
this is my dataframe:

```<code>A   B     1    2    5

1   1     26    5   5
2   6     4     2   2
2   3     8     7   7
2   4     3    10   19
</code>```

So here you can see that columns are 'A' 'B' '1' '2' '5'
I want to create a function that accepts one argument as int and then creates a new dataframe that has 'A' 'B' columns and column which was passed as arugment.
so it would go like this, however im not sure how to get the column when its name is an int any ideas ? Thanks!:

```<code>def merge(arg):
    new_df=pd.concat([df.A, df.B, df.arg], axis=1 )
merge(5)

A   B     5

1   1     5
2   6     2
2   3     7
2   4     19
</code>```

答:
Simpliest solution is selecting by subset with copy:

```<code>def merge(arg):
     return df[['A', 'B', str(arg)]].copy()

print (merge(5))
   A  B   5
0  1  1   5
1  2  6   2
2  2  3   7
3  2  4  19
</code>```

Solutions with concat if column is str only convert to str - works nice, but slowier:

```<code>def merge(arg):
     return pd.concat([df.A, df.B, df[str(arg)]], axis=1 )

print (merge(5))
   A  B   5
0  1  1   5
1  2  6   2
2  2  3   7
3  2  4  19
</code>```

If int:

```<code>def merge(arg):
     return pd.concat([df.A, df.B, df[arg]], axis=1 )

print (merge(5))
   A  B   5
0  1  1   5
1  2  6   2
2  2  3   7
3  2  4  19
</code>```

Body:
this is my dataframe:

A   B     1    2    5

1   1     26    5   5
2   6     4     2   2
2   3     8     7   7
2   4     3    10   19

So here you can see that columns are 'A' 'B' '1' '2' '5'
I want to create a function that accepts one argument as int and then creates a new dataframe that has 'A' 'B' columns and column which was passed as arugment.
so it would go like this, however im not sure how to get the column when its name is an int any ideas ? Thanks!:

def merge(arg):
    new_df=pd.concat([df.A, df.B, df.arg], axis=1 )
merge(5)

A   B     5

1   1     5
2   6     2
2   3     7
2   4     19

Body:
Simpliest solution is selecting by subset with copy:

def merge(arg):
     return df[['A', 'B', str(arg)]].copy()

print (merge(5))
   A  B   5
0  1  1   5
1  2  6   2
2  2  3   7
3  2  4  19

Solutions with concat if column is str only convert to str - works nice, but slowier:

def merge(arg):
     return pd.concat([df.A, df.B, df[str(arg)]], axis=1 )

print (merge(5))
   A  B   5
0  1  1   5
1  2  6   2
2  2  3   7
3  2  4  19

If int:

def merge(arg):
     return pd.concat([df.A, df.B, df[arg]], axis=1 )

print (merge(5))
   A  B   5
0  1  1   5
1  2  6   2
2  2  3   7
3  2  4  19


问:
matplotilib - instantiating an Axis from matplotlib.axis.py
I'm working on a project where we have to implement a feature for matplotlib. We had to change the code in the Axis class. I have to write unit tests to make sure the things we added work as expected. I have to instantiate an Axis() object, but I don't know how to that. I've searched through the examples but they all use pyplot. Even the code in tests/test_axes.py uses pyplot. Is there a way to instantiate an Axis?</p>

答:
Most pyplot commands instantiate an Axes. E.g.

```<code>import matplotlib.pyplot as plt

ax = plt.gca()
print (ax, type(ax))
</code>```

gives you an Axes as ax. The code prints

```<code><matplotlib.axes._subplots.AxesSubplot object at 0x000000001B8AB0B8>,
<class 'matplotlib.axes._subplots.AxesSubplot'>
</code>```

Further examples, which all give you an Axes instance:

```<code>#1
fig, ax = plt.subplots()
#2
ax = plt.gcf().add_subplot(111)
#3
ax = plt.gcf().add_axes([.1,.1,.9,.9])
</code>```

You simply don't gain anything by not using pyplot.

Body:
Most pyplot commands instantiate an Axes. E.g.

import matplotlib.pyplot as plt

ax = plt.gca()
print (ax, type(ax))

gives you an Axes as ax. The code prints

<matplotlib.axes._subplots.AxesSubplot object at 0x000000001B8AB0B8>,
<class 'matplotlib.axes._subplots.AxesSubplot'>

Further examples, which all give you an Axes instance:

#1
fig, ax = plt.subplots()
#2
ax = plt.gcf().add_subplot(111)
#3
ax = plt.gcf().add_axes([.1,.1,.9,.9])

You simply don't gain anything by not using pyplot.


问:
Create array with a normal distribution
I'm trying to create a vba array of size N, with normally distributed percentages that add 100%. This means that with an input of an integer number N, I should get as output a n-sized array with values that sum 100 and is normally distributed

How can I do this?

Thanks!!

答:
I was curious so I did one.</p>

```<code>Sub NDArray()
Dim arr() As Double
a = InputBox("Number of numbers")

ReDim arr(a - 1) As Double
With Application.WorksheetFunction
    'fill the array with random numbers that fit a normal dist
    For i = 0 To a - 1
        '"A/100" is the target mean and "A/200" is target Std. Dev, change to your desire
        arr(i) = .Norm_Inv(.RandBetween(1, 1000) / 10000, a / 100, a / 200)
    Next i
    'change the numbers to percentage of whole and multiply by 100 to get the values to sum to 100
    x = .Sum(arr)
    For i = 0 To a - 1
        arr(i) = (arr(i) / x) * 100
    Next i
    'post the numbers in Column A on the active sheet
    ActiveSheet.Range("A:A").ClearContents
    ActiveSheet.Range("A1").Resize(a).Value = .Transpose(arr)
End With
End Sub
</code>```

Title: Create array with a normal distribution

Body:
I was curious so I did one.

Sub NDArray()
Dim arr() As Double
a = InputBox("Number of numbers")

ReDim arr(a - 1) As Double
With Application.WorksheetFunction
    'fill the array with random numbers that fit a normal dist
    For i = 0 To a - 1
        '"A/100" is the target mean and "A/200" is target Std. Dev, change to your desire
        arr(i) = .Norm_Inv(.RandBetween(1, 1000) / 10000, a / 100, a / 200)
    Next i
    'change the numbers to percentage of whole and multiply by 100 to get the values to sum to 100
    x = .Sum(arr)
    For i = 0 To a - 1
        arr(i) = (arr(i) / x) * 100
    Next i
    'post the numbers in Column A on the active sheet
    ActiveSheet.Range("A:A").ClearContents
    ActiveSheet.Range("A1").Resize(a).Value = .Transpose(arr)
End With
End Sub


问:
Tree Selection SAPUI5 (with checkboxes). Auto check child nodes when parent node is checked
I have a Tree element with MultiSelect mode, which looks good with checkboxes in every tree item. I wanted to make it so if I select one item, all of its children get selected as well. I have the tree in the view:

```<code> <Tree selectionChange="onPress" itemPress="onPress" includeItemInSelection="true" mode="MultiSelect" items="{path: '/'}" id="Tree">
    <headerToolbar>
       <Toolbar>
          <content>
             <Title level="H2" text="Title"/>
             <ToolbarSpacer/>
          </content>
       </Toolbar>
    </headerToolbar>
    <StandardTreeItem title="{text}"/>
 </Tree>
</code>```

I wanted to know if SAPUI5 had a property for its trees like "Check Children" or something similar. And if not, how could I do it.

Thanks.

答:
There are two approaches that come to mind:

Using binding (the 'better' way).

Using JS manipulation (the 'harder' way - because the TreeItem has no method for retrieving the child items).

I will show you the 'better' way for doing things. The 'harder' one you can do as an extension of this method (if you really want to go that way, I can also add info about how to do it).

Basically you need to extend your model by adding a selected flag. Assuming that you have a typical model structure (as in the explored example), you can use this function:

function addSelectedFlag(aNodes, bSelected) {
    jQuery.each(aNodes, function(iIndex, oNode) {
        oNode.selected = bSelected;
        if (oNode.nodes) {
            addSelectedFlag(oNode.nodes, bSelected);
        }
    });
}
```

<p>Then, when the selection changes, you can do something along the lines of:</p>

```onSelect: function(oEvent) {
    var aItems = oEvent.getParameter("listItems") || [],
        oModel = this.getView().getModel();
    jQuery.each(aItems, function(iIndex, oItem) {
        var oNode = oItem.getBindingContext().getObject(),
            bSelected = oItem.getSelected();
        if (oNode.nodes) {
            addSelectedFlag(oNode.nodes, bSelected);
        }
    });
    oModel.refresh();
}
```

<p>When you specify the template fro your item, you must also bind the selection:</p>

<pre class="lang-xml prettyprint-override"><code><StandardTreeItem title="{text}" selected="{selected}"/>
</code>```

I made a working JsFiddle out of this: https://jsfiddle.net/n4frtzd8/1/

Body:
I have a Tree element with MultiSelect mode, which looks good with checkboxes in every tree item. I wanted to make it so if I select one item, all of its children get selected as well. I have the tree in the view:

 <Tree selectionChange="onPress" itemPress="onPress" includeItemInSelection="true" mode="MultiSelect" items="{path: '/'}" id="Tree">
    <headerToolbar>
       <Toolbar>
          <content>
             <Title level="H2" text="Title"/>
             <ToolbarSpacer/>
          </content>
       </Toolbar>
    </headerToolbar>
    <StandardTreeItem title="{text}"/>
 </Tree>

I wanted to know if SAPUI5 had a property for its trees like "Check Children" or something similar. And if not, how could I do it.

Thanks.

Body:
There are two approaches that come to mind:

Using binding (the 'better' way).

Using JS manipulation (the 'harder' way - because the TreeItem has no method for retrieving the child items).

I will show you the 'better' way for doing things. The 'harder' one you can do as an extension of this method (if you really want to go that way, I can also add info about how to do it).

Basically you need to extend your model by adding a selected flag. Assuming that you have a typical model structure (as in the explored example), you can use this function:

function addSelectedFlag(aNodes, bSelected) {
    jQuery.each(aNodes, function(iIndex, oNode) {
        oNode.selected = bSelected;
        if (oNode.nodes) {
            addSelectedFlag(oNode.nodes, bSelected);
        }
    });
}

Then, when the selection changes, you can do something along the lines of:

onSelect: function(oEvent) {
    var aItems = oEvent.getParameter("listItems") || [],
        oModel = this.getView().getModel();
    jQuery.each(aItems, function(iIndex, oItem) {
        var oNode = oItem.getBindingContext().getObject(),
            bSelected = oItem.getSelected();
        if (oNode.nodes) {
            addSelectedFlag(oNode.nodes, bSelected);
        }
    });
    oModel.refresh();
}

When you specify the template fro your item, you must also bind the selection:

<StandardTreeItem title="{text}" selected="{selected}"/>

I made a working JsFiddle out of this: https://jsfiddle.net/n4frtzd8/1/