问:
OData Expand Child of Child Missing Key Exception
AspNetCore 3.1 web app with OData version 7.5.2 installed.

I am getting an exception when I do a child of child expand:

i.e.

https://localhost:5001/clinic?$expand=clients($expand=allergies)

If I just expand on clients with clinic - it works fine

https://localhost:5001/clinic?$expand=clients

or clients expanded with allergies - it works.

https://localhost:5001/client?$expand=allergies

The second level of expansion is throwing the error.

I have entities defined:

```<code>public class Clinic
{
    public Guid Id { get; set; }
    public string Name { get; set; }

    public string Street { get; set; }
    public string Unit { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zipcode { get; set; }
    public string PhoneNumber { get; set; }
    public string PhoneDescription { get; set; }
    public DateTime Created { get; set; }
    public DateTime Modified { get; set; }
    public Guid ApplicationId { get; set; }

    public virtual Application Application { get; set; }
    public virtual IEnumerable<Client> Clients { get; set; }
}

public class Client
{
    public Guid Id { get; set; }
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string Street { get; set; }
    public string Unit { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zipcode { get; set; }
    public DateTime DateOfBirth { get; set; }
    public ClientStatus Status { get; set; }
    public DateTime Created { get; set; }
    public DateTime Modified { get; set; }
    public Guid ClinicId { get; set; }

    public virtual Clinic Clinic { get; set; }
    public virtual ICollection<ClientAllergy> Allergies { get; set; }
}

public class ClientAllergy
{
    public Guid Id { get; set; }
    public string Allergy { get; set; }
    public string AdditionalInformation { get; set; }
    public string Reaction { get; set; }
    public DateTime Created { get; set; }
    public DateTime Modified { get; set; }
    public Guid ClientId { get; set; }

    public virtual Client Client { get; set; }
}
</code>```

The EdmModel is built:

```<code>        app.UseEndpoints(endpoints =>
        {
            endpoints.Expand().Select().OrderBy().Filter();
            endpoints.EnableDependencyInjection();
            endpoints.MapODataRoute("odata", "", GetEdmModel());
        });
    }

    private static IEdmModel GetEdmModel()
    {
        var builder = new ODataConventionModelBuilder();
        builder.EntitySet<Clinic>("Clinics");
        builder.EntitySet<Client>("Clients");
        builder.EntitySet<Application>("Applications");
        builder.EntitySet<ClientAllergy>("ClientAllergies");
        return builder.GetEdmModel();
    }
</code>```

And the $metadata page looks to be correctly registering the entities.

```<code><?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="4.0" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx">
<edmx:DataServices>
    <Schema Namespace="ABC.Data.Entity" xmlns="http://docs.oasis-open.org/odata/ns/edm">
        <EntityType Name="Clinic">
            <Key>
                <PropertyRef Name="Id" />
            </Key>
            <Property Name="Id" Type="Edm.Guid" Nullable="false" />
            <Property Name="Name" Type="Edm.String" />
            <Property Name="Street" Type="Edm.String" />
            <Property Name="Unit" Type="Edm.String" />
            <Property Name="City" Type="Edm.String" />
            <Property Name="State" Type="Edm.String" />
            <Property Name="Zipcode" Type="Edm.String" />
            <Property Name="PhoneNumber" Type="Edm.String" />
            <Property Name="PhoneDescription" Type="Edm.String" />
            <Property Name="Created" Type="Edm.DateTimeOffset" Nullable="false" />
            <Property Name="Modified" Type="Edm.DateTimeOffset" Nullable="false" />
            <Property Name="ApplicationId" Type="Edm.Guid" />
            <NavigationProperty Name="Application" Type="ABC.Data.Entity.Application">
                <ReferentialConstraint Property="ApplicationId" ReferencedProperty="Id" />
            </NavigationProperty>
            <NavigationProperty Name="Clients" Type="Collection(ABC.Data.Entity.Client)" />
        </EntityType>
        <EntityType Name="Client">
            <Key>
                <PropertyRef Name="Id" />
            </Key>
            <Property Name="Id" Type="Edm.Guid" Nullable="false" />
            <Property Name="LastName" Type="Edm.String" />
            <Property Name="FirstName" Type="Edm.String" />
            <Property Name="MiddleName" Type="Edm.String" />
            <Property Name="Street" Type="Edm.String" />
            <Property Name="Unit" Type="Edm.String" />
            <Property Name="City" Type="Edm.String" />
            <Property Name="State" Type="Edm.String" />
            <Property Name="Zipcode" Type="Edm.String" />
            <Property Name="DateOfBirth" Type="Edm.DateTimeOffset" Nullable="false" />
            <Property Name="Status" Type="ABC.Data.Entity.ClientStatus" Nullable="false" />
            <Property Name="Created" Type="Edm.DateTimeOffset" Nullable="false" />
            <Property Name="Modified" Type="Edm.DateTimeOffset" Nullable="false" />
            <Property Name="ClinicId" Type="Edm.Guid" />
            <NavigationProperty Name="Clinic" Type="ABC.Data.Entity.Clinic">
                <ReferentialConstraint Property="ClinicId" ReferencedProperty="Id" />
            </NavigationProperty>
            <NavigationProperty Name="Allergies" Type="Collection(ABC.Data.Entity.ClientAllergy)" />
        </EntityType>
        <EntityType Name="Application">
            <Key>
                <PropertyRef Name="Id" />
            </Key>
            <Property Name="Id" Type="Edm.Guid" Nullable="false" />
            <Property Name="Name" Type="Edm.String" />
            <Property Name="Created" Type="Edm.DateTimeOffset" Nullable="false" />
            <Property Name="Modified" Type="Edm.DateTimeOffset" Nullable="false" />
            <NavigationProperty Name="Partners" Type="Collection(ABC.Data.Entity.Clinic)" />
        </EntityType>
        <EntityType Name="ClientAllergy">
            <Key>
                <PropertyRef Name="Id" />
            </Key>
            <Property Name="Id" Type="Edm.Guid" Nullable="false" />
            <Property Name="Allergy" Type="Edm.String" />
            <Property Name="AdditionalInformation" Type="Edm.String" />
            <Property Name="Reaction" Type="Edm.String" />
            <Property Name="Created" Type="Edm.DateTimeOffset" Nullable="false" />
            <Property Name="Modified" Type="Edm.DateTimeOffset" Nullable="false" />
            <Property Name="ClientId" Type="Edm.Guid" />
            <NavigationProperty Name="Client" Type="ABC.Data.Entity.Client">
                <ReferentialConstraint Property="ClientId" ReferencedProperty="Id" />
            </NavigationProperty>
        </EntityType>
        <EnumType Name="ClientStatus">
            <Member Name="Active" Value="0" />
            <Member Name="Deceased" Value="1" />
            <Member Name="DMC" Value="2" />
            <Member Name="NotAssess" Value="3" />
            <Member Name="OnHold" Value="4" />
        </EnumType>
    </Schema>
    <Schema Namespace="Default" xmlns="http://docs.oasis-open.org/odata/ns/edm">
        <EntityContainer Name="Container">
            <EntitySet Name="Clinics" EntityType="ABC.Data.Entity.Clinic">
                <NavigationPropertyBinding Path="Application" Target="Applications" />
                <NavigationPropertyBinding Path="Clients" Target="Clients" />
            </EntitySet>
            <EntitySet Name="Clients" EntityType="ABC.Data.Entity.Client">
                <NavigationPropertyBinding Path="Allergies" Target="ClientAllergies" />
                <NavigationPropertyBinding Path="Clinic" Target="Clinics" />
            </EntitySet>
            <EntitySet Name="Applications" EntityType="ABC.Data.Entity.Application">
                <NavigationPropertyBinding Path="Partners" Target="Clinics" />
            </EntitySet>
            <EntitySet Name="ClientAllergies" EntityType="ABC.Data.Entity.ClientAllergy">
                <NavigationPropertyBinding Path="Client" Target="Clients" />
            </EntitySet>
        </EntityContainer>
    </Schema>
</edmx:DataServices>
</edmx:Edmx>
</code>```

答:
This is a Data related issue, your schema looks correct.
The giveaway here is that the error is raised when the objects are being materialized from the data provider into the OData Model, there are many more steps in the stack trace that would be expected if this occurred later in serialization.

Query the same allergies from the database using EF in a unit test or directly in your data store (assuming SQL). If you see any with a NULL value for the Id, then you should fix or remove those records.

A further proof is to try this query:

```<code>https://localhost:5001/clinic?$expand=clients($expand=allergies;$filter=Id ne null)
</code>```

Body:
AspNetCore 3.1 web app with OData version 7.5.2 installed.

I am getting an exception when I do a child of child expand:

i.e.

https://localhost:5001/clinic?$expand=clients($expand=allergies)

If I just expand on clients with clinic - it works fine

https://localhost:5001/clinic?$expand=clients

or clients expanded with allergies - it works.

https://localhost:5001/client?$expand=allergies

The second level of expansion is throwing the error.

I have entities defined:

public class Clinic
{
    public Guid Id { get; set; }
    public string Name { get; set; }

    public string Street { get; set; }
    public string Unit { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zipcode { get; set; }
    public string PhoneNumber { get; set; }
    public string PhoneDescription { get; set; }
    public DateTime Created { get; set; }
    public DateTime Modified { get; set; }
    public Guid ApplicationId { get; set; }

    public virtual Application Application { get; set; }
    public virtual IEnumerable<Client> Clients { get; set; }
}

public class Client
{
    public Guid Id { get; set; }
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string Street { get; set; }
    public string Unit { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zipcode { get; set; }
    public DateTime DateOfBirth { get; set; }
    public ClientStatus Status { get; set; }
    public DateTime Created { get; set; }
    public DateTime Modified { get; set; }
    public Guid ClinicId { get; set; }

    public virtual Clinic Clinic { get; set; }
    public virtual ICollection<ClientAllergy> Allergies { get; set; }
}

public class ClientAllergy
{
    public Guid Id { get; set; }
    public string Allergy { get; set; }
    public string AdditionalInformation { get; set; }
    public string Reaction { get; set; }
    public DateTime Created { get; set; }
    public DateTime Modified { get; set; }
    public Guid ClientId { get; set; }

    public virtual Client Client { get; set; }
}

The EdmModel is built:

        app.UseEndpoints(endpoints =>
        {
            endpoints.Expand().Select().OrderBy().Filter();
            endpoints.EnableDependencyInjection();
            endpoints.MapODataRoute("odata", "", GetEdmModel());
        });
    }

    private static IEdmModel GetEdmModel()
    {
        var builder = new ODataConventionModelBuilder();
        builder.EntitySet<Clinic>("Clinics");
        builder.EntitySet<Client>("Clients");
        builder.EntitySet<Application>("Applications");
        builder.EntitySet<ClientAllergy>("ClientAllergies");
        return builder.GetEdmModel();
    }

And the $metadata page looks to be correctly registering the entities.

<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="4.0" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx">
<edmx:DataServices>
    <Schema Namespace="ABC.Data.Entity" xmlns="http://docs.oasis-open.org/odata/ns/edm">
        <EntityType Name="Clinic">
            <Key>
                <PropertyRef Name="Id" />
            </Key>
            <Property Name="Id" Type="Edm.Guid" Nullable="false" />
            <Property Name="Name" Type="Edm.String" />
            <Property Name="Street" Type="Edm.String" />
            <Property Name="Unit" Type="Edm.String" />
            <Property Name="City" Type="Edm.String" />
            <Property Name="State" Type="Edm.String" />
            <Property Name="Zipcode" Type="Edm.String" />
            <Property Name="PhoneNumber" Type="Edm.String" />
            <Property Name="PhoneDescription" Type="Edm.String" />
            <Property Name="Created" Type="Edm.DateTimeOffset" Nullable="false" />
            <Property Name="Modified" Type="Edm.DateTimeOffset" Nullable="false" />
            <Property Name="ApplicationId" Type="Edm.Guid" />
            <NavigationProperty Name="Application" Type="ABC.Data.Entity.Application">
                <ReferentialConstraint Property="ApplicationId" ReferencedProperty="Id" />
            </NavigationProperty>
            <NavigationProperty Name="Clients" Type="Collection(ABC.Data.Entity.Client)" />
        </EntityType>
        <EntityType Name="Client">
            <Key>
                <PropertyRef Name="Id" />
            </Key>
            <Property Name="Id" Type="Edm.Guid" Nullable="false" />
            <Property Name="LastName" Type="Edm.String" />
            <Property Name="FirstName" Type="Edm.String" />
            <Property Name="MiddleName" Type="Edm.String" />
            <Property Name="Street" Type="Edm.String" />
            <Property Name="Unit" Type="Edm.String" />
            <Property Name="City" Type="Edm.String" />
            <Property Name="State" Type="Edm.String" />
            <Property Name="Zipcode" Type="Edm.String" />
            <Property Name="DateOfBirth" Type="Edm.DateTimeOffset" Nullable="false" />
            <Property Name="Status" Type="ABC.Data.Entity.ClientStatus" Nullable="false" />
            <Property Name="Created" Type="Edm.DateTimeOffset" Nullable="false" />
            <Property Name="Modified" Type="Edm.DateTimeOffset" Nullable="false" />
            <Property Name="ClinicId" Type="Edm.Guid" />
            <NavigationProperty Name="Clinic" Type="ABC.Data.Entity.Clinic">
                <ReferentialConstraint Property="ClinicId" ReferencedProperty="Id" />
            </NavigationProperty>
            <NavigationProperty Name="Allergies" Type="Collection(ABC.Data.Entity.ClientAllergy)" />
        </EntityType>
        <EntityType Name="Application">
            <Key>
                <PropertyRef Name="Id" />
            </Key>
            <Property Name="Id" Type="Edm.Guid" Nullable="false" />
            <Property Name="Name" Type="Edm.String" />
            <Property Name="Created" Type="Edm.DateTimeOffset" Nullable="false" />
            <Property Name="Modified" Type="Edm.DateTimeOffset" Nullable="false" />
            <NavigationProperty Name="Partners" Type="Collection(ABC.Data.Entity.Clinic)" />
        </EntityType>
        <EntityType Name="ClientAllergy">
            <Key>
                <PropertyRef Name="Id" />
            </Key>
            <Property Name="Id" Type="Edm.Guid" Nullable="false" />
            <Property Name="Allergy" Type="Edm.String" />
            <Property Name="AdditionalInformation" Type="Edm.String" />
            <Property Name="Reaction" Type="Edm.String" />
            <Property Name="Created" Type="Edm.DateTimeOffset" Nullable="false" />
            <Property Name="Modified" Type="Edm.DateTimeOffset" Nullable="false" />
            <Property Name="ClientId" Type="Edm.Guid" />
            <NavigationProperty Name="Client" Type="ABC.Data.Entity.Client">
                <ReferentialConstraint Property="ClientId" ReferencedProperty="Id" />
            </NavigationProperty>
        </EntityType>
        <EnumType Name="ClientStatus">
            <Member Name="Active" Value="0" />
            <Member Name="Deceased" Value="1" />
            <Member Name="DMC" Value="2" />
            <Member Name="NotAssess" Value="3" />
            <Member Name="OnHold" Value="4" />
        </EnumType>
    </Schema>
    <Schema Namespace="Default" xmlns="http://docs.oasis-open.org/odata/ns/edm">
        <EntityContainer Name="Container">
            <EntitySet Name="Clinics" EntityType="ABC.Data.Entity.Clinic">
                <NavigationPropertyBinding Path="Application" Target="Applications" />
                <NavigationPropertyBinding Path="Clients" Target="Clients" />
            </EntitySet>
            <EntitySet Name="Clients" EntityType="ABC.Data.Entity.Client">
                <NavigationPropertyBinding Path="Allergies" Target="ClientAllergies" />
                <NavigationPropertyBinding Path="Clinic" Target="Clinics" />
            </EntitySet>
            <EntitySet Name="Applications" EntityType="ABC.Data.Entity.Application">
                <NavigationPropertyBinding Path="Partners" Target="Clinics" />
            </EntitySet>
            <EntitySet Name="ClientAllergies" EntityType="ABC.Data.Entity.ClientAllergy">
                <NavigationPropertyBinding Path="Client" Target="Clients" />
            </EntitySet>
        </EntityContainer>
    </Schema>
</edmx:DataServices>
</edmx:Edmx>

Body:
This is a Data related issue, your schema looks correct.
The giveaway here is that the error is raised when the objects are being materialized from the data provider into the OData Model, there are many more steps in the stack trace that would be expected if this occurred later in serialization.

Query the same allergies from the database using EF in a unit test or directly in your data store (assuming SQL). If you see any with a NULL value for the Id, then you should fix or remove those records.

A further proof is to try this query:

https://localhost:5001/clinic?$expand=clients($expand=allergies;$filter=Id ne null)


来源: stackexchange

问:
Find subset with elements that are furthest apart from eachother
I have an interview question that I can't seem to figure out. Given an array of size N, find the subset of size k such that the elements in the subset are the furthest apart from each other. In other words, maximize the minimum pairwise distance between the elements.

```<code>Example:

Array = [1,2,6,10]
k = 3

answer = [1,6,10]
</code>```

The bruteforce way requires finding all subsets of size k which is exponential in runtime.

One idea I had was to take values evenly spaced from the array. What I mean by this is

Take the 1st and last element

find the difference between them (in this case 10-1) and divide that by k ((10-1)/3=3)

move 2 pointers inward from both ends, picking out elements that are +/- 3 from your previous pick. So in this case, you start from 1 and 10 and find the closest elements to 4 and 7. That would be 6.

This is based on the intuition that the elements should be as evenly spread as possible. I have no idea how to prove it works/doesn't work.  If anyone knows how or has a better algorithm please do share. Thanks!

答:
You can do this in O(n*(log n) + n*log(M)), where M is max(A) - min(A).

The idea is to use binary search to find the maximum separation possible.

First, sort the array. Then, we just need a helper function that takes in a distance d, and greedily builds the longest subarray possible with consecutive elements separated by at least d. We can do this in O(n) time.

If the generated array has length at least k, then the maximum separation possible is >=d. Otherwise, it's strictly less than d. This means we can use binary search to find the maximum value. With some cleverness, you can shrink the 'low' and 'high' bounds of the binary search, but it's already so fast that sorting would become the bottleneck.

Python code:

def maximize_distance(nums: List[int], k: int) -> List[int]:
    """Given an array of numbers and size k, uses binary search
    to find a subset of size k with maximum min-pairwise-distance"""
    assert len(nums) >= k

    if k == 1:
        return [nums[0]]

    nums.sort()

    def longest_separated_array(desired_distance: int) -> List[int]:
        """Given a distance, returns a subarray of nums
        of length k with pairwise differences at least that distance (if
        one exists)."""

        answer = [nums[0]]

        for x in nums[1:]:

            if x - answer[-1] >= desired_distance:
                answer.append(x)

                if len(answer) == k:
                    break

        return answer

    low, high = 0, (nums[-1] - nums[0])

    while low < high:
        mid = (low + high + 1) // 2

        if len(longest_separated_array(mid)) == k:
            low = mid
        else:
            high = mid - 1

    return longest_separated_array(low)
```

ContentLicense: CC BY-SA 3.0

Body:
I have an interview question that I can't seem to figure out. Given an array of size N, find the subset of size k such that the elements in the subset are the furthest apart from each other. In other words, maximize the minimum pairwise distance between the elements.

Example:

Array = [1,2,6,10]
k = 3

answer = [1,6,10]

The bruteforce way requires finding all subsets of size k which is exponential in runtime.

One idea I had was to take values evenly spaced from the array. What I mean by this is

Take the 1st and last element

find the difference between them (in this case 10-1) and divide that by k ((10-1)/3=3)

move 2 pointers inward from both ends, picking out elements that are +/- 3 from your previous pick. So in this case, you start from 1 and 10 and find the closest elements to 4 and 7. That would be 6.

This is based on the intuition that the elements should be as evenly spread as possible. I have no idea how to prove it works/doesn't work.  If anyone knows how or has a better algorithm please do share. Thanks!

ContentLicense: CC BY-SA 4.0


问:
How do I Add a new row to a datagridview without using a dataset or datatable?
I'm using a List to bind to a dataGridView.
I'd like the user to be able to add a new row.

I've tried using a BindingList and a BindingSource.

Here's my binding (not including the combo box column I add to the datagridview before adding a datasource:

```<code>binding = new BindingSource();
binding.DataSource= _Rows;
_dg.DataSource = binding;
</code>```

Here's the code that fills _Rows

```<code>protected override List<BusinessApp> getRows(DataGridViewManager manager)
    {
        var dc = manager.dc.DcHerb;
        return (from p in dc.apps
                where p.zQueueId == this.ZQueueId
                select p)
                      .ToList().ToBusinessApp(dc);
    }
</code>```

Here's what I'm trying to get a new row. No exception occurs, but the dataGridView doesn't appear to change at all, even though _Rows count goes from 68 to 69

```<code>private void newRow()
    {

        _Rows.Add(new BusinessApp(_manager.dc.DcHerb, new app() {acaps="1234"  }));

        //_dg.DataSource = binding; //tried rebinding here
        //_dg.DataSource = _Rows;
    }
</code>```

答:
How about invoking "AddNew()" method on your "binding" member variable that you have defined already?</p>

```<code>binding.AddNew();
// and set the needed columns...
((binding.Current.Row) as <yourTypedDataTableRow>).<columnName> = <someValue>;
</code>```

ContentLicense: CC BY-SA 2.5

Body:
I'm using a List to bind to a dataGridView.
I'd like the user to be able to add a new row.

I've tried using a BindingList and a BindingSource.

Here's my binding (not including the combo box column I add to the datagridview before adding a datasource:

binding = new BindingSource();
binding.DataSource= _Rows;
_dg.DataSource = binding;

Here's the code that fills _Rows

protected override List<BusinessApp> getRows(DataGridViewManager manager)
    {
        var dc = manager.dc.DcHerb;
        return (from p in dc.apps
                where p.zQueueId == this.ZQueueId
                select p)
                      .ToList().ToBusinessApp(dc);
    }

Here's what I'm trying to get a new row. No exception occurs, but the dataGridView doesn't appear to change at all, even though _Rows count goes from 68 to 69

private void newRow()
    {

        _Rows.Add(new BusinessApp(_manager.dc.DcHerb, new app() {acaps="1234"  }));

        //_dg.DataSource = binding; //tried rebinding here
        //_dg.DataSource = _Rows;
    }

Body:
How about invoking "AddNew()" method on your "binding" member variable that you have defined already?

binding.AddNew();
// and set the needed columns...
((binding.Current.Row) as <yourTypedDataTableRow>).<columnName> = <someValue>;


问:
How can I, scrape data from a Javascript Content of a website?
Actually, I am trying to fetch the content from the Product Description from the Nykaa Website.

URL:- https://www.nykaa.com/nykaa-skinshield-matte-foundation/p/460512?productId=460512&pps=1&skuId=460502

This is the URL, and in the section of the Product description, clicking upon the 'Read More' button, at the end there is some text.

The Text which, I want to extract is :

Explore the entire range of Foundation available on Nykaa. Shop more
Nykaa Cosmetics products here.You can browse through the complete
world of Nykaa Cosmetics Foundation . Alternatively, you can also find
many more products from the Nykaa SkinShield Anti-Pollution Matte
Foundation range.

Expiry Date: 15 February 2024

Country of Origin: India

Name of Mfg / Importer / Brand: FSN E-commerce Ventures Pvt Ltd

Address of Mfg / Importer / Brand: 104 Vasan Udyog Bhavan Sun Mill
Compound Senapati Bapat Marg, Lower Parel, Mumbai City Maharashtra -
400013

After inspecting the page, when I, 'disable the javascript' all the content from 'product description' vanishes off. It means the content is loading dynamically with the help of Javascript.

I have used 'selenium' for this purpose. And This, is what I have tried.

```<code>from msilib.schema import Error
from tkinter import ON
from turtle import goto
import time
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import numpy as np
from random import randint
import pandas as pd
import requests
import csv

browser = webdriver.Chrome(
    r'C:\Users\paart\.wdm\drivers\chromedriver\win32\97.0.4692.71\chromedriver.exe')

browser.maximize_window()  # For maximizing window
browser.implicitly_wait(20)  # gives an implicit wait for 20 seconds

browser.get(
    "https://www.nykaa.com/nykaa-skinshield-matte-foundation/p/460512?productId=460512&pps=1&skuId=460502")

# Creates "load more" button object.
browser.implicitly_wait(20)
loadMore = browser.find_element_by_xpath(xpath="/html/body/div[1]/div/div[3]/div[1]/div[2]/div/div/div[2]")

loadMore.click()
browser.implicitly_wait(20)

desc_data = browser.find_elements_by_class_name('content-details')

for desc in desc_data:
    para_details = browser.find_element_by_xpath(
        './/*[@id="content-details"]/p[1]').text
    extra_details = browser.find_elements_by_xpath(
        './/*[@id="content-details"]/p[2]', './/*[@id="content-details"]/p[3]', './/*[@id="content-details"]/p[4]', './/*[@id="content-details"]/p[5]').text
    print(para_details, extra_details)
</code>```

And this, is the output which is displaying.

```<code>PS E:\Web Scraping - Nykaa> python -u "e:\Web Scraping - Nykaa\scrape_nykaa_final.py"
e:\Web Scraping - Nykaa\scrape_nykaa_final.py:16: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
  browser = webdriver.Chrome(

DevTools listening on ws://127.0.0.1:1033/devtools/browser/097c0e11-6f2c-4742-a2b5-cd05bee72661
e:\Web Scraping - Nykaa\scrape_nykaa_final.py:28: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
  loadMore = browser.find_element_by_xpath(
[9312:4972:0206/110327.883:ERROR:ssl_client_socket_impl.cc(996)] handshake failed; returned -1, SSL error code 1, net_error -101
[9312:4972:0206/110328.019:ERROR:ssl_client_socket_impl.cc(996)] handshake failed; returned -1, SSL error code 1, net_error -101
Traceback (most recent call last):
  File "e:\Web Scraping - Nykaa\scrape_nykaa_final.py", line 28, in <module>
    loadMore = browser.find_element_by_xpath(
  File "C:\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 520, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "C:\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1244, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "C:\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 424, in execute
    self.error_handler.check_response(response)
  File "C:\Python310\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[1]/div/div[3]/div[1]/div[2]/div/div/div[2]"}
  (Session info: chrome=97.0.4692.99)
Stacktrace:
Backtrace:
        Ordinal0 [0x00FDFDC3+2555331]
        Ordinal0 [0x00F777F1+2127857]
        Ordinal0 [0x00E72E08+1060360]
        Ordinal0 [0x00E9E49E+1238174]
        Ordinal0 [0x00E9E69B+1238683]
        Ordinal0 [0x00EC9252+1413714]
        Ordinal0 [0x00EB7B54+1342292]
        Ordinal0 [0x00EC75FA+1406458]
        Ordinal0 [0x00EB7976+1341814]
        Ordinal0 [0x00E936B6+1193654]
        Ordinal0 [0x00E94546+1197382]
        GetHandleVerifier [0x01179622+1619522]
        GetHandleVerifier [0x0122882C+2336844]
        GetHandleVerifier [0x010723E1+541697]
        GetHandleVerifier [0x01071443+537699]
        Ordinal0 [0x00F7D18E+2150798]
        Ordinal0 [0x00F81518+2168088]
        Ordinal0 [0x00F81660+2168416]
        Ordinal0 [0x00F8B330+2208560]
        BaseThreadInitThunk [0x76C9FA29+25]
        RtlGetAppContainerNamedObjectPath [0x77337A9E+286]
        RtlGetAppContainerNamedObjectPath [0x77337A6E+238]
</code>```

Please, anyone help me getting this issue resolved, or any another specific piece of the code to write, which I am missing to fetch the text content from Product description. It would be a big help.

Thanks .

答:
You are getting this error because that element is not loaded correctly while you executed the click function.
I use these 2 function to locate elements:

```<code>def find_until_located(eltype,name):
    element = WebDriverWait(driver, 60).until(
    EC.presence_of_element_located((eltype, name)))
    return element
def find_until_clicklable(eltype,name):
    element=WebDriverWait(driver, 60).until(EC.element_to_be_clickable((eltype, name)))
    return element
</code>```

1st argument will be one of them: By.ID, By.XPATH, By.LINK_TEXT, By.PARTIAL_LINK_TEXT, By.NAME, By.TAG_NAME, By.CLASS_NAME, By.CSS_SELECTOR and 2nd one will be your class name or xpath or id etc.
So now, your code will be:

```<code>from msilib.schema import Error
from tkinter import ON
from turtle import goto
import time
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import numpy as np
from random import randint
import pandas as pd
import requests
import csv
browser = webdriver.Chrome(
    r'C:\Users\paart\.wdm\drivers\chromedriver\win32\97.0.4692.71\chromedriver.exe')

def find_until_located(eltype,name):
    # eltype will be one of them By.ID, By.XPATH, By.LINK_TEXT, By.PARTIAL_LINK_TEXT, By.NAME, By.TAG_NAME, By.CLASS_NAME, By.CSS_SELECTOR
    element = WebDriverWait(browser, 60).until(
    EC.presence_of_element_located((eltype, name)))
    return element
def find_until_clicklable(eltype,name):
    element=WebDriverWait(browser, 60).until(EC.element_to_be_clickable((eltype, name)))
    return element

browser.maximize_window()  # For maximizing window
browser.implicitly_wait(20)  # gives an implicit wait for 20 seconds

browser.get(
    "https://www.nykaa.com/nykaa-skinshield-matte-foundation/p/460512?productId=460512&pps=1&skuId=460502")

# Creates "load more" button object.
browser.implicitly_wait(20)
loadMore = find_until_clicklable(By.XPATH, "/html/body/div[1]/div/div[3]/div[1]/div[2]/div/div/div[2]")
loadMore.click()
browser.implicitly_wait(20)

desc_data = browser.find_until_located(By.CLASS_NAME,'content-details')

for desc in desc_data:
    para_details = browser.find_until_located(By.XPATH,
        './/*[@id="content-details"]/p[1]').text
    extra_details = browser.find_until_located(By.XPATH,
        './/*[@id="content-details"]/p[2]', './/*[@id="content-details"]/p[3]', './/*[@id="content-details"]/p[4]', './/*[@id="content-details"]/p[5]').text
    print(para_details, extra_details)
</code>```

Edit:

I realised the problem and then updated the code
 Here is the final code:
```<code>from msilib.schema import Error
from tkinter import ON
from turtle import goto
import time
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import numpy as np
from random import randint
import pandas as pd
import requests
import csv
browser = webdriver.Chrome(
    r'C:\Users\paart\.wdm\drivers\chromedriver\win32\97.0.4692.71\chromedriver.exe')

def find_until_located(eltype,name):
    # eltype will be one of them By.ID, By.XPATH, By.LINK_TEXT, By.PARTIAL_LINK_TEXT, By.NAME, By.TAG_NAME, By.CLASS_NAME, By.CSS_SELECTOR
    element = WebDriverWait(browser, 60).until(EC.presence_of_element_located((eltype, name)))
    return element
def find_until_clicklable(eltype,name):
    element=WebDriverWait(browser, 60).until(EC.element_to_be_clickable((eltype, name)))
    return element
def scroll_to_element(element):
        browser.execute_script("arguments[0].scrollIntoView();", element)
        return

browser.maximize_window()  # For maximizing window
browser.implicitly_wait(20)  # gives an implicit wait for 20 seconds

browser.get(
    "https://www.nykaa.com/nykaa-skinshield-matte-foundation/p/460512?productId=460512&pps=1&skuId=460502")

# Creates "load more" button object.
browser.implicitly_wait(20)
bag_btn=find_until_located(By.CLASS_NAME, 'css-17hv1os')
scroll_to_element(bag_btn)
desc_label=find_until_located(By.CLASS_NAME, 'css-1g43l8l')
scroll_to_element(desc_label)
# Waiting until loads
browser.implicitly_wait(20)
loadMore = find_until_clicklable(By.XPATH, "/html/body/div[1]/div/div[3]/div[1]/div[2]/div/div/div[2]")
loadMore.click()
browser.implicitly_wait(20)

desc_data = find_until_located(By.ID,'content-details')

for desc in desc_data:
    para_details = find_until_located(By.XPATH,
        './/*[@id="content-details"]/p[1]').text
    extra_details = find_until_located(By.XPATH,
        './/*[@id="content-details"]/p[2]', './/*[@id="content-details"]/p[3]', './/*[@id="content-details"]/p[4]', './/*[@id="content-details"]/p[5]').text
    print(para_details, extra_details)
</code>```

Body:
Actually, I am trying to fetch the content from the Product Description from the Nykaa Website.

URL:- https://www.nykaa.com/nykaa-skinshield-matte-foundation/p/460512?productId=460512&pps=1&skuId=460502

This is the URL, and in the section of the Product description, clicking upon the 'Read More' button, at the end there is some text.

The Text which, I want to extract is :

Explore the entire range of Foundation available on Nykaa. Shop more
Nykaa Cosmetics products here.You can browse through the complete
world of Nykaa Cosmetics Foundation . Alternatively, you can also find
many more products from the Nykaa SkinShield Anti-Pollution Matte
Foundation range.

Expiry Date: 15 February 2024

Country of Origin: India

Name of Mfg / Importer / Brand: FSN E-commerce Ventures Pvt Ltd

Address of Mfg / Importer / Brand: 104 Vasan Udyog Bhavan Sun Mill
Compound Senapati Bapat Marg, Lower Parel, Mumbai City Maharashtra -
400013

After inspecting the page, when I, 'disable the javascript' all the content from 'product description' vanishes off. It means the content is loading dynamically with the help of Javascript.

I have used 'selenium' for this purpose. And This, is what I have tried.

from msilib.schema import Error
from tkinter import ON
from turtle import goto
import time
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import numpy as np
from random import randint
import pandas as pd
import requests
import csv

browser = webdriver.Chrome(
    r'C:\Users\paart\.wdm\drivers\chromedriver\win32\97.0.4692.71\chromedriver.exe')

browser.maximize_window()  # For maximizing window
browser.implicitly_wait(20)  # gives an implicit wait for 20 seconds

browser.get(
    "https://www.nykaa.com/nykaa-skinshield-matte-foundation/p/460512?productId=460512&pps=1&skuId=460502")

# Creates "load more" button object.
browser.implicitly_wait(20)
loadMore = browser.find_element_by_xpath(xpath="/html/body/div[1]/div/div[3]/div[1]/div[2]/div/div/div[2]")

loadMore.click()
browser.implicitly_wait(20)

desc_data = browser.find_elements_by_class_name('content-details')

for desc in desc_data:
    para_details = browser.find_element_by_xpath(
        './/*[@id="content-details"]/p[1]').text
    extra_details = browser.find_elements_by_xpath(
        './/*[@id="content-details"]/p[2]', './/*[@id="content-details"]/p[3]', './/*[@id="content-details"]/p[4]', './/*[@id="content-details"]/p[5]').text
    print(para_details, extra_details)

And this, is the output which is displaying.

PS E:\Web Scraping - Nykaa> python -u "e:\Web Scraping - Nykaa\scrape_nykaa_final.py"
e:\Web Scraping - Nykaa\scrape_nykaa_final.py:16: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
  browser = webdriver.Chrome(

DevTools listening on ws://127.0.0.1:1033/devtools/browser/097c0e11-6f2c-4742-a2b5-cd05bee72661
e:\Web Scraping - Nykaa\scrape_nykaa_final.py:28: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
  loadMore = browser.find_element_by_xpath(
[9312:4972:0206/110327.883:ERROR:ssl_client_socket_impl.cc(996)] handshake failed; returned -1, SSL error code 1, net_error -101
[9312:4972:0206/110328.019:ERROR:ssl_client_socket_impl.cc(996)] handshake failed; returned -1, SSL error code 1, net_error -101
Traceback (most recent call last):
  File "e:\Web Scraping - Nykaa\scrape_nykaa_final.py", line 28, in <module>
    loadMore = browser.find_element_by_xpath(
  File "C:\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 520, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "C:\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1244, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "C:\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 424, in execute
    self.error_handler.check_response(response)
  File "C:\Python310\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[1]/div/div[3]/div[1]/div[2]/div/div/div[2]"}
  (Session info: chrome=97.0.4692.99)
Stacktrace:
Backtrace:
        Ordinal0 [0x00FDFDC3+2555331]
        Ordinal0 [0x00F777F1+2127857]
        Ordinal0 [0x00E72E08+1060360]
        Ordinal0 [0x00E9E49E+1238174]
        Ordinal0 [0x00E9E69B+1238683]
        Ordinal0 [0x00EC9252+1413714]
        Ordinal0 [0x00EB7B54+1342292]
        Ordinal0 [0x00EC75FA+1406458]
        Ordinal0 [0x00EB7976+1341814]
        Ordinal0 [0x00E936B6+1193654]
        Ordinal0 [0x00E94546+1197382]
        GetHandleVerifier [0x01179622+1619522]
        GetHandleVerifier [0x0122882C+2336844]
        GetHandleVerifier [0x010723E1+541697]
        GetHandleVerifier [0x01071443+537699]
        Ordinal0 [0x00F7D18E+2150798]
        Ordinal0 [0x00F81518+2168088]
        Ordinal0 [0x00F81660+2168416]
        Ordinal0 [0x00F8B330+2208560]
        BaseThreadInitThunk [0x76C9FA29+25]
        RtlGetAppContainerNamedObjectPath [0x77337A9E+286]
        RtlGetAppContainerNamedObjectPath [0x77337A6E+238]

Please, anyone help me getting this issue resolved, or any another specific piece of the code to write, which I am missing to fetch the text content from Product description. It would be a big help.

Thanks .

Body:
You are getting this error because that element is not loaded correctly while you executed the click function.
I use these 2 function to locate elements:

def find_until_located(eltype,name):
    element = WebDriverWait(driver, 60).until(
    EC.presence_of_element_located((eltype, name)))
    return element
def find_until_clicklable(eltype,name):
    element=WebDriverWait(driver, 60).until(EC.element_to_be_clickable((eltype, name)))
    return element

1st argument will be one of them: By.ID, By.XPATH, By.LINK_TEXT, By.PARTIAL_LINK_TEXT, By.NAME, By.TAG_NAME, By.CLASS_NAME, By.CSS_SELECTOR and 2nd one will be your class name or xpath or id etc.
So now, your code will be:

from msilib.schema import Error
from tkinter import ON
from turtle import goto
import time
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import numpy as np
from random import randint
import pandas as pd
import requests
import csv
browser = webdriver.Chrome(
    r'C:\Users\paart\.wdm\drivers\chromedriver\win32\97.0.4692.71\chromedriver.exe')

def find_until_located(eltype,name):
    # eltype will be one of them By.ID, By.XPATH, By.LINK_TEXT, By.PARTIAL_LINK_TEXT, By.NAME, By.TAG_NAME, By.CLASS_NAME, By.CSS_SELECTOR
    element = WebDriverWait(browser, 60).until(
    EC.presence_of_element_located((eltype, name)))
    return element
def find_until_clicklable(eltype,name):
    element=WebDriverWait(browser, 60).until(EC.element_to_be_clickable((eltype, name)))
    return element

browser.maximize_window()  # For maximizing window
browser.implicitly_wait(20)  # gives an implicit wait for 20 seconds

browser.get(
    "https://www.nykaa.com/nykaa-skinshield-matte-foundation/p/460512?productId=460512&pps=1&skuId=460502")

# Creates "load more" button object.
browser.implicitly_wait(20)
loadMore = find_until_clicklable(By.XPATH, "/html/body/div[1]/div/div[3]/div[1]/div[2]/div/div/div[2]")
loadMore.click()
browser.implicitly_wait(20)

desc_data = browser.find_until_located(By.CLASS_NAME,'content-details')

for desc in desc_data:
    para_details = browser.find_until_located(By.XPATH,
        './/*[@id="content-details"]/p[1]').text
    extra_details = browser.find_until_located(By.XPATH,
        './/*[@id="content-details"]/p[2]', './/*[@id="content-details"]/p[3]', './/*[@id="content-details"]/p[4]', './/*[@id="content-details"]/p[5]').text
    print(para_details, extra_details)

Edit:

I realised the problem and then updated the code
 Here is the final code:

from msilib.schema import Error
from tkinter import ON
from turtle import goto
import time
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import numpy as np
from random import randint
import pandas as pd
import requests
import csv
browser = webdriver.Chrome(
    r'C:\Users\paart\.wdm\drivers\chromedriver\win32\97.0.4692.71\chromedriver.exe')

def find_until_located(eltype,name):
    # eltype will be one of them By.ID, By.XPATH, By.LINK_TEXT, By.PARTIAL_LINK_TEXT, By.NAME, By.TAG_NAME, By.CLASS_NAME, By.CSS_SELECTOR
    element = WebDriverWait(browser, 60).until(EC.presence_of_element_located((eltype, name)))
    return element
def find_until_clicklable(eltype,name):
    element=WebDriverWait(browser, 60).until(EC.element_to_be_clickable((eltype, name)))
    return element
def scroll_to_element(element):
        browser.execute_script("arguments[0].scrollIntoView();", element)
        return

browser.maximize_window()  # For maximizing window
browser.implicitly_wait(20)  # gives an implicit wait for 20 seconds

browser.get(
    "https://www.nykaa.com/nykaa-skinshield-matte-foundation/p/460512?productId=460512&pps=1&skuId=460502")

# Creates "load more" button object.
browser.implicitly_wait(20)
bag_btn=find_until_located(By.CLASS_NAME, 'css-17hv1os')
scroll_to_element(bag_btn)
desc_label=find_until_located(By.CLASS_NAME, 'css-1g43l8l')
scroll_to_element(desc_label)
# Waiting until loads
browser.implicitly_wait(20)
loadMore = find_until_clicklable(By.XPATH, "/html/body/div[1]/div/div[3]/div[1]/div[2]/div/div/div[2]")
loadMore.click()
browser.implicitly_wait(20)

desc_data = find_until_located(By.ID,'content-details')

for desc in desc_data:
    para_details = find_until_located(By.XPATH,
        './/*[@id="content-details"]/p[1]').text
    extra_details = find_until_located(By.XPATH,
        './/*[@id="content-details"]/p[2]', './/*[@id="content-details"]/p[3]', './/*[@id="content-details"]/p[4]', './/*[@id="content-details"]/p[5]').text
    print(para_details, extra_details)


问:
How to append np.array?
I have an np.array d1 of (3,6), and an np.array a4 of (6,).

How can I combine the two np.arrays to form an np.array d2 of (4,6)?

My code is as follows:

```<code>import numpy as np
a1=np.array(range(6))
a2=a1+2
a3=a2+3
a4=a3+4
d1=np.array([a1,a2,a3])
d1.shape
</code>```
```<code>Out[44]: (3, 6)
</code>```
```<code>d2=np.array([a1,a2,a3,a4])
d2.shape
d2
</code>```
```<code>Out[45]: (4, 6)
Out[46]:
array([[ 0,  1,  2,  3,  4,  5],
       [ 2,  3,  4,  5,  6,  7],
       [ 5,  6,  7,  8,  9, 10],
       [ 9, 10, 11, 12, 13, 14]])
</code>```

How to get d2 from d1 and a4?

I tried np.insert and np.append, but maybe my usage is wrong and I didn't get the correct result.

答:
This may be belaboring the point, but:

```<code>In [156]: d1.shape
Out[156]: (3, 6)
In [157]: a4.shape
Out[157]: (6,)               # not (1,6)
In [158]: np.append(d1,a4)
Out[158]:
array([ 0,  1,  2,  3,  4,  5,  2,  3,  4,  5,  6,  7,  5,  6,  7,  8,  9,
       10,  9, 10, 11, 12, 13, 14])
</code>```

np.append says it flattens the arrays, unless we provide an axis.

```<code>In [159]: np.append(d1,a4, axis=0)
Traceback (most recent call last):
  File "<ipython-input-159-dd72c66fd0c0>", line 1, in <module>
    np.append(d1,a4, axis=0)
  File "<__array_function__ internals>", line 180, in append
  File "/usr/local/lib/python3.8/dist-packages/numpy/lib/function_base.py", line 5392, in append
    return concatenate((arr, values), axis=axis)
  File "<__array_function__ internals>", line 180, in concatenate
ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s)
</code>```

Note that append has called np.concatenate.  That's all it really does.  np.append should be removed - it misleads too many novices.

Any ways, concatenate expects the arrays to match in number of dimensions. The error should be clear about the problem.  The solution is, as the other answers show, to turn the (6,) into a (1,6).

Title: How to append np.array?

Body:
I have an np.array d1 of (3,6), and an np.array a4 of (6,).

How can I combine the two np.arrays to form an np.array d2 of (4,6)?

My code is as follows:

import numpy as np
a1=np.array(range(6))
a2=a1+2
a3=a2+3
a4=a3+4
d1=np.array([a1,a2,a3])
d1.shape

Out[44]: (3, 6)

d2=np.array([a1,a2,a3,a4])
d2.shape
d2

Out[45]: (4, 6)
Out[46]:
array([[ 0,  1,  2,  3,  4,  5],
       [ 2,  3,  4,  5,  6,  7],
       [ 5,  6,  7,  8,  9, 10],
       [ 9, 10, 11, 12, 13, 14]])

How to get d2 from d1 and a4?

I tried np.insert and np.append, but maybe my usage is wrong and I didn't get the correct result.

Body:
This may be belaboring the point, but:

In [156]: d1.shape
Out[156]: (3, 6)
In [157]: a4.shape
Out[157]: (6,)               # not (1,6)
In [158]: np.append(d1,a4)
Out[158]:
array([ 0,  1,  2,  3,  4,  5,  2,  3,  4,  5,  6,  7,  5,  6,  7,  8,  9,
       10,  9, 10, 11, 12, 13, 14])

np.append says it flattens the arrays, unless we provide an axis.

In [159]: np.append(d1,a4, axis=0)
Traceback (most recent call last):
  File "<ipython-input-159-dd72c66fd0c0>", line 1, in <module>
    np.append(d1,a4, axis=0)
  File "<__array_function__ internals>", line 180, in append
  File "/usr/local/lib/python3.8/dist-packages/numpy/lib/function_base.py", line 5392, in append
    return concatenate((arr, values), axis=axis)
  File "<__array_function__ internals>", line 180, in concatenate
ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s)

Note that append has called np.concatenate.  That's all it really does.  np.append should be removed - it misleads too many novices.

Any ways, concatenate expects the arrays to match in number of dimensions. The error should be clear about the problem.  The solution is, as the other answers show, to turn the (6,) into a (1,6).


问:
Jenkins role based strategy is not reflecting
I would like to manage my users in Jenkins with admin.I m planning to do it by Role based strategy in configure global setting option. But i cant see the option even Cloudbees Role Based Access Control Plugin is installed in my plugin list.
Any suggestions?

Note: As I am in a env where no internet access,  it is very difficult to re install the plugin and try again.

Thanks
Jeevan

答:
I too had faced the same problem. Jenkins doesn't come with the Role-Based Strategy plugin by default. We have to install this plugin as per our requirements. Sorry, but for installation, you have to go online, because this plugin requires installation from the web.

After connection, just go to 'Manage Plugins'.

-> Under 'Available', search for "role-based".

-> Now you can see the 'Role-Based Authorization Strategy' plugin, which you can select and then easily install.

I recommend, if no build job is running, you should select the 'Download now and install after restart' options, so that the plugin starts working perfectly right after restarting.

Now, go to 'Configure Global Security'.

-> there you can see now 'Role-Based Strategy' under Authorization which you have to select for enabling Role-based strategy on users.


问:
I have some problems in timing in python
I'm trying to learn python from the book
https://www.amazon.com/Learning-Python-5th-Mark-Lutz/dp/1449355730
and I have some problems in chapter 21 that is about timing in python

this book has given this module to get operation time of functions :

```<code>"""
homegrown timing tools for function calls.
Does the total-time, best-of-time, and best of totals time.
"""
import time , sys
timer = time.perf_counter

def total(reps , func , *pargs , **kargs) :
    replist = list(range(reps))
    start   = timer()
    for i in replist :
        ret = func(*pargs , **kargs)

    elapsed = timer() - start
    return (elapsed , ret)

def bestof (reps , func , *pargs , **kargs) :
    best = 2 ** 32
    for i in range(reps) :
        start = timer()
        ret = func(*pargs , **kargs)
        elapsed = timer() - start
        if elapsed < best : best = elapsed

    return (best , ret)

def bestoftotal(reps1 ,reps2 , func , *pargs , **kargs ) :
    return bestof(reps1 , total , reps2  , func , *pargs , **kargs)

</code>```

the first function gives us the total time of a function when we call it 1000 times
the second one calculates the time of each 1000 time operation and gives the best one
and the third one gives the best of totals, mean that calculates totals and gives the best one (min time ), my problem here is exactly in the third one.
in the main program I'm trying to run it and please look at the output :

```<code>import timer
A= timer.bestoftotal(1000 , 1000 , pow , 2 , 1000)
print('pow best totals : ' ,A[0]  , A[1][0])
</code>```

and this is my output

```<code>PS C:\Users\Ata Barzegar\Desktop\learn> & "C:/Users/Ata Barzegar/AppData/Local/Programs/Python/Python37/python.exe" "c:/Users/Ata Barzegar/Desktop/learn/learnpy.py"
pow best totals :  0.0011224999999999152       0.001119799999999893
</code>```

the problem is that we are trying to get the minimum time of total  but the second number is smaller than the first one,plz help me
Also, I should mention this does not happen every time.

答:
```<code>def bestof(reps, func, *pargs, **kargs):
    best = 2 ** 32
    best_ret = None
    for i in range(reps):
        start = timer()
        ret = func(*pargs, **kargs)
        elapsed = timer() - start
        if elapsed < best:
            best = elapsed
            best_ret = ret
    return (best, best_ret)
</code>```

in bestof function, you always return the last ret. So, if you want to compare those two timings, you should declare a best_ret and return it

Body:
I'm trying to learn python from the book
https://www.amazon.com/Learning-Python-5th-Mark-Lutz/dp/1449355730
and I have some problems in chapter 21 that is about timing in python

this book has given this module to get operation time of functions :

"""
homegrown timing tools for function calls.
Does the total-time, best-of-time, and best of totals time.
"""
import time , sys
timer = time.perf_counter

def total(reps , func , *pargs , **kargs) :
    replist = list(range(reps))
    start   = timer()
    for i in replist :
        ret = func(*pargs , **kargs)

    elapsed = timer() - start
    return (elapsed , ret)

def bestof (reps , func , *pargs , **kargs) :
    best = 2 ** 32
    for i in range(reps) :
        start = timer()
        ret = func(*pargs , **kargs)
        elapsed = timer() - start
        if elapsed < best : best = elapsed

    return (best , ret)

def bestoftotal(reps1 ,reps2 , func , *pargs , **kargs ) :
    return bestof(reps1 , total , reps2  , func , *pargs , **kargs)

the first function gives us the total time of a function when we call it 1000 times
the second one calculates the time of each 1000 time operation and gives the best one
and the third one gives the best of totals, mean that calculates totals and gives the best one (min time ), my problem here is exactly in the third one.
in the main program I'm trying to run it and please look at the output :

import timer
A= timer.bestoftotal(1000 , 1000 , pow , 2 , 1000)
print('pow best totals : ' ,A[0]  , A[1][0])

and this is my output

PS C:\Users\Ata Barzegar\Desktop\learn> & "C:/Users/Ata Barzegar/AppData/Local/Programs/Python/Python37/python.exe" "c:/Users/Ata Barzegar/Desktop/learn/learnpy.py"
pow best totals :  0.0011224999999999152       0.001119799999999893

the problem is that we are trying to get the minimum time of total  but the second number is smaller than the first one,plz help me
Also, I should mention this does not happen every time.

Body:
def bestof(reps, func, *pargs, **kargs):
    best = 2 ** 32
    best_ret = None
    for i in range(reps):
        start = timer()
        ret = func(*pargs, **kargs)
        elapsed = timer() - start
        if elapsed < best:
            best = elapsed
            best_ret = ret
    return (best, best_ret)

in bestof function, you always return the last ret. So, if you want to compare those two timings, you should declare a best_ret and return it


问:
Bad URI(is not URI?) while trying to use Capybara Apparition driver
it's my first time trying a webdriver different than selenium with capybara.

I've made this very small script below just to see it running but invalid uri error is thrown.

```<code>require 'capybara'
require 'capybara/DSL'
require 'capybara/apparition'

include Capybara::DSL

Capybara.javascript_driver = :apparition
Capybara.default_driver = :apparition

visit('https://github.com/twalpole/apparition')
</code>```

The error thrown is:

```<code>> ruby test.rb
Traceback (most recent call last):
        15: from test.rb:9:in `<main>'
        14: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/capybara-3.34.0/lib/capybara/DSL.rb:58:in `block (2 levels) in <module:DSL>'
        13: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/capybara-3.34.0/lib/capybara/session.rb:278:in `visit'
        11: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/apparition-0.6.0/lib/capybara/apparition/driver.rb:50:in `browser'
        10: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/apparition-0.6.0/lib/capybara/apparition/driver.rb:69:in `client'
         9: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/apparition-0.6.0/lib/capybara/apparition/driver/chrome_client.rb:16:in `client'
         8: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/apparition-0.6.0/lib/capybara/apparition/driver/chrome_client.rb:16:in `new'
         7: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/apparition-0.6.0/lib/capybara/apparition/driver/chrome_client.rb:33:in `initialize'
         6: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/apparition-0.6.0/lib/capybara/apparition/driver/chrome_client.rb:33:in `new'
         5: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/apparition-0.6.0/lib/capybara/apparition/driver/web_socket_client.rb:10:in `initialize'
         4: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/apparition-0.6.0/lib/capybara/apparition/driver/web_socket_client.rb:10:in `new'
         3: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/apparition-0.6.0/lib/capybara/apparition/driver/web_socket_client.rb:69:in `initialize'
         2: from C:/Ruby26-x64/lib/ruby/2.6.0/uri/common.rb:234:in `parse'
         1: from C:/Ruby26-x64/lib/ruby/2.6.0/uri/rfc3986_parser.rb:73:in `parse'
C:/Ruby26-x64/lib/ruby/2.6.0/uri/rfc3986_parser.rb:67:in `split': bad URI(is not URI?): "ws://127.0.0.1:65515/devtools/browser/1c053b9f-60e6-4cef-822f-4d336aac7fad\r" (URI::InvalidURIError)
</code>```

Am I missing something?

I've these versions installed: capybara (3.34.0), apparition (0.6.0) and chrome (91.0.4472.77) in windows 10 pro 20H2.

答:
I had the same issue while running Apparition driver.

It seems the issue is present while instantiating new TCP Socket done by the Socket Class defined in Apparition/driver/web_socket_client

The URL passed in the initialize of Socket Class seems to have trailing white space .

Workaround

Navigate to the following path Ruby26-x64\lib\ruby\gems\2.6.0\gems\apparition-0.6.0\lib\capybara\apparition\driver

Note:- The location of Ruby26-x64 depends on where you installed ruby on your system for me it was in C drive

Modify the web_socket_client.rb file present in the above path as shown in the below image

Web_Socket_Client Change

This fixed the issue for me

Body:
it's my first time trying a webdriver different than selenium with capybara.

I've made this very small script below just to see it running but invalid uri error is thrown.

require 'capybara'
require 'capybara/DSL'
require 'capybara/apparition'

include Capybara::DSL

Capybara.javascript_driver = :apparition
Capybara.default_driver = :apparition

visit('https://github.com/twalpole/apparition')

The error thrown is:

> ruby test.rb
Traceback (most recent call last):
        15: from test.rb:9:in `'
        14: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/capybara-3.34.0/lib/capybara/DSL.rb:58:in `block (2 levels) in <module:DSL>'
        13: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/capybara-3.34.0/lib/capybara/session.rb:278:in `visit'
        11: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/apparition-0.6.0/lib/capybara/apparition/driver.rb:50:in `browser'
        10: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/apparition-0.6.0/lib/capybara/apparition/driver.rb:69:in `client'
         9: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/apparition-0.6.0/lib/capybara/apparition/driver/chrome_client.rb:16:in `client'
         8: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/apparition-0.6.0/lib/capybara/apparition/driver/chrome_client.rb:16:in `new'
         7: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/apparition-0.6.0/lib/capybara/apparition/driver/chrome_client.rb:33:in `initialize'
         6: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/apparition-0.6.0/lib/capybara/apparition/driver/chrome_client.rb:33:in `new'
         5: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/apparition-0.6.0/lib/capybara/apparition/driver/web_socket_client.rb:10:in `initialize'
         4: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/apparition-0.6.0/lib/capybara/apparition/driver/web_socket_client.rb:10:in `new'
         3: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/apparition-0.6.0/lib/capybara/apparition/driver/web_socket_client.rb:69:in `initialize'
         2: from C:/Ruby26-x64/lib/ruby/2.6.0/uri/common.rb:234:in `parse'
         1: from C:/Ruby26-x64/lib/ruby/2.6.0/uri/rfc3986_parser.rb:73:in `parse'
C:/Ruby26-x64/lib/ruby/2.6.0/uri/rfc3986_parser.rb:67:in `split': bad URI(is not URI?): "ws://127.0.0.1:65515/devtools/browser/1c053b9f-60e6-4cef-822f-4d336aac7fad\r" (URI::InvalidURIError)

Am I missing something?

I've these versions installed: capybara (3.34.0), apparition (0.6.0) and chrome (91.0.4472.77) in windows 10 pro 20H2.


问:
What the logic behind b1 ? b1 : b2 in my coding in if-conditions?
1.Here is the assignment.

Declare a function or that works like ||, but without using the || operator.
/**

@param {any} ??? - the first operand

@param {any} ??? - the second operand

@returns {any} the same result as applying the || operator to the given operands, in order

2.Here is what tried(actually people gave me advice on return b1 ? b1 : b2.But i couldn't understand it ,and haven't found a proper explanation about it online.

```<code>function or(b1, b2) {
  if (b1 == false && b2 == false) return false;
  else return b1 ? b1 : b2;
}
</code>```

3.Here are the coding tests, and the code above passed all of them. But would anyone tell me the logic of b1 ? b1 : b2. I am a beginner, please help me !

//TEST 1

```<code>actual = or("bananas", false);

expected = "bananas";

if (actual === expected) {
  console.log("Yay! Test PASSED.");
} else {
  console.error("Test FAILED. Keep trying!");
  console.log("    actual: ", actual);
  console.log("  expected: ", expected);
}
</code>```

//TEST 2

```<code>actual = or("", "bananas");

expected = "bananas";

if (actual === expected) {
  console.log("Yay! Test PASSED.");
} else {
  console.error("Test FAILED. Keep trying!");
  console.log("    actual: ", actual);
  console.log("  expected: ", expected);
}
</code>```

//TEST 3

```<code>actual = or(true, true);

expected = true;

if (actual === expected) {
  console.log("Yay! Test PASSED.");
} else {
  console.error("Test FAILED. Keep trying!");
  console.log("    actual: ", actual);
  console.log("  expected: ", expected);
}
</code>```

//TEST 4

```<code>actual = or(true, false);

expected = true;

if (actual === expected) {
  console.log("Yay! Test PASSED.");
} else {
  console.error("Test FAILED. Keep trying!");
  console.log("    actual: ", actual);
  console.log("  expected: ", expected);
}
</code>```

答:
b1 ? b1 : b2

This means that if b1 is true/valid it will simply returns the result due to code of b1 otherwise it will simply return the result due to code of b2

Body:
1.Here is the assignment.

Declare a function or that works like ||, but without using the || operator.
/**

@param {any} ??? - the first operand

@param {any} ??? - the second operand

@returns {any} the same result as applying the || operator to the given operands, in order

2.Here is what tried(actually people gave me advice on return b1 ? b1 : b2.But i couldn't understand it ,and haven't found a proper explanation about it online.

function or(b1, b2) {
  if (b1 == false && b2 == false) return false;
  else return b1 ? b1 : b2;
}

3.Here are the coding tests, and the code above passed all of them. But would anyone tell me the logic of b1 ? b1 : b2. I am a beginner, please help me !

//TEST 1

actual = or("bananas", false);

expected = "bananas";

if (actual === expected) {
  console.log("Yay! Test PASSED.");
} else {
  console.error("Test FAILED. Keep trying!");
  console.log("    actual: ", actual);
  console.log("  expected: ", expected);
}

//TEST 2

actual = or("", "bananas");

expected = "bananas";

if (actual === expected) {
  console.log("Yay! Test PASSED.");
} else {
  console.error("Test FAILED. Keep trying!");
  console.log("    actual: ", actual);
  console.log("  expected: ", expected);
}

//TEST 3

actual = or(true, true);

expected = true;

if (actual === expected) {
  console.log("Yay! Test PASSED.");
} else {
  console.error("Test FAILED. Keep trying!");
  console.log("    actual: ", actual);
  console.log("  expected: ", expected);
}

//TEST 4

actual = or(true, false);

expected = true;

if (actual === expected) {
  console.log("Yay! Test PASSED.");
} else {
  console.error("Test FAILED. Keep trying!");
  console.log("    actual: ", actual);
  console.log("  expected: ", expected);
}


问:
Exclude particular Web Api endpoint from the swagger UI
I'm using Swagger UI with asp.net core web api. For my CRUD operations, I have a base controller class, which defines abstract methods:

```<code>public class BaseController<TDto, TEntity>
{
    [HttpPost, Route("create-single")]
    public abstract Task<ObjectResult> CreateAsync(TDto input);

    [HttpPost, Route("create-many")]
    public abstract Task<ObjectResult> CreateManyAsync(IList<TDto> input);

    [HttpGet, Route("get-all")]
    public abstract Task<ObjectResult> GetAllAsync();

    [HttpGet, Route("get/{id}")]
    public abstract Task<ObjectResult> GetByIdAsync(Guid id);

    ...
    ...
}
</code>```

Some controller might not need all the CRUD methods, and I need to disappear those endpoints from the swagger.

For example, I need to disappear /get/{id} endpoint for particular controller, what is the best way to achieve this?

答: You can add the following attribute to Controllers and Actions to exclude them from the generated documentation: [ApiExplorerSettings(IgnoreApi = true)]

Body:
I'm using Swagger UI with asp.net core web api. For my CRUD operations, I have a base controller class, which defines abstract methods:

public class BaseController<TDto, TEntity>
{
    [HttpPost, Route("create-single")]
    public abstract Task<ObjectResult> CreateAsync(TDto input);

    [HttpPost, Route("create-many")]
    public abstract Task<ObjectResult> CreateManyAsync(IList<TDto> input);

    [HttpGet, Route("get-all")]
    public abstract Task<ObjectResult> GetAllAsync();

    [HttpGet, Route("get/{id}")]
    public abstract Task<ObjectResult> GetByIdAsync(Guid id);

    ...
    ...
}

Some controller might not need all the CRUD methods, and I need to disappear those endpoints from the swagger.

For example, I need to disappear /get/{id} endpoint for particular controller, what is the best way to achieve this?


问:
Dayjs: Any workaround for format duration in h m
I am using dayjs library's duration object for duration format.

```<code>dayjs.duration(1500, 'minutes').format('H[h] m[m]') // returns 1h 0m
</code>```

Seems the library converts the 24hrs to 1 day and puts the remaining 1 hour in hours.
Actually, I want the output as 25h 0m.

Is there any way we can achieve this with days.js or any minimal custom logic along with the use of duration object?? Or any other library provides this functionality itself??

答:
Dayjs's documentation about duration formatting suggests you have to give it the formatted including the day part if you want to get the duration in days.

So something like this is expected on the format string:

```<code>dayjs.duration(1500, 'minutes').format('D[d] H[h] m[m]')

==> "1d 1h 0m"
</code>```

Similar would be expected for months and years.

Alternatively you could use the humanise helper, but it is less precise:

```<code>dayjs.duration(1500, 'minutes').humanize()

==> "a day"
</code>```

Body:
I am using dayjs library's duration object for duration format.

dayjs.duration(1500, 'minutes').format('H[h] m[m]') // returns 1h 0m

Seems the library converts the 24hrs to 1 day and puts the remaining 1 hour in hours.
Actually, I want the output as 25h 0m.

Is there any way we can achieve this with days.js or any minimal custom logic along with the use of duration object?? Or any other library provides this functionality itself??

Body:
Dayjs's documentation about duration formatting suggests you have to give it the formatted including the day part if you want to get the duration in days.

So something like this is expected on the format string:

dayjs.duration(1500, 'minutes').format('D[d] H[h] m[m]')

==> "1d 1h 0m"

Similar would be expected for months and years.

Alternatively you could use the humanise helper, but it is less precise:

dayjs.duration(1500, 'minutes').humanize()

==> "a day"


问:
INSERT statement gives column reference is ambiguous error when used in Postgres function
I have the following function in Postgres for inserting values to a table employee_leaves and the table has the following structure

id[PK]
emp_id[varchar]
lt_id[int]
from_dt[date]
from_session_id[int]
to_dt[Date]
to_session_id[int]
reasons[varchar]
contact_details[varchar]
status[int]
notify_users[jsonb]
pending_with[int]
other_info[jsonb]
created_by[int]
created_on[date]
last_updated_by[int]
last_updated_on[date]

The function is as follows:

```<code>-- FUNCTION: company.lib.org.create_edit_employee_leave(json)

-- DROP FUNCTION IF EXISTS "company.lib.org".create_edit_employee_leave(json);

CREATE OR REPLACE FUNCTION "company.lib.org".create_edit_employee_leave(
    params json)
    RETURNS json
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
AS $BODY$
--select"company.lib.org".create_edit_employee_leave('{"emp_id":292, "lt_id":1, "from_dt":"2022-01-10", "from_session_id":1,"to_dt":"2022-01-11" ,"to_session_id":"2", "reasons":"Leave Reason", "contact_details":"8884514965", "status":1, "notify_users":[1,2,3] ,"pending_with":3, "other_info":"{}"," created_by":"123", "last_updated_by":13, "last_updated_on":"2022-01-20 00:00:00"}');

declare
        emp_id character varying(50);
        query text;
        cond text;
        response json;
        message character varying(50);

        lt_id smallint;
        from_dt date;
        from_session_id smallint;
        to_dt date;
        to_session_id smallint;
        reasons character varying(1024);
        contact_details character varying(1024);
        status smallint;
        notify_users json;
        pending_with character varying(50);
        other_info json;
        created_by integer;
        created_on timestamp with time zone;
        last_updated_by integer;
        last_updated_on timestamp with time zone;

        begin
        emp_id = (params->>'emp_id');
        lt_id = (params->>'lt_id');
        from_dt = (params->>'from_dt');
        from_session_id = (params->>'from_session_id');
        to_dt =  (params->>'to_dt');
        to_session_id = (params->>'to_session_id')::smallint;
        reasons = (params->>'reasons');
        contact_details = (params->>'contact_details');
        status = (params->>'status')::smallint;
        notify_users = COALESCE((params ->> 'notify_users')::jsonb,'{}'::jsonb);
        pending_with=(params->>'pending_with');
        other_info = COALESCE((params ->> 'other_info')::jsonb,'{}'::jsonb);
        created_by = (params->>'created_by')::integer;
        created_on = (params->>'created_on')::date;
        last_updated_by = (params->>'last_updated_by')::integer;
        last_updated_on = (params->>'last_updated_on')::date;
        message = '';
        raise info 'leave_application_id %',emp_id;

        if (params->>'action') = 'C' then
        INSERT INTO "company.lib.org".employee_leaves(
        emp_id, lt_id, from_dt, from_session_id, to_dt, to_session_id, reasons, contact_details, status, notify_users, pending_with, other_info, created_by, created_on, last_updated_by, last_updated_on)
        VALUES (
            emp_id, lt_id, from_dt, from_session_id, to_dt, to_session_id, reasons, contact_details, status, notify_users, pending_with, other_info, created_by, created_on, last_updated_by, last_updated_on
        ) returning emp_id into emp_id;
        end if;
        message = 'New Leave application created successfully';

        response = ('{"status": 200, "leave_application_id": "' || emp_id || '", "message":"' || message ||'"}')::json;

       return response;

    EXCEPTION
        WHEN unique_violation THEN
            response = '{"status": 500, "data": "Unique key violation"}';
        WHEN check_violation THEN
            response = '{"status": 500, "data": "Check violation"}';
        WHEN no_data_found THEN
            response = '{"status": 500, "data": "Check"}';
        WHEN others THEN
            RAISE INFO 'Error Name:%', SQLERRM;
            RAISE INFO 'Error State:%', SQLSTATE;
            response = '{"status": 500, "data": "Unknown exception '|| SQLERRM || '"}';
    return response;

    END;

$BODY$;

ALTER FUNCTION "company.lib.org".create_edit_employee_leave(json)
    OWNER TO postgres;
</code>```

Now when I use the following INSERT directly, it is working fine

```<code>INSERT INTO "company.lib.org".employee_leaves(
        emp_id, lt_id, from_dt, from_session_id, to_dt, to_session_id, reasons, contact_details, status, notify_users, pending_with, other_info, created_by, created_on, last_updated_by, last_updated_on)
        VALUES (
            '25506', 1, '2020-01-11', 1, '2020-01-12', 2, 'Leave Reasons', '8884514965', 1, '[25509,25336]', 25509, '{}', 25506, '2022-02-06T06:01:31.025Z', 25506, '2022-02-06T06:01:31.025Z'
        )
</code>```

But when I call function like this,

```<code>select "company.lib.org".create_edit_employee_leave(
    (
        '{"action":"C","emp_id":"25506","lt_id":1,"from_dt":"2020-01-11","from_session_id":1,"to_dt":"2020-01-12","to_session_id":2,"reasons":"LeaveReasons","contact_details":"8884514965","status":1,"notify_users":["25509","25336"],"pending_with":25509,"other_info":{},"created_by":"25506","created_on":"2022-02-06T06:08:11.531Z","last_updated_by":"25506","last_updated_on":"2022-02-06T06:08:11.531Z"}'
    )::json
</code>```

)

It is giving an error like

```<code>INFO:  Error Name:column reference "emp_id" is ambiguous
INFO:  Error State:42702
</code>```

As I have shown in the table structure, the column exists in the table and there are no other columns with the same name. However, I have other tables with the same column name and there is a foreign key constraint that refers to the employee table and the emp_id in that table. Also, this is an INSERT statement. So I am not sure what went wrong here. If someone can tell me what is the error here, that would be helpful.

答: ambiguous is caused by having two tables with identical column names like users.active and profile.active it is clear when your using users or profiles table alone, but if you join them then you have two active columns that belong to different tables, so you need to clarify for the query what you are trying to use: is it users.active or profiles.active explicitly

Body:
I have the following function in Postgres for inserting values to a table employee_leaves and the table has the following structure

id[PK]
emp_id[varchar]
lt_id[int]
from_dt[date]
from_session_id[int]
to_dt[Date]
to_session_id[int]
reasons[varchar]
contact_details[varchar]
status[int]
notify_users[jsonb]
pending_with[int]
other_info[jsonb]
created_by[int]
created_on[date]
last_updated_by[int]
last_updated_on[date]

The function is as follows:

-- FUNCTION: company.lib.org.create_edit_employee_leave(json)

-- DROP FUNCTION IF EXISTS "company.lib.org".create_edit_employee_leave(json);

CREATE OR REPLACE FUNCTION "company.lib.org".create_edit_employee_leave(
    params json)
    RETURNS json
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
AS $BODY$
--select"company.lib.org".create_edit_employee_leave('{"emp_id":292, "lt_id":1, "from_dt":"2022-01-10", "from_session_id":1,"to_dt":"2022-01-11" ,"to_session_id":"2", "reasons":"Leave Reason", "contact_details":"8884514965", "status":1, "notify_users":[1,2,3] ,"pending_with":3, "other_info":"{}"," created_by":"123", "last_updated_by":13, "last_updated_on":"2022-01-20 00:00:00"}');

declare
        emp_id character varying(50);
        query text;
        cond text;
        response json;
        message character varying(50);

        lt_id smallint;
        from_dt date;
        from_session_id smallint;
        to_dt date;
        to_session_id smallint;
        reasons character varying(1024);
        contact_details character varying(1024);
        status smallint;
        notify_users json;
        pending_with character varying(50);
        other_info json;
        created_by integer;
        created_on timestamp with time zone;
        last_updated_by integer;
        last_updated_on timestamp with time zone;

        begin
        emp_id = (params->>'emp_id');
        lt_id = (params->>'lt_id');
        from_dt = (params->>'from_dt');
        from_session_id = (params->>'from_session_id');
        to_dt =  (params->>'to_dt');
        to_session_id = (params->>'to_session_id')::smallint;
        reasons = (params->>'reasons');
        contact_details = (params->>'contact_details');
        status = (params->>'status')::smallint;
        notify_users = COALESCE((params ->> 'notify_users')::jsonb,'{}'::jsonb);
        pending_with=(params->>'pending_with');
        other_info = COALESCE((params ->> 'other_info')::jsonb,'{}'::jsonb);
        created_by = (params->>'created_by')::integer;
        created_on = (params->>'created_on')::date;
        last_updated_by = (params->>'last_updated_by')::integer;
        last_updated_on = (params->>'last_updated_on')::date;
        message = '';
        raise info 'leave_application_id %',emp_id;

        if (params->>'action') = 'C' then
        INSERT INTO "company.lib.org".employee_leaves(
        emp_id, lt_id, from_dt, from_session_id, to_dt, to_session_id, reasons, contact_details, status, notify_users, pending_with, other_info, created_by, created_on, last_updated_by, last_updated_on)
        VALUES (
            emp_id, lt_id, from_dt, from_session_id, to_dt, to_session_id, reasons, contact_details, status, notify_users, pending_with, other_info, created_by, created_on, last_updated_by, last_updated_on
        ) returning emp_id into emp_id;
        end if;
        message = 'New Leave application created successfully';

        response = ('{"status": 200, "leave_application_id": "' || emp_id || '", "message":"' || message ||'"}')::json;

       return response;

    EXCEPTION
        WHEN unique_violation THEN
            response = '{"status": 500, "data": "Unique key violation"}';
        WHEN check_violation THEN
            response = '{"status": 500, "data": "Check violation"}';
        WHEN no_data_found THEN
            response = '{"status": 500, "data": "Check"}';
        WHEN others THEN
            RAISE INFO 'Error Name:%', SQLERRM;
            RAISE INFO 'Error State:%', SQLSTATE;
            response = '{"status": 500, "data": "Unknown exception '|| SQLERRM || '"}';
    return response;

    END;

$BODY$;

ALTER FUNCTION "company.lib.org".create_edit_employee_leave(json)
    OWNER TO postgres;

Now when I use the following INSERT directly, it is working fine

INSERT INTO "company.lib.org".employee_leaves(
        emp_id, lt_id, from_dt, from_session_id, to_dt, to_session_id, reasons, contact_details, status, notify_users, pending_with, other_info, created_by, created_on, last_updated_by, last_updated_on)
        VALUES (
            '25506', 1, '2020-01-11', 1, '2020-01-12', 2, 'Leave Reasons', '8884514965', 1, '[25509,25336]', 25509, '{}', 25506, '2022-02-06T06:01:31.025Z', 25506, '2022-02-06T06:01:31.025Z'
        )

But when I call function like this,

select "company.lib.org".create_edit_employee_leave(
    (
        '{"action":"C","emp_id":"25506","lt_id":1,"from_dt":"2020-01-11","from_session_id":1,"to_dt":"2020-01-12","to_session_id":2,"reasons":"LeaveReasons","contact_details":"8884514965","status":1,"notify_users":["25509","25336"],"pending_with":25509,"other_info":{},"created_by":"25506","created_on":"2022-02-06T06:08:11.531Z","last_updated_by":"25506","last_updated_on":"2022-02-06T06:08:11.531Z"}'
    )::json

)

It is giving an error like

INFO:  Error Name:column reference "emp_id" is ambiguous
INFO:  Error State:42702

As I have shown in the table structure, the column exists in the table and there are no other columns with the same name. However, I have other tables with the same column name and there is a foreign key constraint that refers to the employee table and the emp_id in that table. Also, this is an INSERT statement. So I am not sure what went wrong here. If someone can tell me what is the error here, that would be helpful.


问:
Bulk Data from Europe PMC annotation api
i have a pmc.txt file which contains atleast 20k pmc ids, and the api will only take i think 1000 request each time. i have written the code for one id, but i'm not able to do for the whole file, below is my main code. Please help.</p>
```<code>if __name__ == '__main__':
URL = 'https://www.ebi.ac.uk/europepmc/annotations_api/annotationsByArticleIds'

article_ids = ['PMC:4771370']

for article_id in article_ids:
  params = {
    'articleIds': article_id,
    'section': 'Abstract',
    'provider': 'Europe PMC',
    'format': 'JSON'
  }
json_data = requests.get(URL, params=params).content
r = json.loads(json_data)
df = json_to_dataframe(r)
print(df)
df.to_csv("data.csv")
</code>```

答:
you can read in the data from the file like so:

```<code>with open('pmc.txt', 'r') as file:
    article_ids = [item.replace('\n', '') for item in file]
</code>```

which you can do instead of article_ids = ['PMC:4771370']

Though you are going to have to save your files with a different name (you will have 20,000 files then or instead you have to append your json data to the dataframe before you make it a csv)

Therefore this would be something that you would do to separate the ids into chunks and use them in a single articleid parameter

```<code>if __name__ == '__main__':
    URL = 'https://www.ebi.ac.uk/europepmc/annotations_api/annotationsByArticleIds'

    with open('corpus_processing_input.txt', 'r') as file:
        article_ids = [item.replace('\n', '') for item in file]

    # API only allows 1-8 ids sent at a time
    chunks = [article_ids[x:x+8] for x in range(0, len(article_ids), 8)]

    for count, article_ids in enumerate(chunks):
        params = {
            'articleIds': ','.join(article_ids),
            'section': 'Abstract',
            'provider': 'Europe PMC',
            'format': 'JSON'
        }
        json_data = requests.get(URL, params=params).content
        r = json.loads(json_data)
        df = json_to_dataframe(r)
        print(df)
        df.to_csv(f"data{count}.csv")

</code>```

Body:
i have a pmc.txt file which contains atleast 20k pmc ids, and the api will only take i think 1000 request each time. i have written the code for one id, but i'm not able to do for the whole file, below is my main code. Please help.

if __name__ == '__main__':
URL = 'https://www.ebi.ac.uk/europepmc/annotations_api/annotationsByArticleIds'

article_ids = ['PMC:4771370']

for article_id in article_ids:
  params = {
    'articleIds': article_id,
    'section': 'Abstract',
    'provider': 'Europe PMC',
    'format': 'JSON'
  }
json_data = requests.get(URL, params=params).content
r = json.loads(json_data)
df = json_to_dataframe(r)
print(df)
df.to_csv("data.csv")

Body:
you can read in the data from the file like so:

with open('pmc.txt', 'r') as file:
    article_ids = [item.replace('\n', '') for item in file]

which you can do instead of article_ids = ['PMC:4771370']

Though you are going to have to save your files with a different name (you will have 20,000 files then or instead you have to append your json data to the dataframe before you make it a csv)

Therefore this would be something that you would do to separate the ids into chunks and use them in a single articleid parameter

if __name__ == '__main__':
    URL = 'https://www.ebi.ac.uk/europepmc/annotations_api/annotationsByArticleIds'

    with open('corpus_processing_input.txt', 'r') as file:
        article_ids = [item.replace('\n', '') for item in file]

    # API only allows 1-8 ids sent at a time
    chunks = [article_ids[x:x+8] for x in range(0, len(article_ids), 8)]

    for count, article_ids in enumerate(chunks):
        params = {
            'articleIds': ','.join(article_ids),
            'section': 'Abstract',
            'provider': 'Europe PMC',
            'format': 'JSON'
        }
        json_data = requests.get(URL, params=params).content
        r = json.loads(json_data)
        df = json_to_dataframe(r)
        print(df)
        df.to_csv(f"data{count}.csv")


问:
Using Null variables in conditions in T-Sql
Is there a way to use a variable in the following manner?

```<code>DECLARE @ID int;
SET @ID = NULL;
SELECT *
FROM Market market
WHERE market.ID IS @ID
</code>```

Or is there another way to do this?

Thanks.

答:
Is there a particular reason that you are trying to stick null into a variable rather than just doing:</p>

```<code>SELECT *
FROM Market market
WHERE market.ID IS NULL
</code>```

Body:
Is there a way to use a variable in the following manner?

DECLARE @ID int;
SET @ID = NULL;
SELECT *
FROM Market market
WHERE market.ID IS @ID

Or is there another way to do this?

Thanks.

Body:
Is there a particular reason that you are trying to stick null into a variable rather than just doing:

SELECT *
FROM Market market
WHERE market.ID IS NULL


问:
What is Chart Colors in VSCode?
While making theme I saw this

```<code>    "charts.blue": "#ff0000",
    "charts.green": "#ff0000",
    "charts.orange": "#ff0000",
    "charts.purple": "#ff0000",
    "charts.red": "#ff0000",
    "charts.yellow": "#ff0000",
    "charts.lines": "#ff0000",
    "charts.foreground": "#ff0000"
</code>```

What does it Do?
&
What is charts in VSCode

答:
The first column is the defined name of the specific color for the chart item, the second column is the actual value of the color.

see the official docs.

Title: What is Chart Colors in VSCode?

Body:
While making theme I saw this

    "charts.blue": "#ff0000",
    "charts.green": "#ff0000",
    "charts.orange": "#ff0000",
    "charts.purple": "#ff0000",
    "charts.red": "#ff0000",
    "charts.yellow": "#ff0000",
    "charts.lines": "#ff0000",
    "charts.foreground": "#ff0000"

What does it Do?
&
What is charts in VSCode


问:
How to activate IHubContext service in ASP.NET Core MVC?
I have used signalR in my project. There is a hub that users can send notifications to each other. In login part of the project, I want to send notification to the admin when a user is signed in. I need to send this notification from controller, not client side. I tried:

```<code>//after dependancy injection
_hubContext.Clients.User(***).SendAsync("LoginIn", "text1", "text2", "text3");
</code>```

The following error is shown:

InvalidOperationException: Unable to resolve service for type
'Microsoft.AspNetCore.SignalR.IHubContext' while attempting to
activate 'CustomizedIdentity.Controllers.HomeController'.

I tried to register IHubContext in Services:

```<code>    builder.Services.AddScoped<IHubContext<MessageHub>>();
</code>```

But it does not work.

Update:

```<code>private readonly UserManager<AppUser> _userManager;
private readonly SignInManager<AppUser> _signInManager;
private readonly IUserRepository _userRepository;
private readonly INotificationRepository _notificationRepository;
private readonly IHubContext _hubContext;

public HomeController(
    UserManager<AppUser> userManager,
    SignInManager<AppUser> signInManager,
    IUserRepository userRepository,
    INotificationRepository notificationRepository,
    IHubContext hubContext
)
{
    _userManager = userManager;
    _signInManager = signInManager;
    _userRepository = userRepository;
    _notificationRepository = notificationRepository;
    _hubContext = hubContext;
}
</code>```

答: @Dai solved this problem. I must change IHubContext hubContext to IHubContext<MessageHub> msgHubContext

Body:
I have used signalR in my project. There is a hub that users can send notifications to each other. In login part of the project, I want to send notification to the admin when a user is signed in. I need to send this notification from controller, not client side. I tried:

//after dependancy injection
_hubContext.Clients.User(***).SendAsync("LoginIn", "text1", "text2", "text3");

The following error is shown:

InvalidOperationException: Unable to resolve service for type
'Microsoft.AspNetCore.SignalR.IHubContext' while attempting to
activate 'CustomizedIdentity.Controllers.HomeController'.

I tried to register IHubContext in Services:

    builder.Services.AddScoped<IHubContext<MessageHub>>();

But it does not work.

Update:

private readonly UserManager<AppUser> _userManager;
private readonly SignInManager<AppUser> _signInManager;
private readonly IUserRepository _userRepository;
private readonly INotificationRepository _notificationRepository;
private readonly IHubContext _hubContext;

public HomeController(
    UserManager<AppUser> userManager,
    SignInManager<AppUser> signInManager,
    IUserRepository userRepository,
    INotificationRepository notificationRepository,
    IHubContext hubContext
)
{
    _userManager = userManager;
    _signInManager = signInManager;
    _userRepository = userRepository;
    _notificationRepository = notificationRepository;
    _hubContext = hubContext;
}


问:
DbFunctions, is a type which is not valid in the given context
I tried to use the SqlServerDbFunctionsExtensions.DateDiffDay but DbFunctions _ says

CS0119: DbFunctions is a type which is not valid in the given context

```<code>SqlServerDbFunctionsExtensions.DateDiffDay(this Microsoft.EntityFrameworkCore.DbFunctions _, DateTime.Now, i.Invoicedate) + i.ProductNav.GracePeriod.GetValueOrDefault()
</code>```

I also tried the following but i get the same error

```<code>SqlServerDbFunctionsExtensions.DateDiffDay(DbFunctions _, DateTime.Now, i.Invoicedate) + i.ProductNav.GracePeriod.GetValueOrDefault()
</code>```

答:
The other answers are what you would ordinarily do but I think it worth pointing out that where they imply an extension method is some sort of magical thing that can only be called in a certain way, that isn't really true

Nothing stops you calling an extension method as if it were any normal static method; you just have to provide some instance to call it on as the first argument

Here is an extension method:

```<code>public static string NoL(this string x){
  return x.Replace("l","");
}
</code>```

Note the this on the first argument, making it an extension. You can either call it "on a string" or pass it a string:

```<code>var hw = "Hello World";

var heoword1 = hw.NoL());
var heoword2 = Ext.NoL(hw);
</code>```

In probably all cases you'd do the first form, because static method extensions exist to make the code more compact and readable, but there's technically nothing wrong with your attempt to call the method you did, you just didn't provide a valid first parameter. This would work:

```<code>SqlServerDbFunctionsExtensions.DateDiffDay(EF.Functions, DateTime.Now, i.Invoicedate)
</code>```

EF.Functions being the instance you would ordinarily just call DateDiff on:

```<code>EF.Functions.DateDiff(DateTime.Now, i.Invoicedate)
</code>```

--

So when would you use an extension method like a normal one? Probably not many places, but it may make sense if its signature exactly matches some method that accepts a delegate.

For example, suppose we had an extension method:

```<code>public static class Exts{
  public static string CombineStringAndInt(this string s, int i){
    return s+i;
  }
}
</code>```

It takes a string and an int. LINQ's Select has an overload that accepts a delegate that has arguments of string,int. You could call the method above like this:

```<code>var arr = new[]{"a","b"};

arr.Select((s, i) => s.CombineStringAndInt(i));
</code>```

But nothing prevents you calling it like this:

```<code>arr.Select(Exts.CombineStringAndInt);
</code>```

Because it is a method that accepts a string and int, which is what is demanded by the Select overload that accepts a delegate for a list item and the index it's at

There's nothing magical or weird about the extension method that prevents it being caled like this, or limiting it to only being used in a instance.ExtensionMethod(arg2,arg3..) style

Body:
I tried to use the SqlServerDbFunctionsExtensions.DateDiffDay but DbFunctions _ says

CS0119: DbFunctions is a type which is not valid in the given context

SqlServerDbFunctionsExtensions.DateDiffDay(this Microsoft.EntityFrameworkCore.DbFunctions _, DateTime.Now, i.Invoicedate) + i.ProductNav.GracePeriod.GetValueOrDefault()

I also tried the following but i get the same error

SqlServerDbFunctionsExtensions.DateDiffDay(DbFunctions _, DateTime.Now, i.Invoicedate) + i.ProductNav.GracePeriod.GetValueOrDefault()

Body:
The other answers are what you would ordinarily do but I think it worth pointing out that where they imply an extension method is some sort of magical thing that can only be called in a certain way, that isn't really true

Nothing stops you calling an extension method as if it were any normal static method; you just have to provide some instance to call it on as the first argument

Here is an extension method:

public static string NoL(this string x){
  return x.Replace("l","");
}

Note the this on the first argument, making it an extension. You can either call it "on a string" or pass it a string:

var hw = "Hello World";

var heoword1 = hw.NoL());
var heoword2 = Ext.NoL(hw);

In probably all cases you'd do the first form, because static method extensions exist to make the code more compact and readable, but there's technically nothing wrong with your attempt to call the method you did, you just didn't provide a valid first parameter. This would work:

SqlServerDbFunctionsExtensions.DateDiffDay(EF.Functions, DateTime.Now, i.Invoicedate)

EF.Functions being the instance you would ordinarily just call DateDiff on:

EF.Functions.DateDiff(DateTime.Now, i.Invoicedate)

--

So when would you use an extension method like a normal one? Probably not many places, but it may make sense if its signature exactly matches some method that accepts a delegate.

For example, suppose we had an extension method:

public static class Exts{
  public static string CombineStringAndInt(this string s, int i){
    return s+i;
  }
}

It takes a string and an int. LINQ's Select has an overload that accepts a delegate that has arguments of string,int. You could call the method above like this:

var arr = new[]{"a","b"};

arr.Select((s, i) => s.CombineStringAndInt(i));

But nothing prevents you calling it like this:

arr.Select(Exts.CombineStringAndInt);

Because it is a method that accepts a string and int, which is what is demanded by the Select overload that accepts a delegate for a list item and the index it's at

There's nothing magical or weird about the extension method that prevents it being caled like this, or limiting it to only being used in a instance.ExtensionMethod(arg2,arg3..) style