问:
nested el variables for jsp taglibs
I'm trying to create an el variable that is only available inside a tag so that it would be something like:

```<code><mytaglib:mytag>
  Foo is ${foo}
</mytaglib:mytag>
</code>```

I've followed the documentation I can find for creating a nested el variable, however, ${foo} is available after the end of mytag.

Shortened version of the .tld file is:

```<code><?xml version="1.0" encoding="UTF-8" ?>

<taglib version="2.0" xmlns="http://java.sun.com/xml/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd">
  <description>My Tags</description>
  <tlib-version>1.0</tlib-version>
  <jsp-version>2.0</jsp-version>
  <short-name>mytaglib</short-name>
  <uri>http://www.example.com/taglibs/mytaglib</uri>

  <tag>
    <name>mytag</name>
    <tag-class>com.example.tags.MyTag</tag-class>
    <body-content>JSP</body-content>

    <variable>
      <name-given>used</name-given>
      <variable-class>java.lang.String</variable-class>
      <scope>NESTED</scope>
    </variable>
  </tag>
</taglib>
</code>```

then MyTag does the following ( I've removed all code that shouldn't be related ):

```<code>public class MyTag extends BodyTagSupport
{
  // these are subtags
  private JspFragment below;
  private JspFragment at;

  @Override
  public int doStartTag() throws JspException
  {
    return EVAL_BODY_BUFFERED;
  }

  @Override
  public int doEndTag() throws JspException
  {
    try
    {
      if (aBoolean)
      {
          below.invoke(pageContext.getOut());
      }
      else
      {
          at.invoke(pageContext.getOut());
      }
    }
    catch (IOException e)
    {
      throw new JspException(e);
    }

    return EVAL_PAGE;
  }

  @Override
  public void doInitBody() throws JspException
  {
    pageContext.setAttribute("used", "valueOfUsed");
  }

  @Override
  public int doAfterBody() throws JspException
  {
    try
    {
      bodyContent.writeOut(bodyContent.getEnclosingWriter());
    }
    catch (IOException e)
    {
      throw new JspException(e);
    }

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

My understanding is that pageContext.setAttribute() from within doInitBody should be in a nested scope and you evaluate the actual body in doAfterBody.
That should seem to be the case though as 'used' is available outside the scope of the tag.

General background... tag would look like (simplified form)

```<code><mytaglib:mytag>
  Used is ${used}
</mytaglib:mytag>
</code>```

or

```<code><mytaglib:mytag>
  <mytaglib:at>
    You are at your limit, you have used: ${used}
  </mytaglib:at>
  <mytaglib:below>
    You are below your limit, you have used: ${used}
  </mytaglib:below>
</mytaglib:mytag>
</code>```

How can I get ${used} to be scoped to mytag?

答:
I always reference the JSTL as the model for any tags I write.  So my approach would be to look at the source code of a tag that does something similar, and copy that.
The <c:forEach> tag is a perfect example.

The LoopTagSupport class in JSTL implements the interface javax.servlet.jsp.tagext.TryCatchFinally, and in the doFinally method it calls pageContext.removeAttribute() manually.

Quoting the JSTL1.1 source distribution:

```<code>/**
 * Removes any attributes that this LoopTagSupport set.
 *
 * <p> These attributes are intended to support scripting variables with
 * NESTED scope, so we don't want to pollute attribute space by leaving
 * them lying around.
 */
public void doFinally() {
    /*
     * Make sure to un-expose variables, restoring them to their
     * prior values, if applicable.
     */
    unExposeVariables();
}

/**
 * Removes page attributes that we have exposed and, if applicable,
 * restores them to their prior values (and scopes).
 */
private void unExposeVariables() {
    // "nested" variables are now simply removed
if (itemId != null)
        pageContext.removeAttribute(itemId, PageContext.PAGE_SCOPE);
if (statusId != null)
    pageContext.removeAttribute(statusId, PageContext.PAGE_SCOPE);
}
</code>```

Title: nested el variables for jsp taglibs

Body:
I'm trying to create an el variable that is only available inside a tag so that it would be something like:

<mytaglib:mytag>
  Foo is ${foo}
</mytaglib:mytag>

I've followed the documentation I can find for creating a nested el variable, however, ${foo} is available after the end of mytag.

Shortened version of the .tld file is:

<?xml version="1.0" encoding="UTF-8" ?>

<taglib version="2.0" xmlns="http://java.sun.com/xml/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd">
  <description>My Tags</description>
  <tlib-version>1.0</tlib-version>
  <jsp-version>2.0</jsp-version>
  <short-name>mytaglib</short-name>
  <uri>http://www.example.com/taglibs/mytaglib</uri>

  <tag>
    <name>mytag</name>
    <tag-class>com.example.tags.MyTag</tag-class>
    JSP

    <variable>
      <name-given>used</name-given>
      <variable-class>java.lang.String</variable-class>
      <scope>NESTED</scope>
    </variable>
  </tag>
</taglib>

then MyTag does the following ( I've removed all code that shouldn't be related ):

public class MyTag extends BodyTagSupport
{
  // these are subtags
  private JspFragment below;
  private JspFragment at;

  @Override
  public int doStartTag() throws JspException
  {
    return EVAL_BODY_BUFFERED;
  }

  @Override
  public int doEndTag() throws JspException
  {
    try
    {
      if (aBoolean)
      {
          below.invoke(pageContext.getOut());
      }
      else
      {
          at.invoke(pageContext.getOut());
      }
    }
    catch (IOException e)
    {
      throw new JspException(e);
    }

    return EVAL_PAGE;
  }

  @Override
  public void doInitBody() throws JspException
  {
    pageContext.setAttribute("used", "valueOfUsed");
  }

  @Override
  public int doAfterBody() throws JspException
  {
    try
    {
      bodyContent.writeOut(bodyContent.getEnclosingWriter());
    }
    catch (IOException e)
    {
      throw new JspException(e);
    }

    return SKIP_BODY;
  }
}

My understanding is that pageContext.setAttribute() from within doInitBody should be in a nested scope and you evaluate the actual body in doAfterBody.
That should seem to be the case though as 'used' is available outside the scope of the tag.

General background... tag would look like (simplified form)

<mytaglib:mytag>
  Used is ${used}
</mytaglib:mytag>

or

<mytaglib:mytag>
  <mytaglib:at>
    You are at your limit, you have used: ${used}
  </mytaglib:at>
  <mytaglib:below>
    You are below your limit, you have used: ${used}
  </mytaglib:below>
</mytaglib:mytag>

How can I get ${used} to be scoped to mytag?

Body:
I always reference the JSTL as the model for any tags I write.  So my approach would be to look at the source code of a tag that does something similar, and copy that.
The <c:forEach> tag is a perfect example.

The LoopTagSupport class in JSTL implements the interface javax.servlet.jsp.tagext.TryCatchFinally, and in the doFinally method it calls pageContext.removeAttribute() manually.

Quoting the JSTL1.1 source distribution:

/**
 * Removes any attributes that this LoopTagSupport set.
 *
 * <p> These attributes are intended to support scripting variables with
 * NESTED scope, so we don't want to pollute attribute space by leaving
 * them lying around.
 */
public void doFinally() {
    /*
     * Make sure to un-expose variables, restoring them to their
     * prior values, if applicable.
     */
    unExposeVariables();
}

/**
 * Removes page attributes that we have exposed and, if applicable,
 * restores them to their prior values (and scopes).
 */
private void unExposeVariables() {
    // "nested" variables are now simply removed
if (itemId != null)
        pageContext.removeAttribute(itemId, PageContext.PAGE_SCOPE);
if (statusId != null)
    pageContext.removeAttribute(statusId, PageContext.PAGE_SCOPE);
}


来源: stackexchange

问:
Remove/Delete all resources created while spinning a new VM through powershell
I have created a VM through powershell using template.json file. These are the list of resource types which gets created.

1. Virtual Machine

2. Network Interface

3. Network Security group

4. Disk

Now,what I do is update some apps on the newly created VM, generalize the VM and create an Image of the new VM. Till here I'm able to do it through powershell.

Now my requirement is to delete all the resources created during spinning the VM. I know the cmdlet to remove the VM is this.

```<code>Remove-VM -Name "new 2" -Force
</code>```

Is there any single command to do this task?

Any help or input is highly appreciated. Thanks

答:
I have one script ready in below Link. Just input the VM name and script can handle the rest
http://cloudcompute.info/delete-azure-vm-and-all-associated-resources-using-powershell-script/

Here is the complete script

```<code>Write-Host -NoNewline -ForegroundColor Green "Please enter the VM name you would like to remove:"
$VMName = Read-Host
$vm = Get-AzVm -Name $VMName
$RGName=$vm.ResourceGroupName
Write-Host -ForegroundColor Cyan 'Resource Group Name is identified as-' $RGName
$diagSa = [regex]::match($vm.DiagnosticsProfile.bootDiagnostics.storageUri, '^http[s]?://(.+?)\.').groups[1].value
Write-Host -ForegroundColor Cyan 'Marking Disks for deletion...'
$tags = @{"VMName"=$VMName; "Delete Ready"="Yes"}
$osDiskName = $vm.StorageProfile.OSDisk.Name
$datadisks = $vm.StorageProfile.DataDisks
$ResourceID= (Get-Azdisk -Name $osDiskName).id
New-AzTag -ResourceId $ResourceID -Tag $tags | Out-Null
if ($vm.StorageProfile.DataDisks.Count -gt 0) {
    foreach ($datadisks in $vm.StorageProfile.DataDisks){
    $datadiskname=$datadisks.name
    $ResourceID= (Get-Azdisk -Name $datadiskname).id
    New-AzTag -ResourceId $ResourceID -Tag $tags | Out-Null
    }
}
if ($vm.Name.Length -gt 9) {
    $i = 9
} else {
    $i = $vm.Name.Length - 1
}
$azResourceParams = @{
    'ResourceName' = $VMName
    'ResourceType' = 'Microsoft.Compute/virtualMachines'
    'ResourceGroupName' = $RGName
}
$vmResource = Get-AzResource @azResourceParams
$vmId = $vmResource.Properties.VmId
$diagContainerName = ('bootdiagnostics-{0}-{1}' -f $vm.Name.ToLower().Substring(0, $i), $vmId)
$diagSaRg = (Get-AzStorageAccount | where { $_.StorageAccountName -eq $diagSa }).ResourceGroupName
$saParams = @{
    'ResourceGroupName' = $diagSaRg
    'Name' = $diagSa
}
Write-Host -ForegroundColor Cyan 'Removing Boot Diagnostic disk..'
Get-AzStorageAccount @saParams | Get-AzStorageContainer | where {$_.Name-eq $diagContainerName} | Remove-AzStorageContainer -Force
Write-Host -ForegroundColor Cyan 'Removing Virtual Machine-' $VMName 'in Resource Group-' $RGName '...'
$null = $vm | Remove-AzVM -Force
Write-Host -ForegroundColor Cyan 'Removing Network Interface Cards, Public IP Address(s) used by the VM...'
foreach($nicUri in $vm.NetworkProfile.NetworkInterfaces.Id) {
    $nic = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name $nicUri.Split('/')[-1]
    Remove-AzNetworkInterface -Name $nic.Name -ResourceGroupName $vm.ResourceGroupName -Force
foreach($ipConfig in $nic.IpConfigurations) {
        if($ipConfig.PublicIpAddress -ne $null) {
            Remove-AzPublicIpAddress -ResourceGroupName $vm.ResourceGroupName -Name $ipConfig.PublicIpAddress.Id.Split('/')[-1] -Force
        }
    }
}
Write-Host -ForegroundColor Cyan 'Removing OS disk and Data Disk(s) used by the VM..'
Get-AzResource -tag $tags | where{$_.resourcegroupname -eq $RGName}| Remove-AzResource -force | Out-Null
Write-Host -ForegroundColor Green 'Azure Virtual Machine-' $VMName 'and all the resources associated with the VM were removed sucesfully...'
</code>```

ContentLicense: CC BY-SA 4.0

Body:
I have created a VM through powershell using template.json file. These are the list of resource types which gets created.

1. Virtual Machine

2. Network Interface

3. Network Security group

4. Disk

Now,what I do is update some apps on the newly created VM, generalize the VM and create an Image of the new VM. Till here I'm able to do it through powershell.

Now my requirement is to delete all the resources created during spinning the VM. I know the cmdlet to remove the VM is this.

Remove-VM -Name "new 2" -Force

Is there any single command to do this task?

Any help or input is highly appreciated. Thanks

Body:
I have one script ready in below Link. Just input the VM name and script can handle the rest
http://cloudcompute.info/delete-azure-vm-and-all-associated-resources-using-powershell-script/

Here is the complete script

Write-Host -NoNewline -ForegroundColor Green "Please enter the VM name you would like to remove:"
$VMName = Read-Host
$vm = Get-AzVm -Name $VMName
$RGName=$vm.ResourceGroupName
Write-Host -ForegroundColor Cyan 'Resource Group Name is identified as-' $RGName
$diagSa = [regex]::match($vm.DiagnosticsProfile.bootDiagnostics.storageUri, '^http[s]?://(.+?)\.').groups[1].value
Write-Host -ForegroundColor Cyan 'Marking Disks for deletion...'
$tags = @{"VMName"=$VMName; "Delete Ready"="Yes"}
$osDiskName = $vm.StorageProfile.OSDisk.Name
$datadisks = $vm.StorageProfile.DataDisks
$ResourceID= (Get-Azdisk -Name $osDiskName).id
New-AzTag -ResourceId $ResourceID -Tag $tags | Out-Null
if ($vm.StorageProfile.DataDisks.Count -gt 0) {
    foreach ($datadisks in $vm.StorageProfile.DataDisks){
    $datadiskname=$datadisks.name
    $ResourceID= (Get-Azdisk -Name $datadiskname).id
    New-AzTag -ResourceId $ResourceID -Tag $tags | Out-Null
    }
}
if ($vm.Name.Length -gt 9) {
    $i = 9
} else {
    $i = $vm.Name.Length - 1
}
$azResourceParams = @{
    'ResourceName' = $VMName
    'ResourceType' = 'Microsoft.Compute/virtualMachines'
    'ResourceGroupName' = $RGName
}
$vmResource = Get-AzResource @azResourceParams
$vmId = $vmResource.Properties.VmId
$diagContainerName = ('bootdiagnostics-{0}-{1}' -f $vm.Name.ToLower().Substring(0, $i), $vmId)
$diagSaRg = (Get-AzStorageAccount | where { $_.StorageAccountName -eq $diagSa }).ResourceGroupName
$saParams = @{
    'ResourceGroupName' = $diagSaRg
    'Name' = $diagSa
}
Write-Host -ForegroundColor Cyan 'Removing Boot Diagnostic disk..'
Get-AzStorageAccount @saParams | Get-AzStorageContainer | where {$_.Name-eq $diagContainerName} | Remove-AzStorageContainer -Force
Write-Host -ForegroundColor Cyan 'Removing Virtual Machine-' $VMName 'in Resource Group-' $RGName '...'
$null = $vm | Remove-AzVM -Force
Write-Host -ForegroundColor Cyan 'Removing Network Interface Cards, Public IP Address(s) used by the VM...'
foreach($nicUri in $vm.NetworkProfile.NetworkInterfaces.Id) {
    $nic = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name $nicUri.Split('/')[-1]
    Remove-AzNetworkInterface -Name $nic.Name -ResourceGroupName $vm.ResourceGroupName -Force
foreach($ipConfig in $nic.IpConfigurations) {
        if($ipConfig.PublicIpAddress -ne $null) {
            Remove-AzPublicIpAddress -ResourceGroupName $vm.ResourceGroupName -Name $ipConfig.PublicIpAddress.Id.Split('/')[-1] -Force
        }
    }
}
Write-Host -ForegroundColor Cyan 'Removing OS disk and Data Disk(s) used by the VM..'
Get-AzResource -tag $tags | where{$_.resourcegroupname -eq $RGName}| Remove-AzResource -force | Out-Null
Write-Host -ForegroundColor Green 'Azure Virtual Machine-' $VMName 'and all the resources associated with the VM were removed sucesfully...'


问:
Turning user authentication off in nginx for GraphQL api for Gatsby site
I'm trying to set up user authentication on my Gatsby site, but allow for the GraphQL api.

Here's what I have now:

```<code>location / {
    try_files $uri $uri/ @rewrites;
    auth_basic "Restricted";
    auth_basic_user_file /etc/nginx/.htpasswd;

    location "^~ /api/.*" {
       auth_basic "off";
    }
}
</code>```

EDIT, based on comments below

I tried to add what's in location ~ .php$ as well as what's in rewrites, but got the same error.

```<code>location / {
    try_files $uri $uri/ @rewrites;

    auth_basic "Restricted";
    auth_basic_user_file /etc/nginx/.htpasswd;

    location ^~ /api/ {
        rewrite ^(.*) /index.php?p=$1 last;
        auth_basic off;
    }
}

location @rewrites {
    rewrite ^(.*) /index.php?p=$1 last;
}

# PHP
location ~ \.php$ {
     fastcgi_read_timeout 1200;

     fastcgi_split_path_info ^(.+\.php)(/.+)$;
     fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;

     # below for some setups on php 7.4
     #include snippets/fastcgi-php.conf;
     include /etc/nginx/fastcgi_params;
     fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
</code>```

答:
One mistake is quoting off.

Just remove the quotes: change "off" to off. Then it will be an actual off configuration as opposed to the quoted string.

The other is the non-regex location (^~) for /api/.
The location ^~ best to be explained as a prefix location with the highest priority.
It is not a regular expression type of location.

```<code>location / {
    try_files $uri $uri/ @rewrites;
    auth_basic "Restricted";
    auth_basic_user_file /etc/nginx/.htpasswd;
}

location ^~ /api/ {
   auth_basic off;
   fastcgi_read_timeout 1200;

   fastcgi_split_path_info ^(.+\.php)(/.+)$;
   fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;

   # below for some setups on php 7.4
   #include snippets/fastcgi-php.conf;
   include /etc/nginx/fastcgi_params;
   fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}

location @rewrites {
    rewrite ^(.*) /index.php?p=$1 last;
}

# PHP
location ~ \.php$ {
     fastcgi_read_timeout 1200;

     fastcgi_split_path_info ^(.+\.php)(/.+)$;
     fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;

     # below for some setups on php 7.4
     #include snippets/fastcgi-php.conf;
     include /etc/nginx/fastcgi_params;
     fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
</code>```

Body:
I'm trying to set up user authentication on my Gatsby site, but allow for the GraphQL api.

Here's what I have now:

location / {
    try_files $uri $uri/ @rewrites;
    auth_basic "Restricted";
    auth_basic_user_file /etc/nginx/.htpasswd;

    location "^~ /api/.*" {
       auth_basic "off";
    }
}

EDIT, based on comments below

I tried to add what's in location ~ .php$ as well as what's in rewrites, but got the same error.

location / {
    try_files $uri $uri/ @rewrites;

    auth_basic "Restricted";
    auth_basic_user_file /etc/nginx/.htpasswd;

    location ^~ /api/ {
        rewrite ^(.*) /index.php?p=$1 last;
        auth_basic off;
    }
}

location @rewrites {
    rewrite ^(.*) /index.php?p=$1 last;
}

# PHP
location ~ \.php$ {
     fastcgi_read_timeout 1200;

     fastcgi_split_path_info ^(.+\.php)(/.+)$;
     fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;

     # below for some setups on php 7.4
     #include snippets/fastcgi-php.conf;
     include /etc/nginx/fastcgi_params;
     fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}

Body:
One mistake is quoting off.

Just remove the quotes: change "off" to off. Then it will be an actual off configuration as opposed to the quoted string.

The other is the non-regex location (^~) for /api/.
The location ^~ best to be explained as a prefix location with the highest priority.
It is not a regular expression type of location.

location / {
    try_files $uri $uri/ @rewrites;
    auth_basic "Restricted";
    auth_basic_user_file /etc/nginx/.htpasswd;
}

location ^~ /api/ {
   auth_basic off;
   fastcgi_read_timeout 1200;

   fastcgi_split_path_info ^(.+\.php)(/.+)$;
   fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;

   # below for some setups on php 7.4
   #include snippets/fastcgi-php.conf;
   include /etc/nginx/fastcgi_params;
   fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}

location @rewrites {
    rewrite ^(.*) /index.php?p=$1 last;
}

# PHP
location ~ \.php$ {
     fastcgi_read_timeout 1200;

     fastcgi_split_path_info ^(.+\.php)(/.+)$;
     fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;

     # below for some setups on php 7.4
     #include snippets/fastcgi-php.conf;
     include /etc/nginx/fastcgi_params;
     fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}


问:
How do I generate a uniqueidentifier for a set of records with a distinct key?
I have a record set that looks like this:

| key_sk | unique_id                            |
|--------|--------------------------------------|
| 2      | null                                 |
| 2      | null                                 |
| 3      | 83a1c90b-e58d-4db4-b438-a79edfb28e60 |
| 3      | 83a1c90b-e58d-4db4-b438-a79edfb28e60 |
| 4      | 4ce66783-0b84-4e8a-a0de-c3284e4d9cd0 |
| 5      | null                                 |
```

<p>I would like to generate a unique id for each unique <code>key_sk</code> set where the <code>unique_id</code> is null. For the above I'd like <code>key_sk 2</code> to have a a single <code>unique_id</code> like <code>key_sk 3</code> does.</p>

<p>My attempt below generated a different <code>uniqueidentifier</code> for each set. I think this is because of the recursive nature of common table expressions: each join to the CTE causes <code>NEWID()</code> to be called.</p>

<pre class="lang-sql prettyprint-override"><code>;with update_id_cte as
(
  select distinct hr.key_sk
        ,NEWID() as gened_unique_id
    from history_record hr
   where hr.unique_id is null
)
update hr
   set hr.unique_id = cte.gened_unique_id
  from history_record hr
       join update_id_cte cte
         on hr.key_sk = cte.key_sk
</code>```

There's probably a simpler way than utilizing a CTE to do this. How can I generate and update the history_record table with a single uniqueidentifier for each distinct key_sk?

答:
Instead of select distinct, you can use group by:

```<code>with update_id_cte as (
       select hr.key_sk, NEWID() as gened_unique_id
       from history_record hr
       where hr.unique_id is null
       group by hr.key_sk
      )
update hr
   set hr.unique_id = cte.gened_unique_id
   from history_record hr join
        update_id_cte cte
        on hr.key_sk = cte.key_sk;
</code>```

If it is possible that some key_sk values have both NULL and not-NULL keys AND you want to preserve existing values, you can tweak the logic:

```<code>with update_id_cte as (
       select hr.key_sk, coalesce(max(hr.unique_id), NEWID()) as gened_unique_id
       from history_record hr
       group by hr.key_sk
      )
update hr
   set hr.unique_id = cte.gened_unique_id
   from history_record hr join
        update_id_cte cte
        on hr.key_sk = cte.key_sk
   where hr.unique_id is null;
</code>```

Body:
I have a record set that looks like this:

| key_sk | unique_id                            |
|--------|--------------------------------------|
| 2      | null                                 |
| 2      | null                                 |
| 3      | 83a1c90b-e58d-4db4-b438-a79edfb28e60 |
| 3      | 83a1c90b-e58d-4db4-b438-a79edfb28e60 |
| 4      | 4ce66783-0b84-4e8a-a0de-c3284e4d9cd0 |
| 5      | null                                 |

I would like to generate a unique id for each unique key_sk set where the unique_id is null. For the above I'd like key_sk 2 to have a a single unique_id like key_sk 3 does.

My attempt below generated a different uniqueidentifier for each set. I think this is because of the recursive nature of common table expressions: each join to the CTE causes NEWID() to be called.

;with update_id_cte as
(
  select distinct hr.key_sk
        ,NEWID() as gened_unique_id
    from history_record hr
   where hr.unique_id is null
)
update hr
   set hr.unique_id = cte.gened_unique_id
  from history_record hr
       join update_id_cte cte
         on hr.key_sk = cte.key_sk

There's probably a simpler way than utilizing a CTE to do this. How can I generate and update the history_record table with a single uniqueidentifier for each distinct key_sk?

Body:
Instead of select distinct, you can use group by:

with update_id_cte as (
       select hr.key_sk, NEWID() as gened_unique_id
       from history_record hr
       where hr.unique_id is null
       group by hr.key_sk
      )
update hr
   set hr.unique_id = cte.gened_unique_id
   from history_record hr join
        update_id_cte cte
        on hr.key_sk = cte.key_sk;

If it is possible that some key_sk values have both NULL and not-NULL keys AND you want to preserve existing values, you can tweak the logic:

with update_id_cte as (
       select hr.key_sk, coalesce(max(hr.unique_id), NEWID()) as gened_unique_id
       from history_record hr
       group by hr.key_sk
      )
update hr
   set hr.unique_id = cte.gened_unique_id
   from history_record hr join
        update_id_cte cte
        on hr.key_sk = cte.key_sk
   where hr.unique_id is null;


问:
generate extract number of thumbnails with ffmpeg from a video
I want to get extract number of thumbnails for a video with ffmpeg for example 5-6 how can I do that?

the following command will generate 1 i would like 5-6
shell_exec("ffmpeg -ss 01:23:45 -i input.mkv -vframes 1 -q:v 2 output.jpg");

答:
this worked for me   </p>

```<code>for($thum = 1; $thum <= 5; $thum++)
    {
        $count = $thum * 10;
        shell_exec("ffmpeg -ss $count -i video.mkv -vframes 1 -q:v 2 $thum.jpg");
    }
</code>```

Body:
this worked for me

for($thum = 1; $thum <= 5; $thum++)
    {
        $count = $thum * 10;
        shell_exec("ffmpeg -ss $count -i video.mkv -vframes 1 -q:v 2 $thum.jpg");
    }


问:
Incorrect syntax near 'GO'
How can I execute the following SQL inside a single command (single execution) through ADO.NET?

```<code>ALTER TABLE [MyTable]
    ADD NewCol INT

GO

UPDATE [MyTable]
    SET [NewCol] = 1
</code>```

The batch separator GO is not supported, and without it the second statement fails.

Are there any solutions to this other than using multiple command executions?

答: I placed a semicolon ; after the GO, which was the cause of my error.

ContentLicense: CC BY-SA 3.0

Title: Incorrect syntax near 'GO'

Body:
How can I execute the following SQL inside a single command (single execution) through ADO.NET?

ALTER TABLE [MyTable]
    ADD NewCol INT

GO

UPDATE [MyTable]
    SET [NewCol] = 1

The batch separator GO is not supported, and without it the second statement fails.

Are there any solutions to this other than using multiple command executions?


问:
How do I get remote access with PowerShell?
I want to connect to a specific set of laptops on our domain remotely. I need to have it log to the C drive, and search out a specific folder, then report back on a specific file contained in the folder.
I have found scripts that have shown me how to locate and show if the path exists, along with a specific file name at the end of the path. I created a test file on my laptop and the script worked flawlessly.
However, I need it to reach out over our domain, log into the target laptop, locate the file and contents then create a report on the findings. This will need to be done over VPN to the devices on our network.

My main issue at this point is finding the commands to invoke PowerShell to log onto the remote computers.

答:
A rough overview on how to remote execute commands with PowerShell.

Enable PSRemoting with Enable-PSRemoting MS docs

Establish a remote connection with Enter-PSSession -ComputerName testserver123.global.local

This opens a prompt of the remote server where you can run commands. Your PS prompt will get a prefix indicating the remote server.

A useful link is here from 4sysops


问:
A while loop time complexity
I'm interested in determining the big O time complexity of the following:

```<code>def f(x):
    r = x / 2
    d = 1e-10
    while abs(x - r**2) > d:
        r = (r + x/r) / 2
    return r
</code>```

I believe this is O(log n). To arrive at this, I merely collected empirical data via the timeit module and plotted the results, and saw that a plot that looked logarithmic using the following code:

```<code>ns = np.linspace(1, 50_000, 100, dtype=int)
ts = [timeit.timeit('f({})'.format(n),
                    number=100,
                    globals=globals())
      for n in ns]
plt.plot(ns, ts, 'or')
</code>```

But this seems like a corny way to go about figuring this out. Intuitively, I understand that the body of the while loop involves dividing an expression by 2 some number k times until the while expression is equal to d. This repeated division by 2 gives something like 1/2^k, from which I can see where a log is involved to solve for k. I can't seem to write down a more explicit derivation, though. Any help?

答:
I believe you're correct that it's O(log n).

Here you can see the successive values of r when x = 100000:

```<code>1 50000
2 25001
3 12502
4 6255
5 3136
6 1584
7 823
8 472
9 342
10 317
11 316
12 316
</code>```

(I've rounded them off because the fractions are not interesting).

What you can see if that it goes through two phases.

Phase 1 is when r is large. During these first few iterations, x/r is tiny compared to r. As a result, r + x/r is close to r, so (r + x/r) / 2 is approximately r/2. You can see this in the first 8 iterations.

Phase 2 is when it gets close to the final result. During the last few iterations, x/r is close to r, so r + x/r is close to 2 * r, so (r + x/r) / 2 is close to r. At this point we're just improving the approximation by small amounts. These iterations are not really very dependent on the magnitude of x.

Here's the succession for x = 1000000 (10x the above):

```<code>1 500000
2 250001
3 125002
4 62505
5 31261
6 15646
7 7855
8 3991
9 2121
10 1296
11 1034
12 1001
13 1000
14 1000
</code>```

This time there are 10 iterations in Phase 1, then we again have 4 iterations in Phase 2.

The complexity of the algorithm is dominated by Phase 1, which is logarithmic because it's approximately dividing by 2 each time.

Title: A while loop time complexity

Body:
I'm interested in determining the big O time complexity of the following:

def f(x):
    r = x / 2
    d = 1e-10
    while abs(x - r**2) > d:
        r = (r + x/r) / 2
    return r

I believe this is O(log n). To arrive at this, I merely collected empirical data via the timeit module and plotted the results, and saw that a plot that looked logarithmic using the following code:

ns = np.linspace(1, 50_000, 100, dtype=int)
ts = [timeit.timeit('f({})'.format(n),
                    number=100,
                    globals=globals())
      for n in ns]
plt.plot(ns, ts, 'or')

But this seems like a corny way to go about figuring this out. Intuitively, I understand that the body of the while loop involves dividing an expression by 2 some number k times until the while expression is equal to d. This repeated division by 2 gives something like 1/2^k, from which I can see where a log is involved to solve for k. I can't seem to write down a more explicit derivation, though. Any help?

Body:
I believe you're correct that it's O(log n).

Here you can see the successive values of r when x = 100000:

1 50000
2 25001
3 12502
4 6255
5 3136
6 1584
7 823
8 472
9 342
10 317
11 316
12 316

(I've rounded them off because the fractions are not interesting).

What you can see if that it goes through two phases.

Phase 1 is when r is large. During these first few iterations, x/r is tiny compared to r. As a result, r + x/r is close to r, so (r + x/r) / 2 is approximately r/2. You can see this in the first 8 iterations.

Phase 2 is when it gets close to the final result. During the last few iterations, x/r is close to r, so r + x/r is close to 2 * r, so (r + x/r) / 2 is close to r. At this point we're just improving the approximation by small amounts. These iterations are not really very dependent on the magnitude of x.

Here's the succession for x = 1000000 (10x the above):

1 500000
2 250001
3 125002
4 62505
5 31261
6 15646
7 7855
8 3991
9 2121
10 1296
11 1034
12 1001
13 1000
14 1000

This time there are 10 iterations in Phase 1, then we again have 4 iterations in Phase 2.

The complexity of the algorithm is dominated by Phase 1, which is logarithmic because it's approximately dividing by 2 each time.


问:
How to replace backslash with double backslash?
I'm trying to replace backslashes in my string with two backslashes like so:

```<code>str.gsub!("\\", "\\\\")
</code>```

But, it doesn't do anything. I'm confused...

答:
Note that this answer was givin in the contect of ruby 1.9. As ruby
  2.0 has a new regex engine it might not be valid in that context.

This works:

```<code>str.gsub!("\\", "\\\\\\")
str.gsub!("\\", "\\\\\\\\") # also, will always work
</code>```

Edit: Explanation (via http://www.ruby-forum.com/topic/143645 provided by @vache)

Disclaimer: I'm not familiar with the inner workings of ruby's regex engine, any info here is deducted from the article mentioned above.

The basic thing to know is that the replacement string gets evaluated 2 times.

The first time the slashes do their job as escapes in the string, in the second time gsub will search the string for group references.

as @chris-johnsen mentioned, 6 slashes do not alway work. This leads me to believe something like this is happening:

For 6 slashes. 3 slashes are passed to the group reference layer. The trailing slash is interpreted not as an escape character because nothing is coming after it, it is interpreted as a backslash. So finally this layer returns 2 slashes. If anything would be trailing it, the expression will fail, as the third slash will now function as an escape character.

For 8 slashes: 4 slashes are passed to the group reference layer. The four slashes will in turn be reduced to two.

Body:
I'm trying to replace backslashes in my string with two backslashes like so:

str.gsub!("\\", "\\\\")

But, it doesn't do anything. I'm confused...

Body:
Note that this answer was givin in the contect of ruby 1.9. As ruby
  2.0 has a new regex engine it might not be valid in that context.

This works:

str.gsub!("\\", "\\\\\\")
str.gsub!("\\", "\\\\\\\\") # also, will always work

Edit: Explanation (via http://www.ruby-forum.com/topic/143645 provided by @vache)

Disclaimer: I'm not familiar with the inner workings of ruby's regex engine, any info here is deducted from the article mentioned above.

The basic thing to know is that the replacement string gets evaluated 2 times.

The first time the slashes do their job as escapes in the string, in the second time gsub will search the string for group references.

as @chris-johnsen mentioned, 6 slashes do not alway work. This leads me to believe something like this is happening:

For 6 slashes. 3 slashes are passed to the group reference layer. The trailing slash is interpreted not as an escape character because nothing is coming after it, it is interpreted as a backslash. So finally this layer returns 2 slashes. If anything would be trailing it, the expression will fail, as the third slash will now function as an escape character.

For 8 slashes: 4 slashes are passed to the group reference layer. The four slashes will in turn be reduced to two.


问:
how to call and echo a value from my database for a a certain user using username to identify the user
I have a table named users on my database which is suppose to hold balance in BTC and Cash in two of its rows
I have a php file which i have used to echo the value for each user using the user's username to identify the particular user, but for some reason the echoed data contains the whole row for BTC and Cash value instead of identifying the user using the username and displaying only the value in the column for BTC and Cash where it suppose to display the balance on my website.
So what i am basically looking for is the code to call the data from my database and using username to identify user, display the value for the user.

This is the html where it suppose to display the user's balance on my website

```<code><!doctype html>
<html>
<div class="logged-user-w text-center avatar-inline">
        <div class="logged-user-info-w">
    <div class="logged-user-name">
        <a class="text-primary" href="profile.php"><?php echo $_SESSION['username']; ?></a>

    </div>
//BTC balance//
    <div class="logged-user-role">
            <a class="text-grey" href="wallet.php"><?php include('new.php')?></a>
    </div>
//Cash balance//
    <div class="logged-user-role">
            <a class="text-grey" href="cashwallet.php"><?php include('new.php')?></a>
     </div>

</div>
</html>
</code>```

hare is the php (new.php) which is suppose to get the values from my database and echo it

```<code><?php

      // Server credentials
$servername = "";
$username = "";
$password = "";
$dbname = "";

// Creating mysql connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Checking mysql connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

// Writing a mysql query to retrieve data
$sql = "SELECT * FROM users bitcoin, cash";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // Show each data returned by mysql
  while($row = $result->fetch_assoc()) {
?>

    <!-- USING HTML HERE : Here I am using php within html tags -->
    <p> <?php echo $row["bitcoin"]; ?></p>
    <p> <?php echo $row["cash"]; ?></p>
<?php
  }
} else {
  echo "0 results";
}

// Closing mysql connection
$conn->close();
?>
</code>```

答:
Your sql query is wrong change it to

```<code>$sql = "SELECT * FROM users";
</code>```

Or if you just want the fields like bitcoin and cash you may write a query as

```<code>$sql = "SELECT bitcoin,cash FROM users";
</code>```

And then echo the respective variables.

Body:
I have a table named users on my database which is suppose to hold balance in BTC and Cash in two of its rows
I have a php file which i have used to echo the value for each user using the user's username to identify the particular user, but for some reason the echoed data contains the whole row for BTC and Cash value instead of identifying the user using the username and displaying only the value in the column for BTC and Cash where it suppose to display the balance on my website.
So what i am basically looking for is the code to call the data from my database and using username to identify user, display the value for the user.

This is the html where it suppose to display the user's balance on my website

<!doctype html>

        <?php echo $_SESSION['username']; ?>

//BTC balance//

            <?php include('new.php')?>

//Cash balance//

            <?php include('new.php')?>

hare is the php (new.php) which is suppose to get the values from my database and echo it

<?php

      // Server credentials
$servername = "";
$username = "";
$password = "";
$dbname = "";

// Creating mysql connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Checking mysql connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

// Writing a mysql query to retrieve data
$sql = "SELECT * FROM users bitcoin, cash";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // Show each data returned by mysql
  while($row = $result->fetch_assoc()) {
?>

 <?php echo $row["bitcoin"]; ?>

 <?php echo $row["cash"]; ?>

<?php
  }
} else {
  echo "0 results";
}

// Closing mysql connection
$conn->close();
?>

Body:
Your sql query is wrong change it to

$sql = "SELECT * FROM users";

Or if you just want the fields like bitcoin and cash you may write a query as

$sql = "SELECT bitcoin,cash FROM users";

And then echo the respective variables.


问:
Google Colab is so slow while reading images from Google Drive
I have my own dataset for a deep learning project. I uploaded that into Google Drive and linked it to a Colab page. But Colab could read only 2-3 images in a second, where my computer can dozens of them. (I used imread to read images.)

There is no speed problem with model compiling process of keras, but only with reading images from Google Drive. Does anybody know a solution? Someone suffered of this problem too, but it's still unsolved: Google Colab very slow reading data (images) from Google Drive (I know this is kind of a duplication of the question in the link, but I reposted it because it is still unsolved. I hope this is not a violation of Stack Overflow rules.)

Edit: The code piece that I use for reading images:

```<code>def getDataset(path, classes, pixel=32, rate=0.8):
    X = []
    Y = []

    i = 0
    # getting images:
    for root, _, files in os.walk(path):
        for file in files:
            imagePath = os.path.join(root, file)
            className = os.path.basename(root)

            try:
                image = Image.open(imagePath)
                image = np.asarray(image)
                image = np.array(Image.fromarray(image.astype('uint8')).resize((pixel, pixel)))
                image = image if len(image.shape) == 3 else color.gray2rgb(image)
                X.append(image)
                Y.append(classes[className])
            except:
                print(file, "could not be opened")

    X = np.asarray(X, dtype=np.float32)
    Y = np.asarray(Y, dtype=np.int16).reshape(1, -1)

    return shuffleDataset(X, Y, rate)
</code>```

答: Upload zip files to the drive. After transferring to colab unzip them. File copy overhead is cumbersome therefore you shouldn't copy masses of files instead copy a single zip and unzip.</p>

Body:
I have my own dataset for a deep learning project. I uploaded that into Google Drive and linked it to a Colab page. But Colab could read only 2-3 images in a second, where my computer can dozens of them. (I used imread to read images.)

There is no speed problem with model compiling process of keras, but only with reading images from Google Drive. Does anybody know a solution? Someone suffered of this problem too, but it's still unsolved: Google Colab very slow reading data (images) from Google Drive (I know this is kind of a duplication of the question in the link, but I reposted it because it is still unsolved. I hope this is not a violation of Stack Overflow rules.)

Edit: The code piece that I use for reading images:

def getDataset(path, classes, pixel=32, rate=0.8):
    X = []
    Y = []

    i = 0
    # getting images:
    for root, _, files in os.walk(path):
        for file in files:
            imagePath = os.path.join(root, file)
            className = os.path.basename(root)

            try:
                image = Image.open(imagePath)
                image = np.asarray(image)
                image = np.array(Image.fromarray(image.astype('uint8')).resize((pixel, pixel)))
                image = image if len(image.shape) == 3 else color.gray2rgb(image)
                X.append(image)
                Y.append(classes[className])
            except:
                print(file, "could not be opened")

    X = np.asarray(X, dtype=np.float32)
    Y = np.asarray(Y, dtype=np.int16).reshape(1, -1)

    return shuffleDataset(X, Y, rate)


答:
This is Heron's (Or Babylonian) method for calculating the square root of a number. https://en.wikipedia.org/wiki/Methods_of_computing_square_roots

Big O notation for this requires a numerical analysis approach. For more details on the analysis you can check the wikipedia page listed or look for Heron's error convergence or fixed point iteration. (or look here https://mathcirclesofchicago.org/wp-content/uploads/2015/08/johnson.pdf)

Broad-strokes, if we can write the error e_n = (x-r_n**2) in terms of itself to where e_n = (e_n**2)/(2*(e_n+1))

Then we can see that e_n+1 <= min{(e_n**2)/2,e_n/2} so we have the error decrease quadratically. With the degrees of accuracy effectively doubling each iteration.

Whats different between this analysis and Big-O, is that the time it takes does NOT depend on the size of the input, but instead of the wanted accuracy. So in terms of input, this while loop is O(1) because its number of iterations is bounded by the accuracy not the input.

In terms of accuracy the error is bounded by above by e_n < 2**(-n) so we would need to find -n such that 2**(-n) < d. So log_2(d) = b such that 2^b = d. Assuming d < 2, then n = floor(log_2(d)) would work. So in terms of d, it is O(log(d)).

EDIT: Some more info on error analysis of fixed point iteration http://www.maths.lth.se/na/courses/FMN050/media/material/part3_1.pdf

Title: A while loop time complexity


问:
filtering out values in a multi-dimensional array in php
I have a situation where I have two arrays and I use one array(A) to determine which elements of the second array(B) I need to use. Since I will be combing B multiple times and each time I will not need the previously used elements I have written a function that I call sift_array.

```<code>function sift_array(&$array, $key, $value){
   foreach($array as $element){
      if($element[$key] == $value){$temp[] = $element;}
      else{$temp2[] = $element;}
   }
   $array = $temp2;
   return $temp;
}</code>```

I am curious if there is a faster way to perform this function.

Here is an example of how I use this function.

In my web app users can set up a 'bot' to perform an action so many times while they are away/logged off. I have a program that will run periodically to operate the bots. The bots can not run the same action if it was the last bot to do so.

The bots array has 3 key elements     : account_id, action_id, job_id
The job array has 1 key element      : job_id.

I pass in the bots array then for the $key I pass 'job_id' and for the $value I pass the job id I am working with, say job 1.

```<code>$runTheseBots = sift_array($bots, 'job_id', 1);
executeBots($runTheseBots);</code>```

This will have a faster execution time because all the bots that deal with job 1 are already handled.

```<code>$runTheseBots = sift_array($bots, 'job_id', 2);
executeBots($runTheseBots);</code>```

答:
I'd go with a fold approach:

```<code>function sift($array, $key, $value) {
    return array_reduce($array, function ($a, $elem) use ($key, $value) {
        $elem[$key] == $value ? $a[0] = $elem : $a[1] = $elem;
    }, array());
}

$bots = sift($array, 'job_id', 2);
executeBots($bots[0]);
</code>```

This IMO mostly improves the "interface" by eliminating the pass-by-reference + return and returning a "grouped" array. PHP may be able to optimize this better internally and hence it may be more efficient, but I don't know in how far that is really the case. Might be worth a try though.

OK, a bit of testing shows that it's actually a lot slower. But, if you're only filtering the bots you're interested in and discard the rest, array_filter can give a boost. If you're discarding anyway though, completely unsetting the unneeded values is the fastest by far:

```<code>$bots = array();
for ($i = 0; $i < 100000; $i++) {
    $bots = array('job_id' => mt_rand(1, 100));
}

function sift_reduce($array, $key, $value) {
    return array_reduce($array, function ($a, $elem) use ($key, $value) {
        $elem[$key] == $value ? $a[0] = $elem : $a[1] = $elem;
    }, array());
}

function sift_filter($array, $key, $value) {
    return array_filter($array, function ($elem) use ($key, $value) { return $elem[$key] == $value; });
}

function sift_unset(&$array, $key, $value) {
    foreach ($array as $k => $elem) {
        if ($elem[$key] != $value) {
            unset($array[$k]);
        }
    }
}

function sift_array(&$array, $key, $value){
   foreach($array as $element){
      if($element[$key] == $value){$temp[] = $element;}
      else{$temp2[] = $element;}
   }
   $array = $temp2;
   return $temp;
}

$start = microtime(true);
$sifted = sift_reduce($bots, 'job_id', 5);
echo "reduce:  " . (microtime(true) - $start) * 1000 . "\n";

$start = microtime(true);
$sifted = sift_filter($bots, 'job_id', 5);
echo "filter:  " . (microtime(true) - $start) * 1000 . "\n";

$botsCopy = $bots;
$start = microtime(true);
sift_unset($botsCopy, 'job_id', 5);
echo "unset:   " . (microtime(true) - $start) * 1000 . "\n";

$start = microtime(true);
$sifted = sift_array($bots, 'job_id', 5);
echo "array:   " . (microtime(true) - $start) * 1000 . "\n";
</code>```

Gives:

```<code>reduce:  0.072002410888672
filter:  0.013828277587891
unset:   0.0088214874267578
array:   0.016927719116211
</code>```

Body:
I have a situation where I have two arrays and I use one array(A) to determine which elements of the second array(B) I need to use. Since I will be combing B multiple times and each time I will not need the previously used elements I have written a function that I call sift_array.

function sift_array(&$array, $key, $value){
   foreach($array as $element){
      if($element[$key] == $value){$temp[] = $element;}
      else{$temp2[] = $element;}
   }
   $array = $temp2;
   return $temp;
}

I am curious if there is a faster way to perform this function.

Here is an example of how I use this function.

In my web app users can set up a 'bot' to perform an action so many times while they are away/logged off. I have a program that will run periodically to operate the bots. The bots can not run the same action if it was the last bot to do so.

The bots array has 3 key elements     : account_id, action_id, job_id
The job array has 1 key element      : job_id.

I pass in the bots array then for the $key I pass 'job_id' and for the $value I pass the job id I am working with, say job 1.

$runTheseBots = sift_array($bots, 'job_id', 1);
executeBots($runTheseBots);

This will have a faster execution time because all the bots that deal with job 1 are already handled.

$runTheseBots = sift_array($bots, 'job_id', 2);
executeBots($runTheseBots);

Body:
I'd go with a fold approach:

function sift($array, $key, $value) {
    return array_reduce($array, function ($a, $elem) use ($key, $value) {
        $elem[$key] == $value ? $a[0] = $elem : $a[1] = $elem;
    }, array());
}

$bots = sift($array, 'job_id', 2);
executeBots($bots[0]);

This IMO mostly improves the "interface" by eliminating the pass-by-reference + return and returning a "grouped" array. PHP may be able to optimize this better internally and hence it may be more efficient, but I don't know in how far that is really the case. Might be worth a try though.

OK, a bit of testing shows that it's actually a lot slower. But, if you're only filtering the bots you're interested in and discard the rest, array_filter can give a boost. If you're discarding anyway though, completely unsetting the unneeded values is the fastest by far:

$bots = array();
for ($i = 0; $i < 100000; $i++) {
    $bots = array('job_id' => mt_rand(1, 100));
}

function sift_reduce($array, $key, $value) {
    return array_reduce($array, function ($a, $elem) use ($key, $value) {
        $elem[$key] == $value ? $a[0] = $elem : $a[1] = $elem;
    }, array());
}

function sift_filter($array, $key, $value) {
    return array_filter($array, function ($elem) use ($key, $value) { return $elem[$key] == $value; });
}

function sift_unset(&$array, $key, $value) {
    foreach ($array as $k => $elem) {
        if ($elem[$key] != $value) {
            unset($array[$k]);
        }
    }
}

function sift_array(&$array, $key, $value){
   foreach($array as $element){
      if($element[$key] == $value){$temp[] = $element;}
      else{$temp2[] = $element;}
   }
   $array = $temp2;
   return $temp;
}

$start = microtime(true);
$sifted = sift_reduce($bots, 'job_id', 5);
echo "reduce:  " . (microtime(true) - $start) * 1000 . "\n";

$start = microtime(true);
$sifted = sift_filter($bots, 'job_id', 5);
echo "filter:  " . (microtime(true) - $start) * 1000 . "\n";

$botsCopy = $bots;
$start = microtime(true);
sift_unset($botsCopy, 'job_id', 5);
echo "unset:   " . (microtime(true) - $start) * 1000 . "\n";

$start = microtime(true);
$sifted = sift_array($bots, 'job_id', 5);
echo "array:   " . (microtime(true) - $start) * 1000 . "\n";

Gives:

reduce:  0.072002410888672
filter:  0.013828277587891
unset:   0.0088214874267578
array:   0.016927719116211


问:
Swift: Core Data model relationships to mirror API view model
I am trying to create a core data model but as a Core Data newbie, I'm unclear as to how to use relationships for my purpose.

Essentially I need to store an object that looks something like this class (there are more values required but this is the structure), the purpose being that the below is essentially the model I used when fetching data from a specific API, but I am trying to implement offline functionality so need essentially mirror this model in core data):

```<code>class SheetModel {

     uplift: String
     date: String
     time: String
     density: String
     discrepancy: String
     id: String

  Mass {
      unit:  String
      display: String
      unitName: String
   }
  Density {
      unit:  String
      display: String
      unitName: String
   }
  Volume {
      unit:  String
      display: String
      unitName: String
   }
 }
</code>```

I am unclear how to create a data model to allow me to store something like the above. My (poor) attempt is as follows:

I know that the relationships here are totally wrong but can't figure out what I should be doing here. So obviously the Volume, Mass, Density entities all should belong to the same NSManagedObject that is storing the uplift, date etc. attributes.

答:
You are misunderstanding what "parent entity" means in CoreData! That's how you specify inheritance not containment. So if you had a Circle entity, it would be appropriate for its parent entity to be a more generic Shape entity.

You need to clear out those parent entity settings and instead add "relationships" to your entities. Each of your mass/density/volume entities should have a relationship where the destination is StoredRefuelSheet the delete rule is Nullify and the type is To One. That means you can't share a mass between two sheets and you can delete a mass without deleting its owning sheet. You should also set up an inverse relationship, so the sheet would have a mass relationship with destination Mass, delete rule Cascade, and type To One. That means if you delete a sheet its contained mass is also deleted.

After that, you do actually have an opportunity to use parent entities: your mass/density/volume entities all have the same fields! That would be a good use case for an abstract parent entity that specifies the common fields so you don't have to duplicate them.

Body:
I am trying to create a core data model but as a Core Data newbie, I'm unclear as to how to use relationships for my purpose.

Essentially I need to store an object that looks something like this class (there are more values required but this is the structure), the purpose being that the below is essentially the model I used when fetching data from a specific API, but I am trying to implement offline functionality so need essentially mirror this model in core data):

class SheetModel {

     uplift: String
     date: String
     time: String
     density: String
     discrepancy: String
     id: String

  Mass {
      unit:  String
      display: String
      unitName: String
   }
  Density {
      unit:  String
      display: String
      unitName: String
   }
  Volume {
      unit:  String
      display: String
      unitName: String
   }
 }

I am unclear how to create a data model to allow me to store something like the above. My (poor) attempt is as follows:

I know that the relationships here are totally wrong but can't figure out what I should be doing here. So obviously the Volume, Mass, Density entities all should belong to the same NSManagedObject that is storing the uplift, date etc. attributes.


问:
How to change play_hosts from playbook
I have a file with two playbooks. Inventory is generated dynamically and there is no possibility to change it before starting playbook.

Run with command:

```<code>ansible-playbook -b adapter.yml --limit=host_group
</code>```

adapter.yml

```<code>- name: Prepare stage
  hosts: all
  # The problem is that the inventory contains hosts in the format "x.x.x.x" ie physical address.
  # I need to run a third-party role.
  # But, it needs hosts in the format "instance-alias", that is, the name of the instance.

  tasks:
  # for this I create a new host group
  - name: Add host in new format
    add_host:
      name: "{{ item.alias }}"
      host: "{{ item.ansible_host }}"
      groups: new_format_hosts
    with_items: "{{ groups.all }}"

 # I create a new play host group that matches the previous one in a new format
  - name: Compose new play_hosts group
    add_host:
      name: "{{ item.alias }}"
      groups: new_play_hosts
    when: item.ansible_host in play_hosts
    with_items: "{{ groups.all }}"

- name: Management stage
  hosts: new_format_hosts
  # in this playbook I want to change the composition
  # of the target hosts and launch an external role
  vars:
    hostvars: "{{ hostvars }}"
    play_hosts: "{{ groups.new_play_hosts }}" # THIS DONT WORK

  - name: Run external role
    import_role:
      name: role_name
      tasks_from: file_name

</code>```

But I can’t change play_hosts so that the launched role uses only new hosts.

How to fix it?

Title: How to change play_hosts from playbook

Body:
I have a file with two playbooks. Inventory is generated dynamically and there is no possibility to change it before starting playbook.

Run with command:

ansible-playbook -b adapter.yml --limit=host_group

adapter.yml

- name: Prepare stage
  hosts: all
  # The problem is that the inventory contains hosts in the format "x.x.x.x" ie physical address.
  # I need to run a third-party role.
  # But, it needs hosts in the format "instance-alias", that is, the name of the instance.

  tasks:
  # for this I create a new host group
  - name: Add host in new format
    add_host:
      name: "{{ item.alias }}"
      host: "{{ item.ansible_host }}"
      groups: new_format_hosts
    with_items: "{{ groups.all }}"

 # I create a new play host group that matches the previous one in a new format
  - name: Compose new play_hosts group
    add_host:
      name: "{{ item.alias }}"
      groups: new_play_hosts
    when: item.ansible_host in play_hosts
    with_items: "{{ groups.all }}"

- name: Management stage
  hosts: new_format_hosts
  # in this playbook I want to change the composition
  # of the target hosts and launch an external role
  vars:
    hostvars: "{{ hostvars }}"
    play_hosts: "{{ groups.new_play_hosts }}" # THIS DONT WORK

  - name: Run external role
    import_role:
      name: role_name
      tasks_from: file_name

But I can’t change play_hosts so that the launched role uses only new hosts.

How to fix it?

Body:
This works for me:

- name: Prepare stage
  hosts: localhost

  tasks:
  - name: Show hostavers
    debug:
      msg: "{{ hostvars[item]['ansible_host'] }}"
    with_items: "{{ groups.all }}"

  # for this I create a new host group
  - name: Add host in new format
    add_host:
      name: "{{ hostvars[item].alias }}"
      ansible_host: "{{ hostvars[item].ansible_host }}"
      groups: new_format_hosts
    with_items: "{{ groups.all }}"

- name: Management stage
  hosts: new_format_hosts
  tasks:
  - name: Ping New Format Hosts
    ping:

  - name: Show ansible_host for each host
    debug:
      var: ansible_host

  - name: Show playhosts
    debug:
      var: play_hosts
    delegate_to: localhost
    run_once: yes

This assumes, of course, that alias and ansible_host are set for all the hosts.

The hosts were:

AnsibleTower ansible_host=192.168.124.8 alias=fred
192.168.124.111 ansible_host=192.168.124.111 alias=barney
jaxsatB ansible_host=192.168.124.111 alias=wilma

The relevant output of the playbook was:

PLAY [Management stage] *****************************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************************
Friday 29 May 2020  18:09:26 -0400 (0:00:00.057)       0:00:01.332 ************
ok: [fred]
ok: [barney]
ok: [wilma]

TASK [New Format Hosts] *****************************************************************************************************************************************************************
Friday 29 May 2020  18:09:30 -0400 (0:00:04.308)       0:00:05.641 ************
ok: [barney]
ok: [wilma]
ok: [fred]

TASK [Show ansible_host] ****************************************************************************************************************************************************************
Friday 29 May 2020  18:09:30 -0400 (0:00:00.450)       0:00:06.091 ************
ok: [fred] => {
    "ansible_host": "192.168.124.8"
}
ok: [barney] => {
    "ansible_host": "192.168.124.111"
}
ok: [wilma] => {
    "ansible_host": "192.168.124.111"
}

TASK [Show playhosts] *******************************************************************************************************************************************************************
Friday 29 May 2020  18:09:30 -0400 (0:00:00.109)       0:00:06.200 ************
ok: [fred -> localhost] => {
    "play_hosts": [
        "fred",
        "barney",
        "wilma"
    ]
}

PLAY RECAP ******************************************************************************************************************************************************************************
barney                     : ok=3    changed=0    unreachable=0    failed=0
fred                       : ok=4    changed=0    unreachable=0    failed=0
localhost                  : ok=3    changed=1    unreachable=0    failed=0
wilma                      : ok=3    changed=0    unreachable=0    failed=0

Friday 29 May 2020  18:09:30 -0400 (0:00:00.030)       0:00:06.231 ************

===============================================================================

You do not need to set the play_hosts variable.  That is set by the line hosts: new_format_hosts in the second play.


问:
bladeRF simulink no device available error
I am connecting the bladeRF x115 to simulink with Matlab 2016a on a windows 10 PC.

I have followed the getting started guide on github:
www.nuand.com/bladeRF-doc/guides/bladeRF_windows_installer

Then I made a simple code as in the picture shown below:

I can simulate it only one time because when I tried to simulate it again I got the following error:

MATLAB System block 'testlinking/MATLAB System' error occurred when invoking 'setupImpl' method of 'bladeRF_Simulink'. The error was thrown from '

```<code>'C:\Program Files\bladeRF\matlab\bladeRF.m' at line 116
'C:\Program Files\bladeRF\matlab\bladeRF.m' at line 398
'C:\Program Files\bladeRF\matlab\bladeRF_Simulink.m' at line 364'.
Caused by:
libbladeRF error (-7) in bladeRF_open(): No devices available
Component:Simulink | Category:Block error
</code>```

The reason for this error is because the led 2 is still blinking(device is in use). But it continues blinking even though I have closed matlab and simulink, and I don't know why?

答:
Have a look at:
https://www.mathworks.com/matlabcentral/fileexchange/74591-communications-toolbox-support-package-for-bladerf-2-0?s_tid=mwa_osa_a
„Communications Toolbox Support Package for BladeRF 2.0“

Download the zip-file and navigate matlab to the directory bladerf.

This worked for me. The simulink block releases the bladerf-board so the Simulinkmodel can be stopped and restarted without problems/errors.

Body:
I am connecting the bladeRF x115 to simulink with Matlab 2016a on a windows 10 PC.

I have followed the getting started guide on github:
www.nuand.com/bladeRF-doc/guides/bladeRF_windows_installer

Then I made a simple code as in the picture shown below:

I can simulate it only one time because when I tried to simulate it again I got the following error:

MATLAB System block 'testlinking/MATLAB System' error occurred when invoking 'setupImpl' method of 'bladeRF_Simulink'. The error was thrown from '

'C:\Program Files\bladeRF\matlab\bladeRF.m' at line 116
'C:\Program Files\bladeRF\matlab\bladeRF.m' at line 398
'C:\Program Files\bladeRF\matlab\bladeRF_Simulink.m' at line 364'.
Caused by:
libbladeRF error (-7) in bladeRF_open(): No devices available
Component:Simulink | Category:Block error

The reason for this error is because the led 2 is still blinking(device is in use). But it continues blinking even though I have closed matlab and simulink, and I don't know why?


问:
JavaScript manipulating DOM (onclick event)
I have this code that is supposed to add a div that is a red square every time I click the button. It happens that it adds the div I want 1 click yes and 1 click no. here's the code:

var btnElement = document.querySelector('button');
console.log(btnElement);
btnElement.onclick = function() {
  var index = 0;
  var squareElementCreator = document.createElement('div');
  squareElementCreator.setAttribute('class', 'box');

  var containElement = document.querySelector('#app');
  containElement.appendChild(squareElementCreator);

  var squareElement = document.querySelectorAll('.box');
  console.log(squareElement[index]);
  squareElement[index].style.width = "100px";
  squareElement[index].style.height = "100px";
  squareElement[index].style.backgroundColor = '#f00';
  squareElement[index].style.margin = '10px';
  //console.log(squareElement);

  var container = document.querySelector('#app');
  container.appendChild(squareElement[index]);
  index++;
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><div id="app">
  <input type="text" name="nome" id="nome" />
  <button class="botao">Adicionar</button>
</div></code>```

答:
It seems like you are trying to create a for loop instead of your function? There is some logic issues as well.  Here are some pointers that might help you understand the code below:

Every variable declared within a function, will only be declared when that function is called.  After the function is called, the variables are destroyed and in most cases you cannot access them again.

You seem to be trying to select all your elements on the dom within the same function?  You wont need to do that if you just modify the style property before appending to the dom.

Take some time to do some research about the fundamentals, it will really help you put some quality code together.

     var btnElement = document.querySelector('button');
     var containElement = document.querySelector('#app');
     btnElement.addEventListener('click', function () {

     var squareElement = document.createElement('div')
         squareElement.className = 'box'
         squareElement.style.width = "100px";
         squareElement.style.height = "100px";
         squareElement.style.backgroundColor = '#f00';
         squareElement.style.margin = '10px';
     containElement.appendChild(squareElement)

    })
```

Body:
I have this code that is supposed to add a div that is a red square every time I click the button. It happens that it adds the div I want 1 click yes and 1 click no. here's the code:

var btnElement = document.querySelector('button');
console.log(btnElement);
btnElement.onclick = function() {
  var index = 0;
  var squareElementCreator = document.createElement('div');
  squareElementCreator.setAttribute('class', 'box');

  var containElement = document.querySelector('#app');
  containElement.appendChild(squareElementCreator);

  var squareElement = document.querySelectorAll('.box');
  console.log(squareElement[index]);
  squareElement[index].style.width = "100px";
  squareElement[index].style.height = "100px";
  squareElement[index].style.backgroundColor = '#f00';
  squareElement[index].style.margin = '10px';
  //console.log(squareElement);

  var container = document.querySelector('#app');
  container.appendChild(squareElement[index]);
  index++;
}

  <input type="text" name="nome" id="nome" />
  <button class="botao">Adicionar</button>


问:
Server.Htmlencode performance versus Regex.Replace versus String.Replace
Which is fastest in an ASP.NET app? Trying to catch all manners of naughty XSS/CSRF and the like.

Server.HtmlEncode before displaying, String.Replace calls (one after another), or a complicated regex followed by a Regex.Replace?

Thanks for any info

答:
You would use Server.HtmlEncode... it's not just the "speed", but the fact that that is what the technology is for. It's likely that you're going to forget something in your .Replace().Replace().Replace (which is also slower).

A bit of explaination

Keep in mind that "Server.HtmlEncode" will make one streaming pass through your string, whereas .Replace().Replace().Replace() will make multiple passes to do the same thing.

Regarding the Regex - you're going to have the overhead the Regex being parsed and compiled in the first place.


问:
Ionic 4 Not support Events?
Im using Event fucntion to publish some data in app. But its not working in ionic 4. I need to know ionic 4 support Events or not?

import { Events } from '@ionic-angular';
// Module not found: Error: Can't resolve '@ionic-angular'
```

答:
The problem was in the version. When I updated to the latest patch of version 4, it was working.</p>

```<code>npm i @ionic/angular@4.11.10
</code>```

Title: Ionic 4 Not support Events?

Body:
The problem was in the version. When I updated to the latest patch of version 4, it was working.

npm i @ionic/angular@4.11.10


问:
C++: Stroustrup iterator_traits example not compiling?
I'm trying to compile the code from Stroustrups C++ 4th Ed Page 124 and 125, section Iterator Traits.  Unfortunately, the compilation results in numerous errors which look like they are in the template system.  Does anyone know whats wrong with this code?

Thanks

```<code>#include <iostream>
#include <iterator>
#include <algorithm>
#include <forward_list>
#include <vector>
using namespace std;

template<typename Ran>
void sort_helper(Ran beg, Ran end, random_access_iterator_tag)
{
    sort(beg, end);
}

template<typename For>
void sort_helper(For beg, For end, forward_iterator_tag)
{
    vector<decltype(*beg)> v {beg, end};
    sort(v.begin(), v.end());
    copy(v.begin(), v.end(), beg);
}

template<typename C>
using Iterator_type = typename C::iterator;

template<typename Iter>
using Iterator_category = typename std::iterator_traits<Iter>::iterator_category;

template<typename C> void sort(C& c)
{
    using Iter = Iterator_type<C>; // ex. vector<int>::iterator
    sort_helper(c.begin(), c.end(), Iterator_category<Iter>{});
}

int main(int argc, char *argv[])
{
    forward_list<int> fl = {2, 1, 0};
    vector<int> v = {2, 1, 0};

    sort(fl); // this line causes compilation error
    sort(v);

    return 0;
}
</code>```

Errors:

```<code>In file included from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/x86_64-linux-gnu/bits/c++allocator.h:33,
                 from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/allocator.h:46,
                 from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/string:41,
                 from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/locale_classes.h:40,
                 from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/ios_base.h:41,
                 from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/ios:42,
                 from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/ostream:38,
                 from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/iostream:39,
                 from <source>:1:
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/ext/new_allocator.h: In instantiation of 'class __gnu_cxx::new_allocator<int&>':
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/allocator.h:116:11:   required from 'class std::allocator<int&>'
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/stl_vector.h:87:21:   required from 'struct std::_Vector_base<int&, std::allocator<int&> >'
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/stl_vector.h:389:11:   required from 'class std::vector<int&, std::allocator<int&> >'
<source>:17:28:   required from 'void sort_helper(For, For, std::forward_iterator_tag) [with For = std::_Fwd_list_iterator<int>]'
<source>:31:16:   required from 'void sort(C&) [with C = std::forward_list<int>]'
<source>:39:12:   required from here
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/ext/new_allocator.h:62:26: error: forming pointer to reference type 'int&'
   62 |       typedef _Tp*       pointer;
      |                          ^~~~~~~
... and more ...
</code>```

答:
Within the function sort_helper

```<code>template<typename For>
void sort_helper(For beg, For end, forward_iterator_tag)
{
    vector<decltype(*beg)> v {beg, end};
    sort(v.begin(), v.end());
    copy(v.begin(), v.end(), beg);
}
</code>```

In this statement

```<code>vector<decltype(*beg)> v {beg, end};
</code>```

the template argument is deduced like a referenced type. So in this statement there is declared a vector of references that is invalid.

Change this statement to

```<code>vector<std::decay_t<decltype( *beg )>> v {beg, end};
</code>```

You will need to include the header <type_traits>.

Or you could explicitly to specify removing of reference like

```<code>vector<typename std::remove_reference<decltype( *beg )>::type> v {beg, end};
</code>```

or

```<code>vector<typename std::remove_reference_t<decltype( *beg )>> v {beg, end};
</code>```

Body:
I'm trying to compile the code from Stroustrups C++ 4th Ed Page 124 and 125, section Iterator Traits.  Unfortunately, the compilation results in numerous errors which look like they are in the template system.  Does anyone know whats wrong with this code?

Thanks

#include <iostream>
#include <iterator>
#include <algorithm>
#include <forward_list>
#include <vector>
using namespace std;

template<typename Ran>
void sort_helper(Ran beg, Ran end, random_access_iterator_tag)
{
    sort(beg, end);
}

template<typename For>
void sort_helper(For beg, For end, forward_iterator_tag)
{
    vector<decltype(*beg)> v {beg, end};
    sort(v.begin(), v.end());
    copy(v.begin(), v.end(), beg);
}

template<typename C>
using Iterator_type = typename C::iterator;

template<typename Iter>
using Iterator_category = typename std::iterator_traits<Iter>::iterator_category;

template<typename C> void sort(C& c)
{
    using Iter = Iterator_type<C>; // ex. vector<int>::iterator
    sort_helper(c.begin(), c.end(), Iterator_category<Iter>{});
}

int main(int argc, char *argv[])
{
    forward_list<int> fl = {2, 1, 0};
    vector<int> v = {2, 1, 0};

    sort(fl); // this line causes compilation error
    sort(v);

    return 0;
}

Errors:

In file included from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/x86_64-linux-gnu/bits/c++allocator.h:33,
                 from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/allocator.h:46,
                 from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/string:41,
                 from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/locale_classes.h:40,
                 from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/ios_base.h:41,
                 from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/ios:42,
                 from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/ostream:38,
                 from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/iostream:39,
                 from <source>:1:
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/ext/new_allocator.h: In instantiation of 'class __gnu_cxx::new_allocator<int&>':
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/allocator.h:116:11:   required from 'class std::allocator<int&>'
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/stl_vector.h:87:21:   required from 'struct std::_Vector_base<int&, std::allocator<int&> >'
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/stl_vector.h:389:11:   required from 'class std::vector<int&, std::allocator<int&> >'
<source>:17:28:   required from 'void sort_helper(For, For, std::forward_iterator_tag) [with For = std::_Fwd_list_iterator<int>]'
<source>:31:16:   required from 'void sort(C&) [with C = std::forward_list<int>]'
<source>:39:12:   required from here
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/ext/new_allocator.h:62:26: error: forming pointer to reference type 'int&'
   62 |       typedef _Tp*       pointer;
      |                          ^~~~~~~
... and more ...

Body:
Within the function sort_helper

template<typename For>
void sort_helper(For beg, For end, forward_iterator_tag)
{
    vector<decltype(*beg)> v {beg, end};
    sort(v.begin(), v.end());
    copy(v.begin(), v.end(), beg);
}

In this statement

vector<decltype(*beg)> v {beg, end};

the template argument is deduced like a referenced type. So in this statement there is declared a vector of references that is invalid.

Change this statement to

vector<std::decay_t<decltype( *beg )>> v {beg, end};

You will need to include the header <type_traits>.

Or you could explicitly to specify removing of reference like

vector<typename std::remove_reference<decltype( *beg )>::type> v {beg, end};

or

vector<typename std::remove_reference_t<decltype( *beg )>> v {beg, end};


问:
How to create a record that contains a user in Django test setUp
I have this model in my Django application:

class ClubSession(models.Model):
    location = models.CharField(max_length=200)
    coach = models.ForeignKey('auth.User', on_delete=models.CASCADE)
    date = models.DateTimeField(default=now)
    details = models.TextField()

    def __str__(self):
        return self.location
```

<p>This view implements it:</p>

<pre class="lang-py prettyprint-override"><code>class SessionListView(ListView):
    model = ClubSession
    template_name = 'club_sessions.html'
    context_object_name = 'all_club_sessions_list'
</code>```

I'm trying to test the view. My test class has a setUp which creates a record:

def setUp(self):
    ClubSession.objects.create(location='test location',
                               coach=User(id=1),
                               date='2020-06-01 18:30',
                               details='this is another test')
```

<p>When I run my test I get this error:</p>

```IntegrityError: The row in table 'club_sessions_clubsession' with primary key '1' has an invalid foreign key: club_sessions_clubsession.coach_id contains a value '1' that does not have a corresponding value in auth_user.id.
```

A user with id 1 exists so how do I get this to work? I've tried adding the username but that didn't work either.

答:
I would strongly advise not to use primary keys, especially since dispatching primary keys is the responsibility of the database, and thus that can differ between sessions.

Furthermore tests run on an independent database, so the data stored in the database you use in development or on production is not used.

Probably it is better to first create a user, for example:

```<code>from django.contrib.auth.models import User

# …

def setUp(self):
    user = User.objects.<b>create(</b>username='foo'<b>)</b>
    ClubSession.objects.create(
        location='test location',
        coach=<b>user</b>,
        date='2020-06-01 18:30',
        details='this is another test'
    )</code>```

Body:
I have this model in my Django application:

class ClubSession(models.Model):
    location = models.CharField(max_length=200)
    coach = models.ForeignKey('auth.User', on_delete=models.CASCADE)
    date = models.DateTimeField(default=now)
    details = models.TextField()

    def __str__(self):
        return self.location

This view implements it:

class SessionListView(ListView):
    model = ClubSession
    template_name = 'club_sessions.html'
    context_object_name = 'all_club_sessions_list'

I'm trying to test the view. My test class has a setUp which creates a record:

def setUp(self):
    ClubSession.objects.create(location='test location',
                               coach=User(id=1),
                               date='2020-06-01 18:30',
                               details='this is another test')

When I run my test I get this error:

IntegrityError: The row in table 'club_sessions_clubsession' with primary key '1' has an invalid foreign key: club_sessions_clubsession.coach_id contains a value '1' that does not have a corresponding value in auth_user.id.

A user with id 1 exists so how do I get this to work? I've tried adding the username but that didn't work either.

Body:
I would strongly advise not to use primary keys, especially since dispatching primary keys is the responsibility of the database, and thus that can differ between sessions.

Furthermore tests run on an independent database, so the data stored in the database you use in development or on production is not used.

Probably it is better to first create a user, for example:

from django.contrib.auth.models import User

# …

def setUp(self):
    user = User.objects.create(username='foo')
    ClubSession.objects.create(
        location='test location',
        coach=user,
        date='2020-06-01 18:30',
        details='this is another test'
    )


问:
Reduce resource usage of this JS program?
I wrote the following JS program, which i'm running from the command line using node.

```<code>//step one: create an array of remaining creatures
var remainingCreatures = [];

//some config variables:
var amt = 1000; //amount of creatues generated
var maxhlt = 1000; //max value for creature health stat
var minhlt = 100; //min value for creature health stat
var maxatk = 100; //max value for creature attack stat
var minatk = 1; //min value for creature attack stat

function remove(target) { //helper function to easily remove creatues from the array
  var index = remainingCreatures.indexOf(target);
if (index > -1) {
  remainingCreatures.splice(index, 1);
}

}

//generate creatures
//each creature has an Attack, Health, and Aggressiveness , as well as Instabillity, the average of the three
//A creature's Instabillity is added to the attack points of its opponent in fights.
for (var i = 0; i < amt; i++) {
  var atkVal = Math.floor((Math.random() * maxatk) + minatk);
  var hltVal = Math.floor((Math.random() * maxhlt) + minhlt);
  var insVal = (atkVal + hltVal) / 2;

  remainingCreatures[i] = {num: i, atk: atkVal, hlt: hltVal, ins: insVal, fts: 0, ihlt: hltVal};
}
console.log(amt + " creatues generated, starting melee...");

function fight(cr1, cr2) { //function to handle fighting, randomly creates creature pairs and has them fight
  function attack(a, b) {
         console.log("[ROUND 1] Creature 1 (#" + a.num + ") is attacking first.");
    b.hlt = b.hlt - (a.atk + b.ins);
    console.log("[HIT] Creature #" + b.num + " health reduced to " + b.hlt);
    if (b.hlt <= 0) {
      console.log("[DEATH] Creature #" + b.num + " Eliminated");
      remove(b);
    } else {
      console.log("[ROUND 2] Creature 2 (#" + b.num + ") is attacking second.");
      a.hlt = a.hlt - (b.atk + a.ins);
      console.log("[HIT] Creature #" + a.num + " health reduced to " + a.hlt);
      if (a.hlt <= 0) {
      console.log("[DEATH] Creature #" + a.num + " Eliminated");
      remove(a);
    }
    }
  }
  console.log("[FIGHT] Pair generated: Creature #" + cr1.num + " and Creature #" + cr2.num);
  cr1.fts++;
  cr2.fts++;
   if (cr1.ins <= cr2.ins) {
    attack(cr1, cr2);
   } else {
    attack(cr2, cr1);
   }
}

for(;true;) {
  if(remainingCreatures.length == 1) break;
  fight(remainingCreatures[Math.floor(Math.random() * remainingCreatures.length)], remainingCreatures[Math.floor(Math.random() * remainingCreatures.length)]);
}

console.log(" ");
console.log("[WIN] Creature #" + remainingCreatures[0].num + " has won!");
console.log("Starting array size was " + amt + " creatures")
console.log(remainingCreatures[0]);
</code>```

For some reason, this starts to slow down and eventually choke when amt is set to really big numbers, like one million. For reference, that's the number of objects that will be generated and added to an array - as you can probably see in the code, this array gets looped through a lot. But even with one million objects, each object is only around 80 bytes, maximum. And the calculations this program is doing are very basic.

So my question is: why is running this program so resource intensive, and how can I fix or mitigate it without changing the function of the program too drastically?

答:
First of all, a million of anything will take a toll on performance, no matter how small.
The other issue is that your design is inherently inefficient.

Look at your remove() function. It first finds the index of the element, then removes it. If you have one million elements in that array, on average, it will need to compare the passed value with 500,000 elements just to find it before it can remove. There's one easy way to fix this; pass indices directly rather than using .indexOf.

function fight(ind1, ind2) { //function to handle fighting, randomly creates creature pairs and has them fight
  var cr1 = remainingCreatures[ind1];
  var cr2 = remainingCreatures[ind2];
  function attack(a, b) {
         console.log("[ROUND 1] Creature 1 (#" + a.num + ") is attacking first.");
    b.hlt = b.hlt - (a.atk + b.ins);
    console.log("[HIT] Creature #" + b.num + " health reduced to " + b.hlt);
    if (b.hlt <= 0) {
      console.log("[DEATH] Creature #" + b.num + " Eliminated");
      remove(ind2);
    } else {
      console.log("[ROUND 2] Creature 2 (#" + b.num + ") is attacking second.");
      a.hlt = a.hlt - (b.atk + a.ins);
      console.log("[HIT] Creature #" + a.num + " health reduced to " + a.hlt);
      if (a.hlt <= 0) {
      console.log("[DEATH] Creature #" + a.num + " Eliminated");
      remove(ind1);
    }
    }
  }
  console.log("[FIGHT] Pair generated: Creature #" + cr1.num + " and Creature #" + cr2.num);
  cr1.fts++;
  cr2.fts++;
   if (cr1.ins <= cr2.ins) {
    attack(cr1, cr2);
   } else {
    attack(cr2, cr1);
   }
}

for(;true;) {
  if(remainingCreatures.length == 1) break;
  fight(Math.floor(Math.random() * remainingCreatures.length), Math.floor(Math.random() * remainingCreatures.length));
}
```

<p>There aren't many other ways to easily improve performance, but that alone should be enough. To make your code more readable, though, consider replacing this:</p>

<pre class="lang-js prettyprint-override"><code>for(;true;) {
  if(remainingCreatures.length == 1) break;
    fight(Math.floor(Math.random() * remainingCreatures.length), Math.floor(Math.random() * remainingCreatures.length));
}
</code>```

with this:

// Consider using === and !== as best practice, but not necessary here
while (remainingCreatures.length != 1)
    fight(Math.floor(Math.random() * remainingCreatures.length), Math.floor(Math.random() * remainingCreatures.length));

```

(See this link for info on that comment.)

Body:
I wrote the following JS program, which i'm running from the command line using node.

//step one: create an array of remaining creatures
var remainingCreatures = [];

//some config variables:
var amt = 1000; //amount of creatues generated
var maxhlt = 1000; //max value for creature health stat
var minhlt = 100; //min value for creature health stat
var maxatk = 100; //max value for creature attack stat
var minatk = 1; //min value for creature attack stat

function remove(target) { //helper function to easily remove creatues from the array
  var index = remainingCreatures.indexOf(target);
if (index > -1) {
  remainingCreatures.splice(index, 1);
}

}

//generate creatures
//each creature has an Attack, Health, and Aggressiveness , as well as Instabillity, the average of the three
//A creature's Instabillity is added to the attack points of its opponent in fights.
for (var i = 0; i < amt; i++) {
  var atkVal = Math.floor((Math.random() * maxatk) + minatk);
  var hltVal = Math.floor((Math.random() * maxhlt) + minhlt);
  var insVal = (atkVal + hltVal) / 2;

  remainingCreatures[i] = {num: i, atk: atkVal, hlt: hltVal, ins: insVal, fts: 0, ihlt: hltVal};
}
console.log(amt + " creatues generated, starting melee...");

function fight(cr1, cr2) { //function to handle fighting, randomly creates creature pairs and has them fight
  function attack(a, b) {
         console.log("[ROUND 1] Creature 1 (#" + a.num + ") is attacking first.");
    b.hlt = b.hlt - (a.atk + b.ins);
    console.log("[HIT] Creature #" + b.num + " health reduced to " + b.hlt);
    if (b.hlt <= 0) {
      console.log("[DEATH] Creature #" + b.num + " Eliminated");
      remove(b);
    } else {
      console.log("[ROUND 2] Creature 2 (#" + b.num + ") is attacking second.");
      a.hlt = a.hlt - (b.atk + a.ins);
      console.log("[HIT] Creature #" + a.num + " health reduced to " + a.hlt);
      if (a.hlt <= 0) {
      console.log("[DEATH] Creature #" + a.num + " Eliminated");
      remove(a);
    }
    }
  }
  console.log("[FIGHT] Pair generated: Creature #" + cr1.num + " and Creature #" + cr2.num);
  cr1.fts++;
  cr2.fts++;
   if (cr1.ins <= cr2.ins) {
    attack(cr1, cr2);
   } else {
    attack(cr2, cr1);
   }
}

for(;true;) {
  if(remainingCreatures.length == 1) break;
  fight(remainingCreatures[Math.floor(Math.random() * remainingCreatures.length)], remainingCreatures[Math.floor(Math.random() * remainingCreatures.length)]);
}

console.log(" ");
console.log("[WIN] Creature #" + remainingCreatures[0].num + " has won!");
console.log("Starting array size was " + amt + " creatures")
console.log(remainingCreatures[0]);

For some reason, this starts to slow down and eventually choke when amt is set to really big numbers, like one million. For reference, that's the number of objects that will be generated and added to an array - as you can probably see in the code, this array gets looped through a lot. But even with one million objects, each object is only around 80 bytes, maximum. And the calculations this program is doing are very basic.

So my question is: why is running this program so resource intensive, and how can I fix or mitigate it without changing the function of the program too drastically?

Body:
First of all, a million of anything will take a toll on performance, no matter how small.
The other issue is that your design is inherently inefficient.

Look at your remove() function. It first finds the index of the element, then removes it. If you have one million elements in that array, on average, it will need to compare the passed value with 500,000 elements just to find it before it can remove. There's one easy way to fix this; pass indices directly rather than using .indexOf.

function fight(ind1, ind2) { //function to handle fighting, randomly creates creature pairs and has them fight
  var cr1 = remainingCreatures[ind1];
  var cr2 = remainingCreatures[ind2];
  function attack(a, b) {
         console.log("[ROUND 1] Creature 1 (#" + a.num + ") is attacking first.");
    b.hlt = b.hlt - (a.atk + b.ins);
    console.log("[HIT] Creature #" + b.num + " health reduced to " + b.hlt);
    if (b.hlt <= 0) {
      console.log("[DEATH] Creature #" + b.num + " Eliminated");
      remove(ind2);
    } else {
      console.log("[ROUND 2] Creature 2 (#" + b.num + ") is attacking second.");
      a.hlt = a.hlt - (b.atk + a.ins);
      console.log("[HIT] Creature #" + a.num + " health reduced to " + a.hlt);
      if (a.hlt <= 0) {
      console.log("[DEATH] Creature #" + a.num + " Eliminated");
      remove(ind1);
    }
    }
  }
  console.log("[FIGHT] Pair generated: Creature #" + cr1.num + " and Creature #" + cr2.num);
  cr1.fts++;
  cr2.fts++;
   if (cr1.ins <= cr2.ins) {
    attack(cr1, cr2);
   } else {
    attack(cr2, cr1);
   }
}

for(;true;) {
  if(remainingCreatures.length == 1) break;
  fight(Math.floor(Math.random() * remainingCreatures.length), Math.floor(Math.random() * remainingCreatures.length));
}

There aren't many other ways to easily improve performance, but that alone should be enough. To make your code more readable, though, consider replacing this:

for(;true;) {
  if(remainingCreatures.length == 1) break;
    fight(Math.floor(Math.random() * remainingCreatures.length), Math.floor(Math.random() * remainingCreatures.length));
}

with this:

// Consider using === and !== as best practice, but not necessary here
while (remainingCreatures.length != 1)
    fight(Math.floor(Math.random() * remainingCreatures.length), Math.floor(Math.random() * remainingCreatures.length));

(See this link for info on that comment.)


问:
Pandas breaking rows and preventing Google Data Studio to read file correctly
I have a large dataframe that I'm sending to csv using pandas.to_csv()
When I look at in using print it seems alright:

```<code>print(df.name[160])
--> ALEXANDRE SILVA FARINAZZO

print(df.name[161])
--> ARCHANGELA GABRIELA PRATI FAVARO
</code>```

When I send it to csv and open in Excel I notice that Pandas, when using to_csv, breaks some lines:

I thought it could be something with Excel, but when I import it to Google Data Studio (without saving it or anything) those are exactly the problematic lines I get:

The other problematic rows follow the same problem pattern.
Is there a way to avoid it? Why is that happening?

EDIT

Following @Quang Hoang's suggestion I included a sep='\t' in the to_csv sentence. The problem shifted. Apparently Google Data Studio doesn't recognize '\t' as separator.

EDIT 2

Tried to take out all of the commas in the file using df['column'].str.replace(",","") and apparently commas are not the problem. I'm getting the very first problem again:

答:
I ended up solving it.
Actually there was two problems:

Problem a) In some columns there were some hidden "\n". Had to make a print(repr(string)) to find it out and then str.replace('\n',"")

Problem b) I noticed there was a prob in some rows that had cells with very long strings when uploading to Google Data Studio. Not sure what caused it but I tried shortening some character-dense columns with df['column'].loc[df['column'].str.len() > 1000] = df['column'].str[:1000] and it solved the prob. Not ideal because I missed some data, but worked.

Body:
I have a large dataframe that I'm sending to csv using pandas.to_csv()
When I look at in using print it seems alright:

print(df.name[160])
--> ALEXANDRE SILVA FARINAZZO

print(df.name[161])
--> ARCHANGELA GABRIELA PRATI FAVARO

When I send it to csv and open in Excel I notice that Pandas, when using to_csv, breaks some lines:

I thought it could be something with Excel, but when I import it to Google Data Studio (without saving it or anything) those are exactly the problematic lines I get:

The other problematic rows follow the same problem pattern.
Is there a way to avoid it? Why is that happening?

EDIT

Following @Quang Hoang's suggestion I included a sep='\t' in the to_csv sentence. The problem shifted. Apparently Google Data Studio doesn't recognize '\t' as separator.

EDIT 2

Tried to take out all of the commas in the file using df['column'].str.replace(",","") and apparently commas are not the problem. I'm getting the very first problem again:


问:
Store string as jsonb column with npgsql
I have the following model

```<code>public class Guardian
    {
        public int id { get; set; }
        public string username { get; set; }
        public string email { get; set; }
        public string firstName { get; set; }
        public string lastName { get; set; }
        public string gender { get; set ;}
        public string address { get; set; }

        public string Questions;

        public IList<Urgency> Urgencies { get; set; } = new List<Urgency>();
    }
</code>```

And this line of code on my AppDbContext: DbContext

```<code>builder.Entity<Guardian>().Property(d => d.Questions).HasColumnType(NpgsqlDbType.Jsonb);
</code>```

I want the Questions string field to be stored as JsonB type on the postgreSQL database. I get this error:

```<code>Argument 2: cannot convert from 'NpgsqlTypes.NpgsqlDbType' to 'string'
</code>```

Which totally makes sense, but don't know how should i approach it, i have done research but nothing seems to target my particular situation. I'd truly appreciate some advice.

答:
HasColumnType - which is a general EF Core method - accepts a database type name is a string (e.g. the string jsonb), and not the Npgsql-specific NpgsqlDbType.Enum. You should simply change your code as follows:

builder.Entity<Guardian>().Property(d => d.Questions).HasColumnType("jsonb");
```

See the Npgsql EF Core provider docs for this and other info on JSON mapping.

Body:
I have the following model

public class Guardian
    {
        public int id { get; set; }
        public string username { get; set; }
        public string email { get; set; }
        public string firstName { get; set; }
        public string lastName { get; set; }
        public string gender { get; set ;}
        public string address { get; set; }

        public string Questions;

        public IList<Urgency> Urgencies { get; set; } = new List<Urgency>();
    }

And this line of code on my AppDbContext: DbContext

builder.Entity<Guardian>().Property(d => d.Questions).HasColumnType(NpgsqlDbType.Jsonb);

I want the Questions string field to be stored as JsonB type on the postgreSQL database. I get this error:

Argument 2: cannot convert from 'NpgsqlTypes.NpgsqlDbType' to 'string'

Which totally makes sense, but don't know how should i approach it, i have done research but nothing seems to target my particular situation. I'd truly appreciate some advice.

Body:
HasColumnType - which is a general EF Core method - accepts a database type name is a string (e.g. the string jsonb), and not the Npgsql-specific NpgsqlDbType.Enum. You should simply change your code as follows:

builder.Entity<Guardian>().Property(d => d.Questions).HasColumnType("jsonb");

See the Npgsql EF Core provider docs for this and other info on JSON mapping.