问:
Remove duplicates from MongoDB 4.0
I am trying to remove duplicates from MongoDB but all solutions find fail. Given the current JSON structure:

```<code>{
    "_id": { "$oid": "5cee31bbca8a185b76a692db" },
    "date": { "$date": "2018-10-07T19:11:38.000Z" },
    "id": "1049014405130858496",
    "username": "chrisoldcorn",
    "text": "“The #UK can rest now. The Orange Buffoon is back in his xenophobic #WhiteHouse!” #news #politics #trump #populist #uspoli #ukpolitics #ukpoli #london #scotland #TrumpBaby #usa #america #canada #eu #europe #brexit #maga #msm #gop #elections #election2018 https://medium.com/@chrisoldcorn/trump-babys-uk-visit-a-reflection-1c2aa4ad942 …pic.twitter.com/Y6Yihs9g6K",
    "retweets": 1,
    "favorites": 0,
    "mentions": "@chrisoldcorn",
    "hashtags": "#UK #WhiteHouse #news #politics #trump #populist #uspoli #ukpolitics #ukpoli #london #scotland #TrumpBaby #usa #america #canada #eu #europe #brexit #maga #msm #gop #elections #election2018",
    "geo": "",
    "replies": 0,
    "to": null,
    "lan": "en"
}
</code>```

I need to remove all duplicates based on field "id" in the file.

I have tried db.tweets.ensureIndex( { id:1 }, { unique:true, dropDups:true } ) but I am not sure this is the correct way. I obtain this output:

Can anyone help me?

答:
It looks like you are running a MongoDB with version >3.0 and hence cannot remove dups by ensuring an index

According to the docs:

Changed in version 3.0: The dropDups option is no longer available.

The fastest way to do this would be to

Create a Dump

Drop the collection

Create the new Index

Restore the Dump

All duplicate documents will be dropped during the restore insert

The next best solution will be to run a script to collect all duplicate Ids and remove them

Title: Remove duplicates from MongoDB 4.0

Body:
I am trying to remove duplicates from MongoDB but all solutions find fail. Given the current JSON structure:

{
    "_id": { "$oid": "5cee31bbca8a185b76a692db" },
    "date": { "$date": "2018-10-07T19:11:38.000Z" },
    "id": "1049014405130858496",
    "username": "chrisoldcorn",
    "text": "“The #UK can rest now. The Orange Buffoon is back in his xenophobic #WhiteHouse!” #news #politics #trump #populist #uspoli #ukpolitics #ukpoli #london #scotland #TrumpBaby #usa #america #canada #eu #europe #brexit #maga #msm #gop #elections #election2018 https://medium.com/@chrisoldcorn/trump-babys-uk-visit-a-reflection-1c2aa4ad942 …pic.twitter.com/Y6Yihs9g6K",
    "retweets": 1,
    "favorites": 0,
    "mentions": "@chrisoldcorn",
    "hashtags": "#UK #WhiteHouse #news #politics #trump #populist #uspoli #ukpolitics #ukpoli #london #scotland #TrumpBaby #usa #america #canada #eu #europe #brexit #maga #msm #gop #elections #election2018",
    "geo": "",
    "replies": 0,
    "to": null,
    "lan": "en"
}

I need to remove all duplicates based on field "id" in the file.

I have tried db.tweets.ensureIndex( { id:1 }, { unique:true, dropDups:true } ) but I am not sure this is the correct way. I obtain this output:

Can anyone help me?


来源: stackexchange

问:
Make R project Automatically open Specific Scripts
I am working in team, we mainly use R, I am quite used to use R project in Rstudio, which I like because when I open them I have all my scripts and everything at the right place. However when another member of the team opens one of my project it loads the values and data but does not open the R script (one can see that by physically clicking on the project through the windows explorer rather than using the menu at the top right in R). I guess something can be done in the .Rprofile but I did not find any command to open physically a script, I tried

```<code>file.edit("./Main.R")
</code>```

but it did not open anything. It just got me the message :

Error: could not find function "file.edit"

As always,
Thanks for your help !

**EDIT
I tried to use

```<code>file.show
file.edit
shell.exec(file.path(getwd()), "Main.R"))
</code>```

in the .Rprofile. Nothing worked.

Romain

答:
You can use the following code in the .Rprofile file.

```<code>setHook("rstudio.sessionInit", function(newSession) {
  if (newSession)
    rstudioapi::navigateToFile('<file name>', line = -1L, column = -1L)
}, action = "append")
</code>```

The rstudioapi library has the function navigateToFile to open a file in Rstudio. The problem is that the code in the .Rprofile runs before Rstudio initialization. To deal with this problem you can use the setHook function (from base package) to make the code execute after the Rstudio initialization.

ContentLicense: CC BY-SA 3.0

Body:
I am working in team, we mainly use R, I am quite used to use R project in Rstudio, which I like because when I open them I have all my scripts and everything at the right place. However when another member of the team opens one of my project it loads the values and data but does not open the R script (one can see that by physically clicking on the project through the windows explorer rather than using the menu at the top right in R). I guess something can be done in the .Rprofile but I did not find any command to open physically a script, I tried

file.edit("./Main.R")

but it did not open anything. It just got me the message :

Error: could not find function "file.edit"

As always,
Thanks for your help !

**EDIT
I tried to use

file.show
file.edit
shell.exec(file.path(getwd()), "Main.R"))

in the .Rprofile. Nothing worked.

Romain

Body:
You can use the following code in the .Rprofile file.

setHook("rstudio.sessionInit", function(newSession) {
  if (newSession)
    rstudioapi::navigateToFile('<file name>', line = -1L, column = -1L)
}, action = "append")

The rstudioapi library has the function navigateToFile to open a file in Rstudio. The problem is that the code in the .Rprofile runs before Rstudio initialization. To deal with this problem you can use the setHook function (from base package) to make the code execute after the Rstudio initialization.

ContentLicense: CC BY-SA 4.0


问:
firebaseAuth.logout workf for menu activity but not for profile activity
I may entangled myself in this but I can't seem to see any reason why this doesn't work.

In the MenuActivity the menu items and their functionalities work fine. Log out works perfectly. When I extent the MenuActivity to the ProfileActivity, for some reason the log out button doesn't work the same, the app crashes and displays the message "My application has stopped xClose the application".

Here are all the classes:

MenuActivity.java:

package com.example.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import com.google.firebase.auth.FirebaseAuth;

public class MenuActivity extends AppCompatActivity {

    private FirebaseAuth firebaseAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);

        firebaseAuth = FirebaseAuth.getInstance();
    }

    private void Logout(){
        firebaseAuth.signOut();
        finish();
        startActivity(new Intent(MenuActivity.this, MainActivity.class));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflate = getMenuInflater();
        inflate.inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch(item.getItemId()){
            case R.id.logoutMenu:{
                Logout();
                return true;
            }
            case R.id.profileMenu:
                startActivity(new Intent(MenuActivity.this, ProfileActivity.class));
        }
        return super.onOptionsItemSelected(item);
    }

}
```

<p>ProfileActivity.java</p>

<pre class="lang-java prettyprint-override"><code>package com.example.myapplication;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class ProfileActivity extends MenuActivity {

    private TextView userName, userEmail, phoneNumber;
    private Button editProfile, btnChangePassword;
    private FirebaseAuth firebaseAuth;
    private FirebaseDatabase firebaseDatabase;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);
        userName = findViewById(R.id.tvName3);
        userEmail = findViewById(R.id.tvEmail3);
        phoneNumber = findViewById(R.id.tvPhone3);
        editProfile = findViewById(R.id.btnEditProfile);
        btnChangePassword = findViewById(R.id.btnChangePassword);

        firebaseAuth = FirebaseAuth.getInstance();
        firebaseDatabase = FirebaseDatabase.getInstance();

        final DatabaseReference databaseReference = firebaseDatabase.getReference(firebaseAuth.getUid());

        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                UserProfile userProfile = dataSnapshot.getValue(UserProfile.class);
                userName.setText("Name: " + userProfile.getUserName());
                userEmail.setText("Email: " + userProfile.getUserEmail());
                phoneNumber.setText("Phone: " + userProfile.getUserPhone());
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(ProfileActivity.this, databaseError.getCode(), Toast.LENGTH_SHORT);
            }
        });

        editProfile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(ProfileActivity.this, UpdateProfile.class));
            }
        });
        btnChangePassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(ProfileActivity.this, ChangePassword.class));
            }
        });
    }
}

</code>```

and the menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    android:id="@+id/main_menu" >
    <item
        android:id="@+id/logoutMenu"
        android:orderInCategory="100"
        app:showAsAction="always"
        android:title="Logout">
    </item>

    <item
        android:id="@+id/profileMenu"
        android:orderInCategory="200"
        app:showAsAction="never"
        android:title="Profile">
    </item>

    <item
        android:id="@+id/refreshMenu"
        android:orderInCategory="300"
        app:showAsAction="never"
        android:title="Refresh">
    </item>
</menu>
```

<p>Here are the logcat messages</p>

<pre class="lang-java prettyprint-override"><code>2019-05-31 09:15:56.742 20660-20660/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapplication, PID: 20660
    android.content.res.Resources$NotFoundException: String resource ID #0xfffffffd
        at android.content.res.Resources.getText(Resources.java:338)
        at android.widget.Toast.makeText(Toast.java:304)
        at com.example.myapplication.ProfileActivity$1.onCancelled(ProfileActivity.java:58)
        at com.google.firebase.database.core.ValueEventRegistration.fireCancelEvent(com.google.firebase:firebase-database@@17.0.0:80)
        at com.google.firebase.database.core.view.CancelEvent.fire(com.google.firebase:firebase-database@@17.0.0:40)
        at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@17.0.0:55)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:169)
        at android.app.ActivityThread.main(ActivityThread.java:6585)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
</code>```

Is there something I did wrong in the ProfileActivity that interferes with the menu?

答:
The issue maybe is in the lines that tell the MenuActivity what to do when log out is pressed.

```<code>private void Logout(){
    firebaseAuth.signOut();
    finish();
    startActivity(new Intent(MenuActivity.this, MainActivity.class));
}
</code>```

Tell me if i understood well, the crash happens only when you're in ProfileActivity? Because here you're launching an Intent that pass from MenuActivity to MainActivity, so it may cause problems if you're in ProfileActivity...

Body:
I may entangled myself in this but I can't seem to see any reason why this doesn't work.

In the MenuActivity the menu items and their functionalities work fine. Log out works perfectly. When I extent the MenuActivity to the ProfileActivity, for some reason the log out button doesn't work the same, the app crashes and displays the message "My application has stopped xClose the application".

Here are all the classes:

MenuActivity.java:

package com.example.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import com.google.firebase.auth.FirebaseAuth;

public class MenuActivity extends AppCompatActivity {

    private FirebaseAuth firebaseAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);

        firebaseAuth = FirebaseAuth.getInstance();
    }

    private void Logout(){
        firebaseAuth.signOut();
        finish();
        startActivity(new Intent(MenuActivity.this, MainActivity.class));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflate = getMenuInflater();
        inflate.inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch(item.getItemId()){
            case R.id.logoutMenu:{
                Logout();
                return true;
            }
            case R.id.profileMenu:
                startActivity(new Intent(MenuActivity.this, ProfileActivity.class));
        }
        return super.onOptionsItemSelected(item);
    }

}

ProfileActivity.java

package com.example.myapplication;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class ProfileActivity extends MenuActivity {

    private TextView userName, userEmail, phoneNumber;
    private Button editProfile, btnChangePassword;
    private FirebaseAuth firebaseAuth;
    private FirebaseDatabase firebaseDatabase;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);
        userName = findViewById(R.id.tvName3);
        userEmail = findViewById(R.id.tvEmail3);
        phoneNumber = findViewById(R.id.tvPhone3);
        editProfile = findViewById(R.id.btnEditProfile);
        btnChangePassword = findViewById(R.id.btnChangePassword);

        firebaseAuth = FirebaseAuth.getInstance();
        firebaseDatabase = FirebaseDatabase.getInstance();

        final DatabaseReference databaseReference = firebaseDatabase.getReference(firebaseAuth.getUid());

        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                UserProfile userProfile = dataSnapshot.getValue(UserProfile.class);
                userName.setText("Name: " + userProfile.getUserName());
                userEmail.setText("Email: " + userProfile.getUserEmail());
                phoneNumber.setText("Phone: " + userProfile.getUserPhone());
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(ProfileActivity.this, databaseError.getCode(), Toast.LENGTH_SHORT);
            }
        });

        editProfile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(ProfileActivity.this, UpdateProfile.class));
            }
        });
        btnChangePassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(ProfileActivity.this, ChangePassword.class));
            }
        });
    }
}

and the menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    android:id="@+id/main_menu" >
    <item
        android:id="@+id/logoutMenu"
        android:orderInCategory="100"
        app:showAsAction="always"
        android:title="Logout">
    </item>

    <item
        android:id="@+id/profileMenu"
        android:orderInCategory="200"
        app:showAsAction="never"
        android:title="Profile">
    </item>

    <item
        android:id="@+id/refreshMenu"
        android:orderInCategory="300"
        app:showAsAction="never"
        android:title="Refresh">
    </item>
</menu>

Here are the logcat messages

2019-05-31 09:15:56.742 20660-20660/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapplication, PID: 20660
    android.content.res.Resources$NotFoundException: String resource ID #0xfffffffd
        at android.content.res.Resources.getText(Resources.java:338)
        at android.widget.Toast.makeText(Toast.java:304)
        at com.example.myapplication.ProfileActivity$1.onCancelled(ProfileActivity.java:58)
        at com.google.firebase.database.core.ValueEventRegistration.fireCancelEvent(com.google.firebase:firebase-database@@17.0.0:80)
        at com.google.firebase.database.core.view.CancelEvent.fire(com.google.firebase:firebase-database@@17.0.0:40)
        at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@17.0.0:55)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:169)
        at android.app.ActivityThread.main(ActivityThread.java:6585)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

Is there something I did wrong in the ProfileActivity that interferes with the menu?

Body:
The issue maybe is in the lines that tell the MenuActivity what to do when log out is pressed.

private void Logout(){
    firebaseAuth.signOut();
    finish();
    startActivity(new Intent(MenuActivity.this, MainActivity.class));
}

Tell me if i understood well, the crash happens only when you're in ProfileActivity? Because here you're launching an Intent that pass from MenuActivity to MainActivity, so it may cause problems if you're in ProfileActivity...


问:
Unable to parse array of dictionary inside string
Currently I am working on bus booking module. After user set his departure and arrival city and date of journey, user will be shown a list of available buses. I have done that part successfully. But the problem which I am facing is that, each bus has it's own cancellation policy which is array of dictionary inside string. I am unable to parse it. Inside every dictionary of "apiAvailableBuses" there is "cancellationPolicy" key which has string as value which contains array of dictionary. I have removed other key value pairs from "apiAvailableBuses".

List of available Buses JSON response:

```<code>"apiAvailableBuses":[
   {
      "cancellationPolicy":"[{\"cutoffTime\":\"5\",\"refundInPercentage\":\"90\"}]"
   },
   {
      "cancellationPolicy":"[{\"cutoffTime\":\"9-12\",\"refundInPercentage\":\"25\"},{\"cutoffTime\":\"12-24\",\"refundInPercentage\":\"35\"},{\"cutoffTime\":\"24-48\",\"refundInPercentage\":\"50\"},{\"cutoffTime\":\"48-60\",\"refundInPercentage\":\"75\"},{\"cutoffTime\":\"60\",\"refundInPercentage\":\"90\"}]"
   },
   {
      "cancellationPolicy":"[{\"cutoffTime\":\"5\",\"refundInPercentage\":\"90\"}]"
   },
   {
      "cancellationPolicy":"[{\"cutoffTime\":\"5\",\"refundInPercentage\":\"90\"}]"
   },
   {
      "cancellationPolicy":"[{\"cutoffTime\":\"5\",\"refundInPercentage\":\"90\"}]"
   },
   {
      "cancellationPolicy":"[{\"cutoffTime\":\"5\",\"refundInPercentage\":\"90\"}]"
   },
   {
      "cancellationPolicy":"[{\"cutoffTime\":\"5\",\"refundInPercentage\":\"90\"}]"
   },
   {
      "cancellationPolicy":"[{\"cutoffTime\":\"6-24\",\"refundInPercentage\":\"70\"},{\"cutoffTime\":\"24\",\"refundInPercentage\":\"85\"}]"
   }
]
</code>```

Can anyone help me with a solution for this? If anyone can't understand my question please let me know.

Note: I am not using Codable in my project.

Thanks in advance.

答:
If you don't want to use Codable for some reason, you can use JSONSerialization.

```<code>let input = "[{\"cutoffTime\":\"5\",\"refundInPercentage\":\"90\"}]"
let data = input.data(using: .utf8)!
let parsed = try! JSONSerialization.jsonObject(with: data, options: []) as! Array<Dictionary<String, Any>>

print(parsed) // [["refundInPercentage": 90, "cutoffTime": 5]]
</code>```

Body:
Currently I am working on bus booking module. After user set his departure and arrival city and date of journey, user will be shown a list of available buses. I have done that part successfully. But the problem which I am facing is that, each bus has it's own cancellation policy which is array of dictionary inside string. I am unable to parse it. Inside every dictionary of "apiAvailableBuses" there is "cancellationPolicy" key which has string as value which contains array of dictionary. I have removed other key value pairs from "apiAvailableBuses".

List of available Buses JSON response:

"apiAvailableBuses":[
   {
      "cancellationPolicy":"[{\"cutoffTime\":\"5\",\"refundInPercentage\":\"90\"}]"
   },
   {
      "cancellationPolicy":"[{\"cutoffTime\":\"9-12\",\"refundInPercentage\":\"25\"},{\"cutoffTime\":\"12-24\",\"refundInPercentage\":\"35\"},{\"cutoffTime\":\"24-48\",\"refundInPercentage\":\"50\"},{\"cutoffTime\":\"48-60\",\"refundInPercentage\":\"75\"},{\"cutoffTime\":\"60\",\"refundInPercentage\":\"90\"}]"
   },
   {
      "cancellationPolicy":"[{\"cutoffTime\":\"5\",\"refundInPercentage\":\"90\"}]"
   },
   {
      "cancellationPolicy":"[{\"cutoffTime\":\"5\",\"refundInPercentage\":\"90\"}]"
   },
   {
      "cancellationPolicy":"[{\"cutoffTime\":\"5\",\"refundInPercentage\":\"90\"}]"
   },
   {
      "cancellationPolicy":"[{\"cutoffTime\":\"5\",\"refundInPercentage\":\"90\"}]"
   },
   {
      "cancellationPolicy":"[{\"cutoffTime\":\"5\",\"refundInPercentage\":\"90\"}]"
   },
   {
      "cancellationPolicy":"[{\"cutoffTime\":\"6-24\",\"refundInPercentage\":\"70\"},{\"cutoffTime\":\"24\",\"refundInPercentage\":\"85\"}]"
   }
]

Can anyone help me with a solution for this? If anyone can't understand my question please let me know.

Note: I am not using Codable in my project.

Thanks in advance.

Body:
If you don't want to use Codable for some reason, you can use JSONSerialization.

let input = "[{\"cutoffTime\":\"5\",\"refundInPercentage\":\"90\"}]"
let data = input.data(using: .utf8)!
let parsed = try! JSONSerialization.jsonObject(with: data, options: []) as! Array<Dictionary<String, Any>>

print(parsed) // [["refundInPercentage": 90, "cutoffTime": 5]]


问:
How to find the difference between two slices of strings
Here is my desired outcome

```<code>slice1 := []string{"foo", "bar","hello"}
slice2 := []string{"foo", "bar"}

difference(slice1, slice2)
=> ["hello"]
</code>```

I am looking for the difference between the two string slices!

答:
```<code>func diff(a, b []string) []string {
    temp := map[string]int{}
    for _, s := range a {
        temp[s]++
    }
    for _, s := range b {
        temp[s]--
    }

    var result []string
    for s, v := range temp {
        if v != 0 {
            result = append(result, s)
        }
    }
    return result
}
</code>```

If you want to handle duplicated strings, the v in the map can do that. And you can pick a.Remove(b) ( v>0 ) or b.Remove(a) (v<0)

Body:
Here is my desired outcome

slice1 := []string{"foo", "bar","hello"}
slice2 := []string{"foo", "bar"}

difference(slice1, slice2)
=> ["hello"]

I am looking for the difference between the two string slices!

Body:
func diff(a, b []string) []string {
    temp := map[string]int{}
    for _, s := range a {
        temp[s]++
    }
    for _, s := range b {
        temp[s]--
    }

    var result []string
    for s, v := range temp {
        if v != 0 {
            result = append(result, s)
        }
    }
    return result
}

If you want to handle duplicated strings, the v in the map can do that. And you can pick a.Remove(b) ( v>0 ) or b.Remove(a) (v<0)


问:
UIBarButtonItem with custom UIButton is invisible on iOS <= 10
I need to create a "burger menu" button for my app on my navigationController's left side but since the NavCon is transparent I need to have a shadow on my icon.

Thus I've created a custom UIButton with an image, a drop shadow and the added it as a custom view on a UIBarButtonItem as follows:

```<code>let menuButton = UIButton(type: .custom)
menuButton.addTarget(self, action: #selector(showSideMenu), for: .touchUpInside)
menuButton.setImage(#imageLiteral(resourceName: "menu_white"), for: .normal)
menuButton.tintColor = UIColor.white
menuButton.layer.masksToBounds = false
menuButton.layer.shadowColor = UIColor.black.cgColor
menuButton.layer.shadowOpacity = 1.0
menuButton.layer.shadowRadius = 5
menuButton.layer.shadowOffset = CGSize(width: 0.0, height: 1.0)

self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: menuButton)
</code>```

The code above works perfectly fine on iOS 11, but when I tested my app on ios 9 and 10 (both simulators and real devices) the menu icon is invisible. It is clickable and works as expected, but there is no visible icon.

In the View Hierarchy Debugger I can see a UIButton with 0 width and height, while in ios 11 I can see the normal UIButtonBarStackview with the embedded UIButton.

Any ideas on how to fix this and why this is happening? Thank you very much!

答: You can just call menuButton.sizeToFit() and it will work.</p>

Body:
I need to create a "burger menu" button for my app on my navigationController's left side but since the NavCon is transparent I need to have a shadow on my icon.

Thus I've created a custom UIButton with an image, a drop shadow and the added it as a custom view on a UIBarButtonItem as follows:

let menuButton = UIButton(type: .custom)
menuButton.addTarget(self, action: #selector(showSideMenu), for: .touchUpInside)
menuButton.setImage(#imageLiteral(resourceName: "menu_white"), for: .normal)
menuButton.tintColor = UIColor.white
menuButton.layer.masksToBounds = false
menuButton.layer.shadowColor = UIColor.black.cgColor
menuButton.layer.shadowOpacity = 1.0
menuButton.layer.shadowRadius = 5
menuButton.layer.shadowOffset = CGSize(width: 0.0, height: 1.0)

self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: menuButton)

The code above works perfectly fine on iOS 11, but when I tested my app on ios 9 and 10 (both simulators and real devices) the menu icon is invisible. It is clickable and works as expected, but there is no visible icon.

In the View Hierarchy Debugger I can see a UIButton with 0 width and height, while in ios 11 I can see the normal UIButtonBarStackview with the embedded UIButton.

Any ideas on how to fix this and why this is happening? Thank you very much!


问:
Filtering stderr of subprocess captures empty bytes
I am calling a very noisy CLI command and want to filter the output, presumably stderr. The CLI binary’s output is shapped like python logging, INFO:… or WARNING:…. When run in a shell, the CLI command first has a few "setup" logs, and after a few seconds the actual logs of several dozen per second start.

I've attempted to PIPE the stderr, and to be sure also the stdout, of the process like this:

```<code>process = subprocess.Popen(cmd,
    stderr=subprocess.PIPE,
    stdout=subprocess.PIPE)

    while process.stderr:
        line = process.stderr.readline()
        print("STDERR", line)

    while process.stdout:
        line = process.stdout.readline()
        print("STDOUT", line)
</code>```

This works and the STDERR prints appear for the first bits of the CLI command’s execution. However, when I get to the part after a few seconds the output from my STDERR prints is only b'' — possibly in the same frequency as the normal shell call would output additional logs. I've also tried read() instead of readline() but to no avail. ("STDOUT" never gets called, btw)

Somehow the output that gets logged on a regular shell call is different or too frequent(?) or incomplete(?) for the PIPE to send it through and read and print it properly.

What could I do to debug this further?

答:
An option is to replace those while loops with:

```<code>for line in iter(process.stderr.readline, b''):
    print("STDERR", line)
</code>```

This would at least deal with the fact that process.stderr will never become falsy even when there is nothing to read.

Body:
I am calling a very noisy CLI command and want to filter the output, presumably stderr. The CLI binary’s output is shapped like python logging, INFO:… or WARNING:…. When run in a shell, the CLI command first has a few "setup" logs, and after a few seconds the actual logs of several dozen per second start.

I've attempted to PIPE the stderr, and to be sure also the stdout, of the process like this:

process = subprocess.Popen(cmd,
    stderr=subprocess.PIPE,
    stdout=subprocess.PIPE)

    while process.stderr:
        line = process.stderr.readline()
        print("STDERR", line)

    while process.stdout:
        line = process.stdout.readline()
        print("STDOUT", line)

This works and the STDERR prints appear for the first bits of the CLI command’s execution. However, when I get to the part after a few seconds the output from my STDERR prints is only b'' — possibly in the same frequency as the normal shell call would output additional logs. I've also tried read() instead of readline() but to no avail. ("STDOUT" never gets called, btw)

Somehow the output that gets logged on a regular shell call is different or too frequent(?) or incomplete(?) for the PIPE to send it through and read and print it properly.

What could I do to debug this further?

Body:
An option is to replace those while loops with:

for line in iter(process.stderr.readline, b''):
    print("STDERR", line)

This would at least deal with the fact that process.stderr will never become falsy even when there is nothing to read.


问:
How to have variables accessible across keyframes- AS3
I'm working on a portfolio suggestion application in Flash, which consists of two views: ask user for 3 to 4 points of information, and display recommendations based on that information.

The first view is going swimmingly, as all I need are the graphics to be created.  However, I'm having trouble understanding how a AS3 variable can be seen across keyframes when it was declared and initialized on the first frame.

The only multi-view app I've done is a Restaurant Guide flash app that was described in Adobe Flash CS5 Classroom in a Book.  In that example, the only AS function on four of the views
was a stop() function.

If I were to do the same in this app, and declare the variables as global in the first frame, will they be accessible throughout?

CLARIFICATION

Two comments have said that my question is unclear, so I hope this makes my question more understandable.  I want to know how, if I gather all the user information on Frame 1 and then switch to a view on Frame 15, to access those variables on Frame 1.

答:
This isn't really the way you're supposed to work in AS3.  Each view should be an object in your library with an associated class.  Your document should also have a class associated with it, and it is here that you would hold your common data.  The document class would instantiate each view as it is required and pass in the relevant variables.

That said, if you want to work the old way you shouldn't have trouble declaring a variable on frame 1 like this:

```<code>var myVar:String = "Hello!";
</code>```

and then accessing it on frame 15 of the same timeline like this:

```<code>trace(myVar);
</code>```

If that's what you're doing and it's not working then you'll need to update your question with some code examples.

Body:
This isn't really the way you're supposed to work in AS3.  Each view should be an object in your library with an associated class.  Your document should also have a class associated with it, and it is here that you would hold your common data.  The document class would instantiate each view as it is required and pass in the relevant variables.

That said, if you want to work the old way you shouldn't have trouble declaring a variable on frame 1 like this:

var myVar:String = "Hello!";

and then accessing it on frame 15 of the same timeline like this:

trace(myVar);

If that's what you're doing and it's not working then you'll need to update your question with some code examples.


问:
How to update snapshot with Jest and vue-cli
I should be able to add -u parameter when running my tests, but I can't figure out why it doesn't work:

```<code>npm run test ComponentName.spec.js -u
npm run test ComponentName.spec.js --updateSnapshot
</code>```

but it doesn't work. My package.json:

```<code>"scripts": {
    "test": "vue-cli-service test:unit",
</code>```

I know I can just delete the snapshot files, but I'd like to figure out why the command doesn't work.

答:
In vue-cli 3, your usual npm command calls vue-cli-service and not jest anymore. Vue-cli-service will call jest for you.

Either you can run :

npm run test:unit -- -u

the -- is so that the next arguments have to be passed to the subcommand.

Or

npx vue-cli-service test:unit -u

This will run the tests and upgrade the snapshots.

Body:
I should be able to add -u parameter when running my tests, but I can't figure out why it doesn't work:

npm run test ComponentName.spec.js -u
npm run test ComponentName.spec.js --updateSnapshot

but it doesn't work. My package.json:

"scripts": {
    "test": "vue-cli-service test:unit",

I know I can just delete the snapshot files, but I'd like to figure out why the command doesn't work.


问:
Declare is not valid at this position, expecting
Have Declares which are giving unbefore seen errors, that have attempted to find the solution of. As well as weird as have previous procedures which use same syntax but do not throw any errors

This is for a procedure which uses cursors to retrieve information from a table, then insert it into another table, which is done so that the information within may be used to be able calculate a total, which is passed along along with some required values

```<code>BEGIN
     DECLARE fin INT DEFAULT FALSE;
     DECLARE pol VARCHAR(30) default '';
     DECLARE total_accrued decimal(20,2) DEFAULT '0.00';
     DECLARE total_paid decimal(20,2) DEFAULT '0';
     DECLARE TOTAL DECIMAL(20,2) DEFAULT '0.00';
     DECLARE user_pol varchar(45) default '';

     DECLARE c3 CURSOr FOR SELECT total_accrued FROM fn_policy_mast;
     DECLARE c4 CURSOR FOr SELECT total_amount_paid FROM fn_policy_mast;
     DECLARE c2 CURSOR FOR SELECt policy_number FROM fn_policy_mast;
     DECLARE c1 CURSOr FOR SELECT balance FROM fn_policy_mast;

    -- DECLARE d_policy CURSOR FOR SELECT DISTINCT cod_policy FROM fn_policy_mast;
     DECLARE CONTINUE HANDLER FOR NOT FOUND SET fin = TRUE;

     OPEN c1;
     OPEN c2;
     OPEN c3;
     OPEN c4;
    -- OPEN d_policy;

     read_loop: LOOP

     FETCH c3 INTO total_accrued;
     FETCH c4 INTO total_paid;
     FETCH c2 INTO pol;
     set total = 0;
    -- FETCH d_policy INTO user_pol;

     -- IF user_pol = pol then
      set total_accrued = (SELECT SUM(monthly_premium) FROM col_trans_log WHERE policy_number LIKE pol);
      set total_paid = (SELECT SUM(transaction_amount) FROM col_Trans_log WHERE policy_number LIKE pol);

      set total =  total_paid - total_accrued;
     -- END IF;

    -- set TOTAL = (total_accrued-total_paid);
     UPDATE fn_policy_mast set balance = TOTAL WHERE policy_number like pol;

     IF fin=TRUE THEN
        LEAVE read_loop;
     END IF;

     -- INSERT STATEMENTS HERE
    --  set total = 0;
     END LOOP;
     select * FROM fn_policy_mast;
     close c1;
     close c2;
     CLOSE c3;
     CLOSE c4;

END//
DELIMITER ;````

Expected result to run, accepting the declares, actual experience is thaere is some form of syntax wrong, as it does not properly recognise the declare statements

This is the error message : Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 5

```

答:
Sorry guys, I made a dumb mistake and forget to include the delimiter before the procedure, afterwards it works.

Simply include a "Delimiter //" before the procedure

Body:
Have Declares which are giving unbefore seen errors, that have attempted to find the solution of. As well as weird as have previous procedures which use same syntax but do not throw any errors

This is for a procedure which uses cursors to retrieve information from a table, then insert it into another table, which is done so that the information within may be used to be able calculate a total, which is passed along along with some required values

BEGIN
     DECLARE fin INT DEFAULT FALSE;
     DECLARE pol VARCHAR(30) default '';
     DECLARE total_accrued decimal(20,2) DEFAULT '0.00';
     DECLARE total_paid decimal(20,2) DEFAULT '0';
     DECLARE TOTAL DECIMAL(20,2) DEFAULT '0.00';
     DECLARE user_pol varchar(45) default '';

     DECLARE c3 CURSOr FOR SELECT total_accrued FROM fn_policy_mast;
     DECLARE c4 CURSOR FOr SELECT total_amount_paid FROM fn_policy_mast;
     DECLARE c2 CURSOR FOR SELECt policy_number FROM fn_policy_mast;
     DECLARE c1 CURSOr FOR SELECT balance FROM fn_policy_mast;

    -- DECLARE d_policy CURSOR FOR SELECT DISTINCT cod_policy FROM fn_policy_mast;
     DECLARE CONTINUE HANDLER FOR NOT FOUND SET fin = TRUE;

     OPEN c1;
     OPEN c2;
     OPEN c3;
     OPEN c4;
    -- OPEN d_policy;

     read_loop: LOOP

     FETCH c3 INTO total_accrued;
     FETCH c4 INTO total_paid;
     FETCH c2 INTO pol;
     set total = 0;
    -- FETCH d_policy INTO user_pol;

     -- IF user_pol = pol then
      set total_accrued = (SELECT SUM(monthly_premium) FROM col_trans_log WHERE policy_number LIKE pol);
      set total_paid = (SELECT SUM(transaction_amount) FROM col_Trans_log WHERE policy_number LIKE pol);

      set total =  total_paid - total_accrued;
     -- END IF;

    -- set TOTAL = (total_accrued-total_paid);
     UPDATE fn_policy_mast set balance = TOTAL WHERE policy_number like pol;

     IF fin=TRUE THEN
        LEAVE read_loop;
     END IF;

     -- INSERT STATEMENTS HERE
    --  set total = 0;
     END LOOP;
     select * FROM fn_policy_mast;
     close c1;
     close c2;
     CLOSE c3;
     CLOSE c4;

END//
DELIMITER ;````

Expected result to run, accepting the declares, actual experience is thaere is some form of syntax wrong, as it does not properly recognise the declare statements

This is the error message : Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 5


问:
Passing phone number or CONTACT_ID from android contact details to activity (whatsapp like)
What I'm trying to achieve is to add shortcut to my app in android book contact details, similar to what whatsapp is doing.

I've been following this tutotial: http://blogs.quovantis.com/syncing-contacts-with-an-android-application-2/ and it works well but the author doesn't show how to pass data from contact details to ViewingActivity: https://github.com/ajkh35/ContactsDemo/blob/master/app/src/main/java/com/example/ajay/contacts_4/ViewingActivity.java

There was some comments below the article but no specific answer from the author, can't find anything useful in

```<code>    Uri data = getIntent().getData(); //content://com.android.contacts/data/1169
    List<String> params = data.getPathSegments();
    String first = params.get(0);
    String second = params.get(1);
</code>```

there is some number passed in second param but it's not CONTACT_ID or RAW_CONTACT_ID. Any help?

答:
Ok, so it seems the Uri you're getting from the Contacts app is a Data uri.

Data rows contain info about a specific data-item (like a phone number or an email) of a specific RawContact, so a single Data row "belongs" to a single RawContact which "belongs" to a single Contact.

Luckily, the ContactsContract API allows for implicit joins when querying the Data table, so you can do something like this:

```<code>Uri dataUri = getIntent().getData(); //content://com.android.contacts/data/1169

String[] projection = new String[]{
        Data.CONTACT_ID,
        Data.RAW_CONTACT_ID,
        Data.DISPLAY_NAME,
        Data.MIMETYPE,
        Data.DATA1};

Cursor cur = getContentResolver().query(dataUri, projection, null, null, null);
cur.moveToFirst(); // there should always be exactly one result, since we have a specific data uri here

Log.i("Contact Info", "Got info: id=" + cur.getLong(0) + ", raw-id=" + cur.getLong(1) + ", " + cur.getString(2) + ", " + cur.getString(3) + ", " + cur.getString(4));

cur.close();
</code>```

Body:
What I'm trying to achieve is to add shortcut to my app in android book contact details, similar to what whatsapp is doing.

I've been following this tutotial: http://blogs.quovantis.com/syncing-contacts-with-an-android-application-2/ and it works well but the author doesn't show how to pass data from contact details to ViewingActivity: https://github.com/ajkh35/ContactsDemo/blob/master/app/src/main/java/com/example/ajay/contacts_4/ViewingActivity.java

There was some comments below the article but no specific answer from the author, can't find anything useful in

    Uri data = getIntent().getData(); //content://com.android.contacts/data/1169
    List<String> params = data.getPathSegments();
    String first = params.get(0);
    String second = params.get(1);

there is some number passed in second param but it's not CONTACT_ID or RAW_CONTACT_ID. Any help?

Body:
Ok, so it seems the Uri you're getting from the Contacts app is a Data uri.

Data rows contain info about a specific data-item (like a phone number or an email) of a specific RawContact, so a single Data row "belongs" to a single RawContact which "belongs" to a single Contact.

Luckily, the ContactsContract API allows for implicit joins when querying the Data table, so you can do something like this:

Uri dataUri = getIntent().getData(); //content://com.android.contacts/data/1169

String[] projection = new String[]{
        Data.CONTACT_ID,
        Data.RAW_CONTACT_ID,
        Data.DISPLAY_NAME,
        Data.MIMETYPE,
        Data.DATA1};

Cursor cur = getContentResolver().query(dataUri, projection, null, null, null);
cur.moveToFirst(); // there should always be exactly one result, since we have a specific data uri here

Log.i("Contact Info", "Got info: id=" + cur.getLong(0) + ", raw-id=" + cur.getLong(1) + ", " + cur.getString(2) + ", " + cur.getString(3) + ", " + cur.getString(4));

cur.close();


问:
How to redirect a page
I am using CodeIgniter. Can someone know anything about this?
I am trying to redirect a page using this code:

```<code>window.location.href = "<?php $this->output->set_header(refresh:1; url=login); ?>";
</code>```

But it is not working.

This is the code in my controllers.

```<code>public function view_login(){
    $this->load->view('login');
}
</code>```

This is the code in my routes.

```<code>$route['view_login'] = 'Controller/view_login';
</code>```

Title: How to redirect a page

Body:
I am using CodeIgniter. Can someone know anything about this?
I am trying to redirect a page using this code:

window.location.href = "<?php $this->output->set_header(refresh:1; url=login); ?>";

But it is not working.

This is the code in my controllers.

public function view_login(){
    $this->load->view('login');
}

This is the code in my routes.

$route['view_login'] = 'Controller/view_login';

Body:
In your controller
$this->load->helper('url');

and in your view you can use

<script>
window.location.href = "<?php echo site_url('view_login'); ?>";
</script>


问:
Size-limited queue that holds last N elements in Java
A very simple & quick question on Java libraries: is there a ready-made class that implements a Queue with a fixed maximum size - i.e. it always allows addition of elements, but it will silently remove head elements to accomodate space for newly added elements.

Of course, it's trivial to implement it manually:

```<code>import java.util.LinkedList;

public class LimitedQueue<E> extends LinkedList<E> {
    private int limit;

    public LimitedQueue(int limit) {
        this.limit = limit;
    }

    @Override
    public boolean add(E o) {
        super.add(o);
        while (size() > limit) { super.remove(); }
        return true;
    }
}
</code>```

As far as I see, there's no standard implementation in Java stdlibs, but may be there's one in Apache Commons or something like that?

答:
Use composition not extends (yes I mean extends, as in a reference to the extends keyword in java and yes this is inheritance).  Composition is superier because it completely shields your implementation, allowing you to change the implementation without impacting the users of your class.

I recommend trying something like this (I'm typing directly into this window, so buyer beware of syntax errors):

```<code>public LimitedSizeQueue implements Queue
{
  private int maxSize;
  private LinkedList storageArea;

  public LimitedSizeQueue(final int maxSize)
  {
    this.maxSize = maxSize;
    storageArea = new LinkedList();
  }

  public boolean offer(ElementType element)
  {
    if (storageArea.size() < maxSize)
    {
      storageArea.addFirst(element);
    }
    else
    {
      ... remove last element;
      storageArea.addFirst(element);
    }
  }

  ... the rest of this class
</code>```

A better option (based on the answer by Asaf) might be to wrap the Apache Collections CircularFifoBuffer with a generic class.  For example:

```<code>public LimitedSizeQueue<ElementType> implements Queue<ElementType>
{
    private int maxSize;
    private CircularFifoBuffer storageArea;

    public LimitedSizeQueue(final int maxSize)
    {
        if (maxSize > 0)
        {
            this.maxSize = maxSize;
            storateArea = new CircularFifoBuffer(maxSize);
        }
        else
        {
            throw new IllegalArgumentException("blah blah blah");
        }
    }

    ... implement the Queue interface using the CircularFifoBuffer class
}</code>```

Body:
A very simple & quick question on Java libraries: is there a ready-made class that implements a Queue with a fixed maximum size - i.e. it always allows addition of elements, but it will silently remove head elements to accomodate space for newly added elements.

Of course, it's trivial to implement it manually:

import java.util.LinkedList;

public class LimitedQueue<E> extends LinkedList<E> {
    private int limit;

    public LimitedQueue(int limit) {
        this.limit = limit;
    }

    @Override
    public boolean add(E o) {
        super.add(o);
        while (size() > limit) { super.remove(); }
        return true;
    }
}

As far as I see, there's no standard implementation in Java stdlibs, but may be there's one in Apache Commons or something like that?

Body:
Use composition not extends (yes I mean extends, as in a reference to the extends keyword in java and yes this is inheritance).  Composition is superier because it completely shields your implementation, allowing you to change the implementation without impacting the users of your class.

I recommend trying something like this (I'm typing directly into this window, so buyer beware of syntax errors):

public LimitedSizeQueue implements Queue
{
  private int maxSize;
  private LinkedList storageArea;

  public LimitedSizeQueue(final int maxSize)
  {
    this.maxSize = maxSize;
    storageArea = new LinkedList();
  }

  public boolean offer(ElementType element)
  {
    if (storageArea.size() < maxSize)
    {
      storageArea.addFirst(element);
    }
    else
    {
      ... remove last element;
      storageArea.addFirst(element);
    }
  }

  ... the rest of this class

A better option (based on the answer by Asaf) might be to wrap the Apache Collections CircularFifoBuffer with a generic class.  For example:

public LimitedSizeQueue<ElementType> implements Queue<ElementType>
{
    private int maxSize;
    private CircularFifoBuffer storageArea;

    public LimitedSizeQueue(final int maxSize)
    {
        if (maxSize > 0)
        {
            this.maxSize = maxSize;
            storateArea = new CircularFifoBuffer(maxSize);
        }
        else
        {
            throw new IllegalArgumentException("blah blah blah");
        }
    }

    ... implement the Queue interface using the CircularFifoBuffer class
}


问:
Name rows with a character vector in R
I want to name the rows of a dataframe with a column of a second dataframe.
The dataframe to be named has the same number of rows as the dataframe I want to use for naming. Meaning the name of the ith row of the first dataframe shouldhave the name of the ith value of the column of the second dataframe.

I already tried the following:

row.names(df1) <- as.character(df2[,1])
```

This results in all rows of df1 named after the first value in the vector of dataframe2.

答:
Try this.

#Sample data
df
  a b
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5

df2
  a
1 A
2 B
3 C
4 D
5 E

# Set row names
`rownames<-`(df, df2$a)

# Result
  a b
A 1 1
B 2 2
C 3 3
D 4 4
E 5 5
```

Title: Name rows with a character vector in R

Body:
I want to name the rows of a dataframe with a column of a second dataframe.
The dataframe to be named has the same number of rows as the dataframe I want to use for naming. Meaning the name of the ith row of the first dataframe shouldhave the name of the ith value of the column of the second dataframe.

I already tried the following:

row.names(df1) <- as.character(df2[,1])

This results in all rows of df1 named after the first value in the vector of dataframe2.


问:
"'this' is not available" in debug windows of Android Studio
I get this message in debug windows in Android Studio. This is not a static method, nor is it a class. What does it mean?

答: Inside Lambda block we can't evaluate the value of variables. Changing from the lambda expression to normal expression solved my issue</p>


问:
Why can't make_shared construct this object?
When trying to create a shared pointer to an object I come across this error

```<code>#include <iostream>
#include <vector>

class Foo {
public:
    std::shared_ptr<Foo> getFoo(int i) {
        auto foo = std::make_shared<Foo>(i);
        //auto foo = std::shared_ptr<Foo>(new Foo(i)); "works"
        return foo;
    }
protected:
    Foo(int i) {std::cout << "foo" << std::endl; }
};

int main(int argc, const char * argv[]) {

}
</code>```

I get the buildtime error

```<code> Static_assert failed due to requirement 'is_constructible<Foo, int &>::value' "Can't construct object in make_shared"
</code>```

I can avoid this by making the constructor private, But I would like to know why make_shared fails and and shared_ptr(new) works?

答:
@SombreroChicken provided a good answer why this is the case.
I want to provide a possible solution

```<code>#include <iostream>
#include <memory>

class Foo {
private:
    struct Token{};
public:
    std::shared_ptr<Foo> getFoo(int i) {
        auto foo = std::make_shared<Foo>(i, Token{});
        //auto foo = std::shared_ptr<Foo>(new Foo(i)); "works"
        return foo;
    }
    Foo(int i, Token) : Foo(i) {}

protected:
    Foo(int i) {std::cout << "foo" << std::endl; }
};

int main(int argc, const char * argv[]) {

}
</code>```

it works by making a public constructor that requires something only Foo can make, so anyone who received the 'token' can call the constructor

NOTE: passing such tag parameters may be better done as the first parameter, because

to be consistent with standard library

certain compilers (notably msvc) have special handling of it that improves compile times

Body:
When trying to create a shared pointer to an object I come across this error

#include <iostream>
#include <vector>

class Foo {
public:
    std::shared_ptr<Foo> getFoo(int i) {
        auto foo = std::make_shared<Foo>(i);
        //auto foo = std::shared_ptr<Foo>(new Foo(i)); "works"
        return foo;
    }
protected:
    Foo(int i) {std::cout << "foo" << std::endl; }
};

int main(int argc, const char * argv[]) {

}

I get the buildtime error

 Static_assert failed due to requirement 'is_constructible<Foo, int &>::value' "Can't construct object in make_shared"

I can avoid this by making the constructor private, But I would like to know why make_shared fails and and shared_ptr(new) works?

Body:
@SombreroChicken provided a good answer why this is the case.
I want to provide a possible solution

#include <iostream>
#include <memory>

class Foo {
private:
    struct Token{};
public:
    std::shared_ptr<Foo> getFoo(int i) {
        auto foo = std::make_shared<Foo>(i, Token{});
        //auto foo = std::shared_ptr<Foo>(new Foo(i)); "works"
        return foo;
    }
    Foo(int i, Token) : Foo(i) {}

protected:
    Foo(int i) {std::cout << "foo" << std::endl; }
};

int main(int argc, const char * argv[]) {

}

it works by making a public constructor that requires something only Foo can make, so anyone who received the 'token' can call the constructor

NOTE: passing such tag parameters may be better done as the first parameter, because

to be consistent with standard library

certain compilers (notably msvc) have special handling of it that improves compile times


问:
Which OpenGL version is most stable and currently used?
I've been thinking of making an additional wrapper for my project to use OpenGL rather then Allegro. I was not sure which OpenGL version to go for since I know that some computers cannot run recent versions such as v4.4. Also, I need a version which compiles no problem in Linux, Windows, Mac.</p>

答: Your safe bet is OpenGL 2.1, it needs to be supported by the driver on your target system though. OpenGL ES, used on several mobile platforms, is basically a simplified OpenGL 2, so even porting to those would be fairly easy. I highly recommend using libGlew as VJo said.</p>


问:
Tableau Query for using DateDiff and DateTrun at same time
I am new to tableau and i need to find all row set filter on basis of given time frame and then grouping the result of same minute.I can crack this problem in Sql as following

```<code>select count(x)
from table t
where datediff(30, min, date)
group by datepart(min, date)
</code>```

How to fix it in tableau ?

答:
I'm not really sure of what you want but this will give you the number of record per minute, for the records whose date is in the last 30 minutes:</p>

```<code>select datepart(MI, date) , COUNT(*) as NumberOfRecordsInMinute
from table t
where date >= dateADD(MI, -30, getdate())
group by datepart(MI, date)
order by datepart(MI, date)
</code>```

Body:
I am new to tableau and i need to find all row set filter on basis of given time frame and then grouping the result of same minute.I can crack this problem in Sql as following

select count(x)
from table t
where datediff(30, min, date)
group by datepart(min, date)

How to fix it in tableau ?

Body:
I'm not really sure of what you want but this will give you the number of record per minute, for the records whose date is in the last 30 minutes:

select datepart(MI, date) , COUNT(*) as NumberOfRecordsInMinute
from table t
where date >= dateADD(MI, -30, getdate())
group by datepart(MI, date)
order by datepart(MI, date)


问:
Having trouble with django.core.exceptions.SuspiciousFileOperation: The joined path
"python manage.py collectstatic" is not working, I think it might not work for any file and this fa-brands-400.eot happened to be the first file.

Error:

```<code>django.core.exceptions.SuspiciousFileOperation: The joined path (/Users/monica/music-emotion/static/webfonts/fa-brands-400.eot) is located outside of the base path component (/Users/monica/music-emotion/staticfiles)

</code>```

Is settings.py supposed to be located in root or can it be in a subdirectory?
My settings.py has this at the end-

```<code># Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/
STATICFILES_DIRS = (
  os.path.join('me_main/static'),
)

STATIC_URL = '/static/'

# Configure Django App for Heroku.
import django_heroku
django_heroku.settings(locals())

</code>```

答:
I had the same error I just removed

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

and it works fine now.

Body:
"python manage.py collectstatic" is not working, I think it might not work for any file and this fa-brands-400.eot happened to be the first file.

Error:

django.core.exceptions.SuspiciousFileOperation: The joined path (/Users/monica/music-emotion/static/webfonts/fa-brands-400.eot) is located outside of the base path component (/Users/monica/music-emotion/staticfiles)

Is settings.py supposed to be located in root or can it be in a subdirectory?
My settings.py has this at the end-

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/
STATICFILES_DIRS = (
  os.path.join('me_main/static'),
)

STATIC_URL = '/static/'

# Configure Django App for Heroku.
import django_heroku
django_heroku.settings(locals())


问:
Get greatest rows based on multiple columns in MySQL
I have a table that looks like:

```<code>id | title | value | language
---+-------+-------+---------
1  | a     | 1800  |    NULL
2  | a     | 1900  |    NULL
3  | b     | 1700  |    NULL
4  | b     | 1750  |    NULL
5  | b     | 1790  |    1
6  | c     | 1892  |    NULL
7  | c     | 1900  |    1
8  | c     | 1910  |    2
9  | d     | 3020  |    NULL
</code>```

Would like to have the following result:

```<code>id | title | value | language
---+-------+-------+---------
2  | a     | 1900  |    NULL
4  | b     | 1750  |    NULL
5  | b     | 1790  |    1
6  | c     | 1892  |    NULL
7  | c     | 1900  |    1
8  | c     | 1910  |    2
9  | d     | 3020  |    NULL
</code>```

The point is to select the greatest value in value column of every language of every title - greatest being the latest. Secondly, would like to avoid Aggregate functions like MAX, DISTINCT or GROUP-BY as I am building a MySQL View using the MERGE algorithm, and don't want to end up creating a temporary table (See the bottom section of https://dev.mysql.com/doc/refman/5.6/en/view-algorithms.html).

So far this works, but only returns greatest row per title:

```<code>SELECT t1.title
FROM table t1
LEFT OUTER JOIN table t2
  ON t1.title = t2.title
        AND t1.value < t2.value
WHERE t2.title IS NULL
</code>```

How can I create one that takes language into account like the results above? Thanx.

答:
This should give you what you want.</p>

```<code>SELECT t1.title, t1.value, t1.language
FROM [Table] t1
LEFT OUTER JOIN [Table] t2 ON
     t1.title = t2.title AND
     (IFNULL(t1.language, '') = IFNULL(t2.language, ''))
WHERE
     t1.value > t2.value;
</code>```

Body:
I have a table that looks like:

id | title | value | language
---+-------+-------+---------
1  | a     | 1800  |    NULL
2  | a     | 1900  |    NULL
3  | b     | 1700  |    NULL
4  | b     | 1750  |    NULL
5  | b     | 1790  |    1
6  | c     | 1892  |    NULL
7  | c     | 1900  |    1
8  | c     | 1910  |    2
9  | d     | 3020  |    NULL

Would like to have the following result:

id | title | value | language
---+-------+-------+---------
2  | a     | 1900  |    NULL
4  | b     | 1750  |    NULL
5  | b     | 1790  |    1
6  | c     | 1892  |    NULL
7  | c     | 1900  |    1
8  | c     | 1910  |    2
9  | d     | 3020  |    NULL

The point is to select the greatest value in value column of every language of every title - greatest being the latest. Secondly, would like to avoid Aggregate functions like MAX, DISTINCT or GROUP-BY as I am building a MySQL View using the MERGE algorithm, and don't want to end up creating a temporary table (See the bottom section of https://dev.mysql.com/doc/refman/5.6/en/view-algorithms.html).

So far this works, but only returns greatest row per title:

SELECT t1.title
FROM table t1
LEFT OUTER JOIN table t2
  ON t1.title = t2.title
        AND t1.value < t2.value
WHERE t2.title IS NULL

How can I create one that takes language into account like the results above? Thanx.

Body:
This should give you what you want.

SELECT t1.title, t1.value, t1.language
FROM [Table] t1
LEFT OUTER JOIN [Table] t2 ON
     t1.title = t2.title AND
     (IFNULL(t1.language, '') = IFNULL(t2.language, ''))
WHERE
     t1.value > t2.value;


问:
How get Id of element when i click on the element
I have 2 div(HTML) element and id is div1 and div2, When I click on any div then I want id of current div... idea?</p>

```<code><div id="div1"></div>
<div id="div2"></div>

<div id="displayid"></div>
</code>```

```<code>if(clickedDivId = this.id){
var div = document.getElementsByTagName("div");
var divCount = div.length;
var clickedDivId;
for (var i = 0; i < divCount; i += 1) {

    div[i].onclick = function(e) {
        clickedDivId = this.id;
        event.stopPropagation();

    };
}
}

document.getElementById("displayid").innerHTML = ??clickedDivId??how;
</code>```

答:
use this.id to get id of clicked div.

document.querySelectorAll('div')
    .forEach(div => {
        div.addEventListener('click', function() {
            console.log(this.id);
        })
    })```
<pre class="snippet-code-html lang-html prettyprint-override"><code><div id="div1">Div 1</div>
<div id="div2">Div 2</div>

<div id="displayid">Div 3</div></code>```

Body:
I have 2 div(HTML) element and id is div1 and div2, When I click on any div then I want id of current div... idea?

if(clickedDivId = this.id){
var div = document.getElementsByTagName("div");
var divCount = div.length;
var clickedDivId;
for (var i = 0; i < divCount; i += 1) {

    div[i].onclick = function(e) {
        clickedDivId = this.id;
        event.stopPropagation();

    };
}
}

document.getElementById("displayid").innerHTML = ??clickedDivId??how;

Body:
use this.id to get id of clicked div.

document.querySelectorAll('div')
    .forEach(div => {
        div.addEventListener('click', function() {
            console.log(this.id);
        })
    })

<div id="div1">Div 1</div>
<div id="div2">Div 2</div>

<div id="displayid">Div 3</div>


问:
Replicate a data frame by group
I have the following data frame:

```<code>df = structure(list(Group = c(1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3,
3), index = c(1, 2, 3, 4, 1, 2, 3, 4, 5, 6, 1, 2, 3)), row.names = c(NA,
-13L), class = c("tbl_df", "tbl", "data.frame"))
</code>```

I would like to replicate the column index according to the Group column, one time with each number appearing n consecutive times, and a second time all the numbers appear as a group n times, where n is the size of the group (similarly to rep versus rep with each).

So the output would look like this (lets look only at Group 1 because it is too long):

First option:

```<code>df = structure(list(Group = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1), index = c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4,
4, 4, 4)), row.names = c(NA, -16L), class = c("tbl_df", "tbl",
"data.frame"))
</code>```

Second option:

```<code>df = structure(list(Group = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1), index = c(1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1,
2, 3, 4)), row.names = c(NA, -16L), class = c("tbl_df", "tbl",
"data.frame"))
</code>```

How do I do this with group_by?

答:
You can use a combination of do and lapply to replicate the whole group

```<code>df %>% group_by(Group) %>%
  do(lapply(.,rep,times=nrow(.)) %>% as.data.frame())
df %>% group_by(Group) %>%
  do(lapply(.,rep,each=nrow(.)) %>% as.data.frame())
</code>```

Title: Replicate a data frame by group

Body:
I have the following data frame:

df = structure(list(Group = c(1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3,
3), index = c(1, 2, 3, 4, 1, 2, 3, 4, 5, 6, 1, 2, 3)), row.names = c(NA,
-13L), class = c("tbl_df", "tbl", "data.frame"))

I would like to replicate the column index according to the Group column, one time with each number appearing n consecutive times, and a second time all the numbers appear as a group n times, where n is the size of the group (similarly to rep versus rep with each).

So the output would look like this (lets look only at Group 1 because it is too long):

First option:

df = structure(list(Group = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1), index = c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4,
4, 4, 4)), row.names = c(NA, -16L), class = c("tbl_df", "tbl",
"data.frame"))

Second option:

df = structure(list(Group = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1), index = c(1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1,
2, 3, 4)), row.names = c(NA, -16L), class = c("tbl_df", "tbl",
"data.frame"))

How do I do this with group_by?

Body:
You can use a combination of do and lapply to replicate the whole group

df %>% group_by(Group) %>%
  do(lapply(.,rep,times=nrow(.)) %>% as.data.frame())
df %>% group_by(Group) %>%
  do(lapply(.,rep,each=nrow(.)) %>% as.data.frame())


问:
What are some widely used cryptography / hash libraries in C?
I've been working on cryptography implementations in C.
I am required to use hash a message using any one of the popular hash functions like SHA,MD5, etc.

In Java, there is a security library which takes care of these things.

But how do I do the same in C?

for example:
char *str = "this is a message";
char *hash = SHA(str);

Something of this sort. It would be of great help if some one can point me to some library which has already implemented these functions which i can call for my program.

Thanks!

答:
sphlib is an opensource library which provides optimized (but portable) implementations in C of many hash functions.

OpenSSL is a more generic cryptographic library, which is widely deployed and provides implementations of hash functions, too (less hash functions than sphlib, but it also includes other cryptographic primitives).


问:
The loop does not work for the second iteration

The loop does not run after the first time

```<code>class Solution {
    public static void main(String[] argh) {
        int sum = 0;
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        for (int i = 0; i < t; i++) {
            int a = in.nextInt();
            int b = in.nextInt();
            int n = in.nextInt();

            for (i = 0; i < n; i++) {
                sum += (a + (Math.pow(2, i) * b));
                System.out.print(sum + " ");
            }

        }
        in.close();
    }
}
</code>```

Input   2  0 2 10  5 3 5

Your Output (stdout)  2 6 14 30 62 126 254 510 1022 2046

Expected Output Download  2 6 14 30 62 126 254 510 1022 2046 8 14 26 50
  98

答:
In your code, both inner and outer loops use the same variable. After the first inner loop execution finish value of i = 10. So in the second iteration of the outer loop, the loop condition fails. Since i = 10, t = 2 </p>

```<code>public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    int t = in.nextInt();
    for (int i = 0; i < t; i++) {
        //sum should be here
        int sum = 0;

        int a = in.nextInt();
        int b = in.nextInt();
        int n = in.nextInt();

        for (int j = 0; j < n; j++) {
            sum += (a + (Math.pow(2, j) * b));
            System.out.print(sum + " ");
        }

    }
    in.close();
}
</code>```

<p>And also variable sum should be declared inside the outer loop to reset count after the inner loop execution is completed once. </p>

Body:
The loop does not run after the first time

class Solution {
    public static void main(String[] argh) {
        int sum = 0;
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        for (int i = 0; i < t; i++) {
            int a = in.nextInt();
            int b = in.nextInt();
            int n = in.nextInt();

            for (i = 0; i < n; i++) {
                sum += (a + (Math.pow(2, i) * b));
                System.out.print(sum + " ");
            }

        }
        in.close();
    }
}

Input   2  0 2 10  5 3 5

Your Output (stdout)  2 6 14 30 62 126 254 510 1022 2046

Expected Output Download  2 6 14 30 62 126 254 510 1022 2046 8 14 26 50
  98

Body:
In your code, both inner and outer loops use the same variable. After the first inner loop execution finish value of i = 10. So in the second iteration of the outer loop, the loop condition fails. Since i = 10, t = 2

public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    int t = in.nextInt();
    for (int i = 0; i < t; i++) {
        //sum should be here
        int sum = 0;

        int a = in.nextInt();
        int b = in.nextInt();
        int n = in.nextInt();

        for (int j = 0; j < n; j++) {
            sum += (a + (Math.pow(2, j) * b));
            System.out.print(sum + " ");
        }

    }
    in.close();
}

And also variable sum should be declared inside the outer loop to reset count after the inner loop execution is completed once.


问:
How to fix - Promise { <pending> } in Node.js and use it as variable anywhere
I'm accessing google API for converting coordinates into detailed objects using node-geocoder library from npmjs. Everything went well and I'm getting the expected object from geocoder API. The problem started the moment when I thought of using the data outside the promise function. I want to use the values outside the promise/async-await function.
Below is the code I've tried, Pls take a look and help me. TIA...

```<code>function goecoderPromiseFunction() {

    return new Promise(function (resolve, reject) {

        geocoder.reverse({ lat: 45.767, lon: 4.833 })

            .then(data => {

                cityName = data[0].city;

                resolve(cityName);

            })
            .catch(err => {

                console.log(err);

            });
    });

}

async function app() {

    var a = await goecoderPromiseFunction();
    return a;

}

var a = app();

console.log("a->", a);
</code>```

I expect the variable "a" should print the city name "Lyon", but it prints

a-> Promise { < pending > }

答:
An asynchronous function actually returns a promise that 'resolves' to the function's return value. You are therefore assigning a promise to the value of a. If you are in the global scope, you obviously cannot use async/await so you need to use either a self-executing async function or you need to run

```<code>a.then(data => console.log('a->', data));
</code>```

to get what you are looking for.

Find out more about async functions here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

Body:
I'm accessing google API for converting coordinates into detailed objects using node-geocoder library from npmjs. Everything went well and I'm getting the expected object from geocoder API. The problem started the moment when I thought of using the data outside the promise function. I want to use the values outside the promise/async-await function.
Below is the code I've tried, Pls take a look and help me. TIA...

function goecoderPromiseFunction() {

    return new Promise(function (resolve, reject) {

        geocoder.reverse({ lat: 45.767, lon: 4.833 })

            .then(data => {

                cityName = data[0].city;

                resolve(cityName);

            })
            .catch(err => {

                console.log(err);

            });
    });

}

async function app() {

    var a = await goecoderPromiseFunction();
    return a;

}

var a = app();

console.log("a->", a);

I expect the variable "a" should print the city name "Lyon", but it prints

a-> Promise { < pending > }

Body:
An asynchronous function actually returns a promise that 'resolves' to the function's return value. You are therefore assigning a promise to the value of a. If you are in the global scope, you obviously cannot use async/await so you need to use either a self-executing async function or you need to run

a.then(data => console.log('a->', data));

to get what you are looking for.

Find out more about async functions here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function


问:
Failed to install or launch the test runner error in jenkins
I encountered an error in Jenkins while executing a job named UITestsJob which runs UI tests. This UITestsJob is pipeline job and its definition is in Jenkinsfile. Additioanally, I have another jobs in the same workspace and sometimes these jobs starts at the same time with this UITestsJob. I guess when two jobs starts at the same time I get this error:

UITestsJob-Runner.app encountered an error (Failed to install or launch the test runner. (Underlying error: The operation couldn’t be completed. (DVTCoreSimulatorAdditionsErrorDomain error 0.)))

I have already done cleaning step at the beginning of the pipeline, which is

```<code>sh 'rm -rf output'
sh 'rm -rf "$DERIVED_DATA_DIR"'
sh 'rm -rf "$PWD/Pods/"'
sh 'rm -rf ~/Library/Caches/CocoaPods
</code>```

And after that I am installing pod with cleaning caches which is

```<code> sh '(pod cache clean --all --verbose && pod install)'
</code>```

After I get error, I run these commands from terminal

```<code>xcrun simctl shutdown all
xcrun simctl erase all
</code>```

And this error is gone. I do not want to include this into my pipeline file, since at the same time there could be other simulators that are running for other jobs. I can not find any solution other than erasing all of the simulators from terminal. Is there any permanent solution for this error? How can I fix this issue and after I can always trust that my job is running without breaking down because of 'TestRunner' error?

答: I guess finally we come up with a solution for this issue, we installed Build Blocker Plugin to jenkins and add jobs to wait for each other to start execution, hope this helps you too! Thank you!

Body:
I encountered an error in Jenkins while executing a job named UITestsJob which runs UI tests. This UITestsJob is pipeline job and its definition is in Jenkinsfile. Additioanally, I have another jobs in the same workspace and sometimes these jobs starts at the same time with this UITestsJob. I guess when two jobs starts at the same time I get this error:

UITestsJob-Runner.app encountered an error (Failed to install or launch the test runner. (Underlying error: The operation couldn’t be completed. (DVTCoreSimulatorAdditionsErrorDomain error 0.)))

I have already done cleaning step at the beginning of the pipeline, which is

sh 'rm -rf output'
sh 'rm -rf "$DERIVED_DATA_DIR"'
sh 'rm -rf "$PWD/Pods/"'
sh 'rm -rf ~/Library/Caches/CocoaPods

And after that I am installing pod with cleaning caches which is

 sh '(pod cache clean --all --verbose && pod install)'

After I get error, I run these commands from terminal

xcrun simctl shutdown all
xcrun simctl erase all

And this error is gone. I do not want to include this into my pipeline file, since at the same time there could be other simulators that are running for other jobs. I can not find any solution other than erasing all of the simulators from terminal. Is there any permanent solution for this error? How can I fix this issue and after I can always trust that my job is running without breaking down because of 'TestRunner' error?


问:
In PySpark is there any way to dynamically register UDF using functions of Python class given at run time?
I am new to Python, so apologizing if there is any mistake in my approach

I have a scenario where client can provide its custom Python functions and wants to register them as UDF in PySpark.

Based on my initial understanding, I am expecting a function which return a dictionary of function name and function definition, from importing module and calling this method at run time.

Random Custom function class example

    class CustomFuntions():

        def reverse_statement(self, statement):
            temp_list = statement.split(' ')
            temp_list.reverse()
            return ' '.join(temp_list)

        def get_functions(self):
            return {
                "rev_statement" : self.reverse_statement
                }

```

<p>Registration Part</p>

<pre class="lang-py prettyprint-override"><code>    from importlib import import_module

    class UDFRegistration():

        def __init__(self, spark):
            self.spark = spark

        def get_function_dict(self, module_name, class_name, method_name):
            instance = getattr(import_module(module_name), class_name)
            func_dict = getattr(instance(), method_name)()
            return func_dict

        def register_udf(self, module_name, class_name, method_name):
            f_dict = self.get_function_dict(module_name, class_name, method_name)

            for func_name, func_def in f_dict.items():
                self.spark.udf.register(func_name, func_def)
</code>```

Main class

    CUST_UDF_MODULE = 'custom_functions'
    CUST_UDF_CLASS = 'CustomFuntions'
    CUST_UDF_METHOD = 'get_functions'

    if __name__ == '__main__':

        spark = SparkSession.builder.getOrCreate()
        spark.sparkContext.addPyFile("custom_functions.py")

        print('Registering custom UDF')
        udf_reg = UDFRegistration(spark)
        udf_reg.register_udf(CUST_UDF_MODULE, CUST_UDF_CLASS, CUST_UDF_METHOD)

        spark.sql('select rev_statement(\'!!! WORLD HELLO\')').show()

```

<p>Getting following error while running it on local machine.</p>

<p><em>Console Output</em></p>

<pre class="lang-none prettyprint-override"><code>To adjust logging level use sc.setLogLevel(newLevel). For SparkR, use setLogLevel(newLevel).
Registering custom UDF
19/05/30 12:17:57 ERROR Executor: Exception in task 0.0 in stage 0.0 (TID 0)
java.io.IOException: Cannot run program "python": CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(Unknown Source)
    at org.apache.spark.api.python.PythonWorkerFactory.createSimpleWorker(PythonWorkerFactory.scala:155)
    at org.apache.spark.api.python.PythonWorkerFactory.create(PythonWorkerFactory.scala:97)
    at org.apache.spark.SparkEnv.createPythonWorker(SparkEnv.scala:117)
    at org.apache.spark.api.python.BasePythonRunner.compute(PythonRunner.scala:108)
    at org.apache.spark.sql.execution.python.BatchEvalPythonExec.evaluate(BatchEvalPythonExec.scala:77)
    at org.apache.spark.sql.execution.python.EvalPythonExec$$anonfun$doExecute$1.apply(EvalPythonExec.scala:127)
    at org.apache.spark.sql.execution.python.EvalPythonExec$$anonfun$doExecute$1.apply(EvalPythonExec.scala:89)
    at org.apache.spark.rdd.RDD$$anonfun$mapPartitions$1$$anonfun$apply$23.apply(RDD.scala:801)
    at org.apache.spark.rdd.RDD$$anonfun$mapPartitions$1$$anonfun$apply$23.apply(RDD.scala:801)
    at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:52)
    at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:324)
    at org.apache.spark.rdd.RDD.iterator(RDD.scala:288)
    at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:52)
    at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:324)
    at org.apache.spark.rdd.RDD.iterator(RDD.scala:288)
    at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:52)
    at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:324)
    at org.apache.spark.rdd.RDD.iterator(RDD.scala:288)
    at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:52)
    at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:324)
    at org.apache.spark.rdd.RDD.iterator(RDD.scala:288)
    at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:90)
    at org.apache.spark.scheduler.Task.run(Task.scala:121)
    at org.apache.spark.executor.Executor$TaskRunner$$anonfun$10.apply(Executor.scala:408)
    at org.apache.spark.util.Utils$.tryWithSafeFinally(Utils.scala:1360)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:414)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(Unknown Source)
    at java.lang.ProcessImpl.start(Unknown Source)
    ... 30 more
19/05/30 12:17:57 WARN TaskSetManager: Lost task 0.0 in stage 0.0 (TID 0, localhost, executor driver): java.io.IOException: Cannot run program "python": CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(Unknown Source)
    at org.apache.spark.api.python.PythonWorkerFactory.createSimpleWorker(PythonWorkerFactory.scala:155)
    at org.apache.spark.api.python.PythonWorkerFactory.create(PythonWorkerFactory.scala:97)
    at org.apache.spark.SparkEnv.createPythonWorker(SparkEnv.scala:117)
    at org.apache.spark.api.python.BasePythonRunner.compute(PythonRunner.scala:108)
    at org.apache.spark.sql.execution.python.BatchEvalPythonExec.evaluate(BatchEvalPythonExec.scala:77)
    at org.apache.spark.sql.execution.python.EvalPythonExec$$anonfun$doExecute$1.apply(EvalPythonExec.scala:127)
    at org.apache.spark.sql.execution.python.EvalPythonExec$$anonfun$doExecute$1.apply(EvalPythonExec.scala:89)
    at org.apache.spark.rdd.RDD$$anonfun$mapPartitions$1$$anonfun$apply$23.apply(RDD.scala:801)
    at org.apache.spark.rdd.RDD$$anonfun$mapPartitions$1$$anonfun$apply$23.apply(RDD.scala:801)
    at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:52)
    at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:324)
    at org.apache.spark.rdd.RDD.iterator(RDD.scala:288)
    at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:52)
    at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:324)
    at org.apache.spark.rdd.RDD.iterator(RDD.scala:288)
    at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:52)
    at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:324)
    at org.apache.spark.rdd.RDD.iterator(RDD.scala:288)
    at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:52)
    at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:324)
    at org.apache.spark.rdd.RDD.iterator(RDD.scala:288)
    at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:90)
    at org.apache.spark.scheduler.Task.run(Task.scala:121)
    at org.apache.spark.executor.Executor$TaskRunner$$anonfun$10.apply(Executor.scala:408)
    at org.apache.spark.util.Utils$.tryWithSafeFinally(Utils.scala:1360)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:414)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(Unknown Source)
    at java.lang.ProcessImpl.start(Unknown Source)
    ... 30 more

19/05/30 12:17:57 ERROR TaskSetManager: Task 0 in stage 0.0 failed 1 times; aborting job
Traceback (most recent call last):
  File "C:\Users\USERNAME\eclipse-workspace\Workspace\SparkMain.py", line 31, in <module>
    spark.sql('select rev_statement(\'!!! WORLD HELLO\')').show()
  File "C:\Users\USERNAME\AppData\Local\Continuum\anaconda3\lib\site-packages\pyspark\sql\dataframe.py", line 378, in show
    print(self._jdf.showString(n, 20, vertical))
  File "C:\Users\USERNAME\AppData\Local\Continuum\anaconda3\lib\site-packages\py4j\java_gateway.py", line 1257, in __call__
    answer, self.gateway_client, self.target_id, self.name)
  File "C:\Users\USERNAME\AppData\Local\Continuum\anaconda3\lib\site-packages\pyspark\sql\utils.py", line 63, in deco
    return f(*a, **kw)
  File "C:\Users\USERNAME\AppData\Local\Continuum\anaconda3\lib\site-packages\py4j\protocol.py", line 328, in get_return_value
    format(target_id, ".", name), value)
py4j.protocol.Py4JJavaError: An error occurred while calling o30.showString.
: org.apache.spark.SparkException: Job aborted due to stage failure: Task 0 in stage 0.0 failed 1 times, most recent failure: Lost task 0.0 in stage 0.0 (TID 0, localhost, executor driver): java.io.IOException: Cannot run program "python": CreateProcess error=2, The system cannot find the file specified

    at java.lang.ProcessBuilder.start(Unknown Source)

    at org.apache.spark.api.python.PythonWorkerFactory.createSimpleWorker(PythonWorkerFactory.scala:155)

    at org.apache.spark.api.python.PythonWorkerFactory.create(PythonWorkerFactory.scala:97)

    at org.apache.spark.SparkEnv.createPythonWorker(SparkEnv.scala:117)

    at org.apache.spark.api.python.BasePythonRunner.compute(PythonRunner.scala:108)

    at org.apache.spark.sql.execution.python.BatchEvalPythonExec.evaluate(BatchEvalPythonExec.scala:77)

    at org.apache.spark.sql.execution.python.EvalPythonExec$$anonfun$doExecute$1.apply(EvalPythonExec.scala:127)

    at org.apache.spark.sql.execution.python.EvalPythonExec$$anonfun$doExecute$1.apply(EvalPythonExec.scala:89)

    at org.apache.spark.rdd.RDD$$anonfun$mapPartitions$1$$anonfun$apply$23.apply(
</code>```

答:
As highlighted by user10938362 the issue was on system configuration side & not on code side

After setting PYSPARK_PYTHON in PyDev interpreter of eclipse, code executed successfully

PYSPARK_PYTHON =  C:\Users\User\anaconda3\python.exe

Body:
I am new to Python, so apologizing if there is any mistake in my approach

I have a scenario where client can provide its custom Python functions and wants to register them as UDF in PySpark.

Based on my initial understanding, I am expecting a function which return a dictionary of function name and function definition, from importing module and calling this method at run time.

Random Custom function class example

    class CustomFuntions():

        def reverse_statement(self, statement):
            temp_list = statement.split(' ')
            temp_list.reverse()
            return ' '.join(temp_list)

        def get_functions(self):
            return {
                "rev_statement" : self.reverse_statement
                }

Registration Part

    from importlib import import_module

    class UDFRegistration():

        def __init__(self, spark):
            self.spark = spark

        def get_function_dict(self, module_name, class_name, method_name):
            instance = getattr(import_module(module_name), class_name)
            func_dict = getattr(instance(), method_name)()
            return func_dict

        def register_udf(self, module_name, class_name, method_name):
            f_dict = self.get_function_dict(module_name, class_name, method_name)

            for func_name, func_def in f_dict.items():
                self.spark.udf.register(func_name, func_def)

Main class

    CUST_UDF_MODULE = 'custom_functions'
    CUST_UDF_CLASS = 'CustomFuntions'
    CUST_UDF_METHOD = 'get_functions'

    if __name__ == '__main__':

        spark = SparkSession.builder.getOrCreate()
        spark.sparkContext.addPyFile("custom_functions.py")

        print('Registering custom UDF')
        udf_reg = UDFRegistration(spark)
        udf_reg.register_udf(CUST_UDF_MODULE, CUST_UDF_CLASS, CUST_UDF_METHOD)

        spark.sql('select rev_statement(\'!!! WORLD HELLO\')').show()

Getting following error while running it on local machine.

Console Output

To adjust logging level use sc.setLogLevel(newLevel). For SparkR, use setLogLevel(newLevel).
Registering custom UDF
19/05/30 12:17:57 ERROR Executor: Exception in task 0.0 in stage 0.0 (TID 0)
java.io.IOException: Cannot run program "python": CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(Unknown Source)
    at org.apache.spark.api.python.PythonWorkerFactory.createSimpleWorker(PythonWorkerFactory.scala:155)
    at org.apache.spark.api.python.PythonWorkerFactory.create(PythonWorkerFactory.scala:97)
    at org.apache.spark.SparkEnv.createPythonWorker(SparkEnv.scala:117)
    at org.apache.spark.api.python.BasePythonRunner.compute(PythonRunner.scala:108)
    at org.apache.spark.sql.execution.python.BatchEvalPythonExec.evaluate(BatchEvalPythonExec.scala:77)
    at org.apache.spark.sql.execution.python.EvalPythonExec$$anonfun$doExecute$1.apply(EvalPythonExec.scala:127)
    at org.apache.spark.sql.execution.python.EvalPythonExec$$anonfun$doExecute$1.apply(EvalPythonExec.scala:89)
    at org.apache.spark.rdd.RDD$$anonfun$mapPartitions$1$$anonfun$apply$23.apply(RDD.scala:801)
    at org.apache.spark.rdd.RDD$$anonfun$mapPartitions$1$$anonfun$apply$23.apply(RDD.scala:801)
    at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:52)
    at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:324)
    at org.apache.spark.rdd.RDD.iterator(RDD.scala:288)
    at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:52)
    at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:324)
    at org.apache.spark.rdd.RDD.iterator(RDD.scala:288)
    at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:52)
    at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:324)
    at org.apache.spark.rdd.RDD.iterator(RDD.scala:288)
    at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:52)
    at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:324)
    at org.apache.spark.rdd.RDD.iterator(RDD.scala:288)
    at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:90)
    at org.apache.spark.scheduler.Task.run(Task.scala:121)
    at org.apache.spark.executor.Executor$TaskRunner$$anonfun$10.apply(Executor.scala:408)
    at org.apache.spark.util.Utils$.tryWithSafeFinally(Utils.scala:1360)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:414)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(Unknown Source)
    at java.lang.ProcessImpl.start(Unknown Source)
    ... 30 more
19/05/30 12:17:57 WARN TaskSetManager: Lost task 0.0 in stage 0.0 (TID 0, localhost, executor driver): java.io.IOException: Cannot run program "python": CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(Unknown Source)
    at org.apache.spark.api.python.PythonWorkerFactory.createSimpleWorker(PythonWorkerFactory.scala:155)
    at org.apache.spark.api.python.PythonWorkerFactory.create(PythonWorkerFactory.scala:97)
    at org.apache.spark.SparkEnv.createPythonWorker(SparkEnv.scala:117)
    at org.apache.spark.api.python.BasePythonRunner.compute(PythonRunner.scala:108)
    at org.apache.spark.sql.execution.python.BatchEvalPythonExec.evaluate(BatchEvalPythonExec.scala:77)
    at org.apache.spark.sql.execution.python.EvalPythonExec$$anonfun$doExecute$1.apply(EvalPythonExec.scala:127)
    at org.apache.spark.sql.execution.python.EvalPythonExec$$anonfun$doExecute$1.apply(EvalPythonExec.scala:89)
    at org.apache.spark.rdd.RDD$$anonfun$mapPartitions$1$$anonfun$apply$23.apply(RDD.scala:801)
    at org.apache.spark.rdd.RDD$$anonfun$mapPartitions$1$$anonfun$apply$23.apply(RDD.scala:801)
    at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:52)
    at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:324)
    at org.apache.spark.rdd.RDD.iterator(RDD.scala:288)
    at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:52)
    at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:324)
    at org.apache.spark.rdd.RDD.iterator(RDD.scala:288)
    at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:52)
    at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:324)
    at org.apache.spark.rdd.RDD.iterator(RDD.scala:288)
    at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:52)
    at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:324)
    at org.apache.spark.rdd.RDD.iterator(RDD.scala:288)
    at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:90)
    at org.apache.spark.scheduler.Task.run(Task.scala:121)
    at org.apache.spark.executor.Executor$TaskRunner$$anonfun$10.apply(Executor.scala:408)
    at org.apache.spark.util.Utils$.tryWithSafeFinally(Utils.scala:1360)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:414)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(Unknown Source)
    at java.lang.ProcessImpl.start(Unknown Source)
    ... 30 more

19/05/30 12:17:57 ERROR TaskSetManager: Task 0 in stage 0.0 failed 1 times; aborting job
Traceback (most recent call last):
  File "C:\Users\USERNAME\eclipse-workspace\Workspace\SparkMain.py", line 31, in <module>
    spark.sql('select rev_statement(\'!!! WORLD HELLO\')').show()
  File "C:\Users\USERNAME\AppData\Local\Continuum\anaconda3\lib\site-packages\pyspark\sql\dataframe.py", line 378, in show
    print(self._jdf.showString(n, 20, vertical))
  File "C:\Users\USERNAME\AppData\Local\Continuum\anaconda3\lib\site-packages\py4j\java_gateway.py", line 1257, in __call__
    answer, self.gateway_client, self.target_id, self.name)
  File "C:\Users\USERNAME\AppData\Local\Continuum\anaconda3\lib\site-packages\pyspark\sql\utils.py", line 63, in deco
    return f(*a, **kw)
  File "C:\Users\USERNAME\AppData\Local\Continuum\anaconda3\lib\site-packages\py4j\protocol.py", line 328, in get_return_value
    format(target_id, ".", name), value)
py4j.protocol.Py4JJavaError: An error occurred while calling o30.showString.
: org.apache.spark.SparkException: Job aborted due to stage failure: Task 0 in stage 0.0 failed 1 times, most recent failure: Lost task 0.0 in stage 0.0 (TID 0, localhost, executor driver): java.io.IOException: Cannot run program "python": CreateProcess error=2, The system cannot find the file specified

    at java.lang.ProcessBuilder.start(Unknown Source)

    at org.apache.spark.api.python.PythonWorkerFactory.createSimpleWorker(PythonWorkerFactory.scala:155)

    at org.apache.spark.api.python.PythonWorkerFactory.create(PythonWorkerFactory.scala:97)

    at org.apache.spark.SparkEnv.createPythonWorker(SparkEnv.scala:117)

    at org.apache.spark.api.python.BasePythonRunner.compute(PythonRunner.scala:108)

    at org.apache.spark.sql.execution.python.BatchEvalPythonExec.evaluate(BatchEvalPythonExec.scala:77)

    at org.apache.spark.sql.execution.python.EvalPythonExec$$anonfun$doExecute$1.apply(EvalPythonExec.scala:127)

    at org.apache.spark.sql.execution.python.EvalPythonExec$$anonfun$doExecute$1.apply(EvalPythonExec.scala:89)

    at org.apache.spark.rdd.RDD$$anonfun$mapPartitions$1$$anonfun$apply$23.apply(


问:
Invoking a step function synchronously from a lambda function
I have a lambda function which is triggered by a FIFO SQS. I only want one instance of the function running. The function invokes a state machine and the state machine takes longer to finish than the lambda function. I want the lambda function to finish only after the step function has completed it's execution.</p>

答:
AWS Step Functions are only invoked asynchronously. A state machine can run for up to 1 year so synchronous invocation is not possible. Depending on your workflow you might find Activities useful.

Activities are an AWS Step Functions feature that enables you to have
  a task in your state machine where the work is performed by a worker
  that can be hosted on Amazon Elastic Compute Cloud (Amazon EC2),
  Amazon Elastic Container Service (Amazon ECS), mobile
  devices—basically anywhere.