问:
When GC.KeepAlive(this) is needed when doing P/Invoke on unmanaged resources?
I have a TestNet wrapper for a native component. The native component exposes a blocking TestNative::Foo() that communicates with managed part through calling managed callbacks and a weak GCHandle that is used to retrieve the reference to the .NET wrapper and provides a context. The GCHandle is weak since the .NET wrapper is meant to hide the fact that is handling unmanaged resources to user and deliberately doesn't implement the IDisposable interface: being non weak it would prevent TestNet instances from being collected at all, creating a memory leak. What's happening is that in Release build only the garbage collector will collect reference to .NET wrapper while executing the managed callback, even before both TestNative::Foo() and surprisingly TestNet::Foo() unblocks. I understood the problem my self and I can fix it by issuing a GC.KeepAlive(this) after the P/Invoke call but since the knowledge of this is not very widespread, it seems a lot of people are doing it wrong. I have few questions:

Is GC.KeepAlive(this) always needed in a managed method if last instruction is a P/Invoke call on unmanaged resources or it's just needed in this special case, namely the switch to managed execution context while marshaling the managed callback from native code? The question could be: should I put GC.KeepAlive(this) everywhere? This old microsoft blog (original link is 404, here is cached) seems to suggest so! But this would be game changer and basically it would mean that most people never did P/Invoke correctly, because this would require reviewing most P/Invoke calls in wrappers. Is there for example a rule that say that garbage collector (EDIT: or better the finalizer) can't run for objects that belong to the current thread while execution context is unamanaged (native)?

Where I can find proper documentation? I could find CodeAnalysis policy CA2115 pointing to generically use GC.KeepAlive(this) any time a unmanaged resource is accessed with P/Invoke. In general GC.KeepAlive(this) seems to be very rarely needed when dealing with finalizers.

Why is this happening only in Release build? It looks like an optimization but not being needed at all in Debug build hides an important behavior of the garbage collector.

NOTE: I have no problem with delegates being collected, that is a different issue which I know how to handle properly. The issue here is with objects holding unmanaged resources being collected when P/Invoke calls are not finished yet.

It follows code that clearly manifest the problem. Creates a C# console application and a C++ Dll1 project and build them in Release mode:

Program.cs:

```<code>using System;
using System.Runtime.InteropServices;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var test = new TestNet();
            try
            {
                test.Foo();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
    }

    class TestNet
    {
        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
        delegate void Callback(IntPtr data);

        static Callback _callback;

        IntPtr _nativeHandle;
        GCHandle _thisHandle;

        static TestNet()
        {
            // NOTE: Keep delegates references so they can be
            // stored persistently in unmanaged resources
            _callback = callback;
        }

        public TestNet()
        {
            _nativeHandle = CreateTestNative();

            // Keep a weak handle to self. Weak is necessary
            // to not prevent garbage collection of TestNet instances
            _thisHandle = GCHandle.Alloc(this, GCHandleType.Weak);

            TestNativeSetCallback(_nativeHandle, _callback, GCHandle.ToIntPtr(_thisHandle));
        }

        ~TestNet()
        {
            Console.WriteLine("this.~TestNet()");
            FreeTestNative(_nativeHandle);
            _thisHandle.Free();
        }

        public void Foo()
        {
            Console.WriteLine("this.Foo() begins");
            TestNativeFoo(_nativeHandle);

            // This is never printed when the object is collected!
            Console.WriteLine("this.Foo() ends");

            // Without the following GC.KeepAlive(this) call
            // in Release build the program will consistently collect
            // the object in callback() and crash on next iteration
            //GC.KeepAlive(this);
        }

        static void callback(IntPtr data)
        {
            Console.WriteLine("TestNet.callback() begins");
            // Retrieve the weak reference to self. As soon as the istance
            // of TestNet exists.
            var self = (TestNet)GCHandle.FromIntPtr(data).Target;
            self.callback();

            // Enforce garbage collection. On release build
            self = null;
            GC.Collect();
            GC.WaitForPendingFinalizers();
            Console.WriteLine("TestNet.callback() ends");
        }

        void callback()
        {
            Console.WriteLine("this.callback()");
        }

        [DllImport("Dll1", CallingConvention = CallingConvention.Cdecl)]
        static extern IntPtr CreateTestNative();

        [DllImport("Dll1", CallingConvention = CallingConvention.Cdecl)]
        static extern void FreeTestNative(IntPtr obj);

        [DllImport("Dll1", CallingConvention = CallingConvention.Cdecl)]
        static extern void TestNativeSetCallback(IntPtr obj, Callback callback, IntPtr data);

        [DllImport("Dll1", CallingConvention = CallingConvention.Cdecl)]
        static extern void TestNativeFoo(IntPtr obj);
    }
}
</code>```

Dll1.cpp:

```<code>#include <iostream>

extern "C" typedef void (*Callback)(void *data);

class TestNative
{
public:
    void SetCallback(Callback callback1, void *data);
    void Foo();
private:
    Callback m_callback;
    void *m_data;
};

void TestNative::SetCallback(Callback callback, void * data)
{
    m_callback = callback;
    m_data = data;
}

void TestNative::Foo()
{
    // Foo() will never end
    while (true)
    {
        m_callback(m_data);
    }
}

extern "C"
{
    __declspec(dllexport) TestNative * CreateTestNative()
    {
        return new TestNative();
    }

    __declspec(dllexport) void FreeTestNative(TestNative *obj)
    {
        delete obj;
    }

    __declspec(dllexport) void TestNativeSetCallback(TestNative *obj, Callback callback1, void * data)
    {
        obj->SetCallback(callback1, data);
    }

    __declspec(dllexport) void TestNativeFoo(TestNative *obj)
    {
        obj->Foo();
    }
}
</code>```

The output is consistently:

this.Foo() begins
TestNet.callback() begins
this.callback()
this.~TestNet()
TestNet.callback() ends
TestNet.callback() begins
System.NullReferenceException: Object reference not set to an instance of an object.
```

If one uncomment the GC.KeepAlive(this) call in TestNet.Foo() the program correctly never ends.

答:
Summarizing very useful comments and research done:

1) Is GC.KeepAlive(this) always needed in a managed instance method if last instruction is a P/Invoke call using unmanaged resources hold by the instance?

Yes, if you don't want the user of the API to have last responsibility of holding a non-collectible reference for the instance of the managed object in pathological cases, look the example below. But it's not the only way: HandleRef or SafeHandle techiniques can also be used to prolong the lifetime of a managed object when doing P/Invoke Interop.

The example will subsequently call native methods through managed instances holding native resources:

```<code>using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            new Thread(delegate()
            {
                // Run a separate thread enforcing GC collections every second
                while(true)
                {
                    GC.Collect();
                    Thread.Sleep(1000);
                }
            }).Start();

            while (true)
            {
                var test = new TestNet();
                test.Foo();
                TestNet.Dump();
            }
        }
    }

    class TestNet
    {
        static ManualResetEvent _closed;
        static long _closeTime;
        static long _fooEndTime;

        IntPtr _nativeHandle;

        public TestNet()
        {
            _closed = new ManualResetEvent(false);
            _closeTime = -1;
            _fooEndTime = -1;
            _nativeHandle = CreateTestNative();
        }

        public static void Dump()
        {
            // Ensure the now the object will now be garbage collected
            GC.Collect();
            GC.WaitForPendingFinalizers();

            // Wait for current object to be garbage collected
            _closed.WaitOne();
            Trace.Assert(_closeTime != -1);
            Trace.Assert(_fooEndTime != -1);
            if (_closeTime <= _fooEndTime)
                Console.WriteLine("WARN: Finalize() commenced before Foo() return");
            else
                Console.WriteLine("Finalize() commenced after Foo() return");
        }

        ~TestNet()
        {
            _closeTime = Stopwatch.GetTimestamp();
            FreeTestNative(_nativeHandle);
            _closed.Set();
        }

        public void Foo()
        {
            // The native implementation just sleeps for 250ms
            TestNativeFoo(_nativeHandle);

            // Uncomment to have all Finalize() to commence after Foo()
            //GC.KeepAlive(this);
            _fooEndTime = Stopwatch.GetTimestamp();
        }

        [DllImport("Dll1", CallingConvention = CallingConvention.Cdecl)]
        static extern IntPtr CreateTestNative();

        [DllImport("Dll1", CallingConvention = CallingConvention.Cdecl)]
        static extern void FreeTestNative(IntPtr obj);

        [DllImport("Dll1", CallingConvention = CallingConvention.Cdecl)]
        static extern void TestNativeFoo(IntPtr obj);
    }
}
</code>```

For the native call to be always safe we expect finalizer to be called only after Foo() return. Instead we can easily enforce violations by manually invoking garbage collection in a background thread. Output follows:

Finalize() commenced after Foo() return
WARN: Finalize() commenced before Foo() return
Finalize() commenced after Foo() return
Finalize() commenced after Foo() return
Finalize() commenced after Foo() return
WARN: Finalize() commenced before Foo() return
Finalize() commenced after Foo() return
```

2) Where I can find documentation?

Documentation of GC.KeepAlive() provides an example very similar to the managed callback in the original question. HandleRef has also very interesting considerations about lifecycle of managed objects and Interop:

If you use platform invoke to call a managed object, and the object is
  not referenced elsewhere after the platform invoke call, it is
  possible for the garbage collector to finalize the managed object.
  This action releases the resource and invalidates the handle, causing
  the platform invoke call to fail. Wrapping a handle with HandleRef
  guarantees that the managed object is not garbage collected until the
  platform invoke call completes.

Also link[1] found by @GSerg explains when an object is eligible for collection, pointing that this reference is not in the root set, allowing it to be collected also when instance method has not returned.

3) Why is this happening only in Release build?

It's an optimization and can happen also in Debug build, with optimization enabled, as pointed by @SimonMourier. It's not enabled by default also in Debug because it could prevent debugging of variables in the current method scope, as explained in these other answers.

[1]
https://devblogs.microsoft.com/oldnewthing/20100810-00/?p=13193?

Body:
I have a TestNet wrapper for a native component. The native component exposes a blocking TestNative::Foo() that communicates with managed part through calling managed callbacks and a weak GCHandle that is used to retrieve the reference to the .NET wrapper and provides a context. The GCHandle is weak since the .NET wrapper is meant to hide the fact that is handling unmanaged resources to user and deliberately doesn't implement the IDisposable interface: being non weak it would prevent TestNet instances from being collected at all, creating a memory leak. What's happening is that in Release build only the garbage collector will collect reference to .NET wrapper while executing the managed callback, even before both TestNative::Foo() and surprisingly TestNet::Foo() unblocks. I understood the problem my self and I can fix it by issuing a GC.KeepAlive(this) after the P/Invoke call but since the knowledge of this is not very widespread, it seems a lot of people are doing it wrong. I have few questions:

Is GC.KeepAlive(this) always needed in a managed method if last instruction is a P/Invoke call on unmanaged resources or it's just needed in this special case, namely the switch to managed execution context while marshaling the managed callback from native code? The question could be: should I put GC.KeepAlive(this) everywhere? This old microsoft blog (original link is 404, here is cached) seems to suggest so! But this would be game changer and basically it would mean that most people never did P/Invoke correctly, because this would require reviewing most P/Invoke calls in wrappers. Is there for example a rule that say that garbage collector (EDIT: or better the finalizer) can't run for objects that belong to the current thread while execution context is unamanaged (native)?

Where I can find proper documentation? I could find CodeAnalysis policy CA2115 pointing to generically use GC.KeepAlive(this) any time a unmanaged resource is accessed with P/Invoke. In general GC.KeepAlive(this) seems to be very rarely needed when dealing with finalizers.

Why is this happening only in Release build? It looks like an optimization but not being needed at all in Debug build hides an important behavior of the garbage collector.

NOTE: I have no problem with delegates being collected, that is a different issue which I know how to handle properly. The issue here is with objects holding unmanaged resources being collected when P/Invoke calls are not finished yet.

It follows code that clearly manifest the problem. Creates a C# console application and a C++ Dll1 project and build them in Release mode:

Program.cs:

using System;
using System.Runtime.InteropServices;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var test = new TestNet();
            try
            {
                test.Foo();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
    }

    class TestNet
    {
        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
        delegate void Callback(IntPtr data);

        static Callback _callback;

        IntPtr _nativeHandle;
        GCHandle _thisHandle;

        static TestNet()
        {
            // NOTE: Keep delegates references so they can be
            // stored persistently in unmanaged resources
            _callback = callback;
        }

        public TestNet()
        {
            _nativeHandle = CreateTestNative();

            // Keep a weak handle to self. Weak is necessary
            // to not prevent garbage collection of TestNet instances
            _thisHandle = GCHandle.Alloc(this, GCHandleType.Weak);

            TestNativeSetCallback(_nativeHandle, _callback, GCHandle.ToIntPtr(_thisHandle));
        }

        ~TestNet()
        {
            Console.WriteLine("this.~TestNet()");
            FreeTestNative(_nativeHandle);
            _thisHandle.Free();
        }

        public void Foo()
        {
            Console.WriteLine("this.Foo() begins");
            TestNativeFoo(_nativeHandle);

            // This is never printed when the object is collected!
            Console.WriteLine("this.Foo() ends");

            // Without the following GC.KeepAlive(this) call
            // in Release build the program will consistently collect
            // the object in callback() and crash on next iteration
            //GC.KeepAlive(this);
        }

        static void callback(IntPtr data)
        {
            Console.WriteLine("TestNet.callback() begins");
            // Retrieve the weak reference to self. As soon as the istance
            // of TestNet exists.
            var self = (TestNet)GCHandle.FromIntPtr(data).Target;
            self.callback();

            // Enforce garbage collection. On release build
            self = null;
            GC.Collect();
            GC.WaitForPendingFinalizers();
            Console.WriteLine("TestNet.callback() ends");
        }

        void callback()
        {
            Console.WriteLine("this.callback()");
        }

        [DllImport("Dll1", CallingConvention = CallingConvention.Cdecl)]
        static extern IntPtr CreateTestNative();

        [DllImport("Dll1", CallingConvention = CallingConvention.Cdecl)]
        static extern void FreeTestNative(IntPtr obj);

        [DllImport("Dll1", CallingConvention = CallingConvention.Cdecl)]
        static extern void TestNativeSetCallback(IntPtr obj, Callback callback, IntPtr data);

        [DllImport("Dll1", CallingConvention = CallingConvention.Cdecl)]
        static extern void TestNativeFoo(IntPtr obj);
    }
}

Dll1.cpp:

#include <iostream>

extern "C" typedef void (*Callback)(void *data);

class TestNative
{
public:
    void SetCallback(Callback callback1, void *data);
    void Foo();
private:
    Callback m_callback;
    void *m_data;
};

void TestNative::SetCallback(Callback callback, void * data)
{
    m_callback = callback;
    m_data = data;
}

void TestNative::Foo()
{
    // Foo() will never end
    while (true)
    {
        m_callback(m_data);
    }
}

extern "C"
{
    __declspec(dllexport) TestNative * CreateTestNative()
    {
        return new TestNative();
    }

    __declspec(dllexport) void FreeTestNative(TestNative *obj)
    {
        delete obj;
    }

    __declspec(dllexport) void TestNativeSetCallback(TestNative *obj, Callback callback1, void * data)
    {
        obj->SetCallback(callback1, data);
    }

    __declspec(dllexport) void TestNativeFoo(TestNative *obj)
    {
        obj->Foo();
    }
}

The output is consistently:

this.Foo() begins
TestNet.callback() begins
this.callback()
this.~TestNet()
TestNet.callback() ends
TestNet.callback() begins
System.NullReferenceException: Object reference not set to an instance of an object.

If one uncomment the GC.KeepAlive(this) call in TestNet.Foo() the program correctly never ends.

Body:
Summarizing very useful comments and research done:

1) Is GC.KeepAlive(this) always needed in a managed instance method if last instruction is a P/Invoke call using unmanaged resources hold by the instance?

Yes, if you don't want the user of the API to have last responsibility of holding a non-collectible reference for the instance of the managed object in pathological cases, look the example below. But it's not the only way: HandleRef or SafeHandle techiniques can also be used to prolong the lifetime of a managed object when doing P/Invoke Interop.

The example will subsequently call native methods through managed instances holding native resources:

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            new Thread(delegate()
            {
                // Run a separate thread enforcing GC collections every second
                while(true)
                {
                    GC.Collect();
                    Thread.Sleep(1000);
                }
            }).Start();

            while (true)
            {
                var test = new TestNet();
                test.Foo();
                TestNet.Dump();
            }
        }
    }

    class TestNet
    {
        static ManualResetEvent _closed;
        static long _closeTime;
        static long _fooEndTime;

        IntPtr _nativeHandle;

        public TestNet()
        {
            _closed = new ManualResetEvent(false);
            _closeTime = -1;
            _fooEndTime = -1;
            _nativeHandle = CreateTestNative();
        }

        public static void Dump()
        {
            // Ensure the now the object will now be garbage collected
            GC.Collect();
            GC.WaitForPendingFinalizers();

            // Wait for current object to be garbage collected
            _closed.WaitOne();
            Trace.Assert(_closeTime != -1);
            Trace.Assert(_fooEndTime != -1);
            if (_closeTime <= _fooEndTime)
                Console.WriteLine("WARN: Finalize() commenced before Foo() return");
            else
                Console.WriteLine("Finalize() commenced after Foo() return");
        }

        ~TestNet()
        {
            _closeTime = Stopwatch.GetTimestamp();
            FreeTestNative(_nativeHandle);
            _closed.Set();
        }

        public void Foo()
        {
            // The native implementation just sleeps for 250ms
            TestNativeFoo(_nativeHandle);

            // Uncomment to have all Finalize() to commence after Foo()
            //GC.KeepAlive(this);
            _fooEndTime = Stopwatch.GetTimestamp();
        }

        [DllImport("Dll1", CallingConvention = CallingConvention.Cdecl)]
        static extern IntPtr CreateTestNative();

        [DllImport("Dll1", CallingConvention = CallingConvention.Cdecl)]
        static extern void FreeTestNative(IntPtr obj);

        [DllImport("Dll1", CallingConvention = CallingConvention.Cdecl)]
        static extern void TestNativeFoo(IntPtr obj);
    }
}

For the native call to be always safe we expect finalizer to be called only after Foo() return. Instead we can easily enforce violations by manually invoking garbage collection in a background thread. Output follows:

Finalize() commenced after Foo() return
WARN: Finalize() commenced before Foo() return
Finalize() commenced after Foo() return
Finalize() commenced after Foo() return
Finalize() commenced after Foo() return
WARN: Finalize() commenced before Foo() return
Finalize() commenced after Foo() return

2) Where I can find documentation?

Documentation of GC.KeepAlive() provides an example very similar to the managed callback in the original question. HandleRef has also very interesting considerations about lifecycle of managed objects and Interop:

If you use platform invoke to call a managed object, and the object is
  not referenced elsewhere after the platform invoke call, it is
  possible for the garbage collector to finalize the managed object.
  This action releases the resource and invalidates the handle, causing
  the platform invoke call to fail. Wrapping a handle with HandleRef
  guarantees that the managed object is not garbage collected until the
  platform invoke call completes.

Also link[1] found by @GSerg explains when an object is eligible for collection, pointing that this reference is not in the root set, allowing it to be collected also when instance method has not returned.

3) Why is this happening only in Release build?

It's an optimization and can happen also in Debug build, with optimization enabled, as pointed by @SimonMourier. It's not enabled by default also in Debug because it could prevent debugging of variables in the current method scope, as explained in these other answers.

[1]
https://devblogs.microsoft.com/oldnewthing/20100810-00/?p=13193?


来源: stackexchange

问:
Find indices of a 2d list in python, which contain another particular sublist
Suppose I have a 2D list in python (the length of each list within is not the same, say the first list have 3 elements, but the second one has 4 elements). I have another 1D list with exactly 2 elements. I need to find the indices of the 2D list that contain both the elements of the 1D list (need not to be in sequence). I want to do this task very efficiently as it will be a part of a huge analysis task (not using loops especially).

For example:

```<code>2D list: [[4, 0, 2], [0, 3, 2], [3, 0, 4], [5, 3, 4], [3, 5, 6], [8, 1, 2], [7, 3, 6], [1, 7, 6], [8, 7, 1], [2, 3, 7, 8]]

1D list: [3, 4]

output: 2, 3
</code>```

It is not essential that I use the list structure, is there any other structure in python that I can do it more efficiently?

答:
Not sure about efficiency, but this is the list comprehension that gets the result you want:

```<code>>>> list2d = [[4, 0, 2], [0, 3, 2], [3, 0, 4], [5, 3, 4], [3, 5, 6], [8, 1, 2], [7, 3, 6], [1, 7, 6], [8, 7, 1], [2, 3, 7, 8]]
>>> list1d = [3, 4]
>>> result = [list2d.index(sublist) for sublist in list2d if all(value in sublist for value in list1d)]
>>> result
[2, 3]
</code>```

Note that list comprehensions tend to perform better than using Python's map/filter operations, at least when there are lambda functions involved (which would be likely in your case).

ContentLicense: CC BY-SA 4.0

Body:
Suppose I have a 2D list in python (the length of each list within is not the same, say the first list have 3 elements, but the second one has 4 elements). I have another 1D list with exactly 2 elements. I need to find the indices of the 2D list that contain both the elements of the 1D list (need not to be in sequence). I want to do this task very efficiently as it will be a part of a huge analysis task (not using loops especially).

For example:

2D list: [[4, 0, 2], [0, 3, 2], [3, 0, 4], [5, 3, 4], [3, 5, 6], [8, 1, 2], [7, 3, 6], [1, 7, 6], [8, 7, 1], [2, 3, 7, 8]]

1D list: [3, 4]

output: 2, 3

It is not essential that I use the list structure, is there any other structure in python that I can do it more efficiently?

Body:
Not sure about efficiency, but this is the list comprehension that gets the result you want:

>>> list2d = [[4, 0, 2], [0, 3, 2], [3, 0, 4], [5, 3, 4], [3, 5, 6], [8, 1, 2], [7, 3, 6], [1, 7, 6], [8, 7, 1], [2, 3, 7, 8]]
>>> list1d = [3, 4]
>>> result = [list2d.index(sublist) for sublist in list2d if all(value in sublist for value in list1d)]
>>> result
[2, 3]

Note that list comprehensions tend to perform better than using Python's map/filter operations, at least when there are lambda functions involved (which would be likely in your case).


问:
Cast angular http response into class
I am trying to cast a response object from an HTTP Post request in my angular project to a Person class that I have defined.  I have defined a generic post method in an HTTP Service, and am calling that in my person service with the generic replaced with Person.  So, I would think, since I have done that the HTTP response should be a Person, but it's not - it's just an Object.  I need it to be a Person because my Person class has some custom logic on it that I need to access.  I could write a helper method in my person service, but I feel like this should work - especially since VS Code intellisense says that the response in my component is a Person when I hover my mouse over it.

Here is my code:

http.service.ts

```<code>@Injectable()
export class HttpService {
    baseUrl = 'https://baseurl.com';

    constructor(private http: HttpClient) { }

    post<T>(endpointUrl: string, body: any): Observable<T> {
        const fullUrl = this.baseUrl + endpointUrl;
        return this.http
            .post<T>(fullUrl, body)
            .pipe(
                map(response => response as T)
            );
    }
}
</code>```

person.service.ts

```<code>@Injectable()
export class PersonService {

    constructor(private httpService: HttpService) { }

    newPerson(body: Person): Observable<Person> {
        return this.httpService.post<Person>('/people', JSON.stringify(body));
    }
}
</code>```

person.component.ts

```<code>@Component({
    selector: 'app-person',
    templateUrl: './person.component.html',
    styleUrls: ['./person.component.css'],
})
export class PersonComponent {
        person: Person = new Person();

        onSubmit(id: number) {
        if (id == 0) {
            console.log(this.person);                              // Person {id: 0, ...
            console.log(this.person.constructor.name);             // Person
            let body = this.person.fromFormGroup(this.formGroup);

            this.personService.newPerson(body)
                .subscribe(response => {                           // VS Code intellisense says this: (parameter) response : Person
                    this.person = response as Person;
                    console.log(this.person);                      // {id: 72, ...
                    console.log(this.person.constructor.name);     // Object

                    // Trying a different way
                    this.person = <Person>response;
                    console.log(this.person);                      // {id: 72, ...
                    console.log(this.person.constructor.name);     // Object
                })

        }
    }

}
</code>```

答:
Response from the http service will be a json object which is deserialised to a javascript object. Your person service cannot convert JavaScript object received in the api response to person object out of the box. Remember typescript code is transpiled to javascript code. To convert the javascript object to your Person class object you will have to manually instantiate your class object and fill the properties from the response of the api by projecting the response to person object. You can do the following [notice the map operator used to project the response to person]-

person.service.ts

@Injectable()
export class PersonService {

  constructor(private httpService: HttpService) {}

  newPerson(body: Person): Observable<Person> {
    return this.httpService.post<Person>('/.people', JSON.stringify(body))
      .pipe(
        map(response => {

          const person = new Person();
          //fill the person props from response
          return person;
        }),
      );
  }
}
```

Title: Cast angular http response into class

Body:
I am trying to cast a response object from an HTTP Post request in my angular project to a Person class that I have defined.  I have defined a generic post method in an HTTP Service, and am calling that in my person service with the generic replaced with Person.  So, I would think, since I have done that the HTTP response should be a Person, but it's not - it's just an Object.  I need it to be a Person because my Person class has some custom logic on it that I need to access.  I could write a helper method in my person service, but I feel like this should work - especially since VS Code intellisense says that the response in my component is a Person when I hover my mouse over it.

Here is my code:

http.service.ts

@Injectable()
export class HttpService {
    baseUrl = 'https://baseurl.com';

    constructor(private http: HttpClient) { }

    post<T>(endpointUrl: string, body: any): Observable<T> {
        const fullUrl = this.baseUrl + endpointUrl;
        return this.http
            .post<T>(fullUrl, body)
            .pipe(
                map(response => response as T)
            );
    }
}

person.service.ts

@Injectable()
export class PersonService {

    constructor(private httpService: HttpService) { }

    newPerson(body: Person): Observable<Person> {
        return this.httpService.post<Person>('/people', JSON.stringify(body));
    }
}

person.component.ts

@Component({
    selector: 'app-person',
    templateUrl: './person.component.html',
    styleUrls: ['./person.component.css'],
})
export class PersonComponent {
        person: Person = new Person();

        onSubmit(id: number) {
        if (id == 0) {
            console.log(this.person);                              // Person {id: 0, ...
            console.log(this.person.constructor.name);             // Person
            let body = this.person.fromFormGroup(this.formGroup);

            this.personService.newPerson(body)
                .subscribe(response => {                           // VS Code intellisense says this: (parameter) response : Person
                    this.person = response as Person;
                    console.log(this.person);                      // {id: 72, ...
                    console.log(this.person.constructor.name);     // Object

                    // Trying a different way
                    this.person = <Person>response;
                    console.log(this.person);                      // {id: 72, ...
                    console.log(this.person.constructor.name);     // Object
                })

        }
    }

}


问:
Change Port of Mysql Server in Phpmyadmin
I have two instances of MySql (5.1.73-1-log) running on my server, one on port 3306 (default), one on port 3307. Now, I want to change PhpMyAdmin (3.3.7deb7)  to take the second instance, on 3307, instead the default one.
Thus, I add the following line to config.inc.php:

```<code>    $cfg['Servers'][$i]['port'] = '3307';
</code>```

Now, though PhpMyAdmin says localhost:3307 (look Screenshot), the databases that it accesses are still those from the instance that runs on the default port.

How do I change the settings in order to make the port change real?

答:
first open Xamppp Control Panel click on Config Button, at the next page click on Services and Port Settings, at that page you can set all ports for all of your services.
also you should change content of Phpmyadmin(config.inc.php) file as below</p>

```<code>$cfg['Servers'][$i]['host'] = '127.0.0.1';
$cfg['Servers'][$i]['port']= 'your preferred port number';
</code>```

ContentLicense: CC BY-SA 3.0

Body:
I have two instances of MySql (5.1.73-1-log) running on my server, one on port 3306 (default), one on port 3307. Now, I want to change PhpMyAdmin (3.3.7deb7)  to take the second instance, on 3307, instead the default one.
Thus, I add the following line to config.inc.php:

    $cfg['Servers'][$i]['port'] = '3307';

Now, though PhpMyAdmin says localhost:3307 (look Screenshot), the databases that it accesses are still those from the instance that runs on the default port.

How do I change the settings in order to make the port change real?

Body:
first open Xamppp Control Panel click on Config Button, at the next page click on Services and Port Settings, at that page you can set all ports for all of your services.
also you should change content of Phpmyadmin(config.inc.php) file as below

$cfg['Servers'][$i]['host'] = '127.0.0.1';
$cfg['Servers'][$i]['port']= 'your preferred port number';


问:
How to extract two consecutive digits from a text field in MySQL?
I have a MySQL database and I have a query as:

```<code>SELECT `id`, `originaltext` FROM `source` WHERE `originaltext` regexp '[0-9][0-9]'
</code>```

This detects all originaltexts which have numbers with 2 digits in it.

I need MySQL to return those numbers as a field, so i can manipulate them further.

Ideally, if I can add additional criteria that is should be > 20 would be great, but i can do that separately as well.

答:
I think the cleaner way is using REGEXP_SUBSTR():

This extracts exactly two any digits:

```<code>SELECT REGEXP_SUBSTR(`originalText`,'[0-9]{2}') AS `twoDigits` FROM `source`;
</code>```

This extracts exactly two digits, but from 20-99 (example: 1112 return null; 1521 returns 52):

```<code>SELECT REGEXP_SUBSTR(`originalText`,'[2-9][0-9]') AS `twoDigits` FROM `source`;
</code>```

I test both in v8.0 and they work. That's all, good luck!

Body:
I have a MySQL database and I have a query as:

SELECT `id`, `originaltext` FROM `source` WHERE `originaltext` regexp '[0-9][0-9]'

This detects all originaltexts which have numbers with 2 digits in it.

I need MySQL to return those numbers as a field, so i can manipulate them further.

Ideally, if I can add additional criteria that is should be > 20 would be great, but i can do that separately as well.

Body:
I think the cleaner way is using REGEXP_SUBSTR():

This extracts exactly two any digits:

SELECT REGEXP_SUBSTR(`originalText`,'[0-9]{2}') AS `twoDigits` FROM `source`;

This extracts exactly two digits, but from 20-99 (example: 1112 return null; 1521 returns 52):

SELECT REGEXP_SUBSTR(`originalText`,'[2-9][0-9]') AS `twoDigits` FROM `source`;

I test both in v8.0 and they work. That's all, good luck!


问:
How to keep a GitHub fork up to date without a merge commit or using CLI?
The normal GitHub flow to contribute to a repo is to create a fork of the upstream, clone a local copy where you make changes, then push back up to your fork and then create a PR to have your changes merged into upstream.

But if upstream changes after that, how do you update your fork without creating a merge commit (and also without using the git CLI)?

I already know how to do this in a way that will create a merge commit or which depend on the git command line interface. This question is specifically about using the GitHub.com website or GitHub Desktop application only (no CLI).

Since this is a very common workflow it seems like there should be some simple way to do it using the GitHub GUI.

To reiterate: any answers that use the CLI or create a merge commit (e.g. this way) will not be answering this question since I'm explicitly looking for a non-CLI solution.

答:
Update
Note: Non-CLI based approach that might help:
Is there a way to make GitHub Desktop rebase a branch against master?

The only key here is doing a rebase, so the above answer should help.

CLI way (which is easier and using git, so it should be more comprehensive by default)

There are some practices that you should use to avoid this.

Don't work on the master branch in your fork.

```<code>$ git clone <your fork>
$ git checkout -b feature_branch
</code>```

You can work in your feature_branch and then raise a Pull Request.

Once your changes are merged in the upstream master, you can pull from upstream to your origin. Since the master on upstream will have your commits sitting neatly on top of it, there won't be a merge commit.

```<code>$ git checkout master
$ git pull upstream master
$ git push origin master
</code>```

In the case, where the maintainer has diverged from the master that you have in your fork, that is, it's not linear any more, you need to pull a fresh copy of it. That should not be a problem as your changes are already in the upstream.

If the master in upstream has moved ahead while you were working on your PR, then you can rebase on you feature_branch.

```<code>$ git checkout master
$ git pull upstream master
$ git push origin master
$ git checkout feature_branch
$ git rebase master
</code>```

Please refer to this document for detailed reference: Fork and pull request workflow

Body:
Update
Note: Non-CLI based approach that might help:
Is there a way to make GitHub Desktop rebase a branch against master?

The only key here is doing a rebase, so the above answer should help.

CLI way (which is easier and using git, so it should be more comprehensive by default)

There are some practices that you should use to avoid this.

Don't work on the master branch in your fork.

$ git clone <your fork>
$ git checkout -b feature_branch

You can work in your feature_branch and then raise a Pull Request.

Once your changes are merged in the upstream master, you can pull from upstream to your origin. Since the master on upstream will have your commits sitting neatly on top of it, there won't be a merge commit.

$ git checkout master
$ git pull upstream master
$ git push origin master

In the case, where the maintainer has diverged from the master that you have in your fork, that is, it's not linear any more, you need to pull a fresh copy of it. That should not be a problem as your changes are already in the upstream.

If the master in upstream has moved ahead while you were working on your PR, then you can rebase on you feature_branch.

$ git checkout master
$ git pull upstream master
$ git push origin master
$ git checkout feature_branch
$ git rebase master

Please refer to this document for detailed reference: Fork and pull request workflow


问:
MongoDb c# driver update multiple documents in one atomic operation
I want to ask you if the following code will be executed in one atomic operation. I'm using mongodb c# driver.

The input to the method is list of id of the objects I want to update.

```<code>public void Update(IEnumerable<string> ids)
{
    var query = Query<T>.Where(t => ids.Contains(t.Id));
    var update = Update.Set("Modified", DateTime.Now); //this is just example of update

    var options = new MongoUpdateOptions {Flags = UpdateFlags.Multi};

    Collection.Update(query, update, options);
}
</code>```

I'm interesting about the case, when I have milions of documents to update. What will happen if there will be a fail (power or hardware problem) during this update? Will be the database in a consistent state?

thanks.

答:
Requirments

MongoDB >= 4.0
C# Driver >= 2.7
Here how i did it...

TL;DR

Goto "Sample Code"

Sessions

In my DbContext class, where i had access to my client (IMongoClient), i defined sessions:

```<code>public IClientSessionHandle StartSession()
{
    return _client.StartSession(new ClientSessionOptions());
}

public async Task<IClientSessionHandle> StartSessionAsync()
{
    return await _client.StartSessionAsync(new ClientSessionOptions());
}
</code>```

And as documentation says, they are capable of executing multiple transactions one after another, if only these transactions are coming one after another

A session is used to group together a series of operations that are
  related to each other and should be executed with the same session
  options. Sessions are also used for transactions.

these session said that they should be closed as soon as you ending your operation...

so you should actually write it like following, or perform manual disposing depend on your scenario:

```<code>// db is what i named my context, where i defined all my collections and database related stuffs.
// if you have direct access to client, you can call `StartSession/Async` exactly over the `client` object
using(var session = _db.StartSessionAsync()) {
    //... All transactional code will go here
}
// Here, on ending the block, Will automatically call `Dispose` method, and the object will no longer exists
</code>```

over session object

Transactions

Transactions are started, committed or aborted using methods of
  IClientSession. A session can only execute one transaction at a time,
  but a session can execute more than one transaction as long as each
  transaction is committed or aborted before the next one is started.

In this step you need to start a transaction before actually perform changes on database...

```<code>session.StartTransaction();
</code>```

Once the session start, you should perform your transactions, and at the end...

If process is successful, you should call:

```<code>session.CommitTransaction();
</code>```

Otherwise, you need to rollback

```<code>session.AbortTransaction();
</code>```

Sample Code

As you can see i have two write operation on mongodb, and another external process which is critical in my case, and i need these three to be performed together,.. the first two are managed by transaction, and as long as the third doesn't throw exception, the database should keep its new state.

```<code>bool error;
using (var session = await _db.StartSessionAsync())
{
    session.StartTransaction();

    try
    {
        var deletedImage = _db.GetUserStore<ApplicationUser>().CollectionInstance.UpdateOneAsync(
            Builders<ApplicationUser>.Filter.Where(w => w.Id == userId),
            Builders<ApplicationUser>.Update.Pull(x => x.ProfilePictures, photoFromRepo));

        await _db.ProfilePicture().DeleteAsync(new ObjectId(photoFromRepo.ImageId));

        if (photoFromRepo.CloudinaryPublicId != null)
        {
            var deleteParams = new DeletionParams(photoFromRepo.CloudinaryPublicId);
            var result = _cloudinary.Destroy(deleteParams);
            if (result.Result == "ok")
            {
                // ignore
            }
            else
            {
                throw new Exception("Cannot delete file from cloud service...");
            }
        }

        await session.CommitTransactionAsync();
        error = false;
    }
    catch (Exception ex)
    {
        await session.AbortTransactionAsync();
        error = true;
    }
}
</code>```

Does this work? Does it support multiple collections? Only god knows, i write this based on documentation and some sample i saw earlier today on my way home, and what i thought that may be right and possible ...

Extra Notes:

There are options that you can pass session, one of these option controls read/write concerns, and another one, control how much the data should be early before performing transactions (what it mean? i didn't got it myself, if you understand, please edit my post)

```<code>public class ClientSessionOptions
{
    public bool? CausalConsistency { get; set; }
    public TransactionOptions DefaultTransactionOptions { get; set; }
}

public class TransactionOptions
{
    public ReadConcern ReadConcern { get; };
    public ReadPreference ReadPreference { get; };
    public WriteConcern WriteConcern { get; };

    public TransactionOptions(
        Optional<ReadConcern> readConcern = default(Optional<ReadConcern>),
        Optional<ReadPreference> readPreference = default(Optional<ReadPreference>),
        Optional<WriteConcern> writeConcern = default(Optional<WriteConcern>));

    public TransactionOptions With(
        Optional<ReadConcern> readConcern = default(Optional<ReadConcern>),
        Optional<ReadPreference> readPreference = default(Optional<ReadPreference>),
        Optional<WriteConcern> writeConcern = default(Optional<WriteConcern>))
}
</code>```

Body:
I want to ask you if the following code will be executed in one atomic operation. I'm using mongodb c# driver.

The input to the method is list of id of the objects I want to update.

public void Update(IEnumerable<string> ids)
{
    var query = Query<T>.Where(t => ids.Contains(t.Id));
    var update = Update.Set("Modified", DateTime.Now); //this is just example of update

    var options = new MongoUpdateOptions {Flags = UpdateFlags.Multi};

    Collection.Update(query, update, options);
}

I'm interesting about the case, when I have milions of documents to update. What will happen if there will be a fail (power or hardware problem) during this update? Will be the database in a consistent state?

thanks.

Body:
Requirments

MongoDB >= 4.0
C# Driver >= 2.7
Here how i did it...

TL;DR

Goto "Sample Code"

Sessions

In my DbContext class, where i had access to my client (IMongoClient), i defined sessions:

public IClientSessionHandle StartSession()
{
    return _client.StartSession(new ClientSessionOptions());
}

public async Task<IClientSessionHandle> StartSessionAsync()
{
    return await _client.StartSessionAsync(new ClientSessionOptions());
}

And as documentation says, they are capable of executing multiple transactions one after another, if only these transactions are coming one after another

A session is used to group together a series of operations that are
  related to each other and should be executed with the same session
  options. Sessions are also used for transactions.

these session said that they should be closed as soon as you ending your operation...

so you should actually write it like following, or perform manual disposing depend on your scenario:

// db is what i named my context, where i defined all my collections and database related stuffs.
// if you have direct access to client, you can call `StartSession/Async` exactly over the `client` object
using(var session = _db.StartSessionAsync()) {
    //... All transactional code will go here
}
// Here, on ending the block, Will automatically call `Dispose` method, and the object will no longer exists

over session object

Transactions

Transactions are started, committed or aborted using methods of
  IClientSession. A session can only execute one transaction at a time,
  but a session can execute more than one transaction as long as each
  transaction is committed or aborted before the next one is started.

In this step you need to start a transaction before actually perform changes on database...

session.StartTransaction();

Once the session start, you should perform your transactions, and at the end...

If process is successful, you should call:

session.CommitTransaction();

Otherwise, you need to rollback

session.AbortTransaction();

Sample Code

As you can see i have two write operation on mongodb, and another external process which is critical in my case, and i need these three to be performed together,.. the first two are managed by transaction, and as long as the third doesn't throw exception, the database should keep its new state.

bool error;
using (var session = await _db.StartSessionAsync())
{
    session.StartTransaction();

    try
    {
        var deletedImage = _db.GetUserStore<ApplicationUser>().CollectionInstance.UpdateOneAsync(
            Builders<ApplicationUser>.Filter.Where(w => w.Id == userId),
            Builders<ApplicationUser>.Update.Pull(x => x.ProfilePictures, photoFromRepo));

        await _db.ProfilePicture().DeleteAsync(new ObjectId(photoFromRepo.ImageId));

        if (photoFromRepo.CloudinaryPublicId != null)
        {
            var deleteParams = new DeletionParams(photoFromRepo.CloudinaryPublicId);
            var result = _cloudinary.Destroy(deleteParams);
            if (result.Result == "ok")
            {
                // ignore
            }
            else
            {
                throw new Exception("Cannot delete file from cloud service...");
            }
        }

        await session.CommitTransactionAsync();
        error = false;
    }
    catch (Exception ex)
    {
        await session.AbortTransactionAsync();
        error = true;
    }
}

Does this work? Does it support multiple collections? Only god knows, i write this based on documentation and some sample i saw earlier today on my way home, and what i thought that may be right and possible ...

Extra Notes:

There are options that you can pass session, one of these option controls read/write concerns, and another one, control how much the data should be early before performing transactions (what it mean? i didn't got it myself, if you understand, please edit my post)

public class ClientSessionOptions
{
    public bool? CausalConsistency { get; set; }
    public TransactionOptions DefaultTransactionOptions { get; set; }
}

public class TransactionOptions
{
    public ReadConcern ReadConcern { get; };
    public ReadPreference ReadPreference { get; };
    public WriteConcern WriteConcern { get; };

    public TransactionOptions(
        Optional<ReadConcern> readConcern = default(Optional<ReadConcern>),
        Optional<ReadPreference> readPreference = default(Optional<ReadPreference>),
        Optional<WriteConcern> writeConcern = default(Optional<WriteConcern>));

    public TransactionOptions With(
        Optional<ReadConcern> readConcern = default(Optional<ReadConcern>),
        Optional<ReadPreference> readPreference = default(Optional<ReadPreference>),
        Optional<WriteConcern> writeConcern = default(Optional<WriteConcern>))
}


问:
Laravel Eloquent ignore attribute if not in table during insert
I have a model Foo that corresponds to a table with the following columns.

id

description

user_id

I'm setting the attributes of the Foo model individually (no mass assignment)

```<code>$foo = new Foo;

$foo->id = 1;
$foo->description = "hello kitty";
$foo->user_id = 55;

//...
</code>```

$foo is sent to another class for additional processing, but because that class needs a bit more information, I would like to simply add it to the $foo model.

```<code>//...
$foo->bar = $additional_information;

Event::fire(DoStuffWithFoo($foo));

$foo->save();  //error
</code>```

the problem is when I $foo->save(), it complains that bar isn't a column.

I know I can unset($foo->bar); before saving, but...

Is it possible to tell Eloquent to simply ignore any non relevant attributes?

答:
I understand this question is old but it was in top results for a recent search where I was trying to solve a similar problem and I think that this may be an ideal case for Laravel accessors/mutators. I have tested this on Laravel 5.6 but believe it may work as far back as 4.2.

By creating a mutator and accessor rather than a public property it will allow adding the field to fillable for mass assignment while still excluding it from the internal attributes (thus preventing it from errantly saving to the DB). I understand the original request excluded mass-assignment but that doesn't necessarily exclude this answer. I think an example will help:

```<code>class Foo extends Model
{
    //Allow bar in mass assignment
    protected $fillable = [
            "bar"
        ];

    /**
     * Store bar in protected variable instead of attributes
     * Because bar is not set in attributes, Laravel will not try to save it to database
     */
    protected $bar;

    /**
     * Mutator method to set bar's value
     */
    public function setBarAttribute($value)
    {
        $this->bar = $value;
    }

    /**
     * Accessor method to retrieve bar's value
     */
    public function getBarAttribute()
    {
        return $this->bar;
    }
}
</code>```

When this model is created using mass-assignment the mutator (setBarAttribute) method will be called for bar if it exists in the mass-assigned values. Anytime the bar property is accessed the respective get/set method will be called. Because the mutator does not set the value of bar in the model's internal attributes variable the model will not save bar to the database.

Body:
I have a model Foo that corresponds to a table with the following columns.

id

description

user_id

I'm setting the attributes of the Foo model individually (no mass assignment)

$foo = new Foo;

$foo->id = 1;
$foo->description = "hello kitty";
$foo->user_id = 55;

//...

$foo is sent to another class for additional processing, but because that class needs a bit more information, I would like to simply add it to the $foo model.

//...
$foo->bar = $additional_information;

Event::fire(DoStuffWithFoo($foo));

$foo->save();  //error

the problem is when I $foo->save(), it complains that bar isn't a column.

I know I can unset($foo->bar); before saving, but...

Is it possible to tell Eloquent to simply ignore any non relevant attributes?

Body:
I understand this question is old but it was in top results for a recent search where I was trying to solve a similar problem and I think that this may be an ideal case for Laravel accessors/mutators. I have tested this on Laravel 5.6 but believe it may work as far back as 4.2.

By creating a mutator and accessor rather than a public property it will allow adding the field to fillable for mass assignment while still excluding it from the internal attributes (thus preventing it from errantly saving to the DB). I understand the original request excluded mass-assignment but that doesn't necessarily exclude this answer. I think an example will help:

class Foo extends Model
{
    //Allow bar in mass assignment
    protected $fillable = [
            "bar"
        ];

    /**
     * Store bar in protected variable instead of attributes
     * Because bar is not set in attributes, Laravel will not try to save it to database
     */
    protected $bar;

    /**
     * Mutator method to set bar's value
     */
    public function setBarAttribute($value)
    {
        $this->bar = $value;
    }

    /**
     * Accessor method to retrieve bar's value
     */
    public function getBarAttribute()
    {
        return $this->bar;
    }
}

When this model is created using mass-assignment the mutator (setBarAttribute) method will be called for bar if it exists in the mass-assigned values. Anytime the bar property is accessed the respective get/set method will be called. Because the mutator does not set the value of bar in the model's internal attributes variable the model will not save bar to the database.


问:
MySQL Insert string with python inserts as individual characters
I'm trying to do a simple insert with python

```<code>mydb = mysql.connector.connect(
  host="localhost",
  user="**",
  passwd="**"
)

mycursor = mydb.cursor()

sql = "INSERT INTO sponge_cakes.links (`url`) VALUES (%s)"
val = ("test1")

mycursor.executemany(sql, val)

mydb.commit()
</code>```

This sort of works, but it inserts the string character by character (creating a new entry for each character)

How can I get it to insert the entire string so it looks like this

```<code>id  | link
1   | test1
</code>```

Instead of

```<code>id  | link
1   | t
2   | e
...
</code>```

答:
The problem is you're using mycursor.executemany(sql, val) which is meant to run the same SQL statement with multiple values.

It might look like "test1" is just a single word, but a word can also be considered as a series of characters, and that's what executemany is interpreting it as.

Change it to mycursor.execute(sql, val) and it'll work as expected with a single value.

Body:
I'm trying to do a simple insert with python

mydb = mysql.connector.connect(
  host="localhost",
  user="**",
  passwd="**"
)

mycursor = mydb.cursor()

sql = "INSERT INTO sponge_cakes.links (`url`) VALUES (%s)"
val = ("test1")

mycursor.executemany(sql, val)

mydb.commit()

This sort of works, but it inserts the string character by character (creating a new entry for each character)

How can I get it to insert the entire string so it looks like this

id  | link
1   | test1

Instead of

id  | link
1   | t
2   | e
...


问:
F# function to sort Excel "variants" in Excel-Dna
When trying to sort a 1d array of variants (here by "variant" I mean all the Excel types, eg bool, double (and date), string, various errors...) with the following function :

```<code>[<ExcelFunction(Category="test", Description="sort variants.")>]
    let sort_variant ([<ExcelArgument(Description= "Array to sort.")>] arr : obj[]): obj[] =
        arr
        |> Array.sort
</code>```

I get the following error : Error  FS0001  The type 'obj' does not support the 'comparison' constraint. For example, it does not support the 'System.IComparable' interface, probably meaning that there is no generic ordering function available on all obj types.

But Excel has a natural ordering function, which I'd like to emulate (at least ballpark). Eg double (and dates) < string < bool < error...

My question : What is the idiomatic way to sort an array of "variants" in F# / Excel-Dna? (I am after a function which takes an obj[] and return an obj[], nothing else, not a macro...)

My (temporary?) solution :
I created a “discriminated union” type

```<code>type XLVariant = D of double | S of string | B of bool | NIL of string
</code>```

(not really sure whether NIL is necessary but it did not hurt. Also in my real life code I added a DT of DateTime instance as I need to distinguish dates from doubles).

```<code>let toXLVariant (x : obj) : XLVariant =
    match x with
    | :? double as d -> D d
    | :? string as s -> S s
    | :? bool   as b -> B b
    | _              -> NIL "unknown match"

let ofXLVariant (x : XLVariant) : obj =
    match x with
    | D d   -> box d
    | S s   -> box s
    | B b   -> box b
    | NIL _ -> box ExcelError.ExcelErrorRef

[<ExcelFunction(Category="test", Description="sort variants.")>]
let sort_variant ([<ExcelArgument(Description= "Array to sort.")>] arr : obj[]): obj[] =
    arr
    |> Array.map toXLVariant
    |> Array.sort
    |> Array.map ofXLVariant
</code>```

(for the sake of simplicity, I missed the Errors types, but the idea is the same)

答:
This seems a bit more explicit to me, since it just sticks to the CLR type system:</p>

```<code>// Compare objects in the way Excel would
let xlCompare (v1 : obj) (v2 : obj) =
    match (v1, v2) with
    | (:? double as d1), (:? double as d2) -> d1.CompareTo(d2)
    | (:? double), _ -> -1
    | _, (:? double) -> 1
    | (:? string as s1), (:? string as s2) -> s1.CompareTo(s2)
    | (:? string), _ -> -1
    | _, (:? string) -> 1
    | (:? bool as b1), (:? bool as b2) -> b1.CompareTo(b2)
    | (:? bool), _ -> -1
    | _, (:? bool) -> 1
    | _              -> 2

[<ExcelFunction(Category="test", Description="sort variants.")>]
let sort_variant ([<ExcelArgument(Description= "Array to sort.")>] arr : obj[]): obj[] =
    Array.sortWith xlCompare arr
</code>```

Body:
When trying to sort a 1d array of variants (here by "variant" I mean all the Excel types, eg bool, double (and date), string, various errors...) with the following function :

[<ExcelFunction(Category="test", Description="sort variants.")>]
    let sort_variant ([<ExcelArgument(Description= "Array to sort.")>] arr : obj[]): obj[] =
        arr
        |> Array.sort

I get the following error : Error  FS0001  The type 'obj' does not support the 'comparison' constraint. For example, it does not support the 'System.IComparable' interface, probably meaning that there is no generic ordering function available on all obj types.

But Excel has a natural ordering function, which I'd like to emulate (at least ballpark). Eg double (and dates) < string < bool < error...

My question : What is the idiomatic way to sort an array of "variants" in F# / Excel-Dna? (I am after a function which takes an obj[] and return an obj[], nothing else, not a macro...)

My (temporary?) solution :
I created a “discriminated union” type

type XLVariant = D of double | S of string | B of bool | NIL of string

(not really sure whether NIL is necessary but it did not hurt. Also in my real life code I added a DT of DateTime instance as I need to distinguish dates from doubles).

let toXLVariant (x : obj) : XLVariant =
    match x with
    | :? double as d -> D d
    | :? string as s -> S s
    | :? bool   as b -> B b
    | _              -> NIL "unknown match"

let ofXLVariant (x : XLVariant) : obj =
    match x with
    | D d   -> box d
    | S s   -> box s
    | B b   -> box b
    | NIL _ -> box ExcelError.ExcelErrorRef

[<ExcelFunction(Category="test", Description="sort variants.")>]
let sort_variant ([<ExcelArgument(Description= "Array to sort.")>] arr : obj[]): obj[] =
    arr
    |> Array.map toXLVariant
    |> Array.sort
    |> Array.map ofXLVariant

(for the sake of simplicity, I missed the Errors types, but the idea is the same)

Body:
This seems a bit more explicit to me, since it just sticks to the CLR type system:

// Compare objects in the way Excel would
let xlCompare (v1 : obj) (v2 : obj) =
    match (v1, v2) with
    | (:? double as d1), (:? double as d2) -> d1.CompareTo(d2)
    | (:? double), _ -> -1
    | _, (:? double) -> 1
    | (:? string as s1), (:? string as s2) -> s1.CompareTo(s2)
    | (:? string), _ -> -1
    | _, (:? string) -> 1
    | (:? bool as b1), (:? bool as b2) -> b1.CompareTo(b2)
    | (:? bool), _ -> -1
    | _, (:? bool) -> 1
    | _              -> 2

[<ExcelFunction(Category="test", Description="sort variants.")>]
let sort_variant ([<ExcelArgument(Description= "Array to sort.")>] arr : obj[]): obj[] =
    Array.sortWith xlCompare arr


问:
How to login into Yahoo Mail using Selenium
I am recently working through examples in Automate the Boring Stuff in Python, and came across this example which I have some issues with. In this exercise, I tried to log in to Yahoo Mail using Selenium on the Chrome browser. The code does not produce any error and successfully prints the last line, however, it fails to click on the submit button which leaves me stuck at the login page despite successfully filling up the password. However, if I click on the login button manually, I can login successfully.

I have tried both passwordelem.submit() and passwordsubmitbtn.click() but neither works. I have also tried changing the user-agent in chrome but to no avail.

```<code>from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options

browser = webdriver.Chrome()
browser.get('https://login.yahoo.com?.src=ym&.lang=en-US&.intl=us&.done=https%3A%2F%2Fmail.yahoo.com%2Fd')
wait = WebDriverWait(browser, 10)

emailelem = wait.until(EC.presence_of_element_located((By.ID,'login-username')))
emailelem.send_keys('xxxxxx@yahoo.com')

wait.until(EC.presence_of_element_located((By.ID,'login-signin')))
emailelem.submit()

passwordelem = wait.until(EC.presence_of_element_located((By.ID,'login-passwd')))
passwordelem.send_keys('xxxxxx')
##passwordelem.submit()

passwordsubmitbtn = wait.until(EC.element_to_be_clickable((By.ID,'login-signin')))
passwordsubmitbtn.click()

print ('Done')
</code>```

答:
To click() on the button with text as Sign in providing valid valid credentials on Yahoo Mail Login Page you have to induce WebDriverWait for the element to be clickable and you can use the following Locator Strategy:

Code Block:

```<code>from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

options = webdriver.ChromeOptions()
options.add_argument('start-maximized')
#options.add_argument('disable-infobars')
options.add_argument('--disable-extensions')
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get('https://login.yahoo.com')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.phone-no"))).send_keys("LinX123@yahoo.com")
driver.find_element_by_css_selector("input.orko-button-primary.orko-button#login-signin").click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#login-passwd"))).send_keys("LinX123@yahoo.com")
driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.pure-button.puree-button-primary.puree-spinner-button"))))
</code>```

Browser Snapshot

PS: Note the visible spinner-button highlighted when the login process starts.

Body:
I am recently working through examples in Automate the Boring Stuff in Python, and came across this example which I have some issues with. In this exercise, I tried to log in to Yahoo Mail using Selenium on the Chrome browser. The code does not produce any error and successfully prints the last line, however, it fails to click on the submit button which leaves me stuck at the login page despite successfully filling up the password. However, if I click on the login button manually, I can login successfully.

I have tried both passwordelem.submit() and passwordsubmitbtn.click() but neither works. I have also tried changing the user-agent in chrome but to no avail.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options

browser = webdriver.Chrome()
browser.get('https://login.yahoo.com?.src=ym&.lang=en-US&.intl=us&.done=https%3A%2F%2Fmail.yahoo.com%2Fd')
wait = WebDriverWait(browser, 10)

emailelem = wait.until(EC.presence_of_element_located((By.ID,'login-username')))
emailelem.send_keys('xxxxxx@yahoo.com')

wait.until(EC.presence_of_element_located((By.ID,'login-signin')))
emailelem.submit()

passwordelem = wait.until(EC.presence_of_element_located((By.ID,'login-passwd')))
passwordelem.send_keys('xxxxxx')
##passwordelem.submit()

passwordsubmitbtn = wait.until(EC.element_to_be_clickable((By.ID,'login-signin')))
passwordsubmitbtn.click()

print ('Done')

Body:
To click() on the button with text as Sign in providing valid valid credentials on Yahoo Mail Login Page you have to induce WebDriverWait for the element to be clickable and you can use the following Locator Strategy:

Code Block:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

options = webdriver.ChromeOptions()
options.add_argument('start-maximized')
#options.add_argument('disable-infobars')
options.add_argument('--disable-extensions')
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get('https://login.yahoo.com')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.phone-no"))).send_keys("LinX123@yahoo.com")
driver.find_element_by_css_selector("input.orko-button-primary.orko-button#login-signin").click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#login-passwd"))).send_keys("LinX123@yahoo.com")
driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.pure-button.puree-button-primary.puree-spinner-button"))))

Browser Snapshot

PS: Note the visible spinner-button highlighted when the login process starts.


问:
how to produce a customer retention table /cohort analysis with SQL
I'm trying to write an SQL query (Presto SQL syntax) to produce a customer retention table (see sample below).
A customer who makes at least one transaction in a month is considered as retained for that month.

this is the table

```<code>user_id       transaction_date
bdcff651- .   2018-01-01
bdcff641 .     2018-03-15
</code>```

this is the result I would like to get

The first row should be understood as follows:
Out of all customers who made their first transaction in the month of Jan 2018 (defined as “Jan Activation Cohort”), 35% subsequently made a transaction during the one month period following their first transaction date, 23% in the next month, 15% in the next month and so on.

```<code>  Date         1st Month   2nd Month       3rd Month

2018-01-01      35%               23% .         15%
2018-02-0       33 %             26% .         13%
2018-03-0       36%               27%           12%
</code>```

As an example, if person XYZ makes his first transaction on 10th February 2018, his 1st month will be from 11th February 2018 to 10th March 2018, 2nd month will be from 11th March 2018 to 10th April 2018 and so on. This person’s details need to appear in the Feb 2018 cohort in the Customer Retention Table.

would appreciate any help! thanks.

答:
I am not familiar with Presto exactly, and do not have a way to test Presto code. However, it looks like from searching around a bit that it wouldn't be too hard to convert to Presto syntax from something like SQL Server syntax. Here is what I would do in SQL Server and you should be able to carry the concept over to Presto:

```<code>with transactions_info_per_user as (
select user_id, min(transaction_date) as first_transaction,
       convert(datepart(year, min(transaction_date)) as varchar(4)) + convert(datepart(month, min(transaction_date)) as varchar(2)) as activation_cohort
from my_table
group by user_id
),

users_per_activation_cohort as (
select activation_cohort, count(*) as number_of_users
from transactions_info_per_user
group by activation_cohort
),

months_after_activation_per_purchase as (
select distinct mt.user_id, ti.activation_cohort, datediff(month, mt.transaction_date, ti.first_transaction) AS months_after_activation
from my_table mt
left join transactions_info_per_user as ti
on mt.user_id = ti.user_id
),

final as (
select activation_cohort, months_after_activation, count(*) as user_count_per_cohort_with_purchase_per_month_after_activation
from months_after_activation_per_purchase
group by activation_cohort, months_after_activation
)

select activation_cohort, months_after_activation,
       convert(user_count_per_cohort_with_purchase_per_month_after_activation as decimal(9,2)) / convert(users_per_activation_cohort as decimal(9,2)) * 100
from final

--Then pivot months_after_activation into columns
</code>```

I was very explicit with the naming of things so you could follow the thought process. Here is an example of how to pivot in Presto. Hopefully this helps you!

Body:
I'm trying to write an SQL query (Presto SQL syntax) to produce a customer retention table (see sample below).
A customer who makes at least one transaction in a month is considered as retained for that month.

this is the table

user_id       transaction_date
bdcff651- .   2018-01-01
bdcff641 .     2018-03-15

this is the result I would like to get

The first row should be understood as follows:
Out of all customers who made their first transaction in the month of Jan 2018 (defined as “Jan Activation Cohort”), 35% subsequently made a transaction during the one month period following their first transaction date, 23% in the next month, 15% in the next month and so on.

  Date         1st Month   2nd Month       3rd Month

2018-01-01      35%               23% .         15%
2018-02-0       33 %             26% .         13%
2018-03-0       36%               27%           12%

As an example, if person XYZ makes his first transaction on 10th February 2018, his 1st month will be from 11th February 2018 to 10th March 2018, 2nd month will be from 11th March 2018 to 10th April 2018 and so on. This person’s details need to appear in the Feb 2018 cohort in the Customer Retention Table.

would appreciate any help! thanks.

Body:
I am not familiar with Presto exactly, and do not have a way to test Presto code. However, it looks like from searching around a bit that it wouldn't be too hard to convert to Presto syntax from something like SQL Server syntax. Here is what I would do in SQL Server and you should be able to carry the concept over to Presto:

with transactions_info_per_user as (
select user_id, min(transaction_date) as first_transaction,
       convert(datepart(year, min(transaction_date)) as varchar(4)) + convert(datepart(month, min(transaction_date)) as varchar(2)) as activation_cohort
from my_table
group by user_id
),

users_per_activation_cohort as (
select activation_cohort, count(*) as number_of_users
from transactions_info_per_user
group by activation_cohort
),

months_after_activation_per_purchase as (
select distinct mt.user_id, ti.activation_cohort, datediff(month, mt.transaction_date, ti.first_transaction) AS months_after_activation
from my_table mt
left join transactions_info_per_user as ti
on mt.user_id = ti.user_id
),

final as (
select activation_cohort, months_after_activation, count(*) as user_count_per_cohort_with_purchase_per_month_after_activation
from months_after_activation_per_purchase
group by activation_cohort, months_after_activation
)

select activation_cohort, months_after_activation,
       convert(user_count_per_cohort_with_purchase_per_month_after_activation as decimal(9,2)) / convert(users_per_activation_cohort as decimal(9,2)) * 100
from final

--Then pivot months_after_activation into columns

I was very explicit with the naming of things so you could follow the thought process. Here is an example of how to pivot in Presto. Hopefully this helps you!


问:
Spark SQL - difference between gzip vs snappy vs lzo compression formats
I am trying to use Spark SQL to write parquet file.

By default Spark SQL supports gzip, but it also supports other compression formats like snappy and lzo.

What is the difference between these compression formats?

答:
Compression Ratio :
GZIP compression uses more CPU resources than Snappy or LZO, but provides a higher compression ratio.

General Usage :
GZip is often a good choice for cold data, which is accessed infrequently.
Snappy or LZO are a better choice for hot data, which is accessed frequently.

Snappy often performs better than LZO. It is worth running tests to see if you detect a significant difference.

Splittablity :
If you need your compressed data to be splittable, BZip2, LZO, and Snappy formats are splittable, but GZip is not.

GZIP compresses data 30% more as compared to Snappy and 2x more CPU when reading GZIP data compared to one that is consuming Snappy data.

LZO focus on decompression speed at low CPU usage and higher compression at the cost of more CPU.

For longer term/static storage, the GZip compression is still better.

See  extensive research and  benchmark code and results in this article (Performance of various general compression algorithms – some of them are unbelievably fast!).


问:
Is Lightweight Code Generation (LCG) dead?
In the .NET 2.0-3.5 frameworks, LCG (aka the DynamicMethod class) was a decent way to emit lightweight methods at runtime when no class structure was needed to support them.

In .NET 4.0, expression trees now support statements and blocks, and as such appear to provide sufficient functionality to build just about any functionality you could require from such a method, and can be constructed in a much easier and safer way than directly emitting CIL op-codes. (This statement is borne from today's experimentation of converting some of our most complex LCG code to use expression tree building and compilation instead.)

So is there any reason why one would use LCG in any new code? Is there anything it can do that expression trees cannot? Or is it now a 'dead' piece of functionality?

答:
There is no point in constructing CIL directly without any intermediate steps. But it is perfectly fine to use your own intermediate languages that are targeting IL at the end. Expression trees, etc., are not sufficient - it is only one language, whereas while implementing DSLs you'd need many different semantics.

You can emit easily unsafe code (with lots of ldftns and alike), you can emit tail calls (not sure if it is possible with expressions), nonvirtual calls to virtual methods, you can efficiently construct large state automatons with labels and jumps, etc. Expressions trees are so limiting that I simply cannot understand how can they be compared to a raw CIL.

ContentLicense: CC BY-SA 2.5


答:
If your 1D list and your sublists are instead sets, you can

```<code>list2d = [
    {0, 2, 4}, {0, 2, 3}, {0, 3, 4}, {3, 4, 5}, {3, 5, 6},
    {8, 1, 2}, {3, 6, 7}, {1, 6, 7}, {8, 1, 7}, {8, 2, 3, 7}
]
set1d = {3, 4}

i = 0
res = set()
for s in list2d:
    if set1d <= s:
        res.add(i)
    i += 1
</code>```

I'd very tentatively guess that's the fastest one can achieve in pure python

Title: Find indices of a 2d list in python, which contain another particular sublist

Body:
Suppose I have a 2D list in python (the length of each list within is not the same, say the first list have 3 elements, but the second one has 4 elements). I have another 1D list with exactly 2 elements. I need to find the indices of the 2D list that contain both the elements of the 1D list (need not to be in sequence). I want to do this task very efficiently as it will be a part of a huge analysis task (not using loops especially).

For example:

2D list: [[4, 0, 2], [0, 3, 2], [3, 0, 4], [5, 3, 4], [3, 5, 6], [8, 1, 2], [7, 3, 6], [1, 7, 6], [8, 7, 1], [2, 3, 7, 8]]

1D list: [3, 4]

output: 2, 3

It is not essential that I use the list structure, is there any other structure in python that I can do it more efficiently?

Body:
If your 1D list and your sublists are instead sets, you can

list2d = [
    {0, 2, 4}, {0, 2, 3}, {0, 3, 4}, {3, 4, 5}, {3, 5, 6},
    {8, 1, 2}, {3, 6, 7}, {1, 6, 7}, {8, 1, 7}, {8, 2, 3, 7}
]
set1d = {3, 4}

i = 0
res = set()
for s in list2d:
    if set1d <= s:
        res.add(i)
    i += 1

I'd very tentatively guess that's the fastest one can achieve in pure python


问:
How do I hide HTML video controls for all the videos on one page
I have a page filled with movies created using a php foreach loop like so:

```<code>   foreach ($movies as $movie) {

        $pos = strrpos($movie, '/');
        $id = $pos === false ? $movie : substr($movie, $pos + 1);
            $image = $lookup[pathinfo($movie, PATHINFO_FILENAME)] ?? $temp;
            $sub = $lookup2[pathinfo($movie, PATHINFO_FILENAME)];
            $withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/', '', $id);
            $xml = simplexml_load_file($dir . '/xml/' . $withoutExt . '.xml');
        echo '<div class="grid-containerTest">';
        if (file_exists($dir . '/xml/' . $withoutExt . '.xml')){
        echo '<div class="grid-item item1"><h3>' . $xml->title . '</h3></div>';
        }else{
            echo '<div class="grid-item item1"><h3>' . $withoutExt . '</h3></div>';
        }
        echo '<div class="grid-item item2"><video width="300" height="400" poster="'. $image .'" preload="none" id="videoElement">';
        echo '<source src="'. $movie .'" type="video/mp4">';
        echo '<track src="' . $sub . '" kind="subtitles" srclang="en-US" label="English" />Your browser does not support HTML5 video.</video></div>';
        echo '<div class="grid-item item3">' . $xml->overview . '<br /><br />';
        echo "Added on: " . date("F d Y.", filectime($movie)) . '</div>';
        echo '<br /><br /></div>';
    }
</code>```

Then I have some JS stored in the footer that hides the controls until a user hovers over them:

```<code><script type="text/javascript">
    (function(window) {
        function setupVideo()
        {
            var v = document.getElementById('videoElement');
            v.addEventListener('mouseover', function() { this.controls = true; }, false);
            v.addEventListener('mouseout', function() { this.controls = false; }, false);
        }

        window.addEventListener('load', setupVideo, false);
    })(window);
</script>
</code>```

It works fine. For the first video loaded into the page. The others are unplayable as there's no longer any controls on them. So what can I do to make sure it works for every video?

答:
First of all, you can only use the same id attribute once on a page. An id is supposed to be unique. Instead of using id="videoElement", I would recommend using a class (eg class="videoElement").

Besides that document.getElementById only returns a single element, so your listeners won't be added to all elements. There are several ways to solve this. One would be looping through all elements and adding the listeners.

```<code>document.querySelectorAll('.videoElement').forEach(function (videoElement) {
    // Add listeners to video element: videoElement.addEventListener....
});
</code>```

Or you could use event delegation here. You could set the events to the body and then check whether or not the target of the event is a videoElement.

```<code>document.body.addEventListener('mouseover', function (mouseEvent) {
    if (mouseEvent.target.getAttribute('class') === 'videoElement') {
        // show controls
    }
});
</code>```

Body:
I have a page filled with movies created using a php foreach loop like so:

   foreach ($movies as $movie) {

        $pos = strrpos($movie, '/');
        $id = $pos === false ? $movie : substr($movie, $pos + 1);
            $image = $lookup[pathinfo($movie, PATHINFO_FILENAME)] ?? $temp;
            $sub = $lookup2[pathinfo($movie, PATHINFO_FILENAME)];
            $withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/', '', $id);
            $xml = simplexml_load_file($dir . '/xml/' . $withoutExt . '.xml');
        echo '
';
        if (file_exists($dir . '/xml/' . $withoutExt . '.xml')){
        echo '

' . $xml->title . '

';
        }else{
            echo '

' . $withoutExt . '

';
        }
        echo '
<video width="300" height="400" poster="'. $image .'" preload="none" id="videoElement">';
        echo '<source src="'. $movie .'" type="video/mp4">';
        echo '<track src="' . $sub . '" kind="subtitles" srclang="en-US" label="English" />Your browser does not support HTML5 video.</video>
';
        echo '
' . $xml->overview . '

';
        echo "Added on: " . date("F d Y.", filectime($movie)) . '
';
        echo '

';
    }

Then I have some JS stored in the footer that hides the controls until a user hovers over them:

It works fine. For the first video loaded into the page. The others are unplayable as there's no longer any controls on them. So what can I do to make sure it works for every video?

Body:
First of all, you can only use the same id attribute once on a page. An id is supposed to be unique. Instead of using id="videoElement", I would recommend using a class (eg class="videoElement").

Besides that document.getElementById only returns a single element, so your listeners won't be added to all elements. There are several ways to solve this. One would be looping through all elements and adding the listeners.

document.querySelectorAll('.videoElement').forEach(function (videoElement) {
    // Add listeners to video element: videoElement.addEventListener....
});

Or you could use event delegation here. You could set the events to the body and then check whether or not the target of the event is a videoElement.

document.body.addEventListener('mouseover', function (mouseEvent) {
    if (mouseEvent.target.getAttribute('class') === 'videoElement') {
        // show controls
    }
});


问:
how to set coreSize parameter in Hystrix?
I'm working on a spring boot project about electronic contract recently.And There has an interface of raiseContract().Considering that the traffic of this interface will be large in the future,My Leader let me use Hystrix to defender it.And I did not use it before.I am learning it and trying to use it on the interface.I use ThreadPool Isolation Strategy and I don't konw how to set
the parameter of coreSize reasonable in ThreadPoolProperties.In other words,I want to know what should I follow to set it.

I did a lot of research,but I did not get the answer.All of Answer is about the meaning of coreSize,maxQueueSize etc.

Here is my code:

@HystrixCommand(
            groupKey = "contractGroup",
            commandKey = "raiseContract",
            fallbackMethod = "raiseContractFallback",
            threadPoolProperties = {
                    @HystrixProperty(name = "coreSize", value = "20"),
                    @HystrixProperty(name = "maxQueueSize", value = "150"),
                    @HystrixProperty(name = "queueSizeRejectionThreshold", value = "100")},
            commandProperties = {
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "15000"),
                    @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "5"),
                    @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "50"),
                    @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "3000"),
                    @HystrixProperty(name = "fallback.isolation.semaphore.maxConcurrentRequests", value = "20")
            })
```

答:
As you are already aware of, there are 3 main threadPool configurations:

coreSize: Number of threads that will be maintained in the pool

maxSize: Defines how much extra threads are allowed in case need arises.

maxQueueSize: Queue size of the tasks

Now lets start with an example. Assume there is a service using hystrix, call it HystrixService, for which coreSize = maxSize = n and maxQueueSize = -1 (default case). This means at a time at most 'n' tasks will be executed. Any extra task that is coming will be rejected (fallback will be executed).

So, in ideal scenario, you have to ensure that this HystrixService doesn't reject any request coming to it. You need to know at max how many requests can there be on HystrixService. So if throughput on HystrixService is 10 Requests per second, then max concurrent requests on HystrixService can be 10. Now suppose latency of HystrixService is 2 sec, then by the time it responds to first 10 requests, 10 more requests will come. i.e. total requests = 2 * 10 = 20. So coreSize in this case should be 20.

This is same as mentioned in hystrix documentation,

coreSize = Peak Request per sec × P99 latency + some breathing room

Now, you can keep maxSize and queueSize a bit high, so that it doesn't reject requests, in case there are sudden throughput spikes on your service.


问:
PageSpeed Insights doesn't recognize WebP. format
I have a plugin that converts all the images that in my website to a WebP. next-gen format.
After a simple check, I can see that the images are converted.
the check

But the only problem is that PageSpeed Insights still giving me the same message that I should convert to next-gen format.
https://developers.google.com/speed/pagespeed/insights/?url=https%3A%2F%2Fsuper-zava.co.il%2F%25D7%25A6%25D7%2591%25D7%2599%25D7%25A2%25D7%25AA-%25D7%2593%25D7%2599%25D7%25A8%25D7%2594

Where is the problem?

答:
Maybe you have changed something in the meantime, but it is now delivered properly:

Google Chrome Dev Tools Screenshot

Internet Explorer Dev Tools Screenshot
(somewhat odd; see content-encoding delivered as gzip?)


问:
Set a form's action attribute when submitting?
How do I change a form's action attribute right after clicking the submit button?  </p>

答:
You can do that on javascript side .

```<code><input type="submit" value="Send It!" onClick="return ActionDeterminator();">
</code>```

When clicked, the JavaScript function ActionDeterminator() determines the alternate action URL. Example code.

```<code>function ActionDeterminator() {
  if(document.myform.reason[0].checked == true) {
    document.myform.action = 'http://google.com';
  }
  if(document.myform.reason[1].checked == true) {
    document.myform.action = 'http://microsoft.com';
    document.myform.method = 'get';
  }
  if(document.myform.reason[2].checked == true) {
    document.myform.action = 'http://yahoo.com';
  }
  return true;
}
</code>```

Body:
You can do that on javascript side .

<input type="submit" value="Send It!" onClick="return ActionDeterminator();">

When clicked, the JavaScript function ActionDeterminator() determines the alternate action URL. Example code.

function ActionDeterminator() {
  if(document.myform.reason[0].checked == true) {
    document.myform.action = 'http://google.com';
  }
  if(document.myform.reason[1].checked == true) {
    document.myform.action = 'http://microsoft.com';
    document.myform.method = 'get';
  }
  if(document.myform.reason[2].checked == true) {
    document.myform.action = 'http://yahoo.com';
  }
  return true;
}


问:
How to compile WebAssembly into regular assembly / native code (or Cpp, etc)?
There's plenty of info on compiling to WebAssembly from languages like C++ or Rust, F.e. there's plenty of search resuilts on LLVM to Wasm, but what about WASM to LLVM.

But is there a way to compile from WebAssembly to regular assembly (or other "native" languages like C++ or ASM)?

Note, this is not a duplicate of How to convert javascript to LLVM IR?. This issue is not about converting JavaScript to native code, it is about converting WebAssembly to native code (f.e. native machine code). JavaScript and WebAssembly are different things.

答:
There are few experimental facility exist. The Liftoff (google V8 embedded) compiler for WebAssembly must be doing this task internally; I am not aware of any exposed V8 API for application to use it for this task.

Lucetc says, it can compiles WebAssembly modules (.wasm or .wat files) into native code (.o or .so files).
https://github.com/fastly/lucet

wasm2c: says, it can convert wasm files to C source and header
https://github.com/WebAssembly/wabt/tree/master/wasm2c


问:
Reverse Triangle using stars
I am having to make a triangle and upside down triangle using asterisks for my class.

I have made the top half however, I am having a great deal of trouble making the upside down part

```<code>for(var count=1; count < 8; count++) {
    for(var count2=1; count2<=count; count2++) {
        document.write("*");
       }
    document.write("<br/>");
}
</code>```

Whatever I try, it either does not print at all or prints the result of the above code.

答:
I suppose you're talking about this, when you say "upside down triangle"

```<code>********
*******
******
*****
****
***
**
*
</code>```

If so, then using "just what you have done" so far, you can do it like

```<code>for(var i=0;i<8;i++){
   //for(var spaces=0;spaces<i;spaces++){
        //  document.write(" ");
   //}
   for(var ast=8;ast>i;ast--){
        document.write("*");
   }
   document.write("<br />");
}
</code>```

Also added another loop, if you want "upside down with spaces"

Title: Reverse Triangle using stars

Body:
I am having to make a triangle and upside down triangle using asterisks for my class.

I have made the top half however, I am having a great deal of trouble making the upside down part

for(var count=1; count < 8; count++) {
    for(var count2=1; count2<=count; count2++) {
        document.write("*");
       }
    document.write("
");
}

Whatever I try, it either does not print at all or prints the result of the above code.

Body:
I suppose you're talking about this, when you say "upside down triangle"

********
*******
******
*****
****
***
**
*

If so, then using "just what you have done" so far, you can do it like

for(var i=0;i<8;i++){
   //for(var spaces=0;spaces<i;spaces++){
        //  document.write(" ");
   //}
   for(var ast=8;ast>i;ast--){
        document.write("*");
   }
   document.write("<br />");
}

Also added another loop, if you want "upside down with spaces"


问:
Compile multiple raster extractions to one table
I have 3 rasters in which I have extracted data from using a polyline from a shapefile. Currently, I have the extraction as 3 separate lists. Is there A way I can do an extraction from all three rasters and compile them to one table with different columns for the data from each raster?

This is the current code I am using

```<code>  Harney_Transects <- readOGR(dsn = ".", layer = "Transect_HN")

  MeanTreeHeightHarneyBefore=raster('HN_TrMean_B_Clip.tif')
  ScanAngleHarneyBefore= raster('HNScanAngle_B_Clip.tif')
  MeanShrubHeightHarneyBefore= raster('HN_MeanShrub_B_Clip.tif')

  Extraction_Shrub_Harney= extract(MeanShrubHeightHarneyBefore,Harney_Transects)
  Extraction_Tree_Harney= extract(MeanTreeHeightHarneyBefore,Harney_Transects)
  Extraction_ScanAngle_Harney= extract(ScanAngleHarneyBefore,Harney_Transects)
</code>```

答:
In short, you can stack() all the rasters you want to extract data from, and extract from the stack.

Here's a fully reproducible example using two rasters and a SpatialLines object, like you have in your question. Skip to the last code chunk for a direct answer to your question.

```<code>library(sp)
library(raster)

# function to generate random rasters
gen_raster <- function(){
  r <- raster(nrows = 10, ncols = 10, res = 1,
              xmn = 0, xmx = 10, ymn = 0, ymx = 10,
              vals = rnorm(100, 5, 1))
  return(r)
}

# generate 2 random rasters
r1 <- gen_raster()
r2 <- gen_raster()

# view
par(mfrow = c(1,2))
  plot(r1, main = "raster 1"); plot(r2, main = "raster 2")
dev.off()
</code>```

```<code># generate transect (`SpatialLines` object)
m <- as.matrix(data.frame(x = 5.5, y = seq(0, 10, 1)))
l <- list(Lines(Line(m), "m"))
l <- SpatialLines(l)

# view the transect
plot(r1, main = "raster 1 with transect"); lines(l)
</code>```

Running extract on the stacked rasters returns a list with a matrix in it. The last thing you'll want is to pull this out as a data.frame, which is a bit tricky.

```<code>rs <- stack(r1, r2)            # stack any amount of rasters to extract from
re <- extract(rs, l)           # extract at locations `l`
do.call(rbind.data.frame, re)  # convert to data.frame

    layer.1  layer.2
1  4.586890 5.115136
2  4.780503 5.093281
3  6.877302 3.337345
4  5.913230 3.755099
5  4.907834 4.887160
6  5.576908 5.386136
7  3.572350 5.225392
8  4.778727 5.391765
9  6.600041 4.205841
10 6.946321 5.544172
</code>```

The names of the columns are the names of the raster layers in the stack. You can access these names with names(rs), and modify them with names(rs) <- c("new_name_1", "new_name_2").

Body:
I have 3 rasters in which I have extracted data from using a polyline from a shapefile. Currently, I have the extraction as 3 separate lists. Is there A way I can do an extraction from all three rasters and compile them to one table with different columns for the data from each raster?

This is the current code I am using

  Harney_Transects <- readOGR(dsn = ".", layer = "Transect_HN")

  MeanTreeHeightHarneyBefore=raster('HN_TrMean_B_Clip.tif')
  ScanAngleHarneyBefore= raster('HNScanAngle_B_Clip.tif')
  MeanShrubHeightHarneyBefore= raster('HN_MeanShrub_B_Clip.tif')

  Extraction_Shrub_Harney= extract(MeanShrubHeightHarneyBefore,Harney_Transects)
  Extraction_Tree_Harney= extract(MeanTreeHeightHarneyBefore,Harney_Transects)
  Extraction_ScanAngle_Harney= extract(ScanAngleHarneyBefore,Harney_Transects)

Body:
In short, you can stack() all the rasters you want to extract data from, and extract from the stack.

Here's a fully reproducible example using two rasters and a SpatialLines object, like you have in your question. Skip to the last code chunk for a direct answer to your question.

library(sp)
library(raster)

# function to generate random rasters
gen_raster <- function(){
  r <- raster(nrows = 10, ncols = 10, res = 1,
              xmn = 0, xmx = 10, ymn = 0, ymx = 10,
              vals = rnorm(100, 5, 1))
  return(r)
}

# generate 2 random rasters
r1 <- gen_raster()
r2 <- gen_raster()

# view
par(mfrow = c(1,2))
  plot(r1, main = "raster 1"); plot(r2, main = "raster 2")
dev.off()

# generate transect (`SpatialLines` object)
m <- as.matrix(data.frame(x = 5.5, y = seq(0, 10, 1)))
l <- list(Lines(Line(m), "m"))
l <- SpatialLines(l)

# view the transect
plot(r1, main = "raster 1 with transect"); lines(l)

Running extract on the stacked rasters returns a list with a matrix in it. The last thing you'll want is to pull this out as a data.frame, which is a bit tricky.

rs <- stack(r1, r2)            # stack any amount of rasters to extract from
re <- extract(rs, l)           # extract at locations `l`
do.call(rbind.data.frame, re)  # convert to data.frame

    layer.1  layer.2
1  4.586890 5.115136
2  4.780503 5.093281
3  6.877302 3.337345
4  5.913230 3.755099
5  4.907834 4.887160
6  5.576908 5.386136
7  3.572350 5.225392
8  4.778727 5.391765
9  6.600041 4.205841
10 6.946321 5.544172

The names of the columns are the names of the raster layers in the stack. You can access these names with names(rs), and modify them with names(rs) <- c("new_name_1", "new_name_2").