问:
Kotlin recursively get size of a folder
I have seen how to return size of a folder in Java: Get size of folder or file, but I couldn't find it in Kotlin.

How do I get the size of a folder in Kotlin?

答:
So here is how to do it :

```<code>private fun dirSize(dir: File): Long {

        if (dir.exists()) {
            var result: Long = 0
            val fileList = dir.listFiles()
            for (i in fileList!!.indices) {
                if (fileList[i].isDirectory) {
                    result += dirSize(fileList[i])
                } else {
                    result += fileList[i].length()
                }
            }
            return result
        }
        return 0
    }
</code>```

And if you want a readable string your can do this :

```<code>private fun getStringSize(size: Long): String {
        if (size <= 0)
            return "0MB"
        val units = arrayOf("B", "KB", "MB", "GB", "TB")
        val digitGroups = (Math.log10(size.toDouble()) / Math.log10(1024.0)).toInt()
        return DecimalFormat("#,##0.#").format(size / Math.pow(1024.0, digitGroups.toDouble())) + " " + units[digitGroups]
    }
</code>```

How to use it :

```<code>val directory = File(filesDir.absolutePath + File.separator + DIRECTORY_NAME)
println(getStringSize(dirSize(directory)))
</code>```

Hope it's will help some of you.

Title: Kotlin recursively get size of a folder

Body:
So here is how to do it :

private fun dirSize(dir: File): Long {

        if (dir.exists()) {
            var result: Long = 0
            val fileList = dir.listFiles()
            for (i in fileList!!.indices) {
                if (fileList[i].isDirectory) {
                    result += dirSize(fileList[i])
                } else {
                    result += fileList[i].length()
                }
            }
            return result
        }
        return 0
    }

And if you want a readable string your can do this :

private fun getStringSize(size: Long): String {
        if (size <= 0)
            return "0MB"
        val units = arrayOf("B", "KB", "MB", "GB", "TB")
        val digitGroups = (Math.log10(size.toDouble()) / Math.log10(1024.0)).toInt()
        return DecimalFormat("#,##0.#").format(size / Math.pow(1024.0, digitGroups.toDouble())) + " " + units[digitGroups]
    }

How to use it :

val directory = File(filesDir.absolutePath + File.separator + DIRECTORY_NAME)
println(getStringSize(dirSize(directory)))

Hope it's will help some of you.


来源: stackexchange

问:
MySQL updating 2 columns with random and conditional value based on that random
I have such a table, with dates in format YYYY-MM-DD

```<code>id | state_id | created_on  | closed_on  |
1  | 1        | 2020-02-19  | NULL       |
2  | 2        | 2020-01-02  | 2020-01-03 |
3  | 1        | 2020-01-05  | NULL       |
4  | 4        | 2020-02-07  | 2020-02-08 |
4  | 3        | 2020-02-20  | NULL       |
</code>```

I need to update the STATE with a random state between 2 and 6 WHERE

state_id is 1 OR state_id is 3

created_on is in the past

I can do so with this (IT WORKS PERFECTLY):

UPDATE table SET state_id = FLOOR(2 + rand() * 6)
WHERE created_on < CURRENT_DATE()
AND (state_id=1 OR state_id=3)
```

<p>What I need to do additionally in the same update, would be to set <em>closed_on</em>:</p>

<ul>
<li>NULL if <em>the new random state</em> is 3</li>
<li><code>CURRENT_DATE()</code> if <em>the new random state</em> IS anything but 3</li>
</ul>

<p>I thought about using <code>SET @newState = FLOOR(2 + rand() * 6)</code>, but this way all the records would have the same <em>state_id</em></p>

<p>is it possible to update with different values? </p>

<p>something like this</p>

<pre class="lang-sql prettyprint-override"><code>UPDATE table SET state_id = FLOOR(2 + rand() * 6), closed_on = IF(new_state_id = 3, NULL, CURRENT_DATE())
WHERE created_on < CURRENT_DATE()
AND (state_id=1 OR state_id=3)
</code>```

答:
You could self-join the table with a subquery that computes the random number, so you can reuse that value in the outer query:</p>

```<code>update mytable t
inner join (
    select id, floor(2 + rand() * 6) rnd
    from mytable
    where state_id in (1, 3)
    and created_on < CURRENT_DATE()
) t1 using(id)
set
    t.state_id = t1.rnd,
    t.closed_on = if(t1.rnd = 3, null, current_date)
</code>```

ContentLicense: CC BY-SA 4.0

Body:
I have such a table, with dates in format YYYY-MM-DD

id | state_id | created_on  | closed_on  |
1  | 1        | 2020-02-19  | NULL       |
2  | 2        | 2020-01-02  | 2020-01-03 |
3  | 1        | 2020-01-05  | NULL       |
4  | 4        | 2020-02-07  | 2020-02-08 |
4  | 3        | 2020-02-20  | NULL       |

I need to update the STATE with a random state between 2 and 6 WHERE

state_id is 1 OR state_id is 3

created_on is in the past

I can do so with this (IT WORKS PERFECTLY):

UPDATE table SET state_id = FLOOR(2 + rand() * 6)
WHERE created_on < CURRENT_DATE()
AND (state_id=1 OR state_id=3)

What I need to do additionally in the same update, would be to set closed_on:

NULL if the new random state is 3

CURRENT_DATE() if the new random state IS anything but 3

I thought about using SET @newState = FLOOR(2 + rand() * 6), but this way all the records would have the same state_id

is it possible to update with different values?

something like this

UPDATE table SET state_id = FLOOR(2 + rand() * 6), closed_on = IF(new_state_id = 3, NULL, CURRENT_DATE())
WHERE created_on < CURRENT_DATE()
AND (state_id=1 OR state_id=3)

Body:
You could self-join the table with a subquery that computes the random number, so you can reuse that value in the outer query:

update mytable t
inner join (
    select id, floor(2 + rand() * 6) rnd
    from mytable
    where state_id in (1, 3)
    and created_on < CURRENT_DATE()
) t1 using(id)
set
    t.state_id = t1.rnd,
    t.closed_on = if(t1.rnd = 3, null, current_date)


问:
ManagementForm Data is Missing When Using Formset Prefix
Anyone know why this raise a ManagementForm Data is Missing when I use Formset Prefix?

From Shell

```<code>>>> from django import forms
>>> from django.forms.formsets import formset_factory
>>>
>>> class CheckBox (forms.Form):
...     overwrite = forms.BooleanField (required = False)
...
>>>
>>> data = {
...     'form-TOTAL_FORMS': '2',
...     'form-INITIAL_FORMS': '0',
...     'form-MAX_NUM_FORMS': '3',
...     'checkbox-0-overwrite': True,
...     'checkbox-1-overwrite': False,
... }
>>>
>>> CheckBoxFormSet = formset_factory (CheckBox)
>>> formset = CheckBoxFormSet (data)
>>> formset.is_valid ()
True
>>> formset.cleaned_data
[{}, {}]
>>>
</code>```

Adding Prefix to Formset

```<code>>>> formset = CheckBoxFormSet (data, prefix = 'checkbox')
>>> formset.is_valid ()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
.
.
.
django.core.exceptions.ValidationError: ['ManagementForm data is missing or has been tampered with']
</code>```

Django Doc mentioned to use prefix to distinguish between different formset in 'a' view.  Does that apply if I use it in the same view but in different method that processes a different HTML page like the example?  Doing what Django suggested in the example also triggered ManagementForm Data is Missing Error.

For example:

forms.py

```<code>class NodeForm (forms.Form):

    cars = forms.CharField (required = False)
    trucks = forms.CharField (required = False)

class CheckBox (forms.Form):
    overwrite = forms.BooleanField (required = False)
</code>```

views.py

```<code>def cars (request):

    CarsFormSet = formset_factory (CarsForm, formset = BaseNodeFormSet, extra = 2, max_num = 5)

    if request.method == 'POST':

        cars_formset = CarsFormSet (request.POST, prefix = 'carsform')

        if cars_formset.is_valid ():
            data = cars_formset.cleaned_data

            context = {'data': data}
            return render (request, 'vehicleform/response.html', context)
        else:
            cars_formset = CarsFormSet (prefix = 'carsform')

     context = {...previously entered data from POST...}

     return render (request, 'vehicleform/carsform.html', context)

def trucks (request):

    TrucksFormSet = formset_factory (TrucksForm, extra = 2, max_num = 5)

    if request.method == 'POST':

        trucks_formset = TrucksFormSet (request.POST, prefix = 'trucksform')

        if trucks_formset.is_valid ():
            data = truck_formset.cleaned_data

            context = {'data': data}
            return render (request, 'vehicleform/success.html', context)

        else:
            trucks_formset = TrucksFormSet (prefix = 'trucksform')

     return HttpResponse ('No overwrite data.')
</code>```

Update 1

I've narrowed it down to the actual Data.  It doesn't like my data for some reason.

Update 2

I've verified both the name in the form and the data is identical.  It prints only one checkbox-0-overwrite while I've stated 2 in my data.  Wonder why formset didn't work for checkbox.

```<code>>>> CheckBoxFormSet = formset_factory (CheckBox)
>>> formset = CheckBoxFormSet (prefix = 'checkbox')
>>>
>>> for form in formset:
...     print (form)
...
<tr><th><label for="id_checkbox-0-overwrite">Overwrite:</label></th><td><input id="id_checkbox-0-overwrite" name="checkbox-0-overwrite" type="checkbox" /></td></tr>
>>>
</code>```

Update 3

I'm not sure what's going on anymore.  This seems to generate the forms without the prefix.  Still getting the error the moment I insert the prefix.

```<code>>>> CheckBoxFormSet = formset_factory (CheckBox)
>>> formset = CheckBoxFormSet (data)
>>> formset.is_valid ()
True
>>> for form in formset:
...     print (form)
...
<tr><th><label for="id_form-0-overwrite">Overwrite:</label></th><td><input id="id_form-0-overwrite" name="form-0-overwrite" type="checkbox" /></td></tr>
<tr><th><label for="id_form-1-overwrite">Overwrite:</label></th><td><input id="id_form-1-overwrite" name="form-1-overwrite" type="checkbox" /></td></tr>
>>>
>>>
>>> data {
...    'form-TOTAL_FORMS': '2',
...    'form-INITIAL_FORMS': '0',
...    'form-MAX_NUM_FORMS': '3',
...    'checkbox-0-overwrite': True
}
</code>```

Update 4

The html template below is being generated and created by the first form, cars, as I've updated from the example above.  The second form which only insert the checkboxes next to the data being passed by the first form.  Displaying the formset in the template and clicking Submit is still giving me that "ManagementForm" error.  I'll try to create a brand new form with only the checkbox to see if that gives me any error.

Response.html

```<code><!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/html">
<head lang="en">
    <meta charset="UTF-8">
    {% load staticfiles %}
    <link rel="stylesheet" type="text/css" href="{% static 'nodeform/style.css' %}" >
    <title>Vehicle Information</title>
</head>
<body>
    <h1>Vehicle Information:</h1>
    <h4>Location: {{ location }}</h4>
    <form action="trucks" method="POST">{% csrf_token %}
    {{ checkbox_formset.management_form }}
       {% for form in checkbox_formset %}
        {{ form }}
       {% endfor %}
    <br>
    <p><input type="submit" value="Confirm">
    <a href="{% url 'carsform' %}">
        <button type="button">Cancel</button></a></p>
    </form>
</body>
</html>
</code>```

Update 5

I'm not sure if I understand it correctly but I think the failure is within the form's action and how I obtain the data. The initial form (carsform.html) had the form tag with no action:

carsform.html

```<code><form action="" method="POST">{% csrf_token %}...</form>
</code>```

It perform a POST and then pass the information gathered to the next page/form (response.html).  In addition, it adds a formset of checkboxes to the previous data like so:

response.html

```<code><form action="trucks" method="POST">{% csrf_token %}...</form>
</code>```

Output:

```<code>Audi (Obtained from cars)    []  <---Checkbox inserted from response.html manually & obtaining data from method trucks
Toyota (Obtained from cars)  []  <---Checkbox inserted from response.html manually & obtaining data from method trucks
</code>```

When user hit "Submit", the response.html form will then process and "reverse" itself back to the trucks again.  This time with no data from the cars method to process.  This eventually raised a ManagementForm Error.

I've tested this by inserting 2 formsets into the initial page (carsform.html) and click Submit.  The result I see on the next page/form (response.html) has both the data of the first and second formset.

My next question is how do I create the second form (response.html) to obtain the data without the error?

答:
This isn't exactly OP's problem but was the first result that came up when I searched for this error so I thought I'd share what was wrong with my prefix (which was included), as it wasn't obvious.

Django forms framework automatically defines _id_for_label if you don't set one to a form field's attributes, but doesn't remove them from formset data. So if your label is "my_label", it'll be rendered as "id_my_label", and come back in the POST data as such.

It took me a while to spot this in my request.POST (data below)

```<code>MyFormSet = formset_factory(MyForm, prefix='my_form')

data = {
    'id_result_form-MAX_NUM_FORMS': '1000', # note how django added 'id_'
    'id_result_form-INITIAL_FORMS': '0',
    'id_result_form-TOTAL_FORMS': '1',
}

formset = MyFormSet(data, prefix='my_form')
formset.is_valid()
>>> False

formset = MyFormSet(data, prefix='id_my_form') # added 'id_'
formset.is_valid()
>>> True
</code>```

ContentLicense: CC BY-SA 3.0

Body:
Anyone know why this raise a ManagementForm Data is Missing when I use Formset Prefix?

From Shell

>>> from django import forms
>>> from django.forms.formsets import formset_factory
>>>
>>> class CheckBox (forms.Form):
...     overwrite = forms.BooleanField (required = False)
...
>>>
>>> data = {
...     'form-TOTAL_FORMS': '2',
...     'form-INITIAL_FORMS': '0',
...     'form-MAX_NUM_FORMS': '3',
...     'checkbox-0-overwrite': True,
...     'checkbox-1-overwrite': False,
... }
>>>
>>> CheckBoxFormSet = formset_factory (CheckBox)
>>> formset = CheckBoxFormSet (data)
>>> formset.is_valid ()
True
>>> formset.cleaned_data
[{}, {}]
>>>

Adding Prefix to Formset

>>> formset = CheckBoxFormSet (data, prefix = 'checkbox')
>>> formset.is_valid ()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
.
.
.
django.core.exceptions.ValidationError: ['ManagementForm data is missing or has been tampered with']

Django Doc mentioned to use prefix to distinguish between different formset in 'a' view.  Does that apply if I use it in the same view but in different method that processes a different HTML page like the example?  Doing what Django suggested in the example also triggered ManagementForm Data is Missing Error.

For example:

forms.py

class NodeForm (forms.Form):

    cars = forms.CharField (required = False)
    trucks = forms.CharField (required = False)

class CheckBox (forms.Form):
    overwrite = forms.BooleanField (required = False)

views.py

def cars (request):

    CarsFormSet = formset_factory (CarsForm, formset = BaseNodeFormSet, extra = 2, max_num = 5)

    if request.method == 'POST':

        cars_formset = CarsFormSet (request.POST, prefix = 'carsform')

        if cars_formset.is_valid ():
            data = cars_formset.cleaned_data

            context = {'data': data}
            return render (request, 'vehicleform/response.html', context)
        else:
            cars_formset = CarsFormSet (prefix = 'carsform')

     context = {...previously entered data from POST...}

     return render (request, 'vehicleform/carsform.html', context)

def trucks (request):

    TrucksFormSet = formset_factory (TrucksForm, extra = 2, max_num = 5)

    if request.method == 'POST':

        trucks_formset = TrucksFormSet (request.POST, prefix = 'trucksform')

        if trucks_formset.is_valid ():
            data = truck_formset.cleaned_data

            context = {'data': data}
            return render (request, 'vehicleform/success.html', context)

        else:
            trucks_formset = TrucksFormSet (prefix = 'trucksform')

     return HttpResponse ('No overwrite data.')

Update 1

I've narrowed it down to the actual Data.  It doesn't like my data for some reason.

Update 2

I've verified both the name in the form and the data is identical.  It prints only one checkbox-0-overwrite while I've stated 2 in my data.  Wonder why formset didn't work for checkbox.

>>> CheckBoxFormSet = formset_factory (CheckBox)
>>> formset = CheckBoxFormSet (prefix = 'checkbox')
>>>
>>> for form in formset:
...     print (form)
...

Overwrite:	<input id="id_checkbox-0-overwrite" name="checkbox-0-overwrite" type="checkbox" />

>>>

Update 3

I'm not sure what's going on anymore.  This seems to generate the forms without the prefix.  Still getting the error the moment I insert the prefix.

>>> CheckBoxFormSet = formset_factory (CheckBox)
>>> formset = CheckBoxFormSet (data)
>>> formset.is_valid ()
True
>>> for form in formset:
...     print (form)
...

Overwrite:	<input id="id_form-0-overwrite" name="form-0-overwrite" type="checkbox" />

Overwrite:	<input id="id_form-1-overwrite" name="form-1-overwrite" type="checkbox" />

>>>
>>>
>>> data {
...    'form-TOTAL_FORMS': '2',
...    'form-INITIAL_FORMS': '0',
...    'form-MAX_NUM_FORMS': '3',
...    'checkbox-0-overwrite': True
}

Update 4

The html template below is being generated and created by the first form, cars, as I've updated from the example above.  The second form which only insert the checkboxes next to the data being passed by the first form.  Displaying the formset in the template and clicking Submit is still giving me that "ManagementForm" error.  I'll try to create a brand new form with only the checkbox to see if that gives me any error.

Response.html

<!DOCTYPE html>

    <meta charset="UTF-8">
    {% load staticfiles %}
    <link rel="stylesheet" type="text/css" href="{% static 'nodeform/style.css' %}" >
    <title>Vehicle Information</title>

Vehicle Information:

Location: {{ location }}

    <form action="trucks" method="POST">{% csrf_token %}
    {{ checkbox_formset.management_form }}
       {% for form in checkbox_formset %}
        {{ form }}
       {% endfor %}

<input type="submit" value="Confirm">

        <button type="button">Cancel</button>

    </form>

Update 5

I'm not sure if I understand it correctly but I think the failure is within the form's action and how I obtain the data. The initial form (carsform.html) had the form tag with no action:

carsform.html

<form action="" method="POST">{% csrf_token %}...</form>

It perform a POST and then pass the information gathered to the next page/form (response.html).  In addition, it adds a formset of checkboxes to the previous data like so:

response.html

<form action="trucks" method="POST">{% csrf_token %}...</form>

Output:

Audi (Obtained from cars)    []  <---Checkbox inserted from response.html manually & obtaining data from method trucks
Toyota (Obtained from cars)  []  <---Checkbox inserted from response.html manually & obtaining data from method trucks

When user hit "Submit", the response.html form will then process and "reverse" itself back to the trucks again.  This time with no data from the cars method to process.  This eventually raised a ManagementForm Error.

I've tested this by inserting 2 formsets into the initial page (carsform.html) and click Submit.  The result I see on the next page/form (response.html) has both the data of the first and second formset.

My next question is how do I create the second form (response.html) to obtain the data without the error?

Body:
This isn't exactly OP's problem but was the first result that came up when I searched for this error so I thought I'd share what was wrong with my prefix (which was included), as it wasn't obvious.

Django forms framework automatically defines _id_for_label if you don't set one to a form field's attributes, but doesn't remove them from formset data. So if your label is "my_label", it'll be rendered as "id_my_label", and come back in the POST data as such.

It took me a while to spot this in my request.POST (data below)

MyFormSet = formset_factory(MyForm, prefix='my_form')

data = {
    'id_result_form-MAX_NUM_FORMS': '1000', # note how django added 'id_'
    'id_result_form-INITIAL_FORMS': '0',
    'id_result_form-TOTAL_FORMS': '1',
}

formset = MyFormSet(data, prefix='my_form')
formset.is_valid()
>>> False

formset = MyFormSet(data, prefix='id_my_form') # added 'id_'
formset.is_valid()
>>> True


问:
Anyone succeed with enabling VM diagnostics using azurerm_virtual_machine_extension?
Enabling VM diagnostics in Azure is such a pain.  I've gotten it working using ARM templates, the Azure PowerShell SDK, and the Azure CLI.  But I've been trying for days now to enable VM diagnostics for both Windows and Linux VMs using Terraform and the azurerm_virtual_machine_extension resource.  Still not working, ugh!

Here's what I have so far (I've tweaked this a bit to simplify it for this post, so hope I didn't break anything with my manual edits):

```<code>  resource "azurerm_virtual_machine_extension" "vm-linux" {
  count                      = "${local.is_windows_vm == "false" ? 1 : 0}"
  depends_on                 = ["azurerm_virtual_machine_data_disk_attachment.vm"]
  name                       = "LinuxDiagnostic"
  location                   = "${var.location}"
  resource_group_name        = "${var.resource_group_name}"
  virtual_machine_name       = "${local.vm_name}"
  publisher                  = "Microsoft.Azure.Diagnostics"
  type                       = "LinuxDiagnostic"
  type_handler_version       = "3.0"
  auto_upgrade_minor_version = "true"

  # The JSON file referenced below was created by running "az vm diagnostics get-default-config", and adding/verifying the "__DIAGNOSTIC_STORAGE_ACCOUNT__" and "__VM_RESOURCE_ID__" placeholders.
  settings = <<SETTINGS
    {
      "ladCfg": "${base64encode(replace(replace(file("${path.module}/.diag-settings/linux_diag_config.json"), "__DIAGNOSTIC_STORAGE_ACCOUNT__", "${module.vm_storage_account.name}"), "__VM_RESOURCE_ID__", "${local.metricsresourceid}"))}",
      "storageAccount": "${module.vm_storage_account.name}"
    }
SETTINGS

  # SAS token below: Do not include the leading question mark, as per https://learn.microsoft.com/en-us/azure/virtual-machines/extensions/diagnostics-linux.
  protected_settings = <<SETTINGS
    {
      "storageAccountName": "${module.vm_storage_account.name}",
      "storageAccountSasToken": "${replace(data.azurerm_storage_account_sas.current.sas, "/^\\?/", "")}",
      "storageAccountEndPoint": "https://core.windows.net/"
    }
SETTINGS
}

resource "azurerm_virtual_machine_extension" "vm-win" {
  count                      = "${local.is_windows_vm == "true" ? 1 : 0}"
  depends_on                 = ["azurerm_virtual_machine_data_disk_attachment.vm"]
  name                       = "Microsoft.Insights.VMDiagnosticsSettings"
  location                   = "${var.location}"
  resource_group_name        = "${var.resource_group_name}"
  virtual_machine_name       = "${local.vm_name}"
  publisher                  = "Microsoft.Azure.Diagnostics"
  type                       = "IaaSDiagnostics"
  type_handler_version       = "1.9"
  auto_upgrade_minor_version = "true"

  # The JSON file referenced below was created by running "az vm diagnostics get-default-config --is-windows-os", and adding/verifying the "__DIAGNOSTIC_STORAGE_ACCOUNT__" and "__VM_RESOURCE_ID__" placeholders.
  settings = <<SETTINGS
    {
      "wadCfg": "${base64encode(replace(replace(file("${path.module}/.diag-settings/windows_diag_config.json"), "__DIAGNOSTIC_STORAGE_ACCOUNT__", "${module.vm_storage_account.name}"), "__VM_RESOURCE_ID__", "${local.metricsresourceid}"))}",
      "storageAccount": "${module.vm_storage_account.name}"
    }
SETTINGS

  protected_settings = <<SETTINGS
    {
      "storageAccountName": "${module.vm_storage_account.name}",
      "storageAccountSasToken": "${data.azurerm_storage_account_sas.current.sas}",
      "storageAccountEndPoint": "https://core.windows.net/"
    }
SETTINGS
}
</code>```

Notice that for both Linux and Windows I'm loading the diagnostics details from a JSON file within the code base, as per the comments.  These are the default configs provided by Azure, so they should be valid.

When I deploy these, the Linux VM extension deploys successfully, but in the Azure portal the extension says "Problems detected in generated mdsd configuration".  And if I look at the VM's "Diagnostic settings" it says "Error encountered: TypeError: Object doesn't support property or method 'diagnosticMonitorConfiguration'".
The Windows VM extension fails to deploy altogether, saying that it "Failed to read configuration".  If I view the extension in the portal it displays the following error:

```<code>"code": "ComponentStatus//failed/-3",
"level": "Error",
"displayStatus": "Provisioning failed",
"message": "Error starting the diagnostics extension"
</code>```

And if I look at the "Diagnostics settings" pane it just hangs with a never-ending ". . ." animation.

However, if I look at the "terraform apply" output for both VM extensions, the decoded settings look exactly as intended, matching the config files with the placeholders correctly replaced.

Any suggestions on how to get this working?

Thanks in advance!

答:
As the question was asked more than a year ago this is more for people like me who are trying this for the first time.
We only use linux vms so this advice applies to that:

protected settings should use PROTECTED_SETTINGS not SETTINGS (which you can see in @rv23 answer above)

From the documentation I am following https://learn.microsoft.com/en-gb/azure/virtual-machines/extensions/diagnostics-linux#protected-settings you can see you need to specify storageAccountSasToken not storageAccountKey:

Here is my redacted version of config (replace all bits in all caps with your own settings ):

```<code>    resource "azurerm_virtual_machine_extension" "vm_linux_diagnostics" {
    count = "1"

    name = "NAME"

        resource_group_name = "YOUR RESOURCE GROUP NAME"
        location            = "YOUR LOCATION"

        virtual_machine_name = "TARGET MACHINE NAME"

        publisher                  = "Microsoft.Azure.Diagnostics"
        type                       = "LinuxDiagnostic"
        type_handler_version       = "3.0"
        auto_upgrade_minor_version = "true"

        settings = <<SETTINGS
        {
            "StorageAccount": "tfnpfsnhsuk",
            "ladCfg": {
                "sampleRateInSeconds": 15,
                "diagnosticMonitorConfiguration": {
                    "metrics": {
                        "metricAggregation": [
                            {
                                "scheduledTransferPeriod": "PT1M"
                            },
                            {
                                "scheduledTransferPeriod": "PT1H"
                            }
                        ],
                        "resourceId": "VM ID"
                    },
                    "eventVolume": "Medium",
                    "performanceCounters": {
                        "sinks": "",
                        .... MORE METRICS - THAT YOU REQUIRE
            }
            }
        }
        SETTINGS

        protected_settings = <<PROTECTED_SETTINGS
        {
            "storageAccountName": "YOUR_ACCOUNT_NAME",
            "storageAccountSasToken": "YOUR SAS TOKEN"
        }
        PROTECTED_SETTINGS

        tags = "YOUR TAG"
        }
</code>```

Body:
Enabling VM diagnostics in Azure is such a pain.  I've gotten it working using ARM templates, the Azure PowerShell SDK, and the Azure CLI.  But I've been trying for days now to enable VM diagnostics for both Windows and Linux VMs using Terraform and the azurerm_virtual_machine_extension resource.  Still not working, ugh!

Here's what I have so far (I've tweaked this a bit to simplify it for this post, so hope I didn't break anything with my manual edits):

  resource "azurerm_virtual_machine_extension" "vm-linux" {
  count                      = "${local.is_windows_vm == "false" ? 1 : 0}"
  depends_on                 = ["azurerm_virtual_machine_data_disk_attachment.vm"]
  name                       = "LinuxDiagnostic"
  location                   = "${var.location}"
  resource_group_name        = "${var.resource_group_name}"
  virtual_machine_name       = "${local.vm_name}"
  publisher                  = "Microsoft.Azure.Diagnostics"
  type                       = "LinuxDiagnostic"
  type_handler_version       = "3.0"
  auto_upgrade_minor_version = "true"

  # The JSON file referenced below was created by running "az vm diagnostics get-default-config", and adding/verifying the "__DIAGNOSTIC_STORAGE_ACCOUNT__" and "__VM_RESOURCE_ID__" placeholders.
  settings = <<SETTINGS
    {
      "ladCfg": "${base64encode(replace(replace(file("${path.module}/.diag-settings/linux_diag_config.json"), "__DIAGNOSTIC_STORAGE_ACCOUNT__", "${module.vm_storage_account.name}"), "__VM_RESOURCE_ID__", "${local.metricsresourceid}"))}",
      "storageAccount": "${module.vm_storage_account.name}"
    }
SETTINGS

  # SAS token below: Do not include the leading question mark, as per https://learn.microsoft.com/en-us/azure/virtual-machines/extensions/diagnostics-linux.
  protected_settings = <<SETTINGS
    {
      "storageAccountName": "${module.vm_storage_account.name}",
      "storageAccountSasToken": "${replace(data.azurerm_storage_account_sas.current.sas, "/^\\?/", "")}",
      "storageAccountEndPoint": "https://core.windows.net/"
    }
SETTINGS
}

resource "azurerm_virtual_machine_extension" "vm-win" {
  count                      = "${local.is_windows_vm == "true" ? 1 : 0}"
  depends_on                 = ["azurerm_virtual_machine_data_disk_attachment.vm"]
  name                       = "Microsoft.Insights.VMDiagnosticsSettings"
  location                   = "${var.location}"
  resource_group_name        = "${var.resource_group_name}"
  virtual_machine_name       = "${local.vm_name}"
  publisher                  = "Microsoft.Azure.Diagnostics"
  type                       = "IaaSDiagnostics"
  type_handler_version       = "1.9"
  auto_upgrade_minor_version = "true"

  # The JSON file referenced below was created by running "az vm diagnostics get-default-config --is-windows-os", and adding/verifying the "__DIAGNOSTIC_STORAGE_ACCOUNT__" and "__VM_RESOURCE_ID__" placeholders.
  settings = <<SETTINGS
    {
      "wadCfg": "${base64encode(replace(replace(file("${path.module}/.diag-settings/windows_diag_config.json"), "__DIAGNOSTIC_STORAGE_ACCOUNT__", "${module.vm_storage_account.name}"), "__VM_RESOURCE_ID__", "${local.metricsresourceid}"))}",
      "storageAccount": "${module.vm_storage_account.name}"
    }
SETTINGS

  protected_settings = <<SETTINGS
    {
      "storageAccountName": "${module.vm_storage_account.name}",
      "storageAccountSasToken": "${data.azurerm_storage_account_sas.current.sas}",
      "storageAccountEndPoint": "https://core.windows.net/"
    }
SETTINGS
}

Notice that for both Linux and Windows I'm loading the diagnostics details from a JSON file within the code base, as per the comments.  These are the default configs provided by Azure, so they should be valid.

When I deploy these, the Linux VM extension deploys successfully, but in the Azure portal the extension says "Problems detected in generated mdsd configuration".  And if I look at the VM's "Diagnostic settings" it says "Error encountered: TypeError: Object doesn't support property or method 'diagnosticMonitorConfiguration'".
The Windows VM extension fails to deploy altogether, saying that it "Failed to read configuration".  If I view the extension in the portal it displays the following error:

"code": "ComponentStatus//failed/-3",
"level": "Error",
"displayStatus": "Provisioning failed",
"message": "Error starting the diagnostics extension"

And if I look at the "Diagnostics settings" pane it just hangs with a never-ending ". . ." animation.

However, if I look at the "terraform apply" output for both VM extensions, the decoded settings look exactly as intended, matching the config files with the placeholders correctly replaced.

Any suggestions on how to get this working?

Thanks in advance!

Body:
As the question was asked more than a year ago this is more for people like me who are trying this for the first time.
We only use linux vms so this advice applies to that:

protected settings should use PROTECTED_SETTINGS not SETTINGS (which you can see in @rv23 answer above)

From the documentation I am following https://learn.microsoft.com/en-gb/azure/virtual-machines/extensions/diagnostics-linux#protected-settings you can see you need to specify storageAccountSasToken not storageAccountKey:

Here is my redacted version of config (replace all bits in all caps with your own settings ):

    resource "azurerm_virtual_machine_extension" "vm_linux_diagnostics" {
    count = "1"

    name = "NAME"

        resource_group_name = "YOUR RESOURCE GROUP NAME"
        location            = "YOUR LOCATION"

        virtual_machine_name = "TARGET MACHINE NAME"

        publisher                  = "Microsoft.Azure.Diagnostics"
        type                       = "LinuxDiagnostic"
        type_handler_version       = "3.0"
        auto_upgrade_minor_version = "true"

        settings = <<SETTINGS
        {
            "StorageAccount": "tfnpfsnhsuk",
            "ladCfg": {
                "sampleRateInSeconds": 15,
                "diagnosticMonitorConfiguration": {
                    "metrics": {
                        "metricAggregation": [
                            {
                                "scheduledTransferPeriod": "PT1M"
                            },
                            {
                                "scheduledTransferPeriod": "PT1H"
                            }
                        ],
                        "resourceId": "VM ID"
                    },
                    "eventVolume": "Medium",
                    "performanceCounters": {
                        "sinks": "",
                        .... MORE METRICS - THAT YOU REQUIRE
            }
            }
        }
        SETTINGS

        protected_settings = <<PROTECTED_SETTINGS
        {
            "storageAccountName": "YOUR_ACCOUNT_NAME",
            "storageAccountSasToken": "YOUR SAS TOKEN"
        }
        PROTECTED_SETTINGS

        tags = "YOUR TAG"
        }


问:
How do I negotiate alert boxes in Watir?
I am trying to create an automation script for a website using Ruby with
Watir.

I ran into a situation where I have to choose 'Yes' or 'No' in an
alert box. The problem is that I am not able to identify the alert box
as a component of the page so that I can obtain its identifier and use
it to select an option.

I found some information how to
create an alert box, but I need to navigate through one. Can
anyone help me out?

答:
It sounds like you have a confirm box if you can choose two options ("OK" or "Cancel"). If you want to simulate clicking "OK", you have to put something like this before you trigger the confirm.

```<code># return true for confirm to simulate clicking OK
b.execute_script("window.confirm = function() {return true}")
</code>```

If you just have an alert with a single "OK" button, do this.

```<code># don't return anything for alert
b.execute_script("window.alert = function() {}")
</code>```

See this page for more examples.

Body:
It sounds like you have a confirm box if you can choose two options ("OK" or "Cancel"). If you want to simulate clicking "OK", you have to put something like this before you trigger the confirm.

# return true for confirm to simulate clicking OK
b.execute_script("window.confirm = function() {return true}")

If you just have an alert with a single "OK" button, do this.

# don't return anything for alert
b.execute_script("window.alert = function() {}")

See this page for more examples.


问:
How to get data remotely from mySQL database installed in raspberry pi in WinForms application?
I'm collecting data of sensors on my raspberry pi mySQL database, and theese data need to be used on an windows form application. The following steps have been done:

The bind-address is commented out on raspberry config file

The connection between windows form application and raspberry mySQL database is successful with the following code.

```<code>private void button1_Click(object sender, EventArgs e)
{
    MySqlConnectionStringBuilder conn_string = new MySqlConnectionStringBuilder();
    conn_string.Server = "192.168.x.xxx";
    conn_string.Port = 3306;
    conn_string.UserID = "root";
    conn_string.Password = "password";
    conn_string.Database = "db_name";
    MySqlConnection MyCon = new MySqlConnection(conn_string.ToString());

    try
    {
        MyCon.Open();
        MessageBox.Show("Open");
        MyCon.Close();
        MessageBox.Show("Close");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
</code>```

When I try to get data with the following code and same connection

```<code>private void button1_Click(object sender, EventArgs e)
{

    MySqlConnectionStringBuilder conn_string = new MySqlConnectionStringBuilder();
    conn_string.Server = "192.168.x.xxx";
    conn_string.Port = 3306;
    conn_string.UserID = "root";
    conn_string.Password = "password";
    conn_string.Database = "db_name";
    MySqlConnection MyCon = new MySqlConnection(conn_string.ToString());

    MyCon.Open();

    MySqlDataAdapter sqlDa = new MySqlDataAdapter("SELECT * FROM tab_name", MyCon);
    sqlDa.SelectCommand.CommandType = CommandType.StoredProcedure;
    DataTable dtblData = new DataTable();
    sqlDa.Fill(dtblData);
    dataGridView1.DataSource = dtblData;

}
</code>```

This message appears

MySql.Data.MySqlClient.MySqlException: 'Procedure or function 'SELECT * FROMtab_nameWHERE 1' cannot be found in database 'db_name'.'

There are about 150000 data in specified database/table can be seen with following picture.

Any help will be appreciated.

答:
"SELECT * FROM tab_name" is not a stored procedure, so CommandType.StoredProcedure is incorrect.

make it

```<code>sqlDa.SelectCommand.CommandType = CommandType.Text;
</code>```

or remove entirely, because CommandType.Text is default.

the remaining commandType should work with table name:

```<code>MySqlDataAdapter sqlDa = new MySqlDataAdapter("tab_name", MyCon);
sqlDa.SelectCommand.CommandType = CommandType.TableDirect;
</code>```

Body:
I'm collecting data of sensors on my raspberry pi mySQL database, and theese data need to be used on an windows form application. The following steps have been done:

The bind-address is commented out on raspberry config file

The connection between windows form application and raspberry mySQL database is successful with the following code.

private void button1_Click(object sender, EventArgs e)
{
    MySqlConnectionStringBuilder conn_string = new MySqlConnectionStringBuilder();
    conn_string.Server = "192.168.x.xxx";
    conn_string.Port = 3306;
    conn_string.UserID = "root";
    conn_string.Password = "password";
    conn_string.Database = "db_name";
    MySqlConnection MyCon = new MySqlConnection(conn_string.ToString());

    try
    {
        MyCon.Open();
        MessageBox.Show("Open");
        MyCon.Close();
        MessageBox.Show("Close");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

When I try to get data with the following code and same connection

private void button1_Click(object sender, EventArgs e)
{

    MySqlConnectionStringBuilder conn_string = new MySqlConnectionStringBuilder();
    conn_string.Server = "192.168.x.xxx";
    conn_string.Port = 3306;
    conn_string.UserID = "root";
    conn_string.Password = "password";
    conn_string.Database = "db_name";
    MySqlConnection MyCon = new MySqlConnection(conn_string.ToString());

    MyCon.Open();

    MySqlDataAdapter sqlDa = new MySqlDataAdapter("SELECT * FROM tab_name", MyCon);
    sqlDa.SelectCommand.CommandType = CommandType.StoredProcedure;
    DataTable dtblData = new DataTable();
    sqlDa.Fill(dtblData);
    dataGridView1.DataSource = dtblData;

}

This message appears

MySql.Data.MySqlClient.MySqlException: 'Procedure or function 'SELECT * FROMtab_nameWHERE 1' cannot be found in database 'db_name'.'

There are about 150000 data in specified database/table can be seen with following picture.

Any help will be appreciated.

Body:
"SELECT * FROM tab_name" is not a stored procedure, so CommandType.StoredProcedure is incorrect.

make it

sqlDa.SelectCommand.CommandType = CommandType.Text;

or remove entirely, because CommandType.Text is default.

the remaining commandType should work with table name:

MySqlDataAdapter sqlDa = new MySqlDataAdapter("tab_name", MyCon);
sqlDa.SelectCommand.CommandType = CommandType.TableDirect;


问:
It is taking too much time to process frames while doing pixel by pixel operation of video frames
```<code>import cv2
import numpy as np

cap = cv2.VideoCapture(0)

def threshold_slow(image):
    h = image.shape[0]
    w = image.shape[1]
    for x in range(0, w):
        for y in range(0, h):
            k = np.array(image[x, y])
            print(k)

def video():
    while True:
        ret,frame = cap.read()
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        threshold_slow(frame)
        cv2.imshow('frame',frame)
        key = cv2.waitKey(25)
        if key == ord('q'):
            break
if __name__ == '__main__':
    video()
cap.release()
cv2.destroyAllWindows()
</code>```

I have done almost everything I can and still I am not able to reslove this. Anybody who has some idea please help with some piece of code. When I put print outside the for loop then it was working fine. But then I was not getting the value of each pixel in the image.

答:
print is unavoidably slow (in comparison to the rest of the code). Assuming a 256x256 image, your code will print 65536 values. Depending on the format (I'm not familar with OpenCV, but lets assume 1 byte per pixel), that ranges from 2 to 4 bytes of output for every pixel (convert 8-bit unsigned byte to text + end of line), so 128kB-320kB, that your terminal then needs to scroll.

Your best bet is to limit the region of pixels you try to print: ie, specify a suitably small region using appropriate parameters to your range invocations.

Body:
import cv2
import numpy as np

cap = cv2.VideoCapture(0)

def threshold_slow(image):
    h = image.shape[0]
    w = image.shape[1]
    for x in range(0, w):
        for y in range(0, h):
            k = np.array(image[x, y])
            print(k)

def video():
    while True:
        ret,frame = cap.read()
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        threshold_slow(frame)
        cv2.imshow('frame',frame)
        key = cv2.waitKey(25)
        if key == ord('q'):
            break
if __name__ == '__main__':
    video()
cap.release()
cv2.destroyAllWindows()

I have done almost everything I can and still I am not able to reslove this. Anybody who has some idea please help with some piece of code. When I put print outside the for loop then it was working fine. But then I was not getting the value of each pixel in the image.


问:
Which tool can replace RFT and Selenium?
I want to automate testing of java based applications, not exactly portals though.
RFT 7 version that we have is not compatible with latest version of Firefox, so we need another tool, preferably open source.

Please suggest some replacement for Selenium and RFT. The purpose is automation of Functional testing.

答:
Both RFT (Rational Functional Tester) and Selenium is used for Functional & Regression test of web. RFT is commercial & Selenium is open source/free tool.

There are some other free tools that you can try. Such as

TestProject

Sahi

Watir


问:
How to create memory segment with fixed size, place data in fixed position inside the segment, using MinGW
I am using MinGW to create a win32 exe.
I would like to create a memory segment with a fixed size, and after that place variable inside this segment at a fixed address relative to the start of the segment. Has anyone an idea of how to do this?

I was able to declare my own segment with:

```<code>  .codeflash BLOCK(__section_alignment__) :
  {
    __codeflash_start__ = . ;
    *(.codeflash)
    __codeflash_end__ = . ;
  }
</code>```

And place variables inside this segment using:

```<code>__attribute__((section(".codeflash")))
</code>```

I am using the default linker script.

Thanks.

答:
I used the suggestion above to solve the problem:
Added everything to a struct and put this struct inside the section.</p>

Body:
I am using MinGW to create a win32 exe.
I would like to create a memory segment with a fixed size, and after that place variable inside this segment at a fixed address relative to the start of the segment. Has anyone an idea of how to do this?

I was able to declare my own segment with:

  .codeflash BLOCK(__section_alignment__) :
  {
    __codeflash_start__ = . ;
    *(.codeflash)
    __codeflash_end__ = . ;
  }

And place variables inside this segment using:

__attribute__((section(".codeflash")))

I am using the default linker script.

Thanks.


问:
How to save each row of a DataFrame as a HDFS file using pyspark
I have a file that is ctrl A delimited and has the following header:

filename','file_metadata','data_content','status','errortype','error_message'

I need to dump individual files to hdfs for each record of the file to directories like - basepath_errortype_filename/file.json , and the content of the file would be the data_content column.

Showing the sample data:

```<code>>>> ff_df = ff_rdd.toDF(['file_name','file_metadata','data_content','status','error_type','error_message'])
>>> ff_df .show()
+--------------+-------------+--------------------+------+-------------+--------------------+
|     file_name|file_metadata|        data_content|status|   error_type|       error_message|
+--------------+-------------+--------------------+------+-------------+--------------------+
|test_file.json|     metadata|{ "fruit": "Apple...|FAILED| INVALID_JSON|     could not parse|
|demo_file.json|     metadata|{ "fruit": "Apple...|FAILED|MISSING_RULES|No matching rules...|
+--------------+-------------+--------------------+------+-------------+--------------------+
</code>```

Now I need these two rows as two files in hdfs, in folders /tmp/INVALID_JSON_test_file and /tmp/MISSING_RULES_demo_file . I have written the following pyspark code , but I am not getting the desired result. Please help

```<code>def write_file(line)
 tokens=line.split("\x01")
 file_name=tokens[0]
 error_type=tokens[4]
 content=tokens[2]
 #define path to saved file
 file_name = %s + "/" +
 directory_name = basePath"/"error_type"/"file_name
 return directory_name

# get the file content
ff_rdd = sc.textFile("/tmp/pyspark1.txt").map(lambda line: line.split("\x01"))
ff_df = ff_rdd.toDF(['file_name','file_metadata','data_content','status','error_type','error_message'])
content_df = ff_df.select("data_content")

file_path = sc.textFile("/tmp/pyspark1.txt").map(lambda line: write_file(line))
content_df.rdd.saveAsTextFile("file_path")```

```

答:
Sample Input:

```<code>+--------------+-------------+-------------------+------+-------------+-----------------+
|     file_name|file_metadata|       data_content|status|   error_type|    error_message|
+--------------+-------------+-------------------+------+-------------+-----------------+
|test_file.json|     metadata|{ "fruit": "Apple"}|FAILED| INVALID_JSON|  could not parse|
|demo_file.json|     metadata|   { "fruit": "Ab"}|FAILED|MISSING_RULES|No matching rules|
+--------------+-------------+-------------------+------+-------------+-----------------+
</code>```

First, we will concatenate error_type column and file_name column (only the file name, excluding the extension) to create newColumn.

```<code>final_df = df_new.withColumn("newColumn",concat(col("error_type"),lit("_"),split("file_name","\.")[0]))
</code>```

On running df_new.show(truncate=false) we will see the sample output as :

```<code>+--------------+-------------+-------------------+------+-------------+-----------------+-----------------------+
|file_name     |file_metadata|data_content       |status|error_type   |error_message    |newColumn              |
+--------------+-------------+-------------------+------+-------------+-----------------+-----------------------+
|test_file.json|metadata     |{ "fruit": "Apple"}|FAILED|INVALID_JSON |could not parse  |INVALID_JSON_test_file |
|demo_file.json|metadata     |{ "fruit": "Ab"}   |FAILED|MISSING_RULES|No matching rules|MISSING_RULES_demo_file|
+--------------+-------------+-------------------+------+-------------+-----------------+-----------------------+
</code>```

To achieve the directory structure in the required format ex: Base_directory/INVALID_JSON_test_file ,while writing we will have to partition the final_df on the basis of the newColumn created.

We can write using the following :

```<code>final_df.select("data_content","newColumn").write.partitionBy("newColumn").save(FilePath)
</code>```

By default a parquet file will be written.
I don't think it will be possible to write the output as a text file as it doesn't accept multiple columns, we will require the newColumn to be written along with the data_content because we are partitioning the dataframe on the basis of newColumn.

Body:
I have a file that is ctrl A delimited and has the following header:

filename','file_metadata','data_content','status','errortype','error_message'

I need to dump individual files to hdfs for each record of the file to directories like - basepath_errortype_filename/file.json , and the content of the file would be the data_content column.

Showing the sample data:

>>> ff_df = ff_rdd.toDF(['file_name','file_metadata','data_content','status','error_type','error_message'])
>>> ff_df .show()
+--------------+-------------+--------------------+------+-------------+--------------------+
|     file_name|file_metadata|        data_content|status|   error_type|       error_message|
+--------------+-------------+--------------------+------+-------------+--------------------+
|test_file.json|     metadata|{ "fruit": "Apple...|FAILED| INVALID_JSON|     could not parse|
|demo_file.json|     metadata|{ "fruit": "Apple...|FAILED|MISSING_RULES|No matching rules...|
+--------------+-------------+--------------------+------+-------------+--------------------+

Now I need these two rows as two files in hdfs, in folders /tmp/INVALID_JSON_test_file and /tmp/MISSING_RULES_demo_file . I have written the following pyspark code , but I am not getting the desired result. Please help

def write_file(line)
 tokens=line.split("\x01")
 file_name=tokens[0]
 error_type=tokens[4]
 content=tokens[2]
 #define path to saved file
 file_name = %s + "/" +
 directory_name = basePath"/"error_type"/"file_name
 return directory_name

# get the file content
ff_rdd = sc.textFile("/tmp/pyspark1.txt").map(lambda line: line.split("\x01"))
ff_df = ff_rdd.toDF(['file_name','file_metadata','data_content','status','error_type','error_message'])
content_df = ff_df.select("data_content")

file_path = sc.textFile("/tmp/pyspark1.txt").map(lambda line: write_file(line))
content_df.rdd.saveAsTextFile("file_path")```

Body:
Sample Input:

+--------------+-------------+-------------------+------+-------------+-----------------+
|     file_name|file_metadata|       data_content|status|   error_type|    error_message|
+--------------+-------------+-------------------+------+-------------+-----------------+
|test_file.json|     metadata|{ "fruit": "Apple"}|FAILED| INVALID_JSON|  could not parse|
|demo_file.json|     metadata|   { "fruit": "Ab"}|FAILED|MISSING_RULES|No matching rules|
+--------------+-------------+-------------------+------+-------------+-----------------+

First, we will concatenate error_type column and file_name column (only the file name, excluding the extension) to create newColumn.

final_df = df_new.withColumn("newColumn",concat(col("error_type"),lit("_"),split("file_name","\.")[0]))

On running df_new.show(truncate=false) we will see the sample output as :

+--------------+-------------+-------------------+------+-------------+-----------------+-----------------------+
|file_name     |file_metadata|data_content       |status|error_type   |error_message    |newColumn              |
+--------------+-------------+-------------------+------+-------------+-----------------+-----------------------+
|test_file.json|metadata     |{ "fruit": "Apple"}|FAILED|INVALID_JSON |could not parse  |INVALID_JSON_test_file |
|demo_file.json|metadata     |{ "fruit": "Ab"}   |FAILED|MISSING_RULES|No matching rules|MISSING_RULES_demo_file|
+--------------+-------------+-------------------+------+-------------+-----------------+-----------------------+

To achieve the directory structure in the required format ex: Base_directory/INVALID_JSON_test_file ,while writing we will have to partition the final_df on the basis of the newColumn created.

We can write using the following :

final_df.select("data_content","newColumn").write.partitionBy("newColumn").save(FilePath)

By default a parquet file will be written.
I don't think it will be possible to write the output as a text file as it doesn't accept multiple columns, we will require the newColumn to be written along with the data_content because we are partitioning the dataframe on the basis of newColumn.


问:
Create procedure that deletes data from several tables. SQL Server
I need to create a procedure that allows me to delete data from several tables at once, searching through the CUIT. There are four tables shown here:

I must pass a CUIT value in, and if it matches any item saved in the database, I need it to be deleted. I could not find the syntax to be able to erase more than two tables at once, if you could give me a hand I would greatly appreciate it. I clarify by the doubts that the procedure is actually much larger, in total it would be about 12 tables, but as I am interested only the syntax I pass this fragment.

I have problems trying to join the tables using INNER JOIN.

I am using SQL Server 2016. Thank you

答:
Considering your design, i would implement Cascading on your Foreign Key. This is a sample, but should get you on the right path:</p>

```<code>USE Sandbox;
GO

CREATE TABLE dbo.ParentTable (PK int IDENTITY PRIMARY KEY,
                              I int);

CREATE TABLE dbo.ChildTable1 (PK int IDENTITY PRIMARY KEY,
                              FK int,
                              S varchar(10));

CREATE TABLE dbo.ChildTable2 (PK int IDENTITY PRIMARY KEY,
                              FK int,
                              S varchar(10));

CREATE TABLE dbo.ChildTable3 (PK int IDENTITY PRIMARY KEY,
                              FK int,
                              S varchar(10));
GO
--Create keys with CASCADE ON DELETE
ALTER TABLE dbo.ChildTable1 ADD CONSTRAINT FK1 FOREIGN KEY (FK) REFERENCES dbo.ParentTable(PK) ON DELETE CASCADE;
GO
ALTER TABLE dbo.ChildTable2 ADD CONSTRAINT FK2 FOREIGN KEY (FK) REFERENCES dbo.ParentTable(PK) ON DELETE CASCADE;
GO
ALTER TABLE dbo.ChildTable3 ADD CONSTRAINT FK3 FOREIGN KEY (FK) REFERENCES dbo.ParentTable(PK) ON DELETE CASCADE;
GO

INSERT INTO dbo.ParentTable (I)
VALUES(1),(7),(9);

INSERT INTO dbo.ChildTable1 (FK,
                            S)
VALUES(1,'abc'),(3,'def');

INSERT INTO dbo.ChildTable2 (FK,
                            S)
VALUES(1,'xyz'),(2,'qwe');

INSERT INTO dbo.ChildTable3 (FK,
                            S)
VALUES(1,'123');
GO

SELECT *
FROM dbo.ParentTable PT
     LEFT JOIN dbo.ChildTable1 CT1 ON PT.PK = CT1.PK
     LEFT JOIN dbo.ChildTable2 CT2 ON PT.PK = CT2.PK
     LEFT JOIN dbo.ChildTable3 CT3 ON PT.PK = CT3.PK;

--1, as we're going to DELETE that
SELECT *
FROM dbo.ChildTable1 CT1
WHERE CT1.FK = 1;

SELECT *
FROM dbo.ChildTable1 CT2
WHERE CT2.FK = 1;

SELECT *
FROM dbo.ChildTable1 CT3
WHERE CT3.FK = 1;
GO

--Perform the DELETE, which will CASCADE to the child tables.
DELETE FROM dbo.ParentTable
WHERE PK = 1;
GO
--no rows
SELECT *
FROM dbo.ParentTable PT
     LEFT JOIN dbo.ChildTable1 CT1 ON PT.PK = CT1.PK
     LEFT JOIN dbo.ChildTable2 CT2 ON PT.PK = CT2.PK
     LEFT JOIN dbo.ChildTable3 CT3 ON PT.PK = CT3.PK;

SELECT *
FROM dbo.ChildTable1 CT1
WHERE CT1.FK = 1;

SELECT *
FROM dbo.ChildTable1 CT2
WHERE CT2.FK = 1;

SELECT *
FROM dbo.ChildTable1 CT3
WHERE CT3.FK = 1;

GO
DROP TABLE dbo.ChildTable3;
DROP TABLE dbo.ChildTable2;
DROP TABLE dbo.ChildTable1;
DROP TABLE dbo.ParentTable;
</code>```

Body:
Considering your design, i would implement Cascading on your Foreign Key. This is a sample, but should get you on the right path:

USE Sandbox;
GO

CREATE TABLE dbo.ParentTable (PK int IDENTITY PRIMARY KEY,
                              I int);

CREATE TABLE dbo.ChildTable1 (PK int IDENTITY PRIMARY KEY,
                              FK int,
                              S varchar(10));

CREATE TABLE dbo.ChildTable2 (PK int IDENTITY PRIMARY KEY,
                              FK int,
                              S varchar(10));

CREATE TABLE dbo.ChildTable3 (PK int IDENTITY PRIMARY KEY,
                              FK int,
                              S varchar(10));
GO
--Create keys with CASCADE ON DELETE
ALTER TABLE dbo.ChildTable1 ADD CONSTRAINT FK1 FOREIGN KEY (FK) REFERENCES dbo.ParentTable(PK) ON DELETE CASCADE;
GO
ALTER TABLE dbo.ChildTable2 ADD CONSTRAINT FK2 FOREIGN KEY (FK) REFERENCES dbo.ParentTable(PK) ON DELETE CASCADE;
GO
ALTER TABLE dbo.ChildTable3 ADD CONSTRAINT FK3 FOREIGN KEY (FK) REFERENCES dbo.ParentTable(PK) ON DELETE CASCADE;
GO

INSERT INTO dbo.ParentTable (I)
VALUES(1),(7),(9);

INSERT INTO dbo.ChildTable1 (FK,
                            S)
VALUES(1,'abc'),(3,'def');

INSERT INTO dbo.ChildTable2 (FK,
                            S)
VALUES(1,'xyz'),(2,'qwe');

INSERT INTO dbo.ChildTable3 (FK,
                            S)
VALUES(1,'123');
GO

SELECT *
FROM dbo.ParentTable PT
     LEFT JOIN dbo.ChildTable1 CT1 ON PT.PK = CT1.PK
     LEFT JOIN dbo.ChildTable2 CT2 ON PT.PK = CT2.PK
     LEFT JOIN dbo.ChildTable3 CT3 ON PT.PK = CT3.PK;

--1, as we're going to DELETE that
SELECT *
FROM dbo.ChildTable1 CT1
WHERE CT1.FK = 1;

SELECT *
FROM dbo.ChildTable1 CT2
WHERE CT2.FK = 1;

SELECT *
FROM dbo.ChildTable1 CT3
WHERE CT3.FK = 1;
GO

--Perform the DELETE, which will CASCADE to the child tables.
DELETE FROM dbo.ParentTable
WHERE PK = 1;
GO
--no rows
SELECT *
FROM dbo.ParentTable PT
     LEFT JOIN dbo.ChildTable1 CT1 ON PT.PK = CT1.PK
     LEFT JOIN dbo.ChildTable2 CT2 ON PT.PK = CT2.PK
     LEFT JOIN dbo.ChildTable3 CT3 ON PT.PK = CT3.PK;

SELECT *
FROM dbo.ChildTable1 CT1
WHERE CT1.FK = 1;

SELECT *
FROM dbo.ChildTable1 CT2
WHERE CT2.FK = 1;

SELECT *
FROM dbo.ChildTable1 CT3
WHERE CT3.FK = 1;

GO
DROP TABLE dbo.ChildTable3;
DROP TABLE dbo.ChildTable2;
DROP TABLE dbo.ChildTable1;
DROP TABLE dbo.ParentTable;


问:
Python module problem
I have two .py files called "notepad.py" & "pycolour.py". First is in the parent folder, second is in "subdir" folder.
First file:

```<code>#notepad.py
from Tkinter import *
from subdir import pycolour

class SimpleNotepad(object):

    def __init__(self):
        self.frame = Frame(root, bg='#707070', bd=1)
        self.text_field = Text(self.frame, font='Verdana 10', bd=3, wrap='word')
        self.text_field.focus_set()
        self.btn = Button(root, text='try to colour', command=self.try_to_colour)

        self.coloured = pycolour.PyColour(root)

        self.frame.pack(expand=True, fill='both')
        self.text_field.pack(expand=True, fill='both')
        self.btn.pack()

    def try_to_colour(self):
        txt = self.text_field.get(1.0, 'end')
        text = str(txt).split('\n')
        for i in range(len(text)-1):
            self.coloured.colourize(i+1, len(text[i]))

root = Tk()
app = SimpleNotepad()
root.mainloop()
</code>```

Second file:

```<code>#pycolour.py
from Tkinter import *
import keyword

#colorizing the Python code
class PyColour(Text):

    def __init__(self, parent):
        Text.__init__(self, parent)
        self.focus_set()
        self.tag_conf()

        self.bind('<Key>', self.key_pressed)

    tags = {'com' : '#009999',   #comment is darkgreen
            'str' : '#F50000',   #string is red
            'kw'  : '#F57A00',   #keyword is orange
            'obj' : '#7A00F5',   #function or class name is purple
            'int' : '#3333FF'    #integers is darkblue
            }

    def tag_conf(self):
        for tag, value in self.tags.items():
            self.tag_configure(tag, foreground=value)

    def tag_delete(self, start, end):
        for tag in self.tags.items():
            self.tag_remove(tag, start, end)

    def key_pressed(self, key):
        if key.char in ' :[(]),"\'':
            self.edit_separator() #for undo/redo

        cline = self.index(INSERT).split('.')[0]
        lastcol = 0
        char = self.get('%s.%d'%(cline, lastcol))
        while char != '\n':
            lastcol += 1
            char = self.get('%s.%d'%(cline, lastcol))

        self.colourize(cline,lastcol)

    def colourize(self, cline, lastcol):
        buffer = self.get('%s.%d'%(cline,0),'%s.%d'%(cline,lastcol))
        tokenized = buffer.split(' ')

        self.tag_remove('%s.%d'%(cline, 0), '%s.%d'%(cline, lastcol))

        quotes = 0
        start = 0
        for i in range(len(buffer)):
            if buffer[i] in ['"',"'"]:
                if quotes:
                   self.tag_add('str', '%s.%d'%(cline, start), '%s.%d'%(cline, i+1))
                   quotes = 0
                else:
                    start = i
                    quotes = 1
            elif buffer[i] == '#':
                self.tag_add('com', '%s.%d'%(cline, i), '%s.%d'%(cline, len(buffer)))
                break

        start, end = 0, 0
        obj_flag = 0
        for token in tokenized:
            end = start + len(token)+1
            if obj_flag:
                self.tag_add('obj', '%s.%d'%(cline, start), '%s.%d'%(cline, end))
                obj_flag = 0
            if token.strip() in keyword.kwlist:
                self.tag_add('kw', '%s.%d'%(cline, start), '%s.%d'%(cline, end))
                if token.strip() in ['def','class']: obj_flag = 1
            else:
                for index in range(len(token)):
                    try: int(token[index])
                    except ValueError: pass
                    else: self.tag_add('int', '%s.%d'%(cline, start+index))
            start += len(token)+1

if __name__ == '__main__':
    root = Tk()
    st = PyColour(root)
    st.pack(fill='both', expand='yes')
    root.mainloop()
</code>```

"pycolour.py" colours the python syntax in it's own text widget. I want to colour python syntax in text widget called "text_field" from "notepad.py", so i wrote try_to_colour function. the problem is that i do not understand why this function doesn't work properly.

答:
PyColour is not a utility class but a Text widget. It can only color itself. So what you need to do is replace

```<code>self.text_field = Text(self.frame, ....)
</code>```

with

```<code>self.text_field = PyColor(self.frame)
# find a different way to set font, etc.
</code>```

The problem is that PyColor calls self.tag_add() and similar methods in colourize(). These work only on the PyColor instance self, not on your widget.

Title: Python module problem

Body:
I have two .py files called "notepad.py" & "pycolour.py". First is in the parent folder, second is in "subdir" folder.
First file:

#notepad.py
from Tkinter import *
from subdir import pycolour

class SimpleNotepad(object):

    def __init__(self):
        self.frame = Frame(root, bg='#707070', bd=1)
        self.text_field = Text(self.frame, font='Verdana 10', bd=3, wrap='word')
        self.text_field.focus_set()
        self.btn = Button(root, text='try to colour', command=self.try_to_colour)

        self.coloured = pycolour.PyColour(root)

        self.frame.pack(expand=True, fill='both')
        self.text_field.pack(expand=True, fill='both')
        self.btn.pack()

    def try_to_colour(self):
        txt = self.text_field.get(1.0, 'end')
        text = str(txt).split('\n')
        for i in range(len(text)-1):
            self.coloured.colourize(i+1, len(text[i]))

root = Tk()
app = SimpleNotepad()
root.mainloop()

Second file:

#pycolour.py
from Tkinter import *
import keyword

#colorizing the Python code
class PyColour(Text):

    def __init__(self, parent):
        Text.__init__(self, parent)
        self.focus_set()
        self.tag_conf()

        self.bind('<Key>', self.key_pressed)

    tags = {'com' : '#009999',   #comment is darkgreen
            'str' : '#F50000',   #string is red
            'kw'  : '#F57A00',   #keyword is orange
            'obj' : '#7A00F5',   #function or class name is purple
            'int' : '#3333FF'    #integers is darkblue
            }

    def tag_conf(self):
        for tag, value in self.tags.items():
            self.tag_configure(tag, foreground=value)

    def tag_delete(self, start, end):
        for tag in self.tags.items():
            self.tag_remove(tag, start, end)

    def key_pressed(self, key):
        if key.char in ' :[(]),"\'':
            self.edit_separator() #for undo/redo

        cline = self.index(INSERT).split('.')[0]
        lastcol = 0
        char = self.get('%s.%d'%(cline, lastcol))
        while char != '\n':
            lastcol += 1
            char = self.get('%s.%d'%(cline, lastcol))

        self.colourize(cline,lastcol)

    def colourize(self, cline, lastcol):
        buffer = self.get('%s.%d'%(cline,0),'%s.%d'%(cline,lastcol))
        tokenized = buffer.split(' ')

        self.tag_remove('%s.%d'%(cline, 0), '%s.%d'%(cline, lastcol))

        quotes = 0
        start = 0
        for i in range(len(buffer)):
            if buffer[i] in ['"',"'"]:
                if quotes:
                   self.tag_add('str', '%s.%d'%(cline, start), '%s.%d'%(cline, i+1))
                   quotes = 0
                else:
                    start = i
                    quotes = 1
            elif buffer[i] == '#':
                self.tag_add('com', '%s.%d'%(cline, i), '%s.%d'%(cline, len(buffer)))
                break

        start, end = 0, 0
        obj_flag = 0
        for token in tokenized:
            end = start + len(token)+1
            if obj_flag:
                self.tag_add('obj', '%s.%d'%(cline, start), '%s.%d'%(cline, end))
                obj_flag = 0
            if token.strip() in keyword.kwlist:
                self.tag_add('kw', '%s.%d'%(cline, start), '%s.%d'%(cline, end))
                if token.strip() in ['def','class']: obj_flag = 1
            else:
                for index in range(len(token)):
                    try: int(token[index])
                    except ValueError: pass
                    else: self.tag_add('int', '%s.%d'%(cline, start+index))
            start += len(token)+1

if __name__ == '__main__':
    root = Tk()
    st = PyColour(root)
    st.pack(fill='both', expand='yes')
    root.mainloop()

"pycolour.py" colours the python syntax in it's own text widget. I want to colour python syntax in text widget called "text_field" from "notepad.py", so i wrote try_to_colour function. the problem is that i do not understand why this function doesn't work properly.

Body:
PyColour is not a utility class but a Text widget. It can only color itself. So what you need to do is replace

self.text_field = Text(self.frame, ....)

with

self.text_field = PyColor(self.frame)
# find a different way to set font, etc.

The problem is that PyColor calls self.tag_add() and similar methods in colourize(). These work only on the PyColor instance self, not on your widget.


问:
Comparing a bit to a boolean
Say I have a set of flags, encoded in a uint16_t flags. For example, AMAZING_FLAG = 0x02.
Now, I have a function. This function needs to check if I want to change the flag, because if I want to do that, I need to write to flash. And that is expensive. Therefore, I want a check which tells me if flags & AMAZING_FLAG is equal to doSet. This is the first idea:

```<code>setAmazingFlag(bool doSet)
{
    if ((flags & AMAZING_FLAG) != (doSet ? AMAZING_FLAG : 0)) {
        // Really expensive thing
        // Update flags
    }
}
</code>```

This is not an intuitive if statement.
I feel like there should be a better way, something like:

```<code>if ((flags & AMAZING_FLAG) != doSet){

}
</code>```

But this does not actually work, true seems to be equal to 0x01.

So, is there a neat way to compare a bit to a boolean?

答:
You can create a mask based on doSet value:

```<code>#define AMAZING_FLAG_IDX 1
#define AMAZING_FLAG (1u << AMAZING_FLAG_IDX)
...

uint16_t set_mask = doSet << AMAZING_FLAG_IDX;
</code>```

Now your check can look like this:

```<code>setAmazingFlag(bool doSet)
{
    const uint16_t set_mask = doSet << AMAZING_FLAG_IDX;

    if (flags & set_mask) {
        // Really expensive thing
        // Update flags
    }
}
</code>```

On some architectures, !! may be compiled to a branch and by this, you may have two branches:

Normalisation by !!(expr)

Compare to doSet

The advantage of my proposal is a guaranteed single branch.

Note: make sure you don't introduce undefined behaviour by shifting left by more than 30 (assuming integer is 32 bits). This can be easily achieved by a static_assert(AMAZING_FLAG_IDX < sizeof(int)*CHAR_BIT-1, "Invalid AMAZING_FLAG_IDX");

Title: Comparing a bit to a boolean

Body:
Say I have a set of flags, encoded in a uint16_t flags. For example, AMAZING_FLAG = 0x02.
Now, I have a function. This function needs to check if I want to change the flag, because if I want to do that, I need to write to flash. And that is expensive. Therefore, I want a check which tells me if flags & AMAZING_FLAG is equal to doSet. This is the first idea:

setAmazingFlag(bool doSet)
{
    if ((flags & AMAZING_FLAG) != (doSet ? AMAZING_FLAG : 0)) {
        // Really expensive thing
        // Update flags
    }
}

This is not an intuitive if statement.
I feel like there should be a better way, something like:

if ((flags & AMAZING_FLAG) != doSet){

}

But this does not actually work, true seems to be equal to 0x01.

So, is there a neat way to compare a bit to a boolean?

Body:
You can create a mask based on doSet value:

#define AMAZING_FLAG_IDX 1
#define AMAZING_FLAG (1u << AMAZING_FLAG_IDX)
...

uint16_t set_mask = doSet << AMAZING_FLAG_IDX;

Now your check can look like this:

setAmazingFlag(bool doSet)
{
    const uint16_t set_mask = doSet << AMAZING_FLAG_IDX;

    if (flags & set_mask) {
        // Really expensive thing
        // Update flags
    }
}

On some architectures, !! may be compiled to a branch and by this, you may have two branches:

Normalisation by !!(expr)

Compare to doSet

The advantage of my proposal is a guaranteed single branch.

Note: make sure you don't introduce undefined behaviour by shifting left by more than 30 (assuming integer is 32 bits). This can be easily achieved by a static_assert(AMAZING_FLAG_IDX < sizeof(int)*CHAR_BIT-1, "Invalid AMAZING_FLAG_IDX");


问:
Find any of the arrays in postgres column
Assuming I have a table with a column of type array, in my case double precision[].

I can't figure out how to find all the rows containing any of a list of arrays.

Example:

Let my column look like so:

What's the syntax for selecting the row containing, for example, {1,88} or {1,58}?

I tried a lot of variations of:

```<code>SELECT * FROM "table" WHERE "col1" = ANY('{1,88}, {1,58}');
</code>```

Thank you in advance

答:
You can use IN.

```<code>...
col1 IN ('{1, 88}'::double precision[],
         '{1, 58}'::double precision[])
...
</code>```

Body:
Assuming I have a table with a column of type array, in my case double precision[].

I can't figure out how to find all the rows containing any of a list of arrays.

Example:

Let my column look like so:

What's the syntax for selecting the row containing, for example, {1,88} or {1,58}?

I tried a lot of variations of:

SELECT * FROM "table" WHERE "col1" = ANY('{1,88}, {1,58}');

Thank you in advance

Body:
You can use IN.

...
col1 IN ('{1, 88}'::double precision[],
         '{1, 58}'::double precision[])
...


问:
Absolute/outer and inner namespace confusion in C#
```<code>using Foo.Uber;

namespace MyStuff.Foo
{
    class SomeClass{
        void DoStuff(){
            // I want to reference the outer "absolute" Foo.Uber
            // but the compiler thinks I'm refering to MyStuff.Foo.Uber
            var x = Foo.Uber.Bar();
        }
    }
}
</code>```

How could I solve this? Just moving the using statement inside my namespace doesn't help.

答:
You can do this using a namespace alias qualifier (typically global::) to refer to the default / root namespace:

global::Foo.Uber

Body:
using Foo.Uber;

namespace MyStuff.Foo
{
    class SomeClass{
        void DoStuff(){
            // I want to reference the outer "absolute" Foo.Uber
            // but the compiler thinks I'm refering to MyStuff.Foo.Uber
            var x = Foo.Uber.Bar();
        }
    }
}

How could I solve this? Just moving the using statement inside my namespace doesn't help.


答:
select name
from table
where name not in (
    select name
    from table
    where paid = 'Yes'
)
```

Title: Postgresql - How to get record from the same table that doesn't occur specific data

Body:
In PostgreSQL, if i have this kind of record :

|---------------------|------------------|
|      Name           |     Paid         |
|---------------------|------------------|
|        Teddy        |        No        |
|---------------------|------------------|
|        Andy         |        No        |
|---------------------|------------------|
|        Andy         |        Yes       |
|---------------------|------------------|
|        Sarah        |        Yes       |
|---------------------|------------------|
|        Michael      |        No        |
|---------------------|------------------|
|        Michael      |        Yes       |
|---------------------|------------------|

How do i get Names that don't have 'Yes' occurence in the table?

In this example the result should be : Teddy.

Thanks !


问:
How to show a button after a specific time period?
I am developing an android app in which when user click  on the in button, out button should be hidden for 8 hours or hours given by the user and after 8 hours out button should be visible and if the user forgot to click on the out button it should automatically mark him out.

I searched I come to know about android services but I didn't understand how to achieve this by using service.

Thanks for the help.

答:
Your question is too broad and unclear regarding your exact requirements. However, I can list a few points to help you get a logic.

Scenarios and Steps:

Initially, there are 2 buttons on the activity, In and Out.

Once the user clicks on the In button, simply hide the Out button, and save a timestamp in your SharedPreferences file.

With this, if the activity is still being displayed, the Out button will be hidden.

Create an AlarmManager as answered by Bhaskar Pasupula, but for 8 Hours ahead of the currentTime.

When your class receives the BroadcastReceiver, cross-check the difference between currentTimeStamp (fetched inside the receiver) and the timestamp saved in your SharedPreferences. If the difference is greater than equal to 8 hours, you can delete the key/value from the file.

Now, inside your activity, under onCreate method, fetch the value for the same key as used for saving the check-in timestamp. If the key is present, it means the user hasn't pressed the out button yet. The reason is simply that 8 hours haven't passed yet. Hence, hide the button before the activity is displayed to the user.

If the key is not present, it means, 8 hours has passed and hence, show the button.


问:
SOLR: Search spellcheck cases handling is not proper

Case 1:

If I search for "Name:blck", the result is as below -- which is correct

No result found and spellcheck suggestion for "black"

```<code>{
  "responseHeader":{
    "status":0,
    "QTime":119,
    "params":{
      "q":"Name:blck",
      "fl":"Name",
      "rows":"5",
      "_":"1582184918494"}},
  "response":{"numFound":0,"start":0,"docs":[]
  },
  "spellcheck":{
    "suggestions":[
      "blck",{
        "numFound":1,
        "startOffset":5,
        "endOffset":9,
        "origFreq":0,
        "suggestion":[{
            "word":"black",
            "freq":32435}]}],
    "correctlySpelled":false}}
</code>```

Case 2

If I search for "Name:blck+top", the result is as below -- here I need spellcheck for blck word

```<code>{
  "responseHeader":{
    "status":0,
    "QTime":48,
    "params":{
      "q":"Name:blck+top",
      "fl":"Name",
      "rows":"5",
      "_":"1582184918494"}},
  "response":{"numFound":14442,"start":0,"docs":[
      {
        "Name":"Shimmery Seamless Top"},
      {
        "Name":"Ruffle Sleeve Top"},
      {
        "Name":"Ruffle Sleeve Top"},
      {
        "Name":"Ruffle Sleeve Top"},
      {
        "Name":"Ruffle Sleeve Top"}]
  },
  "spellcheck":{
    "suggestions":[],
    "correctlySpelled":true}}
</code>```

So, Basically I just want to show the spellcheck suggestion if any word from the search is wrong.

答:
I get the result for my field which has below data in solr.

```<code>{
    "description":"Black Top"
},
{
    "description":"Sleeve Top"
},
{
    "description":"Seamless Top"
}
</code>```

My field is description and the its defined in the solr.xml as below.

```<code><field name="description" type="text_general"/>
</code>```

The query for the same is :

```<code>http://10.168.72.101:8983/solr/knowledge_combined/select?df=description&fl=description&q=blck%20top&spellcheck=on
</code>```

Here is the response I Received from solr.

Here is the spell check configuration added in the solr.config.xml

```<code><searchComponent name="spellcheck" class="solr.SpellCheckComponent">

    <str name="queryAnalyzerFieldType">text_general</str>

    <lst name="spellchecker">
      <str name="name">default</str>
      <str name="field">description</str>
      <str name="classname">solr.DirectSolrSpellChecker</str>
      <str name="distanceMeasure">internal</str>
      <float name="accuracy">0.5</float>
      <int name="maxEdits">2</int>
      <int name="minPrefix">1</int>
      <int name="maxInspections">5</int>
      <int name="minQueryLength">4</int>
      <float name="maxQueryFrequency">0.01</float>
    </lst>
</searchComponent>

<requestHandler name="/spell" class="solr.SearchHandler" startup="lazy">
    <lst name="defaults">
      <str name="spellcheck.dictionary">default</str>
      <str name="spellcheck">on</str>
      <str name="spellcheck.extendedResults">true</str>
      <str name="spellcheck.count">10</str>
      <str name="spellcheck.alternativeTermCount">5</str>
      <str name="spellcheck.maxResultsForSuggest">5</str>
      <str name="spellcheck.collate">true</str>
      <str name="spellcheck.collateExtendedResults">true</str>
      <str name="spellcheck.maxCollationTries">10</str>
      <str name="spellcheck.maxCollations">5</str>
    </lst>
    <arr name="last-components">
      <str>spellcheck</str>
    </arr>
</requestHandler>
</code>```

Body:
Case 1:

If I search for "Name:blck", the result is as below -- which is correct

No result found and spellcheck suggestion for "black"

{
  "responseHeader":{
    "status":0,
    "QTime":119,
    "params":{
      "q":"Name:blck",
      "fl":"Name",
      "rows":"5",
      "_":"1582184918494"}},
  "response":{"numFound":0,"start":0,"docs":[]
  },
  "spellcheck":{
    "suggestions":[
      "blck",{
        "numFound":1,
        "startOffset":5,
        "endOffset":9,
        "origFreq":0,
        "suggestion":[{
            "word":"black",
            "freq":32435}]}],
    "correctlySpelled":false}}

Case 2

If I search for "Name:blck+top", the result is as below -- here I need spellcheck for blck word

{
  "responseHeader":{
    "status":0,
    "QTime":48,
    "params":{
      "q":"Name:blck+top",
      "fl":"Name",
      "rows":"5",
      "_":"1582184918494"}},
  "response":{"numFound":14442,"start":0,"docs":[
      {
        "Name":"Shimmery Seamless Top"},
      {
        "Name":"Ruffle Sleeve Top"},
      {
        "Name":"Ruffle Sleeve Top"},
      {
        "Name":"Ruffle Sleeve Top"},
      {
        "Name":"Ruffle Sleeve Top"}]
  },
  "spellcheck":{
    "suggestions":[],
    "correctlySpelled":true}}

So, Basically I just want to show the spellcheck suggestion if any word from the search is wrong.

Body:
I get the result for my field which has below data in solr.

{
    "description":"Black Top"
},
{
    "description":"Sleeve Top"
},
{
    "description":"Seamless Top"
}

My field is description and the its defined in the solr.xml as below.

<field name="description" type="text_general"/>

The query for the same is :

http://10.168.72.101:8983/solr/knowledge_combined/select?df=description&fl=description&q=blck%20top&spellcheck=on

Here is the response I Received from solr.

Here is the spell check configuration added in the solr.config.xml

<searchComponent name="spellcheck" class="solr.SpellCheckComponent">

    <str name="queryAnalyzerFieldType">text_general</str>

    <lst name="spellchecker">
      <str name="name">default</str>
      <str name="field">description</str>
      <str name="classname">solr.DirectSolrSpellChecker</str>
      <str name="distanceMeasure">internal</str>
      <float name="accuracy">0.5</float>
      <int name="maxEdits">2</int>
      <int name="minPrefix">1</int>
      <int name="maxInspections">5</int>
      <int name="minQueryLength">4</int>
      <float name="maxQueryFrequency">0.01</float>
    </lst>
</searchComponent>

<requestHandler name="/spell" class="solr.SearchHandler" startup="lazy">
    <lst name="defaults">
      <str name="spellcheck.dictionary">default</str>
      <str name="spellcheck">on</str>
      <str name="spellcheck.extendedResults">true</str>
      <str name="spellcheck.count">10</str>
      <str name="spellcheck.alternativeTermCount">5</str>
      <str name="spellcheck.maxResultsForSuggest">5</str>
      <str name="spellcheck.collate">true</str>
      <str name="spellcheck.collateExtendedResults">true</str>
      <str name="spellcheck.maxCollationTries">10</str>
      <str name="spellcheck.maxCollations">5</str>
    </lst>
    <arr name="last-components">
      <str>spellcheck</str>
    </arr>
</requestHandler>


问:
VB Compiler Conversion from string to number
This is more of a curiosity question on what the VB compiler is doing. Basically the following code generates an error,

```<code>    If "String" = CInt(1) Then

    End If
</code>```

As it should. What makes me curious is the error reported is

```<code>Conversion from string "String" to type 'Double' is not valid.
</code>```

So, I guess my question is, why is the compiler attempting to convert to a Double when i would assume it should be converting to Integer?

答:
Following can give some hint.

For following

```<code>If "String" = CInt(1) Then

End If
</code>```

The innerexception stacktrace shows

```<code>at Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(String Value, NumberFormatInfo NumberFormat)
at Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(String Value)
</code>```

Even if you change the statement as

If "String" = CDbl(1) Then

or

If "String" = CDec(1) Then

It still shows the innerexception stacktrace  as given above.

It means it has to do nothing with the right hand side value. It is behavior of compiler while doing implicit conversion to convert the string to more accommodating data type which is double(long would be too long-pun intended ).

This behavior can be proved by changing the statement to :

```<code>If CInt("String") = CLng(1) Then

End If
</code>```

For this the innerexception stacktrace shows

```<code>at Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat)
at Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value)
</code>```

Which means even for explicit type conversion it first tries to convert the string to double(most accommodating) and then converts it to integer.

Body:
This is more of a curiosity question on what the VB compiler is doing. Basically the following code generates an error,

    If "String" = CInt(1) Then

    End If

As it should. What makes me curious is the error reported is

Conversion from string "String" to type 'Double' is not valid.

So, I guess my question is, why is the compiler attempting to convert to a Double when i would assume it should be converting to Integer?

Body:
Following can give some hint.

For following

If "String" = CInt(1) Then

End If

The innerexception stacktrace shows

at Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(String Value, NumberFormatInfo NumberFormat)
at Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(String Value)

Even if you change the statement as

If "String" = CDbl(1) Then

or

If "String" = CDec(1) Then

It still shows the innerexception stacktrace  as given above.

It means it has to do nothing with the right hand side value. It is behavior of compiler while doing implicit conversion to convert the string to more accommodating data type which is double(long would be too long-pun intended ).

This behavior can be proved by changing the statement to :

If CInt("String") = CLng(1) Then

End If

For this the innerexception stacktrace shows

at Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat)
at Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value)

Which means even for explicit type conversion it first tries to convert the string to double(most accommodating) and then converts it to integer.


问:
Problems when Upgrading to the newest version of JHipster
After reading the chapter on Upgrading to the newest version of JHipster from Full Stack Develpment with Jhipster I executed the upgrade and everything when ok, but when it finished, all the changes in files like: src\main\java\es\mibar\web\config\SecurityConfiguration.java are gone.

That includes changes that I need after my upgrade, so the question is:

Is there any flag that keeps my files and at the same time upgrades the version? Because going through every change is quite long.

The ones detailed here https://www.jhipster.tech/upgrading-an-application/ do not look like helpful in that aspect

```<code>@Override
public void configure(HttpSecurity http) throws Exception {
    // @formatter:off
    http
        .csrf()
        .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
    .and()
        .addFilterBefore(corsFilter, CsrfFilter.class)
        .exceptionHandling()
            .authenticationEntryPoint(problemSupport)
            .accessDeniedHandler(problemSupport)
    .and()
        .rememberMe()
        .rememberMeServices(rememberMeServices)
        .rememberMeParameter("remember-me")
        .key(jHipsterProperties.getSecurity().getRememberMe().getKey())
    .and()
        .formLogin()
        .loginProcessingUrl("/api/authentication")
        .successHandler(ajaxAuthenticationSuccessHandler())
        .failureHandler(ajaxAuthenticationFailureHandler())
        .permitAll()
    .and()
        .logout()
        .logoutUrl("/api/logout")
        .logoutSuccessHandler(ajaxLogoutSuccessHandler())
        .permitAll()
    // .and()
    //     .headers()
    //     .contentSecurityPolicy("default-src 'self'; frame-src 'self' data:; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://storage.googleapis.com; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:")
    // .and()
    //     .referrerPolicy(ReferrerPolicyHeaderWriter.ReferrerPolicy.STRICT_ORIGIN_WHEN_CROSS_ORIGIN)
    // .and()
    //     .featurePolicy("geolocation 'none'; midi 'none'; sync-xhr 'none'; microphone 'none'; camera 'none'; magnetometer 'none'; gyroscope 'none'; speaker 'none'; fullscreen 'self'; payment 'none'")
    // .and()
    //     .frameOptions()
    //     .deny()
    .and()
        .authorizeRequests()
        .antMatchers("/api/authenticate").permitAll()
        .antMatchers("/api/register").permitAll()
        .antMatchers("/api/activate").permitAll()
        .antMatchers("/api/account/reset-password/init").permitAll()
        .antMatchers("/api/account/reset-password/finish").permitAll()
        .antMatchers("/api/feedbacks").permitAll()
        .antMatchers("/api/menudailyfollowers").permitAll()
        .antMatchers("/api/config-variables").permitAll()
        .antMatchers("/api/locals").permitAll()
        .antMatchers("/api/locations").permitAll()
        .antMatchers("/api/degustation-menus").permitAll()
        .antMatchers("/api/daily-menus").permitAll()
        .antMatchers("/api/courses").permitAll()
        .antMatchers("/api/tags").permitAll()
        .antMatchers("/api/payments").permitAll()
        .antMatchers("/api/photos").permitAll()
        .antMatchers("/api/spectacles").permitAll()
        .antMatchers("/api/comments").permitAll()
        .antMatchers("/api/feedbacks").permitAll()
        .antMatchers("/api/menudailyfollowers").permitAll()
        .antMatchers(HttpMethod.GET, "/api/locals/*/view").permitAll()
        .antMatchers(HttpMethod.GET, "/api/locals/*").permitAll()
        .antMatchers(HttpMethod.GET, "/api/locations/*/view").permitAll()
        .antMatchers(HttpMethod.GET, "/api/locations/*").permitAll()
        .antMatchers(HttpMethod.GET, "/api/degustation-menus/*/view").permitAll()
        .antMatchers(HttpMethod.GET, "/api/degustation-menus/*").permitAll()
        .antMatchers(HttpMethod.GET, "/api/daily-menus/*/view").permitAll()
        .antMatchers(HttpMethod.GET, "/api/daily-menus/*").permitAll()
        .antMatchers(HttpMethod.GET, "/api/courses/*/view").permitAll()
        .antMatchers(HttpMethod.GET, "/api/courses/*").permitAll()
        .antMatchers(HttpMethod.GET, "/api/tags/*").permitAll()
        .antMatchers(HttpMethod.GET, "/api/payments/*").permitAll()
        .antMatchers(HttpMethod.GET, "/api/photos/*/view").permitAll()
        .antMatchers(HttpMethod.GET, "/api/photos/*").permitAll()
        .antMatchers(HttpMethod.GET, "/api/spectacles/*/view").permitAll()
        .antMatchers(HttpMethod.GET, "/api/spectacles/*").permitAll()
        .antMatchers(HttpMethod.GET, "/api/comments/*/view").permitAll()
        .antMatchers(HttpMethod.GET, "/api/comments/*").permitAll()
        .antMatchers(HttpMethod.GET, "/feedbacks/new").permitAll()
        .antMatchers(HttpMethod.GET, "/menudailyfollowers/new").permitAll()
        .antMatchers("/api/**").authenticated()
        .antMatchers("/management/health").permitAll()
        .antMatchers("/management/info").permitAll()
        .antMatchers("/management/prometheus").permitAll()
        .antMatchers("/management/**").hasAuthority(AuthoritiesConstants.ADMIN);
    // @formatter:on
}
</code>```

}

The part that is in // comments and all the added             .antMatchers("/api/locations").permitAll() are gone.

Thanks

答: Beside the possibility to control changes via git, you may want to follow these tipps: https://stackoverflow.com/a/60258520/7773582</p>

Body:
After reading the chapter on Upgrading to the newest version of JHipster from Full Stack Develpment with Jhipster I executed the upgrade and everything when ok, but when it finished, all the changes in files like: src\main\java\es\mibar\web\config\SecurityConfiguration.java are gone.

That includes changes that I need after my upgrade, so the question is:

Is there any flag that keeps my files and at the same time upgrades the version? Because going through every change is quite long.

The ones detailed here https://www.jhipster.tech/upgrading-an-application/ do not look like helpful in that aspect

@Override
public void configure(HttpSecurity http) throws Exception {
    // @formatter:off
    http
        .csrf()
        .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
    .and()
        .addFilterBefore(corsFilter, CsrfFilter.class)
        .exceptionHandling()
            .authenticationEntryPoint(problemSupport)
            .accessDeniedHandler(problemSupport)
    .and()
        .rememberMe()
        .rememberMeServices(rememberMeServices)
        .rememberMeParameter("remember-me")
        .key(jHipsterProperties.getSecurity().getRememberMe().getKey())
    .and()
        .formLogin()
        .loginProcessingUrl("/api/authentication")
        .successHandler(ajaxAuthenticationSuccessHandler())
        .failureHandler(ajaxAuthenticationFailureHandler())
        .permitAll()
    .and()
        .logout()
        .logoutUrl("/api/logout")
        .logoutSuccessHandler(ajaxLogoutSuccessHandler())
        .permitAll()
    // .and()
    //     .headers()
    //     .contentSecurityPolicy("default-src 'self'; frame-src 'self' data:; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://storage.googleapis.com; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:")
    // .and()
    //     .referrerPolicy(ReferrerPolicyHeaderWriter.ReferrerPolicy.STRICT_ORIGIN_WHEN_CROSS_ORIGIN)
    // .and()
    //     .featurePolicy("geolocation 'none'; midi 'none'; sync-xhr 'none'; microphone 'none'; camera 'none'; magnetometer 'none'; gyroscope 'none'; speaker 'none'; fullscreen 'self'; payment 'none'")
    // .and()
    //     .frameOptions()
    //     .deny()
    .and()
        .authorizeRequests()
        .antMatchers("/api/authenticate").permitAll()
        .antMatchers("/api/register").permitAll()
        .antMatchers("/api/activate").permitAll()
        .antMatchers("/api/account/reset-password/init").permitAll()
        .antMatchers("/api/account/reset-password/finish").permitAll()
        .antMatchers("/api/feedbacks").permitAll()
        .antMatchers("/api/menudailyfollowers").permitAll()
        .antMatchers("/api/config-variables").permitAll()
        .antMatchers("/api/locals").permitAll()
        .antMatchers("/api/locations").permitAll()
        .antMatchers("/api/degustation-menus").permitAll()
        .antMatchers("/api/daily-menus").permitAll()
        .antMatchers("/api/courses").permitAll()
        .antMatchers("/api/tags").permitAll()
        .antMatchers("/api/payments").permitAll()
        .antMatchers("/api/photos").permitAll()
        .antMatchers("/api/spectacles").permitAll()
        .antMatchers("/api/comments").permitAll()
        .antMatchers("/api/feedbacks").permitAll()
        .antMatchers("/api/menudailyfollowers").permitAll()
        .antMatchers(HttpMethod.GET, "/api/locals/*/view").permitAll()
        .antMatchers(HttpMethod.GET, "/api/locals/*").permitAll()
        .antMatchers(HttpMethod.GET, "/api/locations/*/view").permitAll()
        .antMatchers(HttpMethod.GET, "/api/locations/*").permitAll()
        .antMatchers(HttpMethod.GET, "/api/degustation-menus/*/view").permitAll()
        .antMatchers(HttpMethod.GET, "/api/degustation-menus/*").permitAll()
        .antMatchers(HttpMethod.GET, "/api/daily-menus/*/view").permitAll()
        .antMatchers(HttpMethod.GET, "/api/daily-menus/*").permitAll()
        .antMatchers(HttpMethod.GET, "/api/courses/*/view").permitAll()
        .antMatchers(HttpMethod.GET, "/api/courses/*").permitAll()
        .antMatchers(HttpMethod.GET, "/api/tags/*").permitAll()
        .antMatchers(HttpMethod.GET, "/api/payments/*").permitAll()
        .antMatchers(HttpMethod.GET, "/api/photos/*/view").permitAll()
        .antMatchers(HttpMethod.GET, "/api/photos/*").permitAll()
        .antMatchers(HttpMethod.GET, "/api/spectacles/*/view").permitAll()
        .antMatchers(HttpMethod.GET, "/api/spectacles/*").permitAll()
        .antMatchers(HttpMethod.GET, "/api/comments/*/view").permitAll()
        .antMatchers(HttpMethod.GET, "/api/comments/*").permitAll()
        .antMatchers(HttpMethod.GET, "/feedbacks/new").permitAll()
        .antMatchers(HttpMethod.GET, "/menudailyfollowers/new").permitAll()
        .antMatchers("/api/**").authenticated()
        .antMatchers("/management/health").permitAll()
        .antMatchers("/management/info").permitAll()
        .antMatchers("/management/prometheus").permitAll()
        .antMatchers("/management/**").hasAuthority(AuthoritiesConstants.ADMIN);
    // @formatter:on
}

}

The part that is in // comments and all the added             .antMatchers("/api/locations").permitAll() are gone.

Thanks