问:
session-per-request implementation for WCF, NHibernate, and Ninject
I am trying to implement a session-per-request model in my WCF application, and I have read countless documents on this topic, but looks like there is not a complete demonstration of this. I actually came across some very useful articles such as this one:

NHibernate's ISession, scoped for a single WCF-call

but these are all from the old days when NHibernate and Ninject did not have WCF specific implementations, therefore they achieved what I need by implementing their custom service providers, etc. Since both Ninject and NHibernate have WCF support now, I want to keep things consistent by using their modules, but I ended up here...

The basic setup and flow should be something like this:

Set CurrentSessionContext to WcfOperationSessionContext in nhibernate configuration

On service start, begin request, or anywhere around the init time, open session and bind it to the current context

Repositories get the current session instance using SessionFactory.GetCurrentSession() method

Unbind and close session at the end of the lifecycle

My initial problem was that I wasn't able to access to the wcf lifecycle to handle my bindings. After digging into the ninject code a bit, I managed to hook my methods to ServiceHost's Opening / Closing events without changing much, but then I wasn't able to access to the OperationContext since it is thread-static.

Later I tried enabling asp.net compatibility and using Application_BeginRequest and Application_EndRequest, and it looked very promising, but I don't think that's the best solution since I should be binding stuff to the service instance, rather than the http request.

Has anyone ever achieved this using ninject's built-in wcf extension libraries? Or any ideas on what I might be doing wrong?

答:
Hy

You can do the following:

```<code>public class DomainModule : NinjectModule
{
    private const string RealSessionIndicator = "RealSession";

    private readonly ProxyGenerator proxyGenerator = new ProxyGenerator();

    public override void Load()
    {
        this.Bind<ISession>().ToMethod(ctx => ctx.Kernel.Get<ISessionFactory>().OpenSession())
            .When(r => r.Parameters.Any(p => p.Name == RealSessionIndicator))
            .InRequestScope();

        this.Bind<Func<ISession>>().ToMethod(ctx => () => ctx.Kernel.Get<ISession>(new Parameter(RealSessionIndicator, (object)null, true)));

        this.Bind<ISession>()
            .ToMethod(this.CreateSessionProxy)
            .InTransientScope();

        this.Bind<ISessionFactory>().ToMethod(ctx => ctx.Kernel.Get<Configuration>().BuildSessionFactory()).InSingletonScope();
    }

    private ISession CreateSessionProxy(IContext ctx)
    {
        var session = (ISession)this.proxyGenerator.CreateInterfaceProxyWithoutTarget(typeof(ISession), new[] { typeof(ISessionImplementor) }, ctx.Kernel.Get<SessionInterceptor>());
        return session;
    }
}

public class SessionInterceptor : IInterceptor
{
    private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

    private readonly Func<ISession> sessionProvider;

    public SessionInterceptor(Func<ISession> sessionProvider)
    {
        this.sessionProvider = sessionProvider;
    }

    public void Intercept(IInvocation invocation)
    {
        try
        {
            var session = this.sessionProvider();
            invocation.ReturnValue = invocation.Method.Invoke(session, invocation.Arguments);
        }
        catch (TargetInvocationException exception)
        {
            Log.Error(exception);
            throw;
        }
    }
}
</code>```

With that you can use everywhere ISession without caring about the details. You can edit InRequestScope with InScope(ctx => OperationContext.Current) to use WCF scope

Body:
Hy

You can do the following:

public class DomainModule : NinjectModule
{
    private const string RealSessionIndicator = "RealSession";

    private readonly ProxyGenerator proxyGenerator = new ProxyGenerator();

    public override void Load()
    {
        this.Bind<ISession>().ToMethod(ctx => ctx.Kernel.Get<ISessionFactory>().OpenSession())
            .When(r => r.Parameters.Any(p => p.Name == RealSessionIndicator))
            .InRequestScope();

        this.Bind<Func<ISession>>().ToMethod(ctx => () => ctx.Kernel.Get<ISession>(new Parameter(RealSessionIndicator, (object)null, true)));

        this.Bind<ISession>()
            .ToMethod(this.CreateSessionProxy)
            .InTransientScope();

        this.Bind<ISessionFactory>().ToMethod(ctx => ctx.Kernel.Get<Configuration>().BuildSessionFactory()).InSingletonScope();
    }

    private ISession CreateSessionProxy(IContext ctx)
    {
        var session = (ISession)this.proxyGenerator.CreateInterfaceProxyWithoutTarget(typeof(ISession), new[] { typeof(ISessionImplementor) }, ctx.Kernel.Get<SessionInterceptor>());
        return session;
    }
}

public class SessionInterceptor : IInterceptor
{
    private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

    private readonly Func<ISession> sessionProvider;

    public SessionInterceptor(Func<ISession> sessionProvider)
    {
        this.sessionProvider = sessionProvider;
    }

    public void Intercept(IInvocation invocation)
    {
        try
        {
            var session = this.sessionProvider();
            invocation.ReturnValue = invocation.Method.Invoke(session, invocation.Arguments);
        }
        catch (TargetInvocationException exception)
        {
            Log.Error(exception);
            throw;
        }
    }
}

With that you can use everywhere ISession without caring about the details. You can edit InRequestScope with InScope(ctx => OperationContext.Current) to use WCF scope


来源: stackexchange

问:
I have a props with JSX.Element. How do I mock the same in the jest test
Im my react component, I have props like below

```<code>interface Props {
  errorMessage: JSX.Element;
  addCommentCancel: () => void;
  triggerAddComment: () => void;
  showAddCommentForm: boolean;
}
</code>```

and in the test I'm invoking the component like below

```<code>const props =  {
  errorMessage: {},
  addCommentCancel: () => jest.fn(),
  triggerAddComment: () => jest.fn(),
  showAddCommentForm: true
};
let wrapper = mount(<IncidentCommentList {...props} />);
</code>```

I get an error

```<code>Type '{}' is not assignable to type 'Element'.
</code>```

How do I mock props element?

答:
```<code>const props =  {
  errorMessage: <div>errorMessage</div>,
  addCommentCancel: () => jest.fn(),
  triggerAddComment: () => jest.fn(),
  showAddCommentForm: true
};
let wrapper = mount(<IncidentCommentList {...props} />);
</code>```

<p>The above code fixed the issue. </p>

ContentLicense: CC BY-SA 4.0

Body:
Im my react component, I have props like below

interface Props {
  errorMessage: JSX.Element;
  addCommentCancel: () => void;
  triggerAddComment: () => void;
  showAddCommentForm: boolean;
}

and in the test I'm invoking the component like below

const props =  {
  errorMessage: {},
  addCommentCancel: () => jest.fn(),
  triggerAddComment: () => jest.fn(),
  showAddCommentForm: true
};
let wrapper = mount(<IncidentCommentList {...props} />);

I get an error

Type '{}' is not assignable to type 'Element'.

How do I mock props element?

Body:
const props =  {
  errorMessage: <div>errorMessage</div>,
  addCommentCancel: () => jest.fn(),
  triggerAddComment: () => jest.fn(),
  showAddCommentForm: true
};
let wrapper = mount(<IncidentCommentList {...props} />);

The above code fixed the issue.


问:
All combinations for specific arrangement- python
Id like to compute all the combination for different, specific arrangements and put them in separate list.

The general form of an element is like [[a#, b#], [a#, b#]], where the # can take on numbers from 1-4.

The goal is to make a list of all the possible elements that can result for a certain form/arrangement. like if the form is [[ai, bi], [ai, bi]], where i can be a value from 1-4.
The output for this form would look like this,

```<code>A=[[[a1, b1], [a1, b1]], [[a2, b2], [a2, b2]], [[a3, b3], [a3, b3]], [[a4, b4], [a4, b4]]]
</code>```

Other i form is [[ai, bj], [ai, bj]], where i and j can be value from 1-4 but i≠j, and the outcome would be a list of all the possibilities. Similarly, the form could be [[ai, bj], [ak, bj]], where i, j, k can ba values from 1-4 but i≠j≠k

Im not even sure how to approach this so there isn't any code Ive started on so Im just looking for input or advice on how to work this out.

答:
A = [[[f'a{i}', f'b{i}']] * 2 for i in range(1, 5)]
```

<p>For earliear versions of python that don't support f strings</p>

<pre class="lang-py prettyprint-override"><code>A = [[['a' + str(i), 'b' + str(i)]] * 2 for i in range(1, 5)]
</code>```

Body:
Id like to compute all the combination for different, specific arrangements and put them in separate list.

The general form of an element is like [[a#, b#], [a#, b#]], where the # can take on numbers from 1-4.

The goal is to make a list of all the possible elements that can result for a certain form/arrangement. like if the form is [[ai, bi], [ai, bi]], where i can be a value from 1-4.
The output for this form would look like this,

A=[[[a1, b1], [a1, b1]], [[a2, b2], [a2, b2]], [[a3, b3], [a3, b3]], [[a4, b4], [a4, b4]]]

Other i form is [[ai, bj], [ai, bj]], where i and j can be value from 1-4 but i≠j, and the outcome would be a list of all the possibilities. Similarly, the form could be [[ai, bj], [ak, bj]], where i, j, k can ba values from 1-4 but i≠j≠k

Im not even sure how to approach this so there isn't any code Ive started on so Im just looking for input or advice on how to work this out.

Body:
A = [[[f'a{i}', f'b{i}']] * 2 for i in range(1, 5)]

For earliear versions of python that don't support f strings

A = [[['a' + str(i), 'b' + str(i)]] * 2 for i in range(1, 5)]


问:
SwiftUI: how to make network request when value of binding changes?
I'm trying to update my chart view when I swipe to another page in PagerView. When I swipe currentIndex changes. But I don't understand how to get notified when currentIndex changes.
Here is my code:

```<code>struct MainView: View {

    let network = Network()

    @State private var currentIndex: Int = 0
    @State private var sources: [Source] = []

    var body: some View {
        return ZStack {
            ...
            VStack {
                Text("Температура")
                    .defaultFont(font: .system(size: 30), weight: .regular)

                PagerView(pagesCount: self.sources.count, currentIndex: self.$currentIndex) {
                    ForEach(self.sources, id: \.self) { t in
                        ...
                    }
                }

                if !sources.isEmpty {
                    ChartView(sourceId: $sources[currentIndex].id)
                } else {
                    Spacer()
                }
            }
        }
    }

}
</code>```

PagerView binds to currentIndex so when I swipe a page currentIndex changes. ChartView has a method loadData and I want to call it when I swipe a page to load new chart depends on sources[currentIndex].id. Here is code of ChartView:

```<code>struct ChartView: View {

    @Binding var sourceId: String {
        didSet {
            loadData()
        }
    }
    @State private var points: [TemperaturePoint] = []

    var body: some View {
        GeometryReader { proxy in
            ChartView.makePath(by: self.points, with: proxy)
                .stroke(Color.white, lineWidth: 2)
        }.onAppear(perform: loadData)
    }

    func loadData() {
        network.getPoints(sourceId: sourceId) { response in
            switch response {
            case .result(let array):
                self.points = TemperaturePoint.smooth(points: array.results)

            case .error(let error):
                print(error)
            }
        }
    }

}
</code>```

So question is how to make network calling when Binding variable changes? Or maybe I mistake and need to use another way to code this.

答:
When bound state is changed only body of view with @Binding is called, ie. actually only refresh happens, and only of part dependent of bound state, so even .onAppear is not called.

Taking about into account and the fact that entire ChartView depends on new identifier, the solution would be to make force refresh view as whole, so .onAppear called again, and loaded new data.

Here is code. Tested with Xcode 11.4 / iOS 13.4 (with some simplified replicated version).

```<code>struct ChartView: View {

    @Binding var sourceId: String   // << just binding
    @State private var points: [TemperaturePoint] = []

    var body: some View {
        GeometryReader { proxy in
            ChartView.makePath(by: self.points, with: proxy)
                .stroke(Color.white, lineWidth: 2)
        }
        .id(sourceId)                    // << here !!
        .onAppear(perform: loadData)
    }

    func loadData() {
        network.getPoints(sourceId: sourceId) { response in
            switch response {
            case .result(let array):
                self.points = TemperaturePoint.smooth(points: array.results)

            case .error(let error):
                print(error)
            }
        }
    }
}
</code>```

Body:
I'm trying to update my chart view when I swipe to another page in PagerView. When I swipe currentIndex changes. But I don't understand how to get notified when currentIndex changes.
Here is my code:

struct MainView: View {

    let network = Network()

    @State private var currentIndex: Int = 0
    @State private var sources: [Source] = []

    var body: some View {
        return ZStack {
            ...
            VStack {
                Text("Температура")
                    .defaultFont(font: .system(size: 30), weight: .regular)

                PagerView(pagesCount: self.sources.count, currentIndex: self.$currentIndex) {
                    ForEach(self.sources, id: \.self) { t in
                        ...
                    }
                }

                if !sources.isEmpty {
                    ChartView(sourceId: $sources[currentIndex].id)
                } else {
                    Spacer()
                }
            }
        }
    }

}

PagerView binds to currentIndex so when I swipe a page currentIndex changes. ChartView has a method loadData and I want to call it when I swipe a page to load new chart depends on sources[currentIndex].id. Here is code of ChartView:

struct ChartView: View {

    @Binding var sourceId: String {
        didSet {
            loadData()
        }
    }
    @State private var points: [TemperaturePoint] = []

    var body: some View {
        GeometryReader { proxy in
            ChartView.makePath(by: self.points, with: proxy)
                .stroke(Color.white, lineWidth: 2)
        }.onAppear(perform: loadData)
    }

    func loadData() {
        network.getPoints(sourceId: sourceId) { response in
            switch response {
            case .result(let array):
                self.points = TemperaturePoint.smooth(points: array.results)

            case .error(let error):
                print(error)
            }
        }
    }

}

So question is how to make network calling when Binding variable changes? Or maybe I mistake and need to use another way to code this.

Body:
When bound state is changed only body of view with @Binding is called, ie. actually only refresh happens, and only of part dependent of bound state, so even .onAppear is not called.

Taking about into account and the fact that entire ChartView depends on new identifier, the solution would be to make force refresh view as whole, so .onAppear called again, and loaded new data.

Here is code. Tested with Xcode 11.4 / iOS 13.4 (with some simplified replicated version).

struct ChartView: View {

    @Binding var sourceId: String   // << just binding
    @State private var points: [TemperaturePoint] = []

    var body: some View {
        GeometryReader { proxy in
            ChartView.makePath(by: self.points, with: proxy)
                .stroke(Color.white, lineWidth: 2)
        }
        .id(sourceId)                    // << here !!
        .onAppear(perform: loadData)
    }

    func loadData() {
        network.getPoints(sourceId: sourceId) { response in
            switch response {
            case .result(let array):
                self.points = TemperaturePoint.smooth(points: array.results)

            case .error(let error):
                print(error)
            }
        }
    }
}


问:
Pandas vs Numpy Speed
With Pandas using Numpy under the hood I was curious as to why straight numpy code (509 ms) was 12x faster than doing the same operation with a dataframe (6.38 s) in the example below?</p>

```<code># function with numpy arrays
def f_np(freq, asd):
    for f in np.arange(21.,2000.,1.):
        fi = freq/f
        gi =  (1+fi**2) / ((1-fi**2)**2 + fi**2) * asd
        df['fi'] = fi
        df['gi'] = gi
        # process each df ...

# function with dataframe
def f_df(df):
    for f in np.arange(21.,2000.,1.):
        df['fi'] = df.Freq/f
        df['gi'] = (1+df.fi**2) / ((1-df.fi**2)**2 + df.fi**2) * df.ASD
        # process each df ...

freq =  np.arange(20., 2000., .1)
asd = np.ones(len(freq))
df = pd.DataFrame({'Freq':freq, 'ASD':asd})

%timeit f_np(freq, asd)
%timeit f_df(df)

509 ms ± 723 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)
6.38 s ± 20.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
</code>```

答:
Are you sure that the difference in speed is because of "some operation with a dataframe" in this specific case? I think the difference in speed is attributed to the fact that you created fi and gi variables and assigned the variables on the columns in the first example, but you didn't do that in the second example. The results were similar when I assigned a variable in both.

```<code>import pandas as pd,numpy as np
# function with numpy arrays
def f_np(freq, asd):
    for f in np.arange(21.,2000.,1.):
        fi = freq/f
        gi =  (1+fi**2) / ((1-fi**2)**2 + fi**2) * asd
        df['fi'] = fi
        df['gi'] = gi
        # process each df ...

# function with dataframe
def f_df(df):
    for f in np.arange(21.,2000.,1.):
        fi = freq/f
        gi =  (1+fi**2) / ((1-fi**2)**2 + fi**2) * asd
        df['fi'] = fi
        df['gi'] = gi
        # process each df ...

freq =  np.arange(20., 2000., .1)
asd = np.ones(len(freq))
df = pd.DataFrame({'Freq':freq, 'ASD':asd})

%timeit f_np(freq, asd)
%timeit f_df(df)
#562 ms ± 9.23 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
#569 ms ± 17.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
</code>```

Title: Pandas vs Numpy Speed

Body:
With Pandas using Numpy under the hood I was curious as to why straight numpy code (509 ms) was 12x faster than doing the same operation with a dataframe (6.38 s) in the example below?

# function with numpy arrays
def f_np(freq, asd):
    for f in np.arange(21.,2000.,1.):
        fi = freq/f
        gi =  (1+fi**2) / ((1-fi**2)**2 + fi**2) * asd
        df['fi'] = fi
        df['gi'] = gi
        # process each df ...

# function with dataframe
def f_df(df):
    for f in np.arange(21.,2000.,1.):
        df['fi'] = df.Freq/f
        df['gi'] = (1+df.fi**2) / ((1-df.fi**2)**2 + df.fi**2) * df.ASD
        # process each df ...

freq =  np.arange(20., 2000., .1)
asd = np.ones(len(freq))
df = pd.DataFrame({'Freq':freq, 'ASD':asd})

%timeit f_np(freq, asd)
%timeit f_df(df)

509 ms ± 723 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)
6.38 s ± 20.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

Body:
Are you sure that the difference in speed is because of "some operation with a dataframe" in this specific case? I think the difference in speed is attributed to the fact that you created fi and gi variables and assigned the variables on the columns in the first example, but you didn't do that in the second example. The results were similar when I assigned a variable in both.

import pandas as pd,numpy as np
# function with numpy arrays
def f_np(freq, asd):
    for f in np.arange(21.,2000.,1.):
        fi = freq/f
        gi =  (1+fi**2) / ((1-fi**2)**2 + fi**2) * asd
        df['fi'] = fi
        df['gi'] = gi
        # process each df ...

# function with dataframe
def f_df(df):
    for f in np.arange(21.,2000.,1.):
        fi = freq/f
        gi =  (1+fi**2) / ((1-fi**2)**2 + fi**2) * asd
        df['fi'] = fi
        df['gi'] = gi
        # process each df ...

freq =  np.arange(20., 2000., .1)
asd = np.ones(len(freq))
df = pd.DataFrame({'Freq':freq, 'ASD':asd})

%timeit f_np(freq, asd)
%timeit f_df(df)
#562 ms ± 9.23 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
#569 ms ± 17.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)


问:
How to use withDefault wrappers?
I want to use MutableMap with defaults:

```<code>val myMap = mutableMapOf<String, Set<String>>().withDefault { mutableSetOf() }
</code>```

but I can't use getOrImplicitDefault method because withDefault returns MutableMap<String, Set<String>> type. Moreover, I can't cast to MutableMapWithDefault interface because this is a private interface.

I can't use get method either because it returns a nullable type. It is ok because this is a method on the MutableMap interface (moreover it doesn't call defaultValue callback for take default value).

Seems like this functionality is not correctly implemented in Kotlin,  or I am using it wrong. So, how do I use withDefault wrappers correctly?

答:
Map withDefault returns a wrapper of the read-only map, having the implicit default value provided with the specified function defaultValue. This implicit default value is used when the original map doesn't contain a value for the key specified. See the usage of getOrElse & getOrDefault as well that works the same way as for lists -

```<code>val emojiMap = mapOf("Grinning Face" to "", "Winking face" to "",
                "Zany face" to "").withDefault({""})

fun main()
{
    println(emojiMap) // {Grinning Face=, Winking face=, Zany face=}
    println(emojiMap.getValue("Grinning Face")) //
    println(emojiMap.getValue("Zany face")) //
    println(emojiMap.getValue("Smiling face")) //

    // getOrElse & getOrDefault
    println(emojiMap.getOrElse("Flushed face"){""}) //
    println(emojiMap.getOrDefault("Sad face","")) //
}
</code>```

ContentLicense: CC BY-SA 3.0

Title: How to use withDefault wrappers?

Body:
I want to use MutableMap with defaults:

val myMap = mutableMapOf<String, Set<String>>().withDefault { mutableSetOf() }

but I can't use getOrImplicitDefault method because withDefault returns MutableMap<String, Set<String>> type. Moreover, I can't cast to MutableMapWithDefault interface because this is a private interface.

I can't use get method either because it returns a nullable type. It is ok because this is a method on the MutableMap interface (moreover it doesn't call defaultValue callback for take default value).

Seems like this functionality is not correctly implemented in Kotlin,  or I am using it wrong. So, how do I use withDefault wrappers correctly?

Body:
Map withDefault returns a wrapper of the read-only map, having the implicit default value provided with the specified function defaultValue. This implicit default value is used when the original map doesn't contain a value for the key specified. See the usage of getOrElse & getOrDefault as well that works the same way as for lists -

val emojiMap = mapOf("Grinning Face" to "", "Winking face" to "",
                "Zany face" to "").withDefault({""})

fun main()
{
    println(emojiMap) // {Grinning Face=, Winking face=, Zany face=}
    println(emojiMap.getValue("Grinning Face")) //
    println(emojiMap.getValue("Zany face")) //
    println(emojiMap.getValue("Smiling face")) //

    // getOrElse & getOrDefault
    println(emojiMap.getOrElse("Flushed face"){""}) //
    println(emojiMap.getOrDefault("Sad face","")) //
}


问:
Python WebSockets using socketio and eventlet - cannot kill eventlet server
Im adding a websocket server to my app so it can communicate with a web-based version of itself.
For this I am using eventlet.

The problem i am running in to is once I start the server, I cannot get it to die. Even if i close my application, the process remains running in the background while the server stays alive. I have been googling and testing random things for days now and just cannot get this to happen. Im hoping someone here can help me.

Currently, i have a singleton class with a function that starts the listening:

```<code>    def run_server(self, addr, port):
        eventlet.wsgi.server(eventlet.listen((addr, port)), self.APP)
</code>```

and for info, the app is:

```<code>socketio.WSGIApp(SIO, static_files={'/': {'content_type': 'text/html',
                                                    'filename': 'index.html'}})
</code>```

and then I am starting this server on a thread in the app:

```<code>def run_on_thread(addr, port):
    obj = WebSocket()
    t = threading.Thread(target=obj.run_server, args=(addr, port))
    t.setDaemon(True)
    return obj, t
</code>```

The thread gets started in my application, and as mentioned - everything works fine. All my messages are sent and received.

But nothing I can find will kill this server until i End Task on the python process.

答:
The eventlet web server does not currently have a good stop mechanism when using WebSocket. The following warning is in the documentation:

Warning: At the moment server() will always wait for active connections to finish before exiting, even if there’s an exception raised inside it (all exceptions are handled the same way, including greenlet.GreenletExit and those inheriting from BaseException).

While this may not be an issue normally, when it comes to long running HTTP connections (like eventlet.websocket) it will become problematic and calling wait() on a thread that runs the server may hang, even after using kill(), as long as there are active connections.

Body:
Im adding a websocket server to my app so it can communicate with a web-based version of itself.
For this I am using eventlet.

The problem i am running in to is once I start the server, I cannot get it to die. Even if i close my application, the process remains running in the background while the server stays alive. I have been googling and testing random things for days now and just cannot get this to happen. Im hoping someone here can help me.

Currently, i have a singleton class with a function that starts the listening:

    def run_server(self, addr, port):
        eventlet.wsgi.server(eventlet.listen((addr, port)), self.APP)

and for info, the app is:

socketio.WSGIApp(SIO, static_files={'/': {'content_type': 'text/html',
                                                    'filename': 'index.html'}})

and then I am starting this server on a thread in the app:

def run_on_thread(addr, port):
    obj = WebSocket()
    t = threading.Thread(target=obj.run_server, args=(addr, port))
    t.setDaemon(True)
    return obj, t

The thread gets started in my application, and as mentioned - everything works fine. All my messages are sent and received.

But nothing I can find will kill this server until i End Task on the python process.


问:
Interactive Graph and Tree visualization for Desktop Application in Haskell
I have seen that there are bindings to graphviz, but it appears to just work with the Dot language and so I'm assuming that it would be more appropriate for static visualization. We have more of a dynamic, often updated, interactive need for automatic laying out and working with graphs and trees. Is there an appropriate library for such a thing in Haskell?

I need something that will work on at least both Linux and Windows

答: There are bindings to ubigraph, a closed source graph visualization program/library that has a free-ware version available for download.  Ubigraph is interactive, fairly fast, and really damned easy to use given the hubigraph bindings - see an old answer of mine for a code example.</p>


问:
How do I use arrow functions in React
I'm working on a node and react project where when i try to use an arrow function, it brings the error that the function is not defined. I've tried following every tutorial I can get but I'm not able to solve this. Here is my code;

```<code>import React from 'react';
import './App.css';
import {BrowserRouter as Router, Link} from 'react-router-dom';

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state={apiResponse:""}
  }

  callAPI(){
    fetch("http://localhost:9000/testAPI")
    .then(res=> res.text())
    .then(res=> this.setState({apiResponse: res}));
  }

  componentWillMount(){
    this.callAPI();
  }

  Trial = ()=>{
    return(
      <div>this is my div</div>
    );
  }

render(){
  return (
    <div className="App">
      <p>{this.state.apiResponse}</p>

      <Trial />

    </div>
  );
}
}

export default App;
</code>```

and the error message i'm getting is:

```<code>./src/App.js
  Line 33:8:  'Trial' is not defined  react/jsx-no-undef

Search for the keywords to learn more about each error.
This error occurred during the build time and cannot be dismissed
</code>```

any assistance will be highly appreciated

答:
Better way to write Trial component is outside to the class either in same file or in other file depend on reusability.

```<code>//outside class in  different file

export function Trial(){
    return(
      <div>this is my div</div>
    );
  }

//outside class in a same  file

function Trial(){
        return(
          <div>this is my div</div>
        );
      }
</code>```

If you don't want to do this,

you should write {this.Trail()} instead <Trial/> as Trial is a class instance method and not a component.

Title: How do I use arrow functions in React

Body:
I'm working on a node and react project where when i try to use an arrow function, it brings the error that the function is not defined. I've tried following every tutorial I can get but I'm not able to solve this. Here is my code;

import React from 'react';
import './App.css';
import {BrowserRouter as Router, Link} from 'react-router-dom';

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state={apiResponse:""}
  }

  callAPI(){
    fetch("http://localhost:9000/testAPI")
    .then(res=> res.text())
    .then(res=> this.setState({apiResponse: res}));
  }

  componentWillMount(){
    this.callAPI();
  }

  Trial = ()=>{
    return(

this is my div

    );
  }

render(){
  return (

{this.state.apiResponse}

      <Trial />

  );
}
}

export default App;

and the error message i'm getting is:

./src/App.js
  Line 33:8:  'Trial' is not defined  react/jsx-no-undef

Search for the keywords to learn more about each error.
This error occurred during the build time and cannot be dismissed

any assistance will be highly appreciated

Body:
Better way to write Trial component is outside to the class either in same file or in other file depend on reusability.

//outside class in  different file

export function Trial(){
    return(
      <div>this is my div</div>
    );
  }

//outside class in a same  file

function Trial(){
        return(
          <div>this is my div</div>
        );
      }

If you don't want to do this,

you should write {this.Trail()} instead <Trial/> as Trial is a class instance method and not a component.


问:
MySQL (MariaDB) get row position in ORDER BY
First of all, I already looked at this post but I couldnt get it to work.

So I wanna get the position of a row in my table with a group by argument. E.g. I have an table with various statistics in it. It looks like this.

```<code>+-----------+-------+--------+------+--------------+
| player_id | kills | deaths | wins | played_games |
+-----------+-------+--------+------+--------------+
|         1 |     0 |      2 |    1 |            3 |
|         2 |     0 |      1 |    0 |            1 |
|         3 |     0 |      0 |    0 |            0 |
|         5 |     0 |      1 |    0 |            1 |
|        13 |     0 |      1 |    0 |            1 |
|        14 |     3 |      1 |    2 |            3 |
|        29 |     0 |      3 |    0 |            3 |
|        30 |     0 |      1 |    0 |            2 |
|        32 |     5 |      0 |    1 |            1 |
+-----------+-------+--------+------+--------------+
</code>```

So I wanna display the players rank position sorted by wins.
In this example player 14 would be Rank #1.

I can do a simple SELECT * FROM tbl ORDER BY wins ASC then enumerate the rows, but it seems wasteful to load a potentially large resultset just for a single row.

答:
If you are running MariaDB 10.3 or higher, just want use window functions :

```<code>select t.*, rank() over(order by wins desc) rn
from mytable t
order by wins desc
</code>```

rank() assigns the same score to players that have the same number of wins.

In earlier versions, one emulation option is a correlated subquery:

```<code>select
    t.*,
    (select count(*) + 1 from mytable t1 where t1.wins > t.wins) rn
from mytable t
order by wins desc
</code>```

Body:
First of all, I already looked at this post but I couldnt get it to work.

So I wanna get the position of a row in my table with a group by argument. E.g. I have an table with various statistics in it. It looks like this.

+-----------+-------+--------+------+--------------+
| player_id | kills | deaths | wins | played_games |
+-----------+-------+--------+------+--------------+
|         1 |     0 |      2 |    1 |            3 |
|         2 |     0 |      1 |    0 |            1 |
|         3 |     0 |      0 |    0 |            0 |
|         5 |     0 |      1 |    0 |            1 |
|        13 |     0 |      1 |    0 |            1 |
|        14 |     3 |      1 |    2 |            3 |
|        29 |     0 |      3 |    0 |            3 |
|        30 |     0 |      1 |    0 |            2 |
|        32 |     5 |      0 |    1 |            1 |
+-----------+-------+--------+------+--------------+

So I wanna display the players rank position sorted by wins.
In this example player 14 would be Rank #1.

I can do a simple SELECT * FROM tbl ORDER BY wins ASC then enumerate the rows, but it seems wasteful to load a potentially large resultset just for a single row.

Body:
If you are running MariaDB 10.3 or higher, just want use window functions :

select t.*, rank() over(order by wins desc) rn
from mytable t
order by wins desc

rank() assigns the same score to players that have the same number of wins.

In earlier versions, one emulation option is a correlated subquery:

select
    t.*,
    (select count(*) + 1 from mytable t1 where t1.wins > t.wins) rn
from mytable t
order by wins desc


问:
How to emit only last request in subscribe method
I send a form using complex logic inside concatMap operator. And all works fine, but in my subscribe method I have additional http requests, which fires 2 times instead of 1. I tried to use takeLast operator, but it didn't help. I need to make requests in subscribe method only after all requests completed in concatMap.
For example, if I have two updateMember() events, after each request I get invoked function from subscribe method.

```<code>makeRequest(value) {
  const team = toFormData(this.form.controls.profile.value);

  this.teamService
  .sendTeam(team)
  .pipe(
    concatMap(() => {
      const tasks$ = [];

      for (const [index, m] of this.form.controls.members.value.entries()) {
        const member = toFormData(m);

        if (m.id) {
          tasks$.push(this.teamService.updateMember(member, m.id).pipe(take(1)));
        } else if (!member.entries().next().done) {
          if (member.has('name')) {
            tasks$.push(this.teamService.createMember(member).pipe(take(1)));
          } else {
            (this.form.controls.members as FormArray).controls[index].markAllAsTouched();
            this.scrollToFirstInvalidControl();
          }
        }
      }

      return tasks$.length ? concat(...tasks$) : EMPTY;
    }),
  )
  .subscribe(() => {

    this.teamService.getMembers().subscribe();

  });
}
</code>```

What should I use to make only necessary requests?

答:
I think using takeLast was a good idea, but it's important where you place it:

this.teamService
  .sendTeam(tem)
  .pipe(
    concatMap(() => {
      /* ... */

      return tasks.length ? concat(...tasks).pipe(last()) : EMPTY;
    }),

    // You can avoid to manually subscribe in the `subscribe`'s callback
    switchMap(() => this.teamService.getMembers())
  ).subscribe()
```

<p><code>concatMap</code> subscribes to the an observable resulted from calling the provided callback function.</p>

<p>In this case, that observable can either be <code>concat(...tasks).pipe(last())</code> or <code>EMPTY</code>.</p>

<p>If it's <code>concat(...tasks).pipe(last())</code>, it will wait for each <code>task</code> to complete. <code>last</code> also uses <code>takeLast(1)</code> internally.</p>

<p>By the way, <code>concat</code> can be seen this way:</p>

<pre class="lang-js prettyprint-override"><code>// concat(observables[])

from(observables[])
  .pipe(
    mergeMap(obs$ => obs$, 1)
  )
</code>```

If it's EMPTY, it will immediately send a complete notification, meaning that teamService.getMembers() from  switchMap(() => this.teamService.getMembers()) won't be invoked.

Body:
I send a form using complex logic inside concatMap operator. And all works fine, but in my subscribe method I have additional http requests, which fires 2 times instead of 1. I tried to use takeLast operator, but it didn't help. I need to make requests in subscribe method only after all requests completed in concatMap.
For example, if I have two updateMember() events, after each request I get invoked function from subscribe method.

makeRequest(value) {
  const team = toFormData(this.form.controls.profile.value);

  this.teamService
  .sendTeam(team)
  .pipe(
    concatMap(() => {
      const tasks$ = [];

      for (const [index, m] of this.form.controls.members.value.entries()) {
        const member = toFormData(m);

        if (m.id) {
          tasks$.push(this.teamService.updateMember(member, m.id).pipe(take(1)));
        } else if (!member.entries().next().done) {
          if (member.has('name')) {
            tasks$.push(this.teamService.createMember(member).pipe(take(1)));
          } else {
            (this.form.controls.members as FormArray).controls[index].markAllAsTouched();
            this.scrollToFirstInvalidControl();
          }
        }
      }

      return tasks$.length ? concat(...tasks$) : EMPTY;
    }),
  )
  .subscribe(() => {

    this.teamService.getMembers().subscribe();

  });
}

What should I use to make only necessary requests?

Body:
I think using takeLast was a good idea, but it's important where you place it:

this.teamService
  .sendTeam(tem)
  .pipe(
    concatMap(() => {
      /* ... */

      return tasks.length ? concat(...tasks).pipe(last()) : EMPTY;
    }),

    // You can avoid to manually subscribe in the `subscribe`'s callback
    switchMap(() => this.teamService.getMembers())
  ).subscribe()

concatMap subscribes to the an observable resulted from calling the provided callback function.

In this case, that observable can either be concat(...tasks).pipe(last()) or EMPTY.

If it's concat(...tasks).pipe(last()), it will wait for each task to complete. last also uses takeLast(1) internally.

By the way, concat can be seen this way:

// concat(observables[])

from(observables[])
  .pipe(
    mergeMap(obs$ => obs$, 1)
  )

If it's EMPTY, it will immediately send a complete notification, meaning that teamService.getMembers() from  switchMap(() => this.teamService.getMembers()) won't be invoked.


问:
How new keyword works in c#
There's a class which is compiled into a dll

```<code>//HeaderFile.h
//version 1.0
class __declspec(dllexport) A {
    int variable;
//member functions omitted for clarity
};

//implementation file omitted for clarity
</code>```

You build an exe which uses above class from the dll it was compiled into

```<code>#include "HeaderFile.h"
int main() {
    A *obj = new A();
    obj->CallSomeFuncOnObj();

    //
    //whatever
    //
}
</code>```

Up til now your program is working fine. But now you recompile your dll with the following code

```<code>//HeaderFile.h
//version 2.0
class __declspec(dllexport) A {
    int variable;
    int anotherVariable;
//member functions omitted for clarity
};

//implementation file omitted for clarity
</code>```

and you do not recompile your exe but start using the recompiled dll from the old exe. What will happen now is that your exe has the code that will allocate memory = sizeof(class A version 1.0) but the constructor in your new dll has code that assumes it is being passed a memory block = sizeof(class A version 2.0). There's an integer's worth size difference between the two - a recipe for unpredictability.

A similar example is shown in the first chapter of an excellent book - Essential COM by Don Box. Now for the question. In a similar situation in c#(or any other .Net languare) what would happen?

答: In COM, your DLL implements an object factory: The DLL creates the object itself in order to avoid such 'syncho' problems. In .NET, the CLR instantiates the object based on type knowledge pulled from the DLL where the type is implemented. In both cases, the problem you mention is avoided.</p>

OwnerDisplayName: Raminder

ContentLicense: CC BY-SA 2.5

Title: How new keyword works in c#

Body:
There's a class which is compiled into a dll

//HeaderFile.h
//version 1.0
class __declspec(dllexport) A {
    int variable;
//member functions omitted for clarity
};

//implementation file omitted for clarity

You build an exe which uses above class from the dll it was compiled into

#include "HeaderFile.h"
int main() {
    A *obj = new A();
    obj->CallSomeFuncOnObj();

    //
    //whatever
    //
}

Up til now your program is working fine. But now you recompile your dll with the following code

//HeaderFile.h
//version 2.0
class __declspec(dllexport) A {
    int variable;
    int anotherVariable;
//member functions omitted for clarity
};

//implementation file omitted for clarity

and you do not recompile your exe but start using the recompiled dll from the old exe. What will happen now is that your exe has the code that will allocate memory = sizeof(class A version 1.0) but the constructor in your new dll has code that assumes it is being passed a memory block = sizeof(class A version 2.0). There's an integer's worth size difference between the two - a recipe for unpredictability.

A similar example is shown in the first chapter of an excellent book - Essential COM by Don Box. Now for the question. In a similar situation in c#(or any other .Net languare) what would happen?

OwnerDisplayName: Serge - appTranslator


问:
Initialization of a struct instance data member in constructor?
I have a c++ class which as a private data member has a struct defined:

```<code>Class example {

...

private:
   struct Impl;
   Impl& structure_;
};
</code>```

Assuming that the struct Impl is defined as follows:

```<code>struct example::Impl{
    int m1;
    int m2;
    int m3;
};
</code>```

how would I be able to initialize the struct instance ( structure_ ) in the class's constructor?

Right now I have:

```<code>example::example() :
     structure_ .m1(00),
     structure_ .m2(00),
     structure_ .m3(00) {
...
}
</code>```

.. for the initialization list, but I'm getting this error:

'example::structure_' : must be initialized in constructor base/member initializer list

how would I fix this?

Thanks

答: Impl is a reference, so you need to initialize it with an actual Impl object before it can be used.</p>

Body:
I have a c++ class which as a private data member has a struct defined:

Class example {

...

private:
   struct Impl;
   Impl& structure_;
};

Assuming that the struct Impl is defined as follows:

struct example::Impl{
    int m1;
    int m2;
    int m3;
};

how would I be able to initialize the struct instance ( structure_ ) in the class's constructor?

Right now I have:

example::example() :
     structure_ .m1(00),
     structure_ .m2(00),
     structure_ .m3(00) {
...
}

.. for the initialization list, but I'm getting this error:

'example::structure_' : must be initialized in constructor base/member initializer list

how would I fix this?

Thanks


问:
How to definitively align vertically and centrally with the bootstrap?
I tried a lot before creating this doubt, but I didn't find anything like that, I can't align a div vertically with the bootstrap, how can I do this?

<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />

Nome

Dev

  PORTFÓLIO

```

答:
You can use css :

```<code><div class="parent">
   <div class="inner"/>
</div>
</code>```

where child is :

```<code>.parent {
  display : flex;
  align-items : center;
  justify-content: center;
}
</code>```

Body:
You can use css :

<div class="parent">
   <div class="inner"/>
</div>

where child is :

.parent {
  display : flex;
  align-items : center;
  justify-content: center;
}


问:
Navigation drawer title according to the Drawer item
I have used getSupportActionBar().setTitle("drawer_title"); and the title changes according to drawer item. But when I go back the title does not get updated. for eg: If I'm at dashboard-fragment toolbar-title is dashboard and after that when I go to message-fragment toolbar-title changes to message, but, after when I press back button it goes back to dashboard-fragment but the title does not updated to dashboard it remains message.

How could I update the title again??

答: This works for me I just used getActivity().setTitle("fragment_title"); inside onCreateView() method of fragment.


问:
iPad - UIViewController only kinda rotates
Alright, so here's the problem I'm having (it's a weird one.)

I added a new UIViewController for the app I'm building. The first view controller works perfectly, orients correctly, etc. The second one orients horribly.

This is how it looks if you start in landscape and then change orientation to portrait:

And then, if you start in portrait and switch to landscape:

It's worth noting that the button isn't a problem, it's there as a trigger for some code and doesn't need to stay correctly placed. Also, switching tabs makes the orientation display correctly, but it breaks as soon as you switch again.

I have absolutely no idea why this is happening. The only code I've added to this view controller is an IBAction that doesn't have any bearing (I commented it out to be sure.) Any ideas?

Thanks,

jkap

答:
for your button try this: b

```<code>uttonReference.autoresizingMask =  UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleWidth   |
UIViewAutoresizingFlexibleHeight |
UIViewAutoresizingFlexibleRightMargin  |
UIViewAutoresizingFlexibleTopMargin  |
UIViewAutoresizingFlexibleBottomMargin;
</code>```

for every subview (component in your view) set AutoresizeMask, actualy that mask is UIViewAutoresizingNone

Body:
for your button try this: b

uttonReference.autoresizingMask =  UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleWidth   |
UIViewAutoresizingFlexibleHeight |
UIViewAutoresizingFlexibleRightMargin  |
UIViewAutoresizingFlexibleTopMargin  |
UIViewAutoresizingFlexibleBottomMargin;

for every subview (component in your view) set AutoresizeMask, actualy that mask is UIViewAutoresizingNone


答:
Aligning an element vertically is made very easy with flexbox. Use the class d-flex on the .col element inside the .row so it is displayed via display:flex. The class .flex-column forces the content to be flexed in a vertical way. .justify-content-center centers the element vertically. I applied a height of 300 px to the .col so you can see the effect.

You asked about centering it vertically. If you want to center it horizontally too, simply add align-items-center as a class to the col like so:

```<code>col bg-light d-flex flex-column justify-content-center align-items-center
</code>```

.col {
  min-height:300px;
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>

<div class="container">
  <div class="col bg-light d-flex flex-column justify-content-center">
      <h1>Nome</h1>
  </div>

</div></code>```

Title: How to definitively align vertically and centrally with the bootstrap?

Body:
I tried a lot before creating this doubt, but I didn't find anything like that, I can't align a div vertically with the bootstrap, how can I do this?

<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />

Nome

Dev

  PORTFÓLIO

Body:
Aligning an element vertically is made very easy with flexbox. Use the class d-flex on the .col element inside the .row so it is displayed via display:flex. The class .flex-column forces the content to be flexed in a vertical way. .justify-content-center centers the element vertically. I applied a height of 300 px to the .col so you can see the effect.

You asked about centering it vertically. If you want to center it horizontally too, simply add align-items-center as a class to the col like so:

col bg-light d-flex flex-column justify-content-center align-items-center

.col {
  min-height:300px;
}

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>

<div class="container">
  <div class="col bg-light d-flex flex-column justify-content-center">
      <h1>Nome</h1>
  </div>

</div>


问:
MVC disabling client side validation on a model field
I have a model for creating/editing items and the items can belong to a category.

The category renders as a drop down so the user can choose a category, or fill in a new category via a textbox.

The issue I am having is the dropdown seems to want to force the user to select a value from the drop down. The expected use is for that field to be  optional.

Here are the relevant model properties:

```<code>[Display(Name = "Category")]
public int CategoryId { get; set; }
public System.Web.Mvc.SelectList Categories { get; set; }
</code>```

And in my view:

```<code><div class="form-group">
    @Html.LabelFor(m => m.CategoryId, new { @class = "control-label" })
    @Html.DropDownList("CategoryId", Model.Categories, "Select a category", new { @class = "form-control" })
</div>
<input type="submit" value="Create" class="btn btn-primary" title="Create new item" />
</code>```

And how I set the properties in controller:

```<code>// categories is a List of POCO class with CategoryId and CategoryName fields.
Categories = new SelectList(categories, "CategoryId", "CategoryName")
</code>```

When I browse to the page and click Create, the category drop down is focused on, meaning the JS deems it missing, but no error message is displayed as I do not have the ValidationMessageFor() helper set.

The rendered classes for the  are

```<code>form-control input-validation-error
</code>```

So how do I make this field optional?

Edit: The data-* attributes after rendering on the select are

```<code>data-val-number="The field Category must be a number." data-val-required="The Category field is required."
</code>```

答:
You have to make CategoryId nullable as :

```<code>[Display(Name = "Category")]
public int? CategoryId { get; set; }
public System.Web.Mvc.SelectList Categories { get; set; }
</code>```

This way you can make Categories as an optional field.

Body:
I have a model for creating/editing items and the items can belong to a category.

The category renders as a drop down so the user can choose a category, or fill in a new category via a textbox.

The issue I am having is the dropdown seems to want to force the user to select a value from the drop down. The expected use is for that field to be  optional.

Here are the relevant model properties:

[Display(Name = "Category")]
public int CategoryId { get; set; }
public System.Web.Mvc.SelectList Categories { get; set; }

And in my view:

    @Html.LabelFor(m => m.CategoryId, new { @class = "control-label" })
    @Html.DropDownList("CategoryId", Model.Categories, "Select a category", new { @class = "form-control" })

<input type="submit" value="Create" class="btn btn-primary" title="Create new item" />

And how I set the properties in controller:

// categories is a List of POCO class with CategoryId and CategoryName fields.
Categories = new SelectList(categories, "CategoryId", "CategoryName")

When I browse to the page and click Create, the category drop down is focused on, meaning the JS deems it missing, but no error message is displayed as I do not have the ValidationMessageFor() helper set.

The rendered classes for the  are

form-control input-validation-error

So how do I make this field optional?

Edit: The data-* attributes after rendering on the select are

data-val-number="The field Category must be a number." data-val-required="The Category field is required."

Body:
You have to make CategoryId nullable as :

[Display(Name = "Category")]
public int? CategoryId { get; set; }
public System.Web.Mvc.SelectList Categories { get; set; }

This way you can make Categories as an optional field.


问:
How to keep track of faulted items in TPL pipeline in (thread)safe way
I am using TPL pipeline design together with Stephen Cleary's Try library In short it wraps value/exception and floats it down the pipeline. So even items that have thrown exceptions inside their processing methods, at the end when I await resultsBlock.Completion; have Status=RunToCompletion. So I need other way how to register faulted items. Here is small sample:

```<code>var downloadBlock = new TransformBlock<int, Try<int>>(construct => Try.Create(() =>
{
    //SomeProcessingMethod();
    return 1;
}));
var processBlock = new TransformBlock<Try<int>, Try<int>>(construct => construct.Map(value =>
{
    //SomeProcessingMethod();
    return 1;
}));
var resultsBlock = new ActionBlock<Try<int>>(construct =>
{
    if (construct.IsException)
    {
        var exception = construct.Exception;
        switch (exception)
        {
            case GoogleApiException gex:
                //_notificationService.NotifyUser("OMG, my dear sir, I think I messed something up:/"
                //Register that this item was faulted, so we know that we need to retry it.
                break;
            default:
                break;
        }
    }
});
</code>```

One solution would be to create a List<int> FaultedItems; where I would insert all faulted items in my Exception handling block and then after await resultsBlock.Completion; I could check if the list is not empty and create new pipeline for faulted items. My question is if I use a List<int> am I at risk of running into problems with thread safety if I decide to play with MaxDegreeOfParallelism settings and I'd be better off using some ConcurrentCollection? Or maybe this approach is flawed in some other way?

答:
I converted a retry-block implementation from an answer to a similar question, to work with Stephen Cleary's Try types as input and output. The method CreateRetryTransformBlock returns a TransformBlock<Try<TInput>, Try<TOutput>>, and the method CreateRetryActionBlock returns something that is practically an ActionBlock<Try<TInput>>.

Three more options are available, the MaxAttemptsPerItem, MinimumRetryDelay and MaxRetriesTotal, on top of the standard execution options.

```<code>public class RetryExecutionDataflowBlockOptions : ExecutionDataflowBlockOptions
{
    /// <summary>The limit after which an item is returned as failed.</summary>
    public int MaxAttemptsPerItem { get; set; } = 1;
    /// <summary>The minimum delay duration before retrying an item.</summary>
    public TimeSpan MinimumRetryDelay { get; set; } = TimeSpan.Zero;
    /// <summary>The limit after which the block transitions to a faulted
    /// state (unlimited is the default).</summary>
    public int MaxRetriesTotal { get; set; } = -1;
}

public class RetryLimitException : Exception
{
    public RetryLimitException(string message, Exception innerException)
        : base(message, innerException) { }
}

public static TransformBlock<Try<TInput>, Try<TOutput>>
    CreateRetryTransformBlock<TInput, TOutput>(
    Func<TInput, Task<TOutput>> transform,
    RetryExecutionDataflowBlockOptions dataflowBlockOptions)
{
    if (transform == null) throw new ArgumentNullException(nameof(transform));
    if (dataflowBlockOptions == null)
        throw new ArgumentNullException(nameof(dataflowBlockOptions));
    int maxAttemptsPerItem = dataflowBlockOptions.MaxAttemptsPerItem;
    int maxRetriesTotal = dataflowBlockOptions.MaxRetriesTotal;
    TimeSpan retryDelay = dataflowBlockOptions.MinimumRetryDelay;
    if (maxAttemptsPerItem < 1) throw new ArgumentOutOfRangeException(
        nameof(dataflowBlockOptions.MaxAttemptsPerItem));
    if (maxRetriesTotal < -1) throw new ArgumentOutOfRangeException(
        nameof(dataflowBlockOptions.MaxRetriesTotal));
    if (retryDelay < TimeSpan.Zero) throw new ArgumentOutOfRangeException(
        nameof(dataflowBlockOptions.MinimumRetryDelay));

    var internalCTS = CancellationTokenSource
        .CreateLinkedTokenSource(dataflowBlockOptions.CancellationToken);

    var maxDOP = dataflowBlockOptions.MaxDegreeOfParallelism;
    var taskScheduler = dataflowBlockOptions.TaskScheduler;

    var exceptionsCount = 0;
    SemaphoreSlim semaphore;
    if (maxDOP == DataflowBlockOptions.Unbounded)
    {
        semaphore = new SemaphoreSlim(Int32.MaxValue);
    }
    else
    {
        semaphore = new SemaphoreSlim(maxDOP, maxDOP);

        // The degree of parallelism is controlled by the semaphore
        dataflowBlockOptions.MaxDegreeOfParallelism = DataflowBlockOptions.Unbounded;

        // Use a limited-concurrency scheduler for preserving the processing order
        dataflowBlockOptions.TaskScheduler = new ConcurrentExclusiveSchedulerPair(
            taskScheduler, maxDOP).ConcurrentScheduler;
    }

    var block = new TransformBlock<Try<TInput>, Try<TOutput>>(async item =>
    {
        // Continue on captured context after every await
        if (item.IsException) return Try<TOutput>.FromException(item.Exception);
        var result1 = await ProcessOnceAsync(item);
        if (item.IsException || result1.IsValue) return result1;
        for (int i = 2; i <= maxAttemptsPerItem; i++)
        {
            await Task.Delay(retryDelay, internalCTS.Token);
            var result = await ProcessOnceAsync(item);
            if (result.IsValue) return result;
        }
        return result1; // Return the first-attempt exception
    }, dataflowBlockOptions);

    dataflowBlockOptions.MaxDegreeOfParallelism = maxDOP; // Restore initial value
    dataflowBlockOptions.TaskScheduler = taskScheduler; // Restore initial value

    _ = block.Completion.ContinueWith(_ => internalCTS.Dispose(),
        TaskScheduler.Default);

    return block;

    async Task<Try<TOutput>> ProcessOnceAsync(Try<TInput> item)
    {
        await semaphore.WaitAsync(internalCTS.Token);
        try
        {
            var result = await item.Map(transform);
            if (item.IsValue && result.IsException)
            {
                ObserveNewException(result.Exception);
            }
            return result;
        }
        finally
        {
            semaphore.Release();
        }
    }

    void ObserveNewException(Exception ex)
    {
        if (maxRetriesTotal == -1) return;
        uint newCount = (uint)Interlocked.Increment(ref exceptionsCount);
        if (newCount <= (uint)maxRetriesTotal) return;
        if (newCount == (uint)maxRetriesTotal + 1)
        {
            internalCTS.Cancel(); // The block has failed
            throw new RetryLimitException($"The max retry limit " +
                $"({maxRetriesTotal}) has been reached.", ex);
        }
        throw new OperationCanceledException();
    }
}

public static ITargetBlock<Try<TInput>> CreateRetryActionBlock<TInput>(
    Func<TInput, Task> action,
    RetryExecutionDataflowBlockOptions dataflowBlockOptions)
{
    if (action == null) throw new ArgumentNullException(nameof(action));
    var block = CreateRetryTransformBlock<TInput, object>(async input =>
    {
        await action(input).ConfigureAwait(false); return null;
    }, dataflowBlockOptions);
    var nullTarget = DataflowBlock.NullTarget<Try<object>>();
    block.LinkTo(nullTarget);
    return block;
}
</code>```

Usage example:

```<code>var downloadBlock = CreateRetryTransformBlock(async (int construct) =>
{
    int result = await DownloadAsync(construct);
    return result;
}, new RetryExecutionDataflowBlockOptions()
{
    MaxDegreeOfParallelism = 10,
    MaxAttemptsPerItem = 3,
    MaxRetriesTotal = 100,
    MinimumRetryDelay = TimeSpan.FromSeconds(10)
});

var processBlock = new TransformBlock<Try<int>, Try<int>>(
    construct => construct.Map(async value =>
{
    return await ProcessAsync(value);
}));

downloadBlock.LinkTo(processBlock,
    new DataflowLinkOptions() { PropagateCompletion = true });
</code>```

To keep things simple, in case that an item has been retried the maximum number of times, the exception preserved is the first one that occurred. The subsequent exceptions are lost. In most cases the lost exceptions are going to be of the same type as the first one anyway.

Caution: The above implementation does not have an efficient input queue. If you feed this block with millions of items, the memory usage will explode.

Body:
I am using TPL pipeline design together with Stephen Cleary's Try library In short it wraps value/exception and floats it down the pipeline. So even items that have thrown exceptions inside their processing methods, at the end when I await resultsBlock.Completion; have Status=RunToCompletion. So I need other way how to register faulted items. Here is small sample:

var downloadBlock = new TransformBlock<int, Try<int>>(construct => Try.Create(() =>
{
    //SomeProcessingMethod();
    return 1;
}));
var processBlock = new TransformBlock<Try<int>, Try<int>>(construct => construct.Map(value =>
{
    //SomeProcessingMethod();
    return 1;
}));
var resultsBlock = new ActionBlock<Try<int>>(construct =>
{
    if (construct.IsException)
    {
        var exception = construct.Exception;
        switch (exception)
        {
            case GoogleApiException gex:
                //_notificationService.NotifyUser("OMG, my dear sir, I think I messed something up:/"
                //Register that this item was faulted, so we know that we need to retry it.
                break;
            default:
                break;
        }
    }
});

One solution would be to create a List<int> FaultedItems; where I would insert all faulted items in my Exception handling block and then after await resultsBlock.Completion; I could check if the list is not empty and create new pipeline for faulted items. My question is if I use a List<int> am I at risk of running into problems with thread safety if I decide to play with MaxDegreeOfParallelism settings and I'd be better off using some ConcurrentCollection? Or maybe this approach is flawed in some other way?

Body:
I converted a retry-block implementation from an answer to a similar question, to work with Stephen Cleary's Try types as input and output. The method CreateRetryTransformBlock returns a TransformBlock<Try<TInput>, Try<TOutput>>, and the method CreateRetryActionBlock returns something that is practically an ActionBlock<Try<TInput>>.

Three more options are available, the MaxAttemptsPerItem, MinimumRetryDelay and MaxRetriesTotal, on top of the standard execution options.

public class RetryExecutionDataflowBlockOptions : ExecutionDataflowBlockOptions
{
    /// <summary>The limit after which an item is returned as failed.</summary>
    public int MaxAttemptsPerItem { get; set; } = 1;
    /// <summary>The minimum delay duration before retrying an item.</summary>
    public TimeSpan MinimumRetryDelay { get; set; } = TimeSpan.Zero;
    /// <summary>The limit after which the block transitions to a faulted
    /// state (unlimited is the default).</summary>
    public int MaxRetriesTotal { get; set; } = -1;
}

public class RetryLimitException : Exception
{
    public RetryLimitException(string message, Exception innerException)
        : base(message, innerException) { }
}

public static TransformBlock<Try<TInput>, Try<TOutput>>
    CreateRetryTransformBlock<TInput, TOutput>(
    Func<TInput, Task<TOutput>> transform,
    RetryExecutionDataflowBlockOptions dataflowBlockOptions)
{
    if (transform == null) throw new ArgumentNullException(nameof(transform));
    if (dataflowBlockOptions == null)
        throw new ArgumentNullException(nameof(dataflowBlockOptions));
    int maxAttemptsPerItem = dataflowBlockOptions.MaxAttemptsPerItem;
    int maxRetriesTotal = dataflowBlockOptions.MaxRetriesTotal;
    TimeSpan retryDelay = dataflowBlockOptions.MinimumRetryDelay;
    if (maxAttemptsPerItem < 1) throw new ArgumentOutOfRangeException(
        nameof(dataflowBlockOptions.MaxAttemptsPerItem));
    if (maxRetriesTotal < -1) throw new ArgumentOutOfRangeException(
        nameof(dataflowBlockOptions.MaxRetriesTotal));
    if (retryDelay < TimeSpan.Zero) throw new ArgumentOutOfRangeException(
        nameof(dataflowBlockOptions.MinimumRetryDelay));

    var internalCTS = CancellationTokenSource
        .CreateLinkedTokenSource(dataflowBlockOptions.CancellationToken);

    var maxDOP = dataflowBlockOptions.MaxDegreeOfParallelism;
    var taskScheduler = dataflowBlockOptions.TaskScheduler;

    var exceptionsCount = 0;
    SemaphoreSlim semaphore;
    if (maxDOP == DataflowBlockOptions.Unbounded)
    {
        semaphore = new SemaphoreSlim(Int32.MaxValue);
    }
    else
    {
        semaphore = new SemaphoreSlim(maxDOP, maxDOP);

        // The degree of parallelism is controlled by the semaphore
        dataflowBlockOptions.MaxDegreeOfParallelism = DataflowBlockOptions.Unbounded;

        // Use a limited-concurrency scheduler for preserving the processing order
        dataflowBlockOptions.TaskScheduler = new ConcurrentExclusiveSchedulerPair(
            taskScheduler, maxDOP).ConcurrentScheduler;
    }

    var block = new TransformBlock<Try<TInput>, Try<TOutput>>(async item =>
    {
        // Continue on captured context after every await
        if (item.IsException) return Try<TOutput>.FromException(item.Exception);
        var result1 = await ProcessOnceAsync(item);
        if (item.IsException || result1.IsValue) return result1;
        for (int i = 2; i <= maxAttemptsPerItem; i++)
        {
            await Task.Delay(retryDelay, internalCTS.Token);
            var result = await ProcessOnceAsync(item);
            if (result.IsValue) return result;
        }
        return result1; // Return the first-attempt exception
    }, dataflowBlockOptions);

    dataflowBlockOptions.MaxDegreeOfParallelism = maxDOP; // Restore initial value
    dataflowBlockOptions.TaskScheduler = taskScheduler; // Restore initial value

    _ = block.Completion.ContinueWith(_ => internalCTS.Dispose(),
        TaskScheduler.Default);

    return block;

    async Task<Try<TOutput>> ProcessOnceAsync(Try<TInput> item)
    {
        await semaphore.WaitAsync(internalCTS.Token);
        try
        {
            var result = await item.Map(transform);
            if (item.IsValue && result.IsException)
            {
                ObserveNewException(result.Exception);
            }
            return result;
        }
        finally
        {
            semaphore.Release();
        }
    }

    void ObserveNewException(Exception ex)
    {
        if (maxRetriesTotal == -1) return;
        uint newCount = (uint)Interlocked.Increment(ref exceptionsCount);
        if (newCount <= (uint)maxRetriesTotal) return;
        if (newCount == (uint)maxRetriesTotal + 1)
        {
            internalCTS.Cancel(); // The block has failed
            throw new RetryLimitException($"The max retry limit " +
                $"({maxRetriesTotal}) has been reached.", ex);
        }
        throw new OperationCanceledException();
    }
}

public static ITargetBlock<Try<TInput>> CreateRetryActionBlock<TInput>(
    Func<TInput, Task> action,
    RetryExecutionDataflowBlockOptions dataflowBlockOptions)
{
    if (action == null) throw new ArgumentNullException(nameof(action));
    var block = CreateRetryTransformBlock<TInput, object>(async input =>
    {
        await action(input).ConfigureAwait(false); return null;
    }, dataflowBlockOptions);
    var nullTarget = DataflowBlock.NullTarget<Try<object>>();
    block.LinkTo(nullTarget);
    return block;
}

Usage example:

var downloadBlock = CreateRetryTransformBlock(async (int construct) =>
{
    int result = await DownloadAsync(construct);
    return result;
}, new RetryExecutionDataflowBlockOptions()
{
    MaxDegreeOfParallelism = 10,
    MaxAttemptsPerItem = 3,
    MaxRetriesTotal = 100,
    MinimumRetryDelay = TimeSpan.FromSeconds(10)
});

var processBlock = new TransformBlock<Try<int>, Try<int>>(
    construct => construct.Map(async value =>
{
    return await ProcessAsync(value);
}));

downloadBlock.LinkTo(processBlock,
    new DataflowLinkOptions() { PropagateCompletion = true });

To keep things simple, in case that an item has been retried the maximum number of times, the exception preserved is the first one that occurred. The subsequent exceptions are lost. In most cases the lost exceptions are going to be of the same type as the first one anyway.

Caution: The above implementation does not have an efficient input queue. If you feed this block with millions of items, the memory usage will explode.


问:
Gradient using autograd function error python
I am trying to compute the gradient of some function using autograd but fails and shows the below error

```<code>from autograd import grad
def f(a):
   return a[0]*np.sin(2*np.pi*a[1]) + a[2]*np.sin(2*np.pi*a[3])

a=[1.0,1.0,1.0,1.0]
gr = grad(f,0)
print(gr(a))

</code>```

File "C:\Users\user\Desktop\auto.py", line 23, in f
    return a[0]*np.sin(2*np.pi*a[1]) + a[2]*np.sin(2*np.pi*a[3])

TypeError: loop of ufunc does not support argument 0 of type ArrayBox which has no callable sin method

答:
I had the same issue. I think this a problem with autograd. You have to import numpy from autograd! Add at the beginning : import autograd.numpy as np</p>

```<code>from autograd import grad
import autograd.numpy as np ; <-----------

def f(a):
   return a[0]*np.sin(2*np.pi*a[1]) + a[2]*np.sin(2*np.pi*a[3])

a=[1.0,1.0,1.0,1.0]
gr = grad(f,0)
print(gr(a))
</code>```

Body:
I am trying to compute the gradient of some function using autograd but fails and shows the below error

from autograd import grad
def f(a):
   return a[0]*np.sin(2*np.pi*a[1]) + a[2]*np.sin(2*np.pi*a[3])

a=[1.0,1.0,1.0,1.0]
gr = grad(f,0)
print(gr(a))

File "C:\Users\user\Desktop\auto.py", line 23, in f
    return a[0]*np.sin(2*np.pi*a[1]) + a[2]*np.sin(2*np.pi*a[3])

TypeError: loop of ufunc does not support argument 0 of type ArrayBox which has no callable sin method

Body:
I had the same issue. I think this a problem with autograd. You have to import numpy from autograd! Add at the beginning : import autograd.numpy as np

from autograd import grad
import autograd.numpy as np ; <-----------

def f(a):
   return a[0]*np.sin(2*np.pi*a[1]) + a[2]*np.sin(2*np.pi*a[3])

a=[1.0,1.0,1.0,1.0]
gr = grad(f,0)
print(gr(a))


问:
How to store variations for html elements in BeautifulSoup calls through many sites?
I am scraping site data, the data itself is very simple and obviously displayed on each page.  But since each site is different, the actual HTML structure varies.

eg

site 1

```<code>site = 'https://www.site1.com/items'

page = requests.get(site)
soup = BeautifulSoup(page.content, 'html.parser')

#items are stored in divs with class "item-container"
item_soup = soup.find_all(class_ = 'item-container')

items_to_store = []
for item in item_soup:
    #within the div, the "true" name is stored as the alt-text for the item's image
    items_to_store.append(item.img['alt'])
</code>```

site 2

```<code>site = 'https://www.site2.com/section/items'

page = requests.get(site)
soup = BeautifulSoup(page.content, 'html.parser')

#items are stored in spans with class "item-title"
item_soup = soup.find_all(class_ = "item-title")

items_to_store = []
for item in item_soup:
    #within the span, the "true" name is stored as text
    items_to_store.append(item.text)
</code>```

These two snippets are identical except for

url

the tag details for finding the items

the way the item name is extracted

Rather than just copy/paste the code for each site, and replacing the relevant bits, I'd prefer to write a function.  Ideally this function would take in the site, reference a dictionary where I store the relevant tag details, and pull the relevant data to re-scrape.

Is there a way to fill the parameters in soup.find_all() and items_to_store.append() dynamically?

答:
Rather than .find_all() you can use CSS selectors. Then you can define one function that takes URL and return CSS selector string and function that takes tag as parameter and extracts the information from the tag.

For example:

```<code>txt_site1 = '''
    <div class="item-container"><img alt="this is the information from site 1" /></div>
'''

txt_site2 = '''
    <span class="item-title">this is the information from site 2</span>
'''

def sites(site):
    if 'www.site1.com' in site:
        return 'div.item-container img[alt]', lambda tag: tag['alt']
    elif 'www.site2.com' in site:
        return 'span.item-title', lambda tag: tag.text

def universal_scraper(soup, url):
    css_selector, item_getter = sites(url)

    item_soup = soup.select(css_selector)

    for item in item_soup:
        yield item_getter(item)

all_items = []
soup = BeautifulSoup(txt_site1, 'html.parser')  # load site1
all_items.extend(universal_scraper(soup, 'https://www.site1.com/items'))
soup = BeautifulSoup(txt_site2, 'html.parser')  # load site2
all_items.extend(universal_scraper(soup, 'https://www.site2.com/section/items'))

print(all_items)
</code>```

Prints:

```<code>['this is the information from site 1', 'this is the information from site 2']
</code>```

If you want solution with .find_all(), you can try this:

```<code>def sites(site):
    if 'www.site1.com' in site:
        return lambda soup: soup.find_all(class_ = 'item-container'), lambda tag: tag.img['alt']
    elif 'www.site2.com' in site:
        return lambda soup: soup.find_all(class_ = "item-title"), lambda tag: tag.text

def universal_scraper(soup, url):
    item_finder, item_getter = sites(url)

    item_soup = item_finder(soup)

    for item in item_soup:
        yield item_getter(item)

# the rest is the same...
</code>```

Body:
I am scraping site data, the data itself is very simple and obviously displayed on each page.  But since each site is different, the actual HTML structure varies.

eg

site 1

site = 'https://www.site1.com/items'

page = requests.get(site)
soup = BeautifulSoup(page.content, 'html.parser')

#items are stored in divs with class "item-container"
item_soup = soup.find_all(class_ = 'item-container')

items_to_store = []
for item in item_soup:
    #within the div, the "true" name is stored as the alt-text for the item's image
    items_to_store.append(item.img['alt'])

site 2

site = 'https://www.site2.com/section/items'

page = requests.get(site)
soup = BeautifulSoup(page.content, 'html.parser')

#items are stored in spans with class "item-title"
item_soup = soup.find_all(class_ = "item-title")

items_to_store = []
for item in item_soup:
    #within the span, the "true" name is stored as text
    items_to_store.append(item.text)

These two snippets are identical except for

url

the tag details for finding the items

the way the item name is extracted

Rather than just copy/paste the code for each site, and replacing the relevant bits, I'd prefer to write a function.  Ideally this function would take in the site, reference a dictionary where I store the relevant tag details, and pull the relevant data to re-scrape.

Is there a way to fill the parameters in soup.find_all() and items_to_store.append() dynamically?

Body:
Rather than .find_all() you can use CSS selectors. Then you can define one function that takes URL and return CSS selector string and function that takes tag as parameter and extracts the information from the tag.

For example:

txt_site1 = '''
    <div class="item-container"><img alt="this is the information from site 1" /></div>
'''

txt_site2 = '''
    <span class="item-title">this is the information from site 2</span>
'''

def sites(site):
    if 'www.site1.com' in site:
        return 'div.item-container img[alt]', lambda tag: tag['alt']
    elif 'www.site2.com' in site:
        return 'span.item-title', lambda tag: tag.text

def universal_scraper(soup, url):
    css_selector, item_getter = sites(url)

    item_soup = soup.select(css_selector)

    for item in item_soup:
        yield item_getter(item)

all_items = []
soup = BeautifulSoup(txt_site1, 'html.parser')  # load site1
all_items.extend(universal_scraper(soup, 'https://www.site1.com/items'))
soup = BeautifulSoup(txt_site2, 'html.parser')  # load site2
all_items.extend(universal_scraper(soup, 'https://www.site2.com/section/items'))

print(all_items)

Prints:

['this is the information from site 1', 'this is the information from site 2']

If you want solution with .find_all(), you can try this:

def sites(site):
    if 'www.site1.com' in site:
        return lambda soup: soup.find_all(class_ = 'item-container'), lambda tag: tag.img['alt']
    elif 'www.site2.com' in site:
        return lambda soup: soup.find_all(class_ = "item-title"), lambda tag: tag.text

def universal_scraper(soup, url):
    item_finder, item_getter = sites(url)

    item_soup = item_finder(soup)

    for item in item_soup:
        yield item_getter(item)

# the rest is the same...


问:
Download doesn't work when using code 206 Partial Content
I have a function that forces a file download in the browser, and it works on small files but on large files it doesn't seem to work. When the download starts I get Failed - No File, and I have narrowed it down to the reason is that I am setting a 206 status code (Partial Content). This used to work just fine, but now I am getting the No File message.

When changing it to a status code of 200 the file gets downloaded. When keeping the status code to 206 and removing the Content-Disposition, the file plays in the browser.

Is 206 even needed for large files when downloading?

export class Server {
  private static async send(client: Client, req: http.IncomingMessage, res: http.ServerResponse) {
    let fileSize = client.response.contentLength
    let start = 0, end = fileSize - 1 < start ? start : fileSize - 1
    if (fileSize > (this.app.chunkSize || 5e5)) {
      let range = (req.headers.range || '') as string
      let positions = range.replace(/bytes=/, '').trim().split('-')
      start = parseInt(positions[0] || '0', 10)
      end = parseInt(positions[1] || (fileSize - 1).toString(), 10)
      let chunkSize = (end - start) + 1

      // Setting the Code to 206 here breaks
      client.response.setCode(206)
        .setHeaders({
          'Content-Range': `bytes ${start}-${end}/${fileSize}`,
          'Accept-Ranges': 'bytes',
          'Connection': 'Keep-Alive',
          'Content-Length': chunkSize
        })
    }

    res.writeHead(client.response.code, <any>headers)

    let { store, file } = client.response.fileStore

    let stream: fs.ReadStream = store.readStream(file, { start, end })
      .on('open', () => stream.pipe(res))
      .on('close', () => res.end())
      .on('error', err => res.end(err))
  }
}
```

<p>The read stream here is pretty simple:</p>

<pre class="lang-js prettyprint-override"><code>export default class extends Storage {
  public readStream(filePath: string, options?: FileReadOptions) {
    return fs.createReadStream(this.toPath(filePath), options)
  }
}
</code>```

答:
Send 206 only when explicitly requested by the browser, else send 200

if (!!req.headers.range) {
  // if and only if client sends us range http reader in request,
  // perform your logic, send 206
} else {
  // forget about partial content here,
  // pipe the file to res directly with 200
}

```

Body:
I have a function that forces a file download in the browser, and it works on small files but on large files it doesn't seem to work. When the download starts I get Failed - No File, and I have narrowed it down to the reason is that I am setting a 206 status code (Partial Content). This used to work just fine, but now I am getting the No File message.

When changing it to a status code of 200 the file gets downloaded. When keeping the status code to 206 and removing the Content-Disposition, the file plays in the browser.

Is 206 even needed for large files when downloading?

export class Server {
  private static async send(client: Client, req: http.IncomingMessage, res: http.ServerResponse) {
    let fileSize = client.response.contentLength
    let start = 0, end = fileSize - 1 < start ? start : fileSize - 1
    if (fileSize > (this.app.chunkSize || 5e5)) {
      let range = (req.headers.range || '') as string
      let positions = range.replace(/bytes=/, '').trim().split('-')
      start = parseInt(positions[0] || '0', 10)
      end = parseInt(positions[1] || (fileSize - 1).toString(), 10)
      let chunkSize = (end - start) + 1

      // Setting the Code to 206 here breaks
      client.response.setCode(206)
        .setHeaders({
          'Content-Range': `bytes ${start}-${end}/${fileSize}`,
          'Accept-Ranges': 'bytes',
          'Connection': 'Keep-Alive',
          'Content-Length': chunkSize
        })
    }

    res.writeHead(client.response.code, <any>headers)

    let { store, file } = client.response.fileStore

    let stream: fs.ReadStream = store.readStream(file, { start, end })
      .on('open', () => stream.pipe(res))
      .on('close', () => res.end())
      .on('error', err => res.end(err))
  }
}

The read stream here is pretty simple:

export default class extends Storage {
  public readStream(filePath: string, options?: FileReadOptions) {
    return fs.createReadStream(this.toPath(filePath), options)
  }
}


问:
Difference of n dictionaries
I'm looking for a way to determine the difference of n dictionaries in the following scenario:
There are n dictionaries. Some of their keys match, but not all. Like this:

```<code>dict_a = {
    'keyA': 'valueA',
    'keyB': 'valueB',
    'keyC': 'valueC',
    'keyD': 'valueD',
    'keyE': 'valueE',
    'keyF': 'valueF',
    'keyG': 'valueG'
}
</code>```

```<code>dict_b = {
    'keyA': 'valueA',
    'keyB': 'valueB',
    'keyX': 'valueX',
    'keyD': 'valueH',
    'keyE': 'valueE',
    'keyF': 'valueF',
    'keyG': 'valueG'
}
</code>```

I want to be able, to tell if the value of a key is different from the value of the same key in another dictionary. In the examble above keyD should be returned, because its value is different in dict_a and dict_b. keyX should not be returned, as it only occurs in dict_b This is relatively easy with only two dictionaries, but what is the best practise to accomplish this with n dictionaries?

答:
You can create this simple lambda function to do so:

```<code>>>> diff_keys = lambda x,y: [key for key in x.keys() if key in y.keys() and x[key] != y[key]]

>>> diff_keys(dict_a, dict_b)
['keyD']
</code>```

And to handle n other dicts, you can create another function that can do so like this one:

```<code>>>> def get_different_keys(original_dict: dict, other_dicts: list):
...     different_keys = []
...     for dict_x in other_dicts:
...         different_keys.extend(diff_keys(original_dict, dict_x))
...         original_dict.update(dict_x)
...     return different_keys

>>> get_different_keys(dict_a, [dict_b]))
['keyD']
</code>```

Title: Difference of n dictionaries

Body:
I'm looking for a way to determine the difference of n dictionaries in the following scenario:
There are n dictionaries. Some of their keys match, but not all. Like this:

dict_a = {
    'keyA': 'valueA',
    'keyB': 'valueB',
    'keyC': 'valueC',
    'keyD': 'valueD',
    'keyE': 'valueE',
    'keyF': 'valueF',
    'keyG': 'valueG'
}

dict_b = {
    'keyA': 'valueA',
    'keyB': 'valueB',
    'keyX': 'valueX',
    'keyD': 'valueH',
    'keyE': 'valueE',
    'keyF': 'valueF',
    'keyG': 'valueG'
}

I want to be able, to tell if the value of a key is different from the value of the same key in another dictionary. In the examble above keyD should be returned, because its value is different in dict_a and dict_b. keyX should not be returned, as it only occurs in dict_b This is relatively easy with only two dictionaries, but what is the best practise to accomplish this with n dictionaries?

Body:
You can create this simple lambda function to do so:

>>> diff_keys = lambda x,y: [key for key in x.keys() if key in y.keys() and x[key] != y[key]]

>>> diff_keys(dict_a, dict_b)
['keyD']

And to handle n other dicts, you can create another function that can do so like this one:

>>> def get_different_keys(original_dict: dict, other_dicts: list):
...     different_keys = []
...     for dict_x in other_dicts:
...         different_keys.extend(diff_keys(original_dict, dict_x))
...         original_dict.update(dict_x)
...     return different_keys

>>> get_different_keys(dict_a, [dict_b]))
['keyD']


问:
Every possible combination in a specific order
all out of ideas so looking for help. So I have two tables with a single column with ID's 1-29, each ID represents a coworker. I have a 3rd table where I have already created every combination of records from the first 2 tables.

The part that I am struggling with is that I need to order my 3rd tables as follows, say person 1 calls person 2, the ID in table 3 will look like this: '1-2', now I need to make the next record start with 2 and have person 2 call someone who is not person 1 (who has already been called) and have person 1 not be called again until the remaining 28 people are already called as to ensure that everyone gets called at least once before looking through to other combos.

It's a bit hard to explain so I am willing to elaborate if needed.

For those wondering, the purpose of this exercise to to create a bit of "call train" to have everyone check up on everyone else. Thanks in advance!

答:
It turns out that 29 is really good for this because it is prime.

What you do is have each call the next on the first cycle, then each call with a gap of 2 on the second, then a gap of 3, and so on.  That results in this pattern:

```<code>1 2 ... 29
1 3 5 ... 28
1 4 8 ... 27
.
.
.
1 29 28 ... 2
</code>```

And you have your 28 call trains, using each possible call only once.

Therefore the first record is 1-2.  The second record is 2-3 and so on.  Until the 29th record is 29-1.  Then we start gaps of 2 to get 1-3 and so on.

We can even figure out a formula for that.

In general the nth record is going to be in the i = 1 + (n div 29)th cycle. (I'm using PostgreSQL SQL operators here.  div means "division and drop the fraction".) It is the j = (n mod 29)th entry in that cycle (mod means the remainder after integer division).  Which means that the call starts at (1 + (j-1)*i) mod 29 and goes to (1 + j*i) mod 29.

Body:
It turns out that 29 is really good for this because it is prime.

What you do is have each call the next on the first cycle, then each call with a gap of 2 on the second, then a gap of 3, and so on.  That results in this pattern:

1 2 ... 29
1 3 5 ... 28
1 4 8 ... 27
.
.
.
1 29 28 ... 2

And you have your 28 call trains, using each possible call only once.

Therefore the first record is 1-2.  The second record is 2-3 and so on.  Until the 29th record is 29-1.  Then we start gaps of 2 to get 1-3 and so on.

We can even figure out a formula for that.

In general the nth record is going to be in the i = 1 + (n div 29)th cycle. (I'm using PostgreSQL SQL operators here.  div means "division and drop the fraction".) It is the j = (n mod 29)th entry in that cycle (mod means the remainder after integer division).  Which means that the call starts at (1 + (j-1)*i) mod 29 and goes to (1 + j*i) mod 29.


问:
Julia InexactError: Int64
I am new to Julia. Got this InexactError . To mention that I have tried to convert to float beforehand and it did not work, maybe I am doing something wrong.</p>

```<code>column = df[:, i]
max = maximum(column)
min = minimum(column)
scaled_column = (column .- min)/max   # This is the error, I think
df[:, i] = scaled_column
</code>```

```<code>julia> VERSION
v"1.4.2"
</code>```

答:
Hard to give a sure answer without a minimal working example of the problem, but in general an InexactError happens when you try to convert a value to an exact type (like integer types, but unlike floating-point types) in which the original value cannot be exactly represented. For example:

```<code>julia> convert(Int, 1.0)
1

julia> convert(Int, 1.5)
ERROR: InexactError: Int64(1.5)
</code>```

Other programming languages arbitrarily chose some way of rounding here (often truncation but sometimes rounding to nearest). Julia doesn't guess and requires you to be explicit. If you want to round, truncate, take a ceiling, etc. you can:

```<code>julia> floor(Int, 1.5)
1

julia> round(Int, 1.5)
2

julia> ceil(Int, 1.5)
2
</code>```

Back to your problem: you're not calling convert anywhere, so why are you getting a conversion error? There are various situations where Julia will automatically call convert for you, typically when you try to assign a value to a typed location. For example, if you have an array of Ints and you assign a floating point value into it, it will be converted automatically:

```<code>julia> v = [1, 2, 3]
3-element Array{Int64,1}:
 1
 2
 3

julia> v[2] = 4.0
4.0

julia> v
3-element Array{Int64,1}:
 1
 4
 3

julia> v[2] = 4.5
ERROR: InexactError: Int64(4.5)
</code>```

So that's likely what's happening to you: you get non-integer values by doing (column .- min)/max and then you try to assign it into an integer location and you get the error.

Title: Julia InexactError: Int64

Body:
I am new to Julia. Got this InexactError . To mention that I have tried to convert to float beforehand and it did not work, maybe I am doing something wrong.

column = df[:, i]
max = maximum(column)
min = minimum(column)
scaled_column = (column .- min)/max   # This is the error, I think
df[:, i] = scaled_column

julia> VERSION
v"1.4.2"

Body:
Hard to give a sure answer without a minimal working example of the problem, but in general an InexactError happens when you try to convert a value to an exact type (like integer types, but unlike floating-point types) in which the original value cannot be exactly represented. For example:

julia> convert(Int, 1.0)
1

julia> convert(Int, 1.5)
ERROR: InexactError: Int64(1.5)

Other programming languages arbitrarily chose some way of rounding here (often truncation but sometimes rounding to nearest). Julia doesn't guess and requires you to be explicit. If you want to round, truncate, take a ceiling, etc. you can:

julia> floor(Int, 1.5)
1

julia> round(Int, 1.5)
2

julia> ceil(Int, 1.5)
2

Back to your problem: you're not calling convert anywhere, so why are you getting a conversion error? There are various situations where Julia will automatically call convert for you, typically when you try to assign a value to a typed location. For example, if you have an array of Ints and you assign a floating point value into it, it will be converted automatically:

julia> v = [1, 2, 3]
3-element Array{Int64,1}:
 1
 2
 3

julia> v[2] = 4.0
4.0

julia> v
3-element Array{Int64,1}:
 1
 4
 3

julia> v[2] = 4.5
ERROR: InexactError: Int64(4.5)

So that's likely what's happening to you: you get non-integer values by doing (column .- min)/max and then you try to assign it into an integer location and you get the error.


问:
log4net is logging all levels, instead of what I've configured it to do
I have the level set to "INFO" but I'm seeing DEBUG events in the log.

Config

<?xml version="1.0"?>

<configuration>
    <configSections>

    </configSections>

    <log4net>
        <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
            <level value="INFO"/>
            <file value="Logs\Log.txt" />
            <appendToFile value="true" />
            <rollingStyle value="Size" />
            <maxSizeRollBackups value="10" />
            <maximumFileSize value="10MB" />
            <staticLogFileName value="true" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
            </layout>
        </appender>

        <root>
            <appender-ref ref="RollingFileAppender"/>
        </root>
    </log4net>
</configuration>
```

<h3>Code</h3>

```private ILog Log { get; set; }

static Program()
{
    XmlConfigurator.Configure();
    //...
    this.Log.Info("Started in RunOnce mode.");
}
```

答:
have a look at

http://logging.apache.org/log4net/release/sdk/log4net.Core.Level.html

It looks like this could be the cuplrit

When logging at level INFO the actual
  level used is not Info but the value
  of LoggerRepository.LevelMap["INFO"].
  The default value for this is Info,
  but this can be changed by
  reconfiguring the level map

Body:
I have the level set to "INFO" but I'm seeing DEBUG events in the log.

Config

<?xml version="1.0"?>

<configuration>
    <configSections>

    </configSections>

    <log4net>
        <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
            <level value="INFO"/>
            <file value="Logs\Log.txt" />
            <appendToFile value="true" />
            <rollingStyle value="Size" />
            <maxSizeRollBackups value="10" />
            <maximumFileSize value="10MB" />
            <staticLogFileName value="true" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
            </layout>
        </appender>

        <root>
            <appender-ref ref="RollingFileAppender"/>
        </root>
    </log4net>
</configuration>

Code

private ILog Log { get; set; }

static Program()
{
    XmlConfigurator.Configure();
    //...
    this.Log.Info("Started in RunOnce mode.");
}