问:
Time complexity of nested 'For loop' with different conditions
Need help understanding how the second loop is executed, I know for the second loop you would multiply by the first loop. First I count the number of instructions and then apply big-oh notation. This is my approach:  would j=2*n be executed 2n times, j>=1 n+1, and j-- n times? New to big-oh notation and can't seem to find a list of 'for loops' examples anywhere, would love the feedback and information on where to practice more.</p>
```<code>acc=10 \\ 1

for(i=5;i<=n/2;i++) \\ 1+n/2+n = (1+3n/2)

   for(j=2*n; j>=1; j--) \\ (1+3n/2)(2n+n+1+n)

       acc -= j + acc++; \\ (1+1+1) (2n+n+1+n)
</code>```

答: Constant factors do not matter for asymptotic complexity. The inner loop starts at 2*n and counts down till 1. It has O(n) iterations. The outer loop starts at 5 and counts up till n/2. It has O(n) iterations. In total the nested loop is O(n*n).

Body:
Need help understanding how the second loop is executed, I know for the second loop you would multiply by the first loop. First I count the number of instructions and then apply big-oh notation. This is my approach:  would j=2*n be executed 2n times, j>=1 n+1, and j-- n times? New to big-oh notation and can't seem to find a list of 'for loops' examples anywhere, would love the feedback and information on where to practice more.

acc=10 \\ 1

for(i=5;i<=n/2;i++) \\ 1+n/2+n = (1+3n/2)

   for(j=2*n; j>=1; j--) \\ (1+3n/2)(2n+n+1+n)

       acc -= j + acc++; \\ (1+1+1) (2n+n+1+n)


来源: stackexchange

问:
Set default reviewers for merge request in GitLab
How I can set default reviewers in GitLab Premium?

In Settings → General I have only Merge request (MR) approvals, not reviewers.

答:
There is no such setting in the UI. If you think it would be useful to others, please file a feature request.

As a workaround, you could use the Merge requests API to set reviewers when creating the MR or by updating it after creation.

How you want to achieve the desired result depends on your workflow.

For example, you could have something subscribe to the project webhook which triggers a script that does the API call whenever it sees an open MR event.

Another option, if you run pipelines on MRs, you can have a job run a script which could set reviewers if none are already set via the API.

Yet another option would be to have a specified reviewer set when a merge request is created through the /assign_reviewer @user quick action by adding it to the default and all MR template files.

ContentLicense: CC BY-SA 4.0


问:
Easiest way to get column width/length from a ResultSet - must I iterate though?
I need to get the column width/length from a ResultSet/ResultSetMetaData knowing only its name? I could create a map of names to indexes and get it that way, but is there another way?

答:
You can get the columns, types and lots more from ResultSetMetaData.

You can't get the number of rows returned without iterating through it.

ContentLicense: CC BY-SA 3.0


问:
Chrome Dev Tools - Modify javascript and reload
Is it possible to modify the JavaScript of a page and then reload the page without reloading the modified JavaScript file (and thus losing modifications)?</p>

答:
In the devtools preferences check the Enable local overrides.

Go to network tab, find the file you want to edit, rigth click on it and select Save for overrides (on the sources/overrides tab you need to add a local folder)

The file appears in a new tab on the Sources tab as local copy, so you can edit this file, and after site reload the new (and edited) override file will load on the site!


问:
std::vector<std::pair<int,std::pair<Bone,std::string> > > not sorting by int?
I have a std::vector<std::pair<int,std::pair<Bone,std::string> > >

I'm trying to sort it with std sort, with the expectation that it is sorted by int, but instead I get 21 errors related to the sort call.

What could be wrong?

Code:

```<code>std::vector<std::pair<int,std::pair<Bone,std::string> > > tempBones;
std::sort(tempBones.begin(),tempBones.end());
</code>```

Errors:

Error  1   error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'const skl::Bone'  c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 102

Error  2   error C2784: 'bool std::operator <(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const _Elem *' from 'const skl::Bone'    c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 102

Error  3   error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'const skl::Bone'    c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 102

Error  4   error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const skl::Bone'    c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 102

Error  5   error C2784: 'bool std::operator <(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::vector<_Ty,_Alloc> &' from 'const skl::Bone'    c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 102

Error  6   error C2784: 'bool std::operator <(const std::list<_Ty,_Ax> &,const std::list<_Ty,_Ax> &)' : could not deduce template argument for 'const std::list<_Ty,_Ax> &' from 'const skl::Bone'   c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 102

Error  7   error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const skl::Bone' c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 102

Error  8   error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'const skl::Bone'   c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 102

Error  9   error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'const skl::Bone' c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 102

Error  10  error C2676: binary '<' : 'const skl::Bone' does not define this operator or a conversion to a type acceptable to the predefined operator   c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 10

Error  11  error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'const skl::Bone'  c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 103

Error  12  error C2784: 'bool std::operator <(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const _Elem *' from 'const skl::Bone'    c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 103

Error  13  error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'const skl::Bone'    c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 103

Error  14  error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const skl::Bone'    c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 103

Error  15  error C2784: 'bool std::operator <(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::vector<_Ty,_Alloc> &' from 'const skl::Bone'    c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 103

Error  16  error C2784: 'bool std::operator <(const std::list<_Ty,_Ax> &,const std::list<_Ty,_Ax> &)' : could not deduce template argument for 'const std::list<_Ty,_Ax> &' from 'const skl::Bone'   c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 103

Error  17  error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const skl::Bone' c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 103

Error  18  error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'const skl::Bone'   c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 103

Error  19  error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'const skl::Bone' c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 103

Error  20  error C2676: binary '<' : 'const skl::Bone' does not define this operator or a conversion to a type acceptable to the predefined operator   c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 103

答:
Why would you expect it to be sorted by int? Any container<T> is sorted by T, and sorting requires that there be a less-than comparison for T. Now for pair<S,T>, there is an operator<, defined as lexicographic ordering, but this requires comparability of both S and T:

```<code>// lexicographic compare:
bool operator<(pair<S,T> a, pair<S,T> b)
{
  return a.first < b. first || (!(b.first < a.first) && (a.second < b.second));
  //                                                    ^^^^^^^^^^^^^^^^^^^^^^^
} //                                                     compare second type!
</code>```

Moving down recursively, you need Bone to be less-than comparable. Do check, any bet that you didn't implement a comparison operator for that type!

Edit: According to your update that is precisely your problem.

Remark: You might wonder why lexicographic compare is the default: It's the only sensible comparison for which !(x < y) && !(y < x) is identical to x == y, provided the same holds for both sub-types individually.

Update: If you only want ordering by the first member, you should write a custom comparator predicate, or use a multi-map instead.

Custom comparator:

```<code>template <typename S, typename T>
bool PairCompareOnlyFirst(const std::pair<S,T> & a, const std::pair<S,T> & b)
{
  return a.first < b.first;
}

std::sort(v.begin(), v.end(), PairCompareOnlyFirst);
</code>```

Note that sort may rearrange elements with the same key. If you want those to maintain their relative ordering, use stable_sort instead.

Multimap:

```<code>typedef std::multimap<int, std::pair<Bone,std::string> > myMapType;
</code>```

The multimap is a more complicated structure than the vector, and its elements are always kept in sorted order by key value, i.e. your int. Which one is preferable depends on what you're doing.

Body:
I have a std::vector<std::pair<int,std::pair<Bone,std::string> > >

I'm trying to sort it with std sort, with the expectation that it is sorted by int, but instead I get 21 errors related to the sort call.

What could be wrong?

Code:

std::vector<std::pair<int,std::pair<Bone,std::string> > > tempBones;
std::sort(tempBones.begin(),tempBones.end());

Errors:

Error  1   error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'const skl::Bone'  c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 102

Error  2   error C2784: 'bool std::operator <(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const _Elem *' from 'const skl::Bone'    c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 102

Error  3   error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'const skl::Bone'    c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 102

Error  4   error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const skl::Bone'    c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 102

Error  5   error C2784: 'bool std::operator <(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::vector<_Ty,_Alloc> &' from 'const skl::Bone'    c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 102

Error  6   error C2784: 'bool std::operator <(const std::list<_Ty,_Ax> &,const std::list<_Ty,_Ax> &)' : could not deduce template argument for 'const std::list<_Ty,_Ax> &' from 'const skl::Bone'   c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 102

Error  7   error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const skl::Bone' c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 102

Error  8   error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'const skl::Bone'   c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 102

Error  9   error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'const skl::Bone' c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 102

Error  10  error C2676: binary '<' : 'const skl::Bone' does not define this operator or a conversion to a type acceptable to the predefined operator   c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 10

Error  11  error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'const skl::Bone'  c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 103

Error  12  error C2784: 'bool std::operator <(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const _Elem *' from 'const skl::Bone'    c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 103

Error  13  error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'const skl::Bone'    c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 103

Error  14  error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const skl::Bone'    c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 103

Error  15  error C2784: 'bool std::operator <(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::vector<_Ty,_Alloc> &' from 'const skl::Bone'    c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 103

Error  16  error C2784: 'bool std::operator <(const std::list<_Ty,_Ax> &,const std::list<_Ty,_Ax> &)' : could not deduce template argument for 'const std::list<_Ty,_Ax> &' from 'const skl::Bone'   c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 103

Error  17  error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const skl::Bone' c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 103

Error  18  error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'const skl::Bone'   c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 103

Error  19  error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'const skl::Bone' c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 103

Error  20  error C2676: binary '<' : 'const skl::Bone' does not define this operator or a conversion to a type acceptable to the predefined operator   c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 103

Body:
Why would you expect it to be sorted by int? Any container<T> is sorted by T, and sorting requires that there be a less-than comparison for T. Now for pair<S,T>, there is an operator<, defined as lexicographic ordering, but this requires comparability of both S and T:

// lexicographic compare:
bool operator<(pair<S,T> a, pair<S,T> b)
{
  return a.first < b. first || (!(b.first < a.first) && (a.second < b.second));
  //                                                    ^^^^^^^^^^^^^^^^^^^^^^^
} //                                                     compare second type!

Moving down recursively, you need Bone to be less-than comparable. Do check, any bet that you didn't implement a comparison operator for that type!

Edit: According to your update that is precisely your problem.

Remark: You might wonder why lexicographic compare is the default: It's the only sensible comparison for which !(x < y) && !(y < x) is identical to x == y, provided the same holds for both sub-types individually.

Update: If you only want ordering by the first member, you should write a custom comparator predicate, or use a multi-map instead.

Custom comparator:

template <typename S, typename T>
bool PairCompareOnlyFirst(const std::pair<S,T> & a, const std::pair<S,T> & b)
{
  return a.first < b.first;
}

std::sort(v.begin(), v.end(), PairCompareOnlyFirst);

Note that sort may rearrange elements with the same key. If you want those to maintain their relative ordering, use stable_sort instead.

Multimap:

typedef std::multimap<int, std::pair<Bone,std::string> > myMapType;

The multimap is a more complicated structure than the vector, and its elements are always kept in sorted order by key value, i.e. your int. Which one is preferable depends on what you're doing.


问:
Angular Material - Form is getting invalid, when mat datepicker model value is less than min date
I am using mat datepicker on my form. I am inserting some records. This works fine with form validation. But when the same record I am trying to update, then my form show invalid. This is due to the date is invalid.

Case when form shows invalid - When min date is greater than model value.

Why this is happening? Is this default behaviour? How to make it work?

component.html

```<code> <form class="form-container" #pointAllocationForm="ngForm" (ngSubmit)="onSubmit()">

<mat-form-field class="example-full-width">
  <div class="start-date">
        <label class="required"><strong>Start Date : </strong></label>
        <mat-form-field>
          <input matInput [matDatepicker]="startDatepicker" [min]="minDate" placeholder="Choose a date" name="StartDate"
            [(ngModel)]="myDate" #StartDate="ngModel" (ngModelChange)="setEndDate($event)" required>
          <mat-datepicker-toggle matSuffix [for]="startDatepicker"></mat-datepicker-toggle>
          <mat-datepicker #startDatepicker></mat-datepicker>
          <mat-error *ngIf="StartDate.invalid && (StartDate.dirty || StartDate.touched)">Please enter a valid date.</mat-error>
        </mat-form-field>
      </div>
</mat-form-field>
       <button class="btn btn-success" type="submit" [disabled]="pointAllocationForm.invalid">
          Update Points </button>
  </form>
</code>```

component.ts

```<code>import {Component} from '@angular/core';

@Component({
  selector: 'datepicker-min-max-example',
  templateUrl: 'datepicker-min-max-example.html',
  styleUrls: ['datepicker-min-max-example.css'],
})
export class DatepickerMinMaxExample {
  minDate = new Date();
  myDate = new Date(2018, 9, 10);
}
</code>```

答: It may be too late but if you call setErrors(null) function from FormComponent after populating your formControl it becomes valid</p>

Body:
I am using mat datepicker on my form. I am inserting some records. This works fine with form validation. But when the same record I am trying to update, then my form show invalid. This is due to the date is invalid.

Case when form shows invalid - When min date is greater than model value.

Why this is happening? Is this default behaviour? How to make it work?

component.html

 <form class="form-container" #pointAllocationForm="ngForm" (ngSubmit)="onSubmit()">

<mat-form-field class="example-full-width">

        Start Date :
        <mat-form-field>
          <input matInput [matDatepicker]="startDatepicker" [min]="minDate" placeholder="Choose a date" name="StartDate"
            [(ngModel)]="myDate" #StartDate="ngModel" (ngModelChange)="setEndDate($event)" required>
          <mat-datepicker-toggle matSuffix [for]="startDatepicker"></mat-datepicker-toggle>
          <mat-datepicker #startDatepicker></mat-datepicker>
          <mat-error *ngIf="StartDate.invalid && (StartDate.dirty || StartDate.touched)">Please enter a valid date.</mat-error>
        </mat-form-field>

</mat-form-field>
       <button class="btn btn-success" type="submit" [disabled]="pointAllocationForm.invalid">
          Update Points </button>
  </form>

component.ts

import {Component} from '@angular/core';

@Component({
  selector: 'datepicker-min-max-example',
  templateUrl: 'datepicker-min-max-example.html',
  styleUrls: ['datepicker-min-max-example.css'],
})
export class DatepickerMinMaxExample {
  minDate = new Date();
  myDate = new Date(2018, 9, 10);
}


问:
AWS. How can I control access to s3 bucket from lambda?
AWS has a large number of buckets that different users have access to. And there is a lambda function that selects data from s3 and gives it to the client via the Api Gateway. The client has the opportunity to specify in the api request from which bucket lambda should make a selection. But how to check that he is requesting exactly the bucket to which he has permission?

In the iam policies, I can only indicate that it can access a specific api resource, but the resource is shared by everyone. In lambda authorizer, I can't get information about the user's rights and permissions (or can I?).

Please tell me how you can solve this issue. Which way to move?

P.S. This should be the authorization of users in amazon, I can't give them my JWT with my data.

答:
It would be your responsibility to code the authentication and permission requirements in your own code. The person making the request via API Gateway is not an IAM User, so AWS does not recognise them and cannot grant access based on the normal AWS permission model.

Your code would need to:

Recognise and authenticate the user

Determine what resources (buckets) that user is permitted to access

Only provide access to permitted resources

How to do this is your decision. You should start with a way of identifying and authenticating the user.


问:
parse remote csv-file with PHP on GAE
I seem to be in a catch-22 with a small app I'm developing in PHP on Google App Engine using Quercus;

I have a remote csv-file which I can download & store in a string

To parse that string I'd ideally use str_getcsv, but Quercus doesn't have that function yet

Quercus does seem to know fgetcsv, but that function expects a file handle which I don't have (and I can't make a new one as GAE doesn't allow files to be created)

Anyone got an idea of how to solve this without having to dismiss the built-in PHP csv-parser functions and write my own parser instead?

答:
You might be able to create a new stream wrapper using stream_wrapper_register.

Here's an example from the manual which reads global variables: http://www.php.net/manual/en/stream.streamwrapper.example-1.php

You could then use it like a normal file handle:

```<code>$csvStr = '...';
$fp = fopen('var://csvStr', 'r+');
while ($row = fgetcsv($fp)) {
    // ...
}
fclose($fp);
</code>```

Title: parse remote csv-file with PHP on GAE

Body:
You might be able to create a new stream wrapper using stream_wrapper_register.

Here's an example from the manual which reads global variables: http://www.php.net/manual/en/stream.streamwrapper.example-1.php

You could then use it like a normal file handle:

$csvStr = '...';
$fp = fopen('var://csvStr', 'r+');
while ($row = fgetcsv($fp)) {
    // ...
}
fclose($fp);


问:
Check distance between labels when importing binary file gnu-assembler
I have the following macro to embed binary data from filename:

#define INCBIN(identifier, filename)                                                          \
    asm(".pushsection .rodata\n"                                                                   \
        "\t.local " #identifier "_begin\n"                                                         \
        "\t.type " #identifier "_begin, @object\n"                                                 \
        "\t.align 16\n" #identifier "_begin:\n"                                                    \
        "\t.incbin \"" filename "\"\n\n"                                                           \
                                                                                \
        "\t.local " #identifier "_end\n"                                                           \
        "\t.type " #identifier "_end, @object\n"                                                   \
        "\t.align 1\n" #identifier "_end:\n"                                                       \
        "\t.byte 0\n"                                                                              \
        "\t.popsection\n");                                                                        \
                                                                                \
    extern std::byte const identifier##_begin[];                                                   \
    extern std::byte const identifier##_end[]
```

I would like to be able to claim that it is an uint32_t instead of an std::byte. This would require the file to have an integer multiple of 4 bytes. Alignment is already taken care of by the macro (align to 16). Can I trigger a compile-time error if the file has a bad size? That is, add some compile-time assert to the assembly.

答:
If you just .p2align 2 after the file, it will round the total size up to a multiple of 4 bytes, regardless of how many bytes .incbin assembled to.  (Since the file started at an aligned position).

If you want to check instead of pad, that's possible at assemble time, using assembler directives.  Obviously not at compile-time proper, since that just translates C++ to asm to later be fed to the assembler.  (In GCC, those steps are actually separate, not all part of the same compiler process.  But logically still sequential in other implementations.)

The distance between two labels in the same file is an assemble-time constant that you can use in GAS expressions like (b-a)&3.

If you use .rept  -((b-a)&3), you'll have a 0 or negative repeat count, the latter of which is an assemble-time error, according to the file size % 4 being non-zero.  (Fun fact: the Linux kernel uses a trick like this in C to do static asserts by generating an array with 0 or negative size).

Or even better, .error can be controlled by .rept or other GAS directives like .ifgt.  (.rept would repeat the error message (b-a)%4 times, vs. if-greater-than-zero repeating it only once.)

```<code>.p2align 2
a:
  .incbin "bin"
  # .p2align 2
b:

.ifgt (b-a)&3         # true if remainder > 0
  .error  "asm static_assert failed: file size not a multiple of 4"
.endif
</code>```
```<code>$ echo xyz > bin   # size 4
$ gcc -c foo.s
$ echo >> bin      # size 5
$ gcc -c foo.s
foo.s: Assembler messages:
foo.s:9: Error: asm static_assert failed: file size not a multiple of 4
</code>```

Tested with GNU assembler (GNU Binutils) 2.36.1

I'll leave it up to you to stuff that back into your inline asm macro, with the filename in the .error string.

Body:
I have the following macro to embed binary data from filename:

#define INCBIN(identifier, filename)                                                          \
    asm(".pushsection .rodata\n"                                                                   \
        "\t.local " #identifier "_begin\n"                                                         \
        "\t.type " #identifier "_begin, @object\n"                                                 \
        "\t.align 16\n" #identifier "_begin:\n"                                                    \
        "\t.incbin \"" filename "\"\n\n"                                                           \
                                                                                \
        "\t.local " #identifier "_end\n"                                                           \
        "\t.type " #identifier "_end, @object\n"                                                   \
        "\t.align 1\n" #identifier "_end:\n"                                                       \
        "\t.byte 0\n"                                                                              \
        "\t.popsection\n");                                                                        \
                                                                                \
    extern std::byte const identifier##_begin[];                                                   \
    extern std::byte const identifier##_end[]

I would like to be able to claim that it is an uint32_t instead of an std::byte. This would require the file to have an integer multiple of 4 bytes. Alignment is already taken care of by the macro (align to 16). Can I trigger a compile-time error if the file has a bad size? That is, add some compile-time assert to the assembly.

Body:
If you just .p2align 2 after the file, it will round the total size up to a multiple of 4 bytes, regardless of how many bytes .incbin assembled to.  (Since the file started at an aligned position).

If you want to check instead of pad, that's possible at assemble time, using assembler directives.  Obviously not at compile-time proper, since that just translates C++ to asm to later be fed to the assembler.  (In GCC, those steps are actually separate, not all part of the same compiler process.  But logically still sequential in other implementations.)

The distance between two labels in the same file is an assemble-time constant that you can use in GAS expressions like (b-a)&3.

If you use .rept  -((b-a)&3), you'll have a 0 or negative repeat count, the latter of which is an assemble-time error, according to the file size % 4 being non-zero.  (Fun fact: the Linux kernel uses a trick like this in C to do static asserts by generating an array with 0 or negative size).

Or even better, .error can be controlled by .rept or other GAS directives like .ifgt.  (.rept would repeat the error message (b-a)%4 times, vs. if-greater-than-zero repeating it only once.)

.p2align 2
a:
  .incbin "bin"
  # .p2align 2
b:

.ifgt (b-a)&3         # true if remainder > 0
  .error  "asm static_assert failed: file size not a multiple of 4"
.endif

$ echo xyz > bin   # size 4
$ gcc -c foo.s
$ echo >> bin      # size 5
$ gcc -c foo.s
foo.s: Assembler messages:
foo.s:9: Error: asm static_assert failed: file size not a multiple of 4

Tested with GNU assembler (GNU Binutils) 2.36.1

I'll leave it up to you to stuff that back into your inline asm macro, with the filename in the .error string.


问:
Is there a way to change avatar in telegram bot using python?
I was trying to find some documentation, but i`ve failed. I would be grateful if you send me doc or example how to do it.</p>

答:
In order to change your profile photo you should use MTPROTOAPI. One of the good libraries written in Python is called telethon. You can check here on how to update profile information.

install telethon:

```<code>python3 -m pip install --upgrade telethon
</code>```

update profile photo:

import asyncio
from telethon import TelegramClient
from telethon.tl.functions.photos import UploadProfilePhotoRequest

# Use your own values from my.telegram.org
api_id = 12345
api_hash = '0123456789abcdef0123456789abcdef'

client = await TelegramClient('anon', api_id, api_hash)

await client(UploadProfilePhotoRequest(
    await client.upload_file('/path/to/some/file')
))
```

Body:
In order to change your profile photo you should use MTPROTOAPI. One of the good libraries written in Python is called telethon. You can check here on how to update profile information.

install telethon:

python3 -m pip install --upgrade telethon

update profile photo:

import asyncio
from telethon import TelegramClient
from telethon.tl.functions.photos import UploadProfilePhotoRequest

# Use your own values from my.telegram.org
api_id = 12345
api_hash = '0123456789abcdef0123456789abcdef'

client = await TelegramClient('anon', api_id, api_hash)

await client(UploadProfilePhotoRequest(
    await client.upload_file('/path/to/some/file')
))


问:
SQL - safely downcast BIGINT to INT
I have a CSV I'm importing into our database. One of the "columns" contains data that should be an INT but some rows have numbers that only fall in the BIGINT range (because they're test data from one of our partners). We store INT internally and have no desire to change.

I want to safely downcast from BIGINT to INT. By safely, I mean no errors should be raised if an arithmetic overflow happens. If the cast/conversion succeeds, I want my script to go on. If it fails, I want it to short-circuit. I can't seem to figure out the proper syntax. This is what I've got:

```<code>DECLARE @UserIDBigInt BIGINT = 9723021913; -- actually provided by query param
--Setting within the INT range successfully converts
--SET @UserIDBigInt = 5;
DECLARE @UserID INT = CONVERT(INT, @UserIDBigInt);
--DECLARE @UserID INT = CAST(@UserIDBigInt AS INT);
SELECT @UserIDBigInt
SELECT @UserID
IF @UserID IS NOT NULL BEGIN
    SELECT 'Handle it as reliable data'
END
</code>```

I've thought about comparing @UserIDBigInt to the valid range of an INT (-2^31 (-2,147,483,648) to 2^31-1 (2,147,483,647)), but I really don't like that approach. That's my fallback. I was hoping for some language constructs or built-in functions I could use. If I absolutely have to compare to the valid range, are there at least some built-in constants (like C#'s int.MinValue & int.MaxValue)?

EDIT: Corrected typo.

答: You could also convert the value to a string, trim it to length and convert to int. not the best way, but a safe easy way for sure</p>

Title: SQL - safely downcast BIGINT to INT

Body:
I have a CSV I'm importing into our database. One of the "columns" contains data that should be an INT but some rows have numbers that only fall in the BIGINT range (because they're test data from one of our partners). We store INT internally and have no desire to change.

I want to safely downcast from BIGINT to INT. By safely, I mean no errors should be raised if an arithmetic overflow happens. If the cast/conversion succeeds, I want my script to go on. If it fails, I want it to short-circuit. I can't seem to figure out the proper syntax. This is what I've got:

DECLARE @UserIDBigInt BIGINT = 9723021913; -- actually provided by query param
--Setting within the INT range successfully converts
--SET @UserIDBigInt = 5;
DECLARE @UserID INT = CONVERT(INT, @UserIDBigInt);
--DECLARE @UserID INT = CAST(@UserIDBigInt AS INT);
SELECT @UserIDBigInt
SELECT @UserID
IF @UserID IS NOT NULL BEGIN
    SELECT 'Handle it as reliable data'
END

I've thought about comparing @UserIDBigInt to the valid range of an INT (-2^31 (-2,147,483,648) to 2^31-1 (2,147,483,647)), but I really don't like that approach. That's my fallback. I was hoping for some language constructs or built-in functions I could use. If I absolutely have to compare to the valid range, are there at least some built-in constants (like C#'s int.MinValue & int.MaxValue)?

EDIT: Corrected typo.


问:
Recursive function to add path to nested object using parent object values
I'm trying to add a "path" to all nested objects using their ancestor's attributes. Essentially I want to represent a "path" of the hierarchical structure to a nested object using a concatenation of its parent object's attribute values instead of using keys (like I could with dot notation, lodash, etc).

What I've tried:

```<code>interface SimpleObject {
    name : string
    slug : string
    slug_path? : string
    fields? : SimpleObject[]
}

const addNestedObjSlug = (data:SimpleObject[], parents:SimpleObject[] = []) => {

    data.map((obj) => {
        obj.slug_path = parents.length > 0 ? `${parents.map(({slug}) => slug).join('.')}.${obj.slug}` : obj.slug
        if(obj.fields && obj.fields.length > 0) {
            parents.push(obj)

            // I think the issue is here, that I probably need to set parents to an empty array at some point
            return addNestedObjSlug(obj.fields, parents)
        }
    })

    return data
}

const desiredResult = addNestedObjSlug([
    {
        name : 'Item 1',
        slug : 'i1',
        fields : [
            {
                name : 'Item 1 - 1',
                slug : 'i1-1'
            },
            {
                name : 'Item 1 - 2',
                slug : 'i1-2'
            },
            {
                name : 'Item 1 - 3',
                slug : 'i1-3'
            }
        ]
    },
    {
        name : 'Item 2',
        slug : 'i2',
        fields : [
            {
                name : 'Item 2 - 1',
                slug : 'i2-1',
                fields : [
                    {
                        name : 'Item 2 - 1 - 1',
                        slug : 'i2-1-1'
                    }
                ]
            }
        ]
    }
])
</code>```

My expected result is:

```<code>[
    {
        "name": "Item 1",
        "slug": "i1",
        "fields": [
            {
                "name": "Item 1 - 1",
                "slug": "i1-1",
                "slug_path": "i1.i1-1"
            },
            {
                "name": "Item 1 - 2",
                "slug": "i1-2",
                "slug_path": "i1.i1-2"
            },
            {
                "name": "Item 1 - 3",
                "slug": "i1-3",
                "slug_path": "i1.i1-3"
            }
        ],
        "slug_path": "i1"
    },
    {
        "name": "Item 2",
        "slug": "i2",
        "fields": [
            {
                "name": "Item 2 - 1",
                "slug": "i2-1",
                "fields": [
                    {
                        "name": "Item 2 - 1 - 1",
                        "slug": "i2-1-1",
                        "slug_path": "i2.i2-1.i2-1-1"
                    }
                ],
                "slug_path": "i2.i2-1"
            }
        ],
        "slug_path": "i2"
    }
]
</code>```

But instead I get the following, where the original nested object's slug is part of the new slug_path attribute of objects which are not ancestors.

```<code>[
    {
        "name": "Item 1",
        "slug": "i1",
        "fields": [
            {
                "name": "Item 1 - 1",
                "slug": "i1-1",
                "slug_path": "i1.i1-1" // correct
            },
            {
                "name": "Item 1 - 2",
                "slug": "i1-2",
                "slug_path": "i1.i1-2" // correct
            },
            {
                "name": "Item 1 - 3",
                "slug": "i1-3",
                "slug_path": "i1.i1-3" // correct
            }
        ],
        "slug_path": "i1" // correct
    },
    {
        "name": "Item 2",
        "slug": "i2",
        "fields": [
            {
                "name": "Item 2 - 1",
                "slug": "i2-1",
                "fields": [
                    {
                        "name": "Item 2 - 1 - 1",
                        "slug": "i2-1-1",
                        "slug_path": "i1.i2.i2-1.i2-1-1" // incorrect
                    }
                ],
                "slug_path": "i1.i2.i2-1" // incorrect
            }
        ],
        "slug_path": "i1.i2" // incorrect
    }
]
</code>```

答:
You could take a closure over the path.

const
    add = p => o => {
        const
            slug_path = p + (p && '.') + o.slug,
            fields = (o.fields || []).map(add(slug_path));

        return { ...o, slug_path, ...(fields.length ? { fields } : {}) };
    },
    addNestedObjSlug = array => array.map(add(''));

console.log(addNestedObjSlug([{ name: 'Item 1', slug: 'i1', fields: [{ name: 'Item 1 - 1', slug: 'i1-1' }, { name: 'Item 1 - 2', slug: 'i1-2' }, { name: 'Item 1 - 3', slug: 'i1-3' }] }, { name: 'Item 2', slug: 'i2', fields: [{ name: 'Item 2 - 1', slug: 'i2-1', fields: [{ name: 'Item 2 - 1 - 1', slug: 'i2-1-1' }] }] }]));```
<pre class="snippet-code-css lang-css prettyprint-override"><code>.as-console-wrapper { max-height: 100% !important; top: 0; }</code>```

Body:
I'm trying to add a "path" to all nested objects using their ancestor's attributes. Essentially I want to represent a "path" of the hierarchical structure to a nested object using a concatenation of its parent object's attribute values instead of using keys (like I could with dot notation, lodash, etc).

What I've tried:

interface SimpleObject {
    name : string
    slug : string
    slug_path? : string
    fields? : SimpleObject[]
}

const addNestedObjSlug = (data:SimpleObject[], parents:SimpleObject[] = []) => {

    data.map((obj) => {
        obj.slug_path = parents.length > 0 ? `${parents.map(({slug}) => slug).join('.')}.${obj.slug}` : obj.slug
        if(obj.fields && obj.fields.length > 0) {
            parents.push(obj)

            // I think the issue is here, that I probably need to set parents to an empty array at some point
            return addNestedObjSlug(obj.fields, parents)
        }
    })

    return data
}

const desiredResult = addNestedObjSlug([
    {
        name : 'Item 1',
        slug : 'i1',
        fields : [
            {
                name : 'Item 1 - 1',
                slug : 'i1-1'
            },
            {
                name : 'Item 1 - 2',
                slug : 'i1-2'
            },
            {
                name : 'Item 1 - 3',
                slug : 'i1-3'
            }
        ]
    },
    {
        name : 'Item 2',
        slug : 'i2',
        fields : [
            {
                name : 'Item 2 - 1',
                slug : 'i2-1',
                fields : [
                    {
                        name : 'Item 2 - 1 - 1',
                        slug : 'i2-1-1'
                    }
                ]
            }
        ]
    }
])

My expected result is:

[
    {
        "name": "Item 1",
        "slug": "i1",
        "fields": [
            {
                "name": "Item 1 - 1",
                "slug": "i1-1",
                "slug_path": "i1.i1-1"
            },
            {
                "name": "Item 1 - 2",
                "slug": "i1-2",
                "slug_path": "i1.i1-2"
            },
            {
                "name": "Item 1 - 3",
                "slug": "i1-3",
                "slug_path": "i1.i1-3"
            }
        ],
        "slug_path": "i1"
    },
    {
        "name": "Item 2",
        "slug": "i2",
        "fields": [
            {
                "name": "Item 2 - 1",
                "slug": "i2-1",
                "fields": [
                    {
                        "name": "Item 2 - 1 - 1",
                        "slug": "i2-1-1",
                        "slug_path": "i2.i2-1.i2-1-1"
                    }
                ],
                "slug_path": "i2.i2-1"
            }
        ],
        "slug_path": "i2"
    }
]

But instead I get the following, where the original nested object's slug is part of the new slug_path attribute of objects which are not ancestors.

[
    {
        "name": "Item 1",
        "slug": "i1",
        "fields": [
            {
                "name": "Item 1 - 1",
                "slug": "i1-1",
                "slug_path": "i1.i1-1" // correct
            },
            {
                "name": "Item 1 - 2",
                "slug": "i1-2",
                "slug_path": "i1.i1-2" // correct
            },
            {
                "name": "Item 1 - 3",
                "slug": "i1-3",
                "slug_path": "i1.i1-3" // correct
            }
        ],
        "slug_path": "i1" // correct
    },
    {
        "name": "Item 2",
        "slug": "i2",
        "fields": [
            {
                "name": "Item 2 - 1",
                "slug": "i2-1",
                "fields": [
                    {
                        "name": "Item 2 - 1 - 1",
                        "slug": "i2-1-1",
                        "slug_path": "i1.i2.i2-1.i2-1-1" // incorrect
                    }
                ],
                "slug_path": "i1.i2.i2-1" // incorrect
            }
        ],
        "slug_path": "i1.i2" // incorrect
    }
]

Body:
You could take a closure over the path.

const
    add = p => o => {
        const
            slug_path = p + (p && '.') + o.slug,
            fields = (o.fields || []).map(add(slug_path));

        return { ...o, slug_path, ...(fields.length ? { fields } : {}) };
    },
    addNestedObjSlug = array => array.map(add(''));

console.log(addNestedObjSlug([{ name: 'Item 1', slug: 'i1', fields: [{ name: 'Item 1 - 1', slug: 'i1-1' }, { name: 'Item 1 - 2', slug: 'i1-2' }, { name: 'Item 1 - 3', slug: 'i1-3' }] }, { name: 'Item 2', slug: 'i2', fields: [{ name: 'Item 2 - 1', slug: 'i2-1', fields: [{ name: 'Item 2 - 1 - 1', slug: 'i2-1-1' }] }] }]));

.as-console-wrapper { max-height: 100% !important; top: 0; }


问:
Adding custom style to a Wiki page using MarkDown in Gitlab
I’m trying to reproduce this example https://about.gitlab.com/handbook/product/technical-writing/markdown-guide/#styles 95

I’m writing this block in the content of the page

```<code><style>
.purple {
  color:inherit;
}

.purple:hover {
  color:rgb(107,79,187);
}
</style>
</code>```

Hey! Hover the cursor over me and guess what?

```<code>{: .purple}
</code>```

But when I go to the content preview it doesn’t work.

What I’m doing wrong?

Thank you.

答:
The page that you linked to is about how markdown works for GitLab's handbook which is built using a Static Site Generator that will interpret those style tags.

For any markdown styling within the GitLab product, including the wiki, you want to refer to the GitLab Flavored Markdown documentation.

Body:
I’m trying to reproduce this example https://about.gitlab.com/handbook/product/technical-writing/markdown-guide/#styles 95

I’m writing this block in the content of the page

Hey! Hover the cursor over me and guess what?

{: .purple}

But when I go to the content preview it doesn’t work.

What I’m doing wrong?

Thank you.