问:
Single quote in SQL
I get this error message:

```<code>03-30 00:51:50.373 30267-30267/com.example.text E/JavaBinder: !!! FAILED BINDER TRANSACTION !!!
03-30 00:51:54.273 30267-30267/com.example.text E/SQLiteLog: (1) near "hand": syntax error
03-30 00:51:54.273 30267-30267/com.example.text E/AndroidRuntime: FATAL EXCEPTION: main
                                                                  Process: com.example.text, PID: 30267
                                                                  android.database.sqlite.SQLiteException: near "hand": syntax error (code 1): , while compiling: INSERT INTO Result (Date, Name, Content) VALUES ('20170330_005134', 'Next, adjust the collar.', 'Next, adjust the collar.

                                                                  First, put your left'hand into miyatsukuchi

                                                                  and adjust the collar on the right side

                                                                  so It lies ﬂat. Then, adjust the left collar

                                                                  so it lies ﬂat. There should be a ﬁst-sized space between

                                                                  the back of your neck and the collar.');
                                                                      at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                      at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1093)
                                                                      at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:670)
                                                                      at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                      at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
                                                                      at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
                                                                      at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1798)
                                                                      at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1729)
                                                                      at seeto.fyp.textcamera.ResultHelper.addResult(ResultHelper.java:101)
                                                                      at seeto.fyp.textcamera.ResultActivity$2.onClick(ResultActivity.java:74)
                                                                      at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:162)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                      at android.os.Looper.loop(Looper.java:145)
                                                                      at android.app.ActivityThread.main(ActivityThread.java:5938)
                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                      at java.lang.reflect.Method.invoke(Method.java:372)
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
</code>```

I know the problem occurs on the single quote in the text, while I need the single quote (or double quote) in some case.

I have tried content = content.replace("'", "\'");, but it seems doesn't work.

How can I insert the data to the database with the single and double quote?

```<code>public void addResult(String name, String date, String content) {
    database.execSQL("INSERT INTO " + tbName + " (" + keyDate + ", " + keyName + ", " + keyContent + ") VALUES ('" + date + "', '" + name + "', '" + content + "');");
}
</code>```

答:
Typically, you can escape single quotes by doubling them up and double quotes by inserting a backslash.

So, using your example:

```<code>content = content.replace("'", "''")
content = content.replace("\"", "\\\"")
</code>```

Title: Single quote in SQL

Body:
I get this error message:

03-30 00:51:50.373 30267-30267/com.example.text E/JavaBinder: !!! FAILED BINDER TRANSACTION !!!
03-30 00:51:54.273 30267-30267/com.example.text E/SQLiteLog: (1) near "hand": syntax error
03-30 00:51:54.273 30267-30267/com.example.text E/AndroidRuntime: FATAL EXCEPTION: main
                                                                  Process: com.example.text, PID: 30267
                                                                  android.database.sqlite.SQLiteException: near "hand": syntax error (code 1): , while compiling: INSERT INTO Result (Date, Name, Content) VALUES ('20170330_005134', 'Next, adjust the collar.', 'Next, adjust the collar.

                                                                  First, put your left'hand into miyatsukuchi

                                                                  and adjust the collar on the right side

                                                                  so It lies ﬂat. Then, adjust the left collar

                                                                  so it lies ﬂat. There should be a ﬁst-sized space between

                                                                  the back of your neck and the collar.');
                                                                      at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                      at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1093)
                                                                      at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:670)
                                                                      at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                      at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
                                                                      at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
                                                                      at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1798)
                                                                      at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1729)
                                                                      at seeto.fyp.textcamera.ResultHelper.addResult(ResultHelper.java:101)
                                                                      at seeto.fyp.textcamera.ResultActivity$2.onClick(ResultActivity.java:74)
                                                                      at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:162)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                      at android.os.Looper.loop(Looper.java:145)
                                                                      at android.app.ActivityThread.main(ActivityThread.java:5938)
                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                      at java.lang.reflect.Method.invoke(Method.java:372)
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)

I know the problem occurs on the single quote in the text, while I need the single quote (or double quote) in some case.

I have tried content = content.replace("'", "\'");, but it seems doesn't work.

How can I insert the data to the database with the single and double quote?

public void addResult(String name, String date, String content) {
    database.execSQL("INSERT INTO " + tbName + " (" + keyDate + ", " + keyName + ", " + keyContent + ") VALUES ('" + date + "', '" + name + "', '" + content + "');");
}

Body:
Typically, you can escape single quotes by doubling them up and double quotes by inserting a backslash.

So, using your example:

content = content.replace("'", "''")
content = content.replace("\"", "\\\"")


来源: stackexchange

问:
Using PhantomJS in Ruby on Rails application
I would like to use PhantomJS as part of my main application lifecycle to take screenshots of a remote URL submitted by the user.

I'm familiar with using Poltergeist in conjunction with Capybara/Rspec. But how would I go about initializing the page object manually?

答:
I really like phantomjs in Rails app.

My suggest are using:

```<code>watir (https://github.com/watir/watir)
phantomjs (http://phantomjs.org/download.html)
</code>```

You can take a screen shot very easy by using follow this: http://watir.github.io/docs/screenshots/

And if you want to use Page, i thinks you should see PageObject in here: https://github.com/watir/watir/wiki/Page-Objects

ContentLicense: CC BY-SA 3.0

Body:
I really like phantomjs in Rails app.

My suggest are using:

watir (https://github.com/watir/watir)
phantomjs (http://phantomjs.org/download.html)

You can take a screen shot very easy by using follow this: http://watir.github.io/docs/screenshots/

And if you want to use Page, i thinks you should see PageObject in here: https://github.com/watir/watir/wiki/Page-Objects


问:
jQuery selector based on deep children text content
This is an example of HTML I am working with.

```<code><html>
    <div class="parent-div">
        <ul>
            <li class="title">Color</li>
            <li>Red</li>
            <li class="title">Shape</li>
            <li>Squared</li>
        </ul>
    </div>
</html>
</code>```

I would like to convert the current HTML in the following list-item

Color: Red

Shape: Squared

And this is what I tried so far:

$("html > div.parent-div > ul > li:contains('Color')")

However it throws an exception:

Uncaught DOMException: Failed to execute 'querySelectorAll' on
  'Document': html > div.parent-div > ul > li:contains('Color')'
  is not a valid selector.

答:
I dont know the reasoning of this, but you can get it with something like this:</p>

```<code>var output = {};
var output_index = 0;
var last_title = "";

var allLi = $("#parent-div li");
var $this = null;

for(var i = 0; i < allLi.length; i++){
   $this = $(this);
   if($this).hasClass(title){
      last_title = $this.html();
   } else {
      output[last_title] = $this.html();
   }
})
</code>```

Body:
This is an example of HTML I am working with.

Color

Red

Shape

Squared

I would like to convert the current HTML in the following list-item

Color: Red

Shape: Squared

And this is what I tried so far:

$("html > div.parent-div > ul > li:contains('Color')")

However it throws an exception:

Uncaught DOMException: Failed to execute 'querySelectorAll' on
  'Document': html > div.parent-div > ul > li:contains('Color')'
  is not a valid selector.

Body:
I dont know the reasoning of this, but you can get it with something like this:

var output = {};
var output_index = 0;
var last_title = "";

var allLi = $("#parent-div li");
var $this = null;

for(var i = 0; i < allLi.length; i++){
   $this = $(this);
   if($this).hasClass(title){
      last_title = $this.html();
   } else {
      output[last_title] = $this.html();
   }
})


问:
using sed or awk to remove a "checkbox" character
I have a script that has a variable that might contain some weird characters:  ✔. I need to remove them but, honestly, I don't even know where to begin to match those characters. I can't copy and paste them into my script, they just show up as ?? ?. How can a match those characters with sed or awk? I don't have the ability to use perl or php or anything much beyond sed or awk due to system availability.

答:
```<code>$ cat file
some weird characters:  ✔. I need to remove
second line of some weird characters:  ✔. I need to remove

$ tr -c -d '[:print:][:space:]' < file
some weird characters:  . I need to remove
second line of some weird characters:  . I need to remove
</code>```


问:
How do I map a column to uppercase in .NET 4.5 C# Entity Framework 6 using both Oracle and SQL Server?
I'm using C#, .NET 4.5 and Entity Framework 6 in my project. It uses both Oracle and SQL Server, depending on the installation at the client.

The approach is database-first, as this database existed already by the time we decided to change the ORM from NHibernate to Entity Framework 6.

The mapping looks like this:

```<code>ToTable(schema + ".Motorista");
Property(x => x.Criacao).HasColumnName("criacao").IsOptional();
</code>```

The table and column names are all in PascalCase in the mapping, which works fine with SQL Server but, in Oracle, all the names are UpperCase which causes an error:

ORA-00942: table or view does not exist

If I manually make it uppercase, then it works fine on Oracle. But I can't do that because of compatibility to SQL Server.

How can I say to Entity Framework to uppercase all the names when using Oracle?

Can I use conventions in this scenario?

答:
Consider a table called "Person" with a column called "Name" in SQL Server but in oracle the table is called "PERSON" with a column called "NAME".

We were able to use our models generated against sql server on our oracle database by adding the following code to the DBContext classe's OnModelCreating

```<code>modelBuilder.Entity<Person>()
  .HasEntitySetName("Person")
  .ToTable("PERSON");

modelBuilder.Entity<Person>()
  .Property(t => t.Name)
  .HasColumnName("NAME");
</code>```

Body:
I'm using C#, .NET 4.5 and Entity Framework 6 in my project. It uses both Oracle and SQL Server, depending on the installation at the client.

The approach is database-first, as this database existed already by the time we decided to change the ORM from NHibernate to Entity Framework 6.

The mapping looks like this:

ToTable(schema + ".Motorista");
Property(x => x.Criacao).HasColumnName("criacao").IsOptional();

The table and column names are all in PascalCase in the mapping, which works fine with SQL Server but, in Oracle, all the names are UpperCase which causes an error:

ORA-00942: table or view does not exist

If I manually make it uppercase, then it works fine on Oracle. But I can't do that because of compatibility to SQL Server.

How can I say to Entity Framework to uppercase all the names when using Oracle?

Can I use conventions in this scenario?

Body:
Consider a table called "Person" with a column called "Name" in SQL Server but in oracle the table is called "PERSON" with a column called "NAME".

We were able to use our models generated against sql server on our oracle database by adding the following code to the DBContext classe's OnModelCreating

modelBuilder.Entity<Person>()
  .HasEntitySetName("Person")
  .ToTable("PERSON");

modelBuilder.Entity<Person>()
  .Property(t => t.Name)
  .HasColumnName("NAME");


问:
How to generate id number like 'xxx0000', 'xxx0001'?
This is my table :

```<code>|   ID    |
| xxx0000 |
| xxx0001 |
| xxx0002 |
</code>```

i want to make my id pattern like that, but i dont know how to generate it?

答:
You have two different pieces of data, so make two different columns.

```<code>ID              INT            NOT NULL   AUTO_INCREMENT,
SomethingElse   SomeOtherType  NOT NULL
</code>```

What SomethingElse is named and what data type it is would be up to you.  xxx doesn't tell us much.

If both of these things combined make up your primary key, you can use a composite key of multiple columns:

```<code>PRIMARY KEY (SomethingElse, ID)
</code>```

The same integrity checks for any primary key will continue to apply, the database will simply check both columns for combined uniqueness.

At this point you have the data you want, now it's just a matter of displaying it how you want.  Whether you do that in SQL or in PHP is up to you.  Whether you want the application to see them as a single value or see the underlying different values, also up to you.

Selecting them as a single value from SQL could be simple enough.  Something like:

```<code>SELECT CONCAT(SomethingElse, ID) AS ID FROM ...
</code>```

If you always want those padded zeroes then this question will help.  Other string manipulations you might want to do would also be tackled one at a time (and each could result in its own Stack Overflow question if you need assistance with it).

But the basic idea here is that what you have is a composite value.  In a relational database you would generally store the underlying values and perform the composition when querying the data.

Body:
This is my table :

|   ID    |
| xxx0000 |
| xxx0001 |
| xxx0002 |

i want to make my id pattern like that, but i dont know how to generate it?

Body:
You have two different pieces of data, so make two different columns.

ID              INT            NOT NULL   AUTO_INCREMENT,
SomethingElse   SomeOtherType  NOT NULL

What SomethingElse is named and what data type it is would be up to you.  xxx doesn't tell us much.

If both of these things combined make up your primary key, you can use a composite key of multiple columns:

PRIMARY KEY (SomethingElse, ID)

The same integrity checks for any primary key will continue to apply, the database will simply check both columns for combined uniqueness.

At this point you have the data you want, now it's just a matter of displaying it how you want.  Whether you do that in SQL or in PHP is up to you.  Whether you want the application to see them as a single value or see the underlying different values, also up to you.

Selecting them as a single value from SQL could be simple enough.  Something like:

SELECT CONCAT(SomethingElse, ID) AS ID FROM ...

If you always want those padded zeroes then this question will help.  Other string manipulations you might want to do would also be tackled one at a time (and each could result in its own Stack Overflow question if you need assistance with it).

But the basic idea here is that what you have is a composite value.  In a relational database you would generally store the underlying values and perform the composition when querying the data.


问:
Can I write files with HTML5/JS?
I wonder if there is any way I can write to files from HTML5/JS? In the broswer ...</p>

答:
As far as I know, you can't write to files from HTML5, because giving a web page access to the user's files would be a security risk.

If you just need to store some data so your page can access it later, HTML5 does have something called Web Storage which can do that.

Or you could store the data in cookies (if it's very small) or on the server.

ContentLicense: CC BY-SA 2.5

Title: Can I write files with HTML5/JS?


问:
Spring integration DSL CPU usage
We use Spring integration DSL pretty heavily in our app and am currently troubleshooting high CPU usage. Looking into the threads that are consuming high CPU, noticed that spring integration call stack is too deep, more than 500+ lines.  Our spring integration flows are long and complex and speak for the deep stack,  but am trying to see if that can be a potential cause for the high CPU usage. Any thoughts on how spring integration DSL can impact CPU will help.

Thanks
Zafer

答: The deep stack is unlikely to be the problem; I suggest you profile your application with YourKit or similar, to see where the time is being spent.</p>

Title: Spring integration DSL CPU usage


问:
How to display actual window size (width and height) automatically?
I have a simple code, which by clicking shows the actual size of window (width and height) but I want to do this without clicking, for example when I change the size of window i want to changed the size automatically and displayed.
thanks.</p>

```<code><!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">show the window size</button>
<p id="demo"></p>
<script>
function myFunction() {
    var w = window.innerWidth;
    var h = window.innerHeight;
    document.getElementById("demo").innerHTML = "Width: " + w + "<br>Height: " + h;
}
</script>
</body>
</html>
</code>```

答:
You could do this.</p>

```<code>window.onresize = myFunction;
</code>```

Body:
I have a simple code, which by clicking shows the actual size of window (width and height) but I want to do this without clicking, for example when I change the size of window i want to changed the size automatically and displayed.
thanks.

<!DOCTYPE html>

<button onclick="myFunction()">show the window size</button>

Body:
You could do this.

window.onresize = myFunction;


问:
Use Elastic Scale package within Azure function
I would like to use the Microsoft.Azure.SqlDatabase.ElasticScale.Client package from within my Azure function.

I've added a project.json as follows:

```<code>{
  "frameworks": {
    "net452":{
      "dependencies": {
        "Microsoft.Azure.SqlDatabase.ElasticScale.Client": "1.3.3"
      }
    }
   }
}
</code>```

Function:

```<code>#r "System.Data"
#r "Newtonsoft.Json"

using System.Net;
using Newtonsoft.Json;
using System.Data.SqlClient;
using Microsoft.Azure.SqlDatabase.ElasticScale.ShardManagement;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
    ShardMapManager shardMapManager;
    return req.CreateResponse(HttpStatusCode.OK);
}
</code>```

I get the following errors:

```<code>error CS0234: The type or namespace name 'SqlDatabase' does not exist in the namespace 'Microsoft.Azure' (are you missing an assembly reference?)
error CS0246: The type or namespace name 'ShardMapManager' could not be found (are you missing a using directive or an assembly reference?)
</code>```

答:
You are missing the #r statement to reference external assembly

The Microsoft.Azure.SqlDatabase.ElasticScale.Client package installs a Microsoft.Azure.SqlDatabase.ElasticScale.Client.dll, so then try:

#r "Microsoft.Azure.SqlDatabase.ElasticScale.Client.dll" or

#r "bin\Microsoft.Azure.SqlDatabase.ElasticScale.Client.dll"

(I've always has issues with the path).

Still, I would recommend not using the CSX scripting approach and instead use precompiled binaries using the technique described here. Then you get the full benefit of C# and you won't have to deal with adding #r statement to include the assembly.

Body:
I would like to use the Microsoft.Azure.SqlDatabase.ElasticScale.Client package from within my Azure function.

I've added a project.json as follows:

{
  "frameworks": {
    "net452":{
      "dependencies": {
        "Microsoft.Azure.SqlDatabase.ElasticScale.Client": "1.3.3"
      }
    }
   }
}

Function:

#r "System.Data"
#r "Newtonsoft.Json"

using System.Net;
using Newtonsoft.Json;
using System.Data.SqlClient;
using Microsoft.Azure.SqlDatabase.ElasticScale.ShardManagement;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
    ShardMapManager shardMapManager;
    return req.CreateResponse(HttpStatusCode.OK);
}

I get the following errors:

error CS0234: The type or namespace name 'SqlDatabase' does not exist in the namespace 'Microsoft.Azure' (are you missing an assembly reference?)
error CS0246: The type or namespace name 'ShardMapManager' could not be found (are you missing a using directive or an assembly reference?)


答:
just add following function for this</p>

```<code>window.onresize = function(event) {
    myFunction();
};
</code>```

Title: How to display actual window size (width and height) automatically?

Body:
just add following function for this

window.onresize = function(event) {
    myFunction();
};


问:
How to align input forms in HTML
I'm new to HTML and I'm trying to learn how to use forms.

The biggest issue I am having so far is aligning the forms. Here is an example of my current HTML file:

```<code><form>
 First Name:<input type="text" name="first"><br />
 Last Name:<input type="text" name="last"><br />
 Email:<input type="text" name="email"><br />
</form>
</code>```

The problem with this is, the field box after 'Email' is drastically different in terms of spacing compared to first, and last name. What is the 'proper' way to make it so that they 'line-up' essentially?

I am trying to practice good form and syntax...a lot of people might do this with CSS I am not sure, I have only learned the very basics of HTML so far.

答:
Well for the very basics you can try aligning them in the table. However the use of table is bad for layout since table is meant for contents.

What you can use is CSS floating techniques.

.styleform label{float:left;}
.styleform input{margin-left:200px;} /* this gives space for the label on the left */
.styleform .clear{clear:both;} /* prevent elements from stacking weirdly */```
<pre class="snippet-code-html lang-html prettyprint-override"><code><div class="styleform">
  <form>
    <label>First Name:</label><input type="text" name="first" />
    <div class="clear"></div>
    <label>Last Name:</label><input type="text" name="first" />
    <div class="clear"></div>
    <label>Email:</label><input type="text" name="first" />
    <div class="clear"></div>
  </form>
</div></code>```

An elaborate article I wrote can be found answering the question of IE7 float problem: IE7 float right problems

Title: How to align input forms in HTML

Body:
I'm new to HTML and I'm trying to learn how to use forms.

The biggest issue I am having so far is aligning the forms. Here is an example of my current HTML file:

<form>
 First Name:<input type="text" name="first">

 Last Name:<input type="text" name="last">

 Email:<input type="text" name="email">

</form>

The problem with this is, the field box after 'Email' is drastically different in terms of spacing compared to first, and last name. What is the 'proper' way to make it so that they 'line-up' essentially?

I am trying to practice good form and syntax...a lot of people might do this with CSS I am not sure, I have only learned the very basics of HTML so far.

Body:
Well for the very basics you can try aligning them in the table. However the use of table is bad for layout since table is meant for contents.

What you can use is CSS floating techniques.

.styleform label{float:left;}
.styleform input{margin-left:200px;} /* this gives space for the label on the left */
.styleform .clear{clear:both;} /* prevent elements from stacking weirdly */

<div class="styleform">
  <form>
    <label>First Name:</label><input type="text" name="first" />
    <div class="clear"></div>
    <label>Last Name:</label><input type="text" name="first" />
    <div class="clear"></div>
    <label>Email:</label><input type="text" name="first" />
    <div class="clear"></div>
  </form>
</div>

An elaborate article I wrote can be found answering the question of IE7 float problem: IE7 float right problems

ContentLicense: CC BY-SA 4.0


问:
Connecting networks by adding edge between same items in the networks
I am studying cellular pathways and I wanted to know how could I use the networkx to show how networks which share same elements are connected.

```<code> HH_signaling = ['GLI1', 'PTCH1', 'PTCH2', 'WNT5A', 'HHIP1', 'MYCN', 'CCND1', 'CCND2', 'BCL2', 'CFLAR', 'FOXF1', 'FOXL1', 'PRDM1', 'JAG2', 'GREM1']
    Wnt_signaling = ['GLI1', 'PTCH1', 'WNT5A', 'HHIP1', 'MYCN', 'CCND1','WNT7A','WNT2','CDK1','CK1']
    Proliferation_signaling = ['GLI1', 'CCNDA', 'BMP4', 'BMP7', 'MTOC2', 'CCND1']

    g = nx.Graph()
    def link_networks(N1, N2, N3, N4=None, N5=None, N6=None, N7=None, N8=None, N9=None):
        for i in N1:
            for i in N2:
                for i in N3:
                    g.add_edge(N1[i],N2[i],N3[i])

link_networks(HH_signaling, Wnt_signaling, Proliferation_signaling)

TypeError: list indices must be integers or slices, not str
</code>```

答:
networkx doesn't have the notion of a hyper node (nor does any other network analysis package that I know) so there is no simple way to recreate your drawing. There are also a few other technical limitations (nodes need to have different names (AFAIK)) that make the naive approach for the graph generation difficult.

To me it seems, that you are trying to re-invent the wheel. Don't you just want a Venn diagram showing the overlap between the different pathways?
Something like:

Code:

```<code>import venn_wordcloud; reload(venn_wordcloud)

HH = ['GLI1', 'PTCH1', 'PTCH2', 'WNT5A', 'HHIP1', 'MYCN', 'CCND1', 'CCND2', 'BCL2', 'CFLAR', 'FOXF1', 'FOXL1', 'PRDM1', 'JAG2', 'GREM1']
Wnt = ['GLI1', 'PTCH1', 'WNT5A', 'HHIP1', 'MYCN', 'CCND1', 'WNT7A', 'WNT2', 'CDK1', 'CK1']
CC = ['GLI1', 'CCNDA', 'BMP4', 'BMP7', 'MTOC2', 'CCND1']

venn_wordcloud.venn3_wordcloud([set(HH), set(Wnt), set(CC)],
                               set_labels=['Hedgehog', 'Wnt', 'Cell Cycle'])
</code>```

The code for venn_wordcloud is available on my github:
https://github.com/paulbrodersen/venn_wordcloud.

Apart from numpy and matplotlib (which you should have installed before installing networkx), it depends on matplotlib-venn and wordcloud, which can be installed via pip:

```<code>pip install matplotlib-venn wordcloud
</code>```

Body:
I am studying cellular pathways and I wanted to know how could I use the networkx to show how networks which share same elements are connected.

 HH_signaling = ['GLI1', 'PTCH1', 'PTCH2', 'WNT5A', 'HHIP1', 'MYCN', 'CCND1', 'CCND2', 'BCL2', 'CFLAR', 'FOXF1', 'FOXL1', 'PRDM1', 'JAG2', 'GREM1']
    Wnt_signaling = ['GLI1', 'PTCH1', 'WNT5A', 'HHIP1', 'MYCN', 'CCND1','WNT7A','WNT2','CDK1','CK1']
    Proliferation_signaling = ['GLI1', 'CCNDA', 'BMP4', 'BMP7', 'MTOC2', 'CCND1']

    g = nx.Graph()
    def link_networks(N1, N2, N3, N4=None, N5=None, N6=None, N7=None, N8=None, N9=None):
        for i in N1:
            for i in N2:
                for i in N3:
                    g.add_edge(N1[i],N2[i],N3[i])

link_networks(HH_signaling, Wnt_signaling, Proliferation_signaling)

TypeError: list indices must be integers or slices, not str

Body:
networkx doesn't have the notion of a hyper node (nor does any other network analysis package that I know) so there is no simple way to recreate your drawing. There are also a few other technical limitations (nodes need to have different names (AFAIK)) that make the naive approach for the graph generation difficult.

To me it seems, that you are trying to re-invent the wheel. Don't you just want a Venn diagram showing the overlap between the different pathways?
Something like:

Code:

import venn_wordcloud; reload(venn_wordcloud)

HH = ['GLI1', 'PTCH1', 'PTCH2', 'WNT5A', 'HHIP1', 'MYCN', 'CCND1', 'CCND2', 'BCL2', 'CFLAR', 'FOXF1', 'FOXL1', 'PRDM1', 'JAG2', 'GREM1']
Wnt = ['GLI1', 'PTCH1', 'WNT5A', 'HHIP1', 'MYCN', 'CCND1', 'WNT7A', 'WNT2', 'CDK1', 'CK1']
CC = ['GLI1', 'CCNDA', 'BMP4', 'BMP7', 'MTOC2', 'CCND1']

venn_wordcloud.venn3_wordcloud([set(HH), set(Wnt), set(CC)],
                               set_labels=['Hedgehog', 'Wnt', 'Cell Cycle'])

The code for venn_wordcloud is available on my github:
https://github.com/paulbrodersen/venn_wordcloud.

Apart from numpy and matplotlib (which you should have installed before installing networkx), it depends on matplotlib-venn and wordcloud, which can be installed via pip:

pip install matplotlib-venn wordcloud


问:
Dynamically use different class depending on current namespace
I would like my class to inherit methods off of its parent, I've done this by extending the class. But i would also like to instantiate a class using the basename(without namespace) and have the correct class be instantiate, chosen based on the current namespace and then fall back to a 'common' class if it doesn't exist.

example

```<code><?php

namespace common\models {

    class Account extends Model{}
    class Orders extends Model{}

    class Customer extends Model{

        use common\models\Account;

        public static function getAccountClass(){
            echo get_class(new Account());
        }
        public static function getOrdersClass(){
            echo get_class(new Order());
        }
    }
}

namespace frontend\models {

    class Account extends common\models\Account {}

    class Customer extends common\models\customer {

    }
}
</code>```

I would like the common\models\Customer::getAccountClass() to output common\models\Account

and

frontend\models\Customer::getAccountClass() to output frontend\models\Account

but as frontend\models\Orders doesn't exist frontend\models\Customer::getOrdersClass() will still output common\models\Orders

答:
There is no "right" way to solve it. However you could do something like this

Add a component or a helper or even add it to the construct of your initial class( depends how much you need to use it ):

```<code>public static function getClass($class)
{
    if (class_exists('frontend' . '\\' . $class)) {
        return frontend . '\\' . $class;
    } else  {
        return $class;
    }
}
</code>```

And then you just can do something like this

```<code>public static function getAccountClass(){
    return self::getClass(Account::class);
}
</code>```

Body:
I would like my class to inherit methods off of its parent, I've done this by extending the class. But i would also like to instantiate a class using the basename(without namespace) and have the correct class be instantiate, chosen based on the current namespace and then fall back to a 'common' class if it doesn't exist.

example

<?php

namespace common\models {

    class Account extends Model{}
    class Orders extends Model{}

    class Customer extends Model{

        use common\models\Account;

        public static function getAccountClass(){
            echo get_class(new Account());
        }
        public static function getOrdersClass(){
            echo get_class(new Order());
        }
    }
}

namespace frontend\models {

    class Account extends common\models\Account {}

    class Customer extends common\models\customer {

    }
}

I would like the common\models\Customer::getAccountClass() to output common\models\Account

and

frontend\models\Customer::getAccountClass() to output frontend\models\Account

but as frontend\models\Orders doesn't exist frontend\models\Customer::getOrdersClass() will still output common\models\Orders

Body:
There is no "right" way to solve it. However you could do something like this

Add a component or a helper or even add it to the construct of your initial class( depends how much you need to use it ):

public static function getClass($class)
{
    if (class_exists('frontend' . '\\' . $class)) {
        return frontend . '\\' . $class;
    } else  {
        return $class;
    }
}

And then you just can do something like this

public static function getAccountClass(){
    return self::getClass(Account::class);
}


问:
Python : easy way to do geometric mean in python?
I wonder is there any easy way to do geometric mean using python but without using python package. If there is not, is there any simple package to do geometric mean?</p>

答:
The formula of the gemetric mean is:

So you can easily write an algorithm like:

```<code><b>import numpy as np</b>

def geo_mean(iterable):
    a = <b>np.array(iterable)</b>
    return a.<b>prod()</b>**(1.0/len(a))</code>```

You do not have to use numpy for that, but it tends to perform operations on arrays faster than Python.  See this answer for why.

In case the chances of overflow are high, you can map the numbers to a log domain first, calculate the sum of these logs, then multiply by 1/n and finally calculate the exponent, like:

```<code>import numpy as np

def geo_mean<b>_overflow</b>(iterable):
    return np.exp(<b>np.log(iterable)</b>.<b>mean()</b>)</code>```

Body:
The formula of the gemetric mean is:

So you can easily write an algorithm like:

import numpy as np

def geo_mean(iterable):
    a = np.array(iterable)
    return a.prod()**(1.0/len(a))

You do not have to use numpy for that, but it tends to perform operations on arrays faster than Python.  See this answer for why.

In case the chances of overflow are high, you can map the numbers to a log domain first, calculate the sum of these logs, then multiply by 1/n and finally calculate the exponent, like:

import numpy as np

def geo_mean_overflow(iterable):
    return np.exp(np.log(iterable).mean())


问:
Find row-wise duplicates by groups for a specific item
I have a data set that has an E-type SKU or non-E-type SKU. My objective is to find whether in any order I sold E-type SKU along with similar non-E-type SKU.

For instance, if I sold E123 with W123, this will be considered duplicate. If I sold E123 with another E123, this won't be considered duplicate. If I sold W123 with W123, it won't be considered duplicate either. To summarize, I need to find a duplicate that has at least one E-type SKU and at least one non-E-type SKU.

The previous example I asked on SO is similar (Find row-wise duplicates by groups) but the challenge with that methodology is that when sub("^E","", Product) is applied, I no longer know whether I am comparing an E-type SKU with E-type SKU or an E-type SKU with non-E-type SKU.

Here's sample data:

```<code>dput(Test_File)
structure(list(Order = c(1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5,
5, 5), Product = c("E12960", "E12960", "E12960", "W12960", "W12960",
"W12960", "E1234", "E2345", "W2355", "A1235", "C-A-1234", "W-1234",
"A-1234", "C-1234")), .Names = c("Order", "Product"), row.names = c(NA,
14L), class = "data.frame")
</code>```

Here's expected output:

```<code>dput(Output_File)
structure(list(Order = c(1, 2, 3, 4, 5), Duplicate = c("N", "Y",
"N", "N", "N")), .Names = c("Order", "Duplicate"), row.names = c(NA,
5L), class = "data.frame")
</code>```

Here's my working code:

```<code>Test_File[,"ESKU_Present"]<-grepl("^E",Test_File$Product,ignore.case = TRUE)

#Strip initial identifiers
toMatch<-c("^E","^W","^A","^C-","^W-","C-A-","^A-")
Test_File[grepl(paste(toMatch,collapse="|"),Test_File$Product,ignore.case = TRUE),"New_Product_ID"]<-sub(paste(toMatch,collapse="|"), "", Test_File$Product)

Output <- Test_File %>%
  dplyr::group_by(Order) %>%
  #find those orders that have at least one ESKU and one non-ESKU
  mutate(Duplicate = (any(ESKU_Present ==c("FALSE")) & any(ESKU_Present == c("TRUE")))) %>%
  dplyr::filter(Duplicate == "TRUE") %>%
  dplyr::summarise(Final_Flag = any(duplicated(New_Product_ID))) %>%
  right_join(Test_File) %>%
  dplyr::select(Order, Final_Flag)

Output[is.na(Output$Final_Flag),"Final_Flag"]<-FALSE
Output<-dplyr::distinct(Output)
</code>```

I have two questions:

a) How do I use data.table to do what I am trying to do?
b) Is there any faster way to do this? I am asking this because on my original data set, which has about 1M rows, the above code is taking forever.

答:
We can do this in data.table.  Convert the 'data.frame' to 'data.table' (setDT(Test_File)), extract the non-numeric ('v1') and numeric substring by matching the numeric and non-numeric characters and replacing it with blanks, then grouped by 'Order' we check if there is any  non-numeric character that is an 'E' as well as the length of unique elements for non-numeric characters is greater than 1 and there is any duplicates characters for numeric part.

```<code>library(data.table)
setDT(Test_File)[, {
           v1 <- sub("\\d+", "", Product)
           v2 <- sub("\\D+", "", Product)
         .(Duplicate = any(v1=="E") & uniqueN(v1)>1 & anyDuplicated(v2)>0)} , Order]
#     Order Duplicate
#1:     1     FALSE
#2:     2      TRUE
#3:     3     FALSE
#4:     4     FALSE
#5:     5     FALSE
</code>```

NOTE: It may be better to keep as logical column ('Duplicate') instead of 'Y/N'

Body:
I have a data set that has an E-type SKU or non-E-type SKU. My objective is to find whether in any order I sold E-type SKU along with similar non-E-type SKU.

For instance, if I sold E123 with W123, this will be considered duplicate. If I sold E123 with another E123, this won't be considered duplicate. If I sold W123 with W123, it won't be considered duplicate either. To summarize, I need to find a duplicate that has at least one E-type SKU and at least one non-E-type SKU.

The previous example I asked on SO is similar (Find row-wise duplicates by groups) but the challenge with that methodology is that when sub("^E","", Product) is applied, I no longer know whether I am comparing an E-type SKU with E-type SKU or an E-type SKU with non-E-type SKU.

Here's sample data:

dput(Test_File)
structure(list(Order = c(1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5,
5, 5), Product = c("E12960", "E12960", "E12960", "W12960", "W12960",
"W12960", "E1234", "E2345", "W2355", "A1235", "C-A-1234", "W-1234",
"A-1234", "C-1234")), .Names = c("Order", "Product"), row.names = c(NA,
14L), class = "data.frame")

Here's expected output:

dput(Output_File)
structure(list(Order = c(1, 2, 3, 4, 5), Duplicate = c("N", "Y",
"N", "N", "N")), .Names = c("Order", "Duplicate"), row.names = c(NA,
5L), class = "data.frame")

Here's my working code:

Test_File[,"ESKU_Present"]<-grepl("^E",Test_File$Product,ignore.case = TRUE)

#Strip initial identifiers
toMatch<-c("^E","^W","^A","^C-","^W-","C-A-","^A-")
Test_File[grepl(paste(toMatch,collapse="|"),Test_File$Product,ignore.case = TRUE),"New_Product_ID"]<-sub(paste(toMatch,collapse="|"), "", Test_File$Product)

Output <- Test_File %>%
  dplyr::group_by(Order) %>%
  #find those orders that have at least one ESKU and one non-ESKU
  mutate(Duplicate = (any(ESKU_Present ==c("FALSE")) & any(ESKU_Present == c("TRUE")))) %>%
  dplyr::filter(Duplicate == "TRUE") %>%
  dplyr::summarise(Final_Flag = any(duplicated(New_Product_ID))) %>%
  right_join(Test_File) %>%
  dplyr::select(Order, Final_Flag)

Output[is.na(Output$Final_Flag),"Final_Flag"]<-FALSE
Output<-dplyr::distinct(Output)

I have two questions:

a) How do I use data.table to do what I am trying to do?
b) Is there any faster way to do this? I am asking this because on my original data set, which has about 1M rows, the above code is taking forever.

Body:
We can do this in data.table.  Convert the 'data.frame' to 'data.table' (setDT(Test_File)), extract the non-numeric ('v1') and numeric substring by matching the numeric and non-numeric characters and replacing it with blanks, then grouped by 'Order' we check if there is any  non-numeric character that is an 'E' as well as the length of unique elements for non-numeric characters is greater than 1 and there is any duplicates characters for numeric part.

library(data.table)
setDT(Test_File)[, {
           v1 <- sub("\\d+", "", Product)
           v2 <- sub("\\D+", "", Product)
         .(Duplicate = any(v1=="E") & uniqueN(v1)>1 & anyDuplicated(v2)>0)} , Order]
#     Order Duplicate
#1:     1     FALSE
#2:     2      TRUE
#3:     3     FALSE
#4:     4     FALSE
#5:     5     FALSE

NOTE: It may be better to keep as logical column ('Duplicate') instead of 'Y/N'


问:
Selecting from table when two fields satisfy OR condition. And if they satisfy AND condition on same fields select the ones with early date
```ID1  ID2     DATE        INITIAL   FINAL
20   328    2016/11/08    01         01
21   328    2016/11/11    01         01
53   766    2016/09/26    00         01
39   766    2016/11/25    01         00```

There are two entries for each ID2

In the above example I want to select records with INITIAL = 01 or FINAL = 01 but also when INITIAL = 01 and FINAL = 01 for same ID2, I want to select the one with earlier date

I am looking for a final set that looks like this

```ID1  ID2     DATE        INITIAL   FINAL
20   328    2016/11/08    01         01
53   766    2016/09/26    00         01
39   766    2016/11/25    01         00```

答:
Treat your 2 cases like separate queries then union them. The WITH is just me building dummy data. This will not address the case of an id2 having 01 initial and 01 final, and another row with 00 and 01 --- but that wasn't in your requirements. Nor will it address the issue if you have two 01 01 rows for the same id2 with the same min date, but again - that wasn't in the spec; I just wanted to state those things to be thorough.</p>

```<code>with records as
  (select 20 as id1, 328 as id2, to_date('2016-11-08','YYYY-MM-DD') as mydate, '01' as myinitial, '01' as myfinal from dual
   union all
   select 21 as id1, 328 as id2, to_date('2016-11-11','YYYY-MM-DD') as mydate, '01' as myinitial, '01' as myfinal from dual
   union all
   select 53 as id1, 766 as id2, to_date('2016-09-26','YYYY-MM-DD') as mydate, '00' as myinitial, '01' as myfinal from dual
   union all
   select 39 as id1, 766 as id2, to_date('2016-11-25','YYYY-MM-DD') as mydate, '01' as myinitial, '00' as myfinal from dual)

select *
from records
where (myinitial = '01' or myfinal = '01')
  and myinitial <> myfinal

union

select *
  from records  r
  where r.myinitial = '01' and r.myfinal = '01'
    and r.mydate =
        (select min(r2.mydate) from records r2
           where r2.myinitial='01' and r2.myfinal='01' and r2.id2 = r.id2)
  ;
</code>```

Body:
ID1  ID2     DATE        INITIAL   FINAL
20   328    2016/11/08    01         01
21   328    2016/11/11    01         01
53   766    2016/09/26    00         01
39   766    2016/11/25    01         00

There are two entries for each ID2

In the above example I want to select records with INITIAL = 01 or FINAL = 01 but also when INITIAL = 01 and FINAL = 01 for same ID2, I want to select the one with earlier date

I am looking for a final set that looks like this

ID1  ID2     DATE        INITIAL   FINAL
20   328    2016/11/08    01         01
53   766    2016/09/26    00         01
39   766    2016/11/25    01         00

Body:
Treat your 2 cases like separate queries then union them. The WITH is just me building dummy data. This will not address the case of an id2 having 01 initial and 01 final, and another row with 00 and 01 --- but that wasn't in your requirements. Nor will it address the issue if you have two 01 01 rows for the same id2 with the same min date, but again - that wasn't in the spec; I just wanted to state those things to be thorough.

with records as
  (select 20 as id1, 328 as id2, to_date('2016-11-08','YYYY-MM-DD') as mydate, '01' as myinitial, '01' as myfinal from dual
   union all
   select 21 as id1, 328 as id2, to_date('2016-11-11','YYYY-MM-DD') as mydate, '01' as myinitial, '01' as myfinal from dual
   union all
   select 53 as id1, 766 as id2, to_date('2016-09-26','YYYY-MM-DD') as mydate, '00' as myinitial, '01' as myfinal from dual
   union all
   select 39 as id1, 766 as id2, to_date('2016-11-25','YYYY-MM-DD') as mydate, '01' as myinitial, '00' as myfinal from dual)

select *
from records
where (myinitial = '01' or myfinal = '01')
  and myinitial <> myfinal

union

select *
  from records  r
  where r.myinitial = '01' and r.myfinal = '01'
    and r.mydate =
        (select min(r2.mydate) from records r2
           where r2.myinitial='01' and r2.myfinal='01' and r2.id2 = r.id2)
  ;


问:
Can't enable focus selector with anchors in css in Safari
I'm aware that certain elements can't accept a focus selector, such as list elements, but I thought that anchors were fine. Using the following code (I'm creating a pure css responsive drop-down menu), I can't make the hidden ul appear when clicking the anchor.

HTML:

```<code><ul>
  <li class="primarylist">
    <a href="#" class='category1anchor'>Text</a>
    <ul class="menudrop">
      <li class="secondarylist">One</li>
      <li class="secondarylist">Two</li>
      <li class="secondarylist">Three</li>
    </ul>
  </li>
</ul>
</code>```

CSS:

```<code>ul.menudrop {
  display: none;
  background-color: white;
}
li.primarylist a:focus + ul.menudrop {
  display: block;
}
</code>```

jsFiddle:
https://jsfiddle.net/kx0kex7x/

Change the jsFiddle selector from focus to hover, and it works perfectly, leaving me without any reason to doubt my syntax is correct. Using active only displays the ul for the duration of the mousedown. Any pointers? (I'm on Safari 10. Seems to work in the latest Chrome and FF fine).

Update

I've tried using the target selector with partial success. If I change my a.categor1anchor links to match newly created unique ids for each ul.menudrop, I can make the ul.menudrop appear on clicking the anchor. However, this is a responsive design and I can't seem to remove the target selector at other page sizes. For example, at a larger page size, I use the hover selector instead, but the last targeted ul.menudrop from a smaller screensize remains open at larger screen sizes. I can't see any way to clear target selectors for responsive designs. Inherit and Initial displays don't affect it.

答: After researching for the past few days and progressing down multiple avenues only to find a slightly convoluted hack is needed to solve every situation I'll find the menu in, I think it's turning out more elegant and clean to just use jQuery alongside my code to handle various dropdowns appearing, etc. I was trying to avoid it but I don't think CSS is still quite robust enough for menus of this type at the time of writing.

Body:
I'm aware that certain elements can't accept a focus selector, such as list elements, but I thought that anchors were fine. Using the following code (I'm creating a pure css responsive drop-down menu), I can't make the hidden ul appear when clicking the anchor.

HTML:

    Text

One

Two

Three

CSS:

ul.menudrop {
  display: none;
  background-color: white;
}
li.primarylist a:focus + ul.menudrop {
  display: block;
}

jsFiddle:
https://jsfiddle.net/kx0kex7x/

Change the jsFiddle selector from focus to hover, and it works perfectly, leaving me without any reason to doubt my syntax is correct. Using active only displays the ul for the duration of the mousedown. Any pointers? (I'm on Safari 10. Seems to work in the latest Chrome and FF fine).

Update

I've tried using the target selector with partial success. If I change my a.categor1anchor links to match newly created unique ids for each ul.menudrop, I can make the ul.menudrop appear on clicking the anchor. However, this is a responsive design and I can't seem to remove the target selector at other page sizes. For example, at a larger page size, I use the hover selector instead, but the last targeted ul.menudrop from a smaller screensize remains open at larger screen sizes. I can't see any way to clear target selectors for responsive designs. Inherit and Initial displays don't affect it.


问:
Typed Objective-C collection to Swift
I have a framework written in Objective-C that I'm importing into a Swift app, using a bridging header. One of my Obj-C classes has a typed collection property:

```<code>@property (strong) NSMutableArray <MyCustomObject*> *arrayOfMyCustomObjects;
</code>```

In the Swift app, that shows up as:

```<code>open var arrayOfMyCustomObjects: NSMutableArray!
</code>```

This page says that a non-mutable collection would retain the type, is this the only way?

// Objective-C

```<code>@property (nonatomic,strong) NSArray<NSDate *> *dueDates;
</code>```

// Swift

```<code>public var dueDates: [NSDate]
</code>```

// Objective-C

```<code>@property (nonatomic,strong) NSMutableArray<NSDate *> *dates;
</code>```

// Swift

```<code>public var dates: NSMutableArray
</code>```

答: NSMutableArray cannot be bridged to Swift in this way. This appears to answer your question: iOS 10 NSMutableDictionary and NSMutableArray not working as before

Title: Typed Objective-C collection to Swift

Body:
I have a framework written in Objective-C that I'm importing into a Swift app, using a bridging header. One of my Obj-C classes has a typed collection property:

@property (strong) NSMutableArray <MyCustomObject*> *arrayOfMyCustomObjects;

In the Swift app, that shows up as:

open var arrayOfMyCustomObjects: NSMutableArray!

This page says that a non-mutable collection would retain the type, is this the only way?

// Objective-C

@property (nonatomic,strong) NSArray<NSDate *> *dueDates;

// Swift

public var dueDates: [NSDate]

// Objective-C

@property (nonatomic,strong) NSMutableArray<NSDate *> *dates;

// Swift

public var dates: NSMutableArray


问:
SOAP-ERROR: Encoding: Violation of encoding rules?
Guys, I'm stuck, banging my head off the desk for the past few hours.

I am trying to consume a service, and I have 8 other functions that I call that are almost IDENTICAL in nature to this one, but this one, results in a 'SOAP-ERROR: Encoding: Violation of encoding rules' error.

Heres the function call (wsdl omitted for security):

```<code>    function CanLoadProduct($data){

    $client = new SoapClient('wsdl-url');

    $params = array('username'   => $this->username,
                    'password'  => $this->password,
                    'prod'      => $data['productid'],
                    'mdn'       => $data['mdn']);

    try {
        $reply = $client->__soapCall("CanLoadProduct", $params);
    } catch (Exception $e) {
        echo 'Error: ',  $e->getMessage(), "\n";
        print_r($params);
        die();
    }

    if( $reply['result'] == 1 ){
        return TRUE;        // 1 = true
    } else {
        return FALSE;
    }

}
</code>```

Ok so this function, connects to a webservice, the required elements are:
username, password, prod, mdn, all 4 of which I supply as part of the $params array. Username/Pass are defined earlier, and do work fine, as the other 8 functions consume the web service without any problems.

The $data[] array (that I pass to the function), contains:
$data['productid']
$data['mdn']
nothing else is used.

I am getting

```<code>SOAP-ERROR: Encoding: Violation of encoding rules
</code>```

for some unexplained reason, and Googling this error gets me nowhere.  Anyone else run into this?  Running PHP 5.2.9-2.  The strange thing is this is identical to this function which works 100%:

```<code>    function GetPIN($productid){

    $client = new SoapClient('wsdl-url');

    $params = array('username'  => $this->username,
                    'password'  => $this->password,
                    'prod'      => $productid);

    try {
        $reply = $client->__soapCall("GetPIN", $params);
    } catch (Exception $e) {
        echo 'Error: ',  $e->getMessage(), "\n";
        die();
    }
        return $reply;
}
</code>```

Here is the WSDL (should have posted this first):

```<code><?xml version="1.0" encoding="ISO-8859-1"?>
<definitions xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:tns="ready:test" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
    xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="ready:test">
<types>
<xsd:schema targetNamespace="ready:test"
>
 <xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/" />
 <xsd:import namespace="http://schemas.xmlsoap.org/wsdl/" />
</xsd:schema>
</types>
<message name="CanLoadProductRequest">
  <part name="username" type="xsd:string" />
  <part name="password" type="xsd:string" />
  <part name="prod" type="xsd:string" />
  <part name="mdn" type="xsd:string" />
  <part name="esn" type="xsd:string" /></message>
<message name="CanLoadProductResponse">
  <part name="result" type="xsd:int" /></message>
<portType name="CanLoadProductPortType">
  <operation name="CanLoadProduct">
    <input message="tns:CanLoadProductRequest"/>
    <output message="tns:CanLoadProductResponse"/>
  </operation>
</portType>

<binding name="CanLoadProductBinding" type="tns:CanLoadProductPortType">
  <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
  <operation name="CanLoadProduct">
    <soap:operation soapAction="{url-removed}" style="rpc"/>
    <input>
        <soap:body use="encoded" namespace=""
           encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
    </input>
    <output>
        <soap:body use="encoded" namespace=""
            encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
    </output>
  </operation>
</binding>
<service name="CanLoadProduct">
  <port name="CanLoadProductPort" binding="tns:CanLoadProductBinding">

    <soap:address location="{url-removed}"/>
  </port>
</service>
</definitions>
</code>```

答:
I had this issue in PhpStorm when doing unit tests. I disabled the wsdl cache and it worked:</p>

```<code>ini_set("soap.wsdl_cache_enabled", "0");
</code>```

Body:
Guys, I'm stuck, banging my head off the desk for the past few hours.

I am trying to consume a service, and I have 8 other functions that I call that are almost IDENTICAL in nature to this one, but this one, results in a 'SOAP-ERROR: Encoding: Violation of encoding rules' error.

Heres the function call (wsdl omitted for security):

    function CanLoadProduct($data){

    $client = new SoapClient('wsdl-url');

    $params = array('username'   => $this->username,
                    'password'  => $this->password,
                    'prod'      => $data['productid'],
                    'mdn'       => $data['mdn']);

    try {
        $reply = $client->__soapCall("CanLoadProduct", $params);
    } catch (Exception $e) {
        echo 'Error: ',  $e->getMessage(), "\n";
        print_r($params);
        die();
    }

    if( $reply['result'] == 1 ){
        return TRUE;        // 1 = true
    } else {
        return FALSE;
    }

}

Ok so this function, connects to a webservice, the required elements are:
username, password, prod, mdn, all 4 of which I supply as part of the $params array. Username/Pass are defined earlier, and do work fine, as the other 8 functions consume the web service without any problems.

The $data[] array (that I pass to the function), contains:
$data['productid']
$data['mdn']
nothing else is used.

I am getting

SOAP-ERROR: Encoding: Violation of encoding rules

for some unexplained reason, and Googling this error gets me nowhere.  Anyone else run into this?  Running PHP 5.2.9-2.  The strange thing is this is identical to this function which works 100%:

    function GetPIN($productid){

    $client = new SoapClient('wsdl-url');

    $params = array('username'  => $this->username,
                    'password'  => $this->password,
                    'prod'      => $productid);

    try {
        $reply = $client->__soapCall("GetPIN", $params);
    } catch (Exception $e) {
        echo 'Error: ',  $e->getMessage(), "\n";
        die();
    }
        return $reply;
}

Here is the WSDL (should have posted this first):

<?xml version="1.0" encoding="ISO-8859-1"?>
<definitions xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:tns="ready:test" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
    xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="ready:test">
<types>
<xsd:schema targetNamespace="ready:test"
>
 <xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/" />
 <xsd:import namespace="http://schemas.xmlsoap.org/wsdl/" />
</xsd:schema>
</types>
<message name="CanLoadProductRequest">
  <part name="username" type="xsd:string" />
  <part name="password" type="xsd:string" />
  <part name="prod" type="xsd:string" />
  <part name="mdn" type="xsd:string" />
  <part name="esn" type="xsd:string" /></message>
<message name="CanLoadProductResponse">
  <part name="result" type="xsd:int" /></message>
<portType name="CanLoadProductPortType">
  <operation name="CanLoadProduct">
    <input message="tns:CanLoadProductRequest"/>
    <output message="tns:CanLoadProductResponse"/>
  </operation>
</portType>

<binding name="CanLoadProductBinding" type="tns:CanLoadProductPortType">
  <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
  <operation name="CanLoadProduct">
    <soap:operation soapAction="{url-removed}" style="rpc"/>
    <input>
        <soap:body use="encoded" namespace=""
           encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
    </input>
    <output>
        <soap:body use="encoded" namespace=""
            encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
    </output>
  </operation>
</binding>
<service name="CanLoadProduct">
  <port name="CanLoadProductPort" binding="tns:CanLoadProductBinding">

    <soap:address location="{url-removed}"/>
  </port>
</service>
</definitions>

Body:
I had this issue in PhpStorm when doing unit tests. I disabled the wsdl cache and it worked:

ini_set("soap.wsdl_cache_enabled", "0");


问:
Make this code non blocking
I'm using the VSphere API, here are the lines that I'm dealing with:

```<code>    task = vm.PowerOff()
    while task.info.state not in [vim.TaskInfo.State.success, vim.TaskInfo.State.error]:
        time.sleep(1)
        log.info("task {} is running".format(task))
    log.ingo("task {} is done".format(task))
</code>```

The problem here is that this blocks the execution completely whilst the task is not finished. I would like the logging part to be ran "in parallel", so I can start other tasks.

I thought about creating a function that would accept a task as parameter, and poll the info.state attribute just like now, but how do I make this non blocking ?

EDIT: I'm using Python 2.7

答: You could use asyncio and create an event loop. You can use asyncio.async() to create an asynchronous task that won't block the event loop execution.

Title: Make this code non blocking

Body:
I'm using the VSphere API, here are the lines that I'm dealing with:

    task = vm.PowerOff()
    while task.info.state not in [vim.TaskInfo.State.success, vim.TaskInfo.State.error]:
        time.sleep(1)
        log.info("task {} is running".format(task))
    log.ingo("task {} is done".format(task))

The problem here is that this blocks the execution completely whilst the task is not finished. I would like the logging part to be ran "in parallel", so I can start other tasks.

I thought about creating a function that would accept a task as parameter, and poll the info.state attribute just like now, but how do I make this non blocking ?

EDIT: I'm using Python 2.7


问:
How do I get recently added Swiftlint Rules
I have the latest version of Swiftlint (via brew).  When I check github, I see that they have added a bunch of rules since the release, even a few a couple of days ago. How do I get those rules onto my mac? Do I have to uninstall the version I have and reinstall the same version? (will that even get the new rules?) Do I have to download the zip from github?</p>

答:
An alternative to brew update is to integrate SwiftLint using CocoaPods, and in that case you would update it with pod update.

I recommend the CocoaPods approach as it garanties that all co-developers get the same errors (and warnings), and avoids situations were one developer doesn't get any error, pushes code and confronts another developer to build errors.


问:
XML to PHP SOAP request
A SOAP request XML looks like the following:

```<code><soapenv:Body>
<net:GetAvailability>
    <net:request>
        <inh:UserCredentials>
           <inh:AgentID>**</inh:AgentID><inh:Password>**</inh:Password>
           <inh:Username>**</inh:Username>
        </inh:UserCredentials>

        <net:AccessCircuit>
            <arr:string>All</arr:string>
        </net:AccessCircuit>

        <net:RequestDetails xsi:type="net:TelephoneNumberAvailabilityRequest" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <net:PerformMPFACCheck>No</net:PerformMPFACCheck>
            <net:ProxyCLI>true</net:ProxyCLI>
            <net:TelephoneNumber>0121****</net:TelephoneNumber>
        </net:RequestDetails>
        <net:UserConsent>Yes</net:UserConsent>
    </net:request>
</net:GetAvailability>
</soapenv:Body>
</code>```

What I need to do is send this XML request using PHP SOAP however im not able to get this working. My current code is as follows:

```<code>$APIParameters = array(
        'request' => array(
            'UserCredentials' => array(
                'Username' => $this->apiUsername,
                'Password' => $this->apiPassword,
                'AgentID' => $this->apiResellerID,
            ),
            'AccessCircuit' => array(
                'string' => 'All'
            ),
            'UserConsent' => 'Yes',

        )
    );

    $APIParameters['request']['RequestDetails'] = new SoapParam( array('PerformMPFACCheck' => 'Yes', 'Postcode' => "****", 'ProxyCLI' => "true", 'TelephoneNumber' => '****'), "TelephoneNumberAvailabilityRequest");

    print_r($APIParameters);
    $apiResult = $SOAPClient->GetAvailability($APIParameters);
</code>```

The print_r() returns the following:

```<code>Array
(
[request] => Array
    (
        [UserCredentials] => Array
            (
                [Username] => ****
                [Password] => ****
                [AgentID] => ****
            )

        [AccessCircuit] => Array
            (
                [string] => All
            )

        [UserConsent] => Yes
        [RequestDetails] => SoapParam Object
            (
                [param_name] => TelephoneNumberAvailabilityRequest
                [param_data] => Array
                    (
                        [PerformMPFACCheck] => Yes
                        [Postcode] => ****
                        [ProxyCLI] => true
                        [TelephoneNumber] => 0121****
                    )

            )

    )
</code>```

)

Yet the SOAP request fails with a Fatal error:  Uncaught SoapFault exception: [a:InternalServiceFault]. What am I doing wrong?! Appreciate any help!

答:
first check that your wsld soap access granted using this code

```<code> <?php
// enter your url
    $client = new SoapClient('enteryoururl');
    var_dump($client->__getFunctions());
?>
</code>```

then check which function have in this url if you have access and your function list show your function then try this

Fatal error: Uncaught SoapFault exception: [ns1:Client.AUTH_1] Authentication Failed

Title: XML to PHP SOAP request

Body:
A SOAP request XML looks like the following:

<soapenv:Body>
<net:GetAvailability>
    <net:request>
        <inh:UserCredentials>
           <inh:AgentID>**</inh:AgentID><inh:Password>**</inh:Password>
           <inh:Username>**</inh:Username>
        </inh:UserCredentials>

        <net:AccessCircuit>
            <arr:string>All</arr:string>
        </net:AccessCircuit>

        <net:RequestDetails xsi:type="net:TelephoneNumberAvailabilityRequest" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <net:PerformMPFACCheck>No</net:PerformMPFACCheck>
            <net:ProxyCLI>true</net:ProxyCLI>
            <net:TelephoneNumber>0121****</net:TelephoneNumber>
        </net:RequestDetails>
        <net:UserConsent>Yes</net:UserConsent>
    </net:request>
</net:GetAvailability>
</soapenv:Body>

What I need to do is send this XML request using PHP SOAP however im not able to get this working. My current code is as follows:

$APIParameters = array(
        'request' => array(
            'UserCredentials' => array(
                'Username' => $this->apiUsername,
                'Password' => $this->apiPassword,
                'AgentID' => $this->apiResellerID,
            ),
            'AccessCircuit' => array(
                'string' => 'All'
            ),
            'UserConsent' => 'Yes',

        )
    );

    $APIParameters['request']['RequestDetails'] = new SoapParam( array('PerformMPFACCheck' => 'Yes', 'Postcode' => "****", 'ProxyCLI' => "true", 'TelephoneNumber' => '****'), "TelephoneNumberAvailabilityRequest");

    print_r($APIParameters);
    $apiResult = $SOAPClient->GetAvailability($APIParameters);

The print_r() returns the following:

Array
(
[request] => Array
    (
        [UserCredentials] => Array
            (
                [Username] => ****
                [Password] => ****
                [AgentID] => ****
            )

        [AccessCircuit] => Array
            (
                [string] => All
            )

        [UserConsent] => Yes
        [RequestDetails] => SoapParam Object
            (
                [param_name] => TelephoneNumberAvailabilityRequest
                [param_data] => Array
                    (
                        [PerformMPFACCheck] => Yes
                        [Postcode] => ****
                        [ProxyCLI] => true
                        [TelephoneNumber] => 0121****
                    )

            )

    )

)

Yet the SOAP request fails with a Fatal error:  Uncaught SoapFault exception: [a:InternalServiceFault]. What am I doing wrong?! Appreciate any help!

Body:
first check that your wsld soap access granted using this code

 <?php
// enter your url
    $client = new SoapClient('enteryoururl');
    var_dump($client->__getFunctions());
?>

then check which function have in this url if you have access and your function list show your function then try this

Fatal error: Uncaught SoapFault exception: [ns1:Client.AUTH_1] Authentication Failed


问:
Image source from disk Xamarin Forms
I have a Xamarin Forms app and what I want to do is to set the source of an Image View to a specific Image in my disk in UWP.
That is to say, I have the absolute path of my image which is in my disk, and I want to set the source of an Image View programmatically.
I have tried using

```<code>image.Source = ImageSource.FromFile(filePath);
</code>```

But it didn't work in UWP, although it does work in Android.

答:
A good way to do what you want is in your ViewModel to use a method where you put the path based on the OS

View

```<code><Image Source="{Binding ImagePath}" />
</code>```

ViewModel example

```<code>public string ImagePath
{
    get
    {
        return _path;
    }
    set
    {
        if (_path != value)
        {
            _path = value;
            OnPropertyChanged("ImagePath");
        }
    }
}
private string _path;
</code>```

When you set a path for an image you can use a function like that:

```<code>public static class OSHelpers {
    /// <summary>
    /// Sets the os image path.
    /// </summary>
    /// <param name="ImageName">Name of the image.</param>
    /// <returns>System.String.</returns>
    public static string SetOSImagePath(string ImageName) {
        string rtn = ImageName;

        if (Device.OS == TargetPlatform.Windows || Device.OS == TargetPlatform.WinPhone) {
            rtn = "Images/" + ImageName;
        }

        return rtn;
    }
}
</code>```

Title: Image source from disk Xamarin Forms

Body:
I have a Xamarin Forms app and what I want to do is to set the source of an Image View to a specific Image in my disk in UWP.
That is to say, I have the absolute path of my image which is in my disk, and I want to set the source of an Image View programmatically.
I have tried using

image.Source = ImageSource.FromFile(filePath);

But it didn't work in UWP, although it does work in Android.

Body:
A good way to do what you want is in your ViewModel to use a method where you put the path based on the OS

View

<Image Source="{Binding ImagePath}" />

ViewModel example

public string ImagePath
{
    get
    {
        return _path;
    }
    set
    {
        if (_path != value)
        {
            _path = value;
            OnPropertyChanged("ImagePath");
        }
    }
}
private string _path;

When you set a path for an image you can use a function like that:

public static class OSHelpers {
    /// <summary>
    /// Sets the os image path.
    /// </summary>
    /// <param name="ImageName">Name of the image.</param>
    /// <returns>System.String.</returns>
    public static string SetOSImagePath(string ImageName) {
        string rtn = ImageName;

        if (Device.OS == TargetPlatform.Windows || Device.OS == TargetPlatform.WinPhone) {
            rtn = "Images/" + ImageName;
        }

        return rtn;
    }
}


问:
sql: optimize query with many conditions
I have this sql query:

```<code>SELECT
    [Id],
    [Content]
FROM
    [MyTable] with (nolock)
where
    Content is not null
    and (PATINDEX('%"number" : "[0-9]%', Content) > 0
         or PATINDEX('%"number":"[0-9]%', Content) > 0
         or PATINDEX('%"number" :"[0-9]%', Content) > 0
         or PATINDEX('%"number": "[0-9]%', Content) > 0
         --del
         or PATINDEX('%"del":"[0-9]%', Content) > 0
         or PATINDEX('%"del":"[0-9]%', Content) > 0
         or PATINDEX('%"del":"[0-9]%', Content) > 0
         or PATINDEX('%"del":"[0-9]%', Content) > 0
)
</code>```

On my server, with caches cleared, it takes more than two minutes to return around 400 rows, maybe because I have a lot of conditions with or.

I've created the query that way because of the possibilities of the "number" string in the Content column of having a space or not between ":" and "number" string or the next digit.

Is there a way to reduce from the or conditions?

答:
Try it

```<code>SELECT
    [Id],
    [Content]
FROM
    [MyTable] with (nolock)
where
    Content is not null
    and (PATINDEX('%"number":"[0-9]%', replace(Content,' ','')) > 0
         or PATINDEX('%"del":"[0-9]%', replace(Content,' ','')) > 0)
</code>```

and you can remove Content is not null part also

Body:
I have this sql query:

SELECT
    [Id],
    [Content]
FROM
    [MyTable] with (nolock)
where
    Content is not null
    and (PATINDEX('%"number" : "[0-9]%', Content) > 0
         or PATINDEX('%"number":"[0-9]%', Content) > 0
         or PATINDEX('%"number" :"[0-9]%', Content) > 0
         or PATINDEX('%"number": "[0-9]%', Content) > 0
         --del
         or PATINDEX('%"del":"[0-9]%', Content) > 0
         or PATINDEX('%"del":"[0-9]%', Content) > 0
         or PATINDEX('%"del":"[0-9]%', Content) > 0
         or PATINDEX('%"del":"[0-9]%', Content) > 0
)

On my server, with caches cleared, it takes more than two minutes to return around 400 rows, maybe because I have a lot of conditions with or.

I've created the query that way because of the possibilities of the "number" string in the Content column of having a space or not between ":" and "number" string or the next digit.

Is there a way to reduce from the or conditions?

Body:
Try it

SELECT
    [Id],
    [Content]
FROM
    [MyTable] with (nolock)
where
    Content is not null
    and (PATINDEX('%"number":"[0-9]%', replace(Content,' ','')) > 0
         or PATINDEX('%"del":"[0-9]%', replace(Content,' ','')) > 0)

and you can remove Content is not null part also


问:
Is Dart Dev Compiler supposed to support dart:io?
I tried compiling a simple HttpServer.bind with DDC and it seems to throw a runtime error saying "Operation not supported" on that line.

Am I doing something wrong, or is DDC not supposed to support that?

答: Dart Dev Compiler does not support dart:io. It is only for web applications.


问:
Difference between INNER JOIN and LEFT SEMI JOIN
What is the difference between an INNER JOIN and LEFT SEMI JOIN?

In the scenario below, why am I getting two different results?

The INNER JOIN result set is a lot larger. Can someone explain? I am trying to get the names within table_1 that only appear in table_2.

```<code>SELECT name
FROM table_1 a
    INNER JOIN table_2 b ON a.name=b.name

SELECT name
FROM table_1 a
    LEFT SEMI JOIN table_2 b ON (a.name=b.name)
</code>```

Body:
What is the difference between an INNER JOIN and LEFT SEMI JOIN?

In the scenario below, why am I getting two different results?

The INNER JOIN result set is a lot larger. Can someone explain? I am trying to get the names within table_1 that only appear in table_2.

SELECT name
FROM table_1 a
    INNER JOIN table_2 b ON a.name=b.name

SELECT name
FROM table_1 a
    LEFT SEMI JOIN table_2 b ON (a.name=b.name)

Body:
Suppose there are 2 tables TableA and TableB with only 2 columns (Id, Data) and following data:

TableA:

+----+---------+
| Id |  Data   |
+----+---------+
|  1 | DataA11 |
|  1 | DataA12 |
|  1 | DataA13 |
|  2 | DataA21 |
|  3 | DataA31 |
+----+---------+

TableB:

+----+---------+
| Id |  Data   |
+----+---------+
|  1 | DataB11 |
|  2 | DataB21 |
|  2 | DataB22 |
|  2 | DataB23 |
|  4 | DataB41 |
+----+---------+

Inner Join on column Id will return columns from both the tables and only the matching records:

.----.---------.----.---------.
| Id |  Data   | Id |  Data   |
:----+---------+----+---------:
|  1 | DataA11 |  1 | DataB11 |
:----+---------+----+---------:
|  1 | DataA12 |  1 | DataB11 |
:----+---------+----+---------:
|  1 | DataA13 |  1 | DataB11 |
:----+---------+----+---------:
|  2 | DataA21 |  2 | DataB21 |
:----+---------+----+---------:
|  2 | DataA21 |  2 | DataB22 |
:----+---------+----+---------:
|  2 | DataA21 |  2 | DataB23 |
'----'---------'----'---------'

Left Join (or Left Outer join) on column Id will return columns from both the tables and matching records with records from left table (Null values from right table):

.----.---------.----.---------.
| Id |  Data   | Id |  Data   |
:----+---------+----+---------:
|  1 | DataA11 |  1 | DataB11 |
:----+---------+----+---------:
|  1 | DataA12 |  1 | DataB11 |
:----+---------+----+---------:
|  1 | DataA13 |  1 | DataB11 |
:----+---------+----+---------:
|  2 | DataA21 |  2 | DataB21 |
:----+---------+----+---------:
|  2 | DataA21 |  2 | DataB22 |
:----+---------+----+---------:
|  2 | DataA21 |  2 | DataB23 |
:----+---------+----+---------:
|  3 | DataA31 |    |         |
'----'---------'----'---------'

Right Join (or Right Outer join) on column Id will return columns from both the tables and matching records with records from right table (Null values from left table):

┌────┬─────────┬────┬─────────┐
│ Id │  Data   │ Id │  Data   │
├────┼─────────┼────┼─────────┤
│  1 │ DataA11 │  1 │ DataB11 │
│  1 │ DataA12 │  1 │ DataB11 │
│  1 │ DataA13 │  1 │ DataB11 │
│  2 │ DataA21 │  2 │ DataB21 │
│  2 │ DataA21 │  2 │ DataB22 │
│  2 │ DataA21 │  2 │ DataB23 │
│    │         │  4 │ DataB41 │
└────┴─────────┴────┴─────────┘

Full Outer Join on column Id will return columns from both the tables and matching records with records from left table (Null values from right table) and records from right table (Null values from left table):

╔════╦═════════╦════╦═════════╗
║ Id ║  Data   ║ Id ║  Data   ║
╠════╬═════════╬════╬═════════╣
║  - ║         ║    ║         ║
║  1 ║ DataA11 ║  1 ║ DataB11 ║
║  1 ║ DataA12 ║  1 ║ DataB11 ║
║  1 ║ DataA13 ║  1 ║ DataB11 ║
║  2 ║ DataA21 ║  2 ║ DataB21 ║
║  2 ║ DataA21 ║  2 ║ DataB22 ║
║  2 ║ DataA21 ║  2 ║ DataB23 ║
║  3 ║ DataA31 ║    ║         ║
║    ║         ║  4 ║ DataB41 ║
╚════╩═════════╩════╩═════════╝

Left Semi Join on column Id will return columns only from left table and matching records only from left table:

┌────┬─────────┐
│ Id │  Data   │
├────┼─────────┤
│  1 │ DataA11 │
│  1 │ DataA12 │
│  1 │ DataA13 │
│  2 │ DataA21 │
└────┴─────────┘


问:
Get IDs for duplicate rows (considering all other columns) in Apache Spark
I have a Spark sql dataframe, consisting of an ID column and n "data" columns, i.e.

```<code>id | dat1 | dat2 | ... | datn
</code>```

The id columnn is uniquely determined, whereas, looking at dat1 ... datn there may be duplicates.

My goal is to find the ids of those duplicates.

My approach so far:

get the duplicate rows using groupBy:

dup_df = df.groupBy(df.columns[1:]).count().filter('count > 1')

join the dup_df with the entire df to get the duplicate rows including id:

df.join(dup_df, df.columns[1:])

I am quite certain that this is basically correct, it fails because the dat1 ... datn columns contain null values.

To do the join on null values, I found .e.g this SO post. But this would require to construct a huge "string join condition".

Thus my questions:

Is there a simple / more generic / more pythonic way to do joins on null values?

Or, even better, is there another (easier, more beautiful, ...) method to get the desired ids?

BTW: I am using Spark 2.1.0 and Python 3.5.3

答:
If number ids per group is relatively small you can groupBy and collect_list. Required imports

from pyspark.sql.functions import collect_list, size
```

<p>example data:</p>

<pre class="lang-py prettyprint-override"><code>df = sc.parallelize([
    (1, "a", "b", 3),
    (2, None, "f", None),
    (3, "g", "h", 4),
    (4, None, "f", None),
    (5, "a", "b", 3)
]).toDF(["id"])
</code>```

query:

```<code>(df
   .groupBy(df.columns[1:])
   .agg(collect_list("id").alias("ids"))
   .where(size("ids") > 1))
</code>```

and the result:

+----+---+----+------+
|  _2| _3|  _4|   ids|
+----+---+----+------+
|null|  f|null|[2, 4]|
|   a|  b|   3|[1, 5]|
+----+---+----+------+
```

<p>You can apply <code>explode</code> twice (or use an <code>udf</code>) to an output equivalent to the one returned from <code>join</code>.</p>

<p>You can also identify groups using minimal <code>id</code> per group. A few additional imports:</p>

<pre class="lang-py prettyprint-override"><code>from pyspark.sql.window import Window
from pyspark.sql.functions import col, count, min
</code>```

window definition:

w = Window.partitionBy(df.columns[1:])
```

<p>query:</p>

<pre class="lang-py prettyprint-override"><code>(df
    .select(
        "*",
        count("*").over(w).alias("_cnt"),
        min("id").over(w).alias("group"))
    .where(col("_cnt") > 1))
</code>```

and the result:

+---+----+---+----+----+-----+
| id|  _2| _3|  _4|_cnt|group|
+---+----+---+----+----+-----+
|  2|null|  f|null|   2|    2|
|  4|null|  f|null|   2|    2|
|  1|   a|  b|   3|   2|    1|
|  5|   a|  b|   3|   2|    1|
+---+----+---+----+----+-----+
```

You can further use group column for self join.

Body:
I have a Spark sql dataframe, consisting of an ID column and n "data" columns, i.e.

id | dat1 | dat2 | ... | datn

The id columnn is uniquely determined, whereas, looking at dat1 ... datn there may be duplicates.

My goal is to find the ids of those duplicates.

My approach so far:

get the duplicate rows using groupBy:

dup_df = df.groupBy(df.columns[1:]).count().filter('count > 1')

join the dup_df with the entire df to get the duplicate rows including id:

df.join(dup_df, df.columns[1:])

I am quite certain that this is basically correct, it fails because the dat1 ... datn columns contain null values.

To do the join on null values, I found .e.g this SO post. But this would require to construct a huge "string join condition".

Thus my questions:

Is there a simple / more generic / more pythonic way to do joins on null values?

Or, even better, is there another (easier, more beautiful, ...) method to get the desired ids?

BTW: I am using Spark 2.1.0 and Python 3.5.3

Body:
If number ids per group is relatively small you can groupBy and collect_list. Required imports

from pyspark.sql.functions import collect_list, size

example data:

df = sc.parallelize([
    (1, "a", "b", 3),
    (2, None, "f", None),
    (3, "g", "h", 4),
    (4, None, "f", None),
    (5, "a", "b", 3)
]).toDF(["id"])

query:

(df
   .groupBy(df.columns[1:])
   .agg(collect_list("id").alias("ids"))
   .where(size("ids") > 1))

and the result:

+----+---+----+------+
|  _2| _3|  _4|   ids|
+----+---+----+------+
|null|  f|null|[2, 4]|
|   a|  b|   3|[1, 5]|
+----+---+----+------+

You can apply explode twice (or use an udf) to an output equivalent to the one returned from join.

You can also identify groups using minimal id per group. A few additional imports:

from pyspark.sql.window import Window
from pyspark.sql.functions import col, count, min

window definition:

w = Window.partitionBy(df.columns[1:])

query:

(df
    .select(
        "*",
        count("*").over(w).alias("_cnt"),
        min("id").over(w).alias("group"))
    .where(col("_cnt") > 1))

and the result:

+---+----+---+----+----+-----+
| id|  _2| _3|  _4|_cnt|group|
+---+----+---+----+----+-----+
|  2|null|  f|null|   2|    2|
|  4|null|  f|null|   2|    2|
|  1|   a|  b|   3|   2|    1|
|  5|   a|  b|   3|   2|    1|
+---+----+---+----+----+-----+

You can further use group column for self join.


问:
pandas: calculate percentiles within subgroups?
I've got a Pandas dataframe that looks like this:

```<code> school_id  uni_id  points
 123        44      180
 123        45      160
 123        45      160
 123        48      110
 124        44      180
 124        45      160
 124        47      130
 123        48      120
</code>```

Generated as follows, to help kind answerers:

```<code>df = pd.DataFrame({
    'school_id': [123, 123, 123, 123, 124, 124, 124, 124],
    'school_id': [44, 45, 45, 48, 44, 45, 47, 48],
    'points': [180, 160, 160, 110, 180, 160, 130, 120]
})
</code>```

I'd like to add a percentile column, which represents the percentile of the points value for each school. So this dataset would look like this:

```<code> school_id  uni_id  points  percentile
 123        44      180     100
 123        45      160     50
 123        45      160     50
 123        48      110     0
 124        44      180     100
 124        45      160     66
 124        47      130     33
 123        48      120     0
</code>```

What's the best way to do this? I assume that I need to group by school_id, then do df.quantile() within each subgroup somehow, then ungroup?

UPDATE: Maybe I need to start with something like this... df.groupby('school_id')['points'].rank(ascending=False) and then divide the rank by the length of each group to normalise it between 0 and 100?

答:
You can specify pct=True while computing numerical data ranks between the subgroups grouped by "school_id" as an additional arg to GroupBy.rank method:

```<code>df.assign(percentile=df.groupby("school_id")['points'].rank(pct=True).mul(100))
</code>```

To check (for one instance):

```<code>from scipy.stats import percentileofscore
df.groupby("school_id")['points'].apply(percentileofscore, 160)

school_id
123    70.000000
124    66.666667
Name: points, dtype: float64
</code>```

Body:
I've got a Pandas dataframe that looks like this:

 school_id  uni_id  points
 123        44      180
 123        45      160
 123        45      160
 123        48      110
 124        44      180
 124        45      160
 124        47      130
 123        48      120

Generated as follows, to help kind answerers:

df = pd.DataFrame({
    'school_id': [123, 123, 123, 123, 124, 124, 124, 124],
    'school_id': [44, 45, 45, 48, 44, 45, 47, 48],
    'points': [180, 160, 160, 110, 180, 160, 130, 120]
})

I'd like to add a percentile column, which represents the percentile of the points value for each school. So this dataset would look like this:

 school_id  uni_id  points  percentile
 123        44      180     100
 123        45      160     50
 123        45      160     50
 123        48      110     0
 124        44      180     100
 124        45      160     66
 124        47      130     33
 123        48      120     0

What's the best way to do this? I assume that I need to group by school_id, then do df.quantile() within each subgroup somehow, then ungroup?

UPDATE: Maybe I need to start with something like this... df.groupby('school_id')['points'].rank(ascending=False) and then divide the rank by the length of each group to normalise it between 0 and 100?

Body:
You can specify pct=True while computing numerical data ranks between the subgroups grouped by "school_id" as an additional arg to GroupBy.rank method:

df.assign(percentile=df.groupby("school_id")['points'].rank(pct=True).mul(100))

To check (for one instance):

from scipy.stats import percentileofscore
df.groupby("school_id")['points'].apply(percentileofscore, 160)

school_id
123    70.000000
124    66.666667
Name: points, dtype: float64


问:
Multiple dex files define in Android Studio
I know this is a common issue, but I've checked all other posts and none of them helped. A lot of them are for eclipse, and the ones for android studio don't help.

```<code>Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexException: Multiple dex files define Lcom/android/volley/VolleyError;
</code>```

What I've tried:

Search through files for duplicate VolleyError (only one VolleyError.java and one VolleyError.class).

Reimport project from github

Download from github then open from AS

Delete and reinstall a fresh copy of Android Studio

First, I get the bug that it is a repository but there is no git, so I just remove volley (that worked for my teammate). Then, while all other 4 members get it to work, mine stalls and bugs there. There are also 4 warnings, one is below, the rest are similar:

```<code>Warning:warning: Ignoring InnerClasses attribute for an anonymous inner class
(org.apache.commons.logging.impl.WeakHashtable$1) that doesn't come with an
associated EnclosingMethod attribute. This class was probably produced by a
compiler that did not target the modern .class file format. The recommended
solution is to recompile the class from source, using an up-to-date compiler
and without specifying any "-target" type options. The consequence of ignoring
this warning is that reflective operations on this class will incorrectly
indicate that it is *not* an inner class.
</code>```

If it helps, this is the gradle file below:

```<code>apply plugin: 'com.android.application'

dependencies {

    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile files('libs/commons-logging-1.2.jar')
    compile files('libs/com.fasterxml.jackson.databind.jar')
    compile files('libs/gson-2.3.1.jar')
    compile files('libs/httpclient-4.5.1.jar')
    compile files('libs/httpcore-4.4.3.jar')
    compile files('libs/jackson-core-2.7.1.jar')
    compile files('libs/sun.misc.BASE64Decoder.jar')
    compile files('libs/jackson-annotations-2.7.1.jar')
    compile files('libs/jackson-databind-2.7.1-1.jar')
    compile 'com.android.support:appcompat-v7:25.3.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.mcxiaoke.volley:library-aar:1.0.0'
    compile 'com.android.volley:volley:1.0.0'
    compile 'com.android.support:recyclerview-v7:25.3.0'
    compile 'com.android.support:gridlayout-v7:25.3.0'
    compile 'com.android.support:cardview-v7:25.3.0'
    compile 'com.android.support:design:25.3.0'
    compile 'com.android.volley:volley:1.0.0'
    testCompile 'junit:junit:4.12'
}

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"

    defaultConfig {
        applicationId "com.csc301.team7.era"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/LGPL2.1'
    }
}
</code>```

Removing two of the extra volley lines in gradle in turn gives me this error in gradle:

```<code>Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexException: Multiple dex files define Lcom/fasterxml/jackson/databind/AbstractTypeResolver;
</code>```

but I can't find AbstractTypeResolver.

Please, any help getting this project to run would be greatly appreciated.

答:
This error:

```<code>Ignoring InnerClasses attribute for an anonymous inner class
</code>```

is an issue I had too. It is an issue related to inner classes when using Proguard. It can be solved by adding the following to your proguard file:

```<code>-keepattributes EnclosingMethod
-keepattributes InnerClasses
</code>```

Proguard and inner classes often lead to issues. By adding the lines above, you indicate to Proguard that it should keep inner classes where they are and not treat them as if they are in their own files.

Body:
I know this is a common issue, but I've checked all other posts and none of them helped. A lot of them are for eclipse, and the ones for android studio don't help.

Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexException: Multiple dex files define Lcom/android/volley/VolleyError;

What I've tried:

Search through files for duplicate VolleyError (only one VolleyError.java and one VolleyError.class).

Reimport project from github

Download from github then open from AS

Delete and reinstall a fresh copy of Android Studio

First, I get the bug that it is a repository but there is no git, so I just remove volley (that worked for my teammate). Then, while all other 4 members get it to work, mine stalls and bugs there. There are also 4 warnings, one is below, the rest are similar:

Warning:warning: Ignoring InnerClasses attribute for an anonymous inner class
(org.apache.commons.logging.impl.WeakHashtable$1) that doesn't come with an
associated EnclosingMethod attribute. This class was probably produced by a
compiler that did not target the modern .class file format. The recommended
solution is to recompile the class from source, using an up-to-date compiler
and without specifying any "-target" type options. The consequence of ignoring
this warning is that reflective operations on this class will incorrectly
indicate that it is *not* an inner class.

If it helps, this is the gradle file below:

apply plugin: 'com.android.application'

dependencies {

    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile files('libs/commons-logging-1.2.jar')
    compile files('libs/com.fasterxml.jackson.databind.jar')
    compile files('libs/gson-2.3.1.jar')
    compile files('libs/httpclient-4.5.1.jar')
    compile files('libs/httpcore-4.4.3.jar')
    compile files('libs/jackson-core-2.7.1.jar')
    compile files('libs/sun.misc.BASE64Decoder.jar')
    compile files('libs/jackson-annotations-2.7.1.jar')
    compile files('libs/jackson-databind-2.7.1-1.jar')
    compile 'com.android.support:appcompat-v7:25.3.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.mcxiaoke.volley:library-aar:1.0.0'
    compile 'com.android.volley:volley:1.0.0'
    compile 'com.android.support:recyclerview-v7:25.3.0'
    compile 'com.android.support:gridlayout-v7:25.3.0'
    compile 'com.android.support:cardview-v7:25.3.0'
    compile 'com.android.support:design:25.3.0'
    compile 'com.android.volley:volley:1.0.0'
    testCompile 'junit:junit:4.12'
}

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"

    defaultConfig {
        applicationId "com.csc301.team7.era"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/LGPL2.1'
    }
}

Removing two of the extra volley lines in gradle in turn gives me this error in gradle:

Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexException: Multiple dex files define Lcom/fasterxml/jackson/databind/AbstractTypeResolver;

but I can't find AbstractTypeResolver.

Please, any help getting this project to run would be greatly appreciated.

Body:
This error:

Ignoring InnerClasses attribute for an anonymous inner class

is an issue I had too. It is an issue related to inner classes when using Proguard. It can be solved by adding the following to your proguard file:

-keepattributes EnclosingMethod
-keepattributes InnerClasses

Proguard and inner classes often lead to issues. By adding the lines above, you indicate to Proguard that it should keep inner classes where they are and not treat them as if they are in their own files.


问:
applicationWillFinishLaunching not being called applescript Xcode
for some reason, my handler applicationWillFinishLaunching_(aNotification)
isn't being called when my app starts. Any suggestions?

答:
You need to place this method in your AppDelegate.applescript file and make sure the AppDelegate is properly linked in IB.

To test this start a new project to see how it's configured correctly. You also can try if another AppDelegate method works like

```<code>on applicationDidFinishLaunching:aNotification
log "works"
end
</code>```

If this isn't your problem post your code.

Body:
You need to place this method in your AppDelegate.applescript file and make sure the AppDelegate is properly linked in IB.

To test this start a new project to see how it's configured correctly. You also can try if another AppDelegate method works like

on applicationDidFinishLaunching:aNotification
log "works"
end

If this isn't your problem post your code.