问:
How to solve "command find requires authentication" using Golang and MongoDriver
I need help to fix the error. I'm using IBM mongo services.

go version go1.13.6 darwin/amd64

mongo driver version 1.2.1
The connection is working, I can read and write but sometimes it returns
: command find requires authentication and command insert requires authentication

```<code>MONGO_DB_URI=mongodb://username:password:port,host/dbname?authSource=admin&replicaSet=replset&connect=direct&alias=default
</code>```

Connect:

```<code> func ConnectDatabase() *mongo.Client {
    clientOptions := options.Client().ApplyURI(os.Getenv("MONGO_DB_URI"))

    ctx, _ := context.WithTimeout(context.Background(), 30*time.Second)
    var err error
    client, err = mongo.Connect(ctx, clientOptions)

    if err != nil {
        log.Fatal(err)
    }

    ctx, _ = context.WithTimeout(context.Background(), 10*time.Second)
    err = client.Ping(ctx, nil)

    if err != nil {
        log.Fatal(err)
        return nil
    }

    fmt.Println("Connected to MongoDB!")
    return client
}
</code>```

Read:

```<code>func FindAll(collectionName string, query bson.M) (*mongo.Cursor, error) {
    collection := client.Database("dbname").Collection(collectionName)
    singleResult, err := collection.Find(context.TODO(), query)
    return singleResult, err
}
</code>```

Read:

```<code>    ctx, _ := context.WithTimeout(context.Background(), 20*time.Second)
    cur, err := mongo.GetCollection("collection_name").Find(ctx, createQuery())
    if err != nil {
        log.Println(err.Error())
    }

</code>```

I'm using the same database and the same configurations at our another Python Project. No exceptions.

答:
There is a difference between connecting to a DB and performing operations on the DB.
Mongo lets you connect without authentication because you have to be able to connect to be able to authenticate.

```<code>var cred options.Credential

cred.AuthSource = YourAuthSource
cred.Username = YourUserName
cred.Password = YourPassword

// set client options
clientOptions := options.Client().ApplyURI(os.Getenv("MONGO_DB_URI")).SetAuth(cred)

//... the rest of your code
</code>```

Hope this helps.

Body:
I need help to fix the error. I'm using IBM mongo services.

go version go1.13.6 darwin/amd64

mongo driver version 1.2.1
The connection is working, I can read and write but sometimes it returns
: command find requires authentication and command insert requires authentication

MONGO_DB_URI=mongodb://username:password:port,host/dbname?authSource=admin&replicaSet=replset&connect=direct&alias=default

Connect:

 func ConnectDatabase() *mongo.Client {
    clientOptions := options.Client().ApplyURI(os.Getenv("MONGO_DB_URI"))

    ctx, _ := context.WithTimeout(context.Background(), 30*time.Second)
    var err error
    client, err = mongo.Connect(ctx, clientOptions)

    if err != nil {
        log.Fatal(err)
    }

    ctx, _ = context.WithTimeout(context.Background(), 10*time.Second)
    err = client.Ping(ctx, nil)

    if err != nil {
        log.Fatal(err)
        return nil
    }

    fmt.Println("Connected to MongoDB!")
    return client
}

Read:

func FindAll(collectionName string, query bson.M) (*mongo.Cursor, error) {
    collection := client.Database("dbname").Collection(collectionName)
    singleResult, err := collection.Find(context.TODO(), query)
    return singleResult, err
}

Read:

    ctx, _ := context.WithTimeout(context.Background(), 20*time.Second)
    cur, err := mongo.GetCollection("collection_name").Find(ctx, createQuery())
    if err != nil {
        log.Println(err.Error())
    }

I'm using the same database and the same configurations at our another Python Project. No exceptions.

Body:
There is a difference between connecting to a DB and performing operations on the DB.
Mongo lets you connect without authentication because you have to be able to connect to be able to authenticate.

var cred options.Credential

cred.AuthSource = YourAuthSource
cred.Username = YourUserName
cred.Password = YourPassword

// set client options
clientOptions := options.Client().ApplyURI(os.Getenv("MONGO_DB_URI")).SetAuth(cred)

//... the rest of your code

Hope this helps.


来源: stackexchange

问:
Get domain name without subdomains using JavaScript?
How to get the domain name without subdomains?

e.g. if the url is "http://one.two.roothost.co.uk/page.html" how to get "roothost.co.uk"?

答:
My solution worked for me: Get "gocustom.com" from "shop.gocustom.com"</p>

```<code>var site_domain_name = 'shop.gocustom.com';
alert(site_domain_name);
var strsArray = site_domain_name.split('.');
var strsArrayLen = strsArray.length;
alert(strsArray[eval(strsArrayLen - 2)]+'.'+strsArray[eval(strsArrayLen - 1)])
</code>```

ContentLicense: CC BY-SA 3.0

Body:
My solution worked for me: Get "gocustom.com" from "shop.gocustom.com"

var site_domain_name = 'shop.gocustom.com';
alert(site_domain_name);
var strsArray = site_domain_name.split('.');
var strsArrayLen = strsArray.length;
alert(strsArray[eval(strsArrayLen - 2)]+'.'+strsArray[eval(strsArrayLen - 1)])

ContentLicense: CC BY-SA 4.0


问:
Delete ClusterRoleBinding and Namespace from process running in same Namespace in Kubernetes
I create a Namespace on a kube cluster and install my app within that Namespace. The app sends regular heartbeat requests to the server and if it gets a "remove yourself" response from the server, it deletes itself by calling delete on the entire kube Namespace. I also give the app cluster wide access by creating a ClusterRoleBinding, making a ServiceAccount subject to the ClusterRoleBinding and running the pod with this ServiceAccount.

The problem is that I want to remove the ClusterRoleBinding as part of app's self removal process (if that's possible).  If I remove the ClusterRoleBinding before then the app won't be able to issue a delete on the Namespace so it seems like a chicken-and-egg problem. Is there a way to do this?

This is what I have already tried to no avail:

Added a PreStop handler in the app container. So now when the app calls delete on the entire Namespace, kube calls this handler before killing the container. In this PreStop handler if I sleep for more than 5 sec before calling delete on ClusterRoleBinding I get "Unauthorized" response back from kubernetes.

This lead me to think that maybe the ServiceAccount that's linked to the ClusterRoleBinding gets deleted before the app has had a chance to delete the ClusterRoleBinding in the PreStop handler. So to test this, before issuing delete on the Namespace I add a finalizer to the ServiceAccount, then in the PreStop handler I wait for 5 sec, issue delete on the ClusterRoleBinding (again get "Unauthorized" error back), then I get the ServiceAccount object by name (get "Unauthorized" error back), remove the finalizer from the ServiceAccount (get error "error="finalizer doesn't exist for object ''") because it can't remove the finalizer on an empty object. When I use kubectl I find that the ServiceAccount exists but is in "Terminating" state as expected with the finalizer still set.

Does kube revoke access when ServiceAccount is in "Terminating" state even though its not yet hard deleted?

Is there a way to remove the ClusterRoleBinding and the Namespace from the same process that is running in the Namespace that needs to be deleted? (Given that the ClusterRoleBinding we want to remove gives the app permission to delete the Namespace in the first place)

Any help would be much appreciated!

The YAML definitions for ClusterRoleBinding and ServiceAccount are as below:

### ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
    creationTimestamp: null
    name: xyz-myapp-cluster-admin
roleRef:
    apiGroup: rbac.authorization.k8s.io
    kind: ClusterRole
    name: cluster-admin
subjects:
- kind: ServiceAccount
  name: xyz
  namespace: xyz-myapp

### ServiceAccount
apiVersion: v1
kind: ServiceAccount
metadata:
    creationTimestamp: null
    name: xyz
    namespace: xyz-myapp
```

<p>Relevant application logs:</p>

```time="2020-02-18T16:08:33Z" level=info msg="App instructed to remove itself"
time="2020-02-18T16:08:33Z" level=info msg="Created finalizer 'xyz.myapp.com/my-finalizer' on ServiceAccount"
time="2020-02-18T16:08:33Z" level=info msg="Called delete on Namespace"
time="2020-02-18T16:08:38Z" level=info msg="PreStop handler called"
time="2020-02-18T16:08:38Z" level=info msg="----- sleeping for 5 sec -----"
time="2020-02-18T16:08:43Z" level=info msg="Deleting ClusterRoleBinding"
time="2020-02-18T16:08:43Z" level=warning msg="Failed to delete ClusterRoleBinding" error="Unexpected error removing ClusterRolebinding: Unauthorized"
time="2020-02-18T16:08:43Z" level=warning msg="Failed to get ServiceAccount" error=Unauthorized
time="2020-02-18T16:08:43Z" level=warning msg="Failed to remove finalizer from ServiceAccount" error="finalizer 'xyz.myapp.com/my-finalizer' doesn't exist for object ''"
```

答:
After digging through kubernetes documentation, the most reliable way I found to do this is:

When the app gets a "remove yourself" response back from server it makes the ClusterRoleBinding the owner of the Namespace the app runs in.

This can be done by adding ClusterRoleBinding under Namespace's medatadata.owerReferences using patch.

Once ClusterRoleBinding is successfully added as an owner for Namespace, then the app can call delete on the ClusterRoleBinding using DeletePropagationBackground.

Below is an example of how a patch that adds a ClusterRoleBinding to ownerReferences of a Namespace can be applied (in Golang).

import (
    "encoding/json"
    "k8s.io/client-go/kubernetes"
    v1 "k8s.io/api/core/v1"
    rbacv1 "k8s.io/api/rbac/v1"
    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type ownerReferencePatch struct {
    Op    string                  `json:"op"`
    Path  string                  `json:"path"`
    Value []metav1.OwnerReference `json:"value"`
}

func AddClusterRoleBindingOwnerReferenceToNamespace(client kubernetes.Interface, crb *rbacv1.ClusterRoleBinding, ns *v1.Namespace) (*v1.Namespace, error) {
    patch, err := json.Marshal([]ownerReferencePatch{
        {
            Op:   "add",
            Path: "/metadata/ownerReferences",
            Value: []metav1.OwnerReference{
                {
                    APIVersion:         crb.RoleRef.APIGroup,
                    BlockOwnerDeletion: func(in bool) *bool {return &in}(true),
                    Kind:               "ClusterRoleBinding",
                    Name:               crb.GetName(),
                    UID:                crb.GetUID(),
                },
            },
        },
    })
    if err != nil {
        return nil, err
    }

    return client.CoreV1().Namespaces().Patch(ns.GetName(), types.JSONPatchType, patch)
}
```

Body:
I create a Namespace on a kube cluster and install my app within that Namespace. The app sends regular heartbeat requests to the server and if it gets a "remove yourself" response from the server, it deletes itself by calling delete on the entire kube Namespace. I also give the app cluster wide access by creating a ClusterRoleBinding, making a ServiceAccount subject to the ClusterRoleBinding and running the pod with this ServiceAccount.

The problem is that I want to remove the ClusterRoleBinding as part of app's self removal process (if that's possible).  If I remove the ClusterRoleBinding before then the app won't be able to issue a delete on the Namespace so it seems like a chicken-and-egg problem. Is there a way to do this?

This is what I have already tried to no avail:

Added a PreStop handler in the app container. So now when the app calls delete on the entire Namespace, kube calls this handler before killing the container. In this PreStop handler if I sleep for more than 5 sec before calling delete on ClusterRoleBinding I get "Unauthorized" response back from kubernetes.

This lead me to think that maybe the ServiceAccount that's linked to the ClusterRoleBinding gets deleted before the app has had a chance to delete the ClusterRoleBinding in the PreStop handler. So to test this, before issuing delete on the Namespace I add a finalizer to the ServiceAccount, then in the PreStop handler I wait for 5 sec, issue delete on the ClusterRoleBinding (again get "Unauthorized" error back), then I get the ServiceAccount object by name (get "Unauthorized" error back), remove the finalizer from the ServiceAccount (get error "error="finalizer doesn't exist for object ''") because it can't remove the finalizer on an empty object. When I use kubectl I find that the ServiceAccount exists but is in "Terminating" state as expected with the finalizer still set.

Does kube revoke access when ServiceAccount is in "Terminating" state even though its not yet hard deleted?

Is there a way to remove the ClusterRoleBinding and the Namespace from the same process that is running in the Namespace that needs to be deleted? (Given that the ClusterRoleBinding we want to remove gives the app permission to delete the Namespace in the first place)

Any help would be much appreciated!

The YAML definitions for ClusterRoleBinding and ServiceAccount are as below:

### ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
    creationTimestamp: null
    name: xyz-myapp-cluster-admin
roleRef:
    apiGroup: rbac.authorization.k8s.io
    kind: ClusterRole
    name: cluster-admin
subjects:
- kind: ServiceAccount
  name: xyz
  namespace: xyz-myapp

### ServiceAccount
apiVersion: v1
kind: ServiceAccount
metadata:
    creationTimestamp: null
    name: xyz
    namespace: xyz-myapp

Relevant application logs:

time="2020-02-18T16:08:33Z" level=info msg="App instructed to remove itself"
time="2020-02-18T16:08:33Z" level=info msg="Created finalizer 'xyz.myapp.com/my-finalizer' on ServiceAccount"
time="2020-02-18T16:08:33Z" level=info msg="Called delete on Namespace"
time="2020-02-18T16:08:38Z" level=info msg="PreStop handler called"
time="2020-02-18T16:08:38Z" level=info msg="----- sleeping for 5 sec -----"
time="2020-02-18T16:08:43Z" level=info msg="Deleting ClusterRoleBinding"
time="2020-02-18T16:08:43Z" level=warning msg="Failed to delete ClusterRoleBinding" error="Unexpected error removing ClusterRolebinding: Unauthorized"
time="2020-02-18T16:08:43Z" level=warning msg="Failed to get ServiceAccount" error=Unauthorized
time="2020-02-18T16:08:43Z" level=warning msg="Failed to remove finalizer from ServiceAccount" error="finalizer 'xyz.myapp.com/my-finalizer' doesn't exist for object ''"


问:
Count the number of groups of the same value in a matrix row in R
In the example below I am showing the first row of a matrix from a simulation in R. The row represents one indivudual in my simulation and columns represent days. If an individual has fever in a given day, then that is coded as "1" in this matrix. I want to output the number of times an individual got fever. In the example below this would be 4 times (4 sets of 1s). Does anyone know how to code this to count adjacent 1s as a single occurrence?

out_fever[1,]

```<code> [1] 0 1 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0
</code>```

答:
We loop over the rows with apply and use rle to calculate the number of blocks of 1 in each row

```<code>out_fever$n_fever <- apply(out_fever, 1,  function(x)  with(rle(x), sum(values==1)))
</code>```

Using a reproducible example for the first row

```<code>v1 <- c(0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
      1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0)
with(rle(v1),  sum(values == 1))
#[1] 4
</code>```

Body:
In the example below I am showing the first row of a matrix from a simulation in R. The row represents one indivudual in my simulation and columns represent days. If an individual has fever in a given day, then that is coded as "1" in this matrix. I want to output the number of times an individual got fever. In the example below this would be 4 times (4 sets of 1s). Does anyone know how to code this to count adjacent 1s as a single occurrence?

out_fever[1,]

 [1] 0 1 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0

Body:
We loop over the rows with apply and use rle to calculate the number of blocks of 1 in each row

out_fever$n_fever <- apply(out_fever, 1,  function(x)  with(rle(x), sum(values==1)))

Using a reproducible example for the first row

v1 <- c(0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
      1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0)
with(rle(v1),  sum(values == 1))
#[1] 4


问:
allowedLateness on Global Window custom trigger
I have created a custom trigger and processing function for my event stream.

```<code>DataStream<DynamoDBRow> dynamoDBRows =
    sensorEvents
        .keyBy("id")
        .window(GlobalWindows.create())
        .trigger(new MyCustomTrigger())
        .allowedLateness(Time.minutes(1)) # Note
        .process(new MyCustomWindowProcessFunction());
</code>```

My trigger is based on an event parameter. Once the event-end signal is received, the MyCustomWindowProcessFunction() is applied on the window elements.

```<code>@Slf4j
public class MyCustomTrigger extends Trigger<SensorEvent, GlobalWindow> {

  @Override
  public TriggerResult onElement(SensorEvent element, long timestamp, GlobalWindow window, TriggerContext ctx) throws Exception {

    if (element.isEventEnd() == true) {
      return TriggerResult.FIRE_AND_PURGE;
    }

    return TriggerResult.CONTINUE;
  }

  @Override
  public TriggerResult onProcessingTime(long time, GlobalWindow window, TriggerContext ctx) throws Exception {
    return TriggerResult.CONTINUE;
  }

  @Override
  public TriggerResult onEventTime(long time, GlobalWindow window, TriggerContext ctx) throws Exception {
    return TriggerResult.CONTINUE;
  }

  @Override
  public void clear(GlobalWindow window, TriggerContext ctx) throws Exception {}
}
</code>```

There could be few sensor data, that could come even after the Trigger. So I added .allowedLateness(Time.minutes(1)), to ensure that, those events are not missed out, while processing.

In my case, allowedLateness is not working.

After going through the documents, I found this

How can I include allowedLateness in GlobalWindow?

Note: I also tried setting environment Time Characteristic

```<code>env.setStreamTimeCharacteristic(TimeCharacteristic.IngestionTime);
</code>```

Update: 20-02-2020

Currently thinking of the below approach. (Not working so far)

```<code>@Slf4j
public class JourneyTrigger extends Trigger<SensorEvent, GlobalWindow> {

  private final long allowedLatenessMillis;

  public JourneyTrigger(Time allowedLateness) {
    this.allowedLatenessMillis = allowedLateness.toMilliseconds();
  }

  @Override
  public TriggerResult onElement(SensorEvent element, long timestamp, GlobalWindow window, TriggerContext ctx) throws Exception {

    if (element.isEventEnd() == true) {
      log.info("Timer started with allowedLatenessMillis " + allowedLatenessMillis);
      ctx.registerEventTimeTimer(System.currentTimeMillis() + allowedLatenessMillis);
    }

    return TriggerResult.CONTINUE;
  }

  @Override
  public TriggerResult onEventTime(long time, GlobalWindow window, TriggerContext ctx) throws Exception {
    log.info("onEvenTime called at "+System.currentTimeMillis() );
    return TriggerResult.FIRE_AND_PURGE;
  }

  @Override
  public TriggerResult onProcessingTime(long time, GlobalWindow window, TriggerContext ctx) throws Exception {
    return TriggerResult.CONTINUE;
  }

  @Override
  public void clear(GlobalWindow window, TriggerContext ctx) throws Exception {}
}
</code>```

答: To be honest I don't see a reason to use the GlobalWindow here. You could just use the KeyedProcessFunction that would serve the same purpose as Your Trigger, basically, it would gather all elements from the beginning of the event till the end of the event into the ListState and then when You receive the isEventEnd()==true, You could simply schedule the EventTime timer that would fire after a minute and would emit the results gathered inside ListState.

Body:
I have created a custom trigger and processing function for my event stream.

DataStream<DynamoDBRow> dynamoDBRows =
    sensorEvents
        .keyBy("id")
        .window(GlobalWindows.create())
        .trigger(new MyCustomTrigger())
        .allowedLateness(Time.minutes(1)) # Note
        .process(new MyCustomWindowProcessFunction());

My trigger is based on an event parameter. Once the event-end signal is received, the MyCustomWindowProcessFunction() is applied on the window elements.

@Slf4j
public class MyCustomTrigger extends Trigger<SensorEvent, GlobalWindow> {

  @Override
  public TriggerResult onElement(SensorEvent element, long timestamp, GlobalWindow window, TriggerContext ctx) throws Exception {

    if (element.isEventEnd() == true) {
      return TriggerResult.FIRE_AND_PURGE;
    }

    return TriggerResult.CONTINUE;
  }

  @Override
  public TriggerResult onProcessingTime(long time, GlobalWindow window, TriggerContext ctx) throws Exception {
    return TriggerResult.CONTINUE;
  }

  @Override
  public TriggerResult onEventTime(long time, GlobalWindow window, TriggerContext ctx) throws Exception {
    return TriggerResult.CONTINUE;
  }

  @Override
  public void clear(GlobalWindow window, TriggerContext ctx) throws Exception {}
}

There could be few sensor data, that could come even after the Trigger. So I added .allowedLateness(Time.minutes(1)), to ensure that, those events are not missed out, while processing.

In my case, allowedLateness is not working.

After going through the documents, I found this

How can I include allowedLateness in GlobalWindow?

Note: I also tried setting environment Time Characteristic

env.setStreamTimeCharacteristic(TimeCharacteristic.IngestionTime);

Update: 20-02-2020

Currently thinking of the below approach. (Not working so far)

@Slf4j
public class JourneyTrigger extends Trigger<SensorEvent, GlobalWindow> {

  private final long allowedLatenessMillis;

  public JourneyTrigger(Time allowedLateness) {
    this.allowedLatenessMillis = allowedLateness.toMilliseconds();
  }

  @Override
  public TriggerResult onElement(SensorEvent element, long timestamp, GlobalWindow window, TriggerContext ctx) throws Exception {

    if (element.isEventEnd() == true) {
      log.info("Timer started with allowedLatenessMillis " + allowedLatenessMillis);
      ctx.registerEventTimeTimer(System.currentTimeMillis() + allowedLatenessMillis);
    }

    return TriggerResult.CONTINUE;
  }

  @Override
  public TriggerResult onEventTime(long time, GlobalWindow window, TriggerContext ctx) throws Exception {
    log.info("onEvenTime called at "+System.currentTimeMillis() );
    return TriggerResult.FIRE_AND_PURGE;
  }

  @Override
  public TriggerResult onProcessingTime(long time, GlobalWindow window, TriggerContext ctx) throws Exception {
    return TriggerResult.CONTINUE;
  }

  @Override
  public void clear(GlobalWindow window, TriggerContext ctx) throws Exception {}
}


问:
C# ErrorProvider Want to know if any are Active
I want to know if any ErrorProvider are active in my form.
being able to find this out might help reduce my code..

I did find this thing here Counting ErrorProvider

but incase someone knows a better way... so here goes.

Ok so basically I have a WinForm which has many TextBoxes
Now when user enters values I use Validating to perform validation and if it does not match Regex I set the ErrorProvider ON for that Control.. similarly if the user changes the value to a acceptable one I switch ErrorProvider OFF for that Control..

but when SAVE is clicked i have to do another check anyways incase the user did not listen to me and change the thing like he was supposed to and still clicked SAVE.. I dont want the thing crashing..

soo mm is there like a thing where I could say if ErrorProviders is not active then proceed with save else message box saying change it.

[ANOTHER QUESTION]

Umm When Validating it only Validates when the Control loses Focus... I kinda of want it to do validation when user stops typing.. I hope you get what I mean

Like Email Address(textbox) when user is typing his/her name in I [DON'T] want it to do validation yet, but when user has finished entering is waiting for ErrorProvider to disappear(But it doesn't coz it only does that when control loses focus) 2 odd seconds after typing can i make the validation take place?

答:
In my case, I didn't use a static class but an instance of error counter.

```<code>public class ErrorCounter
{
    private List<string> _propertiesError = new List<string>();
    private static ObjectIDGenerator _IDGenerator = new ObjectIDGenerator();

    public bool HasErrors
    {
        get => ErrorCount != 0;
    }

    public int ErrorCount
    {
        get => _propertiesError.Count;
    }

    /// <summary>
    /// Record object validation rule state.
    /// </summary>
    /// <param name="sender">"this" object reference must be passed into parameter each time SetError is called</param>
    /// <param name="message"></param>
    /// <param name="property"></param>
    /// <returns></returns>
    public string SetError(object sender, string property, string message)
    {
        string propertyUniqueID = GetPropertyUniqueID(sender, property);

        if (string.IsNullOrWhiteSpace(message))
        {
            if (_propertiesError.Exists(x => x == propertyUniqueID))
            {
                _propertiesError.Remove(propertyUniqueID);
            }
        }
        else
        {
            if (!_propertiesError.Exists(x => x == propertyUniqueID))
            {
                _propertiesError.Add(propertyUniqueID);
            }
        }

        return message;
    }

    private string GetPropertyUniqueID(object sender, string property)
    {
        bool dummyFirstTime;

        return property + "_" + _IDGenerator.GetId(sender, out dummyFirstTime);
    }
}
</code>```

Usage :
Declare in your main ViewModel

```<code>public class MainViewModel : ViewModelBase, IDataErrorInfo
...
private ErrorCounter _errorCounter = new ErrorCounter();
...
// Entry validation rules
public string Error => string.Empty;
public string this[string columnName]
{
    get
    {
        switch (columnName)
        {
            case nameof(myProperty_1):
                if (string.IsNullOrWhiteSpace(myProperty_1))
                    return _errorCounter.SetError(this, columnName, "Error 1");
                break;
            case nameof(myProperty_2):
                if (string.IsNullOrWhiteSpace(myProperty_2))
                    return _errorCounter.SetError(this, columnName, "Error 2");
                break;
            default:
                break;
        }

        return _errorCounter.SetError(this, columnName, string.Empty);
    }
}
</code>```

ObjectIDGenerator combined with the propertie name allows to count only once each properties.
If you need to use the same instance of _errorCounter in an object collection member of another class, pass it to the constructor of the other class.

and that's all :-)

Body:
In my case, I didn't use a static class but an instance of error counter.

public class ErrorCounter
{
    private List<string> _propertiesError = new List<string>();
    private static ObjectIDGenerator _IDGenerator = new ObjectIDGenerator();

    public bool HasErrors
    {
        get => ErrorCount != 0;
    }

    public int ErrorCount
    {
        get => _propertiesError.Count;
    }

    /// <summary>
    /// Record object validation rule state.
    /// </summary>
    /// <param name="sender">"this" object reference must be passed into parameter each time SetError is called</param>
    /// <param name="message"></param>
    /// <param name="property"></param>
    /// <returns></returns>
    public string SetError(object sender, string property, string message)
    {
        string propertyUniqueID = GetPropertyUniqueID(sender, property);

        if (string.IsNullOrWhiteSpace(message))
        {
            if (_propertiesError.Exists(x => x == propertyUniqueID))
            {
                _propertiesError.Remove(propertyUniqueID);
            }
        }
        else
        {
            if (!_propertiesError.Exists(x => x == propertyUniqueID))
            {
                _propertiesError.Add(propertyUniqueID);
            }
        }

        return message;
    }

    private string GetPropertyUniqueID(object sender, string property)
    {
        bool dummyFirstTime;

        return property + "_" + _IDGenerator.GetId(sender, out dummyFirstTime);
    }
}

Usage :
Declare in your main ViewModel

public class MainViewModel : ViewModelBase, IDataErrorInfo
...
private ErrorCounter _errorCounter = new ErrorCounter();
...
// Entry validation rules
public string Error => string.Empty;
public string this[string columnName]
{
    get
    {
        switch (columnName)
        {
            case nameof(myProperty_1):
                if (string.IsNullOrWhiteSpace(myProperty_1))
                    return _errorCounter.SetError(this, columnName, "Error 1");
                break;
            case nameof(myProperty_2):
                if (string.IsNullOrWhiteSpace(myProperty_2))
                    return _errorCounter.SetError(this, columnName, "Error 2");
                break;
            default:
                break;
        }

        return _errorCounter.SetError(this, columnName, string.Empty);
    }
}

ObjectIDGenerator combined with the propertie name allows to count only once each properties.
If you need to use the same instance of _errorCounter in an object collection member of another class, pass it to the constructor of the other class.

and that's all :-)


问:
data passing in navigation based application
i am working on a navigation based application
in the rootviewcontroller i hav a few UITextFields. There is a button on rootviewController, on clicking that button i am changing the view using pushviewcontroller. I want to use values entered in these UItextFields in mapview ie my 2nd view.

PLZ suggest me something.

答:
Before pushing assign the values to the second controller.
Say you have to send value of textfield1. Now

secondController.textfield1 = textfield1;

Then do your pushing of controller.


问:
How can I handle InvalidSignatureException while validating JWT in Spring Boot?
I am developing my own Spring Boot authentication server and resource server, and they work normally.
The authentication server provides RS256 JWS token and the resource server is able to verify the token.

I was so happy and I would like to know what would happen if I use a different RSA key pair to verify the JWS token. It came out:

```<code>org.springframework.security.jwt.crypto.sign.InvalidSignatureException: RSA Signature did not match content
    at org.springframework.security.jwt.crypto.sign.RsaVerifier.verify(RsaVerifier.java:55)
    at org.springframework.security.jwt.JwtImpl.verifySignature(JwtHelper.java:287)
    at org.springframework.security.oauth2.provider.token.store.jwk.JwkVerifyingJwtAccessTokenConverter.decode(JwkVerifyingJwtAccessTokenConverter.java:122)
    at org.springframework.security.oauth2.provider.token.store.JwtTokenStore.convertAccessToken(JwtTokenStore.java:88)
    at org.springframework.security.oauth2.provider.token.store.JwtTokenStore.readAccessToken(JwtTokenStore.java:80)
    at org.springframework.security.oauth2.provider.token.store.jwk.JwkTokenStore.readAccessToken(JwkTokenStore.java:212)
    at org.springframework.security.oauth2.provider.token.DefaultTokenServices.loadAuthentication(DefaultTokenServices.java:229)
    at org.springframework.security.oauth2.provider.token.DefaultTokenServices$$FastClassBySpringCGLIB$$5a1f25c.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:684)
    at org.springframework.security.oauth2.provider.token.DefaultTokenServices$$EnhancerBySpringCGLIB$$14463cf2.loadAuthentication(<generated>)
    at org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationManager.authenticate(OAuth2AuthenticationManager.java:83)
    at org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationProcessingFilter.doFilter(OAuth2AuthenticationProcessingFilter.java:150)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
...
</code>```

and the response is <500 Internal Server Error> with body:

```<code><!doctype html>
<html lang="en">

<head>
    <title>HTTP Status 500 – Internal Server Error</title>
    <style type="text/css">
        h1 {
            font-family: Tahoma, Arial, sans-serif;
            color: white;
            background-color: #525D76;
            font-size: 22px;
        }

        h2 {
            font-family: Tahoma, Arial, sans-serif;
            color: white;
            background-color: #525D76;
            font-size: 16px;
        }

        h3 {
            font-family: Tahoma, Arial, sans-serif;
            color: white;
            background-color: #525D76;
            font-size: 14px;
        }

        body {
            font-family: Tahoma, Arial, sans-serif;
            color: black;
            background-color: white;
        }

        b {
            font-family: Tahoma, Arial, sans-serif;
            color: white;
            background-color: #525D76;
        }

        p {
            font-family: Tahoma, Arial, sans-serif;
            background: white;
            color: black;
            font-size: 12px;
        }

        a {
            color: black;
        }

        a.name {
            color: black;
        }

        .line {
            height: 1px;
            background-color: #525D76;
            border: none;
        }
    </style>
</head>

<body>
    <h1>HTTP Status 500 – Internal Server Error</h1>
</body>

</html>
</code>```

I would like to handle this error into custom JSON error body, or at least look like the same as the normal AuthenticationException

e.g. <401 Unauthorized>

```<code>{
    "error": "invalid_token",
    "error_description": "Invalid JWT/JWS: RSA Signature did not match content"
}
</code>```

My Spring / Boot dependencies:

```<code>plugins {
    id 'org.springframework.boot' version '2.2.4.RELEASE'
    ...
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
    compile("org.springframework.boot:spring-boot-starter-security")
    compile("org.springframework.security.oauth.boot:spring-security-oauth2-autoconfigure:2.1.0.RELEASE")
    compile('org.springframework.boot:spring-boot-starter-web')
    testCompile('org.springframework.boot:spring-boot-starter-web')
    compile("org.springframework.cloud:spring-cloud-starter-sleuth")
    compile("org.springframework.security:spring-security-jwt")
    ...
}
</code>```

What I have tried:

Handle in @ControllerAdvice (From other answers in Stackoverflow, should be the request still haven't passed into MVC, so useless)

Register custom AuthenticationEntryPoint through ResourceServerConfigurerAdapter.configure(HttpSecurity) (Don't know why it can't help, but tried)

Edit

Tried the suggestion from @Jazzepi, Implementing HandlerExceptionResolver or extending AbstractHandlerExceptionResolver with method:

```<code>@Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        if (ex instanceof InvalidSignatureException) {
            ModelAndView mav = new ModelAndView(new MappingJackson2JsonView());
            mav.addObject("key", "value");
            return mav;
        }
        return null;
    }
</code>```

And then tried to register through WebMvcConfigurator.extendHandlerExceptionResolvers(List<HandleExceptionResolvers>) (It can't help, still output the same response as above)

Edit:

How I build and decode the token?

Follows exactly this tutorial: https://www.baeldung.com/spring-security-oauth2-jws-jwk

How to re-play this situation?

Start both auth and resource server.

Get a token from auth server and use the token in the resource server once to make the resource server cache the public key in local

Change the keystore file in the auth server and restart

Get a new token from the restarted auth server

Use the new token in the resource server

答:
Spring Boot comes with a couple of classes that map exceptions to methods to run when those exceptions are thrown.

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.html#addDefaultHandlerExceptionResolvers-java.util.List-

Registers a HandlerExceptionResolverComposite with this chain of exception resolvers:

ExceptionHandlerExceptionResolver for handling exceptions through
  ExceptionHandler methods.

ResponseStatusExceptionResolver for exceptions annotated with
  ResponseStatus.

DefaultHandlerExceptionResolver for resolving known Spring exception
  types

I think you want to create your own exception handler. It will give you a chance to run whatever code you want in response to your specific exception. Probably want to implement this interface. Checkout the docs though because Spring provides a rich variety of abstract classes that you can extend that will make your life easier instead of writing from the interface from scratch.

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/HandlerExceptionResolver.html

Finally you need to register your handler with Spring. This looks like a good way to do it.

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/config/annotation/WebMvcConfigurer.html#extendHandlerExceptionResolvers-java.util.List-

Good luck!

To troubleshoot I would set a breakpoint in
org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolver.java on the doResolveException and see if it's catching and handling your exception there (I think it is).

There's an order amongst these resolvers in Spring. You probably want to give yours the highest order so that it gets a chance to handle the exception before the default one.

@Order(Ordered.HIGHEST_PRECEDENCE)

You also probably want your handler to use response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ex.getMessage()); sendError instead of mucking around with the ModelAndView() stuff. That's for telling spring which webpage to redirect to. Look at the default handler individual methods to get an idea about different ways you can handle errors.

Body:
I am developing my own Spring Boot authentication server and resource server, and they work normally.
The authentication server provides RS256 JWS token and the resource server is able to verify the token.

I was so happy and I would like to know what would happen if I use a different RSA key pair to verify the JWS token. It came out:

org.springframework.security.jwt.crypto.sign.InvalidSignatureException: RSA Signature did not match content
    at org.springframework.security.jwt.crypto.sign.RsaVerifier.verify(RsaVerifier.java:55)
    at org.springframework.security.jwt.JwtImpl.verifySignature(JwtHelper.java:287)
    at org.springframework.security.oauth2.provider.token.store.jwk.JwkVerifyingJwtAccessTokenConverter.decode(JwkVerifyingJwtAccessTokenConverter.java:122)
    at org.springframework.security.oauth2.provider.token.store.JwtTokenStore.convertAccessToken(JwtTokenStore.java:88)
    at org.springframework.security.oauth2.provider.token.store.JwtTokenStore.readAccessToken(JwtTokenStore.java:80)
    at org.springframework.security.oauth2.provider.token.store.jwk.JwkTokenStore.readAccessToken(JwkTokenStore.java:212)
    at org.springframework.security.oauth2.provider.token.DefaultTokenServices.loadAuthentication(DefaultTokenServices.java:229)
    at org.springframework.security.oauth2.provider.token.DefaultTokenServices$$FastClassBySpringCGLIB$$5a1f25c.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:684)
    at org.springframework.security.oauth2.provider.token.DefaultTokenServices$$EnhancerBySpringCGLIB$$14463cf2.loadAuthentication(<generated>)
    at org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationManager.authenticate(OAuth2AuthenticationManager.java:83)
    at org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationProcessingFilter.doFilter(OAuth2AuthenticationProcessingFilter.java:150)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
...

and the response is <500 Internal Server Error> with body:

<!doctype html>

    <title>HTTP Status 500 – Internal Server Error</title>

HTTP Status 500 – Internal Server Error

I would like to handle this error into custom JSON error body, or at least look like the same as the normal AuthenticationException

e.g. <401 Unauthorized>

{
    "error": "invalid_token",
    "error_description": "Invalid JWT/JWS: RSA Signature did not match content"
}

My Spring / Boot dependencies:

plugins {
    id 'org.springframework.boot' version '2.2.4.RELEASE'
    ...
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
    compile("org.springframework.boot:spring-boot-starter-security")
    compile("org.springframework.security.oauth.boot:spring-security-oauth2-autoconfigure:2.1.0.RELEASE")
    compile('org.springframework.boot:spring-boot-starter-web')
    testCompile('org.springframework.boot:spring-boot-starter-web')
    compile("org.springframework.cloud:spring-cloud-starter-sleuth")
    compile("org.springframework.security:spring-security-jwt")
    ...
}

What I have tried:

Handle in @ControllerAdvice (From other answers in Stackoverflow, should be the request still haven't passed into MVC, so useless)

Register custom AuthenticationEntryPoint through ResourceServerConfigurerAdapter.configure(HttpSecurity) (Don't know why it can't help, but tried)

Edit

Tried the suggestion from @Jazzepi, Implementing HandlerExceptionResolver or extending AbstractHandlerExceptionResolver with method:

@Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        if (ex instanceof InvalidSignatureException) {
            ModelAndView mav = new ModelAndView(new MappingJackson2JsonView());
            mav.addObject("key", "value");
            return mav;
        }
        return null;
    }

And then tried to register through WebMvcConfigurator.extendHandlerExceptionResolvers(List<HandleExceptionResolvers>) (It can't help, still output the same response as above)

Edit:

How I build and decode the token?

Follows exactly this tutorial: https://www.baeldung.com/spring-security-oauth2-jws-jwk

How to re-play this situation?

Start both auth and resource server.

Get a token from auth server and use the token in the resource server once to make the resource server cache the public key in local

Change the keystore file in the auth server and restart

Get a new token from the restarted auth server

Use the new token in the resource server


问:
AccountsJs TypeORM
I have a bit of a problem with account and TypeORM

I've configured everything and there are no errors, but I can't seem to create users, I run the mutation but it doesn't fail and it runs in like 2ms, but when I go to the DB (MongoDB Atlas) I don't see the user collection and there are no users created.

Here is my config:

import { AccountsModule } from "@accounts/graphql-api";
import { AccountsServer } from "@accounts/server";
import { AccountsPassword } from "@accounts/password";
import AccountsTypeorm from "@accounts/typeorm";
import { Connection } from "typeorm";

export const setUpAccounts = async (connection: Connection) => {
  const db = new AccountsTypeorm({ connection, cache: 1000 });
  const password = new AccountsPassword({
    twoFactor: {
      appName: "Events"
    }
  });

  const accountsServer = new AccountsServer(
    {
      db,
      tokenSecret: "terrible secret",
      siteUrl: "http://localhost:3000"
    },
    {
      password
    }
  );

  const accounts = AccountsModule.forRoot({
    accountsServer,
    headerName: "x-events-token"
  });

  return accounts;
};

```

<p>index file:</p>

<pre class="lang-js prettyprint-override"><code>require("dotenv").config();
import "reflect-metadata";

import express, { Application } from "express";
import { ApolloServer, makeExecutableSchema } from "apollo-server-express";
import { mergeTypeDefs } from "graphql-toolkit";
import { buildTypeDefsAndResolvers } from "type-graphql";

import { resolvers } from "./graphql";
import { connectDatabase } from "./db";
import { setUpAccounts } from "./lib/accounts";
import { CategoryResolver } from "./modules/Category/CategoryResolver";

const port = process.env.PORT;

const corsOptions = {
  origin: "http://localhost:3000",
  credentials: true
};
const mount = async (app: Application): Promise<void> => {
  const connection = await connectDatabase();

  const accounts = await setUpAccounts(connection);

  const schema = await buildTypeDefsAndResolvers({
    resolvers: [resolvers, CategoryResolver, accounts.resolvers]
  });

  const server = new ApolloServer({
    schema: makeExecutableSchema({
      typeDefs: mergeTypeDefs([accounts.typeDefs, schema.typeDefs]),
      resolvers: schema.resolvers,
      schemaDirectives: {
        ...accounts.schemaDirectives
      }
    }),
    context: accounts.context,
    playground: true
  });

  server.applyMiddleware({
    app: app,
    path: "/graphql",
    cors: corsOptions
  });

  app.listen(port, () => {
    console.log(`[ app ]: running on http://localhost:${port}`);
  });
};

mount(express());

</code>```

connectDatabase:

import { createConnection, Connection } from "typeorm";
import path from "path";

const user = process.env.DB_USER;
const userPassword = process.env.DB_USER_PASSWORD;
const cluster = process.env.DB_CLUSTER;

const uri = `mongodb+srv://${user}:${userPassword}@${cluster}.mongodb.net/`;

export const connectDatabase = async (): Promise<Connection> => {
  const connection = await createConnection({
    url: uri,
    type: "mongodb",
    database: "main",
    w: "majority",
    useNewUrlParser: true,
    useUnifiedTopology: true,
    logger: "advanced-console",
    logging: "all",
    entities: [
      path.join(__dirname, "/../entities/**/*.*"),
      ...require("@accounts/typeorm").entities
    ]
  });

  console.log(connection.options.entities);

  return connection;
};

```

答:
Nvm I fixed it by moving accounts.resolvers from buildTypeDefsAndResolvers to makeExecutableSchema:

schema: makeExecutableSchema({
    typeDefs: mergeTypeDefs([accounts.typeDefs, schema.typeDefs]),
    resolvers: mergeResolvers([schema.resolvers, accounts.resolvers]),
    schemaDirectives: {
      ...accounts.schemaDirectives
    }
})
```

Works now :)

Title: AccountsJs TypeORM

Body:
I have a bit of a problem with account and TypeORM

I've configured everything and there are no errors, but I can't seem to create users, I run the mutation but it doesn't fail and it runs in like 2ms, but when I go to the DB (MongoDB Atlas) I don't see the user collection and there are no users created.

Here is my config:

import { AccountsModule } from "@accounts/graphql-api";
import { AccountsServer } from "@accounts/server";
import { AccountsPassword } from "@accounts/password";
import AccountsTypeorm from "@accounts/typeorm";
import { Connection } from "typeorm";

export const setUpAccounts = async (connection: Connection) => {
  const db = new AccountsTypeorm({ connection, cache: 1000 });
  const password = new AccountsPassword({
    twoFactor: {
      appName: "Events"
    }
  });

  const accountsServer = new AccountsServer(
    {
      db,
      tokenSecret: "terrible secret",
      siteUrl: "http://localhost:3000"
    },
    {
      password
    }
  );

  const accounts = AccountsModule.forRoot({
    accountsServer,
    headerName: "x-events-token"
  });

  return accounts;
};

index file:

require("dotenv").config();
import "reflect-metadata";

import express, { Application } from "express";
import { ApolloServer, makeExecutableSchema } from "apollo-server-express";
import { mergeTypeDefs } from "graphql-toolkit";
import { buildTypeDefsAndResolvers } from "type-graphql";

import { resolvers } from "./graphql";
import { connectDatabase } from "./db";
import { setUpAccounts } from "./lib/accounts";
import { CategoryResolver } from "./modules/Category/CategoryResolver";

const port = process.env.PORT;

const corsOptions = {
  origin: "http://localhost:3000",
  credentials: true
};
const mount = async (app: Application): Promise<void> => {
  const connection = await connectDatabase();

  const accounts = await setUpAccounts(connection);

  const schema = await buildTypeDefsAndResolvers({
    resolvers: [resolvers, CategoryResolver, accounts.resolvers]
  });

  const server = new ApolloServer({
    schema: makeExecutableSchema({
      typeDefs: mergeTypeDefs([accounts.typeDefs, schema.typeDefs]),
      resolvers: schema.resolvers,
      schemaDirectives: {
        ...accounts.schemaDirectives
      }
    }),
    context: accounts.context,
    playground: true
  });

  server.applyMiddleware({
    app: app,
    path: "/graphql",
    cors: corsOptions
  });

  app.listen(port, () => {
    console.log(`[ app ]: running on http://localhost:${port}`);
  });
};

mount(express());

connectDatabase:

import { createConnection, Connection } from "typeorm";
import path from "path";

const user = process.env.DB_USER;
const userPassword = process.env.DB_USER_PASSWORD;
const cluster = process.env.DB_CLUSTER;

const uri = `mongodb+srv://${user}:${userPassword}@${cluster}.mongodb.net/`;

export const connectDatabase = async (): Promise<Connection> => {
  const connection = await createConnection({
    url: uri,
    type: "mongodb",
    database: "main",
    w: "majority",
    useNewUrlParser: true,
    useUnifiedTopology: true,
    logger: "advanced-console",
    logging: "all",
    entities: [
      path.join(__dirname, "/../entities/**/*.*"),
      ...require("@accounts/typeorm").entities
    ]
  });

  console.log(connection.options.entities);

  return connection;
};

Body:
Nvm I fixed it by moving accounts.resolvers from buildTypeDefsAndResolvers to makeExecutableSchema:

schema: makeExecutableSchema({
    typeDefs: mergeTypeDefs([accounts.typeDefs, schema.typeDefs]),
    resolvers: mergeResolvers([schema.resolvers, accounts.resolvers]),
    schemaDirectives: {
      ...accounts.schemaDirectives
    }
})

Works now :)


问:
Refactor try and Except codeblocks in Python
Currently my app is working with several external APIs, and those functions can raise some kind of error, and I would like to be aware of all kind of situations and don't crash my app. In the following piece of code you can see my function, using slack api (it's just an example, not real example), and you can see how I'm creating a space giving the name and the emails. That function can returns several errors, like APIerror, TypeError... etc etc

Also you can see, all the functions in that file, can raise some errors so it is using try and except and try to don't break our app. In the except part, I don't want to do anything special, just return the error or I don't know, raise an custom error. In the future would be cool if I can enqueue that task if something goes wrong.

slack_client.py

def create_space():
  try:
    slack_api.create.space(spaceName='space name', email=emails)
  except (...):
    ...
```

<pre class="lang-py prettyprint-override"><code>def delete_space():
  try:
    slack_api.delete.space(spaceId=space_id)
  except (...):
    ...

</code>```

I would like to refactor the try and except part, because I don't want to copy and paste try and except during all my slack_client class, but in case the connection is down, or some parameter is wron, the "Slack" API will raise me an error.

I thought about create a class, to run each function inside try and catch, but it's a bit weird and I will loose the arguments part like "spaceName='space name'", which I think it's really cool and clear.

def execute_func(func, *args):
  try:
     func(args)
  except (..):
     ...

```

So would like some help for this kind of situation, because handle external API is not always really easy... (at the moment working with 2 external APIs)

答:
In a previous life I used VAX/VMS and still remember great pleasure at discoverting LIB$SIG_TO_RET which converted a signal to a return value. You can do exactly the same in not many lines of Python, along the lines you indicated

```<code>def execute_func(func, *args, **kwargs):
  try:
     result = func( *args, **kwargs)
     return (True, result)

  except Exception as exc:
       return ( False, exc)
</code>```

Usage

```<code>result = execute_func( func, a1, a2, ... k1=v1, k2=v2, ...)
if  result[0]:
    func_result = result[1] # if it's  useful
    ...
else:
     # it went wrong, deal with the exception
     caught_exception = result[1]
     print( caught_exception)
</code>```

Body:
Currently my app is working with several external APIs, and those functions can raise some kind of error, and I would like to be aware of all kind of situations and don't crash my app. In the following piece of code you can see my function, using slack api (it's just an example, not real example), and you can see how I'm creating a space giving the name and the emails. That function can returns several errors, like APIerror, TypeError... etc etc

Also you can see, all the functions in that file, can raise some errors so it is using try and except and try to don't break our app. In the except part, I don't want to do anything special, just return the error or I don't know, raise an custom error. In the future would be cool if I can enqueue that task if something goes wrong.

slack_client.py

def create_space():
  try:
    slack_api.create.space(spaceName='space name', email=emails)
  except (...):
    ...

def delete_space():
  try:
    slack_api.delete.space(spaceId=space_id)
  except (...):
    ...

I would like to refactor the try and except part, because I don't want to copy and paste try and except during all my slack_client class, but in case the connection is down, or some parameter is wron, the "Slack" API will raise me an error.

I thought about create a class, to run each function inside try and catch, but it's a bit weird and I will loose the arguments part like "spaceName='space name'", which I think it's really cool and clear.

def execute_func(func, *args):
  try:
     func(args)
  except (..):
     ...

So would like some help for this kind of situation, because handle external API is not always really easy... (at the moment working with 2 external APIs)

Body:
In a previous life I used VAX/VMS and still remember great pleasure at discoverting LIB$SIG_TO_RET which converted a signal to a return value. You can do exactly the same in not many lines of Python, along the lines you indicated

def execute_func(func, *args, **kwargs):
  try:
     result = func( *args, **kwargs)
     return (True, result)

  except Exception as exc:
       return ( False, exc)

Usage

result = execute_func( func, a1, a2, ... k1=v1, k2=v2, ...)
if  result[0]:
    func_result = result[1] # if it's  useful
    ...
else:
     # it went wrong, deal with the exception
     caught_exception = result[1]
     print( caught_exception)


问:
Best practice of handling hbase connection and table in java?
I'm using hbase-client 1.2.3, I saw there is a comment in Connection.getTable() method:

Retrieve a Table implementation for accessing a table.

The returned Table is not thread safe, a new instance should be
  created for each using thread.

This is a lightweight operation, pooling or caching of the
  returned Table

is neither required nor desired.

So I start wondering what's the best practice to handle connection and table?

For example, I have a main class, will start several threads, let's call A,B,C...

Now I call "Connection connection = ConnectionFactory.createConnection();" in the main method, and pass the connection to each thread as a param to each thread. And then init Table class in each thread.

I want to know is this the best way? Will it cause some threadsafe or efficiency or any other problems?

答:
According to the documentation:

Connection creation is a heavy-weight operation. Connection implementations are thread-safe, so that the client can create a connection once, and share it with different threads. Table and Admin instances, on the other hand, are light-weight and are not thread-safe. Typically, a single connection per client application is instantiated and every thread will obtain its own Table instance. Caching or pooling of Table and Admin is not recommended.

So I would suggest that you initialize a connection only once and a new table every time it's required.

Sample Code:

//initialize connection
Configuration hBaseConfig = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(hBaseConfig);

//initialize table
try (Table table = connection.getTable(TableName.valueOf("table-name"))) {
    //use table
}
```


问:
Linq replace values until first X value
I have a list:

```<code>var l = new List<int?>{0,0,0,0,0,0,4,5,6,3,0,0,2,3};
</code>```

I want to replace all starting "n" zeros values with null until find first value != 0 and break.

I tried to use Select:

```<code>var l2 = l.Select((x, i) => i == 0 || l[i - 1] == 0 ? (int?)null : x)
</code>```

Actual Result:

```<code>{null,null,null,null,null,null,null,5,6,3,0,null,null,3}
</code>```

Expected Result:

```<code>{null,null,null,null,null,null,4,5,6,3,0,0,2,3}
</code>```

MRE:

```<code>public static void Main()
{
    var l = new List<int?>{0,0,0,0,0,0,4,5,6,3,0,0,2,3};
    var l2 = l.Select((x, i) => i == 0 || l[i - 1] == 0 ? (int?)null : x);
    Console.WriteLine(string.Join(",", l2));
}
</code>```

Note that this question is a rework of https://stackoverflow.com/q/60304342/1248177. I think that the question had some legs so here we go.

答:
I prefer this solution, but here is the another one:

Count how many element match your condition.

Prepend a collection of substituteto your source after skipping the right number of elements.

Code:

```<code>public static IEnumerable<T> ReplaceWhile<T>(this IEnumerable<T> source, Func<T, bool> predicate, T substitute)
{
    var zerosLength = source.TakeWhile(predicate).Count();
    return Enumerable.Repeat(substitute, zerosLength).Concat(source.Skip(zerosLength));
}
</code>```

Try it Online!

Title: Linq replace values until first X value

Body:
I have a list:

var l = new List<int?>{0,0,0,0,0,0,4,5,6,3,0,0,2,3};

I want to replace all starting "n" zeros values with null until find first value != 0 and break.

I tried to use Select:

var l2 = l.Select((x, i) => i == 0 || l[i - 1] == 0 ? (int?)null : x)

Actual Result:

{null,null,null,null,null,null,null,5,6,3,0,null,null,3}

Expected Result:

{null,null,null,null,null,null,4,5,6,3,0,0,2,3}

MRE:

public static void Main()
{
    var l = new List<int?>{0,0,0,0,0,0,4,5,6,3,0,0,2,3};
    var l2 = l.Select((x, i) => i == 0 || l[i - 1] == 0 ? (int?)null : x);
    Console.WriteLine(string.Join(",", l2));
}

Note that this question is a rework of https://stackoverflow.com/q/60304342/1248177. I think that the question had some legs so here we go.

Body:
I prefer this solution, but here is the another one:

Count how many element match your condition.

Prepend a collection of substituteto your source after skipping the right number of elements.

Code:

public static IEnumerable<T> ReplaceWhile<T>(this IEnumerable<T> source, Func<T, bool> predicate, T substitute)
{
    var zerosLength = source.TakeWhile(predicate).Count();
    return Enumerable.Repeat(substitute, zerosLength).Concat(source.Skip(zerosLength));
}

Try it Online!


问:
My neural network has a very bad accuracy
```<code>
import tensorflow as tf
import pandas as pd
import numpy as np
import matplotlib as plt
from sklearn.model_selection import train_test_split
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
import functools

COLUMNS = ["Alter", "Gender", "BMI", "Fever", "Nausea", "Fatigue",
                  "WBC", "RBC", "HGB", "Plat", "AST1", "ALT1", "ALT4", "ALT12", "ALT24", "ALT36", "ALT48", "ALT24w",
                  "RNABase", "RNA4", "Baseline", "Endstage"]

feature_name = COLUMNS[:-1]
LABEL_NAME = 'Endstage'

batch_size = 32

def get_dataset(file_path, **kwargs):
    train_dataset = tf.data.experimental.make_csv_dataset(
        file_path,
        batch_size = batch_size,
        label_name = LABEL_NAME,
        ignore_errors=True,
        num_epochs=5,
        **kwargs
    )
    return train_dataset

train_data = get_dataset("HCVnew.csv")

def show_batch(dataset):
  for batch, label in dataset.take(1):
    for key, value in batch.items():
      print("{:20s}: {}".format(key,value.numpy()))

SELECT_COLUMNS = ["Alter", "Gender", "BMI", "Fever", "Nausea", "Fatigue",
                  "WBC", "RBC", "HGB", "Plat", "AST1", "ALT1", "ALT4", "ALT12", "ALT24", "ALT36", "ALT48", "ALT24w",
                  "RNABase", "RNA4", "Baseline", "Endstage"]

DEFAULTS = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

uni_data = get_dataset("HCVnew.csv", select_columns = SELECT_COLUMNS, column_defaults = DEFAULTS)

def pack(features, label):
  return tf.stack(list(features.values()), axis=-1), label

packed_dataset = uni_data.map(pack)
"""
for features, labels in packed_dataset.take(1):
  print(features.numpy())
  print()
  print(labels.numpy())
"""

class PackNumericFeatures(object):
  def __init__(self, names):
    self.names = names

  def __call__(self, features, labels):
    numeric_features = [features.pop(name) for name in self.names]
    numeric_features = [tf.cast(feat, tf.float32) for feat in numeric_features]
    numeric_features = tf.stack(numeric_features, axis=-1)
    features['numeric'] = numeric_features

    return features, labels

NUMERIC_FEATURES = ["Alter", "Gender", "BMI", "Fever", "Nausea", "Fatigue",
                  "WBC", "RBC", "HGB", "Plat", "AST1", "ALT1", "ALT4", "ALT12", "ALT24", "ALT36", "ALT48", "ALT24w",
                  "RNABase", "RNA4", "Baseline"]

packed_train_data = train_data.map(
    PackNumericFeatures(NUMERIC_FEATURES))

#show_batch(packed_train_data)

desc = pd.read_csv("HCVnew.csv")[NUMERIC_FEATURES].describe()
desc

MEAN = np.array(desc.T['mean'])
STD = np.array(desc.T['std'])

def normalize_numeric_data(data, mean, std):
  # Center the data
  return (data-mean)/std

normalizer = functools.partial(normalize_numeric_data, mean=MEAN, std=STD)

numeric_column = tf.feature_column.numeric_column('numeric', normalizer_fn=normalizer, shape=[len(NUMERIC_FEATURES)])
numeric_columns = [numeric_column]

numeric_layer = tf.keras.layers.DenseFeatures(numeric_columns)

#preprocessing_layer = tf.keras.layers.DenseFeatures(numeric_columns)

model = tf.keras.Sequential([
  numeric_layer,
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dense(1),
])

model.compile(
    loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
    optimizer='adam',
    metrics=['accuracy'])

train_data = packed_train_data.shuffle(500)

model.fit(train_data, epochs=20)

</code>```

My neural network has an accuracy of 25%, what is very bad. My training data contains 1200 samples but just after one or two epochs the accuracy remains 25% and I have tried to change batch size and amount of epochs but nothing works. The number of labels is 4 (1, 2, 3, 4).
If someone has an idea what I could improve then please tell me.
Thanks a lot for your help!

答:
The number of labels is 4 (1, 2, 3, 4)

If you have 4 labels, then this is not a binary problem and therefore you need to adapt your network to a multi-label classification :

One hot encode your labels

Use a last Dense layer with NB_CLASS neurones (4 in your case) and a Softmax activation

Use categorical_crossentropy as loss

If you use a Dense layer like that :

```<code>tf.keras.layers.Dense(1)
</code>```

It will have the default parameters, and in this case have a linear activation which is not what you want in a binary OR multi-label classification.

Body:
import tensorflow as tf
import pandas as pd
import numpy as np
import matplotlib as plt
from sklearn.model_selection import train_test_split
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
import functools

COLUMNS = ["Alter", "Gender", "BMI", "Fever", "Nausea", "Fatigue",
                  "WBC", "RBC", "HGB", "Plat", "AST1", "ALT1", "ALT4", "ALT12", "ALT24", "ALT36", "ALT48", "ALT24w",
                  "RNABase", "RNA4", "Baseline", "Endstage"]

feature_name = COLUMNS[:-1]
LABEL_NAME = 'Endstage'

batch_size = 32

def get_dataset(file_path, **kwargs):
    train_dataset = tf.data.experimental.make_csv_dataset(
        file_path,
        batch_size = batch_size,
        label_name = LABEL_NAME,
        ignore_errors=True,
        num_epochs=5,
        **kwargs
    )
    return train_dataset

train_data = get_dataset("HCVnew.csv")

def show_batch(dataset):
  for batch, label in dataset.take(1):
    for key, value in batch.items():
      print("{:20s}: {}".format(key,value.numpy()))

SELECT_COLUMNS = ["Alter", "Gender", "BMI", "Fever", "Nausea", "Fatigue",
                  "WBC", "RBC", "HGB", "Plat", "AST1", "ALT1", "ALT4", "ALT12", "ALT24", "ALT36", "ALT48", "ALT24w",
                  "RNABase", "RNA4", "Baseline", "Endstage"]

DEFAULTS = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

uni_data = get_dataset("HCVnew.csv", select_columns = SELECT_COLUMNS, column_defaults = DEFAULTS)

def pack(features, label):
  return tf.stack(list(features.values()), axis=-1), label

packed_dataset = uni_data.map(pack)
"""
for features, labels in packed_dataset.take(1):
  print(features.numpy())
  print()
  print(labels.numpy())
"""

class PackNumericFeatures(object):
  def __init__(self, names):
    self.names = names

  def __call__(self, features, labels):
    numeric_features = [features.pop(name) for name in self.names]
    numeric_features = [tf.cast(feat, tf.float32) for feat in numeric_features]
    numeric_features = tf.stack(numeric_features, axis=-1)
    features['numeric'] = numeric_features

    return features, labels

NUMERIC_FEATURES = ["Alter", "Gender", "BMI", "Fever", "Nausea", "Fatigue",
                  "WBC", "RBC", "HGB", "Plat", "AST1", "ALT1", "ALT4", "ALT12", "ALT24", "ALT36", "ALT48", "ALT24w",
                  "RNABase", "RNA4", "Baseline"]

packed_train_data = train_data.map(
    PackNumericFeatures(NUMERIC_FEATURES))

#show_batch(packed_train_data)

desc = pd.read_csv("HCVnew.csv")[NUMERIC_FEATURES].describe()
desc

MEAN = np.array(desc.T['mean'])
STD = np.array(desc.T['std'])

def normalize_numeric_data(data, mean, std):
  # Center the data
  return (data-mean)/std

normalizer = functools.partial(normalize_numeric_data, mean=MEAN, std=STD)

numeric_column = tf.feature_column.numeric_column('numeric', normalizer_fn=normalizer, shape=[len(NUMERIC_FEATURES)])
numeric_columns = [numeric_column]

numeric_layer = tf.keras.layers.DenseFeatures(numeric_columns)

#preprocessing_layer = tf.keras.layers.DenseFeatures(numeric_columns)

model = tf.keras.Sequential([
  numeric_layer,
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dense(1),
])

model.compile(
    loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
    optimizer='adam',
    metrics=['accuracy'])

train_data = packed_train_data.shuffle(500)

model.fit(train_data, epochs=20)

My neural network has an accuracy of 25%, what is very bad. My training data contains 1200 samples but just after one or two epochs the accuracy remains 25% and I have tried to change batch size and amount of epochs but nothing works. The number of labels is 4 (1, 2, 3, 4).
If someone has an idea what I could improve then please tell me.
Thanks a lot for your help!

Body:
The number of labels is 4 (1, 2, 3, 4)

If you have 4 labels, then this is not a binary problem and therefore you need to adapt your network to a multi-label classification :

One hot encode your labels

Use a last Dense layer with NB_CLASS neurones (4 in your case) and a Softmax activation

Use categorical_crossentropy as loss

If you use a Dense layer like that :

tf.keras.layers.Dense(1)

It will have the default parameters, and in this case have a linear activation which is not what you want in a binary OR multi-label classification.


问:
@RefreshScope can't get along with @Bean
I have a controller which need refresh config from config server so I add @RefreshScope on it. Meanwhile this controller need to calling a backend API so that I defined the restTemplate Bean. But once I start this application, exception occur. Can anyone tell me why these two annotation make circulate reference?</p>

```<code>Error: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'scopedTarget.frontEndApplication':
Unsatisfied dependency expressed through field 'restTemplate'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'restTemplate': Requested bean is currently in creation: Is there an unresolvable circular reference?
</code>```

```<code>@SpringBootApplication
@RestController
@EnableDiscoveryClient
@RefreshScope
public class FrontEndApplication {
    @Value("${msg:Hello default}")
    private String message;

    public static void main(String[] args) {
        SpringApplication.run(FrontEndApplication.class, args);
    }

    @Bean
    RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @Autowired
    RestTemplate restTemplate;

}
</code>```

答:
First, don't put @RefreshScope on the controller. Typically you'd want to do it in a class that stores state. If it's a configuration property it's better to use a @ConfigurationProperty annotation on a POJO and call @EnableConfigurationProperties.

Also your main class does everything, can you please just separate it into separate classes and try again? It's not a good idea to have your main class also be a controller, repository and a service at the same time.

Body:
I have a controller which need refresh config from config server so I add @RefreshScope on it. Meanwhile this controller need to calling a backend API so that I defined the restTemplate Bean. But once I start this application, exception occur. Can anyone tell me why these two annotation make circulate reference?

Error: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'scopedTarget.frontEndApplication':
Unsatisfied dependency expressed through field 'restTemplate'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'restTemplate': Requested bean is currently in creation: Is there an unresolvable circular reference?

@SpringBootApplication
@RestController
@EnableDiscoveryClient
@RefreshScope
public class FrontEndApplication {
    @Value("${msg:Hello default}")
    private String message;

    public static void main(String[] args) {
        SpringApplication.run(FrontEndApplication.class, args);
    }

    @Bean
    RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @Autowired
    RestTemplate restTemplate;

}


问:
Easiest way to copy a table from one database to another?
What is the best method to copy the data from a table in one database to a table in another database when the databases are under different users?

I know that I can use

```<code>INSERT INTO database2.table2 SELECT * from database1.table1
</code>```

But here the problem is that both database1 and database2 are under different MySQL users. So user1 can access database1 only and user2 can access database2 only. Any idea?

答: create table destination_customer like sakila.customer(Database_name.tablename), this will only copy the structure of the source table, for data also to get copied with the structure do this create table destination_customer as select * from sakila.customer

Body:
What is the best method to copy the data from a table in one database to a table in another database when the databases are under different users?

I know that I can use

INSERT INTO database2.table2 SELECT * from database1.table1

But here the problem is that both database1 and database2 are under different MySQL users. So user1 can access database1 only and user2 can access database2 only. Any idea?


问:
Apache HTTP Client Fluent
Hi I want to use Apache HTTP Client Fluent to create a request to download a file. I need to add Autorization Basic Auth to the request to pass in a username and password which I can't find a good example of how to do.

I can see a addHeader method but can't find good examples of how to construct it. Thanks!

So far the code i have is:

```<code>String auth = username + ":"+ token
byte[] encodedBytes = Base64.encodeBase64(auth.getBytes());
String encodedAuth = new String(encodedBytes)
URL downloadFileURL = new URL (urlbuild)

Executor executor = Executor.newInstance();
executor.execute(Request.Get(downloadFileURL.toURI())
 .setHeader(HttpHeaders.AUTHORIZATION, "Basic " + encodedAuth )
 .connectTimeout(1000))
  .saveContent(new File(mobileAppPath + System.getProperty("file.separator") + mobileApp.name));
</code>```

答:
the problem was due to Java trying to validate the SSL certificate i used Java code below to trust all hosts before executing the request.
https://www.javatips.net/api/java.security.cert.x509certificate</p>

Title: Apache HTTP Client Fluent

Body:
Hi I want to use Apache HTTP Client Fluent to create a request to download a file. I need to add Autorization Basic Auth to the request to pass in a username and password which I can't find a good example of how to do.

I can see a addHeader method but can't find good examples of how to construct it. Thanks!

So far the code i have is:

String auth = username + ":"+ token
byte[] encodedBytes = Base64.encodeBase64(auth.getBytes());
String encodedAuth = new String(encodedBytes)
URL downloadFileURL = new URL (urlbuild)

Executor executor = Executor.newInstance();
executor.execute(Request.Get(downloadFileURL.toURI())
 .setHeader(HttpHeaders.AUTHORIZATION, "Basic " + encodedAuth )
 .connectTimeout(1000))
  .saveContent(new File(mobileAppPath + System.getProperty("file.separator") + mobileApp.name));


问:
How to create several buffers around points of a simple feature in a loop?
I have sample data representing 2 points (sf). I want to create buffers with the width 2, 1 and 0.5. My approach is the below. Is there any way to do this in more efficient way? How can I wrap this process up in a loop?</p>

```<code>library(sf)

g = st_sfc(c(st_point(1:2),st_point(3:2)))

g.2 <- st_buffer(g,2)
g.1 <- st_buffer(g,1)
g.05 <- st_buffer(g,0.5)

plot(st_geometry(g.2))
plot(st_geometry(g.1), add=TRUE)
plot(st_geometry(g.05), add=TRUE)
plot(st_geometry(g), add=TRUE)
</code>```

答:
Sticking to your request (using a loop) see here an example

```<code>library(sf)
g = st_sfc(c(st_point(1:2),st_point(3:2)))
#Buffers
buffers=c(2,1,0.5)
#Initialise result
result=g
#Loop
for (i in buffers){
  #Appending buffered points
  result=c(result,st_buffer(g,i))
}
#Remove initial element
result=result[-1]
plot(result, axes=TRUE)
</code>```

Body:
I have sample data representing 2 points (sf). I want to create buffers with the width 2, 1 and 0.5. My approach is the below. Is there any way to do this in more efficient way? How can I wrap this process up in a loop?

library(sf)

g = st_sfc(c(st_point(1:2),st_point(3:2)))

g.2 <- st_buffer(g,2)
g.1 <- st_buffer(g,1)
g.05 <- st_buffer(g,0.5)

plot(st_geometry(g.2))
plot(st_geometry(g.1), add=TRUE)
plot(st_geometry(g.05), add=TRUE)
plot(st_geometry(g), add=TRUE)

Body:
Sticking to your request (using a loop) see here an example

library(sf)
g = st_sfc(c(st_point(1:2),st_point(3:2)))
#Buffers
buffers=c(2,1,0.5)
#Initialise result
result=g
#Loop
for (i in buffers){
  #Appending buffered points
  result=c(result,st_buffer(g,i))
}
#Remove initial element
result=result[-1]
plot(result, axes=TRUE)


问:
How do I find the closest values in a Pandas series to an input number?
I have seen:

how do I find the closest value to a given number in an array?

How do I find the closest array element to an arbitrary (non-member) number?.

These relate to vanilla python and not pandas.

If I have the series:

```<code>ix   num
0    1
1    6
2    4
3    5
4    2
</code>```

And I input 3, how can I (efficiently) find?

The index of 3 if it is found in the series

The index of the value below and above 3 if it is not found in the series.

Ie. With the above series {1,6,4,5,2}, and input 3, I should get values (4,2) with indexes (2,4).

答:
You can use numpy.searchsorted. If your search column is not already sorted, you can make a DataFrame that is sorted and remember the mapping between them with pandas.argsort. (This is better than the above methods if you plan on finding the closest value more than once.)

Once it's sorted, find the closest values for your inputs like this:

indLeft = np.searchsorted(df['column'], input, side='left')
indRight = np.searchsorted(df['column'], input, side='right')

valLeft = df['column'][indLeft]
valRight = df['column'][indRight]
```

Body:
I have seen:

how do I find the closest value to a given number in an array?

How do I find the closest array element to an arbitrary (non-member) number?.

These relate to vanilla python and not pandas.

If I have the series:

ix   num
0    1
1    6
2    4
3    5
4    2

And I input 3, how can I (efficiently) find?

The index of 3 if it is found in the series

The index of the value below and above 3 if it is not found in the series.

Ie. With the above series {1,6,4,5,2}, and input 3, I should get values (4,2) with indexes (2,4).


问:
How can I return 3 degrees of precision from oracle number?
We have a scientific application that is in Access and I migrating to Oracle.  Access datatype is Number.  Oracle is number (20,12).

The data is small numbers that we what to show to user in a report as 1.000 E-5 for example.

However the number is actually something like 0.0000099999 which is 0.99999 E-5.

I can convert the number to scientific notation but it returns 0.99999 E-5 without rounding.  I'd like to be able to round this number.  Unfortunately the numbers are all over in terms of # of decimal points.  For example:

0.9999E-5

0.9999E-10

So just a straight ROUND(N,X) would not work I think.  I want to both round to 3 decimals and show in scientific notation.  Essentially this is the scientific concept of degrees of precision where the # of decimals varies but your # of significant figures is always 3.

答:
If every number needs to be rounded (based on your latest comment) I'm assuming the expected output would be as follows:

0.00009999 => 1.000E-04

0.0009999 => 1.000E-03

0.009999 => 1.000E-02

0.09999 => 1.000E-01

0.9999 => 1.000E-00

If so, please try :

select to_char(round(<input>,abs(trunc(log(10,<input>)))),'0.999EEEE') from dual

Example :

select to_char(round(0.00009999,abs(trunc(log(10,0.00009999)))),'0.999EEEE') from dual

Output :

1.000E-04


问:
Where to put component did mount in my next.js file
Halo guys, sorry for this stupid question but where do I put componentDidMount() in my react js document?

I am trying to insert google analytics script and I found a video on it but I dont know where to put that element. I will give you a screenshot of my file.

Thank you
(I know my editor is weird, but visual studio code keeps on crashing)

答:
You might want to create a Layout Component

Example:

```<code>// components/layout.js
import React from 'react'
import { initGA, logPageView } from '../utils/analytics'

export default class Layout extends React.Component {
  componentDidMount () {
    if (!window.GA_INITIALIZED) {
      initGA()
      window.GA_INITIALIZED = true
    }
    logPageView()
  }
  render () {
    return (
      <div>
        {this.props.children}
      </div>
    )
  }
}
</code>```

You can then use Layout to wrap other components

Example:

```<code>// pages/about.js
import Layout from '../components/layout'

export default () => (
  <Layout>
    <div>About us</div>
  </Layout>
)
</code>```

Example:

```<code>// utils/analytics.js
import ReactGA from 'react-ga'

export const initGA = () => {
  console.log('GA init')
  ReactGA.initialize('UA-xxxxxxxxx-1')
}
export const logPageView = () => {
  console.log(`Logging pageview for ${window.location.pathname}`)
  ReactGA.set({ page: window.location.pathname })
  ReactGA.pageview(window.location.pathname)
}
export const logEvent = (category = '', action = '') => {
  if (category && action) {
    ReactGA.event({ category, action })
  }
}
export const logException = (description = '', fatal = false) => {
  if (description) {
    ReactGA.exception({ description, fatal })
  }
}
</code>```

I hope this helps.

Body:
You might want to create a Layout Component

Example:

// components/layout.js
import React from 'react'
import { initGA, logPageView } from '../utils/analytics'

export default class Layout extends React.Component {
  componentDidMount () {
    if (!window.GA_INITIALIZED) {
      initGA()
      window.GA_INITIALIZED = true
    }
    logPageView()
  }
  render () {
    return (
      <div>
        {this.props.children}
      </div>
    )
  }
}

You can then use Layout to wrap other components

Example:

// pages/about.js
import Layout from '../components/layout'

export default () => (
  <Layout>
    <div>About us</div>
  </Layout>
)

Example:

// utils/analytics.js
import ReactGA from 'react-ga'

export const initGA = () => {
  console.log('GA init')
  ReactGA.initialize('UA-xxxxxxxxx-1')
}
export const logPageView = () => {
  console.log(`Logging pageview for ${window.location.pathname}`)
  ReactGA.set({ page: window.location.pathname })
  ReactGA.pageview(window.location.pathname)
}
export const logEvent = (category = '', action = '') => {
  if (category && action) {
    ReactGA.event({ category, action })
  }
}
export const logException = (description = '', fatal = false) => {
  if (description) {
    ReactGA.exception({ description, fatal })
  }
}

I hope this helps.


问:
Unicode and incorrect number of arguments errors with Python 3 and MySQL connector
I'm trying to execute a simple SELECT query on a MySQL(MariaDB) database in Python 3.

I'm getting very weird errors

        query = (
            'SELECT user_id, user_password_hash, user_password_salt '
            'FROM users '
            'WHERE user_username = "%s"'
        )

        print(f'Trying login, username {username}, password {password}')

        try:
            cursor = self.connection.cursor(prepared=True)
            cursor.execute(query, (username,))

            results = cursor.fetchone()

            print(results)
            ...

        except Error as e:
            print('Error in login query: ' + str(e))
            raise e
```

<p>Running this code gives me the following error:
<code>Error in login query: 1210: Incorrect number of arguments executing prepared statement</code></p>

<p>I don't understand - it requires one argument, and I've given it one argument.
Removing the comma in the tuple in <code>execute()</code> doesn't fix this problem.</p>

<p>Now, if I remove the quotes from the query, I get an entirely different error:</p>

<pre class="lang-py prettyprint-override"><code>        query = (
            'SELECT user_id, user_password_hash, user_password_salt '
            'FROM users '
            'WHERE user_username = %s'
        )
</code>```

```<code>UnicodeDecodeError: 'utf-8' codec can't decode byte 0x9b in position 0: invalid start byte

The above exception was the direct cause of the following exception:

SystemError: <class 'UnicodeDecodeError'> returned a result with an error set
</code>```

The table is using utf8mb4, so that shouldn't be a problem.

Also, running SELECT * FROM users WHERE user_username="jeff" in the MySQL console works fine.

This should be a really trivial query, but I've got absolutely no idea what's going on - any help would be greatly appreciated.

EDIT: When experimenting in an isolated Python process, I reproduced the problem by running this:

import mysql.connector

conn = mysql.connector.connect(host="localhost",
                               database="lucidlab",
                               user="lucidlab",
                               password="lucidlab")

cur = conn.cursor()
cur.execute("SELECT * FROM users WHERE user_username='jeff'")

results = cur.fetchall() # ERROR
print(results)
```

This makes me think that perhaps it's a database encoding problem?

EDIT: I've solved the problem. Please see my answer below.

答:
query = """SELECT user_id, user_password_hash, user_password_salt
            FROM users
            WHERE user_username = %s"""

use this it will work..

Body:
I'm trying to execute a simple SELECT query on a MySQL(MariaDB) database in Python 3.

I'm getting very weird errors

        query = (
            'SELECT user_id, user_password_hash, user_password_salt '
            'FROM users '
            'WHERE user_username = "%s"'
        )

        print(f'Trying login, username {username}, password {password}')

        try:
            cursor = self.connection.cursor(prepared=True)
            cursor.execute(query, (username,))

            results = cursor.fetchone()

            print(results)
            ...

        except Error as e:
            print('Error in login query: ' + str(e))
            raise e

Running this code gives me the following error:
Error in login query: 1210: Incorrect number of arguments executing prepared statement

I don't understand - it requires one argument, and I've given it one argument.
Removing the comma in the tuple in execute() doesn't fix this problem.

Now, if I remove the quotes from the query, I get an entirely different error:

        query = (
            'SELECT user_id, user_password_hash, user_password_salt '
            'FROM users '
            'WHERE user_username = %s'
        )

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x9b in position 0: invalid start byte

The above exception was the direct cause of the following exception:

SystemError: <class 'UnicodeDecodeError'> returned a result with an error set

The table is using utf8mb4, so that shouldn't be a problem.

Also, running SELECT * FROM users WHERE user_username="jeff" in the MySQL console works fine.

This should be a really trivial query, but I've got absolutely no idea what's going on - any help would be greatly appreciated.

EDIT: When experimenting in an isolated Python process, I reproduced the problem by running this:

import mysql.connector

conn = mysql.connector.connect(host="localhost",
                               database="lucidlab",
                               user="lucidlab",
                               password="lucidlab")

cur = conn.cursor()
cur.execute("SELECT * FROM users WHERE user_username='jeff'")

results = cur.fetchall() # ERROR
print(results)

This makes me think that perhaps it's a database encoding problem?

EDIT: I've solved the problem. Please see my answer below.


问:
How to extract from sheet a, multiply by a factor in sheet b and paste in sheet c while including preceding data
im trying to figure out a way to automate this process. I've broken it down in to the following steps but don't know how to start.. VBA code is a slight edit from record macro. Any guidance would be really appreciated!

1) Check how many cities starting in current month in 'Launch Matrix tab',

2) Extract data from 'sample data tab',

3) multiply (2)'s data with the amount of cities starting, in order to see full impact of X amount of cities

4) and paste in 'Consolidated Tab' under the right month

Consolidated tab looks exactly the same as the sample data tab, only difference is it shows a consolidated view.

Code for first 2 months looks like the following:

```<code>'
' Macro2 Macro
'

'
    ActiveCell.FormulaR1C1 = _
        "=('Launch Matrix'!R2C4*'Small City Data'!R[-1]C+'Launch Matrix'!R3C4*'Medium City Data'!R[-1]C+'Launch Matrix'!R4C4*'Large City Data'!R[-1]C)"
    Range("C5").Select
    Selection.Copy
    Range("C3").Select
    Selection.End(xlToRight).Select
    Range("L5").Select
    Range(Selection, Selection.End(xlToLeft)).Select
    Range("D5:L5").Select
    Range("L5").Activate
    ActiveSheet.Paste
    Range("D5").Select
    Application.CutCopyMode = False
    ActiveCell.FormulaR1C1 = _
        "=('Launch Matrix'!R2C4*'Small City Data'!R[-1]C+'Launch Matrix'!R3C4*'Medium City Data'!R[-1]C+'Launch Matrix'!R4C4*'Large City Data'!R[-1]C)+('Launch Matrix'!R2C5*'Small City Data'!R[-1]C[-1]+'Launch Matrix'!R3C5*'Medium City Data'!R[-1]C[-1]+'Launch Matrix'!R4C5*'Large City Data'!R[-1]C[-1])"
    Range("D5").Select
    Selection.Copy
    Range(Selection, Selection.End(xlToRight)).Select
    ActiveSheet.Paste
End Sub
</code>```

I want Feb-20 in consolidated tab to include launches in Feb-20 (i.e. Month 1 rev/costs * # of launches in Feb-20) and Month 2 data of Jan-20 launches (Month 2 rev/costs * # of launches in Jan-20) and so on and so forth.
Thank you!

答:
I don't have time to simulate your whole set-up so here is a slimmed-down example which should give you some pointers.

This is worth a read.

```<code>Sub x()

Dim rLaunch As Range, c As Long

'table showing monthly totals of small etc
Set rLaunch = Worksheets("Launch").Range("C2").CurrentRegion
'remove headings so just have numbers
Set rLaunch = rLaunch.Offset(, 2).Resize(, rLaunch.Columns.Count - 2)

'loop through each column of table (months)
For c = 1 To rLaunch.Columns.Count
    'revenue
    'multiply small number by small revenue and add to same for medium and large
    Worksheets("Consolidated").Range("A3").Offset(, c).Value = _
        rLaunch(1, c) * Worksheets("Small").Range("B4").Offset(, c) + _
        rLaunch(2, c) * Worksheets("Medium").Range("B4").Offset(, c) + _
        rLaunch(3, c) * Worksheets("Large").Range("B4").Offset(, c)
Next c

End Sub
</code>```

Launch sheet

Small city sheet (medium and large are the same)

Consolidated sheet

Body:
im trying to figure out a way to automate this process. I've broken it down in to the following steps but don't know how to start.. VBA code is a slight edit from record macro. Any guidance would be really appreciated!

1) Check how many cities starting in current month in 'Launch Matrix tab',

2) Extract data from 'sample data tab',

3) multiply (2)'s data with the amount of cities starting, in order to see full impact of X amount of cities

4) and paste in 'Consolidated Tab' under the right month

Consolidated tab looks exactly the same as the sample data tab, only difference is it shows a consolidated view.

Code for first 2 months looks like the following:

'
' Macro2 Macro
'

'
    ActiveCell.FormulaR1C1 = _
        "=('Launch Matrix'!R2C4*'Small City Data'!R[-1]C+'Launch Matrix'!R3C4*'Medium City Data'!R[-1]C+'Launch Matrix'!R4C4*'Large City Data'!R[-1]C)"
    Range("C5").Select
    Selection.Copy
    Range("C3").Select
    Selection.End(xlToRight).Select
    Range("L5").Select
    Range(Selection, Selection.End(xlToLeft)).Select
    Range("D5:L5").Select
    Range("L5").Activate
    ActiveSheet.Paste
    Range("D5").Select
    Application.CutCopyMode = False
    ActiveCell.FormulaR1C1 = _
        "=('Launch Matrix'!R2C4*'Small City Data'!R[-1]C+'Launch Matrix'!R3C4*'Medium City Data'!R[-1]C+'Launch Matrix'!R4C4*'Large City Data'!R[-1]C)+('Launch Matrix'!R2C5*'Small City Data'!R[-1]C[-1]+'Launch Matrix'!R3C5*'Medium City Data'!R[-1]C[-1]+'Launch Matrix'!R4C5*'Large City Data'!R[-1]C[-1])"
    Range("D5").Select
    Selection.Copy
    Range(Selection, Selection.End(xlToRight)).Select
    ActiveSheet.Paste
End Sub

I want Feb-20 in consolidated tab to include launches in Feb-20 (i.e. Month 1 rev/costs * # of launches in Feb-20) and Month 2 data of Jan-20 launches (Month 2 rev/costs * # of launches in Jan-20) and so on and so forth.
Thank you!

Body:
I don't have time to simulate your whole set-up so here is a slimmed-down example which should give you some pointers.

This is worth a read.

Sub x()

Dim rLaunch As Range, c As Long

'table showing monthly totals of small etc
Set rLaunch = Worksheets("Launch").Range("C2").CurrentRegion
'remove headings so just have numbers
Set rLaunch = rLaunch.Offset(, 2).Resize(, rLaunch.Columns.Count - 2)

'loop through each column of table (months)
For c = 1 To rLaunch.Columns.Count
    'revenue
    'multiply small number by small revenue and add to same for medium and large
    Worksheets("Consolidated").Range("A3").Offset(, c).Value = _
        rLaunch(1, c) * Worksheets("Small").Range("B4").Offset(, c) + _
        rLaunch(2, c) * Worksheets("Medium").Range("B4").Offset(, c) + _
        rLaunch(3, c) * Worksheets("Large").Range("B4").Offset(, c)
Next c

End Sub

Launch sheet

Small city sheet (medium and large are the same)

Consolidated sheet


问:
Vue computed function to match elements from 2 different arrays
Currently, I'm working with Vue v2.x.x. I have an array:

sectionTitles = ['Technology', 'Data', 'Poverty and Research', ...]
```
<p>and I have <code>jobsData</code> that looks like this:</p>
<pre class="lang-js prettyprint-override"><code>[{'title': 'Software Engineer', mainTag: 'Data', ...}...]
</code>```

I want to display
 in an
 when the sectionTitle matches the job.mainTag.

I was reading in the Vue docs that you shouldn't combine v-if with v-for, so I created a computed method to be able to filter the jobs. Here is what I did so far:

window.onload = function () {
    var app = new Vue({
    delimiters: ['${', '}'],
    el: '#app',
    data: {
      jobs: jobsData,
      sectionTitles: ['Data','Poverty Research Unit', 'Technology']
    },
    computed: {
      matchingTitles: function (sectionTitle) {
        return this.jobs.filter(function (job, sectionTitle) {
          job.mainTag === sectionTitle;
        })
      }
    }
  })
}
```
<pre class="lang-html prettyprint-override"><code><div id="app">
  <template v-for="title in sectionTitles">
    <h4 class="h3">{{ title }}</h4>
    <ul class="list-none p-0 color-mid-background" id="jobs-list">
      <li class="py-1 px-2" v-for="job in matchingTitles(title)">

          ${job.title}

      </li>
    </ul>
  </template>
</div>
</code>```

So basically I want to only display
 when the sectionTitle (for example Data) matches the job.mainTag. How can I go about achieving this in Vue?

答:
Change your computed method to just a method. Then change your filter to return a value. Also for displaying in Vue you want to use {{....}} not ${...}

new Vue({
  el: '#app',
    data: {
      jobs: [{'title': 'Software Engineer', mainTag: 'Data'}],
      sectionTitles: ['Data','Poverty Research Unit', 'Technology']
    },
    methods: {
      matchingTitles: function (sectionTitle) {
        return this.jobs.filter ((job)=>{
          return job.mainTag === sectionTitle;
        })
      }
    }
})```
<pre class="snippet-code-html lang-html prettyprint-override"><code><script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <template v-for="title in sectionTitles">
    <h4 class="h3">{{ title }}</h4>
    <ul class="list-none p-0 color-mid-background" id="jobs-list">
      <li class="py-1 px-2" v-for="job in matchingTitles(title)">

          {{job.title}}

      </li>
    </ul>
  </template>
</div></code>```

Body:
Currently, I'm working with Vue v2.x.x. I have an array:

sectionTitles = ['Technology', 'Data', 'Poverty and Research', ...]

and I have jobsData that looks like this:

[{'title': 'Software Engineer', mainTag: 'Data', ...}...]

I want to display
 in an
 when the sectionTitle matches the job.mainTag.

I was reading in the Vue docs that you shouldn't combine v-if with v-for, so I created a computed method to be able to filter the jobs. Here is what I did so far:

window.onload = function () {
    var app = new Vue({
    delimiters: ['${', '}'],
    el: '#app',
    data: {
      jobs: jobsData,
      sectionTitles: ['Data','Poverty Research Unit', 'Technology']
    },
    computed: {
      matchingTitles: function (sectionTitle) {
        return this.jobs.filter(function (job, sectionTitle) {
          job.mainTag === sectionTitle;
        })
      }
    }
  })
}

  <template v-for="title in sectionTitles">

{{ title }}

        <a :href="`${job.url}`">
          ${job.title}

  </template>

So basically I want to only display
 when the sectionTitle (for example Data) matches the job.mainTag. How can I go about achieving this in Vue?

Body:
Change your computed method to just a method. Then change your filter to return a value. Also for displaying in Vue you want to use {{....}} not ${...}

new Vue({
  el: '#app',
    data: {
      jobs: [{'title': 'Software Engineer', mainTag: 'Data'}],
      sectionTitles: ['Data','Poverty Research Unit', 'Technology']
    },
    methods: {
      matchingTitles: function (sectionTitle) {
        return this.jobs.filter ((job)=>{
          return job.mainTag === sectionTitle;
        })
      }
    }
})

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <template v-for="title in sectionTitles">
    <h4 class="h3">{{ title }}</h4>
    <ul class="list-none p-0 color-mid-background" id="jobs-list">
      <li class="py-1 px-2" v-for="job in matchingTitles(title)">
        <a :href="job.url">
          {{job.title}}
        </a>
      </li>
    </ul>
  </template>
</div>