问:
MySQL join is scanning the table rather than using the index
I have following tables:

SHOW CREATE TABLE access_token_status;

```<code>CREATE TABLE `access_token_status` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `status` varchar(10) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `status` (`status`),
  KEY `idx_access_token_status_status_lookup_2` (`status`,`id`),
  KEY `idx_access_token_status_status_lookup_1` (`id`,`status`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
</code>```

and

SHOW CREATE TABLE user;

```<code>CREATE TABLE `user` (
  `id` varchar(17) NOT NULL,
  `short_lived_access_token_status_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_user_status_lookup_1` (`id`,`short_lived_access_token_status_id`),
  KEY `idx_user_status_lookup_2` (`short_lived_access_token_status_id`,`id`),
  KEY `ix_user_short_lived_access_token_status_id` (`short_lived_access_token_status_id`),
  CONSTRAINT `user_ibfk_1` FOREIGN KEY (`short_lived_access_token_status_id`) REFERENCES `access_token_status` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
</code>```

and

SHOW CREATE TABLE account;

```<code>CREATE TABLE `account` (
  `id` varchar(17) NOT NULL,
  `user_id` varchar(17) NOT NULL,
  `track` tinyint(1) NOT NULL,
  `estimated_time_to_regain_access` int(11) NOT NULL,
  `media_list_fetched_at` datetime NOT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_account_next_fetch_lookup` (`user_id`,`estimated_time_to_regain_access`,`track`,`media_list_fetched_at`),
  CONSTRAINT `account_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`),
  CONSTRAINT `account_chk_1` CHECK ((`track` in (0,1)))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
</code>```

When I try to explain the following query

```<code>explain select a.*
        from account a
        inner join user u
        on a.user_id = u.id
        inner  join access_token_status as s
        on u.short_lived_access_token_status_id = s.id and s.status = 'valid'
        where
            u.short_lived_access_token_status_id = 3
            and a.estimated_time_to_regain_access = 0
            and a.track = true
            and a.media_list_fetched_at > '2020-05-30 12:31:01'
        limit 1
        for update of a SKIP LOCKED
</code>```

I get this output:

```<code># id, select_type, table, partitions, type, possible_keys, key, key_len, ref, rows, filtered, Extra
'1', 'SIMPLE', 's', NULL, 'const', 'PRIMARY,status,idx_access_token_status_status_lookup_2,idx_access_token_status_status_lookup_1', 'PRIMARY', '4', 'const', '1', '100.00', NULL
'1', 'SIMPLE', 'a', NULL, 'index', 'idx_account_next_fetch_lookup', 'idx_account_next_fetch_lookup', '80', NULL, '2', '50.00', 'Using where; Using index'
'1', 'SIMPLE', 'u', NULL, 'eq_ref', 'PRIMARY,idx_user_status_lookup_1,idx_user_status_lookup_2,ix_user_short_lived_access_token_status_id', 'PRIMARY', '70', 'media_meta.a.user_id', '1', '100.00', 'Using where'
</code>```

It seems like indexes are not being used for some tables. This is an issue for my case as the scanned rows get locked and other queries will skip them (due to SKIP LOCKED which is required to make sure queries are not blocked on each other)

I'm not sure what index I'm missing or if I need to change something in the query

答:
Seems to be "over-normalization".

I don't see any advantage in moving status out of the table into another table.  Doing so complicates optimization and does not save much, if any, space.

In fact, you could use an ENUM('invalid', 'valid', ...) NOT NULL, which would take 1 byte instead of 4 bytes for INT.

In InnoDB, when you have PRIMARY KEY(id), you have a BTree ordered by id.  Hence, any secondary index starting with id is redundant.  Note also that a PRIMARY KEY is, by definition, UNIQUE.

```<code>    where
        u.short_lived_access_token_status_id = 3
        and a.estimated_time_to_regain_access = 0
        and a.track = true
        and a.media_list_fetched_at > '2020-05-30 12:31:01'
</code>```

would like to use either

```<code>INDEX(short_lived_access_token_status_id, ...)
</code>```

but that seems very unlikely due to low cardinality, or

```<code>INDEX(estimated_time_to_regain_access, track,   -- in either order
      media_list_fetched_at)    -- last, since it is a 'range'
</code>```

Even better would be this "covering" index:

```<code>INDEX(estimated_time_to_regain_access, track,
      media_list_fetched_at, user_id)
</code>```

Body:
I have following tables:

SHOW CREATE TABLE access_token_status;

CREATE TABLE `access_token_status` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `status` varchar(10) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `status` (`status`),
  KEY `idx_access_token_status_status_lookup_2` (`status`,`id`),
  KEY `idx_access_token_status_status_lookup_1` (`id`,`status`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

and

SHOW CREATE TABLE user;

CREATE TABLE `user` (
  `id` varchar(17) NOT NULL,
  `short_lived_access_token_status_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_user_status_lookup_1` (`id`,`short_lived_access_token_status_id`),
  KEY `idx_user_status_lookup_2` (`short_lived_access_token_status_id`,`id`),
  KEY `ix_user_short_lived_access_token_status_id` (`short_lived_access_token_status_id`),
  CONSTRAINT `user_ibfk_1` FOREIGN KEY (`short_lived_access_token_status_id`) REFERENCES `access_token_status` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

and

SHOW CREATE TABLE account;

CREATE TABLE `account` (
  `id` varchar(17) NOT NULL,
  `user_id` varchar(17) NOT NULL,
  `track` tinyint(1) NOT NULL,
  `estimated_time_to_regain_access` int(11) NOT NULL,
  `media_list_fetched_at` datetime NOT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_account_next_fetch_lookup` (`user_id`,`estimated_time_to_regain_access`,`track`,`media_list_fetched_at`),
  CONSTRAINT `account_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`),
  CONSTRAINT `account_chk_1` CHECK ((`track` in (0,1)))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

When I try to explain the following query

explain select a.*
        from account a
        inner join user u
        on a.user_id = u.id
        inner  join access_token_status as s
        on u.short_lived_access_token_status_id = s.id and s.status = 'valid'
        where
            u.short_lived_access_token_status_id = 3
            and a.estimated_time_to_regain_access = 0
            and a.track = true
            and a.media_list_fetched_at > '2020-05-30 12:31:01'
        limit 1
        for update of a SKIP LOCKED

I get this output:

# id, select_type, table, partitions, type, possible_keys, key, key_len, ref, rows, filtered, Extra
'1', 'SIMPLE', 's', NULL, 'const', 'PRIMARY,status,idx_access_token_status_status_lookup_2,idx_access_token_status_status_lookup_1', 'PRIMARY', '4', 'const', '1', '100.00', NULL
'1', 'SIMPLE', 'a', NULL, 'index', 'idx_account_next_fetch_lookup', 'idx_account_next_fetch_lookup', '80', NULL, '2', '50.00', 'Using where; Using index'
'1', 'SIMPLE', 'u', NULL, 'eq_ref', 'PRIMARY,idx_user_status_lookup_1,idx_user_status_lookup_2,ix_user_short_lived_access_token_status_id', 'PRIMARY', '70', 'media_meta.a.user_id', '1', '100.00', 'Using where'

It seems like indexes are not being used for some tables. This is an issue for my case as the scanned rows get locked and other queries will skip them (due to SKIP LOCKED which is required to make sure queries are not blocked on each other)

I'm not sure what index I'm missing or if I need to change something in the query

Body:
Seems to be "over-normalization".

I don't see any advantage in moving status out of the table into another table.  Doing so complicates optimization and does not save much, if any, space.

In fact, you could use an ENUM('invalid', 'valid', ...) NOT NULL, which would take 1 byte instead of 4 bytes for INT.

In InnoDB, when you have PRIMARY KEY(id), you have a BTree ordered by id.  Hence, any secondary index starting with id is redundant.  Note also that a PRIMARY KEY is, by definition, UNIQUE.

    where
        u.short_lived_access_token_status_id = 3
        and a.estimated_time_to_regain_access = 0
        and a.track = true
        and a.media_list_fetched_at > '2020-05-30 12:31:01'

would like to use either

INDEX(short_lived_access_token_status_id, ...)

but that seems very unlikely due to low cardinality, or

INDEX(estimated_time_to_regain_access, track,   -- in either order
      media_list_fetched_at)    -- last, since it is a 'range'

Even better would be this "covering" index:

INDEX(estimated_time_to_regain_access, track,
      media_list_fetched_at, user_id)


来源: stackexchange

问:
Is it recommended to mock concrete class?
Most of the examples given in mocking framework website is to mock Interface. Let say NSubstitute that I'm currently using, all their mocking examples is to mock interface.

But in reality, I saw some developer mock concrete class instead. Is it recommended to mock concrete class?

答:
Supposed we have:

```<code>class Foo {
    fun bar() = if (someCondition) {
        “Yes”
    } else {
        “No”
    }
}
</code>```

There’s nothing preventing us to do the following mocking in the test code:

```<code>val foo = mock<Foo>()
whenever(foo.bar()).thenReturn(“Maybe”)
</code>```

The problem is it is setting up incorrect behavior of class Foo. The real instance of class Foo will never be able to return “Maybe”.

ContentLicense: CC BY-SA 3.0

Body:
Supposed we have:

class Foo {
    fun bar() = if (someCondition) {
        “Yes”
    } else {
        “No”
    }
}

There’s nothing preventing us to do the following mocking in the test code:

val foo = mock<Foo>()
whenever(foo.bar()).thenReturn(“Maybe”)

The problem is it is setting up incorrect behavior of class Foo. The real instance of class Foo will never be able to return “Maybe”.

ContentLicense: CC BY-SA 4.0


问:
Importing an image dynamically in React Component
I'm trying to dynamically load an image in some React Component. I'm using require instead of simply passing the paths in order to improve performance.

I gathered I could use an inline require and it does work. However, when I try to pass as a prop instead, I get errors.

What am I doing wrong?

EDIT: Turns out both work, I was doing something else wrong which was throwing the error. (Bonus question still applies tho)

```<code>import React from 'react';

// This works
export SomeComponent = () => (
  <div>
    <img src={require(`../images/my-logo.svg`)} />
  </div>
)

// This works too!
export SomeComponent = ({image}) => (
  <div>
    <img src={require(`../images/${image}`)} />
  </div>
)

<SomeComponent image="my-logo.svg" />
</code>```

Bonus question: Can this be done with ES6 import vs CommonJs require?

答:
For React 16+, @Tomasz Mularczyk's answer needs a small tweak:

```<code>loadImage(name) {
    import(`../images/${name}`)
      .then(image => {
        console.log(image); // This will show an object with a `default` property as the image you imported
        this.setState({ src: image.default })
      })
 }
</code>```

Full Source:

```<code>class Image extends React.Component {
  constructor(props) {
    super(props)
    this.state = { src: null }
  }

  componentDidMount() {
    this.loadImage(this.props.name)
  }

  componentDidUpdate(prevProps) {
    if (prevProps.name !== this.props.name) {
      this.loadImage(this.props.name)
    }
  }

  loadImage(name) {
    import(`../images/${name}`)
      .then(image => {
        console.log(image); // This will show an object with a `default` property as the image you imported
        this.setState({ src: image.default })
      })
  }

  render() {
    return <img src={this.state.src} />
  }
}
</code>```

Body:
I'm trying to dynamically load an image in some React Component. I'm using require instead of simply passing the paths in order to improve performance.

I gathered I could use an inline require and it does work. However, when I try to pass as a prop instead, I get errors.

What am I doing wrong?

EDIT: Turns out both work, I was doing something else wrong which was throwing the error. (Bonus question still applies tho)

import React from 'react';

// This works
export SomeComponent = () => (

    <img src={require(`../images/my-logo.svg`)} />

)

// This works too!
export SomeComponent = ({image}) => (

    <img src={require(`../images/${image}`)} />

)

<SomeComponent image="my-logo.svg" />

Bonus question: Can this be done with ES6 import vs CommonJs require?

Body:
For React 16+, @Tomasz Mularczyk's answer needs a small tweak:

loadImage(name) {
    import(`../images/${name}`)
      .then(image => {
        console.log(image); // This will show an object with a `default` property as the image you imported
        this.setState({ src: image.default })
      })
 }

Full Source:

class Image extends React.Component {
  constructor(props) {
    super(props)
    this.state = { src: null }
  }

  componentDidMount() {
    this.loadImage(this.props.name)
  }

  componentDidUpdate(prevProps) {
    if (prevProps.name !== this.props.name) {
      this.loadImage(this.props.name)
    }
  }

  loadImage(name) {
    import(`../images/${name}`)
      .then(image => {
        console.log(image); // This will show an object with a `default` property as the image you imported
        this.setState({ src: image.default })
      })
  }

  render() {
    return <img src={this.state.src} />
  }
}


问:
Need help in removing junk char in VBA Excel
I am trying to read the XMl file in text format.

```<code>Set FS = CreateObject("Scripting.FileSystemObject")
Set TS = FS.OpenTextFile(FilePath, 1, False)
Do While (TS.AtEndOfStream = False)
    s = TS.ReadLine
Loop
</code>```

But while reading i al also getting some junk Char showing Square box even i tried using the Trim but unable to remove this. Please let me know how to remove this char.

答:
I know you said you are reading the file in as text, but since it is in XML format, would it be possible to save the file with a .xml extension? This way you could bypass your problem completely, and just read it in using DOM the object created by CreateObject("MSXML2.DOMDocument.6.0")...

Another solution (piggybacking off of @Neil's suggestion) may to be check if the character being read in is equal to vbNewline, vbCrLf, or vbLf characters. If they are equal, then simply dont include them in your data pull.

Body:
I am trying to read the XMl file in text format.

Set FS = CreateObject("Scripting.FileSystemObject")
Set TS = FS.OpenTextFile(FilePath, 1, False)
Do While (TS.AtEndOfStream = False)
    s = TS.ReadLine
Loop

But while reading i al also getting some junk Char showing Square box even i tried using the Trim but unable to remove this. Please let me know how to remove this char.


问:
How to optimally divide an array into two subarrays so that sum of elements in both are same, otherwise give an error?
How to optimally divide an array into two subarrays so that sum of elements in both subarrays is same, otherwise give an error?

Example 1

Given the array

```<code>10,  20 , 30 , 5 , 40 , 50 , 40 , 15
</code>```

It can be divided as

```<code>10, 20, 30, 5, 40
</code>```

and

```<code>50, 40, 15
</code>```

Each subarray sums up to 105.

Example 2

```<code>10,  20,  30,  5,  40,  50,  40,  10
</code>```

The array cannot be divided into 2 arrays of an equal sum.

答:
```<code>a=[int(g) for g in input().split()]     #for taking the array as input in a
                                     single line
leftsum=0
n=len(a)
for i in range(n):
    leftsum+=a[i]                       #calculates the sum of first subarray
    rightsum=0
    for j in range(i+1):
        rightsum+=a[j]                  #calculates the sum of other subarray
    if leftsum==rightsum:
        pos=i+1                         #if the sum of subarrays are equal,
        break                           set position where the condition
                                        gets satisfied and exit the loop
    else:
        pos=-1                          #if the sum of subarrays is not
                                        equal, set position to -1
if pos=-1 or pos=n:
    print('It is not possible.')
else:                                   #printing the sub arrays`
    for k in range(n):
        if pos=k:
            print('')
        print(str(a[k]),end='')
</code>```

Body:
How to optimally divide an array into two subarrays so that sum of elements in both subarrays is same, otherwise give an error?

Example 1

Given the array

10,  20 , 30 , 5 , 40 , 50 , 40 , 15

It can be divided as

10, 20, 30, 5, 40

and

50, 40, 15

Each subarray sums up to 105.

Example 2

10,  20,  30,  5,  40,  50,  40,  10

The array cannot be divided into 2 arrays of an equal sum.


问:
How to add user in centos without manual password prompt
I am trying to automate setting up a user on centos as part of my script. Here is what i am doing:-

```<code>/usr/sbin/useradd my-user
/usr/bin/passwd my-user
</code>```

You will be be prompted to enter password for the my-user user

Now is there a way to skip the second command so that there is no manual prompt, so that i can have something in the script to auto create my-user on centos without needing to manually enter the password

答:
You can change password programmatically, using:</p>

```<code>echo -e "xyz\nxyz\n" | /usr/bin/passwd my-user
</code>```

Body:
I am trying to automate setting up a user on centos as part of my script. Here is what i am doing:-

/usr/sbin/useradd my-user
/usr/bin/passwd my-user

You will be be prompted to enter password for the my-user user

Now is there a way to skip the second command so that there is no manual prompt, so that i can have something in the script to auto create my-user on centos without needing to manually enter the password

Body:
You can change password programmatically, using:

echo -e "xyz\nxyz\n" | /usr/bin/passwd my-user


问:
Unit test the new Kotlin coroutine StateFlow
Recently, the class StateFlow was introduced as part of Kotlin coroutines.

I'm currently trying it and encountered an issue while trying to unit test my ViewModel. What I want to achieve: testing that my StateFlow is receiving all the state values in the correct order in my ViewModel.

My code is as follows.

ViewModel:

class WalletViewModel(private val getUserWallets: GetUersWallets) : ViewModel() {

val userWallet: StateFlow<State<UserWallets>> get() = _userWallets
private val _userWallets: MutableStateFlow<State<UserWallets>> =
        MutableStateFlow(State.Init)

fun getUserWallets() {
    viewModelScope.launch {
        getUserWallets.getUserWallets()
            .onStart { _userWallets.value = State.Loading }
            .collect { _userWallets.value = it }
    }
}
```
<p>My test:</p>
<pre class="lang-kotlin prettyprint-override"><code>@Test
fun `observe user wallets ok`() = runBlockingTest {
    Mockito.`when`(api.getAssetWallets()).thenReturn(TestUtils.getAssetsWalletResponseOk())
    Mockito.`when`(api.getFiatWallets()).thenReturn(TestUtils.getFiatWalletResponseOk())

    viewModel.getUserWallets()

    val res = arrayListOf<State<UserWallets>>()
    viewModel.userWallet.toList(res) //doesn't works

    Assertions.assertThat(viewModel.userWallet.value is State.Success).isTrue() //works, last value enmited
}
</code>```

Accessing the last value emitted works. But what I want to test is that all the emitted values are emitted in the correct order.

With this piece of code: viewModel.userWallet.toList(res) I'm getting the following error:

```<code>java.lang.IllegalStateException: This job has not completed yet
    at kotlinx.coroutines.JobSupport.getCompletionExceptionOrNull(JobSupport.kt:1189)
    at kotlinx.coroutines.test.TestBuildersKt.runBlockingTest(TestBuilders.kt:53)
    at kotlinx.coroutines.test.TestBuildersKt.runBlockingTest$default(TestBuilders.kt:45)
    at WalletViewModelTest.observe user wallets ok(WalletViewModelTest.kt:52)
....
</code>```

I guess I'm missing something obvious. But not sure why as I'm just getting started with coroutines and Flow and this error seems to happen when not using runBlockingTest, which I use already.

EDIT:

As a temporary solution, I'm testing it as a live data:

@Captor
lateinit var captor: ArgumentCaptor<State<UserWallets>>

@Mock
lateinit var walletsObserver: Observer<State<UserWallets>>

@Test
fun `observe user wallets ok`() = runBlockingTest {
    viewModel.userWallet.asLiveData().observeForever(walletsObserver)

    viewModel.getUserWallets()
    captor.run {
        Mockito.verify(walletsObserver, Mockito.times(3)).onChanged(capture())
        Assertions.assertThat(allValues[0] is State.Init).isTrue()
        Assertions.assertThat(allValues[1] is State.Loading).isTrue()
        Assertions.assertThat(allValues[2] is State.Success).isTrue()
    }
}
```

答:
runBlockingTest just skips the delays in your case but not override the dispatcher used in the ViewModel with your test dispatcher. You need to inject TestCoroutineDispatcher to your ViewModel or since you are using viewModelScope.launch {} which already uses Dispatchers.Main by default, you need to override the main dispatcher via Dispatchers.setMain(testCoroutineDispatcher). You can create and add the following rule to your test file.

```<code>class MainCoroutineRule(
        val testDispatcher: TestCoroutineDispatcher = TestCoroutineDispatcher()
) : TestWatcher() {

    override fun starting(description: Description?) {
        super.starting(description)
        Dispatchers.setMain(testDispatcher)
    }

    override fun finished(description: Description?) {
        super.finished(description)
        Dispatchers.resetMain()
        testDispatcher.cleanupTestCoroutines()
    }
}
</code>```

And in your test file

```<code>@get:Rule
var mainCoroutineRule = MainCoroutineRule()

@Test
fun `observe user wallets ok`() = mainCoroutineRule.testDispatcher.runBlockingTest {
}
</code>```

Btw it is always a good practice to inject dispatchers. For instance if you would have been using a dispatcher other than Dispatchers.Main in your coroutine scope like viewModelScope.launch(Dispatchers.Default), then your test will fail again even if you are using a test dispatcher. The reason is you can only override main dispatcher with Dispatchers.setMain() as it can be understood from its name but not Dispatchers.IO or Dispatchers.Default. In that case you need to inject mainCoroutineRule.testDispatcher to your view model and use the injected dispatcher rather than hardcoding it.

Body:
Recently, the class StateFlow was introduced as part of Kotlin coroutines.

I'm currently trying it and encountered an issue while trying to unit test my ViewModel. What I want to achieve: testing that my StateFlow is receiving all the state values in the correct order in my ViewModel.

My code is as follows.

ViewModel:

class WalletViewModel(private val getUserWallets: GetUersWallets) : ViewModel() {

val userWallet: StateFlow<State<UserWallets>> get() = _userWallets
private val _userWallets: MutableStateFlow<State<UserWallets>> =
        MutableStateFlow(State.Init)

fun getUserWallets() {
    viewModelScope.launch {
        getUserWallets.getUserWallets()
            .onStart { _userWallets.value = State.Loading }
            .collect { _userWallets.value = it }
    }
}

My test:

@Test
fun `observe user wallets ok`() = runBlockingTest {
    Mockito.`when`(api.getAssetWallets()).thenReturn(TestUtils.getAssetsWalletResponseOk())
    Mockito.`when`(api.getFiatWallets()).thenReturn(TestUtils.getFiatWalletResponseOk())

    viewModel.getUserWallets()

    val res = arrayListOf<State<UserWallets>>()
    viewModel.userWallet.toList(res) //doesn't works

    Assertions.assertThat(viewModel.userWallet.value is State.Success).isTrue() //works, last value enmited
}

Accessing the last value emitted works. But what I want to test is that all the emitted values are emitted in the correct order.

With this piece of code: viewModel.userWallet.toList(res) I'm getting the following error:

java.lang.IllegalStateException: This job has not completed yet
    at kotlinx.coroutines.JobSupport.getCompletionExceptionOrNull(JobSupport.kt:1189)
    at kotlinx.coroutines.test.TestBuildersKt.runBlockingTest(TestBuilders.kt:53)
    at kotlinx.coroutines.test.TestBuildersKt.runBlockingTest$default(TestBuilders.kt:45)
    at WalletViewModelTest.observe user wallets ok(WalletViewModelTest.kt:52)
....

I guess I'm missing something obvious. But not sure why as I'm just getting started with coroutines and Flow and this error seems to happen when not using runBlockingTest, which I use already.

EDIT:

As a temporary solution, I'm testing it as a live data:

@Captor
lateinit var captor: ArgumentCaptor<State<UserWallets>>

@Mock
lateinit var walletsObserver: Observer<State<UserWallets>>

@Test
fun `observe user wallets ok`() = runBlockingTest {
    viewModel.userWallet.asLiveData().observeForever(walletsObserver)

    viewModel.getUserWallets()
    captor.run {
        Mockito.verify(walletsObserver, Mockito.times(3)).onChanged(capture())
        Assertions.assertThat(allValues[0] is State.Init).isTrue()
        Assertions.assertThat(allValues[1] is State.Loading).isTrue()
        Assertions.assertThat(allValues[2] is State.Success).isTrue()
    }
}

Body:
runBlockingTest just skips the delays in your case but not override the dispatcher used in the ViewModel with your test dispatcher. You need to inject TestCoroutineDispatcher to your ViewModel or since you are using viewModelScope.launch {} which already uses Dispatchers.Main by default, you need to override the main dispatcher via Dispatchers.setMain(testCoroutineDispatcher). You can create and add the following rule to your test file.

class MainCoroutineRule(
        val testDispatcher: TestCoroutineDispatcher = TestCoroutineDispatcher()
) : TestWatcher() {

    override fun starting(description: Description?) {
        super.starting(description)
        Dispatchers.setMain(testDispatcher)
    }

    override fun finished(description: Description?) {
        super.finished(description)
        Dispatchers.resetMain()
        testDispatcher.cleanupTestCoroutines()
    }
}

And in your test file

@get:Rule
var mainCoroutineRule = MainCoroutineRule()

@Test
fun `observe user wallets ok`() = mainCoroutineRule.testDispatcher.runBlockingTest {
}

Btw it is always a good practice to inject dispatchers. For instance if you would have been using a dispatcher other than Dispatchers.Main in your coroutine scope like viewModelScope.launch(Dispatchers.Default), then your test will fail again even if you are using a test dispatcher. The reason is you can only override main dispatcher with Dispatchers.setMain() as it can be understood from its name but not Dispatchers.IO or Dispatchers.Default. In that case you need to inject mainCoroutineRule.testDispatcher to your view model and use the injected dispatcher rather than hardcoding it.


问:
Handling ForeignKey in GraphQL (graphene)
I'm using Django-Graphene to build and API for my webapp. I've built this simple structure where I have Product that belong to a category:

```<code>from graphene_django import DjangoObjectType
import graphene
from .models import ProductModel, CategoryModel

class CategoryType(DjangoObjectType):
    class Meta:
        model = CategoryModel

class ProductType(DjangoObjectType):
    category = graphene.Field(CategoryType)

    class Meta:
        model = ProductModel

class CategoryInput(graphene.InputObjectType):
    title = graphene.String()

class ProductInput(graphene.InputObjectType):
    title = graphene.String()
    category = graphene.Field(CategoryInput)

class Query(graphene.ObjectType):
    products = graphene.List(ProductType)
    categories = graphene.List(CategoryType)

    def resolve_products(self, info):
        return ProductModel.objects.all()

    def resolve_categories(self, info):
        return CategoryModel.objects.all()

class CreateProduct(graphene.Mutation):
    class Arguments:
        product_data = ProductInput(required=True)

    product = graphene.Field(ProductType)

    @staticmethod
    def mutate(self, info, product_data):
        product = ProductModel.objects.create(**product_data)
        product.save()
        return CreateProduct(product=product)

class CreateCategory(graphene.Mutation):
    class Arguments:
        title = graphene.String()

    category = graphene.Field(CategoryType)

    def mutate(self, info, title):
        category = CategoryModel(title=title)
        category.save()
        return CreateCategory(category=category)

class Mutations(graphene.ObjectType):
    create_product = CreateProduct.Field()
    create_category = CreateCategory.Field()

schema = graphene.Schema(query=Query, mutation=Mutations)
</code>```

But when I try to mutate, I need to pass an instance of the CategoryModel, which I can't figure out  how to do:

```<code>mutation X {
  createProduct(productData: {title: "title", category: {}}) {
    product {
      id
    }
  }
}
</code>```

How can I create products which require a title and a category?

答:
I once had similar problem yesterday, but seems I haven't gotten a better grasp of GRAPHENE. But I wrote some funny codes which worked, tho there could be a more graphene way of solving it.

class ProductInput(graphene.InputObjectType):
    name = graphene.String()

class ProductPayload(graphene.Mutation):
    product = graphene.Field(ProductType)

    class Meta:
        input = ProductInput(required=True)
        category_name = graphene.String(required=True)

    def mutate(self, info, input=None, category_name=None):
        cat = None
        if category_name is not None:
            cat = Category.objects.get(name=category_name)
        product = Product.objects.create(name=input.name, category=cat)

        return ProductPayload(product=product)

class ProductMutation(graphene.ObjectType):
    create_product = ProductPayload.Field()

schema = graphene.Schema(mutation=ProductMutation
```

<p>To make a mutation in your graphiql,</p>

<pre class="lang-js prettyprint-override"><code>mutation {
    createProduct(input: {name: "Your product name"}, category_name: "your category name") {
        product {
            name,
            category {
                name

            }

        }
    }
}
</code>```

Body:
I'm using Django-Graphene to build and API for my webapp. I've built this simple structure where I have Product that belong to a category:

from graphene_django import DjangoObjectType
import graphene
from .models import ProductModel, CategoryModel

class CategoryType(DjangoObjectType):
    class Meta:
        model = CategoryModel

class ProductType(DjangoObjectType):
    category = graphene.Field(CategoryType)

    class Meta:
        model = ProductModel

class CategoryInput(graphene.InputObjectType):
    title = graphene.String()

class ProductInput(graphene.InputObjectType):
    title = graphene.String()
    category = graphene.Field(CategoryInput)

class Query(graphene.ObjectType):
    products = graphene.List(ProductType)
    categories = graphene.List(CategoryType)

    def resolve_products(self, info):
        return ProductModel.objects.all()

    def resolve_categories(self, info):
        return CategoryModel.objects.all()

class CreateProduct(graphene.Mutation):
    class Arguments:
        product_data = ProductInput(required=True)

    product = graphene.Field(ProductType)

    @staticmethod
    def mutate(self, info, product_data):
        product = ProductModel.objects.create(**product_data)
        product.save()
        return CreateProduct(product=product)

class CreateCategory(graphene.Mutation):
    class Arguments:
        title = graphene.String()

    category = graphene.Field(CategoryType)

    def mutate(self, info, title):
        category = CategoryModel(title=title)
        category.save()
        return CreateCategory(category=category)

class Mutations(graphene.ObjectType):
    create_product = CreateProduct.Field()
    create_category = CreateCategory.Field()

schema = graphene.Schema(query=Query, mutation=Mutations)

But when I try to mutate, I need to pass an instance of the CategoryModel, which I can't figure out  how to do:

mutation X {
  createProduct(productData: {title: "title", category: {}}) {
    product {
      id
    }
  }
}

How can I create products which require a title and a category?

Body:
I once had similar problem yesterday, but seems I haven't gotten a better grasp of GRAPHENE. But I wrote some funny codes which worked, tho there could be a more graphene way of solving it.

class ProductInput(graphene.InputObjectType):
    name = graphene.String()

class ProductPayload(graphene.Mutation):
    product = graphene.Field(ProductType)

    class Meta:
        input = ProductInput(required=True)
        category_name = graphene.String(required=True)

    def mutate(self, info, input=None, category_name=None):
        cat = None
        if category_name is not None:
            cat = Category.objects.get(name=category_name)
        product = Product.objects.create(name=input.name, category=cat)

        return ProductPayload(product=product)

class ProductMutation(graphene.ObjectType):
    create_product = ProductPayload.Field()

schema = graphene.Schema(mutation=ProductMutation

To make a mutation in your graphiql,

mutation {
    createProduct(input: {name: "Your product name"}, category_name: "your category name") {
        product {
            name,
            category {
                name

            }

        }
    }
}


问:
Threading in Python, "communication" between threads
I have two functions: def is_updated_database(): is checking if database is updated and the other  onedef scrape_links(database): is scraping through set of links(that it downloaded from aforementioned database).

So what I want do is when def is_updated_database(): finds that the updated is downloaded, I want to stop def scrape_links(database): and reload it with a new function parameter(database which would be a list of new links).

My attempt: I know how to run two threads, but I have no idea how to "connect" them, so that if something happens to one then something should happen to another one.

答:
Well, one way to solve this problem, may be the checking of database state, and if something new appears there, you could return the new database object, and after that scrape the links, probably this is losing it's multithreading functionality, but that's the way it works.

I don't think that any code examples are required here for you to understand what I mean.


问:
Apache Beam - Sliding Windows Only Emit Earliest Active Window
I'm trying to use Apache Beam (via Scio) to run a continuous aggregation of the last 3 days of data (processing time) from a streaming source and output results from the earliest, active window every 5 minutes. Earliest meaning the window with the earliest start time, active meaning that the end of the window hasn't yet passed. Essentially I'm trying to get a 'rolling' aggregation by dropping the non-overlapping period between sliding windows.

A visualization of what I'm trying to accomplish with an example sliding window of size 3 days and period 1 day:

```<code>early firing -  ^       no firing - x
                |

                               ** stop firing from this window once time passes this point
          ^  ^  ^  ^  ^  ^  ^  ^
          |  |  |  |  |  |  |  |      ** stop firing from this window once time passes this point
w1:       +====================+^  ^  ^
                  x x x x x x x |  |  |
w2:              +====================+^  ^  ^
                         x x x x x x x |  |  |
w3:                     +====================+

time: ----d1-----d2-----d3-----d4-----d5-----d6-----d7---->
</code>```

I've tried using sliding windows (size=3 days, period=5 min), but they produce a new window for every 3 days/5 min combination in the future and are emitting early results for every window. I tried using trigger = AfterWatermark.pastEndOfWindow(), but I need early results when the job first starts. I've tried comparing the pane data (isLast, timestamp, etc.) between windows but they seem identical.

My most recent attempt, which seems somewhat of a hack, included attaching window information to each key in a DoFn, re-windowing into a fixed window, and attempting to group and reduce to the oldest window from the attached data, but the final reduceByKey doesn't seem to output anything.

DoFn to attach window information

// ValueType is just a case class I'm using for objects

type DoFnT = DoFn[KV[String, ValueType], KV[String, (ValueType, Instant)]]

class Test extends DoFnT {
  // Window.toString looks like the following:
  // [2020-05-16T23:57:00.000Z..2020-05-17T00:02:00.000Z)
  def parseWindow(window: String): Instant = {
    Instant.parse(
      window
        .stripPrefix("[")
        .stripSuffix(")")
        .split("\\.\\.")(1))
  }

  @ProcessElement
  def process(
        context: DoFnT#ProcessContext,
        window: BoundedWindow): Unit = {
    context.output(
      KV.of(
        context.element().getKey,
        (context.element().getValue, parseWindow(window.toString))
      )
    )
  }
}
```

<pre class="lang-scala prettyprint-override"><code>sc
  .pubsubSubscription(...)
  .keyBy(_.key)
  .withSlidingWindows(
    size = Duration.standardDays(3),
    period = Duration.standardMinutes(5),
    options = WindowOptions(
      accumulationMode = DISCARDING_FIRED_PANES,
      allowedLateness = Duration.ZERO,
      trigger = Repeatedly.forever(
        AfterWatermark.pastEndOfWindow()
          .withEarlyFirings(
            AfterProcessingTime
              .pastFirstElementInPane()
              .plusDelayOf(Duration.standardMinutes(1)))))))
  .reduceByKey(ValueType.combineFunction())
  .applyPerKeyDoFn(new Test())
  .withFixedWindows(
    duration = Duration.standardMinutes(5),
    options = WindowOptions(
      accumulationMode = DISCARDING_FIRED_PANES,
      trigger = AfterWatermark.pastEndOfWindow(),
      allowedLateness = Duration.ZERO))
  .reduceByKey((x, y) => if (x._2.isBefore(y._2)) x else y)
  .saveAsCustomOutput(
    TextIO.write()...
  )
</code>```

Any suggestions?

答:
First, regarding processing time: If you want to window according to processing time, you should set your event time to the processing time. This is perfectly fine - it means that the event you are processing is the event of ingesting the record, not the event that the record represents.

Now you can use sliding windows off-the-shelf to get the aggregation you want, grouped the way you want.

But you are correct that it is a bit of a headache to trigger the way you want. Triggers are not easily expressive enough to say "output the last 3 day aggregation but only begin when the window is 5 minutes from over" and even less able to express "for the first 3 day period from pipeline startup, output the whole time".

I believe a stateful ParDo(DoFn) will be your best choice. State is partitioned per key and window. Since you want to have interactions across 3 day aggregations you will need to run your DoFn in the global window and manage the partitioning of the aggregations yourself. You tagged your question google-cloud-dataflow and Dataflow does not support MapState so you will need to use a ValueState that holds a map of the active 3 day aggregations, starting new aggregations as needed and removing old ones when they are done. Separately, you can easily track the aggregation from which you want to periodically output, and have a timer callback that periodically emits the active aggregation. Something like the following pseudo-Java; you can translate to Scala and insert your own types:

DoFn<> {
  @StateId("activePeriod") StateSpec<ValueState<Period>> activePeriod = StateSpecs.value();

  @StateId("accumulators") StateSpec<ValueState<Map<Period, Accumulator>>> accumulators = StateSpecs.value();

  @TimerId("nextPeriod") TimerSpec nextPeriod = TimerSpecs.timer(TimeDomain.EVENT_TIME);

  @TimerId("output") TimerSpec outputTimer = TimerSpecs.timer(TimeDomain.EVENT_TIME);

  @ProcessElement
  public void process(
      @Element element,
      @TimerId("nextPeriod") Timer nextPeriod,
      @TimerId("output") Timer output,
      @StateId("activePeriod") ValueState<Period> activePeriod
      @StateId("accumulators") ValueState<Map<Period, Accumulator>> accumulators) {

    // Set nextPeriod if it isn't already running
    // Set output if it isn't already running
    // Set activePeriod if it isn't already set
    // Add the element to the appropriate accumulator
  }

  @OnTimer("nextPeriod")
  public void onNextPeriod(
      @TimerId("nextPeriod") Timer nextPeriod,
      @StateId("activePriod") ValueState<Period> activePeriod {

    // Set activePeriod to the next one
    // Clear the period we will never read again
    // Reset the timer (there's a one-time change in this logic after the first window; add a flag for this)
  }

  @OnTimer("output")
  public void onOutput(
      @TimerId("output") Timer output,
      @StateId("activePriod") ValueState<Period> activePeriod,
      @StateId("accumulators") ValueState<MapState<Period, Accumulator>> {

    // Output the current accumulator for the active period
    // Reset the timer
  }
}
```

I do have some reservations about this, because the outputs we are working so hard to suppress are not comparable to the outputs that are "replacing" them. I would be interesting in learning more about the use case. It is possible there is a more straightforward way to express the result you are interested in.

Body:
I'm trying to use Apache Beam (via Scio) to run a continuous aggregation of the last 3 days of data (processing time) from a streaming source and output results from the earliest, active window every 5 minutes. Earliest meaning the window with the earliest start time, active meaning that the end of the window hasn't yet passed. Essentially I'm trying to get a 'rolling' aggregation by dropping the non-overlapping period between sliding windows.

A visualization of what I'm trying to accomplish with an example sliding window of size 3 days and period 1 day:

early firing -  ^       no firing - x
                |

                               ** stop firing from this window once time passes this point
          ^  ^  ^  ^  ^  ^  ^  ^
          |  |  |  |  |  |  |  |      ** stop firing from this window once time passes this point
w1:       +====================+^  ^  ^
                  x x x x x x x |  |  |
w2:              +====================+^  ^  ^
                         x x x x x x x |  |  |
w3:                     +====================+

time: ----d1-----d2-----d3-----d4-----d5-----d6-----d7---->

I've tried using sliding windows (size=3 days, period=5 min), but they produce a new window for every 3 days/5 min combination in the future and are emitting early results for every window. I tried using trigger = AfterWatermark.pastEndOfWindow(), but I need early results when the job first starts. I've tried comparing the pane data (isLast, timestamp, etc.) between windows but they seem identical.

My most recent attempt, which seems somewhat of a hack, included attaching window information to each key in a DoFn, re-windowing into a fixed window, and attempting to group and reduce to the oldest window from the attached data, but the final reduceByKey doesn't seem to output anything.

DoFn to attach window information

// ValueType is just a case class I'm using for objects

type DoFnT = DoFn[KV[String, ValueType], KV[String, (ValueType, Instant)]]

class Test extends DoFnT {
  // Window.toString looks like the following:
  // [2020-05-16T23:57:00.000Z..2020-05-17T00:02:00.000Z)
  def parseWindow(window: String): Instant = {
    Instant.parse(
      window
        .stripPrefix("[")
        .stripSuffix(")")
        .split("\\.\\.")(1))
  }

  @ProcessElement
  def process(
        context: DoFnT#ProcessContext,
        window: BoundedWindow): Unit = {
    context.output(
      KV.of(
        context.element().getKey,
        (context.element().getValue, parseWindow(window.toString))
      )
    )
  }
}

sc
  .pubsubSubscription(...)
  .keyBy(_.key)
  .withSlidingWindows(
    size = Duration.standardDays(3),
    period = Duration.standardMinutes(5),
    options = WindowOptions(
      accumulationMode = DISCARDING_FIRED_PANES,
      allowedLateness = Duration.ZERO,
      trigger = Repeatedly.forever(
        AfterWatermark.pastEndOfWindow()
          .withEarlyFirings(
            AfterProcessingTime
              .pastFirstElementInPane()
              .plusDelayOf(Duration.standardMinutes(1)))))))
  .reduceByKey(ValueType.combineFunction())
  .applyPerKeyDoFn(new Test())
  .withFixedWindows(
    duration = Duration.standardMinutes(5),
    options = WindowOptions(
      accumulationMode = DISCARDING_FIRED_PANES,
      trigger = AfterWatermark.pastEndOfWindow(),
      allowedLateness = Duration.ZERO))
  .reduceByKey((x, y) => if (x._2.isBefore(y._2)) x else y)
  .saveAsCustomOutput(
    TextIO.write()...
  )

Any suggestions?

Body:
First, regarding processing time: If you want to window according to processing time, you should set your event time to the processing time. This is perfectly fine - it means that the event you are processing is the event of ingesting the record, not the event that the record represents.

Now you can use sliding windows off-the-shelf to get the aggregation you want, grouped the way you want.

But you are correct that it is a bit of a headache to trigger the way you want. Triggers are not easily expressive enough to say "output the last 3 day aggregation but only begin when the window is 5 minutes from over" and even less able to express "for the first 3 day period from pipeline startup, output the whole time".

I believe a stateful ParDo(DoFn) will be your best choice. State is partitioned per key and window. Since you want to have interactions across 3 day aggregations you will need to run your DoFn in the global window and manage the partitioning of the aggregations yourself. You tagged your question google-cloud-dataflow and Dataflow does not support MapState so you will need to use a ValueState that holds a map of the active 3 day aggregations, starting new aggregations as needed and removing old ones when they are done. Separately, you can easily track the aggregation from which you want to periodically output, and have a timer callback that periodically emits the active aggregation. Something like the following pseudo-Java; you can translate to Scala and insert your own types:

DoFn<> {
  @StateId("activePeriod") StateSpec<ValueState<Period>> activePeriod = StateSpecs.value();

  @StateId("accumulators") StateSpec<ValueState<Map<Period, Accumulator>>> accumulators = StateSpecs.value();

  @TimerId("nextPeriod") TimerSpec nextPeriod = TimerSpecs.timer(TimeDomain.EVENT_TIME);

  @TimerId("output") TimerSpec outputTimer = TimerSpecs.timer(TimeDomain.EVENT_TIME);

  @ProcessElement
  public void process(
      @Element element,
      @TimerId("nextPeriod") Timer nextPeriod,
      @TimerId("output") Timer output,
      @StateId("activePeriod") ValueState<Period> activePeriod
      @StateId("accumulators") ValueState<Map<Period, Accumulator>> accumulators) {

    // Set nextPeriod if it isn't already running
    // Set output if it isn't already running
    // Set activePeriod if it isn't already set
    // Add the element to the appropriate accumulator
  }

  @OnTimer("nextPeriod")
  public void onNextPeriod(
      @TimerId("nextPeriod") Timer nextPeriod,
      @StateId("activePriod") ValueState<Period> activePeriod {

    // Set activePeriod to the next one
    // Clear the period we will never read again
    // Reset the timer (there's a one-time change in this logic after the first window; add a flag for this)
  }

  @OnTimer("output")
  public void onOutput(
      @TimerId("output") Timer output,
      @StateId("activePriod") ValueState<Period> activePeriod,
      @StateId("accumulators") ValueState<MapState<Period, Accumulator>> {

    // Output the current accumulator for the active period
    // Reset the timer
  }
}

I do have some reservations about this, because the outputs we are working so hard to suppress are not comparable to the outputs that are "replacing" them. I would be interesting in learning more about the use case. It is possible there is a more straightforward way to express the result you are interested in.


问:
This expression is not callable. Type 'Number' has no call signatures
Hi I'm new in Typescript, I have an object-type variable in which might be different values from different types or nested object. now my question is that how can I define a model for this object to don't face the example error when call different keys?

For example:

```<code>export class Controller {
protected static response(res: Response, statusCode: number = 200, data: any, user: string = '', dev: string = '', code: number = 200, result: string = 'success'){
    res.status(statusCode).send({
        data: data,
        message: {
            user: '',
            dev: ''
        },
        code: 403,
        result: 'Error'
    })
}

 ERROR: res.status ---> This expression is not callable. Type 'Number' has no call signatures
</code>```

答:
I was getting this error as well, and realized I'd just forgotten to import Response. Adding an import line solved the problem for me. </p>

```<code>import express, {Request, Response} from 'express';
</code>```

Body:
Hi I'm new in Typescript, I have an object-type variable in which might be different values from different types or nested object. now my question is that how can I define a model for this object to don't face the example error when call different keys?

For example:

export class Controller {
protected static response(res: Response, statusCode: number = 200, data: any, user: string = '', dev: string = '', code: number = 200, result: string = 'success'){
    res.status(statusCode).send({
        data: data,
        message: {
            user: '',
            dev: ''
        },
        code: 403,
        result: 'Error'
    })
}

 ERROR: res.status ---> This expression is not callable. Type 'Number' has no call signatures

Body:
I was getting this error as well, and realized I'd just forgotten to import Response. Adding an import line solved the problem for me.

import express, {Request, Response} from 'express';


问:
Concatenate Series using For
I'm having some trouble creating a DataFrame with some Series. Is there a way to concat them with a for? Because each time I try I only get the last Series in the DF, when I really want it to concat it the columns and not in place.

```<code>suma_queries = list()
for query in queries:
    cur.execute(query)
    schema = lib.get_schema_sql(cursor = cur)
    table = lib.get_table_sql(cur)
    df = pd.DataFrame(data = table, columns = schema)
    suma_queries.append(df.iloc[:,18].sum())

suma_queries = pd.Series(suma_queries)
concat_df = pd.concat([suma_queries], axis=1)
</code>```

As you see, for each "suma_queries" Series gotten from the for, I try to concatenate it to a dataframe called concat_df, and so on for the next "suma_queries" Series, but in the end, I only get the last Series, because the for replaces the value.

What I want at the end should be a dataframe like:

```<code>Series1      Series2    Series3 …   SeriesN
s1_1         s2_1       s3_1        sn_1
s1_2         …          …           …
s1_3         …          …           …
…            …          …           …
s1_n         s2_n       s3_n        sn_n
</code>```

where each column is a series.

Please let me know if there is way to do it,

Thanks!!

答:
you should reasign the appended dataframe back to itself in the for loop:</p>

```<code>for query in queries:
    cur.execute(query)
    schema = lib.get_schema_sql(cursor = cur)
    table = lib.get_table_sql(cur)
    df = pd.DataFrame(data = table, columns = schema)
    suma_queries=suma_queries.append(df.iloc[:,18].sum())

suma_queries = pd.Series(suma_queries)
concat_df = pd.concat([suma_queries], axis=1)
</code>```

Title: Concatenate Series using For

Body:
I'm having some trouble creating a DataFrame with some Series. Is there a way to concat them with a for? Because each time I try I only get the last Series in the DF, when I really want it to concat it the columns and not in place.

suma_queries = list()
for query in queries:
    cur.execute(query)
    schema = lib.get_schema_sql(cursor = cur)
    table = lib.get_table_sql(cur)
    df = pd.DataFrame(data = table, columns = schema)
    suma_queries.append(df.iloc[:,18].sum())

suma_queries = pd.Series(suma_queries)
concat_df = pd.concat([suma_queries], axis=1)

As you see, for each "suma_queries" Series gotten from the for, I try to concatenate it to a dataframe called concat_df, and so on for the next "suma_queries" Series, but in the end, I only get the last Series, because the for replaces the value.

What I want at the end should be a dataframe like:

Series1      Series2    Series3 …   SeriesN
s1_1         s2_1       s3_1        sn_1
s1_2         …          …           …
s1_3         …          …           …
…            …          …           …
s1_n         s2_n       s3_n        sn_n

where each column is a series.

Please let me know if there is way to do it,

Thanks!!

Body:
you should reasign the appended dataframe back to itself in the for loop:

for query in queries:
    cur.execute(query)
    schema = lib.get_schema_sql(cursor = cur)
    table = lib.get_table_sql(cur)
    df = pd.DataFrame(data = table, columns = schema)
    suma_queries=suma_queries.append(df.iloc[:,18].sum())

suma_queries = pd.Series(suma_queries)
concat_df = pd.concat([suma_queries], axis=1)


问:
Access to wwwroot - Asp.Net Core MVC working well on local host but not in published app
I'm having a lot of trouble trying to get my App to work when
published. Basically, the code is supposed to create a doc from
template using Open XML sdk, then save to wwwroot and then upload to
blob storage.

It's working fine using local host. Have read and tried some stuff re
accessing static files - but nothing seems to work. Any help would be
very much appreciated. Relevant code is below:

```<code>[HttpGet]
public IActionResult GenerateDocxBrowser(MemoryStream mem, string filepath, string inputLastName, string inputTitle, string requestID, string dateReceived, string complaintType, string complaintDetails, string nameString, string no, string street, string town, string postcode)
{
        var list = _context.Complaints.Where(s => s.ComplaintId.ToString().Contains(requestID)).ToList();
        using (mem = new MemoryStream())
        {

        filepath = @"wwwroot\RequestTemplate.docx";

        nameString = list.Select(s => s.NameString).FirstOrDefault();
            complaintDetails = list.Select(s => s.Complaint).FirstOrDefault();
            street = list.Select(s => s.AddressStreet).FirstOrDefault();
            town = list.Select(s => s.AddressTown).FirstOrDefault();
            using (WordprocessingDocument document = WordprocessingDocument.Open(filepath,true))
            {
                document.GetMergeFields("LastName").ReplaceWithText(inputLastName);
                document.GetMergeFields("Title").ReplaceWithText(inputTitle);
                document.GetMergeFields("ComplaintID").ReplaceWithText(requestID);
                document.GetMergeFields("DateReceived").ReplaceWithText(dateReceived);
                document.GetMergeFields("ComplaintType").ReplaceWithText(complaintType);
                document.GetMergeFields("ComplaintDetails").ReplaceWithText(complaintDetails);
                document.GetMergeFields("NameString").ReplaceWithText(nameString);
                document.GetMergeFields("AddressLn1").ReplaceWithText(no + " " + street);
                document.GetMergeFields("AddressLn2").ReplaceWithText(town + " TAS " + postcode);
                document.SaveAs(@"wwwroot\" + requestID + ".docx");
                document.MainDocumentPart.Document.Save();
                document.Close();
            }
        }

    const string StorageAccountName = "xxx";
    const string StorageAccountKey = "xxxxxx";
    var storageAccount = new CloudStorageAccount(
    new StorageCredentials(StorageAccountName, StorageAccountKey), true);

    var blobClient = storageAccount.CreateCloudBlobClient();
    var container = blobClient.GetContainerReference("tasman/Request Images");

    CloudBlockBlob blockBlob = container.GetBlockBlobReference(requestID + ".docx");

    blockBlob.UploadFromFileAsync(@"wwwroot\" + requestID + ".docx");

    return View();
}
</code>```

答:
Your .SaveAs() field should be relative, currently its literally saving to wwwroot somewhere on the drive. You can specify the relative path a few different ways - one of them is below:

```<code>var saveToFolder = Path.Combine(Environment.CurrentDirectory, $"/wwwroot/{requestID}.docx");
</code>```

Body:
I'm having a lot of trouble trying to get my App to work when
published. Basically, the code is supposed to create a doc from
template using Open XML sdk, then save to wwwroot and then upload to
blob storage.

It's working fine using local host. Have read and tried some stuff re
accessing static files - but nothing seems to work. Any help would be
very much appreciated. Relevant code is below:

[HttpGet]
public IActionResult GenerateDocxBrowser(MemoryStream mem, string filepath, string inputLastName, string inputTitle, string requestID, string dateReceived, string complaintType, string complaintDetails, string nameString, string no, string street, string town, string postcode)
{
        var list = _context.Complaints.Where(s => s.ComplaintId.ToString().Contains(requestID)).ToList();
        using (mem = new MemoryStream())
        {

        filepath = @"wwwroot\RequestTemplate.docx";

        nameString = list.Select(s => s.NameString).FirstOrDefault();
            complaintDetails = list.Select(s => s.Complaint).FirstOrDefault();
            street = list.Select(s => s.AddressStreet).FirstOrDefault();
            town = list.Select(s => s.AddressTown).FirstOrDefault();
            using (WordprocessingDocument document = WordprocessingDocument.Open(filepath,true))
            {
                document.GetMergeFields("LastName").ReplaceWithText(inputLastName);
                document.GetMergeFields("Title").ReplaceWithText(inputTitle);
                document.GetMergeFields("ComplaintID").ReplaceWithText(requestID);
                document.GetMergeFields("DateReceived").ReplaceWithText(dateReceived);
                document.GetMergeFields("ComplaintType").ReplaceWithText(complaintType);
                document.GetMergeFields("ComplaintDetails").ReplaceWithText(complaintDetails);
                document.GetMergeFields("NameString").ReplaceWithText(nameString);
                document.GetMergeFields("AddressLn1").ReplaceWithText(no + " " + street);
                document.GetMergeFields("AddressLn2").ReplaceWithText(town + " TAS " + postcode);
                document.SaveAs(@"wwwroot\" + requestID + ".docx");
                document.MainDocumentPart.Document.Save();
                document.Close();
            }
        }

    const string StorageAccountName = "xxx";
    const string StorageAccountKey = "xxxxxx";
    var storageAccount = new CloudStorageAccount(
    new StorageCredentials(StorageAccountName, StorageAccountKey), true);

    var blobClient = storageAccount.CreateCloudBlobClient();
    var container = blobClient.GetContainerReference("tasman/Request Images");

    CloudBlockBlob blockBlob = container.GetBlockBlobReference(requestID + ".docx");

    blockBlob.UploadFromFileAsync(@"wwwroot\" + requestID + ".docx");

    return View();
}

Body:
Your .SaveAs() field should be relative, currently its literally saving to wwwroot somewhere on the drive. You can specify the relative path a few different ways - one of them is below:

var saveToFolder = Path.Combine(Environment.CurrentDirectory, $"/wwwroot/{requestID}.docx");


问:
.gitignore not ignoring node_modules
Even after adding the .gitignore file to the root of the .git repository, I cannot make git ignore the node_modules directory.

The files have not been added for Git to track.

I have gone through earlier stack overflow questions, and also tried adding a comment line to the first line, since apparently Git doesn't read the first line of that file, and it still doesn't work. I have tried to use the following command also, with no avail:

```<code>git rm --cached -r .
</code>```

Could anyone help me out? The content of .gitignore:

```<code>#first line
node_modules/
</code>```

答:
My project structure was as follows:</p>

```<code>project_name
 |
 |__ client
 |      |_ node_modules/
 |
 |__ server

$ sudo nano .gitignore

Changed # dependencies
        /node_modules to client/node_modules

$ git add .     /* to add modified gitignore file */
$ git status    /* to observe changes */
</code>```

Title: .gitignore not ignoring node_modules

Body:
Even after adding the .gitignore file to the root of the .git repository, I cannot make git ignore the node_modules directory.

The files have not been added for Git to track.

I have gone through earlier stack overflow questions, and also tried adding a comment line to the first line, since apparently Git doesn't read the first line of that file, and it still doesn't work. I have tried to use the following command also, with no avail:

git rm --cached -r .

Could anyone help me out? The content of .gitignore:

#first line
node_modules/

Body:
My project structure was as follows:

project_name
 |
 |__ client
 |      |_ node_modules/
 |
 |__ server

$ sudo nano .gitignore

Changed # dependencies
        /node_modules to client/node_modules

$ git add .     /* to add modified gitignore file */
$ git status    /* to observe changes */


答:
You can use chpasswd command with echo. Here is a simple example</p>

```<code>/bin/echo my-user:password | /usr/sbin/chpasswd
</code>```

Title: How to add user in centos without manual password prompt

Body:
You can use chpasswd command with echo. Here is a simple example

/bin/echo my-user:password | /usr/sbin/chpasswd


问:
Work with an Amazon S3 response stream after response has been disposed
I'm using the Amazon SDK and I have a method that returns a Stream for an object stored in Amazon S3 service.

It contains something like this:

```<code>var request = new GetObjectRequest().WithBucketName(bucketName).WithKey(keyName);
using (var response = client.GetObject(request))
{
    return response.ResponseStream;
}
</code>```

Obviously, when doing this, the stream is not readable from the calling method because the request object has been disposed and when this is done, it closes the stream.

I don't want to download the file to a MemoryStream or a FileStream.

If I don't use the using clause, the garbage collector will dispose the request object at some point so I can't just not use it.

What I'm asking is, is there a way to wrap or copy the Stream into another Stream and then return it without having to download the file?

I'm using .NET 3.5.

Edit: The method is inherited from an abstract class and the caller method doesn't know it is working with Amazon. So it HAS to return a Stream.

答:
You can't work with the stream after it's disposed, but you can postpone disposing the response object until after the response stream has been used.  There are three options I can suggest.

Return Response.  One option is to return the response object to the caller.  The caller can access the contained response stream and then dispose the response when done.  This is the easiest change but requires the caller to change as well.

Wrap the stream.  Instead of returning the response stream directly, create a new object that extends Stream and wraps the response stream but also has a reference to the response itself.  Then when your wrapper is disposed, you can internally dispose the response object.  This requires only changing your called code as long as the type being returned is just Stream.

Callback.  Don't return anything from the method and use a callback mechanism instead.  Accept an Action<Stream> as a parameter and call this callback with the stream.  That way the callers code is called and you still have control over disposing the response and stream.  This is the safest mechanism since you're not relying on the caller to dispose anything, but requires the most changes.

Body:
I'm using the Amazon SDK and I have a method that returns a Stream for an object stored in Amazon S3 service.

It contains something like this:

var request = new GetObjectRequest().WithBucketName(bucketName).WithKey(keyName);
using (var response = client.GetObject(request))
{
    return response.ResponseStream;
}

Obviously, when doing this, the stream is not readable from the calling method because the request object has been disposed and when this is done, it closes the stream.

I don't want to download the file to a MemoryStream or a FileStream.

If I don't use the using clause, the garbage collector will dispose the request object at some point so I can't just not use it.

What I'm asking is, is there a way to wrap or copy the Stream into another Stream and then return it without having to download the file?

I'm using .NET 3.5.

Edit: The method is inherited from an abstract class and the caller method doesn't know it is working with Amazon. So it HAS to return a Stream.


问:
How do I detect if a Spark DataFrame has a column
When I create a DataFrame from a JSON file in Spark SQL, how can I tell if a given column exists before calling .select

Example JSON schema:

```<code>{
  "a": {
    "b": 1,
    "c": 2
  }
}
</code>```

This is what I want to do:

potential_columns = Seq("b", "c", "d")
df = sqlContext.read.json(filename)
potential_columns.map(column => if(df.hasColumn(column)) df.select(s"a.$column"))
```

<p>but I can't find a good function for <code>hasColumn</code>. The closest I've gotten is to test if the column is in this somewhat awkward array: </p>

```scala> df.select("a.*").columns
res17: Array[String] = Array(b, c)
```

答:
For nested columns you can use </p>

```<code>df.schema.simpleString().find('column_name')
</code>```

Body:
When I create a DataFrame from a JSON file in Spark SQL, how can I tell if a given column exists before calling .select

Example JSON schema:

{
  "a": {
    "b": 1,
    "c": 2
  }
}

This is what I want to do:

potential_columns = Seq("b", "c", "d")
df = sqlContext.read.json(filename)
potential_columns.map(column => if(df.hasColumn(column)) df.select(s"a.$column"))

but I can't find a good function for hasColumn. The closest I've gotten is to test if the column is in this somewhat awkward array:

scala> df.select("a.*").columns
res17: Array[String] = Array(b, c)

Body:
For nested columns you can use

df.schema.simpleString().find('column_name')


问:
using "//?" to run javascript in Visual Studio code
https://puu.sh/FRzbQ/15519a1399.png

https://puu.sh/FRzh1/88c911756f.png

I was watching a video, and he used "//?" after a line of code in vs code, and then it returned the expected value. But that does not work for me. All I get is this

https://puu.sh/FRzeI/1ac1ca1b40.png

答: That is a pro feature of an Extension called Quokka: https://quokkajs.com/docs/index.html</p>


问:
For conditional type in object key, how to remove certain key to produce a subset object type?
Here is how I played with some advanced types. I want to make a config type that extends BaseConfig. With this config type, inter to a dataProducer type (Named as Conf in the example) that has the same property that is marked as 'yes' in the config type.

```<code>interface BaseConfig1 {
    a: 'yes'|'no';
    b: 'yes'|'no';
}

interface BaseConfig2 {
    a?: 'yes';
    b?: 'yes';
}

interface Config1 extends BaseConfig1 {
    a: 'yes';
}

interface Config2 extends BaseConfig2 {
    a: 'yes';
}

type Conf<T, D> = {
    [P in keyof T]: T[P] extends 'yes'? ((data:D) => string): never;
};

// Error. Missing property b.
const dataProducer1: Conf<Config1, any> = {a: (data:any) => ''};

// Works
const dataProducer2: Conf<Config2, any> = {a: (data:any) => ''};
</code>```

Ideally, BaseConfig1 is what I actually want to define. You need to mark 'Yes' in the child type to config the output type. But TS insist me to provide a b property whose type is never. Aren't never supposed to be left out in the types just like the Filter type?
type Filter<T, U> = T extends U ? T : never;

The best I can do is BaseConfig2, any better ideas?

Update: I do want to force the result type to have properties marked as 'yes' type. For the above example, the result type should have "a" property.

Update2: Another question, for BaseConfig2, b's type is 'yes'|undefined, so why the Conf<Config2, any> would expect a optional b property with undefined type instead of a b with never type.

Update3: Tried a different way to define Conf, but doesn't work.

```<code>type Conf<T, D> = {
    [P in keyof T & T[P] extends 'yes']: (data:D) => string;
};
</code>```

答:
```<code>interface BaseConfig {
  a: 'yes'|'no';
  b: 'yes'|'no';
}

interface BaseConfigD {
  a: (data:any) => string;
  b: (data:any) => string;
}

interface Config extends BaseConfig {
  a: 'yes';
}

// Extract the property names that extend 'yes'
type ConfProps<T> = {
  [P in keyof T]: T[P] extends 'yes'? P: never;
}[keyof T];

// Properties of Config that extend 'yes'
type ConfigProps = ConfProps<Config>;

// Properties of BaseConfigD that extend 'yes' from Config..
type BaseConfigDBasedOnConfigProps = Pick<BaseConfigD, ConfigProps>;

const dataProducer1: BaseConfigDBasedOnConfigProps = {a: (data:any) => ''};
</code>```

Body:
Here is how I played with some advanced types. I want to make a config type that extends BaseConfig. With this config type, inter to a dataProducer type (Named as Conf in the example) that has the same property that is marked as 'yes' in the config type.

interface BaseConfig1 {
    a: 'yes'|'no';
    b: 'yes'|'no';
}

interface BaseConfig2 {
    a?: 'yes';
    b?: 'yes';
}

interface Config1 extends BaseConfig1 {
    a: 'yes';
}

interface Config2 extends BaseConfig2 {
    a: 'yes';
}

type Conf<T, D> = {
    [P in keyof T]: T[P] extends 'yes'? ((data:D) => string): never;
};

// Error. Missing property b.
const dataProducer1: Conf<Config1, any> = {a: (data:any) => ''};

// Works
const dataProducer2: Conf<Config2, any> = {a: (data:any) => ''};

Ideally, BaseConfig1 is what I actually want to define. You need to mark 'Yes' in the child type to config the output type. But TS insist me to provide a b property whose type is never. Aren't never supposed to be left out in the types just like the Filter type?
type Filter<T, U> = T extends U ? T : never;

The best I can do is BaseConfig2, any better ideas?

Update: I do want to force the result type to have properties marked as 'yes' type. For the above example, the result type should have "a" property.

Update2: Another question, for BaseConfig2, b's type is 'yes'|undefined, so why the Conf<Config2, any> would expect a optional b property with undefined type instead of a b with never type.

Update3: Tried a different way to define Conf, but doesn't work.

type Conf<T, D> = {
    [P in keyof T & T[P] extends 'yes']: (data:D) => string;
};


问:
Display Meta Fields PHP WP
I am using the Category and Taxonomy Meta Fields plugin to create some necessary fields in the product categories.

When trying to display this information in php, it does not appear.

```<code>$cate = get_queried_object();
$cateID = $cate->term_id;
if (function_exists('get_all_wp_terms_meta'))
{
   print_r( get_all_wp_terms_meta($cateID) );
}
</code>```

in page archive-product.php, return Array ( )

答:
Use the plugin Advanced Custom Fields

In file archive-product.php use:

```<code>$cateID = get_queried_object();
$return = get_field('NameField', $cateID);
</code>```

if using a flexible field, name the layout in the field settings and use

```<code>if ( have_rows( 'NameItem', $cateID ) ) :
    while ( have_rows( 'NameItem', $cateID ) ) : the_row();
        if( get_row_layout() == 'NameLayout' ):
            echo get_sub_field('NameFiel');
         endif;
    endwhile;
else :
</code>```

Title: Display Meta Fields PHP WP

Body:
I am using the Category and Taxonomy Meta Fields plugin to create some necessary fields in the product categories.

When trying to display this information in php, it does not appear.

$cate = get_queried_object();
$cateID = $cate->term_id;
if (function_exists('get_all_wp_terms_meta'))
{
   print_r( get_all_wp_terms_meta($cateID) );
}

in page archive-product.php, return Array ( )

Body:
Use the plugin Advanced Custom Fields

In file archive-product.php use:

$cateID = get_queried_object();
$return = get_field('NameField', $cateID);

if using a flexible field, name the layout in the field settings and use

if ( have_rows( 'NameItem', $cateID ) ) :
    while ( have_rows( 'NameItem', $cateID ) ) : the_row();
        if( get_row_layout() == 'NameLayout' ):
            echo get_sub_field('NameFiel');
         endif;
    endwhile;
else :