问:
Find the kth smallest element in an unsorted array of non-negative integers
Not allowed to modify the array ( The array is read only ).
Using constant extra space is allowed.

ex:
A : [2 1 4 3 2]
k : 3

answer : 2

I did it below way. The answer is correct but need to be more memory efficient.

```<code>void insert_sorted(vector<int> &B, int a,int k)
{
    for(int i=0;i<k;i++)
    {
        if(B[i]>=a)
        {
            for(int j=k-1;j>i;j--)
                B[j]=B[j-1];
            B[i]=a;
            return;
        }
    }
}

int Solution::kthsmallest(const vector<int> &A, int k) {

    vector <int> B;
    for(int i=0;i<k;i++)
    {
        B.push_back(INT_MAX);
    }
    int l=A.size();

    for(int i=0;i<l;i++)
    {
        if(B[k-1]>=A[i])
            insert_sorted(B,A[i],k);
    }

    return B[k-1];
}
</code>```

答:
One possible solution is binary search.

Let A be the input array; we want to find a number b such that exactly k items in A are smaller than b.

Obviously, b must be inside the range [0, max(A)].
And we do binary search starting with this range.

Suppose we are searching within range [lo, hi].
Let c = (lo + hi)/2 which is the middle pivot.
There are three cases:

number of items in A less than c are less than k.
In this case the number we search for should be larger than c, so it should be in range (c, hi]

number of items in A less than c are larger than k.
Similarly, the number we search for is in range [lo, c)

number of items in A less than c equals to k.
In this case, the answer is the minimum element in A that is greater than or equals to c. This can be find by doing a linear search in A again

The complexity is O(n log m), where m is the max element in A.

```<code>/* assume k is 0 based, i.e. 0 <= k < n */
int kth_element(const vector<int> &A, int k){
    int lo = 0, hi = *max_element(A.begin(), A.end());
    while (lo <= hi){
        int mid = (lo + hi) / 2;
        int rank_lo = count_if(A.begin(), A.end(), [=](int i){ return i < mid;});
        int rank_hi = count_if(A.begin(), A.end(), [=](int i){ return i <= mid;});

        if (rank_lo <= k && k < rank_hi)
            return mid;

        if (k >= rank_hi)
            lo = mid + 1;
        else
            hi = mid - 1;
    }
}
</code>```

Although it's not the answer to this particular problem (as it requires a modifiable collection), there is a function called std::nth_element, which rearranges the elements so that the kth element is at position k, and all elements at positions less than k are smaller than or equal to the kth element, where k is a input parameter.

Body:
Not allowed to modify the array ( The array is read only ).
Using constant extra space is allowed.

ex:
A : [2 1 4 3 2]
k : 3

answer : 2

I did it below way. The answer is correct but need to be more memory efficient.

void insert_sorted(vector<int> &B, int a,int k)
{
    for(int i=0;i<k;i++)
    {
        if(B[i]>=a)
        {
            for(int j=k-1;j>i;j--)
                B[j]=B[j-1];
            B[i]=a;
            return;
        }
    }
}

int Solution::kthsmallest(const vector<int> &A, int k) {

    vector <int> B;
    for(int i=0;i<k;i++)
    {
        B.push_back(INT_MAX);
    }
    int l=A.size();

    for(int i=0;i<l;i++)
    {
        if(B[k-1]>=A[i])
            insert_sorted(B,A[i],k);
    }

    return B[k-1];
}

Body:
One possible solution is binary search.

Let A be the input array; we want to find a number b such that exactly k items in A are smaller than b.

Obviously, b must be inside the range [0, max(A)].
And we do binary search starting with this range.

Suppose we are searching within range [lo, hi].
Let c = (lo + hi)/2 which is the middle pivot.
There are three cases:

number of items in A less than c are less than k.
In this case the number we search for should be larger than c, so it should be in range (c, hi]

number of items in A less than c are larger than k.
Similarly, the number we search for is in range [lo, c)

number of items in A less than c equals to k.
In this case, the answer is the minimum element in A that is greater than or equals to c. This can be find by doing a linear search in A again

The complexity is O(n log m), where m is the max element in A.

/* assume k is 0 based, i.e. 0 <= k < n */
int kth_element(const vector<int> &A, int k){
    int lo = 0, hi = *max_element(A.begin(), A.end());
    while (lo <= hi){
        int mid = (lo + hi) / 2;
        int rank_lo = count_if(A.begin(), A.end(), [=](int i){ return i < mid;});
        int rank_hi = count_if(A.begin(), A.end(), [=](int i){ return i <= mid;});

        if (rank_lo <= k && k < rank_hi)
            return mid;

        if (k >= rank_hi)
            lo = mid + 1;
        else
            hi = mid - 1;
    }
}

Although it's not the answer to this particular problem (as it requires a modifiable collection), there is a function called std::nth_element, which rearranges the elements so that the kth element is at position k, and all elements at positions less than k are smaller than or equal to the kth element, where k is a input parameter.


来源: stackexchange

问:
Import xlsx file into Python using xlrd
Trying to import a .xlsx file into Python. I've google and google. I had this code written and working for .csv but it needs to be for .xlsx files. So I basically pieced together and did a hail mary in hopes that it would work. Any and all help is greatly appreciated!

** Added my whole code and a snippet of the data file. I need it to import .xlsx and execute the data.

```<code>import xlrd

workbook = xlrd.open_workbook('GAT_US_PartReview_2017-06-23.xlsx')

worksheet = workbook.get_sheet(0, update_pos=True)

header = []
report_card = []

# Count Stmnts for Required Data
valid_count = 0
app_req_count = 0
intr_req_count = 0
oe_intr_req_count = 0
part_img_req_count = 0
upc_req_count = 0
unspsc_req_count = 0
msds_req_count = 0

# Count Stmts for Missing Data
missing_app_count = 0
missing_intr_count = 0
missing_oe_intr_count = 0
missing_mpcc_count = 0
missing_attr_values_count = 0
missing_part_img_count = 0
missing_upc_count = 0
missing_warr_text_count = 0
missing_warr_pdf_count = 0
missing_unspsc_count = 0
missing_msds_count = 0

for row_num, row in enumerate(worksheet):
    if row_num <= 4:
        #  print(row)  # Print out the header
        header.append([row[0], row[2]])
    else:
        hq_line, part_no, part_class, appl_req, appl_count, intr_req,
        intr_count, oe_intr_req, has_oe_intr, has_attr_editor,
        has_attr_values, part_img_req, has_part_img, has_mpcc, warr_req,
        has_warr_txt, has_warr_pdf, msds_req, has_msds, upc_req, has_upc,
        has_unspsc, attr_count, attrval_count, valid_part = row

    if valid_part == 'YES':
        valid_count += 1

        # Required Parts Count
        if appl_req == 'YES':
            app_req_count += 1
        if intr_req == 'YES':
            intr_req_count += 1
        if oe_intr_req == 'YES':
            oe_intr_req_count += 1
        if part_img_req == 'YES':
            part_img_req_count += 1
        if upc_req == 'YES':
            upc_req_count += 1
        if msds_req == 'YES':
            msds_req_count += 1

        # Missing Data Counts
        if appl_req == 'YES' and appl_count == '0':
            missing_app_count += 1
        if intr_req == 'YES' and intr_count == '0':
            missing_intr_count += 1
        if oe_intr_req == 'YES' and has_oe_intr == '0':
             missing_oe_intr_count += 1
        if has_mpcc == 'NO':
             missing_mpcc_count += 1
        if has_attr_values == 'NO':
            missing_attr_values_count += 1
        if has_part_img == 'NO':
            missing_part_img_count += 1
        if upc_req == 'YES' and has_upc == '0':
            missing_upc_count += 1
        if warr_req == 'YES' and has_warr_txt == 'NO':
            missing_warr_text_count += 1
        if warr_req == 'YES' and has_warr_pdf == 'NO':
            missing_warr_pdf_count += 1
        if has_unspsc == 'NO':
            missing_unspsc_count += 1
        if msds_req == 'YES' and has_msds == 'NO':
            missing_msds_count += 1

# Statements for Required Counts
valid_parts = ('Number of Valid Parts: ', '{:,}'.format(valid_count))
application_required = ('Application Records Required: ',
'{:,}'.format(app_req_count))
interchange_required = ('Interchange Records Required: ',
'{:,}'.format(intr_req_count))
oe_interchange_required = ('OE Interchange Records Required: ',
'{:,}'.format(oe_intr_req_count))
mpcc = ('MPCC Required: ', '{:,}'.format(valid_count))  # Every valid part
requires a MPCC
attributes = ('Attributes Required: ', '{:,}'.format(valid_count))  # Every
valid part requires attributes
image_required = ('Image Required: ', '{:,}'.format(part_img_req_count))
upc = ('UPC Requited: ', '{:,}'.format(upc_req_count))
warranties = ('Warranty Text/PDF Required: ', '{:,}'.format(valid_count))  #
Require warranty text/pdf on all parts
unspsc = ('UNSPSC Code Required: ', '{:,}'.format(valid_count))  # Require
UNSPSC Codes for all parts
msds = ('MSDS Required: ', '{:,}'.format(msds_req_count))

# Statements for Missing Counts
missing_applications = ('Missing Applications: ',
'{:,}'.format(missing_app_count))
missing_interchange = ('Missing Interchange: ',
'{:,}'.format(missing_intr_count))
missing_oe_interchange = ('Missing OE Interchange: ',
'{:,}'.format(missing_oe_intr_count))
missing_mpcc = ('Missing MPCC: ', '{:,}'.format(missing_mpcc_count))
missing_attributes = ('Missing Attributes: ',
'{:,}'.format(missing_attr_values_count))
missing_image = ('Missing Image: ', '{:,}'.format(missing_part_img_count))
missing_UPC = ('Missing UPC: ', '{:,}'.format(missing_upc_count))
missing_warranty_text = ('Missing Warranty Text: ',
'{:,}'.format(missing_warr_text_count))
missing_warranty_pdf = ('Missing Warranty PDF: ',
'{:,}'.format(missing_warr_pdf_count))
missing_unspsc = ('Missing UNSPSC Code: ',
'{:,}'.format(missing_unspsc_count))
missing_msds = ('Missing MSDS: ', '{:,}'.format(missing_msds_count))

# CSV Output
report_card.append(valid_parts)
report_card.append(application_required)
report_card.append(interchange_required)
report_card.append(oe_interchange_required)
report_card.append(mpcc)
report_card.append(attributes)
report_card.append(image_required)
report_card.append(upc)
report_card.append(warranties)
report_card.append(unspsc)
report_card.append(msds)
report_card.append(missing_applications)
report_card.append(missing_interchange)
report_card.append(missing_oe_interchange)
report_card.append(missing_mpcc)
report_card.append(missing_attributes)
report_card.append(missing_image)
report_card.append(missing_UPC)
report_card.append(missing_warranty_text)
report_card.append(missing_warranty_pdf)
report_card.append(missing_unspsc)
report_card.append(missing_msds)

for row in header:
    print(row)

for x in report_card:
    print(x)

with open('Report_Card.csv', 'w', newline='') as f:
    writer = csv.writer(f)
    writer.writerows(header)
    writer.writerows(report_card)
</code>```

CSV file:

```<code>Supplier Line:,,Gates Rubber - Denver (GAT),,,,,,,,,,,,,,,,,,,,,,
Summary:,,Parts HQ Abbr,,,,,,,,,,,,,,,,,,,,,,
ACCT No:,,40013586,,,,,,,,,,,,,,,,,,,,,,
RecCount:,,10221,,,,,,,,,,,,,,,,,,,,,,
Applicable Date:,,"June 14, 2017 (Wednesday)",,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,
HQ Line,Part No,Part Class,Appl Req,Appl Count ,Intr Req,Intr Count ,OE Intr Req,Has OE Intr,Has Attr Editor, Has Attr Values,Part IMG Req,Has Part IMG,Has MPCC,Warr Req,Has Warr TXT,Has Warr PDF,MSDS Req,Has MSDS,UPC Req,Has UPC,Has UNSPSC,Attr Count ,AttrVal Count ,Valid Part
GAT,'27210',S,NO,0,YES,1,YES,NO,YES,YES,YES,YES,YES,YES,YES,YES,NO,NO,YES,YES,YES,30,13,YES
GAT,'27211',O,NO,0,YES,1,YES,NO,YES,YES,YES,YES,YES,YES,YES,YES,NO,NO,YES,YES,YES,30,14,YES
GAT,'27212',S,NO,0,YES,1,YES,NO,YES,YES,YES,YES,YES,YES,YES,YES,NO,NO,YES,YES,YES,30,13,YES
GAT,'27213',S,NO,0,YES,1,YES,NO,YES,YES,YES,YES,YES,YES,YES,YES,NO,NO,YES,YES,YES,30,13,YES
GAT,'27220',S,NO,0,YES,2,YES,NO,YES,YES,YES,YES,YES,YES,YES,YES,NO,NO,YES,YES,YES,35,20,YES
GAT,'27221',S,NO,0,YES,2,YES,NO,YES,YES,YES,YES,YES,YES,YES,YES,NO,NO,YES,YES,YES,35,20,YES
</code>```

答:
Your code for accessing the xlrd library needed a few fixes as follows:

```<code>import xlrd

workbook = xlrd.open_workbook('GAT_US_PartReview_2017-06-23.xlsx')
worksheet = workbook.sheet_by_index(0)

header = []
report_card = []

# Count Stmnts for Required Data
valid_count = 0
app_req_count = 0
intr_req_count = 0
oe_intr_req_count = 0
part_img_req_count = 0
upc_req_count = 0
unspsc_req_count = 0
msds_req_count = 0

# Count Stmts for Missing Data
missing_app_count = 0
missing_intr_count = 0
missing_oe_intr_count = 0
missing_mpcc_count = 0
missing_attr_values_count = 0
missing_part_img_count = 0
missing_upc_count = 0
missing_warr_text_count = 0
missing_warr_pdf_count = 0
missing_unspsc_count = 0
missing_msds_count = 0

for row_num in range(worksheet.nrows):
    row = worksheet.row_values(row_num)

    if row_num <= 4:
        #  print(row)  # Print out the header
        header.append([row[0], row[2]])
    else:
        hq_line, part_no, part_class, appl_req, appl_count, intr_req, \
        intr_count, oe_intr_req, has_oe_intr, has_attr_editor, \
        has_attr_values, part_img_req, has_part_img, has_mpcc, warr_req, \
        has_warr_txt, has_warr_pdf, msds_req, has_msds, upc_req, has_upc, \
        has_unspsc, attr_count, attrval_count, valid_part = row

        if valid_part == 'YES':
            valid_count += 1

            # Required Parts Count
            if appl_req == 'YES':
                app_req_count += 1
            if intr_req == 'YES':
                intr_req_count += 1
            if oe_intr_req == 'YES':
                oe_intr_req_count += 1
            if part_img_req == 'YES':
                part_img_req_count += 1
            if upc_req == 'YES':
                upc_req_count += 1
            if msds_req == 'YES':
                msds_req_count += 1

            # Missing Data Counts
            if appl_req == 'YES' and appl_count == '0':
                missing_app_count += 1
            if intr_req == 'YES' and intr_count == '0':
                missing_intr_count += 1
            if oe_intr_req == 'YES' and has_oe_intr == '0':
                 missing_oe_intr_count += 1
            if has_mpcc == 'NO':
                 missing_mpcc_count += 1
            if has_attr_values == 'NO':
                missing_attr_values_count += 1
            if has_part_img == 'NO':
                missing_part_img_count += 1
            if upc_req == 'YES' and has_upc == '0':
                missing_upc_count += 1
            if warr_req == 'YES' and has_warr_txt == 'NO':
                missing_warr_text_count += 1
            if warr_req == 'YES' and has_warr_pdf == 'NO':
                missing_warr_pdf_count += 1
            if has_unspsc == 'NO':
                missing_unspsc_count += 1
            if msds_req == 'YES' and has_msds == 'NO':
                missing_msds_count += 1

# Statements for Required Counts
valid_parts = ('Number of Valid Parts: ', '{:,}'.format(valid_count))
application_required = ('Application Records Required: ', '{:,}'.format(app_req_count))
interchange_required = ('Interchange Records Required: ', '{:,}'.format(intr_req_count))
oe_interchange_required = ('OE Interchange Records Required: ', '{:,}'.format(oe_intr_req_count))
mpcc = ('MPCC Required: ', '{:,}'.format(valid_count))  # Every valid part requires a MPCC
attributes = ('Attributes Required: ', '{:,}'.format(valid_count))  # Every valid part requires attributes
image_required = ('Image Required: ', '{:,}'.format(part_img_req_count))
upc = ('UPC Requited: ', '{:,}'.format(upc_req_count))
warranties = ('Warranty Text/PDF Required: ', '{:,}'.format(valid_count))  # Require warranty text/pdf on all parts
unspsc = ('UNSPSC Code Required: ', '{:,}'.format(valid_count))  # Require UNSPSC Codes for all parts
msds = ('MSDS Required: ', '{:,}'.format(msds_req_count))

# Statements for Missing Counts
missing_applications = ('Missing Applications: ', '{:,}'.format(missing_app_count))
missing_interchange = ('Missing Interchange: ', '{:,}'.format(missing_intr_count))
missing_oe_interchange = ('Missing OE Interchange: ', '{:,}'.format(missing_oe_intr_count))
missing_mpcc = ('Missing MPCC: ', '{:,}'.format(missing_mpcc_count))
missing_attributes = ('Missing Attributes: ', '{:,}'.format(missing_attr_values_count))
missing_image = ('Missing Image: ', '{:,}'.format(missing_part_img_count))
missing_UPC = ('Missing UPC: ', '{:,}'.format(missing_upc_count))
missing_warranty_text = ('Missing Warranty Text: ', '{:,}'.format(missing_warr_text_count))
missing_warranty_pdf = ('Missing Warranty PDF: ', '{:,}'.format(missing_warr_pdf_count))
missing_unspsc = ('Missing UNSPSC Code: ', '{:,}'.format(missing_unspsc_count))
missing_msds = ('Missing MSDS: ', '{:,}'.format(missing_msds_count))

# CSV Output
report_card.append(valid_parts)
report_card.append(application_required)
report_card.append(interchange_required)
report_card.append(oe_interchange_required)
report_card.append(mpcc)
report_card.append(attributes)
report_card.append(image_required)
report_card.append(upc)
report_card.append(warranties)
report_card.append(unspsc)
report_card.append(msds)
report_card.append(missing_applications)
report_card.append(missing_interchange)
report_card.append(missing_oe_interchange)
report_card.append(missing_mpcc)
report_card.append(missing_attributes)
report_card.append(missing_image)
report_card.append(missing_UPC)
report_card.append(missing_warranty_text)
report_card.append(missing_warranty_pdf)
report_card.append(missing_unspsc)
report_card.append(missing_msds)

for row in header:
    print(row)

for x in report_card:
    print(x)

with open('Report_Card.csv', 'w', newline='') as f:
    writer = csv.writer(f)
    writer.writerows(header)
    writer.writerows(report_card)
</code>```

For the data you have provided, this gives you a Report_Card.csv file as follows:

Supplier Line:  Gates Rubber - Denver (GAT)
Summary:    Parts HQ Abbr
ACCT No:    40013586
RecCount:   10221
Applicable Date:    June 14, 2017 (Wednesday)
Number of Valid Parts:  6
Application Records Required:   0
Interchange Records Required:   6
OE Interchange Records Required:    6
MPCC Required:  6
Attributes Required:    6
Image Required:     6
UPC Requited:   6
Warranty Text/PDF Required:     6
UNSPSC Code Required:   6
MSDS Required:  0
Missing Applications:   0
Missing Interchange:    0
Missing OE Interchange:     0
Missing MPCC:   0
Missing Attributes:     0
Missing Image:  0
Missing UPC:    0
Missing Warranty Text:  0
Missing Warranty PDF:   0
Missing UNSPSC Code:    0
Missing MSDS:   0
```

Your code could certainly do with a rethink. Rather than having so many variables, i.e. one per column, think about using something like a Python Counter() which works like a dictionary. Working with lists or dictionaries would make your code much simpler.

ContentLicense: CC BY-SA 3.0

Title: Import xlsx file into Python using xlrd

Body:
Trying to import a .xlsx file into Python. I've google and google. I had this code written and working for .csv but it needs to be for .xlsx files. So I basically pieced together and did a hail mary in hopes that it would work. Any and all help is greatly appreciated!

** Added my whole code and a snippet of the data file. I need it to import .xlsx and execute the data.

import xlrd

workbook = xlrd.open_workbook('GAT_US_PartReview_2017-06-23.xlsx')

worksheet = workbook.get_sheet(0, update_pos=True)

header = []
report_card = []

# Count Stmnts for Required Data
valid_count = 0
app_req_count = 0
intr_req_count = 0
oe_intr_req_count = 0
part_img_req_count = 0
upc_req_count = 0
unspsc_req_count = 0
msds_req_count = 0

# Count Stmts for Missing Data
missing_app_count = 0
missing_intr_count = 0
missing_oe_intr_count = 0
missing_mpcc_count = 0
missing_attr_values_count = 0
missing_part_img_count = 0
missing_upc_count = 0
missing_warr_text_count = 0
missing_warr_pdf_count = 0
missing_unspsc_count = 0
missing_msds_count = 0

for row_num, row in enumerate(worksheet):
    if row_num <= 4:
        #  print(row)  # Print out the header
        header.append([row[0], row[2]])
    else:
        hq_line, part_no, part_class, appl_req, appl_count, intr_req,
        intr_count, oe_intr_req, has_oe_intr, has_attr_editor,
        has_attr_values, part_img_req, has_part_img, has_mpcc, warr_req,
        has_warr_txt, has_warr_pdf, msds_req, has_msds, upc_req, has_upc,
        has_unspsc, attr_count, attrval_count, valid_part = row

    if valid_part == 'YES':
        valid_count += 1

        # Required Parts Count
        if appl_req == 'YES':
            app_req_count += 1
        if intr_req == 'YES':
            intr_req_count += 1
        if oe_intr_req == 'YES':
            oe_intr_req_count += 1
        if part_img_req == 'YES':
            part_img_req_count += 1
        if upc_req == 'YES':
            upc_req_count += 1
        if msds_req == 'YES':
            msds_req_count += 1

        # Missing Data Counts
        if appl_req == 'YES' and appl_count == '0':
            missing_app_count += 1
        if intr_req == 'YES' and intr_count == '0':
            missing_intr_count += 1
        if oe_intr_req == 'YES' and has_oe_intr == '0':
             missing_oe_intr_count += 1
        if has_mpcc == 'NO':
             missing_mpcc_count += 1
        if has_attr_values == 'NO':
            missing_attr_values_count += 1
        if has_part_img == 'NO':
            missing_part_img_count += 1
        if upc_req == 'YES' and has_upc == '0':
            missing_upc_count += 1
        if warr_req == 'YES' and has_warr_txt == 'NO':
            missing_warr_text_count += 1
        if warr_req == 'YES' and has_warr_pdf == 'NO':
            missing_warr_pdf_count += 1
        if has_unspsc == 'NO':
            missing_unspsc_count += 1
        if msds_req == 'YES' and has_msds == 'NO':
            missing_msds_count += 1

# Statements for Required Counts
valid_parts = ('Number of Valid Parts: ', '{:,}'.format(valid_count))
application_required = ('Application Records Required: ',
'{:,}'.format(app_req_count))
interchange_required = ('Interchange Records Required: ',
'{:,}'.format(intr_req_count))
oe_interchange_required = ('OE Interchange Records Required: ',
'{:,}'.format(oe_intr_req_count))
mpcc = ('MPCC Required: ', '{:,}'.format(valid_count))  # Every valid part
requires a MPCC
attributes = ('Attributes Required: ', '{:,}'.format(valid_count))  # Every
valid part requires attributes
image_required = ('Image Required: ', '{:,}'.format(part_img_req_count))
upc = ('UPC Requited: ', '{:,}'.format(upc_req_count))
warranties = ('Warranty Text/PDF Required: ', '{:,}'.format(valid_count))  #
Require warranty text/pdf on all parts
unspsc = ('UNSPSC Code Required: ', '{:,}'.format(valid_count))  # Require
UNSPSC Codes for all parts
msds = ('MSDS Required: ', '{:,}'.format(msds_req_count))

# Statements for Missing Counts
missing_applications = ('Missing Applications: ',
'{:,}'.format(missing_app_count))
missing_interchange = ('Missing Interchange: ',
'{:,}'.format(missing_intr_count))
missing_oe_interchange = ('Missing OE Interchange: ',
'{:,}'.format(missing_oe_intr_count))
missing_mpcc = ('Missing MPCC: ', '{:,}'.format(missing_mpcc_count))
missing_attributes = ('Missing Attributes: ',
'{:,}'.format(missing_attr_values_count))
missing_image = ('Missing Image: ', '{:,}'.format(missing_part_img_count))
missing_UPC = ('Missing UPC: ', '{:,}'.format(missing_upc_count))
missing_warranty_text = ('Missing Warranty Text: ',
'{:,}'.format(missing_warr_text_count))
missing_warranty_pdf = ('Missing Warranty PDF: ',
'{:,}'.format(missing_warr_pdf_count))
missing_unspsc = ('Missing UNSPSC Code: ',
'{:,}'.format(missing_unspsc_count))
missing_msds = ('Missing MSDS: ', '{:,}'.format(missing_msds_count))

# CSV Output
report_card.append(valid_parts)
report_card.append(application_required)
report_card.append(interchange_required)
report_card.append(oe_interchange_required)
report_card.append(mpcc)
report_card.append(attributes)
report_card.append(image_required)
report_card.append(upc)
report_card.append(warranties)
report_card.append(unspsc)
report_card.append(msds)
report_card.append(missing_applications)
report_card.append(missing_interchange)
report_card.append(missing_oe_interchange)
report_card.append(missing_mpcc)
report_card.append(missing_attributes)
report_card.append(missing_image)
report_card.append(missing_UPC)
report_card.append(missing_warranty_text)
report_card.append(missing_warranty_pdf)
report_card.append(missing_unspsc)
report_card.append(missing_msds)

for row in header:
    print(row)

for x in report_card:
    print(x)

with open('Report_Card.csv', 'w', newline='') as f:
    writer = csv.writer(f)
    writer.writerows(header)
    writer.writerows(report_card)

CSV file:

Supplier Line:,,Gates Rubber - Denver (GAT),,,,,,,,,,,,,,,,,,,,,,
Summary:,,Parts HQ Abbr,,,,,,,,,,,,,,,,,,,,,,
ACCT No:,,40013586,,,,,,,,,,,,,,,,,,,,,,
RecCount:,,10221,,,,,,,,,,,,,,,,,,,,,,
Applicable Date:,,"June 14, 2017 (Wednesday)",,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,
HQ Line,Part No,Part Class,Appl Req,Appl Count ,Intr Req,Intr Count ,OE Intr Req,Has OE Intr,Has Attr Editor, Has Attr Values,Part IMG Req,Has Part IMG,Has MPCC,Warr Req,Has Warr TXT,Has Warr PDF,MSDS Req,Has MSDS,UPC Req,Has UPC,Has UNSPSC,Attr Count ,AttrVal Count ,Valid Part
GAT,'27210',S,NO,0,YES,1,YES,NO,YES,YES,YES,YES,YES,YES,YES,YES,NO,NO,YES,YES,YES,30,13,YES
GAT,'27211',O,NO,0,YES,1,YES,NO,YES,YES,YES,YES,YES,YES,YES,YES,NO,NO,YES,YES,YES,30,14,YES
GAT,'27212',S,NO,0,YES,1,YES,NO,YES,YES,YES,YES,YES,YES,YES,YES,NO,NO,YES,YES,YES,30,13,YES
GAT,'27213',S,NO,0,YES,1,YES,NO,YES,YES,YES,YES,YES,YES,YES,YES,NO,NO,YES,YES,YES,30,13,YES
GAT,'27220',S,NO,0,YES,2,YES,NO,YES,YES,YES,YES,YES,YES,YES,YES,NO,NO,YES,YES,YES,35,20,YES
GAT,'27221',S,NO,0,YES,2,YES,NO,YES,YES,YES,YES,YES,YES,YES,YES,NO,NO,YES,YES,YES,35,20,YES

Body:
Your code for accessing the xlrd library needed a few fixes as follows:

import xlrd

workbook = xlrd.open_workbook('GAT_US_PartReview_2017-06-23.xlsx')
worksheet = workbook.sheet_by_index(0)

header = []
report_card = []

# Count Stmnts for Required Data
valid_count = 0
app_req_count = 0
intr_req_count = 0
oe_intr_req_count = 0
part_img_req_count = 0
upc_req_count = 0
unspsc_req_count = 0
msds_req_count = 0

# Count Stmts for Missing Data
missing_app_count = 0
missing_intr_count = 0
missing_oe_intr_count = 0
missing_mpcc_count = 0
missing_attr_values_count = 0
missing_part_img_count = 0
missing_upc_count = 0
missing_warr_text_count = 0
missing_warr_pdf_count = 0
missing_unspsc_count = 0
missing_msds_count = 0

for row_num in range(worksheet.nrows):
    row = worksheet.row_values(row_num)

    if row_num <= 4:
        #  print(row)  # Print out the header
        header.append([row[0], row[2]])
    else:
        hq_line, part_no, part_class, appl_req, appl_count, intr_req, \
        intr_count, oe_intr_req, has_oe_intr, has_attr_editor, \
        has_attr_values, part_img_req, has_part_img, has_mpcc, warr_req, \
        has_warr_txt, has_warr_pdf, msds_req, has_msds, upc_req, has_upc, \
        has_unspsc, attr_count, attrval_count, valid_part = row

        if valid_part == 'YES':
            valid_count += 1

            # Required Parts Count
            if appl_req == 'YES':
                app_req_count += 1
            if intr_req == 'YES':
                intr_req_count += 1
            if oe_intr_req == 'YES':
                oe_intr_req_count += 1
            if part_img_req == 'YES':
                part_img_req_count += 1
            if upc_req == 'YES':
                upc_req_count += 1
            if msds_req == 'YES':
                msds_req_count += 1

            # Missing Data Counts
            if appl_req == 'YES' and appl_count == '0':
                missing_app_count += 1
            if intr_req == 'YES' and intr_count == '0':
                missing_intr_count += 1
            if oe_intr_req == 'YES' and has_oe_intr == '0':
                 missing_oe_intr_count += 1
            if has_mpcc == 'NO':
                 missing_mpcc_count += 1
            if has_attr_values == 'NO':
                missing_attr_values_count += 1
            if has_part_img == 'NO':
                missing_part_img_count += 1
            if upc_req == 'YES' and has_upc == '0':
                missing_upc_count += 1
            if warr_req == 'YES' and has_warr_txt == 'NO':
                missing_warr_text_count += 1
            if warr_req == 'YES' and has_warr_pdf == 'NO':
                missing_warr_pdf_count += 1
            if has_unspsc == 'NO':
                missing_unspsc_count += 1
            if msds_req == 'YES' and has_msds == 'NO':
                missing_msds_count += 1

# Statements for Required Counts
valid_parts = ('Number of Valid Parts: ', '{:,}'.format(valid_count))
application_required = ('Application Records Required: ', '{:,}'.format(app_req_count))
interchange_required = ('Interchange Records Required: ', '{:,}'.format(intr_req_count))
oe_interchange_required = ('OE Interchange Records Required: ', '{:,}'.format(oe_intr_req_count))
mpcc = ('MPCC Required: ', '{:,}'.format(valid_count))  # Every valid part requires a MPCC
attributes = ('Attributes Required: ', '{:,}'.format(valid_count))  # Every valid part requires attributes
image_required = ('Image Required: ', '{:,}'.format(part_img_req_count))
upc = ('UPC Requited: ', '{:,}'.format(upc_req_count))
warranties = ('Warranty Text/PDF Required: ', '{:,}'.format(valid_count))  # Require warranty text/pdf on all parts
unspsc = ('UNSPSC Code Required: ', '{:,}'.format(valid_count))  # Require UNSPSC Codes for all parts
msds = ('MSDS Required: ', '{:,}'.format(msds_req_count))

# Statements for Missing Counts
missing_applications = ('Missing Applications: ', '{:,}'.format(missing_app_count))
missing_interchange = ('Missing Interchange: ', '{:,}'.format(missing_intr_count))
missing_oe_interchange = ('Missing OE Interchange: ', '{:,}'.format(missing_oe_intr_count))
missing_mpcc = ('Missing MPCC: ', '{:,}'.format(missing_mpcc_count))
missing_attributes = ('Missing Attributes: ', '{:,}'.format(missing_attr_values_count))
missing_image = ('Missing Image: ', '{:,}'.format(missing_part_img_count))
missing_UPC = ('Missing UPC: ', '{:,}'.format(missing_upc_count))
missing_warranty_text = ('Missing Warranty Text: ', '{:,}'.format(missing_warr_text_count))
missing_warranty_pdf = ('Missing Warranty PDF: ', '{:,}'.format(missing_warr_pdf_count))
missing_unspsc = ('Missing UNSPSC Code: ', '{:,}'.format(missing_unspsc_count))
missing_msds = ('Missing MSDS: ', '{:,}'.format(missing_msds_count))

# CSV Output
report_card.append(valid_parts)
report_card.append(application_required)
report_card.append(interchange_required)
report_card.append(oe_interchange_required)
report_card.append(mpcc)
report_card.append(attributes)
report_card.append(image_required)
report_card.append(upc)
report_card.append(warranties)
report_card.append(unspsc)
report_card.append(msds)
report_card.append(missing_applications)
report_card.append(missing_interchange)
report_card.append(missing_oe_interchange)
report_card.append(missing_mpcc)
report_card.append(missing_attributes)
report_card.append(missing_image)
report_card.append(missing_UPC)
report_card.append(missing_warranty_text)
report_card.append(missing_warranty_pdf)
report_card.append(missing_unspsc)
report_card.append(missing_msds)

for row in header:
    print(row)

for x in report_card:
    print(x)

with open('Report_Card.csv', 'w', newline='') as f:
    writer = csv.writer(f)
    writer.writerows(header)
    writer.writerows(report_card)

For the data you have provided, this gives you a Report_Card.csv file as follows:

Supplier Line:  Gates Rubber - Denver (GAT)
Summary:    Parts HQ Abbr
ACCT No:    40013586
RecCount:   10221
Applicable Date:    June 14, 2017 (Wednesday)
Number of Valid Parts:  6
Application Records Required:   0
Interchange Records Required:   6
OE Interchange Records Required:    6
MPCC Required:  6
Attributes Required:    6
Image Required:     6
UPC Requited:   6
Warranty Text/PDF Required:     6
UNSPSC Code Required:   6
MSDS Required:  0
Missing Applications:   0
Missing Interchange:    0
Missing OE Interchange:     0
Missing MPCC:   0
Missing Attributes:     0
Missing Image:  0
Missing UPC:    0
Missing Warranty Text:  0
Missing Warranty PDF:   0
Missing UNSPSC Code:    0
Missing MSDS:   0

Your code could certainly do with a rethink. Rather than having so many variables, i.e. one per column, think about using something like a Python Counter() which works like a dictionary. Working with lists or dictionaries would make your code much simpler.


问:
How to force to return in time swift 3
I am a newbie in swift language. The thing I want to ask is, why return works before the body of function? For example:
If I have a function

```<code>func somefunc(completion: @escaping ([String]){
 let something = ["One", "Two", "Three", "Four", "Five"]
 completion(something)
}

func appending() -> Int {
 var somestring = [String]()
    somefunc { (result) in
     for i in 0..<result.count{
       somestring.append(result[i])
      }
    }
 return somestring.count
}
</code>```

So, in that case function appending() will return 0. However I want it should be 5. Return works before the somefunc will append the result to variable i. How to set them order? Or something like that?

答:
OK, let's set aside your somefunc method, which we might infer from presence of @escaping, is trying to do something asynchronously. Clearly, in your attempt to simplify the question, you've converted it to something that isn't asynchronous. But let's assume it really was calling its completion handler closure asynchronously (i.e. later) on the main thread. You could then do something like:

```<code>var stringsForTable = [String]()

func appending() {
    somefunc { results in
        self.stringsForTable.append(contentsOf: results)
        self.tableView.reloadData()
    }
}
</code>```

Note, appending isn't returning anything, because you can't return data from an asynchronous function (because asynchronous functions don't call their completion handler closures until later, until well after appending has finished). But what appending can do is trigger whatever you need for your UI to be reloaded when the completion handler is called.

This is just a hypothetical example, but it illustrates the pattern. We'd have to see what somefunc is actually doing to advise further.

Title: How to force to return in time swift 3

Body:
I am a newbie in swift language. The thing I want to ask is, why return works before the body of function? For example:
If I have a function

func somefunc(completion: @escaping ([String]){
 let something = ["One", "Two", "Three", "Four", "Five"]
 completion(something)
}

func appending() -> Int {
 var somestring = [String]()
    somefunc { (result) in
     for i in 0..<result.count{
       somestring.append(result[i])
      }
    }
 return somestring.count
}

So, in that case function appending() will return 0. However I want it should be 5. Return works before the somefunc will append the result to variable i. How to set them order? Or something like that?

Body:
OK, let's set aside your somefunc method, which we might infer from presence of @escaping, is trying to do something asynchronously. Clearly, in your attempt to simplify the question, you've converted it to something that isn't asynchronous. But let's assume it really was calling its completion handler closure asynchronously (i.e. later) on the main thread. You could then do something like:

var stringsForTable = [String]()

func appending() {
    somefunc { results in
        self.stringsForTable.append(contentsOf: results)
        self.tableView.reloadData()
    }
}

Note, appending isn't returning anything, because you can't return data from an asynchronous function (because asynchronous functions don't call their completion handler closures until later, until well after appending has finished). But what appending can do is trigger whatever you need for your UI to be reloaded when the completion handler is called.

This is just a hypothetical example, but it illustrates the pattern. We'd have to see what somefunc is actually doing to advise further.


问:
Not sure how to use scrapy's itemLoaders
I'm trying to  learn how to use scrappy's itemLoaders, can anybody told me what am I doing wrong???I would like to thank you in advance.

```<code>import scrapy
from items.items import ItemsItem
from scrapy.loader import ItemLoader

class ItemspiderSpider(scrapy.Spider):
    name = 'itemspider'
    allowed_domains = ['yellowpages.com']
    start_urls = ['https://www.yellowpages.com/search?search_terms=handyman&geo_location_terms=Miami%2C+FL']

    def parse(self, response):
    #create the loader using the response
    l = ItemLoader(item=ItemsItem(), response=response)
    #create a for loop
    for listing in response.css('div.search-results.organic div.srp-listing'):
         l.add_css('Name', listing.css('a.business-name span::text').extract())
         l.add_css('Details', response.urljoin(listing.css('a.business-name::attr(href)')))
         l.add_css('WebSite', listing.css('a.track-visit-website::attr(href)').extract_first())
         l.add_css('Phones', listing.css('div.phones::text').extract())

         yield l.load_item()
</code>```

When I run the code I keep getting this error:

```<code>root@debian:~/Desktop/items/items/spiders# scrapy runspider itemspider.py -o item.csv
/usr/local/lib/python3.5/dist-packages/scrapy/spiderloader.py:37: UserWarning: There are several spiders with the same name:

  ItemspiderSpider named 'itemspider' (in items.spiders.itemspider)
  ItemspiderSpider named 'itemspider' (in items.spiders.itemspiderLog)

  This can cause unexpected behavior.
  warnings.warn(msg, UserWarning)
2017-07-04 16:33:20 [scrapy.utils.log] INFO: Scrapy 1.4.0 started (bot: items)
2017-07-04 16:33:20 [scrapy.utils.log] INFO: Overridden settings: {'BOT_NAME': 'items', 'FEED_FORMAT': 'csv', 'SPIDER_LOADER_WARN_ONLY': True, 'SPIDER_MODULES': ['items.spiders'], 'FEED_URI': 'item.csv', 'ROBOTSTXT_OBEY': True, 'NEWSPIDER_MODULE': 'items.spiders'}
2017-07-04 16:33:20 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.corestats.CoreStats',
 'scrapy.extensions.memusage.MemoryUsage',
 'scrapy.extensions.telnet.TelnetConsole',
 'scrapy.extensions.feedexport.FeedExporter',
 'scrapy.extensions.logstats.LogStats']
2017-07-04 16:33:20 [scrapy.middleware] INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.robotstxt.RobotsTxtMiddleware',
 'scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
 'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
 'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
 'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
 'scrapy.downloadermiddlewares.retry.RetryMiddleware',
 'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
 'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
 'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
 'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
 'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware',
 'scrapy.downloadermiddlewares.stats.DownloaderStats']
2017-07-04 16:33:20 [scrapy.middleware] INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
 'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
 'scrapy.spidermiddlewares.referer.RefererMiddleware',
 'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
 'scrapy.spidermiddlewares.depth.DepthMiddleware']
2017-07-04 16:33:20 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2017-07-04 16:33:20 [scrapy.core.engine] INFO: Spider opened
2017-07-04 16:33:20 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2017-07-04 16:33:20 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6023
2017-07-04 16:33:21 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.yellowpages.com/robots.txt> (referer: None)
2017-07-04 16:33:23 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.yellowpages.com/search?search_terms=handyman&geo_location_terms=Miami%2C+FL> (referer: None)
2017-07-04 16:33:24 [scrapy.core.scraper] ERROR: Spider error processing <GET https://www.yellowpages.com/search?search_terms=handyman&geo_location_terms=Miami%2C+FL> (referer: None)
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/scrapy/utils/defer.py", line 102, in iter_errback
    yield next(it)
  File "/usr/local/lib/python3.5/dist-packages/scrapy/spidermiddlewares/offsite.py", line 29, in process_spider_output
    for x in result:
  File "/usr/local/lib/python3.5/dist-packages/scrapy/spidermiddlewares/referer.py", line 339, in <genexpr>
    return (_set_referer(r) for r in result or ())
  File "/usr/local/lib/python3.5/dist-packages/scrapy/spidermiddlewares/urllength.py", line 37, in <genexpr>
    return (r for r in result or () if _filter(r))
  File "/usr/local/lib/python3.5/dist-packages/scrapy/spidermiddlewares/depth.py", line 58, in <genexpr>
    return (r for r in result or () if _filter(r))
  File "/root/Desktop/items/items/spiders/itemspider.py", line 17, in parse
    l.add_css('Details', response.urljoin(listing.css('a.business-name::attr(href)')))
  File "/usr/local/lib/python3.5/dist-packages/scrapy/http/response/text.py", line 82, in urljoin
    return urljoin(get_base_url(self), url)
  File "/usr/lib/python3.5/urllib/parse.py", line 416, in urljoin
    base, url, _coerce_result = _coerce_args(base, url)
  File "/usr/lib/python3.5/urllib/parse.py", line 112, in _coerce_args
    raise TypeError("Cannot mix str and non-str arguments")
TypeError: Cannot mix str and non-str arguments
2017-07-04 16:33:24 [scrapy.core.engine] INFO: Closing spider (finished)
2017-07-04 16:33:24 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 503,
 'downloader/request_count': 2,
 'downloader/request_method_count/GET': 2,
 'downloader/response_bytes': 52924,
 'downloader/response_count': 2,
 'downloader/response_status_count/200': 2,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2017, 7, 4, 21, 33, 24, 121098),
 'log_count/DEBUG': 3,
 'log_count/ERROR': 1,
 'log_count/INFO': 7,
 'memusage/max': 49471488,
 'memusage/startup': 49471488,
 'response_received_count': 2,
 'scheduler/dequeued': 1,
 'scheduler/dequeued/memory': 1,
 'scheduler/enqueued': 1,
 'scheduler/enqueued/memory': 1,
 'spider_exceptions/TypeError': 1,
 'start_time': datetime.datetime(2017, 7, 4, 21, 33, 20, 705391)}
2017-07-04 16:33:24 [scrapy.core.engine] INFO: Spider closed (finished)
</code>```

Not sure what is going this is actually the first time I tried to use the ItemLoaders

答:
There are a few issues with your code:

response.urljoin() expects a single string as parameter, not a list. You are passing the result of listing.css(), which is a SelectorList. You can use response.urljoin(listing.css('a.business-name::attr(href)').extract_first())

you need to instantiate one item loader per loop iteration, otherwise, you're accumulating values for each field of a single yielded item

you are using .add_css() with some values (result of .extract...() calls. .add_css() needs a CSS selector string, not the result of a selector extraction. The CSS extraction will then be done by the item loader. Or, you can use .add_value() if you want to pass the "final" field value directly.

Here are 2 versions that should get you going:

```<code>import scrapy
from items.items import ItemsItem
from scrapy.loader import ItemLoader

class ItemspiderSpider(scrapy.Spider):
    name = 'itemspider'
    allowed_domains = ['yellowpages.com']
    start_urls = ['https://www.yellowpages.com/search?search_terms=handyman&geo_location_terms=Miami%2C+FL']

    def parse(self, response):

      for listing in response.css('div.search-results.organic div.srp-listing'):

         # create the loader using the SELECTOR, inside the loop
         l = ItemLoader(item=ItemsItem())

         # use .add_value() since we pass the extraction result directly
         l.add_value('Name', listing.css('a.business-name span::text').extract())

         # pass a single value to response.urljoin()
         l.add_value('Details',
                     response.urljoin(
                         listing.css('a.business-name::attr(href)').extract_first()
                     ))
         l.add_value('WebSite', listing.css('a.track-visit-website::attr(href)').extract_first())
         l.add_value('Phones', listing.css('div.phones::text').extract())

         yield l.load_item()
</code>```

Or, using .add_css():

```<code>import scrapy
from items.items import ItemsItem
from scrapy.loader import ItemLoader

class ItemspiderSpider(scrapy.Spider):
    name = 'itemspider'
    allowed_domains = ['yellowpages.com']
    start_urls = ['https://www.yellowpages.com/search?search_terms=handyman&geo_location_terms=Miami%2C+FL']

    def parse(self, response):

        for listing in response.css('div.search-results.organic div.srp-listing'):

            # pass the 'listing' selector to the item loader
            # so that CSS selection is relative to it
            l = ItemLoader(ItemsItem(), selector=listing)

            l.add_css('Name', 'a.business-name span::text')
            l.add_css('Details', 'a.business-name::attr(href)')
            l.add_css('WebSite', 'a.track-visit-website::attr(href)')
            l.add_css('Phones', 'div.phones::text')

            yield l.load_item()
</code>```

Body:
I'm trying to  learn how to use scrappy's itemLoaders, can anybody told me what am I doing wrong???I would like to thank you in advance.

import scrapy
from items.items import ItemsItem
from scrapy.loader import ItemLoader

class ItemspiderSpider(scrapy.Spider):
    name = 'itemspider'
    allowed_domains = ['yellowpages.com']
    start_urls = ['https://www.yellowpages.com/search?search_terms=handyman&geo_location_terms=Miami%2C+FL']

    def parse(self, response):
    #create the loader using the response
    l = ItemLoader(item=ItemsItem(), response=response)
    #create a for loop
    for listing in response.css('div.search-results.organic div.srp-listing'):
         l.add_css('Name', listing.css('a.business-name span::text').extract())
         l.add_css('Details', response.urljoin(listing.css('a.business-name::attr(href)')))
         l.add_css('WebSite', listing.css('a.track-visit-website::attr(href)').extract_first())
         l.add_css('Phones', listing.css('div.phones::text').extract())

         yield l.load_item()

When I run the code I keep getting this error:

root@debian:~/Desktop/items/items/spiders# scrapy runspider itemspider.py -o item.csv
/usr/local/lib/python3.5/dist-packages/scrapy/spiderloader.py:37: UserWarning: There are several spiders with the same name:

  ItemspiderSpider named 'itemspider' (in items.spiders.itemspider)
  ItemspiderSpider named 'itemspider' (in items.spiders.itemspiderLog)

  This can cause unexpected behavior.
  warnings.warn(msg, UserWarning)
2017-07-04 16:33:20 [scrapy.utils.log] INFO: Scrapy 1.4.0 started (bot: items)
2017-07-04 16:33:20 [scrapy.utils.log] INFO: Overridden settings: {'BOT_NAME': 'items', 'FEED_FORMAT': 'csv', 'SPIDER_LOADER_WARN_ONLY': True, 'SPIDER_MODULES': ['items.spiders'], 'FEED_URI': 'item.csv', 'ROBOTSTXT_OBEY': True, 'NEWSPIDER_MODULE': 'items.spiders'}
2017-07-04 16:33:20 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.corestats.CoreStats',
 'scrapy.extensions.memusage.MemoryUsage',
 'scrapy.extensions.telnet.TelnetConsole',
 'scrapy.extensions.feedexport.FeedExporter',
 'scrapy.extensions.logstats.LogStats']
2017-07-04 16:33:20 [scrapy.middleware] INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.robotstxt.RobotsTxtMiddleware',
 'scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
 'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
 'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
 'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
 'scrapy.downloadermiddlewares.retry.RetryMiddleware',
 'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
 'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
 'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
 'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
 'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware',
 'scrapy.downloadermiddlewares.stats.DownloaderStats']
2017-07-04 16:33:20 [scrapy.middleware] INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
 'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
 'scrapy.spidermiddlewares.referer.RefererMiddleware',
 'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
 'scrapy.spidermiddlewares.depth.DepthMiddleware']
2017-07-04 16:33:20 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2017-07-04 16:33:20 [scrapy.core.engine] INFO: Spider opened
2017-07-04 16:33:20 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2017-07-04 16:33:20 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6023
2017-07-04 16:33:21 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.yellowpages.com/robots.txt> (referer: None)
2017-07-04 16:33:23 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.yellowpages.com/search?search_terms=handyman&geo_location_terms=Miami%2C+FL> (referer: None)
2017-07-04 16:33:24 [scrapy.core.scraper] ERROR: Spider error processing <GET https://www.yellowpages.com/search?search_terms=handyman&geo_location_terms=Miami%2C+FL> (referer: None)
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/scrapy/utils/defer.py", line 102, in iter_errback
    yield next(it)
  File "/usr/local/lib/python3.5/dist-packages/scrapy/spidermiddlewares/offsite.py", line 29, in process_spider_output
    for x in result:
  File "/usr/local/lib/python3.5/dist-packages/scrapy/spidermiddlewares/referer.py", line 339, in <genexpr>
    return (_set_referer(r) for r in result or ())
  File "/usr/local/lib/python3.5/dist-packages/scrapy/spidermiddlewares/urllength.py", line 37, in <genexpr>
    return (r for r in result or () if _filter(r))
  File "/usr/local/lib/python3.5/dist-packages/scrapy/spidermiddlewares/depth.py", line 58, in <genexpr>
    return (r for r in result or () if _filter(r))
  File "/root/Desktop/items/items/spiders/itemspider.py", line 17, in parse
    l.add_css('Details', response.urljoin(listing.css('a.business-name::attr(href)')))
  File "/usr/local/lib/python3.5/dist-packages/scrapy/http/response/text.py", line 82, in urljoin
    return urljoin(get_base_url(self), url)
  File "/usr/lib/python3.5/urllib/parse.py", line 416, in urljoin
    base, url, _coerce_result = _coerce_args(base, url)
  File "/usr/lib/python3.5/urllib/parse.py", line 112, in _coerce_args
    raise TypeError("Cannot mix str and non-str arguments")
TypeError: Cannot mix str and non-str arguments
2017-07-04 16:33:24 [scrapy.core.engine] INFO: Closing spider (finished)
2017-07-04 16:33:24 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 503,
 'downloader/request_count': 2,
 'downloader/request_method_count/GET': 2,
 'downloader/response_bytes': 52924,
 'downloader/response_count': 2,
 'downloader/response_status_count/200': 2,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2017, 7, 4, 21, 33, 24, 121098),
 'log_count/DEBUG': 3,
 'log_count/ERROR': 1,
 'log_count/INFO': 7,
 'memusage/max': 49471488,
 'memusage/startup': 49471488,
 'response_received_count': 2,
 'scheduler/dequeued': 1,
 'scheduler/dequeued/memory': 1,
 'scheduler/enqueued': 1,
 'scheduler/enqueued/memory': 1,
 'spider_exceptions/TypeError': 1,
 'start_time': datetime.datetime(2017, 7, 4, 21, 33, 20, 705391)}
2017-07-04 16:33:24 [scrapy.core.engine] INFO: Spider closed (finished)

Not sure what is going this is actually the first time I tried to use the ItemLoaders

Body:
There are a few issues with your code:

response.urljoin() expects a single string as parameter, not a list. You are passing the result of listing.css(), which is a SelectorList. You can use response.urljoin(listing.css('a.business-name::attr(href)').extract_first())

you need to instantiate one item loader per loop iteration, otherwise, you're accumulating values for each field of a single yielded item

you are using .add_css() with some values (result of .extract...() calls. .add_css() needs a CSS selector string, not the result of a selector extraction. The CSS extraction will then be done by the item loader. Or, you can use .add_value() if you want to pass the "final" field value directly.

Here are 2 versions that should get you going:

import scrapy
from items.items import ItemsItem
from scrapy.loader import ItemLoader

class ItemspiderSpider(scrapy.Spider):
    name = 'itemspider'
    allowed_domains = ['yellowpages.com']
    start_urls = ['https://www.yellowpages.com/search?search_terms=handyman&geo_location_terms=Miami%2C+FL']

    def parse(self, response):

      for listing in response.css('div.search-results.organic div.srp-listing'):

         # create the loader using the SELECTOR, inside the loop
         l = ItemLoader(item=ItemsItem())

         # use .add_value() since we pass the extraction result directly
         l.add_value('Name', listing.css('a.business-name span::text').extract())

         # pass a single value to response.urljoin()
         l.add_value('Details',
                     response.urljoin(
                         listing.css('a.business-name::attr(href)').extract_first()
                     ))
         l.add_value('WebSite', listing.css('a.track-visit-website::attr(href)').extract_first())
         l.add_value('Phones', listing.css('div.phones::text').extract())

         yield l.load_item()

Or, using .add_css():

import scrapy
from items.items import ItemsItem
from scrapy.loader import ItemLoader

class ItemspiderSpider(scrapy.Spider):
    name = 'itemspider'
    allowed_domains = ['yellowpages.com']
    start_urls = ['https://www.yellowpages.com/search?search_terms=handyman&geo_location_terms=Miami%2C+FL']

    def parse(self, response):

        for listing in response.css('div.search-results.organic div.srp-listing'):

            # pass the 'listing' selector to the item loader
            # so that CSS selection is relative to it
            l = ItemLoader(ItemsItem(), selector=listing)

            l.add_css('Name', 'a.business-name span::text')
            l.add_css('Details', 'a.business-name::attr(href)')
            l.add_css('WebSite', 'a.track-visit-website::attr(href)')
            l.add_css('Phones', 'div.phones::text')

            yield l.load_item()


问:
Mysql column with null values - what are the space requirements?
I have a table with quite a lot entries.
I need an additional column with an integer value or null.
The thing is that only very few rows will have that field populated.

So i wonder whether its better to create a seperate table where i link the entries in an 1:1 relation.

I know one integer entry takes 4 bytes in mysql/myisam. If I have the column set to allow null values, and only 100 of 100 000 rows have the field populated, will the rest still consume 4 bytes for every null value?

Or is mysql intelligent enough to set the value where it is populated and just regard everything as null, where nothing is set?

答:
It will use bitfields to store nulls so it may need less than one byte. But, even if it did - who cares, unless you are using 3.5" floppies to store your backend in ;-)

NULL in MySQL (Performance & Storage)

ContentLicense: CC BY-SA 2.5


问:
JavaScript Watch removing watched value from object
i'm trying to watch an objects value, and if it changes, so some stuff..

So this is my object,

```<code>var list = [{
   object: undefined,
   index: 0,
   src: 'url here',
   active: { val: 0 }
}]
</code>```

So I made active an additional object as I create a new object from the above, but make the active value the value from above, this keeps a reference of this object between the 2 objects.

```<code>var newList = [];
newList.push({
  object: undefined,
  index: 0,
  src: list[i].src,
  active: list[i].active // Use reference to old list
});
</code>```

So i'm trying to watch the active value like so:

```<code>(list.active).watch('val', function() {
  if (list.active.val === 1) {
    console.log('active');
    list.object.classList.add('active');
  } else {
    console.log('not active');
    list.object.classList.remove('active');
  }
});
</code>```

However it appears that when I watch this value it is being removed, as if I console.log out the list, then the value is  set to undefined! I'm changing the value of list.active.val after adding the watch events.

Here is the Polyfill i'm using for the watch functionality.

```<code>// object.watch
if (!Object.prototype.watch) {
  Object.defineProperty(Object.prototype, "watch", {
    enumerable: false,
    configurable: true,
    writable: false,
    value: function (prop, handler) {
      var oldval = this[prop],
          newval = oldval,
          getter = function () {
            return newval;
          },
          setter = function (val) {
           oldval = newval;
            return newval = handler.call(this, prop, oldval, val);
          };

      if (delete this[prop]) { // can't watch constants
        Object.defineProperty(this, prop, {
          get: getter,
          set: setter,
          enumerable: true,
          configurable: true
        });
      }
    }
  });
}

// object.unwatch
if (!Object.prototype.unwatch) {
  Object.defineProperty(Object.prototype, "unwatch", {
    enumerable: false,
    configurable: true,
    writable: false,
    value: function (prop) {
      var val = this[prop];
      delete this[prop]; // remove accessors
      this[prop] = val;
    }
  });
}
</code>```

Edit 1

Added the proxy-observe Polyfill however this doesn't appear to be watching still, I have added it like so:

```<code>list[0] = Object.observe(list[0], function(changeset) {
  console.log('changed');
});
</code>```

list[0].active contains the object { val: 0 }, so it should be observicing this object.

Not getting any errors, it's just doing nothing, ideas?

答:
You don't need to implement a custom watch/unwatch features,
Ecmascript 2015 already provides a specific api:

Proxy

There are a plenty of polyfills to make it working on legacy browsers.

There was a proposal called Object.Observe to address what you need and you can find a Proxy porting here: https://github.com/anywhichway/proxy-observe

Follows a basic working example:

// Your Observable target
const target = Object.create(null);

const observer = {
  set(target, key, value) {
    console.log(`Should we set '${value}' as value of '${key}' property?`)

    target[key] = value;
  },
};

const observable = new Proxy(target, observer);
observable.someKindOfProperty = 'Hello World'; ```

Body:
i'm trying to watch an objects value, and if it changes, so some stuff..

So this is my object,

var list = [{
   object: undefined,
   index: 0,
   src: 'url here',
   active: { val: 0 }
}]

So I made active an additional object as I create a new object from the above, but make the active value the value from above, this keeps a reference of this object between the 2 objects.

var newList = [];
newList.push({
  object: undefined,
  index: 0,
  src: list[i].src,
  active: list[i].active // Use reference to old list
});

So i'm trying to watch the active value like so:

(list.active).watch('val', function() {
  if (list.active.val === 1) {
    console.log('active');
    list.object.classList.add('active');
  } else {
    console.log('not active');
    list.object.classList.remove('active');
  }
});

However it appears that when I watch this value it is being removed, as if I console.log out the list, then the value is  set to undefined! I'm changing the value of list.active.val after adding the watch events.

Here is the Polyfill i'm using for the watch functionality.

// object.watch
if (!Object.prototype.watch) {
  Object.defineProperty(Object.prototype, "watch", {
    enumerable: false,
    configurable: true,
    writable: false,
    value: function (prop, handler) {
      var oldval = this[prop],
          newval = oldval,
          getter = function () {
            return newval;
          },
          setter = function (val) {
           oldval = newval;
            return newval = handler.call(this, prop, oldval, val);
          };

      if (delete this[prop]) { // can't watch constants
        Object.defineProperty(this, prop, {
          get: getter,
          set: setter,
          enumerable: true,
          configurable: true
        });
      }
    }
  });
}

// object.unwatch
if (!Object.prototype.unwatch) {
  Object.defineProperty(Object.prototype, "unwatch", {
    enumerable: false,
    configurable: true,
    writable: false,
    value: function (prop) {
      var val = this[prop];
      delete this[prop]; // remove accessors
      this[prop] = val;
    }
  });
}

Edit 1

Added the proxy-observe Polyfill however this doesn't appear to be watching still, I have added it like so:

list[0] = Object.observe(list[0], function(changeset) {
  console.log('changed');
});

list[0].active contains the object { val: 0 }, so it should be observicing this object.

Not getting any errors, it's just doing nothing, ideas?


问:
Issue with Selected row in GridView Paging
I have a Gridview where I have functionality of Update row. Upon clicking update row a text box is generated in the row where user can update the record for the particular row. However, if the user selects row 2 of gridview( page=1)for update and then clicks on say page 4 without saving.  Page 4 is now displayed and row 2 is enabled for update on page 4. Moreover, if the user selects row 2 of gridview (page=1) then clicks on say page 4.  Page 4 is now displayed and row 2 is highlighted, so to the User it looks like they have row2 of page 4 selected,  I need to have the selectedrow move with the paging to not confuse users.
Below is my Gridview defination:

```<code><asp:GridView ID="grdDisountList" runat="server" PersistedSelection="false" CellPadding="4" ForeColor="#333333" GridLines="None"
        AutoGenerateColumns="false" Width="100%" OnRowEditing="EditRow" OnRowCancelingEdit="CancelEditRow"
        OnRowUpdating="UpdateRow" DataKeyNames="Discount_Id" AllowPaging="true" OnRowDataBound="grdDisountList_RowDataBound"
        PageSize="15" OnPageIndexChanging="OnPageIndexChanging" OnRowCommand="SelectRow" OnSelectedIndexChanged="grdDisountList_SelectedIndexChanged">
</code>```

Any Ideas?

答:
The selected row only works on the visible items in the GridView. So when you change a page the selected item will still be the 2nd selected row, even though it is a completely different item.

The best option is to set the selected row to -1 in the OnPageIndexChanging event.

```<code>protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    GridView1.SelectedIndex = -1;
    GridView1.EditIndex = -1;
}
</code>```

Os if you really want to keep the SelectedIndex, save it in a Session or ViewState along with the PageIndex and re-select the correct one based on those values.

Body:
I have a Gridview where I have functionality of Update row. Upon clicking update row a text box is generated in the row where user can update the record for the particular row. However, if the user selects row 2 of gridview( page=1)for update and then clicks on say page 4 without saving.  Page 4 is now displayed and row 2 is enabled for update on page 4. Moreover, if the user selects row 2 of gridview (page=1) then clicks on say page 4.  Page 4 is now displayed and row 2 is highlighted, so to the User it looks like they have row2 of page 4 selected,  I need to have the selectedrow move with the paging to not confuse users.
Below is my Gridview defination:

<asp:GridView ID="grdDisountList" runat="server" PersistedSelection="false" CellPadding="4" ForeColor="#333333" GridLines="None"
        AutoGenerateColumns="false" Width="100%" OnRowEditing="EditRow" OnRowCancelingEdit="CancelEditRow"
        OnRowUpdating="UpdateRow" DataKeyNames="Discount_Id" AllowPaging="true" OnRowDataBound="grdDisountList_RowDataBound"
        PageSize="15" OnPageIndexChanging="OnPageIndexChanging" OnRowCommand="SelectRow" OnSelectedIndexChanged="grdDisountList_SelectedIndexChanged">

Any Ideas?

Body:
The selected row only works on the visible items in the GridView. So when you change a page the selected item will still be the 2nd selected row, even though it is a completely different item.

The best option is to set the selected row to -1 in the OnPageIndexChanging event.

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    GridView1.SelectedIndex = -1;
    GridView1.EditIndex = -1;
}

Os if you really want to keep the SelectedIndex, save it in a Session or ViewState along with the PageIndex and re-select the correct one based on those values.


问:
Symfony: Disable environment variables from caching
I am running a Symfony 2.8.6 application on nginx/php-fpm.

There are multiple domains that are resolved into this server, and basically what I want to do is change RDB configuration according to which domain was used to access.

So my nginx.conf has lines like fastcgi_param SYMFONY__SOME__PARAM $host, but I have a problem.

This parameter injection is cached and not working as intended.

For example, there are two domains a.example.com and b.example.com, and they point to my server.
I want it to connect to different MySQL server when accessed through different domain, but it ignores the domain and connects to the same server always.

What I've confirmed:

Nginx passes the variable correctly.

The output of var_dump($_SERVER['SYMFONY__SOME__PARAM']) changes as expected.

The parameter is stored in app/cache/prod/appProdProjectContainer.php

AFAIS there are two options: disabling configuration cache totally, or disabling caching environment variables.

I think the latter option is more preferrable, but I even don't know how to disable the cache, whether totally or partially.

答:
How about changing cache directory for each environemnt.</p>

```<code>fastcgi_param SYMFONY__CACHE_DIR /path/to/cache
</code>```

Body:
How about changing cache directory for each environemnt.

fastcgi_param SYMFONY__CACHE_DIR /path/to/cache


问:
How to find Android tablet or mobile in Phonegap?
I need set android device smart phone portrait only? window.isTablet is find the mobile or tablet only, so I need to set screen lock orientation and write code too.

答:
Using Cordova Screen Orientation Plugin we fixed.

In app.js

```<code>document.addEventListener("deviceready", function() {
    if (window.innerHeight <= 736 || window.innerWidth <= 736) {
        screen.orientation.lock('portrait');
    } else {
        screen.orientation.unlock('portrait');
    }
});

window.addEventListener("orientationchange", function() {
    if (window.innerHeight <= 736 || window.innerWidth <= 736) {
        screen.orientation.lock('portrait');
    } else {
        screen.orientation.unlock('portrait');
    }
});
</code>```

Body:
Using Cordova Screen Orientation Plugin we fixed.

In app.js

document.addEventListener("deviceready", function() {
    if (window.innerHeight <= 736 || window.innerWidth <= 736) {
        screen.orientation.lock('portrait');
    } else {
        screen.orientation.unlock('portrait');
    }
});

window.addEventListener("orientationchange", function() {
    if (window.innerHeight <= 736 || window.innerWidth <= 736) {
        screen.orientation.lock('portrait');
    } else {
        screen.orientation.unlock('portrait');
    }
});


问:
Android Bottom Navigation bar not opening activity as linked to it
I am a newbie to Android development. I am learning UI designing as of now. I want a solution where there is a bottom bar with 5 options linked directly to 5 different activities. I got Java solutions from other stack overflow answers (How to change activity on bottom navigation button click? ) - 2nd Answer by sushil, but it has no activity - XML files in it for me to understand.

Bottom bar like this:

Activity to be loaded based on bottom bar:

答:
```<code>bottomNavigationView.setOnNavigationItemSelectedListener(
    new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.action_favorites:
                     Intent intent1 = new Intent(this, AnActivity.class);
                     startActivity(intent1);
                     break;
                case R.id.action_schedules:
                  Intent intent2 = new Intent(this, AnotherActivity.class);
                  startActivity(intent2);
                  break;
                case R.id.action_music:
                   Intent intent3 = new Intent(this, AnotherActivity.class);
                startActivity(intent3);
                break;
            }
            return true;
        }
    });
</code>```


问:
Laravel hasManyThrough in depth
I have been looking at documentation regarding the hasManyThrough relationship in Laravel, and fro some reason I am struggling with it as the documentation makes it look simple (maybe I am overthinking it)....

I have three Models tables at the moment:

```<code>-User (extends authenticatable)
    id,
    name,
    surname,
    email,
    password.

-Order
    id,
    user_id,
    name,
    unit,
    qty

-Product (i need to add a qty table to show how many is ordered).
    id,
    order_id,
    name,
    unit,
    description,
    family
</code>```

My Products model is used by Admin to CRUD products.

Please can someone explain to me how I will be able to display to admin which user made an order, and what does the order consist of.
I also want to display to the user a 'order history'..

Please will anyone be able to assist..

UPDATE

```<code>-order_product
  id,
  order_id,
  product_id
  quantity
</code>```

product controller:

```<code>  public function orders()
    {
       return $this->belongsToMany('App\Order')->withPivot('quantity');
    }
</code>```

order controller:

```<code>  public function products()
    {
       return $this->belongsToMany('App\Product')->withPivot('quantity');
    }
</code>```

答:
I think you need create one more table like order_details</p>

```<code>-User (extends authenticatable)
    id,
    name,
    surname,
    email,
    password.

-Order
    id,
    user_id,
    name,
    unit,
    qty

-Product (i need to add a qty table to show how many is ordered).
    id,
    name,
    unit,
    description,
    family
-Order_detail(Store product_id, order_id (You can put qty to this table))
    id,
    order_id,
    product_id
</code>```

Title: Laravel hasManyThrough in depth

Body:
I have been looking at documentation regarding the hasManyThrough relationship in Laravel, and fro some reason I am struggling with it as the documentation makes it look simple (maybe I am overthinking it)....

I have three Models tables at the moment:

-User (extends authenticatable)
    id,
    name,
    surname,
    email,
    password.

-Order
    id,
    user_id,
    name,
    unit,
    qty

-Product (i need to add a qty table to show how many is ordered).
    id,
    order_id,
    name,
    unit,
    description,
    family

My Products model is used by Admin to CRUD products.

Please can someone explain to me how I will be able to display to admin which user made an order, and what does the order consist of.
I also want to display to the user a 'order history'..

Please will anyone be able to assist..

UPDATE

-order_product
  id,
  order_id,
  product_id
  quantity

product controller:

  public function orders()
    {
       return $this->belongsToMany('App\Order')->withPivot('quantity');
    }

order controller:

  public function products()
    {
       return $this->belongsToMany('App\Product')->withPivot('quantity');
    }

Body:
I think you need create one more table like order_details

-User (extends authenticatable)
    id,
    name,
    surname,
    email,
    password.

-Order
    id,
    user_id,
    name,
    unit,
    qty

-Product (i need to add a qty table to show how many is ordered).
    id,
    name,
    unit,
    description,
    family
-Order_detail(Store product_id, order_id (You can put qty to this table))
    id,
    order_id,
    product_id


问:
Custom 404 Error Page in PHP
I am a novice in PHP, I hope you can help me with this problem.
How can I create a custom 404 error page in PHP without going anywhere or redirect it to other page.

Ex. mysite.com/no-where

The page no-where does not exists. I want to display a 404 error message with design on that URL.

How can I get the current URL? I got an error on this. Coz I want to check the header if its 404.

```<code>$url = $_SERVER['REQUEST_URI'];
$array = get_headers($url);
$string = $array[0];
if(strpos($string,"200")) {
    echo 'url exists';
} else {
    echo 'url does not exist';
}
</code>```

答:
If you are using apache as a web server you can create a file named .htaccess in the root directory of your website. This can contain various rules, including error handling, as follows:

```<code>ErrorDocument 404 (Your site error 404 page URL)
</code>```

For example:

```<code>ErrorDocument 404 http://example.domain/custom-404-page.html
</code>```

Title: Custom 404 Error Page in PHP

Body:
I am a novice in PHP, I hope you can help me with this problem.
How can I create a custom 404 error page in PHP without going anywhere or redirect it to other page.

Ex. mysite.com/no-where

The page no-where does not exists. I want to display a 404 error message with design on that URL.

How can I get the current URL? I got an error on this. Coz I want to check the header if its 404.

$url = $_SERVER['REQUEST_URI'];
$array = get_headers($url);
$string = $array[0];
if(strpos($string,"200")) {
    echo 'url exists';
} else {
    echo 'url does not exist';
}

Body:
If you are using apache as a web server you can create a file named .htaccess in the root directory of your website. This can contain various rules, including error handling, as follows:

ErrorDocument 404 (Your site error 404 page URL)

For example:

ErrorDocument 404 http://example.domain/custom-404-page.html


问:
Extjs: Reuse the same grid in TabPanel
in a Extjs application I have a Grid and a Tabs line over it. Content of the Grid depends on the selected Tab.
Say tabs has Jan-Feb-Mar-... values. Clicking of the Tab I would reload grid's store

Question: is it possible to avoid duplicating of the 12 grid components in favor to have one shared instance?

Thanks

Disclaimer: searching at the sencha's forum, google, stackoverflow was not successful :(

答: I haven't tried this myself, but I imagine that you could create a TabPanel with empty tabs and size the TabPanel so that only the tab strip is visible.  Under that (using the appropriate layout, border, vbox, etc.) create your GridPanel and use the TabPanel's activate event to reload the grid based on the currently-active tab.

Title: Extjs: Reuse the same grid in TabPanel


问:
How to declare parameter as function without return value?
I have a function in kotlin

```<code>fun printExecutionTime(block: () -> Any) {
    run {
        val currentTimeMillis = System.currentTimeMillis()
        block()
        Logr.d("Execution time of " + block.javaClass.name + " -> " + System.currentTimeMillis().minus(currentTimeMillis))
    }
}
</code>```

In java code, I want to pass void reference function as parameter, but can't bcs of return value

```<code>PerformanceKt.printExecutionTime(this::voidFunc);
</code>```

One way will be to use interface

```<code>interface Action {
    fun call()
}
</code>```

Is it possible to declare it in kotlin without an extra interface, so the code above will work?

答:
you can using java.lang.Runnable to avoiding introduce additional interface ,for example:

```<code>fun printExecutionTime(block: Runnable) {
  run {
    val currentTimeMillis = System.currentTimeMillis()
    block.run()
    //...
  }
}
</code>```

then you can using java method reference expression as below:

```<code>PerformanceKt.printExecutionTime(this::voidFunc);

void voidFunc(){...}
</code>```

Body:
I have a function in kotlin

fun printExecutionTime(block: () -> Any) {
    run {
        val currentTimeMillis = System.currentTimeMillis()
        block()
        Logr.d("Execution time of " + block.javaClass.name + " -> " + System.currentTimeMillis().minus(currentTimeMillis))
    }
}

In java code, I want to pass void reference function as parameter, but can't bcs of return value

PerformanceKt.printExecutionTime(this::voidFunc);

One way will be to use interface

interface Action {
    fun call()
}

Is it possible to declare it in kotlin without an extra interface, so the code above will work?

Body:
you can using java.lang.Runnable to avoiding introduce additional interface ,for example:

fun printExecutionTime(block: Runnable) {
  run {
    val currentTimeMillis = System.currentTimeMillis()
    block.run()
    //...
  }
}

then you can using java method reference expression as below:

PerformanceKt.printExecutionTime(this::voidFunc);

void voidFunc(){...}


问:
Unable to compile angular component in handsontable cell
Really stuck in a problem, I’m using handsontable with angular2. And in handsontable I’m using cell renderes to populate cells and that cell contains an angular component.

But the problem is that cell is not compiling in angular scope

答:
You will need to compile the component you want to display in the cell and add the component's html to the cell in a custom renderer. Here's an example:

import { Component, ComponentFactory, ComponentFactoryResolver, ComponentRef,
  OnInit, OnDestroy, ViewChild, ViewContainerRef } from '@angular/core';
import * as $ from 'jquery';
import * as _ from 'lodash';

@Component({
  selector: 'my-table-cell',
  template: 'Counter: {{counter}}, data: {{data.id}}'
})
export class MyTableCellComponent {
  counter = 0;
  data: any;
}

@Component({
  selector: 'my-table',
  template: `
    <hot-table [options]="currentOptions">
    </hot-table>

`,
})
export class MyTableComponent implements OnInit, OnDestroy {

  @ViewChild('templateContainer', { read: ViewContainerRef })
    templateContainer: ViewContainerRef;

  options: ht.GridOptions;
  private cellComponentFactory: ComponentFactory<MyCellComponent>;
  private cellTemplateComponents: {[id: string]:
    ComponentRef<MyTableCellComponent>} = {};

  constructor(private factoryResolver: ComponentFactoryResolver) {};

  public ngOnInit() {
    this.options = { columns: [{
      renderer: this.rendererLinkCell.bind(this),
      data: [{id: 1}, {id: 2}, {id: 3}, {id: 4}]
    }]};
    this.cellComponentFactory = this.factoryResolver
      .resolveComponentFactory(MyTableCellComponent);
  }

  public ngOnDestroy() {
    _.forOwn(this.cellTemplateComponents, (component) => {
      component.destroy();
    });
  }

  private rendererLinkCell(_instance: any, td: Element, _row: number,
      _col: number, _columnKey: string, data: any, _cellProperties) {
    let component = this.cellTemplateComponents[data.id];
    if (!component) {
      component = this.templateContainer
        .createComponent(this.cellComponentFactory);
      _.extend(component.instance, { data });
      component.changeDetectorRef.detectChanges();
      this.cellTemplateComponents[data.id] = component;
    }
    $(td).html(component.location.nativeElement);
  };
}
```

I haven't had time to test it, so let me know if it works.

Body:
You will need to compile the component you want to display in the cell and add the component's html to the cell in a custom renderer. Here's an example:

import { Component, ComponentFactory, ComponentFactoryResolver, ComponentRef,
  OnInit, OnDestroy, ViewChild, ViewContainerRef } from '@angular/core';
import * as $ from 'jquery';
import * as _ from 'lodash';

@Component({
  selector: 'my-table-cell',
  template: '<b (click)="counter++">Counter: {{counter}}, data: {{data.id}}<b>'
})
export class MyTableCellComponent {
  counter = 0;
  data: any;
}

@Component({
  selector: 'my-table',
  template: `
    <hot-table [options]="currentOptions">
    </hot-table>
    <div #templateContainer
        [hidden]="true">
    </div>`,
})
export class MyTableComponent implements OnInit, OnDestroy {

  @ViewChild('templateContainer', { read: ViewContainerRef })
    templateContainer: ViewContainerRef;

  options: ht.GridOptions;
  private cellComponentFactory: ComponentFactory<MyCellComponent>;
  private cellTemplateComponents: {[id: string]:
    ComponentRef<MyTableCellComponent>} = {};

  constructor(private factoryResolver: ComponentFactoryResolver) {};

  public ngOnInit() {
    this.options = { columns: [{
      renderer: this.rendererLinkCell.bind(this),
      data: [{id: 1}, {id: 2}, {id: 3}, {id: 4}]
    }]};
    this.cellComponentFactory = this.factoryResolver
      .resolveComponentFactory(MyTableCellComponent);
  }

  public ngOnDestroy() {
    _.forOwn(this.cellTemplateComponents, (component) => {
      component.destroy();
    });
  }

  private rendererLinkCell(_instance: any, td: Element, _row: number,
      _col: number, _columnKey: string, data: any, _cellProperties) {
    let component = this.cellTemplateComponents[data.id];
    if (!component) {
      component = this.templateContainer
        .createComponent(this.cellComponentFactory);
      _.extend(component.instance, { data });
      component.changeDetectorRef.detectChanges();
      this.cellTemplateComponents[data.id] = component;
    }
    $(td).html(component.location.nativeElement);
  };
}

I haven't had time to test it, so let me know if it works.


问:
Why isn't tcpdump rotating save files?
I'm trying to get tcpdump to rotate through a number of pcap files based on both file size and time using the following command:

```<code>tcpdump -i lo -G 60 -W 5 -C 1 -w /var/tmp/trace
</code>```

I expected it to rotate through 5 different log files (named trace0, trace1, trace2 etc.) when they either reach a maxmimum size of 1M (from the -C option) or after 60 seconds have elapsed (from the -G option). Instead only trace0 is created and it gets overwritten once the 60 seconds have elapsed.

Based on the man page's explanation of the -G argument I thought that, because I am also using -C, the files would be saved with the format 'file<count>':

-G rotate_seconds
  If specified, rotates the dump file specified with the -w option every rotate_seconds seconds. Savefiles will have the name specified by -w which should include a time format as defined by strftime(3). If no time format is specified, each new file will overwrite the previous.
  If used in conjunction with the -C option, filenames will take the form of `file<count>'.

If I omit the -G option then multiple files will be created as expected, but in that case they only rotate based on file size and not time.

Is there something I'm missing that would explain why I'm only getting trace0, and not the other four? My tcpdump version is 4.9.0.

答:
If no time format is specified, each new file will overwrite the previous.

You'll have to add something like %H:%M:%S to the end of your file name to prevent the override from happening. To control the amount of files, you can workaround by hooking into post-rotate command

Cleanup script:

```<code>echo 'ls -tp trace*| tail -n +6 | xargs -I{} sh -c "echo \"Removing {}\"; rm {}"' > cleanup.sh && chmod a+x cleanup.sh
</code>```

Tcpdump command

```<code>sudo tcpdump -i lo0 -G 1 -W 5 -C 1 -w trace%H:%M:%S -z ./cleanup.sh
</code>```

Example output:

```<code>tcpdump: listening on lo0, link-type NULL (BSD loopback), capture size 262144 bytes
Removing trace11:45:580
Removing trace11:45:570
Removing trace11:45:590
Removing trace11:46:000
Removing trace11:46:010
</code>```

Resulting files

```<code>ls -la trace*
-rw-r--r--  1 root  staff   24 Jul  5 11:48 trace11:48:250
-rw-r--r--  1 root  staff  894 Jul  5 11:48 trace11:48:270
-rw-r--r--  1 root  staff  894 Jul  5 11:48 trace11:48:280
-rw-r--r--  1 root  staff  894 Jul  5 11:48 trace11:48:290
-rw-r--r--  1 root  staff  894 Jul  5 11:48 trace11:48:300
</code>```

Title: Why isn't tcpdump rotating save files?

Body:
I'm trying to get tcpdump to rotate through a number of pcap files based on both file size and time using the following command:

tcpdump -i lo -G 60 -W 5 -C 1 -w /var/tmp/trace

I expected it to rotate through 5 different log files (named trace0, trace1, trace2 etc.) when they either reach a maxmimum size of 1M (from the -C option) or after 60 seconds have elapsed (from the -G option). Instead only trace0 is created and it gets overwritten once the 60 seconds have elapsed.

Based on the man page's explanation of the -G argument I thought that, because I am also using -C, the files would be saved with the format 'file<count>':

-G rotate_seconds
  If specified, rotates the dump file specified with the -w option every rotate_seconds seconds. Savefiles will have the name specified by -w which should include a time format as defined by strftime(3). If no time format is specified, each new file will overwrite the previous.
  If used in conjunction with the -C option, filenames will take the form of `file<count>'.

If I omit the -G option then multiple files will be created as expected, but in that case they only rotate based on file size and not time.

Is there something I'm missing that would explain why I'm only getting trace0, and not the other four? My tcpdump version is 4.9.0.

Body:
If no time format is specified, each new file will overwrite the previous.

You'll have to add something like %H:%M:%S to the end of your file name to prevent the override from happening. To control the amount of files, you can workaround by hooking into post-rotate command

Cleanup script:

echo 'ls -tp trace*| tail -n +6 | xargs -I{} sh -c "echo \"Removing {}\"; rm {}"' > cleanup.sh && chmod a+x cleanup.sh

Tcpdump command

sudo tcpdump -i lo0 -G 1 -W 5 -C 1 -w trace%H:%M:%S -z ./cleanup.sh

Example output:

tcpdump: listening on lo0, link-type NULL (BSD loopback), capture size 262144 bytes
Removing trace11:45:580
Removing trace11:45:570
Removing trace11:45:590
Removing trace11:46:000
Removing trace11:46:010

Resulting files

ls -la trace*
-rw-r--r--  1 root  staff   24 Jul  5 11:48 trace11:48:250
-rw-r--r--  1 root  staff  894 Jul  5 11:48 trace11:48:270
-rw-r--r--  1 root  staff  894 Jul  5 11:48 trace11:48:280
-rw-r--r--  1 root  staff  894 Jul  5 11:48 trace11:48:290
-rw-r--r--  1 root  staff  894 Jul  5 11:48 trace11:48:300


问:
If i login with a user it logs in with any password

<?php
require 'includes/common.php';
$email=$_POST['email'];
$password=$_POST['password'];
$email= mysqli_real_escape_string($con, $email);
$password= mysqli_real_escape_string($con, $password);
//md5($password);
 $select_user_query="select id , email from users where email='$email' and password= $password";
 //removed 'email' to email//
        $select_user_result= mysqli_query($con, $select_user_query) or die (mysqli_error($con));

               if (mysqli_num_rows($select_user_result==0))
               {
                   echo 'There no such user ';
               }

               else
               {
                   $row=mysqli_fetch_array($select_user_result);
                   $_SESSION['email']=$email;
                   $_SESSION['id']= $row[0];
                   header('location:products.php');
               }

?>           ```

 here is the code

**

here i am geting

email and password from post method

From my login page

i think most of the code is right

If i login with a user it logs in with any password

**

答:
Your error is not in the query. it is on your condition

```<code>if(mysqli_num_rows($select_user_result) == 0){
    echo 'No User found';
}else{
   echo 'oK';
}
</code>```

You ca try this way. I think your error is you inserted the ==0 inside the mysqli_num_rows() it must be outside.

Body:
<?php
require 'includes/common.php';
$email=$_POST['email'];
$password=$_POST['password'];
$email= mysqli_real_escape_string($con, $email);
$password= mysqli_real_escape_string($con, $password);
//md5($password);
 $select_user_query="select id , email from users where email='$email' and password= $password";
 //removed 'email' to email//
        $select_user_result= mysqli_query($con, $select_user_query) or die (mysqli_error($con));

               if (mysqli_num_rows($select_user_result==0))
               {
                   echo 'There no such user ';
               }

               else
               {
                   $row=mysqli_fetch_array($select_user_result);
                   $_SESSION['email']=$email;
                   $_SESSION['id']= $row[0];
                   header('location:products.php');
               }

?>

 here is the code

**

here i am geting

email and password from post method

From my login page

i think most of the code is right

If i login with a user it logs in with any password

**

Body:
Your error is not in the query. it is on your condition

if(mysqli_num_rows($select_user_result) == 0){
    echo 'No User found';
}else{
   echo 'oK';
}

You ca try this way. I think your error is you inserted the ==0 inside the mysqli_num_rows() it must be outside.


问:
android Toolbar can not show shadow
Here is my xml code:
```</p>

```<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                                 xmlns:app="http://schemas.android.com/apk/res-auto"
                                                 xmlns:tools="http://schemas.android.com/tools"
                                                 android:layout_width="match_parent"
                                                 android:layout_height="match_parent"
                                                 tools:context="xxxxxxxxx">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppThemeMD.NoActionBar.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/white"
            app:theme="@style/AppThemeBase"/>

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_vehicle_info"/>
</android.support.design.widget.CoordinatorLayout>
```

<p>```

1.When I run my app.I Can't see shadow of Toolbar.

2.But when I switch to another app,then switch back to my app.The shadow appeared.

Anyway to show shadow default?thx!

And here is my phone: Huawei 6x,Android7.0

答:
add this in your AndroidManifest.xml

```<code><application
  ...
  android:hardwareAccelerated="true">
</code>```

and add this in your toobar.xml :

```<code>android:clipToPadding="false"
</code>```

Title: android Toolbar can not show shadow

Body:
Here is my xml code:
```</p>

<pre><code><?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                                 xmlns:app="http://schemas.android.com/apk/res-auto"
                                                 xmlns:tools="http://schemas.android.com/tools"
                                                 android:layout_width="match_parent"
                                                 android:layout_height="match_parent"
                                                 tools:context="xxxxxxxxx">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppThemeMD.NoActionBar.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/white"
            app:theme="@style/AppThemeBase"/>

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_vehicle_info"/>
</android.support.design.widget.CoordinatorLayout>
</code></pre>

<p>```

1.When I run my app.I Can't see shadow of Toolbar.

2.But when I switch to another app,then switch back to my app.The shadow appeared.

Anyway to show shadow default?thx!

And here is my phone: Huawei 6x,Android7.0

Body:
add this in your AndroidManifest.xml

<application
  ...
  android:hardwareAccelerated="true">

and add this in your toobar.xml :

android:clipToPadding="false"


问:
Is it the efficeint program to rotate array in left direction?
```<code>#include<stdio.h>
#include<stdlib.h>

main()
{
    int i,j,l,m,n;
    j=0;
    printf("\nenter 5 element single dimension array\n");
    printf("enter shift rate\n");
    scanf("%d",&n);

    /* Here we take input from user that by what times user wants to rotate the array in left. */

    int arr[5],arrb[n];
    for(i=0;i<=4;i++){
        scanf("%d",&arr[i]);
    }

    /* Here we have taken another array. */

    for(i=0;i<=4;i++){
        printf("%d",arr[i]);
    }
    for(i=0;i<n;i++){
        arrb[j]=arr[i];
        j++;
        // These loop will shift array element to left by position which's entered by user.
    }
    printf("\n");

    for(i=0;i<=3;i++){
        arr[i]=arr[i+n];
    }
    for(i=0;i<=4;i++){
        if(n==1 && i==4)
            break;
        if(n==2 && i==3)
            break;
        if(n==3 && i==2)
            break;
        printf("%d",arr[i]);
    }

    //To combine these two arrays. Make it look like  single array instead of two
    for(i=0;i<n;i++){
        printf("%d",arrb[i]);
    }
    // Final sorted array will get printed here
}
</code>```

答:
I think you want to do something like this (you should check that 0 <= n <= 5, too):

```<code>int b[5];
int k = 0;
for(i=0; i<5; i++){

    if (i < 5 - n)
        b[i] = arr[i+n];
    else
    {
        b[i] = arr[k];
        k++;
    }
}
</code>```

Array b is used to save the rotated matrix.

Body:
I think you want to do something like this (you should check that 0 <= n <= 5, too):

int b[5];
int k = 0;
for(i=0; i<5; i++){

    if (i < 5 - n)
        b[i] = arr[i+n];
    else
    {
        b[i] = arr[k];
        k++;
    }
}

Array b is used to save the rotated matrix.


问:
Why Android crashes instead of showing the error in debug mode
At first, my story:

So after a while, I have found the reason which causes the crashing of my app in debug mode!!
I used replace command to replace a fragment with another one, but it crashes and exits from debug mode. In the same sample, it works fine and finally, I got the problem that was about my forgetfulness about the implementation of its listener. Though, in the code, there was a throw command for unimplementation!

```<code>@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}
</code>```

In Android Monitor when the crash happens, only these lines are appended:

07-05 08:05:54.797 6171-6171/com.shoniz.saledistributemobility

  D/AndroidRuntime: Shutting down VM

07-05 08:05:54.799    6171-6171/com.shoniz.saledistributemobility
  I/art: System.exit    called, status: 2

07-05 08:05:54.799    6171-6171/com.shoniz.saledistributemobility
  I/AndroidRuntime: VM    exiting with result code 2, cleanup skipped.

07-05 08:05:54.801    1303-1576/system_process D/GraphicsStats: Buffer
  count: 3

Now my question

Is there any way to force Android Studio to stop running and show the reason of the crash to save our time?! Something like a work that Visual Studio does.

答:
The problem with your code is, that you assume the Context to implement OnFragmentInteractionListener. You should find out, which instance you receive. This might not be your Activity directly, but another context wrapping it.

Throwing this error if your assertions fail is actually fine. It should as well be logged within Logcat. Maybe it would be better to use IllegalArgumentException, IllegalStateException or AssertionError.

If you want to log an error only you can use Log.e(tag, message) or Timber.e(message) when using the Timber logging utility.

Body:
At first, my story:

So after a while, I have found the reason which causes the crashing of my app in debug mode!!
I used replace command to replace a fragment with another one, but it crashes and exits from debug mode. In the same sample, it works fine and finally, I got the problem that was about my forgetfulness about the implementation of its listener. Though, in the code, there was a throw command for unimplementation!

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

In Android Monitor when the crash happens, only these lines are appended:

07-05 08:05:54.797 6171-6171/com.shoniz.saledistributemobility

  D/AndroidRuntime: Shutting down VM

07-05 08:05:54.799    6171-6171/com.shoniz.saledistributemobility
  I/art: System.exit    called, status: 2

07-05 08:05:54.799    6171-6171/com.shoniz.saledistributemobility
  I/AndroidRuntime: VM    exiting with result code 2, cleanup skipped.

07-05 08:05:54.801    1303-1576/system_process D/GraphicsStats: Buffer
  count: 3

Now my question

Is there any way to force Android Studio to stop running and show the reason of the crash to save our time?! Something like a work that Visual Studio does.