问:
allowedLateness on Global Window custom trigger
I have created a custom trigger and processing function for my event stream.

```<code>DataStream<DynamoDBRow> dynamoDBRows =
    sensorEvents
        .keyBy("id")
        .window(GlobalWindows.create())
        .trigger(new MyCustomTrigger())
        .allowedLateness(Time.minutes(1)) # Note
        .process(new MyCustomWindowProcessFunction());
</code>```

My trigger is based on an event parameter. Once the event-end signal is received, the MyCustomWindowProcessFunction() is applied on the window elements.

```<code>@Slf4j
public class MyCustomTrigger extends Trigger<SensorEvent, GlobalWindow> {

  @Override
  public TriggerResult onElement(SensorEvent element, long timestamp, GlobalWindow window, TriggerContext ctx) throws Exception {

    if (element.isEventEnd() == true) {
      return TriggerResult.FIRE_AND_PURGE;
    }

    return TriggerResult.CONTINUE;
  }

  @Override
  public TriggerResult onProcessingTime(long time, GlobalWindow window, TriggerContext ctx) throws Exception {
    return TriggerResult.CONTINUE;
  }

  @Override
  public TriggerResult onEventTime(long time, GlobalWindow window, TriggerContext ctx) throws Exception {
    return TriggerResult.CONTINUE;
  }

  @Override
  public void clear(GlobalWindow window, TriggerContext ctx) throws Exception {}
}
</code>```

There could be few sensor data, that could come even after the Trigger. So I added .allowedLateness(Time.minutes(1)), to ensure that, those events are not missed out, while processing.

In my case, allowedLateness is not working.

After going through the documents, I found this

How can I include allowedLateness in GlobalWindow?

Note: I also tried setting environment Time Characteristic

```<code>env.setStreamTimeCharacteristic(TimeCharacteristic.IngestionTime);
</code>```

Update: 20-02-2020

Currently thinking of the below approach. (Not working so far)

```<code>@Slf4j
public class JourneyTrigger extends Trigger<SensorEvent, GlobalWindow> {

  private final long allowedLatenessMillis;

  public JourneyTrigger(Time allowedLateness) {
    this.allowedLatenessMillis = allowedLateness.toMilliseconds();
  }

  @Override
  public TriggerResult onElement(SensorEvent element, long timestamp, GlobalWindow window, TriggerContext ctx) throws Exception {

    if (element.isEventEnd() == true) {
      log.info("Timer started with allowedLatenessMillis " + allowedLatenessMillis);
      ctx.registerEventTimeTimer(System.currentTimeMillis() + allowedLatenessMillis);
    }

    return TriggerResult.CONTINUE;
  }

  @Override
  public TriggerResult onEventTime(long time, GlobalWindow window, TriggerContext ctx) throws Exception {
    log.info("onEvenTime called at "+System.currentTimeMillis() );
    return TriggerResult.FIRE_AND_PURGE;
  }

  @Override
  public TriggerResult onProcessingTime(long time, GlobalWindow window, TriggerContext ctx) throws Exception {
    return TriggerResult.CONTINUE;
  }

  @Override
  public void clear(GlobalWindow window, TriggerContext ctx) throws Exception {}
}
</code>```

答:
Finally, I was able to achieve my requirement using the below custom trigger.

```<code>import lombok.extern.slf4j.Slf4j;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.triggers.Trigger;
import org.apache.flink.streaming.api.windowing.triggers.TriggerResult;
import org.apache.flink.streaming.api.windowing.windows.GlobalWindow;

@Slf4j
public class JourneyTrigger extends Trigger<SensorEvent, GlobalWindow> {

  private final long allowedLatenessMillis;

  public JourneyTrigger(Time allowedLateness) {
    this.allowedLatenessMillis = allowedLateness.toMilliseconds();
  }

  @Override
  public TriggerResult onElement(SensorEvent element, long timestamp, GlobalWindow window, TriggerContext ctx) throws Exception {

    if (element.isEventEnd()==true) {
      log.info("Timer started with allowedLatenessMillis " + allowedLatenessMillis);
      ctx.registerProcessingTimeTimer(System.currentTimeMillis() + allowedLatenessMillis);
    }

    return TriggerResult.CONTINUE;
  }

  @Override
  public TriggerResult onProcessingTime(long time, GlobalWindow window, TriggerContext ctx) throws Exception {
    log.info("onProcessingTime called at "+System.currentTimeMillis() );
    return TriggerResult.FIRE_AND_PURGE;
  }

  @Override
  public TriggerResult onEventTime(long time, GlobalWindow window, TriggerContext ctx) throws Exception {
    return TriggerResult.CONTINUE;
  }

  @Override
  public void clear(GlobalWindow window, TriggerContext ctx) throws Exception {}
}
</code>```

Also in the Driver.java class, set environment Time Characteristic

```<code>env.setStreamTimeCharacteristic(TimeCharacteristic.ProcessingTime);
</code>```

Body:
I have created a custom trigger and processing function for my event stream.

DataStream<DynamoDBRow> dynamoDBRows =
    sensorEvents
        .keyBy("id")
        .window(GlobalWindows.create())
        .trigger(new MyCustomTrigger())
        .allowedLateness(Time.minutes(1)) # Note
        .process(new MyCustomWindowProcessFunction());

My trigger is based on an event parameter. Once the event-end signal is received, the MyCustomWindowProcessFunction() is applied on the window elements.

@Slf4j
public class MyCustomTrigger extends Trigger<SensorEvent, GlobalWindow> {

  @Override
  public TriggerResult onElement(SensorEvent element, long timestamp, GlobalWindow window, TriggerContext ctx) throws Exception {

    if (element.isEventEnd() == true) {
      return TriggerResult.FIRE_AND_PURGE;
    }

    return TriggerResult.CONTINUE;
  }

  @Override
  public TriggerResult onProcessingTime(long time, GlobalWindow window, TriggerContext ctx) throws Exception {
    return TriggerResult.CONTINUE;
  }

  @Override
  public TriggerResult onEventTime(long time, GlobalWindow window, TriggerContext ctx) throws Exception {
    return TriggerResult.CONTINUE;
  }

  @Override
  public void clear(GlobalWindow window, TriggerContext ctx) throws Exception {}
}

There could be few sensor data, that could come even after the Trigger. So I added .allowedLateness(Time.minutes(1)), to ensure that, those events are not missed out, while processing.

In my case, allowedLateness is not working.

After going through the documents, I found this

How can I include allowedLateness in GlobalWindow?

Note: I also tried setting environment Time Characteristic

env.setStreamTimeCharacteristic(TimeCharacteristic.IngestionTime);

Update: 20-02-2020

Currently thinking of the below approach. (Not working so far)

@Slf4j
public class JourneyTrigger extends Trigger<SensorEvent, GlobalWindow> {

  private final long allowedLatenessMillis;

  public JourneyTrigger(Time allowedLateness) {
    this.allowedLatenessMillis = allowedLateness.toMilliseconds();
  }

  @Override
  public TriggerResult onElement(SensorEvent element, long timestamp, GlobalWindow window, TriggerContext ctx) throws Exception {

    if (element.isEventEnd() == true) {
      log.info("Timer started with allowedLatenessMillis " + allowedLatenessMillis);
      ctx.registerEventTimeTimer(System.currentTimeMillis() + allowedLatenessMillis);
    }

    return TriggerResult.CONTINUE;
  }

  @Override
  public TriggerResult onEventTime(long time, GlobalWindow window, TriggerContext ctx) throws Exception {
    log.info("onEvenTime called at "+System.currentTimeMillis() );
    return TriggerResult.FIRE_AND_PURGE;
  }

  @Override
  public TriggerResult onProcessingTime(long time, GlobalWindow window, TriggerContext ctx) throws Exception {
    return TriggerResult.CONTINUE;
  }

  @Override
  public void clear(GlobalWindow window, TriggerContext ctx) throws Exception {}
}

Body:
Finally, I was able to achieve my requirement using the below custom trigger.

import lombok.extern.slf4j.Slf4j;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.triggers.Trigger;
import org.apache.flink.streaming.api.windowing.triggers.TriggerResult;
import org.apache.flink.streaming.api.windowing.windows.GlobalWindow;

@Slf4j
public class JourneyTrigger extends Trigger<SensorEvent, GlobalWindow> {

  private final long allowedLatenessMillis;

  public JourneyTrigger(Time allowedLateness) {
    this.allowedLatenessMillis = allowedLateness.toMilliseconds();
  }

  @Override
  public TriggerResult onElement(SensorEvent element, long timestamp, GlobalWindow window, TriggerContext ctx) throws Exception {

    if (element.isEventEnd()==true) {
      log.info("Timer started with allowedLatenessMillis " + allowedLatenessMillis);
      ctx.registerProcessingTimeTimer(System.currentTimeMillis() + allowedLatenessMillis);
    }

    return TriggerResult.CONTINUE;
  }

  @Override
  public TriggerResult onProcessingTime(long time, GlobalWindow window, TriggerContext ctx) throws Exception {
    log.info("onProcessingTime called at "+System.currentTimeMillis() );
    return TriggerResult.FIRE_AND_PURGE;
  }

  @Override
  public TriggerResult onEventTime(long time, GlobalWindow window, TriggerContext ctx) throws Exception {
    return TriggerResult.CONTINUE;
  }

  @Override
  public void clear(GlobalWindow window, TriggerContext ctx) throws Exception {}
}

Also in the Driver.java class, set environment Time Characteristic

env.setStreamTimeCharacteristic(TimeCharacteristic.ProcessingTime);


来源: stackexchange

问:
Xcode 11.2.1 Error - Module compiled with Swift 5.1 cannot be imported by the Swift 5.1.2 compiler
After updating to Xcode 11.2.1, I cannot import AudioKit without getting this error:
"Module compiled with Swift 5.1 cannot be imported by the Swift 5.1.2 compiler".
I've said yes to "Build libraries for distribution" but still getting the same error. Is there a way of fixing this error? Thanks!</p>

答: The library (not the app) needs to set the build setting "Build libraries for distribution" so that it exports the necessary interface file to enable linkage across different Swift versions.</p>

ContentLicense: CC BY-SA 4.0


问:
Multiple form submits with enterkey
I have an angular form, when the focus is on submit button and user press and hold submit button it submits the form multiple times.
I can disable the button on submit, but i have multiple forms
Is there a best way to handle this globally using angular or html.</p>

答:
It would be useful to apply for debouncing. You can use it easily in js. If you use lodash library it is even easier. Some helpful links;

https://css-tricks.com/debouncing-throttling-explained-examples/

https://lodash.com/docs/4.17.15#debounce

On the other hand, there is a similar issue in the following link and it can help you to think in another way.
Preventing multiple clicks on button

Title: Multiple form submits with enterkey


问:
Facing a difficulty in a bubble sort algorithm in C
First of all I am beginner in C, so I am sorry if my question seems stupid.
I was learning how to use the bubble sort algorithm in C, and I came through this code :

```<code>#include <stdio.h>

int main() {
    int ctr, inner, outer, didSwap, temp;
    int nums[10] = {
        78,
        16,
        21,
        7,
        13,
        9,
        22,
        52,
        67,
        19
    };

    //Listing the array before sorting

    for (ctr = 0; ctr < 10; ctr++) {
        printf("%d\n", nums[ctr]);
    }

    //Sorting the arrays

    for (outer = 0; outer < 9; outer++) {
        didSwap = 0;
        for (inner = outer; inner < 10; inner++) {
            if (nums[inner] < nums[outer]) {
                temp = nums[inner];
                nums[inner] = nums[outer];
                nums[outer] = temp;
                didSwap = 1;
            }
        }
        if (didSwap == 0) {
            break;
        }
    }

    //Listing the array after sorting

    printf("\n\nThis is the sorted array\n");
    for (ctr = 0; ctr < 10; ctr++) {
        printf("%d\n", nums[ctr]);
    }

    return 0;
}
</code>```

The code works fine, but what I want to understand is how in the second for loop it is written inner = outer, and in the next if statement it is comparing the elements of the array were one of them has the same number as inner and the other has the same number as outer.
And since we said that inner = outer, that means that we are comparing the same element.
The way I think about it is, if outer = 0, and since inner = outer, then inner will be 0 too, so the next if statement will be if (nums[0] < nums[0]) and that doesn't make any sense.

I know that I am probably wrong about it because the code works fine, but what did I think wrong?

Thanks in advance.

答:
Indeed the initialisation of inner in the inner loop could be changed to inner = outer + 1 as the test will always be false in the first iteration.

Note however that if the first element is also the smallest, all tests in this inner loop will be false and didSwap will not be set to 1, causing the outer loop to stop too. This is incorrect if the rest of the array is not sorted.

You can fix this problem by changing the loop to:

```<code>for (outer = 0; outer < 9; outer++) {
    for (inner = outer + 1; inner < 10; inner++) {
        if (nums[inner] < nums[outer]) {
            temp = nums[inner];
            nums[inner] = nums[outer];
            nums[outer] = temp;
        }
    }
}
</code>```

Which will perform 45 iterations in all cases.

To improve the best case performance, you must slightly change the algorithm to only swap adjacent items:

```<code>for (outer = 10; outer-- > 0; ) {
    didSwap = 0;
    for (inner = 0; inner < outer; inner++) {
        if (nums[inner] > nums[inner + 1]) {
            temp = nums[inner];
            nums[inner] = nums[inner + 1];
            nums[inner + 1] = temp;
            didSwap = 1;
        }
    }
    if (didSwap == 0) {
        break;
    }
}
</code>```

This will run in linear time if the array is already sorted, but still take O(N2) iterations on average.

Body:
First of all I am beginner in C, so I am sorry if my question seems stupid.
I was learning how to use the bubble sort algorithm in C, and I came through this code :

#include <stdio.h>

int main() {
    int ctr, inner, outer, didSwap, temp;
    int nums[10] = {
        78,
        16,
        21,
        7,
        13,
        9,
        22,
        52,
        67,
        19
    };

    //Listing the array before sorting

    for (ctr = 0; ctr < 10; ctr++) {
        printf("%d\n", nums[ctr]);
    }

    //Sorting the arrays

    for (outer = 0; outer < 9; outer++) {
        didSwap = 0;
        for (inner = outer; inner < 10; inner++) {
            if (nums[inner] < nums[outer]) {
                temp = nums[inner];
                nums[inner] = nums[outer];
                nums[outer] = temp;
                didSwap = 1;
            }
        }
        if (didSwap == 0) {
            break;
        }
    }

    //Listing the array after sorting

    printf("\n\nThis is the sorted array\n");
    for (ctr = 0; ctr < 10; ctr++) {
        printf("%d\n", nums[ctr]);
    }

    return 0;
}

The code works fine, but what I want to understand is how in the second for loop it is written inner = outer, and in the next if statement it is comparing the elements of the array were one of them has the same number as inner and the other has the same number as outer.
And since we said that inner = outer, that means that we are comparing the same element.
The way I think about it is, if outer = 0, and since inner = outer, then inner will be 0 too, so the next if statement will be if (nums[0] < nums[0]) and that doesn't make any sense.

I know that I am probably wrong about it because the code works fine, but what did I think wrong?

Thanks in advance.

Body:
Indeed the initialisation of inner in the inner loop could be changed to inner = outer + 1 as the test will always be false in the first iteration.

Note however that if the first element is also the smallest, all tests in this inner loop will be false and didSwap will not be set to 1, causing the outer loop to stop too. This is incorrect if the rest of the array is not sorted.

You can fix this problem by changing the loop to:

for (outer = 0; outer < 9; outer++) {
    for (inner = outer + 1; inner < 10; inner++) {
        if (nums[inner] < nums[outer]) {
            temp = nums[inner];
            nums[inner] = nums[outer];
            nums[outer] = temp;
        }
    }
}

Which will perform 45 iterations in all cases.

To improve the best case performance, you must slightly change the algorithm to only swap adjacent items:

for (outer = 10; outer-- > 0; ) {
    didSwap = 0;
    for (inner = 0; inner < outer; inner++) {
        if (nums[inner] > nums[inner + 1]) {
            temp = nums[inner];
            nums[inner] = nums[inner + 1];
            nums[inner + 1] = temp;
            didSwap = 1;
        }
    }
    if (didSwap == 0) {
        break;
    }
}

This will run in linear time if the array is already sorted, but still take O(N2) iterations on average.


问:
Using vue-underscore in Nuxt.js
I’m currently reworking an application that I was making in VueJS for Nuxt, however I get an error missing stack frames that I can’t find much about – I’m trying to use vue-underscore and while previously in VueJS this worked fine, I’m now having this  missing stack frames error message appear.

In a vue-underscore.js file I setup underscore like this:

```<code>import Vue from 'vue'
import underscore from 'vue-underscore'

Vue.use(underscore)
</code>```

I import {_} like this in my component:

```<code>import {_} from 'vue-underscore';
</code>```

Error message here:

```<code>"use strict";
    __webpack_require__.r(__webpack_exports__);
    /* harmony import */ var vue__WEBPACK_IMPORTED_MODULE_0__ =
    __webpack_require__(/*! vue */ "vue");
    /* harmony import */ var vue__WEBPACK_IMPORTED_MODULE_0___default =
    /*#__PURE__*/__webpack_require__.n(vue__WEBPACK_IMPORTED_MODULE_0__);
    /* harmony import */ var vue_underscore__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! vue-underscore */ "vue-underscore");
/* harmony import */ var vue_underscore__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(vue_underscore__WEBPACK_IMPORTED_MODULE_1__);
 vue__WEBPACK_IMPORTED_MODULE_0___default.a.use(vue_underscore__WEBPACK_IMPORTED_MODULE_1___default.a);
</code>```

Any help gratefully received.

答:
I had the same problem. We both were badly using it!

Let's summarize.

Add the repo

npm install vue-underscore

OR

yarn add vue-underscore

Create a file named plugins/underscore.js

```<code>import Vue from 'vue'
import underscore from 'vue-underscore'
Vue.use(underscore)
</code>```

Add it to the nuxtconfig.js file

plugins: [{ src: '~/plugins/underscore', ssr: false }]

Use it

```<code><script>
export default {
  name: 'MyComponent',
  mounted() {
    console.log(this.$_.range(2000, 2020))
  }
}
</script>
</code>```

Title: Using vue-underscore in Nuxt.js

Body:
I’m currently reworking an application that I was making in VueJS for Nuxt, however I get an error missing stack frames that I can’t find much about – I’m trying to use vue-underscore and while previously in VueJS this worked fine, I’m now having this  missing stack frames error message appear.

In a vue-underscore.js file I setup underscore like this:

import Vue from 'vue'
import underscore from 'vue-underscore'

Vue.use(underscore)

I import {_} like this in my component:

import {_} from 'vue-underscore';

Error message here:

"use strict";
    __webpack_require__.r(__webpack_exports__);
    /* harmony import */ var vue__WEBPACK_IMPORTED_MODULE_0__ =
    __webpack_require__(/*! vue */ "vue");
    /* harmony import */ var vue__WEBPACK_IMPORTED_MODULE_0___default =
    /*#__PURE__*/__webpack_require__.n(vue__WEBPACK_IMPORTED_MODULE_0__);
    /* harmony import */ var vue_underscore__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! vue-underscore */ "vue-underscore");
/* harmony import */ var vue_underscore__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(vue_underscore__WEBPACK_IMPORTED_MODULE_1__);
 vue__WEBPACK_IMPORTED_MODULE_0___default.a.use(vue_underscore__WEBPACK_IMPORTED_MODULE_1___default.a);

Any help gratefully received.

Body:
I had the same problem. We both were badly using it!

Let's summarize.

Add the repo

npm install vue-underscore

OR

yarn add vue-underscore

Create a file named plugins/underscore.js

import Vue from 'vue'
import underscore from 'vue-underscore'
Vue.use(underscore)

Add it to the nuxtconfig.js file

plugins: [{ src: '~/plugins/underscore', ssr: false }]

Use it

<script>
export default {
  name: 'MyComponent',
  mounted() {
    console.log(this.$_.range(2000, 2020))
  }
}
</script>


问:
ValueError Django CSV
All I'm trying to do is through Django display my CSV table on the page.
And everything seems to work, but for some reason I get an error and I can’t find a solution to it

```<code>ValueError: The view csv_app.views.csv_simple_read didn't return an HttpResponse object. It returned None instead.
[19/Feb/2020 21:40:07] "GET / HTTP/1.1" 500 57986
</code>```

I'm new to django. I would be very grateful for any help.

Views.py

```<code>import csv
import os

def csv_simple_read(request):
 path = os.path.dirname(__file__)
 file = os.path.join(path, 'csv_simple_read.csv')

 with open(file) as csv_file:
  csv_reader = csv.reader(csv_file, delimiter=';')
  line_count = 0
  for row in csv_reader:
    print('\n\nColumn names are {}, {}, {}, {}'.format(row[0], row[1], row[2], row[3]))
    line_count += 1
</code>```

csv_simple_read.csv

```<code>test3;2020-02-16;05:22:49;OK
test2;2020-02-16;05:22:25;OK
test1;2020-02-16;05:22:10;OK
test3;2020-02-16;05:22:49;OK
test2;2020-02-16;05:22:25;OK
test1;2020-02-16;05:22:10;OK
</code>```

csv_home.html

```<code> <body>
 <h3>CSV Example - Read Write Examples</h3>
 <ul>
 <br>
 <li>Read Operation
 <ul>
 <li>
 <a href="{% url 'csv_simple_read' %}">Simple CSV Read Operation</a>
 </li>
 </ul>
 </li>
 </ul>
 {{csv_data}}
 {% if csv_data %}
 sad
 {{csv_data}}
 {% endif %}
 </body>
</code>```

答:
Let's change your view code a bit for transferring from Python to HTML. </p>

```<code>with open(file) as csv_file:
  csv_reader = csv.reader(csv_file, delimiter=';')
  line_count = 0
  resultlist = []
  for row in csv_reader:
    resultdict = {}
    resultdict['name'] = row[0]
    resultdict['date'] = row[1]
    resultdict['time'] = row[2]
    resultdict['status'] = row[3]
    resultlist.append(resultdict)

    line_count += 1
return render(request, "csv_home.html", {'results': resultlist})
</code>```

<p>In your HTML file you can do this </p>

```<code>{% for result in results %}
    {{ result.name }}
    {{ result.date }}
{% endfor %}
</code>```

Title: ValueError Django CSV

Body:
All I'm trying to do is through Django display my CSV table on the page.
And everything seems to work, but for some reason I get an error and I can’t find a solution to it

ValueError: The view csv_app.views.csv_simple_read didn't return an HttpResponse object. It returned None instead.
[19/Feb/2020 21:40:07] "GET / HTTP/1.1" 500 57986

I'm new to django. I would be very grateful for any help.

Views.py

import csv
import os

def csv_simple_read(request):
 path = os.path.dirname(__file__)
 file = os.path.join(path, 'csv_simple_read.csv')

 with open(file) as csv_file:
  csv_reader = csv.reader(csv_file, delimiter=';')
  line_count = 0
  for row in csv_reader:
    print('\n\nColumn names are {}, {}, {}, {}'.format(row[0], row[1], row[2], row[3]))
    line_count += 1

csv_simple_read.csv

test3;2020-02-16;05:22:49;OK
test2;2020-02-16;05:22:25;OK
test1;2020-02-16;05:22:10;OK
test3;2020-02-16;05:22:49;OK
test2;2020-02-16;05:22:25;OK
test1;2020-02-16;05:22:10;OK

csv_home.html

CSV Example - Read Write Examples

Read Operation

 Simple CSV Read Operation

 {{csv_data}}
 {% if csv_data %}
 sad
 {{csv_data}}
 {% endif %}

Body:
Let's change your view code a bit for transferring from Python to HTML.

with open(file) as csv_file:
  csv_reader = csv.reader(csv_file, delimiter=';')
  line_count = 0
  resultlist = []
  for row in csv_reader:
    resultdict = {}
    resultdict['name'] = row[0]
    resultdict['date'] = row[1]
    resultdict['time'] = row[2]
    resultdict['status'] = row[3]
    resultlist.append(resultdict)

    line_count += 1
return render(request, "csv_home.html", {'results': resultlist})

In your HTML file you can do this

{% for result in results %}
    {{ result.name }}
    {{ result.date }}
{% endfor %}


问:
MySQL DROP DATABASE Privilege?
In MySQL, how can I revoke only the DROP DATABASE privilege, without revoking DROP TABLE privileges?

答: Just assign the DROP privilege for the individual tables, not for the entire database.</p>

ContentLicense: CC BY-SA 3.0

Title: MySQL DROP DATABASE Privilege?


问:
Laravel : using model factories to generate one-to-many relationships
I have a problem with generating multiple one-to-many relationship for a model using factories in Laravel. The factories seem to only be generating one ClubFixture per Club, when they should be generating 5 ClubFixtures per Club.

Models

Club

```<code><?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Club extends Model
{
  //Table associated with the model
  protected $table = 'clubs';

   protected $fillable = ['name', 'league', 'age_group', 'colour', 'county', 'country'];

   public function fixtures(){
     return $this->hasMany('App\ClubFixture', 'club_id', 'id');
   }
}
</code>```

ClubFixture

```<code>namespace App;

use Illuminate\Database\Eloquent\Model;

class ClubFixture extends Model
{
  //Table associated with the model
  protected $table = 'club_fixtures';
}
</code>```

Modal Factories

```<code>$factory->define(App\Club::class, function (Faker\Generator $faker) {
    return [
        'name' => $faker->name,
        'league' => $faker->word,
        'age_group' => $faker->word,
        'colour' => $faker->hexcolor,
        'county' => $faker->state,
        'country' => $faker->country,
        'emblem' => $faker->imageUrl(80, 80),
        'banner' => $faker->imageUrl,
        'matches' => $faker->randomDigit,
        'wins' => $faker->randomDigit,
        'losses' => $faker->randomDigit,
    ];
});

$factory->define(App\ClubFixture::class, function (Faker\Generator $faker) {
    return [
        'club_id' => function () {
            return factory(App\Club::class)->create()->id;
        },
        'opposition' => $faker->name,
        'address' => $faker->address,
        'datetime' => $faker->dateTimeBetween('now', '+30 weeks'),
        'type' => $faker->randomElement(['home', 'away', 'none']),
    ];
});
</code>```

Database seeder

```<code>factory(App\Club::class, 100)->create()->each(function ($u) {
     factory(App\ClubFixture::class, 5)->create();
});
</code>```

Expected Result: Each Club should have 5 ClubFixtures associated with it

Actual Result: Some Clubs have no ClubFixtures, some only have one.

I've tried this answer, but get an error with saveMany does not exist and relationship is null.

You can download the SQL resulting database here.

Can someone tell me what I am doing wrong here?

答:
As an alternative to @baikho's answer, you can specify the foreign key directly instead of using the ->saveMany() method. I use this personally since I don't usually use ->hasMany() and other relationship methods.

```<code>use Illuminate\Database\Seeder;

class ClubSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        factory(App\Club::class, 100)->create()->each(function(App\Club $c) {
            factory(App\ClubFixture::class, 5)->create(['club_id' => $c->id]);
        });
    }
}
</code>```

Note that you want to change the ->make() method to the ->create() method as well when using this syntax.

This is also helpful if your database schema is not in the third normalized form or if you have some conditional fields.

For example if a User (parent table) is a male, the Clothing (child table) can have certain options that are different than if the user was a female. Those fields don't have a foreign key, but they are conditional on the User.

Finally you can remove the PHPDoc comment defining the $c variable and just pop the classname directly into the function argument. Its a bit cleaner and more legible that way.

Body:
I have a problem with generating multiple one-to-many relationship for a model using factories in Laravel. The factories seem to only be generating one ClubFixture per Club, when they should be generating 5 ClubFixtures per Club.

Models

Club

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Club extends Model
{
  //Table associated with the model
  protected $table = 'clubs';

   protected $fillable = ['name', 'league', 'age_group', 'colour', 'county', 'country'];

   public function fixtures(){
     return $this->hasMany('App\ClubFixture', 'club_id', 'id');
   }
}

ClubFixture

namespace App;

use Illuminate\Database\Eloquent\Model;

class ClubFixture extends Model
{
  //Table associated with the model
  protected $table = 'club_fixtures';
}

Modal Factories

$factory->define(App\Club::class, function (Faker\Generator $faker) {
    return [
        'name' => $faker->name,
        'league' => $faker->word,
        'age_group' => $faker->word,
        'colour' => $faker->hexcolor,
        'county' => $faker->state,
        'country' => $faker->country,
        'emblem' => $faker->imageUrl(80, 80),
        'banner' => $faker->imageUrl,
        'matches' => $faker->randomDigit,
        'wins' => $faker->randomDigit,
        'losses' => $faker->randomDigit,
    ];
});

$factory->define(App\ClubFixture::class, function (Faker\Generator $faker) {
    return [
        'club_id' => function () {
            return factory(App\Club::class)->create()->id;
        },
        'opposition' => $faker->name,
        'address' => $faker->address,
        'datetime' => $faker->dateTimeBetween('now', '+30 weeks'),
        'type' => $faker->randomElement(['home', 'away', 'none']),
    ];
});

Database seeder

factory(App\Club::class, 100)->create()->each(function ($u) {
     factory(App\ClubFixture::class, 5)->create();
});

Expected Result: Each Club should have 5 ClubFixtures associated with it

Actual Result: Some Clubs have no ClubFixtures, some only have one.

I've tried this answer, but get an error with saveMany does not exist and relationship is null.

You can download the SQL resulting database here.

Can someone tell me what I am doing wrong here?

Body:
As an alternative to @baikho's answer, you can specify the foreign key directly instead of using the ->saveMany() method. I use this personally since I don't usually use ->hasMany() and other relationship methods.

use Illuminate\Database\Seeder;

class ClubSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        factory(App\Club::class, 100)->create()->each(function(App\Club $c) {
            factory(App\ClubFixture::class, 5)->create(['club_id' => $c->id]);
        });
    }
}

Note that you want to change the ->make() method to the ->create() method as well when using this syntax.

This is also helpful if your database schema is not in the third normalized form or if you have some conditional fields.

For example if a User (parent table) is a male, the Clothing (child table) can have certain options that are different than if the user was a female. Those fields don't have a foreign key, but they are conditional on the User.

Finally you can remove the PHPDoc comment defining the $c variable and just pop the classname directly into the function argument. Its a bit cleaner and more legible that way.


问:
Why does my method overwrite positions in my array
So I have this method here

```<code>    while(oMenu == 1 || oMenu == 2){
            oMeny = Kbd.readInt("\nClick 1 to make an account\nClick 2 to login\nClick 3 to exit the program");
            if(oMeny == 1){
                for(int i = 0; Account[i] != null; i++){
                    if(Account[i] == null){
                        pos = i;
                    }
                }

                Account[pos] = new Account();

            }

            if(oMeny == 2){
                String s = Kbd.readString("Input your accountnumber: ");
                for(int i = 0; Account[i] != null; i++){
                    if(Account[i] != null && s.equals(Account[i].getAccountNumber())){
                        System.out.println("Welcome!");
                         // Here is rest of my code , the "inner" menu that works menyMetod(iMeny,mMeny);
                    }
                    else{
                        System.out.println("There are no accounts with that given accountnumber!");
                    }
                }
            }

        }
    }
</code>```

I want to understand why if I access the oMeny == 1 and make 2 accounts Why I can't seem to access the first one I make but rather the latest one? It seems that somehow my array "overwrites" the first empty position. Basically I want to find the first empty position in my array, so in the first case it's always index 0 and then the next time I make an account again, it should be Index 1 logically.

EDIT : Heres my code for the Account class

```<code>public class Account{

private int money, transactions;
private String AccountNumber;

public Account(){
    money = Kbd.readInt("\nHow much money do you want to put in?");
    AccountNumber = Kbd.readString("\nWhat account number do you want?");
}
</code>```

答:
The error is here:

```<code>for (int i = 0; accounts[i] != null; i++) {
     if (accounts[i] == null)
</code>```

The for loop repeats as long i points to a non-null entry. Therefore the if-condition is never true.

This becomes quickly obvious when you run the program line by line in a debugger.

Next time please provide a complete code example that can be compiled. Your code is full of error, It took me a lot of time to fix it before I was able to execute it.

Corrected code:

```<code>import java.util.Scanner;

class Main
{
    static Scanner kbd = new Scanner(System.in);
    static Account[] accounts = new Account[100];

    static class Account
    {
        //public int money;
        public String accountNumber;

        public Account()
        {
            //System.out.println("\nHow much money do you want to put in?");
            //money = Kbd.nextInt();
            System.out.println("\nWhat account number do you want?");
            accountNumber = kbd.next();
        }
    }

    public static void main(String[] args)
    {
        int oMenu = 1;
        int pos = 0;
        while (oMenu == 1 || oMenu == 2)
        {
            System.out.println("\nClick 1 to make an account\nClick 2 to login\nClick 3 to exit the program");
            oMenu = kbd.nextInt();
            if (oMenu == 1)
            {
                for (int i = 0; i<accounts.length; i++)
                {
                    if (accounts[i] == null)
                    {
                        accounts[i] = new Account();
                        break;
                    }
                }
            }

            if (oMenu == 2)
            {
                System.out.println("Input your accountnumber: ");
                String s = kbd.next();
                Account found=null;
                for (int i = 0; i<accounts.length; i++)
                {
                    if (accounts[i] != null && s.equals(accounts[i].accountNumber))
                    {
                        found=accounts[i];
                    }
                }
                if (found!=null)
                {
                    System.out.println("Welcome! nr. "+found.accountNumber);
                }
                else
                {
                    System.out.println("There are no accounts with that given accountnumber!");
                }
            }
        }
    }
}
</code>```

Notice how I also fixed the second for-loop.

Body:
So I have this method here

    while(oMenu == 1 || oMenu == 2){
            oMeny = Kbd.readInt("\nClick 1 to make an account\nClick 2 to login\nClick 3 to exit the program");
            if(oMeny == 1){
                for(int i = 0; Account[i] != null; i++){
                    if(Account[i] == null){
                        pos = i;
                    }
                }

                Account[pos] = new Account();

            }

            if(oMeny == 2){
                String s = Kbd.readString("Input your accountnumber: ");
                for(int i = 0; Account[i] != null; i++){
                    if(Account[i] != null && s.equals(Account[i].getAccountNumber())){
                        System.out.println("Welcome!");
                         // Here is rest of my code , the "inner" menu that works menyMetod(iMeny,mMeny);
                    }
                    else{
                        System.out.println("There are no accounts with that given accountnumber!");
                    }
                }
            }

        }
    }

I want to understand why if I access the oMeny == 1 and make 2 accounts Why I can't seem to access the first one I make but rather the latest one? It seems that somehow my array "overwrites" the first empty position. Basically I want to find the first empty position in my array, so in the first case it's always index 0 and then the next time I make an account again, it should be Index 1 logically.

EDIT : Heres my code for the Account class

public class Account{

private int money, transactions;
private String AccountNumber;

public Account(){
    money = Kbd.readInt("\nHow much money do you want to put in?");
    AccountNumber = Kbd.readString("\nWhat account number do you want?");
}

Body:
The error is here:

for (int i = 0; accounts[i] != null; i++) {
     if (accounts[i] == null)

The for loop repeats as long i points to a non-null entry. Therefore the if-condition is never true.

This becomes quickly obvious when you run the program line by line in a debugger.

Next time please provide a complete code example that can be compiled. Your code is full of error, It took me a lot of time to fix it before I was able to execute it.

Corrected code:

import java.util.Scanner;

class Main
{
    static Scanner kbd = new Scanner(System.in);
    static Account[] accounts = new Account[100];

    static class Account
    {
        //public int money;
        public String accountNumber;

        public Account()
        {
            //System.out.println("\nHow much money do you want to put in?");
            //money = Kbd.nextInt();
            System.out.println("\nWhat account number do you want?");
            accountNumber = kbd.next();
        }
    }

    public static void main(String[] args)
    {
        int oMenu = 1;
        int pos = 0;
        while (oMenu == 1 || oMenu == 2)
        {
            System.out.println("\nClick 1 to make an account\nClick 2 to login\nClick 3 to exit the program");
            oMenu = kbd.nextInt();
            if (oMenu == 1)
            {
                for (int i = 0; i<accounts.length; i++)
                {
                    if (accounts[i] == null)
                    {
                        accounts[i] = new Account();
                        break;
                    }
                }
            }

            if (oMenu == 2)
            {
                System.out.println("Input your accountnumber: ");
                String s = kbd.next();
                Account found=null;
                for (int i = 0; i<accounts.length; i++)
                {
                    if (accounts[i] != null && s.equals(accounts[i].accountNumber))
                    {
                        found=accounts[i];
                    }
                }
                if (found!=null)
                {
                    System.out.println("Welcome! nr. "+found.accountNumber);
                }
                else
                {
                    System.out.println("There are no accounts with that given accountnumber!");
                }
            }
        }
    }
}

Notice how I also fixed the second for-loop.


问:
how to get the Material of group in sketchup?
I have done iterating through the entities in a group, getting all the faces, and then getting the material of each face. However, if the material belongs to a group, there is no material on the face. How do I get the material of the group in this case? I use the C API.</p>

答:
A face can have a front material (and back material) and still be in a group with a group material.  In fact it's very common.  In that case, the face material takes precedence over the group material.

```<code>SU_RESULT SUFaceGetFrontMaterial(SUFaceRef face, SUMaterialRef* material);
SU_RESULT SUFaceGetBackMaterial(SUFaceRef face, SUMaterialRef* material);
</code>```

In addition, there is a much more direct way to get the material of a group.

```<code>SUDrawingElementRef draw = SUGroupToDrawingElement(group);
SUMaterialRef material = SU_INVALID;
SUDrawingElementGetMaterial(draw, &material);
</code>```

Body:
A face can have a front material (and back material) and still be in a group with a group material.  In fact it's very common.  In that case, the face material takes precedence over the group material.

SU_RESULT SUFaceGetFrontMaterial(SUFaceRef face, SUMaterialRef* material);
SU_RESULT SUFaceGetBackMaterial(SUFaceRef face, SUMaterialRef* material);

In addition, there is a much more direct way to get the material of a group.

SUDrawingElementRef draw = SUGroupToDrawingElement(group);
SUMaterialRef material = SU_INVALID;
SUDrawingElementGetMaterial(draw, &material);


问:
Question about using PowerShell Select-String, exiftool(-k)
In a PowerShell script I'm trying to filter the output of the exiftool(-k).exe command below, using Select-String.

I've tried numerous permutations, but none work, and I always see the unfiltered output.  What do I need to do to filter the output of this command?

```<code>Start-Process -FilePath "C:\PowerShell\exiftool(-k).exe" -ArgumentList test.jpg |
  Select-String -pattern 'GPS' -SimpleMatch
</code>```

答:
You cannot directly receive output from a Start-Process call[1], so using it in a pipeline is pointless.

In fact, on Windows your program launched with Start-Process runs in a different, new window, which is where you saw the unfiltered output (given that no Select-String was applied there); in your calling window, Start-Process produced no output at all, and therefore nothing was sent to Select-String, and the pipeline as a whole produced no output.

Never use Start-Process to synchronously invoke a console application whose output you want to capture or redirect - simply call the application directly:

```<code>& "C:\PowerShell\exiftool(-k).exe" test.jpg | Select-String GPS -SimpleMatch
</code>```

Note that &, the call operator, is needed for this invocation, because your executable path is (double-)quoted (of necessity here, because the file name contains ( and )); & is only needed for executable paths that are quoted and/or contain variable references; you wouldn't need it to call git ..., for instance.

[1] While you would see the program's output in the caller's window if you added -NoNewWindow -Wait to a Start-Process call, you still wouldn't be able to capture, pass on or redirect it.

Body:
In a PowerShell script I'm trying to filter the output of the exiftool(-k).exe command below, using Select-String.

I've tried numerous permutations, but none work, and I always see the unfiltered output.  What do I need to do to filter the output of this command?

Start-Process -FilePath "C:\PowerShell\exiftool(-k).exe" -ArgumentList test.jpg |
  Select-String -pattern 'GPS' -SimpleMatch

Body:
You cannot directly receive output from a Start-Process call[1], so using it in a pipeline is pointless.

In fact, on Windows your program launched with Start-Process runs in a different, new window, which is where you saw the unfiltered output (given that no Select-String was applied there); in your calling window, Start-Process produced no output at all, and therefore nothing was sent to Select-String, and the pipeline as a whole produced no output.

Never use Start-Process to synchronously invoke a console application whose output you want to capture or redirect - simply call the application directly:

& "C:\PowerShell\exiftool(-k).exe" test.jpg | Select-String GPS -SimpleMatch

Note that &, the call operator, is needed for this invocation, because your executable path is (double-)quoted (of necessity here, because the file name contains ( and )); & is only needed for executable paths that are quoted and/or contain variable references; you wouldn't need it to call git ..., for instance.

[1] While you would see the program's output in the caller's window if you added -NoNewWindow -Wait to a Start-Process call, you still wouldn't be able to capture, pass on or redirect it.


问:
PySpark pandas_udfs java.lang.IllegalArgumentException error
Does anyone have experience using pandas UDFs on a local pyspark session running on Windows?  I've used them on linux with good results, but I've been unsuccessful on my Windows machine.

Environment:

```<code>python==3.7
pyarrow==0.15
pyspark==2.3.4
pandas==0.24
</code>```

java version "1.8.0_74"

Sample script:

from pyspark.sql.functions import pandas_udf, PandasUDFType
from pyspark.sql import SparkSession

spark = SparkSession.builder.master("local").getOrCreate()
spark.conf.set("spark.sql.execution.arrow.enabled", "true")
spark.conf.set("spark.sql.execution.arrow.fallback.enabled", "false")

df = spark.createDataFrame(
    [(1, 1.0), (1, 2.0), (2, 3.0), (2, 5.0), (2, 10.0)],
    ("id", "v"))

@pandas_udf("id long, v double", PandasUDFType.GROUPED_MAP)
def subtract_mean(pdf):
    # pdf is a pandas.DataFrame
    v = pdf.v
    return pdf.assign(v=v - v.mean())

out_df = df.groupby("id").apply(subtract_mean).toPandas()
print(out_df.head())

# +---+----+
# | id|   v|
# +---+----+
# |  1|-0.5|
# |  1| 0.5|
# |  2|-3.0|
# |  2|-1.0|
# |  2| 4.0|
# +---+----+
```

<p>After running for a loooong time (splits the toPandas stage into 200 tasks each taking over a second) it returns an error like this:</p>

```Traceback (most recent call last):
  File "C:\miniconda3\envs\pandas_udf\lib\site-packages\pyspark\sql\dataframe.py", line 1953, in toPandas
    tables = self._collectAsArrow()
  File "C:\miniconda3\envs\pandas_udf\lib\site-packages\pyspark\sql\dataframe.py", line 2004, in _collectAsArrow
    sock_info = self._jdf.collectAsArrowToPython()
  File "C:\miniconda3\envs\pandas_udf\lib\site-packages\py4j\java_gateway.py", line 1257, in __call__
    answer, self.gateway_client, self.target_id, self.name)
  File "C:\miniconda3\envs\pandas_udf\lib\site-packages\pyspark\sql\utils.py", line 63, in deco
    return f(*a, **kw)
  File "C:\miniconda3\envs\pandas_udf\lib\site-packages\py4j\protocol.py", line 328, in get_return_value
    format(target_id, ".", name), value)
py4j.protocol.Py4JJavaError: An error occurred while calling o62.collectAsArrowToPython.
: org.apache.spark.SparkException: Job aborted due to stage failure: Task 69 in stage 3.0 failed 1 times, most recent failure: Lost task 69.0 in stage 3.0 (TID 201, localhost, executor driver): java.lang.IllegalArgumentException
    at java.nio.ByteBuffer.allocate(Unknown Source)
    at org.apache.arrow.vector.ipc.message.MessageChannelReader.readNextMessage(MessageChannelReader.java:64)
    at org.apache.arrow.vector.ipc.message.MessageSerializer.deserializeSchema(MessageSerializer.java:104)
    at org.apache.arrow.vector.ipc.ArrowStreamReader.readSchema(ArrowStreamReader.java:128)
    at org.apache.arrow.vector.ipc.ArrowReader.initialize(ArrowReader.java:181)
    at org.apache.arrow.vector.ipc.ArrowReader.ensureInitialized(ArrowReader.java:172)
    at org.apache.arrow.vector.ipc.ArrowReader.getVectorSchemaRoot(ArrowReader.java:65)
    at org.apache.spark.sql.execution.python.ArrowPythonRunner$$anon$1.read(ArrowPythonRunner.scala:161)
    at org.apache.spark.sql.execution.python.ArrowPythonRunner$$anon$1.read(ArrowPythonRunner.scala:121)
    at org.apache.spark.api.python.BasePythonRunner$ReaderIterator.hasNext(PythonRunner.scala:290)
    at org.apache.spark.InterruptibleIterator.hasNext(InterruptibleIterator.scala:37)
    at scala.collection.Iterator$$anon$12.hasNext(Iterator.scala:439)
    at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:408)
    at org.apache.spark.sql.execution.arrow.ArrowConverters$$anon$2.hasNext(ArrowConverters.scala:96)
    at scala.collection.Iterator$class.foreach(Iterator.scala:893)
    at org.apache.spark.sql.execution.arrow.ArrowConverters$$anon$2.foreach(ArrowConverters.scala:94)
    at scala.collection.generic.Growable$class.$plus$plus$eq(Growable.scala:59)
    at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:104)
    at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:48)
    at scala.collection.TraversableOnce$class.to(TraversableOnce.scala:310)
    at org.apache.spark.sql.execution.arrow.ArrowConverters$$anon$2.to(ArrowConverters.scala:94)
    at scala.collection.TraversableOnce$class.toBuffer(TraversableOnce.scala:302)
    at org.apache.spark.sql.execution.arrow.ArrowConverters$$anon$2.toBuffer(ArrowConverters.scala:94)
    at scala.collection.TraversableOnce$class.toArray(TraversableOnce.scala:289)
    at org.apache.spark.sql.execution.arrow.ArrowConverters$$anon$2.toArray(ArrowConverters.scala:94)
    at org.apache.spark.rdd.RDD$$anonfun$collect$1$$anonfun$12.apply(RDD.scala:945)
    at org.apache.spark.rdd.RDD$$anonfun$collect$1$$anonfun$12.apply(RDD.scala:945)
    at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:2074)
    at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:2074)
    at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:87)
    at org.apache.spark.scheduler.Task.run(Task.scala:109)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:345)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
```

答:
Your java.lang.IllegalArgumentException in pandas_udf has to do with pyarrow version, not with OS environment. See this issue for details.

You have two routs of action:

Downgrade pyarrow to v.0.14, or

Add environment variable ARROW_PRE_0_15_IPC_FORMAT=1 to SPARK_HOME/conf/spark-env.sh

On Windows, you'll need to have a spark-env.cmd file in the conf directory: set ARROW_PRE_0_15_IPC_FORMAT=1, as suggested by Jonathan Taws

Body:
Does anyone have experience using pandas UDFs on a local pyspark session running on Windows?  I've used them on linux with good results, but I've been unsuccessful on my Windows machine.

Environment:

python==3.7
pyarrow==0.15
pyspark==2.3.4
pandas==0.24

java version "1.8.0_74"

Sample script:

from pyspark.sql.functions import pandas_udf, PandasUDFType
from pyspark.sql import SparkSession

spark = SparkSession.builder.master("local").getOrCreate()
spark.conf.set("spark.sql.execution.arrow.enabled", "true")
spark.conf.set("spark.sql.execution.arrow.fallback.enabled", "false")

df = spark.createDataFrame(
    [(1, 1.0), (1, 2.0), (2, 3.0), (2, 5.0), (2, 10.0)],
    ("id", "v"))

@pandas_udf("id long, v double", PandasUDFType.GROUPED_MAP)
def subtract_mean(pdf):
    # pdf is a pandas.DataFrame
    v = pdf.v
    return pdf.assign(v=v - v.mean())

out_df = df.groupby("id").apply(subtract_mean).toPandas()
print(out_df.head())

# +---+----+
# | id|   v|
# +---+----+
# |  1|-0.5|
# |  1| 0.5|
# |  2|-3.0|
# |  2|-1.0|
# |  2| 4.0|
# +---+----+

After running for a loooong time (splits the toPandas stage into 200 tasks each taking over a second) it returns an error like this:

Traceback (most recent call last):
  File "C:\miniconda3\envs\pandas_udf\lib\site-packages\pyspark\sql\dataframe.py", line 1953, in toPandas
    tables = self._collectAsArrow()
  File "C:\miniconda3\envs\pandas_udf\lib\site-packages\pyspark\sql\dataframe.py", line 2004, in _collectAsArrow
    sock_info = self._jdf.collectAsArrowToPython()
  File "C:\miniconda3\envs\pandas_udf\lib\site-packages\py4j\java_gateway.py", line 1257, in __call__
    answer, self.gateway_client, self.target_id, self.name)
  File "C:\miniconda3\envs\pandas_udf\lib\site-packages\pyspark\sql\utils.py", line 63, in deco
    return f(*a, **kw)
  File "C:\miniconda3\envs\pandas_udf\lib\site-packages\py4j\protocol.py", line 328, in get_return_value
    format(target_id, ".", name), value)
py4j.protocol.Py4JJavaError: An error occurred while calling o62.collectAsArrowToPython.
: org.apache.spark.SparkException: Job aborted due to stage failure: Task 69 in stage 3.0 failed 1 times, most recent failure: Lost task 69.0 in stage 3.0 (TID 201, localhost, executor driver): java.lang.IllegalArgumentException
    at java.nio.ByteBuffer.allocate(Unknown Source)
    at org.apache.arrow.vector.ipc.message.MessageChannelReader.readNextMessage(MessageChannelReader.java:64)
    at org.apache.arrow.vector.ipc.message.MessageSerializer.deserializeSchema(MessageSerializer.java:104)
    at org.apache.arrow.vector.ipc.ArrowStreamReader.readSchema(ArrowStreamReader.java:128)
    at org.apache.arrow.vector.ipc.ArrowReader.initialize(ArrowReader.java:181)
    at org.apache.arrow.vector.ipc.ArrowReader.ensureInitialized(ArrowReader.java:172)
    at org.apache.arrow.vector.ipc.ArrowReader.getVectorSchemaRoot(ArrowReader.java:65)
    at org.apache.spark.sql.execution.python.ArrowPythonRunner$$anon$1.read(ArrowPythonRunner.scala:161)
    at org.apache.spark.sql.execution.python.ArrowPythonRunner$$anon$1.read(ArrowPythonRunner.scala:121)
    at org.apache.spark.api.python.BasePythonRunner$ReaderIterator.hasNext(PythonRunner.scala:290)
    at org.apache.spark.InterruptibleIterator.hasNext(InterruptibleIterator.scala:37)
    at scala.collection.Iterator$$anon$12.hasNext(Iterator.scala:439)
    at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:408)
    at org.apache.spark.sql.execution.arrow.ArrowConverters$$anon$2.hasNext(ArrowConverters.scala:96)
    at scala.collection.Iterator$class.foreach(Iterator.scala:893)
    at org.apache.spark.sql.execution.arrow.ArrowConverters$$anon$2.foreach(ArrowConverters.scala:94)
    at scala.collection.generic.Growable$class.$plus$plus$eq(Growable.scala:59)
    at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:104)
    at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:48)
    at scala.collection.TraversableOnce$class.to(TraversableOnce.scala:310)
    at org.apache.spark.sql.execution.arrow.ArrowConverters$$anon$2.to(ArrowConverters.scala:94)
    at scala.collection.TraversableOnce$class.toBuffer(TraversableOnce.scala:302)
    at org.apache.spark.sql.execution.arrow.ArrowConverters$$anon$2.toBuffer(ArrowConverters.scala:94)
    at scala.collection.TraversableOnce$class.toArray(TraversableOnce.scala:289)
    at org.apache.spark.sql.execution.arrow.ArrowConverters$$anon$2.toArray(ArrowConverters.scala:94)
    at org.apache.spark.rdd.RDD$$anonfun$collect$1$$anonfun$12.apply(RDD.scala:945)
    at org.apache.spark.rdd.RDD$$anonfun$collect$1$$anonfun$12.apply(RDD.scala:945)
    at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:2074)
    at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:2074)
    at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:87)
    at org.apache.spark.scheduler.Task.run(Task.scala:109)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:345)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)


问:
Rename BitBucket repository using REST API 2.0?
The only other chatter I've found about a rename-repo operation is based on the 1.0 REST API, which stopped working a year or so ago.

I should be able to use this documentation, but so far no luck: I find the "request example" confusing.

The "Note:" clearly implies that it should be possible to rename a repository using the 2.0 API. ("Note: Changing the name of the repository will cause the location to be changed.")

But a PUT to 2.0/repositories/owner/repo with data {"full_name": "owner/newname"} doesn't seem to change the name. Evidently it does locate the right repository, because I get back JSON describing that repository, but still with the original name. The web UI finds the original name, does not find the new name.

I would just like a working example of the data body to PUT to the 2.0/repositories/owner/repo path to rename owner/repo. Thanks.

(n.b. I already know how to rename a repo using the web UI. I must rename 84 repositories and would rather not do it by hand.)

答: It appears that {"name": "newname"} works. Attempting to change "full_name" or "slug" does not. TIL...</p>


问:
How to implement chat using jQuery, PHP, and MySQL?
I have made a chat script using php, mysql and jquery. It uses json to get data from the server. It makes fixed interval requests to the server with the lastly fetched message id to get new messages from the server. But when multiple users will be chatting then thousands and crores of requests will be made to the server within an hour and the hosting people will block it for sure.

Th gmail chat uses socket I think. Because it does not sends fixed interval requests for sure. Could any one of you please give me some sample code or some direction to solve this issue.

Please I need help desperately.

Many thanks in advance. My respect and regards for all.

答:
If the host you are using would "block it for sure" if it's making that many requests, then you may want to consider getting a different host or upgrading your hosting package before worrying about your code. Check out how Facebook implements their chat:

The method we chose to get text from
  one user to another involves loading
  an iframe on each Facebook page, and
  having that iframe's Javascript make
  an HTTP GET request over a persistent
  connection that doesn't return until
  the server has data for the client.
  The request gets reestablished if it's
  interrupted or times out. This isn't
  by any means a new technique: it's a
  variation of Comet, specifically XHR
  long polling, and/or BOSH.

ContentLicense: CC BY-SA 2.5

LastEditorDisplayName: ryeguy

OwnerDisplayName: ryeguy


问:
c++ self in initialisation list
i have this code snippet

```<code>class Osoba{
         Osoba(char* imie,int wiek){
                     this->imie=new char[strlen(imie)+1];
                     strcpy(this->imie,imie);
                     this->wiek=wiek;
                     cout<<"Utworzono Osobe "<<this->imie<<endl;
         }
         Osoba(Osoba& x){
                 Osoba(x.imie,x.wiek);
         }
[...]
</code>```

and when i call the copy constructor it doesnt work (creates and destroyes object).

Edit:
if i use

```<code>         Osoba(Osoba& x): Osoba(x.imie,x.wiek){
</code>```

i get type 'class Osoba' is not a direct base of 'Osoba'

how is this done ?

答:
First of all, the constructor is not just any method, you can't just use at as a mutator method.

that said,
Am I right in thinking that your class had two fields, imie and wiek?

just do the init code again, this time with x.imie and x.wiek as input

Title: c++ self in initialisation list

Body:
i have this code snippet

class Osoba{
         Osoba(char* imie,int wiek){
                     this->imie=new char[strlen(imie)+1];
                     strcpy(this->imie,imie);
                     this->wiek=wiek;
                     cout<<"Utworzono Osobe "<<this->imie<<endl;
         }
         Osoba(Osoba& x){
                 Osoba(x.imie,x.wiek);
         }
[...]

and when i call the copy constructor it doesnt work (creates and destroyes object).

Edit:
if i use

         Osoba(Osoba& x): Osoba(x.imie,x.wiek){

i get type 'class Osoba' is not a direct base of 'Osoba'

how is this done ?


问:
how to change an object type to its actual type?
I have an object which is actually string/integer.
I can get its type by obj.GetType().
In the runtime I know it is a string.
The problem is a function only accepts string or integer.
If I wrote

```<code>int cnt = 0;
public object why()
{
    cnt++;
    if (cnt % 2 == 0) return 0;
    return "";
}
void A(int input) {
}
void A(string input) {
}
void Test()
{
    object obj = why();
    MethodInfo method = obj.GetType().GetMethod(nameof(A), new[] { obj.GetType() });
    //Need to call A by obj, but it threw an error :)
    method.Invoke(null, new[] { obj });
}
</code>```

It returns error as it rejects object. It could work if

```<code>func((string)obj)
</code>```

But I need also cater integer ( and possibly other type too later, thus it may not appropriate to apply conditional statement for the scenario)

the optimum way is changing the type of the object before assign it to the function, but I am not sure if it is possible, which is string in the example func((string)obj).

答:
You can use reflection to get the correct method based on the type of the object. Then you can use Invoke() to actually call the method with the argument.

```<code>object obj = why();
MethodInfo method = typeof(YourClass).GetMethod(nameof(YourClass.A), new [] { obj.GetType() } );
method.Invoke(maybe_an_object_here, new [] { obj } );
</code>```

The class YourClass is the class which contains the method A.

maybe_an_object_here is the object instance of YourClass on which you want to call the A method on. Depending on what you are doing this argument can be this (if it is an object of YourClass). In case it is a static method (it is not according to your code provided) you do not provide an object of that class but instead use the value null since you don't need objects to call static methods.

Body:
I have an object which is actually string/integer.
I can get its type by obj.GetType().
In the runtime I know it is a string.
The problem is a function only accepts string or integer.
If I wrote

int cnt = 0;
public object why()
{
    cnt++;
    if (cnt % 2 == 0) return 0;
    return "";
}
void A(int input) {
}
void A(string input) {
}
void Test()
{
    object obj = why();
    MethodInfo method = obj.GetType().GetMethod(nameof(A), new[] { obj.GetType() });
    //Need to call A by obj, but it threw an error :)
    method.Invoke(null, new[] { obj });
}

It returns error as it rejects object. It could work if

func((string)obj)

But I need also cater integer ( and possibly other type too later, thus it may not appropriate to apply conditional statement for the scenario)

the optimum way is changing the type of the object before assign it to the function, but I am not sure if it is possible, which is string in the example func((string)obj).

Body:
You can use reflection to get the correct method based on the type of the object. Then you can use Invoke() to actually call the method with the argument.

object obj = why();
MethodInfo method = typeof(YourClass).GetMethod(nameof(YourClass.A), new [] { obj.GetType() } );
method.Invoke(maybe_an_object_here, new [] { obj } );

The class YourClass is the class which contains the method A.

maybe_an_object_here is the object instance of YourClass on which you want to call the A method on. Depending on what you are doing this argument can be this (if it is an object of YourClass). In case it is a static method (it is not according to your code provided) you do not provide an object of that class but instead use the value null since you don't need objects to call static methods.


问:
How add many google map markers from my database?
I have laravel API LAT & LNG and i want to show all markers in a map. I tried to make a loop to red it but show only one mark. How can I solve this?  </p>

```<code>
var lat ;
var lng ;

var mylist ;
  Future<List<Post>> getData() async {
    // start get post
      http.Response response = await http.get(
          " mysite.com/api/getall",
          headers: {"Accept": "application/json"},);
      List collection = json.decode(response.body);
       mylist = collection.map((json) => Post.fromJson(json)).toList();
     for(var i in mylist){
        lat = i.lat ;
        lng = i.lng ;
      }
  } // end getData
///////////////////////////

                  FlutterMap(
                        options: new MapOptions(
                          center: new LatLng(lat, lng),
                          zoom: 12.0,
                        ),
                        layers: [
                          new TileLayerOptions(
                              urlTemplate:
                                  "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
                              subdomains: ['a', 'b', 'c']),
                          new MarkerLayerOptions(markers: [
                            new Marker(
                                width: 45.0,
                                height: 45.0,
                                point: new LatLng(lat, lng),
                                builder: (context) => new Container(
                                      child: IconButton(
                                        icon: Icon(Icons.location_on),
                                        color: Colors.red,
                                        iconSize: 45.0,
                                        onPressed: () {
                                          print('Marker tapped');
                                        },
                                      ),

</code>```

答:
In MarkerLayerOptions you can actually pass a list of markers. At the moment you only have one marker in the list, so that is why you only see one marker.

So you would basically do this (not tested, you might need to adjust the syntax a bit...)

```<code>var mylist ;
List myMarkers = [];

Future<List<Post>> getData() async {
    // start get post
      http.Response response = await http.get(
          " mysite.com/api/getall",
          headers: {"Accept": "application/json"},);
      List collection = json.decode(response.body);
       mylist = collection.map((json) => Post.fromJson(json)).toList();
     for(var i in mylist){
        lat = i.lat ;
        lng = i.lng ;
        var nextMarker = new Marker(
                            width: 45.0,
                            height: 45.0,
                            point: new LatLng(lat, lng),
                            builder: (context) => new Container(
                                  child: IconButton(
                                    icon: Icon(Icons.location_on),
                                    color: Colors.red,
                                    iconSize: 45.0,
                                    onPressed: () {
                                      print('Marker tapped');
                                    },
                                  ),
                             );
        myMarkers.add(nextMarker);
      }
  } // end getData

  ///////////////////////////

              FlutterMap(
                    options: new MapOptions(
                      center: new LatLng(lat, lng),
                      zoom: 12.0,
                    ),
                    layers: [
                      new TileLayerOptions(
                          urlTemplate:
                              "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
                          subdomains: ['a', 'b', 'c']),
                      new MarkerLayerOptions(markers: myMarkers),
                    ])
</code>```

As stated above, do not expect that code to work out of the box, it is only here, to illustrate the general idea.

Body:
I have laravel API LAT & LNG and i want to show all markers in a map. I tried to make a loop to red it but show only one mark. How can I solve this?

var lat ;
var lng ;

var mylist ;
  Future<List<Post>> getData() async {
    // start get post
      http.Response response = await http.get(
          " mysite.com/api/getall",
          headers: {"Accept": "application/json"},);
      List collection = json.decode(response.body);
       mylist = collection.map((json) => Post.fromJson(json)).toList();
     for(var i in mylist){
        lat = i.lat ;
        lng = i.lng ;
      }
  } // end getData
///////////////////////////

                  FlutterMap(
                        options: new MapOptions(
                          center: new LatLng(lat, lng),
                          zoom: 12.0,
                        ),
                        layers: [
                          new TileLayerOptions(
                              urlTemplate:
                                  "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
                              subdomains: ['a', 'b', 'c']),
                          new MarkerLayerOptions(markers: [
                            new Marker(
                                width: 45.0,
                                height: 45.0,
                                point: new LatLng(lat, lng),
                                builder: (context) => new Container(
                                      child: IconButton(
                                        icon: Icon(Icons.location_on),
                                        color: Colors.red,
                                        iconSize: 45.0,
                                        onPressed: () {
                                          print('Marker tapped');
                                        },
                                      ),

Body:
In MarkerLayerOptions you can actually pass a list of markers. At the moment you only have one marker in the list, so that is why you only see one marker.

So you would basically do this (not tested, you might need to adjust the syntax a bit...)

var mylist ;
List myMarkers = [];

Future<List<Post>> getData() async {
    // start get post
      http.Response response = await http.get(
          " mysite.com/api/getall",
          headers: {"Accept": "application/json"},);
      List collection = json.decode(response.body);
       mylist = collection.map((json) => Post.fromJson(json)).toList();
     for(var i in mylist){
        lat = i.lat ;
        lng = i.lng ;
        var nextMarker = new Marker(
                            width: 45.0,
                            height: 45.0,
                            point: new LatLng(lat, lng),
                            builder: (context) => new Container(
                                  child: IconButton(
                                    icon: Icon(Icons.location_on),
                                    color: Colors.red,
                                    iconSize: 45.0,
                                    onPressed: () {
                                      print('Marker tapped');
                                    },
                                  ),
                             );
        myMarkers.add(nextMarker);
      }
  } // end getData

  ///////////////////////////

              FlutterMap(
                    options: new MapOptions(
                      center: new LatLng(lat, lng),
                      zoom: 12.0,
                    ),
                    layers: [
                      new TileLayerOptions(
                          urlTemplate:
                              "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
                          subdomains: ['a', 'b', 'c']),
                      new MarkerLayerOptions(markers: myMarkers),
                    ])

As stated above, do not expect that code to work out of the box, it is only here, to illustrate the general idea.


问:
How to extract JSON data with PHP
I'm trying to extract the value from the event key (delivered) using PHP. I thought the follow would work below but I'm getting no results. I know this is probably a simple thing to do and probably looking too far into it.

How I am trying to extract the value

```<code>$status = json_decode($status, true);
echo $status[1]['event'];
</code>```

Here is my JSON file

```<code>{
   "events":[
      {
         "email":"email@gmail.com",
         "date":"2020-02-17T22:16:58.000+01:00",
         "subject":"PHPMailer SMTP test",
         "messageId":"<hdskjfjsdhfsjdkfdksh>",
         "event":"delivered",
         "tag":"",
         "from":"test@gmail.com"
      }
   ]
}
</code>```

Any help would be appreciated :)

答:
I figured it out. So I am parsing a json response from CURL...

```<code>$response = curl_exec($curl);
</code>```

I changed this

```<code>$status = json_decode($status, true);
echo $status['events'][0]['event'];
</code>```

to

```<code>$status = json_decode($response, true);
echo $status['events'][0]['event'];
</code>```

I totally missed that mistake. Thanks to everyone who reach out!

Title: How to extract JSON data with PHP

Body:
I'm trying to extract the value from the event key (delivered) using PHP. I thought the follow would work below but I'm getting no results. I know this is probably a simple thing to do and probably looking too far into it.

How I am trying to extract the value

$status = json_decode($status, true);
echo $status[1]['event'];

Here is my JSON file

{
   "events":[
      {
         "email":"email@gmail.com",
         "date":"2020-02-17T22:16:58.000+01:00",
         "subject":"PHPMailer SMTP test",
         "messageId":"<hdskjfjsdhfsjdkfdksh>",
         "event":"delivered",
         "tag":"",
         "from":"test@gmail.com"
      }
   ]
}

Any help would be appreciated :)

Body:
I figured it out. So I am parsing a json response from CURL...

$response = curl_exec($curl);

I changed this

$status = json_decode($status, true);
echo $status['events'][0]['event'];

to

$status = json_decode($response, true);
echo $status['events'][0]['event'];

I totally missed that mistake. Thanks to everyone who reach out!


问:
Distributed configuration in Google Cloud Platform?
I have a Google Cloud Function (written in Python) that needs to read some config. The config is essentially a dictionary (map) of strings => values.

Example:

```<code>config = {
  'key1': 'value1',
  'key2': 'value2',
  'key3': ['value31', 'value32', 'value33']
}
</code>```

I want to be able to edit this config without performing a re-deploy of my Google Cloud Function.

Ideally, I'd like to store this config somewhere so that I can read it later when necessary. Does Google have something like this already?

One thing I can think of is just using something like GCS to store a .json/.yaml file and reading it periodically, but I wanted to check if there were other ways of storing this config. Especially if Google has some sort of distributed config mechanism already built.

答:
There is not a special purpose service for distributing configuration values to Cloud Functions.

As you supposed, GCS is a very straightforward way to handle this.

You can also consider a managed Redis instance with Cloud Memorystore.  A small JSON string stored in a Redis database could work out nicely for your system.

Of course, Redis has a different a notion of "durability" than GCS -- so you will want to weigh your options here.

PS: Firestore is yet another option.  It is fully managed & free with small amounts of data.  This is, in a certain sense, overkill -- but it would work.

Body:
I have a Google Cloud Function (written in Python) that needs to read some config. The config is essentially a dictionary (map) of strings => values.

Example:

config = {
  'key1': 'value1',
  'key2': 'value2',
  'key3': ['value31', 'value32', 'value33']
}

I want to be able to edit this config without performing a re-deploy of my Google Cloud Function.

Ideally, I'd like to store this config somewhere so that I can read it later when necessary. Does Google have something like this already?

One thing I can think of is just using something like GCS to store a .json/.yaml file and reading it periodically, but I wanted to check if there were other ways of storing this config. Especially if Google has some sort of distributed config mechanism already built.


问:
Git clone using http with user and password
I try to use git clone http://user:password@host.com but I have some problems with adding user or password because the user is using email which contains an other @ and password is using an esclamation mark ( ! )

the command looks like http://user@home.com:!password@host.com/git

This is not working - I tried 0x21password instead of !password but the system don't except this.

Any idea?

答: have you tried http://user%40home.com:!password@host.com/git</p>


问:
How to list all the recently installed softwares in linux other than rpm and yum command
I want to list all the recently installed softwares in linux other than rpm and yum command(because these commands will list only the softwares installed using them). I want to list apps installed from all the sources.</p>

答:
Taking into account the basic interpretation of what an "installation" is: placing an executable program anywhere under PATH, you can do what you want by searching recently modified files in PATH directories:

```<code>for d in ${PATH//:/ }; do
  if test -d "$d"; then
    find "$d" -type f -executable -mtime -1
  fi
done
</code>```

This snippet takes PATH (which is a concatenation of directory names separated by colon), and for each directory finds executable files which were modified within last day.

To find executables installed within, e.g. 2 days, use -mtime -2.

Surely, this implies some level of trust that nothing malicious was done during installation because a bad player can easily adjust the modification times through, e.g. touch command.

Body:
Taking into account the basic interpretation of what an "installation" is: placing an executable program anywhere under PATH, you can do what you want by searching recently modified files in PATH directories:

for d in ${PATH//:/ }; do
  if test -d "$d"; then
    find "$d" -type f -executable -mtime -1
  fi
done

This snippet takes PATH (which is a concatenation of directory names separated by colon), and for each directory finds executable files which were modified within last day.

To find executables installed within, e.g. 2 days, use -mtime -2.

Surely, this implies some level of trust that nothing malicious was done during installation because a bad player can easily adjust the modification times through, e.g. touch command.


问:
chisquare test in r that keeps row names
I'm building an employee survey with two waves, and I want to make sure that each wave is balanced in terms of some demographic variables, such as ethnicity and gender. Here is a fictitious sample of the data:

```<code>library(tidyverse)
sample_data <- tibble(demographics = c("White / Female", "Non-White / Female", "White / Male", "Non-White / Male", "White / Transgender", "Non-White / Transgender"),
                      wave_1 = c(40, 38, 60, 56, 0, 2),
                      wave_2 = c(38, 39, 62, 58, 1, 0))
</code>```

If I run the chisq.test() on sample_data, I get an error:

```<code>library(stats)
chisq.test(sample_data)

Error in chisq.test(sample_data) :
  all entries of 'x' must be nonnegative and finite
</code>```

But I don't get the error if I just use the two count columns:

```<code>sample_data_count <- sample_data %>%
  dplyr::select(wave_1, wave_2)
chisq.test(sample_data_count)

    Pearson's Chi-squared test

data:  sample_data_count
X-squared = 3.1221, df = 5, p-value = 0.6812

Warning message:
In chisq.test(sample_data_count) :
  Chi-squared approximation may be incorrect
</code>```

I understand that R doesn't like that I have my demographics in the sample_data, but it's hard not having them in if I want to look at the observed values by various demographics. Is there a way to run the chisquare test with those row names in?

I saw an example using at http://www.sthda.com/english/wiki/chi-square-test-of-independence-in-r using this dataset (file_path <- "http://www.sthda.com/sthda/RDoc/data/housetasks.txt") that does do a chi square test in r with the row names still in it.

Any help would be appreciated!

答:
Because it also iincludes  character column.  According to ?chisq.test

x - a numeric vector or matrix. x and y can also both be factors.

y -  a numeric vector; ignored if x is a matrix. If x is a factor, y should be a factor of the same length.

If we  want to pass a numeric matrix, either select the numeric columns or  convert the 'demographics' to row names, convert to matrix and apply the test

```<code>library(dplyr)
library(tibble)
sample_data %>%
   column_to_rownames('demographics') %>%
   as.matrix %>%
   chisq.test
</code>```

Body:
I'm building an employee survey with two waves, and I want to make sure that each wave is balanced in terms of some demographic variables, such as ethnicity and gender. Here is a fictitious sample of the data:

library(tidyverse)
sample_data <- tibble(demographics = c("White / Female", "Non-White / Female", "White / Male", "Non-White / Male", "White / Transgender", "Non-White / Transgender"),
                      wave_1 = c(40, 38, 60, 56, 0, 2),
                      wave_2 = c(38, 39, 62, 58, 1, 0))

If I run the chisq.test() on sample_data, I get an error:

library(stats)
chisq.test(sample_data)

Error in chisq.test(sample_data) :
  all entries of 'x' must be nonnegative and finite

But I don't get the error if I just use the two count columns:

sample_data_count <- sample_data %>%
  dplyr::select(wave_1, wave_2)
chisq.test(sample_data_count)

    Pearson's Chi-squared test

data:  sample_data_count
X-squared = 3.1221, df = 5, p-value = 0.6812

Warning message:
In chisq.test(sample_data_count) :
  Chi-squared approximation may be incorrect

I understand that R doesn't like that I have my demographics in the sample_data, but it's hard not having them in if I want to look at the observed values by various demographics. Is there a way to run the chisquare test with those row names in?

I saw an example using at http://www.sthda.com/english/wiki/chi-square-test-of-independence-in-r using this dataset (file_path <- "http://www.sthda.com/sthda/RDoc/data/housetasks.txt") that does do a chi square test in r with the row names still in it.

Any help would be appreciated!

Body:
Because it also iincludes  character column.  According to ?chisq.test

x - a numeric vector or matrix. x and y can also both be factors.

y -  a numeric vector; ignored if x is a matrix. If x is a factor, y should be a factor of the same length.

If we  want to pass a numeric matrix, either select the numeric columns or  convert the 'demographics' to row names, convert to matrix and apply the test

library(dplyr)
library(tibble)
sample_data %>%
   column_to_rownames('demographics') %>%
   as.matrix %>%
   chisq.test


问:
How to get integer list from a string but not integer list?
I want to create an integer list from the string list called a. The list I want to create is shown in the integer list called b.

```<code>a = ["ab","ac","ad","ae","af","ab","ab"]
b = [1,2,3,4,5,1,1]

</code>```

I've tried that solution but it takes long time for thousands of data.

```<code>a = ["ab","ac","ad","ae","af","ab","ab"]
b = list(set(a))
for i in range(0,len(a)):
    if a[i] in b:
        a[i] = b.index(a[i])+1
print(a)
</code>```

Thanks

答:
```<code># Input array of strings.
a = ["ab","ac","ad","ae","af","ab","ab"]

d = {}
counter = 0
b = []
for item in a:
    if item not in d:
        counter += 1
        d[item] = counter
    b.append(d[item])

>>> b
[1, 2, 3, 4, 5, 1, 1]
</code>```

Body:
I want to create an integer list from the string list called a. The list I want to create is shown in the integer list called b.

a = ["ab","ac","ad","ae","af","ab","ab"]
b = [1,2,3,4,5,1,1]

I've tried that solution but it takes long time for thousands of data.

a = ["ab","ac","ad","ae","af","ab","ab"]
b = list(set(a))
for i in range(0,len(a)):
    if a[i] in b:
        a[i] = b.index(a[i])+1
print(a)

Thanks


答:
Based only on the values provide in you question you can gather the unique values to a list and then use the index of the unique list to provide the numbers for each value to get the expected output.

```<code>a = ["ab", "ac", "ad", "ae", "af", "ab", "ab"]
unique_list = []
b = []

for value in a:
    if value not in unique_list:
        unique_list.append(value)

for value in a:
    for ndex, unique_value in enumerate(unique_list):
        if value == unique_value:
            b.append(ndex+1)
            break

print(b)
</code>```

Results:

```<code>[1, 2, 3, 4, 5, 1, 1]
</code>```

That said there is no way to know for sure what your string values translate to without more context in your question.

UPDATE:

Based on questions in the comments I also tested this with a set and ran a new random set of 40,000 values 10 times for set() and 10 times for list. The list appears to run faster unless I am doing something wrong here.

```<code>import time
import random

times_for_set = []
times_for_list = []
times_for_dict = []

def run_comparison_set():
    a = []
    for _ in range(40000):
        x = random.choice('abcdefghijklmnopqrstuvwxyz')
        y = random.choice('abcdefghijklmnopqrstuvwxyz')
        a.append('{}{}'.format(x, y))

    unique_list = set(a)
    b = []

    start_time = time.time()
    for value in a:
        for ndex, unique_value in enumerate(unique_list):
            if value == unique_value:
                b.append(ndex+1)
                break
    times_for_set.append(time.time() - start_time)

def run_comparison_list():
    a = []
    for _ in range(40000):
        x = random.choice('abcdefghijklmnopqrstuvwxyz')
        y = random.choice('abcdefghijklmnopqrstuvwxyz')
        a.append('{}{}'.format(x, y))

    unique_list = []
    b = []
    for value in a:
        if value not in unique_list:
            unique_list.append(value)
    start_time = time.time()
    for value in a:
        for ndex, unique_value in enumerate(unique_list):
            if value == unique_value:
                b.append(ndex + 1)
                break
    times_for_list.append(time.time() - start_time)

def run_comparison_dict():
    a = []
    for _ in range(40000):
        x = random.choice('abcdefghijklmnopqrstuvwxyz')
        y = random.choice('abcdefghijklmnopqrstuvwxyz')
        a.append('{}{}'.format(x, y))

    counter = 0
    d = {}
    b = []
    start_time = time.time()
    for item in a:
        if item not in d:
            counter += 1
            d[item] = counter
        b.append(d[item])
    times_for_dict.append(time.time() - start_time)

for i in range(10):
    run_comparison_set()
    run_comparison_list()
    run_comparison_dict()

print('Average time for set: ', sum(times_for_set) / len(times_for_set))
print('Average time for list: ', sum(times_for_list) / len(times_for_list))
print('Average time for dict: ', sum(times_for_dict) / len(times_for_dict))
</code>```

Results:

```<code>Average time for set:  0.8128192901611329
Average time for list:  0.6368690490722656
Average time for dict:  0.00530548095703125
</code>```

So it appears that list and set are much slower than a dict.

Title: How to get integer list from a string but not integer list?

Body:
Based only on the values provide in you question you can gather the unique values to a list and then use the index of the unique list to provide the numbers for each value to get the expected output.

a = ["ab", "ac", "ad", "ae", "af", "ab", "ab"]
unique_list = []
b = []

for value in a:
    if value not in unique_list:
        unique_list.append(value)

for value in a:
    for ndex, unique_value in enumerate(unique_list):
        if value == unique_value:
            b.append(ndex+1)
            break

print(b)

Results:

[1, 2, 3, 4, 5, 1, 1]

That said there is no way to know for sure what your string values translate to without more context in your question.

UPDATE:

Based on questions in the comments I also tested this with a set and ran a new random set of 40,000 values 10 times for set() and 10 times for list. The list appears to run faster unless I am doing something wrong here.

import time
import random

times_for_set = []
times_for_list = []
times_for_dict = []

def run_comparison_set():
    a = []
    for _ in range(40000):
        x = random.choice('abcdefghijklmnopqrstuvwxyz')
        y = random.choice('abcdefghijklmnopqrstuvwxyz')
        a.append('{}{}'.format(x, y))

    unique_list = set(a)
    b = []

    start_time = time.time()
    for value in a:
        for ndex, unique_value in enumerate(unique_list):
            if value == unique_value:
                b.append(ndex+1)
                break
    times_for_set.append(time.time() - start_time)

def run_comparison_list():
    a = []
    for _ in range(40000):
        x = random.choice('abcdefghijklmnopqrstuvwxyz')
        y = random.choice('abcdefghijklmnopqrstuvwxyz')
        a.append('{}{}'.format(x, y))

    unique_list = []
    b = []
    for value in a:
        if value not in unique_list:
            unique_list.append(value)
    start_time = time.time()
    for value in a:
        for ndex, unique_value in enumerate(unique_list):
            if value == unique_value:
                b.append(ndex + 1)
                break
    times_for_list.append(time.time() - start_time)

def run_comparison_dict():
    a = []
    for _ in range(40000):
        x = random.choice('abcdefghijklmnopqrstuvwxyz')
        y = random.choice('abcdefghijklmnopqrstuvwxyz')
        a.append('{}{}'.format(x, y))

    counter = 0
    d = {}
    b = []
    start_time = time.time()
    for item in a:
        if item not in d:
            counter += 1
            d[item] = counter
        b.append(d[item])
    times_for_dict.append(time.time() - start_time)

for i in range(10):
    run_comparison_set()
    run_comparison_list()
    run_comparison_dict()

print('Average time for set: ', sum(times_for_set) / len(times_for_set))
print('Average time for list: ', sum(times_for_list) / len(times_for_list))
print('Average time for dict: ', sum(times_for_dict) / len(times_for_dict))

Results:

Average time for set:  0.8128192901611329
Average time for list:  0.6368690490722656
Average time for dict:  0.00530548095703125

So it appears that list and set are much slower than a dict.


问:
Groovy Split String with possible String
I am using groovy to parse a in a file which needs to be split into an array using groovy.

The problem is that the used delimiter "," is in the one field so a the split command is not correctly applied. The output will use a different delimiter (e.g. "|")

```<code>ELEMENT 1-01,"X0185-CX2F-01-PW(SATop,20211,10.42.81.0)",2020-02-18 09:00:00,15,0.00,0.00,0.00,0.00,0.00,0,0,0
</code>```

Does the groovy split(",") function consider the field as a single string literal?

答:
As so often, a regexp or a split will work at first glance, and then
horribly fail once data show its real face.  So as many data formats a
proper parser is usually safe route and this is also true for CSV.

Pick one you like and then it's
usually just little bit experimenting and you are good to go.  E.g.

```<code>@Grab(group='de.siegmar', module='fastcsv', version='1.0.3')
import de.siegmar.fastcsv.reader.*

def data = '''ELEMENT 1-01,"X0185-CX2F-01-PW(SATop,20211,10.42.81.0)",2020-02-18 09:00:00,15,0.00,0.00,0.00,0.00,0.00,0,0,0'''
def csvReader = new CsvReader()
try (def csvParser = csvReader.parse(new java.io.StringReader(data))) {
    def row = csvParser.nextRow()
    assert row.getField(1)=='X0185-CX2F-01-PW(SATop,20211,10.42.81.0)'
}
</code>```

If you are not convinced, think about the next problems you will face:

line breaks

" inside quoted cells

Body:
I am using groovy to parse a in a file which needs to be split into an array using groovy.

The problem is that the used delimiter "," is in the one field so a the split command is not correctly applied. The output will use a different delimiter (e.g. "|")

ELEMENT 1-01,"X0185-CX2F-01-PW(SATop,20211,10.42.81.0)",2020-02-18 09:00:00,15,0.00,0.00,0.00,0.00,0.00,0,0,0

Does the groovy split(",") function consider the field as a single string literal?

Body:
As so often, a regexp or a split will work at first glance, and then
horribly fail once data show its real face.  So as many data formats a
proper parser is usually safe route and this is also true for CSV.

Pick one you like and then it's
usually just little bit experimenting and you are good to go.  E.g.

@Grab(group='de.siegmar', module='fastcsv', version='1.0.3')
import de.siegmar.fastcsv.reader.*

def data = '''ELEMENT 1-01,"X0185-CX2F-01-PW(SATop,20211,10.42.81.0)",2020-02-18 09:00:00,15,0.00,0.00,0.00,0.00,0.00,0,0,0'''
def csvReader = new CsvReader()
try (def csvParser = csvReader.parse(new java.io.StringReader(data))) {
    def row = csvParser.nextRow()
    assert row.getField(1)=='X0185-CX2F-01-PW(SATop,20211,10.42.81.0)'
}

If you are not convinced, think about the next problems you will face:

line breaks

" inside quoted cells


问:
Detect focus initiated by tab key?
I want to detect the focus event of an element, but only if it was initiated by the user pressing the tab key. For example:

```<code><input type="text" id="foo" />
<input type="text" id="detect" />
</code>```

If the user is focused on #foo and presses Tab, I want the event to fire once #detect becomes focused (or a conditional inside the focus event to be true). Conversely, if the user simply clicks on the #detect field to focus it, I do not want the event to fire (or I want the conditional inside the focus event call to be false).

I don't want to use the keydown event of #foo and check if the tab key was pressed, as I want the approach to be independent of any other element.

I looked through the console output of the following code, but couldn't notice any real differences between the two methods of focusing:

```<code>$('#detect').on('focus', function(e){
   console.log(e);
});
</code>```

(fiddle)

Is this possible to accomplish in a relatively simple way?

答:
I use the following:

(function() {
  const tabHistory = [];

  window.addEventListener('keyup', function (e) {
    const code = e.keyCode || e.which;
    const index = tabHistory.length === 0 ? 1 : tabHistory.length + 1;

    if (code == 9) {
      tabHistory.push({
        element: e.target,
        index
      });

      console.log(index, e.target, tabHistory);
    }
  });
})();
```

I recommend keeping a track of the focused elements, this way you make sure the user will be able to tab as you expected.

Hope it helps!

Title: Detect focus initiated by tab key?

Body:
I want to detect the focus event of an element, but only if it was initiated by the user pressing the tab key. For example:

<input type="text" id="foo" />
<input type="text" id="detect" />

If the user is focused on #foo and presses Tab, I want the event to fire once #detect becomes focused (or a conditional inside the focus event to be true). Conversely, if the user simply clicks on the #detect field to focus it, I do not want the event to fire (or I want the conditional inside the focus event call to be false).

I don't want to use the keydown event of #foo and check if the tab key was pressed, as I want the approach to be independent of any other element.

I looked through the console output of the following code, but couldn't notice any real differences between the two methods of focusing:

$('#detect').on('focus', function(e){
   console.log(e);
});

(fiddle)

Is this possible to accomplish in a relatively simple way?

Body:
I use the following:

(function() {
  const tabHistory = [];

  window.addEventListener('keyup', function (e) {
    const code = e.keyCode || e.which;
    const index = tabHistory.length === 0 ? 1 : tabHistory.length + 1;

    if (code == 9) {
      tabHistory.push({
        element: e.target,
        index
      });

      console.log(index, e.target, tabHistory);
    }
  });
})();

I recommend keeping a track of the focused elements, this way you make sure the user will be able to tab as you expected.

Hope it helps!


问:
Importing model classes from other apps without causing circular reference in Django
I have 3 apps products, sales, purchases. each app has a correspondingly named Model class, Product, Sale, and Purchase.

```<code>products/models.py
class Product(models.Model):
    Name = models.CharField(max_length=32)

sales/models.py
class Sale(models.Model):
    Product = models.ForeignKey('products.Product', on_delete=models.CASCADE)

purchases/models.py
class Purchase(models.Model):
    Product = models.ForeignKey('products.Product', on_delete=models.CASCADE)
</code>```

And I decided to make custom managers for the Model classes so that I can keep all the logic in the model files (by overriding objects attr for each class) when I'm writing the methods in the custom manager I imported Sale model In products.models and Product model in sales.models which creates a circular reference, I was able to get away with it by performing the imports in the methods themselves but I remember reading online that circular imports are sign of bad code writing.

So my question is how can I avoid circular imports in this case and have access to the Product Model in sales.models and Sale in products.models.

答:
Running into circular importing means you are fighting against the separation you've created, or you're not writing appropriately decoupled code. In your case, I think you may be trying too hard to separate these models into separate apps. Surely sales and purchases are part of the same set of functionality and share lots of business logic. Even products could live in the same app, although you might have more of an argument there.

If you're coming from another language where it's customary not to have multiple classes in a single file, you should know that is not a best practice in python. Note in the official Django tutorial, the polls app has both Question and Answer models in the same file.

I think a single app named commerce with all three models in the same models.py file would make sense.

# commerce/models.py

class Product(models.Model):
    name = models.CharField(max_length=32)

class Sale(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE)

class Purchase(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
```

Body:
I have 3 apps products, sales, purchases. each app has a correspondingly named Model class, Product, Sale, and Purchase.

products/models.py
class Product(models.Model):
    Name = models.CharField(max_length=32)

sales/models.py
class Sale(models.Model):
    Product = models.ForeignKey('products.Product', on_delete=models.CASCADE)

purchases/models.py
class Purchase(models.Model):
    Product = models.ForeignKey('products.Product', on_delete=models.CASCADE)

And I decided to make custom managers for the Model classes so that I can keep all the logic in the model files (by overriding objects attr for each class) when I'm writing the methods in the custom manager I imported Sale model In products.models and Product model in sales.models which creates a circular reference, I was able to get away with it by performing the imports in the methods themselves but I remember reading online that circular imports are sign of bad code writing.

So my question is how can I avoid circular imports in this case and have access to the Product Model in sales.models and Sale in products.models.