问:
How to send message to client in Socket.Stream?
There is an application in C # to which I need to attach a web interface with a minimum response time (several timers are displayed), so I want to connect via socket mozilla has a great example just for me. It already shows the connection, handshake and reading a message from the client. Bonus, the JS side is shown. It just doesn't show how to compose a response to the client. The following page shows what bytes the message is from, but I still don't I can make it right.
Whenever you try to send something to the client, the connection is immediately terminated, even if you send the same byte array that came.
Please help me to make the function of sending a message, I couldn't myself ((</p>

答:
I found how to do it. If anyone needs it, I'll leave my code here. It is the same as on mozila but I added the SendEcho function

Code server on C#

```<code> using System;
 using System.Collections.Generic;
 using System.Net;
 using System.Net.Sockets;
 using System.Text;
 using System.Text.RegularExpressions;

 namespace TestSocket
 {
class Program
{

    public static void Main()
    {

        string ip = "127.0.0.1";
        int port = 80;
        var server = new TcpListener(IPAddress.Parse(ip), port);

        string swkaSha1Base64 = "";
        byte[] response = new byte[1];
        server.Start();
        Console.WriteLine("Server has started on {0}:{1}, Waiting for a connection...", ip, port);

        TcpClient client = server.AcceptTcpClient();
        Console.WriteLine("A client connected.");

        NetworkStream stream = client.GetStream();

        // enter to an infinite cycle to be able to handle every change in stream
        while (true)
        {
            while (!stream.DataAvailable) ;
            while (client.Available < 3) ; // match against "get"

            byte[] bytes = new byte[client.Available];
            stream.Read(bytes, 0, client.Available);
            string s = Encoding.UTF8.GetString(bytes);

            if (Regex.IsMatch(s, "^GET", RegexOptions.IgnoreCase))
            {
                Console.WriteLine("=====Handshaking from client=====\n{0}", s);

                // 1. Obtain the value of the "Sec-WebSocket-Key" request header without any leading or trailing whitespace
                // 2. Concatenate it with "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" (a special GUID specified by RFC 6455)
                // 3. Compute SHA-1 and Base64 hash of the new value
                // 4. Write the hash back as the value of "Sec-WebSocket-Accept" response header in an HTTP response
                string swk = Regex.Match(s, "Sec-WebSocket-Key: (.*)").Groups[1].Value.Trim();
                string swka = swk + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
                byte[] swkaSha1 = System.Security.Cryptography.SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(swka));
                swkaSha1Base64 = Convert.ToBase64String(swkaSha1);

                // HTTP/1.1 defines the sequence CR LF as the end-of-line marker
                response = Encoding.UTF8.GetBytes(
                    "HTTP/1.1 101 Switching Protocols\r\n" +
                    "Connection: Upgrade\r\n" +
                    "Upgrade: websocket\r\n" +
                    "Sec-WebSocket-Accept: " + swkaSha1Base64 + "\r\n\r\n");

                string x = Encoding.UTF8.GetString(response);
                Console.WriteLine(x);

                stream.Write(response, 0, response.Length);
            }
            else
            {
                bool fin = (bytes[0] & 0b10000000) != 0,
                    mask = (bytes[1] & 0b10000000) != 0; // must be true, "All messages from the client to the server have this bit set"

                if (bytes[0] == 0x88)
                {
                    Console.WriteLine("соединение закрыто");
                }

                int opcode = bytes[0] & 0b00001111, // expecting 1 - text message
                    msglen = bytes[1] - 128, // & 0111 1111
                    offset = 2;

                if (msglen == 126)
                {
                    // was ToUInt16(bytes, offset) but the result is incorrect
                    msglen = BitConverter.ToUInt16(new byte[] { bytes[3], bytes[2] }, 0);
                    offset = 4;
                }
                else if (msglen == 127)
                {
                    Console.WriteLine("TODO: msglen == 127, needs qword to store msglen");
                    // i don't really know the byte order, please edit this
                    // msglen = BitConverter.ToUInt64(new byte[] { bytes[5], bytes[4], bytes[3], bytes[2], bytes[9], bytes[8], bytes[7], bytes[6] }, 0);
                    // offset = 10;
                }

                if (msglen == 0)
                    Console.WriteLine("msglen == 0");
                else if (mask)
                {
                    byte[] decoded = new byte[msglen];
                    byte[] masks = new byte[4] { bytes[offset], bytes[offset + 1], bytes[offset + 2], bytes[offset + 3] };
                    offset += 4;

                    for (int i = 0; i < msglen; ++i)
                        decoded[i] = (byte)(bytes[offset + i] ^ masks[i % 4]);

                    string text = Encoding.UTF8.GetString(decoded);

                    SendEcho(stream, text); //my update

                    Console.WriteLine("{0}", text);
                }
                else
                    Console.WriteLine("mask bit not set");

                Console.WriteLine();
            }
        }
    }
    private static void SendEcho(NetworkStream stream, string inputText)
    {

        byte[] sendBytes = Encoding.UTF8.GetBytes(inputText);
        byte lengthHeader = 0;
        byte[] lengthCount = new byte[] { };

        if (sendBytes.Length <= 125)
            lengthHeader = (byte)sendBytes.Length;

        if (125 < sendBytes.Length && sendBytes.Length < 65535) //System.UInt16
        {
            lengthHeader = 126;

               lengthCount = new byte[] {
                (byte)(sendBytes.Length >> 8),
                (byte)(sendBytes.Length)
            };
        }

        if (sendBytes.Length > 65535)//max 2_147_483_647 but .Length -> System.Int32
        { lengthHeader = 127;
            lengthCount = new byte[] {
                (byte)(sendBytes.Length >> 56),
                (byte)(sendBytes.Length >> 48),
                (byte)(sendBytes.Length >> 40),
                (byte)(sendBytes.Length >> 32),
                (byte)(sendBytes.Length >> 24),
                (byte)(sendBytes.Length >> 16),
                (byte)(sendBytes.Length >> 8),
                (byte)sendBytes.Length,
            };
        }

        List<byte> responseArray = new List<byte>() { 0b10000001 };

        responseArray.Add(lengthHeader);
        responseArray.AddRange(lengthCount);
        responseArray.AddRange(sendBytes);

        stream.Write(responseArray.ToArray(), 0, responseArray.Count);
    }

}
}
</code>```

HTML and JS code on browser (i use chrome 98)

```<code><!doctype html>
<style>
textarea { vertical-align: bottom; }
#output { overflow: auto; }
#output > p { overflow-wrap: break-word; }
#output span { color: blue; }
#output span.error { color: red; }
</style>
<h2>WebSocket Test</h2>
<textarea cols=60 rows=6></textarea>
<button>send</button>
<div id=output></div>
<script>
// http://www.websocket.org/echo.html

var button = document.querySelector("button"),
    output = document.querySelector("#output"),
    textarea = document.querySelector("textarea"),
    // wsUri = "ws://echo.websocket.org/",
    wsUri = "ws://127.0.0.1/",
    websocket = new WebSocket(wsUri);

button.addEventListener("click", onClickButton);

websocket.onopen = function (e) {
    writeToScreen("CONNECTED");
    doSend("WebSocket rocks");
};

websocket.onclose = function (e) {
    writeToScreen("DISCONNECTED");
};

websocket.onmessage = function (e) {
    writeToScreen("<span>RESPONSE: " + e.data + "</span>");
};

websocket.onerror = function (e) {
    writeToScreen("<span class=error>ERROR:</span> " + e.data);
};

function doSend(message) {
    writeToScreen("SENT: " + message);
    websocket.send(message);
}

function writeToScreen(message) {
    output.insertAdjacentHTML("afterbegin", "<p>" + message + "</p>");
}

function onClickButton() {
    var text = textarea.value;

    text && doSend(text);
    textarea.value = "";
    textarea.focus();
}
</script>
</code>```

Body:
I found how to do it. If anyone needs it, I'll leave my code here. It is the same as on mozila but I added the SendEcho function

Code server on C#

 using System;
 using System.Collections.Generic;
 using System.Net;
 using System.Net.Sockets;
 using System.Text;
 using System.Text.RegularExpressions;

 namespace TestSocket
 {
class Program
{

    public static void Main()
    {

        string ip = "127.0.0.1";
        int port = 80;
        var server = new TcpListener(IPAddress.Parse(ip), port);

        string swkaSha1Base64 = "";
        byte[] response = new byte[1];
        server.Start();
        Console.WriteLine("Server has started on {0}:{1}, Waiting for a connection...", ip, port);

        TcpClient client = server.AcceptTcpClient();
        Console.WriteLine("A client connected.");

        NetworkStream stream = client.GetStream();

        // enter to an infinite cycle to be able to handle every change in stream
        while (true)
        {
            while (!stream.DataAvailable) ;
            while (client.Available < 3) ; // match against "get"

            byte[] bytes = new byte[client.Available];
            stream.Read(bytes, 0, client.Available);
            string s = Encoding.UTF8.GetString(bytes);

            if (Regex.IsMatch(s, "^GET", RegexOptions.IgnoreCase))
            {
                Console.WriteLine("=====Handshaking from client=====\n{0}", s);

                // 1. Obtain the value of the "Sec-WebSocket-Key" request header without any leading or trailing whitespace
                // 2. Concatenate it with "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" (a special GUID specified by RFC 6455)
                // 3. Compute SHA-1 and Base64 hash of the new value
                // 4. Write the hash back as the value of "Sec-WebSocket-Accept" response header in an HTTP response
                string swk = Regex.Match(s, "Sec-WebSocket-Key: (.*)").Groups[1].Value.Trim();
                string swka = swk + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
                byte[] swkaSha1 = System.Security.Cryptography.SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(swka));
                swkaSha1Base64 = Convert.ToBase64String(swkaSha1);

                // HTTP/1.1 defines the sequence CR LF as the end-of-line marker
                response = Encoding.UTF8.GetBytes(
                    "HTTP/1.1 101 Switching Protocols\r\n" +
                    "Connection: Upgrade\r\n" +
                    "Upgrade: websocket\r\n" +
                    "Sec-WebSocket-Accept: " + swkaSha1Base64 + "\r\n\r\n");

                string x = Encoding.UTF8.GetString(response);
                Console.WriteLine(x);

                stream.Write(response, 0, response.Length);
            }
            else
            {
                bool fin = (bytes[0] & 0b10000000) != 0,
                    mask = (bytes[1] & 0b10000000) != 0; // must be true, "All messages from the client to the server have this bit set"

                if (bytes[0] == 0x88)
                {
                    Console.WriteLine("соединение закрыто");
                }

                int opcode = bytes[0] & 0b00001111, // expecting 1 - text message
                    msglen = bytes[1] - 128, // & 0111 1111
                    offset = 2;

                if (msglen == 126)
                {
                    // was ToUInt16(bytes, offset) but the result is incorrect
                    msglen = BitConverter.ToUInt16(new byte[] { bytes[3], bytes[2] }, 0);
                    offset = 4;
                }
                else if (msglen == 127)
                {
                    Console.WriteLine("TODO: msglen == 127, needs qword to store msglen");
                    // i don't really know the byte order, please edit this
                    // msglen = BitConverter.ToUInt64(new byte[] { bytes[5], bytes[4], bytes[3], bytes[2], bytes[9], bytes[8], bytes[7], bytes[6] }, 0);
                    // offset = 10;
                }

                if (msglen == 0)
                    Console.WriteLine("msglen == 0");
                else if (mask)
                {
                    byte[] decoded = new byte[msglen];
                    byte[] masks = new byte[4] { bytes[offset], bytes[offset + 1], bytes[offset + 2], bytes[offset + 3] };
                    offset += 4;

                    for (int i = 0; i < msglen; ++i)
                        decoded[i] = (byte)(bytes[offset + i] ^ masks[i % 4]);

                    string text = Encoding.UTF8.GetString(decoded);

                    SendEcho(stream, text); //my update

                    Console.WriteLine("{0}", text);
                }
                else
                    Console.WriteLine("mask bit not set");

                Console.WriteLine();
            }
        }
    }
    private static void SendEcho(NetworkStream stream, string inputText)
    {

        byte[] sendBytes = Encoding.UTF8.GetBytes(inputText);
        byte lengthHeader = 0;
        byte[] lengthCount = new byte[] { };

        if (sendBytes.Length <= 125)
            lengthHeader = (byte)sendBytes.Length;

        if (125 < sendBytes.Length && sendBytes.Length < 65535) //System.UInt16
        {
            lengthHeader = 126;

               lengthCount = new byte[] {
                (byte)(sendBytes.Length >> 8),
                (byte)(sendBytes.Length)
            };
        }

        if (sendBytes.Length > 65535)//max 2_147_483_647 but .Length -> System.Int32
        { lengthHeader = 127;
            lengthCount = new byte[] {
                (byte)(sendBytes.Length >> 56),
                (byte)(sendBytes.Length >> 48),
                (byte)(sendBytes.Length >> 40),
                (byte)(sendBytes.Length >> 32),
                (byte)(sendBytes.Length >> 24),
                (byte)(sendBytes.Length >> 16),
                (byte)(sendBytes.Length >> 8),
                (byte)sendBytes.Length,
            };
        }

        List<byte> responseArray = new List<byte>() { 0b10000001 };

        responseArray.Add(lengthHeader);
        responseArray.AddRange(lengthCount);
        responseArray.AddRange(sendBytes);

        stream.Write(responseArray.ToArray(), 0, responseArray.Count);
    }

}
}

HTML and JS code on browser (i use chrome 98)

<!doctype html>
<style>
textarea { vertical-align: bottom; }
#output { overflow: auto; }
#output > p { overflow-wrap: break-word; }
#output span { color: blue; }
#output span.error { color: red; }
</style>
<h2>WebSocket Test</h2>
<textarea cols=60 rows=6></textarea>
<button>send</button>
<div id=output></div>
<script>
// http://www.websocket.org/echo.html

var button = document.querySelector("button"),
    output = document.querySelector("#output"),
    textarea = document.querySelector("textarea"),
    // wsUri = "ws://echo.websocket.org/",
    wsUri = "ws://127.0.0.1/",
    websocket = new WebSocket(wsUri);

button.addEventListener("click", onClickButton);

websocket.onopen = function (e) {
    writeToScreen("CONNECTED");
    doSend("WebSocket rocks");
};

websocket.onclose = function (e) {
    writeToScreen("DISCONNECTED");
};

websocket.onmessage = function (e) {
    writeToScreen("<span>RESPONSE: " + e.data + "</span>");
};

websocket.onerror = function (e) {
    writeToScreen("<span class=error>ERROR:</span> " + e.data);
};

function doSend(message) {
    writeToScreen("SENT: " + message);
    websocket.send(message);
}

function writeToScreen(message) {
    output.insertAdjacentHTML("afterbegin", "<p>" + message + "</p>");
}

function onClickButton() {
    var text = textarea.value;

    text && doSend(text);
    textarea.value = "";
    textarea.focus();
}
</script>


来源: stackexchange

问:
Unexpected Token - Java Enum
Is there any option in java to Create an enum with true and false like below,

```<code>public enum options {
true,
false,
both
}
</code>```

Now getting unexpected token error as I am using true and false. thank you

Regards
haru

答:
The values of your enum should be formatted like constants; all-caps with underscores between words (if they are more than one word, which in your case, they are not). If you need to be able to convert them to/from strings that do not match the name and case of the enum constants, I would suggest adding a parameter with the string and methods to convert in each direction:

```<code>public enum Options {
    TRUE("true"),
    FALSE("false"),
    BOTH("both");

    private final String description;

    private Options(final String description) {
        this.description = description;
    }

    public String getDescription() {
        return description;
    }

    public static Options parse(String description) {
        for (Options option : Options.values()) {
            if (option.getDescription().equals(description)) {
                return option;
            }
        }
        throw new IllegalArgumentException("no such option: " + description);
    }
}
</code>```

If you call Options.parse("true") it will return Options.TRUE and if you call Options.TRUE.getDescription() it will return "true". If you call Options.parse("none") it will throw an IllegalArgumentException.

ContentLicense: CC BY-SA 4.0

Title: Unexpected Token - Java Enum

Body:
Is there any option in java to Create an enum with true and false like below,

public enum options {
true,
false,
both
}

Now getting unexpected token error as I am using true and false. thank you

Regards
haru

Body:
The values of your enum should be formatted like constants; all-caps with underscores between words (if they are more than one word, which in your case, they are not). If you need to be able to convert them to/from strings that do not match the name and case of the enum constants, I would suggest adding a parameter with the string and methods to convert in each direction:

public enum Options {
    TRUE("true"),
    FALSE("false"),
    BOTH("both");

    private final String description;

    private Options(final String description) {
        this.description = description;
    }

    public String getDescription() {
        return description;
    }

    public static Options parse(String description) {
        for (Options option : Options.values()) {
            if (option.getDescription().equals(description)) {
                return option;
            }
        }
        throw new IllegalArgumentException("no such option: " + description);
    }
}

If you call Options.parse("true") it will return Options.TRUE and if you call Options.TRUE.getDescription() it will return "true". If you call Options.parse("none") it will throw an IllegalArgumentException.


问:
select * only return one value
I want select multiple (all) values from table Account.

```<code>string query = "SELECT * FROM Account";
        SqlConnection connection = new SqlConnection(connectionString);
        SqlCommand command = new SqlCommand(query, connection);
        SqlDataReader reader;
        connection.Open();
        reader = command.ExecuteReader();
        reader.Read();
        label1.Text = reader["PasswordHash"].ToString();
        connection.Close();
</code>```

Why is this always returning only the first row. Actually it return one row, because if I set in where clause something like where id = 2 and id = 3 it still returns only one value.
Table have more than one value i checked form Management Studio, there query run as they should.

Thanks in advance.

答:
You should do

```<code>while (reader.Read())
    // process information
</code>```

You need to iterate all the information you retrieved.

Sidenote: Use using statements on your SqlConnection, SqlCommand, and SqlDataReader to make sure the objects get disposed correctly.

ContentLicense: CC BY-SA 3.0

Title: select * only return one value

Body:
I want select multiple (all) values from table Account.

string query = "SELECT * FROM Account";
        SqlConnection connection = new SqlConnection(connectionString);
        SqlCommand command = new SqlCommand(query, connection);
        SqlDataReader reader;
        connection.Open();
        reader = command.ExecuteReader();
        reader.Read();
        label1.Text = reader["PasswordHash"].ToString();
        connection.Close();

Why is this always returning only the first row. Actually it return one row, because if I set in where clause something like where id = 2 and id = 3 it still returns only one value.
Table have more than one value i checked form Management Studio, there query run as they should.

Thanks in advance.

Body:
You should do

while (reader.Read())
    // process information

You need to iterate all the information you retrieved.

Sidenote: Use using statements on your SqlConnection, SqlCommand, and SqlDataReader to make sure the objects get disposed correctly.


问:
Is there a way to name a Processor in Kafka Streams DSL in Scala
I’ve been trying to name a KStream using Kafka Streams DSL in Scala but I cannot find a way to name a processor in org.apache.kafka.streams.scala.kstream.Consumed.
Although there is a java method org.apache.kafka.streams.kstream.Consumed#as but it throws an exception. Does anyone knows what can be done?

```<code>ClassCastException invoking Processor. Do the Processor's input types match the deserialized types? Check the Serde setup and change the default Serdes in StreamConfig or provide correct Serdes via method parameters. Make sure the Processor can accept the deserialized input of type key: [B, and value: [B.
Note that although incorrect Serdes are a common cause of error, the cast exception might have another cause (in user code, for example). For example, if a processor wires in a store, but casts the generics incorrectly, a class cast exception could be raised during processing, but the cause would not be wrong Serdes.
org.apache.kafka.streams.errors.StreamsException: ClassCastException invoking Processor. Do the Processor's input types match the deserialized types? Check the Serde setup and change the default Serdes in StreamConfig or provide correct Serdes via method parameters. Make sure the Processor can accept the deserialized input of type key: [B, and value: [B.
Note that although incorrect Serdes are a common cause of error, the cast exception might have another cause (in user code, for example). For example, if a processor wires in a store, but casts the generics incorrectly, a class cast exception could be raised during processing, but the cause would not be wrong Serdes.
    at org.apache.kafka.streams.processor.internals.ProcessorNode.process(ProcessorNode.java:146)
    at org.apache.kafka.streams.processor.internals.ProcessorContextImpl.forward(ProcessorContextImpl.java:236)
    at org.apache.kafka.streams.processor.internals.ProcessorContextImpl.forward(ProcessorContextImpl.java:216)
    at org.apache.kafka.streams.processor.internals.ProcessorContextImpl.forward(ProcessorContextImpl.java:168)
    at org.apache.kafka.streams.processor.internals.SourceNode.process(SourceNode.java:85)
    at org.apache.kafka.streams.processor.internals.StreamTask.lambda$process$1(StreamTask.java:678)
    at ...
</code>```

EDIT:
The code that I used:

```<code>val someEvents = builder
  .stream[String, String]("some_events")
(org.apache.kafka.streams.kstream.Consumed.as("some_event_stream"))
</code>```

The code I should've used:

```<code>val someEvents = builder
  .stream[String, String]("some_events")
(org.apache.kafka.streams.kstream.Consumed.as("some_event_stream")
  .withKeySerde(Serdes.String())
  .withValueSerde(Serdes.String()))
</code>```

答:
Initial code:

```<code>val someEvents = builder
    .stream[String, String]("some_events")
  (org.apache.kafka.streams.kstream.Consumed.as("some_event_stream"))
</code>```

I made two mistakes:

Put  Consumed parameter in a separate line. (todo needs explanation why it doesn't work in Scala)

Used Java method org.apache.kafka.streams.kstream.Consumed.as and didn't provide SerDes.

This works:

```<code>val someEvents = builder.stream[String, String]("some_events")(org.apache.kafka.streams.kstream.Consumed.as("some_event_stream").withKeySerde(Serdes.String()).withValueSerde(Serdes.String()))
</code>```

Body:
I’ve been trying to name a KStream using Kafka Streams DSL in Scala but I cannot find a way to name a processor in org.apache.kafka.streams.scala.kstream.Consumed.
Although there is a java method org.apache.kafka.streams.kstream.Consumed#as but it throws an exception. Does anyone knows what can be done?

ClassCastException invoking Processor. Do the Processor's input types match the deserialized types? Check the Serde setup and change the default Serdes in StreamConfig or provide correct Serdes via method parameters. Make sure the Processor can accept the deserialized input of type key: [B, and value: [B.
Note that although incorrect Serdes are a common cause of error, the cast exception might have another cause (in user code, for example). For example, if a processor wires in a store, but casts the generics incorrectly, a class cast exception could be raised during processing, but the cause would not be wrong Serdes.
org.apache.kafka.streams.errors.StreamsException: ClassCastException invoking Processor. Do the Processor's input types match the deserialized types? Check the Serde setup and change the default Serdes in StreamConfig or provide correct Serdes via method parameters. Make sure the Processor can accept the deserialized input of type key: [B, and value: [B.
Note that although incorrect Serdes are a common cause of error, the cast exception might have another cause (in user code, for example). For example, if a processor wires in a store, but casts the generics incorrectly, a class cast exception could be raised during processing, but the cause would not be wrong Serdes.
    at org.apache.kafka.streams.processor.internals.ProcessorNode.process(ProcessorNode.java:146)
    at org.apache.kafka.streams.processor.internals.ProcessorContextImpl.forward(ProcessorContextImpl.java:236)
    at org.apache.kafka.streams.processor.internals.ProcessorContextImpl.forward(ProcessorContextImpl.java:216)
    at org.apache.kafka.streams.processor.internals.ProcessorContextImpl.forward(ProcessorContextImpl.java:168)
    at org.apache.kafka.streams.processor.internals.SourceNode.process(SourceNode.java:85)
    at org.apache.kafka.streams.processor.internals.StreamTask.lambda$process$1(StreamTask.java:678)
    at ...

EDIT:
The code that I used:

val someEvents = builder
  .stream[String, String]("some_events")
(org.apache.kafka.streams.kstream.Consumed.as("some_event_stream"))

The code I should've used:

val someEvents = builder
  .stream[String, String]("some_events")
(org.apache.kafka.streams.kstream.Consumed.as("some_event_stream")
  .withKeySerde(Serdes.String())
  .withValueSerde(Serdes.String()))

Body:
Initial code:

val someEvents = builder
    .stream[String, String]("some_events")
  (org.apache.kafka.streams.kstream.Consumed.as("some_event_stream"))

I made two mistakes:

Put  Consumed parameter in a separate line. (todo needs explanation why it doesn't work in Scala)

Used Java method org.apache.kafka.streams.kstream.Consumed.as and didn't provide SerDes.

This works:

val someEvents = builder.stream[String, String]("some_events")(org.apache.kafka.streams.kstream.Consumed.as("some_event_stream").withKeySerde(Serdes.String()).withValueSerde(Serdes.String()))


问:
how to improve face recognition accuracy in facenet
I am trying to develop a Bollywood face recognition system in videos. I have used facenet to generate training image embedding and store 128-bit face embedding in the elastic search index. recognition happens by using test face embedding being compared with elastic indexed embedding using l2 similarity measure. it seems that chances of misclassification with low similarity threshold is very high. if I increase the threshold the recognition will be poor(less). how can we improve the model in this setup. does triplet loss training come into the picture here, if so how can I do that? any help with this will be very much helpful and well appreciated. thank you</p>

答:
From my experience (I'm using face_tecognition/ dlib for making 128d face encodings) Elasticsearch works perfectly, even much better then instant dlib methods:

```<code>                s_body = {"size": app_conf.ELASTIC_MAX_RESULTS, "min_score": 1 - tolerance,
                          "query": {
                                "script_score": {
                                    "query": {
                                        "match_all": {}
                                    },
                            "script": {
                                "source": "1 / (1 + l2norm(params.query_vector, 'face_vector'))",
                                "params": {"query_vector": wanted_encoding}
                                        }
                                    }
                                }
                        }
                res = es.search(index="idx", body=s_body)
...
 for hit in res["hits"]["hits"]:
    ...
</code>```

Update from 13.02.202:
Also, as I already wrote I'm using standrd pre-trained face_recognition_model.
However, some preprocessing measures are implemented too:

check if face is rotated:

def face_align(img, box_index, eyes=None, checksideview=True):
"""[Rotate face to upright position]

```<code>  Args:
      img ([nparray]): [base input phoyo]
      box_index ([int]): [number of facebox is multiface]
      eyes ([list]): [coordinates of eyes ("left_eye" (x, y) - "right_eye" (x, y))]

  Returns:
      [boolean]: [True  if face is rotated too much: more then 5 fegrees]
      [nparray]: [corrected/rotated to upright position image]
  """
  reload(app_conf)

  if not eyes: # no eyes data being provided
      boxes, eyes = mp_boxes(img, fl_use_dlib=True, fl_use_mediapipe=False, fl_verbous=False)

  if eyes:
      left_eye_x = eyes[box_index][0][0]
      left_eye_y = eyes[box_index][0][1]
      left_eye = (left_eye_x, left_eye_y)

      right_eye_x = eyes[box_index][1][0]
      right_eye_y = eyes[box_index][1][1]
      right_eye = (right_eye_x, right_eye_y)

      if checksideview: #check if face rotated tooo much
          left_chin_x = eyes[box_index][2][0]
          left_chin_y = eyes[box_index][2][1]
          left_chin = (left_chin_x, left_chin_y)

          right_chin_x = eyes[box_index][3][0]
          right_chin_y =eyes[box_index][3][1]
          right_chin = (right_chin_x, right_chin_y)

          right_dist = euclidean_distance(right_chin, right_eye)
          left_dist = euclidean_distance(left_chin, left_eye)

          k_side_face = right_dist / left_dist
          if 0.5 > k_side_face or k_side_face > 2.0: #change acorrding to exp.
              return False, img # face is "from a side" tooo much

      if left_eye_y > right_eye_y:
          point_3rd = (right_eye_x, left_eye_y)
          direction = -1 #rotate clockwise
      else:
          point_3rd = (left_eye_x, right_eye_y)
          direction = 1 #rotate counter clockwise

      a = euclidean_distance(eyes[box_index][0], point_3rd)
      b = euclidean_distance(eyes[box_index][1], point_3rd)
      c = euclidean_distance(eyes[box_index][1], eyes[box_index][0])
      cos_a = (b*b + c*c - a*a)/(2*b*c)

      angle = np.arccos(cos_a)
      angle = (angle * 180) / pi

      if angle < app_conf.MIN_FACE_ROTATION: #if rotation less then 5 degrees
          return True, img

      if direction == -1:
          angle = 90 - angle

      new_img = Image.fromarray(img, mode="RGB")
      new_img = np.array(new_img.rotate(direction * angle))
      new_img = cv2.cvtColor(new_img, cv2.COLOR_BGR2RGB)
      return True, new_img

  else:
      return True, img #do nothing
</code>```

And "eyes" I'm getting from:

```<code>def mp_boxes(image, confid=0.4, fl_use_dlib=True, fl_use_mediapipe=True, fl_verbous=True):
    """[Function uses Medipipe CNN to find out all faces boxes on the given image.
     It's about 50 times faster even on CPU but sometime consupms huge amount of RAM...
    then original dlib / Face_recognition functions.]
    If Mediapipe found no face boxes (it's possible and depends of image features,
    the function consequently tries to use dlib CNN and HOG models for that.)

    Args:
        image ([np.array]): [image in memory to be proccessed]

    Returns:
        boxes [list]: [list of coordinates of face boundaries boxes]
        eyes [list]: [list of (left_eye, right_eye) for every box]
    """
    reload(app_conf)
    mod5_68 = "large"
    boxes = []
    dboxes = []
    eyes = []
    if fl_use_dlib:
        if DLIB_USE_CUDA: #tryin to use CNN on GPU first
            try:
                dboxes = frfl(image, number_of_times_to_upsample=1, model="cnn")
                for box in dboxes: #checking MIN_FACEBOX_DIM conditions
                    if (box[1] - box[3]) > app_conf.MIN_FACEBOX_DIM[0] and (box[2] - box[0]) > app_conf.MIN_FACEBOX_DIM[1]:
                        boxes.append(box)
                if fl_verbous:
                    print("[INFO] %s: dlib CNN being used and found %dface box" % (datetime.now(), len(boxes)))
            except:
                dboxes = frfl(image, number_of_times_to_upsample=1, model="hog")
                for box in dboxes:
                    if (box[1] - box[3]) > app_conf.MIN_FACEBOX_DIM[0] and (box[2] - box[0]) > app_conf.MIN_FACEBOX_DIM[1]:
                        boxes.append(box)
                if fl_verbous:
                    print("[INFO] %s: dlib HOG being used and found %d face box" % (datetime.now(), len(boxes)))
        else:
                dboxes = frfl(image, number_of_times_to_upsample=1, model="hog")
                for box in dboxes:
                    if (box[1] - box[3]) > app_conf.MIN_FACEBOX_DIM[0] and (box[2] - box[0]) > app_conf.MIN_FACEBOX_DIM[1]:
                        boxes.append(box)
                if fl_verbous:
                    print("[INFO] %s: dlib HOG being used and found %d face box" % (datetime.now(), len(boxes)))

    del(dboxes)

    if fl_use_mediapipe:
        face_detection = mp_face_detection.FaceDetection(min_detection_confidence=confid)
        iheigth, iwidth = image.shape[:2]
        results = face_detection.process(image)
        if results.detections:
    # further with  results.detections:
            for d in results.detections:
                xmin = int(iwidth * d.location_data.relative_bounding_box.xmin)
                ymin = int(iheigth * d.location_data.relative_bounding_box.ymin)
                ymax = ymin + int(iheigth * d.location_data.relative_bounding_box.height)
                xmax = xmin + int(iwidth * d.location_data.relative_bounding_box.width)
                if (xmax - xmin) > app_conf.MIN_FACEBOX_DIM[0] and (ymax - ymin) > app_conf.MIN_FACEBOX_DIM[1]:
                    boxes.append([ymin, xmax, ymax, xmin])
        else:
            if fl_verbous:
                    print("[INFO] %s: Mediapipe found nothing :(" % datetime.now())

        del(face_detection)
        del(results)

    if boxes:
        for box in boxes:
            basic_landmarks = frfland(image, [box], mod5_68)
            eyes.append((basic_landmarks[0]["left_eye"][2],
                         basic_landmarks[0]["right_eye"][1],
                         basic_landmarks[0]["chin"][16],
                         basic_landmarks[0]["chin"][0]))
        if fl_verbous:
            print("[INFO] %s: Mediapipe addded some too and now totally %d face boxes are collected." % (datetime.now(), len(boxes)))
        return boxes, eyes
    else:
        return None, None
</code>```

Where

```<code>frfland = face_recognition.face_landmarks()
mp_face_detection = mediaoioe.solutions.face_detection
</code>```

Minimal facebox is 30 x 30 pixel

Maximum picture size is 1280 x 1024 (using cv2.resize). Such reduction is criticaly important for the case of CUDA picture processing: my NVODIA with 4 GB onboard can't work with bigger files.

I'm using both dlib and Mediapipe face box detection: sometimes it is useful :)

Body:
From my experience (I'm using face_tecognition/ dlib for making 128d face encodings) Elasticsearch works perfectly, even much better then instant dlib methods:

                s_body = {"size": app_conf.ELASTIC_MAX_RESULTS, "min_score": 1 - tolerance,
                          "query": {
                                "script_score": {
                                    "query": {
                                        "match_all": {}
                                    },
                            "script": {
                                "source": "1 / (1 + l2norm(params.query_vector, 'face_vector'))",
                                "params": {"query_vector": wanted_encoding}
                                        }
                                    }
                                }
                        }
                res = es.search(index="idx", body=s_body)
...
 for hit in res["hits"]["hits"]:
    ...

Update from 13.02.202:
Also, as I already wrote I'm using standrd pre-trained face_recognition_model.
However, some preprocessing measures are implemented too:

check if face is rotated:

def face_align(img, box_index, eyes=None, checksideview=True):
"""[Rotate face to upright position]

  Args:
      img ([nparray]): [base input phoyo]
      box_index ([int]): [number of facebox is multiface]
      eyes ([list]): [coordinates of eyes ("left_eye" (x, y) - "right_eye" (x, y))]

  Returns:
      [boolean]: [True  if face is rotated too much: more then 5 fegrees]
      [nparray]: [corrected/rotated to upright position image]
  """
  reload(app_conf)

  if not eyes: # no eyes data being provided
      boxes, eyes = mp_boxes(img, fl_use_dlib=True, fl_use_mediapipe=False, fl_verbous=False)

  if eyes:
      left_eye_x = eyes[box_index][0][0]
      left_eye_y = eyes[box_index][0][1]
      left_eye = (left_eye_x, left_eye_y)

      right_eye_x = eyes[box_index][1][0]
      right_eye_y = eyes[box_index][1][1]
      right_eye = (right_eye_x, right_eye_y)

      if checksideview: #check if face rotated tooo much
          left_chin_x = eyes[box_index][2][0]
          left_chin_y = eyes[box_index][2][1]
          left_chin = (left_chin_x, left_chin_y)

          right_chin_x = eyes[box_index][3][0]
          right_chin_y =eyes[box_index][3][1]
          right_chin = (right_chin_x, right_chin_y)

          right_dist = euclidean_distance(right_chin, right_eye)
          left_dist = euclidean_distance(left_chin, left_eye)

          k_side_face = right_dist / left_dist
          if 0.5 > k_side_face or k_side_face > 2.0: #change acorrding to exp.
              return False, img # face is "from a side" tooo much

      if left_eye_y > right_eye_y:
          point_3rd = (right_eye_x, left_eye_y)
          direction = -1 #rotate clockwise
      else:
          point_3rd = (left_eye_x, right_eye_y)
          direction = 1 #rotate counter clockwise

      a = euclidean_distance(eyes[box_index][0], point_3rd)
      b = euclidean_distance(eyes[box_index][1], point_3rd)
      c = euclidean_distance(eyes[box_index][1], eyes[box_index][0])
      cos_a = (b*b + c*c - a*a)/(2*b*c)

      angle = np.arccos(cos_a)
      angle = (angle * 180) / pi

      if angle < app_conf.MIN_FACE_ROTATION: #if rotation less then 5 degrees
          return True, img

      if direction == -1:
          angle = 90 - angle

      new_img = Image.fromarray(img, mode="RGB")
      new_img = np.array(new_img.rotate(direction * angle))
      new_img = cv2.cvtColor(new_img, cv2.COLOR_BGR2RGB)
      return True, new_img

  else:
      return True, img #do nothing

And "eyes" I'm getting from:

def mp_boxes(image, confid=0.4, fl_use_dlib=True, fl_use_mediapipe=True, fl_verbous=True):
    """[Function uses Medipipe CNN to find out all faces boxes on the given image.
     It's about 50 times faster even on CPU but sometime consupms huge amount of RAM...
    then original dlib / Face_recognition functions.]
    If Mediapipe found no face boxes (it's possible and depends of image features,
    the function consequently tries to use dlib CNN and HOG models for that.)

    Args:
        image ([np.array]): [image in memory to be proccessed]

    Returns:
        boxes [list]: [list of coordinates of face boundaries boxes]
        eyes [list]: [list of (left_eye, right_eye) for every box]
    """
    reload(app_conf)
    mod5_68 = "large"
    boxes = []
    dboxes = []
    eyes = []
    if fl_use_dlib:
        if DLIB_USE_CUDA: #tryin to use CNN on GPU first
            try:
                dboxes = frfl(image, number_of_times_to_upsample=1, model="cnn")
                for box in dboxes: #checking MIN_FACEBOX_DIM conditions
                    if (box[1] - box[3]) > app_conf.MIN_FACEBOX_DIM[0] and (box[2] - box[0]) > app_conf.MIN_FACEBOX_DIM[1]:
                        boxes.append(box)
                if fl_verbous:
                    print("[INFO] %s: dlib CNN being used and found %dface box" % (datetime.now(), len(boxes)))
            except:
                dboxes = frfl(image, number_of_times_to_upsample=1, model="hog")
                for box in dboxes:
                    if (box[1] - box[3]) > app_conf.MIN_FACEBOX_DIM[0] and (box[2] - box[0]) > app_conf.MIN_FACEBOX_DIM[1]:
                        boxes.append(box)
                if fl_verbous:
                    print("[INFO] %s: dlib HOG being used and found %d face box" % (datetime.now(), len(boxes)))
        else:
                dboxes = frfl(image, number_of_times_to_upsample=1, model="hog")
                for box in dboxes:
                    if (box[1] - box[3]) > app_conf.MIN_FACEBOX_DIM[0] and (box[2] - box[0]) > app_conf.MIN_FACEBOX_DIM[1]:
                        boxes.append(box)
                if fl_verbous:
                    print("[INFO] %s: dlib HOG being used and found %d face box" % (datetime.now(), len(boxes)))

    del(dboxes)

    if fl_use_mediapipe:
        face_detection = mp_face_detection.FaceDetection(min_detection_confidence=confid)
        iheigth, iwidth = image.shape[:2]
        results = face_detection.process(image)
        if results.detections:
    # further with  results.detections:
            for d in results.detections:
                xmin = int(iwidth * d.location_data.relative_bounding_box.xmin)
                ymin = int(iheigth * d.location_data.relative_bounding_box.ymin)
                ymax = ymin + int(iheigth * d.location_data.relative_bounding_box.height)
                xmax = xmin + int(iwidth * d.location_data.relative_bounding_box.width)
                if (xmax - xmin) > app_conf.MIN_FACEBOX_DIM[0] and (ymax - ymin) > app_conf.MIN_FACEBOX_DIM[1]:
                    boxes.append([ymin, xmax, ymax, xmin])
        else:
            if fl_verbous:
                    print("[INFO] %s: Mediapipe found nothing :(" % datetime.now())

        del(face_detection)
        del(results)

    if boxes:
        for box in boxes:
            basic_landmarks = frfland(image, [box], mod5_68)
            eyes.append((basic_landmarks[0]["left_eye"][2],
                         basic_landmarks[0]["right_eye"][1],
                         basic_landmarks[0]["chin"][16],
                         basic_landmarks[0]["chin"][0]))
        if fl_verbous:
            print("[INFO] %s: Mediapipe addded some too and now totally %d face boxes are collected." % (datetime.now(), len(boxes)))
        return boxes, eyes
    else:
        return None, None

Where

frfland = face_recognition.face_landmarks()
mp_face_detection = mediaoioe.solutions.face_detection

Minimal facebox is 30 x 30 pixel

Maximum picture size is 1280 x 1024 (using cv2.resize). Such reduction is criticaly important for the case of CUDA picture processing: my NVODIA with 4 GB onboard can't work with bigger files.

I'm using both dlib and Mediapipe face box detection: sometimes it is useful :)


问:
preg_match unconditionally return key for unmatched optional capture group
```<code>$pattern = "/^(?<animal>DOG|CAT)?(?<color>BLUE|RED)?$/i";
$str = "DOG";

preg_match($pattern, $str, $matches);
$matches = array_filter($matches, 'is_string', ARRAY_FILTER_USE_KEY);

// $str = "dog" returns [animal] => DOG

//$str = "dogBLUE" returns [animal] => dog and [color] => BLUE
print_r($matches);
</code>```

I have an example http://sandbox.onlinephpfunctions.com/code/2428bc68adcf9929557d86dc1ae72552c3681b58 too

Both named capture groups are optional and so keys will only be returned if a match is found.

My Question

How can I unconditionally return keys for any of my possible named capture groups? Empty String '' would be great if it group ain't found.

Input of "DOG" resulting in [animal] => 'DOG', [color] => '' is what I'm looking for.

I was hoping for a flag on preg_match to do this, but couldn't find anything.

Update: I just want to avoid doing isset($matches[OPTIONAL_GROUP])

Thanks!

答:
Allow duplicate named captures (with J or with (?J) for PHP < 7.3) and initialize groups you need at the start of the pattern:

```<code>~ (?<animal>) (?<color>)

  ^ (?<animal>DOG|CAT)? (?<color>RED|BLUE)? $
~xiJ
</code>```

demo

Advantage: you can choose the order of groups in the result array.

Or without to change your pattern:

```<code>$arr = ['animal' => '', 'color' => ''];

$result = array_intersect_key($matches, $arr) + $arr;
</code>```

demo

Notice: array_intersect_key($matches + $arr, $arr) produces exactly the same result.

Body:
$pattern = "/^(?<animal>DOG|CAT)?(?<color>BLUE|RED)?$/i";
$str = "DOG";

preg_match($pattern, $str, $matches);
$matches = array_filter($matches, 'is_string', ARRAY_FILTER_USE_KEY);

// $str = "dog" returns [animal] => DOG

//$str = "dogBLUE" returns [animal] => dog and [color] => BLUE
print_r($matches);

I have an example http://sandbox.onlinephpfunctions.com/code/2428bc68adcf9929557d86dc1ae72552c3681b58 too

Both named capture groups are optional and so keys will only be returned if a match is found.

My Question

How can I unconditionally return keys for any of my possible named capture groups? Empty String '' would be great if it group ain't found.

Input of "DOG" resulting in [animal] => 'DOG', [color] => '' is what I'm looking for.

I was hoping for a flag on preg_match to do this, but couldn't find anything.

Update: I just want to avoid doing isset($matches[OPTIONAL_GROUP])

Thanks!

Body:
Allow duplicate named captures (with J or with (?J) for PHP < 7.3) and initialize groups you need at the start of the pattern:

~ (?<animal>) (?<color>)

  ^ (?<animal>DOG|CAT)? (?<color>RED|BLUE)? $
~xiJ

demo

Advantage: you can choose the order of groups in the result array.

Or without to change your pattern:

$arr = ['animal' => '', 'color' => ''];

$result = array_intersect_key($matches, $arr) + $arr;

demo

Notice: array_intersect_key($matches + $arr, $arr) produces exactly the same result.


问:
Function wont eager compile
I recently tried to eager compile my javascript function from the guide shown in the link-https://v8.dev/blog/code-caching-for-devs

But even after enclosing the function in IIFE heuristics as said above, the V8 System analyzer shows its lazy compiled, and unoptimized. It is not showing under the eager compile section. The creation time however shifted to the front, but there is no noticeable performance change. Does this mean that eager compile functions don't perform faster? Then what exactly is the advantage of a eager compile function?

(pls note, the below function is created for learning purposes. I use this to learn about eager compiling js functions):
script.js:

```<code>const a = 1000;
const b = 2000;

const add = function(a,b) {
    var sampleObject2={
        firstName: "John",
        lastName: "Doe",
        age: 70,
        eyeColor: "blue"
      };
    var c=a**b;
    var d=b**c;
    c=c+21;
    d=d+c;
    d=d**sampleObject2.age;
    console.log(c**d);
    return c**d;
};
window.addEventListener("click", ()=>{
  var start=performance.now()
  add(a,b);
  var end=performance.now()
  console.log(end-start);
},false);
</code>```

答:
(V8 developer here.)

the V8 System analyzer shows its lazy compiled

That's not what I'm seeing: with the code as written in your question, i.e. const add = function(a, b) { ... };, the System Analyzer says type: LazyCompile~.

When I add the parentheses that (currently!) get interpreted as an eager-compilation hint, i.e. const add = (function(a, b) = { ... });, that changes to type: Function~ (as in your screenshot; so I guess you did actually add the parentheses before running the test that produced that screenshot).

and unoptimized

That is expected. Optimization has nothing to do with lazy compilation. There is no way to optimize a function right away, because attempting that would make no sense: the optimizer can't do anything useful if it doesn't have type feedback to work with, which can only be collected during unoptimized execution.

Does this mean that eager compile functions don't perform faster?

Correct, the function's own performance doesn't change at all.

Then what exactly is the advantage of a eager compile function?

Preparing a function for later lazy compilation has a small cost. In case the function will be needed soon, compiling it right away avoids this cost. In fact, I do see a tiny difference in the times that your test reports; although I haven't verified that this difference is actually due to eager vs. lazy compilation, it may well be caused by something else.

If eager-compiling everything was a good idea, then engines would do it -- that'd be much easier. In reality, lazy-compiling is almost always worth the tiny amount of extra work it creates, because it makes startup/pageload faster, and can avoid quite a bit of memory consumption when there are unused functions, e.g. in your included libraries. So JavaScript engines accept a lot of internal complexity to make lazy compilation possible. I wouldn't recommend attempting to side-step that.

(For completeness: a couple of years ago V8's implementation of lazy compilation was not as optimized as it is today, and manually hinting eager compilation could achieve more significant improvements, especially for deeply nested functions. But these times are gone! And now it's just one more example that illustrates that in general you shouldn't go to great lengths to adapt your code to what engines are currently doing well or not-so-well.)

Title: Function wont eager compile

Body:
I recently tried to eager compile my javascript function from the guide shown in the link-https://v8.dev/blog/code-caching-for-devs

But even after enclosing the function in IIFE heuristics as said above, the V8 System analyzer shows its lazy compiled, and unoptimized. It is not showing under the eager compile section. The creation time however shifted to the front, but there is no noticeable performance change. Does this mean that eager compile functions don't perform faster? Then what exactly is the advantage of a eager compile function?

(pls note, the below function is created for learning purposes. I use this to learn about eager compiling js functions):
script.js:

const a = 1000;
const b = 2000;

const add = function(a,b) {
    var sampleObject2={
        firstName: "John",
        lastName: "Doe",
        age: 70,
        eyeColor: "blue"
      };
    var c=a**b;
    var d=b**c;
    c=c+21;
    d=d+c;
    d=d**sampleObject2.age;
    console.log(c**d);
    return c**d;
};
window.addEventListener("click", ()=>{
  var start=performance.now()
  add(a,b);
  var end=performance.now()
  console.log(end-start);
},false);


问:
Vue: adding style to app overrides styles of child components
I have this simple page, designed with Vue and Tailwind:

App.vue:

```<code><template>
<!--  <div class="bg-blue-500 flex flex-col h-screen min-h-screen " id="app">-->
    <div id = "app" class = "bg-blue-500 flex flex-col h-screen min-h-screen   app">
        <Header/>

      <main id ="app-main">
        <p>app</p>
        <router-link to="/home">Home</router-link>
        <router-link to="/about">About</router-link>

      </main>

      <Footer/>
  </div>
</template>

<script>
import Header from './components/Header.vue'
import Footer from './components/Footer.vue'

export default {
  name: 'App',
  components: {
    Header, Footer
  }
}
</script>

<style>
#app {

}

#app-main  {
  display: flex;
  flex: 1 1 0%;

}
</style>
</code>```

However if I edit #app style:

```<code>#app {
 background-color: purple;
}
</code>```

So as can be seen, when Tailwind is used, the styling behaves as expected: everything outside header and footer is blue, header and footer - their respective colors. However an attempt to set a color in the

答:
If I understood you correctly, you are trying to change the color of the blue app region.

Notice the class bg-blue-500 on #app element? That's the class controlling the element's background color.

I can't check it but I bet renaming it to bh-purple-500 will color the background purple. You can find more default colors in the docs.

They way you're doing it overrides the styles of the child elements.

Body:
I have this simple page, designed with Vue and Tailwind:

App.vue:

<template>

app

        <router-link to="/home">Home</router-link>
        <router-link to="/about">About</router-link>

</template>

However if I edit #app style:

#app {
 background-color: purple;
}

So as can be seen, when Tailwind is used, the styling behaves as expected: everything outside header and footer is blue, header and footer - their respective colors. However an attempt to set a color in the


问:
Escaping strings in bash
I have this simple bash script:

```<code>#!/bin/bash
isRunning=$(ps -Af | awk '{if ($11 == "HomeDir") { print $0 }}' | wc -l)
if [ $isRunning == "0" ]; then
    echo "Script is running"
fi
</code>```

The script returns:

```<code>./script.sh: line 3: [ 0: command not found
</code>```

What did i do wrong?

答:
to simplify:</p>

```<code>if [ -n "$(ps -Af | awk '$11 == "HomeDir"')" ]; then echo "running"; fi
</code>```

Title: Escaping strings in bash

Body:
I have this simple bash script:

#!/bin/bash
isRunning=$(ps -Af | awk '{if ($11 == "HomeDir") { print $0 }}' | wc -l)
if [ $isRunning == "0" ]; then
    echo "Script is running"
fi

The script returns:

./script.sh: line 3: [ 0: command not found

What did i do wrong?

Body:
to simplify:

if [ -n "$(ps -Af | awk '$11 == "HomeDir"')" ]; then echo "running"; fi


问:
How can I add hidden/invisible text fields or values into a document?
How can I add hidden text fields or values into a document?

I have to receive documents and update a database with certain values. I want to add hidden fields into a document so that I could access the values when I get the envelope. How can I do that? The hidden fields I found in the documentation were focused on hiding sensitive data from a receiver. I want the field to be completely hidden.

答:
Meta-data can be added at the envelope, document, and/or recipient (signer) levels.

Envelope-level meta-data can be optionally shown on the certificate of completion.

Here's a live API example that demonstrates use of the different types of meta-data while creating an envelope


问:
In a Github Action how to reference a private package for a test?
I have written a test file for a Github Action:

test.yml

```<code>name: Test
on:
  push:
    branches: [main]
  pull_request:
    branches: [main]
jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [14.x, 16.x]
    steps:
      - uses: actions/checkout@v2
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v2
        with:
          node-version: ${{ matrix.node-version }}
      - run: npm ci
      - run: npm run build --if-present
      - run: npm test
</code>```

but my package.json uses an organization private repo. When the Github Action runs it fails and I get the error:

```<code>npm ERR! code E404
npm ERR! 404 Not Found - GET https://registry.npmjs.org/@org/repo/
npm ERR! 404
npm ERR! 404  '@org/repo@1.2.3' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/runner/.npm/_logs/2022-02-10T17_17_46_961Z-debug.log
Error: Process completed with exit code 1.
</code>```

My research trying to solve the issue:

Package is not publishing to npm (not in the npm registry)

Getting 404 when attempting to publish new package to NPM

Installing npm package fails with 404

404 error while publishing npm package - npm ERR! 404 Not Found - PUT https://registry.npmjs.org/

In my Github Action how can I properly reference the private org repo so that my test will work correctly or is there a step I'm missing?

答:
You have to add authorization to on CI with URL + token - it's done through .npmrc file.

Create a token: npm token create --read-only

Add it to your secrets on GitHub named NPM_TOKEN

Before running npm ci create a local file with your token:

Add a step: - run: echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" >> .npmrc

Everything else stays as it is.

Body:
I have written a test file for a Github Action:

test.yml

name: Test
on:
  push:
    branches: [main]
  pull_request:
    branches: [main]
jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [14.x, 16.x]
    steps:
      - uses: actions/checkout@v2
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v2
        with:
          node-version: ${{ matrix.node-version }}
      - run: npm ci
      - run: npm run build --if-present
      - run: npm test

but my package.json uses an organization private repo. When the Github Action runs it fails and I get the error:

npm ERR! code E404
npm ERR! 404 Not Found - GET https://registry.npmjs.org/@org/repo/
npm ERR! 404
npm ERR! 404  '@org/repo@1.2.3' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/runner/.npm/_logs/2022-02-10T17_17_46_961Z-debug.log
Error: Process completed with exit code 1.

My research trying to solve the issue:

Package is not publishing to npm (not in the npm registry)

Getting 404 when attempting to publish new package to NPM

Installing npm package fails with 404

404 error while publishing npm package - npm ERR! 404 Not Found - PUT https://registry.npmjs.org/

In my Github Action how can I properly reference the private org repo so that my test will work correctly or is there a step I'm missing?


问:
Remove fields from object inside a pipeline stage i.e. not using $unset/$project in a separate stage
It is clear to add/overwrite 1 or more fields to an object in a pipeline, we can use $mergeObjects:

{$project: {X: {$mergeObjects: [ '$$CURRENT', {foo:'bar', fin:7} ]}} }
```
<p>Is there a compact way to <em>remove</em> fields?  The best I have is:</p>
<pre class="lang-js prettyprint-override"><code>{$project: {X: {$arrayToObject: {$filter: {
                                  input: {$objectToArray: "$$CURRENT"},
                                  cond: {$not:{$in:['$$this.k', ['foo','fin']]}}
                }}}
}}
</code>```

We'd really like the following but this does not work:

{$project: {X: {$mergeObjects: [ '$$CURRENT', {foo:'$$REMOVE', fin:'$$REMOVE'} ]}} }
```
<p>This is a generic pipeline operator challenge.  It is not specific to a shape of input nor is there a target output.  For the want of a real example, consider these input docs:</p>
<pre class="lang-js prettyprint-override"><code>    {"brand": "B1", "color": "red", o1:"A1", o2:["B","C"]},
    {"brand": "B1", "color": "red", o1:"A2", o2:["D","E"]},
    {"brand": "B1", "color": "blue", o1:"A2", o2:["D","E"]},
...
</code>```

I wish to group by brand and color and then perform operations on the material (e.g. cnt below) as well as carry along "the rest of the doc" for other operations including $project.  This is straightforward with $group and $$CURRENT but it copies brand and color into the $push array:

db.foo.aggregate([
    {$group: {_id: {c:'$color',b:'$brand'}, cnt: {$sum:1}, XX: {$push: "$$CURRENT"}}}
]);

{
    "_id" : {
        "c" : "red",
        "b" : "B1"
    },
    "cnt" : 2,
    "XX" : [
        {
            "_id" : 0,
            "brand" : "B1",  // duped because of generic pickup by $$CURRENT
            "color" : "red", // duped because of generic pickup by $$CURRENT
            "o1" : "A1",
            "o2" : [
                "B",
                "C"
            ]
        },
        {
            "_id" : 1,
            "brand" : "B1", // duped because of generic pickup by $$CURRENT
            "color" : "red", // duped because of generic pickup by $$CURRENT
            "o1" : "A2",
            "o2" : [
                "D",
                "E"
            ]
        }
    ]
}
```

The fields going into the $group _id can be removed from the object going into $push with the objectToArray-filter-arrayToObject pattern.  The question is:  is there a more compact way to remove 1 or more fields from an object?

答:
We have 3 stage operators to remove fields

$project and field : 0

$set and and  field : $$REMOVE

$unset field

They take as arguments

a root field

or a field path

Limitations

They don't accept variable fields for example i cant
{$project : {"$$aField" : 0 }} meaning i can't use them with dynamic keys

the path sometimes is very general, for example i might want to remove the field in the first 2 members of an array and not from all array members

For the last 2 cases we can

unwind to make the nested document more flat to be able to use stage operators  (still dont fix the dynamic keys problem)

use the object to array, filter, and back to object which is slow and complicated

*MongoDB doesnt support for now removeKey($$document,$$key) also we dont have a way to access nested fields with creteria in an easy way, without nested map functions etc. Until they add those, i think we don't have alternative.

Query

this is an example of using a path to remove fields from nested documents

in your group, you could also unset before grouping if you didnt want some fields to added in the $push $$CURRENT

Test code here

aggregate([{"$unset":["XX.o1", "XX.o2"]}])
```

Body:
It is clear to add/overwrite 1 or more fields to an object in a pipeline, we can use $mergeObjects:

{$project: {X: {$mergeObjects: [ '$$CURRENT', {foo:'bar', fin:7} ]}} }

Is there a compact way to remove fields?  The best I have is:

{$project: {X: {$arrayToObject: {$filter: {
                                  input: {$objectToArray: "$$CURRENT"},
                                  cond: {$not:{$in:['$$this.k', ['foo','fin']]}}
                }}}
}}

We'd really like the following but this does not work:

{$project: {X: {$mergeObjects: [ '$$CURRENT', {foo:'$$REMOVE', fin:'$$REMOVE'} ]}} }

This is a generic pipeline operator challenge.  It is not specific to a shape of input nor is there a target output.  For the want of a real example, consider these input docs:

    {"brand": "B1", "color": "red", o1:"A1", o2:["B","C"]},
    {"brand": "B1", "color": "red", o1:"A2", o2:["D","E"]},
    {"brand": "B1", "color": "blue", o1:"A2", o2:["D","E"]},
...

I wish to group by brand and color and then perform operations on the material (e.g. cnt below) as well as carry along "the rest of the doc" for other operations including $project.  This is straightforward with $group and $$CURRENT but it copies brand and color into the $push array:

db.foo.aggregate([
    {$group: {_id: {c:'$color',b:'$brand'}, cnt: {$sum:1}, XX: {$push: "$$CURRENT"}}}
]);

{
    "_id" : {
        "c" : "red",
        "b" : "B1"
    },
    "cnt" : 2,
    "XX" : [
        {
            "_id" : 0,
            "brand" : "B1",  // duped because of generic pickup by $$CURRENT
            "color" : "red", // duped because of generic pickup by $$CURRENT
            "o1" : "A1",
            "o2" : [
                "B",
                "C"
            ]
        },
        {
            "_id" : 1,
            "brand" : "B1", // duped because of generic pickup by $$CURRENT
            "color" : "red", // duped because of generic pickup by $$CURRENT
            "o1" : "A2",
            "o2" : [
                "D",
                "E"
            ]
        }
    ]
}

The fields going into the $group _id can be removed from the object going into $push with the objectToArray-filter-arrayToObject pattern.  The question is:  is there a more compact way to remove 1 or more fields from an object?


答:
Looking at your code my recommendation is to use some other object to add the code to. Text of a label is not an appropriate choice.

Try using a foreach loop into a list to retrieve all the data that has been returned.

Title: select * only return one value


问:
What is the correct Way to integrate KSOAP library in Android Studio?
I am trying something like this,

In Gradle,

Inside Build Types,

```<code>    repositories {
        maven { url 'http://ksoap2-android.googleco/svde.cmomn/2-repo'
  }
</code>```

and in dependencies . . .

```<code>    compile 'com.google.code.ksoap2-android:ksoap2-android:3.6.0'
</code>```

Error: Failed to resolve dependencies

答:
this is mine in new versions of Android Studio:

in build.gradle (module):

```<code>dependencies {
    ...
    implementation 'com.google.code.ksoap2-android:ksoap2-android:3.6.4'
}
</code>```

in settings.gradle:

```<code>pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        maven { url 'https://oss.sonatype.org/content/repositories/ksoap2-android-releases/' }
    }
}
rootProject.name = "idt.pda"
include ':app'
</code>```

Body:
I am trying something like this,

In Gradle,

Inside Build Types,

    repositories {
        maven { url 'http://ksoap2-android.googleco/svde.cmomn/2-repo'
  }

and in dependencies . . .

    compile 'com.google.code.ksoap2-android:ksoap2-android:3.6.0'

Error: Failed to resolve dependencies

Body:
this is mine in new versions of Android Studio:

in build.gradle (module):

dependencies {
    ...
    implementation 'com.google.code.ksoap2-android:ksoap2-android:3.6.4'
}

in settings.gradle:

pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        maven { url 'https://oss.sonatype.org/content/repositories/ksoap2-android-releases/' }
    }
}
rootProject.name = "idt.pda"
include ':app'


问:
How to access auth_user_group table by ID
How can I directly specify the ID column of auth_user_group table in queries/DML?

I am referring to auth_user_group.id, not auth_group.id nor auth_user_group.group_id.

For example, I want to directly issue DML like:

```<code>delete from auth_user_group where auth_user_group.id = 1

select id, user_id, group_id from auth_user_group where auth_user_group.id = 1;
</code>```

I am not interested in the following workaround which avoids auth_user_group by requiring knowledge of the group name, instead of the auth_user_group.id, and results in two queries instead of 1:

```<code>group = Groups.get(name='foo')
user.groups.remove(group)
</code>```

If you wanted to directly query a junction/M:N table in Django, normally you would create a "through Model" and can then query it directly. However in this case, the django.contrib.auth.models definition do not use a through model. I'm willing to create a custom AbstractUser where groups is defined as a through table if that would work. But I'm not sure how to specify the through table since auth_user_group does not have its own model in django.contrib.auth.models.

答:
Since there is no explicit through model, you can access the implicit through model

Group.user_set.through.objects.get(id=1)

or

User.groups.through.objects.get(id=1)

https://docs.djangoproject.com/en/4.0/ref/models/fields/#django.db.models.ManyToManyField.through

Body:
How can I directly specify the ID column of auth_user_group table in queries/DML?

I am referring to auth_user_group.id, not auth_group.id nor auth_user_group.group_id.

For example, I want to directly issue DML like:

delete from auth_user_group where auth_user_group.id = 1

select id, user_id, group_id from auth_user_group where auth_user_group.id = 1;

I am not interested in the following workaround which avoids auth_user_group by requiring knowledge of the group name, instead of the auth_user_group.id, and results in two queries instead of 1:

group = Groups.get(name='foo')
user.groups.remove(group)

If you wanted to directly query a junction/M:N table in Django, normally you would create a "through Model" and can then query it directly. However in this case, the django.contrib.auth.models definition do not use a through model. I'm willing to create a custom AbstractUser where groups is defined as a through table if that would work. But I'm not sure how to specify the through table since auth_user_group does not have its own model in django.contrib.auth.models.


问:
Introducing a second y axis into a relplot() call with multiple plots

The Problem

I have 2 dataframes which I combine and then melt with pandas.  I need to multi-plot them (as below) and the code needs to be scalable.  They consist of 2 variables which form the 'key' column below ('x' and 'y' here), across multiple 'stations' (just 2 here, but needs to be scalable).  I've used relplot() to be able to multi-plot the two variables on each graph, and different stations on separate graphs.

Is there any way to maintain this format but introduce a 2nd y axis to each plot?  'x' and 'y' need to be on different scales in my actual data.  I've seen examples where the relplot call is stored with y = 1st variable, and a 2nd lineplot call is added for the 2nd variable with ax.twinx() included in it.  So in example below, 'x' and 'y' would each have a y axis on the same graph.

How would I make that work with a melted dataframe (e.g. below) where 'key' = 2 variables and 'station' can be length n?  Or is the answer to scrap that df format and start again?

Example Code

The multi-plot as it stands:

```<code>import numpy as np
np.random.seed(123)
date_range = pd.period_range('1981-01-01','1981-01-04',freq='D')
x = np.random.randint(1, 10, (4,2))
y = np.random.randint(1, 10, (4,2))
x = pd.DataFrame(x, index = date_range, columns = ['station1','station2'])
y = pd.DataFrame(y, index = date_range + pd.to_timedelta(1, unit="D"), columns = ['station1','station2'])

#keep information where each data point comes from
x["key"], y["key"] = "x", "y"
#moving index into a column
x = x.reset_index()
y = y.reset_index()
#and changing it to datetime values that seaborn can understand
#necessary because pd.Period data is used
x["index"] = pd.to_datetime(x["index"].astype(str))
y["index"] = pd.to_datetime(y["index"].astype(str))

#combining dataframes and reshaping
df = pd.concat([x, y]).melt(["index", "key"], var_name="station", value_name="station_value")

#plotting
fg = sns.relplot(data=df, x = "index", y = "station_value", kind = "line", hue = "key", row = "station")

#shouldn't be necessary but this example had too many ticks for the interval
from matplotlib.dates import DateFormatter, DayLocator
fg.axes[0,0].xaxis.set_major_locator(DayLocator(interval=1))
fg.axes[0,0].xaxis.set_major_formatter(DateFormatter("%y-%m-%d"))

plt.show()
</code>```

答:
You could relplot for only one key (without hue), then similar to the linked thread, loop the subplots, create a twinx, and lineplot the second key/station combo:

```<code>#plotting
fg = sns.relplot(data=df[df['key']=='x'], x="index", y="station_value", kind="line", row="station")

for station, ax in fg.axes_dict.items():
    ax1 = ax.twinx()
    sns.lineplot(data=df[(df['key'] == 'y') & (df['station'] == station)], x='index', y='station_value', color='orange', ci=None, ax=ax1)
    ax1.set_ylabel('')
</code>```

Output:

Body:
The Problem

I have 2 dataframes which I combine and then melt with pandas.  I need to multi-plot them (as below) and the code needs to be scalable.  They consist of 2 variables which form the 'key' column below ('x' and 'y' here), across multiple 'stations' (just 2 here, but needs to be scalable).  I've used relplot() to be able to multi-plot the two variables on each graph, and different stations on separate graphs.

Is there any way to maintain this format but introduce a 2nd y axis to each plot?  'x' and 'y' need to be on different scales in my actual data.  I've seen examples where the relplot call is stored with y = 1st variable, and a 2nd lineplot call is added for the 2nd variable with ax.twinx() included in it.  So in example below, 'x' and 'y' would each have a y axis on the same graph.

How would I make that work with a melted dataframe (e.g. below) where 'key' = 2 variables and 'station' can be length n?  Or is the answer to scrap that df format and start again?

Example Code

The multi-plot as it stands:

import numpy as np
np.random.seed(123)
date_range = pd.period_range('1981-01-01','1981-01-04',freq='D')
x = np.random.randint(1, 10, (4,2))
y = np.random.randint(1, 10, (4,2))
x = pd.DataFrame(x, index = date_range, columns = ['station1','station2'])
y = pd.DataFrame(y, index = date_range + pd.to_timedelta(1, unit="D"), columns = ['station1','station2'])

#keep information where each data point comes from
x["key"], y["key"] = "x", "y"
#moving index into a column
x = x.reset_index()
y = y.reset_index()
#and changing it to datetime values that seaborn can understand
#necessary because pd.Period data is used
x["index"] = pd.to_datetime(x["index"].astype(str))
y["index"] = pd.to_datetime(y["index"].astype(str))

#combining dataframes and reshaping
df = pd.concat([x, y]).melt(["index", "key"], var_name="station", value_name="station_value")

#plotting
fg = sns.relplot(data=df, x = "index", y = "station_value", kind = "line", hue = "key", row = "station")

#shouldn't be necessary but this example had too many ticks for the interval
from matplotlib.dates import DateFormatter, DayLocator
fg.axes[0,0].xaxis.set_major_locator(DayLocator(interval=1))
fg.axes[0,0].xaxis.set_major_formatter(DateFormatter("%y-%m-%d"))

plt.show()

Body:
You could relplot for only one key (without hue), then similar to the linked thread, loop the subplots, create a twinx, and lineplot the second key/station combo:

#plotting
fg = sns.relplot(data=df[df['key']=='x'], x="index", y="station_value", kind="line", row="station")

for station, ax in fg.axes_dict.items():
    ax1 = ax.twinx()
    sns.lineplot(data=df[(df['key'] == 'y') & (df['station'] == station)], x='index', y='station_value', color='orange', ci=None, ax=ax1)
    ax1.set_ylabel('')

Output:


问:
disable dark mode in django admin

The Problem

I installed Django after couple of months. Current version is 3.2.4.

Earlier Django-admin was just light-mode. Current Django-admin switches automatically to dark or light according to system theme. Well, I do not want this behaviour. I want Django-admin to be light theme irrespective of system theme.
I am Not using 3rd party theme for Django-admin.

What I Tried?

As per one of the comment in this answer,

I decided to comment "@media (prefers-color-scheme: dark)" section in /static/admin/css/base.css . Not sure that it's good solution but the fastest one.

I tried commenting the code as shown here but does not work. I tried clearing cache and even tried switching browser but none of them seems to work. In browsers, I always prefer light theme. How can I achieve this? Any help is appreciable. Thanks in advance :)

System Details:

OS: Fedora 5.11.12-300.fc34.x86_64

Python-Verion: 3.9.5

答:
The accepted answer only fixes it for the current user. A solution that would fix it for all users of the site would be similar, but would require copying base.css to your static/css directory and editing it there.

Alternatively (and this is what I did), just pip install django-light and add it to your INSTALLED_APPS just before django.contrib.admin

Title: disable dark mode in django admin


问:
PyQt5 Modules not Found?
This is a complete fresh install of MacOS!

Platform:

```<code>Apple MacBook Air - M1
MacOS Monterey
</code>```

Package Info:

```<code>Homebrew: 3.3.14
pip: 22.0.3
Python: 3.9.10 (installed via 'brew install python')
Latest-Xcode
</code>```

My $PATH environment:

```<code>/opt/homebrew/bin
/opt/homebrew/sbin
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
</code>```

I tried adding a combination of these to my $PATH, to no avail:

```<code>/opt/homebrew/lib/python3.9/site-packages
/opt/homebrew/Cellar/pyqt@5/5.15.6/lib/python3.9/site-packages/PyQt5
/opt/homebrew/lib/python3.9/site-packages/PyQt5
/opt/homebrew/opt/pyqt@5/5.15.6/bin
/opt/homebrew/opt/qt@5/5.15.6/bin
</code>```

What I want to do:

```<code>pip install pyqt5
</code>```

What happens when I try:

```<code>  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... error
  error: subprocess-exited-with-error

  × Preparing metadata (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> [29 lines of output]
      Traceback (most recent call last):
        File "/opt/homebrew/lib/python3.9/site-packages/pip/_vendor/pep517/in_process/_in_process.py", line 156, in prepare_metadata_for_build_wheel
          hook = backend.prepare_metadata_for_build_wheel
      AttributeError: module 'sipbuild.api' has no attribute 'prepare_metadata_for_build_wheel'

      During handling of the above exception, another exception occurred:

      Traceback (most recent call last):
        File "/opt/homebrew/lib/python3.9/site-packages/pip/_vendor/pep517/in_process/_in_process.py", line 363, in <module>
          main()
        File "/opt/homebrew/lib/python3.9/site-packages/pip/_vendor/pep517/in_process/_in_process.py", line 345, in main
          json_out['return_val'] = hook(**hook_input['kwargs'])
        File "/opt/homebrew/lib/python3.9/site-packages/pip/_vendor/pep517/in_process/_in_process.py", line 160, in prepare_metadata_for_build_wheel
          whl_basename = backend.build_wheel(metadata_directory, config_settings)
        File "/opt/homebrew/Cellar/python@3.9/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/sipbuild/api.py", line 51, in build_wheel
          project = AbstractProject.bootstrap('pep517')
        File "/opt/homebrew/Cellar/python@3.9/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/sipbuild/abstract_project.py", line 83, in bootstrap
          project.setup(pyproject, tool, tool_description)
        File "/opt/homebrew/Cellar/python@3.9/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/sipbuild/project.py", line 594, in setup
          self.apply_user_defaults(tool)
        File "project.py", line 63, in apply_user_defaults
          super().apply_user_defaults(tool)
        File "/opt/homebrew/Cellar/python@3.9/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pyqtbuild/project.py", line 70, in apply_user_defaults
          super().apply_user_defaults(tool)
        File "/opt/homebrew/Cellar/python@3.9/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/sipbuild/project.py", line 241, in apply_user_defaults
          self.builder.apply_user_defaults(tool)
        File "/opt/homebrew/Cellar/python@3.9/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pyqtbuild/builder.py", line 67, in apply_user_defaults
          raise PyProjectOptionException('qmake',
      sipbuild.pyproject.PyProjectOptionException
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.
</code>```

If I install it this way, I get no errors:

```<code>brew install pyqt@5
</code>```

But my VS Code shows up like this, and none of submodules work:

My program used to work just fine, but I had recently installed Python 3.10 and have since uninstalled it. Ever since the new Python install, I've had this issue. Not sure how to tackle this. Thank you!

答:
I've asked this question a number of different ways in a number of different places; none of the suggested answers work for me.

The closest thing to an answer that I got to was this:
https://www.zhihu.com/question/437075754/answer/1993415429

If you're unable to read it, it suggests this:

Pre-work, install homebrew, python 3.9.

First enter in the terminal:

```<code>brew install pyqt5
echo 'export PATH="/opt/homebrew/opt/qt@5/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/opt/homebrew/opt/pyqt@5/5.15.4_1/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc
</code>```

Then, copy all the contents of
/opt/homebrew/Cellar/pyqt@5/5.15.4_1/lib/python3.9/site-packages to
lib/python3.9/site-packages folder. If your homebrew installation
address and python installation address are not the default, you need
to modify these two addresses.

Finally, try it in python

```<code>import PyQt5
</code>```

But this didn't work for me, either.

In the end, I ended up just installing PyQt6:

```<code>pip install PyQt6
</code>```

The install completed without any errors. Now I am working on updating my code. Hopefully this will be useful for someone else coming here from an angry Google search. Code on, my friends.

Title: PyQt5 Modules not Found?

Body:
This is a complete fresh install of MacOS!

Platform:

Apple MacBook Air - M1
MacOS Monterey

Package Info:

Homebrew: 3.3.14
pip: 22.0.3
Python: 3.9.10 (installed via 'brew install python')
Latest-Xcode

My $PATH environment:

/opt/homebrew/bin
/opt/homebrew/sbin
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin

I tried adding a combination of these to my $PATH, to no avail:

/opt/homebrew/lib/python3.9/site-packages
/opt/homebrew/Cellar/pyqt@5/5.15.6/lib/python3.9/site-packages/PyQt5
/opt/homebrew/lib/python3.9/site-packages/PyQt5
/opt/homebrew/opt/pyqt@5/5.15.6/bin
/opt/homebrew/opt/qt@5/5.15.6/bin

What I want to do:

pip install pyqt5

What happens when I try:

  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... error
  error: subprocess-exited-with-error

  × Preparing metadata (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> [29 lines of output]
      Traceback (most recent call last):
        File "/opt/homebrew/lib/python3.9/site-packages/pip/_vendor/pep517/in_process/_in_process.py", line 156, in prepare_metadata_for_build_wheel
          hook = backend.prepare_metadata_for_build_wheel
      AttributeError: module 'sipbuild.api' has no attribute 'prepare_metadata_for_build_wheel'

      During handling of the above exception, another exception occurred:

      Traceback (most recent call last):
        File "/opt/homebrew/lib/python3.9/site-packages/pip/_vendor/pep517/in_process/_in_process.py", line 363, in <module>
          main()
        File "/opt/homebrew/lib/python3.9/site-packages/pip/_vendor/pep517/in_process/_in_process.py", line 345, in main
          json_out['return_val'] = hook(**hook_input['kwargs'])
        File "/opt/homebrew/lib/python3.9/site-packages/pip/_vendor/pep517/in_process/_in_process.py", line 160, in prepare_metadata_for_build_wheel
          whl_basename = backend.build_wheel(metadata_directory, config_settings)
        File "/opt/homebrew/Cellar/python@3.9/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/sipbuild/api.py", line 51, in build_wheel
          project = AbstractProject.bootstrap('pep517')
        File "/opt/homebrew/Cellar/python@3.9/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/sipbuild/abstract_project.py", line 83, in bootstrap
          project.setup(pyproject, tool, tool_description)
        File "/opt/homebrew/Cellar/python@3.9/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/sipbuild/project.py", line 594, in setup
          self.apply_user_defaults(tool)
        File "project.py", line 63, in apply_user_defaults
          super().apply_user_defaults(tool)
        File "/opt/homebrew/Cellar/python@3.9/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pyqtbuild/project.py", line 70, in apply_user_defaults
          super().apply_user_defaults(tool)
        File "/opt/homebrew/Cellar/python@3.9/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/sipbuild/project.py", line 241, in apply_user_defaults
          self.builder.apply_user_defaults(tool)
        File "/opt/homebrew/Cellar/python@3.9/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pyqtbuild/builder.py", line 67, in apply_user_defaults
          raise PyProjectOptionException('qmake',
      sipbuild.pyproject.PyProjectOptionException
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.

If I install it this way, I get no errors:

brew install pyqt@5

But my VS Code shows up like this, and none of submodules work:

My program used to work just fine, but I had recently installed Python 3.10 and have since uninstalled it. Ever since the new Python install, I've had this issue. Not sure how to tackle this. Thank you!

Body:
I've asked this question a number of different ways in a number of different places; none of the suggested answers work for me.

The closest thing to an answer that I got to was this:
https://www.zhihu.com/question/437075754/answer/1993415429

If you're unable to read it, it suggests this:

Pre-work, install homebrew, python 3.9.

First enter in the terminal:

brew install pyqt5
echo 'export PATH="/opt/homebrew/opt/qt@5/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/opt/homebrew/opt/pyqt@5/5.15.4_1/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc

Then, copy all the contents of
/opt/homebrew/Cellar/pyqt@5/5.15.4_1/lib/python3.9/site-packages to
lib/python3.9/site-packages folder. If your homebrew installation
address and python installation address are not the default, you need
to modify these two addresses.

Finally, try it in python

import PyQt5

But this didn't work for me, either.

In the end, I ended up just installing PyQt6:

pip install PyQt6

The install completed without any errors. Now I am working on updating my code. Hopefully this will be useful for someone else coming here from an angry Google search. Code on, my friends.


问:
ContainsKey Thread Safe
In the following code:

```<code>public class StringCache
{
    private readonly object lockobj = new object();

    private readonly Dictionary<int, string> cache = new Dictionary<int, string>();

    public string GetMemberInfo(int key)
    {
        if (cache.ContainsKey(key))
            return cache[key];
        lock (lockobj)
        {
            if (!cache.ContainsKey(key))
                cache[key] = GetString(key);
        }
        return cache[key];
    }

    private static string GetString(int key)
    {
        return "Not Important";
    }
}
</code>```

1) Is ContainsKey thread safe?  IOW, what happens if that method is executing when another thread is adding something to the dictionary?
2) For the first return cache[key], is there any chance that it could return a garbled value?

TIA,

MB

答:
The inherent thread safety of ContainsKey doesn't matter, since there is no synchronization between ContainsKey & cache[key].

For example:

```<code>if (cache.ContainsKey(key))
   // Switch to another thread, which deletes the key.
   return cache[key];
</code>```

MSDN is pretty clear on this point:

To allow the collection to be accessed
  by multiple threads for reading and
  writing, you must implement your own
  synchronization.

For more info, JaredPar posted a great blog entry at http://blogs.msdn.com/jaredpar/archive/2009/02/11/why-are-thread-safe-collections-so-hard.aspx on thread-safe collections.

OwnerDisplayName: MBeckius

ContentLicense: CC BY-SA 2.5

Title: ContainsKey Thread Safe

Body:
In the following code:

public class StringCache
{
    private readonly object lockobj = new object();

    private readonly Dictionary<int, string> cache = new Dictionary<int, string>();

    public string GetMemberInfo(int key)
    {
        if (cache.ContainsKey(key))
            return cache[key];
        lock (lockobj)
        {
            if (!cache.ContainsKey(key))
                cache[key] = GetString(key);
        }
        return cache[key];
    }

    private static string GetString(int key)
    {
        return "Not Important";
    }
}

1) Is ContainsKey thread safe?  IOW, what happens if that method is executing when another thread is adding something to the dictionary?
2) For the first return cache[key], is there any chance that it could return a garbled value?

TIA,

MB

Body:
The inherent thread safety of ContainsKey doesn't matter, since there is no synchronization between ContainsKey & cache[key].

For example:

if (cache.ContainsKey(key))
   // Switch to another thread, which deletes the key.
   return cache[key];

MSDN is pretty clear on this point:

To allow the collection to be accessed
  by multiple threads for reading and
  writing, you must implement your own
  synchronization.

For more info, JaredPar posted a great blog entry at http://blogs.msdn.com/jaredpar/archive/2009/02/11/why-are-thread-safe-collections-so-hard.aspx on thread-safe collections.

OwnerDisplayName: Michael

LastEditorDisplayName: Michael


问:
How to add conditions in Unity unit test hooks for test case
Similarly to how SpecFlow has tag features where one can add a @mytag to a scenario and in hooks, say for example in the before scenario, I can do a check if @mytag, do some code.

Is there a way to have my hooks in unity to conditionally do something before the scene starts?

```<code>[UnitySetUp]
public IEnumerator UnitySetUp()
{
    //if scenario contains this tag, do this code
    if(mytag)
    {
      do some code
    }
    yield return null;
}

</code>```

答:
I found the answer. It's using TestContext.</p>
```<code>[SetUp]
public void SetUp()
{
    if ((string)TestContext.CurrentContext.Test.Properties.Get("tag") == "mytag")
        //do some code
}

[Test]
[NUnit.Framework.Property("tag", "mytag")]
public void TestWithTag()
{

}
</code>```

Body:
Similarly to how SpecFlow has tag features where one can add a @mytag to a scenario and in hooks, say for example in the before scenario, I can do a check if @mytag, do some code.

Is there a way to have my hooks in unity to conditionally do something before the scene starts?

[UnitySetUp]
public IEnumerator UnitySetUp()
{
    //if scenario contains this tag, do this code
    if(mytag)
    {
      do some code
    }
    yield return null;
}

Body:
I found the answer. It's using TestContext.

[SetUp]
public void SetUp()
{
    if ((string)TestContext.CurrentContext.Test.Properties.Get("tag") == "mytag")
        //do some code
}

[Test]
[NUnit.Framework.Property("tag", "mytag")]
public void TestWithTag()
{

}