问:
Why is it faster to create a list of classes than a list of list of integers in C#?
Backgorund

Hi,

I am creating a game where the AI is collecting possible moves from positions which is done millions of times per turn. I am trying to figure out the fastest way to store these moves containing e.g. pieceFromSquare, pieceToSquare and other move related things.

Currently I have a Move class which contains public variables for the move as per below:

```<code>public class Move
{
    public int FromSquare;
    public int ToSquare;
    public int MoveType;

    public static Move GetMove(int fromSquare, int toSquare, int moveType)
    {
        Move move = new Move();

        move.FromSquare = fromSquare;
        move.ToSquare = toSquare;
        move.MoveType = moveType;

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

When the AI finds a move it stores the move in a List. I was thinking if it would be faster to store the move as a list of integers instead so I ran a test:

Test

```<code>public void RunTimerTest()
    {
        // Function A
        startTime = DateTime.UtcNow;
        moveListA = new List<List<int>>();
        for (int i = 0; i < numberOfIterations; i++)
        {
            FunctionA();
        }
        PrintResult((float)Math.Round((DateTime.UtcNow - startTime).TotalSeconds, 2), "Function A");

        // Function B
        startTime = DateTime.UtcNow;
        moveListB = new List<Move>();
        for (int i = 0; i < numberOfIterations; i++)
        {
            FunctionB();
        }
        PrintResult((float)Math.Round((DateTime.UtcNow - startTime).TotalSeconds, 2), "Function B");
    }

    private void FunctionA()
    {
        moveListA.Add(new List<int>() { 1, 123, 10});
    }

    private void FunctionB()
    {
        moveListB.Add(Move.GetMove(1, 123, 10));
    }
</code>```

The test results in the following result when running over 10 million times:

Function A ran for: 4,58 s.

Function B ran for: 1,47 s.

So it is more than 3 times faster to create the class and populate its variables than to create a list of integers.

Questions

Why is it so much faster to create a class than a list of integers?

Is there an even faster way to store this type of data?

答:
I examined four options, and allocating arrays for each record is by for the slowest. I did not check allocating class objects, as I was going for the faster options.

struct Move {} stores three integer values into readonly fields of a strcuture.

struct FMove {} stores an fixed int array of size 3

(int,int,int) stores a tuple of three  int values

int[] allocates an array of three values.

With the following results. I am tracking time and size allocated in bytes.

Storage
Iterations
Time
Size

Move struct
10000000 allocations
0.2633584 seconds
120000064 bytes

FMove struct
10000000 allocations
0.3572664 seconds
120000064 bytes

Tuple
10000000 allocations
0.702174 seconds
160000064 bytes

Array
10000000 allocations
1.2226393 seconds
480000064 bytes

```<code>public readonly struct Move
{
    public readonly int FromSquare;
    public readonly int ToSquare;
    public readonly int MoveType;

    public Move(int fromSquare, int toSquare, int moveType) : this()
    {
        FromSquare = fromSquare;
        ToSquare = toSquare;
        MoveType = moveType;
    }
}

public unsafe struct FMove
{
    fixed int Data[3];

    public FMove(int fromSquare, int toSquare, int moveType) : this()
    {
        Data[0] = fromSquare;
        Data[1] = toSquare;
        Data[2] = moveType;
    }

    public int FromSquare { get => Data[0]; }
    public int ToSquare { get => Data[1]; }
    public int MoveType { get => Data[2]; }
}

static class Program
{
    static void Main(string[] args)
    {
        // Always compile with Release to time
        const int count = 10000000;
        Console.WriteLine("Burn-in start");
        // Burn-in. Do some calc to spool up
        // the CPU
        AllocateArray(count/10);
        Console.WriteLine("Burn-in end");
        double[] timing = new double[4];
        // store timing results for four different
        // allocation types

        var sw = new Stopwatch();
        Console.WriteLine("Timming start");

        long startMemory;

        startMemory = GC.GetTotalMemory(true);
        sw.Restart();
        var r4 = AllocateArray(count);
        sw.Stop();
        var s4 = GC.GetTotalMemory(true) - startMemory;
        timing[3] = sw.Elapsed.TotalSeconds;

        startMemory = GC.GetTotalMemory(true);
        sw.Restart();
        var r1 = AllocateMove(count);
        sw.Stop();
        var s1 = GC.GetTotalMemory(true) - startMemory;
        timing[0] = sw.Elapsed.TotalSeconds;

        startMemory = GC.GetTotalMemory(true);
        sw.Restart();
        var r2 = AllocateFMove(count);
        sw.Stop();
        var s2 = GC.GetTotalMemory(true) - startMemory;
        timing[1] = sw.Elapsed.TotalSeconds;

        startMemory = GC.GetTotalMemory(true);
        sw.Restart();
        var r3 = AllocateTuple(count);
        sw.Stop();
        var s3 = GC.GetTotalMemory(true) - startMemory;
        timing[2] = sw.Elapsed.TotalSeconds;

        Console.WriteLine($"| Storage | Iterations | Time | Size |");
        Console.WriteLine($"|---|---|---|---|");
        Console.WriteLine($"|  Move struct| {r1.Count} allocations | {timing[0]} seconds | {s1} bytes |");
        Console.WriteLine($"| FMove struct| {r2.Count} allocations | {timing[1]} seconds | {s2} bytes |");
        Console.WriteLine($"|        Tuple| {r3.Count} allocations | {timing[2]} seconds | {s3} bytes |");
        Console.WriteLine($"|        Array| {r4.Count} allocations | {timing[3]} seconds | {s4} bytes |");
    }

    static List<Move> AllocateMove(int count)
    {
        var result = new List<Move>(count);
        for (int i = 0; i < count; i++)
        {
            result.Add(new Move(1, 123, 10));
        }
        return result;
    }
    static List<FMove> AllocateFMove(int count)
    {
        var result = new List<FMove>(count);
        for (int i = 0; i < count; i++)
        {
            result.Add(new FMove(1, 123, 10));
        }
        return result;
    }

    static List<(int from, int to, int type)> AllocateTuple(int count)
    {
        var result = new List<(int from, int to, int type)>(count);
        for (int i = 0; i < count; i++)
        {
            result.Add((1, 123, 10));
        }
        return result;
    }

    static List<int[]> AllocateArray(int count)
    {
        var result = new List<int[]>(count);
        for (int i = 0; i < count; i++)
        {
            result.Add(new int[] { 1, 123, 10});
        }
        return result;
    }

}
</code>```

Based on the comments, I decided to use BenchmarkDotNet for the above comparison also and the results are quite similar

Method
Count
Mean
Error
StdDev
Ratio

Move
10000000
115.9 ms
2.27 ms
2.23 ms
0.10

FMove
10000000
149.7 ms
2.04 ms
1.91 ms
0.12

Tuple
10000000
154.8 ms
2.99 ms
2.80 ms
0.13

Array
10000000
1,217.5 ms
23.84 ms
25.51 ms
1.00

I decided to add a class allocation (called CMove) with the following definition

```<code>public class CMove
{
    public readonly int FromSquare;
    public readonly int ToSquare;
    public readonly int MoveType;

    public CMove(int fromSquare, int toSquare, int moveType)
    {
        FromSquare = fromSquare;
        ToSquare = toSquare;
        MoveType = moveType;
    }
}
</code>```

And used the above as a baseline for benchmarking. I also tried different allocation sizes. And here is a summary of the results.

Anything below 1.0 means it is faster than CMove. As you can see array allocations is always bad. For a few allocations it does not matter much, but for a lot of allocations there are clear winners.

Body:
Backgorund

Hi,

I am creating a game where the AI is collecting possible moves from positions which is done millions of times per turn. I am trying to figure out the fastest way to store these moves containing e.g. pieceFromSquare, pieceToSquare and other move related things.

Currently I have a Move class which contains public variables for the move as per below:

public class Move
{
    public int FromSquare;
    public int ToSquare;
    public int MoveType;

    public static Move GetMove(int fromSquare, int toSquare, int moveType)
    {
        Move move = new Move();

        move.FromSquare = fromSquare;
        move.ToSquare = toSquare;
        move.MoveType = moveType;

        return move;
    }
}

When the AI finds a move it stores the move in a List. I was thinking if it would be faster to store the move as a list of integers instead so I ran a test:

Test

public void RunTimerTest()
    {
        // Function A
        startTime = DateTime.UtcNow;
        moveListA = new List<List<int>>();
        for (int i = 0; i < numberOfIterations; i++)
        {
            FunctionA();
        }
        PrintResult((float)Math.Round((DateTime.UtcNow - startTime).TotalSeconds, 2), "Function A");

        // Function B
        startTime = DateTime.UtcNow;
        moveListB = new List<Move>();
        for (int i = 0; i < numberOfIterations; i++)
        {
            FunctionB();
        }
        PrintResult((float)Math.Round((DateTime.UtcNow - startTime).TotalSeconds, 2), "Function B");
    }

    private void FunctionA()
    {
        moveListA.Add(new List<int>() { 1, 123, 10});
    }

    private void FunctionB()
    {
        moveListB.Add(Move.GetMove(1, 123, 10));
    }

The test results in the following result when running over 10 million times:

Function A ran for: 4,58 s.

Function B ran for: 1,47 s.

So it is more than 3 times faster to create the class and populate its variables than to create a list of integers.

Questions

Why is it so much faster to create a class than a list of integers?

Is there an even faster way to store this type of data?

Body:
I examined four options, and allocating arrays for each record is by for the slowest. I did not check allocating class objects, as I was going for the faster options.

struct Move {} stores three integer values into readonly fields of a strcuture.

struct FMove {} stores an fixed int array of size 3

(int,int,int) stores a tuple of three  int values

int[] allocates an array of three values.

With the following results. I am tracking time and size allocated in bytes.

Storage
Iterations
Time
Size

Move struct
10000000 allocations
0.2633584 seconds
120000064 bytes

FMove struct
10000000 allocations
0.3572664 seconds
120000064 bytes

Tuple
10000000 allocations
0.702174 seconds
160000064 bytes

Array
10000000 allocations
1.2226393 seconds
480000064 bytes

public readonly struct Move
{
    public readonly int FromSquare;
    public readonly int ToSquare;
    public readonly int MoveType;

    public Move(int fromSquare, int toSquare, int moveType) : this()
    {
        FromSquare = fromSquare;
        ToSquare = toSquare;
        MoveType = moveType;
    }
}

public unsafe struct FMove
{
    fixed int Data[3];

    public FMove(int fromSquare, int toSquare, int moveType) : this()
    {
        Data[0] = fromSquare;
        Data[1] = toSquare;
        Data[2] = moveType;
    }

    public int FromSquare { get => Data[0]; }
    public int ToSquare { get => Data[1]; }
    public int MoveType { get => Data[2]; }
}

static class Program
{
    static void Main(string[] args)
    {
        // Always compile with Release to time
        const int count = 10000000;
        Console.WriteLine("Burn-in start");
        // Burn-in. Do some calc to spool up
        // the CPU
        AllocateArray(count/10);
        Console.WriteLine("Burn-in end");
        double[] timing = new double[4];
        // store timing results for four different
        // allocation types

        var sw = new Stopwatch();
        Console.WriteLine("Timming start");

        long startMemory;

        startMemory = GC.GetTotalMemory(true);
        sw.Restart();
        var r4 = AllocateArray(count);
        sw.Stop();
        var s4 = GC.GetTotalMemory(true) - startMemory;
        timing[3] = sw.Elapsed.TotalSeconds;

        startMemory = GC.GetTotalMemory(true);
        sw.Restart();
        var r1 = AllocateMove(count);
        sw.Stop();
        var s1 = GC.GetTotalMemory(true) - startMemory;
        timing[0] = sw.Elapsed.TotalSeconds;

        startMemory = GC.GetTotalMemory(true);
        sw.Restart();
        var r2 = AllocateFMove(count);
        sw.Stop();
        var s2 = GC.GetTotalMemory(true) - startMemory;
        timing[1] = sw.Elapsed.TotalSeconds;

        startMemory = GC.GetTotalMemory(true);
        sw.Restart();
        var r3 = AllocateTuple(count);
        sw.Stop();
        var s3 = GC.GetTotalMemory(true) - startMemory;
        timing[2] = sw.Elapsed.TotalSeconds;

        Console.WriteLine($"| Storage | Iterations | Time | Size |");
        Console.WriteLine($"|---|---|---|---|");
        Console.WriteLine($"|  Move struct| {r1.Count} allocations | {timing[0]} seconds | {s1} bytes |");
        Console.WriteLine($"| FMove struct| {r2.Count} allocations | {timing[1]} seconds | {s2} bytes |");
        Console.WriteLine($"|        Tuple| {r3.Count} allocations | {timing[2]} seconds | {s3} bytes |");
        Console.WriteLine($"|        Array| {r4.Count} allocations | {timing[3]} seconds | {s4} bytes |");
    }

    static List<Move> AllocateMove(int count)
    {
        var result = new List<Move>(count);
        for (int i = 0; i < count; i++)
        {
            result.Add(new Move(1, 123, 10));
        }
        return result;
    }
    static List<FMove> AllocateFMove(int count)
    {
        var result = new List<FMove>(count);
        for (int i = 0; i < count; i++)
        {
            result.Add(new FMove(1, 123, 10));
        }
        return result;
    }

    static List<(int from, int to, int type)> AllocateTuple(int count)
    {
        var result = new List<(int from, int to, int type)>(count);
        for (int i = 0; i < count; i++)
        {
            result.Add((1, 123, 10));
        }
        return result;
    }

    static List<int[]> AllocateArray(int count)
    {
        var result = new List<int[]>(count);
        for (int i = 0; i < count; i++)
        {
            result.Add(new int[] { 1, 123, 10});
        }
        return result;
    }

}

Based on the comments, I decided to use BenchmarkDotNet for the above comparison also and the results are quite similar

Method
Count
Mean
Error
StdDev
Ratio

Move
10000000
115.9 ms
2.27 ms
2.23 ms
0.10

FMove
10000000
149.7 ms
2.04 ms
1.91 ms
0.12

Tuple
10000000
154.8 ms
2.99 ms
2.80 ms
0.13

Array
10000000
1,217.5 ms
23.84 ms
25.51 ms
1.00

I decided to add a class allocation (called CMove) with the following definition

public class CMove
{
    public readonly int FromSquare;
    public readonly int ToSquare;
    public readonly int MoveType;

    public CMove(int fromSquare, int toSquare, int moveType)
    {
        FromSquare = fromSquare;
        ToSquare = toSquare;
        MoveType = moveType;
    }
}

And used the above as a baseline for benchmarking. I also tried different allocation sizes. And here is a summary of the results.

Anything below 1.0 means it is faster than CMove. As you can see array allocations is always bad. For a few allocations it does not matter much, but for a lot of allocations there are clear winners.


来源: stackexchange

问:
How to get asterisk* at the end of children to be red colored?
When required prop is true, I want * at the end of children to be red colored. Current code below is printing out 'Label Component*'. Before the change, I had it {required ? `${children} * : children} and it worked except * was default black color. All I'm trying to do is turn it into red colored * and facing difficulties. What am I doing wrong? https://codesandbox.io/s/label-component-ts-zw392?file=/src/App.tsx:0-1356</p>
```<code>import * as React from "react";

export interface ILabelProps {
  weight?: "normal" | "bold";
  htmlFor?: string;
  children?: React.ReactNode;
  testId?: string;
  required?: boolean;
}

export const Label: React.FunctionComponent<ILabelProps> = ({
  htmlFor,
  weight = "normal",
  testId,
  required = false,
  children
}: ILabelProps) => {
  const dataRef = React.useRef(null);

  const createTestId = (
    ref: HTMLElement,
    testId: string | undefined,
    testIdName: string = "data-testid"
  ) => {
    if (ref && testId) {
      ref.setAttribute(testIdName, testId);
    }
  };

  React.useEffect(() => {
    if (dataRef.current) {
      createTestId(dataRef.current, testId);
    }
  }, [dataRef, testId]);

  const requiredAsterisk = `<span color="red">*</span>`;

  return (
    <label ref={dataRef} htmlFor={htmlFor} style={{ fontWeight: weight }}>
      {required ? (
        <span>
          {children}
          {requiredAsterisk}
        </span>
      ) : (
        children
      )}
    </label>
  );
};

export const Content: React.FunctionComponent = () => {
  return (
    <div>
      <input type="text"></input>
    </div>
  );
};

export default function App() {
  return (
    <Label
      htmlFor="some-id"
      testId="testing-label"
      weight="normal"
      required={true}
    >
      Label Component
    </Label>
  );
}

</code>```

答:
You can't set the color with a color prop. If you want to set it inline it needs to be inside a 'style' prop as mentioned above. So line 36 becomes</p>
```<code>const requiredAsterisk = <span style={{ color: "red" }}>*</span>;
</code>```

ContentLicense: CC BY-SA 4.0

Body:
When required prop is true, I want * at the end of children to be red colored. Current code below is printing out 'Label Component*'. Before the change, I had it {required ? `${children} * : children} and it worked except * was default black color. All I'm trying to do is turn it into red colored * and facing difficulties. What am I doing wrong? https://codesandbox.io/s/label-component-ts-zw392?file=/src/App.tsx:0-1356

import * as React from "react";

export interface ILabelProps {
  weight?: "normal" | "bold";
  htmlFor?: string;
  children?: React.ReactNode;
  testId?: string;
  required?: boolean;
}

export const Label: React.FunctionComponent<ILabelProps> = ({
  htmlFor,
  weight = "normal",
  testId,
  required = false,
  children
}: ILabelProps) => {
  const dataRef = React.useRef(null);

  const createTestId = (
    ref: HTMLElement,
    testId: string | undefined,
    testIdName: string = "data-testid"
  ) => {
    if (ref && testId) {
      ref.setAttribute(testIdName, testId);
    }
  };

  React.useEffect(() => {
    if (dataRef.current) {
      createTestId(dataRef.current, testId);
    }
  }, [dataRef, testId]);

  const requiredAsterisk = `<span color="red">*</span>`;

  return (

      {required ? (

          {children}
          {requiredAsterisk}

      ) : (
        children
      )}

  );
};

export const Content: React.FunctionComponent = () => {
  return (

      <input type="text"></input>

  );
};

export default function App() {
  return (

      Label Component

  );
}

Body:
You can't set the color with a color prop. If you want to set it inline it needs to be inside a 'style' prop as mentioned above. So line 36 becomes

const requiredAsterisk = <span style={{ color: "red" }}>*</span>;


问:
Find duplicate folder names and ignore further subdirectory matches
I have a data directory where duplicate folders have been created at various hierarchy levels. I want to find these duplicates using case insensitive comparison. I also want to ignore further subdirectory matches for every match, so as to reduce the noise, since in my usecase directory names are quite unique and indicate that all subdirectories are also likely to be duplicates (basically a given folder and all its subfolders has been copied at various locations). In other words, if 2 directories match but their parents, while being in different paths, also match, just return the parents.

For example, if I have the following folders:

```<code>a/b/Dup1
a/b/dup1/ignore1
c/dup1/ignore1
</code>```

I want the output to be something like:

```<code>'dup1' found in:
a/b/Dup1
a/b/dup1
c/dup1
</code>```

Any other output that lists all the case-insensitive duplicates and their relative or absolute path is also acceptable (or even just the name of the duplicate directory, since it should be easy to list its locations from there).

答:
You can try combining find, sort and uniq tools to get desired result. This one liner should work:

find yourstartingpath -type d -printf '%f\n' |sort -i |uniq -icd |while read c x;do echo "===== $x found $c times in =====";find yourstartingpath  -type d -iname "$x" -printf '%p\n';done

This oneliner will not remove subdirectories from search, but I believe it would be hard to do so with only bash core utilities. If it is necessary, depending on your system probably it would be better to write program in either perl or python to get what you need. Eventually you may restrict your results to folders, which have some common subdirectories, ie. using following oneliner:

find yourstartingpath -type d -printf '%P\n'  |awk -F/ -v OFS='/'  'NF>=4 {print $(NF-1),$NF}' |sort -i |uniq -id |while read x;do echo "======= $x "; find yourstartingpath -type d -ipath "*$x";done

but it will remove Dup1 folder from your example (because it does not have ignore1 subfolder).

Body:
I have a data directory where duplicate folders have been created at various hierarchy levels. I want to find these duplicates using case insensitive comparison. I also want to ignore further subdirectory matches for every match, so as to reduce the noise, since in my usecase directory names are quite unique and indicate that all subdirectories are also likely to be duplicates (basically a given folder and all its subfolders has been copied at various locations). In other words, if 2 directories match but their parents, while being in different paths, also match, just return the parents.

For example, if I have the following folders:

a/b/Dup1
a/b/dup1/ignore1
c/dup1/ignore1

I want the output to be something like:

'dup1' found in:
a/b/Dup1
a/b/dup1
c/dup1

Any other output that lists all the case-insensitive duplicates and their relative or absolute path is also acceptable (or even just the name of the duplicate directory, since it should be easy to list its locations from there).


问:
Firestore Security Rule to allow update nested field
I have a document with structure

```<code>name: "blablabla"
members:
    123: true
    456: true
</code>```

Users can add themselves in members

```<code>docRef.update(FieldPath.of("members", myUid), true);
</code>```

How do I write a security rule to allow this?

I tried

```<code>allow update: if is_valid(request.resource.data);
function is_valid(data) {
  return data.keys() == ['members']
      && data.members.keys() == [request.auth.uid]
      && data.members[request.auth.uid] == true;
}
</code>```

But I get PERMISSION_DENIED: Missing or insufficient permissions.

(However in Security Rules Simulator access is allowed)

答:
I came up with a condition using removedKeys. In theory, this should work as it should.</p>
```<code>allow update: if is_valid(request.resource.data);
function is_valid(data) {
  return data.keys() == ['members']
      && data.members.keys() == [request.auth.uid]
      && afterData().members.diff(resource.data.members).removedKeys().size() == 0;
}
function afterData() {
  return getAfter(/databases/$(database)/documents).data;
}
</code>```

Body:
I have a document with structure

name: "blablabla"
members:
    123: true
    456: true

Users can add themselves in members

docRef.update(FieldPath.of("members", myUid), true);

How do I write a security rule to allow this?

I tried

allow update: if is_valid(request.resource.data);
function is_valid(data) {
  return data.keys() == ['members']
      && data.members.keys() == [request.auth.uid]
      && data.members[request.auth.uid] == true;
}

But I get PERMISSION_DENIED: Missing or insufficient permissions.

(However in Security Rules Simulator access is allowed)

Body:
I came up with a condition using removedKeys. In theory, this should work as it should.

allow update: if is_valid(request.resource.data);
function is_valid(data) {
  return data.keys() == ['members']
      && data.members.keys() == [request.auth.uid]
      && afterData().members.diff(resource.data.members).removedKeys().size() == 0;
}
function afterData() {
  return getAfter(/databases/$(database)/documents).data;
}


问:
Problem while using bs4: 'NoneType' object is not subscriptable
I'm trying to scrape a Goodreads Page to get all editions of a book, but when I run the code I get this error:

```<code>Traceback (most recent call last):
  File "C:/xxx/PycharmProjects/wikipedia_pageview/isbn.py", line 141, in <module>
    ed_details = get_editions_details(isbn)
  File "C:/xxx/PycharmProjects/wikipedia_pageview/isbn.py", line 79, in get_editions_details
    if ed_link := f"https://www.goodreads.com{ed_item['href']}":...
TypeError: 'NoneType' object is not subscriptable
</code>```

I tried to put conditions for this reason in the selected areas but they don't work.
Code:

```<code>def get_editions_details(isbn):
# Create the search URL with the ISBN of the book
data = {'q': isbn}
book_url = get_page("https://www.goodreads.com/search", data)
#print(book_url)
# Parse the markup with Beautiful Soup
soup = bs(book_url.text, 'lxml')
# Retrieve from the book's page the link for other editions
# and the total number of editions
if ed_item := soup.find("div", class_="otherEditionsLink"):
    if ed_item := ed_item.find("a"):
        print(ed_item)
    else:
        pass

if ed_item:
    ed_num = ed_item.text.strip().split(' ')[-1].strip('()')

if ed_link := f"https://www.goodreads.com{ed_item['href']}":#capire...
    print(ed_link)
else:
    pass
return((ed_link, int(ed_num), isbn))

if __name__ == "__main__":
        try:
            os.mkdir('./urls_files')
        except Exception:
            pass

    isbns = get_isbn()

    for isbn in isbns:
            ed_details = get_editions_details(isbn)
            get_editions_urls(ed_details)
</code>```

答:
What happens?

The indentation in your example seems to be not correct and will not handle wrong or missing isbn or editionlinks.

How to fix?

Assign the values to ed_link and ed_num in the moment you can be sure there exist a href in ed_item else set them to None or 0 or handle these issue in another way:

```<code>def get_editions_details(isbn):

    data = {'q': isbn}
    book_url = requests.get("https://www.goodreads.com/search", data)
    soup = bs(book_url.text, 'lxml')

    ed_link = None
    ed_num = 0

    if ed_item := soup.find("div", class_="otherEditionsLink"):
        if ed_item := ed_item.find("a"):
            ed_link = f"https://www.goodreads.com{ed_item['href']}"
            ed_num = ed_item.text.strip().split(' ')[-1].strip('()')
        else:
            pass

    return((ed_link, int(ed_num), isbn))

if __name__ == "__main__":
    #just as example to simulate an error
    ed_details = get_editions_details(1)
    if ed_details[0]:
        get_editions_urls(ed_details)
    else:
        print(f'no editionlinks for isbn:{ed_details[2]}')
</code>```

Body:
I'm trying to scrape a Goodreads Page to get all editions of a book, but when I run the code I get this error:

Traceback (most recent call last):
  File "C:/xxx/PycharmProjects/wikipedia_pageview/isbn.py", line 141, in <module>
    ed_details = get_editions_details(isbn)
  File "C:/xxx/PycharmProjects/wikipedia_pageview/isbn.py", line 79, in get_editions_details
    if ed_link := f"https://www.goodreads.com{ed_item['href']}":...
TypeError: 'NoneType' object is not subscriptable

I tried to put conditions for this reason in the selected areas but they don't work.
Code:

def get_editions_details(isbn):
# Create the search URL with the ISBN of the book
data = {'q': isbn}
book_url = get_page("https://www.goodreads.com/search", data)
#print(book_url)
# Parse the markup with Beautiful Soup
soup = bs(book_url.text, 'lxml')
# Retrieve from the book's page the link for other editions
# and the total number of editions
if ed_item := soup.find("div", class_="otherEditionsLink"):
    if ed_item := ed_item.find("a"):
        print(ed_item)
    else:
        pass

if ed_item:
    ed_num = ed_item.text.strip().split(' ')[-1].strip('()')

if ed_link := f"https://www.goodreads.com{ed_item['href']}":#capire...
    print(ed_link)
else:
    pass
return((ed_link, int(ed_num), isbn))

if __name__ == "__main__":
        try:
            os.mkdir('./urls_files')
        except Exception:
            pass

    isbns = get_isbn()

    for isbn in isbns:
            ed_details = get_editions_details(isbn)
            get_editions_urls(ed_details)

Body:
What happens?

The indentation in your example seems to be not correct and will not handle wrong or missing isbn or editionlinks.

How to fix?

Assign the values to ed_link and ed_num in the moment you can be sure there exist a href in ed_item else set them to None or 0 or handle these issue in another way:

def get_editions_details(isbn):

    data = {'q': isbn}
    book_url = requests.get("https://www.goodreads.com/search", data)
    soup = bs(book_url.text, 'lxml')

    ed_link = None
    ed_num = 0

    if ed_item := soup.find("div", class_="otherEditionsLink"):
        if ed_item := ed_item.find("a"):
            ed_link = f"https://www.goodreads.com{ed_item['href']}"
            ed_num = ed_item.text.strip().split(' ')[-1].strip('()')
        else:
            pass

    return((ed_link, int(ed_num), isbn))

if __name__ == "__main__":
    #just as example to simulate an error
    ed_details = get_editions_details(1)
    if ed_details[0]:
        get_editions_urls(ed_details)
    else:
        print(f'no editionlinks for isbn:{ed_details[2]}')


问:
Azure Functions with java - How to get the logs written within the function?
I've deployed a queue triggered azure function with Java in Azure. I've added logback-classic and lombok in pom.xml for logging.
But the logs are not displayed on the function's monitor > invocation details or the log-streaming service in portal.
But I could see the logs written with context.getLogger(). The logs writter with logback logger is not visible. Please let me know how to check my logs in function invocation.

Following is the queue triggerred azure function handle

```<code>public class QueueHandlerFunction {

  @FunctionName("queuetriggertest")
  public void queueMessageHandler(@QueueTrigger(name = "msg",
      queueName = "my-test-queue", connection = "MyQStorage") final String payload,
      final ExecutionContext context) {

    //Logs with this logger is visible
    context.getLogger().info("Received Message From my-test-queue : " + payload);

    MySampleService.handleQueueMessage(payload);
  }
}
</code>```

Following is the MySampleService class with lombok logger

```<code>@Slf4j
public class MySampleService {

  public static void handleQueueMessage(final String payload) {

    log.info("<<<<<<<<<<<< INSIDE THE SERVICE HANDLE >>>>>>>>>>>>");
    if (StringUtils.isNotBlank(payload)) {
        log.info("Received Payload : {}", payload); //This log not available
        // TODO Work with incoming payload
    } else {
      log.info("Message payload is Blank."); //This log not available
    }
  }

}
</code>```

Following is he logback.xml placed in the resources folder of the maven project.

```<code><?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<configuration>
   <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
      <encoder>
         <pattern>%d{E MMM dd yyyy hh:mm:ss a} [%thread] %-5level %logger{36}
                - %msg%n</pattern>
      </encoder>
   </appender>

   <logger name="com.howayig.test" level="INFO" />
   <root level="INFO">
      <appender-ref ref="STDOUT" />
   </root>
</configuration>
</code>```

And I've the following dependencies in the pom.xml

```<code><dependencies>
        <dependency>
            <groupId>com.microsoft.azure.functions</groupId>
            <artifactId>azure-functions-java-library</artifactId>
            <version>1.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.7</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.20</version>
        </dependency>
    </dependencies>
</code>```

EDIT: Attached portal screenshot for the function invocation logs...

EDIT 2: Added screenshots of local execution and deployed one in portal.

Logs in command prompt when is executed locally

Live streaming console service of the function from the portal for a new message in queue

Invocation details of the function from the portal for a new message in queue

答:
Updated in 2022

Application Insights agent supports SLF4j logs (for log4j2, logback, and jul) out of the box. Quoting from https://learn.microsoft.com/en-us/azure/azure-monitor/app/java-standalone-config#auto-collected-logging

Log4j, Logback, and java.util.logging are auto-instrumented, and
logging performed via these logging frameworks is auto-collected.
Logging is only captured if it first meets the level that is
configured for the logging framework, and second, also meets the level
that is configured for Application Insights.

For more details, refer to a similar question: https://stackoverflow.com/a/71203259/894565

Body:
I've deployed a queue triggered azure function with Java in Azure. I've added logback-classic and lombok in pom.xml for logging.
But the logs are not displayed on the function's monitor > invocation details or the log-streaming service in portal.
But I could see the logs written with context.getLogger(). The logs writter with logback logger is not visible. Please let me know how to check my logs in function invocation.

Following is the queue triggerred azure function handle

public class QueueHandlerFunction {

  @FunctionName("queuetriggertest")
  public void queueMessageHandler(@QueueTrigger(name = "msg",
      queueName = "my-test-queue", connection = "MyQStorage") final String payload,
      final ExecutionContext context) {

    //Logs with this logger is visible
    context.getLogger().info("Received Message From my-test-queue : " + payload);

    MySampleService.handleQueueMessage(payload);
  }
}

Following is the MySampleService class with lombok logger

@Slf4j
public class MySampleService {

  public static void handleQueueMessage(final String payload) {

    log.info("<<<<<<<<<<<< INSIDE THE SERVICE HANDLE >>>>>>>>>>>>");
    if (StringUtils.isNotBlank(payload)) {
        log.info("Received Payload : {}", payload); //This log not available
        // TODO Work with incoming payload
    } else {
      log.info("Message payload is Blank."); //This log not available
    }
  }

}

Following is he logback.xml placed in the resources folder of the maven project.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<configuration>
   <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
      <encoder>
         <pattern>%d{E MMM dd yyyy hh:mm:ss a} [%thread] %-5level %logger{36}
                - %msg%n</pattern>
      </encoder>
   </appender>

   <logger name="com.howayig.test" level="INFO" />
   <root level="INFO">
      <appender-ref ref="STDOUT" />
   </root>
</configuration>

And I've the following dependencies in the pom.xml

<dependencies>
        <dependency>
            <groupId>com.microsoft.azure.functions</groupId>
            <artifactId>azure-functions-java-library</artifactId>
            <version>1.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.7</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.20</version>
        </dependency>
    </dependencies>

EDIT: Attached portal screenshot for the function invocation logs...

EDIT 2: Added screenshots of local execution and deployed one in portal.

Logs in command prompt when is executed locally

Live streaming console service of the function from the portal for a new message in queue

Invocation details of the function from the portal for a new message in queue


问:
How to upload an image from a remote website, by using javascript?
Is there any way I can do that? For example, I have an

```<code><input type="file" id="upload_file" />
</code>```

Obviously I can't just

```<code>$('#upload_file').val('http://www.site.com/path/to/image.jpg').parent().submit();
</code>```

答:
IF that site supports what are you trying to do it should work.
Most of file uploads are very strict, as you can't just upload a file wherever you want even if you know the upload script.

If host supports sending a link to the picture and he knows what to do with it ... I don't see why your script should not work.

```<code><form method="post" action="http://www.example.org/">
     <input type="text" name="upload_file" id="upload_file" value="" />
     <input type="submit" onclick="$('#upload_file').val('http://www.example.org/example.jpg');">
</form>
</code>```

ContentLicense: CC BY-SA 3.0

Body:
Is there any way I can do that? For example, I have an

<input type="file" id="upload_file" />

Obviously I can't just

$('#upload_file').val('http://www.site.com/path/to/image.jpg').parent().submit();

Body:
IF that site supports what are you trying to do it should work.
Most of file uploads are very strict, as you can't just upload a file wherever you want even if you know the upload script.

If host supports sending a link to the picture and he knows what to do with it ... I don't see why your script should not work.

<form method="post" action="http://www.example.org/">
     <input type="text" name="upload_file" id="upload_file" value="" />
     <input type="submit" onclick="$('#upload_file').val('http://www.example.org/example.jpg');">
</form>


问:
How to align icon with element instead of element text
I have a svg icon and a span element side by side. When the text overflows inside the span element, the svg also comes down along with it. I don't want it to happen.

Here is a  of what I'm doing.

As you can see, the arrow icon is proper in Section 1 but in Section 2, it comes down with the text.

Here is the HTML code -

.rightContent {
  border: 2px solid green;
  height: 100vh;
  overflow-y: scroll;
}

.sectionHeading {
  background: #f7f9fa;
  text-align: left;
  font-family: sf pro display, -apple-system, BlinkMacSystemFont, Roboto, segoe ui, Helvetica, Arial, sans-serif, apple color emoji, segoe ui emoji, segoe ui symbol;
  font-weight: 700;
  line-height: 1.2;
  letter-spacing: -.02rem;
  font-size: 1rem;
  padding-top: .75rem;
  padding-bottom: 1.05rem;
}

.sectionHeading p {
  padding: 0;
  margin: 0;
}

.sectionHeading svg {
  float: right;
}

.sectionHeading small {
  font-size: 0.75rem;
  font-weight: 500;
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><div class="rightContent col col-lg-3 col-md-3 sol-sm-3" style="border: 2px solid green;">
  <!-- Section 1: Introduction -->
  <div class="row">
    <div class="card">
      <div class="sectionHeading">
        <span>Section 1: Introduction</span>
        <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-caret-down" viewBox="0 0 16 16">
                                <path d="M3.204 5h9.592L8 10.481 3.204 5zm-.753.659 4.796 5.48a1 1 0 0 0 1.506 0l4.796-5.48c.566-.647.106-1.659-.753-1.659H3.204a1 1 0 0 0-.753 1.659z"/>
                            </svg>
        <p></p>
        <small>14 / 14 | 1hr 5min</small>
      </div>
    </div>
  </div>

  <!-- Section 2: Experience -->
  <div class="row">
    <div class="card">
      <div class="sectionHeading">
        <span>Section 2: Experiences In The Corporate World</span>
        <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-caret-down" viewBox="0 0 16 16">
                                <path d="M3.204 5h9.592L8 10.481 3.204 5zm-.753.659 4.796 5.48a1 1 0 0 0 1.506 0l4.796-5.48c.566-.647.106-1.659-.753-1.659H3.204a1 1 0 0 0-.753 1.659z"/>
                            </svg>
        <p></p>
        <small>19 / 19 | 2hr 15min</small>
      </div>
    </div>
  </div></code>```

Most of the questions already present deal with aligning icons with text, but as I mentioned earlier, I want to align icons with span element and keep it fixed there, instead of aligning it with text.

Please feel free to share your inputs. I'll be grateful to learn from others since I'm still learning CSS. Thank you.

答:
You had a few small problems like an empty p tag which I fixed.

I wrapped small & span tags in a container and Since you're using bootstrap, I added d-flex justify-content-between classes to sectionHeading. This fixed the problem.

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box
}

.rightContent {
  border: 2px solid green;
  height: 100vh;
  overflow-y: scroll;
}

.sectionHeading {
  background: #f7f9fa;
  text-align: left;
  font-family: sf pro display, -apple-system, BlinkMacSystemFont, Roboto, segoe ui, Helvetica, Arial, sans-serif, apple color emoji, segoe ui emoji, segoe ui symbol;
  font-weight: 700;
  line-height: 1.2;
  letter-spacing: -.02rem;
  font-size: 1rem;
  padding-top: .75rem;
  padding-bottom: 1.05rem;
}

.sectionHeading p {
  padding: 0;
  margin: 0;
}

.sectionHeading svg {
  float: right;
}

.sectionHeading small {
  font-size: 0.75rem;
  font-weight: 500;
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

<div class="rightContent col col-lg-3 col-md-3 sol-sm-3" style="border: 2px solid green;">
  <!-- Section 1: Introduction -->
  <div class="row">
    <div class="card">
      <div class="sectionHeading d-flex justify-content-between">
        <div>
          <span>Section 1: Introduction</span><br/>
          <small>14 / 14 | 1hr 5min</small>
        </div>
        <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-caret-down" viewBox="0 0 16 16">
                                <path d="M3.204 5h9.592L8 10.481 3.204 5zm-.753.659 4.796 5.48a1 1 0 0 0 1.506 0l4.796-5.48c.566-.647.106-1.659-.753-1.659H3.204a1 1 0 0 0-.753 1.659z"/>
                            </svg>
      </div>
    </div>
  </div>

  <!-- Section 2: Experience -->
  <div class="row">
    <div class="card">
      <div class="sectionHeading d-flex justify-content-between">
        <div>
          <span>Section 2: Experiences In The Corporate World</span><br/>
          <small>19 / 19 | 2hr 15min</small>
        </div>
        <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-caret-down" viewBox="0 0 16 16">
                                <path d="M3.204 5h9.592L8 10.481 3.204 5zm-.753.659 4.796 5.48a1 1 0 0 0 1.506 0l4.796-5.48c.566-.647.106-1.659-.753-1.659H3.204a1 1 0 0 0-.753 1.659z"/>
                            </svg>
      </div>
    </div>
  </div></code>```

Body:
I have a svg icon and a span element side by side. When the text overflows inside the span element, the svg also comes down along with it. I don't want it to happen.

Here is a  of what I'm doing.

As you can see, the arrow icon is proper in Section 1 but in Section 2, it comes down with the text.

Here is the HTML code -

.rightContent {
  border: 2px solid green;
  height: 100vh;
  overflow-y: scroll;
}

.sectionHeading {
  background: #f7f9fa;
  text-align: left;
  font-family: sf pro display, -apple-system, BlinkMacSystemFont, Roboto, segoe ui, Helvetica, Arial, sans-serif, apple color emoji, segoe ui emoji, segoe ui symbol;
  font-weight: 700;
  line-height: 1.2;
  letter-spacing: -.02rem;
  font-size: 1rem;
  padding-top: .75rem;
  padding-bottom: 1.05rem;
}

.sectionHeading p {
  padding: 0;
  margin: 0;
}

.sectionHeading svg {
  float: right;
}

.sectionHeading small {
  font-size: 0.75rem;
  font-weight: 500;
}

        Section 1: Introduction
        <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-caret-down" viewBox="0 0 16 16">
                                <path d="M3.204 5h9.592L8 10.481 3.204 5zm-.753.659 4.796 5.48a1 1 0 0 0 1.506 0l4.796-5.48c.566-.647.106-1.659-.753-1.659H3.204a1 1 0 0 0-.753 1.659z"/>
                            </svg>

        14 / 14 | 1hr 5min

        Section 2: Experiences In The Corporate World
        <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-caret-down" viewBox="0 0 16 16">
                                <path d="M3.204 5h9.592L8 10.481 3.204 5zm-.753.659 4.796 5.48a1 1 0 0 0 1.506 0l4.796-5.48c.566-.647.106-1.659-.753-1.659H3.204a1 1 0 0 0-.753 1.659z"/>
                            </svg>

        19 / 19 | 2hr 15min

Most of the questions already present deal with aligning icons with text, but as I mentioned earlier, I want to align icons with span element and keep it fixed there, instead of aligning it with text.

Please feel free to share your inputs. I'll be grateful to learn from others since I'm still learning CSS. Thank you.

Body:
You had a few small problems like an empty p tag which I fixed.

I wrapped small & span tags in a container and Since you're using bootstrap, I added d-flex justify-content-between classes to sectionHeading. This fixed the problem.

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box
}

.rightContent {
  border: 2px solid green;
  height: 100vh;
  overflow-y: scroll;
}

.sectionHeading {
  background: #f7f9fa;
  text-align: left;
  font-family: sf pro display, -apple-system, BlinkMacSystemFont, Roboto, segoe ui, Helvetica, Arial, sans-serif, apple color emoji, segoe ui emoji, segoe ui symbol;
  font-weight: 700;
  line-height: 1.2;
  letter-spacing: -.02rem;
  font-size: 1rem;
  padding-top: .75rem;
  padding-bottom: 1.05rem;
}

.sectionHeading p {
  padding: 0;
  margin: 0;
}

.sectionHeading svg {
  float: right;
}

.sectionHeading small {
  font-size: 0.75rem;
  font-weight: 500;
}

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

<div class="rightContent col col-lg-3 col-md-3 sol-sm-3" style="border: 2px solid green;">
  <!-- Section 1: Introduction -->
  <div class="row">
    <div class="card">
      <div class="sectionHeading d-flex justify-content-between">
        <div>
          <span>Section 1: Introduction</span><br/>
          <small>14 / 14 | 1hr 5min</small>
        </div>
        <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-caret-down" viewBox="0 0 16 16">
                                <path d="M3.204 5h9.592L8 10.481 3.204 5zm-.753.659 4.796 5.48a1 1 0 0 0 1.506 0l4.796-5.48c.566-.647.106-1.659-.753-1.659H3.204a1 1 0 0 0-.753 1.659z"/>
                            </svg>
      </div>
    </div>
  </div>

  <!-- Section 2: Experience -->
  <div class="row">
    <div class="card">
      <div class="sectionHeading d-flex justify-content-between">
        <div>
          <span>Section 2: Experiences In The Corporate World</span><br/>
          <small>19 / 19 | 2hr 15min</small>
        </div>
        <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-caret-down" viewBox="0 0 16 16">
                                <path d="M3.204 5h9.592L8 10.481 3.204 5zm-.753.659 4.796 5.48a1 1 0 0 0 1.506 0l4.796-5.48c.566-.647.106-1.659-.753-1.659H3.204a1 1 0 0 0-.753 1.659z"/>
                            </svg>
      </div>
    </div>
  </div>


问:
How can I move the "ul" block of navbar to the right without affecting the toggle button functionality of responsiveness in the navbar?
I am using bootstrap NavBar ..

When trying to move the ul block to the right it disables the toggle button responsiveness ...

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

    Rehan's Thoughts
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">

            </button>

          Home

          About

          Contact

          Blog

```

答:
Add w-100(width: 100%;) to your ul element and also add justify-content-end or (justify-content: right;)

  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

    Rehan's Thoughts
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">

            </button>

          Home

          About

          Contact

          Blog

```

Body:
Add w-100(width: 100%;) to your ul element and also add justify-content-end or (justify-content: right;)

<head>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
</head>

<nav class="navbar navbar-expand-lg navbar-light bg-dark">
  <div class="container-fluid">
    <a class="navbar-brand nav-text text-white" href="#">Rehan's Thoughts</a>
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
    <div class="collapse navbar-collapse" id="navbarNav">
      <ul class="navbar-nav w-100 justify-content-end">
        <li class="nav-item">
          <a class="nav-link text-white active" aria-current="page" href="#">Home</a>
        </li>
        <li class="nav-item">
          <a class="nav-link text-white" href="#">About</a>
        </li>
        <li class="nav-item">
          <a class="nav-link text-white" href="#">Contact</a>
        </li>
        <li class="nav-item">
          <a class="nav-link text-white disabled">Blog</a>
        </li>
      </ul>
    </div>
  </div>
</nav>


问:
React Testing with history passed

Problem Statement

I'm using create react app with Jest. In my test, I simply want to render out a component that needs history passed to the state before it can load. I've been researching and trying to get to this to work for awhile.
The source I've been using shows how to pass the history to the router - https://testing-library.com/docs/example-react-router/

When running the test an error says

"Cannot read property 'state' of undefined"

I believe it's because the history isn't being properly passed in during the test.

I'm really new to testing and am hoping someone can point me in the right direction to having it render the component during the test successfully. Thanks!

Code

The rendered component:

```<code>const [data, setData] = useState({
            pack: props && props.location.state && props.location.state.hasOwnProperty('package')
                ? props.location.state.package
                : ''
        });

const ContactPage = (props) => (
    Contact &&
    <Layout>

            <div className="contact-Letter">
                    {Contact(props)}
            </div>

    </Layout>
);
</code>```

The test component "attempts":

```<code>import React from 'react';
import Contacts from './contact'
import { BrowserRouter as Router, } from "react-router-dom";
import { render, screen } from '@testing-library/react';
import { createMemoryHistory } from "history";

const {
    ContactPage
} = Contacts();

test("renders location state", () => {
    const history = createMemoryHistory();

    render(
      <Router history={history}>
        <ContactPage />
      </Router>
    );

  });
</code>```

The Error:

```<code> TypeError: Cannot read property 'state' of undefined

    > 29 |             pack: props && props.location.state && props.location.state.hasOwnProperty('package')
         |                                           ^
      30 |                 ? props.location.state.package
      31 |                 : ''
      32 |         });
</code>```

答:
Try adding history.push = jest.fn() right below const history = createMemoryHistory()

If you want to test the history is updated, you'd do:
expect(history.push).toHaveBeenCalledWith('foo/bar/whatever')

Title: React Testing with history passed

Body:
Problem Statement

I'm using create react app with Jest. In my test, I simply want to render out a component that needs history passed to the state before it can load. I've been researching and trying to get to this to work for awhile.
The source I've been using shows how to pass the history to the router - https://testing-library.com/docs/example-react-router/

When running the test an error says

"Cannot read property 'state' of undefined"

I believe it's because the history isn't being properly passed in during the test.

I'm really new to testing and am hoping someone can point me in the right direction to having it render the component during the test successfully. Thanks!

Code

The rendered component:

const [data, setData] = useState({
            pack: props && props.location.state && props.location.state.hasOwnProperty('package')
                ? props.location.state.package
                : ''
        });

const ContactPage = (props) => (
    Contact &&
    <Layout>

                    {Contact(props)}

    </Layout>
);

The test component "attempts":

import React from 'react';
import Contacts from './contact'
import { BrowserRouter as Router, } from "react-router-dom";
import { render, screen } from '@testing-library/react';
import { createMemoryHistory } from "history";

const {
    ContactPage
} = Contacts();

test("renders location state", () => {
    const history = createMemoryHistory();

    render(
      <Router history={history}>
        <ContactPage />
      </Router>
    );

  });

The Error:

 TypeError: Cannot read property 'state' of undefined

    > 29 |             pack: props && props.location.state && props.location.state.hasOwnProperty('package')
         |                                           ^
      30 |                 ? props.location.state.package
      31 |                 : ''
      32 |         });


问:
matrices are not aligned error message
I have the following dataframe of returns

```<code>ret
Out[3]:
Symbol            FX      OGDC       PIB       WTI
Date
2010-03-02  0.000443  0.006928  0.000000  0.012375
2010-03-03 -0.000690 -0.007873  0.000171  0.014824
2010-03-04 -0.001354  0.001545  0.000007 -0.008195
2010-03-05 -0.001578  0.008796 -0.000164  0.015955
</code>```

And the following weights for each symbol:

```<code>df3
Out[4]:
  Symbol    Weight
0   OGDC  0.182022
1    WTI  0.534814
2     FX  0.131243
3    PIB  0.151921
</code>```

I am trying to get a weighted return for each day and tried:

```<code>port_ret = ret.dot(df3)
</code>```

but I get the following error message:

```<code>ValueError: matrices are not aligned
</code>```

My objective is to have a weighted return for each date such that, for example 2010-03-02 would be as follows:

```<code>weighted_ret = 0.000443*.131243+.006928*.182022+0.000*0.151921+0.012375*.534814 = 0.007937512
</code>```

I am not sure why I am getting this error but would be very happy for an alternative solution to the weighted return

答:
For a dot product of dataframe dfA by dataframe dfB, the column names of dfA must coincide with the index of dfB, otherwise you'll get the error 'ValueError: matrices are not aligned'</p>
```<code>dfA = pd.DataFrame( data = [[1, 2], [3, 4], [5, 6]], columns=['one', 'two'])
dfB = pd.DataFrame( data = [[1, 2, 3], [4, 5, 6]], index=['one', 'two'])
dfA.dot(dfB)
</code>```

Title: matrices are not aligned error message

Body:
I have the following dataframe of returns

ret
Out[3]:
Symbol            FX      OGDC       PIB       WTI
Date
2010-03-02  0.000443  0.006928  0.000000  0.012375
2010-03-03 -0.000690 -0.007873  0.000171  0.014824
2010-03-04 -0.001354  0.001545  0.000007 -0.008195
2010-03-05 -0.001578  0.008796 -0.000164  0.015955

And the following weights for each symbol:

df3
Out[4]:
  Symbol    Weight
0   OGDC  0.182022
1    WTI  0.534814
2     FX  0.131243
3    PIB  0.151921

I am trying to get a weighted return for each day and tried:

port_ret = ret.dot(df3)

but I get the following error message:

ValueError: matrices are not aligned

My objective is to have a weighted return for each date such that, for example 2010-03-02 would be as follows:

weighted_ret = 0.000443*.131243+.006928*.182022+0.000*0.151921+0.012375*.534814 = 0.007937512

I am not sure why I am getting this error but would be very happy for an alternative solution to the weighted return

Body:
For a dot product of dataframe dfA by dataframe dfB, the column names of dfA must coincide with the index of dfB, otherwise you'll get the error 'ValueError: matrices are not aligned'

dfA = pd.DataFrame( data = [[1, 2], [3, 4], [5, 6]], columns=['one', 'two'])
dfB = pd.DataFrame( data = [[1, 2, 3], [4, 5, 6]], index=['one', 'two'])
dfA.dot(dfB)


问:
How to enhance the speed of my C++ program in reading delimited text files?
I show you C# and C++ code that execute the same job: to read the same text file delimited by “|” and save  with “#” delimited text.

When I execute  C++ program, the time elapsed is 169 seconds.

UPDATE 1: Thanks to Seth (compilation with: cl /EHsc /Ox /Ob2 /Oi) and GWW for changing the positions of string s outside the loops, the elapsed time was reduced to 53 seconds. I updated the code also.

UPDATE 2: Do you have any other suggestion to enhace the C++ code?

When I execute the C# program, the elapsed time is 34 seconds!

The question is, how can I enhance the speed of C++ comparing with the C# one?

C++ Program:

```<code>int main ()
{
    Timer t;
    cout << t.ShowStart() << endl;

    ifstream input("in.txt");
    ofstream output("out.txt", ios::out);
    char const row_delim = '\n';
    char const field_delim = '|';
    string s1, s2;

    while (input)
    {
        if (!getline( input, s1, row_delim ))
            break;
        istringstream iss(s1);
        while (iss)
        {
            if (!getline(iss, s2, field_delim ))
                break;
            output << s2 << "#";
        }
        output << "\n";
    }

    t.Stop();
    cout << t.ShowEnd() << endl;
    cout << "Executed in: " << t.ElapsedSeconds() << " seconds." << endl;
    return 0;
}
</code>```

C# program:

```<code>    static void Main(string[] args)
    {
        long i;
        Stopwatch sw = new Stopwatch();
        Console.WriteLine(DateTime.Now);
        sw.Start();
        StreamReader sr = new StreamReader("in.txt", Encoding.Default);
        StreamWriter wr = new StreamWriter("out.txt", false, Encoding.Default);
        object[] cols = new object[0];  // allocates more elements automatically when filling
        string line;
        while (!string.Equals(line = sr.ReadLine(), null)) // Fastest way
        {
        cols = line.Split('|');  // Faster than using a List<>
        foreach (object col in cols)
            wr.Write(col + "#");
        wr.WriteLine();
        }
        sw.Stop();
        Console.WriteLine("Conteo tomó {0} secs", sw.Elapsed);
        Console.WriteLine(DateTime.Now);
    }
</code>```

UPDATE 3:

Well, I must say I am very happy for the help received and because the answer to my question has been satisfied.

I changed the text of the question a little to be more specific, and I tested the solutions that kindly raised Molbdlino and Bo Persson.

Keeping Seth indications for the compile command (i.e. cl /EHsc /Ox /Ob2 /Oi pgm.cpp):

Bo Persson's solution took 18 seconds on average to complete the execution, really a good one taking in account that the code is near to what I like).

Molbdlino solution took 6 seconds on average, really amazing!! (thanks to Constantine also).

Never too late to learn, and I learned valuable things with my question.

My best regards.

答:
How about this for the central loop</p>

```<code>while (getline( input, s1, row_delim ))
{
    for (string::iterator c = s1.begin(); c != s1.end(); ++c)
        if (*c == field_delim)
            *c = '#';

    output << s1 << '\n';
}
</code>```

Body:
I show you C# and C++ code that execute the same job: to read the same text file delimited by “|” and save  with “#” delimited text.

When I execute  C++ program, the time elapsed is 169 seconds.

UPDATE 1: Thanks to Seth (compilation with: cl /EHsc /Ox /Ob2 /Oi) and GWW for changing the positions of string s outside the loops, the elapsed time was reduced to 53 seconds. I updated the code also.

UPDATE 2: Do you have any other suggestion to enhace the C++ code?

When I execute the C# program, the elapsed time is 34 seconds!

The question is, how can I enhance the speed of C++ comparing with the C# one?

C++ Program:

int main ()
{
    Timer t;
    cout << t.ShowStart() << endl;

    ifstream input("in.txt");
    ofstream output("out.txt", ios::out);
    char const row_delim = '\n';
    char const field_delim = '|';
    string s1, s2;

    while (input)
    {
        if (!getline( input, s1, row_delim ))
            break;
        istringstream iss(s1);
        while (iss)
        {
            if (!getline(iss, s2, field_delim ))
                break;
            output << s2 << "#";
        }
        output << "\n";
    }

    t.Stop();
    cout << t.ShowEnd() << endl;
    cout << "Executed in: " << t.ElapsedSeconds() << " seconds." << endl;
    return 0;
}

C# program:

    static void Main(string[] args)
    {
        long i;
        Stopwatch sw = new Stopwatch();
        Console.WriteLine(DateTime.Now);
        sw.Start();
        StreamReader sr = new StreamReader("in.txt", Encoding.Default);
        StreamWriter wr = new StreamWriter("out.txt", false, Encoding.Default);
        object[] cols = new object[0];  // allocates more elements automatically when filling
        string line;
        while (!string.Equals(line = sr.ReadLine(), null)) // Fastest way
        {
        cols = line.Split('|');  // Faster than using a List<>
        foreach (object col in cols)
            wr.Write(col + "#");
        wr.WriteLine();
        }
        sw.Stop();
        Console.WriteLine("Conteo tomó {0} secs", sw.Elapsed);
        Console.WriteLine(DateTime.Now);
    }

UPDATE 3:

Well, I must say I am very happy for the help received and because the answer to my question has been satisfied.

I changed the text of the question a little to be more specific, and I tested the solutions that kindly raised Molbdlino and Bo Persson.

Keeping Seth indications for the compile command (i.e. cl /EHsc /Ox /Ob2 /Oi pgm.cpp):

Bo Persson's solution took 18 seconds on average to complete the execution, really a good one taking in account that the code is near to what I like).

Molbdlino solution took 6 seconds on average, really amazing!! (thanks to Constantine also).

Never too late to learn, and I learned valuable things with my question.

My best regards.

Body:
How about this for the central loop

while (getline( input, s1, row_delim ))
{
    for (string::iterator c = s1.begin(); c != s1.end(); ++c)
        if (*c == field_delim)
            *c = '#';

    output << s1 << '\n';
}


问:
Value Accessor issues on a radio button component
So I'm trying to make just a really simple and clean radio button component that can be used with value accessor to be used in the common angular forms scenarios with FormsModule, ReactiveFormsModule and using changeDetectionStrategy.OnPush but I've ran into the issue(s) you can see ON THIS Stackblitz where I'm getting unexpected results which I understand is probably me missing something since it's been a long time since I've done a custom radio.

When it first loads, you get the radio outlines but no labels until you click something and writeValue and onModelChange are undefined while I get a  No value accessor for form control with name: error. So could use another pair of eyes so I can use the instance with good 'ol ngModel, formControlName, FormBuilder.

You'll see in the Stackblitz the value accessor is there and implementing ControlValueAccessor so I'm a bit confused since I've done the same approach with another component recently just fine. I could copy/paste parts of the blitz here but figured the example should be enough however if not just let me know.

答:
```<code>providers: [RADIO_VALUE_ACCESSOR]
</code>```

You are missing this in your code. You need to add this in providers in component decortor

Body:
providers: [RADIO_VALUE_ACCESSOR]

You are missing this in your code. You need to add this in providers in component decortor


问:
Python Multiprocessing Skip Child Segfault
I'm trying to use multiprocessing for a function that can potentially return a segfault (I have no control over this ATM). In cases where the child process hits a segfault, I want only that child to fail, but all other child tasks to continue/return their results.

I've already switched from multiprocessing.Pool to concurrent.futures.ProcessPoolExecutor avoid the issue of the child process hanging forever (or until an arbitrary timeout) as documented in this bug: https://bugs.python.org/issue22393.

However the issue I face now, is that when the first child task hits a segfault, all in-flight child processes get marked as broken (concurrent.futures.process.BrokenProcessPool).

Is there a way to only mark actually broken child processes as broken?

Code I'm running in Python 3.7.4:

```<code>import concurrent.futures
import ctypes
from time import sleep

def do_something(x):
    print(f"{x}; in do_something")
    sleep(x*3)
    if x == 2:
        # raise a segmentation fault internally
        return x, ctypes.string_at(0)
    return x, x-1

nums = [1, 2, 3, 1.5]
executor = concurrent.futures.ProcessPoolExecutor()
result_futures = []
for num in nums:
    # Using submit with a list instead of map lets you get past the first exception
    # Example: https://stackoverflow.com/a/53346191/7619676
    future = executor.submit(do_something, num)
    result_futures.append(future)

# Wait for all results
concurrent.futures.wait(result_futures)

# After a segfault is hit for any child process (i.e. is "terminated abruptly"), the process pool becomes unusable
# and all running/pending child processes' results are set to broken
for future in result_futures:
    try:
        print(future.result())
    except concurrent.futures.process.BrokenProcessPool:
        print("broken")
</code>```

Result:

```<code>(1, 0)
broken
broken
(1.5, 0.5)
</code>```

Desired result:

```<code>(1, 0)
broken
(3, 2)
(1.5, 0.5)
</code>```

答:
Based on @Richard Sheridan's answer, I ended up using the code below. This version doesn't require setting a timeout, which is something I couldn't do for my use case.</p>
```<code>import ctypes
import multiprocessing
from typing import List
from time import sleep

def do_something(x, result):
    print(f"{x} starting")
    sleep(x * 3)
    if x == 2:
        # raise a segmentation fault internally
        y = ctypes.string_at(0)
    y = x
    print(f"{x} done")
    results_queue.put(y)

def wait_for_process_slot(
    processes: List,
    concurrency: int = multiprocessing.cpu_count() - 1,
    wait_sec: int = 1,
) -> int:
    """Blocks main process if `concurrency` processes are already running.

    Alternative to `multiprocessing.Semaphore.acquire`
    useful for when child processes might fail and not be able to signal.
    Relies instead on the main's (parent's) tracking of `multiprocessing.Process`es.

    """
    counter = 0
    while True:
        counter = sum([1 for i, p in processes.items() if p.is_alive()])
        if counter < concurrency:
            return counter
        sleep(wait_sec)

if __name__ == "__main__":
    # "spawn" results in an OSError b/c pickling a segfault fails?
    ctx = multiprocessing.get_context()
    manager = ctx.Manager()
    results_queue = manager.Queue(maxsize=-1)

    concurrency = multiprocessing.cpu_count() - 1  # reserve 1 CPU for waiting
    nums = [3, 1, 2, 1.5]
    all_processes = {}
    for idx, num in enumerate(nums):
        num_running_processes = wait_for_process_slot(all_processes, concurrency)

        p = ctx.Process(target=do_something, args=(num, results_queue), daemon=True)
        all_processes.update({idx: p})
        p.start()

    # Wait for the last batch of processes not blocked by wait_for_process_slot to finish
    for p in all_processes.values():
        p.join()

    # Check last batch of processes for bad processes
    # Relies on all processes having finished (the p.joins above)
    bad_nums = [idx for idx, p in all_processes.items() if p.exitcode != 0]
</code>```

Body:
I'm trying to use multiprocessing for a function that can potentially return a segfault (I have no control over this ATM). In cases where the child process hits a segfault, I want only that child to fail, but all other child tasks to continue/return their results.

I've already switched from multiprocessing.Pool to concurrent.futures.ProcessPoolExecutor avoid the issue of the child process hanging forever (or until an arbitrary timeout) as documented in this bug: https://bugs.python.org/issue22393.

However the issue I face now, is that when the first child task hits a segfault, all in-flight child processes get marked as broken (concurrent.futures.process.BrokenProcessPool).

Is there a way to only mark actually broken child processes as broken?

Code I'm running in Python 3.7.4:

import concurrent.futures
import ctypes
from time import sleep

def do_something(x):
    print(f"{x}; in do_something")
    sleep(x*3)
    if x == 2:
        # raise a segmentation fault internally
        return x, ctypes.string_at(0)
    return x, x-1

nums = [1, 2, 3, 1.5]
executor = concurrent.futures.ProcessPoolExecutor()
result_futures = []
for num in nums:
    # Using submit with a list instead of map lets you get past the first exception
    # Example: https://stackoverflow.com/a/53346191/7619676
    future = executor.submit(do_something, num)
    result_futures.append(future)

# Wait for all results
concurrent.futures.wait(result_futures)

# After a segfault is hit for any child process (i.e. is "terminated abruptly"), the process pool becomes unusable
# and all running/pending child processes' results are set to broken
for future in result_futures:
    try:
        print(future.result())
    except concurrent.futures.process.BrokenProcessPool:
        print("broken")

Result:

(1, 0)
broken
broken
(1.5, 0.5)

Desired result:

(1, 0)
broken
(3, 2)
(1.5, 0.5)

Body:
Based on @Richard Sheridan's answer, I ended up using the code below. This version doesn't require setting a timeout, which is something I couldn't do for my use case.

import ctypes
import multiprocessing
from typing import List
from time import sleep

def do_something(x, result):
    print(f"{x} starting")
    sleep(x * 3)
    if x == 2:
        # raise a segmentation fault internally
        y = ctypes.string_at(0)
    y = x
    print(f"{x} done")
    results_queue.put(y)

def wait_for_process_slot(
    processes: List,
    concurrency: int = multiprocessing.cpu_count() - 1,
    wait_sec: int = 1,
) -> int:
    """Blocks main process if `concurrency` processes are already running.

    Alternative to `multiprocessing.Semaphore.acquire`
    useful for when child processes might fail and not be able to signal.
    Relies instead on the main's (parent's) tracking of `multiprocessing.Process`es.

    """
    counter = 0
    while True:
        counter = sum([1 for i, p in processes.items() if p.is_alive()])
        if counter < concurrency:
            return counter
        sleep(wait_sec)

if __name__ == "__main__":
    # "spawn" results in an OSError b/c pickling a segfault fails?
    ctx = multiprocessing.get_context()
    manager = ctx.Manager()
    results_queue = manager.Queue(maxsize=-1)

    concurrency = multiprocessing.cpu_count() - 1  # reserve 1 CPU for waiting
    nums = [3, 1, 2, 1.5]
    all_processes = {}
    for idx, num in enumerate(nums):
        num_running_processes = wait_for_process_slot(all_processes, concurrency)

        p = ctx.Process(target=do_something, args=(num, results_queue), daemon=True)
        all_processes.update({idx: p})
        p.start()

    # Wait for the last batch of processes not blocked by wait_for_process_slot to finish
    for p in all_processes.values():
        p.join()

    # Check last batch of processes for bad processes
    # Relies on all processes having finished (the p.joins above)
    bad_nums = [idx for idx, p in all_processes.items() if p.exitcode != 0]


问:
jq How to filter array and add element
using jq i'm trying to add data to a specific element in my json below :

```<code>{
  "users": [
    {
      "username": "karim",
      "queue": [
        "default"
      ]
    },
    {
      "username": "admin",
      "queue": [
        "apps",
        "prod"
      ]
    }
  ]
}
</code>```

what i want to do is to add items in queue[] of user admin like this

```<code>{
  "users": [
    {
      "username": "hive",
      "queue": [
        "default"
      ]
    },
    {
      "username": "admin",
      "queue": [
        "apps",
        "prod",
        "dev"
      ]
    }
  ]
}
</code>```

This is the command i used

```<code>jq '.users[] | select(.username == "admin").queue += ["dev"]' file.json
</code>```

But the result is not as expected

```<code>{
  "username": "hive",
  "queue": [
    "default"
  ]
}
{
  "username": "admin",
  "queue": [
    "apps",
    "prod",
    "dev"
  ]
}
</code>```

Why users array doesn't appear ? I need to keep it in the result

答:
With the pipe you are changing the context down to an array element, which is what you want for the selection. If you put parentheses around the pipe and the selection, you will keep the assignment and thus the filter's output on top-level:

jq '(.users[] | select(.username == "admin")).queue += ["dev"]'
```
<pre class="lang-json prettyprint-override"><code>{
  "users": [
    {
      "username": "karim",
      "queue": [
        "default"
      ]
    },
    {
      "username": "admin",
      "queue": [
        "apps",
        "prod",
        "dev"
      ]
    }
  ]
}
</code>```

Demo

Title: jq How to filter array and add element

Body:
using jq i'm trying to add data to a specific element in my json below :

{
  "users": [
    {
      "username": "karim",
      "queue": [
        "default"
      ]
    },
    {
      "username": "admin",
      "queue": [
        "apps",
        "prod"
      ]
    }
  ]
}

what i want to do is to add items in queue[] of user admin like this

{
  "users": [
    {
      "username": "hive",
      "queue": [
        "default"
      ]
    },
    {
      "username": "admin",
      "queue": [
        "apps",
        "prod",
        "dev"
      ]
    }
  ]
}

This is the command i used

jq '.users[] | select(.username == "admin").queue += ["dev"]' file.json

But the result is not as expected

{
  "username": "hive",
  "queue": [
    "default"
  ]
}
{
  "username": "admin",
  "queue": [
    "apps",
    "prod",
    "dev"
  ]
}

Why users array doesn't appear ? I need to keep it in the result

Body:
With the pipe you are changing the context down to an array element, which is what you want for the selection. If you put parentheses around the pipe and the selection, you will keep the assignment and thus the filter's output on top-level:

jq '(.users[] | select(.username == "admin")).queue += ["dev"]'

{
  "users": [
    {
      "username": "karim",
      "queue": [
        "default"
      ]
    },
    {
      "username": "admin",
      "queue": [
        "apps",
        "prod",
        "dev"
      ]
    }
  ]
}

Demo


问:
linphone-iOS CallKit crashed in background
I am using the linphone sdk on iOS

I used PushKit to receive incoming calls and used CallKit to display calls

Callkit works properly when the app is in the foreground
But when the app is completely closed, the app crashes when it receives the incoming push.

In the didReceiveIncomingPushWith method, when the push is received, I immediately call reportNewIncomingCall
But it crashes in the background.

答: On iOS 13.0 and above, VoIP push notifications must be handled by call kit. Receiving a VoIP push and not handling it through call kit result in your app crashing. This behavior is intended.</p>


问:
How to use deleteById jpa repository if my id is a String?
I need to delete an entity using its ID, tried using JPA repository delete by id method

```<code> productRepository.deleteById(id);
</code>```

Where "id" is supposed to be Long- If passed string value will give an error: Long expected

```<code>Sample id= "402880f17ebd7e44017ebd7fe9ee0000"
</code>```

Hence I can't convert the String to Long

Entity class

```<code>@Entity
@Table(name = "products")
public class Product {

@Id @GeneratedValue(generator = "system-uuid")
@GenericGenerator(name="system-uuid",strategy = "uuid")
@Column(name="product_id")
private String id;

@Column(name="price")
@NotNull
private float productPrice;

@Column(name="product_name")
@NotNull
private String productName;

@Column(name="description")
private String productDesc;

@Column(name="stock_available")
private int productStock;

@ManyToOne(optional = false)
@JoinColumn(name = "shop_id")
private Shop shop;

public Product(){
}

public Product(String id, float productPrice, String productName, String productDesc, int productStock, Shop shop) {
    this.id = id;
    this.productPrice = productPrice;
    this.productName = productName;
    this.productDesc = productDesc;
    this.productStock = productStock;
    this.shop = shop;
}
}
</code>```

Repository Class:

```<code>@Repository
public interface ProductRepository extends JpaRepository<Product,Long>{
}
</code>```

答:
In your productRepository change the second generic type on the interface from long to string. So Change your code snippet

```<code>@Repository
public interface ProductRepository extends JpaRepository<Product,Long> {}
</code>```

to:

```<code>@Repository
public interface ProductRepository extends JpaRepository<Product,String> {}
</code>```

Also, you don't need @Repository for Spring data JPA interfaces.

Body:
I need to delete an entity using its ID, tried using JPA repository delete by id method

 productRepository.deleteById(id);

Where "id" is supposed to be Long- If passed string value will give an error: Long expected

Sample id= "402880f17ebd7e44017ebd7fe9ee0000"

Hence I can't convert the String to Long

Entity class

@Entity
@Table(name = "products")
public class Product {

@Id @GeneratedValue(generator = "system-uuid")
@GenericGenerator(name="system-uuid",strategy = "uuid")
@Column(name="product_id")
private String id;

@Column(name="price")
@NotNull
private float productPrice;

@Column(name="product_name")
@NotNull
private String productName;

@Column(name="description")
private String productDesc;

@Column(name="stock_available")
private int productStock;

@ManyToOne(optional = false)
@JoinColumn(name = "shop_id")
private Shop shop;

public Product(){
}

public Product(String id, float productPrice, String productName, String productDesc, int productStock, Shop shop) {
    this.id = id;
    this.productPrice = productPrice;
    this.productName = productName;
    this.productDesc = productDesc;
    this.productStock = productStock;
    this.shop = shop;
}
}

Repository Class:

@Repository
public interface ProductRepository extends JpaRepository<Product,Long>{
}

Body:
In your productRepository change the second generic type on the interface from long to string. So Change your code snippet

@Repository
public interface ProductRepository extends JpaRepository<Product,Long> {}

to:

@Repository
public interface ProductRepository extends JpaRepository<Product,String> {}

Also, you don't need @Repository for Spring data JPA interfaces.


问:
Perl backticks using bash
In Perl, the default shell to execute backticks is sh. I'd like to switch to use bash for its richer syntax. So far I found that the suggested solution is

```<code>`bash -c \"echo a b\"`
</code>```

The apparent drawback is that the escaped double quotes, which means I will have difficulty to use double quotes in my bash args. For example, if I wanted to run commands requiring double quotes in bash

```<code>echo "a'b"
</code>```

The above method will be very awkward.

Perl's system() call has a solution for this problem: to use ARRAY args,

```<code>system("bash", "-c", qq(echo "a'b"));
</code>```

This keeps my original bash command unmodified, and almost always.

I'd like to use ARRAY args in backticks too. Is it possible?

答:
Capture::Tiny is a very nice option: as the SYNOPSIS shows, you can do

use Capture::Tiny 'capture';
my ($output, $error_output, $exit_code) = capture {
    system(@whatever);
};
```

as well as using system inside capture_stdout if you want the simpler behavior of backticks.

Plus it's very general-purpose, working on Perl code (even Perl code that does weird stuff) as well as external programs, so it's a good thing to have in your toolbox.

Title: Perl backticks using bash

Body:
In Perl, the default shell to execute backticks is sh. I'd like to switch to use bash for its richer syntax. So far I found that the suggested solution is

`bash -c \"echo a b\"`

The apparent drawback is that the escaped double quotes, which means I will have difficulty to use double quotes in my bash args. For example, if I wanted to run commands requiring double quotes in bash

echo "a'b"

The above method will be very awkward.

Perl's system() call has a solution for this problem: to use ARRAY args,

system("bash", "-c", qq(echo "a'b"));

This keeps my original bash command unmodified, and almost always.

I'd like to use ARRAY args in backticks too. Is it possible?

Body:
Capture::Tiny is a very nice option: as the SYNOPSIS shows, you can do

use Capture::Tiny 'capture';
my ($output, $error_output, $exit_code) = capture {
    system(@whatever);
};

as well as using system inside capture_stdout if you want the simpler behavior of backticks.

Plus it's very general-purpose, working on Perl code (even Perl code that does weird stuff) as well as external programs, so it's a good thing to have in your toolbox.


问:
Custom Loss Function - Keras
I am developing a regression model to predict cryptocurrency prices, and I have created a simple loss function. The idea is simple the Y target is the price change from a certain lookup window, so either the values are positive or negative. The idea is to first apply an mae loss function and then penalize where y_pred is positive and y_true is negative and vice versa. And reduce the loss where y_pred is positive and y_true is also positive and vice versa. Yet when I train with my loss function the precision does not get higher then 0.50 where it gets to around 0.535 with the regular mae loss function. Any idea what could cause this?

The loss function:

```<code>def loss_fn(
    # the loss mode [mae, rmse, mape, huber].
    mode="mae",
    # the threshold.
    threshold=0.0,
    # penalize incorrect predictions (when predicted positive and is negative and reversed) (should be >= 1).
    penalizer=1.0,
    # reduce correct predictions (when predicted positive and is positive and reversed) (should be <= 1).
    reducer=1.0,
):
    def loss_function(y_true, y_pred):
        if mode == "mae":
            loss = keras.losses.MAE(y_true, y_pred)
        elif mode == "rmse":
            loss = K.sqrt(K.mean(K.square(y_pred - y_true)))
        elif mode == "mape":
            loss = keras.losses.mean_absolute_percentage_error(y_true, y_pred)
        elif mode == "huber":
            loss = keras.losses.Huber()(y_true, y_pred)
        if penalizer != 1.0 or reducer != 1.0:
            # apply penalizer.
            mask = tf.where(
                tf.logical_or(
                    tf.logical_and(K.less_equal(y_pred, -1 * threshold), K.greater(y_true, 0.0)),
                    tf.logical_and(K.greater_equal(y_pred, threshold), K.less(y_true, 0.0)),
                ),
                penalizer,
                1.0,
            )[:, 0]
            loss = tf.multiply(loss, mask)
            # apply reducer.
            mask = tf.where(
                tf.logical_or(
                    tf.logical_and(K.less_equal(y_pred, -1 * threshold), K.less(y_true, 0.0)),
                    tf.logical_and(K.greater_equal(y_pred, threshold), K.greater(y_true, 0.0)),
                ),
                reducer,
                1.0,
            )[:, 0]
            loss = tf.multiply(loss, mask)
            loss = tf.math.reduce_mean(loss)
        return loss
    return loss_function
loss = loss_fn(mode="mae", threshold=0.0, penalizer=3.0, reducer=1.0/3.0)
</code>```

Does anyone see any errors or mistakes that could cause this?

Training logs from "mae" as loss:

```<code>Epoch 1/250
2829/2829 [==============================] - 44s 12ms/step - loss: 0.8713 - precision: 0.5311 - val_loss: 0.9731 - val_precision: 0.5343
Epoch 2/250
2829/2829 [==============================] - 32s 11ms/step - loss: 0.8705 - precision: 0.5341 - val_loss: 0.9732 - val_precision: 0.5323
Epoch 3/250
2829/2829 [==============================] - 31s 11ms/step - loss: 0.8702 - precision: 0.5343 - val_loss: 0.9727 - val_precision: 0.5372
Epoch 4/250
2829/2829 [==============================] - 31s 11ms/step - loss: 0.8701 - precision: 0.5345 - val_loss: 0.9730 - val_precision: 0.5336
Epoch 5/250
2829/2829 [==============================] - 32s 11ms/step - loss: 0.8700 - precision: 0.5344 - val_loss: 0.9732 - val_precision: 0.5316
Epoch 6/250
2829/2829 [==============================] - 32s 11ms/step - loss: 0.8699 - precision: 0.5347 - val_loss: 0.9726 - val_precision: 0.5334
Epoch 7/250
2829/2829 [==============================] - 32s 11ms/step - loss: 0.8697 - precision: 0.5346 - val_loss: 0.9731 - val_precision: 0.5331
Epoch 8/250
2829/2829 [==============================] - 32s 11ms/step - loss: 0.8695 - precision: 0.5343 - val_loss: 0.9722 - val_precision: 0.5382
Epoch 9/250
2829/2829 [==============================] - 32s 11ms/step - loss: 0.8693 - precision: 0.5346 - val_loss: 0.9724 - val_precision: 0.5330
Epoch 10/250
2829/2829 [==============================] - 32s 11ms/step - loss: 0.8693 - precision: 0.5345 - val_loss: 0.9732 - val_precision: 0.5331
Epoch 11/250
2829/2829 [==============================] - 32s 11ms/step - loss: 0.8692 - precision: 0.5342 - val_loss: 0.9738 - val_precision: 0.5339
Epoch 12/250
2829/2829 [==============================] - 31s 11ms/step - loss: 0.8690 - precision: 0.5345 - val_loss: 0.9729 - val_precision: 0.5356
Epoch 13/250
2829/2829 [==============================] - 31s 11ms/step - loss: 0.8687 - precision: 0.5350 - val_loss: 0.9728 - val_precision: 0.5342
</code>```

Training logs from the custom loss function (EarlyStopping enabled):

```<code>Epoch 1/250
2829/2829 [==============================] - 42s 12ms/step - loss: 1.4488 - precision: 0.5039 - val_loss: 1.5693 - val_precision: 0.5021
Epoch 2/250
2829/2829 [==============================] - 33s 12ms/step - loss: 1.4520 - precision: 0.5022 - val_loss: 1.6135 - val_precision: 0.5132
Epoch 3/250
2829/2829 [==============================] - 33s 12ms/step - loss: 1.4517 - precision: 0.5019 - val_loss: 1.6874 - val_precision: 0.4983
Epoch 4/250
2829/2829 [==============================] - 33s 12ms/step - loss: 1.4536 - precision: 0.5017 - val_loss: 1.6885 - val_precision: 0.4982
Epoch 5/250
2829/2829 [==============================] - 33s 12ms/step - loss: 1.4513 - precision: 0.5028 - val_loss: 1.6654 - val_precision: 0.5004
Epoch 6/250
2829/2829 [==============================] - 34s 12ms/step - loss: 1.4578 - precision: 0.4997 - val_loss: 1.5716 - val_precision: 0.5019
</code>```

答:
Any idea what could cause this?

Assuming you set the seed for reproducibility, otherwise it could simply be initialization, when you change the loss function you change the domain over which the gradient is going to iterate to optimize your network.

And since you don't have any guarantee that your model is going to reach the global minima but, most likely, will stop at a local minima it coud just mean that, given the same initialization (set seed) the optimization process stopped at a different local minima.

Title: Custom Loss Function - Keras

Body:
I am developing a regression model to predict cryptocurrency prices, and I have created a simple loss function. The idea is simple the Y target is the price change from a certain lookup window, so either the values are positive or negative. The idea is to first apply an mae loss function and then penalize where y_pred is positive and y_true is negative and vice versa. And reduce the loss where y_pred is positive and y_true is also positive and vice versa. Yet when I train with my loss function the precision does not get higher then 0.50 where it gets to around 0.535 with the regular mae loss function. Any idea what could cause this?

The loss function:

def loss_fn(
    # the loss mode [mae, rmse, mape, huber].
    mode="mae",
    # the threshold.
    threshold=0.0,
    # penalize incorrect predictions (when predicted positive and is negative and reversed) (should be >= 1).
    penalizer=1.0,
    # reduce correct predictions (when predicted positive and is positive and reversed) (should be <= 1).
    reducer=1.0,
):
    def loss_function(y_true, y_pred):
        if mode == "mae":
            loss = keras.losses.MAE(y_true, y_pred)
        elif mode == "rmse":
            loss = K.sqrt(K.mean(K.square(y_pred - y_true)))
        elif mode == "mape":
            loss = keras.losses.mean_absolute_percentage_error(y_true, y_pred)
        elif mode == "huber":
            loss = keras.losses.Huber()(y_true, y_pred)
        if penalizer != 1.0 or reducer != 1.0:
            # apply penalizer.
            mask = tf.where(
                tf.logical_or(
                    tf.logical_and(K.less_equal(y_pred, -1 * threshold), K.greater(y_true, 0.0)),
                    tf.logical_and(K.greater_equal(y_pred, threshold), K.less(y_true, 0.0)),
                ),
                penalizer,
                1.0,
            )[:, 0]
            loss = tf.multiply(loss, mask)
            # apply reducer.
            mask = tf.where(
                tf.logical_or(
                    tf.logical_and(K.less_equal(y_pred, -1 * threshold), K.less(y_true, 0.0)),
                    tf.logical_and(K.greater_equal(y_pred, threshold), K.greater(y_true, 0.0)),
                ),
                reducer,
                1.0,
            )[:, 0]
            loss = tf.multiply(loss, mask)
            loss = tf.math.reduce_mean(loss)
        return loss
    return loss_function
loss = loss_fn(mode="mae", threshold=0.0, penalizer=3.0, reducer=1.0/3.0)

Does anyone see any errors or mistakes that could cause this?

Training logs from "mae" as loss:

Epoch 1/250
2829/2829 [==============================] - 44s 12ms/step - loss: 0.8713 - precision: 0.5311 - val_loss: 0.9731 - val_precision: 0.5343
Epoch 2/250
2829/2829 [==============================] - 32s 11ms/step - loss: 0.8705 - precision: 0.5341 - val_loss: 0.9732 - val_precision: 0.5323
Epoch 3/250
2829/2829 [==============================] - 31s 11ms/step - loss: 0.8702 - precision: 0.5343 - val_loss: 0.9727 - val_precision: 0.5372
Epoch 4/250
2829/2829 [==============================] - 31s 11ms/step - loss: 0.8701 - precision: 0.5345 - val_loss: 0.9730 - val_precision: 0.5336
Epoch 5/250
2829/2829 [==============================] - 32s 11ms/step - loss: 0.8700 - precision: 0.5344 - val_loss: 0.9732 - val_precision: 0.5316
Epoch 6/250
2829/2829 [==============================] - 32s 11ms/step - loss: 0.8699 - precision: 0.5347 - val_loss: 0.9726 - val_precision: 0.5334
Epoch 7/250
2829/2829 [==============================] - 32s 11ms/step - loss: 0.8697 - precision: 0.5346 - val_loss: 0.9731 - val_precision: 0.5331
Epoch 8/250
2829/2829 [==============================] - 32s 11ms/step - loss: 0.8695 - precision: 0.5343 - val_loss: 0.9722 - val_precision: 0.5382
Epoch 9/250
2829/2829 [==============================] - 32s 11ms/step - loss: 0.8693 - precision: 0.5346 - val_loss: 0.9724 - val_precision: 0.5330
Epoch 10/250
2829/2829 [==============================] - 32s 11ms/step - loss: 0.8693 - precision: 0.5345 - val_loss: 0.9732 - val_precision: 0.5331
Epoch 11/250
2829/2829 [==============================] - 32s 11ms/step - loss: 0.8692 - precision: 0.5342 - val_loss: 0.9738 - val_precision: 0.5339
Epoch 12/250
2829/2829 [==============================] - 31s 11ms/step - loss: 0.8690 - precision: 0.5345 - val_loss: 0.9729 - val_precision: 0.5356
Epoch 13/250
2829/2829 [==============================] - 31s 11ms/step - loss: 0.8687 - precision: 0.5350 - val_loss: 0.9728 - val_precision: 0.5342

Training logs from the custom loss function (EarlyStopping enabled):

Epoch 1/250
2829/2829 [==============================] - 42s 12ms/step - loss: 1.4488 - precision: 0.5039 - val_loss: 1.5693 - val_precision: 0.5021
Epoch 2/250
2829/2829 [==============================] - 33s 12ms/step - loss: 1.4520 - precision: 0.5022 - val_loss: 1.6135 - val_precision: 0.5132
Epoch 3/250
2829/2829 [==============================] - 33s 12ms/step - loss: 1.4517 - precision: 0.5019 - val_loss: 1.6874 - val_precision: 0.4983
Epoch 4/250
2829/2829 [==============================] - 33s 12ms/step - loss: 1.4536 - precision: 0.5017 - val_loss: 1.6885 - val_precision: 0.4982
Epoch 5/250
2829/2829 [==============================] - 33s 12ms/step - loss: 1.4513 - precision: 0.5028 - val_loss: 1.6654 - val_precision: 0.5004
Epoch 6/250
2829/2829 [==============================] - 34s 12ms/step - loss: 1.4578 - precision: 0.4997 - val_loss: 1.5716 - val_precision: 0.5019


问:
How to create a PowerShell alias for [System.Web.HttpUtility]::UrlDecode()?
I already figured out how to use UrlDecode in PowerShell:

```<code>PS Z:\> Add-Type -AssemblyName System.Web
PS Z:\> [System.Web.HttpUtility]::UrlDecode("First+Line%0D%0ASecond+Line ")
</code>```

I also figured out that I can load System.Web by adding corresponding command to "C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1" so I don't have to do this manually each time I open PowerShell.

However, I still have to type [System.Web.HttpUtility]::UrlDecode each time which is a hassle even when using tab to auto-complete.

Is there a way to create an alias so that instead of

```<code>[System.Web.HttpUtility]::UrlDecode("First+Line%0D%0ASecond+Line")
</code>```

I can just type

```<code>urdc("First+Line%0D%0ASecond+Line")
</code>```

or better yet

```<code>urdc "First+Line%0D%0ASecond+Line"
</code>```

?

答:
Here is how you can create a simple wrapper function using automatic variables $input and $args:

```<code>function urdc {
    if($MyInvocation.ExpectingInput) {
        return $input.ForEach({
            [System.Web.HttpUtility]::UrlDecode($_)
        })
    }
    $args.ForEach({
        [System.Web.HttpUtility]::UrlDecode($_)
    })
}
</code>```

Now you could use it like:

```<code>urdc "First+Line%0D%0ASecond+Line" "First+Line%0D%0ASecond+Line"
</code>```

Or from pipeline:

```<code>"First+Line%0D%0ASecond+Line", "First+Line%0D%0ASecond+Line" | urdc
</code>```

Body:
I already figured out how to use UrlDecode in PowerShell:

PS Z:\> Add-Type -AssemblyName System.Web
PS Z:\> [System.Web.HttpUtility]::UrlDecode("First+Line%0D%0ASecond+Line ")

I also figured out that I can load System.Web by adding corresponding command to "C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1" so I don't have to do this manually each time I open PowerShell.

However, I still have to type [System.Web.HttpUtility]::UrlDecode each time which is a hassle even when using tab to auto-complete.

Is there a way to create an alias so that instead of

[System.Web.HttpUtility]::UrlDecode("First+Line%0D%0ASecond+Line")

I can just type

urdc("First+Line%0D%0ASecond+Line")

or better yet

urdc "First+Line%0D%0ASecond+Line"

?

Body:
Here is how you can create a simple wrapper function using automatic variables $input and $args:

function urdc {
    if($MyInvocation.ExpectingInput) {
        return $input.ForEach({
            [System.Web.HttpUtility]::UrlDecode($_)
        })
    }
    $args.ForEach({
        [System.Web.HttpUtility]::UrlDecode($_)
    })
}

Now you could use it like:

urdc "First+Line%0D%0ASecond+Line" "First+Line%0D%0ASecond+Line"

Or from pipeline:

"First+Line%0D%0ASecond+Line", "First+Line%0D%0ASecond+Line" | urdc


问:
GCP Uptime Checks to check private / internal endpoints
I was wondering if anyone else has come across this problem, and if so, what solution was chosen.

We currently use GCP Uptime Checks for our internet facing endpoints - however, we do want to also use them to check non-internet facing endpoints (i.e locked down by a security group for example).

Already considered:

Whitelist the GCP Uptime IPs - this was dismissed as we do not want to maintain a mass amount of IPs on our security groups - which may or may not change over time.

Use GCPs new Private Uptime Checks - this only supports checking endpoints residing in GCP (we need to also check private endpoints in AWS)

Set up a reverse NGINX Proxy VM - which we can lock down to GCP Uptime IPs (in one place, as opposed to multiple) - our services can then just whitelist the Proxy VM

Try and achieve the same logic using the NGINX Ingress Controller in GKE

Has anyone else faced this problem?

Thanks in advance!

答:
You could Create private uptime checks for your GCP endpoints and use an AWS connector project to import the metrics from your AWS endpoints.

Follow these steps provided in the Quickstart for Amazon EC2 guide to monitor your AWS endpoints:

Create a Google Cloud project

Connect an AWS account

Authorize AWS applications

Use Monitoring services with AWS