问:
Multiple onStop / onResume calls in Android activity

Background

When putting my application to background (navigating elsewhere on my Android phone), onStop() is called once, just as expected (Activity lifecycle)

Question rationale: the onStop() contains code which developer expects to be run once.

Question

Why, in case I am locking my phone (single power button press) while in my application's activity - onStop() gets called 3 times?

Update

CASE #1

```<code>05-28 14:06:29.322 24347-24347/com... D/DEBUG: onCreate
05-28 14:06:29.380 24347-24347/com... D/DEBUG: onStart
05-28 14:06:29.380 24347-24347/com... D/DEBUG: onResume
</code>```

While activity is running, I navigate elsewhere off the app:

```<code>05-28 14:06:31.954 24347-24347/com... D/DEBUG: onStop
</code>```

At this point I navigate back to the app and open it:

```<code>05-28 14:06:34.314 24347-24347/com... D/DEBUG: onStart
05-28 14:06:34.314 24347-24347/com... D/DEBUG: onResume
</code>```

CASE #2

```<code>05-28 14:09:01.411 24347-24347/com... D/DEBUG: onCreate
05-28 14:09:01.453 24347-24347/com... D/DEBUG: onStart
05-28 14:09:01.453 24347-24347/com... D/DEBUG: onResume
</code>```

At this point I lock my device:

```<code>05-28 14:35:55.017 30961-30961/com... D/DEBUG: onStop
05-28 14:35:55.278 30961-30961/com... D/DEBUG: onDestroy
05-28 14:35:55.367 30961-30961/com... D/DEBUG: onCreate
05-28 14:35:55.411 30961-30961/com... D/DEBUG: onStart
05-28 14:35:55.419 30961-30961/com... D/DEBUG: onResume
05-28 14:35:55.440 30961-30961/com... D/DEBUG: onStop
05-28 14:35:55.441 30961-30961/com... D/DEBUG: onDestroy
05-28 14:35:55.451 30961-30961/com... D/DEBUG: onCreate
05-28 14:35:55.493 30961-30961/com... D/DEBUG: onStart
05-28 14:35:55.502 30961-30961/com... D/DEBUG: onResume
05-28 14:35:55.830 30961-30961/com... D/DEBUG: onStop
05-28 14:35:55.969 30961-30961/com... D/DEBUG: onDestroy
05-28 14:35:56.004 30961-30961/com... D/DEBUG: onCreate
05-28 14:35:56.044 30961-30961/com... D/DEBUG: onStart
05-28 14:35:56.052 30961-30961/com... D/DEBUG: onResume
</code>```

I unlock it to see the activity which is still present:

```<code>05-28 14:39:38.225 30961-30961/com... D/DEBUG: onResume
05-28 14:39:38.415 30961-30961/com... D/DEBUG: onStop
05-28 14:39:38.416 30961-30961/com... D/DEBUG: onDestroy
05-28 14:39:38.465 30961-30961/com... D/DEBUG: onCreate
05-28 14:39:38.610 30961-30961/com... D/DEBUG: onStart
05-28 14:39:38.624 30961-30961/com... D/DEBUG: onResume
</code>```

Not sure if it is relevant, but the activity makes use of VLC library to play video. Same result is reached whether the video is currently on playback or not.

Update

My activity, in AndroidManifest.xml is defined with android:screenOrientation="sensorLandscape". The problem disappears once the property is removed (android:screenOrientation="unspecified").

答:
Correct issue was pointed out by David Wasser

The cause of activity being destroyed and re-created is similar to change of orientation.

My activity is using android:screenOrientation="sensorLandscape", and I had no clue the "orientation" might change when destroying the activity.

My application depends both on onStop() and onResume() calls, so I will implement onResume() as they are similar.

Solution based on Saving the Instance State

```<code>private boolean onResumeCalledAlready;
private String ON_RESUME_CALLED_PREFERENCE_KEY = "onResumeCalled";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Record onResume not called yet
    if (savedInstanceState != null) {
        // Restore value of members from saved state
        onResumeCalledAlready = savedInstanceState.getBoolean(ON_RESUME_CALLED_PREFERENCE_KEY);
    } else {
        onResumeCalledAlready = false;
    }
}

@Override
protected void onResume() {
    super.onResume();
    if (!onResumeCalledAlready) {
        onResumeCalledAlready = true;
        // Do once
        callMeOnlyOnce();
    }
    // Everything else might be called multiple times
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    // Save current onResumeCalledAlready state
    outState.putBoolean(ON_RESUME_CALLED_PREFERENCE_KEY, onResumeCalledAlready);
    super.onSaveInstanceState(outState);
}
</code>```

Body:
Background

When putting my application to background (navigating elsewhere on my Android phone), onStop() is called once, just as expected (Activity lifecycle)

Question rationale: the onStop() contains code which developer expects to be run once.

Question

Why, in case I am locking my phone (single power button press) while in my application's activity - onStop() gets called 3 times?

Update

CASE #1

05-28 14:06:29.322 24347-24347/com... D/DEBUG: onCreate
05-28 14:06:29.380 24347-24347/com... D/DEBUG: onStart
05-28 14:06:29.380 24347-24347/com... D/DEBUG: onResume

While activity is running, I navigate elsewhere off the app:

05-28 14:06:31.954 24347-24347/com... D/DEBUG: onStop

At this point I navigate back to the app and open it:

05-28 14:06:34.314 24347-24347/com... D/DEBUG: onStart
05-28 14:06:34.314 24347-24347/com... D/DEBUG: onResume

CASE #2

05-28 14:09:01.411 24347-24347/com... D/DEBUG: onCreate
05-28 14:09:01.453 24347-24347/com... D/DEBUG: onStart
05-28 14:09:01.453 24347-24347/com... D/DEBUG: onResume

At this point I lock my device:

05-28 14:35:55.017 30961-30961/com... D/DEBUG: onStop
05-28 14:35:55.278 30961-30961/com... D/DEBUG: onDestroy
05-28 14:35:55.367 30961-30961/com... D/DEBUG: onCreate
05-28 14:35:55.411 30961-30961/com... D/DEBUG: onStart
05-28 14:35:55.419 30961-30961/com... D/DEBUG: onResume
05-28 14:35:55.440 30961-30961/com... D/DEBUG: onStop
05-28 14:35:55.441 30961-30961/com... D/DEBUG: onDestroy
05-28 14:35:55.451 30961-30961/com... D/DEBUG: onCreate
05-28 14:35:55.493 30961-30961/com... D/DEBUG: onStart
05-28 14:35:55.502 30961-30961/com... D/DEBUG: onResume
05-28 14:35:55.830 30961-30961/com... D/DEBUG: onStop
05-28 14:35:55.969 30961-30961/com... D/DEBUG: onDestroy
05-28 14:35:56.004 30961-30961/com... D/DEBUG: onCreate
05-28 14:35:56.044 30961-30961/com... D/DEBUG: onStart
05-28 14:35:56.052 30961-30961/com... D/DEBUG: onResume

I unlock it to see the activity which is still present:

05-28 14:39:38.225 30961-30961/com... D/DEBUG: onResume
05-28 14:39:38.415 30961-30961/com... D/DEBUG: onStop
05-28 14:39:38.416 30961-30961/com... D/DEBUG: onDestroy
05-28 14:39:38.465 30961-30961/com... D/DEBUG: onCreate
05-28 14:39:38.610 30961-30961/com... D/DEBUG: onStart
05-28 14:39:38.624 30961-30961/com... D/DEBUG: onResume

Not sure if it is relevant, but the activity makes use of VLC library to play video. Same result is reached whether the video is currently on playback or not.

Update

My activity, in AndroidManifest.xml is defined with android:screenOrientation="sensorLandscape". The problem disappears once the property is removed (android:screenOrientation="unspecified").

Body:
Correct issue was pointed out by David Wasser

The cause of activity being destroyed and re-created is similar to change of orientation.

My activity is using android:screenOrientation="sensorLandscape", and I had no clue the "orientation" might change when destroying the activity.

My application depends both on onStop() and onResume() calls, so I will implement onResume() as they are similar.

Solution based on Saving the Instance State

private boolean onResumeCalledAlready;
private String ON_RESUME_CALLED_PREFERENCE_KEY = "onResumeCalled";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Record onResume not called yet
    if (savedInstanceState != null) {
        // Restore value of members from saved state
        onResumeCalledAlready = savedInstanceState.getBoolean(ON_RESUME_CALLED_PREFERENCE_KEY);
    } else {
        onResumeCalledAlready = false;
    }
}

@Override
protected void onResume() {
    super.onResume();
    if (!onResumeCalledAlready) {
        onResumeCalledAlready = true;
        // Do once
        callMeOnlyOnce();
    }
    // Everything else might be called multiple times
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    // Save current onResumeCalledAlready state
    outState.putBoolean(ON_RESUME_CALLED_PREFERENCE_KEY, onResumeCalledAlready);
    super.onSaveInstanceState(outState);
}


来源: stackexchange

问:
What is a 'non-nominal type' in Swift?
I see this error when trying to extend unorthodox 'types' like (Int, Int), or Any:

Non-nominal type 'Any' cannot be extended

So what makes a type non-nominal? What's the difference between a non-nominal type like Any or (Int) and a regular nominal type like Int?

答:
The currently accepted answer is incorrect; the actual answer comes from a deep and esoteric part of Swift's type system.

Most types in Swift are nominal types—they're "named" types declared in a particular module as part of user code. Seemingly primitive types like Int and Array are actually structs defined in the Swift module, which is automatically imported into every Swift source file. Even Optional itself is an enum in the Swift module, although the compiler adds a little bit of magic like optional chaining and force-unwrapping.

The few exceptions are called "non-nominal types". There may be others, but the main ones are:

Function types, like (Int) -> String

Tuple types, like (Int, String)

Metatypes, like String.Type (the type of the expression String.self)

Existentials, like CustomStringConvertible & Error

Existentials deserve a little more explanation. An existential contains a value whose exact type has been abstracted away, but which is known to conform to a certain set of protocols. For example:

```<code>// You can put anything that conforms to `CustomStringConvertible` in `x`
let x: CustomStringConvertible

// You can put anything that conforms to both `CustomStringConvertible`
// and `Error` in `y`
let y: CustomStringConvertible & Error

// You can put anything in `z`; `Any` is an existential that doesn't
// require you to conform to any particular protocols
let z: Any
</code>```

Non-nominal types all simply combine other types together in some ad-hoc way. (They're sometimes called "structural types" because they define some kind of general structure for other types to slot into.) They don't need to be explicitly defined, and they don't belong to any particular module—FooKit's (Int, String) is the same as BarKit's (Int, String). But all of their behavior is defined by the language—a non-nominal type can't have methods or properties of its own, can't conform to protocols, and therefore can't be extended.

So you can't extend Any because Any is a special thing that's built into the language, just like a function type or a tuple type. It just happens to have a name written in letters, not in punctuation.

(So why can you extend CustomStringConvertible? Because, depending on context, CustomStringConvertible might mean the protocol or it might mean an existential containing a value conforming to the protocol. When you write extension CustomStringConvertible, you're extending the protocol, but when you write let x: CustomStringConvertible, you're declaring a variable whose type is "existential containing a value conforming to the protocol". This is kind of confusing, and some of Swift's maintainers would actually like to require that the existential be written as Any<CustomStringConvertible> to make it more clear. Not terribly likely to happen now that they're trying to preserve source stability, though.)

ContentLicense: CC BY-SA 3.0

Title: What is a 'non-nominal type' in Swift?

Body:
The currently accepted answer is incorrect; the actual answer comes from a deep and esoteric part of Swift's type system.

Most types in Swift are nominal types—they're "named" types declared in a particular module as part of user code. Seemingly primitive types like Int and Array are actually structs defined in the Swift module, which is automatically imported into every Swift source file. Even Optional itself is an enum in the Swift module, although the compiler adds a little bit of magic like optional chaining and force-unwrapping.

The few exceptions are called "non-nominal types". There may be others, but the main ones are:

Function types, like (Int) -> String

Tuple types, like (Int, String)

Metatypes, like String.Type (the type of the expression String.self)

Existentials, like CustomStringConvertible & Error

Existentials deserve a little more explanation. An existential contains a value whose exact type has been abstracted away, but which is known to conform to a certain set of protocols. For example:

// You can put anything that conforms to `CustomStringConvertible` in `x`
let x: CustomStringConvertible

// You can put anything that conforms to both `CustomStringConvertible`
// and `Error` in `y`
let y: CustomStringConvertible & Error

// You can put anything in `z`; `Any` is an existential that doesn't
// require you to conform to any particular protocols
let z: Any

Non-nominal types all simply combine other types together in some ad-hoc way. (They're sometimes called "structural types" because they define some kind of general structure for other types to slot into.) They don't need to be explicitly defined, and they don't belong to any particular module—FooKit's (Int, String) is the same as BarKit's (Int, String). But all of their behavior is defined by the language—a non-nominal type can't have methods or properties of its own, can't conform to protocols, and therefore can't be extended.

So you can't extend Any because Any is a special thing that's built into the language, just like a function type or a tuple type. It just happens to have a name written in letters, not in punctuation.

(So why can you extend CustomStringConvertible? Because, depending on context, CustomStringConvertible might mean the protocol or it might mean an existential containing a value conforming to the protocol. When you write extension CustomStringConvertible, you're extending the protocol, but when you write let x: CustomStringConvertible, you're declaring a variable whose type is "existential containing a value conforming to the protocol". This is kind of confusing, and some of Swift's maintainers would actually like to require that the existential be written as Any<CustomStringConvertible> to make it more clear. Not terribly likely to happen now that they're trying to preserve source stability, though.)

ContentLicense: CC BY-SA 4.0


问:
passing variable from one script to another in shell scripting
I have a script suppose script.sh
it has two scripts to run parallaly inside

```<code>sh script1.sh &
sh script2.sh &
wait;
echo $var1
echo $var2
</code>```

i want to get these two variable(var1,var2) printed while running script.sh

```<code>cat script1.sh

var1=1;
export $var1

cat script2.sh

var1=1;
export $var1
</code>```

答:
This can be done with coprocesses in bash 4.x, if your script1 and script2 write their variables to stdout:

```<code>#!/usr/bin/env bash
#              ^^^- Must be bash, not /bin/sh; must NOT start this script with "sh name".

start_seconds=$SECONDS
run_script1() { sleep 5; echo "one"; }  # replace these with your script1 or script2
run_script2() { sleep 5; echo "two"; }

coproc coproc_script1 { run_script1; }
coproc coproc_script2 { run_script2; }

read -u "${coproc_script1[0]}" var1
read -u "${coproc_script2[0]}" var2
echo "End time: $(( SECONDS - start_seconds )); var1=$var1; var2=$var2"
</code>```

If the values weren't running in parallel, your End time would be 10 or more. Instead, it should in practice be 5 or 6 with the above code.

Body:
I have a script suppose script.sh
it has two scripts to run parallaly inside

sh script1.sh &
sh script2.sh &
wait;
echo $var1
echo $var2

i want to get these two variable(var1,var2) printed while running script.sh

cat script1.sh

var1=1;
export $var1

cat script2.sh

var1=1;
export $var1

Body:
This can be done with coprocesses in bash 4.x, if your script1 and script2 write their variables to stdout:

#!/usr/bin/env bash
#              ^^^- Must be bash, not /bin/sh; must NOT start this script with "sh name".

start_seconds=$SECONDS
run_script1() { sleep 5; echo "one"; }  # replace these with your script1 or script2
run_script2() { sleep 5; echo "two"; }

coproc coproc_script1 { run_script1; }
coproc coproc_script2 { run_script2; }

read -u "${coproc_script1[0]}" var1
read -u "${coproc_script2[0]}" var2
echo "End time: $(( SECONDS - start_seconds )); var1=$var1; var2=$var2"

If the values weren't running in parallel, your End time would be 10 or more. Instead, it should in practice be 5 or 6 with the above code.


问:
Delete array from ionic2 local storage
my data gets stored in an string

i use JSON.parse to convert data to arrays

```<code>this.items = JSON.parse(todos);
</code>```

I have a results page which prints out my arrays:
Array1
Array2
Array3

I have a delete button after every array which deletes the item from the list but not from local storage. Am I doing something wrong?

list.html

```<code><ion-item-sliding *ngFor="let item of items">
  <ion-item>
  <p>{{item.amount}}tk X {{item.class}} / {{item.size}}ml / {{item.proof}}%</p>
  </ion-item>
  <ion-item-options>
  <button danger (click)="removePost(item)">
  <ion-icon name="trash"></ion-icon>Remove
  </button>
  </ion-item-options>
  </ion-item-sliding>
</code>```

list.ts

```<code>removePost(item){
    let index = this.items.indexOf(item);
   if(index > -1){
      this.items.splice(index, 1); // works
      this.storage.remove(this.items[index]); // doesn't work
    }
}
</code>```

答:
if you have a JSON array in the local storage as a string and if you want to remove an object from local storage array then this will not work.

you can't remove an object because the whole value of localstorage is a string value. what you can do is after removing the value using splice you can set the localstorage again

```<code>removePost(item){
   let index = this.items.indexOf(item);
   if(index > -1){
      this.items.splice(index, 1); // works
      //this.storage.remove(this.items[index]); // doesn't work
      localStorage.setitem("name_of_the_storage",JSON.stringify(this.items))
    }
}
</code>```

Title: Delete array from ionic2 local storage

Body:
my data gets stored in an string

i use JSON.parse to convert data to arrays

this.items = JSON.parse(todos);

I have a results page which prints out my arrays:
Array1
Array2
Array3

I have a delete button after every array which deletes the item from the list but not from local storage. Am I doing something wrong?

list.html

<ion-item-sliding *ngFor="let item of items">
  <ion-item>

{{item.amount}}tk X {{item.class}} / {{item.size}}ml / {{item.proof}}%

  </ion-item>
  <ion-item-options>
  <button danger (click)="removePost(item)">
  <ion-icon name="trash"></ion-icon>Remove
  </button>
  </ion-item-options>
  </ion-item-sliding>

list.ts

removePost(item){
    let index = this.items.indexOf(item);
   if(index > -1){
      this.items.splice(index, 1); // works
      this.storage.remove(this.items[index]); // doesn't work
    }
}

Body:
if you have a JSON array in the local storage as a string and if you want to remove an object from local storage array then this will not work.

you can't remove an object because the whole value of localstorage is a string value. what you can do is after removing the value using splice you can set the localstorage again

removePost(item){
   let index = this.items.indexOf(item);
   if(index > -1){
      this.items.splice(index, 1); // works
      //this.storage.remove(this.items[index]); // doesn't work
      localStorage.setitem("name_of_the_storage",JSON.stringify(this.items))
    }
}


问:
Shell script to parse through a file ( csv ) and process line by line
Hi Need a shell script to parse through the csv file - Line by line and then field by field ]

the file will look like this

```<code>X1,X2,X3,X4
Y1,Y2,Y3,Y4
</code>```

I need to extract each of these X1,X2....

I wrote a script but it fails if the line exceeds one line..

答:
Here's how I would do it.

First i set the IFS environment variable to tell read that "," is the field separator.

```<code>export IFS=","
</code>```

Given the file "input" containing the data you provided, I can use the following code:

```<code>cat test | while read a b c d; do echo "$a:$b:$c:$d"; done
</code>```

To quickly recap what is going on above. cat test | reads the file and pipes it to while. while runs the code between do and done while read returns true. read reads a line from standard input and separates it into variables ("a", "b", "c" and "d") according to the value of $IFS. Finally echo just displays the variables we read.

Which gives me the following output

```<code>X1:X2:X3:X4
Y1:Y2:Y3:Y4
</code>```

BTW, the BASH manual is always good reading. You'll learn something new every time you read it.

Body:
Hi Need a shell script to parse through the csv file - Line by line and then field by field ]

the file will look like this

X1,X2,X3,X4
Y1,Y2,Y3,Y4

I need to extract each of these X1,X2....

I wrote a script but it fails if the line exceeds one line..

Body:
Here's how I would do it.

First i set the IFS environment variable to tell read that "," is the field separator.

export IFS=","

Given the file "input" containing the data you provided, I can use the following code:

cat test | while read a b c d; do echo "$a:$b:$c:$d"; done

To quickly recap what is going on above. cat test | reads the file and pipes it to while. while runs the code between do and done while read returns true. read reads a line from standard input and separates it into variables ("a", "b", "c" and "d") according to the value of $IFS. Finally echo just displays the variables we read.

Which gives me the following output

X1:X2:X3:X4
Y1:Y2:Y3:Y4

BTW, the BASH manual is always good reading. You'll learn something new every time you read it.

ContentLicense: CC BY-SA 2.5


问:
Get complete cases of data frame based on formular
I have a data frame with panel data and I would like to get all complete observations based on a formular.

While extracting the variables from the formula is pretty straightforward (using get_all_vars), I would also like to incorporate things like lags (in this case from the dplyr-package) in formulas.

Let's take the formular:

```<code>y ~ x1 + lag(x2, 1) + lag(x3, 2)
</code>```

and the data frame

```<code>  y    x1  x2   x3
1 2    3   NA   NA
2 3    2   2    2
3 2    6   5    3
4 5    8   6    9
5 1    3   7    4
6 1    0   1    2
</code>```

I would like to get something like this:

```<code>  y    x1  x2   x3
1 2    3   NA   NA
2 3    2   NA   NA
3 2    6   2    NA
4 5    8   5    2
5 1    3   6    3
6 1    0   7    9
</code>```

In the end I want to use this to create dummies for the different time periods (and since my panel is unbalanced and for some years no case is complete, creating them for all periods does not work).

Any suggestions?

答:
you can accomplish your example output given you sample input using model.frame

```<code>my_df <- read.table(text=" y    x1  x2   x3
                         1 2    3   NA   NA
                         2 3    2   2    2
                         3 2    6   5    3
                         4 5    8   6    9
                         5 1    3   7    4
                         6 1    0   1    2 ", header=T)

out_df <- model.frame(y ~ x1 + dplyr::lag(x2, 1) + dplyr::lag(x3, 2),
                      data=my_df,
                      na.action=NULL)
names(out_df) <- names(my_df)
out_df

#output
    y x1 x2 x3
  1 2  3 NA NA
  2 3  2 NA NA
  3 2  6  2 NA
  4 5  8  5  2
  5 1  3  6  3
  6 1  0  7  9
</code>```

Body:
I have a data frame with panel data and I would like to get all complete observations based on a formular.

While extracting the variables from the formula is pretty straightforward (using get_all_vars), I would also like to incorporate things like lags (in this case from the dplyr-package) in formulas.

Let's take the formular:

y ~ x1 + lag(x2, 1) + lag(x3, 2)

and the data frame

  y    x1  x2   x3
1 2    3   NA   NA
2 3    2   2    2
3 2    6   5    3
4 5    8   6    9
5 1    3   7    4
6 1    0   1    2

I would like to get something like this:

  y    x1  x2   x3
1 2    3   NA   NA
2 3    2   NA   NA
3 2    6   2    NA
4 5    8   5    2
5 1    3   6    3
6 1    0   7    9

In the end I want to use this to create dummies for the different time periods (and since my panel is unbalanced and for some years no case is complete, creating them for all periods does not work).

Any suggestions?

Body:
you can accomplish your example output given you sample input using model.frame

my_df <- read.table(text=" y    x1  x2   x3
                         1 2    3   NA   NA
                         2 3    2   2    2
                         3 2    6   5    3
                         4 5    8   6    9
                         5 1    3   7    4
                         6 1    0   1    2 ", header=T)

out_df <- model.frame(y ~ x1 + dplyr::lag(x2, 1) + dplyr::lag(x3, 2),
                      data=my_df,
                      na.action=NULL)
names(out_df) <- names(my_df)
out_df

#output
    y x1 x2 x3
  1 2  3 NA NA
  2 3  2 NA NA
  3 2  6  2 NA
  4 5  8  5  2
  5 1  3  6  3
  6 1  0  7  9


问:
Powershell convert string to array
How do I change:

```<code>$Text = "Apple Pear Peach Banana"
</code>```

to

```<code>$Text = @("Apple", "Pear", "Peach", "Banana")
</code>```

I am planning to feed the array to a foreach loop. The input the user is prompted to enter fruit with a space between (I will use Read-Host for that). So then I need to convert a space-separated string to an array for the foreach loop.

Thank you...

答:
I would use the -split regex operator, like so:

```<code>$text = -split $text
</code>```

You can also use it directly in the foreach() loop declaration:

```<code>foreach($fruit in -split $text)
{
    "$fruit is a fruit"
}
</code>```

In unary mode (like above), -split defaults to splitting on the delimeter \s+ (1 or more whitespace characters).

This is nice if a user accidentally enters consecutive spaces:

```<code>PS C:\> $text = Read-Host 'Input fruit names'
Input fruit names: Apple Pear   Peaches  Banana
PS C:\> $text = -split $text
PS C:\> $text
Apple
Pear
Peaches
Banana
</code>```

Title: Powershell convert string to array

Body:
How do I change:

$Text = "Apple Pear Peach Banana"

to

$Text = @("Apple", "Pear", "Peach", "Banana")

I am planning to feed the array to a foreach loop. The input the user is prompted to enter fruit with a space between (I will use Read-Host for that). So then I need to convert a space-separated string to an array for the foreach loop.

Thank you...

Body:
I would use the -split regex operator, like so:

$text = -split $text

You can also use it directly in the foreach() loop declaration:

foreach($fruit in -split $text)
{
    "$fruit is a fruit"
}

In unary mode (like above), -split defaults to splitting on the delimeter \s+ (1 or more whitespace characters).

This is nice if a user accidentally enters consecutive spaces:

PS C:\> $text = Read-Host 'Input fruit names'
Input fruit names: Apple Pear   Peaches  Banana
PS C:\> $text = -split $text
PS C:\> $text
Apple
Pear
Peaches
Banana


问:
Override default function in typescript class
I have made this button class in typescript. I have given it a "default" function to handle click events. But what if i instead want to be to pass a function as an argument and call the passed one on click instead?

Here the code:

```<code>abstract class BaseButton {

  element:HTMLElement;
  label:string;

  constructor(element:HTMLElement, label:string) {
    console.log(this);
    this.element = element;
    this.label = label;
    this.init();
  }

  init():void {
    this.setText(this.label);
    this.addListeners();
  }

  setText(label:string):void {
    this.element.innerHTML = label;
  }

  kill():void {
    this.removeListeners();
  }

  addListeners():void {
    this.element.addEventListener("mouseover", this.onMouseOver);
    this.element.addEventListener("click", this.onClick);
    this.element.addEventListener("mouseout", this.onMouseOut);
    this.element.addEventListener('touchstart', this.onTouchStart);
    this.element.addEventListener('touchmove', this.onTouchMove);
    this.element.addEventListener('touchcancel', this.onTouchCancel);
    this.element.addEventListener('touchend', this.onTouchEnd);
  }

  removeListeners():void {
    this.element.removeEventListener("mouseover", this.onMouseOver);
    this.element.removeEventListener("click", this.onClick);
    this.element.removeEventListener("mouseout", this.onMouseOut);
    this.element.removeEventListener('touchstart', this.onTouchStart);
    this.element.removeEventListener('touchmove', this.onTouchMove);
    this.element.removeEventListener('touchcancel', this.onTouchCancel);
    this.element.removeEventListener('touchend', this.onTouchEnd);
  }

  onTouchStart():void {

  }

  onTouchMove():void {

  }

  onTouchCancel():void {

  }

  onTouchEnd():void {

  }

  onMouseOver():void {
    console.log('mouse over');
  }

  onMouseOut():void {
    console.log('mouse out');
  }

  abstract onClick(event:Event):void
}

class Button extends BaseButton {

  element:HTMLElement;
  label:string;
  callback:any;

  constructor(element:HTMLElement, label:string, callback?: () => void) {
    super(element,label);
    this.element = element;
    this.label = label;
    this.callback = callback;
  }

  onClick(event:Event):void {
    if (this.callback) {
      console.log('ddd');
      this.callback();
    }
  }
}

const el = document.getElementById('btn');
const button = new Button(el,'Click');

const ell = document.getElementById('btnn');
const buttonn = new Button(ell,'Click');

const elll = document.getElementById('btnnn');
const buttonnn = new Button(elll,'Click', () => {
  alert('fff');
});
</code>```

答:
What if you did something like this (with the rest of your code added)?</p>

```<code>class Button {
    clickListener; // with any types

    constructor(clickListener = defaultClickListener) {
        this.clickListener = clickListener;
        this.clickListener();
    }
}

function defaultClickListener():void {
    console.log(0);
}

new Button(() => console.log(1));
</code>```

Body:
I have made this button class in typescript. I have given it a "default" function to handle click events. But what if i instead want to be to pass a function as an argument and call the passed one on click instead?

Here the code:

abstract class BaseButton {

  element:HTMLElement;
  label:string;

  constructor(element:HTMLElement, label:string) {
    console.log(this);
    this.element = element;
    this.label = label;
    this.init();
  }

  init():void {
    this.setText(this.label);
    this.addListeners();
  }

  setText(label:string):void {
    this.element.innerHTML = label;
  }

  kill():void {
    this.removeListeners();
  }

  addListeners():void {
    this.element.addEventListener("mouseover", this.onMouseOver);
    this.element.addEventListener("click", this.onClick);
    this.element.addEventListener("mouseout", this.onMouseOut);
    this.element.addEventListener('touchstart', this.onTouchStart);
    this.element.addEventListener('touchmove', this.onTouchMove);
    this.element.addEventListener('touchcancel', this.onTouchCancel);
    this.element.addEventListener('touchend', this.onTouchEnd);
  }

  removeListeners():void {
    this.element.removeEventListener("mouseover", this.onMouseOver);
    this.element.removeEventListener("click", this.onClick);
    this.element.removeEventListener("mouseout", this.onMouseOut);
    this.element.removeEventListener('touchstart', this.onTouchStart);
    this.element.removeEventListener('touchmove', this.onTouchMove);
    this.element.removeEventListener('touchcancel', this.onTouchCancel);
    this.element.removeEventListener('touchend', this.onTouchEnd);
  }

  onTouchStart():void {

  }

  onTouchMove():void {

  }

  onTouchCancel():void {

  }

  onTouchEnd():void {

  }

  onMouseOver():void {
    console.log('mouse over');
  }

  onMouseOut():void {
    console.log('mouse out');
  }

  abstract onClick(event:Event):void
}

class Button extends BaseButton {

  element:HTMLElement;
  label:string;
  callback:any;

  constructor(element:HTMLElement, label:string, callback?: () => void) {
    super(element,label);
    this.element = element;
    this.label = label;
    this.callback = callback;
  }

  onClick(event:Event):void {
    if (this.callback) {
      console.log('ddd');
      this.callback();
    }
  }
}

const el = document.getElementById('btn');
const button = new Button(el,'Click');

const ell = document.getElementById('btnn');
const buttonn = new Button(ell,'Click');

const elll = document.getElementById('btnnn');
const buttonnn = new Button(elll,'Click', () => {
  alert('fff');
});

Body:
What if you did something like this (with the rest of your code added)?

class Button {
    clickListener; // with any types

    constructor(clickListener = defaultClickListener) {
        this.clickListener = clickListener;
        this.clickListener();
    }
}

function defaultClickListener():void {
    console.log(0);
}

new Button(() => console.log(1));


问:
Spring Batch: JdbcPagingItemReader does not work through setSortKeys and cast(id as unsigned)
I am working with Spring Batch version 3.0.7 with Mysql

I had the problem that the person_reader table has the id column defined how varchar and thus the following happens for a simple select ordered:

```<code>id
--
1
10 <--- !!!
2
3
...
</code>```

What is need it is

```<code>id
--
1
2
3
...
10 <--- ok!
...
</code>```

The solution is use ORDER BY cast(id as unsigned)

If I use JdbcCursorItemReader I can use in peace:

```<code>setSql("SELECT id, first_name, last_name FROM person_reader ORDER BY cast(id as unsigned)");
</code>```

It works fine. Until here all is ok.

Note: for the batch process, is very important read the data in the correct order.

The problem is with JdbcPagingItemReader

First Try:

```<code>    JdbcPagingItemReader<Person> itemReader = new JdbcPagingItemReader<>();
    itemReader.setDataSource(dataSource);
    itemReader.setPageSize(100);
    itemReader.setRowMapper(new PersonRowMapper());

    MySqlPagingQueryProvider pagingQueryProvider = new MySqlPagingQueryProvider();
    pagingQueryProvider.setSelectClause("SELECT id, first_name, last_name");
    pagingQueryProvider.setFromClause("FROM person_reader ORDER BY cast(id as unsigned)");

    Map<String, Order> sortKeys= new HashMap<>();
    sortKeys.put("id", Order.ASCENDING);
    pagingQueryProvider.setSortKeys(sortKeys);

    itemReader.setQueryProvider(pagingQueryProvider);
</code>```

Observe:

setFromClause("FROM person_reader ORDER BY cast(id as unsigned)")

put("id", Order.ASCENDING)

The following error appears:

```<code>org.springframework.jdbc.BadSqlGrammarException: StatementCallback;
bad SQL grammar [SELECT id, first_name, last_name FROM person_reader ORDER BY cast(id as unsigned) ORDER BY id ASC LIMIT 100];
nested exception is java.sql.SQLSyntaxErrorException:
You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY id ASC LIMIT 100' at line 1
</code>```

Has sense, the SQL generated SELECT id, first_name, last_name FROM person_reader ORDER BY cast(id as unsigned) ORDER BY id ASC LIMIT 100 is not correct.

Second Try

```<code>    JdbcPagingItemReader<Person> itemReader = new JdbcPagingItemReader<>();
    itemReader.setDataSource(dataSource);
    itemReader.setPageSize(100);
    itemReader.setRowMapper(new PersonRowMapper());

    MySqlPagingQueryProvider pagingQueryProvider = new MySqlPagingQueryProvider();
    pagingQueryProvider.setSelectClause("SELECT id, first_name, last_name");
    pagingQueryProvider.setFromClause("FROM person_reader");

    Map<String, Order> sortKeys= new HashMap<>();
    sortKeys.put("cast(id as unsigned)", Order.ASCENDING);
    pagingQueryProvider.setSortKeys(sortKeys);

    itemReader.setQueryProvider(pagingQueryProvider);
</code>```

Observe:

setFromClause("FROM person_reader")

put("cast(id as unsigned)", Order.ASCENDING)

The following error appears:

```<code>org.springframework.jdbc.UncategorizedSQLException: StatementCallback;
uncategorized SQLException for SQL [SELECT id, first_name, last_name FROM person_reader ORDER BY cast(id as unsigned) ASC LIMIT 100];
SQL state [S0022]; error code [0]; Column 'cast(id as unsigned)' not found.;
nested exception is java.sql.SQLException: Column 'cast(id as unsigned)' not found.
</code>```

Has sense about the 'missing' column

Order is an enum and thus it can't be extended.

What is the correct approach?

Alpha

If I use in MySQL's workbench the following:

SELECT id, first_name, last_name, cast(id as unsigned) as sb_sort_column FROM person_reader ORDER BY sb_sort_column ASC

It works fine. If I use:

```<code>MySqlPagingQueryProvider pagingQueryProvider = new MySqlPagingQueryProvider();
pagingQueryProvider.setSelectClause("SELECT id, first_name, last_name, cast(id as unsigned) as sb_sort_column");
pagingQueryProvider.setFromClause("FROM person_reader");

Map<String, Order> sortKeys= new HashMap<>();
sortKeys.put("sb_sort_column", Order.ASCENDING);
pagingQueryProvider.setSortKeys(sortKeys);

itemReader.setQueryProvider(pagingQueryProvider);
</code>```

The following error arises:

```<code>org.springframework.jdbc.BadSqlGrammarException:
PreparedStatementCallback;
bad SQL grammar [SELECT id, first_name, last_name, cast(id as unsigned) as sb_sort_column FROM person_reader WHERE ((sb_sort_column > ?)) ORDER BY sb_sort_column ASC LIMIT 100];
nested exception is java.sql.SQLSyntaxErrorException:
Unknown column 'sb_sort_column' in 'where clause'
</code>```

From above see carefully the 3rd line about the sql statement generated.

答:
Manuel's earlier answer will work, so removing the duplicate portion of mine...

Unfortunately, despite being functionally accurate, doing an ORDER BY on a function will probably run pretty poorly, since it will have to scan the entire table to do the cast, and I don't believe MySQL lets you create an index on function yet.

That said, as of MySQL 5.7.6, you could add a virtual column CAST(ID as UNSIGNED) as SORT_ID to the table and then index the virtual column.

Body:
I am working with Spring Batch version 3.0.7 with Mysql

I had the problem that the person_reader table has the id column defined how varchar and thus the following happens for a simple select ordered:

id
--
1
10 <--- !!!
2
3
...

What is need it is

id
--
1
2
3
...
10 <--- ok!
...

The solution is use ORDER BY cast(id as unsigned)

If I use JdbcCursorItemReader I can use in peace:

setSql("SELECT id, first_name, last_name FROM person_reader ORDER BY cast(id as unsigned)");

It works fine. Until here all is ok.

Note: for the batch process, is very important read the data in the correct order.

The problem is with JdbcPagingItemReader

First Try:

    JdbcPagingItemReader<Person> itemReader = new JdbcPagingItemReader<>();
    itemReader.setDataSource(dataSource);
    itemReader.setPageSize(100);
    itemReader.setRowMapper(new PersonRowMapper());

    MySqlPagingQueryProvider pagingQueryProvider = new MySqlPagingQueryProvider();
    pagingQueryProvider.setSelectClause("SELECT id, first_name, last_name");
    pagingQueryProvider.setFromClause("FROM person_reader ORDER BY cast(id as unsigned)");

    Map<String, Order> sortKeys= new HashMap<>();
    sortKeys.put("id", Order.ASCENDING);
    pagingQueryProvider.setSortKeys(sortKeys);

    itemReader.setQueryProvider(pagingQueryProvider);

Observe:

setFromClause("FROM person_reader ORDER BY cast(id as unsigned)")

put("id", Order.ASCENDING)

The following error appears:

org.springframework.jdbc.BadSqlGrammarException: StatementCallback;
bad SQL grammar [SELECT id, first_name, last_name FROM person_reader ORDER BY cast(id as unsigned) ORDER BY id ASC LIMIT 100];
nested exception is java.sql.SQLSyntaxErrorException:
You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY id ASC LIMIT 100' at line 1

Has sense, the SQL generated SELECT id, first_name, last_name FROM person_reader ORDER BY cast(id as unsigned) ORDER BY id ASC LIMIT 100 is not correct.

Second Try

    JdbcPagingItemReader<Person> itemReader = new JdbcPagingItemReader<>();
    itemReader.setDataSource(dataSource);
    itemReader.setPageSize(100);
    itemReader.setRowMapper(new PersonRowMapper());

    MySqlPagingQueryProvider pagingQueryProvider = new MySqlPagingQueryProvider();
    pagingQueryProvider.setSelectClause("SELECT id, first_name, last_name");
    pagingQueryProvider.setFromClause("FROM person_reader");

    Map<String, Order> sortKeys= new HashMap<>();
    sortKeys.put("cast(id as unsigned)", Order.ASCENDING);
    pagingQueryProvider.setSortKeys(sortKeys);

    itemReader.setQueryProvider(pagingQueryProvider);

Observe:

setFromClause("FROM person_reader")

put("cast(id as unsigned)", Order.ASCENDING)

The following error appears:

org.springframework.jdbc.UncategorizedSQLException: StatementCallback;
uncategorized SQLException for SQL [SELECT id, first_name, last_name FROM person_reader ORDER BY cast(id as unsigned) ASC LIMIT 100];
SQL state [S0022]; error code [0]; Column 'cast(id as unsigned)' not found.;
nested exception is java.sql.SQLException: Column 'cast(id as unsigned)' not found.

Has sense about the 'missing' column

Order is an enum and thus it can't be extended.

What is the correct approach?

Alpha

If I use in MySQL's workbench the following:

SELECT id, first_name, last_name, cast(id as unsigned) as sb_sort_column FROM person_reader ORDER BY sb_sort_column ASC

It works fine. If I use:

MySqlPagingQueryProvider pagingQueryProvider = new MySqlPagingQueryProvider();
pagingQueryProvider.setSelectClause("SELECT id, first_name, last_name, cast(id as unsigned) as sb_sort_column");
pagingQueryProvider.setFromClause("FROM person_reader");

Map<String, Order> sortKeys= new HashMap<>();
sortKeys.put("sb_sort_column", Order.ASCENDING);
pagingQueryProvider.setSortKeys(sortKeys);

itemReader.setQueryProvider(pagingQueryProvider);

The following error arises:

org.springframework.jdbc.BadSqlGrammarException:
PreparedStatementCallback;
bad SQL grammar [SELECT id, first_name, last_name, cast(id as unsigned) as sb_sort_column FROM person_reader WHERE ((sb_sort_column > ?)) ORDER BY sb_sort_column ASC LIMIT 100];
nested exception is java.sql.SQLSyntaxErrorException:
Unknown column 'sb_sort_column' in 'where clause'

From above see carefully the 3rd line about the sql statement generated.


问:
How to get all registered routes in Express?
I have a web application built using Node.js and Express. Now I would like to list all registered routes with their appropriate methods.

E.g., if I have executed

```<code>app.get('/', function (...) { ... });
app.get('/foo/:id', function (...) { ... });
app.post('/foo/:id', function (...) { ... });
</code>```

I would like to retrieve an object (or something equivalent to that) such as:

```<code>{
  get: [ '/', '/foo/:id' ],
  post: [ '/foo/:id' ]
}
</code>```

Is this possible, and if so, how?

答:
https://www.npmjs.com/package/express-list-endpoints works pretty well.

Example

Usage:

```<code>const all_routes = require('express-list-endpoints');
console.log(all_routes(app));
</code>```

Output:

```<code>[ { path: '*', methods: [ 'OPTIONS' ] },
  { path: '/', methods: [ 'GET' ] },
  { path: '/sessions', methods: [ 'POST' ] },
  { path: '/sessions', methods: [ 'DELETE' ] },
  { path: '/users', methods: [ 'GET' ] },
  { path: '/users', methods: [ 'POST' ] } ]
</code>```

Body:
I have a web application built using Node.js and Express. Now I would like to list all registered routes with their appropriate methods.

E.g., if I have executed

app.get('/', function (...) { ... });
app.get('/foo/:id', function (...) { ... });
app.post('/foo/:id', function (...) { ... });

I would like to retrieve an object (or something equivalent to that) such as:

{
  get: [ '/', '/foo/:id' ],
  post: [ '/foo/:id' ]
}

Is this possible, and if so, how?

Body:
https://www.npmjs.com/package/express-list-endpoints works pretty well.

Example

Usage:

const all_routes = require('express-list-endpoints');
console.log(all_routes(app));

Output:

[ { path: '*', methods: [ 'OPTIONS' ] },
  { path: '/', methods: [ 'GET' ] },
  { path: '/sessions', methods: [ 'POST' ] },
  { path: '/sessions', methods: [ 'DELETE' ] },
  { path: '/users', methods: [ 'GET' ] },
  { path: '/users', methods: [ 'POST' ] } ]


问:
I can't set up Kubernetes in Centos 7: Unable to update cni config
I am trying to follow docs to setup a one node Kubernetes cluster with Centos 7.

kubeadm init will return no error but kubectl get nodes will return:

```<code>NAME      STATUS     AGE       VERSION
[MY_IP]   NotReady   22s       v1.6.4
</code>```

system log repeats:

```<code>Jun  6 16:21:48 localhost kubelet: W0606 16:21:48.064388   11520 cni.go:157] Unable to update cni config: No networks found in /etc/cni/net.d
Jun  6 16:21:48 localhost kubelet: E0606 16:21:48.064537   11520 kubelet.go:2067] Container runtime network not ready: NetworkReady=false reason:NetworkPluginNotReady message:docker: network plugin is not ready: cni config uninitialized
</code>```

I can only find info about this in Kubernetes github logs but they talk about a  bug and I haven't found a workaround. Thanks

答:
you can run this command</p>

```<code>kubectl create -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel-rbac.yml
kubectl create -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
</code>```

Body:
I am trying to follow docs to setup a one node Kubernetes cluster with Centos 7.

kubeadm init will return no error but kubectl get nodes will return:

NAME      STATUS     AGE       VERSION
[MY_IP]   NotReady   22s       v1.6.4

system log repeats:

Jun  6 16:21:48 localhost kubelet: W0606 16:21:48.064388   11520 cni.go:157] Unable to update cni config: No networks found in /etc/cni/net.d
Jun  6 16:21:48 localhost kubelet: E0606 16:21:48.064537   11520 kubelet.go:2067] Container runtime network not ready: NetworkReady=false reason:NetworkPluginNotReady message:docker: network plugin is not ready: cni config uninitialized

I can only find info about this in Kubernetes github logs but they talk about a  bug and I haven't found a workaround. Thanks

Body:
you can run this command

kubectl create -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel-rbac.yml
kubectl create -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml


问:
How to fix internal or external command errors in Netbeans?
It builds fine, but when netbeans tries to start the emulator, the following error occurs:

'Blackberry' is not recognized as an internal or external command, operable
  program or batch file.

I'm searching for a general solution. Because I found many of the same type of errors on the net (But each one without an answer).

'xxx' is not recognized as an internal or external command, operable
  program or batch file.

So how do you make Netbeans recognize 'xxx'?

答:
add the path to your executable to your system path variable .

it will look at those path while executing any executable if executable found from any of those path it OR from current dir it will execute otherwise it will throw the same error.

For Linux you have

```<code>PATH=$PATH:/home/user/path/to/executable
</code>```

for windows you will have something like ,edit it. follow this tutorial for more info

Body:
add the path to your executable to your system path variable .

it will look at those path while executing any executable if executable found from any of those path it OR from current dir it will execute otherwise it will throw the same error.

For Linux you have

PATH=$PATH:/home/user/path/to/executable

for windows you will have something like ,edit it. follow this tutorial for more info


问:
Fillna PySpark Dataframe with numpy array Error
The following is a sample of my Spark DataFrame with the printSchema below it:

```<code>+--------------------+---+------+------+--------------------+
|           device_id|age|gender| group|                apps|
+--------------------+---+------+------+--------------------+
|-9073325454084204615| 24|     M|M23-26|                null|
|-8965335561582270637| 28|     F|F27-28|[1.0,1.0,1.0,1.0,...|
|-8958861370644389191| 21|     M|  M22-|[4.0,0.0,0.0,0.0,...|
|-8956021912595401048| 21|     M|  M22-|                null|
|-8910497777165914301| 25|     F|F24-26|                null|
+--------------------+---+------+------+--------------------+
only showing top 5 rows

root
 |-- device_id: long (nullable = true)
 |-- age: integer (nullle = true)
 |-- gender: string (nullable = true)
 |-- group: string (nullable = true)
 |-- apps: vector (nullable = true)
</code>```

I'm trying to fill the null in the 'apps' column with np.zeros(19237). However When I execute

```<code>df.fillna({'apps': np.zeros(19237)}))
</code>```

I get an error

```<code>Py4JJavaError: An error occurred while calling o562.fill.
: java.lang.IllegalArgumentException: Unsupported value type java.util.ArrayList
</code>```

Or if I try

```<code>df.fillna({'apps': DenseVector(np.zeros(19237)})))
</code>```

I get an error

```<code>AttributeError: 'numpy.ndarray' object has no attribute '_get_object_id'
</code>```

Any ideas?

答:
DataFrameNaFunctions support only a subset of native (no UDTs) types, so you'll need an UDF here.

```<code>from pyspark.sql.functions import coalesce, col, udf
from pyspark.ml.linalg import Vectors, VectorUDT

def zeros(n):
    def zeros_():
        return Vectors.sparse(n, {})
    return udf(zeros_, VectorUDT())()
</code>```

Example usage:

```<code>df = spark.createDataFrame(
    [(1, Vectors.dense([1, 2, 3])), (2, None)],
    ("device_id", "apps"))

df.withColumn("apps", coalesce(col("apps"), zeros(3))).show()
</code>```

+---------+-------------+
|device_id|         apps|
+---------+-------------+
|        1|[1.0,2.0,3.0]|
|        2|    (3,[],[])|
+---------+-------------+
```

Body:
The following is a sample of my Spark DataFrame with the printSchema below it:

+--------------------+---+------+------+--------------------+
|           device_id|age|gender| group|                apps|
+--------------------+---+------+------+--------------------+
|-9073325454084204615| 24|     M|M23-26|                null|
|-8965335561582270637| 28|     F|F27-28|[1.0,1.0,1.0,1.0,...|
|-8958861370644389191| 21|     M|  M22-|[4.0,0.0,0.0,0.0,...|
|-8956021912595401048| 21|     M|  M22-|                null|
|-8910497777165914301| 25|     F|F24-26|                null|
+--------------------+---+------+------+--------------------+
only showing top 5 rows

root
 |-- device_id: long (nullable = true)
 |-- age: integer (nullle = true)
 |-- gender: string (nullable = true)
 |-- group: string (nullable = true)
 |-- apps: vector (nullable = true)

I'm trying to fill the null in the 'apps' column with np.zeros(19237). However When I execute

df.fillna({'apps': np.zeros(19237)}))

I get an error

Py4JJavaError: An error occurred while calling o562.fill.
: java.lang.IllegalArgumentException: Unsupported value type java.util.ArrayList

Or if I try

df.fillna({'apps': DenseVector(np.zeros(19237)})))

I get an error

AttributeError: 'numpy.ndarray' object has no attribute '_get_object_id'

Any ideas?

Body:
DataFrameNaFunctions support only a subset of native (no UDTs) types, so you'll need an UDF here.

from pyspark.sql.functions import coalesce, col, udf
from pyspark.ml.linalg import Vectors, VectorUDT

def zeros(n):
    def zeros_():
        return Vectors.sparse(n, {})
    return udf(zeros_, VectorUDT())()

Example usage:

df = spark.createDataFrame(
    [(1, Vectors.dense([1, 2, 3])), (2, None)],
    ("device_id", "apps"))

df.withColumn("apps", coalesce(col("apps"), zeros(3))).show()

+---------+-------------+
|device_id|         apps|
+---------+-------------+
|        1|[1.0,2.0,3.0]|
|        2|    (3,[],[])|
+---------+-------------+


问:
Javascript find a position of a word in a string containing HTML
I got the following HTML:

```<code><div id="editable_phrase">
   My
   <span style="background-color: #19FC49;"> first</span>
   <span style="background-color: #09AC20;"> first</span> //note 2 firsts here
   <span style="background-color: #D2C3EA;"> phrase</span>
</div>
</code>```

And the original phrase that is - My first phrase

I need to find a position of a first letter of a selected word in my original phrase. So if I select (highlight with a mouse) phrase, I need to get 10 (or whatever it is). I tried to use charAt() or window.getSelection but still getting wrong results.

Moreover, if I select the same word but in different positions (like inm my example above where there are 2 first words) I get the same result as it finds the position of the first word while I need the second one.

I can use jQuery. Any ideas would be welcome. Thank you.

答:
Here's an answer that combines baao's answer and <s>Joseph Marikle's answer</s> (has since been deleted).

selection.anchorNode.textContent gets the word that is being highlighted.

$("#editable_phrase").bind('mouseup', function(e) {
  var selection;
  var selectedWordPos;
  if (window.getSelection) {
    selection = window.getSelection();
  } else if (document.selection) {
    selection = document.selection.createRange();
  }
  selectedWordPos = $("#editable_phrase").text().replace(/\s\s+/g, ' ').indexOf(selection.anchorNode.textContent.replace(/\s\s+/g, ' '));
  console.log(selectedWordPos);
});```
<pre class="snippet-code-html lang-html prettyprint-override"><code><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="editable_phrase">
  My
  <span style="background-color: #19FC49;"> first</span>
  <span style="background-color: #D2C3EA;"> phrase</span>
</div></code>```

Body:
I got the following HTML:

   My
    first
    first //note 2 firsts here
    phrase

And the original phrase that is - My first phrase

I need to find a position of a first letter of a selected word in my original phrase. So if I select (highlight with a mouse) phrase, I need to get 10 (or whatever it is). I tried to use charAt() or window.getSelection but still getting wrong results.

Moreover, if I select the same word but in different positions (like inm my example above where there are 2 first words) I get the same result as it finds the position of the first word while I need the second one.

I can use jQuery. Any ideas would be welcome. Thank you.

Body:
Here's an answer that combines baao's answer and <s>Joseph Marikle's answer</s> (has since been deleted).

selection.anchorNode.textContent gets the word that is being highlighted.

$("#editable_phrase").bind('mouseup', function(e) {
  var selection;
  var selectedWordPos;
  if (window.getSelection) {
    selection = window.getSelection();
  } else if (document.selection) {
    selection = document.selection.createRange();
  }
  selectedWordPos = $("#editable_phrase").text().replace(/\s\s+/g, ' ').indexOf(selection.anchorNode.textContent.replace(/\s\s+/g, ' '));
  console.log(selectedWordPos);
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="editable_phrase">
  My
  <span style="background-color: #19FC49;"> first</span>
  <span style="background-color: #D2C3EA;"> phrase</span>
</div>


问:
Unable to persist a 'write' to a specific datanode in HDFS
I'm trying to create an indexing strategy which requires the index blocks to be on the same datanode as the data blocks to reduce latency at the times of data retrieval. I've managed to write the code for reading the datablocks related to a specific file. For writing, I open up a socket connection to a specific datanode, write my data, and then close the socket. Unfortunately, I'm not sure 'where' or 'how' the data is being written using this method because when I query the HDFS using hadoop fs -ls I can't see my data written anywhere (in some stray file maybe?!), but my program executes without any errors.

Here's my code:

```<code>import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdfs.DFSClient;
import org.apache.hadoop.hdfs.DistributedFileSystem;
import org.apache.hadoop.hdfs.protocol.LocatedBlocks;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.net.NetUtils;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.hadoop.hdfs.protocol.ExtendedBlock;
import org.apache.hadoop.hdfs.server.datanode.DataNode;
import org.apache.hadoop.hdfs.server.protocol.DatanodeRegistration;

import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.channels.FileChannel;
import java.nio.file.OpenOption;
import java.nio.file.StandardOpenOption;
import java.util.Random;
import java.nio.ByteBuffer;

import javax.net.SocketFactory;

import org.apache.hadoop.security. UserGroupInformation;

public class CopyOfChunkedIndexes {

    public static void main(String[] args) throws Exception {
        if (args.length != 1) {
            System.err.println("Usage: ChunkedIndexes <input path>");
            System.exit(-1);
        }

        Configuration conf = new Configuration();
        conf.set("fs.default.name", "hdfs://localhost:9000");               //for defaulting to HDFS rather than local filesystem
        conf.set("hadoop.security.authentication", "simple");               //disable authentication
        conf.set("hadoop.security.authorization", "false");                 //disable authorization

        Job job = Job.getInstance(conf, "Chunked Indexes");
        job.setJarByClass(CopyOfChunkedIndexes.class);

        Path inputPath = new Path("/user/hadoop-user/sample.txt");
        FileInputFormat.setInputPaths(job, inputPath);

        try{
            FileSystem fs = FileSystem.get(conf);
            DistributedFileSystem dfs = (DistributedFileSystem) fs;
            DFSClient dfsclient = dfs.getClient();

            System.out.println("Proceeding for file: " + inputPath.toString());

            FileStatus fileStatus = fs.getFileStatus(inputPath);
            BlockLocation[] bLocations = fs.getFileBlockLocations(inputPath, 0, fileStatus.getLen());

            for(int i = 0; i < bLocations.length; i++)
            {
                System.out.println("Block[" + + i + "]::");
                System.out.println("\nHost(s): ");

                String[] temp = bLocations[i].getHosts();
                for(int j = 0; j < temp.length; j++)
                {
                    System.out.println(temp[j] + "\t");
                }

                System.out.println("\nBlock length: " + bLocations[i].getLength() +
                                    "\n\nDataNode(s) hosting this block: ");

                temp = bLocations[i].getNames();
                for(int j = 0; j < temp.length; j++)
                {
                    System.out.println(temp[j] + "\t");
                }

                System.out.println("\nOffset: " + bLocations[i].getOffset());

                //READING A BLOCK
                FSDataInputStream in = fs.open(inputPath);
                in.seek(bLocations[i].getOffset());

                byte[] buf = new byte[(int)bLocations[i].getLength()];
                in.read(buf, (int)bLocations[i].getOffset(), (int)bLocations[i].getLength());
                in.close();

                System.out.println(new String(buf, "UTF-8"));
                System.out.println("--------------------------------------------------------------------------------------------");
            }

            //WRITE A FILE TO A SPECIFIC DATANODE
            for(int i = 0; i < bLocations.length; i++)
            {
                System.out.println("Block[" + + i + "]::");
                String[] temp;

                System.out.println("\n\nDataNode(s) hosting this block: ");                        //Name(s) = datanode addresses

                temp = bLocations[i].getNames();
                for(int j = 0; j < temp.length; j++)
                {
                    System.out.println(temp[j].split(":")[0] + "\t" + temp[j].split(":")[1]);      //host vs. port
                }

                Socket sock = SocketFactory.getDefault().createSocket();
                InetSocketAddress targetAddr = new InetSocketAddress(temp[0].split(":")[0], Integer.parseInt(temp[0].split(":")[1]));
                NetUtils.connect(sock, targetAddr, 10000);
                sock.setSoTimeout(10000);

                OutputStream baseStream = NetUtils.getOutputStream(sock, 10000);
                DataOutputStream oStream = new DataOutputStream(new BufferedOutputStream(baseStream, 10000));
                oStream.writeBytes("-----------------------------------------Sample text-----------------------------------------------");

                sock.close();
                System.out.println("Data written, socket closed!");
            }

        }catch(Exception ex){
            ex.printStackTrace();
        }

    }
}
</code>```

Any help as to where I'm going wrong would be deeply appreciated! Thanks!

[PS: I'm using Hadoop 2.2.0 on a Linux VM. I've disabled authorization/authentication in my code above because I want to access a datanode directly (without the 'overhead' of authentication) as this is for testing purposes.]

答: There's a class called BlockPlacementPolicy, which (in theory) can be extended for customizing how HDFS choose a datanode. Though hacky, this approach may work for someone looking to do something similar in the future and stumbles upon this question.

Body:
I'm trying to create an indexing strategy which requires the index blocks to be on the same datanode as the data blocks to reduce latency at the times of data retrieval. I've managed to write the code for reading the datablocks related to a specific file. For writing, I open up a socket connection to a specific datanode, write my data, and then close the socket. Unfortunately, I'm not sure 'where' or 'how' the data is being written using this method because when I query the HDFS using hadoop fs -ls I can't see my data written anywhere (in some stray file maybe?!), but my program executes without any errors.

Here's my code:

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdfs.DFSClient;
import org.apache.hadoop.hdfs.DistributedFileSystem;
import org.apache.hadoop.hdfs.protocol.LocatedBlocks;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.net.NetUtils;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.hadoop.hdfs.protocol.ExtendedBlock;
import org.apache.hadoop.hdfs.server.datanode.DataNode;
import org.apache.hadoop.hdfs.server.protocol.DatanodeRegistration;

import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.channels.FileChannel;
import java.nio.file.OpenOption;
import java.nio.file.StandardOpenOption;
import java.util.Random;
import java.nio.ByteBuffer;

import javax.net.SocketFactory;

import org.apache.hadoop.security. UserGroupInformation;

public class CopyOfChunkedIndexes {

    public static void main(String[] args) throws Exception {
        if (args.length != 1) {
            System.err.println("Usage: ChunkedIndexes <input path>");
            System.exit(-1);
        }

        Configuration conf = new Configuration();
        conf.set("fs.default.name", "hdfs://localhost:9000");               //for defaulting to HDFS rather than local filesystem
        conf.set("hadoop.security.authentication", "simple");               //disable authentication
        conf.set("hadoop.security.authorization", "false");                 //disable authorization

        Job job = Job.getInstance(conf, "Chunked Indexes");
        job.setJarByClass(CopyOfChunkedIndexes.class);

        Path inputPath = new Path("/user/hadoop-user/sample.txt");
        FileInputFormat.setInputPaths(job, inputPath);

        try{
            FileSystem fs = FileSystem.get(conf);
            DistributedFileSystem dfs = (DistributedFileSystem) fs;
            DFSClient dfsclient = dfs.getClient();

            System.out.println("Proceeding for file: " + inputPath.toString());

            FileStatus fileStatus = fs.getFileStatus(inputPath);
            BlockLocation[] bLocations = fs.getFileBlockLocations(inputPath, 0, fileStatus.getLen());

            for(int i = 0; i < bLocations.length; i++)
            {
                System.out.println("Block[" + + i + "]::");
                System.out.println("\nHost(s): ");

                String[] temp = bLocations[i].getHosts();
                for(int j = 0; j < temp.length; j++)
                {
                    System.out.println(temp[j] + "\t");
                }

                System.out.println("\nBlock length: " + bLocations[i].getLength() +
                                    "\n\nDataNode(s) hosting this block: ");

                temp = bLocations[i].getNames();
                for(int j = 0; j < temp.length; j++)
                {
                    System.out.println(temp[j] + "\t");
                }

                System.out.println("\nOffset: " + bLocations[i].getOffset());

                //READING A BLOCK
                FSDataInputStream in = fs.open(inputPath);
                in.seek(bLocations[i].getOffset());

                byte[] buf = new byte[(int)bLocations[i].getLength()];
                in.read(buf, (int)bLocations[i].getOffset(), (int)bLocations[i].getLength());
                in.close();

                System.out.println(new String(buf, "UTF-8"));
                System.out.println("--------------------------------------------------------------------------------------------");
            }

            //WRITE A FILE TO A SPECIFIC DATANODE
            for(int i = 0; i < bLocations.length; i++)
            {
                System.out.println("Block[" + + i + "]::");
                String[] temp;

                System.out.println("\n\nDataNode(s) hosting this block: ");                        //Name(s) = datanode addresses

                temp = bLocations[i].getNames();
                for(int j = 0; j < temp.length; j++)
                {
                    System.out.println(temp[j].split(":")[0] + "\t" + temp[j].split(":")[1]);      //host vs. port
                }

                Socket sock = SocketFactory.getDefault().createSocket();
                InetSocketAddress targetAddr = new InetSocketAddress(temp[0].split(":")[0], Integer.parseInt(temp[0].split(":")[1]));
                NetUtils.connect(sock, targetAddr, 10000);
                sock.setSoTimeout(10000);

                OutputStream baseStream = NetUtils.getOutputStream(sock, 10000);
                DataOutputStream oStream = new DataOutputStream(new BufferedOutputStream(baseStream, 10000));
                oStream.writeBytes("-----------------------------------------Sample text-----------------------------------------------");

                sock.close();
                System.out.println("Data written, socket closed!");
            }

        }catch(Exception ex){
            ex.printStackTrace();
        }

    }
}

Any help as to where I'm going wrong would be deeply appreciated! Thanks!

[PS: I'm using Hadoop 2.2.0 on a Linux VM. I've disabled authorization/authentication in my code above because I want to access a datanode directly (without the 'overhead' of authentication) as this is for testing purposes.]


问:
Mocked CloseableHttpClient still passes connection to real server while testing
i try to test a method which uses a CloseableHttpClient Connection. I decided to go for Mockito and mocked all relevant classes and methods. But still, as soon as I start the test it goes straight to the real server instead of being intercepted by the mocked CloseableHttpClient.

The test

```<code>  @Test
    public void testBid() throws IOException {
        //given:
        HttpGet httpGet = mock(HttpGet.class);
        HttpResponse httpResponse = mock(HttpResponse.class);

        StatusLine statusLine = mock(StatusLine.class);

        ObserverImp observer = mock(ObserverImp.class);

        CloseableHttpClient closeableHttpClient = mock(CloseableHttpClient.class);
        CloseableHttpResponse closeableHttpResponse = mock(CloseableHttpResponse.class);

        //and:
        when(statusLine.getStatusCode()).thenReturn(200);
        when(httpResponse.getStatusLine()).thenReturn(statusLine);
        when(closeableHttpClient.execute(httpGet)).thenReturn(closeableHttpResponse);
        when(observer.getKey()).thenReturn("##213");

        Buy buy = new Buy(observer);
        buy.bid(14455);
    }
</code>```

And the related Implementation

```<code>public void buy(double price) {
        String queryArgs = "command=order&amount=1" + "&price=" + String.valueOf(price); // generates query

        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost post = new HttpPost("wwww.hm.edu/projectGroup1");
        post.addHeader("Key", observer.getKey());
        try {
            post.setEntity(new ByteArrayEntity(queryArgs.getBytes("UTF-8")));
        } catch (UnsupportedEncodingException e) {
            System.out.println("Exception in run");
        }
        List<NameValuePair> params = new ArrayList<>();

        params.add(new BasicNameValuePair("command", "order"));
        params.add(new BasicNameValuePair("amount", "1"));
        params.add(new BasicNameValuePair("price", String.valueOf(price)));
        try {
            post.setEntity(new UrlEncodedFormEntity(params));
            CloseableHttpResponse response = httpClient.execute(post);
            HttpEntity entity = response.getEntity();
            Scanner in = new Scanner(entity.getContent());
            String orderNumber = "";
            while (in.hasNext()) {
                orderNumber = in.nextLine();
            }
            String[] findOrderNumber = orderNumber.split(".");
            long lastOrderNumber = -1;
            try {
                lastOrderNumber = Long.valueOf(findOrderNumber[3]);
            } catch (NumberFormatException exception) {
                System.out.println("NumberFormatException");
            } finally {
                if (lastOrderNumber != -1) {
                    observer.setOrderNumber(lastOrderNumber);
                }
            }
            in.close();
            EntityUtils.consume(entity);
            httpClient.close();
        } catch (IOException e) {
            System.out.println("Exception occured during process");
        }
    }
</code>```

I tried several code snippets of tests I found in tutorials or similar questions here but with no success. Can you please tell me what I do wrong here?

Thank you :)

答:
In your buy method, you're creating httpClient using static method, like this

```<code>CloseableHttpClient httpClient = HttpClients.createDefault();
</code>```

now everytime you call buy the real http client gets created and it call real services. Instead, you should pass httpClient as dependency as mock httpClient from your test code, like

```<code>public void buy(double price, HttpClient httpClient) {
..
}
</code>```

and in test:

```<code>public void test() {

    HttpClient httpClient = mock(HttpClient.class)
    // mock the behavour

    // SUT
    buy(..., httpClient);

}
</code>```

Body:
i try to test a method which uses a CloseableHttpClient Connection. I decided to go for Mockito and mocked all relevant classes and methods. But still, as soon as I start the test it goes straight to the real server instead of being intercepted by the mocked CloseableHttpClient.

The test

  @Test
    public void testBid() throws IOException {
        //given:
        HttpGet httpGet = mock(HttpGet.class);
        HttpResponse httpResponse = mock(HttpResponse.class);

        StatusLine statusLine = mock(StatusLine.class);

        ObserverImp observer = mock(ObserverImp.class);

        CloseableHttpClient closeableHttpClient = mock(CloseableHttpClient.class);
        CloseableHttpResponse closeableHttpResponse = mock(CloseableHttpResponse.class);

        //and:
        when(statusLine.getStatusCode()).thenReturn(200);
        when(httpResponse.getStatusLine()).thenReturn(statusLine);
        when(closeableHttpClient.execute(httpGet)).thenReturn(closeableHttpResponse);
        when(observer.getKey()).thenReturn("##213");

        Buy buy = new Buy(observer);
        buy.bid(14455);
    }

And the related Implementation

public void buy(double price) {
        String queryArgs = "command=order&amount=1" + "&price=" + String.valueOf(price); // generates query

        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost post = new HttpPost("wwww.hm.edu/projectGroup1");
        post.addHeader("Key", observer.getKey());
        try {
            post.setEntity(new ByteArrayEntity(queryArgs.getBytes("UTF-8")));
        } catch (UnsupportedEncodingException e) {
            System.out.println("Exception in run");
        }
        List<NameValuePair> params = new ArrayList<>();

        params.add(new BasicNameValuePair("command", "order"));
        params.add(new BasicNameValuePair("amount", "1"));
        params.add(new BasicNameValuePair("price", String.valueOf(price)));
        try {
            post.setEntity(new UrlEncodedFormEntity(params));
            CloseableHttpResponse response = httpClient.execute(post);
            HttpEntity entity = response.getEntity();
            Scanner in = new Scanner(entity.getContent());
            String orderNumber = "";
            while (in.hasNext()) {
                orderNumber = in.nextLine();
            }
            String[] findOrderNumber = orderNumber.split(".");
            long lastOrderNumber = -1;
            try {
                lastOrderNumber = Long.valueOf(findOrderNumber[3]);
            } catch (NumberFormatException exception) {
                System.out.println("NumberFormatException");
            } finally {
                if (lastOrderNumber != -1) {
                    observer.setOrderNumber(lastOrderNumber);
                }
            }
            in.close();
            EntityUtils.consume(entity);
            httpClient.close();
        } catch (IOException e) {
            System.out.println("Exception occured during process");
        }
    }

I tried several code snippets of tests I found in tutorials or similar questions here but with no success. Can you please tell me what I do wrong here?

Thank you :)

Body:
In your buy method, you're creating httpClient using static method, like this

CloseableHttpClient httpClient = HttpClients.createDefault();

now everytime you call buy the real http client gets created and it call real services. Instead, you should pass httpClient as dependency as mock httpClient from your test code, like

public void buy(double price, HttpClient httpClient) {
..
}

and in test:

public void test() {

    HttpClient httpClient = mock(HttpClient.class)
    // mock the behavour

    // SUT
    buy(..., httpClient);

}


问:
Friendly URLs in Phoenix 1.3 undefined error
I'm trying to implement friendly URLs in my app based on the following guide https://elixircasts.io/seo-friendly-urls-with-phoenix.

The slug is being created in the db from the post title ok, but I cannot get the post to show when looking up by the slug and get the following error:

function Driverless.Articles.get_by!/2 is undefined or private

post_controller.ex

```<code>...
  def show(conn, %{"id" => id}) do
    post = Articles.get_by!(Post, slug: id)
    render(conn, "show.html", post: post)
  end
...
</code>```

post.ex

```<code>...
  def changeset(%Post{} = post, attrs) do
    attrs = Map.merge(attrs, slug_map(attrs))
    post
    |> cast(attrs, [:title, :snippet, :body, :slug])
    |> validate_required([:title, :snippet, :body])
  end

  defp slug_map(%{"title" => title}) do
    slug = String.downcase(title) |> String.replace(" ", "-")
    %{"slug" => slug}
  end
  defp slug_map(_attrs) do
    %{}
  end
...
</code>```

I'm sure there's a small error somewhere, but just can't spot it.

答:
Just solved this:

Unlike the original example in Elixir casts, we leave the post controller as is:

```<code>  def show(conn, %{"id" => id}) do
    post = Articles.get_post!(id)
    render(conn, "show.html", post: post)
  end
</code>```

I just made the change to:

articles.ex

From:

```<code>def get_post!(id), do: Repo.get!(Post, id)
</code>```

To

```<code>def get_post!(id), do: Repo.get_by!(Post, slug: id)
</code>```

Body:
I'm trying to implement friendly URLs in my app based on the following guide https://elixircasts.io/seo-friendly-urls-with-phoenix.

The slug is being created in the db from the post title ok, but I cannot get the post to show when looking up by the slug and get the following error:

function Driverless.Articles.get_by!/2 is undefined or private

post_controller.ex

...
  def show(conn, %{"id" => id}) do
    post = Articles.get_by!(Post, slug: id)
    render(conn, "show.html", post: post)
  end
...

post.ex

...
  def changeset(%Post{} = post, attrs) do
    attrs = Map.merge(attrs, slug_map(attrs))
    post
    |> cast(attrs, [:title, :snippet, :body, :slug])
    |> validate_required([:title, :snippet, :body])
  end

  defp slug_map(%{"title" => title}) do
    slug = String.downcase(title) |> String.replace(" ", "-")
    %{"slug" => slug}
  end
  defp slug_map(_attrs) do
    %{}
  end
...

I'm sure there's a small error somewhere, but just can't spot it.

Body:
Just solved this:

Unlike the original example in Elixir casts, we leave the post controller as is:

  def show(conn, %{"id" => id}) do
    post = Articles.get_post!(id)
    render(conn, "show.html", post: post)
  end

I just made the change to:

articles.ex

From:

def get_post!(id), do: Repo.get!(Post, id)

To

def get_post!(id), do: Repo.get_by!(Post, slug: id)


问:
Folder property of File Search Launch Condition: how to specify custom folder under [TARGETDIR]?
I'm trying to set a file search launch condition for my installer. I need it to search for a specified file in a subfolder of my [TARGETDIR] folder.

I tried setting the Folder property to:

[TARGETDIR]/Subfolder

[TARGETDIR]Subfolder

[TARGETDIR]Subfolder/

[TARGETDIR]/Subfolder/

[TARGETDIR]/"Subfolder"

None of the above solutions worked. The file located in subfolder never gets found. It works if I only specify [TARGETDIR] with the Depth property set to 1, but I would rather specify directly the directory to search.

I found a similar question here, but there is no answer.

What is the correct way to set the Folder property to work with a custom folder ?

答:
The difficulty here is that TARGETDIR is not set until later than the search is done, so it has no value yet. Even if if it did have a value, it's what is changed by the browse dialog that can change where the user installs thet app (see the property window of Application Folder in File System on Target Machine).

This means you must use standard folder properties in your search. If you want to search what you think may be where the file is installed, then use:

[ProgramFilesFolder]SomeFolder\SomeOtherFolder

or something similar if your assumed value of TARGETDIR is in Program Files x86.


问:
What would cause an HTML5 canvas toBlob to create an incomplete image?
I have the following javascript produce images from a canvas and upload them to a server.

```<code>var can = document.createElement('canvas');
can.width = 600;
can.height = 600;
var ctx = can.getContext('2d');
ctx.fillRect(0, 0, can.width, can.height);
ctx.fillText("Julia", can.width/2, can.height/2);
can.toBlob(uploadImage, "image/jpg", 0.9);

function uploadImage(jpeg) {
  var data = new FormData();
  data.append('image', jpeg, 'image.jpg');
  ...
}
</code>```

Every so often, the result looks like the above, only partially drawn. Multiple canvases are processed and uploaded serially, only moving on in the completion of the ajax (in the ... part), so only one at a time.

Have you seen this happen? If so, where in this process should I debug further? Maybe a setting or something in the context object?

Edit

The upload is an ajax post with a promise resolved only on the success branch. It actually uses angular's $http service:

```<code>$http({method: 'POST', url: '...', data: data}).then(function(response) {
    // callback that processes and uploads the next image
});
</code>```

答:
We were facing with similar except part with canvas. You can debug it simply.

Print given blob to page as img src to make sure that file was created properly. Print also blob size.

Open browser's developer tools -> Network -> Filter XHR and start uploading

Assign finished(status 200) ajax request and look at Request Headers -> Content-Length. If this number is equal to blob size on page, it was uploaded properly and find issue on server side. Else check ajax call.

Make sure that server has set max client request size appropriate to your usecase. For example nginx has default value 1M.

TIP: set Content-Type by blob.type in $http headers. Default value for POST is application/json.

Body:
I have the following javascript produce images from a canvas and upload them to a server.

var can = document.createElement('canvas');
can.width = 600;
can.height = 600;
var ctx = can.getContext('2d');
ctx.fillRect(0, 0, can.width, can.height);
ctx.fillText("Julia", can.width/2, can.height/2);
can.toBlob(uploadImage, "image/jpg", 0.9);

function uploadImage(jpeg) {
  var data = new FormData();
  data.append('image', jpeg, 'image.jpg');
  ...
}

Every so often, the result looks like the above, only partially drawn. Multiple canvases are processed and uploaded serially, only moving on in the completion of the ajax (in the ... part), so only one at a time.

Have you seen this happen? If so, where in this process should I debug further? Maybe a setting or something in the context object?

Edit

The upload is an ajax post with a promise resolved only on the success branch. It actually uses angular's $http service:

$http({method: 'POST', url: '...', data: data}).then(function(response) {
    // callback that processes and uploads the next image
});


问:
Fast inter-row computation with Spark on grouped data
I've got a CSV file (> 3GB) of the following structure

```<code>groupId | time | value
1            0       2
1            1       1
1            2       4
2            0       6
2            1       2
</code>```

and want to add a column (value t-1), that contains the value - within the same group - of the "row" a time step earlier:

```<code>groupId | time | value | value t-1
1            0       2           -
1            1       1           2
1            2       4           1
2            0       6           -
2            1       2           6
</code>```

I imagine the costly part is to search for the preceding row. Somehow it seems like a job for map reduce without a reduction - if this makes sense. But as far as I understand I can't be sure to have all data of the same group on the same worker.

Is Spark the right tool for the job?

My best alternative solution is to split the file into multiple files (one per group) and just run multiple instances of a python script that sorts and sequentially adds the value t-1 values

答:
This can be achieved by using the spark's Window function as shown below.

```<code>import org.apache.spark.sql.expressions.Window

val df = Seq((1,0,2), (1,1,1), (1,2,4), (2,0,6), (2,1,2)).toDF("groupId", "time", "value")

val result = df.withColumn("value_t-1", sum($"value").over(Window.partitionBy("groupId").orderBy("time").rowsBetween(-1,-1)))
</code>```

Output:

```<code>scala> result.show()
+-------+----+-----+---------+
|groupId|time|value|value_t-1|
+-------+----+-----+---------+
|      1|   0|    2|     null|
|      1|   1|    1|        2|
|      1|   2|    4|        1|
|      2|   0|    6|     null|
|      2|   1|    2|        6|
+-------+----+-----+---------+
</code>```

Python version:

```<code>>>> from pyspark.sql.window import Window
>>> import pyspark.sql.functions as func
>>> df = spark.createDataFrame([(1,0,2), (1,1,1), (1,2,4), (2,0,6), (2,1,2)], ["groupId", "time", "value"])
>>> result = df.withColumn("value_t-1", func.sum(df.value).over(Window.partitionBy(df.groupId).orderBy(df.time).rowsBetween(-1,-1)))
>>> result.show()
+-------+----+-----+---------+
|groupId|time|value|value_t-1|
+-------+----+-----+---------+
|      1|   0|    2|     null|
|      1|   1|    1|        2|
|      1|   2|    4|        1|
|      2|   0|    6|     null|
|      2|   1|    2|        6|
+-------+----+-----+---------+
</code>```

Body:
I've got a CSV file (> 3GB) of the following structure

groupId | time | value
1            0       2
1            1       1
1            2       4
2            0       6
2            1       2

and want to add a column (value t-1), that contains the value - within the same group - of the "row" a time step earlier:

groupId | time | value | value t-1
1            0       2           -
1            1       1           2
1            2       4           1
2            0       6           -
2            1       2           6

I imagine the costly part is to search for the preceding row. Somehow it seems like a job for map reduce without a reduction - if this makes sense. But as far as I understand I can't be sure to have all data of the same group on the same worker.

Is Spark the right tool for the job?

My best alternative solution is to split the file into multiple files (one per group) and just run multiple instances of a python script that sorts and sequentially adds the value t-1 values

Body:
This can be achieved by using the spark's Window function as shown below.

import org.apache.spark.sql.expressions.Window

val df = Seq((1,0,2), (1,1,1), (1,2,4), (2,0,6), (2,1,2)).toDF("groupId", "time", "value")

val result = df.withColumn("value_t-1", sum($"value").over(Window.partitionBy("groupId").orderBy("time").rowsBetween(-1,-1)))

Output:

scala> result.show()
+-------+----+-----+---------+
|groupId|time|value|value_t-1|
+-------+----+-----+---------+
|      1|   0|    2|     null|
|      1|   1|    1|        2|
|      1|   2|    4|        1|
|      2|   0|    6|     null|
|      2|   1|    2|        6|
+-------+----+-----+---------+

Python version:

>>> from pyspark.sql.window import Window
>>> import pyspark.sql.functions as func
>>> df = spark.createDataFrame([(1,0,2), (1,1,1), (1,2,4), (2,0,6), (2,1,2)], ["groupId", "time", "value"])
>>> result = df.withColumn("value_t-1", func.sum(df.value).over(Window.partitionBy(df.groupId).orderBy(df.time).rowsBetween(-1,-1)))
>>> result.show()
+-------+----+-----+---------+
|groupId|time|value|value_t-1|
+-------+----+-----+---------+
|      1|   0|    2|     null|
|      1|   1|    1|        2|
|      1|   2|    4|        1|
|      2|   0|    6|     null|
|      2|   1|    2|        6|
+-------+----+-----+---------+


问:
Passing Data between sibling components in Angular

Above image depicts my Angular 2 application , Main component has two child
components "FromComponent" and "ToComponent".

"FromComponent" displays a list of items with a checkbox beside each item.

When user selects/deselects an item in FromComponent , it should be added/removed from ToComponent list of items.

what is the best way to implement this in angular 2 ?

答:
You would want to use a shared service that stores this information. Using observables as the storage mechanism would allow the To Component to subscribe to that subject and watch for any changes that the From component makes to that observable.

Check out more information on Subject and BehaviorSubject types in rxjs.


问:
help, stored procedures and cursors
i have to write a stored procedure, where you give the month, and a credit card number, and it calulates 1%  for each transaction made in the first 10 days of the month, 2% for transactions between 10 and 20, and 3% for transactions above 20.
And i must use cursors.

i wrote this code, but i get some errors when i try to run the precedure

```<code>   create procedure cardP
   /* month ex 1,3,5 etc*/
   @minas int,
   @cardNo bigint

as
/* creating the cursor*/
DECLARE sinallages CURSOR
FOR SELECT cc_number,day([DateTime]),charged_amount FROM transactions
where cc_number=@cardNo and month([DateTime])=@minas

/* declaring local variables*/
declare @poso int,@karta bigint,@mera int,@pos float,@pos2 float,@pos3 float,
@count int,@counter int

open sinallages

set @count=(select count(cc_number) from transactions where cc_number=@cardNo and month([DateTime])=@minas )
/* get 1st row*/
fetch sinallages into @karta,@mera,@poso
while (/*@@sqlstatus != 2*/@counter<@count)
  begin
    if day(@mera)<=10
    set @pos =@poso+ @poso * 0.01
    else
    if day(@mera)>10 and day(@mera)<=20
    set @pos2 =@poso+ @poso * 0.02
    else
    if day(@mera) > 20
    set @pos3 =@poso+ @poso * 0.03
    fetch sinallages into @karta,@mera,@poso
  set @counter=@counter+1
  end

close sinallages

return
</code>```

when i call the procedure i get

EXEC cardP @minas = 5, @cardNo =4929569752542450

```<code>Msg 16915, Level 16, State 1, Procedure cardP, Line 20
A cursor with the name 'sinallages' already exists.
Msg 16922, Level 16, State 1, Procedure cardP, Line 31
</code>```

Cursor Fetch: Implicit conversion from data type datetime to int is not allowed.

thank you :) i now deallocate the cursor at the end of the stored procedure and removed the day(). Now i want to print the pos+pos2+pos3. I use print  pos+pos2+pos3  but it doesnt print anything. why is that ??

```<code>    ................
      set @counter=@counter+1
  end
print @pos+@pos2+@pos3
close sinallages

return
DEALLOCATE sinallages;
</code>```

it seems like hte variables po,pos2,pos3 are left null??

答:
After

```<code>close sinallages
</code>```

You need to call

```<code>deallocate sinallages
</code>```

Have a look at DEALLOCATE (Transact-SQL)

Title: help, stored procedures and cursors

Body:
i have to write a stored procedure, where you give the month, and a credit card number, and it calulates 1%  for each transaction made in the first 10 days of the month, 2% for transactions between 10 and 20, and 3% for transactions above 20.
And i must use cursors.

i wrote this code, but i get some errors when i try to run the precedure

   create procedure cardP
   /* month ex 1,3,5 etc*/
   @minas int,
   @cardNo bigint

as
/* creating the cursor*/
DECLARE sinallages CURSOR
FOR SELECT cc_number,day([DateTime]),charged_amount FROM transactions
where cc_number=@cardNo and month([DateTime])=@minas

/* declaring local variables*/
declare @poso int,@karta bigint,@mera int,@pos float,@pos2 float,@pos3 float,
@count int,@counter int

open sinallages

set @count=(select count(cc_number) from transactions where cc_number=@cardNo and month([DateTime])=@minas )
/* get 1st row*/
fetch sinallages into @karta,@mera,@poso
while (/*@@sqlstatus != 2*/@counter<@count)
  begin
    if day(@mera)<=10
    set @pos =@poso+ @poso * 0.01
    else
    if day(@mera)>10 and day(@mera)<=20
    set @pos2 =@poso+ @poso * 0.02
    else
    if day(@mera) > 20
    set @pos3 =@poso+ @poso * 0.03
    fetch sinallages into @karta,@mera,@poso
  set @counter=@counter+1
  end

close sinallages

return

when i call the procedure i get

EXEC cardP @minas = 5, @cardNo =4929569752542450

Msg 16915, Level 16, State 1, Procedure cardP, Line 20
A cursor with the name 'sinallages' already exists.
Msg 16922, Level 16, State 1, Procedure cardP, Line 31

Cursor Fetch: Implicit conversion from data type datetime to int is not allowed.

thank you :) i now deallocate the cursor at the end of the stored procedure and removed the day(). Now i want to print the pos+pos2+pos3. I use print  pos+pos2+pos3  but it doesnt print anything. why is that ??

    ................
      set @counter=@counter+1
  end
print @pos+@pos2+@pos3
close sinallages

return
DEALLOCATE sinallages;

it seems like hte variables po,pos2,pos3 are left null??

Body:
After

close sinallages

You need to call

deallocate sinallages

Have a look at DEALLOCATE (Transact-SQL)


问:
MS Access: Trying to do DSum() across multiple fields in a form but it won't filter by criteria
I am trying to create a form that takes sums from certain fields and displays it next to its appropriate field name. I've attached pictures of it in datasheet form (how it's displayed) and its design form.

In more detail, the description items on the left are the "fields" (that I will call "tags" from now on and will be listed in other tables in a column called Associated Bid Items). I've basically tagged several other items in tables and queries throughout my Access App. I am trying to collect all items with the same tags/fields and add up Total MH (man-hour) units associated with these items.

The code I have in the second textbox in the second image is:

```<code>=DSum("[Total MH]","qry-PipingHandleMH","[Associated Bid Item]=[Text_BidItem].Value")
</code>```

where Total MH is what I am summing up, qry-PipingHandleMH is the query I'm pulling from, and Text_BidItem is the first textbox in the second image. I want to sum only the Total MH where the Associated Bid Item (column in other tables where the tags are listed) matches Text_BidItem.

NOW THE PROBLEM: Several items in other tables and queries have already been tagged with each of the tags shown in the first image. Each of those descriptions/tags in the first image should have a Total MH associated with it. The SQL code above does not separate the totals into its rows appropriately as shown in the first image. Here is the longer version of the code:

```<code>=DSum("[Total MH]","qry-PipingHandleMH","[Associated Bid Item]=[Text_BidItem].Value")+DSum("[Total MH]","qry-PipingMakeupMH","[Associated Bid Item]=[Text_BidItem].Value")+DSum("[Total MH]","qry-PipingValvesMH","[Associated Bid Item]=[Text_BidItem].Value")+DSum("[Total MH]","qry-PipingWeldingMH","[Associated Bid Item]=[Text_BidItem].Value")+DSum("[Total MH]","qry-CoatingAGMH","[Associated Bid Item]=[Text_BidItem].Value")+DSum("[Total MH]","qry-CoatingUGMH","[Associated Bid Item]=[Text_BidItem].Value")
</code>```

Basically the same code applied to different tables but added together. That's is whats really in the second textbox in the second image currently. As you can see in the first image it is all applied to the "Large Bore Piping 2" and greater" row. Why isn't the summation of Total MH seperating into its respective rows? DSum() is working but the filtering and separation is just all wrong.

答:
Must concatenate variable parameters. Reference to Text_BidItem is a variable parameter.

=DSum("[Total MH]","qry-PipingHandleMH","[Associated Bid Item]=" & [Text_BidItem])

If [Associated Bid Item] is a text type then will need apostrophe delimiters:

=DSum("[Total MH]","qry-PipingHandleMH","[Associated Bid Item]='" & [Text_BidItem] & "'")

Advise no spaces or punctuation/special characters (underscore only exception) in naming convention.

Body:
I am trying to create a form that takes sums from certain fields and displays it next to its appropriate field name. I've attached pictures of it in datasheet form (how it's displayed) and its design form.

In more detail, the description items on the left are the "fields" (that I will call "tags" from now on and will be listed in other tables in a column called Associated Bid Items). I've basically tagged several other items in tables and queries throughout my Access App. I am trying to collect all items with the same tags/fields and add up Total MH (man-hour) units associated with these items.

The code I have in the second textbox in the second image is:

=DSum("[Total MH]","qry-PipingHandleMH","[Associated Bid Item]=[Text_BidItem].Value")

where Total MH is what I am summing up, qry-PipingHandleMH is the query I'm pulling from, and Text_BidItem is the first textbox in the second image. I want to sum only the Total MH where the Associated Bid Item (column in other tables where the tags are listed) matches Text_BidItem.

NOW THE PROBLEM: Several items in other tables and queries have already been tagged with each of the tags shown in the first image. Each of those descriptions/tags in the first image should have a Total MH associated with it. The SQL code above does not separate the totals into its rows appropriately as shown in the first image. Here is the longer version of the code:

=DSum("[Total MH]","qry-PipingHandleMH","[Associated Bid Item]=[Text_BidItem].Value")+DSum("[Total MH]","qry-PipingMakeupMH","[Associated Bid Item]=[Text_BidItem].Value")+DSum("[Total MH]","qry-PipingValvesMH","[Associated Bid Item]=[Text_BidItem].Value")+DSum("[Total MH]","qry-PipingWeldingMH","[Associated Bid Item]=[Text_BidItem].Value")+DSum("[Total MH]","qry-CoatingAGMH","[Associated Bid Item]=[Text_BidItem].Value")+DSum("[Total MH]","qry-CoatingUGMH","[Associated Bid Item]=[Text_BidItem].Value")

Basically the same code applied to different tables but added together. That's is whats really in the second textbox in the second image currently. As you can see in the first image it is all applied to the "Large Bore Piping 2" and greater" row. Why isn't the summation of Total MH seperating into its respective rows? DSum() is working but the filtering and separation is just all wrong.