问:
How to stop telethon?
I'm trying to stop bot execution. But i can't do it. When i write in the telegram bot command /stop, that he must stop. Code:

```<code># -*- coding: utf-8 -*-
import telebot
import asyncio
from telethon import TelegramClient,sync,events,utils
from telethon.events import StopPropagation

api_id = ***
api_hash = '******'
username = '*****'
id_channel = -***
token = '*****'

bot = telebot.TeleBot(token)
client = TelegramClient(username, api_id, api_hash)

@bot.message_handler(commands=['start','stop'])
def start(message):
    if '/start' in message.text:
        handler_state(True)
        string = f'<b>Привет {message.from_user.first_name}!</b>\nВведите канал для парсинга и свой канал.\n<i>Пример: ```channel_pars,your_channel```</i>'
        bot.send_message(message.chat.id, string, parse_mode='html')
    elif '/stop' in message.text:
        string = '<b>Парсер остановлен!</b>'
        bot.send_message(message.chat.id, string, parse_mode='html')
        handler_state(False)

def handler_channel(my_channel,parse_channel,stop_script='start'):
    @client.on(events.NewMessage(chats=(parse_channel)))
    async def normal_handler(event):
        if 'stop' in stop_script:
            await client.disconnect()
        #print(event.message.file.media)  # картинка
        message = event.message.to_dict()['message']
        send_data(message)

    client.start()
    client.run_until_disconnected()

def send_data(message):
    bot = telebot.TeleBot(token)
    bot.send_message(id_channel,message)

def handler_state(state):
    if not state:
        handler_channel('default','default','stop')

    my_channel = None
    parse_channel = None
    @bot.message_handler(content_types=['text'])
    def send_message(message):
        if state:
            if ',' not in message.text:
                return bot.send_message(message.chat.id,'<b>Проверьте правильность ввода!</b>',parse_mode='html')
            channels = message.text.split(',')
            my_channel = channels[-1]
            parse_channel = channels[0]
            if not my_channel or not parse_channel:
                return bot.send_message(message.chat.id,'<b>Проверьте правильность ввода!</b>',parse_mode='html')

            bot.send_message(message.chat.id,f'Введенные данные: ```{parse_channel},{my_channel}``` \n <b>Парсим</b>',parse_mode='html')

        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        if state:
            handler_channel(my_channel,parse_channel)

bot.polling(none_stop=True)
</code>```

Here is the error that occurs:

```<code>p1.py:36: RuntimeWarning: coroutine 'AuthMethods._start' was never awaited
  client.start()
p1.py:37: RuntimeWarning: coroutine 'UpdateMethods._run_until_disconnected' was never awaited
  client.run_until_disconnected()
</code>```

答:
Similar to how you start Telethon with client.connect() (or a variant, like start or using a with block), you would stop it with client.disconnect().

But your issue seems different.

You seem to be using telebot, which is a threaded library. Using threading and asyncio together requires special care and a deeper understanding on how both libraries work. While How to combine python asyncio with threads? may help, I'd recommend replacing telebot with an asynchronous bot library (such as Telethon itself, and then you would only need to worry about one library).

Title: How to stop telethon?

Body:
I'm trying to stop bot execution. But i can't do it. When i write in the telegram bot command /stop, that he must stop. Code:

# -*- coding: utf-8 -*-
import telebot
import asyncio
from telethon import TelegramClient,sync,events,utils
from telethon.events import StopPropagation

api_id = ***
api_hash = '******'
username = '*****'
id_channel = -***
token = '*****'

bot = telebot.TeleBot(token)
client = TelegramClient(username, api_id, api_hash)

@bot.message_handler(commands=['start','stop'])
def start(message):
    if '/start' in message.text:
        handler_state(True)
        string = f'Привет {message.from_user.first_name}!\nВведите канал для парсинга и свой канал.\nПример:
channel_pars,your_channel
'
        bot.send_message(message.chat.id, string, parse_mode='html')
    elif '/stop' in message.text:
        string = 'Парсер остановлен!'
        bot.send_message(message.chat.id, string, parse_mode='html')
        handler_state(False)

def handler_channel(my_channel,parse_channel,stop_script='start'):
    @client.on(events.NewMessage(chats=(parse_channel)))
    async def normal_handler(event):
        if 'stop' in stop_script:
            await client.disconnect()
        #print(event.message.file.media)  # картинка
        message = event.message.to_dict()['message']
        send_data(message)

    client.start()
    client.run_until_disconnected()

def send_data(message):
    bot = telebot.TeleBot(token)
    bot.send_message(id_channel,message)

def handler_state(state):
    if not state:
        handler_channel('default','default','stop')

    my_channel = None
    parse_channel = None
    @bot.message_handler(content_types=['text'])
    def send_message(message):
        if state:
            if ',' not in message.text:
                return bot.send_message(message.chat.id,'Проверьте правильность ввода!',parse_mode='html')
            channels = message.text.split(',')
            my_channel = channels[-1]
            parse_channel = channels[0]
            if not my_channel or not parse_channel:
                return bot.send_message(message.chat.id,'Проверьте правильность ввода!',parse_mode='html')

            bot.send_message(message.chat.id,f'Введенные данные:
{parse_channel},{my_channel}
 \n Парсим',parse_mode='html')

        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        if state:
            handler_channel(my_channel,parse_channel)

bot.polling(none_stop=True)

Here is the error that occurs:

p1.py:36: RuntimeWarning: coroutine 'AuthMethods._start' was never awaited
  client.start()
p1.py:37: RuntimeWarning: coroutine 'UpdateMethods._run_until_disconnected' was never awaited
  client.run_until_disconnected()


来源: stackexchange

问:
Angular Typescript why can assign a Promise<ClassA> to object of classA
I just start to learn angular and I found my company project has the following code:

```<code>currentCDFlow: CDCurrentFlowDTO;

this.currentCDFlow = await this.serivce.getCDMaster();
</code>```

and the getCDMaster() code is

```<code>public async getCDMaster(): Promise<CDCurrentFlowDTO> {
    return await this.http.post<CDCurrentFlowDTO>(Constants.BASE_HREF + '/getCDMaster', null, this.RequestJson).toPromise().catch(err => { throw err });
  }
</code>```

as getCDMaster() is returning Promise<CDCurrentFlowDTO> (a Promise object), why it can assign to object of CDCurrentFlowDTO? I expect it can only assign to object of Promise<CDCurrentFlowDTO>, but not just CDCurrentFlowDTO (without enclosed by Promise<...>).

As in java,

String a = new ArrayList<String>();

should have compile error...

Is this typescript feature?

答:
the reason is Promise. A promise is an object representing an asynchronous return or you can say, a promise of something. The reason your code works in the assignment is because of the await syntax behind the function call. The await syntax works with Promise<T> makes the execution wait for the promise to finish, or to eventually return the object they 'promise' and that is why it works...

Check this article out to better understand... https://www.positronx.io/angular-8-es-6-typescript-promises-examples/

Update

Okay lets use your code as a working example...you have the following function defined

```<code>public async getCDMaster(): Promise<CDCurrentFlowDTO> {
  return await this.http.post<CDCurrentFlowDTO>(Constants.BASE_HREF + '/getCDMaster', null, this.RequestJson).toPromise().catch(err => { throw err });
}

/*
and then you call it here...
*/

// First you declare the variable to be of type CDCurrentFlowDTO but your
// function will return a Promise<CDCurrentFlowDTO> so let us see how this
// works out...
currentCDFlow: CDCurrentFlowDTO;

/*
Since the await syntax will cause the function call to wait for the returning Promise to complete then having the call like below will work
*/
this.currentCDFlow = await this.serivce.getCDMaster();

/*
But if we were to call it without the await:
*/
this.service.getCDMaster() // we then expect the Promise<CDCurrentFlowDTO> to return
</code>```

If we still want to execute asynchronously we can do this for example...

```<code>//...

currentCDFlow: CDCurrentFlowDTO;

this.service.getCDMaster().then((result) => {
  currentCDFlow = result;

  //if we log here we should see the value
  console.log(currentCDFlow);
});
// but if we log here we expect undefined because the Promise has not returned yet
console.log(currentCDFlow);
</code>```

The async you have for you function definition is not necessary because any function that is defined as async will return a Promise and yet the return value is already a Promise and yet you await for it...you can define it like this instead

```<code>publicgetCDMaster(): Promise<CDCurrentFlowDTO> {
  return this.http.post<CDCurrentFlowDTO>(Constants.BASE_HREF + '/getCDMaster', null, this.RequestJson).toPromise().catch(err => { throw err });
}
</code>```

ContentLicense: CC BY-SA 4.0

Body:
I just start to learn angular and I found my company project has the following code:

currentCDFlow: CDCurrentFlowDTO;

this.currentCDFlow = await this.serivce.getCDMaster();

and the getCDMaster() code is

public async getCDMaster(): Promise<CDCurrentFlowDTO> {
    return await this.http.post<CDCurrentFlowDTO>(Constants.BASE_HREF + '/getCDMaster', null, this.RequestJson).toPromise().catch(err => { throw err });
  }

as getCDMaster() is returning Promise<CDCurrentFlowDTO> (a Promise object), why it can assign to object of CDCurrentFlowDTO? I expect it can only assign to object of Promise<CDCurrentFlowDTO>, but not just CDCurrentFlowDTO (without enclosed by Promise<...>).

As in java,

String a = new ArrayList<String>();

should have compile error...

Is this typescript feature?

Body:
the reason is Promise. A promise is an object representing an asynchronous return or you can say, a promise of something. The reason your code works in the assignment is because of the await syntax behind the function call. The await syntax works with Promise<T> makes the execution wait for the promise to finish, or to eventually return the object they 'promise' and that is why it works...

Check this article out to better understand... https://www.positronx.io/angular-8-es-6-typescript-promises-examples/

Update

Okay lets use your code as a working example...you have the following function defined

public async getCDMaster(): Promise<CDCurrentFlowDTO> {
  return await this.http.post<CDCurrentFlowDTO>(Constants.BASE_HREF + '/getCDMaster', null, this.RequestJson).toPromise().catch(err => { throw err });
}

/*
and then you call it here...
*/

// First you declare the variable to be of type CDCurrentFlowDTO but your
// function will return a Promise<CDCurrentFlowDTO> so let us see how this
// works out...
currentCDFlow: CDCurrentFlowDTO;

/*
Since the await syntax will cause the function call to wait for the returning Promise to complete then having the call like below will work
*/
this.currentCDFlow = await this.serivce.getCDMaster();

/*
But if we were to call it without the await:
*/
this.service.getCDMaster() // we then expect the Promise<CDCurrentFlowDTO> to return

If we still want to execute asynchronously we can do this for example...

//...

currentCDFlow: CDCurrentFlowDTO;

this.service.getCDMaster().then((result) => {
  currentCDFlow = result;

  //if we log here we should see the value
  console.log(currentCDFlow);
});
// but if we log here we expect undefined because the Promise has not returned yet
console.log(currentCDFlow);

The async you have for you function definition is not necessary because any function that is defined as async will return a Promise and yet the return value is already a Promise and yet you await for it...you can define it like this instead

publicgetCDMaster(): Promise<CDCurrentFlowDTO> {
  return this.http.post<CDCurrentFlowDTO>(Constants.BASE_HREF + '/getCDMaster', null, this.RequestJson).toPromise().catch(err => { throw err });
}


问:
Reorder elements of SVG ( z-index ) in D3.js
I realise this question has been asked before but I can't get to the bottom of it.

Here is my chart... http://www.gogeye.com/financialnews/piechart/index3.html

All I want to do is have the coin render behind the graph. I know D3 renders in order they are appended.

I have tried to re-append the coin but can't seem to get it working.

I've tried reordering when things are appended in the DOM but keep getting errors probably because variables are getting called before being defined etc.

Can someone give me an example of how to fix this with my code? I don't want you to do the work for me but I've been pulling my hair out for so long, I can't seem to apply other peoples examples to mine.

thanks

答:
I am using the D3.js, and found that it has a built-in function for changing the z-order of SVG elements programmatically after the original drawing.

RipTutorial: svg--the-drawing-order covers the d3 builtin function

Quotes from this link:

selection.raise(): Re-inserts each selected element, in order, as the last child of its parent. selection.lower(): Re-inserts each selected element, in order, as the first child of its parent.

```<code>d3.selectAll("circle").on("mouseenter", function(){
    d3.select(this).raise();
});

d3.selectAll("circle").on("mouseleave", function(){
    d3.select(this).lower();
});

</code>```

see live example their jsFiddle

ContentLicense: CC BY-SA 3.0

Body:
I am using the D3.js, and found that it has a built-in function for changing the z-order of SVG elements programmatically after the original drawing.

RipTutorial: svg--the-drawing-order covers the d3 builtin function

Quotes from this link:

selection.raise(): Re-inserts each selected element, in order, as the last child of its parent. selection.lower(): Re-inserts each selected element, in order, as the first child of its parent.

d3.selectAll("circle").on("mouseenter", function(){
    d3.select(this).raise();
});

d3.selectAll("circle").on("mouseleave", function(){
    d3.select(this).lower();
});

see live example their jsFiddle


问:
Calling Clojure from Java: Why is the "new" style (clojure.java.api.Clojure) better than the "old" one (gen-class)?
Something is puzzling me after reading the this great answer to a related question:

There are two possibilities to share a function that I wrote in Clojure with Java developers

The first one is providing it in a JAR file so that they can call it as if I had written it in Java.  Sounds great for Clojure advocacy.

The second one, the purportedly better way, requires those Java developers to use things like clojure.lang.IFn or clojure.lang.RT and invoking functions by passing their names as strings (!) instead of just calling them.

Why is the second approach "the better one"?

答:
You are sorta setting up a false dichotomy here. Every approach involves creating a jar file: that is just how JVM programs are distributed. But there are 3 different ways for Java code to invoke Clojure code contained in a jar:

Use methods in clojure.lang.RT to initialize the runtime, load files, and then look up vars. This is the old, deprecated approach.

Use methods in clojure.java.api.Clojure to look up functions and invoke them. This is the newer version of (1), and hides some of the messy stuff you could accidentally get wrong.

Use gen-class in the Clojure library to define a more Java-friendly interface to the Clojure functions.

You can still do (3) - there's nothing wrong with it exactly. But gen-class is a pretty clunky tool, and except for the simplest examples like exposing a number of static methods, it's just not a lot of fun, and it's not easy to provide an API that "feels" like a Java API using Clojure.

But you know what's great at providing an API that feels like Java? Java! So what I recommend if you want to make a Clojure library easy to use in Java is to include some Java code in your Clojure library. That Java code, written by you, bridges the language gap. It accesses your Clojure code by mechanism (2) above, and presents a Java-friendly facade so the outside world doesn't have to know there's Clojure underneath.

amalloy/thrift-gen is an example of a library I wrote years ago following this approach. It would not be at all easy to write this in pure Clojure, just because traditional Java idioms are very foreign to Clojure, and it doesn't support them all very well. By writing my own Java shim instead, Java clients get a very comfortable interface to work with, and I can just write Clojure that feels like Clojure instead of a bunch of gen-class nonsense.


问:
Is there any function for Tampermonkey to accept HTML/CSS scripts?
I've been Finding this out for months. How can we let HTML codes get accepted on Tampermonkey?
My code here is

 'check_circle';```

and I want to put this script on Tampermonkey, But it dosn't accept it. How do i figure this out?

答:
Sorry For The Late Reply

Thanks! It Worked, And Now I've No Worries!

Body:
I've been Finding this out for months. How can we let HTML codes get accepted on Tampermonkey?
My code here is

 'check_circle';

and I want to put this script on Tampermonkey, But it dosn't accept it. How do i figure this out?


问:
Error in C++17 fold expression to calculate factorial
The following program gives errors in both g++ and clang...

```<code>#include <iostream>
#include <utility>

using namespace std;

//Since C++17 one can use fold expression to calculate factorial:
template <class T, T N, class I = std::make_integer_sequence<T, N>>
struct factorial;

template <class T, T N, T... Is>
struct factorial<T,N,std::index_sequence<T, Is...>> {
   static constexpr T value = (static_cast<T>(1)* ... *(Is + 1));
};

int main(int argc, char** argv) {

    std::cout << factorial<int, 5>::value << std::endl;

    return 0;
}
</code>```

Errors listed in gcc are as follows:

```<code> error: type/value mismatch at argument 1 in template parameter list for 'template<long unsigned int ..._Idx> using index_sequence = std::integer_sequence<long unsigned int, _Idx ...>'
   24 | struct factorial<T,N,std::index_sequence<T, Is...>> {
      |                                               ^~~
note:   expected a constant of type 'long unsigned int', got 'T'
error: template argument 3 is invalid
   24 | struct factorial<T,N,std::index_sequence<T, Is...>> {
      |                                                  ^~
In function 'int main(int, char**)':
error: incomplete type 'factorial<int, 5>' used in nested name specifier
   31 |     std::cout << factorial<int, 5>::value << std::endl;
      |                                     ^~~~~
</code>```

Clang errors are as follows:

```<code>error: template argument for non-type template parameter must be an expression
struct factorial<T,N,std::index_sequence<T, Is...>> {
                                         ^
D:\Programs\msys64\mingw64\include\c++\10.1.0\utility:344:22: note: template parameter is declared here
  template<size_t... _Idx>
                     ^
main.cpp:32:18: error: implicit instantiation of undefined template 'factorial<int, 5, std::integer_sequence<int, 0, 1, 2, 3, 4> >'
    std::cout << factorial<int, 5>::value << std::endl;
                 ^
main.cpp:22:8: note: template is declared here
struct factorial;
       ^
</code>```

This example is shown in many sites.

Request somebody to help me correcting it.

答:
You used the wrong type for your result object.

std::index_sequence is a template that accepts only a pack of std::size_t. It doesn't accept a type parameter, because the type is fixed. It is an alias template for std::integer_sequence<std::size_t, Ints...>.

So you trying to specify a type as its the first argument in std::index_sequence<T,...> is wrong.

What you want is the more general type trait std::integer_sequence, making your specialization into this

```<code>template <class T, T N, T... Is>
struct factorial<T,N,std::integer_sequence<T, Is...>> {
   static constexpr T value = (static_cast<T>(1)* ... *(Is + 1));
};
</code>```

Live code example

Body:
The following program gives errors in both g++ and clang...

#include <iostream>
#include <utility>

using namespace std;

//Since C++17 one can use fold expression to calculate factorial:
template <class T, T N, class I = std::make_integer_sequence<T, N>>
struct factorial;

template <class T, T N, T... Is>
struct factorial<T,N,std::index_sequence<T, Is...>> {
   static constexpr T value = (static_cast<T>(1)* ... *(Is + 1));
};

int main(int argc, char** argv) {

    std::cout << factorial<int, 5>::value << std::endl;

    return 0;
}

Errors listed in gcc are as follows:

 error: type/value mismatch at argument 1 in template parameter list for 'template<long unsigned int ..._Idx> using index_sequence = std::integer_sequence<long unsigned int, _Idx ...>'
   24 | struct factorial<T,N,std::index_sequence<T, Is...>> {
      |                                               ^~~
note:   expected a constant of type 'long unsigned int', got 'T'
error: template argument 3 is invalid
   24 | struct factorial<T,N,std::index_sequence<T, Is...>> {
      |                                                  ^~
In function 'int main(int, char**)':
error: incomplete type 'factorial<int, 5>' used in nested name specifier
   31 |     std::cout << factorial<int, 5>::value << std::endl;
      |                                     ^~~~~

Clang errors are as follows:

error: template argument for non-type template parameter must be an expression
struct factorial<T,N,std::index_sequence<T, Is...>> {
                                         ^
D:\Programs\msys64\mingw64\include\c++\10.1.0\utility:344:22: note: template parameter is declared here
  template<size_t... _Idx>
                     ^
main.cpp:32:18: error: implicit instantiation of undefined template 'factorial<int, 5, std::integer_sequence<int, 0, 1, 2, 3, 4> >'
    std::cout << factorial<int, 5>::value << std::endl;
                 ^
main.cpp:22:8: note: template is declared here
struct factorial;
       ^

This example is shown in many sites.

Request somebody to help me correcting it.

Body:
You used the wrong type for your result object.

std::index_sequence is a template that accepts only a pack of std::size_t. It doesn't accept a type parameter, because the type is fixed. It is an alias template for std::integer_sequence<std::size_t, Ints...>.

So you trying to specify a type as its the first argument in std::index_sequence<T,...> is wrong.

What you want is the more general type trait std::integer_sequence, making your specialization into this

template <class T, T N, T... Is>
struct factorial<T,N,std::integer_sequence<T, Is...>> {
   static constexpr T value = (static_cast<T>(1)* ... *(Is + 1));
};

Live code example


问:
How to compute a cumulative sum under a limit with Spark?
After several tries and some research, I'm stuck on trying to solve the following problem with Spark.

I have a Dataframe of elements with a priority and a quantity.

```<code>+------+-------+--------+---+
|family|element|priority|qty|
+------+-------+--------+---+
|    f1| elmt 1|       1| 20|
|    f1| elmt 2|       2| 40|
|    f1| elmt 3|       3| 10|
|    f1| elmt 4|       4| 50|
|    f1| elmt 5|       5| 40|
|    f1| elmt 6|       6| 10|
|    f1| elmt 7|       7| 20|
|    f1| elmt 8|       8| 10|
+------+-------+--------+---+
</code>```

I have a fixed limit quantity :

```<code>+------+--------+
|family|limitQty|
+------+--------+
|    f1|     100|
+------+--------+
</code>```

I want to mark as "ok" the elements whose the cumulative sum is under the limit. Here is the expected result :

```<code>+------+-------+--------+---+---+
|family|element|priority|qty| ok|
+------+-------+--------+---+---+
|    f1| elmt 1|       1| 20|  1| -> 20 < 100   => ok
|    f1| elmt 2|       2| 40|  1| -> 20 + 40 < 100  => ok
|    f1| elmt 3|       3| 10|  1| -> 20 + 40 + 10 < 100   => ok
|    f1| elmt 4|       4| 50|  0| -> 20 + 40 + 10 + 50 > 100   => ko
|    f1| elmt 5|       5| 40|  0| -> 20 + 40 + 10 + 40 > 100   => ko
|    f1| elmt 6|       6| 10|  1| -> 20 + 40 + 10 + 10 < 100   => ok
|    f1| elmt 7|       7| 20|  1| -> 20 + 40 + 10 + 10 + 20 < 100   => ok
|    f1| elmt 8|       8| 10|  0| -> 20 + 40 + 10 + 10 + 20 + 10 > 100   => ko
+------+-------+--------+---+---+
</code>```

I try to solve if with a cumulative sum :

    initDF
      .join(limitQtyDF, Seq("family"), "left_outer")
      .withColumn("cumulSum", sum($"qty").over(Window.partitionBy("family").orderBy("priority")))
      .withColumn("ok", when($"cumulSum" <= $"limitQty", 1).otherwise(0))
      .drop("cumulSum", "limitQty")
```

<p>But it's not enough because the elements after the element that is up to the limit are not take into account.
I can't find a way to solve it with Spark. Do you have an idea ?</p>

<p>Here is the corresponding Scala code :</p>

<pre class="lang-scala prettyprint-override"><code>    val sparkSession = SparkSession.builder()
      .master("local[*]")
      .getOrCreate()

    import sparkSession.implicits._

    val initDF = Seq(
      ("f1", "elmt 1", 1, 20),
      ("f1", "elmt 2", 2, 40),
      ("f1", "elmt 3", 3, 10),
      ("f1", "elmt 4", 4, 50),
      ("f1", "elmt 5", 5, 40),
      ("f1", "elmt 6", 6, 10),
      ("f1", "elmt 7", 7, 20),
      ("f1", "elmt 8", 8, 10)
    ).toDF("family", "element", "priority", "qty")

    val limitQtyDF = Seq(("f1", 100)).toDF("family", "limitQty")

    val expectedDF = Seq(
      ("f1", "elmt 1", 1, 20, 1),
      ("f1", "elmt 2", 2, 40, 1),
      ("f1", "elmt 3", 3, 10, 1),
      ("f1", "elmt 4", 4, 50, 0),
      ("f1", "elmt 5", 5, 40, 0),
      ("f1", "elmt 6", 6, 10, 1),
      ("f1", "elmt 7", 7, 20, 1),
      ("f1", "elmt 8", 8, 10, 0)
    ).toDF("family", "element", "priority", "qty", "ok").show()
</code>```

Thank you for your help !

答:
Cumulative sum for each group

```<code>from pyspark.sql.window import Window as window
from pyspark.sql.types import IntegerType,StringType,FloatType,StructType,StructField,DateType
schema = StructType() \
        .add(StructField("empno",IntegerType(),True)) \
        .add(StructField("ename",StringType(),True)) \
        .add(StructField("job",StringType(),True)) \
        .add(StructField("mgr",StringType(),True)) \
        .add(StructField("hiredate",DateType(),True)) \
        .add(StructField("sal",FloatType(),True)) \
        .add(StructField("comm",StringType(),True)) \
        .add(StructField("deptno",IntegerType(),True))

emp = spark.read.csv('data/emp.csv',schema)
dept_partition = window.partitionBy(emp.deptno).orderBy(emp.sal)
emp_win = emp.withColumn("dept_cum_sal",
                         f.sum(emp.sal).over(dept_partition.rowsBetween(window.unboundedPreceding, window.currentRow)))
emp_win.show()
</code>```

Results appear like below:

```<code>+-----+------+---------+----+----------+------+-------+------+------------
+
|empno| ename|      job| mgr|  hiredate|   sal|   comm|deptno|dept_cum_sal|
+-----+------+---------+----+----------+------+-------+------+------------
+
| 7369| SMITH|    CLERK|7902|1980-12-17| 800.0|   null|    20|       800.0|
| 7876| ADAMS|    CLERK|7788|1983-01-12|1100.0|   null|    20|      1900.0|
| 7566| JONES|  MANAGER|7839|1981-04-02|2975.0|   null|    20|      4875.0|
| 7788| SCOTT|  ANALYST|7566|1982-12-09|3000.0|   null|    20|      7875.0|
| 7902|  FORD|  ANALYST|7566|1981-12-03|3000.0|   null|    20|     10875.0|
| 7934|MILLER|    CLERK|7782|1982-01-23|1300.0|   null|    10|      1300.0|
| 7782| CLARK|  MANAGER|7839|1981-06-09|2450.0|   null|    10|      3750.0|
| 7839|  KING|PRESIDENT|null|1981-11-17|5000.0|   null|    10|      8750.0|
| 7900| JAMES|    CLERK|7698|1981-12-03| 950.0|   null|    30|       950.0|
| 7521|  WARD| SALESMAN|7698|1981-02-22|1250.0| 500.00|    30|      2200.0|
| 7654|MARTIN| SALESMAN|7698|1981-09-28|1250.0|1400.00|    30|      3450.0|
| 7844|TURNER| SALESMAN|7698|1981-09-08|1500.0|   0.00|    30|      4950.0|
| 7499| ALLEN| SALESMAN|7698|1981-02-20|1600.0| 300.00|    30|      6550.0|
| 7698| BLAKE|  MANAGER|7839|1981-05-01|2850.0|   null|    30|      9400.0|
+-----+------+---------+----+----------+------+-------+------+------------+
</code>```

Body:
After several tries and some research, I'm stuck on trying to solve the following problem with Spark.

I have a Dataframe of elements with a priority and a quantity.

+------+-------+--------+---+
|family|element|priority|qty|
+------+-------+--------+---+
|    f1| elmt 1|       1| 20|
|    f1| elmt 2|       2| 40|
|    f1| elmt 3|       3| 10|
|    f1| elmt 4|       4| 50|
|    f1| elmt 5|       5| 40|
|    f1| elmt 6|       6| 10|
|    f1| elmt 7|       7| 20|
|    f1| elmt 8|       8| 10|
+------+-------+--------+---+

I have a fixed limit quantity :

+------+--------+
|family|limitQty|
+------+--------+
|    f1|     100|
+------+--------+

I want to mark as "ok" the elements whose the cumulative sum is under the limit. Here is the expected result :

+------+-------+--------+---+---+
|family|element|priority|qty| ok|
+------+-------+--------+---+---+
|    f1| elmt 1|       1| 20|  1| -> 20 < 100   => ok
|    f1| elmt 2|       2| 40|  1| -> 20 + 40 < 100  => ok
|    f1| elmt 3|       3| 10|  1| -> 20 + 40 + 10 < 100   => ok
|    f1| elmt 4|       4| 50|  0| -> 20 + 40 + 10 + 50 > 100   => ko
|    f1| elmt 5|       5| 40|  0| -> 20 + 40 + 10 + 40 > 100   => ko
|    f1| elmt 6|       6| 10|  1| -> 20 + 40 + 10 + 10 < 100   => ok
|    f1| elmt 7|       7| 20|  1| -> 20 + 40 + 10 + 10 + 20 < 100   => ok
|    f1| elmt 8|       8| 10|  0| -> 20 + 40 + 10 + 10 + 20 + 10 > 100   => ko
+------+-------+--------+---+---+

I try to solve if with a cumulative sum :

    initDF
      .join(limitQtyDF, Seq("family"), "left_outer")
      .withColumn("cumulSum", sum($"qty").over(Window.partitionBy("family").orderBy("priority")))
      .withColumn("ok", when($"cumulSum" <= $"limitQty", 1).otherwise(0))
      .drop("cumulSum", "limitQty")

But it's not enough because the elements after the element that is up to the limit are not take into account.
I can't find a way to solve it with Spark. Do you have an idea ?

Here is the corresponding Scala code :

    val sparkSession = SparkSession.builder()
      .master("local[*]")
      .getOrCreate()

    import sparkSession.implicits._

    val initDF = Seq(
      ("f1", "elmt 1", 1, 20),
      ("f1", "elmt 2", 2, 40),
      ("f1", "elmt 3", 3, 10),
      ("f1", "elmt 4", 4, 50),
      ("f1", "elmt 5", 5, 40),
      ("f1", "elmt 6", 6, 10),
      ("f1", "elmt 7", 7, 20),
      ("f1", "elmt 8", 8, 10)
    ).toDF("family", "element", "priority", "qty")

    val limitQtyDF = Seq(("f1", 100)).toDF("family", "limitQty")

    val expectedDF = Seq(
      ("f1", "elmt 1", 1, 20, 1),
      ("f1", "elmt 2", 2, 40, 1),
      ("f1", "elmt 3", 3, 10, 1),
      ("f1", "elmt 4", 4, 50, 0),
      ("f1", "elmt 5", 5, 40, 0),
      ("f1", "elmt 6", 6, 10, 1),
      ("f1", "elmt 7", 7, 20, 1),
      ("f1", "elmt 8", 8, 10, 0)
    ).toDF("family", "element", "priority", "qty", "ok").show()

Thank you for your help !

Body:
Cumulative sum for each group

from pyspark.sql.window import Window as window
from pyspark.sql.types import IntegerType,StringType,FloatType,StructType,StructField,DateType
schema = StructType() \
        .add(StructField("empno",IntegerType(),True)) \
        .add(StructField("ename",StringType(),True)) \
        .add(StructField("job",StringType(),True)) \
        .add(StructField("mgr",StringType(),True)) \
        .add(StructField("hiredate",DateType(),True)) \
        .add(StructField("sal",FloatType(),True)) \
        .add(StructField("comm",StringType(),True)) \
        .add(StructField("deptno",IntegerType(),True))

emp = spark.read.csv('data/emp.csv',schema)
dept_partition = window.partitionBy(emp.deptno).orderBy(emp.sal)
emp_win = emp.withColumn("dept_cum_sal",
                         f.sum(emp.sal).over(dept_partition.rowsBetween(window.unboundedPreceding, window.currentRow)))
emp_win.show()

Results appear like below:

+-----+------+---------+----+----------+------+-------+------+------------
+
|empno| ename|      job| mgr|  hiredate|   sal|   comm|deptno|dept_cum_sal|
+-----+------+---------+----+----------+------+-------+------+------------
+
| 7369| SMITH|    CLERK|7902|1980-12-17| 800.0|   null|    20|       800.0|
| 7876| ADAMS|    CLERK|7788|1983-01-12|1100.0|   null|    20|      1900.0|
| 7566| JONES|  MANAGER|7839|1981-04-02|2975.0|   null|    20|      4875.0|
| 7788| SCOTT|  ANALYST|7566|1982-12-09|3000.0|   null|    20|      7875.0|
| 7902|  FORD|  ANALYST|7566|1981-12-03|3000.0|   null|    20|     10875.0|
| 7934|MILLER|    CLERK|7782|1982-01-23|1300.0|   null|    10|      1300.0|
| 7782| CLARK|  MANAGER|7839|1981-06-09|2450.0|   null|    10|      3750.0|
| 7839|  KING|PRESIDENT|null|1981-11-17|5000.0|   null|    10|      8750.0|
| 7900| JAMES|    CLERK|7698|1981-12-03| 950.0|   null|    30|       950.0|
| 7521|  WARD| SALESMAN|7698|1981-02-22|1250.0| 500.00|    30|      2200.0|
| 7654|MARTIN| SALESMAN|7698|1981-09-28|1250.0|1400.00|    30|      3450.0|
| 7844|TURNER| SALESMAN|7698|1981-09-08|1500.0|   0.00|    30|      4950.0|
| 7499| ALLEN| SALESMAN|7698|1981-02-20|1600.0| 300.00|    30|      6550.0|
| 7698| BLAKE|  MANAGER|7839|1981-05-01|2850.0|   null|    30|      9400.0|
+-----+------+---------+----+----------+------+-------+------+------------+


答: async/await syntax is what allows currentCDFlow to be set from the promise. This is a feature in ES6. Asynchronous functions are prefixed with the async keyword and await suspends the execution until the promise is fulfilled and will unwrap the returned value.

Title: Angular Typescript why can assign a Promise<ClassA> to object of classA


问:
Checking if float is equivalent to an integer value in python
In Python 3, I am checking whether a given value is triangular, that is, it can be represented as n * (n + 1) / 2 for some positive integer n.

Can I just write:

```<code>import math

def is_triangular1(x):
    num = (1 / 2) * (math.sqrt(8 * x + 1) - 1)
    return int(num) == num
</code>```

Or do I need to do check within a tolerance instead?

```<code>epsilon = 0.000000000001
def is_triangular2(x):
    num = (1 / 2) * (math.sqrt(8 * x + 1) - 1)
    return abs(int(num) - num) < epsilon
</code>```

I checked that both of the functions return same results for x up to 1,000,000. But I am not sure if generally speaking int(x) == x will always correctly determine whether a number is integer, because of the cases when for example 5 is represented as 4.99999999999997 etc.

As far as I know, the second way is the correct one if I do it in C, but I am not sure about Python 3.

答:
floats can exactly represent all integers in their range - floating-point equality is only tricky if you care about the bit after the point. So, as long as all of the calculations in your formula return whole numbers for the cases you're interested in, int(num) == num is perfectly safe.

So, we need to prove that for any triangular number, every piece of maths you do can be done with integer arithmetic (and anything coming out as a non-integer must imply that x is not triangular):

To start with, we can assume that x must be an integer - this is required in the definition of 'triangular number'.

This being the case, 8*x + 1 will also be an integer, since the integers are closed under + and * .

math.sqrt() returns float; but if x is triangular, then the square root will be a whole number - ie, again exactly represented.

So, for all x that should return true in your functions, int(num) == num will be true, and so your istriangular1 will always work. The only sticking point, as mentioned in the comments to the question, is that Python 2 by default does integer division in the same way as C - int/int => int, truncating if the result can't be represented exactly as an int. So, 1/2 == 0. This is fixed in Python 3, or by having the line

```<code>from __future__ import division
</code>```

near the top of your code.

Body:
In Python 3, I am checking whether a given value is triangular, that is, it can be represented as n * (n + 1) / 2 for some positive integer n.

Can I just write:

import math

def is_triangular1(x):
    num = (1 / 2) * (math.sqrt(8 * x + 1) - 1)
    return int(num) == num

Or do I need to do check within a tolerance instead?

epsilon = 0.000000000001
def is_triangular2(x):
    num = (1 / 2) * (math.sqrt(8 * x + 1) - 1)
    return abs(int(num) - num) < epsilon

I checked that both of the functions return same results for x up to 1,000,000. But I am not sure if generally speaking int(x) == x will always correctly determine whether a number is integer, because of the cases when for example 5 is represented as 4.99999999999997 etc.

As far as I know, the second way is the correct one if I do it in C, but I am not sure about Python 3.

Body:
floats can exactly represent all integers in their range - floating-point equality is only tricky if you care about the bit after the point. So, as long as all of the calculations in your formula return whole numbers for the cases you're interested in, int(num) == num is perfectly safe.

So, we need to prove that for any triangular number, every piece of maths you do can be done with integer arithmetic (and anything coming out as a non-integer must imply that x is not triangular):

To start with, we can assume that x must be an integer - this is required in the definition of 'triangular number'.

This being the case, 8*x + 1 will also be an integer, since the integers are closed under + and * .

math.sqrt() returns float; but if x is triangular, then the square root will be a whole number - ie, again exactly represented.

So, for all x that should return true in your functions, int(num) == num will be true, and so your istriangular1 will always work. The only sticking point, as mentioned in the comments to the question, is that Python 2 by default does integer division in the same way as C - int/int => int, truncating if the result can't be represented exactly as an int. So, 1/2 == 0. This is fixed in Python 3, or by having the line

from __future__ import division

near the top of your code.


问:
Couldnt display data from firebase in recyclerview
I am developing an android app which shows the user with the latest jobs from various fields. I want to display the jobs that the user has applied so far in "Your Applications" section.

But the values are not getting retrieved. It displays a blank activity. I have attached the code.

Applications.java

```<code>import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.Application;
import android.app.ProgressDialog;
import android.os.Bundle;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;

public class Applications extends AppCompatActivity {

    DatabaseReference databaseReference;
    FirebaseDatabase firebaseDatabase;
    RecyclerView recyclerView;
    FirebaseUser user;
    FirebaseAuth mAuth;
    ArrayList<Post> posts;
    MyAdap adap;
    ProgressDialog pd;
    long maxid=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_applications);
        recyclerView = findViewById(R.id.work);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAuth = FirebaseAuth.getInstance();
        user = mAuth.getCurrentUser();
        posts = new ArrayList<>();
        pd= new ProgressDialog(Applications.this);
        pd.setMessage("Loading...");
        pd.show();

        getSupportActionBar().setTitle("Your Applications");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        firebaseDatabase = FirebaseDatabase.getInstance();
        databaseReference = firebaseDatabase.getReference("Job Applications");

        Query query = databaseReference.orderByChild(String.valueOf(maxid+1)).equalTo(user.getUid());
        query.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot ds: dataSnapshot.getChildren()){
                    Post p = ds.getValue(Post.class);
                    posts.add(p);
                }
                pd.dismiss();
                adap = new MyAdap(Applications.this, posts);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                pd.dismiss();
            }
        });
    }
}
</code>```

Posts.java

```<code>    public class Post {
    private String jobtitle, jobtype, company, location;

    public Post() {
    }

    public Post(String jobtitle, String jobtype, String company, String location) {
        this.jobtitle = jobtitle;
        this.jobtype = jobtype;
        this.company = company;
        this.location = location;
    }

    public String getJobtitle() {
        return jobtitle;
    }

    public void setJobtitle(String jobtitle) {
        this.jobtitle = jobtitle;
    }

    public String getJobtype() {
        return jobtype;
    }

    public void setJobtype(String jobtype) {
        this.jobtype = jobtype;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }
</code>```

MyAdap.java

```<code>import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class MyAdap extends RecyclerView.Adapter<MyAdap.ViewHolder> {

    Context cb;
    ArrayList<Post> posts;

    public MyAdap(Context c, ArrayList<Post> applications){
        cb=c;
        posts=applications;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(cb).inflate(R.layout.post, parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.jobTitle.setText(posts.get(position).getJobtitle());
        holder.jobType.setText(posts.get(position).getJobtype());
        holder.location.setText(posts.get(position).getLocation());
        holder.companyName.setText(posts.get(position).getCompany());
    }

    @Override
    public int getItemCount() {
        return posts.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView jobTitle, jobType,companyName,location;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            jobTitle = itemView.findViewById(R.id.jobTitle);
            jobType = itemView.findViewById(R.id.jobType);
            companyName = itemView.findViewById(R.id.companyName);
            location = itemView.findViewById(R.id.location);
        }
    }
}
</code>```

Here is the image of the database.

答:
I think you have a problem with your orderByChild and equalTo method in the Query section. Please try without the Query and if you want to add sorting read details here.
And one more thing you don't have any jobtype in your database but in your POJO class you have this field in your Constructor.

```<code>databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot ds: dataSnapshot.getChildren()){
                Post p = ds.getValue(Post.class);
                posts.add(p);
            }
            pd.dismiss();
            adap = new MyAdap(Applications.this, posts);
            recyclerView.setAdapter(adap);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            pd.dismiss();
        }
    });
</code>```

Body:
I am developing an android app which shows the user with the latest jobs from various fields. I want to display the jobs that the user has applied so far in "Your Applications" section.

But the values are not getting retrieved. It displays a blank activity. I have attached the code.

Applications.java

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.Application;
import android.app.ProgressDialog;
import android.os.Bundle;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;

public class Applications extends AppCompatActivity {

    DatabaseReference databaseReference;
    FirebaseDatabase firebaseDatabase;
    RecyclerView recyclerView;
    FirebaseUser user;
    FirebaseAuth mAuth;
    ArrayList<Post> posts;
    MyAdap adap;
    ProgressDialog pd;
    long maxid=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_applications);
        recyclerView = findViewById(R.id.work);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAuth = FirebaseAuth.getInstance();
        user = mAuth.getCurrentUser();
        posts = new ArrayList<>();
        pd= new ProgressDialog(Applications.this);
        pd.setMessage("Loading...");
        pd.show();

        getSupportActionBar().setTitle("Your Applications");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        firebaseDatabase = FirebaseDatabase.getInstance();
        databaseReference = firebaseDatabase.getReference("Job Applications");

        Query query = databaseReference.orderByChild(String.valueOf(maxid+1)).equalTo(user.getUid());
        query.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot ds: dataSnapshot.getChildren()){
                    Post p = ds.getValue(Post.class);
                    posts.add(p);
                }
                pd.dismiss();
                adap = new MyAdap(Applications.this, posts);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                pd.dismiss();
            }
        });
    }
}

Posts.java

    public class Post {
    private String jobtitle, jobtype, company, location;

    public Post() {
    }

    public Post(String jobtitle, String jobtype, String company, String location) {
        this.jobtitle = jobtitle;
        this.jobtype = jobtype;
        this.company = company;
        this.location = location;
    }

    public String getJobtitle() {
        return jobtitle;
    }

    public void setJobtitle(String jobtitle) {
        this.jobtitle = jobtitle;
    }

    public String getJobtype() {
        return jobtype;
    }

    public void setJobtype(String jobtype) {
        this.jobtype = jobtype;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

MyAdap.java

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class MyAdap extends RecyclerView.Adapter<MyAdap.ViewHolder> {

    Context cb;
    ArrayList<Post> posts;

    public MyAdap(Context c, ArrayList<Post> applications){
        cb=c;
        posts=applications;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(cb).inflate(R.layout.post, parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.jobTitle.setText(posts.get(position).getJobtitle());
        holder.jobType.setText(posts.get(position).getJobtype());
        holder.location.setText(posts.get(position).getLocation());
        holder.companyName.setText(posts.get(position).getCompany());
    }

    @Override
    public int getItemCount() {
        return posts.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView jobTitle, jobType,companyName,location;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            jobTitle = itemView.findViewById(R.id.jobTitle);
            jobType = itemView.findViewById(R.id.jobType);
            companyName = itemView.findViewById(R.id.companyName);
            location = itemView.findViewById(R.id.location);
        }
    }
}

Here is the image of the database.

Body:
I think you have a problem with your orderByChild and equalTo method in the Query section. Please try without the Query and if you want to add sorting read details here.
And one more thing you don't have any jobtype in your database but in your POJO class you have this field in your Constructor.

databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot ds: dataSnapshot.getChildren()){
                Post p = ds.getValue(Post.class);
                posts.add(p);
            }
            pd.dismiss();
            adap = new MyAdap(Applications.this, posts);
            recyclerView.setAdapter(adap);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            pd.dismiss();
        }
    });


答:
I did the following... not super happy about it, but it works.</p>

```<code>import numpy as np
import pandas as pd

filename = 'test_data'

df = pd.read_excel(filename + '.xlsx', header=None)

df_list = np.split(df, df[df.isnull().all(1)].index)

del df_list[0]

for i, df in enumerate(df_list):

    df.iloc[3, 2:] = df.iloc[2, 2:]

    new_header = df.iloc[3]
    df.columns = new_header

    df = df.iloc[4:]
    df_tmp = df.drop(['Frame'], axis=1)
    df = df_tmp.set_index("Time")
    df.dropna(axis=1, how='all', inplace=True)
    df.columns.name = None

    df_list[i] = df

df = pd.concat(df_list, axis=1)
df = df.reindex(sorted(df.columns), axis=1)

df.to_csv(filename + '.csv')
</code>```

Title: Python: Pandas - tidy data frame from multiple header excel sheet

Body:
I have an excel sheet output from a software tool that is structured in the following multi-header way.
excel structure:

+---+-------+--------------+--------------+
|   |       |              |              |
+---+-------+--------------+--------------+
|   |       | not relevant | not relevant |
+---+-------+--------------+--------------+
|   |       | X1           | Y1           |
+---+-------+--------------+--------------+
|fr | Time  | not relevant | not relevant |
+---+-------+--------------+--------------+
| 1 | 0.000 | 12           | 32           |
+---+-------+--------------+--------------+
| 2 | 0.010 | 23           | 3            |
+---+-------+--------------+--------------+
| 3 | 0.020 | 45           | 4            |
+---+-------+--------------+--------------+
| 4 | 0.030 | 4            | 1            |
+---+-------+--------------+--------------+
|   |       |              |              |
+---+-------+--------------+--------------+
|   |       | not relevant |              |
+---+-------+--------------+--------------+
|   |       | Y2           |              |
+---+-------+--------------+--------------+
|fr | Time  | not relevant |              |
+---+-------+--------------+--------------+
| 1 | 0.000 | 5            |              |
+---+-------+--------------+--------------+
| 2 | 0.010 | 89           |              |
+---+-------+--------------+--------------+
| 3 | 0.020 | 5            |              |
+---+-------+--------------+--------------+
| 4 | 0.030 | 3            |              |
+---+-------+--------------+--------------+
|   |       |              |              |
+---+-------+--------------+--------------+
|   |       | not relevant |              |
+---+-------+--------------+--------------+
|   |       | X3           |              |
+---+-------+--------------+--------------+
|fr | Time  | not relevant |              |
+---+-------+--------------+--------------+
| 1 | 0.000 | 17           |              |
+---+-------+--------------+--------------+
| 2 | 0.010 | 2            |              |
+---+-------+--------------+--------------+
| 3 | 0.020 | 4            |              |
+---+-------+--------------+--------------+
| 4 | 0.030 | 23           |              |
+---+-------+--------------+--------------+

csv structure:

,,,
,,not relevant,not relevant
,,X1,Y1
fr,Time,not relevant,not relevant
1,0.000,12,32
2,0.010,23,3
3,0.020,45,4
4,0.030,4,1
,,,
,,not relevant,
,,Y2,
fr,Time,not relevant,
1,0.000,5,
2,0.010,89,
3,0.020,5,
4,0.030,3,
,,,
,,not relevant,
,,X3,
fr,Time,not relevant,
1,0.000,17,
2,0.010,2,
3,0.020,4,
4,0.030,23,

I am looking for a fast way to convert this messy data into a tidy pandas dataframe.

The timestamps are identical in value and number for each individual sub-series.

the number of sub-series is variable.

The end result should look as follows.

  Time    X1     Y1     Y2     X3
  0.000   12     32     5      17
  0.010   23     3      89     2
  0.020   45     4      5      4
  0.030   4      1      3      23

Body:
I did the following... not super happy about it, but it works.

import numpy as np
import pandas as pd

filename = 'test_data'

df = pd.read_excel(filename + '.xlsx', header=None)

df_list = np.split(df, df[df.isnull().all(1)].index)

del df_list[0]

for i, df in enumerate(df_list):

    df.iloc[3, 2:] = df.iloc[2, 2:]

    new_header = df.iloc[3]
    df.columns = new_header

    df = df.iloc[4:]
    df_tmp = df.drop(['Frame'], axis=1)
    df = df_tmp.set_index("Time")
    df.dropna(axis=1, how='all', inplace=True)
    df.columns.name = None

    df_list[i] = df

df = pd.concat(df_list, axis=1)
df = df.reindex(sorted(df.columns), axis=1)

df.to_csv(filename + '.csv')


问:
Applying Dark mode is triggering the build method of StatefulWidget a couple of time
I am trying to apply Dark theme to the application, when I turn on and off the Switch for the dark theme I can see the build method is called a couple of times.

why is that?

is this normal behavior of flutter?

will this cause any performance issue?

Note: I have added a print statement in the build method to find out how many times the build method is called out.

Is there a change I should do in my code to correct this behavior?

```<code>import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    print("MyApp");
    return ChangeNotifierProvider<ThemeChanger>(
      create: (_) => ThemeChanger(ThemeData.dark()),
      child: new MaterialAppWithTheme(),
    );
  }
}

class MaterialAppWithTheme extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    print("MaterialAppWithTheme");
    final theme = Provider.of<ThemeChanger>(context);

    return MaterialApp(
      home: SettingScreen(),
      theme: theme.getTheme(),
    );
  }
}

class SettingScreen extends StatelessWidget {
  static final routeName = "./SettingScreen";

  @override
  Widget build(BuildContext context) {
    print("SettingScreen");
    ThemeData themeData = Theme.of(context);
    return Scaffold(
      appBar: AppBar(
        title: const Text(
          "Settings",
        ),
      ),
      body: ListView(
        children: <Widget>[
          const SizedBox(
            height: 10,
          ),
          Card(
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(12),
            ),
            child: Column(
              children: <Widget>[
                Container(
                  padding: const EdgeInsets.all(8.0),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(10),
                    color: themeData.primaryColor,
                  ),
                  width: double.infinity,
                  child: const Text(
                    "User Settings",
                    style: TextStyle(fontSize: 15, color: Colors.white),
                    textAlign: TextAlign.center,
                  ),
                ),
                const SizedBox(
                  height: 10,
                ),
                DarkThemeOption(),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

class DarkThemeOption extends StatefulWidget {
  @override
  _DarkThemeOptionState createState() => _DarkThemeOptionState();
}

class _DarkThemeOptionState extends State<DarkThemeOption> {
  var enableDarkTheme = false;
  var themeProvider;
  ThemeData themeData;

  @override
  void initState() {
    print("_DarkThemeOptionState init");
    themeProvider = Provider.of<ThemeChanger>(context, listen: false);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    print("_DarkThemeOptionState");
    themeData = Theme.of(context);
    return Padding(
      padding: const EdgeInsets.only(top: 5, left: 22, bottom: 10),
      child: Row(
        children: <Widget>[
          const Text(
            "Enable Dark Theme",
            style: TextStyle(
              fontWeight: FontWeight.bold,
              color: Colors.blue,
            ),
          ),
          const Spacer(),
          Switch(
              activeColor: themeData.accentColor,
              value: enableDarkTheme,
              onChanged: (value) async {
                // final status = await authProvider.updateThemeSetting(value);
                setState(() {
                  enableDarkTheme = value;
                  if (!enableDarkTheme) {
                    themeProvider.setTheme(ThemeData.light());
                  } else {
                    themeProvider.setTheme(ThemeData.dark());
                  }
                });
              }),
        ],
      ),
    );
  }
}

class ThemeChanger with ChangeNotifier {
  ThemeData _themeData;

  ThemeChanger(this._themeData);

  getTheme() => _themeData;
  setTheme(ThemeData theme) {
    _themeData = theme;

    notifyListeners();
  }
}

</code>```

From the logs, I can see each time I turn on and off the dark mode switch I see the setting screen and _DarkThemeOptionState being called more than 5 times.

```<code>I/flutter ( 1176): MaterialAppWithTheme
I/flutter ( 1176): SettingScreen
I/flutter ( 1176): _DarkThemeOptionState
I/flutter ( 1176): SettingScreen
I/flutter ( 1176): _DarkThemeOptionState
I/flutter ( 1176): SettingScreen
I/flutter ( 1176): _DarkThemeOptionState
I/flutter ( 1176): SettingScreen
I/flutter ( 1176): _DarkThemeOptionState
I/flutter ( 1176): SettingScreen
I/flutter ( 1176): _DarkThemeOptionState
I/flutter ( 1176): MaterialAppWithTheme
I/flutter ( 1176): SettingScreen
I/flutter ( 1176): _DarkThemeOptionState
I/flutter ( 1176): SettingScreen
I/flutter ( 1176): _DarkThemeOptionState
I/flutter ( 1176): SettingScreen
I/flutter ( 1176): _DarkThemeOptionState
I/flutter ( 1176): SettingScreen
I/flutter ( 1176): _DarkThemeOptionState
I/flutter ( 1176): SettingScreen
I/flutter ( 1176): _DarkThemeOptionState
I/flutter ( 1176): SettingScreen
I/flutter ( 1176): _DarkThemeOptionState
I/flutter ( 1176): MaterialAppWithTheme
I/flutter ( 1176): SettingScreen
I/flutter ( 1176): _DarkThemeOptionState
I/flutter ( 1176): SettingScreen
I/flutter ( 1176): _DarkThemeOptionState
I/flutter ( 1176): SettingScreen
I/flutter ( 1176): _DarkThemeOptionState
I/flutter ( 1176): SettingScreen
I/flutter ( 1176): _DarkThemeOptionState
I/flutter ( 1176): SettingScreen
I/flutter ( 1176): _DarkThemeOptionState
I/flutter ( 1176): SettingScreen
I/flutter ( 1176): _DarkThemeOptionState
</code>```

Light Mode

Dark Mode

答: It is normal, because when changing the theme, localizations, etc., it must re-render the view, therefore the compilation method is called again</p>

Body:
I am trying to apply Dark theme to the application, when I turn on and off the Switch for the dark theme I can see the build method is called a couple of times.

why is that?

is this normal behavior of flutter?

will this cause any performance issue?

Note: I have added a print statement in the build method to find out how many times the build method is called out.

Is there a change I should do in my code to correct this behavior?

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    print("MyApp");
    return ChangeNotifierProvider<ThemeChanger>(
      create: (_) => ThemeChanger(ThemeData.dark()),
      child: new MaterialAppWithTheme(),
    );
  }
}

class MaterialAppWithTheme extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    print("MaterialAppWithTheme");
    final theme = Provider.of<ThemeChanger>(context);

    return MaterialApp(
      home: SettingScreen(),
      theme: theme.getTheme(),
    );
  }
}

class SettingScreen extends StatelessWidget {
  static final routeName = "./SettingScreen";

  @override
  Widget build(BuildContext context) {
    print("SettingScreen");
    ThemeData themeData = Theme.of(context);
    return Scaffold(
      appBar: AppBar(
        title: const Text(
          "Settings",
        ),
      ),
      body: ListView(
        children: <Widget>[
          const SizedBox(
            height: 10,
          ),
          Card(
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(12),
            ),
            child: Column(
              children: <Widget>[
                Container(
                  padding: const EdgeInsets.all(8.0),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(10),
                    color: themeData.primaryColor,
                  ),
                  width: double.infinity,
                  child: const Text(
                    "User Settings",
                    style: TextStyle(fontSize: 15, color: Colors.white),
                    textAlign: TextAlign.center,
                  ),
                ),
                const SizedBox(
                  height: 10,
                ),
                DarkThemeOption(),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

class DarkThemeOption extends StatefulWidget {
  @override
  _DarkThemeOptionState createState() => _DarkThemeOptionState();
}

class _DarkThemeOptionState extends State<DarkThemeOption> {
  var enableDarkTheme = false;
  var themeProvider;
  ThemeData themeData;

  @override
  void initState() {
    print("_DarkThemeOptionState init");
    themeProvider = Provider.of<ThemeChanger>(context, listen: false);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    print("_DarkThemeOptionState");
    themeData = Theme.of(context);
    return Padding(
      padding: const EdgeInsets.only(top: 5, left: 22, bottom: 10),
      child: Row(
        children: <Widget>[
          const Text(
            "Enable Dark Theme",
            style: TextStyle(
              fontWeight: FontWeight.bold,
              color: Colors.blue,
            ),
          ),
          const Spacer(),
          Switch(
              activeColor: themeData.accentColor,
              value: enableDarkTheme,
              onChanged: (value) async {
                // final status = await authProvider.updateThemeSetting(value);
                setState(() {
                  enableDarkTheme = value;
                  if (!enableDarkTheme) {
                    themeProvider.setTheme(ThemeData.light());
                  } else {
                    themeProvider.setTheme(ThemeData.dark());
                  }
                });
              }),
        ],
      ),
    );
  }
}

class ThemeChanger with ChangeNotifier {
  ThemeData _themeData;

  ThemeChanger(this._themeData);

  getTheme() => _themeData;
  setTheme(ThemeData theme) {
    _themeData = theme;

    notifyListeners();
  }
}

From the logs, I can see each time I turn on and off the dark mode switch I see the setting screen and _DarkThemeOptionState being called more than 5 times.

I/flutter ( 1176): MaterialAppWithTheme
I/flutter ( 1176): SettingScreen
I/flutter ( 1176): _DarkThemeOptionState
I/flutter ( 1176): SettingScreen
I/flutter ( 1176): _DarkThemeOptionState
I/flutter ( 1176): SettingScreen
I/flutter ( 1176): _DarkThemeOptionState
I/flutter ( 1176): SettingScreen
I/flutter ( 1176): _DarkThemeOptionState
I/flutter ( 1176): SettingScreen
I/flutter ( 1176): _DarkThemeOptionState
I/flutter ( 1176): MaterialAppWithTheme
I/flutter ( 1176): SettingScreen
I/flutter ( 1176): _DarkThemeOptionState
I/flutter ( 1176): SettingScreen
I/flutter ( 1176): _DarkThemeOptionState
I/flutter ( 1176): SettingScreen
I/flutter ( 1176): _DarkThemeOptionState
I/flutter ( 1176): SettingScreen
I/flutter ( 1176): _DarkThemeOptionState
I/flutter ( 1176): SettingScreen
I/flutter ( 1176): _DarkThemeOptionState
I/flutter ( 1176): SettingScreen
I/flutter ( 1176): _DarkThemeOptionState
I/flutter ( 1176): MaterialAppWithTheme
I/flutter ( 1176): SettingScreen
I/flutter ( 1176): _DarkThemeOptionState
I/flutter ( 1176): SettingScreen
I/flutter ( 1176): _DarkThemeOptionState
I/flutter ( 1176): SettingScreen
I/flutter ( 1176): _DarkThemeOptionState
I/flutter ( 1176): SettingScreen
I/flutter ( 1176): _DarkThemeOptionState
I/flutter ( 1176): SettingScreen
I/flutter ( 1176): _DarkThemeOptionState
I/flutter ( 1176): SettingScreen
I/flutter ( 1176): _DarkThemeOptionState

Light Mode

Dark Mode


问:
How to execute a stored procedure in Oracle
I have written a stored procedure and i want to execute it.

The procedure is as follows:

```<code>create or replace procedure details( p_cursor OUT SYS_REFCURSOR)
is
begin
    OPEN p_cursor FOR
       select name,address,phone,email
       from client
       join ccondition on ccondition.clientno=client.clientno
       where condition!='Acrophobia' and condition!='Heart Condition';

END details;
</code>```

I have tried different methods to execute but i got different sort of errors when I tried every one of them.

答:
As the procedure returns the result via OUT parameter, you have to declare something which will accept it. As I don't have your tables, I'll use Scott's EMP, just to show how it works in SQL*Plus.

```<code>SQL> create or replace procedure details (p_cursor out sys_refcursor)
  2  is
  3  begin
  4    open p_cursor for
  5      select empno, ename, job, sal
  6      from emp
  7      where deptno = 10;
  8  end;
  9  /

Procedure created.

SQL> var rc refcursor;
SQL>
SQL> begin
  2    details (:rc);
  3  end;
  4  /

PL/SQL procedure successfully completed.

SQL> print rc

     EMPNO ENAME      JOB              SAL
---------- ---------- --------- ----------
      7782 CLARK      MANAGER         2450
      7839 KING       PRESIDENT       5000
      7934 MILLER     CLERK           1300

SQL>
</code>```

It would probably help if you posted what you tried so that we could help you fix those errors (if possible), because - maybe you wanted some other option.

If you use Oracle Live SQL, something like this could do the job:

```<code>create table emp as
  select 7782 empno, 'CLARK' ename from dual union all
  select 7839      , 'KING'        from dual union all
  select 7934      , 'MILLER'      from dual;

create or replace procedure details (p_cursor out sys_refcursor)
is
begin
  open p_cursor for
    select empno, ename
    from emp;
end;
/

declare
  rc sys_refcursor;
  l_empno emp.empno%type;
  l_ename emp.ename%type;
begin
  details (rc);
  loop
    fetch rc into l_empno, l_ename;
    exit when rc%notfound;
    dbms_output.put_line(l_empno||' - '|| l_ename);
  end loop;
end;
/
</code>```

Body:
I have written a stored procedure and i want to execute it.

The procedure is as follows:

create or replace procedure details( p_cursor OUT SYS_REFCURSOR)
is
begin
    OPEN p_cursor FOR
       select name,address,phone,email
       from client
       join ccondition on ccondition.clientno=client.clientno
       where condition!='Acrophobia' and condition!='Heart Condition';

END details;

I have tried different methods to execute but i got different sort of errors when I tried every one of them.

Body:
As the procedure returns the result via OUT parameter, you have to declare something which will accept it. As I don't have your tables, I'll use Scott's EMP, just to show how it works in SQL*Plus.

SQL> create or replace procedure details (p_cursor out sys_refcursor)
  2  is
  3  begin
  4    open p_cursor for
  5      select empno, ename, job, sal
  6      from emp
  7      where deptno = 10;
  8  end;
  9  /

Procedure created.

SQL> var rc refcursor;
SQL>
SQL> begin
  2    details (:rc);
  3  end;
  4  /

PL/SQL procedure successfully completed.

SQL> print rc

     EMPNO ENAME      JOB              SAL
---------- ---------- --------- ----------
      7782 CLARK      MANAGER         2450
      7839 KING       PRESIDENT       5000
      7934 MILLER     CLERK           1300

SQL>

It would probably help if you posted what you tried so that we could help you fix those errors (if possible), because - maybe you wanted some other option.

If you use Oracle Live SQL, something like this could do the job:

create table emp as
  select 7782 empno, 'CLARK' ename from dual union all
  select 7839      , 'KING'        from dual union all
  select 7934      , 'MILLER'      from dual;

create or replace procedure details (p_cursor out sys_refcursor)
is
begin
  open p_cursor for
    select empno, ename
    from emp;
end;
/

declare
  rc sys_refcursor;
  l_empno emp.empno%type;
  l_ename emp.ename%type;
begin
  details (rc);
  loop
    fetch rc into l_empno, l_ename;
    exit when rc%notfound;
    dbms_output.put_line(l_empno||' - '|| l_ename);
  end loop;
end;
/


问:
Unexpected undefined in function
My code output the position of every word in alphabet.

function alphabetPosition(text) {
    const alphabet = 'abcdefghijklmnopqrstuvwxyz'.split("");
    const arrayFromText = text.toLowerCase().split("");
    alphabet.map((letter, position) => {
        if (arrayFromText.includes(letter) && alphabet.includes(letter)) {
            return position + 1;
        }
    })
}
console.log(alphabetPosition('hello'));```
</div>
</div>
</p>

<p>Now it get <code>undefined</code>, but if i change to:</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>function alphabetPosition(text) {
    const alphabet = 'abcdefghijklmnopqrstuvwxyz'.split("");
    const arrayFromText = text.toLowerCase().split("");
    alphabet.map((letter, position) => {
        if (arrayFromText.includes(letter) && alphabet.includes(letter)) {
            console.log(position + 1);
        }
    })
}
console.log(alphabetPosition('hello'));</code>```

I get the expected result.
 Why i get undefined in first example, and how to output the result using return statement, not console.log ?

答:
First Example

return is returning from the function expression passed to map

return is not returning from the alphabetPosition function, because the function returns nothing, your console statement will output undefined

Second Example

inside the map it's just outputting to the log

having the console.log inside the map ensures the position + 1 is defined, but note that it's not returning anything from that function

because you have no return statement at the end of alphabetPosition, the function returns an undefined

you have two log methods called, you should have seen the value of position + 1 plus an undefined since the function returns nothing and you're still calling a log method

One way to make it work as expected is to return your mapping.  I added a filter to ignore all the undefined values in the array:

function alphabetPosition(text) {
  const alphabet = 'abcdefghijklmnopqrstuvwxyz'.split("");
  const arrayFromText = text.toLowerCase().split("");
  return alphabet.map((letter, position) => {
    if (arrayFromText.includes(letter) && alphabet.includes(letter)) {
      return position + 1;
    }
  }).filter(v=>!!v)
}
console.log(alphabetPosition('hello'));```

Title: Unexpected undefined in function

Body:
My code output the position of every word in alphabet.

function alphabetPosition(text) {
    const alphabet = 'abcdefghijklmnopqrstuvwxyz'.split("");
    const arrayFromText = text.toLowerCase().split("");
    alphabet.map((letter, position) => {
        if (arrayFromText.includes(letter) && alphabet.includes(letter)) {
            return position + 1;
        }
    })
}
console.log(alphabetPosition('hello'));

Now it get undefined, but if i change to:

function alphabetPosition(text) {
    const alphabet = 'abcdefghijklmnopqrstuvwxyz'.split("");
    const arrayFromText = text.toLowerCase().split("");
    alphabet.map((letter, position) => {
        if (arrayFromText.includes(letter) && alphabet.includes(letter)) {
            console.log(position + 1);
        }
    })
}
console.log(alphabetPosition('hello'));

I get the expected result.
 Why i get undefined in first example, and how to output the result using return statement, not console.log ?


问:
How to optimize the query? t-sql
This query works about 3 minutes and returns 7279 rows:

```<code>SELECT identity(int,1,1) as id, c.client_code, a.account_num,
        c.client_short_name, u.uso, us.fio, null as new, null as txt
INTO #ttable
FROM accounts a INNER JOIN Clients c ON
    c.id = a.client_id INNER JOIN Uso u ON c.uso_id = u.uso_id INNER JOIN
    Magazin m ON a.account_id = m.account_id LEFT JOIN Users us ON
    m.user_id = us.user_id
WHERE m.status_id IN ('1','5','9') AND m.account_new_num is null
    AND u.branch_id = @branch_id
ORDER BY c.client_code;
</code>```

The type of 'client_code' field is VARCHAR(6).

Is it possible to somehow optimize this query?

答:
Always try to list tables earlier if they are referenced in the where clause - it cuts off row combinations as early as possible. In this case, the Magazin table has some predicates in the where clause, but is listed way down in the tables list. This means that all the other joins have to be made before the Magazin rows can be filtered - possibly millions of extra rows.

Try this (and let us know how it went):

```<code>SELECT ...
INTO #ttable
FROM accounts a
INNER JOIN Magazin m ON a.account_id = m.account_id
INNER JOIN Clients c ON c.id = a.client_id
INNER JOIN Uso u ON c.uso_id = u.uso_id
LEFT JOIN Users us ON m.user_id = us.user_id
WHERE m.status_id IN ('1','5','9')
AND m.account_new_num is null
AND u.branch_id = @branch_id
ORDER BY c.client_code;
</code>```

This kind of optimization can greatly improve query performance.

Title: How to optimize the query? t-sql

Body:
This query works about 3 minutes and returns 7279 rows:

SELECT identity(int,1,1) as id, c.client_code, a.account_num,
        c.client_short_name, u.uso, us.fio, null as new, null as txt
INTO #ttable
FROM accounts a INNER JOIN Clients c ON
    c.id = a.client_id INNER JOIN Uso u ON c.uso_id = u.uso_id INNER JOIN
    Magazin m ON a.account_id = m.account_id LEFT JOIN Users us ON
    m.user_id = us.user_id
WHERE m.status_id IN ('1','5','9') AND m.account_new_num is null
    AND u.branch_id = @branch_id
ORDER BY c.client_code;

The type of 'client_code' field is VARCHAR(6).

Is it possible to somehow optimize this query?

Body:
Always try to list tables earlier if they are referenced in the where clause - it cuts off row combinations as early as possible. In this case, the Magazin table has some predicates in the where clause, but is listed way down in the tables list. This means that all the other joins have to be made before the Magazin rows can be filtered - possibly millions of extra rows.

Try this (and let us know how it went):

SELECT ...
INTO #ttable
FROM accounts a
INNER JOIN Magazin m ON a.account_id = m.account_id
INNER JOIN Clients c ON c.id = a.client_id
INNER JOIN Uso u ON c.uso_id = u.uso_id
LEFT JOIN Users us ON m.user_id = us.user_id
WHERE m.status_id IN ('1','5','9')
AND m.account_new_num is null
AND u.branch_id = @branch_id
ORDER BY c.client_code;

This kind of optimization can greatly improve query performance.


问:
Configuring hosting and deep linking using the same domain - Branch.io
I have purchased a site on bigrock and also hosted on a server. However I now plan to deeplink my domain using branch.io . Is it possible to host my website and deeplink only the url path using the same root domain. </p>

答: It is not possible if you are trying to set a custom root domain for your Branch App which you are already using. We always need full control of the domain if you want to use it as a link domain. So to make it work it'll have to be exclusive to Branch Name servers only. Any custom domain that you use as your app link domain can not be used for other purposes.</p>


问:
Read a Text file having row in parenthesis and values separated by comma using pandas
I want to read a text file which contains data in parenthesis as row and values in it as column.The format of txt file is below :

```<code>(a, b, c, d) (a1, b1, (c1,c12,c13), d1) (a2, b2, (c2,c22,c23), d2) (a3, b3, (c3,c32,c33), d3) (a4, b4, (c4,c42,c43), d4)
</code>```

I want the data in this format :

```<code>a  b  c  d
a1 b1 c1 d1
a2 b2 c2 d2
a3 b3 c3 d3
a4 b4 c4 d4
</code>```

When i am reading text file as csv file it reads all the data in one row only. It shows 1 row and all the columns.
Please help me with this problem.

答:
Could you please try following. Where Input_file is your actual file.

```<code>#!/usr/bin/python3
import re

f = open("Input_file", "r")
text=f.read()
f.close()
text2=''
if ' ' in text:
    text2 = re.sub('^\\(|,|\\)\n$|\\)$','',text)
    text2 = text2.replace(') (' , '\n')
    text2 = text2.replace('\\)','\n')
    text2 = re.sub('\\)|\\(','',text2)
    print(text2)
</code>```

Proof of concept: Let's say following is Input_file:

```<code>cat Input_file
(a, b, c, d) (a1, b1, c1, d1) (a2, b2, c2, d2) (a3, b3, c3, d3) (a4, b4, c4, d4)
(a, b, c, d) (a1, b1, c1, d1) (a2, b2, c2, d2) (a3, b3, c3, d3) (a4, b4, c4, d4)
</code>```

When we run script output will be as follows.

```<code>./script.py
a b c d
a1 b1 c1 d1
a2 b2 c2 d2
a3 b3 c3 d3
a4 b4 c4 d4
a b c d
a1 b1 c1 d1
a2 b2 c2 d2
a3 b3 c3 d3
a4 b4 c4 d4
</code>```

Detailed explanation: Reading Input_file complete content and saving it to variable text.  Now closing that file by close  command. Now with a for loop going  through all contents of Input_file which are stored in variable text. Then substituting starting ), ending )\n and , with NULL in all lines. Now replacing starting ( with NULL and then replacing ) ( with new line for all occurrences to make output look like OP's request.

Body:
I want to read a text file which contains data in parenthesis as row and values in it as column.The format of txt file is below :

(a, b, c, d) (a1, b1, (c1,c12,c13), d1) (a2, b2, (c2,c22,c23), d2) (a3, b3, (c3,c32,c33), d3) (a4, b4, (c4,c42,c43), d4)

I want the data in this format :

a  b  c  d
a1 b1 c1 d1
a2 b2 c2 d2
a3 b3 c3 d3
a4 b4 c4 d4

When i am reading text file as csv file it reads all the data in one row only. It shows 1 row and all the columns.
Please help me with this problem.

Body:
Could you please try following. Where Input_file is your actual file.

#!/usr/bin/python3
import re

f = open("Input_file", "r")
text=f.read()
f.close()
text2=''
if ' ' in text:
    text2 = re.sub('^\\(|,|\\)\n$|\\)$','',text)
    text2 = text2.replace(') (' , '\n')
    text2 = text2.replace('\\)','\n')
    text2 = re.sub('\\)|\\(','',text2)
    print(text2)

Proof of concept: Let's say following is Input_file:

cat Input_file
(a, b, c, d) (a1, b1, c1, d1) (a2, b2, c2, d2) (a3, b3, c3, d3) (a4, b4, c4, d4)
(a, b, c, d) (a1, b1, c1, d1) (a2, b2, c2, d2) (a3, b3, c3, d3) (a4, b4, c4, d4)

When we run script output will be as follows.

./script.py
a b c d
a1 b1 c1 d1
a2 b2 c2 d2
a3 b3 c3 d3
a4 b4 c4 d4
a b c d
a1 b1 c1 d1
a2 b2 c2 d2
a3 b3 c3 d3
a4 b4 c4 d4

Detailed explanation: Reading Input_file complete content and saving it to variable text.  Now closing that file by close  command. Now with a for loop going  through all contents of Input_file which are stored in variable text. Then substituting starting ), ending )\n and , with NULL in all lines. Now replacing starting ( with NULL and then replacing ) ( with new line for all occurrences to make output look like OP's request.


问:
How to send foreignkey data type in API by DRF
I have API written by Django Rest Framework:

models.py

```<code>STATUSES = (("F", "free"),
            ("R", "registered"),
            ("N", "not free")
            )
class Collector(models.Model):
    ip = models.TextField()

    class Meta:
        verbose_name = 'Коллектор'
        verbose_name_plural = 'Коллекторы'
        ordering = ('ip',)

    def __str__(self):
        return str(self.ip)

class Port(models.Model):
    port = models.CharField(max_length=1000)
    status = models.CharField(max_length=1, choices=STATUSES, default="F")
    collector_link = models.ForeignKey(Collector, on_delete=models.CASCADE)

    class Meta:
        verbose_name = 'Порт'
        verbose_name_plural = 'Порты'
        ordering = ('port',)

    def __str__(self):
        return str(self.port)
</code>```

serializers.py

```<code>class CollectorSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Collector
        fields = ("__all__")

class PortSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Port
        fields = ("__all__")
</code>```

views.py

```<code>class CollectorAPIView(APIView):
    serializer_class = serializers.CollectorSerializer
    allowed_methods = ["POST"]

    def post(self, request, *args, **kwargs):
        ip = request.POST['ip']
        obj = models.Collector(ip=request.POST['ip'])
        obj.save()
        return Response(status=status.HTTP_200_OK)

class PortAPIView(APIView):
    serializer_class = serializers.PortSerializer
    allowed_methods = ["POST"]

    def post(self, request, *args, **kwargs):
        port, status1, collector_link = request.POST["port"], request.POST["status"], request.POST["collector_link"]
        entry = models.Port(port=port, status=status1, collector_link=collector_link)
</code>```

entry.save()
            return Response(status=status.HTTP_201_CREATED)
In result, when I'm trying to send data to API in json format like {"port": "1", "status": "F", "collector_link": "10"} (collector_link=10 is id of collector instance in data base) I'm getting the next error:

ValueError at /sm/api/v1/port/ Cannot assign "'10'":
  "Port.collector_link" must be a "Collector" instance
  How I can to fix it?
  .

答:
Please change your view to

```<code>class PortAPIView(APIView):
    serializer_class = serializers.PortSerializer
    allowed_methods = ["POST"]

    def post(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return response.Ok(serializer.data)
</code>```

And you serializer to

```<code>class PortSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Port
        fields = ("__all__")

    def create(self, validated_data):
        collector = Collector.objects.create(ip=validated_data["collector"])
        port = Port.objects.create(port=validated_data["port"], status=validated_data["status"], collector_link=collector)
        return port
</code>```

This should resolve your query. I hope it helps! :)

Body:
I have API written by Django Rest Framework:

models.py

STATUSES = (("F", "free"),
            ("R", "registered"),
            ("N", "not free")
            )
class Collector(models.Model):
    ip = models.TextField()

    class Meta:
        verbose_name = 'Коллектор'
        verbose_name_plural = 'Коллекторы'
        ordering = ('ip',)

    def __str__(self):
        return str(self.ip)

class Port(models.Model):
    port = models.CharField(max_length=1000)
    status = models.CharField(max_length=1, choices=STATUSES, default="F")
    collector_link = models.ForeignKey(Collector, on_delete=models.CASCADE)

    class Meta:
        verbose_name = 'Порт'
        verbose_name_plural = 'Порты'
        ordering = ('port',)

    def __str__(self):
        return str(self.port)

serializers.py

class CollectorSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Collector
        fields = ("__all__")

class PortSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Port
        fields = ("__all__")

views.py

class CollectorAPIView(APIView):
    serializer_class = serializers.CollectorSerializer
    allowed_methods = ["POST"]

    def post(self, request, *args, **kwargs):
        ip = request.POST['ip']
        obj = models.Collector(ip=request.POST['ip'])
        obj.save()
        return Response(status=status.HTTP_200_OK)

class PortAPIView(APIView):
    serializer_class = serializers.PortSerializer
    allowed_methods = ["POST"]

    def post(self, request, *args, **kwargs):
        port, status1, collector_link = request.POST["port"], request.POST["status"], request.POST["collector_link"]
        entry = models.Port(port=port, status=status1, collector_link=collector_link)

entry.save()
            return Response(status=status.HTTP_201_CREATED)
In result, when I'm trying to send data to API in json format like {"port": "1", "status": "F", "collector_link": "10"} (collector_link=10 is id of collector instance in data base) I'm getting the next error:

ValueError at /sm/api/v1/port/ Cannot assign "'10'":
  "Port.collector_link" must be a "Collector" instance
  How I can to fix it?
  .

Body:
Please change your view to

class PortAPIView(APIView):
    serializer_class = serializers.PortSerializer
    allowed_methods = ["POST"]

    def post(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return response.Ok(serializer.data)

And you serializer to

class PortSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Port
        fields = ("__all__")

    def create(self, validated_data):
        collector = Collector.objects.create(ip=validated_data["collector"])
        port = Port.objects.create(port=validated_data["port"], status=validated_data["status"], collector_link=collector)
        return port

This should resolve your query. I hope it helps! :)


问:
I'm trying to merge two sorted arrays but getting java.lang.ArrayIndexOutOfBoundsException: 5 can't figure out why
It is going out bounds on the first if statement even after I'm checking the values of j and k before incrementing

```<code>`int[] Merge(int[]a,int[]b){
int n=a.length+b.length;
int[] sol= new int[n];
int i=0,j=0,k=0;

while(i<n){

if(a[j]<b[k]){

    sol[i]=a[j];

    if(j<a.length)j++;
}
else if(b[k]<a[j]){

        sol[i]=b[k];

        if(k<b.length)k++;

}

i++;

}

return sol;

}`
</code>```

I can't understand where the array is going out of bounds

答:
Consider the case when either j=a.length-1 or k=b.length-1 then your if condition [if(j<a.length)j++; or if(k<b.length)k++;]passes  and makes j==a.length to true  or k==b.length to true and the while loop access the array again when it throws ArrayIndexOutofBound exception . Also your code doesn't handle common elements in the array . Please check the correct code below

```<code>int[] Merge(int[]a,int[]b){
int n=a.length+b.length;
        int[] sol= new int[n];
        int i=0,j=0,k=0;

        while(i<n){
            if(a[j]<=b[k]){ // equality check handles common case

                sol[i++]=a[j++];
                if(j==a.length)
                   break;

            }
            else if(b[k]<a[j]){
                sol[i++]=b[k++];
                if(k==b.length)
                    break ;

            }

        }
        if(j==a.length) {
            while (k != b.length)
                sol[i++]=b[k++];
        }
        if(k==b.length) {
            while (j != a.length)
                sol[i++]=a[j++];
        }
        return sol;
}
</code>```

Body:
It is going out bounds on the first if statement even after I'm checking the values of j and k before incrementing

`int[] Merge(int[]a,int[]b){
int n=a.length+b.length;
int[] sol= new int[n];
int i=0,j=0,k=0;

while(i<n){

if(a[j]<b[k]){

    sol[i]=a[j];

    if(j<a.length)j++;
}
else if(b[k]<a[j]){

        sol[i]=b[k];

        if(k<b.length)k++;

}

i++;

}

return sol;

}`

I can't understand where the array is going out of bounds

Body:
Consider the case when either j=a.length-1 or k=b.length-1 then your if condition [if(j<a.length)j++; or if(k<b.length)k++;]passes  and makes j==a.length to true  or k==b.length to true and the while loop access the array again when it throws ArrayIndexOutofBound exception . Also your code doesn't handle common elements in the array . Please check the correct code below

int[] Merge(int[]a,int[]b){
int n=a.length+b.length;
        int[] sol= new int[n];
        int i=0,j=0,k=0;

        while(i<n){
            if(a[j]<=b[k]){ // equality check handles common case

                sol[i++]=a[j++];
                if(j==a.length)
                   break;

            }
            else if(b[k]<a[j]){
                sol[i++]=b[k++];
                if(k==b.length)
                    break ;

            }

        }
        if(j==a.length) {
            while (k != b.length)
                sol[i++]=b[k++];
        }
        if(k==b.length) {
            while (j != a.length)
                sol[i++]=a[j++];
        }
        return sol;
}


问:
Python Web Framework for Small Team
I have 4 days off and I will use this time to rewrite our RoR (Ruby on Rails) Application in a python web framework just for fun ;-] (and why not make the switch, RoR is great but keep changing all the time, can be exhausting.)

I don't know the python web framework very well, I've glad web.py, django, cherry.py, pylons/pyramid and few others. Our requirements are (put everything can be irrelevant) :

MVC (Strict or not)

Small Team (2-3 people included one designer)

Fun to use

REST support

Multilevel caching (DB query, page cache)

Nginx Support (X-Accel-Redirect File Download)

Heavy traffic (1,200,000 ~ views)

Urls rewrting (Multi-domains support not only subdomain)

Not a problem if it's not hype

Not a problem if there is no plugins

Either SQL or NOSQL (can be fun to try NOSQL)

So what you would advise ?

答:
In addition to the other frameworks mentioned (which are all good options), you should check out web2py. It's a feature-packed, full-stack framework that's very easy to set up, learn, and use. It was originally inspired by Ruby on Rails, so if you're rewriting an RoR application, you may find it more comfortable than some of the other Python frameworks. Here are some details regarding your requirements:

MVC:

http://web2py.com/book/default/chapter/01#Model-View-Controller

REST support:

http://vimeo.com/21133657

https://groups.google.com/d/msg/web2py/gcqEcXIo7RI/I8BhkK0G_XUJ

Multilevel caching (DB query, page cache):

http://web2py.com/book/default/chapter/04#cache

http://web2py.com/book/default/chapter/06#Caching-Selects

Nginx Support:

http://code.google.com/p/web2py/source/browse/scripts/setup-web2py-nginx-uwsgi-ubuntu.sh

Urls rewriting:

http://web2py.com/book/default/chapter/04#URL-Rewrite

Not a problem if there is no plugins

Plugin documentation

plugin_wiki

Plugins

More plugins

Appliances

Slices

Either SQL or NOSQL (can be fun to try NOSQL)

SQL support: SQLite, PostgreSQL, MySQL, Oracle, MSSQL, FireBird, DB2, Informix, Ingres, Cubrid.

NoSQL support: Google App Engine, CouchDB (partial), MongoDB (partial)

Easy to add additional database adapters (SQL or NoSQL).

The framework is under very active development (new releases every 2-4 weeks), yet is committed to maintaining backward compatibility, so existing apps won't break upon upgrade. If you have any questions, you'll get lots of help from the friendly and responsive mailing list.

Title: Python Web Framework for Small Team