问:
Datagridview Horizontal to vertical button
The following code isn't working when exporting the datagridview data when trying to make it vertical with headers along the left side along with text beside each one. Once this is flipped the user would click on button1 to export to excel.

```<code>Imports System.Data.DataTable
Imports System.IO
Imports Microsoft.Office.Interop
Public Class Form1
Dim table As New DataTable(0)
Public checkBoxList As List(Of CheckBox)
Private ds As DataSet = Nothing
Private dt As DataTable = Nothing
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ds = New DataSet()
    dt = New DataTable()
    ds.Tables.Add("Table")
    Dim my_DataView As DataView = ds.Tables(0).DefaultView
    DataGridView1.DataSource = my_DataView
    table.Columns.Add("Forename", Type.GetType("System.String"))
    table.Columns.Add("Surname", Type.GetType("System.String"))
    table.Columns.Add("Food", Type.GetType("System.String"))
    checkBoxList = New List(Of CheckBox) From {CheckBox1, CheckBox2, CheckBox3, CheckBox4}

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim currentDataSet As DataSet = FlipDataSet(ds) ' Flip the DataSet
    Dim values As String = "" &
    String.Join(" & ", checkBoxList _
    .Where(Function(cb) cb.Checked).Select(Function(cb) cb.Text))

    ' use values for placing into your DataGridView
    CheckBox1.Text = values
    CheckBox2.Text = values
    CheckBox3.Text = values
    CheckBox4.Text = values

    table.Rows.Add(TextBox1.Text, TextBox2.Text, values.ToString)
    DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
    DataGridView1.RowTemplate.Height = 100
    DataGridView1.AllowUserToAddRows = False
    DataGridView1.DataSource = table

    'Save to excel with headers
    Dim ExcelApp As Object, ExcelBook As Object
    Dim ExcelSheet As Object
    Dim i As Integer
    Dim j As Integer

    'create object of excel
    ExcelApp = CreateObject("Excel.Application")
    ExcelBook = ExcelApp.WorkBooks.Add
    ExcelSheet = ExcelBook.WorkSheets(1)

    With ExcelSheet
        For Each column As DataGridViewColumn In DataGridView1.Columns
            .cells(1, column.Index + 1) = column.HeaderText
        Next
        For i = 1 To Me.DataGridView1.RowCount
            .cells(i + 1, 1) = Me.DataGridView1.Rows(i - 1).Cells("Forename").Value
            For j = 1 To DataGridView1.Columns.Count - 1
                .cells(i + 1, j + 1) = DataGridView1.Rows(i - 1).Cells(j).Value
            Next
        Next

    End With

    ExcelApp.Visible = True
    '
    ExcelSheet = Nothing
    ExcelBook = Nothing
    ExcelApp = Nothing
End Sub

Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

End Sub
Public Function FlipDataSet(ByVal my_DataSet As DataSet) As DataSet
    Dim ds As New DataSet()

    For Each dt As DataTable In my_DataSet.Tables
        Dim table As New DataTable()

        For i As Integer = 0 To dt.Rows.Count
            table.Columns.Add(Convert.ToString(i))
        Next
        Dim r As DataRow
        For k As Integer = 0 To dt.Columns.Count - 1
            r = table.NewRow()
            r(0) = dt.Columns(k).ToString()
            For j As Integer = 1 To dt.Rows.Count
                r(j) = dt.Rows(j - 1)(k)
            Next
            table.Rows.Add(r)
        Next

        ds.Tables.Add(table)
    Next

    Return ds
End Function

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim currentDataSet As DataSet = FlipDataSet(ds) ' Flip the DataSet
    Dim currentDataView As DataView = currentDataSet.Tables(0).DefaultView
    DataGridView1.DataSource = currentDataView

    Button2.Enabled = False

End Sub
End Class
</code>```

When clicking button2 it should flip the data with the following;

```<code>Private Sub Button2_Click(sender As Object, e As EventArgs) Handles
    Button2.Click
    Dim currentDataSet As DataSet = FlipDataSet(ds) ' Flip the DataSet
    Dim currentDataView As DataView = currentDataSet.Tables(0).DefaultView
    DataGridView1.DataSource = currentDataView

    Button2.Enabled = False

    End Sub
    End Class
</code>```

I've tried debugging but it i can't seem to find anything wrong? It will allow me to insert data in the textbox's whilst selecting checkbox's and when clicking button 1 to export it works fine, but it doesn't flip the data.

Please can anyone suggest how to fix this as i have a presentation on the 8th June and this data needs to automatically be flipped

Sourcecode: Download Myproject

Image of target

答:
Answered:</p>

```<code>Imports Microsoft.Office.Interop
Imports System.Runtime.InteropServices

Public Class Form1
Private ds As DataSet = Nothing
Private dt As DataTable = Nothing

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    DataGridView1.AllowUserToAddRows = False

    dt = New DataTable("MyTable")
    dt.Columns.Add("Forename", Type.GetType("System.String"))
    dt.Columns.Add("Surname", Type.GetType("System.String"))
    dt.Columns.Add("Food", Type.GetType("System.String"))

    ds = New DataSet
    ds.Tables.Add(dt)

    Dim my_DataView As DataView = ds.Tables("MyTable").DefaultView
    DataGridView1.DataSource = my_DataView
End Sub

Private Sub Button_AddRowData_Click(sender As Object, e As EventArgs) Handles Button_AddRowData.Click
    Dim foods As String = String.Join(" & ", CheckedListBox1.CheckedItems.Cast(Of String))
    dt.Rows.Add(New Object() {TextBox_Forename.Text, TextBox_Surname.Text, foods})
End Sub

Private Sub Button_FlipAndSave_Click(sender As Object, e As EventArgs) Handles Button_FlipAndSave.Click
    FlipAndSave(ds.Tables("MyTable"))
End Sub

Private Sub FlipAndSave(table As DataTable)
    Dim ExcelApp As New Excel.Application
    Dim WrkBk As Excel.Workbook = ExcelApp.Workbooks.Add()
    Dim WrkSht As Excel.Worksheet = CType(WrkBk.Worksheets(1), Excel.Worksheet)

    With WrkSht
        For ci As Integer = 0 To table.Columns.Count - 1
            .Cells(ci + 1, 1) = table.Columns(ci).ColumnName
        Next
        For ri As Integer = 0 To table.Rows.Count - 1
            For ci As Integer = 0 To table.Columns.Count - 1
                .Cells(ci + 1, ri + 2) = table.Rows(ri).Item(ci).ToString
            Next
        Next
    End With

    ExcelApp.Visible = True

    'use this lines if you want to automatically save the WorkBook
    'WrkBk.SaveAs("C:\Some Folder\My Workbook.xlsx") '(.xls) if you have an old version of Excel

    'ExcelApp.Quit() 'use this line if you want to close the Excel Application

    ReleaseObject(ExcelApp)
    ReleaseObject(WrkBk)
    ReleaseObject(WrkSht)
End Sub

Private Sub ReleaseObject(obj As Object)
    Marshal.ReleaseComObject(obj)
    obj = Nothing
End Sub
End Class
</code>```

Body:
The following code isn't working when exporting the datagridview data when trying to make it vertical with headers along the left side along with text beside each one. Once this is flipped the user would click on button1 to export to excel.

Imports System.Data.DataTable
Imports System.IO
Imports Microsoft.Office.Interop
Public Class Form1
Dim table As New DataTable(0)
Public checkBoxList As List(Of CheckBox)
Private ds As DataSet = Nothing
Private dt As DataTable = Nothing
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ds = New DataSet()
    dt = New DataTable()
    ds.Tables.Add("Table")
    Dim my_DataView As DataView = ds.Tables(0).DefaultView
    DataGridView1.DataSource = my_DataView
    table.Columns.Add("Forename", Type.GetType("System.String"))
    table.Columns.Add("Surname", Type.GetType("System.String"))
    table.Columns.Add("Food", Type.GetType("System.String"))
    checkBoxList = New List(Of CheckBox) From {CheckBox1, CheckBox2, CheckBox3, CheckBox4}

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim currentDataSet As DataSet = FlipDataSet(ds) ' Flip the DataSet
    Dim values As String = "" &
    String.Join(" & ", checkBoxList _
    .Where(Function(cb) cb.Checked).Select(Function(cb) cb.Text))

    ' use values for placing into your DataGridView
    CheckBox1.Text = values
    CheckBox2.Text = values
    CheckBox3.Text = values
    CheckBox4.Text = values

    table.Rows.Add(TextBox1.Text, TextBox2.Text, values.ToString)
    DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
    DataGridView1.RowTemplate.Height = 100
    DataGridView1.AllowUserToAddRows = False
    DataGridView1.DataSource = table

    'Save to excel with headers
    Dim ExcelApp As Object, ExcelBook As Object
    Dim ExcelSheet As Object
    Dim i As Integer
    Dim j As Integer

    'create object of excel
    ExcelApp = CreateObject("Excel.Application")
    ExcelBook = ExcelApp.WorkBooks.Add
    ExcelSheet = ExcelBook.WorkSheets(1)

    With ExcelSheet
        For Each column As DataGridViewColumn In DataGridView1.Columns
            .cells(1, column.Index + 1) = column.HeaderText
        Next
        For i = 1 To Me.DataGridView1.RowCount
            .cells(i + 1, 1) = Me.DataGridView1.Rows(i - 1).Cells("Forename").Value
            For j = 1 To DataGridView1.Columns.Count - 1
                .cells(i + 1, j + 1) = DataGridView1.Rows(i - 1).Cells(j).Value
            Next
        Next

    End With

    ExcelApp.Visible = True
    '
    ExcelSheet = Nothing
    ExcelBook = Nothing
    ExcelApp = Nothing
End Sub

Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

End Sub
Public Function FlipDataSet(ByVal my_DataSet As DataSet) As DataSet
    Dim ds As New DataSet()

    For Each dt As DataTable In my_DataSet.Tables
        Dim table As New DataTable()

        For i As Integer = 0 To dt.Rows.Count
            table.Columns.Add(Convert.ToString(i))
        Next
        Dim r As DataRow
        For k As Integer = 0 To dt.Columns.Count - 1
            r = table.NewRow()
            r(0) = dt.Columns(k).ToString()
            For j As Integer = 1 To dt.Rows.Count
                r(j) = dt.Rows(j - 1)(k)
            Next
            table.Rows.Add(r)
        Next

        ds.Tables.Add(table)
    Next

    Return ds
End Function

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim currentDataSet As DataSet = FlipDataSet(ds) ' Flip the DataSet
    Dim currentDataView As DataView = currentDataSet.Tables(0).DefaultView
    DataGridView1.DataSource = currentDataView

    Button2.Enabled = False

End Sub
End Class

When clicking button2 it should flip the data with the following;

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles
    Button2.Click
    Dim currentDataSet As DataSet = FlipDataSet(ds) ' Flip the DataSet
    Dim currentDataView As DataView = currentDataSet.Tables(0).DefaultView
    DataGridView1.DataSource = currentDataView

    Button2.Enabled = False

    End Sub
    End Class

I've tried debugging but it i can't seem to find anything wrong? It will allow me to insert data in the textbox's whilst selecting checkbox's and when clicking button 1 to export it works fine, but it doesn't flip the data.

Please can anyone suggest how to fix this as i have a presentation on the 8th June and this data needs to automatically be flipped

Sourcecode: Download Myproject

Image of target

Body:
Answered:

Imports Microsoft.Office.Interop
Imports System.Runtime.InteropServices

Public Class Form1
Private ds As DataSet = Nothing
Private dt As DataTable = Nothing

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    DataGridView1.AllowUserToAddRows = False

    dt = New DataTable("MyTable")
    dt.Columns.Add("Forename", Type.GetType("System.String"))
    dt.Columns.Add("Surname", Type.GetType("System.String"))
    dt.Columns.Add("Food", Type.GetType("System.String"))

    ds = New DataSet
    ds.Tables.Add(dt)

    Dim my_DataView As DataView = ds.Tables("MyTable").DefaultView
    DataGridView1.DataSource = my_DataView
End Sub

Private Sub Button_AddRowData_Click(sender As Object, e As EventArgs) Handles Button_AddRowData.Click
    Dim foods As String = String.Join(" & ", CheckedListBox1.CheckedItems.Cast(Of String))
    dt.Rows.Add(New Object() {TextBox_Forename.Text, TextBox_Surname.Text, foods})
End Sub

Private Sub Button_FlipAndSave_Click(sender As Object, e As EventArgs) Handles Button_FlipAndSave.Click
    FlipAndSave(ds.Tables("MyTable"))
End Sub

Private Sub FlipAndSave(table As DataTable)
    Dim ExcelApp As New Excel.Application
    Dim WrkBk As Excel.Workbook = ExcelApp.Workbooks.Add()
    Dim WrkSht As Excel.Worksheet = CType(WrkBk.Worksheets(1), Excel.Worksheet)

    With WrkSht
        For ci As Integer = 0 To table.Columns.Count - 1
            .Cells(ci + 1, 1) = table.Columns(ci).ColumnName
        Next
        For ri As Integer = 0 To table.Rows.Count - 1
            For ci As Integer = 0 To table.Columns.Count - 1
                .Cells(ci + 1, ri + 2) = table.Rows(ri).Item(ci).ToString
            Next
        Next
    End With

    ExcelApp.Visible = True

    'use this lines if you want to automatically save the WorkBook
    'WrkBk.SaveAs("C:\Some Folder\My Workbook.xlsx") '(.xls) if you have an old version of Excel

    'ExcelApp.Quit() 'use this line if you want to close the Excel Application

    ReleaseObject(ExcelApp)
    ReleaseObject(WrkBk)
    ReleaseObject(WrkSht)
End Sub

Private Sub ReleaseObject(obj As Object)
    Marshal.ReleaseComObject(obj)
    obj = Nothing
End Sub
End Class


来源: stackexchange

问:
choose n largest elements in two vector
I have two vectors, each contains n unsorted elements, how can I get n largest elements in these two vectors?

my solution is merge two vector into one with 2n elements, and then use std::nth_element algorithm, but I found that's not quite efficient, so anyone has more efficient solution. Really appreciate.

答:
```<code>vector<T> heap;
heap.reserve(n + 1);

vector<T>::iterator left = leftVec.begin(), right = rightVec.begin();

for (int i = 0; i < n; i++) {
    if (left != leftVec.end()) heap.push_back(*left++);
    else if (right != rightVec.end()) heap.push_back(*right++);
}

if (left == leftVec.end() && right == rightVec.end()) return heap;

make_heap(heap.begin(), heap.end(), greater<T>());

while (left != leftVec.end()) {
    heap.push_back(*left++);
    push_heap(heap.begin(), heap.end(), greater<T>());
    pop_heap(heap.begin(), heap.end(), greater<T>());
    heap.pop_back();
}

/* ... repeat for right ... */

return heap;
</code>```

Note I use *_heap directly rather than priority_queue because priority_queue does not provide access to its underlying data structure. This is O(N log n), slightly better than the naive O(N log N) method if n << N.

ContentLicense: CC BY-SA 2.5

Title: choose n largest elements in two vector

Body:
vector<T> heap;
heap.reserve(n + 1);

vector<T>::iterator left = leftVec.begin(), right = rightVec.begin();

for (int i = 0; i < n; i++) {
    if (left != leftVec.end()) heap.push_back(*left++);
    else if (right != rightVec.end()) heap.push_back(*right++);
}

if (left == leftVec.end() && right == rightVec.end()) return heap;

make_heap(heap.begin(), heap.end(), greater<T>());

while (left != leftVec.end()) {
    heap.push_back(*left++);
    push_heap(heap.begin(), heap.end(), greater<T>());
    pop_heap(heap.begin(), heap.end(), greater<T>());
    heap.pop_back();
}

/* ... repeat for right ... */

return heap;

Note I use *_heap directly rather than priority_queue because priority_queue does not provide access to its underlying data structure. This is O(N log n), slightly better than the naive O(N log N) method if n << N.


问:
Navigation Bar Expands When isTranslucent = False (Swift 3)
I have a table view with a search bar. But, when I click the search bar it seems that the Navigation Bar expands and creates a blank white space beneath it. However, this only occurs when self.navigationController?.navigationBar.isTranslucent = false but when I comment this line, the white space never appears. Also, the white bar appears only when I press the search bar. Here are some pictures for reference:

This is when I don't press the search bar.

Then when I press the search bar a white space appears under it.

And then when I cancel out of the search bar the white space is between the navigation bar and the search bar.

答:
Add this line in viewDidLoad:

```<code> self.automaticallyAdjustsScrollViewInsets = false
</code>```

ContentLicense: CC BY-SA 3.0

Body:
Add this line in viewDidLoad:

 self.automaticallyAdjustsScrollViewInsets = false


问:
python panda join dynamic columns by removing duplicates
we have an use case where we need to join all column values in a row by removing duplicates.Data is stored in a data frame of panda.
For eg consider below data frame df  with columns A,B,C

```<code>A   B   C
X1  AX  X1
X2  X2  X1
X3  X3  X3
X4  XX  XX
</code>```

I would like to add a new column which concatenates A to B to C and remove duplicates if any found by preserving the order. The output would be like

```<code>A   B   C   Newcol

X1  AX  X1  X1_AX
X2  X2  X1  X2_X1
X3  X3  X3  X3
X4  XX  XX  X4_XX
</code>```

Note that the number of columns are dynamic.
As of now I am doing it by using the command

```<code>df.apply(lambda x: '-'.join(x.dropna().astype(str).drop_duplicates()),axis=1)
</code>```

But this is very slow and takes around 150 seconds for my data.
But since 90% of the data frame are usually with only 2 columns , I put an if statement in my code and run the below command for cases with 2 columns

```<code>t1=pd.Series(np.where(df.iloc[:,0].dropna().astype(str) != df.iloc[:,1].dropna().astype(str), df.iloc[:,0].dropna().astype(str)+"-"+df.iloc[:,1].dropna().astype(str),df.iloc[:,1].dropna().astype(str)))
</code>```

which takes around 55.3 milli seconds

or even

```<code>t1=df.iloc[:,0].dropna().astype(str).where(df.iloc[:,0].dropna().astype(str) == df.iloc[:,1].dropna().astype(str), df.iloc[:,0].dropna().astype(str)+"-"+df.iloc[:,1].dropna().astype(str))
</code>```

both consumes almost same time ( 55 ms opposed to 150 seconds ), but issue is that it is applicable only for 2 columns.
I would like to create a generalised statement , so that it can handle n number of columns.
I tried using reduce on top ,but it gave  error while i tried for 3 columns.

```<code>reduce((lambda x,y:pd.Series(np.where(df.iloc[:,x].dropna().astype(str) != df.iloc[:,y].dropna().astype(str), df.iloc[:,x].dropna().astype(str)+"-"+df.iloc[:,y].dropna().astype(str),df.iloc[:,y].dropna().astype(str)))),list(range(df.shape[1])))
</code>```

TypeError: '>=' not supported between instances of 'str' and 'int'

Please note that the df is actually a chunk of a multicore parallel task. So it would be great if the suggestions excludes parallelism.

答:
Try

```<code>df['new'] = df.astype('str').apply(lambda x: '_'.join(set(x)), axis = 1)

    A   B   C   new
0   X1  AX  X1  AX_X1
1   X2  X2  X1  X1_X2
2   X3  X3  X3  X3
3   X4  XX  XX  X4_XX
</code>```

EDIT: Maintain the order of the column values

```<code>def my_append(x):
    l = []
    for elm in x:
        if elm not in l:
            l.append(elm)
    return '_'.join(l)

df['New col']=df.astype('str').apply(my_append, axis = 1)

1000 loops, best of 3: 871 µs per loop
</code>```

Returns

```<code>    A   B   C   New col
0   X1  AX  X1  X1_AX
1   X2  X2  X1  X2_X1
2   X3  X3  X3  X3
3   X4  XX  XX  X4_XX
</code>```

EDIT 1: In case you have nan in any column like this

```<code>    A   B   C
0   X1  AX  X1
1   X2  X2  X1
2   X3  X3  X3
3   NaN XX  XX
</code>```

Handle that in the function and then apply

```<code>def my_append(x):
l = []
for elm in x:
    if elm not in l:
        l.append(elm)
l = [x for x in l if str(x) != 'nan']
return '_'.join(l)

df['New col']=df.astype('str').apply(my_append, axis = 1)

    A   B   C   New col
0   X1  AX  X1  X1_AX
1   X2  X2  X1  X2_X1
2   X3  X3  X3  X3
3   NaN XX  XX  XX
</code>```

Body:
we have an use case where we need to join all column values in a row by removing duplicates.Data is stored in a data frame of panda.
For eg consider below data frame df  with columns A,B,C

A   B   C
X1  AX  X1
X2  X2  X1
X3  X3  X3
X4  XX  XX

I would like to add a new column which concatenates A to B to C and remove duplicates if any found by preserving the order. The output would be like

A   B   C   Newcol

X1  AX  X1  X1_AX
X2  X2  X1  X2_X1
X3  X3  X3  X3
X4  XX  XX  X4_XX

Note that the number of columns are dynamic.
As of now I am doing it by using the command

df.apply(lambda x: '-'.join(x.dropna().astype(str).drop_duplicates()),axis=1)

But this is very slow and takes around 150 seconds for my data.
But since 90% of the data frame are usually with only 2 columns , I put an if statement in my code and run the below command for cases with 2 columns

t1=pd.Series(np.where(df.iloc[:,0].dropna().astype(str) != df.iloc[:,1].dropna().astype(str), df.iloc[:,0].dropna().astype(str)+"-"+df.iloc[:,1].dropna().astype(str),df.iloc[:,1].dropna().astype(str)))

which takes around 55.3 milli seconds

or even

t1=df.iloc[:,0].dropna().astype(str).where(df.iloc[:,0].dropna().astype(str) == df.iloc[:,1].dropna().astype(str), df.iloc[:,0].dropna().astype(str)+"-"+df.iloc[:,1].dropna().astype(str))

both consumes almost same time ( 55 ms opposed to 150 seconds ), but issue is that it is applicable only for 2 columns.
I would like to create a generalised statement , so that it can handle n number of columns.
I tried using reduce on top ,but it gave  error while i tried for 3 columns.

reduce((lambda x,y:pd.Series(np.where(df.iloc[:,x].dropna().astype(str) != df.iloc[:,y].dropna().astype(str), df.iloc[:,x].dropna().astype(str)+"-"+df.iloc[:,y].dropna().astype(str),df.iloc[:,y].dropna().astype(str)))),list(range(df.shape[1])))

TypeError: '>=' not supported between instances of 'str' and 'int'

Please note that the df is actually a chunk of a multicore parallel task. So it would be great if the suggestions excludes parallelism.

Body:
Try

df['new'] = df.astype('str').apply(lambda x: '_'.join(set(x)), axis = 1)

    A   B   C   new
0   X1  AX  X1  AX_X1
1   X2  X2  X1  X1_X2
2   X3  X3  X3  X3
3   X4  XX  XX  X4_XX

EDIT: Maintain the order of the column values

def my_append(x):
    l = []
    for elm in x:
        if elm not in l:
            l.append(elm)
    return '_'.join(l)

df['New col']=df.astype('str').apply(my_append, axis = 1)

1000 loops, best of 3: 871 µs per loop

Returns

    A   B   C   New col
0   X1  AX  X1  X1_AX
1   X2  X2  X1  X2_X1
2   X3  X3  X3  X3
3   X4  XX  XX  X4_XX

EDIT 1: In case you have nan in any column like this

    A   B   C
0   X1  AX  X1
1   X2  X2  X1
2   X3  X3  X3
3   NaN XX  XX

Handle that in the function and then apply

def my_append(x):
l = []
for elm in x:
    if elm not in l:
        l.append(elm)
l = [x for x in l if str(x) != 'nan']
return '_'.join(l)

df['New col']=df.astype('str').apply(my_append, axis = 1)

    A   B   C   New col
0   X1  AX  X1  X1_AX
1   X2  X2  X1  X2_X1
2   X3  X3  X3  X3
3   NaN XX  XX  XX


问:
Google Cloud Dataflow issue with writing the data (TextIO or DatastoreIO)
OK, everyone. Another Dataflow question from a Dataflow newbie. (Just started playing with it this week..)

I'm creating a datapipe to take in a list of product names and generate autocomplete data. The data processing part is all working fine, it seems, but I'm missing something obvious because when I add my last ".apply" to use either DatastoreIO or TextIO to write the data out, I'm getting a syntax error in my IDE that says the following:

"The method apply(DatastoreV1.Write) is undefined for the type ParDo.SingleOutput>,Entity>"

If gives me an option add a cast to the method receiver, but that obviously isn't the answer. Do I need to do some other step before I try to write the data out? My last step before trying to write the data is a call to an Entity helper for Dataflow to change my Pipeline structure from > to , which seems to me like what I'd need to write to Datastore.

I got so frustrated with this thing the last few days, I even decided to write the data to some AVRO files instead so I could just load it in Datastore by hand. Imagine how ticked I was when I got all that done and got the exact same error in the exact same place on my call to TextIO. That is why I think I must be missing something very obvious here.

Here is my code. I included it all for reference, but you probably just need to look at the main[] at the bottom. Any input would be greatly appreciated! Thanks!

MrSimmonsSr

```<code>package com.client.autocomplete;

import com.client.autocomplete.AutocompleteOptions;

import com.google.datastore.v1.Entity;
import com.google.datastore.v1.Key;
import com.google.datastore.v1.Value;

import static com.google.datastore.v1.client.DatastoreHelper.makeKey;
import static com.google.datastore.v1.client.DatastoreHelper.makeValue;
import org.apache.beam.sdk.coders.DefaultCoder;

import org.apache.beam.sdk.Pipeline;
import org.apache.beam.sdk.PipelineResult;
import org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO;
import org.apache.beam.sdk.values.PCollection;
import org.apache.beam.sdk.values.PCollectionList;
import com.google.api.services.bigquery.model.TableRow;
import com.google.common.base.MoreObjects;
import org.apache.beam.sdk.io.TextIO;
import org.apache.beam.sdk.io.gcp.datastore.DatastoreIO;
import org.apache.beam.sdk.options.PipelineOptionsFactory;
import org.apache.beam.sdk.transforms.PTransform;
import org.apache.beam.sdk.transforms.Create;
import org.apache.beam.sdk.transforms.DoFn;
import org.apache.beam.sdk.transforms.MapElements;
import org.apache.beam.sdk.transforms.ParDo;
import org.apache.beam.sdk.transforms.SimpleFunction;
import org.apache.beam.sdk.transforms.GroupByKey;
import org.apache.beam.sdk.transforms.DoFn.ProcessContext;
import org.apache.beam.sdk.transforms.DoFn.ProcessElement;
import org.apache.beam.sdk.extensions.jackson.ParseJsons;
import org.apache.beam.sdk.values.KV;
import org.apache.beam.sdk.options.Default;
import org.apache.beam.sdk.options.Description;
import org.apache.beam.sdk.options.PipelineOptionsFactory;
import org.apache.beam.sdk.options.StreamingOptions;
import org.apache.beam.sdk.options.Validation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;

/*
 * A simple Dataflow pipeline to create autocomplete data from a list of
 * product names. It then loads that prefix data into Google Cloud Datastore for consumption by
 * a Google Cloud Function. That function will take in a prefix and return a list of 10 product names
 *
 * Pseudo Code Steps
 * 1. Load a list of product names from Cloud Storage
 * 2. Generate prefixes for use with autocomplete, based on the product names
 * 3. Merge the prefix data together with 10 products per prefix
 * 4. Write that  prefix data to the Cloud Datastore as a KV with a <String>, List<String> structure
 *
 */

public class ClientAutocompletePipeline {
    private static final Logger LOG = LoggerFactory.getLogger(ClientAutocompletePipeline.class);

    /**
     * A DoFn that keys each product name by all of its prefixes.
     * This creates one row in the PCollection for each prefix<->product_name pair
     */
    private static class AllPrefixes
    extends DoFn<String, KV<String, String>> {
        private final int minPrefix;
        private final int maxPrefix;

        public AllPrefixes(int minPrefix) {
            this(minPrefix, 10);
        }

        public AllPrefixes(int minPrefix, int maxPrefix) {
            this.minPrefix = minPrefix;
            this.maxPrefix = maxPrefix;
        }
        @ProcessElement
        public void processElement(ProcessContext c) {
            String productName= c.element().toString();
            for (int i = minPrefix; i <= Math.min(productName.length(), maxPrefix); i++) {
                c.output(KV.of(productName.substring(0, i), c.element()));
            }
        }
    }

    /**
     * Takes as input the top product names per prefix, and emits an entity
     * suitable for writing to Cloud Datastore.
     *
     */
    static class FormatForDatastore extends DoFn<KV<String, List<String>>, Entity> {
        private String kind;
        private String ancestorKey;

        public FormatForDatastore(String kind, String ancestorKey) {
            this.kind = kind;
            this.ancestorKey = ancestorKey;
        }

        @ProcessElement
        public void processElement(ProcessContext c) {
            // Initialize an EntityBuilder and get it a valid key
            Entity.Builder entityBuilder = Entity.newBuilder();
            Key key = makeKey(kind, ancestorKey).build();
            entityBuilder.setKey(key);

            // New HashMap to hold all the properties of the Entity
            Map<String, Value> properties = new HashMap<>();
            String prefix = c.element().getKey();
            String productsString = "Products[";

            // iterate through the product names and add each one to the productsString
            for (String productName : c.element().getValue()) {
                // products.add(productName);
                productsString += productName + ", ";
            }
            productsString += "]";

            properties.put("prefix", makeValue(prefix).build());
            properties.put("products", makeValue(productsString).build());
            entityBuilder.putAllProperties(properties);
            c.output(entityBuilder.build());
        }
    }

    /**
     * Options supported by this class.
     *
     * <p>Inherits standard Beam example configuration options.
     */
    public interface Options
    extends AutocompleteOptions {
        @Description("Input text file")
        @Validation.Required
        String getInputFile();
        void setInputFile(String value);

        @Description("Cloud Datastore entity kind")
        @Default.String("prefix-product-map")
        String getKind();
        void setKind(String value);

        @Description("Whether output to Cloud Datastore")
        @Default.Boolean(true)
        Boolean getOutputToDatastore();
        void setOutputToDatastore(Boolean value);

        @Description("Cloud Datastore ancestor key")
        @Default.String("root")
        String getDatastoreAncestorKey();
        void setDatastoreAncestorKey(String value);

        @Description("Cloud Datastore output project ID, defaults to project ID")
        String getOutputProject();
        void setOutputProject(String value);
    }

    public static void main(String[] args)  throws IOException{

        Options options = PipelineOptionsFactory.fromArgs(args).withValidation().as(Options.class);

        //  create the pipeline
        Pipeline p = Pipeline.create(options);

        PCollection<String> toWrite = p

            // A step to read in the product names from a text file on GCS
            .apply(TextIO.read().from("gs://sample-product-data/clean_product_names.txt"))

            // Next expand the product names into KV pairs with prefix as key (<KV<String, String>>)
            .apply("Explode Prefixes", ParDo.of(new AllPrefixes(2)))

            // Apply a GroupByKey transform to the PCollection "flatCollection" to create "productsGroupedByPrefix".
            .apply(GroupByKey.<String, String>create())

            // Now format the PCollection for writing into the Google Datastore
            .apply("FormatForDatastore", ParDo.of(new FormatForDatastore(options.getKind(),
                    options.getDatastoreAncestorKey()))

            // Write the processed data to the Google Cloud Datastore
            // NOTE: This is the line that I'm getting the error on!!
            .apply(DatastoreIO.v1().write().withProjectId(MoreObjects.firstNonNull(
                    options.getOutputProject(), options.getOutputProject()))));

        // Run the pipeline.
        PipelineResult result = p.run();
    }
}
</code>```

答:
I think you need another closing parenthesis. I've removed some of the extraneous bits and reindent according to the parentheses:

```<code>PCollection<String> toWrite = p
    .apply(TextIO.read().from("..."))
    .apply("Explode Prefixes", ...)
    .apply(GroupByKey.<String, String>create())
    .apply("FormatForDatastore", ParDo.of(new FormatForDatastore(
      options.getKind(), options.getDatastoreAncestorKey()))
        .apply(...);
</code>```

Specifically, you need another parenthesis to close the apply("FormatForDatastore", ...). Right now, it is trying to call ParDo.of(...).apply(...) which doesn't work.

Body:
OK, everyone. Another Dataflow question from a Dataflow newbie. (Just started playing with it this week..)

I'm creating a datapipe to take in a list of product names and generate autocomplete data. The data processing part is all working fine, it seems, but I'm missing something obvious because when I add my last ".apply" to use either DatastoreIO or TextIO to write the data out, I'm getting a syntax error in my IDE that says the following:

"The method apply(DatastoreV1.Write) is undefined for the type ParDo.SingleOutput>,Entity>"

If gives me an option add a cast to the method receiver, but that obviously isn't the answer. Do I need to do some other step before I try to write the data out? My last step before trying to write the data is a call to an Entity helper for Dataflow to change my Pipeline structure from > to , which seems to me like what I'd need to write to Datastore.

I got so frustrated with this thing the last few days, I even decided to write the data to some AVRO files instead so I could just load it in Datastore by hand. Imagine how ticked I was when I got all that done and got the exact same error in the exact same place on my call to TextIO. That is why I think I must be missing something very obvious here.

Here is my code. I included it all for reference, but you probably just need to look at the main[] at the bottom. Any input would be greatly appreciated! Thanks!

MrSimmonsSr

package com.client.autocomplete;

import com.client.autocomplete.AutocompleteOptions;

import com.google.datastore.v1.Entity;
import com.google.datastore.v1.Key;
import com.google.datastore.v1.Value;

import static com.google.datastore.v1.client.DatastoreHelper.makeKey;
import static com.google.datastore.v1.client.DatastoreHelper.makeValue;
import org.apache.beam.sdk.coders.DefaultCoder;

import org.apache.beam.sdk.Pipeline;
import org.apache.beam.sdk.PipelineResult;
import org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO;
import org.apache.beam.sdk.values.PCollection;
import org.apache.beam.sdk.values.PCollectionList;
import com.google.api.services.bigquery.model.TableRow;
import com.google.common.base.MoreObjects;
import org.apache.beam.sdk.io.TextIO;
import org.apache.beam.sdk.io.gcp.datastore.DatastoreIO;
import org.apache.beam.sdk.options.PipelineOptionsFactory;
import org.apache.beam.sdk.transforms.PTransform;
import org.apache.beam.sdk.transforms.Create;
import org.apache.beam.sdk.transforms.DoFn;
import org.apache.beam.sdk.transforms.MapElements;
import org.apache.beam.sdk.transforms.ParDo;
import org.apache.beam.sdk.transforms.SimpleFunction;
import org.apache.beam.sdk.transforms.GroupByKey;
import org.apache.beam.sdk.transforms.DoFn.ProcessContext;
import org.apache.beam.sdk.transforms.DoFn.ProcessElement;
import org.apache.beam.sdk.extensions.jackson.ParseJsons;
import org.apache.beam.sdk.values.KV;
import org.apache.beam.sdk.options.Default;
import org.apache.beam.sdk.options.Description;
import org.apache.beam.sdk.options.PipelineOptionsFactory;
import org.apache.beam.sdk.options.StreamingOptions;
import org.apache.beam.sdk.options.Validation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;

/*
 * A simple Dataflow pipeline to create autocomplete data from a list of
 * product names. It then loads that prefix data into Google Cloud Datastore for consumption by
 * a Google Cloud Function. That function will take in a prefix and return a list of 10 product names
 *
 * Pseudo Code Steps
 * 1. Load a list of product names from Cloud Storage
 * 2. Generate prefixes for use with autocomplete, based on the product names
 * 3. Merge the prefix data together with 10 products per prefix
 * 4. Write that  prefix data to the Cloud Datastore as a KV with a <String>, List<String> structure
 *
 */

public class ClientAutocompletePipeline {
    private static final Logger LOG = LoggerFactory.getLogger(ClientAutocompletePipeline.class);

    /**
     * A DoFn that keys each product name by all of its prefixes.
     * This creates one row in the PCollection for each prefix<->product_name pair
     */
    private static class AllPrefixes
    extends DoFn<String, KV<String, String>> {
        private final int minPrefix;
        private final int maxPrefix;

        public AllPrefixes(int minPrefix) {
            this(minPrefix, 10);
        }

        public AllPrefixes(int minPrefix, int maxPrefix) {
            this.minPrefix = minPrefix;
            this.maxPrefix = maxPrefix;
        }
        @ProcessElement
        public void processElement(ProcessContext c) {
            String productName= c.element().toString();
            for (int i = minPrefix; i <= Math.min(productName.length(), maxPrefix); i++) {
                c.output(KV.of(productName.substring(0, i), c.element()));
            }
        }
    }

    /**
     * Takes as input the top product names per prefix, and emits an entity
     * suitable for writing to Cloud Datastore.
     *
     */
    static class FormatForDatastore extends DoFn<KV<String, List<String>>, Entity> {
        private String kind;
        private String ancestorKey;

        public FormatForDatastore(String kind, String ancestorKey) {
            this.kind = kind;
            this.ancestorKey = ancestorKey;
        }

        @ProcessElement
        public void processElement(ProcessContext c) {
            // Initialize an EntityBuilder and get it a valid key
            Entity.Builder entityBuilder = Entity.newBuilder();
            Key key = makeKey(kind, ancestorKey).build();
            entityBuilder.setKey(key);

            // New HashMap to hold all the properties of the Entity
            Map<String, Value> properties = new HashMap<>();
            String prefix = c.element().getKey();
            String productsString = "Products[";

            // iterate through the product names and add each one to the productsString
            for (String productName : c.element().getValue()) {
                // products.add(productName);
                productsString += productName + ", ";
            }
            productsString += "]";

            properties.put("prefix", makeValue(prefix).build());
            properties.put("products", makeValue(productsString).build());
            entityBuilder.putAllProperties(properties);
            c.output(entityBuilder.build());
        }
    }

    /**
     * Options supported by this class.
     *
     *
Inherits standard Beam example configuration options.
     */
    public interface Options
    extends AutocompleteOptions {
        @Description("Input text file")
        @Validation.Required
        String getInputFile();
        void setInputFile(String value);

        @Description("Cloud Datastore entity kind")
        @Default.String("prefix-product-map")
        String getKind();
        void setKind(String value);

        @Description("Whether output to Cloud Datastore")
        @Default.Boolean(true)
        Boolean getOutputToDatastore();
        void setOutputToDatastore(Boolean value);

        @Description("Cloud Datastore ancestor key")
        @Default.String("root")
        String getDatastoreAncestorKey();
        void setDatastoreAncestorKey(String value);

        @Description("Cloud Datastore output project ID, defaults to project ID")
        String getOutputProject();
        void setOutputProject(String value);
    }

    public static void main(String[] args)  throws IOException{

        Options options = PipelineOptionsFactory.fromArgs(args).withValidation().as(Options.class);

        //  create the pipeline
        Pipeline p = Pipeline.create(options);

        PCollection<String> toWrite = p

            // A step to read in the product names from a text file on GCS
            .apply(TextIO.read().from("gs://sample-product-data/clean_product_names.txt"))

            // Next expand the product names into KV pairs with prefix as key (<KV<String, String>>)
            .apply("Explode Prefixes", ParDo.of(new AllPrefixes(2)))

            // Apply a GroupByKey transform to the PCollection "flatCollection" to create "productsGroupedByPrefix".
            .apply(GroupByKey.<String, String>create())

            // Now format the PCollection for writing into the Google Datastore
            .apply("FormatForDatastore", ParDo.of(new FormatForDatastore(options.getKind(),
                    options.getDatastoreAncestorKey()))

            // Write the processed data to the Google Cloud Datastore
            // NOTE: This is the line that I'm getting the error on!!
            .apply(DatastoreIO.v1().write().withProjectId(MoreObjects.firstNonNull(
                    options.getOutputProject(), options.getOutputProject()))));

        // Run the pipeline.
        PipelineResult result = p.run();
    }
}

Body:
I think you need another closing parenthesis. I've removed some of the extraneous bits and reindent according to the parentheses:

PCollection<String> toWrite = p
    .apply(TextIO.read().from("..."))
    .apply("Explode Prefixes", ...)
    .apply(GroupByKey.<String, String>create())
    .apply("FormatForDatastore", ParDo.of(new FormatForDatastore(
      options.getKind(), options.getDatastoreAncestorKey()))
        .apply(...);

Specifically, you need another parenthesis to close the apply("FormatForDatastore", ...). Right now, it is trying to call ParDo.of(...).apply(...) which doesn't work.


问:
calling main method from another method in the same class
How to call the main method?

```<code>void prompt()
{
    System.out.println("Do you want to continue?");
    Scanner confirm = new Scanner(System.in);
String con = confirm.nextLine();
if (con  == "y")
{
//call the main method once again.
}
}
</code>```

When I use
    main();
    It asks for the value of "args" yet I'm not sure what value I should put in it.

答:
Signature of main method is: public static void main(String[] args)

The main method accepts a single argument: an array of elements of type String.

public static void main(String[] args)

This array is the mechanism through which the runtime system passes information to your application. For example:

```<code>public static void main(String[] args) {
   System.out.println("args = " + args);
}

public static void prompt() {
    System.out.println("Do you want to continue?");
    Scanner confirm = new Scanner(System.in);
    String con = confirm.nextLine();
    if (con  == "y") {

      String[] args = {<set string array>};
      main(args);

    }

}
</code>```

For more details, look at this Oracle document: The main Method

Body:
How to call the main method?

void prompt()
{
    System.out.println("Do you want to continue?");
    Scanner confirm = new Scanner(System.in);
String con = confirm.nextLine();
if (con  == "y")
{
//call the main method once again.
}
}

When I use
    main();
    It asks for the value of "args" yet I'm not sure what value I should put in it.

Body:
Signature of main method is: public static void main(String[] args)

The main method accepts a single argument: an array of elements of type String.

public static void main(String[] args)

This array is the mechanism through which the runtime system passes information to your application. For example:

public static void main(String[] args) {
   System.out.println("args = " + args);
}

public static void prompt() {
    System.out.println("Do you want to continue?");
    Scanner confirm = new Scanner(System.in);
    String con = confirm.nextLine();
    if (con  == "y") {

      String[] args = {<set string array>};
      main(args);

    }

}

For more details, look at this Oracle document: The main Method


问:
Scale tiny low-resolution app to larger screen size
I am creating a retro arcade game in Java. The screen resolution for the game is 304 x 256, which I want to keep to preserve the retro characteristics of the game (visuals, animations, font blockiness, etc.).

But when I render this on a large desktop display, it is too small, as one would expect.

I'd like to be able to scale the window up say by a constant factor, without having to code the various paint(Graphics) methods to be knowledgeable about the fact that there's a scale-up. That is, I'd like the rendering code believe that the screen is 304 x 256. I also don't want to have to change my desktop resolution or go into full screen exclusive mode. Just want a big window with scaled up pixels, essentially.

I'd be looking for something along the following lines:

```<code>scale(myJFrame, 4);
</code>```

and have all the contents automatically scale up.

UPDATE: Regarding input, my game happens to use keyboard input, so I don't myself need the inverse transform that trashgod describes. Still I can imagine that others would need that, so I think it's an appropriate suggestion.

答:
One approach, suggested here, is to rely on drawImage() to scale an image of the content. Your game would render itself in the graphics context of a BufferedImage, rather than your implementation of paintComponent(). If the game includes mouse interaction, you'll have to scale the mouse coordinates as shown. In the variation below, I've given the CENTER panel a preferred size that is a multiple of SCALE = 8 and added the original as an icon in the WEST of a BorderLayout. As the default, CENTER, ignores a component's preferred size, you may want to add it to a (possibly nested) panel having FlowLayout. Resize the frame to see the effect.

```<code>f.setLayout(new FlowLayout());
f.add(new Grid(NAME));
//f.add(new JLabel(ICON), BorderLayout.WEST);
</code>```

```<code>import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;

/**
 * @see https://stackoverflow.com/a/44373975/230513
 * @see http://stackoverflow.com/questions/2900801
 */
public class Grid extends JPanel implements MouseMotionListener {

    private static final String NAME = "OptionPane.informationIcon";
    private static final Icon ICON = UIManager.getIcon(NAME);
    private static final int SCALE = 8;
    private final BufferedImage image;
    private int imgW, imgH, paneW, paneH;

    public Grid(String name) {
        super(true);
        imgW = ICON.getIconWidth();
        imgH = ICON.getIconHeight();
        image = new BufferedImage(imgW, imgH, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = (Graphics2D) image.getGraphics();
        ICON.paintIcon(null, g2d, 0, 0);
        g2d.dispose();
        this.addMouseMotionListener(this);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(imgW * SCALE, imgH * SCALE);
    }

    @Override
    protected void paintComponent(Graphics g) {
        paneW = this.getWidth();
        paneH = this.getHeight();
        g.drawImage(image, 0, 0, paneW, paneH, null);
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        Point p = e.getPoint();
        int x = p.x * imgW / paneW;
        int y = p.y * imgH / paneH;
        int c = image.getRGB(x, y);
        this.setToolTipText(x + "," + y + ": "
            + String.format("%08X", c));
    }

    @Override
    public void mouseDragged(MouseEvent e) {
    }

    private static void create() {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new Grid(NAME));
        f.add(new JLabel(ICON), BorderLayout.WEST);
        f.pack();
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                create();
            }
        });
    }
}
</code>```

Body:
I am creating a retro arcade game in Java. The screen resolution for the game is 304 x 256, which I want to keep to preserve the retro characteristics of the game (visuals, animations, font blockiness, etc.).

But when I render this on a large desktop display, it is too small, as one would expect.

I'd like to be able to scale the window up say by a constant factor, without having to code the various paint(Graphics) methods to be knowledgeable about the fact that there's a scale-up. That is, I'd like the rendering code believe that the screen is 304 x 256. I also don't want to have to change my desktop resolution or go into full screen exclusive mode. Just want a big window with scaled up pixels, essentially.

I'd be looking for something along the following lines:

scale(myJFrame, 4);

and have all the contents automatically scale up.

UPDATE: Regarding input, my game happens to use keyboard input, so I don't myself need the inverse transform that trashgod describes. Still I can imagine that others would need that, so I think it's an appropriate suggestion.

Body:
One approach, suggested here, is to rely on drawImage() to scale an image of the content. Your game would render itself in the graphics context of a BufferedImage, rather than your implementation of paintComponent(). If the game includes mouse interaction, you'll have to scale the mouse coordinates as shown. In the variation below, I've given the CENTER panel a preferred size that is a multiple of SCALE = 8 and added the original as an icon in the WEST of a BorderLayout. As the default, CENTER, ignores a component's preferred size, you may want to add it to a (possibly nested) panel having FlowLayout. Resize the frame to see the effect.

f.setLayout(new FlowLayout());
f.add(new Grid(NAME));
//f.add(new JLabel(ICON), BorderLayout.WEST);

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;

/**
 * @see https://stackoverflow.com/a/44373975/230513
 * @see http://stackoverflow.com/questions/2900801
 */
public class Grid extends JPanel implements MouseMotionListener {

    private static final String NAME = "OptionPane.informationIcon";
    private static final Icon ICON = UIManager.getIcon(NAME);
    private static final int SCALE = 8;
    private final BufferedImage image;
    private int imgW, imgH, paneW, paneH;

    public Grid(String name) {
        super(true);
        imgW = ICON.getIconWidth();
        imgH = ICON.getIconHeight();
        image = new BufferedImage(imgW, imgH, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = (Graphics2D) image.getGraphics();
        ICON.paintIcon(null, g2d, 0, 0);
        g2d.dispose();
        this.addMouseMotionListener(this);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(imgW * SCALE, imgH * SCALE);
    }

    @Override
    protected void paintComponent(Graphics g) {
        paneW = this.getWidth();
        paneH = this.getHeight();
        g.drawImage(image, 0, 0, paneW, paneH, null);
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        Point p = e.getPoint();
        int x = p.x * imgW / paneW;
        int y = p.y * imgH / paneH;
        int c = image.getRGB(x, y);
        this.setToolTipText(x + "," + y + ": "
            + String.format("%08X", c));
    }

    @Override
    public void mouseDragged(MouseEvent e) {
    }

    private static void create() {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new Grid(NAME));
        f.add(new JLabel(ICON), BorderLayout.WEST);
        f.pack();
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                create();
            }
        });
    }
}


问:
IIS/HttpHandler: Get relative absolute path?
I've got a site setup in IIS to run at http://localhost/WebApplication6. In my web application I have a handler (implements IHttpHandler). When I print context.Request.Url.AbsolutePath, I get /WebApplication6/whaetever. I want to trim off /WebApplication6 (the local site name). How can I do that? Is there a way to get the "WebApplication6" bit so I know what to trim off? (inside IHttpHandler.ProcessRequest).

答:
Your best bet would be HttpRequest.AppRelativeCurrentExecutionFilePath - it provides path relative to your web application root directory. However, it will be in form of "~/whatever" where ~/ indicates app relative path. If your requirement is to get /whatever then you can strip off ~ using string functions.

BTW, here's good article that will help you make sense of all paths: http://www.west-wind.com/weblog/posts/132081.aspx


问:
How to set view to a specific fragment when clicked recognized in navigation drawer in Android?
I have used a sectionsPageAdapter class in Java which is given below.
This is a tabbed activity and I later added a navigation drawer to it.
Now I wish to let user navigate through tabs from the main activity by swiping (which works successfully) and also through nav drawer as I have included the same fragments names there also.
what code should i write in the switch case to direct user to the clicked fragment activity?

Main Activity.java

```<code>public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    private SectionsPageAdapter mSectionsPageAdapter;
    private ViewPager mViewPager;
    private ArrayAdapter<String> mAdapter;
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG,"onCreate: Starting");

        mSectionsPageAdapter =new SectionsPageAdapter(getSupportFragmentManager());

        mViewPager =(ViewPager)findViewById(R.id.container);
        setupViewPager(mViewPager);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);
        mDrawerLayout =(DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList=(ListView)findViewById(R.id.navList);
        addDrawerItems();
        mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position)
                {
                    case 0:
                          // what Code??
                        break;
                    case 1:

                        break;
                }
            }
        });

    }

    private void setupViewPager(ViewPager viewPager){
        SectionsPageAdapter adapter=new SectionsPageAdapter(getSupportFragmentManager());
        adapter.addFragment(new Tab1Fragment(),"Our Picks");
        adapter.addFragment(new Tab2Fragment(),"Technology");
        adapter.addFragment(new Tab3Fragment(),"Fashion");
        viewPager.setAdapter(adapter);
    }

    private void addDrawerItems() {
        String[] osArray = { "Our Picks", "Technology","Fashion" };
        mAdapter = new ArrayAdapter<String>(this,R.layout.drawerpart1, osArray);
        mDrawerList.setAdapter(mAdapter);
    }
</code>```

SectionsPageAdapter.java

```<code>public class SectionsPageAdapter extends FragmentPagerAdapter {

    private final List<Fragment> mFragmentList=new ArrayList<>();
    private final List<String> mFragmentTitleList=new ArrayList<>();

    public void addFragment(Fragment fragment,String title){
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);

    }

    public SectionsPageAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }
}
</code>```

activity_main.xml

```<code><?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/drawer_layout"
android:layout_height="match_parent"
tools:context="com.example.aa.news.MainActivity">

    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        >

        <android.support.v4.view.ViewPager
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="@dimen/appbar_padding_top"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </android.support.design.widget.AppBarLayout>

    </android.support.design.widget.CoordinatorLayout>

<ListView
    android:id="@+id/navList"
    android:layout_width="250dp"
    android:layout_height="match_parent"
    android:layout_gravity="left|start"
    android:background="#ffeeeeee"/>

</android.support.v4.widget.DrawerLayout>
</code>```

drawerpart1.xml

```<code><?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/label"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dip"
    android:textSize="16dip"
    android:textStyle="bold" >
</TextView>
</code>```

Tab1Fragment

```<code>public class Tab1Fragment extends Fragment {
    private static final String TAG = "Tab1Fragment";
    private Button btn1;

    @Nullable
    @Override
    public View onCreateView(@Nullable LayoutInflater inflater, @Nullable ViewGroup container,@Nullable Bundle savedInstanceState)
    {
        View view= inflater.inflate(R.layout.tab1_fragment,container,false);
        btn1=(Button) view.findViewById(R.id.btn1);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(),"Button 1 Clicked",Toast.LENGTH_SHORT).show();
            }
        });

        return view;

    }

}
</code>```

tab1_fragment.xml

```<code><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:id="@+id/btn1"
        android:text="Button 1" />

    <TextView
        android:layout_height="60dp"
        android:layout_width="120dp"
        android:id="@+id/tv1"
        android:text="Fragment 1"/>
</LinearLayout>
</code>```

The other fragments tab2 and 3 are same as tab 1.

答:
Use setCurrentItem() on viewpager</p>

```<code>mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
    @Override
    public void onItemClick (AdapterView < ? > parent, View view,int position, long id){
        mViewPager.setCurrentItem(position);
    }
});
</code>```

Body:
I have used a sectionsPageAdapter class in Java which is given below.
This is a tabbed activity and I later added a navigation drawer to it.
Now I wish to let user navigate through tabs from the main activity by swiping (which works successfully) and also through nav drawer as I have included the same fragments names there also.
what code should i write in the switch case to direct user to the clicked fragment activity?

Main Activity.java

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    private SectionsPageAdapter mSectionsPageAdapter;
    private ViewPager mViewPager;
    private ArrayAdapter<String> mAdapter;
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG,"onCreate: Starting");

        mSectionsPageAdapter =new SectionsPageAdapter(getSupportFragmentManager());

        mViewPager =(ViewPager)findViewById(R.id.container);
        setupViewPager(mViewPager);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);
        mDrawerLayout =(DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList=(ListView)findViewById(R.id.navList);
        addDrawerItems();
        mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position)
                {
                    case 0:
                          // what Code??
                        break;
                    case 1:

                        break;
                }
            }
        });

    }

    private void setupViewPager(ViewPager viewPager){
        SectionsPageAdapter adapter=new SectionsPageAdapter(getSupportFragmentManager());
        adapter.addFragment(new Tab1Fragment(),"Our Picks");
        adapter.addFragment(new Tab2Fragment(),"Technology");
        adapter.addFragment(new Tab3Fragment(),"Fashion");
        viewPager.setAdapter(adapter);
    }

    private void addDrawerItems() {
        String[] osArray = { "Our Picks", "Technology","Fashion" };
        mAdapter = new ArrayAdapter<String>(this,R.layout.drawerpart1, osArray);
        mDrawerList.setAdapter(mAdapter);
    }

SectionsPageAdapter.java

public class SectionsPageAdapter extends FragmentPagerAdapter {

    private final List<Fragment> mFragmentList=new ArrayList<>();
    private final List<String> mFragmentTitleList=new ArrayList<>();

    public void addFragment(Fragment fragment,String title){
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);

    }

    public SectionsPageAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/drawer_layout"
android:layout_height="match_parent"
tools:context="com.example.aa.news.MainActivity">

    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        >

        <android.support.v4.view.ViewPager
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="@dimen/appbar_padding_top"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </android.support.design.widget.AppBarLayout>

    </android.support.design.widget.CoordinatorLayout>

<ListView
    android:id="@+id/navList"
    android:layout_width="250dp"
    android:layout_height="match_parent"
    android:layout_gravity="left|start"
    android:background="#ffeeeeee"/>

</android.support.v4.widget.DrawerLayout>

drawerpart1.xml

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/label"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dip"
    android:textSize="16dip"
    android:textStyle="bold" >
</TextView>

Tab1Fragment

public class Tab1Fragment extends Fragment {
    private static final String TAG = "Tab1Fragment";
    private Button btn1;

    @Nullable
    @Override
    public View onCreateView(@Nullable LayoutInflater inflater, @Nullable ViewGroup container,@Nullable Bundle savedInstanceState)
    {
        View view= inflater.inflate(R.layout.tab1_fragment,container,false);
        btn1=(Button) view.findViewById(R.id.btn1);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(),"Button 1 Clicked",Toast.LENGTH_SHORT).show();
            }
        });

        return view;

    }

}

tab1_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:id="@+id/btn1"
        android:text="Button 1" />

    <TextView
        android:layout_height="60dp"
        android:layout_width="120dp"
        android:id="@+id/tv1"
        android:text="Fragment 1"/>
</LinearLayout>

The other fragments tab2 and 3 are same as tab 1.

Body:
Use setCurrentItem() on viewpager

mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
    @Override
    public void onItemClick (AdapterView < ? > parent, View view,int position, long id){
        mViewPager.setCurrentItem(position);
    }
});


问:
Redux: what is the correct way to filter a data array in reducer?
i want to filter an array on search SEARCH_TEXT is an on change action
what I'm confused with is how i return the state when the delete key is pressed and the text now becomes empty, i figure i could use initial state in the else statement but my inclination is this is wrong? when i return just state it has all ready been manipulated in the if statement.

simple example.

thanks in advance.

```<code>const initialState =  ['hello', 'wahhh', 'yo'];

export default function searchSimple(state = initialState, action) {
  switch (action.type) {
    case SEARCH_TEXT:
      if(action.text.length > 0){
        return state.filter(item =>
          item.startsWith(action.text)
        )
      }
      else {
        return state
      }
</code>```

答:
IMO, the right place to filter data is not directly in the reducers but in the selectors.

From redux docs:

Computing Derived Data

Reselect is a simple library for creating memoized, composable selector functions. Reselect selectors can be used to efficiently compute derived data from the Redux store.

I'm currently using selectors to filter and sort data.

No data repetition in the state. You don't have to store a copy of the filtered items.

The same data can be used in different components, each one using a different filter for example.

You can combine selector applying many data computations using selector that you already have in the application.

If you do right, your selectors will be pure functions, then you can easily test them.

Use the same selector in many components.

Body:
i want to filter an array on search SEARCH_TEXT is an on change action
what I'm confused with is how i return the state when the delete key is pressed and the text now becomes empty, i figure i could use initial state in the else statement but my inclination is this is wrong? when i return just state it has all ready been manipulated in the if statement.

simple example.

thanks in advance.

const initialState =  ['hello', 'wahhh', 'yo'];

export default function searchSimple(state = initialState, action) {
  switch (action.type) {
    case SEARCH_TEXT:
      if(action.text.length > 0){
        return state.filter(item =>
          item.startsWith(action.text)
        )
      }
      else {
        return state
      }

ContentLicense: CC BY-SA 4.0


问:
Rails YAML Fixture Error with PaperTrail gem
I'm trying to create a rails fixture for testing a controller that fetches versions persisted with the paper_trail gem (7.0.1). But, I've thus far been unable to populate any versions within fixtures to test against. And, it appears that the YAML file isn't loading/is breaking other tests. Here's the contents of my `versions.yml`` file:

```<code>---
version_001:
  id: 1
  item_type: 'DistributionChannel'
  item_id: 1
  event: 'create'
  whodunnit: <%= User.find_by(email: 'user@example.com').id.to_s %>
  object: nil
  created_at: 2017-05-15 12:00:00.000000000 Z
  object_changes: '---\\nid:\\n- \\n- 1\\nprimary_channel:\\n- \\n- Best Buy\\nsecondary_channel:\\n- \\n- ''\ncreated_at:\\n- \\n- 2017-05-15 12:00:00.000000000 Z\\nupdated_at:\\n- \\n- 2017-05-15 12:00:00.000000000 Z\\n'
</code>```

The schema contains the versions table with the appropriate columns btw.

答:
I eventually got the fixture to load by adding the following to test_helper.rb:

self.set_fixture_class versions: PaperTrail::Version

And, the with following yaml syntax:

```<code>version_001:
  id: 1
  item_type: 'DistributionChannel'
  item_id: 1
  event: 'create'
  whodunnit: <%= ActiveRecord::FixtureSet.identify('user_example').to_s %>
  object: nil
  created_at: 2017-05-15 12:00:00
  object_changes: "---\nid:\n- \n- 1\nprimary_channel:\n- \n- Best Buy\nsecondary_channel:\n- \n- ''\ncreated_at:\n- \n- 2017-05-15 12:00:00.000000000 Z\nupdated_at:\n- \n- 2017-05-15 12:00:00.000000000 Z\n"
</code>```

The addition to test_helper.rb is necessary because the Version table is namespaced within PaperTrail. I've seen some conflicting info about appropriate yaml syntax, so I'm hesitant to explain my theories why that yaml syntax worked (any explanations in comments would be greatly appreciated). But, hopefully this will help point someone in the right direction when testing paper_trail with Minitest and using fixtures.

Body:
I'm trying to create a rails fixture for testing a controller that fetches versions persisted with the paper_trail gem (7.0.1). But, I've thus far been unable to populate any versions within fixtures to test against. And, it appears that the YAML file isn't loading/is breaking other tests. Here's the contents of my `versions.yml`` file:

---
version_001:
  id: 1
  item_type: 'DistributionChannel'
  item_id: 1
  event: 'create'
  whodunnit: <%= User.find_by(email: 'user@example.com').id.to_s %>
  object: nil
  created_at: 2017-05-15 12:00:00.000000000 Z
  object_changes: '---\\nid:\\n- \\n- 1\\nprimary_channel:\\n- \\n- Best Buy\\nsecondary_channel:\\n- \\n- ''\ncreated_at:\\n- \\n- 2017-05-15 12:00:00.000000000 Z\\nupdated_at:\\n- \\n- 2017-05-15 12:00:00.000000000 Z\\n'

The schema contains the versions table with the appropriate columns btw.

Body:
I eventually got the fixture to load by adding the following to test_helper.rb:

self.set_fixture_class versions: PaperTrail::Version

And, the with following yaml syntax:

version_001:
  id: 1
  item_type: 'DistributionChannel'
  item_id: 1
  event: 'create'
  whodunnit: <%= ActiveRecord::FixtureSet.identify('user_example').to_s %>
  object: nil
  created_at: 2017-05-15 12:00:00
  object_changes: "---\nid:\n- \n- 1\nprimary_channel:\n- \n- Best Buy\nsecondary_channel:\n- \n- ''\ncreated_at:\n- \n- 2017-05-15 12:00:00.000000000 Z\nupdated_at:\n- \n- 2017-05-15 12:00:00.000000000 Z\n"

The addition to test_helper.rb is necessary because the Version table is namespaced within PaperTrail. I've seen some conflicting info about appropriate yaml syntax, so I'm hesitant to explain my theories why that yaml syntax worked (any explanations in comments would be greatly appreciated). But, hopefully this will help point someone in the right direction when testing paper_trail with Minitest and using fixtures.


问:
Error with Spark: NoSuchMethodError: scala.Predef$.$conforms()Lscala/Predef$$less$colon$less
I've got an application which streams Twitter posts.
It worked correctly, but after modifying the build path, I didn't manage to set it how it was before. I keep getting this error:

```<code>Exception in thread "main" java.lang.NoSuchMethodError: scala.Predef$.$conforms()Lscala/Predef$$less$colon$less;
at org.apache.spark.util.Utils$.getSystemProperties(Utils.scala:1710)
at org.apache.spark.SparkConf.loadFromSystemProperties(SparkConf.scala:73)
at org.apache.spark.SparkConf.<init>(SparkConf.scala:68)
at org.apache.spark.SparkConf.<init>(SparkConf.scala:55)
at Analytics.init(Analytics.java:20)
at KafkaTwitterProducer.main(KafkaTwitterProducer.java:162)
</code>```

This is my pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>TwitterStreamingAnalyzer</groupId>
  <artifactId>TwitterStreamingAnalyzer</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
   <dependency>
                <groupId>org.apache.kafka</groupId>
                <artifactId>kafka_2.10</artifactId>
                <version>0.10.2.1</version>
           </dependency>

           <dependency>
                <groupId>org.twitter4j</groupId>
                <artifactId>twitter4j-core</artifactId>
                <version>4.0.4</version>
           </dependency>

           <dependency>
                <groupId>org.twitter4j</groupId>
                <artifactId>twitter4j-stream</artifactId>
                <version>4.0.4</version>
           </dependency>

           <dependency>
                <groupId>org.twitter4j</groupId>
                <artifactId>twitter4j-async</artifactId>
                <version>4.0.4</version>
           </dependency>

     </dependencies>
</project>```

I already read all of the similar questions, but none of them helped me, because I tried to import the right versions of the jars, but it still doesn't work.
How can I fix this?

答:
Looks like you are trying to use a kafka library compiled in Scala 2.10 in a Spark cluster compiled in other version (probably 2.11). Try this change in your pom.xml:</p>

```<code><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>TwitterStreamingAnalyzer</groupId>
  <artifactId>TwitterStreamingAnalyzer</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
   <dependency>
                <groupId>org.apache.kafka</groupId>
                <artifactId>kafka_2.11</artifactId>
                <version>0.10.2.1</version>
           </dependency>
           <!-- https://mvnrepository.com/artifact/org.twitter4j/twitter4j-core -->
           <dependency>
                <groupId>org.twitter4j</groupId>
                <artifactId>twitter4j-core</artifactId>
                <version>4.0.4</version>
           </dependency>
           <!-- https://mvnrepository.com/artifact/org.twitter4j/twitter4j-stream -->
           <dependency>
                <groupId>org.twitter4j</groupId>
                <artifactId>twitter4j-stream</artifactId>
                <version>4.0.4</version>
           </dependency>
           <!-- https://mvnrepository.com/artifact/org.twitter4j/twitter4j-async -->
           <dependency>
                <groupId>org.twitter4j</groupId>
                <artifactId>twitter4j-async</artifactId>
                <version>4.0.4</version>
           </dependency>

     </dependencies>
</project>
</code>```

Body:
I've got an application which streams Twitter posts.
It worked correctly, but after modifying the build path, I didn't manage to set it how it was before. I keep getting this error:

Exception in thread "main" java.lang.NoSuchMethodError: scala.Predef$.$conforms()Lscala/Predef$$less$colon$less;
at org.apache.spark.util.Utils$.getSystemProperties(Utils.scala:1710)
at org.apache.spark.SparkConf.loadFromSystemProperties(SparkConf.scala:73)
at org.apache.spark.SparkConf.<init>(SparkConf.scala:68)
at org.apache.spark.SparkConf.<init>(SparkConf.scala:55)
at Analytics.init(Analytics.java:20)
at KafkaTwitterProducer.main(KafkaTwitterProducer.java:162)

This is my pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>TwitterStreamingAnalyzer</groupId>
  <artifactId>TwitterStreamingAnalyzer</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
   <dependency>
                <groupId>org.apache.kafka</groupId>
                <artifactId>kafka_2.10</artifactId>
                <version>0.10.2.1</version>
           </dependency>

           <dependency>
                <groupId>org.twitter4j</groupId>
                <artifactId>twitter4j-core</artifactId>
                <version>4.0.4</version>
           </dependency>

           <dependency>
                <groupId>org.twitter4j</groupId>
                <artifactId>twitter4j-stream</artifactId>
                <version>4.0.4</version>
           </dependency>

           <dependency>
                <groupId>org.twitter4j</groupId>
                <artifactId>twitter4j-async</artifactId>
                <version>4.0.4</version>
           </dependency>

     </dependencies>
</project>

I already read all of the similar questions, but none of them helped me, because I tried to import the right versions of the jars, but it still doesn't work.
How can I fix this?

Body:
Looks like you are trying to use a kafka library compiled in Scala 2.10 in a Spark cluster compiled in other version (probably 2.11). Try this change in your pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>TwitterStreamingAnalyzer</groupId>
  <artifactId>TwitterStreamingAnalyzer</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
   <dependency>
                <groupId>org.apache.kafka</groupId>
                <artifactId>kafka_2.11</artifactId>
                <version>0.10.2.1</version>
           </dependency>
           <!-- https://mvnrepository.com/artifact/org.twitter4j/twitter4j-core -->
           <dependency>
                <groupId>org.twitter4j</groupId>
                <artifactId>twitter4j-core</artifactId>
                <version>4.0.4</version>
           </dependency>
           <!-- https://mvnrepository.com/artifact/org.twitter4j/twitter4j-stream -->
           <dependency>
                <groupId>org.twitter4j</groupId>
                <artifactId>twitter4j-stream</artifactId>
                <version>4.0.4</version>
           </dependency>
           <!-- https://mvnrepository.com/artifact/org.twitter4j/twitter4j-async -->
           <dependency>
                <groupId>org.twitter4j</groupId>
                <artifactId>twitter4j-async</artifactId>
                <version>4.0.4</version>
           </dependency>

     </dependencies>
</project>


问:
How to determine if a function was called with `.call` or `.apply`?
I created this function so that I could chain methods together:

```<code>export default function bindable(fn) {
    return function boundFn(...args) {
        return this === undefined ? fn(...args) : fn(this, ...args);
    }
}
</code>```

It works with the bind operator. For example, you can define a new function like,

```<code> export const flatten = bindable(arrayOfArrays => Array.prototype.concat(...arrayOfArrays));
</code>```

And then use it like this:

```<code>[[1,2,3],[4,5,6]]::flatten()
</code>```

Or like this:

```<code>flatten([[1,2,3],[4,5,6]])
</code>```

Babel REPL

This worked great at first until I realized that if I use it like in the second scenario, but import the helper method as a module, it breaks the context!

```<code> import * as Arr from './array-helper-methods';
 Arr.flatten([1,2,3]); // `this` is now `Arr`
</code>```

So my question is: is there any way that I can reliably detect if a function is called with the bind operator?

答:
That's a large and yet unsolved problem of the bind operator. But no, it only works for methods. You just should provide two versions of your functions if you want to support both styles.

Doing it dynamically is hard. No, you cannot detect whether the function was called using (), as a method, was bound, with call or apply etc. And you really shouldn't be able to anyway.

For your case, I'd choose

```<code>function bindable(fn, isContext) {
    return function boundFn(...args) {
        return isContext(this) ? fn(this, ...args) : fn(...args);
    }
}
export default bindable(bindable, f => typeof f == "function");
</code>```

Then you can use bindable(flatten, Array.isArray) or flatten::bindable(Array.isArray) for flatten. For more complicated cases, you can also incorporate the arity of the function, i.e. whether fn.length + 1 == args.length.

Body:
I created this function so that I could chain methods together:

export default function bindable(fn) {
    return function boundFn(...args) {
        return this === undefined ? fn(...args) : fn(this, ...args);
    }
}

It works with the bind operator. For example, you can define a new function like,

 export const flatten = bindable(arrayOfArrays => Array.prototype.concat(...arrayOfArrays));

And then use it like this:

[[1,2,3],[4,5,6]]::flatten()

Or like this:

flatten([[1,2,3],[4,5,6]])

<a href="https://babeljs.io/repl/#?babili=false&evaluate=true&lineWrap=false&presets=env%2Cstage-0&targets=Node-8&browsers=&builtIns=false&debug=true&experimental=true&loose=true&spec=false&code=function%20bindable(fn)%20%7B%0A%20%20%20%20return%20function%20boundFn(...args)%20%7B%0A%20%20%20%20%20%20%20%20return%20this%20!%3D%3D%20undefined%20%26%26%20args.length%20%3C%20fn.length%20%3F%20this%3A%3Afn(this%2C%20...args)%20%3A%20this%3A%3Afn(...args)%3B%0A%20%20%20%20%7D%0A%7D%0A%0Aconst%20flatten%20%3D%20bindable(arrayOfArrays%20%3D%3E%20Array.prototype.concat(...arrayOfArrays))%3B%0A%0Aconsole.log(%5B%5B1%2C2%2C3%5D%2C%5B4%2C5%2C6%5D%5D%3A%3Aflatten())%3B%0Aconsole.log(flatten(%5B%5B1%2C2%2C3%5D%2C%5B4%2C5%2C6%5D%5D))%3B" rel="nofollow noreferrer">Babel REPL

This worked great at first until I realized that if I use it like in the second scenario, but import the helper method as a module, it breaks the context!

 import * as Arr from './array-helper-methods';
 Arr.flatten([1,2,3]); // `this` is now `Arr`

So my question is: is there any way that I can reliably detect if a function is called with the bind operator?

Body:
That's a large and yet unsolved problem of the bind operator. But no, it only works for methods. You just should provide two versions of your functions if you want to support both styles.

Doing it dynamically is hard. No, you cannot detect whether the function was called using (), as a method, was bound, with call or apply etc. And you really shouldn't be able to anyway.

For your case, I'd choose

function bindable(fn, isContext) {
    return function boundFn(...args) {
        return isContext(this) ? fn(this, ...args) : fn(...args);
    }
}
export default bindable(bindable, f => typeof f == "function");

Then you can use bindable(flatten, Array.isArray) or flatten::bindable(Array.isArray) for flatten. For more complicated cases, you can also incorporate the arity of the function, i.e. whether fn.length + 1 == args.length.


问:
PHP ini file_get_contents external url
I use following PHP function:

file_get_contents('http://example.com');

Whenever I do this on a certain server, the result is empty. When I do it anywhere else, the result is whatever the page's content may be. When I however, on the server where the result is empty, use the function locally - without accessing an external URL (file_get_contents('../simple/internal/path.html');), it does work.

Now, I am pretty sure it has something to do with a certain php.ini configuration. What I am however not sure about is, which one. Please help.

答:
The answers provided above solve the problem but don't explain the strange behaviour the OP described. This explanation should help anyone testing communication between sites in a development environment where these sites all reside on the same host (and the same virtualhost; I'm working with apache 2.4 and php7.0).

There's a subtlety with file_get_contents() I came across that is absolutely relevant here but unaddressed (probably because it's either barely documented or not documented from what I can tell or is documented in an obscure php security model whitepaper I can't find).

With allow_url_fopen set to Off in all relevant contexts (e.g. /etc/php/7.0/apache2/php.ini, /etc/php/7.0/fpm/php.ini, etc...) and allow_url_fopen set to On in the command line context (i.e. /etc/php/7.0/cli/php.ini), calls to file_get_contents() for a local resource will be allowed and no warning will be logged such as:

```<code>file_get_contents('php://input');
</code>```

or

```<code>// Path outside document root that webserver user agent has permission to read. e.g. for an apache2 webserver this user agent might be www-data so a file at /etc/php/7.0/filetoaccess would be successfully read if www-data had permission to read this file
file_get_contents('<file path to file on local machine user agent can access>');
</code>```

or

```<code>// Relative path in same document root
file_get_contents('data/filename.dat')
</code>```

To conclude, the restriction allow_url_fopen = Off is analogous to an iptables rule in the OUTPUT chain, where the restriction is only applied when an attempt to "exit the system" or "change contexts" is made.

N.B. allow_url_fopen set to On in the command line context (i.e. /etc/php/7.0/cli/php.ini) is what I had on my system but I suspect it would have no bearing on the explanation I provided even if it were set to Off unless of course you're testing by running your scripts from the command line itself. I did not test the behaviour with allow_url_fopen set to Off in the command line context.

Title: PHP ini file_get_contents external url

Body:
The answers provided above solve the problem but don't explain the strange behaviour the OP described. This explanation should help anyone testing communication between sites in a development environment where these sites all reside on the same host (and the same virtualhost; I'm working with apache 2.4 and php7.0).

There's a subtlety with file_get_contents() I came across that is absolutely relevant here but unaddressed (probably because it's either barely documented or not documented from what I can tell or is documented in an obscure php security model whitepaper I can't find).

With allow_url_fopen set to Off in all relevant contexts (e.g. /etc/php/7.0/apache2/php.ini, /etc/php/7.0/fpm/php.ini, etc...) and allow_url_fopen set to On in the command line context (i.e. /etc/php/7.0/cli/php.ini), calls to file_get_contents() for a local resource will be allowed and no warning will be logged such as:

file_get_contents('php://input');

or

// Path outside document root that webserver user agent has permission to read. e.g. for an apache2 webserver this user agent might be www-data so a file at /etc/php/7.0/filetoaccess would be successfully read if www-data had permission to read this file
file_get_contents('<file path to file on local machine user agent can access>');

or

// Relative path in same document root
file_get_contents('data/filename.dat')

To conclude, the restriction allow_url_fopen = Off is analogous to an iptables rule in the OUTPUT chain, where the restriction is only applied when an attempt to "exit the system" or "change contexts" is made.

N.B. allow_url_fopen set to On in the command line context (i.e. /etc/php/7.0/cli/php.ini) is what I had on my system but I suspect it would have no bearing on the explanation I provided even if it were set to Off unless of course you're testing by running your scripts from the command line itself. I did not test the behaviour with allow_url_fopen set to Off in the command line context.


问:
Pivot table in SAS
The data looks as following:

```<code>YEAR    MONTH   ID1 ID2 FIELD   FIELD_DESC
2017    4   123 2222    FFF red1
2017    4   123 2222    FFG red2
2017    4   224 2221    XYZ green1
2017    4   225 1234    TYU blue5
</code>```

and desired output is

```<code>YEAR    MONTH   ID1 ID2 blue5   green1  red1    red2
2017    4   123 2222    0   0   1   1
2017    4   224 2221    0   1   0   0
2017    4   225 1234    1   0   0   0
</code>```

On SQL server I used to run the following:

```<code>select year, month, id1, id2, [field1], [field2] .... [fieldn]
from (select year, month, id1, id2, field, field_desc from source_table) P
Pivot (count(field) for field_desc in ([field1], [field2] .... [fieldn])) Pvt
</code>```

The above would give me counts of field of each possible description by year, month, id1, id2.

I'm trying to translate this to SAS.

One approach would be conditional sums

```<code>proc sql;
create table aggr_table as
select year, month, id1, id2,
sum(case when field_desc = 'field1' then 1 else 0 end) as field1
...
sum(case when field_desc = 'fieldn' then 1 else 0 end) as fiendn
from source_table
group by year, month, id1, id2;
quit;
</code>```

Second way I tried to do it was by

```<code>proc sort data = source_table
 by year descending month descending id1 descending id2;
run;

data table_aggr (keep year month id1 id2 field1 .... fieldn);
set source_table
retain field1
....
fieldn 0);

if field_desc = 'field1' then do;
field1 = field1 +1;
end;
....
if field_desc = 'fieldn' then do;
fieldn = fieldn + 1;
end;

if last.id2 then
output;
by year month id1 id2;
run;
</code>```

But the second way does not seem to work

ERROR: BY variables are not properly sorted on data set
  WORK.SOURCE_TABLE

My questions:
1) My googling so far seems to indicate preference for doing this kind of data manipulation in the data step rather than proc sql step, is there a particular advantage ?

2) What did I do wrong in the data step ?

3) Is there a better way to replicate the SQL code at the top of the post ? Both SAS options I'm looking at look rather unwieldy compared to SQL original.

Thank you
Ben

答:
This is what SAS offers PROCs for.

The CLASS statement in most procs (proc means,proc tabulate in particular) lets you summarize by various levels.  So for example:

```<code>proc means data=sashelp.class;
  var height weight;  *the numeric variables you are calculating with;
  class age sex;      *the grouping variables;
  types () age sex age*sex;   *the interactions you want - or use NWAY or WAYS;
run;
</code>```

Or:

```<code>proc tabulate data=sashelp.class;
  var height weight;   *numeric variables to calculate with;
  class age sex;       *grouping variables;
  tables (all age sex age*sex),(height weight)*n;
run;
</code>```

Depending on the proc, you have multiple ways to then get this information.  Many have an out option or output statement (e.g. PROC MEANS has OUTPUT statement to do this).  Additionally, ODS OUTPUT lets you get access to basically anything that's printed on the screen in table form.

```<code>ods output table=want;
proc tabulate data=sashelp.class;
  var height weight;
  class age sex;
  tables (all age sex age*sex),(height weight)*n;
run;
ods output close;
</code>```

--or--

```<code>proc tabulate data=sashelp.class out=want;
  var height weight;
  class age sex;
  tables (all age sex age*sex),(height weight)*n;
run;
</code>```

Title: Pivot table in SAS

Body:
The data looks as following:

YEAR    MONTH   ID1 ID2 FIELD   FIELD_DESC
2017    4   123 2222    FFF red1
2017    4   123 2222    FFG red2
2017    4   224 2221    XYZ green1
2017    4   225 1234    TYU blue5

and desired output is

YEAR    MONTH   ID1 ID2 blue5   green1  red1    red2
2017    4   123 2222    0   0   1   1
2017    4   224 2221    0   1   0   0
2017    4   225 1234    1   0   0   0

On SQL server I used to run the following:

select year, month, id1, id2, [field1], [field2] .... [fieldn]
from (select year, month, id1, id2, field, field_desc from source_table) P
Pivot (count(field) for field_desc in ([field1], [field2] .... [fieldn])) Pvt

The above would give me counts of field of each possible description by year, month, id1, id2.

I'm trying to translate this to SAS.

One approach would be conditional sums

proc sql;
create table aggr_table as
select year, month, id1, id2,
sum(case when field_desc = 'field1' then 1 else 0 end) as field1
...
sum(case when field_desc = 'fieldn' then 1 else 0 end) as fiendn
from source_table
group by year, month, id1, id2;
quit;

Second way I tried to do it was by

proc sort data = source_table
 by year descending month descending id1 descending id2;
run;

data table_aggr (keep year month id1 id2 field1 .... fieldn);
set source_table
retain field1
....
fieldn 0);

if field_desc = 'field1' then do;
field1 = field1 +1;
end;
....
if field_desc = 'fieldn' then do;
fieldn = fieldn + 1;
end;

if last.id2 then
output;
by year month id1 id2;
run;

But the second way does not seem to work

ERROR: BY variables are not properly sorted on data set
  WORK.SOURCE_TABLE

My questions:
1) My googling so far seems to indicate preference for doing this kind of data manipulation in the data step rather than proc sql step, is there a particular advantage ?

2) What did I do wrong in the data step ?

3) Is there a better way to replicate the SQL code at the top of the post ? Both SAS options I'm looking at look rather unwieldy compared to SQL original.

Thank you
Ben

Body:
This is what SAS offers PROCs for.

The CLASS statement in most procs (proc means,proc tabulate in particular) lets you summarize by various levels.  So for example:

proc means data=sashelp.class;
  var height weight;  *the numeric variables you are calculating with;
  class age sex;      *the grouping variables;
  types () age sex age*sex;   *the interactions you want - or use NWAY or WAYS;
run;

Or:

proc tabulate data=sashelp.class;
  var height weight;   *numeric variables to calculate with;
  class age sex;       *grouping variables;
  tables (all age sex age*sex),(height weight)*n;
run;

Depending on the proc, you have multiple ways to then get this information.  Many have an out option or output statement (e.g. PROC MEANS has OUTPUT statement to do this).  Additionally, ODS OUTPUT lets you get access to basically anything that's printed on the screen in table form.

ods output table=want;
proc tabulate data=sashelp.class;
  var height weight;
  class age sex;
  tables (all age sex age*sex),(height weight)*n;
run;
ods output close;

--or--

proc tabulate data=sashelp.class out=want;
  var height weight;
  class age sex;
  tables (all age sex age*sex),(height weight)*n;
run;