问:
Select last Order state from Orders
I have tables:

orders:

```<code>id_order    id_customer
1           1
2           2
3           1
</code>```

orders_history

```<code>id_history  id_order    id_order_state    date_add
1           1           1                 2010-01-01 00:00:00
2           1           2                 2010-01-02 00:00:00
3           1           3                 2010-01-03 00:00:00
4           2           2                 2010-05-01 00:00:00
5           2           3                 2011-05-02 00:00:00
6           3           1                 2011-05-03 00:00:00
7           3           2                 2011-06-01 00:00:00
</code>```

order_state

```<code>id_order_state    name
1                 New
2                 Sent
3                 Rejected
4                 ...
</code>```

How to get all order_id's where last id_order_state of that order (by last I mean this with MAX(id_history) or MAX(date_add)) is not equal 1 or 3?

答:
I'm using "answer for my question" because I need to post results of your queries. So.

Unfortunately not all of your answers guys works. Let's prepare test environment:

```<code>CREATE TABLE `order_history` (
`id_order_history` int(11) NOT NULL AUTO_INCREMENT,
`id_order` int(11) NOT NULL,
`id_order_state` int(11) NOT NULL,
`date_add` datetime NOT NULL,
PRIMARY KEY (`id_order_history`)
) ENGINE=MyISAM AUTO_INCREMENT=11 DEFAULT CHARSET=latin2;

CREATE TABLE `orders` (
`id_order` int(11) NOT NULL AUTO_INCREMENT,
`id_customer` int(11) DEFAULT NULL,
PRIMARY KEY (`id_order`)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=latin2;

INSERT INTO `order_history`
(`id_order_history`, `id_order`, `id_order_state`,     `date_add`) VALUES
(1,1,1,'2011-01-01 00:00:00'),
(2,1,2,'2011-01-01 00:10:00'),
(3,1,3,'2011-01-01 00:20:00'),
(4,2,1,'2011-02-01 00:00:00'),
(5,2,2,'2011-02-01 00:25:01'),
(6,2,3,'2011-02-01 00:25:59'),
(7,3,1,'2011-03-01 00:00:01'),
(8,3,2,'2011-03-01 00:00:02'),
(9,3,3,'2011-03-01 00:01:00'),
(10,3,2,'2011-03-02 00:00:01');
COMMIT;

INSERT INTO `orders` (`id_order`, `id_customer`) VALUES
(1,1),
(2,2),
(3,3),
(4,4),
(5,5),
(6,6),
(7,7);
COMMIT;
</code>```

Now, lets select Last/Max State for each Order, so let's run simple query:

```<code> select id_order, max(date_add) as MaxDate
 from `order_history`
 group by id_order
</code>```

this gives us PROPER results, no rocket science right now:

```<code>id_order         MaxDate
---------+-------------------
1         2011-01-01 00:20:00  //last order_state=3
2         2011-02-01 00:25:59  //last order_state=3
3         2011-03-02 00:00:01  //last order_state=2
</code>```

Now for simplicity, lest change our queries to get Orders where Last State is not equal 3.

We're expecting to get one row result with id_order = 3.

So let's test our queries:

QUERY 1 made by RedFilter:

```<code>select oh.id_order, oh.id_order_state, oh.date_add
from (
    select id_order, max(date_add) as MaxDate
    from `order_history`
    where id_order_state not in (3)
    group by id_order
) ohm
inner join `order_history` oh on ohm.id_order = oh.id_order
and ohm.MaxDate = oh.date_add
</code>```

Result:

```<code>id_order    id_order_state    date_add
-------------------------------------------------
1           2                 2011-01-01 00:10:00
2           2                 2011-02-01 00:25:01
3           2                 2011-03-02 00:00:01
</code>```

So it's not true

QUERY 2 made by Tom H.:

```<code>SELECT DISTINCT OH1.id_order
FROM order_history OH1
LEFT OUTER JOIN order_history OH2 ON
  OH2.id_order = OH1.id_order AND
  OH2.id_order_state NOT IN (3) AND
  OH2.`id_order_history` >= OH1.`id_order_history`
WHERE
  OH2.id_order IS NULL
</code>```

Result:

```<code>id_order
--------
1
2
</code>```

So it's not true

Any suggestions appreciated.

EDIT

Thanks to Andriy M. comment we have proper solution. It's a modification of Tom H. query all should looks as follow:

```<code>SELECT DISTINCT
OH1.id_order
FROM
  order_history OH1
LEFT OUTER JOIN order_history OH2 ON
  OH2.id_order = OH1.id_order
AND OH2.date_add > OH1.date_add
WHERE OH1.id_order_state NOT IN (3) AND OH2.id_order IS NULL
</code>```

EDIT 2:

QUERY 3 made by DRapp:

```<code>select
  distinct orders.`id_order`
from
  ( select oh.id_order,
           max( oh.id_order_history ) LastID_HistoryPerOrder
       from
           order_history oh
       group by
           oh.id_order ) PreQuery
  join order_history oh2
     on PreQuery.id_order = oh2.id_order
     AND PreQuery.LastID_HistoryPerOrder = oh2.id_order_history
     AND NOT oh2.id_order_state IN (1,3)
join orders
  on PreQuery.id_order = orders.id_order
</code>```

Result:

```<code>id_order
--------
3
</code>```

So it's finally true

Title: Select last Order state from Orders

Body:
I have tables:

orders:

id_order    id_customer
1           1
2           2
3           1

orders_history

id_history  id_order    id_order_state    date_add
1           1           1                 2010-01-01 00:00:00
2           1           2                 2010-01-02 00:00:00
3           1           3                 2010-01-03 00:00:00
4           2           2                 2010-05-01 00:00:00
5           2           3                 2011-05-02 00:00:00
6           3           1                 2011-05-03 00:00:00
7           3           2                 2011-06-01 00:00:00

order_state

id_order_state    name
1                 New
2                 Sent
3                 Rejected
4                 ...

How to get all order_id's where last id_order_state of that order (by last I mean this with MAX(id_history) or MAX(date_add)) is not equal 1 or 3?

Body:
I'm using "answer for my question" because I need to post results of your queries. So.

Unfortunately not all of your answers guys works. Let's prepare test environment:

CREATE TABLE `order_history` (
`id_order_history` int(11) NOT NULL AUTO_INCREMENT,
`id_order` int(11) NOT NULL,
`id_order_state` int(11) NOT NULL,
`date_add` datetime NOT NULL,
PRIMARY KEY (`id_order_history`)
) ENGINE=MyISAM AUTO_INCREMENT=11 DEFAULT CHARSET=latin2;

CREATE TABLE `orders` (
`id_order` int(11) NOT NULL AUTO_INCREMENT,
`id_customer` int(11) DEFAULT NULL,
PRIMARY KEY (`id_order`)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=latin2;

INSERT INTO `order_history`
(`id_order_history`, `id_order`, `id_order_state`,     `date_add`) VALUES
(1,1,1,'2011-01-01 00:00:00'),
(2,1,2,'2011-01-01 00:10:00'),
(3,1,3,'2011-01-01 00:20:00'),
(4,2,1,'2011-02-01 00:00:00'),
(5,2,2,'2011-02-01 00:25:01'),
(6,2,3,'2011-02-01 00:25:59'),
(7,3,1,'2011-03-01 00:00:01'),
(8,3,2,'2011-03-01 00:00:02'),
(9,3,3,'2011-03-01 00:01:00'),
(10,3,2,'2011-03-02 00:00:01');
COMMIT;

INSERT INTO `orders` (`id_order`, `id_customer`) VALUES
(1,1),
(2,2),
(3,3),
(4,4),
(5,5),
(6,6),
(7,7);
COMMIT;

Now, lets select Last/Max State for each Order, so let's run simple query:

 select id_order, max(date_add) as MaxDate
 from `order_history`
 group by id_order

this gives us PROPER results, no rocket science right now:

id_order         MaxDate
---------+-------------------
1         2011-01-01 00:20:00  //last order_state=3
2         2011-02-01 00:25:59  //last order_state=3
3         2011-03-02 00:00:01  //last order_state=2

Now for simplicity, lest change our queries to get Orders where Last State is not equal 3.

We're expecting to get one row result with id_order = 3.

So let's test our queries:

QUERY 1 made by RedFilter:

select oh.id_order, oh.id_order_state, oh.date_add
from (
    select id_order, max(date_add) as MaxDate
    from `order_history`
    where id_order_state not in (3)
    group by id_order
) ohm
inner join `order_history` oh on ohm.id_order = oh.id_order
and ohm.MaxDate = oh.date_add

Result:

id_order    id_order_state    date_add
-------------------------------------------------
1           2                 2011-01-01 00:10:00
2           2                 2011-02-01 00:25:01
3           2                 2011-03-02 00:00:01

So it's not true

QUERY 2 made by Tom H.:

SELECT DISTINCT OH1.id_order
FROM order_history OH1
LEFT OUTER JOIN order_history OH2 ON
  OH2.id_order = OH1.id_order AND
  OH2.id_order_state NOT IN (3) AND
  OH2.`id_order_history` >= OH1.`id_order_history`
WHERE
  OH2.id_order IS NULL

Result:

id_order
--------
1
2

So it's not true

Any suggestions appreciated.

EDIT

Thanks to Andriy M. comment we have proper solution. It's a modification of Tom H. query all should looks as follow:

SELECT DISTINCT
OH1.id_order
FROM
  order_history OH1
LEFT OUTER JOIN order_history OH2 ON
  OH2.id_order = OH1.id_order
AND OH2.date_add > OH1.date_add
WHERE OH1.id_order_state NOT IN (3) AND OH2.id_order IS NULL

EDIT 2:

QUERY 3 made by DRapp:

select
  distinct orders.`id_order`
from
  ( select oh.id_order,
           max( oh.id_order_history ) LastID_HistoryPerOrder
       from
           order_history oh
       group by
           oh.id_order ) PreQuery
  join order_history oh2
     on PreQuery.id_order = oh2.id_order
     AND PreQuery.LastID_HistoryPerOrder = oh2.id_order_history
     AND NOT oh2.id_order_state IN (1,3)
join orders
  on PreQuery.id_order = orders.id_order

Result:

id_order
--------
3

So it's finally true


来源: stackexchange

问:
Jekyll: Operation not permitted @ apply2files
I am using Windows 10's Linux subsystem where I installed Jekyll but when I run bundle exec jekyll serve it gives me:

jekyll 3.8.6 | Error:  Operation not permitted @ apply2files - /mnt/e/Work/project/_site/images/banner.jpg

I know there are some incompatibilities with Windows and Linux permissions, because all my files are owned by root for some reason, but there is a fix to this issue?

I found an answer here https://scottdorman.blog/2019/02/27/running-jekyll-on-wsl/ where the author says I have to close and reopen the bash, but it doesn't work.

答: As @Mike Cole mentioned above in the comments: If you are using wsl this can happen. I resolved this by restarting my machine and magically it works now. </p>

ContentLicense: CC BY-SA 4.0


问:
Azure python sdk, how to deploy a vm and it's a Azure Spot instance
Azure python sdk,
how to deploy a vm and it's a Azure Spot instance</p>

答:
If you want to create Azure Spot VM, please refer to the following code. For more deatils, please refer to the doucment and the docuemnt

```<code>from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.compute.v2019_07_01 import ComputeManagementClient
from azure.mgmt.compute.v2019_07_01.models import VirtualMachinePriorityTypes, VirtualMachineEvictionPolicyTypes, BillingProfile
SUBSCRIPTION_ID = 'subscription-id'
GROUP_NAME = 'myResourceGroup'
LOCATION = 'westus'
VM_NAME = 'myVM'
credentials = ServicePrincipalCredentials(
        client_id = 'application-id',
        secret = 'authentication-key',
        tenant = 'tenant-id'
    )
compute_client = ComputeManagementClient(
    credentials,
    SUBSCRIPTION_ID
)
vm_parameters = {
        'location': LOCATION,
        'os_profile': {
            'computer_name': VM_NAME,
            'admin_username': 'azureuser',
            'admin_password': 'Azure12345678'
        },
        'hardware_profile': {
            'vm_size': 'Standard_DS1'
        },
        'storage_profile': {
            'image_reference': {
                'publisher': 'MicrosoftWindowsServer',
                'offer': 'WindowsServer',
                'sku': '2012-R2-Datacenter',
                'version': 'latest'
            }
        },
        'network_profile': {
            'network_interfaces': [{
                'id': nic.id
            }]
        },
        'priority':VirtualMachinePriorityTypes.spot, # use Azure spot intance
        'eviction_policy':VirtualMachineEvictionPolicyTypes.deallocate , #For Azure Spot virtual machines, the only supported value is 'Deallocate'
        'billing_profile': BillingProfile(max_price=float(2))
creation_result = compute_client.virtual_machines.create_or_update(
        GROUP_NAME,
        VM_NAME,
        vm_parameters
    )
print(creation_result.result())
    }
</code>```

Body:
If you want to create Azure Spot VM, please refer to the following code. For more deatils, please refer to the doucment and the docuemnt

from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.compute.v2019_07_01 import ComputeManagementClient
from azure.mgmt.compute.v2019_07_01.models import VirtualMachinePriorityTypes, VirtualMachineEvictionPolicyTypes, BillingProfile
SUBSCRIPTION_ID = 'subscription-id'
GROUP_NAME = 'myResourceGroup'
LOCATION = 'westus'
VM_NAME = 'myVM'
credentials = ServicePrincipalCredentials(
        client_id = 'application-id',
        secret = 'authentication-key',
        tenant = 'tenant-id'
    )
compute_client = ComputeManagementClient(
    credentials,
    SUBSCRIPTION_ID
)
vm_parameters = {
        'location': LOCATION,
        'os_profile': {
            'computer_name': VM_NAME,
            'admin_username': 'azureuser',
            'admin_password': 'Azure12345678'
        },
        'hardware_profile': {
            'vm_size': 'Standard_DS1'
        },
        'storage_profile': {
            'image_reference': {
                'publisher': 'MicrosoftWindowsServer',
                'offer': 'WindowsServer',
                'sku': '2012-R2-Datacenter',
                'version': 'latest'
            }
        },
        'network_profile': {
            'network_interfaces': [{
                'id': nic.id
            }]
        },
        'priority':VirtualMachinePriorityTypes.spot, # use Azure spot intance
        'eviction_policy':VirtualMachineEvictionPolicyTypes.deallocate , #For Azure Spot virtual machines, the only supported value is 'Deallocate'
        'billing_profile': BillingProfile(max_price=float(2))
creation_result = compute_client.virtual_machines.create_or_update(
        GROUP_NAME,
        VM_NAME,
        vm_parameters
    )
print(creation_result.result())
    }


问:
Asp.Net MVC WebApi CORS Request fails - no proposed solutions affecting the outcome
I have an Asp.Net NVC5 web application (running under Visual Studio 2017) on localhost:59569 (SiteApi).  I have a second website (also running under Visual Studio 2017) on localhost:61527 (SiteClient) where a page once loaded makes the following api call to SiteApi:

```<code>$http({
    url: 'http://localhost:59569/api/V2/' + alias,
    method: 'POST',
    data: pm,
    xhrFields: { withCredentials: true },
    headers: { 'Content-Type': 'application/json; charset=utf-8' }
    })
    .then(th, ex);
</code>```

NOTE: I have tried this with and without the xhrFields + withCredentials information using Microsoft IE, Microsoft Edge and Chrome.

Back on SiteApi the resulting preflight call for OPTIONS is intercepted by the following code in Global.asax which executes exactly as written and I can trace through the if statement when an inbound call for OPTIONS triggers it.

```<code>protected void Application_BeginRequest()
{
    if (Request.Headers.AllKeys.Contains("Origin") && Request.HttpMethod == "OPTIONS")
    {
        Response.Clear();
        Response.Headers.Add("Access-Control-Allow-Origin", "*");
        Response.Headers.Add("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With, Session");
        Response.Flush();
        Response.End();
    }
}
</code>```

The intention being to send the desired headers back to the client to allow CORS to function properly - however immediately after this code is executed the web page back on SiteClient reports that the request has been blocked due to missing 'Access-Control-Allow-Origin' header is missing and I can see that none of the headers I have specified have made it back to the client.

In an attempt to have CORS work I have the following nuget packages installed on the SiteAPI project.

Microsoft.AspNet.Cors

Microsoft.AspNet.WebApi.Cors

I adjusted the WebApiConfig.Register() method to include:

```<code>   // Web API configuration and services
   config.EnableCors();
</code>```

I have tried many variations of adding the filter attributes to my controller like so:

```<code>[EnableCors("*", "*", "*", SupportsCredentials = true)]
</code>```

I have tried adding my own custom ActionFilterAttribute from solutions found in other CORS related questions on stackoverflow - for example (among various others):

```<code>    public override void OnActionExecuting(System.Web.Mvc.ActionExecutingContext filterContext)
    {
        filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
        base.OnActionExecuting(filterContext);
</code>```

I have the following to my web.config file:

```<code><handlers>
  <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
  <remove name="OPTIONSVerbHandler" />
  <remove name="TRACEVerbHandler" />
  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</code>```

I have ALL these solutions live in my project and in spite of this I still get CORS errors on the client side.

So the caveat here is that I also have a custom filter that looks up security on each API call - which works fine with ALL calls made from pages running on SiteApi.  In the case of calls from SiteClient (CORS calls) the security filter never fires at all though I am getting 401 errors reported on the client in addition to the errors due to the missing CORS related headers.

I have cleared the caches of all browsers and the server itself.  This is my first time working with CORS and I'm already exhausted from working with what really should be a simple solution.  Looking for solutions here and would appreciate some help from those in the know.

Request Headers:

```<code>Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Connection: keep-alive
Content-Length: 2
Content-Type: application/json; charset=UTF-8
Host: localhost:59569
Origin: http://localhost:61527
Referer: http://localhost:61527/Home/Index
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-site
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.106 Safari/537.36 Edg/80.0.361.54
</code>```

Response Headers:

```<code>Cache-Control: private
Content-Length: 6115
Content-Type: text/html; charset=utf-8
Date: Wed, 19 Feb 2020 00:46:06 GMT
Server: Microsoft-IIS/10.0
WWW-Authenticate: Negotiate
WWW-Authenticate: NTLM
X-Powered-By: ASP.NET
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcbngyMDA4MjZcRGV2XFRlY2hJVFxFQVNJV2ViQXBwXGFwaVxWMlxHZXRDb21tYW5kcw==?=
</code>```

答:
This is the flow I use.  Sometimes browsers don't like "*".  Other times, browsers don't like localhost, either.  This is the logic I use (modify the allow headers as you see fit).  It could be the fact that you aren't allowing the access control headers in your allowed headers, too: </p>

<p>[Add this to Global.asax]</p>

```<code>protected void Application_BeginRequest(object sender, EventArgs e)
{
        var originKey =
            Request.Headers.AllKeys.FirstOrDefault(
                a => a.Equals("origin", StringComparison.InvariantCultureIgnoreCase));

        if (originKey != null && Request.HttpMethod == "OPTIONS"))
        {
// Optional Whitelist check here can return without the headers below to reject CORS
            Response.Headers.Add("Access-Control-Allow-Origin", Request.Headers[originKey]);
            Response.Headers.Add("Access-Control-Allow-Credentials", "true");
            Response.Headers.Add("Access-Control-Allow-Methods", "GET, POST, PUSH, DELETE, OPTIONS");
            Response.Headers.Add("Access-Control-Allow-Headers",
                "Authorization, Content-Type, Access-Control-Allow-Headers, X-Requested-With, Access-Control-Allow-Method, Accept");

            Response.Flush();
            Response.End();
            return;
        }
}
</code>```

Body:
I have an Asp.Net NVC5 web application (running under Visual Studio 2017) on localhost:59569 (SiteApi).  I have a second website (also running under Visual Studio 2017) on localhost:61527 (SiteClient) where a page once loaded makes the following api call to SiteApi:

$http({
    url: 'http://localhost:59569/api/V2/' + alias,
    method: 'POST',
    data: pm,
    xhrFields: { withCredentials: true },
    headers: { 'Content-Type': 'application/json; charset=utf-8' }
    })
    .then(th, ex);

NOTE: I have tried this with and without the xhrFields + withCredentials information using Microsoft IE, Microsoft Edge and Chrome.

Back on SiteApi the resulting preflight call for OPTIONS is intercepted by the following code in Global.asax which executes exactly as written and I can trace through the if statement when an inbound call for OPTIONS triggers it.

protected void Application_BeginRequest()
{
    if (Request.Headers.AllKeys.Contains("Origin") && Request.HttpMethod == "OPTIONS")
    {
        Response.Clear();
        Response.Headers.Add("Access-Control-Allow-Origin", "*");
        Response.Headers.Add("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With, Session");
        Response.Flush();
        Response.End();
    }
}

The intention being to send the desired headers back to the client to allow CORS to function properly - however immediately after this code is executed the web page back on SiteClient reports that the request has been blocked due to missing 'Access-Control-Allow-Origin' header is missing and I can see that none of the headers I have specified have made it back to the client.

In an attempt to have CORS work I have the following nuget packages installed on the SiteAPI project.

Microsoft.AspNet.Cors

Microsoft.AspNet.WebApi.Cors

I adjusted the WebApiConfig.Register() method to include:

   // Web API configuration and services
   config.EnableCors();

I have tried many variations of adding the filter attributes to my controller like so:

[EnableCors("*", "*", "*", SupportsCredentials = true)]

I have tried adding my own custom ActionFilterAttribute from solutions found in other CORS related questions on stackoverflow - for example (among various others):

    public override void OnActionExecuting(System.Web.Mvc.ActionExecutingContext filterContext)
    {
        filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
        base.OnActionExecuting(filterContext);

I have the following to my web.config file:

<handlers>
  <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
  <remove name="OPTIONSVerbHandler" />
  <remove name="TRACEVerbHandler" />
  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>

I have ALL these solutions live in my project and in spite of this I still get CORS errors on the client side.

So the caveat here is that I also have a custom filter that looks up security on each API call - which works fine with ALL calls made from pages running on SiteApi.  In the case of calls from SiteClient (CORS calls) the security filter never fires at all though I am getting 401 errors reported on the client in addition to the errors due to the missing CORS related headers.

I have cleared the caches of all browsers and the server itself.  This is my first time working with CORS and I'm already exhausted from working with what really should be a simple solution.  Looking for solutions here and would appreciate some help from those in the know.

Request Headers:

Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Connection: keep-alive
Content-Length: 2
Content-Type: application/json; charset=UTF-8
Host: localhost:59569
Origin: http://localhost:61527
Referer: http://localhost:61527/Home/Index
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-site
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.106 Safari/537.36 Edg/80.0.361.54

Response Headers:

Cache-Control: private
Content-Length: 6115
Content-Type: text/html; charset=utf-8
Date: Wed, 19 Feb 2020 00:46:06 GMT
Server: Microsoft-IIS/10.0
WWW-Authenticate: Negotiate
WWW-Authenticate: NTLM
X-Powered-By: ASP.NET
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcbngyMDA4MjZcRGV2XFRlY2hJVFxFQVNJV2ViQXBwXGFwaVxWMlxHZXRDb21tYW5kcw==?=

Body:
This is the flow I use.  Sometimes browsers don't like "*".  Other times, browsers don't like localhost, either.  This is the logic I use (modify the allow headers as you see fit).  It could be the fact that you aren't allowing the access control headers in your allowed headers, too:

[Add this to Global.asax]

protected void Application_BeginRequest(object sender, EventArgs e)
{
        var originKey =
            Request.Headers.AllKeys.FirstOrDefault(
                a => a.Equals("origin", StringComparison.InvariantCultureIgnoreCase));

        if (originKey != null && Request.HttpMethod == "OPTIONS"))
        {
// Optional Whitelist check here can return without the headers below to reject CORS
            Response.Headers.Add("Access-Control-Allow-Origin", Request.Headers[originKey]);
            Response.Headers.Add("Access-Control-Allow-Credentials", "true");
            Response.Headers.Add("Access-Control-Allow-Methods", "GET, POST, PUSH, DELETE, OPTIONS");
            Response.Headers.Add("Access-Control-Allow-Headers",
                "Authorization, Content-Type, Access-Control-Allow-Headers, X-Requested-With, Access-Control-Allow-Method, Accept");

            Response.Flush();
            Response.End();
            return;
        }
}


问:
How to reverse order of lines in a div while keeping the style, order of words, etc
I want to reverse my line order while keeping the style of words and the order I created.

HTML:

```<code><textarea id="mytext"">
    Buy - 3 Potatoes $6.25
    Sold - 3 Napkins $7.12
    Buy - 5  Fries $11.62
    Sold - 7 Potatoes $14.32
</textarea>

<div id = "myresult"></div>
</code>```

As you can see I did the order of words and style the words I just need to reverse the lines now.
I'm working on notepad++ and still pretty new to Javascript.

Script:

```<code>  var mytext = document.getElementById('mytext');
  var result = document.getElementById('myresult');
        var lines = mytext.value.split('<br>');
        result.innerHTML = '';
        for(let i = 0;i < lines.length;i++){
        var line = lines[i];
        var list = [];
        list.unshift(line);
        list.unshift("<br>");
        var word = line.split(' ');
        var n1 = line.match(/[0-9]+/g);
        var n2 = line.match(/[0-9]+\.[0-9]+/g);
        var check = line.match(/Buy/);
        var check2 = line.match(/Sell/);
if(check) {
        result.innerHTML += "<span style='color:green;'>" + word[0] + "</span>" + ' ' +  word[2] + ' ' + word[3] + ' total:$' + (n1[0]*[n2]).toFixed(2) + "<br>";
        }
     else if(check2) {
        result.innerHTML += "<span style='color:blue;'>" + word[0] + "</span>" + ' ' +  word[2] + ' ' + word[3] + ' total: $' + (n1[0]*[n2]).toFixed(2) + "<br>";
        }
    else {
        result.innerHTMl + " ";
    }

}
</code>```

As you can see I already style the colors and fix the order. I just need to reverse the line and display it in innerHTML.

My idea was to make a array[], and then for every line in the for loop try to unshift the line and br but everytime it give errors sometimes it gives me a bunch of commas, and one time it it displays no colors or give me a bunch of random words.

Result should look like what is after the snippet run. This is what I want innerHTMl after the code is run. The code has to display innerHTMl inside the div please.

  Sold 7 Potatoes total: $100.24

  Buy 5 Fries total: $58.1

  Sold 3 Napkins total: $21.36

  Buy 3 Potatoes total: $18.75

```

答:
In continuation of my comment above, you could use js reverse() before anything else on creating new elements.

E.g.

```<code>var lines = mytext.value.split("\n").reverse();
</code>```

Body:
I want to reverse my line order while keeping the style of words and the order I created.

HTML:

<textarea id="mytext"">
    Buy - 3 Potatoes $6.25
    Sold - 3 Napkins $7.12
    Buy - 5  Fries $11.62
    Sold - 7 Potatoes $14.32
</textarea>

As you can see I did the order of words and style the words I just need to reverse the lines now.
I'm working on notepad++ and still pretty new to Javascript.

Script:

  var mytext = document.getElementById('mytext');
  var result = document.getElementById('myresult');
        var lines = mytext.value.split('
');
        result.innerHTML = '';
        for(let i = 0;i < lines.length;i++){
        var line = lines[i];
        var list = [];
        list.unshift(line);
        list.unshift("
");
        var word = line.split(' ');
        var n1 = line.match(/[0-9]+/g);
        var n2 = line.match(/[0-9]+\.[0-9]+/g);
        var check = line.match(/Buy/);
        var check2 = line.match(/Sell/);
if(check) {
        result.innerHTML += "" + word[0] + "" + ' ' +  word[2] + ' ' + word[3] + ' total:$' + (n1[0]*[n2]).toFixed(2) + "
";
        }
     else if(check2) {
        result.innerHTML += "" + word[0] + "" + ' ' +  word[2] + ' ' + word[3] + ' total: $' + (n1[0]*[n2]).toFixed(2) + "
";
        }
    else {
        result.innerHTMl + " ";
    }

}

As you can see I already style the colors and fix the order. I just need to reverse the line and display it in innerHTML.

My idea was to make a array[], and then for every line in the for loop try to unshift the line and br but everytime it give errors sometimes it gives me a bunch of commas, and one time it it displays no colors or give me a bunch of random words.

Result should look like what is after the snippet run. This is what I want innerHTMl after the code is run. The code has to display innerHTMl inside the div please.

  Sold 7 Potatoes total: $100.24

  Buy 5 Fries total: $58.1

  Sold 3 Napkins total: $21.36

  Buy 3 Potatoes total: $18.75

Body:
In continuation of my comment above, you could use js reverse() before anything else on creating new elements.

E.g.

var lines = mytext.value.split("\n").reverse();


问:
Capitalizing hyphenated name
I have a script looking like this:

```<code>firstn = input('Please enter your first name: ')
lastn = input('Please enter Your last name: ')
print('Good day, ' + str.capitalize(firstn) + ' ' + str.capitalize(lastn)+ '!')
</code>```

It will work nicely with simple names like jack black or morgan meeman but when I input hyphenated name like jordan-bellfort image then I'd expect "Jordan-Bellfort Image" but I receive "Jordan-bellfort Image".

How can I get python to capitalize the character right after hyphen?

答:
This is a real problem! It is seemingly easily solved with .title() and the like but these suggestions do not solve the actual problem of processing any human name. As in McCormack, O'Brien or de Araugo.

Luckily this is a problem that has been solved. See nameparser

```<code>>>> from nameparser import HumanName
>>> name = HumanName('Shirley Maclaine') # Don't change mixed case names
>>> name.capitalize(force=True)
>>> str(name)
'Shirley MacLaine'
</code>```

ContentLicense: CC BY-SA 3.0

Title: Capitalizing hyphenated name

Body:
I have a script looking like this:

firstn = input('Please enter your first name: ')
lastn = input('Please enter Your last name: ')
print('Good day, ' + str.capitalize(firstn) + ' ' + str.capitalize(lastn)+ '!')

It will work nicely with simple names like jack black or morgan meeman but when I input hyphenated name like jordan-bellfort image then I'd expect "Jordan-Bellfort Image" but I receive "Jordan-bellfort Image".

How can I get python to capitalize the character right after hyphen?

Body:
This is a real problem! It is seemingly easily solved with .title() and the like but these suggestions do not solve the actual problem of processing any human name. As in McCormack, O'Brien or de Araugo.

Luckily this is a problem that has been solved. See nameparser

>>> from nameparser import HumanName
>>> name = HumanName('Shirley Maclaine') # Don't change mixed case names
>>> name.capitalize(force=True)
>>> str(name)
'Shirley MacLaine'


问:
What is the way to make kubernetes nodes have `providerID` spec after creation (manually)?
I'm expecting that kubectl get nodes <node> -o yaml to show the spec.providerID (see reference below) once the kubelet has been provided the additional flag --provider-id=provider://nodeID.  I've used /etc/default/kubelet file to add more flags to the command line when kubelet is start/restarted.  (On a k8s 1.16 cluster)  I see the additional flags via a systemctl status kubelet --no-pager call, so the file is respected.

However, I've not seen the value get returned by kubectl get node <node> -o yaml call.  I was thinking it had to be that the node was already registered, but I think kubectl re-registers when it starts up.  I've seen the log line via journalctl -u kubelet suggest that it has gone through registration.

How can I add a provider ID to a node manually?

https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#nodespec-v1-core

答:
You can edit the node config and append providerID information under spec section.</p>

```<code>kubectl edit node <Node Name>

...

spec:
   podCIDR:
   providerID:
</code>```

Body:
You can edit the node config and append providerID information under spec section.

kubectl edit node <Node Name>

...

spec:
   podCIDR:
   providerID:


问:
Mongoose query for documents that got likes on last 24 hours
I have this article schema:

```<code>   var article  = new mongoose.Schema({

    ....

        likes: [{
                    id: ObjectId,
                    by: {type: ObjectId, ref: 'User'},
                    created_at: {type: Date, default: Date.now}

         }],

....
}, {timestamps: {createdAt: 'created_at'}});
</code>```

on which I store the likes that the story gets. As you can see the field likes stores users who liked the article and the time they liked the story.

Now I want to query all the articles that got likes in the last 24 hours. How is that possible to do in Mongoose?

答:
You can use $max to get last like date and then use $match to check if that date is greater that 24 hours ago:</p>

```<code>Model.aggregate([
    {
        $addFields: {
            lastDate: { $max: "$likes.created_at" }
        }
    },
    {
        $match: {
            lastDate: {
                $gte: new Date(ISODate().getTime() - 1000 * 60 * 60 * 24 )
            }
        }
    }
])
</code>```

Body:
I have this article schema:

   var article  = new mongoose.Schema({

    ....

        likes: [{
                    id: ObjectId,
                    by: {type: ObjectId, ref: 'User'},
                    created_at: {type: Date, default: Date.now}

         }],

....
}, {timestamps: {createdAt: 'created_at'}});

on which I store the likes that the story gets. As you can see the field likes stores users who liked the article and the time they liked the story.

Now I want to query all the articles that got likes in the last 24 hours. How is that possible to do in Mongoose?

Body:
You can use $max to get last like date and then use $match to check if that date is greater that 24 hours ago:

Model.aggregate([
    {
        $addFields: {
            lastDate: { $max: "$likes.created_at" }
        }
    },
    {
        $match: {
            lastDate: {
                $gte: new Date(ISODate().getTime() - 1000 * 60 * 60 * 24 )
            }
        }
    }
])


问:
ASP.NET MVC app works in debug but not when published using Plesk and GoDaddy
I have an MVC app that works perfectly in debug mode and testing, but after I publish using ftp to godaddy, the website opens up and works when toggling between the home page and the login page. If I try logging in I get an error or if I try using the search box on my homepage, I also get an error. Here is the screenshot:

After I log in or basically anything or than 'mydomain'.com/Home/Index/ I get this error, if I try 'mydomain'.com/Home/Search or 'mydomain'.com/Search/Index any other controller or action I get this. What could be the cause because I have tried everything? For example is HomeController/Search:

```<code>public ActionResult Search(string search, string searchlast)
    {
        var doctors = from d in db.Doctors
                      select d;
        if (!String.IsNullOrEmpty(search) || !String.IsNullOrEmpty(searchlast))
        {
            doctors = doctors.Where(d => d.FirstName.Contains(search) && d.LastName.Contains(searchlast));
            return View(doctors.ToList());
        }
        return RedirectToAction("Index", "Home");
    }
</code>```

Here is Views/Home/Search which gives the error:

```<code>@model IEnumerable<Systemz.Models.Check>
@{
    ViewBag.Title = "Search";
}

<div class="jumbotron">
    <h2>Check</h2>
    <h4>Check for availability.</h4>
</div>

@using (Html.BeginForm("Search", "Home", FormMethod.Post))
{
    <p>
        First Name: @Html.TextBox("search") Last Name: @Html.TextBox("searchlast")
        <input type="submit" value="Search" name="search" />
        <a asp-action="Search" asp-controller="Home"></a>
    </p>
}

<table class="table table-hover dataTable">
    <div class="row">
        <div class="col-md-4">
            <thead class="bd-dark white-text">
                <tr>
                    <th>
                        Title:
                    </th>
                    <th>
                        First Name:
                    </th>
                    <th>
                        Last Name:
                    </th>
                    <th>
                        Address:
                    </th>
                    <th>
                        Is Available?
                    </th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model)
                {
                    <tr>
                        <td>
                            @Html.DisplayFor(modelItem => item.Title)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.FirstName)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.LastName)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.Address)
                        </td>
                        <td>
                            @if (item.IsAvailable == true)
                            {<p><a class="btn btn-success"></a></p> }
                            else
                            { <p><a class="btn btn-danger"></a></p>}
                        </td>
                    </tr>
                }
            </tbody>
        </div>
    </div>
</table>
</code>```

答:
You have to upload your database.  There is no such thing as (LocalDb) on your Godaddy hosted server.  You have to upload your database to Godaddy as well, figure out the connection string to that location, and then have that deploy correctly with your site.  If there isn't any firewalls in front of it, you can even test the connection to that database from your local machine.

Edit: And it isn't enough to just upload your database file.  You have to setup the database through their own tool so it attaches correctly and all that.

Body:
I have an MVC app that works perfectly in debug mode and testing, but after I publish using ftp to godaddy, the website opens up and works when toggling between the home page and the login page. If I try logging in I get an error or if I try using the search box on my homepage, I also get an error. Here is the screenshot:

After I log in or basically anything or than 'mydomain'.com/Home/Index/ I get this error, if I try 'mydomain'.com/Home/Search or 'mydomain'.com/Search/Index any other controller or action I get this. What could be the cause because I have tried everything? For example is HomeController/Search:

public ActionResult Search(string search, string searchlast)
    {
        var doctors = from d in db.Doctors
                      select d;
        if (!String.IsNullOrEmpty(search) || !String.IsNullOrEmpty(searchlast))
        {
            doctors = doctors.Where(d => d.FirstName.Contains(search) && d.LastName.Contains(searchlast));
            return View(doctors.ToList());
        }
        return RedirectToAction("Index", "Home");
    }

Here is Views/Home/Search which gives the error:

@model IEnumerable<Systemz.Models.Check>
@{
    ViewBag.Title = "Search";
}

Check

Check for availability.

@using (Html.BeginForm("Search", "Home", FormMethod.Post))
{

        First Name: @Html.TextBox("search") Last Name: @Html.TextBox("searchlast")
        <input type="submit" value="Search" name="search" />

}

                        Title:

                        First Name:

                        Last Name:

                        Address:

                        Is Available?

                @foreach (var item in Model)
                {

                            @Html.DisplayFor(modelItem => item.Title)

                            @Html.DisplayFor(modelItem => item.FirstName)

                            @Html.DisplayFor(modelItem => item.LastName)

                            @Html.DisplayFor(modelItem => item.Address)

                            @if (item.IsAvailable == true)
                            {

 }
                            else
                            {

}

                }


问:
Loss of data type when converting numpy.ndarry.tolist?
So I have an ndarray similar to this example:

```<code>dates = ['3/1/2020','4/15/2020','7/21/2020']
darray = np.asarray([dateutil.parser.parse(d) for d in dates], dtype='datetime64[ns]')

>>> array(['2020-03-01T00:00:00.000000000', '2020-04-15T00:00:00.000000000',
   '2020-07-21T00:00:00.000000000'], dtype='datetime64[ns]')

darray.tolist()

>>> [1583020800000000000, 1586908800000000000, 1595289600000000000]
</code>```

So I'm assuming it's converting to number of nanoseconds since the POSIX origin (1970-01-01). Is there a way to avoid this loss of data type?

答:
Normally tolist is better than list.  It works all-the-way down, and is faster.  list just iterates on the first dimension.  But the conversion to native Python types depends on dtype.  In this case the time units make a difference.

```<code>In [559]: arr = np.array(['2020-03-01T00:00:00.000000000', '2020-04-15T00:00:00.000000000',
     ...:    '2020-07-21T00:00:00.000000000'], dtype='datetime64[ns]')
In [560]: arr.shape
Out[560]: (3,)
In [561]: arr.dtype
Out[561]: dtype('<M8[ns]')
</code>```

list is the equivalent of [x for x in arr], iteration on the first dimension:

```<code>In [562]: list(arr)
Out[562]:
[numpy.datetime64('2020-03-01T00:00:00.000000000'),
 numpy.datetime64('2020-04-15T00:00:00.000000000'),
 numpy.datetime64('2020-07-21T00:00:00.000000000')]
</code>```

tolist converts it to Python objects - all the way down:

```<code>In [563]: arr.tolist()
Out[563]: [1583020800000000000, 1586908800000000000, 1595289600000000000]
</code>```

While ns gives an integer, Other time units give different results:

```<code>In [564]: arr.astype('datetime64[D]')
Out[564]: array(['2020-03-01', '2020-04-15', '2020-07-21'], dtype='datetime64[D]')
In [565]: arr.astype('datetime64[D]').tolist()
Out[565]:
[datetime.date(2020, 3, 1),
 datetime.date(2020, 4, 15),
 datetime.date(2020, 7, 21)]

In [566]: arr.astype('datetime64[s]').tolist()
Out[566]:
[datetime.datetime(2020, 3, 1, 0, 0),
 datetime.datetime(2020, 4, 15, 0, 0),
 datetime.datetime(2020, 7, 21, 0, 0)]
</code>```

Body:
So I have an ndarray similar to this example:

dates = ['3/1/2020','4/15/2020','7/21/2020']
darray = np.asarray([dateutil.parser.parse(d) for d in dates], dtype='datetime64[ns]')

>>> array(['2020-03-01T00:00:00.000000000', '2020-04-15T00:00:00.000000000',
   '2020-07-21T00:00:00.000000000'], dtype='datetime64[ns]')

darray.tolist()

>>> [1583020800000000000, 1586908800000000000, 1595289600000000000]

So I'm assuming it's converting to number of nanoseconds since the POSIX origin (1970-01-01). Is there a way to avoid this loss of data type?

Body:
Normally tolist is better than list.  It works all-the-way down, and is faster.  list just iterates on the first dimension.  But the conversion to native Python types depends on dtype.  In this case the time units make a difference.

In [559]: arr = np.array(['2020-03-01T00:00:00.000000000', '2020-04-15T00:00:00.000000000',
     ...:    '2020-07-21T00:00:00.000000000'], dtype='datetime64[ns]')
In [560]: arr.shape
Out[560]: (3,)
In [561]: arr.dtype
Out[561]: dtype('<M8[ns]')

list is the equivalent of [x for x in arr], iteration on the first dimension:

In [562]: list(arr)
Out[562]:
[numpy.datetime64('2020-03-01T00:00:00.000000000'),
 numpy.datetime64('2020-04-15T00:00:00.000000000'),
 numpy.datetime64('2020-07-21T00:00:00.000000000')]

tolist converts it to Python objects - all the way down:

In [563]: arr.tolist()
Out[563]: [1583020800000000000, 1586908800000000000, 1595289600000000000]

While ns gives an integer, Other time units give different results:

In [564]: arr.astype('datetime64[D]')
Out[564]: array(['2020-03-01', '2020-04-15', '2020-07-21'], dtype='datetime64[D]')
In [565]: arr.astype('datetime64[D]').tolist()
Out[565]:
[datetime.date(2020, 3, 1),
 datetime.date(2020, 4, 15),
 datetime.date(2020, 7, 21)]

In [566]: arr.astype('datetime64[s]').tolist()
Out[566]:
[datetime.datetime(2020, 3, 1, 0, 0),
 datetime.datetime(2020, 4, 15, 0, 0),
 datetime.datetime(2020, 7, 21, 0, 0)]


问:
How do you echo a 4-digit Unicode character in Bash?
I'd like to add the Unicode skull and crossbones to my shell prompt (specifically the 'SKULL AND CROSSBONES' (U+2620)), but I can't figure out the magic incantation to make echo spit it, or any other, 4-digit Unicode character. Two-digit one's are easy. For example, echo -e "\x55", .

In addition to the answers below it should be noted that, obviously, your terminal needs to support Unicode for the output to be what you expect. gnome-terminal does a good job of this, but it isn't necessarily turned on by default.

On macOS's Terminal app Go to Preferences-> Encodings and choose Unicode (UTF-8).

答:
Just put "☠" in your shell script. In the correct locale and on a Unicode-enabled console it'll print just fine:

```<code>$ echo ☠
☠
$
</code>```

An ugly "workaround" would be to output the UTF-8 sequence, but that also depends on the encoding used:

```<code>$ echo -e '\xE2\x98\xA0'
☠
$
</code>```

OwnerDisplayName: masukomi

LastEditorDisplayName: masukomi

Body:
Just put "☠" in your shell script. In the correct locale and on a Unicode-enabled console it'll print just fine:

$ echo ☠
☠
$

An ugly "workaround" would be to output the UTF-8 sequence, but that also depends on the encoding used:

$ echo -e '\xE2\x98\xA0'
☠
$

OwnerDisplayName: saua


问:
String to new object with parameters
How do i create a new instance of an object from a string?

I want to do this:

```<code>Event event = new Event("hello");
event.setName("nice!");
</code>```

but only having

```<code>String object = "Event";
String object_variable_name = "event";
String object_params = "hello";
</code>```

Is this possible?

答:
You can instantiate a class with the reflection API. But you need the full class name, the simple name (= with no constructor) is not enough.

```<code>Class clazz = Class.forName("com.example.Event");
Constructor constructor = clazz.getConstructor(String.class);
Object instance = constructor.newInstance("hello");
</code>```

Assigning it to a variable where the variables name and type are stored in Strings is not possible. The usual pattern to implement this is to use a map:

```<code>Map<String, Object> events = new HashMap<String, Object>();
events.put("event", event);
</code>```

Title: String to new object with parameters

Body:
How do i create a new instance of an object from a string?

I want to do this:

Event event = new Event("hello");
event.setName("nice!");

but only having

String object = "Event";
String object_variable_name = "event";
String object_params = "hello";

Is this possible?

Body:
You can instantiate a class with the reflection API. But you need the full class name, the simple name (= with no constructor) is not enough.

Class clazz = Class.forName("com.example.Event");
Constructor constructor = clazz.getConstructor(String.class);
Object instance = constructor.newInstance("hello");

Assigning it to a variable where the variables name and type are stored in Strings is not possible. The usual pattern to implement this is to use a map:

Map<String, Object> events = new HashMap<String, Object>();
events.put("event", event);


问:
Creating a dataframe from list of lists: Column Data Repeating for Each Record
I have a list of lists that I'm trying to convert into a dataframe. As the title suggests, each record contains the column name and the value. Here's a modified example of what my list looks like:

```<code>    sample_list =
        [['record',''],
        ['fname','John'],
        ['lname','Smith'],
        ['bdate','1985-06-15'],
        ['record',''],
        ['fname','Mary'],
        ['lname','Smith'],
        ['bdate','1990-03-12'],
                          ...]
</code>```

I'm coming from the VBA world and I'd probably handle something like this with a loop. However, I've been warned to not do that for really large datasets (happens to be case where this conversion is needed). What method should I use to give me the following result:

```<code>fname | lname | bdate
John  | Smith | 1985-06-15
Mary  | Smith | 1990-03-12
...
</code>```

Any help or pointing in the right direction is very appreciated.

答:
There are a few possible solutions in this style, I settled on one which I think is decent. I know you said the input is rather large, and you were worried about the effects of an explicit loop, so let me know if this fares poorly.

import pandas as pd

raw_data = [['record', ''],
            ['fname', 'John'],
            ['lname', 'Smith'],
            ['bdate', '1985-06-15'],
            ['record', ''],
            ['fname', 'Mary'],
            ['lname', 'Smith'],
            ['bdate', '1990-03-12']]

# generates dictionaries mapping the column name to the value, for each record
rec_dicts = (dict(raw_data[i + 1:i + 4]) for i in range(0, len(raw_data), 4))

df = pd.DataFrame(rec_dicts)
df['bdate'] = pd.to_datetime(df['bdate'])

print(df)
```

<p>Output:</p>

<pre class="lang-none prettyprint-override"><code>  fname  lname      bdate
0  John  Smith 1985-06-15
1  Mary  Smith 1990-03-12
</code>```

Here is a solution which accounts for missing/different data in records.

import pandas as pd

raw_data = [['record', ''],
            ['fname', 'John'],
            ['lname', 'Smith'],
            ['bdate', '1985-06-15'],
            ['other', 'a value'],
            ['record', ''],
            ['fname', 'Mary'],
            ['lname', 'Smith'],
            ['bdate', '1990-03-12']]

rec_dicts = []
curr_rec = {}

for curr_key, curr_val in raw_data:
    if curr_key == "record":
        rec_dicts.append(curr_rec)
        curr_rec = {}
    else:
        curr_rec[curr_key] = curr_val
rec_dicts.append(curr_rec)

if not rec_dicts[0]:
    rec_dicts = rec_dicts[1:]

print(rec_dicts, end='\n\n')

df = pd.DataFrame(data=rec_dicts)
df['bdate'] = pd.to_datetime(df['bdate'])

print(df)
```

<p>Output:</p>

<pre class="lang-none prettyprint-override"><code>[{'fname': 'John', 'lname': 'Smith', 'bdate': '1985-06-15', 'other': 'a value'}, {'fname': 'Mary', 'lname': 'Smith', 'bdate': '1990-03-12'}]

  fname  lname      bdate    other
0  John  Smith 1985-06-15  a value
1  Mary  Smith 1990-03-12      NaN
</code>```

As with the previous method, there are many possible variations. Which one is best will depend on the specifics of your program.

Body:
I have a list of lists that I'm trying to convert into a dataframe. As the title suggests, each record contains the column name and the value. Here's a modified example of what my list looks like:

    sample_list =
        [['record',''],
        ['fname','John'],
        ['lname','Smith'],
        ['bdate','1985-06-15'],
        ['record',''],
        ['fname','Mary'],
        ['lname','Smith'],
        ['bdate','1990-03-12'],
                          ...]

I'm coming from the VBA world and I'd probably handle something like this with a loop. However, I've been warned to not do that for really large datasets (happens to be case where this conversion is needed). What method should I use to give me the following result:

fname | lname | bdate
John  | Smith | 1985-06-15
Mary  | Smith | 1990-03-12
...

Any help or pointing in the right direction is very appreciated.

Body:
There are a few possible solutions in this style, I settled on one which I think is decent. I know you said the input is rather large, and you were worried about the effects of an explicit loop, so let me know if this fares poorly.

import pandas as pd

raw_data = [['record', ''],
            ['fname', 'John'],
            ['lname', 'Smith'],
            ['bdate', '1985-06-15'],
            ['record', ''],
            ['fname', 'Mary'],
            ['lname', 'Smith'],
            ['bdate', '1990-03-12']]

# generates dictionaries mapping the column name to the value, for each record
rec_dicts = (dict(raw_data[i + 1:i + 4]) for i in range(0, len(raw_data), 4))

df = pd.DataFrame(rec_dicts)
df['bdate'] = pd.to_datetime(df['bdate'])

print(df)

Output:

  fname  lname      bdate
0  John  Smith 1985-06-15
1  Mary  Smith 1990-03-12

Here is a solution which accounts for missing/different data in records.

import pandas as pd

raw_data = [['record', ''],
            ['fname', 'John'],
            ['lname', 'Smith'],
            ['bdate', '1985-06-15'],
            ['other', 'a value'],
            ['record', ''],
            ['fname', 'Mary'],
            ['lname', 'Smith'],
            ['bdate', '1990-03-12']]

rec_dicts = []
curr_rec = {}

for curr_key, curr_val in raw_data:
    if curr_key == "record":
        rec_dicts.append(curr_rec)
        curr_rec = {}
    else:
        curr_rec[curr_key] = curr_val
rec_dicts.append(curr_rec)

if not rec_dicts[0]:
    rec_dicts = rec_dicts[1:]

print(rec_dicts, end='\n\n')

df = pd.DataFrame(data=rec_dicts)
df['bdate'] = pd.to_datetime(df['bdate'])

print(df)

Output:

[{'fname': 'John', 'lname': 'Smith', 'bdate': '1985-06-15', 'other': 'a value'}, {'fname': 'Mary', 'lname': 'Smith', 'bdate': '1990-03-12'}]

  fname  lname      bdate    other
0  John  Smith 1985-06-15  a value
1  Mary  Smith 1990-03-12      NaN

As with the previous method, there are many possible variations. Which one is best will depend on the specifics of your program.


问:
Ensure python script isn't run as root
My first python program (hopefully finished in a thousand years) breaks if run as root, such as with sudo, due to dbus stuff. How can I ensure it wasn't called with sudo prefix or from root prompt # instead of regular user prompt $?

答:
Put this into your Python program/script:

```<code>#import os

if os.geteuid() == 0:
    exit("You cannot call 'mmm' using using 'sudo' powers. Exiting.")
</code>```

Of course you would replace mmm with your own python filename.

Title: Ensure python script isn't run as root

Body:
Put this into your Python program/script:

#import os

if os.geteuid() == 0:
    exit("You cannot call 'mmm' using using 'sudo' powers. Exiting.")

Of course you would replace mmm with your own python filename.


问:
Asp.net razor dropdown list is not populated
I use Microsoft's code to localize my first web app.

I see here that the list is not empty

```<code>@{
    var requestCulture = Context.Features.Get<IRequestCultureFeature>();
    var cultureItems = LocOptions.Value.SupportedUICultures
        .Select(c => new SelectListItem { Value = c.Name, Text = c.DisplayName })
        .ToList();
    var returnUrl = string.IsNullOrEmpty(Context.Request.Path) ? "~/" : $"~{Context.Request.Path.Value}";
}
</code>```

But the dropdown list is empty

```<code><div title="@Localizer["Request culture provider:"] @requestCulture?.Provider?.GetType().Name">
    <form id="selectLanguage" asp-controller="Home"
          asp-action="SetLanguage" asp-route-returnUrl="@returnUrl"
          method="post" class="form-horizontal" role="form">
        <label asp-for="@requestCulture.RequestCulture.UICulture.Name">@Localizer["Language:"]</label> <select name="culture"
          onchange="this.form.submit();"
          asp-for="@requestCulture.RequestCulture.UICulture.Name" asp-items="cultureItems">
        </select>
    </form>
</div>
</code>```

What is wrong in this code?

The generated HTML looks like this

```<code>    <div title="Request culture provider: AcceptLanguageHeaderRequestCultureProvider">
        <form id="selectLanguage" asp-controller="Home" asp-action="SetLanguage" asp-route-returnUrl="/" method="post" class="form-horizontal" role="form">
            Language: <select name="culture" asp-for="de" asp-items="cultureItems"></select>
        </form>
    </div>
</code>```

Here is another example which also has the same issues. https://github.com/aspnet/Entropy/blob/master/samples/Localization.StarterWeb/Views/Shared/_SelectLanguagePartial.cshtml

答:
If you would like to use @Html.DropDownList,try to use below code:

```<code>@{
    var requestCulture = Context.Features.Get<IRequestCultureFeature>();
    var cultureItems = LocOptions.Value.SupportedUICultures
        .Select(c => new SelectListItem { Value = c.Name, Text = c.DisplayName ,Selected = (c.Name == requestCulture.RequestCulture.UICulture.Name )})
        .ToList();
    var returnUrl = string.IsNullOrEmpty(Context.Request.Path) ? "~/" : $"~{Context.Request.Path.Value}";
}
<div title="@Localizer["Request culture provider:"] @requestCulture?.Provider?.GetType().Name">
    <form id="selectLanguage" asp-controller="Home"
          asp-action="SetLanguage" asp-route-returnUrl="@returnUrl"
          method="post" class="form-horizontal" role="form">
        <label asp-for="@requestCulture.RequestCulture.UICulture.Name">@Localizer["Language:"]</label>

        @Html.DropDownList("culture", cultureItems, new
           {
               onchange = @"this.form.submit();"
           })

    </form>
</div>
</code>```

Action:

```<code>[HttpPost]
public IActionResult SetLanguage(string culture, string returnUrl)
</code>```

Body:
I use Microsoft's code to localize my first web app.

I see here that the list is not empty

@{
    var requestCulture = Context.Features.Get<IRequestCultureFeature>();
    var cultureItems = LocOptions.Value.SupportedUICultures
        .Select(c => new SelectListItem { Value = c.Name, Text = c.DisplayName })
        .ToList();
    var returnUrl = string.IsNullOrEmpty(Context.Request.Path) ? "~/" : $"~{Context.Request.Path.Value}";
}

But the dropdown list is empty

    <form id="selectLanguage" asp-controller="Home"
          asp-action="SetLanguage" asp-route-returnUrl="@returnUrl"
          method="post" class="form-horizontal" role="form">
        @Localizer["Language:"] <select name="culture"
          onchange="this.form.submit();"
          asp-for="@requestCulture.RequestCulture.UICulture.Name" asp-items="cultureItems">
        </select>
    </form>

What is wrong in this code?

The generated HTML looks like this

        <form id="selectLanguage" asp-controller="Home" asp-action="SetLanguage" asp-route-returnUrl="/" method="post" class="form-horizontal" role="form">
            Language: <select name="culture" asp-for="de" asp-items="cultureItems"></select>
        </form>

Here is another example which also has the same issues. https://github.com/aspnet/Entropy/blob/master/samples/Localization.StarterWeb/Views/Shared/_SelectLanguagePartial.cshtml

Body:
If you would like to use @Html.DropDownList,try to use below code:

@{
    var requestCulture = Context.Features.Get<IRequestCultureFeature>();
    var cultureItems = LocOptions.Value.SupportedUICultures
        .Select(c => new SelectListItem { Value = c.Name, Text = c.DisplayName ,Selected = (c.Name == requestCulture.RequestCulture.UICulture.Name )})
        .ToList();
    var returnUrl = string.IsNullOrEmpty(Context.Request.Path) ? "~/" : $"~{Context.Request.Path.Value}";
}
<div title="@Localizer["Request culture provider:"] @requestCulture?.Provider?.GetType().Name">
    <form id="selectLanguage" asp-controller="Home"
          asp-action="SetLanguage" asp-route-returnUrl="@returnUrl"
          method="post" class="form-horizontal" role="form">
        <label asp-for="@requestCulture.RequestCulture.UICulture.Name">@Localizer["Language:"]</label>

        @Html.DropDownList("culture", cultureItems, new
           {
               onchange = @"this.form.submit();"
           })

    </form>
</div>

Action:

[HttpPost]
public IActionResult SetLanguage(string culture, string returnUrl)


问:
Pysyft Federated learning, Error with Websockets
I am trying to run a federated learning from pysyft (https://github.com/OpenMined/PySyft/blob/dev/examples/tutorials/advanced/websockets-example-MNIST-parallel/Asynchronous-federated-learning-on-MNIST.ipynb) that creates remote workers and connect to them via websockets. however I am getting an error in folllowing evaluation step.

```<code>future: <Task finished coro=<WebsocketServerWorker._producer_handler() done, defined at C:\Users\Public\Anaconda\lib\site-packages\syft\workers\websocket_server.py:95> exception=AttributeError("'dict' object has no attribute 'owner'")>
Traceback (most recent call last):
  File "C:\Users\Public\Anaconda\lib\site-packages\syft\generic\frameworks\hook\hook_args.py", line 663, in register_response
    register_response_function = register_response_functions[attr_id]
KeyError: 'evaluate'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Public\Anaconda\lib\site-packages\syft\workers\websocket_server.py", line 113, in _producer_handler
    response = self._recv_msg(message)
  File "C:\Users\Public\Anaconda\lib\site-packages\syft\workers\websocket_server.py", line 124, in _recv_msg
    return self.recv_msg(message)
  File "C:\Users\Public\Anaconda\lib\site-packages\syft\workers\base.py", line 310, in recv_msg
    response = self._message_router[type(msg)](msg.contents)
  File "C:\Users\Public\Anaconda\lib\site-packages\syft\workers\base.py", line 457, in execute_command
    command_name, response, list(return_ids), self
  File "C:\Users\Public\Anaconda\lib\site-packages\syft\generic\frameworks\hook\hook_args.py", line 672, in register_response
    new_response = register_response_function(response, response_ids=response_ids, owner=owner)
  File "C:\Users\Public\Anaconda\lib\site-packages\syft\generic\frameworks\hook\hook_args.py", line 766, in <lambda>
    return lambda x, **kwargs: f(lambdas, x, **kwargs)
  File "C:\Users\Public\Anaconda\lib\site-packages\syft\generic\frameworks\hook\hook_args.py", line 522, in two_fold
    return lambdas[0](args[0], **kwargs), lambdas[1](args[1], **kwargs)
  File "C:\Users\Public\Anaconda\lib\site-packages\syft\generic\frameworks\hook\hook_args.py", line 744, in <lambda>
    else lambda i, **kwargs: register_tensor(i, **kwargs)
  File "C:\Users\Public\Anaconda\lib\site-packages\syft\generic\frameworks\hook\hook_args.py", line 712, in register_tensor
    tensor.owner = owner
AttributeError: 'dict' object has no attribute 'owner'
</code>```

There are no clear answer from their forum. does anyone have any clue as to what the issue is in this script.

My syft version:

```<code>syft : 0.2.3a1
syft-proto : 0.1.1a1.post12
torch : 1.4.0
</code>```

答: I came across this problem as well and pushed a fix in https://github.com/OpenMined/PySyft/pull/2948</p>

Body:
I am trying to run a federated learning from pysyft (https://github.com/OpenMined/PySyft/blob/dev/examples/tutorials/advanced/websockets-example-MNIST-parallel/Asynchronous-federated-learning-on-MNIST.ipynb) that creates remote workers and connect to them via websockets. however I am getting an error in folllowing evaluation step.

future: <Task finished coro=<WebsocketServerWorker._producer_handler() done, defined at C:\Users\Public\Anaconda\lib\site-packages\syft\workers\websocket_server.py:95> exception=AttributeError("'dict' object has no attribute 'owner'")>
Traceback (most recent call last):
  File "C:\Users\Public\Anaconda\lib\site-packages\syft\generic\frameworks\hook\hook_args.py", line 663, in register_response
    register_response_function = register_response_functions[attr_id]
KeyError: 'evaluate'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Public\Anaconda\lib\site-packages\syft\workers\websocket_server.py", line 113, in _producer_handler
    response = self._recv_msg(message)
  File "C:\Users\Public\Anaconda\lib\site-packages\syft\workers\websocket_server.py", line 124, in _recv_msg
    return self.recv_msg(message)
  File "C:\Users\Public\Anaconda\lib\site-packages\syft\workers\base.py", line 310, in recv_msg
    response = self._message_router[type(msg)](msg.contents)
  File "C:\Users\Public\Anaconda\lib\site-packages\syft\workers\base.py", line 457, in execute_command
    command_name, response, list(return_ids), self
  File "C:\Users\Public\Anaconda\lib\site-packages\syft\generic\frameworks\hook\hook_args.py", line 672, in register_response
    new_response = register_response_function(response, response_ids=response_ids, owner=owner)
  File "C:\Users\Public\Anaconda\lib\site-packages\syft\generic\frameworks\hook\hook_args.py", line 766, in <lambda>
    return lambda x, **kwargs: f(lambdas, x, **kwargs)
  File "C:\Users\Public\Anaconda\lib\site-packages\syft\generic\frameworks\hook\hook_args.py", line 522, in two_fold
    return lambdas[0](args[0], **kwargs), lambdas[1](args[1], **kwargs)
  File "C:\Users\Public\Anaconda\lib\site-packages\syft\generic\frameworks\hook\hook_args.py", line 744, in <lambda>
    else lambda i, **kwargs: register_tensor(i, **kwargs)
  File "C:\Users\Public\Anaconda\lib\site-packages\syft\generic\frameworks\hook\hook_args.py", line 712, in register_tensor
    tensor.owner = owner
AttributeError: 'dict' object has no attribute 'owner'

There are no clear answer from their forum. does anyone have any clue as to what the issue is in this script.

My syft version:

syft : 0.2.3a1
syft-proto : 0.1.1a1.post12
torch : 1.4.0


问:
Cannot get media stats VLC
I'm using VLC's libvlc-dev library to get a video's media stats but not able to get information regarding the same. I'm using Go's cgo functionality to interact with VLC (3.0.8) I get the following error -

```<code>fatal error: unexpected signal during runtime execution
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x7fa0e03ad78f]

runtime stack:
runtime.throw(0x4ece85, 0x2a)
    /usr/local/go/src/runtime/panic.go:774 +0x72
runtime.sigpanic()
    /usr/local/go/src/runtime/signal_unix.go:378 +0x47c

goroutine 1 [syscall]:
runtime.cgocall(0x4ab660, 0xc000047e40, 0x45eeac)
...
</code>```

My vlc.go file looks like this -

```<code>
//#cgo LDFLAGS: -lvlc
//#include <vlc/vlc.h>
//#include <stdlib.h>
import "C"
import (
    "errors"
    "fmt"
    "os"
    "unsafe"
)

type Vlc struct {
    instance *C.libvlc_instance_t
    player   *C.libvlc_media_player_t
    media    *C.libvlc_media_t
}

type libvlc_media_stats_t struct {
    iReadBytes    int
    fInputBitrate float64
    iDemuxReadBytes     int
    fDemuxBitrate       float64
    iDemuxCorrupted     int
    iDemuxDiscontinuity int
    iDecodedVideo int
    iDecodedAudio int
    iDisplayedPictures int
    iLostPictures      int
    iPlayedAbuffers int
    iLostAbuffers   int
}

...

// Media stats.
func (vlc *Vlc) Stats() error {
    // fmt.Println("vlc")
    var stats *C.libvlc_media_stats_t
    err := C.libvlc_media_get_stats(vlc.media, stats)
    fmt.Println(err)
    return vlc.getLastErr()
}
</code>```

How can i get the Media Stats ?

答:
go-vlc has similar code

```<code>func (this *Media) Stats() (*Stats, error) {
    if this.ptr == nil {
        return nil, os.EINVAL
    }

    var c C.libvlc_media_stats_t

    if C.libvlc_media_get_stats(this.ptr, &c) != 0 {
        return nil, checkError()
    }

    return &Stats{&c}, nil
}
</code>```

stats.go definition is here https://github.com/egelmex/go-vlc/blob/master/stats.go

Title: Cannot get media stats VLC

Body:
I'm using VLC's libvlc-dev library to get a video's media stats but not able to get information regarding the same. I'm using Go's cgo functionality to interact with VLC (3.0.8) I get the following error -

fatal error: unexpected signal during runtime execution
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x7fa0e03ad78f]

runtime stack:
runtime.throw(0x4ece85, 0x2a)
    /usr/local/go/src/runtime/panic.go:774 +0x72
runtime.sigpanic()
    /usr/local/go/src/runtime/signal_unix.go:378 +0x47c

goroutine 1 [syscall]:
runtime.cgocall(0x4ab660, 0xc000047e40, 0x45eeac)
...

My vlc.go file looks like this -

//#cgo LDFLAGS: -lvlc
//#include <vlc/vlc.h>
//#include <stdlib.h>
import "C"
import (
    "errors"
    "fmt"
    "os"
    "unsafe"
)

type Vlc struct {
    instance *C.libvlc_instance_t
    player   *C.libvlc_media_player_t
    media    *C.libvlc_media_t
}

type libvlc_media_stats_t struct {
    iReadBytes    int
    fInputBitrate float64
    iDemuxReadBytes     int
    fDemuxBitrate       float64
    iDemuxCorrupted     int
    iDemuxDiscontinuity int
    iDecodedVideo int
    iDecodedAudio int
    iDisplayedPictures int
    iLostPictures      int
    iPlayedAbuffers int
    iLostAbuffers   int
}

...

// Media stats.
func (vlc *Vlc) Stats() error {
    // fmt.Println("vlc")
    var stats *C.libvlc_media_stats_t
    err := C.libvlc_media_get_stats(vlc.media, stats)
    fmt.Println(err)
    return vlc.getLastErr()
}

How can i get the Media Stats ?

Body:
go-vlc has similar code

func (this *Media) Stats() (*Stats, error) {
    if this.ptr == nil {
        return nil, os.EINVAL
    }

    var c C.libvlc_media_stats_t

    if C.libvlc_media_get_stats(this.ptr, &c) != 0 {
        return nil, checkError()
    }

    return &Stats{&c}, nil
}

stats.go definition is here https://github.com/egelmex/go-vlc/blob/master/stats.go


问:
Databricks Pyspark + How to insert a dataframe schema as a column in a dataframe
I have a function which generates a dataframe:

```<code>def getdata():
    schema_1 = StructType([ StructField('path_name', StringType(), True),
                           StructField('age1', IntegerType(), True),
                           StructField('age2', IntegerType(), True),
                           StructField('age3', IntegerType(), True)])
    data = [('dbfs/123/sd.zip',1,2,3),('dbfs/123/ab.zip',5,6,7)]
    df = spark.createDataFrame(data,schema_1)
    return df
</code>```

I need to insert that dataframe schema into a column of another dataframe. The result should be something like:

```<code>root
 |-- filename: string (nullable = true)
 |-- parsed: struct (nullable = true)
 |    |-- path_name: string (nullable = true)
 |    |-- age1: integer (nullable = true)
 |    |-- age2: integer (nullable = true)
 |    |-- age3: integer (nullable = true)
</code>```

I was trying to do it by using a udf:

```<code>@udf(schema_1)
def my_udf(schema):
  data = getdata(schema)
  List_of_rows = data.collect()
  return List_of_rows
</code>```

And then inserting it into another dataframe I am creating. The whole code I am using is:

```<code>from pyspark.sql.types import *
from pyspark.sql.functions import col
import pandas as pd

schema_1 = StructType([ StructField('path_name', StringType(), True),
                           StructField('age1', IntegerType(), True),
                           StructField('age2', IntegerType(), True),
                           StructField('age3', IntegerType(), True)])

def getdata(schema_1):
    data = [('dbfs/123/sd.zip',1,2,3),('dbfs/123/ab.zip',5,6,7)]
    df = spark.createDataFrame(data,schema_1)
    return df

@udf(schema_1)
def my_udf(scheme):
  data = getdata(scheme)
  List_of_rows = data.collect()
  return List_of_rows

def somefunction(schema_1):
  pd_df = pd.DataFrame(['path'], columns = ["filename"])
  return (spark.createDataFrame(pd_df)
          .withColumn('parsed', my_udf(schema_1))
         )

df_2 = somefunction(schema_1)

display(df_2)
</code>```

However I am getting an error,

Error rendering output: Report error. PicklingError: Could not
  serialize object: Exception: It appears that you are attempting to
  reference SparkContext from a broadcast variable, action, or
  transformation. SparkContext can only be used on the driver, not in
  code that it run on workers. For more information, see SPARK-5063.

and I also think it is not the best approach. Any idea??

答:
Can you not just create a custom schema?  Google for that.  Also, see the sample code which will create a (forced) custom schema for you, even if the first rows (headers) are missing, or incorrect.</p>

```<code>from  pyspark.sql.functions import input_file_name
from pyspark.sql import SQLContext
from pyspark.sql.types import *
sqlContext = SQLContext(sc)

customSchema = StructType([ \
StructField("asset_id", StringType(), True), \
StructField("price_date", StringType(), True), \

etc., etc., etc.,

StructField("close_price", StringType(), True), \
StructField("filename", StringType(), True)])
</code>```

Body:
I have a function which generates a dataframe:

def getdata():
    schema_1 = StructType([ StructField('path_name', StringType(), True),
                           StructField('age1', IntegerType(), True),
                           StructField('age2', IntegerType(), True),
                           StructField('age3', IntegerType(), True)])
    data = [('dbfs/123/sd.zip',1,2,3),('dbfs/123/ab.zip',5,6,7)]
    df = spark.createDataFrame(data,schema_1)
    return df

I need to insert that dataframe schema into a column of another dataframe. The result should be something like:

root
 |-- filename: string (nullable = true)
 |-- parsed: struct (nullable = true)
 |    |-- path_name: string (nullable = true)
 |    |-- age1: integer (nullable = true)
 |    |-- age2: integer (nullable = true)
 |    |-- age3: integer (nullable = true)

I was trying to do it by using a udf:

@udf(schema_1)
def my_udf(schema):
  data = getdata(schema)
  List_of_rows = data.collect()
  return List_of_rows

And then inserting it into another dataframe I am creating. The whole code I am using is:

from pyspark.sql.types import *
from pyspark.sql.functions import col
import pandas as pd

schema_1 = StructType([ StructField('path_name', StringType(), True),
                           StructField('age1', IntegerType(), True),
                           StructField('age2', IntegerType(), True),
                           StructField('age3', IntegerType(), True)])

def getdata(schema_1):
    data = [('dbfs/123/sd.zip',1,2,3),('dbfs/123/ab.zip',5,6,7)]
    df = spark.createDataFrame(data,schema_1)
    return df

@udf(schema_1)
def my_udf(scheme):
  data = getdata(scheme)
  List_of_rows = data.collect()
  return List_of_rows

def somefunction(schema_1):
  pd_df = pd.DataFrame(['path'], columns = ["filename"])
  return (spark.createDataFrame(pd_df)
          .withColumn('parsed', my_udf(schema_1))
         )

df_2 = somefunction(schema_1)

display(df_2)

However I am getting an error,

Error rendering output: Report error. PicklingError: Could not
  serialize object: Exception: It appears that you are attempting to
  reference SparkContext from a broadcast variable, action, or
  transformation. SparkContext can only be used on the driver, not in
  code that it run on workers. For more information, see SPARK-5063.

and I also think it is not the best approach. Any idea??

Body:
Can you not just create a custom schema?  Google for that.  Also, see the sample code which will create a (forced) custom schema for you, even if the first rows (headers) are missing, or incorrect.

from  pyspark.sql.functions import input_file_name
from pyspark.sql import SQLContext
from pyspark.sql.types import *
sqlContext = SQLContext(sc)

customSchema = StructType([ \
StructField("asset_id", StringType(), True), \
StructField("price_date", StringType(), True), \

etc., etc., etc.,

StructField("close_price", StringType(), True), \
StructField("filename", StringType(), True)])


问:
Azure DevOps: Field with default value containing another fields value
I am trying to find a way in Azure DevOps of displaying a field on a User Story layout that is made up of a URL plus the value of another field on the same story.

We have an external support ticket system where all of our support calls are logged. When the story (or even Defect) is created, we have a field where a support reference is entered.

I want another field that combines a URL and the support reference so it creates a link to the support ticket.

Is this do-able?

Thanks,

Craig

答:
This is achievable. You can Add a custom field to a work item type for an inherited process.

1,First you need to create a inherited process.

Go to Organization settings, From the Process page under Boards, open the … context menu of the process you'll use to create an inherited process, and then choose Create inherited process. Choose the same system process—Agile, Basic, Scrum, or CMMI—that was used to create the project that you want to customize.

2, Add a custom field to an existing work item type for the inherited process.

From the Process page of the selected inherited process, choose the work item type(User Story) you want to add the custom field to.

Select the work item type and click new field or ... to add a field under a group.

For example i add a new field Support Url under group Planning(click Options to define a default value for this field).

3, Apply the customized process to your project.

Click team projects of the process shown as below screenshot.

Open the … context menu for the process and choose the Change team projects… option.

Then you will have the custom field with default value for the work item type in your project.

For detailed steps please check Microsoft Document there.

Update:
  Field value made up of a static part, plus another field

There is no direct way or any tool i can find to achieve this. However there is a complicate workaround to achieve this.

You can try creating a service server to to combine the field values and update the workitem field with workitem update rest api, and add a service hook to this service server.

You can refer to the service hook sever provided by Microsoft. Check reate a pull request status server with Node.js


问:
Persistent Storage Option for Standalone One-Off Angular Application
EDIT: How is this off-topic? I'm asking for a specific answer to a specific scenario.

I would like to mess around with some Angular (5+) applications. These are never going to leave my PC or go online or anything of the sort. The data maintained in the application should not be considered volatile and should persist as reliably as if the were being stored in a run-of-the-mill database (SQL or what-have-you). That is to say, I do not want to be tied to web browser storage (cookies, local/session storage, and so on).

Ideally, I would like to omit any sort of server-side portion of an Angular app. Just run ng serve and go, without worrying about any kind of server-side aspect to maintain/run/etc. Typescript is all I need.

So this boils down to my question: What are my Angular service classes going to talk to? I don't foresee the data being anything more complex than some POCOs which may have some relational mappings to one-another. Is there an Angular-only solution for storing these kinds of data? I would love to have my services interact with this persistent data store directly, as opposed to having to call some API. I'm open to just about any solution that could get the job done.

答:
Interesting dilemma...

My first thought is to set up a traditional database that can run locally using a container system, such as Docker.  You can set up a NodeJS app--or similar--to expose rest services to your data.

If that is too much overhead, I'd look into local file storage.  Since you want to be browser independent, you are a bit limited by browser APIs.  You should be able to load local files using Angular's HTTP Service and get requests.  Saving the files might be more complex, to do that without user interaction you'll probably want to use something like Electron or Cordova to deploy a desktop app.


问:
Can't access blob storage locally in chatbot emulator
I used to be able to access blob storage during local testing, but I've started getting a [onTurnError]: StorageError: Forbidden message when teseting locally via emulator. I do have issues connecting to some resources from my local (notably CosmosDB), but I do believe this was working before. I have HTTPS_PROXY value set in my local .env which works for everything except Cosmos (LUIS, QnA Maker, Azure Table Storage, etc are working). So I have two questions:

Has something changed and/or is it possible at all to hit Blob storage via local emulator testing through a proxy.

If not, is it possible to code the bot so that it will use memory storage from my local and blob storage from Azure?

For what it's worth, here is the code I use to set up the state storage but I think the issue is accessing the services through the proxy, not defining these storage objects.

// Memory storage - for development only
const memoryStorage = new MemoryStorage();
const conversationState = new ConversationState(memoryStorage);
const userState = new UserState(memoryStorage);

// Blob storage - for production
/*const blobStorage = new BlobStorage({
    containerName: 'bot-storage',
    storageAccountOrConnectionString: process.env.blobStorageServiceName,
    storageAccessKey: process.env.blobStorageAccessKey
});
const conversationState = new ConversationState(blobStorage);
const userState = new UserState(blobStorage);*/
```

答:
I came up with a workaround to this, but I feel like it's not a great approach. Would like to see other solutions to the issue.

My workaround was to look at my BOTNAME environmental variable, and if it matches what I have set in my local .env file, use MemoryStorage. Otherwise use BlobStorage. It is functioning, but I hate to have extra code only for the sake of local testing.

Here is the code in my index.js file:

if (process.env.BOTNAME == 'OEM_CSC_Support_Bot_Local') {
    // Memory storage - for development only
    console.log(`Using MemoryStorage for state storage`);
    const memoryStorage = new MemoryStorage();
    var conversationState = new ConversationState(memoryStorage);
    var userState = new UserState(memoryStorage);
} else {
    // Blob storage - for production
    console.log(`Using BlobStorage for state storage`);
    const blobStorage = new BlobStorage({
        containerName: 'bot-storage',
        storageAccountOrConnectionString: process.env.blobStorageServiceName,
        storageAccessKey: process.env.blobStorageAccessKey
    });
    var conversationState = new ConversationState(blobStorage);
    var userState = new UserState(blobStorage);
}
```


问:
Getting error in uploading image with AFNetworking in Objective-C
Getting error in uploading image with AFNetworking in Objective-C:

Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad gateway (502)

答:
Take a look at https://en.wikipedia.org/wiki/List_of_HTTP_status_codes

A 502 error indicates a problem with the server you're trying to upload to, and in particular, with one of its upstream servers. If you're not getting this error on other clients trying to upload to the same URL (eg, a web browser), in my experience the culprit is the gateway server and upstream server not dealing with headers the same way, but your mileage may vary.


问:
django model serialization
I want to serialize models so:

```<code>class Schedule(models.Model):
   Title = models.CharField(max_length=512)

class ScheduleEvent1(models.Model):
   ScheduleContent = models.ForeignKey(Schedule)
   Description = models.TextField()

class ScheduleEvent2(models.Model):
   ScheduleContent = models.ForeignKey(Schedule)
   AnotherField = models.TextField()
   ShortDescription = models.TextField()
</code>```

make smth like serializers.serialize('json', Schedule.objects.all())

The result likes

```<code>[
{
    "pk": 2,
    "model": "Schedule",
    "fields": {
        "Title": "Some Title",
        "ScheduleEvent1": [
            {
                "pk": 19,
                "model": "ScheduleEvent1",
                "fields": {
                    "Description": "Some Descr",
                }
            },
            {
                "pk": 20,
                "model": "ScheduleEvent1",
                "fields": {
                    "Description": "Some Descr2222",
                }
            }
        ],
        "ScheduleEvent2": [
            {
                "pk": 15,
                "model": "ScheduleEvent2",
                "fields": {
                    "AnotherField": "Some text",
                    "ShortDescription" : "Some text ...",
                }
            }
        ]
    }
}
</code>```

]

In general. I have entity tree. And I need serialize this tree from root.

tnx for help.

答:
http://docs.djangoproject.com/en/1.3/topics/serialization/#dependencies-during-serialization

Covers the essence of this.  The resulting file is not precisely what's required, since the objects won't be nested.  However it is what Django produces by default and it's what Django will use for deserialization.

If you use Piston you can easily define a Handler which will produce nested JSON, but it won't be precisely as shown because Piston's JSON emitter isn't precisely in the Django format.

Getting to precisely what's hoped-for leads to extending the Django serializer to produce a nested JSON object as the natural key. See http://code.djangoproject.com/browser/django/trunk/django/core/serializers/python.py.  Lines 47-59

Or, you can define a natural_key method which uses Django's 'django.core.serializers.json .Serializer` to emit the JSON serialization of the object instead of some other natural key value.  This is a little weird semantically, but it may do what you want.

Title: django model serialization

Body:
I want to serialize models so:

class Schedule(models.Model):
   Title = models.CharField(max_length=512)

class ScheduleEvent1(models.Model):
   ScheduleContent = models.ForeignKey(Schedule)
   Description = models.TextField()

class ScheduleEvent2(models.Model):
   ScheduleContent = models.ForeignKey(Schedule)
   AnotherField = models.TextField()
   ShortDescription = models.TextField()

make smth like serializers.serialize('json', Schedule.objects.all())

The result likes

[
{
    "pk": 2,
    "model": "Schedule",
    "fields": {
        "Title": "Some Title",
        "ScheduleEvent1": [
            {
                "pk": 19,
                "model": "ScheduleEvent1",
                "fields": {
                    "Description": "Some Descr",
                }
            },
            {
                "pk": 20,
                "model": "ScheduleEvent1",
                "fields": {
                    "Description": "Some Descr2222",
                }
            }
        ],
        "ScheduleEvent2": [
            {
                "pk": 15,
                "model": "ScheduleEvent2",
                "fields": {
                    "AnotherField": "Some text",
                    "ShortDescription" : "Some text ...",
                }
            }
        ]
    }
}

]

In general. I have entity tree. And I need serialize this tree from root.

tnx for help.


问:
In ggplot, using a numeric variable like a factor to create multiple plots, but using the numeric values to control spacing
If I make a data frame like this:

```<code>d1 <- data.frame(class=rep(c("A", "B", "C"), each=100),
                value=c(rnorm(100,0,1), rnorm(100,1,1), rnorm(100,2,1)))
</code>```

I can easily make a violin plot with a separate violin for each class:

```<code>ggplot(d1, aes(x=class, y=value)) + geom_violin()
</code>```

But if I make a data frame and plot like this, with numeric values:

```<code>d2 <- data.frame(timepoint=rep(c(1, 2, 3.5), each=100),
                 value=c(rnorm(100,0,1), rnorm(100,1,1), rnorm(100,2,1)))
ggplot(d2, aes(x=timepoint, y=value)) + geom_violin()
</code>```

I just get a single violin plot like so:

I could do factor(timepoint):

```<code>ggplot(d2, aes(x=factor(timepoint), y=value)) + geom_violin()
</code>```

but then I get a plot with equal spacing. What I want is a plot where the third violin is farther to the right, since it is at time=3.5. That is, where the spacing reflects the actual values of timepoint.

I realize this isn't specific to violin plots, it could be a boxplot or any other kind of plot. Is there a way to do what I want?

答:
As requested,

add group=timepoint to your set of aesthetics, like:

ggplot(d2, aes(x=timepoint, y=value, group=timepoint)) + geom_violin()

Body:
If I make a data frame like this:

d1 <- data.frame(class=rep(c("A", "B", "C"), each=100),
                value=c(rnorm(100,0,1), rnorm(100,1,1), rnorm(100,2,1)))

I can easily make a violin plot with a separate violin for each class:

ggplot(d1, aes(x=class, y=value)) + geom_violin()

But if I make a data frame and plot like this, with numeric values:

d2 <- data.frame(timepoint=rep(c(1, 2, 3.5), each=100),
                 value=c(rnorm(100,0,1), rnorm(100,1,1), rnorm(100,2,1)))
ggplot(d2, aes(x=timepoint, y=value)) + geom_violin()

I just get a single violin plot like so:

I could do factor(timepoint):

ggplot(d2, aes(x=factor(timepoint), y=value)) + geom_violin()

but then I get a plot with equal spacing. What I want is a plot where the third violin is farther to the right, since it is at time=3.5. That is, where the spacing reflects the actual values of timepoint.

I realize this isn't specific to violin plots, it could be a boxplot or any other kind of plot. Is there a way to do what I want?


问:
Running the Following Playbook syntax appears to be correct but getting following ERROR!- 'blockinfile' is not a valid attribute for a Play
Running the Following Playbook syntax appears to be correct but getting following ERROR!-

```<code>ERROR! 'blockinfile' is not a valid attribute for a Play

The error appears to have been in '/root/playbook1.yml': line 2, column 3, but may be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

---
- name: insertupdate
  ^ here
</code>```

My Playbook file code is:

```<code>---
- name: insertupdate
  blockinfile:
    dest: /etc/network/interfaces
    block: |
      iface eth2 inet static
          address 192.168.0.1
          netmask 255.255.255.0
</code>```

By the way I am using Ansible Version 2.x

答:
```<code>-bash-4.2$ cat getUri.yml
---
- name: test playbook
  hosts: localhost
  tasks:
  - name: Check that you can connect (GET) to a page and it returns a status 200
    uri:
      url: http://www.example.com
</code>```

The error is due to hosts and tasks missing in your playbook

Body:
Running the Following Playbook syntax appears to be correct but getting following ERROR!-

ERROR! 'blockinfile' is not a valid attribute for a Play

The error appears to have been in '/root/playbook1.yml': line 2, column 3, but may be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

---
- name: insertupdate
  ^ here

My Playbook file code is:

---
- name: insertupdate
  blockinfile:
    dest: /etc/network/interfaces
    block: |
      iface eth2 inet static
          address 192.168.0.1
          netmask 255.255.255.0

By the way I am using Ansible Version 2.x

Body:
-bash-4.2$ cat getUri.yml
---
- name: test playbook
  hosts: localhost
  tasks:
  - name: Check that you can connect (GET) to a page and it returns a status 200
    uri:
      url: http://www.example.com

The error is due to hosts and tasks missing in your playbook