问:
How to safely stop a IOCP WSARecv() task, and free the WSAOVERLAPPED structure?
My IOCP server program will cost more and more memory while running. After tracking memory leak ,I found that some WSAOVERLAPPED structue feed to WSARecv() is never recycled. I think this is because some malignant client socket only establish connection but never send data or close. So i set a TimerQueueTimer() on every client socket to identify the timeout socket and delete them. But if I free the WSAOVERLAPPED structure when deleting the malignant socket, after a little while ,I got "Free Heap block 014C7D80 modified at 014C7DA8 after it was freed".

Here is some of the relevant code:

```<code>typedef struct _SocketState
{
   char operation;
   SOCKET socket;
   DWORD length;
   HANDLE hTimer;
   HANDLE hCompletion;
   WSAOVERLAPPED* thisOvl;
   char buf[MAX_BUF];
} SocketState;

static WSAOVERLAPPED* new_overlapped(void)
{
   return (WSAOVERLAPPED*)calloc(1, sizeof(WSAOVERLAPPED));
}

static void create_io_completion_port(void)
{
   cpl_port = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
   if (!cpl_port)
   {
        int err = WSAGetLastError();
        exit(1);
   }
}

static void post_reading(SocketState* socketState, WSAOVERLAPPED* ovl)
{
   DWORD flags = 0;
   WSABUF wsabuf = { MAX_BUF, socketState->buf };
   int err=0;
   memset(ovl, 0, sizeof(WSAOVERLAPPED));
   socketState->operation = OP_READ;
   socketState->thisOvl=ovl;
   if (WSARecv(socketState->socket, &wsabuf, 1, NULL, &flags, ovl, NULL)== SOCKET_ERROR)
   {
      err = WSAGetLastError();
      if (err != WSA_IO_PENDING)
      {
        printf("[%s:%d]WSARecv error\n", __FUNCTION__, __LINE__);
        destroy_connection(socketState, ovl);
        return ;
      }
   }
   c_WSARecv++;
}

static void destroy_connection(SocketState* socketState, WSAOVERLAPPED* ovl)
{
    int err=0;
    if(socketState->hTimer != NULL)
    {
        DeleteTimerQueueTimer(hTimerQueue,socketState->hTimer,INVALID_HANDLE_VALUE);
        socketState->hTimer = NULL;
    }
    socketState->hCompletion=NULL; //newSocketState->hCompletion = cpl_port
    closesocket(socketState->socket);
    free(socketState);
    if(ovl!=0)
    {
       free(ovl);
    }
}
VOID CALLBACK TimerRoutine(PVOID lpParam, BOOLEAN TimerOrWaitFired)
{
    SocketState* clientSocketState=(SocketState*)lpParam;
    if (lpParam != NULL)
    {
        if(clientSocketState->hCompletion != NULL)
        {
            PostQueuedCompletionStatus(clientSocketState->hCompletion,-2,(ULONG_PTR)clientSocketState,clientSocketState->thisOvl);
            //should last parameter be NULL?
            //the "-2" is for identify this timeout io after GetQueuedCompletionStatus()
        }
    }
}
</code>```

As I am using C rather then C++ for my server program, I am putting my self in very awkward situation. Basically I can't find a very good IOCP example in C :(.

答:
OVERLAPPED we can free only after I/O completed. so really all what need cancel I/O operation. this can be done by call CancelIoEx or better by call closesocket (The closesocket function will initiate cancellation on the outstanding I/O operations). when I/O completed - you got pointer to OVERLAPPED passed to this I/O and after handle I/O result - you can free or reuse OVERLAPPED

you have structure associated with every socket - SocketState this is correct.
but what must and must not have this structure ?

it mandatory must implement reference counting (because it accessing from multiple threads in complex and unpredictable order). and hold handle to the socket. mandatory have some kind of rundown protection for socket handle for not use it after closesocket/CancelIoEx calls (it implementation already separate question). this is need because A Winsock client must never issue closesocket on socket concurrently with another Winsock function call but we need at any time be able call closesocket for cancel I/O on lost remote side.

from another side it must not have pointer to OVERLAPPED (or it shell class) because multiple I/O can be at once on socket. we can in parallel read and write. it must not have operation member too by same reason - read and write operations for example can be active in parallel. so code like

```<code>socketState->operation = OP_READ;
socketState->thisOvl=ovl;
</code>```

is wrong by design. also no sense have hCompletion inside SocketState because hCompletion is not per socket. here is wrong place for store it.

also we need mandatory use not naked OVERLAPPED structure for pass it to I/O but self class inherited from OVERLAPPED. you need have additional members here - referenced pointer to your SocketState - because when I/O completed - you got back pointer to OVERLAPPED and need from it got pointer to your socketState. the operation also mandatory must be here (instead SocketState) because operation is per I/O but not per socket. so in very general can be next:

```<code>struct SocketState
{
    SOCKET socket;
    HANDLE hTimer;
    ULONG dwRefCount;

    void AddRef();
    void Release();

    _NODISCARD SOCKET LockHandle();
    void Rundown();
    void UnlockHandle();// call closesocket on last unlock

    void OnIoComplete(ULONG operation, ULONG dwErrorCode, ULONG dwBytesTransfered, PVOID buf);

    void StartSomeIo(ULONG operation, PVOID buf, ULONG cb);

    void Close()
    {
        if (LockHandle())
        {
            Rundown();
            UnlockHandle();
        }
    }
};

struct UIrp : OVERLAPPED
{
    SocketState* socketState;
    ULONG operation;
    PVOID buf;

    UIrp(SocketState* socketState, ULONG operation, PVOID buf)
        : socketState(socketState), operation(operation), buf(buf)
    {
        RtlZeroMemory(static_cast<OVERLAPPED*>(this), sizeof(OVERLAPPED));
        socketState->AddRef();
    }

    ~UIrp()
    {
        socketState->Release();
    }

    void OnIoComplete(ULONG dwErrorCode, ULONG dwBytesTransfered)
    {
        socketState->OnIoComplete(operation, dwErrorCode, dwBytesTransfered, buf);
        delete this;
    }
};

void SocketState::StartSomeIo(ULONG operation, PVOID buf, ULONG cb)
{
    if (UIrp* irp = new UIrp(this, operation, buf))
    {
        ULONG dwError = ERROR_INVALID_HANDLE;

        if (SOCKET s = LockHandle())
        {
            dwError = WSA*(s,... irp, 0) == 0 ? NOERROR : WSAGetLastError();
            UnlockHandle();
        }

        switch (dwError)
        {
        case NOERROR:
        case WSA_IO_PENDING:
            break;
        default:
            irp->OnIoComplete(dwError, 0);
        }
    }
}

void PortLoop(HANDLE hCompletionPort)
{
    for (;;)
    {
        OVERLAPPED* lpOverlapped;
        ULONG dwBytesTransfered;
        ULONG_PTR CompletionKey;

        ULONG dwError = GetQueuedCompletionStatus(hCompletionPort, &dwBytesTransfered,
            &CompletionKey, &lpOverlapped, INFINITE) ? NOERROR : GetLastError();

        // probably somehow use CompletionKey

        if (!lpOverlapped)
        {
            break;
        }

        static_cast<UIrp*>(lpOverlapped)->OnIoComplete(dwBytesTransfered, dwError);
    }
}
</code>```

about

set set a TimerQueueTimer() on every client socket

this is possible and correct by design but think not the best in case you have many sockets. i be inherit SocketState from LIST_ENTRY and insert all active sockets to the some list. and by timer periodically check timeout and close sockets

Body:
My IOCP server program will cost more and more memory while running. After tracking memory leak ,I found that some WSAOVERLAPPED structue feed to WSARecv() is never recycled. I think this is because some malignant client socket only establish connection but never send data or close. So i set a TimerQueueTimer() on every client socket to identify the timeout socket and delete them. But if I free the WSAOVERLAPPED structure when deleting the malignant socket, after a little while ,I got "Free Heap block 014C7D80 modified at 014C7DA8 after it was freed".

Here is some of the relevant code:

typedef struct _SocketState
{
   char operation;
   SOCKET socket;
   DWORD length;
   HANDLE hTimer;
   HANDLE hCompletion;
   WSAOVERLAPPED* thisOvl;
   char buf[MAX_BUF];
} SocketState;

static WSAOVERLAPPED* new_overlapped(void)
{
   return (WSAOVERLAPPED*)calloc(1, sizeof(WSAOVERLAPPED));
}

static void create_io_completion_port(void)
{
   cpl_port = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
   if (!cpl_port)
   {
        int err = WSAGetLastError();
        exit(1);
   }
}

static void post_reading(SocketState* socketState, WSAOVERLAPPED* ovl)
{
   DWORD flags = 0;
   WSABUF wsabuf = { MAX_BUF, socketState->buf };
   int err=0;
   memset(ovl, 0, sizeof(WSAOVERLAPPED));
   socketState->operation = OP_READ;
   socketState->thisOvl=ovl;
   if (WSARecv(socketState->socket, &wsabuf, 1, NULL, &flags, ovl, NULL)== SOCKET_ERROR)
   {
      err = WSAGetLastError();
      if (err != WSA_IO_PENDING)
      {
        printf("[%s:%d]WSARecv error\n", __FUNCTION__, __LINE__);
        destroy_connection(socketState, ovl);
        return ;
      }
   }
   c_WSARecv++;
}

static void destroy_connection(SocketState* socketState, WSAOVERLAPPED* ovl)
{
    int err=0;
    if(socketState->hTimer != NULL)
    {
        DeleteTimerQueueTimer(hTimerQueue,socketState->hTimer,INVALID_HANDLE_VALUE);
        socketState->hTimer = NULL;
    }
    socketState->hCompletion=NULL; //newSocketState->hCompletion = cpl_port
    closesocket(socketState->socket);
    free(socketState);
    if(ovl!=0)
    {
       free(ovl);
    }
}
VOID CALLBACK TimerRoutine(PVOID lpParam, BOOLEAN TimerOrWaitFired)
{
    SocketState* clientSocketState=(SocketState*)lpParam;
    if (lpParam != NULL)
    {
        if(clientSocketState->hCompletion != NULL)
        {
            PostQueuedCompletionStatus(clientSocketState->hCompletion,-2,(ULONG_PTR)clientSocketState,clientSocketState->thisOvl);
            //should last parameter be NULL?
            //the "-2" is for identify this timeout io after GetQueuedCompletionStatus()
        }
    }
}

As I am using C rather then C++ for my server program, I am putting my self in very awkward situation. Basically I can't find a very good IOCP example in C :(.

Body:
OVERLAPPED we can free only after I/O completed. so really all what need cancel I/O operation. this can be done by call CancelIoEx or better by call closesocket (The closesocket function will initiate cancellation on the outstanding I/O operations). when I/O completed - you got pointer to OVERLAPPED passed to this I/O and after handle I/O result - you can free or reuse OVERLAPPED

you have structure associated with every socket - SocketState this is correct.
but what must and must not have this structure ?

it mandatory must implement reference counting (because it accessing from multiple threads in complex and unpredictable order). and hold handle to the socket. mandatory have some kind of rundown protection for socket handle for not use it after closesocket/CancelIoEx calls (it implementation already separate question). this is need because A Winsock client must never issue closesocket on socket concurrently with another Winsock function call but we need at any time be able call closesocket for cancel I/O on lost remote side.

from another side it must not have pointer to OVERLAPPED (or it shell class) because multiple I/O can be at once on socket. we can in parallel read and write. it must not have operation member too by same reason - read and write operations for example can be active in parallel. so code like

socketState->operation = OP_READ;
socketState->thisOvl=ovl;

is wrong by design. also no sense have hCompletion inside SocketState because hCompletion is not per socket. here is wrong place for store it.

also we need mandatory use not naked OVERLAPPED structure for pass it to I/O but self class inherited from OVERLAPPED. you need have additional members here - referenced pointer to your SocketState - because when I/O completed - you got back pointer to OVERLAPPED and need from it got pointer to your socketState. the operation also mandatory must be here (instead SocketState) because operation is per I/O but not per socket. so in very general can be next:

struct SocketState
{
    SOCKET socket;
    HANDLE hTimer;
    ULONG dwRefCount;

    void AddRef();
    void Release();

    _NODISCARD SOCKET LockHandle();
    void Rundown();
    void UnlockHandle();// call closesocket on last unlock

    void OnIoComplete(ULONG operation, ULONG dwErrorCode, ULONG dwBytesTransfered, PVOID buf);

    void StartSomeIo(ULONG operation, PVOID buf, ULONG cb);

    void Close()
    {
        if (LockHandle())
        {
            Rundown();
            UnlockHandle();
        }
    }
};

struct UIrp : OVERLAPPED
{
    SocketState* socketState;
    ULONG operation;
    PVOID buf;

    UIrp(SocketState* socketState, ULONG operation, PVOID buf)
        : socketState(socketState), operation(operation), buf(buf)
    {
        RtlZeroMemory(static_cast<OVERLAPPED*>(this), sizeof(OVERLAPPED));
        socketState->AddRef();
    }

    ~UIrp()
    {
        socketState->Release();
    }

    void OnIoComplete(ULONG dwErrorCode, ULONG dwBytesTransfered)
    {
        socketState->OnIoComplete(operation, dwErrorCode, dwBytesTransfered, buf);
        delete this;
    }
};

void SocketState::StartSomeIo(ULONG operation, PVOID buf, ULONG cb)
{
    if (UIrp* irp = new UIrp(this, operation, buf))
    {
        ULONG dwError = ERROR_INVALID_HANDLE;

        if (SOCKET s = LockHandle())
        {
            dwError = WSA*(s,... irp, 0) == 0 ? NOERROR : WSAGetLastError();
            UnlockHandle();
        }

        switch (dwError)
        {
        case NOERROR:
        case WSA_IO_PENDING:
            break;
        default:
            irp->OnIoComplete(dwError, 0);
        }
    }
}

void PortLoop(HANDLE hCompletionPort)
{
    for (;;)
    {
        OVERLAPPED* lpOverlapped;
        ULONG dwBytesTransfered;
        ULONG_PTR CompletionKey;

        ULONG dwError = GetQueuedCompletionStatus(hCompletionPort, &dwBytesTransfered,
            &CompletionKey, &lpOverlapped, INFINITE) ? NOERROR : GetLastError();

        // probably somehow use CompletionKey

        if (!lpOverlapped)
        {
            break;
        }

        static_cast<UIrp*>(lpOverlapped)->OnIoComplete(dwBytesTransfered, dwError);
    }
}

about

set set a TimerQueueTimer() on every client socket

this is possible and correct by design but think not the best in case you have many sockets. i be inherit SocketState from LIST_ENTRY and insert all active sockets to the some list. and by timer periodically check timeout and close sockets


来源: stackexchange

问:
Pandonic way of adding two Series with respect to bin-membership of index

Setup

```<code>>>> import pandas as pd
>>> import numpy as np
>>> s1 = pd.Series([100, 1000, 10000], index=[0, 2, 5])
s1
0      100
2     1000
5    10000
dtype: int64
>>> s2 = pd.Series(np.arange(7)*10)
>>> s2
0     0
1    10
2    20
3    30
4    40
5    50
6    60
dtype: int32
</code>```

Assumptions:

The index of s1 is guaranteed to be monotonically increasing.

There is no index in s2 smaller than the minimum index of s1.

Clarification: The index of s2 cannot be assumed to have any particular order!

Desired result

I want to add the values of s1 to the values of s2 as follows (see comments for explanation):

```<code>>>> result
0      100 # 100 + 0, because index 0 is in [0, 2)
1      110 # 100 + 10, because index 1 is in [0, 2)
2     1020 # 1000 + 20, because index 2 is in [2, 5)
3     1030 # 1000 + 20, because index 3 is in [2, 5)
4     1040 # 1000 + 40, because index 4 is in [2, 5)
5    10050 # 10000 + 50, because index 5 is in [5, inf)
6    10060 # 10000 + 50, because index 6 is in [5, inf)
dtype: int64
</code>```

Attempt

I created versions of s1 and s2 where the bins are the index.

```<code>>>> edges = [*s1.index, np.inf]
>>> s1_binned = pd.Series(s1.values, index=pd.cut(s1.index, bins=edges, right=False))
>>> s2_binned = pd.Series(s2.values, index=pd.cut(s2.index, bins=edges, right=False))
s1_binned
[0.0, 2.0)      100
[2.0, 5.0)     1000
[5.0, inf)    10000
dtype: int64
>>> s2_binned
[0.0, 2.0)     0
[0.0, 2.0)    10
[2.0, 5.0)    20
[2.0, 5.0)    30
[2.0, 5.0)    40
[5.0, inf)    50
[5.0, inf)    60
dtype: int32
</code>```

Then I indexed into s1 with the index of s2 to get the values to add.

```<code>>>> to_add = s1_binned[s2_binned.index]
>>> to_add
[0.0, 2.0)      100
[0.0, 2.0)      100
[2.0, 5.0)     1000
[2.0, 5.0)     1000
[2.0, 5.0)     1000
[5.0, inf)    10000
[5.0, inf)    10000
dtype: int64
</code>```

Finally I can add the values of to_add to s2.

```<code>>>> s2 + to_add.values
0      100
1      110
2     1020
3     1030
4     1040
5    10050
6    10060
dtype: int64
</code>```

I have the feeling that there is a better solution, but I don't have much experience with "mapping" values to bins.

答:
Your feeling was correct - there is a more pandonic way:

```<code>s2+s1.reindex_like(s2).fillna(method='ffill')
</code>```

Let's break it down:

s1.reindex_like(s2) returns a series whose index is like s2, with NaN where new indices were added (i.e. empty rows):

fillna(method='ffill') fills each of those empty rows with the previous non-empty value

EDITED:
In comments, OP explained that s2's index is not assumed to be sorted.

For example:

```<code>import pandas as pd
import numpy as np
s1 = pd.Series([100, 1000, 10000], index=[0, 2, 5])
s2 = pd.Series(np.arange(7)*10)
s2 = s2[[2,3,5,1,0,4,6]]
</code>```

So that s2 is

```<code>2    20
3    30
5    50
1    10
0     0
4    40
6    60
dtype: int64
</code>```

Apparently, my method still works:

```<code>s2+s1.reindex_like(s2, method='ffill')
</code>```

Returns

```<code>2     1020
3     1030
5    10050
1      110
0      100
4     1040
6    10060
dtype: int64
</code>```

Which, if I understand correctly, is the desired output.

ContentLicense: CC BY-SA 4.0

Body:
Setup

>>> import pandas as pd
>>> import numpy as np
>>> s1 = pd.Series([100, 1000, 10000], index=[0, 2, 5])
s1
0      100
2     1000
5    10000
dtype: int64
>>> s2 = pd.Series(np.arange(7)*10)
>>> s2
0     0
1    10
2    20
3    30
4    40
5    50
6    60
dtype: int32

Assumptions:

The index of s1 is guaranteed to be monotonically increasing.

There is no index in s2 smaller than the minimum index of s1.

Clarification: The index of s2 cannot be assumed to have any particular order!

Desired result

I want to add the values of s1 to the values of s2 as follows (see comments for explanation):

>>> result
0      100 # 100 + 0, because index 0 is in [0, 2)
1      110 # 100 + 10, because index 1 is in [0, 2)
2     1020 # 1000 + 20, because index 2 is in [2, 5)
3     1030 # 1000 + 20, because index 3 is in [2, 5)
4     1040 # 1000 + 40, because index 4 is in [2, 5)
5    10050 # 10000 + 50, because index 5 is in [5, inf)
6    10060 # 10000 + 50, because index 6 is in [5, inf)
dtype: int64

Attempt

I created versions of s1 and s2 where the bins are the index.

>>> edges = [*s1.index, np.inf]
>>> s1_binned = pd.Series(s1.values, index=pd.cut(s1.index, bins=edges, right=False))
>>> s2_binned = pd.Series(s2.values, index=pd.cut(s2.index, bins=edges, right=False))
s1_binned
[0.0, 2.0)      100
[2.0, 5.0)     1000
[5.0, inf)    10000
dtype: int64
>>> s2_binned
[0.0, 2.0)     0
[0.0, 2.0)    10
[2.0, 5.0)    20
[2.0, 5.0)    30
[2.0, 5.0)    40
[5.0, inf)    50
[5.0, inf)    60
dtype: int32

Then I indexed into s1 with the index of s2 to get the values to add.

>>> to_add = s1_binned[s2_binned.index]
>>> to_add
[0.0, 2.0)      100
[0.0, 2.0)      100
[2.0, 5.0)     1000
[2.0, 5.0)     1000
[2.0, 5.0)     1000
[5.0, inf)    10000
[5.0, inf)    10000
dtype: int64

Finally I can add the values of to_add to s2.

>>> s2 + to_add.values
0      100
1      110
2     1020
3     1030
4     1040
5    10050
6    10060
dtype: int64

I have the feeling that there is a better solution, but I don't have much experience with "mapping" values to bins.

Body:
Your feeling was correct - there is a more pandonic way:

s2+s1.reindex_like(s2).fillna(method='ffill')

Let's break it down:

s1.reindex_like(s2) returns a series whose index is like s2, with NaN where new indices were added (i.e. empty rows):

fillna(method='ffill') fills each of those empty rows with the previous non-empty value

EDITED:
In comments, OP explained that s2's index is not assumed to be sorted.

For example:

import pandas as pd
import numpy as np
s1 = pd.Series([100, 1000, 10000], index=[0, 2, 5])
s2 = pd.Series(np.arange(7)*10)
s2 = s2[[2,3,5,1,0,4,6]]

So that s2 is

2    20
3    30
5    50
1    10
0     0
4    40
6    60
dtype: int64

Apparently, my method still works:

s2+s1.reindex_like(s2, method='ffill')

Returns

2     1020
3     1030
5    10050
1      110
0      100
4     1040
6    10060
dtype: int64

Which, if I understand correctly, is the desired output.


问:
How to reference a JSON example in set payload
I am using Anypoint Studio 6.1 and Mule 3.8.1 and have a JSON example in main/src/api called response-flow1.json.  I want to set my payload to response-flow1.json if certain rules are met.  How can I reference response-flow1.json in a Set Payload?

Thanks

答:
We have the option to use the dataweave:</p>

```<code><set-payload value="#[dw('readUrl("classpath://examples/filename.json","application/json")')]" doc:name="Set Mock Payload"/>
</code>```

ContentLicense: CC BY-SA 3.0

Body:
We have the option to use the dataweave:

<set-payload value="#[dw('readUrl(&quot;classpath://examples/filename.json&quot;,&quot;application/json&quot;)')]" doc:name="Set Mock Payload"/>


问:
Pandas Series from two-columned DataFrame produces a Series of NaN's
```<code>state_codes = pd.read_csv('name-abbr.csv', header=None)
state_codes.columns = ['State', 'Code']
codes = state_codes['Code']
states = pd.Series(state_codes['State'], index=state_codes['Code'])
</code>```

name-abbr.csv is a two-columned CSV file of US state names in the first column and postal codes in the second: "Alabama" and "AL" in the first row, "Alaska" and "AK" in the second, and so forth.

The above code correctly sets the index, but the Series is all NaN. If I don't set the index, the state names correctly show. But I want both.

I also tried this line:

```<code>states = pd.Series(state_codes.iloc[:,0], index=state_codes.iloc[:,1])
</code>```

Same result. How do I get this to work?

答:
Here is reason called alignment, it means pandas try match index of state_codes['State'].index with new index of state_codes['Code'] and because different get missing values in output, for prevent it is necessary convert Series to numpy array:

```<code>states = pd.Series(state_codes['State'].to_numpy(), index=state_codes['Code'])
</code>```

Or you can use DataFrame.set_index:

```<code>states = state_codes.set_index('Code')['State']
</code>```

Body:
state_codes = pd.read_csv('name-abbr.csv', header=None)
state_codes.columns = ['State', 'Code']
codes = state_codes['Code']
states = pd.Series(state_codes['State'], index=state_codes['Code'])

name-abbr.csv is a two-columned CSV file of US state names in the first column and postal codes in the second: "Alabama" and "AL" in the first row, "Alaska" and "AK" in the second, and so forth.

The above code correctly sets the index, but the Series is all NaN. If I don't set the index, the state names correctly show. But I want both.

I also tried this line:

states = pd.Series(state_codes.iloc[:,0], index=state_codes.iloc[:,1])

Same result. How do I get this to work?

Body:
Here is reason called alignment, it means pandas try match index of state_codes['State'].index with new index of state_codes['Code'] and because different get missing values in output, for prevent it is necessary convert Series to numpy array:

states = pd.Series(state_codes['State'].to_numpy(), index=state_codes['Code'])

Or you can use DataFrame.set_index:

states = state_codes.set_index('Code')['State']


问:
Reading numerical data with csv reader
I am trying to read a csv table with csv reader, I want to rewrite the data into a tuple of tuples, where every subtuple contains the data of one row in the original file.
I am doing the following and it is working fine, but you can see how ugly it looks.

```<code>table1 = tuple()
with open('data.csv', newline='') as f:
    reader = csv.reader(f)
    for row in reader:
        print(row)
        table1 = table1.__add__(tuple([tuple([float(row[0]), float(row[1])])]))  # giving that I know the data is arranged in two columns
print(table1)
</code>```

The output looks good:

```<code>((0.0, 0.0), (0.0, 0.0), (0.00543154732031037, 4.2724591391866636e-05), (0.0347155846128363, 0.00021518683772895567), (0.1014894975795... etc
</code>```

I am sure there is a better way to do this.
Please note that I can only use the builtin modules, this is why I didn't use Pandas.
Thank you for any suggestions.

答:
Why not use a list of tuples - then you could use a list comprehension:

```<code>with open('data.csv', newline='') as f:
    reader = csv.reader(f)
    table1 = [(float(row[0]), float(row[1])) for row in reader]
</code>```

If you really need a tuple instead of a list: tuple() accepts iterables, so you can pass the list comprehension to it:

```<code>    table1 = tuple((float(row[0]), float(row[1])) for row in reader)
</code>```

A list/tuple is suitable where you need repeated access/random access to all contained elements.

An alternative would be a generator - suitable for all cases where you process the input in a "one-time-only, forward-only" fashion:

```<code>def read_data(filename):
    with open(filename, newline='') as f:
        reader = csv.reader(f)
        for row in reader:
            yield (float(row[0]), float(row[1]))

for item in read_data('data.csv'):
    print(item)
</code>```

Of course you could create a list from the generator as well:

```<code>table1 = [item for item in read_data('data.csv')]

# or

table1 = tuple(item for item in read_data('data.csv'))
</code>```

Title: Reading numerical data with csv reader

Body:
I am trying to read a csv table with csv reader, I want to rewrite the data into a tuple of tuples, where every subtuple contains the data of one row in the original file.
I am doing the following and it is working fine, but you can see how ugly it looks.

table1 = tuple()
with open('data.csv', newline='') as f:
    reader = csv.reader(f)
    for row in reader:
        print(row)
        table1 = table1.__add__(tuple([tuple([float(row[0]), float(row[1])])]))  # giving that I know the data is arranged in two columns
print(table1)

The output looks good:

((0.0, 0.0), (0.0, 0.0), (0.00543154732031037, 4.2724591391866636e-05), (0.0347155846128363, 0.00021518683772895567), (0.1014894975795... etc

I am sure there is a better way to do this.
Please note that I can only use the builtin modules, this is why I didn't use Pandas.
Thank you for any suggestions.

Body:
Why not use a list of tuples - then you could use a list comprehension:

with open('data.csv', newline='') as f:
    reader = csv.reader(f)
    table1 = [(float(row[0]), float(row[1])) for row in reader]

If you really need a tuple instead of a list: tuple() accepts iterables, so you can pass the list comprehension to it:

    table1 = tuple((float(row[0]), float(row[1])) for row in reader)

A list/tuple is suitable where you need repeated access/random access to all contained elements.

An alternative would be a generator - suitable for all cases where you process the input in a "one-time-only, forward-only" fashion:

def read_data(filename):
    with open(filename, newline='') as f:
        reader = csv.reader(f)
        for row in reader:
            yield (float(row[0]), float(row[1]))

for item in read_data('data.csv'):
    print(item)

Of course you could create a list from the generator as well:

table1 = [item for item in read_data('data.csv')]

# or

table1 = tuple(item for item in read_data('data.csv'))


问:
TypeError("Tensor is unhashable if Tensor equality is enabled. " K.learning_phase(): 0
I am porting a Keras, Tensorflow, and OpenCV script to TF2 and Keras 2 and have run into a problem. I am getting an error on K.learning_phase(): 0.
The error happens in this code section.

```<code>ef detect_image(self, image):
        if self.model_image_size != (None, None):
            assert self.model_image_size[0]%32 == 0, 'Multiples of 32 required'
            assert self.model_image_size[1]%32 == 0, 'Multiples of 32 required'
            boxed_image = image_preporcess(np.copy(image), tuple(reversed(self.model_image_size)))
            image_data = boxed_image

        out_boxes, out_scores, out_classes = self.sess.run(
            [self.boxes, self.scores, self.classes],
            feed_dict={
                self.yolo_model.input: image_data,
                self.input_image_shape: [image.shape[0], image.shape[1]],
                tf.keras.learning_phase(): 0  })

</code>```

here is a gist to the full code
https://gist.github.com/robisen1/31976de17af9e752c6ba8d1dd0e08906

```<code>Traceback (most recent call last):
  File "webcam_detect.py", line 188, in <module>
    r_image, ObjectsList = yolo.detect_image(frame)
  File "webcam_detect.py", line 110, in detect_image
    K.learning_phase(): 0
  File "C:\Anaconda3\envs\simplecv\lib\site-packages\tensorflow_core\python\framework\ops.py", line 705, in __hash__
    raise TypeError("Tensor is unhashable if Tensor equality is enabled. "
TypeError: Tensor is unhashable if Tensor equality is enabled. Instead, use tensor.experimental_ref() as the key.
(simplecv) PS C:\dev\lacv\yolov3\yolov3ct>
</code>```

I am not sure what is going on. I would appreciate any insights.

答:
You are trying to use Tensorflow 1.x, which works in graph mode whereas TensorFlow 2.x works in eager mode. TensorFlow 1.X requires users to manually stitch together an abstract syntax tree (the graph) by making tf.* API calls. It then requires users to manually compile the abstract syntax tree by passing a set of output tensors and input tensors to a session.run() call. TensorFlow 2.0 executes eagerly (like Python normally does) and in 2.0, graphs and sessions should feel like implementation details.

The error is due to version. If you are using session in TF2 then you need to use the compatible version and same goes with other operations. Also in TF2 it is tf.keras.backend.learning_phase.

Would recommend to go through the guide - Migrate your TensorFlow 1 code to TensorFlow 2.

For Example Below Code throws the error similar to the error you are facing -

```<code>import tensorflow as tf
print(tf.__version__)
x = tf.constant(5)
y = tf.constant(10)
z = tf.constant(20)

# This will show same error.
tensor_set = {x, y, z}
tensor_dict = {x: 'five', y: 'ten', z: 'twenty'}
</code>```

Output -

```<code>2.2.0
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-509b2d8d7ab1> in <module>()
      6
      7 # This will show same error.
----> 8 tensor_set = {x, y, z}
      9 tensor_dict = {x: 'five', y: 'ten', z: 'twenty'}
     10

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in __hash__(self)
    724     if (Tensor._USE_EQUALITY and executing_eagerly_outside_functions() and
    725         (g is None or g.building_function)):
--> 726       raise TypeError("Tensor is unhashable. "
    727                       "Instead, use tensor.ref() as the key.")
    728     else:

TypeError: Tensor is unhashable. Instead, use tensor.ref() as the key.
</code>```

But below code will fix the issue -

```<code>import tensorflow as tf
print(tf.__version__)
x = tf.constant(5)
y = tf.constant(10)
z = tf.constant(20)

#This solves the issue
tensor_set = {x.experimental_ref(), y.experimental_ref(), z.experimental_ref()}
tensor_dict = {x.experimental_ref(): 'five', y.experimental_ref(): 'ten', z.experimental_ref(): 'twenty'}
</code>```

Output -

```<code>2.2.0
WARNING:tensorflow:From <ipython-input-4-05e379e669d9>:12: Tensor.experimental_ref (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use ref() instead.
</code>```

If you are still facing the error, then kindly share the reproducible code for the error like above. Will be happy to help you.

Hope this answers your question. Happy Learning.

Body:
I am porting a Keras, Tensorflow, and OpenCV script to TF2 and Keras 2 and have run into a problem. I am getting an error on K.learning_phase(): 0.
The error happens in this code section.

ef detect_image(self, image):
        if self.model_image_size != (None, None):
            assert self.model_image_size[0]%32 == 0, 'Multiples of 32 required'
            assert self.model_image_size[1]%32 == 0, 'Multiples of 32 required'
            boxed_image = image_preporcess(np.copy(image), tuple(reversed(self.model_image_size)))
            image_data = boxed_image

        out_boxes, out_scores, out_classes = self.sess.run(
            [self.boxes, self.scores, self.classes],
            feed_dict={
                self.yolo_model.input: image_data,
                self.input_image_shape: [image.shape[0], image.shape[1]],
                tf.keras.learning_phase(): 0  })

here is a gist to the full code
https://gist.github.com/robisen1/31976de17af9e752c6ba8d1dd0e08906

Traceback (most recent call last):
  File "webcam_detect.py", line 188, in <module>
    r_image, ObjectsList = yolo.detect_image(frame)
  File "webcam_detect.py", line 110, in detect_image
    K.learning_phase(): 0
  File "C:\Anaconda3\envs\simplecv\lib\site-packages\tensorflow_core\python\framework\ops.py", line 705, in __hash__
    raise TypeError("Tensor is unhashable if Tensor equality is enabled. "
TypeError: Tensor is unhashable if Tensor equality is enabled. Instead, use tensor.experimental_ref() as the key.
(simplecv) PS C:\dev\lacv\yolov3\yolov3ct>

I am not sure what is going on. I would appreciate any insights.

Body:
You are trying to use Tensorflow 1.x, which works in graph mode whereas TensorFlow 2.x works in eager mode. TensorFlow 1.X requires users to manually stitch together an abstract syntax tree (the graph) by making tf.* API calls. It then requires users to manually compile the abstract syntax tree by passing a set of output tensors and input tensors to a session.run() call. TensorFlow 2.0 executes eagerly (like Python normally does) and in 2.0, graphs and sessions should feel like implementation details.

The error is due to version. If you are using session in TF2 then you need to use the compatible version and same goes with other operations. Also in TF2 it is tf.keras.backend.learning_phase.

Would recommend to go through the guide - Migrate your TensorFlow 1 code to TensorFlow 2.

For Example Below Code throws the error similar to the error you are facing -

import tensorflow as tf
print(tf.__version__)
x = tf.constant(5)
y = tf.constant(10)
z = tf.constant(20)

# This will show same error.
tensor_set = {x, y, z}
tensor_dict = {x: 'five', y: 'ten', z: 'twenty'}

Output -

2.2.0
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-509b2d8d7ab1> in <module>()
      6
      7 # This will show same error.
----> 8 tensor_set = {x, y, z}
      9 tensor_dict = {x: 'five', y: 'ten', z: 'twenty'}
     10

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in __hash__(self)
    724     if (Tensor._USE_EQUALITY and executing_eagerly_outside_functions() and
    725         (g is None or g.building_function)):
--> 726       raise TypeError("Tensor is unhashable. "
    727                       "Instead, use tensor.ref() as the key.")
    728     else:

TypeError: Tensor is unhashable. Instead, use tensor.ref() as the key.

But below code will fix the issue -

import tensorflow as tf
print(tf.__version__)
x = tf.constant(5)
y = tf.constant(10)
z = tf.constant(20)

#This solves the issue
tensor_set = {x.experimental_ref(), y.experimental_ref(), z.experimental_ref()}
tensor_dict = {x.experimental_ref(): 'five', y.experimental_ref(): 'ten', z.experimental_ref(): 'twenty'}

Output -

2.2.0
WARNING:tensorflow:From <ipython-input-4-05e379e669d9>:12: Tensor.experimental_ref (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use ref() instead.

If you are still facing the error, then kindly share the reproducible code for the error like above. Will be happy to help you.

Hope this answers your question. Happy Learning.


问:
java.lang.IllegalStateException: The ad size and ad unit ID must be set before loadAd is called.?
I am developing news app and I have implemented adview with databinding but it is giving following exception

```<code>java.lang.IllegalStateException: The ad size and ad unit ID must be set before loadAd is called.
     at com.google.android.gms.internal.ads.zzxv.zza(com.google.android.gms:play-services-ads-lite@@19.1.0:82)
     at com.google.android.gms.ads.BaseAdView.loadAd(com.google.android.gms:play-services-ads-lite@@19.1.0:15)
     at com.google.android.gms.ads.AdView.loadAd(com.google.android.gms:play-services-ads-lite@@19.1.0:20)
     at yodgorbek.komilov.musobaqayangiliklari.ui.TopHeadlinesFragment.initializeAds(TopHeadlinesFragment.kt:95)
     at yodgorbek.komilov.musobaqayangiliklari.ui.TopHeadlinesFragment.onViewCreated(TopHeadlinesFragment.kt:56)
     at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:322)
     at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1185)
     at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1354)
     at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1432)
     at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1495)
     at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:447)
     at androidx.fragment.app.FragmentManager.executeOps(FragmentManager.java:2167)
     at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1990)
     at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1945)
     at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1847)
     at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2621)
     at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2569)
     at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:247)
     at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:541)
     at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:201)
     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1392)
     at android.app.Activity.performStart(Activity.java:7252)
     at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3000)
     at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:185)
     at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:170)
     at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:147)
     at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:73)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1858)
     at android.os.Handler.dispatchMessage(Handler.java:106)
     at android.os.Looper.loop(Looper.java:201)
     at android.app.ActivityThread.main(ActivityThread.java:6820)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:922)
</code>```

but I have tried following answers Ad Size and Ad unit id must be set before loadAd is called and  this The ad size and ad unit ID must be set before loadAd when set programmatically  and this one as well Include AdView with databinding and https://github.com/firebase/quickstart-android/blob/master/admob/app/src/main/java/com/google/samples/quickstart/admobexample/kotlin/MainActivity.kt from StackOverflow it did not solve my problem

below my XML file where I have implemented ad view

```<code><data>

    <variable
        name="adId"
        type="String" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="56dp"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ProgressBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/pb"
        android:gravity="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        ads:adSize="BANNER"
        ads:adUnitId="@{adId}"
        ads:layout_constraintTop_toBottomOf="@+id/pb"
        ads:layout_constraintBottom_toBottomOf="@+id/pb"/>

</androidx.constraintlayout.widget.ConstraintLayout>
</code>```

below my Fragment class

```<code>class TopHeadlinesFragment : Fragment() {

    private lateinit var binding: FragmentTopHeadlinesBinding

    private val viewModel by viewModel<MainViewModel>()

    private lateinit var topHeadlinesAdapter: TopHeadlinesAdapter

    //3
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding =
            DataBindingUtil.inflate(inflater, R.layout.fragment_top_headlines, container, false)
        binding.lifecycleOwner = this
        topHeadlinesAdapter = TopHeadlinesAdapter()
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initViewModel()
        initializeAds()

    }

    private fun initViewModel() {
        viewModel.sportList.observe(this, Observer { result ->

            when (result) {
                is Results.Success -> {
                    val newList = result.data
                    if (newList != null) {
                        topHeadlinesAdapter.updateData(newList)
                    }
                    binding.recyclerView.adapter = topHeadlinesAdapter
                    topHeadlinesAdapter.notifyDataSetChanged()
                }
            }
        })

        viewModel.showLoading.observe(this, Observer { showLoading ->
            pb.visibility = if (showLoading) View.VISIBLE else View.GONE
        })

        viewModel.loadNews()
    }

    private fun initializeAds(){
        MobileAds.initialize(context)

       // adView.adSize = AdSize.BANNER
        binding.adId = "my unit id"
        val adRequest = AdRequest.Builder().build()
        binding.adView.loadAd(adRequest)
    }

}
</code>```

I want to  know where exactly I am making mistake I have tried all StackOverflow and even documentation sample it did solve my issue I have tried set unit id and size programmatically using kotlin and XML file as well it did not work at all any tips will be appreciated.

答:
You can write it like this:</p>

```<code>val adRequest = AdRequest.Builder().build()
binding.adId = "my unit id"
binding.adView.loadAd(adRequest)
</code>```

Body:
I am developing news app and I have implemented adview with databinding but it is giving following exception

java.lang.IllegalStateException: The ad size and ad unit ID must be set before loadAd is called.
     at com.google.android.gms.internal.ads.zzxv.zza(com.google.android.gms:play-services-ads-lite@@19.1.0:82)
     at com.google.android.gms.ads.BaseAdView.loadAd(com.google.android.gms:play-services-ads-lite@@19.1.0:15)
     at com.google.android.gms.ads.AdView.loadAd(com.google.android.gms:play-services-ads-lite@@19.1.0:20)
     at yodgorbek.komilov.musobaqayangiliklari.ui.TopHeadlinesFragment.initializeAds(TopHeadlinesFragment.kt:95)
     at yodgorbek.komilov.musobaqayangiliklari.ui.TopHeadlinesFragment.onViewCreated(TopHeadlinesFragment.kt:56)
     at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:322)
     at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1185)
     at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1354)
     at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1432)
     at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1495)
     at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:447)
     at androidx.fragment.app.FragmentManager.executeOps(FragmentManager.java:2167)
     at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1990)
     at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1945)
     at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1847)
     at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2621)
     at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2569)
     at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:247)
     at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:541)
     at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:201)
     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1392)
     at android.app.Activity.performStart(Activity.java:7252)
     at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3000)
     at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:185)
     at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:170)
     at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:147)
     at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:73)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1858)
     at android.os.Handler.dispatchMessage(Handler.java:106)
     at android.os.Looper.loop(Looper.java:201)
     at android.app.ActivityThread.main(ActivityThread.java:6820)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:922)

but I have tried following answers Ad Size and Ad unit id must be set before loadAd is called and  this The ad size and ad unit ID must be set before loadAd when set programmatically  and this one as well Include AdView with databinding and https://github.com/firebase/quickstart-android/blob/master/admob/app/src/main/java/com/google/samples/quickstart/admobexample/kotlin/MainActivity.kt from StackOverflow it did not solve my problem

below my XML file where I have implemented ad view

<data>

    <variable
        name="adId"
        type="String" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="56dp"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ProgressBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/pb"
        android:gravity="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        ads:adSize="BANNER"
        ads:adUnitId="@{adId}"
        ads:layout_constraintTop_toBottomOf="@+id/pb"
        ads:layout_constraintBottom_toBottomOf="@+id/pb"/>

</androidx.constraintlayout.widget.ConstraintLayout>

below my Fragment class

class TopHeadlinesFragment : Fragment() {

    private lateinit var binding: FragmentTopHeadlinesBinding

    private val viewModel by viewModel<MainViewModel>()

    private lateinit var topHeadlinesAdapter: TopHeadlinesAdapter

    //3
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding =
            DataBindingUtil.inflate(inflater, R.layout.fragment_top_headlines, container, false)
        binding.lifecycleOwner = this
        topHeadlinesAdapter = TopHeadlinesAdapter()
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initViewModel()
        initializeAds()

    }

    private fun initViewModel() {
        viewModel.sportList.observe(this, Observer { result ->

            when (result) {
                is Results.Success -> {
                    val newList = result.data
                    if (newList != null) {
                        topHeadlinesAdapter.updateData(newList)
                    }
                    binding.recyclerView.adapter = topHeadlinesAdapter
                    topHeadlinesAdapter.notifyDataSetChanged()
                }
            }
        })

        viewModel.showLoading.observe(this, Observer { showLoading ->
            pb.visibility = if (showLoading) View.VISIBLE else View.GONE
        })

        viewModel.loadNews()
    }

    private fun initializeAds(){
        MobileAds.initialize(context)

       // adView.adSize = AdSize.BANNER
        binding.adId = "my unit id"
        val adRequest = AdRequest.Builder().build()
        binding.adView.loadAd(adRequest)
    }

}

I want to  know where exactly I am making mistake I have tried all StackOverflow and even documentation sample it did solve my issue I have tried set unit id and size programmatically using kotlin and XML file as well it did not work at all any tips will be appreciated.

Body:
You can write it like this:

val adRequest = AdRequest.Builder().build()
binding.adId = "my unit id"
binding.adView.loadAd(adRequest)


问:
Shape mismatch problem in tensorflow 2.2 training using yolo4.cfg
I recently added a new feature to my yolov3 implementation which is models are currently loaded directly from DarkNet cfg files for convenience, I tested the code with yolov3 configuration as well as yolov4 configuration they both work just fine except for v4 training. Shortly after I start training I get a shapes mismatch error and I'll be very grateful if someone can help me get rid of the error and get to finally complete my project. Please let me know in the comments and I will provide you with any resources you need to help me with fixing the problem and thank you in advance...

This is what I run in order to reproduce:

```<code>if __name__ == '__main__':
    tr = Trainer((608, 608, 3),
                 '../Config/yolo4.cfg',
                 '../Config/beverly_hills.txt',
                 1344, 756, score_threshold=0.1,
                 train_tf_record='../Data/TFRecords/beverly_hills_train.tfrecord',
                 valid_tf_record='../Data/TFRecords/beverly_hills_test.tfrecord')

    tr.train(
        100,
        8,
        1e-3,
        dataset_name='beverly_hills',
        merge_evaluation=False,
        n_epoch_eval=10,
        clear_outputs=True
    )
L
</code>```

links to files you need:

bh_labels.csv (794 Kb)

beverly_hills.txt (162 B)

beverly_hills_train.tfrecord (509 Mb)

beverly_hills_test.tfrecord (89 Mb)

Here is the error message:

```<code>Traceback (most recent call last):
  File "trainer.py", line 629, in <module>
    clear_outputs=True
  File "../Helpers/utils.py", line 62, in wrapper
    result = func(*args, **kwargs)
  File "trainer.py", line 490, in train
    validation_data=valid_dataset,
  File "/root/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 108, in _method_wrapper
    return method(self, *args, **kwargs)
  File "/root/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1090, in fit
    tmp_logs = train_function(iterator)
  File "/root/.local/lib/python3.6/site-packages/tensorflow/python/eager/def_function.py", line 766, in __call__
    result = self._call(*args, **kwds)
  File "/root/.local/lib/python3.6/site-packages/tensorflow/python/eager/def_function.py", line 826, in _call
    return self._stateless_fn(*args, **kwds)
  File "/root/.local/lib/python3.6/site-packages/tensorflow/python/eager/function.py", line 2811, in __call__
    return graph_function._filtered_call(args, kwargs)  # pylint: disable=protected-access
  File "/root/.local/lib/python3.6/site-packages/tensorflow/python/eager/function.py", line 1838, in _filtered_call
    cancellation_manager=cancellation_manager)
  File "/root/.local/lib/python3.6/site-packages/tensorflow/python/eager/function.py", line 1914, in _call_flat
    ctx, args, cancellation_manager=cancellation_manager))
  File "/root/.local/lib/python3.6/site-packages/tensorflow/python/eager/function.py", line 549, in call
    ctx=ctx)
  File "/root/.local/lib/python3.6/site-packages/tensorflow/python/eager/execute.py", line 60, in quick_execute
    inputs, attrs, num_outputs)
tensorflow.python.framework.errors_impl.InvalidArgumentError:  Incompatible shapes: [4,76,76,3,1] vs. [4,19,19,3,1]
     [[node yolo_loss/logistic_loss/mul (defined at ../Helpers/utils.py:260) ]] [Op:__inference_train_function_38735]

Errors may have originated from an input operation.
Input Source operations connected to node yolo_loss/logistic_loss/mul:
 yolo_loss/split_1 (defined at ../Helpers/utils.py:222)
 yolo_loss/split (defined at ../Helpers/utils.py:196)

Function call stack:
train_function
</code>```

And when I change the batch_size to 8 instead of 4, the error mutates into the following(the error source changes):

```<code>Traceback (most recent call last):
  File "/Users/emadboctor/Desktop/Code/yolov3-keras-tf2/Main/trainer.py", line 693, in <module>
    clear_outputs=True,
  File "/Users/emadboctor/Desktop/Code/yolov3-keras-tf2/Helpers/utils.py", line 62, in wrapper
    result = func(*args, **kwargs)
  File "/Users/emadboctor/Desktop/Code/yolov3-keras-tf2/Main/trainer.py", line 526, in train
    validation_data=valid_dataset,
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py", line 66, in _method_wrapper
    return method(self, *args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py", line 848, in fit
    tmp_logs = train_function(iterator)
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py", line 580, in __call__
    result = self._call(*args, **kwds)
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py", line 644, in _call
    return self._stateless_fn(*args, **kwds)
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/eager/function.py", line 2420, in __call__
    return graph_function._filtered_call(args, kwargs)  # pylint: disable=protected-access
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/eager/function.py", line 1665, in _filtered_call
    self.captured_inputs)
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/eager/function.py", line 1746, in _call_flat
    ctx, args, cancellation_manager=cancellation_manager))
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/eager/function.py", line 598, in call
    ctx=ctx)
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/eager/execute.py", line 60, in quick_execute
    inputs, attrs, num_outputs)
tensorflow.python.framework.errors_impl.InvalidArgumentError:  Incompatible shapes: [8,13,13,3,2] vs. [8,52,52,3,2]
     [[node gradient_tape/yolo_loss/sub_5/BroadcastGradientArgs (defined at Users/emadboctor/Desktop/Code/yolov3-keras-tf2/Main/trainer.py:526) ]] [Op:__inference_train_function_42744]

Function call stack:
train_function
</code>```

答:
Adding this line in models.py solved the shapes problem and the training started as expected:

```<code>if '4' in self.model_configuration:
    self.output_layers.reverse()
</code>```

Body:
I recently added a new feature to my yolov3 implementation which is models are currently loaded directly from DarkNet cfg files for convenience, I tested the code with yolov3 configuration as well as yolov4 configuration they both work just fine except for v4 training. Shortly after I start training I get a shapes mismatch error and I'll be very grateful if someone can help me get rid of the error and get to finally complete my project. Please let me know in the comments and I will provide you with any resources you need to help me with fixing the problem and thank you in advance...

This is what I run in order to reproduce:

if __name__ == '__main__':
    tr = Trainer((608, 608, 3),
                 '../Config/yolo4.cfg',
                 '../Config/beverly_hills.txt',
                 1344, 756, score_threshold=0.1,
                 train_tf_record='../Data/TFRecords/beverly_hills_train.tfrecord',
                 valid_tf_record='../Data/TFRecords/beverly_hills_test.tfrecord')

    tr.train(
        100,
        8,
        1e-3,
        dataset_name='beverly_hills',
        merge_evaluation=False,
        n_epoch_eval=10,
        clear_outputs=True
    )
L

links to files you need:

bh_labels.csv (794 Kb)

beverly_hills.txt (162 B)

beverly_hills_train.tfrecord (509 Mb)

beverly_hills_test.tfrecord (89 Mb)

Here is the error message:

Traceback (most recent call last):
  File "trainer.py", line 629, in <module>
    clear_outputs=True
  File "../Helpers/utils.py", line 62, in wrapper
    result = func(*args, **kwargs)
  File "trainer.py", line 490, in train
    validation_data=valid_dataset,
  File "/root/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 108, in _method_wrapper
    return method(self, *args, **kwargs)
  File "/root/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1090, in fit
    tmp_logs = train_function(iterator)
  File "/root/.local/lib/python3.6/site-packages/tensorflow/python/eager/def_function.py", line 766, in __call__
    result = self._call(*args, **kwds)
  File "/root/.local/lib/python3.6/site-packages/tensorflow/python/eager/def_function.py", line 826, in _call
    return self._stateless_fn(*args, **kwds)
  File "/root/.local/lib/python3.6/site-packages/tensorflow/python/eager/function.py", line 2811, in __call__
    return graph_function._filtered_call(args, kwargs)  # pylint: disable=protected-access
  File "/root/.local/lib/python3.6/site-packages/tensorflow/python/eager/function.py", line 1838, in _filtered_call
    cancellation_manager=cancellation_manager)
  File "/root/.local/lib/python3.6/site-packages/tensorflow/python/eager/function.py", line 1914, in _call_flat
    ctx, args, cancellation_manager=cancellation_manager))
  File "/root/.local/lib/python3.6/site-packages/tensorflow/python/eager/function.py", line 549, in call
    ctx=ctx)
  File "/root/.local/lib/python3.6/site-packages/tensorflow/python/eager/execute.py", line 60, in quick_execute
    inputs, attrs, num_outputs)
tensorflow.python.framework.errors_impl.InvalidArgumentError:  Incompatible shapes: [4,76,76,3,1] vs. [4,19,19,3,1]
     [[node yolo_loss/logistic_loss/mul (defined at ../Helpers/utils.py:260) ]] [Op:__inference_train_function_38735]

Errors may have originated from an input operation.
Input Source operations connected to node yolo_loss/logistic_loss/mul:
 yolo_loss/split_1 (defined at ../Helpers/utils.py:222)
 yolo_loss/split (defined at ../Helpers/utils.py:196)

Function call stack:
train_function

And when I change the batch_size to 8 instead of 4, the error mutates into the following(the error source changes):

Traceback (most recent call last):
  File "/Users/emadboctor/Desktop/Code/yolov3-keras-tf2/Main/trainer.py", line 693, in <module>
    clear_outputs=True,
  File "/Users/emadboctor/Desktop/Code/yolov3-keras-tf2/Helpers/utils.py", line 62, in wrapper
    result = func(*args, **kwargs)
  File "/Users/emadboctor/Desktop/Code/yolov3-keras-tf2/Main/trainer.py", line 526, in train
    validation_data=valid_dataset,
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py", line 66, in _method_wrapper
    return method(self, *args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py", line 848, in fit
    tmp_logs = train_function(iterator)
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py", line 580, in __call__
    result = self._call(*args, **kwds)
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py", line 644, in _call
    return self._stateless_fn(*args, **kwds)
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/eager/function.py", line 2420, in __call__
    return graph_function._filtered_call(args, kwargs)  # pylint: disable=protected-access
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/eager/function.py", line 1665, in _filtered_call
    self.captured_inputs)
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/eager/function.py", line 1746, in _call_flat
    ctx, args, cancellation_manager=cancellation_manager))
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/eager/function.py", line 598, in call
    ctx=ctx)
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/eager/execute.py", line 60, in quick_execute
    inputs, attrs, num_outputs)
tensorflow.python.framework.errors_impl.InvalidArgumentError:  Incompatible shapes: [8,13,13,3,2] vs. [8,52,52,3,2]
     [[node gradient_tape/yolo_loss/sub_5/BroadcastGradientArgs (defined at Users/emadboctor/Desktop/Code/yolov3-keras-tf2/Main/trainer.py:526) ]] [Op:__inference_train_function_42744]

Function call stack:
train_function

Body:
Adding this line in models.py solved the shapes problem and the training started as expected:

if '4' in self.model_configuration:
    self.output_layers.reverse()


问:
What's the future of OAuth 1?
I am going to start writing an API for my open source project.
Should I go with OAuth 2 for authentication or OAuth 1?

My main concern with OAuth 1 is than I don't want to invest time in writing API based on it if OAuth 1 going to be outdated soon.

My question is - will OAuth 1 become outdated soon? Also, I think from the point of end-user of the API, OAuth 2 seems to be easier to implement.

Should I just write OAuth2 API and forget about OAuth 1 or are there good reasons to use OAuth 1 for now?

答:
OAuth 2 is in draft stage (current writing, draft 16), but OAuth 1 has already is already in RFC (RFC 5849).

OAuth 2 authorization flow is easier than OAuth 1 but what you might encounter is that you have to choose a draft you want to implement and stick with it. When an RFC for OAuth 2 will be released, you will have to conform to it.

Addon: If OAuth 1 will be obsolete, the RFC will be obsoleted. The IETF will put the RFC into Status "historic". Chances are, they might make OAuth 2 an RFC and historicalize OAuth 1 RFC. Until that happens, OAuth 1 is valid to this day.

I hope this little info can help you.

Title: What's the future of OAuth 1?


问:
Create one-time subscription
I need to create a subscription to an Observable that is immediately disposed of when it is first called.

Is there something like:

```<code>observable.subscribeOnce(func);
</code>```

My use case, I am creating a subscription in an express route handler and the subscription is being called multiple times per request.

答:
Clean and Convenient Version

Expanding on M Fuat NUROĞLU's amazing answer on converting the observable to a promise, here's the very convenient version of it.

const value = await observable.toPromise();

console.log(value)
```

<p>The beauty of this is that we can use that value like a normal variable without introducing another nested block!</p>

<p>This is especially handy when you need to get multiple values from multiple observables. Neat and clean.</p>

<pre class="lang-js prettyprint-override"><code>const content = await contentObservable.toPromise();
const isAuthenticated = await isAuthenticatedObservable.toPromise();

if(isAuthenticated){
   service.foo(content)
}
</code>```

Of course, you will have to make your containing function async if you are to go with this route. You can also just .then the promise if you don't want the containing function to be async

I'm not sure if there are tradeoffs with this approach, feel free to let me know in the comments so we are aware.

P.S. If you liked this answer, don't forget to upvote M Fuat NUROĞLU's Answer as well :)

Title: Create one-time subscription

Body:
I need to create a subscription to an Observable that is immediately disposed of when it is first called.

Is there something like:

observable.subscribeOnce(func);

My use case, I am creating a subscription in an express route handler and the subscription is being called multiple times per request.

Body:
Clean and Convenient Version

Expanding on M Fuat NUROĞLU's amazing answer on converting the observable to a promise, here's the very convenient version of it.

const value = await observable.toPromise();

console.log(value)

The beauty of this is that we can use that value like a normal variable without introducing another nested block!

This is especially handy when you need to get multiple values from multiple observables. Neat and clean.

const content = await contentObservable.toPromise();
const isAuthenticated = await isAuthenticatedObservable.toPromise();

if(isAuthenticated){
   service.foo(content)
}

Of course, you will have to make your containing function async if you are to go with this route. You can also just .then the promise if you don't want the containing function to be async

I'm not sure if there are tradeoffs with this approach, feel free to let me know in the comments so we are aware.

P.S. If you liked this answer, don't forget to upvote M Fuat NUROĞLU's Answer as well :)


问:
Where can I paste my website folder in Ubuntu XAMPP for it to access local host server?
Where can I paste my website folder  in Ubuntu localhost, the files I coded in windows where I used wamp server and store them in www folder </p>

答: Put your folder in /opt/lampp/htdocs/</p>

Body: Put your folder in /opt/lampp/htdocs/


问:
Closure Syntax with and without parameters
iOS 13, Swift 5

I am trying get my head around closure syntax in Swift and almost there. I got the first three bon, and understand them; but the last no. How do I call self.theMethod4 here? the syntax shown is wrong!

```<code>struct ContentView: View {
var body: some View {
    Text("Hello, World!")
      .onAppear {
        self.theMethod {
          print("1")
        }
        self.the2Method("Ya ") {
          print("1")
        }
        self.the3Method("Hello") {arg2do in
          print("1 ",arg2do)
        }
        self.the4Method("World") { (arg2do) -> String in
          print("1 ",arg2do)
        }
      }
}

func theMethod(_ endOfMethod: @escaping () -> Void) {
  print("2")
  endOfMethod()
}

func the2Method(_ arg1:String, endOfMethod: @escaping () -> Void) {
  print("3")
  endOfMethod()
}

func the3Method(_ arg1:String, endOfMethod: @escaping (_ arg2:String) -> Void) {
  print("4")
  endOfMethod("Yo")
}

func the4Method(_ arg1:String, endOfMethod: @escaping (_ arg2: String) -> String) {
  print("5")
  let an = endOfMethod("Pe")
  print("An ",an)
}
}
</code>```

答:
You got the closure syntax ({ (parameters) -> ReturnValueType in statements }) right, but since the fourth method needs a closure that returns a String, you need to return a string.

For example, this:

```<code>self.the4Method("World") { (arg2do) -> String in
    print("1 ",arg2do)
    return "return value"
}
</code>```

prints:

```<code>5
1  Pe
An  return value
</code>```

Explanation of the output:

First the body of the4Method gets run, so 5 gets printed. Then the4Method calls endOfMethod, which is the closure passed in, with the argument "Pe". So print("1 ", "Pe") gets run, causing the second line to be printed. After that, the closure returns "return value" and we are now back to the4Method. The return value gets assigned to an, and print("An ",an) is run, causing the last line to be printed.

Body:
iOS 13, Swift 5

I am trying get my head around closure syntax in Swift and almost there. I got the first three bon, and understand them; but the last no. How do I call self.theMethod4 here? the syntax shown is wrong!

struct ContentView: View {
var body: some View {
    Text("Hello, World!")
      .onAppear {
        self.theMethod {
          print("1")
        }
        self.the2Method("Ya ") {
          print("1")
        }
        self.the3Method("Hello") {arg2do in
          print("1 ",arg2do)
        }
        self.the4Method("World") { (arg2do) -> String in
          print("1 ",arg2do)
        }
      }
}

func theMethod(_ endOfMethod: @escaping () -> Void) {
  print("2")
  endOfMethod()
}

func the2Method(_ arg1:String, endOfMethod: @escaping () -> Void) {
  print("3")
  endOfMethod()
}

func the3Method(_ arg1:String, endOfMethod: @escaping (_ arg2:String) -> Void) {
  print("4")
  endOfMethod("Yo")
}

func the4Method(_ arg1:String, endOfMethod: @escaping (_ arg2: String) -> String) {
  print("5")
  let an = endOfMethod("Pe")
  print("An ",an)
}
}

Body:
You got the closure syntax ({ (parameters) -> ReturnValueType in statements }) right, but since the fourth method needs a closure that returns a String, you need to return a string.

For example, this:

self.the4Method("World") { (arg2do) -> String in
    print("1 ",arg2do)
    return "return value"
}

prints:

5
1  Pe
An  return value

Explanation of the output:

First the body of the4Method gets run, so 5 gets printed. Then the4Method calls endOfMethod, which is the closure passed in, with the argument "Pe". So print("1 ", "Pe") gets run, causing the second line to be printed. After that, the closure returns "return value" and we are now back to the4Method. The return value gets assigned to an, and print("An ",an) is run, causing the last line to be printed.


问:
Laravel and scopes hate me - Sorry - I don't know how else to phase it
So we have all been there - you are doing something simple that is giving you very odd results and you try everything to understand the issue but you can only conclude that the code hates you! (You know you are overlooking something)

That is me right now...

I have a game with many user and users have to be approved to be actually playing so I have created a playing scope on the User object so I can write statements like:

```<code>$game->players()->playing()->count();
</code>```

This returns a count of the players who are actually approved to play.

The scope is:

```<code>public function scopePlaying($query){
    return $query->where('status', Self::APPROVED)
                    ->orderBy('position', 'asc');
}
</code>```

This scope works well, however being approved is not the only condition so I extended the scope to include this other condition:

```<code>public function scopePlaying($query){
    return $query->where('status', Self::APPROVED)
                    ->where('action', '<>', Self::ACTION_FOLD)
                    ->orderBy('position', 'asc');
}
</code>```

And suddenly the scope returns nothing. Ever. If I change the name of the "action" column to something that does not exist on the table instead of it throwing an error it starts working as it did before adding the new where clause.

Is my computer possessed and is just doing this to make me give up programming forever? What am I missing?

Output from the query log:

```<code>[2020-06-02 12:29:04] testing.DEBUG: array (
  0 =>
   array (
     'query' => 'select count(*) as aggregate from "users" where "users"."game_id" = ? and "users"."game_id" is not null and "status" = ? and "action" <> ?',
'bindings' =>
array (
  0 => 'c1f666c7-dc6c-47c1-9abc-8decba505ac5',
  1 => 'approved',
  2 => 'fold',
),
'time' => 0.13,
</code>```

),
)

My Migration for the table:

```<code>Schema::create('users', function (Blueprint $table) {
        $table->uuid('id')->primary();
        $table->string('game_id');
        $table->string('name');
        $table->string('status', 10)->default('online');
        $table->string('action', 10)->nullable();
        $table->smallInteger('position')->nullable();
        $table->timestamps();
    });
</code>```

答:
I've added a similar local scope to a model in one of my projects to replicate:

public function scopeTest($query)
{
    return $query->where('id', '<', 10)->
        where('store_option_id', '<>', 2)->
        orderBy('position', 'asc');
}
```
<p>And the scope works fine for me. I can confirm it doesn't have anything to do with the orderBy clause, or having two where clauses. It could be that your data just doesn't match the where clauses (ie maybe there are no active users that also doesn't have an action of fold). Or maybe 'action' is misspelled in your migration. The best way to debug this would be to execute the query manually on your database.</p>
<h3>Update:</h3>
<p>I'm almost certain there is a typo or something in the code that generates your test data. It's to know what because you haven't posted that code, but I changed all the store_option_id's to NULL and lo and behold: empty set. <em><strong>Or maybe</strong> you meant for those fields to be NULL</em>, <strong>but null doesn't compare to anything in SQL</strong>, see:</p>
<p>https://dev.mysql.com/doc/refman/8.0/en/working-with-null.html</p>
<blockquote>
<p>The NULL value can be surprising until you get used to it. Conceptually, NULL means “a missing unknown value” and it is treated somewhat differently from other values.</p>
<p>To test for NULL, use the IS NULL and IS NOT NULL operators</p>
</blockquote>
<p>...</p>
<blockquote>
<p>You cannot use arithmetic comparison operators such as =, <, or <> to test for NULL</p>
</blockquote>
<p>So if you want to include rows with NULL action columns you must change your scope as such:</p>
<pre class="lang-php prettyprint-override"><code>
public function scopePlaying($query){
    return $query->where('status', Self::APPROVED)->
            where(function ($query) {
                return $query->
                    where('action', '<>', Self::ACTION_FOLD)->
                    orWhereNull('action');
            });
}
</code>```

Body:
So we have all been there - you are doing something simple that is giving you very odd results and you try everything to understand the issue but you can only conclude that the code hates you! (You know you are overlooking something)

That is me right now...

I have a game with many user and users have to be approved to be actually playing so I have created a playing scope on the User object so I can write statements like:

$game->players()->playing()->count();

This returns a count of the players who are actually approved to play.

The scope is:

public function scopePlaying($query){
    return $query->where('status', Self::APPROVED)
                    ->orderBy('position', 'asc');
}

This scope works well, however being approved is not the only condition so I extended the scope to include this other condition:

public function scopePlaying($query){
    return $query->where('status', Self::APPROVED)
                    ->where('action', '<>', Self::ACTION_FOLD)
                    ->orderBy('position', 'asc');
}

And suddenly the scope returns nothing. Ever. If I change the name of the "action" column to something that does not exist on the table instead of it throwing an error it starts working as it did before adding the new where clause.

Is my computer possessed and is just doing this to make me give up programming forever? What am I missing?

Output from the query log:

[2020-06-02 12:29:04] testing.DEBUG: array (
  0 =>
   array (
     'query' => 'select count(*) as aggregate from "users" where "users"."game_id" = ? and "users"."game_id" is not null and "status" = ? and "action" <> ?',
'bindings' =>
array (
  0 => 'c1f666c7-dc6c-47c1-9abc-8decba505ac5',
  1 => 'approved',
  2 => 'fold',
),
'time' => 0.13,

),
)

My Migration for the table:

Schema::create('users', function (Blueprint $table) {
        $table->uuid('id')->primary();
        $table->string('game_id');
        $table->string('name');
        $table->string('status', 10)->default('online');
        $table->string('action', 10)->nullable();
        $table->smallInteger('position')->nullable();
        $table->timestamps();
    });

Body:
I've added a similar local scope to a model in one of my projects to replicate:

public function scopeTest($query)
{
    return $query->where('id', '<', 10)->
        where('store_option_id', '<>', 2)->
        orderBy('position', 'asc');
}

And the scope works fine for me. I can confirm it doesn't have anything to do with the orderBy clause, or having two where clauses. It could be that your data just doesn't match the where clauses (ie maybe there are no active users that also doesn't have an action of fold). Or maybe 'action' is misspelled in your migration. The best way to debug this would be to execute the query manually on your database.

Update:

I'm almost certain there is a typo or something in the code that generates your test data. It's to know what because you haven't posted that code, but I changed all the store_option_id's to NULL and lo and behold: empty set. Or maybe you meant for those fields to be NULL, but null doesn't compare to anything in SQL, see:

https://dev.mysql.com/doc/refman/8.0/en/working-with-null.html

The NULL value can be surprising until you get used to it. Conceptually, NULL means “a missing unknown value” and it is treated somewhat differently from other values.

To test for NULL, use the IS NULL and IS NOT NULL operators

...

You cannot use arithmetic comparison operators such as =, <, or <> to test for NULL

So if you want to include rows with NULL action columns you must change your scope as such:

public function scopePlaying($query){
    return $query->where('status', Self::APPROVED)->
            where(function ($query) {
                return $query->
                    where('action', '<>', Self::ACTION_FOLD)->
                    orWhereNull('action');
            });
}