问:
Relative performance of swap vs compare-and-swap locks on x86
Two common locking idioms are:

```<code>if (!atomic_swap(lockaddr, 1)) /* got the lock */
</code>```

and:

```<code>if (!atomic_compare_and_swap(lockaddr, 0, val)) /* got the lock */
</code>```

where val could simply be a constant or an identifier for the new prospective owner of the lock.

What I'd like to know is whether there tends to be any significant performance difference between the two on x86 (and x86_64) machines. I know this is a fairly broad question since the answer might vary a lot between individual cpu models, but that's part of the reason I'm asking SO rather than just doing benchmarks on a few cpus I have access to.

答:
Using xchg vs cmpxchg to aquire the lock

In terms of performance on Intel's processors, it is the same, but for the sake of simplicity, to have things easier to fathom, I prefer the first way from the examples that you have given. There is no reason to use cmpxchg for acquiring a lock if you can do this with xchg.

According to the Occam's razor principle, simple things are better.

Besides that, locking with xchg is more powerful - you can also check the correctness of the logic of your software, i.e. that you are not accessing the memory byte that has not been explicitly allocated for locking. Thus, you will check that you are using the correctly initialized synchronization variable. Besides that, you will be able to check that you don't unlock twice.

Using normal memory store to release the lock

There is no consensus on whether writing to the synchronization variable on releasing the lock should be done with just a normal memory store (mov) or a bus-locking memory store, i.e. an instruction with implicit or explicit lock-prefix, like xchg.

The approach of using normal memory store to release lock was recommended by Peter Cordes, see the comments below for details.

But there are implementations where both acquiring and releasing the lock is done with a bus-locking memory store, since this approach seems to be straightforward and intuitive. For example, LeaveCriticalSection under Windows 10 uses bus-locking store to release the lock even on a single-socket processor; while on multiple physical processors with Non-Uniform-Memory-Access (NUMA), this issue is even more important.

I've done a micro-benchmarking on a memory manager that does lots of memory allocate/reallocate/free, on a single-socket CPU (Kaby Lake). When there is no contention, i.e. there are fewer threads than the physical cores, with locked release the tests complete about 10% slower, but when there are more threads when physical cores, tests with locked release complete 2% faster. So, on average, normal memory store to release lock outperforms locked memory store.

Example of locking that checks synchronization variable for validity

See this example (Delphi programming language) of safer locking functions that checks data of a synchronization variable for validity, and catches attempts to release locks that were not acquired:

```<code>const
  cLockAvailable = 107; // arbitrary constant, use any unique values that you like, I've chosen prime numbers
  cLockLocked    = 109;
  cLockFinished  = 113;

function AcquireLock(var Target: LONG): Boolean;
var
  R: LONG;
begin
  R := InterlockedExchange(Target, cLockByteLocked);
  case R of
    cLockAvailable: Result := True; // we've got a value that indicates that the lock was available, so return True to the caller indicating that we have acquired the lock
    cLockByteLocked: Result := False; // we've got a value that indicates that the lock was already acquire by someone else, so return False to the caller indicating that we have failed to acquire the lock this time
      else
        begin
          raise Exception.Create('Serious application error - tried to acquire lock using a variable that has not been properly initialized');
        end;
    end;
end;

procedure ReleaseLock(var Target: LONG);
var
  R: LONG;
begin
  // As Peter Cordes pointed out (see comments below), releasing the lock doesn't have to be interlocked, just a normal store. Even for debugging we use normal load. However, Windows 10 uses locked release on LeaveCriticalSection.
  R := Target;
  Target := cLockAvailable;
  if R <> cLockByteLocked  then
  begin
    raise Exception.Create('Serious application error - tried to release a  lock that has not been actually locked');
  end;
end;
</code>```

Your main application goes here:

```<code>var
  AreaLocked: LONG;
begin
  AreaLocked := cLockAvailable; // on program initialization, fill the default value

  ....

 if AcquireLock(AreaLocked) then
 try
   // do something critical with the locked area
   ...

 finally
   ReleaseLock(AreaLocked);
 end;

....

  AreaLocked := cLockFinished; // on program termination, set the special value to catch probable cases when somebody will try to acquire the lock

end.
</code>```

Efficient pause-based spin-wait loops

Test, test-and-set

You may also use the following assembly code (see the "Assembly code example of pause-based spin-wait loop" section below) as a working example of the "pause"-based spin-wait loop.

This code it uses normal memory load while spinning to save resources, as suggested by Peter Cordes. This technique is called "test, test-and-set". You can find out more on this technique at https://stackoverflow.com/a/44916975/6910868

Number of iterations

The pause-based spin-wait loop in this example first tries to acquire the lock by reading the synchronization variable, and if it is not available, utilize pause instruction in a loop of 5000 cycles. After 5000 cycles it calls Windows API function SwitchToThread(). This value of 5000 cycles is empirical. It is based on my tests. Values from 500 to 50000 also seem to be OK, but in some scenarios lower values are better while in other scenarios higher values are better. You can read more on pause-based spin-wait loops at the URL that I gave in the preceding paragraph.

Availability of the pause instruction

Please note that you may use this code only on processors that support SSE2 - you should check the corresponding CPUID bit before calling pause instruction - otherwise there will just be a waste of power. On processors without pause just use other means, like EnterCriticalSection/LeaveCriticalSection or Sleep(0) and then Sleep(1) in a loop. Some people say that on 64-bit processors you may not check for SSE2 to make sure that the pause instruction is implemented, because the original AMD64 architecture adopted Intel's SSE and SSE2 as core instructions, and, practically, if you run 64-bit code, you have already have SSE2 for sure and thus the pause instruction. However, Intel discourages a practice of relying on a presence specific feature and explicitly states that certain feature may vanish in future processors and applications must always check features via CPUID. However, the SSE instructions became ubiquitous and many 64-bit compilers use them without checking (e.g. Delphi for Win64), so chances that in some future processors there will be no SSE2, let alone pause, are very slim.

Assembly code example of pause-based spin-wait loop

```<code>// on entry rcx = address of the byte-lock
// on exit: al (eax) = old value of the byte at [rcx]
@Init:
   mov  edx, cLockByteLocked
   mov  r9d, 5000
   mov  eax, edx
   jmp  @FirstCompare
@DidntLock:
@NormalLoadLoop:
   dec  r9
   jz   @SwitchToThread // for static branch prediction, jump forward means "unlikely"
   pause
@FirstCompare:
   cmp  [rcx], al       // we are using faster, normal load to not consume the resources and only after it is ready, do once again interlocked exchange
   je   @NormalLoadLoop // for static branch prediction, jump backwards means "likely"
   lock xchg [rcx], al
   cmp  eax, edx        // 32-bit comparison is faster on newer processors like Xeon Phi or Cannonlake.
   je   @DidntLock
   jmp  @Finish
@SwitchToThread:
   push  rcx
   call  SwitchToThreadIfSupported
   pop   rcx
   jmp  @Init
@Finish:
</code>```

Body:
Two common locking idioms are:

if (!atomic_swap(lockaddr, 1)) /* got the lock */

and:

if (!atomic_compare_and_swap(lockaddr, 0, val)) /* got the lock */

where val could simply be a constant or an identifier for the new prospective owner of the lock.

What I'd like to know is whether there tends to be any significant performance difference between the two on x86 (and x86_64) machines. I know this is a fairly broad question since the answer might vary a lot between individual cpu models, but that's part of the reason I'm asking SO rather than just doing benchmarks on a few cpus I have access to.

Body:
Using xchg vs cmpxchg to aquire the lock

In terms of performance on Intel's processors, it is the same, but for the sake of simplicity, to have things easier to fathom, I prefer the first way from the examples that you have given. There is no reason to use cmpxchg for acquiring a lock if you can do this with xchg.

According to the Occam's razor principle, simple things are better.

Besides that, locking with xchg is more powerful - you can also check the correctness of the logic of your software, i.e. that you are not accessing the memory byte that has not been explicitly allocated for locking. Thus, you will check that you are using the correctly initialized synchronization variable. Besides that, you will be able to check that you don't unlock twice.

Using normal memory store to release the lock

There is no consensus on whether writing to the synchronization variable on releasing the lock should be done with just a normal memory store (mov) or a bus-locking memory store, i.e. an instruction with implicit or explicit lock-prefix, like xchg.

The approach of using normal memory store to release lock was recommended by Peter Cordes, see the comments below for details.

But there are implementations where both acquiring and releasing the lock is done with a bus-locking memory store, since this approach seems to be straightforward and intuitive. For example, LeaveCriticalSection under Windows 10 uses bus-locking store to release the lock even on a single-socket processor; while on multiple physical processors with Non-Uniform-Memory-Access (NUMA), this issue is even more important.

I've done a micro-benchmarking on a memory manager that does lots of memory allocate/reallocate/free, on a single-socket CPU (Kaby Lake). When there is no contention, i.e. there are fewer threads than the physical cores, with locked release the tests complete about 10% slower, but when there are more threads when physical cores, tests with locked release complete 2% faster. So, on average, normal memory store to release lock outperforms locked memory store.

Example of locking that checks synchronization variable for validity

See this example (Delphi programming language) of safer locking functions that checks data of a synchronization variable for validity, and catches attempts to release locks that were not acquired:

const
  cLockAvailable = 107; // arbitrary constant, use any unique values that you like, I've chosen prime numbers
  cLockLocked    = 109;
  cLockFinished  = 113;

function AcquireLock(var Target: LONG): Boolean;
var
  R: LONG;
begin
  R := InterlockedExchange(Target, cLockByteLocked);
  case R of
    cLockAvailable: Result := True; // we've got a value that indicates that the lock was available, so return True to the caller indicating that we have acquired the lock
    cLockByteLocked: Result := False; // we've got a value that indicates that the lock was already acquire by someone else, so return False to the caller indicating that we have failed to acquire the lock this time
      else
        begin
          raise Exception.Create('Serious application error - tried to acquire lock using a variable that has not been properly initialized');
        end;
    end;
end;

procedure ReleaseLock(var Target: LONG);
var
  R: LONG;
begin
  // As Peter Cordes pointed out (see comments below), releasing the lock doesn't have to be interlocked, just a normal store. Even for debugging we use normal load. However, Windows 10 uses locked release on LeaveCriticalSection.
  R := Target;
  Target := cLockAvailable;
  if R <> cLockByteLocked  then
  begin
    raise Exception.Create('Serious application error - tried to release a  lock that has not been actually locked');
  end;
end;

Your main application goes here:

var
  AreaLocked: LONG;
begin
  AreaLocked := cLockAvailable; // on program initialization, fill the default value

  ....

 if AcquireLock(AreaLocked) then
 try
   // do something critical with the locked area
   ...

 finally
   ReleaseLock(AreaLocked);
 end;

....

  AreaLocked := cLockFinished; // on program termination, set the special value to catch probable cases when somebody will try to acquire the lock

end.

Efficient pause-based spin-wait loops

Test, test-and-set

You may also use the following assembly code (see the "Assembly code example of pause-based spin-wait loop" section below) as a working example of the "pause"-based spin-wait loop.

This code it uses normal memory load while spinning to save resources, as suggested by Peter Cordes. This technique is called "test, test-and-set". You can find out more on this technique at https://stackoverflow.com/a/44916975/6910868

Number of iterations

The pause-based spin-wait loop in this example first tries to acquire the lock by reading the synchronization variable, and if it is not available, utilize pause instruction in a loop of 5000 cycles. After 5000 cycles it calls Windows API function SwitchToThread(). This value of 5000 cycles is empirical. It is based on my tests. Values from 500 to 50000 also seem to be OK, but in some scenarios lower values are better while in other scenarios higher values are better. You can read more on pause-based spin-wait loops at the URL that I gave in the preceding paragraph.

Availability of the pause instruction

Please note that you may use this code only on processors that support SSE2 - you should check the corresponding CPUID bit before calling pause instruction - otherwise there will just be a waste of power. On processors without pause just use other means, like EnterCriticalSection/LeaveCriticalSection or Sleep(0) and then Sleep(1) in a loop. Some people say that on 64-bit processors you may not check for SSE2 to make sure that the pause instruction is implemented, because the original AMD64 architecture adopted Intel's SSE and SSE2 as core instructions, and, practically, if you run 64-bit code, you have already have SSE2 for sure and thus the pause instruction. However, Intel discourages a practice of relying on a presence specific feature and explicitly states that certain feature may vanish in future processors and applications must always check features via CPUID. However, the SSE instructions became ubiquitous and many 64-bit compilers use them without checking (e.g. Delphi for Win64), so chances that in some future processors there will be no SSE2, let alone pause, are very slim.

Assembly code example of pause-based spin-wait loop

// on entry rcx = address of the byte-lock
// on exit: al (eax) = old value of the byte at [rcx]
@Init:
   mov  edx, cLockByteLocked
   mov  r9d, 5000
   mov  eax, edx
   jmp  @FirstCompare
@DidntLock:
@NormalLoadLoop:
   dec  r9
   jz   @SwitchToThread // for static branch prediction, jump forward means "unlikely"
   pause
@FirstCompare:
   cmp  [rcx], al       // we are using faster, normal load to not consume the resources and only after it is ready, do once again interlocked exchange
   je   @NormalLoadLoop // for static branch prediction, jump backwards means "likely"
   lock xchg [rcx], al
   cmp  eax, edx        // 32-bit comparison is faster on newer processors like Xeon Phi or Cannonlake.
   je   @DidntLock
   jmp  @Finish
@SwitchToThread:
   push  rcx
   call  SwitchToThreadIfSupported
   pop   rcx
   jmp  @Init
@Finish:


来源: stackexchange

问:
How to iterate over a list in chunks
I have a Python script which takes as input a list of integers, which I need to work with four integers at a time.  Unfortunately, I don't have control of the input, or I'd have it passed in as a list of four-element tuples.  Currently, I'm iterating over it this way:

```<code>for i in range(0, len(ints), 4):
    # dummy op for example code
    foo += ints[i] * ints[i + 1] + ints[i + 2] * ints[i + 3]
</code>```

It looks a lot like "C-think", though, which makes me suspect there's a more pythonic way of dealing with this situation.  The list is discarded after iterating, so it needn't be preserved.  Perhaps something like this would be better?

```<code>while ints:
    foo += ints[0] * ints[1] + ints[2] * ints[3]
    ints[0:4] = []
</code>```

Still doesn't quite "feel" right, though.  :-/

Related question: How do you split a list into evenly sized chunks in Python?

答:
Quite pythonic here (you may also inline the body of the split_groups function)

```<code>import itertools
def split_groups(iter_in, group_size):
    return ((x for _, x in item) for _, item in itertools.groupby(enumerate(iter_in), key=lambda x: x[0] // group_size))

for x, y, z, w in split_groups(range(16), 4):
    foo += x * y + z * w
</code>```

OwnerDisplayName: Ben Blank

ContentLicense: CC BY-SA 4.0

Title: How to iterate over a list in chunks

Body:
I have a Python script which takes as input a list of integers, which I need to work with four integers at a time.  Unfortunately, I don't have control of the input, or I'd have it passed in as a list of four-element tuples.  Currently, I'm iterating over it this way:

for i in range(0, len(ints), 4):
    # dummy op for example code
    foo += ints[i] * ints[i + 1] + ints[i + 2] * ints[i + 3]

It looks a lot like "C-think", though, which makes me suspect there's a more pythonic way of dealing with this situation.  The list is discarded after iterating, so it needn't be preserved.  Perhaps something like this would be better?

while ints:
    foo += ints[0] * ints[1] + ints[2] * ints[3]
    ints[0:4] = []

Still doesn't quite "feel" right, though.  :-/

Related question: How do you split a list into evenly sized chunks in Python?

LastEditorDisplayName: J.F. Sebastian

Body:
Quite pythonic here (you may also inline the body of the split_groups function)

import itertools
def split_groups(iter_in, group_size):
    return ((x for _, x in item) for _, item in itertools.groupby(enumerate(iter_in), key=lambda x: x[0] // group_size))

for x, y, z, w in split_groups(range(16), 4):
    foo += x * y + z * w

ContentLicense: CC BY-SA 3.0


问:
psycopg2 - Laravel Local Env
I am trying to get the psycopg2 module for python running in my Laravel application. It is working online on Heroku (I installed it there), but I am wondering about how I can get it running locally.

In Laravel I use:

```<code>use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
</code>```

Locally it throws this Error:

```import psycopg2
ImportError: No module named psycopg2```

On heroku, the code works properly, so it is just about the environment.
Does anyone know how to fix that?

PS: I use XAMPP locally

Thank you for your attention!

答: I'm running debian jessie and managed to get python 2.7 installed twice.  When I specify the directory (/usr/bin/python or /usr/local/bin/python) I can make the "No module" error appear and disappear.  Since I haven't seen this explanation before, I thought I'd share my experience.  Seems strange that ALL the attempts at installation acted on the version in /usr/local/bin, while the PATH selects the version at /usr/bin.  I think I will try removing the one at /usr/bin.  Hope this helps someone.</p>

Title: psycopg2 - Laravel Local Env

Body:
I am trying to get the psycopg2 module for python running in my Laravel application. It is working online on Heroku (I installed it there), but I am wondering about how I can get it running locally.

In Laravel I use:

use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;

Locally it throws this Error:

import psycopg2
ImportError: No module named psycopg2

On heroku, the code works properly, so it is just about the environment.
Does anyone know how to fix that?

PS: I use XAMPP locally

Thank you for your attention!


问:
Regex: Exclude first line
I've gone over solutions to similar answers on SE but they haven't worked for me.

Assume the following input:

@@A small note

Thanks

Here or [there][1]

A very nice thing

[1]: http://home.com

I want to exclude the first line. So my final output needs to be the text above, but starting with the line that says "Thanks" (not a blank line).

How can this be done?

答:
You might consider matching the start of a line other than the first line in the string, that starts with a non-whitespace char, and then match everything up to the string end.

You may use

```<code>(?ms)(?!\A)^\S.*
</code>```

See the regex demo.

Details:

(?ms) - MULTILINE (m) modifier making ^ match the start of a line
and the DOTALL modifier (s) that makes a . match any chars including line breaks

(?!\A) - a negative lookahead that fails the match if the current location is the start of the string (\A always matches the start of a string regardless of the presence of the MULTILINE modifier)

^ - start of a line

\S - a non-whitespace char (if there may be leading whitespace before the first non-whitespace char, add [^\S\r\n]* before \S)

.*  - any 0+ chars up to the string end.

Title: Regex: Exclude first line

Body:
You might consider matching the start of a line other than the first line in the string, that starts with a non-whitespace char, and then match everything up to the string end.

You may use

(?ms)(?!\A)^\S.*

See the regex demo.

Details:

(?ms) - MULTILINE (m) modifier making ^ match the start of a line
and the DOTALL modifier (s) that makes a . match any chars including line breaks

(?!\A) - a negative lookahead that fails the match if the current location is the start of the string (\A always matches the start of a string regardless of the presence of the MULTILINE modifier)

^ - start of a line

\S - a non-whitespace char (if there may be leading whitespace before the first non-whitespace char, add [^\S\r\n]* before \S)

.*  - any 0+ chars up to the string end.


问:
Reading local file from absolute path in node fs module
I am trying to read a file from local system using  node js 'fs' module. But for some reason, the 'fs' module is not working when I pass absolute path.

Code:

```<code>let filePath = "/home/mysystem/dev/myproject/sayHello.txt";
let newFile=fs.readFileSync('file://'+filePath);
</code>```

The code is throwing an error as:

```<code>Uncaught Error: ENOENT: no such file or directory, open 'file:///home/mysystem/dev/myproject/sayHello.txt'
</code>```

But I can open the file from browser window using the same path.
fs module is working if I pass relative path. I am using this inside an app built using electron framework.

答:
I got stumped with this for a bit. If using Angular 2 and Typescript on Windows, your absolute path will look like this: </p>

```<code>import { readdirSync } from "fs";
. . .
let x = readdirSync("C:/SAFE/Dir1/Blah");
console.log("files retrieved="+ x.length);
</code>```

Body:
I am trying to read a file from local system using  node js 'fs' module. But for some reason, the 'fs' module is not working when I pass absolute path.

Code:

let filePath = "/home/mysystem/dev/myproject/sayHello.txt";
let newFile=fs.readFileSync('file://'+filePath);

The code is throwing an error as:

Uncaught Error: ENOENT: no such file or directory, open 'file:///home/mysystem/dev/myproject/sayHello.txt'

But I can open the file from browser window using the same path.
fs module is working if I pass relative path. I am using this inside an app built using electron framework.

Body:
I got stumped with this for a bit. If using Angular 2 and Typescript on Windows, your absolute path will look like this:

import { readdirSync } from "fs";
. . .
let x = readdirSync("C:/SAFE/Dir1/Blah");
console.log("files retrieved="+ x.length);


问:
Async transactions in javascript
First of all rollback is something that I do not care about.

I would like to be able to lock a sequence of async functions/promises/tasks (let's call it a "transaction") with a name/id (or array of names), so that they happen in sequence, and so that any other "transaction" with the same name(s) that are run by another part of the system are delayed from starting until the running transaction using the same name(s) has completed.  So it basically is queueing the sequences of async tasks, or "transaction"s.

Here is some example code of the situation:

```<code>function a()
{
  // do stuff
  return new Promise(/*...*/);
}

function b()
{
  // do stuff
  return new Promise(/*...*/);
}

function c()
{
  // do stuff
  return a.then(() => b());
}
</code>```

Now at any time the system could call a, b, or c, and when it does I don't want c and b running at the same time, but obvious c depends on b.

I've been looking for a package on npm to help with this but I haven't found anything, I wonder if anyone can suggest something that I might have missed that would help with this?

答:
Ok, here's my take.
You use a wrapper for your function b which returns and object with 2 methods: doCall and wait. The wrapper should be called only once.

doCall will call your function and trace its completion for the wait() function.

wait() will wait for the completion and always resolve when doCall() finishes

Now for the code, also on CodePen (see developer console):

function wrapPromiseFn(fn) {
  var prev = null;
  var doCall = function() {
    var retValue;

    prev = new Promise(function(resolve, reject) {
      retValue = fn();
      retValue.then(function(result) {
        resolve(true);
      });
      retValue.catch(function(result) {
        resolve(true);
      });
    });

    return retValue;
  };

  var wait = function() {
    return prev ? prev : Promise.resolve(false);
  };

  return {
    doCall: doCall,
    wait: wait
  };
}

function a() {
  return Promise.resolve(42);
}

function b() {
  //return Promise.reject(new Date());
  return Promise.resolve(new Date().getTime());
}

var wrappedB = wrapPromiseFn(b);

function doStuff() {
  return wrappedB.wait().then(function(didWait) {
    return a().then(function(_a) {
      return wrappedB.doCall().then(function(_b) {
        console.log("didWait, a, b: ", didWait, _a, _b);
      });
    });
  });
}

//run it twice to prove it did wait
doStuff().then(doStuff)```

It proves the concept, of course it would need some polish to pass arguments from doCall to the wrapped function.

Title: Async transactions in javascript

Body:
First of all rollback is something that I do not care about.

I would like to be able to lock a sequence of async functions/promises/tasks (let's call it a "transaction") with a name/id (or array of names), so that they happen in sequence, and so that any other "transaction" with the same name(s) that are run by another part of the system are delayed from starting until the running transaction using the same name(s) has completed.  So it basically is queueing the sequences of async tasks, or "transaction"s.

Here is some example code of the situation:

function a()
{
  // do stuff
  return new Promise(/*...*/);
}

function b()
{
  // do stuff
  return new Promise(/*...*/);
}

function c()
{
  // do stuff
  return a.then(() => b());
}

Now at any time the system could call a, b, or c, and when it does I don't want c and b running at the same time, but obvious c depends on b.

I've been looking for a package on npm to help with this but I haven't found anything, I wonder if anyone can suggest something that I might have missed that would help with this?

Body:
Ok, here's my take.
You use a wrapper for your function b which returns and object with 2 methods: doCall and wait. The wrapper should be called only once.

doCall will call your function and trace its completion for the wait() function.

wait() will wait for the completion and always resolve when doCall() finishes

Now for the code, also on CodePen (see developer console):

function wrapPromiseFn(fn) {
  var prev = null;
  var doCall = function() {
    var retValue;

    prev = new Promise(function(resolve, reject) {
      retValue = fn();
      retValue.then(function(result) {
        resolve(true);
      });
      retValue.catch(function(result) {
        resolve(true);
      });
    });

    return retValue;
  };

  var wait = function() {
    return prev ? prev : Promise.resolve(false);
  };

  return {
    doCall: doCall,
    wait: wait
  };
}

function a() {
  return Promise.resolve(42);
}

function b() {
  //return Promise.reject(new Date());
  return Promise.resolve(new Date().getTime());
}

var wrappedB = wrapPromiseFn(b);

function doStuff() {
  return wrappedB.wait().then(function(didWait) {
    return a().then(function(_a) {
      return wrappedB.doCall().then(function(_b) {
        console.log("didWait, a, b: ", didWait, _a, _b);
      });
    });
  });
}

//run it twice to prove it did wait
doStuff().then(doStuff)

It proves the concept, of course it would need some polish to pass arguments from doCall to the wrapped function.


问:
Azure Storage Emulator 403 Forbidden
Via Nuget, I upgraded WindowsAzure.Storage to 8.1.1.

I then downloaded the AzureStorageEmulator 5.1.0.0 client.

My connection string:

```<code>UseDevelopmentStorage=true;
</code>```

I've made no code changes since previously when it was apparently working fine.  I know get the exception:

```<code>Microsoft.WindowsAzure.Storage.StorageException: The remote server returned an error: (403) Forbidden. ---> System.Net.WebException: The remote server returned an error: (403) Forbidden.
   at System.Net.HttpWebRequest.GetResponse()
   at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand`1 cmd, IRetryPolicy policy, OperationContext operationContext) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Core\Executor\Executor.cs:line 677
   --- End of inner exception stack trace ---
   at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand`1 cmd, IRetryPolicy policy, OperationContext operationContext) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Core\Executor\Executor.cs:line 604
   at Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer.CreateIfNotExists(BlobContainerPublicAccessType accessType, BlobRequestOptions requestOptions, OperationContext operationContext) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Blob\CloudBlobContainer.cs:line 233
   at C3.Code.Controls.Application.Storage.Blob.Blob.GetContainer(String containerName) in C:\Dropbox\Dropbox (Scirra Ltd)\Tom\C3 Website\C3Alpha2\Code\Controls\Application\Storage\Blob\Blob.cs:line 112
Request Information
RequestID:621bc19f-eb6a-4a98-b19e-f5b01ac22c26
RequestDate:Thu, 27 Apr 2017 16:17:34 GMT
StatusMessage:Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
ErrorCode:AuthenticationFailed
ErrorMessage:Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
RequestId:621bc19f-eb6a-4a98-b19e-f5b01ac22c26
Time:2017-04-27T16:17:34.5166522Z
</code>```

When I call

```<code>var blobClient = GetClient();
var container = blobClient.GetContainerReference(containerName);
container.CreateIfNotExists(BlobContainerPublicAccessType.Blob);
</code>```

I saw that system time can impact this, so I checked:

```<code>Server DateTime.UtcNow = 27/04/2017 16:17:34
Exception RequestDate = Thu, 27 Apr 2017 16:17:34 GMT
</code>```

Any ideas why I'm seeing this error after updating the client and the nuget package?

Edit: GetClient() Code:

```<code>private static CloudBlobClient GetClient()
{
    var account = CloudStorageAccount.Parse(Settings.Deployment.AzureConnectionString);
    return account.CreateCloudBlobClient();
}
</code>```

Edit: BaseUri

Base URI is:
    http://127.0.0.1:10000/devstoreaccount1

Visit this I get:

```<code><Error>
<Code>InvalidQueryParameterValue</Code>
<Message>
Value for one of the query parameters specified in the request URI is invalid. RequestId:9cc906b0-eec6-44b2-bb3d-f77020af4a4c Time:2017-04-27T16:43:16.8538679Z
</Message>
<QueryParameterName>comp</QueryParameterName>
<QueryParameterValue/>
<Reason/>
</Error>
</code>```

During storage emulator init, it shows it's installed it to:

(localdb)\MSSQLLocalDB as AzureStorageEmulatorDb51

Visiting: http://127.0.0.1:10000/azurestorageemulatordb51

Shows:

```<code><Error>
<Code>OutOfRangeInput</Code>
<Message>
One of the request inputs is out of range. RequestId:dab5e6c1-bc4a-4c65-a4d9-6d44634cb36a Time:2017-04-27T16:47:20.3093597Z
</Message>
</Error>
</code>```

答:
We had the same issue after installing the Application Insights package into our business rules project.
We have an N-tier solution with asp.net core, which has a different version of application insights.

It turns out we installed the non asp.net core package into the business rules, which then produced the Forbidden 403 error when accessing Queue Client.

We removed the non asp.net core application insights, installed the application insights core into the business rules project and all worked ok.

Title: Azure Storage Emulator 403 Forbidden

Body:
Via Nuget, I upgraded WindowsAzure.Storage to 8.1.1.

I then downloaded the AzureStorageEmulator 5.1.0.0 client.

My connection string:

UseDevelopmentStorage=true;

I've made no code changes since previously when it was apparently working fine.  I know get the exception:

Microsoft.WindowsAzure.Storage.StorageException: The remote server returned an error: (403) Forbidden. ---> System.Net.WebException: The remote server returned an error: (403) Forbidden.
   at System.Net.HttpWebRequest.GetResponse()
   at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand`1 cmd, IRetryPolicy policy, OperationContext operationContext) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Core\Executor\Executor.cs:line 677
   --- End of inner exception stack trace ---
   at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand`1 cmd, IRetryPolicy policy, OperationContext operationContext) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Core\Executor\Executor.cs:line 604
   at Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer.CreateIfNotExists(BlobContainerPublicAccessType accessType, BlobRequestOptions requestOptions, OperationContext operationContext) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Blob\CloudBlobContainer.cs:line 233
   at C3.Code.Controls.Application.Storage.Blob.Blob.GetContainer(String containerName) in C:\Dropbox\Dropbox (Scirra Ltd)\Tom\C3 Website\C3Alpha2\Code\Controls\Application\Storage\Blob\Blob.cs:line 112
Request Information
RequestID:621bc19f-eb6a-4a98-b19e-f5b01ac22c26
RequestDate:Thu, 27 Apr 2017 16:17:34 GMT
StatusMessage:Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
ErrorCode:AuthenticationFailed
ErrorMessage:Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
RequestId:621bc19f-eb6a-4a98-b19e-f5b01ac22c26
Time:2017-04-27T16:17:34.5166522Z

When I call

var blobClient = GetClient();
var container = blobClient.GetContainerReference(containerName);
container.CreateIfNotExists(BlobContainerPublicAccessType.Blob);

I saw that system time can impact this, so I checked:

Server DateTime.UtcNow = 27/04/2017 16:17:34
Exception RequestDate = Thu, 27 Apr 2017 16:17:34 GMT

Any ideas why I'm seeing this error after updating the client and the nuget package?

Edit: GetClient() Code:

private static CloudBlobClient GetClient()
{
    var account = CloudStorageAccount.Parse(Settings.Deployment.AzureConnectionString);
    return account.CreateCloudBlobClient();
}

Edit: BaseUri

Base URI is:
    http://127.0.0.1:10000/devstoreaccount1

Visit this I get:

<Error>
InvalidQueryParameterValue
<Message>
Value for one of the query parameters specified in the request URI is invalid. RequestId:9cc906b0-eec6-44b2-bb3d-f77020af4a4c Time:2017-04-27T16:43:16.8538679Z
</Message>
<QueryParameterName>comp</QueryParameterName>
<QueryParameterValue/>
<Reason/>
</Error>

During storage emulator init, it shows it's installed it to:

(localdb)\MSSQLLocalDB as AzureStorageEmulatorDb51

Visiting: http://127.0.0.1:10000/azurestorageemulatordb51

Shows:

<Error>
OutOfRangeInput
<Message>
One of the request inputs is out of range. RequestId:dab5e6c1-bc4a-4c65-a4d9-6d44634cb36a Time:2017-04-27T16:47:20.3093597Z
</Message>
</Error>


问:
Handling scroll Animation in React
What is the correct way of dealing with scroll position in React? I really like smooth scrolling because of better UX. Since manipulating the DOM in React is an anti-pattern I ran into problem: how to scroll smoothly to some position/element? I usually change scrollTop value of an element, but this is a manipulation on the DOM, which is not allowed.

JSBIN

code:

```<code>import React from 'react';
import ReactDOM from 'react-dom';

class App extends React.Component {
  handleClick = e => {
    for (let i = 1; i <= 100; i++) {
      setTimeout(() => (this.node.scrollTop = i), i * 2);
    }
  };

  render() {
    const someArrayToMap = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

    return (
      <div ref={node => this.node = node} style={{overflow: 'auto', height: '100vh'}}>
        <button onClick={this.handleClick}>CLICK TO SCROLL</button>
        {
            [...someArrayToMap,
            ...someArrayToMap,
            ...someArrayToMap,
            ...someArrayToMap,
            ...someArrayToMap,
            ...someArrayToMap,
            ...someArrayToMap].map((e, i) => <div key={i}>some text here</div>)
        }
      </div>
    );
  }
}

ReactDOM.render(<App />, document.getElementById('root'));
</code>```

How to achieve this in a React way?

答:
There are several libraries for scrolling to anchors in React. The one to choose depend on the features you're seeking and the existing setup of your page.

React Scrollable Anchor is a lightweight library that's specifically for scrolling to anchors that are mapped to URL hashes. It also updates the URL hash based on currently focused section. [Full disclosure: I'm the author of this library]

React Scroll, mentioned in another answer, is a more fully featured library for scrolling to anchors, without any reflection of location in the URL.

You can also hook up something like React Router Hash Link Scroll if you're already using React Router, which will then also tie into your URL hash.

Title: Handling scroll Animation in React

Body:
What is the correct way of dealing with scroll position in React? I really like smooth scrolling because of better UX. Since manipulating the DOM in React is an anti-pattern I ran into problem: how to scroll smoothly to some position/element? I usually change scrollTop value of an element, but this is a manipulation on the DOM, which is not allowed.

JSBIN

code:

import React from 'react';
import ReactDOM from 'react-dom';

class App extends React.Component {
  handleClick = e => {
    for (let i = 1; i <= 100; i++) {
      setTimeout(() => (this.node.scrollTop = i), i * 2);
    }
  };

  render() {
    const someArrayToMap = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

    return (

 this.node = node} style={{overflow: 'auto', height: '100vh'}}>
        <button onClick={this.handleClick}>CLICK TO SCROLL</button>
        {
            [...someArrayToMap,
            ...someArrayToMap,
            ...someArrayToMap,
            ...someArrayToMap,
            ...someArrayToMap,
            ...someArrayToMap,
            ...someArrayToMap].map((e, i) =>
some text here
)
        }

    );
  }
}

ReactDOM.render(<App />, document.getElementById('root'));

How to achieve this in a React way?


问:
Can't loop or map over FileReader blob
I'm using the FileReader API to grab a file (preferably a JSON file) and then loop over the information exposed that way and display it to the page. I can grab the information and console log it but every time I try to .forEach or .map over it, it yells at me that I'm doing it wrong. Can someone point out what I'm doing wrong? I've included a link.

https://jsfiddle.net/alexmarple/c7mco54p/4/

```<code>var fileInfo = document.getElementById('file-info');
var localArr;
document.getElementById('input').addEventListener('change', function(){
  var file = this.files[0];
  var reader = new FileReader();
  reader.readAsText(file);
  reader.onload = function(event){
    var arr = event.target.result;
    localArr = arr;
    //console.log(arr);
    arr.forEach(function(item){
     console.log(item);
    });
  // scrub information
  // display in #file-info
 }
}, false);
</code>```

答:
event.target.result is a String, not an Array. You need to JSON.parse it first:

```<code>reader.onload = function(event){
  var arr = JSON.parse(event.target.result);
  // ...
}
</code>```

The way that you use forEach assumes the uploaded file contains an Array. If it's an Object, you could use Object.keys to iterate its properties:

```<code>reader.onload = function(event){
  var obj = JSON.parse(event.target.result);
  Object.keys(obj).forEach(function (key) {
    var value = obj[key];
    // ...
  })
}
</code>```

Title: Can't loop or map over FileReader blob

Body:
I'm using the FileReader API to grab a file (preferably a JSON file) and then loop over the information exposed that way and display it to the page. I can grab the information and console log it but every time I try to .forEach or .map over it, it yells at me that I'm doing it wrong. Can someone point out what I'm doing wrong? I've included a link.

https://jsfiddle.net/alexmarple/c7mco54p/4/

var fileInfo = document.getElementById('file-info');
var localArr;
document.getElementById('input').addEventListener('change', function(){
  var file = this.files[0];
  var reader = new FileReader();
  reader.readAsText(file);
  reader.onload = function(event){
    var arr = event.target.result;
    localArr = arr;
    //console.log(arr);
    arr.forEach(function(item){
     console.log(item);
    });
  // scrub information
  // display in #file-info
 }
}, false);

Body:
event.target.result is a String, not an Array. You need to JSON.parse it first:

reader.onload = function(event){
  var arr = JSON.parse(event.target.result);
  // ...
}

The way that you use forEach assumes the uploaded file contains an Array. If it's an Object, you could use Object.keys to iterate its properties:

reader.onload = function(event){
  var obj = JSON.parse(event.target.result);
  Object.keys(obj).forEach(function (key) {
    var value = obj[key];
    // ...
  })
}


问:
Arranging a 3 dimensional contingency table in R in order to run a Cochran-Mantel-Haenszel analysis?
I am attempting to run a Mantel-Haenszel analysis in R to determine whether or not a comparison of proportions test is still significant when accounting for a 'diagnosis' ratio within groups. This test is available in the stats package.

```<code>library(stats)
mantelhaen.test(x)
</code>```

Having done some reading, I've found that this test can perform an odds ratio test on a contingency table that is n x n x k, as opposed to simply n x n. However, I am having trouble arranging my data in the proper way, as I am fairly new to R. I have created some example data...

```<code>ex.label <- c("A","A","A","A","A","A","A","B","B","B")
ex.status  <- c("+","+","-","+","-","-","-","+","+","-")
ex.diag <- c("X","X","Z","Y","Y","Y","X","Y","Z","Z")
ex.data <- data.frame(ex.label,ex.diag,ex.status)
</code>```

Which looks like this...

```<code>  ex.label ex.diag ex.status
1         A       X         +
2         A       X         +
3         A       Z         -
4         A       Y         +
5         A       Y         -
6         A       Y         -
7         A       X         -
8         B       Y         +
9         B       Z         +
10        B       Z         -
</code>```

I was originally able to use a simple N-1 chi-square to run a comparison of proportions test of + to - for only the A and B, but now I want to be able to account for the ex.diag as well. I'll show a graph here for what I wanted to be looking at, which is basically to compare the significance of the ratio in each column. I was able to do this, but I now want to be able to account for ex.diag.

I tried to use the ftable() function to arrange my data in a way that would work.

```<code>ex.ftable <- ftable(ex.data)
</code>```

Which looks like this...

```<code>                 ex.status - +
ex.label ex.diag
A        X                 1 2
         Y                 2 1
         Z                 1 0
B        X                 0 0
         Y                 0 1
         Z                 1 1
</code>```

However, when I run mantelhaen.test(ex.ftable), I get the error 'x' must be a 3-dimensional array. How can I arrange my data in such a way that I can actually run this test?

答:
In mantelhaen.test the last dimension of the 3-dimensional contingency table x needs to be the stratification variable (ex.diag). This matrix can be generated as follows:

```<code>ex.label <- c("A","A","A","A","A","A","A","B","B","B")
ex.status  <- c("+","+","-","+","-","-","-","+","+","-")
ex.diag <- c("X","X","Z","Y","Y","Y","X","Y","Z","Z")

# Now ex.diag is in the first column
ex.data <- data.frame(ex.diag, ex.label, ex.status)

# The flat table
( ex.ftable <- ftable(ex.data) )

#                  ex.status - +
# ex.diag ex.label
# X       A                  1 2
#         B                  0 0
# Y       A                  2 1
#         B                  0 1
# Z       A                  1 0
#         B                  1 1
</code>```

The 3D matrix can be generated using aperm.

```<code># Trasform the ftable into a 2 x 2 x 3 array
# First dimension: ex.label
# Second dimension: ex.status
# Third dimension: ex.diag
( mtx3D <- aperm(array(t(as.matrix(ex.ftable)),c(2,2,3)),c(2,1,3)) )

#     , , 1
#
#      [,1] [,2]
# [1,]    1    2
# [2,]    0    0
#
# , , 2
#
#      [,1] [,2]
# [1,]    2    1
# [2,]    0    1
#
# , , 3
#
#      [,1] [,2]
# [1,]    1    0
# [2,]    1    1
</code>```

Now the Cochran-Mantel-Haenszel chi-squared test can be performed.

```<code># Cochran-Mantel-Haenszel chi-squared test of the null that
# two nominal variables are conditionally independent in each stratum
#
mantelhaen.test(mtx3D, exact=FALSE)
</code>```

The results of the test is

```<code>        Mantel-Haenszel chi-squared test with continuity correction

data:  mtx3D
Mantel-Haenszel X-squared = 0.23529, df = 1, p-value = 0.6276
alternative hypothesis: true common odds ratio is not equal to 1
95 percent confidence interval:
 NaN NaN
sample estimates:
common odds ratio
              Inf
</code>```

Given the low number of cases,  it is preferable to compute an exact conditional test (option exact=TRUE).

```<code>mantelhaen.test(mtx3D, exact=T)

#         Exact conditional test of independence in 2 x 2 x k tables
#
# data:  mtx3D
# S = 4, p-value = 0.5
# alternative hypothesis: true common odds ratio is not equal to 1
# 95 percent confidence interval:
#  0.1340796       Inf
# sample estimates:
# common odds ratio
#               Inf
</code>```

Body:
I am attempting to run a Mantel-Haenszel analysis in R to determine whether or not a comparison of proportions test is still significant when accounting for a 'diagnosis' ratio within groups. This test is available in the stats package.

library(stats)
mantelhaen.test(x)

Having done some reading, I've found that this test can perform an odds ratio test on a contingency table that is n x n x k, as opposed to simply n x n. However, I am having trouble arranging my data in the proper way, as I am fairly new to R. I have created some example data...

ex.label <- c("A","A","A","A","A","A","A","B","B","B")
ex.status  <- c("+","+","-","+","-","-","-","+","+","-")
ex.diag <- c("X","X","Z","Y","Y","Y","X","Y","Z","Z")
ex.data <- data.frame(ex.label,ex.diag,ex.status)

Which looks like this...

  ex.label ex.diag ex.status
1         A       X         +
2         A       X         +
3         A       Z         -
4         A       Y         +
5         A       Y         -
6         A       Y         -
7         A       X         -
8         B       Y         +
9         B       Z         +
10        B       Z         -

I was originally able to use a simple N-1 chi-square to run a comparison of proportions test of + to - for only the A and B, but now I want to be able to account for the ex.diag as well. I'll show a graph here for what I wanted to be looking at, which is basically to compare the significance of the ratio in each column. I was able to do this, but I now want to be able to account for ex.diag.

I tried to use the ftable() function to arrange my data in a way that would work.

ex.ftable <- ftable(ex.data)

Which looks like this...

                 ex.status - +
ex.label ex.diag
A        X                 1 2
         Y                 2 1
         Z                 1 0
B        X                 0 0
         Y                 0 1
         Z                 1 1

However, when I run mantelhaen.test(ex.ftable), I get the error 'x' must be a 3-dimensional array. How can I arrange my data in such a way that I can actually run this test?

Body:
In mantelhaen.test the last dimension of the 3-dimensional contingency table x needs to be the stratification variable (ex.diag). This matrix can be generated as follows:

ex.label <- c("A","A","A","A","A","A","A","B","B","B")
ex.status  <- c("+","+","-","+","-","-","-","+","+","-")
ex.diag <- c("X","X","Z","Y","Y","Y","X","Y","Z","Z")

# Now ex.diag is in the first column
ex.data <- data.frame(ex.diag, ex.label, ex.status)

# The flat table
( ex.ftable <- ftable(ex.data) )

#                  ex.status - +
# ex.diag ex.label
# X       A                  1 2
#         B                  0 0
# Y       A                  2 1
#         B                  0 1
# Z       A                  1 0
#         B                  1 1

The 3D matrix can be generated using aperm.

# Trasform the ftable into a 2 x 2 x 3 array
# First dimension: ex.label
# Second dimension: ex.status
# Third dimension: ex.diag
( mtx3D <- aperm(array(t(as.matrix(ex.ftable)),c(2,2,3)),c(2,1,3)) )

#     , , 1
#
#      [,1] [,2]
# [1,]    1    2
# [2,]    0    0
#
# , , 2
#
#      [,1] [,2]
# [1,]    2    1
# [2,]    0    1
#
# , , 3
#
#      [,1] [,2]
# [1,]    1    0
# [2,]    1    1

Now the Cochran-Mantel-Haenszel chi-squared test can be performed.

# Cochran-Mantel-Haenszel chi-squared test of the null that
# two nominal variables are conditionally independent in each stratum
#
mantelhaen.test(mtx3D, exact=FALSE)

The results of the test is

        Mantel-Haenszel chi-squared test with continuity correction

data:  mtx3D
Mantel-Haenszel X-squared = 0.23529, df = 1, p-value = 0.6276
alternative hypothesis: true common odds ratio is not equal to 1
95 percent confidence interval:
 NaN NaN
sample estimates:
common odds ratio
              Inf

Given the low number of cases,  it is preferable to compute an exact conditional test (option exact=TRUE).

mantelhaen.test(mtx3D, exact=T)

#         Exact conditional test of independence in 2 x 2 x k tables
#
# data:  mtx3D
# S = 4, p-value = 0.5
# alternative hypothesis: true common odds ratio is not equal to 1
# 95 percent confidence interval:
#  0.1340796       Inf
# sample estimates:
# common odds ratio
#               Inf


问:
How to get YouTube video Id by the video name?
I am developing an android app, and I am trying to display a YouTube video, using the video name (with YouTubePlayer and YouTubeBaceActivity).

I am able to display the video if I have the video Id, but I don't know how to display the video by using only the video name.

I have tried to use HttoUrlConnaction by enter:

https://www.googleapis.com/youtube/v3/search?part=snippet&q=<MOVIE_NAME>&type=video&key=<APIKEY>

but it doesn't work for some reason and returns FileNotFoundException even though it works when I type the URL in google...

So how can I get the video Id, or how can I get the video only with the video name?

Thank You!

答:
Am assuming that you are getting a JSON thrown back from an endpoint. If thats the case then you can simply point to this endpoint and parse the JSON to get the respective urls that you can eventually render on your view. (Refer to following example).

You can use the Volley library or Retrofit library to handle the HTTP requests.

//-----------------------------------------------------------------------

```<code>private static final String endpoint = "http://api.youtube.********/json/videos.json";

//*************************************
//** JSON Video Output from Endpoint
//*************************************
[{
    "name": "Rocky V",
            "url": {
                "small": "http://www.youtube.com/watch?v=VA770wpLX-Q&feature=youtube_gdata_player",
                "medium": "http://www.youtube.com/watch?v=VA770wpLX-Q&feature=youtube_gdata_player",
                "large": "http://www.youtube.com/watch?v=VA770wpLX-Q&feature=youtube_gdata_player"
    },
    "timestamp": "February 1, 2012"
},
{
    "name": "Rambo",
        "url": {
            "small": "http://www.youtube.com/watch?v=VA770wpLX-Q&feature=youtube_gdata_player",
            "medium": "http://www.youtube.com/watch?v=VA770wpLX-Q&feature=youtube_gdata_player",
            "large": "http://www.youtube.com/watch?v=VA770wpLX-Q&feature=youtube_gdata_player"
},
    "timestamp": "March 5, 2014"
}]

//*************************************
//** Parse JSON to Fetch Videos
//*************************************
private void fetchVideos() {

    JsonArrayRequest req = new JsonArrayRequest(endpoint,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());

                    videos.clear();
                    for (int i = 0; i < response.length(); i++) {
                        try {
                            JSONObject object = response.getJSONObject(i);
                            Video video = new Video();
                            video.setName(object.getString("name"));

                            JSONObject url = object.getJSONObject("url");
                            video.setSmall(url.getString("small"));
                            video.setMedium(url.getString("medium"));
                            video.setLarge(url.getString("large"));
                            video.setTimestamp(object.getString("timestamp"));

                            videos.add(video);

                        } catch (JSONException e) {
                            Log.e(TAG, "Json parsing error: " + e.getMessage());
                        }
                    }

                    mAdapter.notifyDataSetChanged();
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "Error: " + error.getMessage());
            //pDialog.hide();
        }
    });

    // Adding request to request queue
    Application.getInstance().addToRequestQueue(req);
}

//-------------------------------------------------------------------
</code>```

Body:
Am assuming that you are getting a JSON thrown back from an endpoint. If thats the case then you can simply point to this endpoint and parse the JSON to get the respective urls that you can eventually render on your view. (Refer to following example).

You can use the Volley library or Retrofit library to handle the HTTP requests.

//-----------------------------------------------------------------------

private static final String endpoint = "http://api.youtube.********/json/videos.json";

//*************************************
//** JSON Video Output from Endpoint
//*************************************
[{
    "name": "Rocky V",
            "url": {
                "small": "http://www.youtube.com/watch?v=VA770wpLX-Q&feature=youtube_gdata_player",
                "medium": "http://www.youtube.com/watch?v=VA770wpLX-Q&feature=youtube_gdata_player",
                "large": "http://www.youtube.com/watch?v=VA770wpLX-Q&feature=youtube_gdata_player"
    },
    "timestamp": "February 1, 2012"
},
{
    "name": "Rambo",
        "url": {
            "small": "http://www.youtube.com/watch?v=VA770wpLX-Q&feature=youtube_gdata_player",
            "medium": "http://www.youtube.com/watch?v=VA770wpLX-Q&feature=youtube_gdata_player",
            "large": "http://www.youtube.com/watch?v=VA770wpLX-Q&feature=youtube_gdata_player"
},
    "timestamp": "March 5, 2014"
}]

//*************************************
//** Parse JSON to Fetch Videos
//*************************************
private void fetchVideos() {

    JsonArrayRequest req = new JsonArrayRequest(endpoint,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());

                    videos.clear();
                    for (int i = 0; i < response.length(); i++) {
                        try {
                            JSONObject object = response.getJSONObject(i);
                            Video video = new Video();
                            video.setName(object.getString("name"));

                            JSONObject url = object.getJSONObject("url");
                            video.setSmall(url.getString("small"));
                            video.setMedium(url.getString("medium"));
                            video.setLarge(url.getString("large"));
                            video.setTimestamp(object.getString("timestamp"));

                            videos.add(video);

                        } catch (JSONException e) {
                            Log.e(TAG, "Json parsing error: " + e.getMessage());
                        }
                    }

                    mAdapter.notifyDataSetChanged();
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "Error: " + error.getMessage());
            //pDialog.hide();
        }
    });

    // Adding request to request queue
    Application.getInstance().addToRequestQueue(req);
}

//-------------------------------------------------------------------


问:
Iterating over multiple rows in Python - Science Data Cleaning
I am working with a large private dataset and I need some assistance in figuring out how to iterate a program over multiple rows.

```<code>import csv

with open('report_export.csv') as f:

    reader = csv.DictReader(f)
    report_export = list(reader)

x = report_export[25]["Text_Content"]
x.split(sep='. ')[1]

 ' Azithromycin is an antibiotic agent and a member of a subclass of
   macrolide antibiotics with bactericidal and bacteriostatic activities.
</code>```

report_export.csv is a data file pull from a local database containing information chemicals from publications. I want some textual information from this file. It is located under the column "Text Content". "25" is a random row that worked as proof of principle for the code. The x.split(sep='. ')[1] is used
 for isolating the desired string and differentiating between decimals and periods. This file is rather large, 5000 rows on CSV, and I want to be able to extract statements about the chemicals similar to output above.

I am having difficulty figuring out how to iterate over multiple rows in this case. I need the program to read over the rows, retrieve the 2nd string (1 position) from the list, and be able to save this data to a new csv file.

Any assistance with achieving the ability to iterate over rows would be helpful.

Thank you!

Best,

PEB

答:
You are almost there. If all you want to do is extract and write to a file, just use a some sort of writer object from csv, and use the reader-writer combination, while you iterate directly over the reader object:

```<code>import csv
with open('report_export.csv', newline='') as infile,\
            open('report_out.csv', 'w', newline='') as outfile:

    reader = csv.DictReader(infile)
    fieldnames = ["Text_content"]
    writer = csv.DicWriter(outfile, fieldnames=fieldnames)

    for row in reader: # iterates over the csv row-by-row
        data = row["Text_Content"].split(sep='. ')[1]
        writer.writerow({"Text_content": data})
</code>```

The above assumes Python 3.

Body:
I am working with a large private dataset and I need some assistance in figuring out how to iterate a program over multiple rows.

import csv

with open('report_export.csv') as f:

    reader = csv.DictReader(f)
    report_export = list(reader)

x = report_export[25]["Text_Content"]
x.split(sep='. ')[1]

 ' Azithromycin is an antibiotic agent and a member of a subclass of
   macrolide antibiotics with bactericidal and bacteriostatic activities.

report_export.csv is a data file pull from a local database containing information chemicals from publications. I want some textual information from this file. It is located under the column "Text Content". "25" is a random row that worked as proof of principle for the code. The x.split(sep='. ')[1] is used
 for isolating the desired string and differentiating between decimals and periods. This file is rather large, 5000 rows on CSV, and I want to be able to extract statements about the chemicals similar to output above.

I am having difficulty figuring out how to iterate over multiple rows in this case. I need the program to read over the rows, retrieve the 2nd string (1 position) from the list, and be able to save this data to a new csv file.

Any assistance with achieving the ability to iterate over rows would be helpful.

Thank you!

Best,

PEB

Body:
You are almost there. If all you want to do is extract and write to a file, just use a some sort of writer object from csv, and use the reader-writer combination, while you iterate directly over the reader object:

import csv
with open('report_export.csv', newline='') as infile,\
            open('report_out.csv', 'w', newline='') as outfile:

    reader = csv.DictReader(infile)
    fieldnames = ["Text_content"]
    writer = csv.DicWriter(outfile, fieldnames=fieldnames)

    for row in reader: # iterates over the csv row-by-row
        data = row["Text_Content"].split(sep='. ')[1]
        writer.writerow({"Text_content": data})

The above assumes Python 3.


问:
wait() and notify() with linelistener
As this is my first post, I apologize for any mistakes it's structure. I am working on a personal project which is a clock made in java to display time/date/day and play an audio file every quarter as well as chime the hour. The GUI is built with swing items.

When the clock reaches a time it must chime/play sound, I want an icon to be displayed on the GUI while the clock is sounding. At the moment, the icon is toggled at the beginning of the method and toggled again at the end. I would like the method to wait() while the audio is being played before toggling the icon the second time. (Perhaps there is a better way to do this.)

I am inexperienced with java and I feel as if I'm using wait() and notify() incorrectly. I've looked at examples until my head spun, and I'm still unsure of the solution I need.

I'm using LineListener to notify that the Clip has finished playing and can proceed to toggle the icon.

This is the methods for playing the chimes:

```<code>public void play() throws InterruptedException {
    while (mute == false) {
        synchronized (chimeLock) {
            ChimeClockGUI.toggleChimeIcon();
            clip.start();
            do {
                chimeLock.wait();
            } while (clip.isActive());
            ChimeClockGUI.toggleChimeIcon();
        }
    }
}
</code>```

And

```<code>public void toll() throws InterruptedException {
    if (mute == false) {
        synchronized (chimeLock) {
            ChimeClockGUI.toggleChimeIcon();
            ONTHEHOUR.clip.start();
            do {
                chimeLock.wait();
            } while (ONTHEHOUR.clip.isActive());
            if (Calendar.getInstance().get(Calendar.HOUR) == 0) {
                clip.loop(11);
                do {
                    chimeLock.wait();
                } while (clip.isActive());
            } else if (Calendar.getInstance().get(Calendar.HOUR) == 1) {
                clip.start();
                do {
                    chimeLock.wait();
                } while (clip.isActive());
            } else {
                clip.loop(Calendar.getInstance().get(Calendar.HOUR) - 1);
                do {
                    chimeLock.wait();
                } while (clip.isActive());
            }
            ChimeClockGUI.toggleChimeIcon();
        }
    }
}
</code>```

And finally, the update() method from LineListener:

```<code>@Override
public void update(LineEvent le) {
    synchronized (chimeLock) {
        LineEvent.Type type = le.getType();
        if (type == LineEvent.Type.OPEN) {
            System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                    .format(new Timestamp(System.currentTimeMillis())) + ": Clip opened...");
        } else if (type == LineEvent.Type.CLOSE) {
            System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                    .format(new Timestamp(System.currentTimeMillis())) + ": Clip closed...");
            chimeLock.notifyAll();
        } else if (type == LineEvent.Type.START) {
            System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                    .format(new Timestamp(System.currentTimeMillis())) + ": Clip started...");
        } else if (type == LineEvent.Type.STOP) {
            System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                    .format(new Timestamp(System.currentTimeMillis())) + ": Clip stopped...");
            chimeLock.notifyAll();
        }
    }
}
</code>```

I'm including the method that calls play() and toll().

```<code>chimeService.scheduleAtFixedRate(() -> {
    try {
        ChimeClockSound.TOLL.toll();
        } catch (InterruptedException ex) {
            Logger.getLogger(ChimeClockGUI.class.getName())
                  .log(Level.SEVERE, null, ex);
    }
</code>```

Any help or suggestions would be very appreciated. Thank you!

答:
The first issue here is that clip.isRunning() returns false until the data actually begins loading, so you should make sure wait() is always called with a do-while loop:

```<code>do {
    wait();
} while (clip.isRunning());
</code>```

It's also better to call wait/notify on a dedicated lock object rather than on the entire instance of the class. This way, other uses of wait/notify don't interfere and your methods don't need to be synchronized, which can slow things down.

```<code>private final Object chimeLock = new Object();
</code>```

With this, you can replace wait() with:

```<code>synchronized (chimeLock) {
    chimeLock.wait();
}
</code>```

...and replace notifyAll() with:

```<code>synchronized (chimeLock) {
    chimeLock.notifyAll();
}
</code>```

...and remove synchronized from your method declarations, since it's now synchronizing on the lock object instead.

The last thing to keep in mind is that InterruptedException is only thrown if the thread is interrupted, which usually means something (like the JVM) wants the thread to stop running. When you catch this, you should wrap things up and return ASAP.

Title: wait() and notify() with linelistener

Body:
As this is my first post, I apologize for any mistakes it's structure. I am working on a personal project which is a clock made in java to display time/date/day and play an audio file every quarter as well as chime the hour. The GUI is built with swing items.

When the clock reaches a time it must chime/play sound, I want an icon to be displayed on the GUI while the clock is sounding. At the moment, the icon is toggled at the beginning of the method and toggled again at the end. I would like the method to wait() while the audio is being played before toggling the icon the second time. (Perhaps there is a better way to do this.)

I am inexperienced with java and I feel as if I'm using wait() and notify() incorrectly. I've looked at examples until my head spun, and I'm still unsure of the solution I need.

I'm using LineListener to notify that the Clip has finished playing and can proceed to toggle the icon.

This is the methods for playing the chimes:

public void play() throws InterruptedException {
    while (mute == false) {
        synchronized (chimeLock) {
            ChimeClockGUI.toggleChimeIcon();
            clip.start();
            do {
                chimeLock.wait();
            } while (clip.isActive());
            ChimeClockGUI.toggleChimeIcon();
        }
    }
}

And

public void toll() throws InterruptedException {
    if (mute == false) {
        synchronized (chimeLock) {
            ChimeClockGUI.toggleChimeIcon();
            ONTHEHOUR.clip.start();
            do {
                chimeLock.wait();
            } while (ONTHEHOUR.clip.isActive());
            if (Calendar.getInstance().get(Calendar.HOUR) == 0) {
                clip.loop(11);
                do {
                    chimeLock.wait();
                } while (clip.isActive());
            } else if (Calendar.getInstance().get(Calendar.HOUR) == 1) {
                clip.start();
                do {
                    chimeLock.wait();
                } while (clip.isActive());
            } else {
                clip.loop(Calendar.getInstance().get(Calendar.HOUR) - 1);
                do {
                    chimeLock.wait();
                } while (clip.isActive());
            }
            ChimeClockGUI.toggleChimeIcon();
        }
    }
}

And finally, the update() method from LineListener:

@Override
public void update(LineEvent le) {
    synchronized (chimeLock) {
        LineEvent.Type type = le.getType();
        if (type == LineEvent.Type.OPEN) {
            System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                    .format(new Timestamp(System.currentTimeMillis())) + ": Clip opened...");
        } else if (type == LineEvent.Type.CLOSE) {
            System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                    .format(new Timestamp(System.currentTimeMillis())) + ": Clip closed...");
            chimeLock.notifyAll();
        } else if (type == LineEvent.Type.START) {
            System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                    .format(new Timestamp(System.currentTimeMillis())) + ": Clip started...");
        } else if (type == LineEvent.Type.STOP) {
            System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                    .format(new Timestamp(System.currentTimeMillis())) + ": Clip stopped...");
            chimeLock.notifyAll();
        }
    }
}

I'm including the method that calls play() and toll().

chimeService.scheduleAtFixedRate(() -> {
    try {
        ChimeClockSound.TOLL.toll();
        } catch (InterruptedException ex) {
            Logger.getLogger(ChimeClockGUI.class.getName())
                  .log(Level.SEVERE, null, ex);
    }

Any help or suggestions would be very appreciated. Thank you!

Body:
The first issue here is that clip.isRunning() returns false until the data actually begins loading, so you should make sure wait() is always called with a do-while loop:

do {
    wait();
} while (clip.isRunning());

It's also better to call wait/notify on a dedicated lock object rather than on the entire instance of the class. This way, other uses of wait/notify don't interfere and your methods don't need to be synchronized, which can slow things down.

private final Object chimeLock = new Object();

With this, you can replace wait() with:

synchronized (chimeLock) {
    chimeLock.wait();
}

...and replace notifyAll() with:

synchronized (chimeLock) {
    chimeLock.notifyAll();
}

...and remove synchronized from your method declarations, since it's now synchronizing on the lock object instead.

The last thing to keep in mind is that InterruptedException is only thrown if the thread is interrupted, which usually means something (like the JVM) wants the thread to stop running. When you catch this, you should wrap things up and return ASAP.


问:
How do I split a list into equally-sized chunks?
How do I split a list of arbitrary length into equal sized chunks?

See How to iterate over a list in chunks if the data result will be used directly for a loop, and does not need to be stored.

For the same question with a string input, see Split string every nth character?. The same techniques generally apply, though there are some variations.

答:
This works in v2/v3, is inlineable, generator-based and uses only the standard library:</p>

```<code>import itertools
def split_groups(iter_in, group_size):
    return ((x for _, x in item) for _, item in itertools.groupby(enumerate(iter_in), key=lambda x: x[0] // group_size))
</code>```

OwnerDisplayName: jespern

Body:
This works in v2/v3, is inlineable, generator-based and uses only the standard library:

import itertools
def split_groups(iter_in, group_size):
    return ((x for _, x in item) for _, item in itertools.groupby(enumerate(iter_in), key=lambda x: x[0] // group_size))


问:
Can I get access to user information in Cognito, without a user token?
I'm using a third party software that allows users to send text messages (twilio/plivo), and I wanted to verify that the phone number is valid and belongs to a user before posting it to DynamoDB.

I have an EC2 instance set up running on nodejs that gets incoming text messages with phone number, so how would I go about cross checking this phone number with cognito, since it's done over a mobile text message and not through a website?

I'm using API gateway on our website to check if a user is indeed who they say they are, but since it needs a token to validate a user I don't think this is possible via text messages?

EDIT: It seems I can see all of my users, if they're verified, and their phone numbers. Surely there's some bash script I can write and invoke, that has administrator IAM rights with cognito to get this same access? Or maybe even do this using nodeJS?

答:
Ah, I figured it out! on my EC2 instance that's private:</p>

```<code>//aws cognito access
const aws = require('aws-sdk');
aws.config.update({
    accessKeyId: 'key',
    secretAccessKey: 'accessKey'
});
const CognitoIdentityServiceProvider = aws.CognitoIdentityServiceProvider;
const client = new CognitoIdentityServiceProvider({ apiVersion: '2016-04-18', region: 'REGION'});

//cognito request things

var params = {
    UserPoolId: 'UserPoolID', /* required */
    AttributesToGet: [
        'sub',
        'email_verified'
        /* more items */
    ],
    Filter: 'phone_number="+someNumber"',
    Limit: 10
};

client.listUsers(params, function(err, data) {
    if (err) {
        console.log(err, err.stack);
    } else {
        console.log(data);
        console.log(data["Users"][0]["Attributes"]);
    }
});
</code>```

Body:
Ah, I figured it out! on my EC2 instance that's private:

//aws cognito access
const aws = require('aws-sdk');
aws.config.update({
    accessKeyId: 'key',
    secretAccessKey: 'accessKey'
});
const CognitoIdentityServiceProvider = aws.CognitoIdentityServiceProvider;
const client = new CognitoIdentityServiceProvider({ apiVersion: '2016-04-18', region: 'REGION'});

//cognito request things

var params = {
    UserPoolId: 'UserPoolID', /* required */
    AttributesToGet: [
        'sub',
        'email_verified'
        /* more items */
    ],
    Filter: 'phone_number="+someNumber"',
    Limit: 10
};

client.listUsers(params, function(err, data) {
    if (err) {
        console.log(err, err.stack);
    } else {
        console.log(data);
        console.log(data["Users"][0]["Attributes"]);
    }
});


问:
Removing characters from strings with LINQ
I'm trying to brush up on my LINQ by writing some simple extension methods. Is there any better way to write such a function as below that removes a given list of characters from a string (using LINQ)?

It helps me to think of the extension methods that LINQ relies on first:

```<code>public static string Remove(this string s, IEnumerable<char> chars)
{
    string removeChars = string.Concat(chars);

    return new string(s.ToCharArray().Where(c => !removeChars.Contains(c)).ToArray());
}
</code>```

But that's pretty ugly. Ergo LINQ.

The difference that I notice in the LINQ statement is that I have to use 'select' whereas with the extension method, I don't have to.

```<code>/// <summary>Strip characters out of a string.</summary>
/// <param name="chars">The characters to remove.</param>
public static string Remove(this string s, IEnumerable<char> chars)
{
    string removeChars = string.Concat(chars);

    var stripped = from c in s.ToCharArray()
                   where !removeChars.Contains(c)
                   select c;

    return new string(stripped.ToArray());
}
</code>```

So I'm wondering if this (last snippet above) is the tersest LINQ statement to accomplish removal of characters.

答:
I would prefer the first form with extension methods though simplified to

```<code>public static string Remove(this string s, IEnumerable<char> chars)
{
    return new string(s.Where(c => !chars.Contains(c)).ToArray());
}
</code>```

As for select keyword, it's obligatory in second form. The documentation says what "A query expression must terminate with either a select clause or a group clause". That's why I would avoid LINQ syntactic sugar.

OwnerDisplayName: Chris

ContentLicense: CC BY-SA 2.5

Body:
I'm trying to brush up on my LINQ by writing some simple extension methods. Is there any better way to write such a function as below that removes a given list of characters from a string (using LINQ)?

It helps me to think of the extension methods that LINQ relies on first:

public static string Remove(this string s, IEnumerable<char> chars)
{
    string removeChars = string.Concat(chars);

    return new string(s.ToCharArray().Where(c => !removeChars.Contains(c)).ToArray());
}

But that's pretty ugly. Ergo LINQ.

The difference that I notice in the LINQ statement is that I have to use 'select' whereas with the extension method, I don't have to.

/// Strip characters out of a string.
/// <param name="chars">The characters to remove.</param>
public static string Remove(this string s, IEnumerable<char> chars)
{
    string removeChars = string.Concat(chars);

    var stripped = from c in s.ToCharArray()
                   where !removeChars.Contains(c)
                   select c;

    return new string(stripped.ToArray());
}

So I'm wondering if this (last snippet above) is the tersest LINQ statement to accomplish removal of characters.

LastEditorDisplayName: Chris

Body:
I would prefer the first form with extension methods though simplified to

public static string Remove(this string s, IEnumerable<char> chars)
{
    return new string(s.Where(c => !chars.Contains(c)).ToArray());
}

As for select keyword, it's obligatory in second form. The documentation says what "A query expression must terminate with either a select clause or a group clause". That's why I would avoid LINQ syntactic sugar.

OwnerDisplayName: Alexander Prokofyev

LastEditorDisplayName: Alexander Prokofyev


问:
What is the best way to arrange variables and methods in a class?
I usually arrange my variables and methods like this:

```<code>class MyClass {
    // public variables
    // public methods
    // private methods
    // private variables
}
</code>```

I want to know what is the best arrangement for defining functions and variables in terms of readability?

EDIT: Also what about enum and class definitions within the class?

答:
Like this:

```<code>class MyClass {
    // private fields
    // public methods
    // private methods
}
</code>```

You shouldn't have any public fields. Use properties instead.

StyleCop has a set of rules on suggested code order. See SA1201:

Within a class, struct, or interface, elements must be positioned in the following order:

Fields

Constructors

Finalizers (Destructors)

Delegates

Events

Enums

Interfaces

Properties

Indexers

Methods

Structs

Classes

and SA1202:

To comply with this rule, adjacent elements of the same type must be positioned in the following order by access level:

public

internal

protected internal

protected

private

If you use StyleCop it will warn you when you break these rules.

Body:
I usually arrange my variables and methods like this:

class MyClass {
    // public variables
    // public methods
    // private methods
    // private variables
}

I want to know what is the best arrangement for defining functions and variables in terms of readability?

EDIT: Also what about enum and class definitions within the class?

Body:
Like this:

class MyClass {
    // private fields
    // public methods
    // private methods
}

You shouldn't have any public fields. Use properties instead.

StyleCop has a set of rules on suggested code order. See SA1201:

Within a class, struct, or interface, elements must be positioned in the following order:

Fields

Constructors

Finalizers (Destructors)

Delegates

Events

Enums

Interfaces

Properties

Indexers

Methods

Structs

Classes

and SA1202:

To comply with this rule, adjacent elements of the same type must be positioned in the following order by access level:

public

internal

protected internal

protected

private

If you use StyleCop it will warn you when you break these rules.


问:
How do CMake placeholders work?
Inside the support libraries for CMake, you'll see command definitions that look like this:

```<code> set(CMAKE_ASM${ASM_DIALECT}_COMPILE_OBJECT "<CMAKE_ASM${ASM_DIALECT}_COMPILER> <DEFINES> <INCLUDES> <FLAGS> -o <OBJECT> -c <SOURCE>")
</code>```

(from /usr/share/cmake-3.5/Modules/CMakeASMInformation.cmake in my install for example)

The objects of interest here to me are the placeholders (phrase obtained here): <DEFINES> <INCLUDES> <FLAGS>; I'm trying to figure out how they interact with the rest of CMake.

Given that I was able to pass flags to my assembler by setting CMAKE_ASM-ATT_FLAGS, I had sort of thought that there might be some magic mapping a placeholder to a variable name... but setting CMAKE_ASM-ATT_INCLUDES didn't seem to do anything.

So how would <INCLUDES> be populated here? Can it be filled in a target-specific manner?

答:
There is a little magic in origin of placeholders:

<DEFINES> is filled by add_definitions, target_compile_definitions commands and corresponded properties,

<INCLUDES> is filled by include_directories, target_include_directories and so,

<FLAGS> is filled by add_compile_options, target_compile_options and so plus CMAKE_<LANG>_FLAGS variable and its configuration-specific pair,

<SOURCE> are source files and

<OBJECT> are corresponded objects.

That is, everything which affects building of C/C++ programs affects (after being language-adapted) on library/executables on other languages.

Title: How do CMake placeholders work?

Body:
Inside the support libraries for CMake, you'll see command definitions that look like this:

 set(CMAKE_ASM${ASM_DIALECT}_COMPILE_OBJECT "<CMAKE_ASM${ASM_DIALECT}_COMPILER> <DEFINES> <INCLUDES> <FLAGS> -o <OBJECT> -c <SOURCE>")

(from /usr/share/cmake-3.5/Modules/CMakeASMInformation.cmake in my install for example)

The objects of interest here to me are the placeholders (phrase obtained here): <DEFINES> <INCLUDES> <FLAGS>; I'm trying to figure out how they interact with the rest of CMake.

Given that I was able to pass flags to my assembler by setting CMAKE_ASM-ATT_FLAGS, I had sort of thought that there might be some magic mapping a placeholder to a variable name... but setting CMAKE_ASM-ATT_INCLUDES didn't seem to do anything.

So how would <INCLUDES> be populated here? Can it be filled in a target-specific manner?


问:
1004 Error when Exporting Excel to PDF
I am currently trying to save an excel document as both an .xls and a PDF with one button. It worked for a bit, but now it gives me an error message and highlights the line begining with ActiveSheet.ExportAsFixedFormat. If I take it out it then highlights the MsgBox line to show an problem. I am wondering if this is caused by the fact that there are 3 commands in this function?

```<code>Sub Rectangle1_Click()
Dim Path As String
Dim FileName1 As String
Dim FileName2 As String
Path1 = "FILE_PATH_IS_HERE"
Path2 = "FILE_PATH_IS_HERE"
FileName1 = Range("J7")
FileName2 = Range("G3")
ActiveWorkbook.SaveAs Filename:=Path1 & FileName1 & FileName2 & ".xls", FileFormat:=xlNormal

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Path2 & FileName1 & FileName2, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

MsgBox "Invoice Saved As Excel Doc and PDF.", , "I Systems"
</code>```

Thanks for all your help in advance!

答:
I had a similiar problem and the answers above didn't work for me. After an hour of pulling my hair out, here was the source of my problem.

I was passing a cell value as part of generating the filename. I was doing this in the format of fileName:= ActiveWorkbook.Path & "\" & CStr(Workbooks.Cells(i,j).Value) & ".pdf". The text in the cell itself was formatted to be in two rows (i.e. "top row text" + (Alt+K) + "bottom row text"). While the string looks normal in Debug.print, MsgBox, or value previews, I am thinking that there is a hidden character which encodes the new line for the cell. I believe this hidden character causes the error when passed as part of the fileName argument. I'm guessing Excel doesn't pick it up but the OS's file name system does.

In any case, this fixed the issue for me. Best of luck, this was a frustrating one to figure out!

Title: 1004 Error when Exporting Excel to PDF

Body:
I am currently trying to save an excel document as both an .xls and a PDF with one button. It worked for a bit, but now it gives me an error message and highlights the line begining with ActiveSheet.ExportAsFixedFormat. If I take it out it then highlights the MsgBox line to show an problem. I am wondering if this is caused by the fact that there are 3 commands in this function?

Sub Rectangle1_Click()
Dim Path As String
Dim FileName1 As String
Dim FileName2 As String
Path1 = "FILE_PATH_IS_HERE"
Path2 = "FILE_PATH_IS_HERE"
FileName1 = Range("J7")
FileName2 = Range("G3")
ActiveWorkbook.SaveAs Filename:=Path1 & FileName1 & FileName2 & ".xls", FileFormat:=xlNormal

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Path2 & FileName1 & FileName2, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

MsgBox "Invoice Saved As Excel Doc and PDF.", , "I Systems"

Thanks for all your help in advance!


Title: What is wrong with my translator? (Python)

Body:
I made a translator using python here is the code:

def answer(plaintext):
    words = {"a":100000,"b":110000,"c":100100,"d":100110,"e":100010,"f":110100,"g":110110,"h":110010,"i":010100"j":010110,"k":101000,"l":111000,"m":101100,"n":101110,"o"101010:,"p":111100,"q":111110,"r":111010,"s":011100,"t":011110,"u":101001,"v":111001,"w":010111,"x":010111,"y":101011,"z":101011}
input = "j, o, s, e"
inputList = input.split(',')
for word in inputList:
    print words[word]

The syntax error is on line 2.

Body:
I assume you want something like:

def answer(plaintext):
    words = {"a":'100000',"b":'110000',"c":'100100',"d":'100110',"e":'100010',"f":'110100',"g":'110110',"h":'110010',"i":'010100',"j":'010110',"k":'101000',"l":'111000',"m":'101100',"n":'101110',"o":'101010',"p":'111100',"q":'111110',"r":'111010',"s":'011100',"t":'011110',"u":'101001',"v":'111001',"w":'010111',"x":'010111',"y":'101011',"z":'101011'}
    inputList = plaintext.split(',')
    for word in inputList:
        print words[word]

text = "j,o,s,e"
answer(text)

You had a bunch of typos in your dict, pay attention to the traceback, it tells you exactly what's wrong. You also never actually called the function you defined. You probably want your dict values to be strings as well.


问:
How to view full dependency tree for nested Go dependencies
I'm trying to debug the following build error in our CI where "A depends on B which can't build because it depends on C." I'm building my data service which doesn't directly depend on kafkaAvailMonitor.go which makes this error hard to trace. In other words:

data (what I'm building) depends on (?) which depends on
kafkaAvailMonitor.go

It may seem trivial to fix for a developer they just do "go get whatever" but I can't do that as part of the release process - I have to find the person that added the dependency and ask them to fix it.

I'm aware that there are tools to visualize the dependency tree and other more sophisticated build systems, but this seems like a pretty basic issue: is there any way I can view the full dependency tree to see what's causing the build issue?

```<code>go build -a -v

../../../msgq/kafkaAvailMonitor.go:8:2: cannot find package
  "github.com/Shopify/sarama/tz/breaker" in any of:
  /usr/lib/go-1.6/src/github.com/Shopify/sarama/tz/breaker (from $GOROOT)
  /home/jenkins/go/src/github.com/Shopify/sarama/tz/breaker (from $GOPATH)
  /home/jenkins/vendor-library/src/github.com/Shopify/sarama/tz/breaker
  /home/jenkins/go/src/github.com/Shopify/sarama/tz/breaker
  /home/jenkins/vendor-library/src/github.com/Shopify/sarama/tz/breaker
</code>```

答:
The above answer still doesn't show me a dependency tree so I've taken the time to write a Python script to do what I need - hopefully that helps other people.

The issue with the above solution (the others proposed like go list) is that it only tells me the top level. They don't "traverse the tree." This is the output I get - which doesn't help any more than what go build gives me.

```<code>.../npd/auth/
   .../mon/mlog
   .../auth/service
</code>```

This is what I'm trying to get - I know that auth is broken (top) and that breaker is broken (bottom) from go build but I have no idea what's in between - my script below gives me this output.

```<code>.../npd/auth/
    .../npd/auth/service
        .../npd/auth/resource
            .../npd/auth/storage
               .../npd/middleware
                  .../npd/metrics/persist
                    .../npd/kafka
                        .../vendor-library/src/github.com/Shopify/sarama
                            .../vendor-library/src/github.com/Shopify/sarama/vz/breaker
</code>```

My Python script:

import subprocess
import os

folder_locations=['.../go/src','.../vendor-library/src']

def getImports(_cwd):
    #When the commands were combined they overflowed the bugger and I couldn't find a workaround
    cmd1 = ["go", "list", "-f", " {{.ImportPath}}","./..."]
    cmd2 = ["go", "list", "-f", " {{.Imports}}","./..."]

    process = subprocess.Popen(' '.join(cmd1), cwd=_cwd,shell=True,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE)

    out1, err = process.communicate()

    process = subprocess.Popen(' '.join(cmd2), cwd=_cwd,shell=True,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE)

    out2, err = process.communicate()
    out2clean=str(out2).replace("b'",'').replace('[','').replace(']','').replace("'",'')

    return str(out1).split('\\n'),out2clean.split('\\n')

def getFullPath(rel_path):
    for i in folder_locations:
        if os.path.exists(i+'/'+rel_path):
            return i+'/'+rel_path
    return None

def getNextImports(start,depth):

    depth=depth+1
    indent = '\t'*(depth+1)

    for i,val in enumerate(start.keys()):

        if depth==1:
            print (val)

        out1,out2=getImports(val)

        noDeps=True
        for j in out2[i].split(' '):
            noDeps=False

            _cwd2=getFullPath(j)
            new_tree = {_cwd2:[]}
            not_exists = (not _cwd2 in alltmp)

            if not_exists:
                print(indent+_cwd2)
                start[val].append(new_tree)
                getNextImports(new_tree,depth)
                alltmp.append(_cwd2)

        if noDeps:
            print(indent+'No deps')

_cwd = '/Users/.../npd/auth'

alltmp=[]
start_root={_cwd:[]}
getNextImports(start_root,0)
```

Body:
I'm trying to debug the following build error in our CI where "A depends on B which can't build because it depends on C." I'm building my data service which doesn't directly depend on kafkaAvailMonitor.go which makes this error hard to trace. In other words:

data (what I'm building) depends on (?) which depends on
kafkaAvailMonitor.go

It may seem trivial to fix for a developer they just do "go get whatever" but I can't do that as part of the release process - I have to find the person that added the dependency and ask them to fix it.

I'm aware that there are tools to visualize the dependency tree and other more sophisticated build systems, but this seems like a pretty basic issue: is there any way I can view the full dependency tree to see what's causing the build issue?

go build -a -v

../../../msgq/kafkaAvailMonitor.go:8:2: cannot find package
  "github.com/Shopify/sarama/tz/breaker" in any of:
  /usr/lib/go-1.6/src/github.com/Shopify/sarama/tz/breaker (from $GOROOT)
  /home/jenkins/go/src/github.com/Shopify/sarama/tz/breaker (from $GOPATH)
  /home/jenkins/vendor-library/src/github.com/Shopify/sarama/tz/breaker
  /home/jenkins/go/src/github.com/Shopify/sarama/tz/breaker
  /home/jenkins/vendor-library/src/github.com/Shopify/sarama/tz/breaker

Body:
The above answer still doesn't show me a dependency tree so I've taken the time to write a Python script to do what I need - hopefully that helps other people.

The issue with the above solution (the others proposed like go list) is that it only tells me the top level. They don't "traverse the tree." This is the output I get - which doesn't help any more than what go build gives me.

.../npd/auth/
   .../mon/mlog
   .../auth/service

This is what I'm trying to get - I know that auth is broken (top) and that breaker is broken (bottom) from go build but I have no idea what's in between - my script below gives me this output.

.../npd/auth/
    .../npd/auth/service
        .../npd/auth/resource
            .../npd/auth/storage
               .../npd/middleware
                  .../npd/metrics/persist
                    .../npd/kafka
                        .../vendor-library/src/github.com/Shopify/sarama
                            .../vendor-library/src/github.com/Shopify/sarama/vz/breaker

My Python script:

import subprocess
import os

folder_locations=['.../go/src','.../vendor-library/src']

def getImports(_cwd):
    #When the commands were combined they overflowed the bugger and I couldn't find a workaround
    cmd1 = ["go", "list", "-f", " {{.ImportPath}}","./..."]
    cmd2 = ["go", "list", "-f", " {{.Imports}}","./..."]

    process = subprocess.Popen(' '.join(cmd1), cwd=_cwd,shell=True,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE)

    out1, err = process.communicate()

    process = subprocess.Popen(' '.join(cmd2), cwd=_cwd,shell=True,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE)

    out2, err = process.communicate()
    out2clean=str(out2).replace("b'",'').replace('[','').replace(']','').replace("'",'')

    return str(out1).split('\\n'),out2clean.split('\\n')

def getFullPath(rel_path):
    for i in folder_locations:
        if os.path.exists(i+'/'+rel_path):
            return i+'/'+rel_path
    return None

def getNextImports(start,depth):

    depth=depth+1
    indent = '\t'*(depth+1)

    for i,val in enumerate(start.keys()):

        if depth==1:
            print (val)

        out1,out2=getImports(val)

        noDeps=True
        for j in out2[i].split(' '):
            noDeps=False

            _cwd2=getFullPath(j)
            new_tree = {_cwd2:[]}
            not_exists = (not _cwd2 in alltmp)

            if not_exists:
                print(indent+_cwd2)
                start[val].append(new_tree)
                getNextImports(new_tree,depth)
                alltmp.append(_cwd2)

        if noDeps:
            print(indent+'No deps')

_cwd = '/Users/.../npd/auth'

alltmp=[]
start_root={_cwd:[]}
getNextImports(start_root,0)