问:
array with unique values over all documents of one collection
Situation:
I have several documents of the same collection(account), each has an attribute of type array(string) named uniquestrings.

Problem: Each entry in uniquestrings must be unique over all documents in mongodb. It seems that MongoDB/Mongoose does not offer such a validation (neither addToSet¹ nor index: {unique: true}² solve the problem). Is there a pattern to restructure my document schema to make sure mongodb itself can validate it? At the moment the software itself checks it before updating the document.

E.g.

account {
  _id: 111,
  uniquestrings: ["a", "b", "c"]
}

account {
  _id: 222,
  uniquestrings: ["d", "e", "f"]
}
```

<p>E.g. prevent <code>account(222).uniquestrings.push("a");</code> by throwing a duplicate error from mongo.</p>

<p>¹ Uniqueness in an array is not enough<br>
² Each item in array has to be unique across the collection</p>

<p><strong>UPDATE1:</strong></p>

<p>More examples. Affected Schema entry looks like:</p>

```var Account = new Schema({
    ...
    uniquestrings: [{type: String, index: {unique: true}}]
    ...
});
```

<p>Now when create 4 account documents. I want only 1 and 2 be ok, and rest should fail.</p>

```var accountModel1 = new Account.Model({uniquestrings: ["a", "b"]});
accountModel1.save(); // OK
var accountModel2 = new Account.Model({uniquestrings: ["c", "d"]});
accountModel2.save(); // OK
var accountModel3 = new Account.Model({uniquestrings: ["c", "d"]});
accountModel3.save(); // FAIL => Not unique, so far so good
var accountModel4 = new Account.Model({uniquestrings: ["X", "d"]});
accountModel4.save(); // OK => But i Want this to faile because "d" is alreay in use.
```

答:
It might be possible, if you are willing to store the unique values in a different collection. It would be structured like this:

{ "uniquestring" : "a", "account" : 111 }
{ "uniquestring" : "b", "account" : 111 }
{ "uniquestring" : "c", "account" : 111 }
{ "uniquestring" : "d", "account" : 222 }
{ "uniquestring" : "e", "account" : 222 }
{ "uniquestring" : "f", "account" : 222 }
```

<p>I am not an expert with Mongoose, but I believe that you can define Models to link collections together, with the account field here referencing the accounts collection's _id field.</p>

<p>Now, you can enforce the uniqueness with a straightforward index:</p>

<pre class="lang-js prettyprint-override"><code>db.uniquestrings.createIndex( { "uniquestring" : 1 } , { unique : true } )
</code>```

Now, your app will have a little more work to do when saving the data (it needs to save to the uniquestrings collection as well as the accounts collection), but you do now have database-level enforcement of the uniqueness of these strings, across the database.

PS edits are welcome from anybody with more detailed knowledge of how to implement and use such models in mongoose.

Body:
Situation:
I have several documents of the same collection(account), each has an attribute of type array(string) named uniquestrings.

Problem: Each entry in uniquestrings must be unique over all documents in mongodb. It seems that MongoDB/Mongoose does not offer such a validation (neither addToSet¹ nor index: {unique: true}² solve the problem). Is there a pattern to restructure my document schema to make sure mongodb itself can validate it? At the moment the software itself checks it before updating the document.

E.g.

account {
  _id: 111,
  uniquestrings: ["a", "b", "c"]
}

account {
  _id: 222,
  uniquestrings: ["d", "e", "f"]
}

E.g. prevent account(222).uniquestrings.push("a"); by throwing a duplicate error from mongo.

¹ Uniqueness in an array is not enough

² Each item in array has to be unique across the collection

UPDATE1:

More examples. Affected Schema entry looks like:

var Account = new Schema({
    ...
    uniquestrings: [{type: String, index: {unique: true}}]
    ...
});

Now when create 4 account documents. I want only 1 and 2 be ok, and rest should fail.

var accountModel1 = new Account.Model({uniquestrings: ["a", "b"]});
accountModel1.save(); // OK
var accountModel2 = new Account.Model({uniquestrings: ["c", "d"]});
accountModel2.save(); // OK
var accountModel3 = new Account.Model({uniquestrings: ["c", "d"]});
accountModel3.save(); // FAIL => Not unique, so far so good
var accountModel4 = new Account.Model({uniquestrings: ["X", "d"]});
accountModel4.save(); // OK => But i Want this to faile because "d" is alreay in use.

Body:
It might be possible, if you are willing to store the unique values in a different collection. It would be structured like this:

{ "uniquestring" : "a", "account" : 111 }
{ "uniquestring" : "b", "account" : 111 }
{ "uniquestring" : "c", "account" : 111 }
{ "uniquestring" : "d", "account" : 222 }
{ "uniquestring" : "e", "account" : 222 }
{ "uniquestring" : "f", "account" : 222 }

I am not an expert with Mongoose, but I believe that you can define Models to link collections together, with the account field here referencing the accounts collection's _id field.

Now, you can enforce the uniqueness with a straightforward index:

db.uniquestrings.createIndex( { "uniquestring" : 1 } , { unique : true } )

Now, your app will have a little more work to do when saving the data (it needs to save to the uniquestrings collection as well as the accounts collection), but you do now have database-level enforcement of the uniqueness of these strings, across the database.

PS edits are welcome from anybody with more detailed knowledge of how to implement and use such models in mongoose.


来源: stackexchange

问:
Signal handling error
When I use my_handler as function there is not error but I can't because I have to use another method there.

If I build code below :
 error: invalid use of member functi
on (did you forget the ‘()’ ?)

This is my first program in C++

```<code>void Client::my_handler(int s){
    if (s == SIGINT){
        printf("SIGINT\n");
    // here I have to use stop_thread method
    }
}

int main (int argc, const char **argv) {
  Client client(argc, argv);
  client.before_start();

  struct sigaction sigIntHandler;

  sigIntHandler.sa_handler = (__sighandler_t) client.my_handler; //error here
  sigemptyset(&sigIntHandler.sa_mask);
  sigIntHandler.sa_flags = 0;
  sigaction(SIGINT, &sigIntHandler, NULL);

  client.run();

  return 0;
}
</code>```

答:
In C++, a member function (a function associated with a class instance, such as client.my_handler here) is not the same as a regular function (something like int main(int arg, const char** argv) here). That's why you can't cast client.my_handler to __sighandler_t.

Client::my_handler has a hidden parameter, a Client*. So really, it's signature (from a certain perspective) looks like void my_handler(Client* this, int s). C++ does some stuff behind the scenes so that you can can reference the variables of this without having to do this-> (most of the time). And when you do something like call

```<code>client.my_handler(SIGINT)
</code>```

C++ is translating that to

```<code>my_handler(&client, SIGINT)
</code>```

Maybe now you can see the issue: __sighandler_t is an alias for a function pointer, but client.my_handler is really two pointers: the function pointer for my_handler and the pointer to client.

A signal handler only accepts a function pointer, not a more general function object (which is a function pointer plus some other data). Signal handling is from C, but function objects are from C++.

The only solution for a signal handler is some sort of global variable.

ContentLicense: CC BY-SA 3.0

Title: Signal handling error

Body:
When I use my_handler as function there is not error but I can't because I have to use another method there.

If I build code below :
 error: invalid use of member functi
on (did you forget the ‘()’ ?)

This is my first program in C++

void Client::my_handler(int s){
    if (s == SIGINT){
        printf("SIGINT\n");
    // here I have to use stop_thread method
    }
}

int main (int argc, const char **argv) {
  Client client(argc, argv);
  client.before_start();

  struct sigaction sigIntHandler;

  sigIntHandler.sa_handler = (__sighandler_t) client.my_handler; //error here
  sigemptyset(&sigIntHandler.sa_mask);
  sigIntHandler.sa_flags = 0;
  sigaction(SIGINT, &sigIntHandler, NULL);

  client.run();

  return 0;
}

Body:
In C++, a member function (a function associated with a class instance, such as client.my_handler here) is not the same as a regular function (something like int main(int arg, const char** argv) here). That's why you can't cast client.my_handler to __sighandler_t.

Client::my_handler has a hidden parameter, a Client*. So really, it's signature (from a certain perspective) looks like void my_handler(Client* this, int s). C++ does some stuff behind the scenes so that you can can reference the variables of this without having to do this-> (most of the time). And when you do something like call

client.my_handler(SIGINT)

C++ is translating that to

my_handler(&client, SIGINT)

Maybe now you can see the issue: __sighandler_t is an alias for a function pointer, but client.my_handler is really two pointers: the function pointer for my_handler and the pointer to client.

A signal handler only accepts a function pointer, not a more general function object (which is a function pointer plus some other data). Signal handling is from C, but function objects are from C++.

The only solution for a signal handler is some sort of global variable.


问:
Putting a dict into a dataframe - python
I have a list called 'gender', of which I counted all the occurrences of the values with Counter:

```<code>gender = ['2',
          'Female,',
          'All Female Group,',
          'All Male Group,',
          'Female,',
          'Couple,',
          'Mixed Group,'....]

gender_count = Counter(gender)
gender_count
Counter({'2': 1,
     'All Female Group,': 222,
     'All Male Group,': 119,
     'Couple,': 256,
     'Female,': 1738,
     'Male,': 2077,
     'Mixed Group,': 212,
     'NA': 16})
</code>```

I want to put this dict into a pandas Dataframe. I have used pd.series(Convert Python dict into a dataframe):

```<code>s = pd.Series(gender_count, name='gender count')
s.index.name = 'gender'
s.reset_index()
</code>```

Which gives me the dataframe I want, but I don't know how to save these steps into a pandas DataFrame.
I also tried using DataFrame.from_dict()

```<code>s2 = pd.DataFrame.from_dict(gender_count, orient='index')
</code>```

But this creates a dataframe with the categories of gender as the index.

I eventually want to use gender categories and the count for a piechart.

答:
what about just </p>

```<code>s = pd.DataFrame(gender_count)
</code>```

Body:
I have a list called 'gender', of which I counted all the occurrences of the values with Counter:

gender = ['2',
          'Female,',
          'All Female Group,',
          'All Male Group,',
          'Female,',
          'Couple,',
          'Mixed Group,'....]

gender_count = Counter(gender)
gender_count
Counter({'2': 1,
     'All Female Group,': 222,
     'All Male Group,': 119,
     'Couple,': 256,
     'Female,': 1738,
     'Male,': 2077,
     'Mixed Group,': 212,
     'NA': 16})

I want to put this dict into a pandas Dataframe. I have used pd.series(Convert Python dict into a dataframe):

s = pd.Series(gender_count, name='gender count')
s.index.name = 'gender'
s.reset_index()

Which gives me the dataframe I want, but I don't know how to save these steps into a pandas DataFrame.
I also tried using DataFrame.from_dict()

s2 = pd.DataFrame.from_dict(gender_count, orient='index')

But this creates a dataframe with the categories of gender as the index.

I eventually want to use gender categories and the count for a piechart.

Body:
what about just

s = pd.DataFrame(gender_count)


问:
Operator Overloading with seq<'T>
In a project I'm experimenting with, I'm constantly multiplying sequence elements.  I wanted to create an operator like this:

```<code>let (.*) (a:seq<'T>) (b:seq<'T>) = Seq.map2 (*) a b
</code>```

However, in FSI it returns:

```<code>val ( .* ) : a:seq<int> -> b:seq<int> -> seq<int>
</code>```

And the following fails:

```<code>  seq [1.0;2.0] .* seq [3.0;4.0];;
  -----^^^

stdin(16,6): error FS0001: This expression was expected to have type
    int
but here has type
    float
</code>```

How can I make the operator work on a generic seq<'T> (provided 'T supports multiplication)?

答:
You need to add the inline keyword to your operator declaration so that the correct overload can be resolved at compile time:

```<code>let inline (.*) (a:seq<'T>) (b:seq<'T>) = Seq.map2 (*) a b
</code>```

```<code>val inline ( .* ) : a:seq< ^T> -> b:seq< ^T> -> seq< ^a> when  ^T :  (static member ( * ) :  ^T *  ^T ->  ^a)
</code>```

Title: Operator Overloading with seq<'T>

Body:
In a project I'm experimenting with, I'm constantly multiplying sequence elements.  I wanted to create an operator like this:

let (.*) (a:seq<'T>) (b:seq<'T>) = Seq.map2 (*) a b

However, in FSI it returns:

val ( .* ) : a:seq<int> -> b:seq<int> -> seq<int>

And the following fails:

  seq [1.0;2.0] .* seq [3.0;4.0];;
  -----^^^

stdin(16,6): error FS0001: This expression was expected to have type
    int
but here has type
    float

How can I make the operator work on a generic seq<'T> (provided 'T supports multiplication)?

Body:
You need to add the inline keyword to your operator declaration so that the correct overload can be resolved at compile time:

let inline (.*) (a:seq<'T>) (b:seq<'T>) = Seq.map2 (*) a b

val inline ( .* ) : a:seq< ^T> -> b:seq< ^T> -> seq< ^a> when  ^T :  (static member ( * ) :  ^T *  ^T ->  ^a)


问:
Big O of recursive functions with multiple function calls?
I'm having trouble determining the worst case time complexity of this recursive function:

```<code>long f(int n) {
  if(n <= 0) return 1;
  else {
    return f(n / 2) + f(n / 2) + f(n / 2);
  }
}
</code>```

I understand that if you were just returning f(n / 2) once, it would be O(log n). So I was wondering if having it three, four, five, etc times would affect the function's big O. Thanks!

答:
Multiplying by a constant never affects the complexity.  Of course, the execution time is roughly tripled, but the issue is how the time increases with respect to the value of n, not what the clock says.

Do note that this applies so easily because you're calling the same function with the same value; this could be reliably replaced by

```<code>return 3 * f(n/2)
</code>```

If you had three different calls, such as

```<code>return f(n/2) + f(int(sqrt(n)) + f(n-1)
</code>```

... then you'd have to compute the complexity of each and consider only the most complex.  That isn't hard with this function; however, with a more complex function, such as the Collatz sequence, you'd have a more difficult time considering all of the 2nd- and 3rd-order complexities.  You would also have more trouble with a call to an indirectly recursive function, such as

```<code>long f(int n);

long g(int n) {
  if(n <= 2) return 1;
  else {
    return f(n*n mod 10) + g(n-2);
  }
}

long f(int n) {
  if(n <= 0) return 1;
  else {
    return f(n / 2) + g(n / 2);
  }
}
</code>```

Bottom line: your question is simple, but beware of follow-up questions.

Body:
I'm having trouble determining the worst case time complexity of this recursive function:

long f(int n) {
  if(n <= 0) return 1;
  else {
    return f(n / 2) + f(n / 2) + f(n / 2);
  }
}

I understand that if you were just returning f(n / 2) once, it would be O(log n). So I was wondering if having it three, four, five, etc times would affect the function's big O. Thanks!

Body:
Multiplying by a constant never affects the complexity.  Of course, the execution time is roughly tripled, but the issue is how the time increases with respect to the value of n, not what the clock says.

Do note that this applies so easily because you're calling the same function with the same value; this could be reliably replaced by

return 3 * f(n/2)

If you had three different calls, such as

return f(n/2) + f(int(sqrt(n)) + f(n-1)

... then you'd have to compute the complexity of each and consider only the most complex.  That isn't hard with this function; however, with a more complex function, such as the Collatz sequence, you'd have a more difficult time considering all of the 2nd- and 3rd-order complexities.  You would also have more trouble with a call to an indirectly recursive function, such as

long f(int n);

long g(int n) {
  if(n <= 2) return 1;
  else {
    return f(n*n mod 10) + g(n-2);
  }
}

long f(int n) {
  if(n <= 0) return 1;
  else {
    return f(n / 2) + g(n / 2);
  }
}

Bottom line: your question is simple, but beware of follow-up questions.


问:
GDB creating a core file
Alright so don't ask why, but I want to create a core file of a test program I made. It's not corrupted and it doesn't screw up, but I want to generate a core file of it. Heres the code:

```<code>#include <stdio.h>
int main(){
printf("TEST");
}
</code>```

As I said its really simple, but I want to be able to dump a core of it. How can I do it?
BTW I'm on Ubuntu 10.04

答: Another option is to use - gcore command.

ContentLicense: CC BY-SA 2.5

Title: GDB creating a core file

Body:
Alright so don't ask why, but I want to create a core file of a test program I made. It's not corrupted and it doesn't screw up, but I want to generate a core file of it. Heres the code:

#include <stdio.h>
int main(){
printf("TEST");
}

As I said its really simple, but I want to be able to dump a core of it. How can I do it?
BTW I'm on Ubuntu 10.04


问:
Chronicle Queue: Usage with less or no lambdas
The documentation shows the usage of an appender or a tailer generally with a lambda, like this:

```<code>appender.writeDocument(wireOut -> wireOut.write("log").marshallable(m ->
      m.write("mkey").text(mkey)
       .write("timestamp").dateTime(now)
       .write("msg").text(data)));
</code>```

For a tailer I I use:

```<code>   int count = 0;
   while (read from tailer ) {
      wire.read("log").marshallable(m -> {
             String mkey = m.read("mkey").text();
            LocalDateTime ts = m.read("timestamp").dateTime();
             String bmsg = m.read("msg").text();
         //... do more stuff, like updating counters
             count++;
       }
   }
</code>```

During the read I would like to do stuff like updating counters, but this is not possible in lambda (needs "effectively final" values/objects).

What is good practice for using the API without lambdas?

Any other ideas on how to do this? (Currently I use AtomicInteger objects)

答:
```<code>static class Log extends AbstractMarshallable {
    String mkey;
    LocalDateTime timestamp;
    String msg;
}

int count;

public void myMethod() {
    Log log = new Log();
    final SingleChronicleQueue q = SingleChronicleQueueBuilder.binary(new File("q4")).build();
    final ExcerptAppender appender = q.acquireAppender();
    final ExcerptTailer tailer = q.createTailer();

    try (final DocumentContext dc = appender.writingDocument()) {
        // this will store the contents of log to the queue
        dc.wire().write("log").marshallable(log);
    }

    try (final DocumentContext dc = tailer.readingDocument()) {
        if (!dc.isData())
             return;
        // this will replace the contents of log
        dc.wire().read("log").marshallable(log);
        //... do more stuff, like updating counters
        count++;
    }
}
</code>```

Body:
The documentation shows the usage of an appender or a tailer generally with a lambda, like this:

appender.writeDocument(wireOut -> wireOut.write("log").marshallable(m ->
      m.write("mkey").text(mkey)
       .write("timestamp").dateTime(now)
       .write("msg").text(data)));

For a tailer I I use:

   int count = 0;
   while (read from tailer ) {
      wire.read("log").marshallable(m -> {
             String mkey = m.read("mkey").text();
            LocalDateTime ts = m.read("timestamp").dateTime();
             String bmsg = m.read("msg").text();
         //... do more stuff, like updating counters
             count++;
       }
   }

During the read I would like to do stuff like updating counters, but this is not possible in lambda (needs "effectively final" values/objects).

What is good practice for using the API without lambdas?

Any other ideas on how to do this? (Currently I use AtomicInteger objects)


问:
Tensorflow TypeError
I'm trying to train a NN using the code in here https://github.com/RobRomijnders/LSTM_tsc.

After lots of changes to fit my data i get this error:

TypeError: Fetch argument None has invalid type

I have search for the answer as i did with previous errors i found but although i get here:
Tensorflow TypeError on session.run arguments/output

i think that my code is not incurring in that (at least i cannot find it)

```<code>import numpy as np
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='3'
import tensorflow as tf
tf.logging.set_verbosity(tf.logging.ERROR)
import matplotlib.pyplot as plt
import sys
sys.path.append('E:\\Descargas\\recommendersystems\\train')
from tsc_model import Model,sample_batch,load_data,check_test

config = {}                             #Put all configuration information into the dict
config['num_layers'] = 3                #number of layers of stacked RNN's
config['hidden_size'] = 120              #memory cells in a layer
config['max_grad_norm'] = 5             #maximum gradient norm during training
config['batch_size'] = batch_size = 30
config['learning_rate'] = .005
config['num_classes'] = 2

max_iterations = 3000
dropout = 0.8
ratio = np.array([0.8,0.9]) #Ratios where to split the training and validation set

direc = 'E:\\Descargas\\recommendersystems\\ets_challenge\\train\\train3.csv'
X_train,X_val,X_test,y_train,y_val,y_test = load_data(direc,ratio)
N,sl = X_train.shape
config['sl'] = sl = X_train.shape[1]
config['num_classes'] = num_classes = len(np.unique(y_train))

# Collect the costs in a numpy fashion
epochs = np.floor(batch_size*max_iterations / N)
print('Train %.0f samples in approximately %d epochs' %(N,epochs))
perf_collect = np.zeros((4,int(np.floor(max_iterations /100))))

#Instantiate a model
model = Model(config)

sess = tf.Session() #Depending on your use, do not forget to close the session
writer = tf.summary.FileWriter("/home/rob/Dropbox/ml_projects/LSTM/log_tb", sess.graph)  #writer for Tensorboard
sess.run(model.init_op)

step = 0
cost_train_ma = -np.log(1/float(num_classes)+1e-9)  #Moving average training cost
acc_train_ma = 0.0
try:
  for i in range(max_iterations):
    X_batch, y_batch = sample_batch(X_train,y_train,batch_size)

    #Next line does the actual training
    cost_train, acc_train,_ = sess.run([model.cost,model.accuracy, model.train_op],feed_dict = {model.input: X_batch,model.labels: y_batch,model.keep_prob:dropout})
    cost_train_ma = cost_train_ma*0.99 + cost_train*0.01
    acc_train_ma = acc_train_ma*0.99 + acc_train*0.01
    if i%100 == 0:
      #Evaluate training performance
      perf_collect[0,step] = cost_train
      perf_collect[1,step] = acc_train

      #Evaluate validation performance
      X_batch, y_batch = sample_batch(X_val,y_val,batch_size)
      cost_val, summ,acc_val = sess.run([model.cost,model.merged,model.accuracy],feed_dict = {model.input: X_batch, model.labels: y_batch, model.keep_prob:1.0})
      perf_collect[1,step] = cost_val
      perf_collect[2,step] = acc_val
      print('At %5.0f/%5.0f: COST %5.3f/%5.3f(%5.3f) -- Acc %5.3f/%5.3f(%5.3f)' %(i,max_iterations,cost_train,cost_val,cost_train_ma,acc_train,acc_val,acc_train_ma))

      #Write information to TensorBoard
      writer.add_summary(summ, i)
      writer.flush()

      step +=1
except KeyboardInterrupt:
  #Pressing ctrl-c will end training. This try-except ensures we still plot the performance
  pass

acc_test,cost_test = check_test(model,sess,X_test,y_test)
epoch = float(i)*batch_size/N
print('After training %.1f epochs, test accuracy is %5.3f and test cost is %5.3f'%(epoch,acc_test,cost_test))

plt.plot(perf_collect[0],label='Train')
plt.plot(perf_collect[1],label = 'Valid')
plt.plot(perf_collect[2],label = 'Valid accuracy')
plt.axis([0, step, 0, np.max(perf_collect)])
plt.legend()
plt.show()
</code>```

And this is the error trace

```<code>TypeError                                 Traceback (most recent call last)
<ipython-input-1-200bd46aa85d> in <module>()
     64       #Evaluate validation performance
     65       X_batch, y_batch = sample_batch(X_val,y_val,batch_size)
---> 66       cost_val, summ,acc_val = sess.run([model.cost,model.merged,model.accuracy],feed_dict = {model.input: X_batch, model.labels: y_batch, model.keep_prob:1.0})
     67       perf_collect[1,step] = cost_val
     68       perf_collect[2,step] = acc_val

\lib\site-packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata)
    765     try:
    766       result = self._run(None, fetches, feed_dict, options_ptr,
--> 767                          run_metadata_ptr)
    768       if run_metadata:
    769         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
    950
    951     # Create a fetch handler to take care of the structure of fetches.
--> 952     fetch_handler = _FetchHandler(self._graph, fetches, feed_dict_string)
    953
    954     # Run request and get response.

\lib\site-packages\tensorflow\python\client\session.py in __init__(self, graph, fetches, feeds)
    406     """
    407     with graph.as_default():
--> 408       self._fetch_mapper = _FetchMapper.for_fetch(fetches)
    409     self._fetches = []
    410     self._targets = []

\lib\site-packages\tensorflow\python\client\session.py in for_fetch(fetch)
    228     elif isinstance(fetch, (list, tuple)):
    229       # NOTE(touts): This is also the code path for namedtuples.
--> 230       return _ListFetchMapper(fetch)
    231     elif isinstance(fetch, dict):
    232       return _DictFetchMapper(fetch)

\lib\site-packages\tensorflow\python\client\session.py in __init__(self, fetches)
    335     """
    336     self._fetch_type = type(fetches)
--> 337     self._mappers = [_FetchMapper.for_fetch(fetch) for fetch in fetches]
    338     self._unique_fetches, self._value_indices = _uniquify_fetches(self._mappers)
    339

\lib\site-packages\tensorflow\python\client\session.py in <listcomp>(.0)
    335     """
    336     self._fetch_type = type(fetches)
--> 337     self._mappers = [_FetchMapper.for_fetch(fetch) for fetch in fetches]
    338     self._unique_fetches, self._value_indices = _uniquify_fetches(self._mappers)
    339

\lib\site-packages\tensorflow\python\client\session.py in for_fetch(fetch)
    225     if fetch is None:
    226       raise TypeError('Fetch argument %r has invalid type %r' %
--> 227                       (fetch, type(fetch)))
    228     elif isinstance(fetch, (list, tuple)):
    229       # NOTE(touts): This is also the code path for namedtuples.

TypeError: Fetch argument None has invalid type <class 'NoneType'>
</code>```

Any help appreciated!

答:
From the error message, it appears that one of model.cost, model.merged, or model.accuracy is None. Looking at the original source, all of these attributes of a Model object seem to be assigned in Model.__init__(): self.cost is assigned here, self.merged is assigned here, and self.accuracy is assigned here.

It is most likely that one of your changes to the code (to make it fit your data) has changed the control flow in that constructor so that one of these assignments no longer happens. I'd recommend first identifying which of the attributes is None, then tracing through Model.__init__() to discover why it is not being set.

Title: Tensorflow TypeError

Body:
I'm trying to train a NN using the code in here https://github.com/RobRomijnders/LSTM_tsc.

After lots of changes to fit my data i get this error:

TypeError: Fetch argument None has invalid type

I have search for the answer as i did with previous errors i found but although i get here:
Tensorflow TypeError on session.run arguments/output

i think that my code is not incurring in that (at least i cannot find it)

import numpy as np
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='3'
import tensorflow as tf
tf.logging.set_verbosity(tf.logging.ERROR)
import matplotlib.pyplot as plt
import sys
sys.path.append('E:\\Descargas\\recommendersystems\\train')
from tsc_model import Model,sample_batch,load_data,check_test

config = {}                             #Put all configuration information into the dict
config['num_layers'] = 3                #number of layers of stacked RNN's
config['hidden_size'] = 120              #memory cells in a layer
config['max_grad_norm'] = 5             #maximum gradient norm during training
config['batch_size'] = batch_size = 30
config['learning_rate'] = .005
config['num_classes'] = 2

max_iterations = 3000
dropout = 0.8
ratio = np.array([0.8,0.9]) #Ratios where to split the training and validation set

direc = 'E:\\Descargas\\recommendersystems\\ets_challenge\\train\\train3.csv'
X_train,X_val,X_test,y_train,y_val,y_test = load_data(direc,ratio)
N,sl = X_train.shape
config['sl'] = sl = X_train.shape[1]
config['num_classes'] = num_classes = len(np.unique(y_train))

# Collect the costs in a numpy fashion
epochs = np.floor(batch_size*max_iterations / N)
print('Train %.0f samples in approximately %d epochs' %(N,epochs))
perf_collect = np.zeros((4,int(np.floor(max_iterations /100))))

#Instantiate a model
model = Model(config)

sess = tf.Session() #Depending on your use, do not forget to close the session
writer = tf.summary.FileWriter("/home/rob/Dropbox/ml_projects/LSTM/log_tb", sess.graph)  #writer for Tensorboard
sess.run(model.init_op)

step = 0
cost_train_ma = -np.log(1/float(num_classes)+1e-9)  #Moving average training cost
acc_train_ma = 0.0
try:
  for i in range(max_iterations):
    X_batch, y_batch = sample_batch(X_train,y_train,batch_size)

    #Next line does the actual training
    cost_train, acc_train,_ = sess.run([model.cost,model.accuracy, model.train_op],feed_dict = {model.input: X_batch,model.labels: y_batch,model.keep_prob:dropout})
    cost_train_ma = cost_train_ma*0.99 + cost_train*0.01
    acc_train_ma = acc_train_ma*0.99 + acc_train*0.01
    if i%100 == 0:
      #Evaluate training performance
      perf_collect[0,step] = cost_train
      perf_collect[1,step] = acc_train

      #Evaluate validation performance
      X_batch, y_batch = sample_batch(X_val,y_val,batch_size)
      cost_val, summ,acc_val = sess.run([model.cost,model.merged,model.accuracy],feed_dict = {model.input: X_batch, model.labels: y_batch, model.keep_prob:1.0})
      perf_collect[1,step] = cost_val
      perf_collect[2,step] = acc_val
      print('At %5.0f/%5.0f: COST %5.3f/%5.3f(%5.3f) -- Acc %5.3f/%5.3f(%5.3f)' %(i,max_iterations,cost_train,cost_val,cost_train_ma,acc_train,acc_val,acc_train_ma))

      #Write information to TensorBoard
      writer.add_summary(summ, i)
      writer.flush()

      step +=1
except KeyboardInterrupt:
  #Pressing ctrl-c will end training. This try-except ensures we still plot the performance
  pass

acc_test,cost_test = check_test(model,sess,X_test,y_test)
epoch = float(i)*batch_size/N
print('After training %.1f epochs, test accuracy is %5.3f and test cost is %5.3f'%(epoch,acc_test,cost_test))

plt.plot(perf_collect[0],label='Train')
plt.plot(perf_collect[1],label = 'Valid')
plt.plot(perf_collect[2],label = 'Valid accuracy')
plt.axis([0, step, 0, np.max(perf_collect)])
plt.legend()
plt.show()

And this is the error trace

TypeError                                 Traceback (most recent call last)
<ipython-input-1-200bd46aa85d> in <module>()
     64       #Evaluate validation performance
     65       X_batch, y_batch = sample_batch(X_val,y_val,batch_size)
---> 66       cost_val, summ,acc_val = sess.run([model.cost,model.merged,model.accuracy],feed_dict = {model.input: X_batch, model.labels: y_batch, model.keep_prob:1.0})
     67       perf_collect[1,step] = cost_val
     68       perf_collect[2,step] = acc_val

\lib\site-packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata)
    765     try:
    766       result = self._run(None, fetches, feed_dict, options_ptr,
--> 767                          run_metadata_ptr)
    768       if run_metadata:
    769         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
    950
    951     # Create a fetch handler to take care of the structure of fetches.
--> 952     fetch_handler = _FetchHandler(self._graph, fetches, feed_dict_string)
    953
    954     # Run request and get response.

\lib\site-packages\tensorflow\python\client\session.py in __init__(self, graph, fetches, feeds)
    406     """
    407     with graph.as_default():
--> 408       self._fetch_mapper = _FetchMapper.for_fetch(fetches)
    409     self._fetches = []
    410     self._targets = []

\lib\site-packages\tensorflow\python\client\session.py in for_fetch(fetch)
    228     elif isinstance(fetch, (list, tuple)):
    229       # NOTE(touts): This is also the code path for namedtuples.
--> 230       return _ListFetchMapper(fetch)
    231     elif isinstance(fetch, dict):
    232       return _DictFetchMapper(fetch)

\lib\site-packages\tensorflow\python\client\session.py in __init__(self, fetches)
    335     """
    336     self._fetch_type = type(fetches)
--> 337     self._mappers = [_FetchMapper.for_fetch(fetch) for fetch in fetches]
    338     self._unique_fetches, self._value_indices = _uniquify_fetches(self._mappers)
    339

\lib\site-packages\tensorflow\python\client\session.py in <listcomp>(.0)
    335     """
    336     self._fetch_type = type(fetches)
--> 337     self._mappers = [_FetchMapper.for_fetch(fetch) for fetch in fetches]
    338     self._unique_fetches, self._value_indices = _uniquify_fetches(self._mappers)
    339

\lib\site-packages\tensorflow\python\client\session.py in for_fetch(fetch)
    225     if fetch is None:
    226       raise TypeError('Fetch argument %r has invalid type %r' %
--> 227                       (fetch, type(fetch)))
    228     elif isinstance(fetch, (list, tuple)):
    229       # NOTE(touts): This is also the code path for namedtuples.

TypeError: Fetch argument None has invalid type <class 'NoneType'>

Any help appreciated!


问:
when i was compile, i passed configure but encounter errors when i was 'making'
I use ./configure and make to compile Dragon from Github on Ubuntu. i passed my configure process even though there is a warning for DarkSUSY which is optional according to the installation document. and i just run 'make' process. after some time, i encounter an error here is my error

```<code>make  all-recursive
make[1]: Entering directory '/media/rainman/d/DM/DRAGON'
Making all in cparamlib
make[2]: Entering directory '/media/rainman/d/DM/DRAGON/cparamlib'
make[2]: Nothing to be done for 'all'.
make[2]: Leaving directory '/media/rainman/d/DM/DRAGON/cparamlib'
make[2]: Entering directory '/media/rainman/d/DM/DRAGON'
/bin/bash ./libtool  --tag=F77   --mode=compile  -DHAVE_CONFIG_H -I.  -I/media/rainman/d/DM/DRAGON/include -I/media/rainman/d/DM/DRAGON/cparamlib -DTIXML_USE_STL -I/media/rainman/d/DM/gsl/scalar/include -I/media/rainman/d/DM/cfitsio/scalar//include   -c -o dmspec.lo dmspec.F
Usage: /media/rainman/d/DM/DRAGON/libtool [OPTION]... [MODE-ARG]..
``Try 'libtool --help' for more information.
libtool:   error: unrecognised option: '-DHAVE_CONFIG_H'
Makefile:756: recipe for target 'dmspec.lo' failed
make[2]: *** [dmspec.lo] Error 1
make[2]: Leaving directory '/media/rainman/d/DM/DRAGON'
Makefile:806: recipe for target 'all-recursive' failed
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory '/media/rainman/d/DM/DRAGON'
Makefile:573: recipe for target 'all' failed
make: *** [all] Error 2
</code>```

on the other computer i successfully pass 'make' while did the same thing but failed. i do not know how to deal with this. could anyone help me out? thanks

答:
The most relevant part of the error you recieved is

```<code>libtool:   error: unrecognised option: '-DHAVE_CONFIG_H'
</code>```

This error looks similar to that encountered in Error installing PCRE, which was due to not having the 'g++' c++ compiler installed.

Try installing build-essential (apt-get install build-essential) which should ensure you have the bare minimum necessary installed to compile most software, including g++.

Body:
I use ./configure and make to compile Dragon from Github on Ubuntu. i passed my configure process even though there is a warning for DarkSUSY which is optional according to the installation document. and i just run 'make' process. after some time, i encounter an error here is my error

make  all-recursive
make[1]: Entering directory '/media/rainman/d/DM/DRAGON'
Making all in cparamlib
make[2]: Entering directory '/media/rainman/d/DM/DRAGON/cparamlib'
make[2]: Nothing to be done for 'all'.
make[2]: Leaving directory '/media/rainman/d/DM/DRAGON/cparamlib'
make[2]: Entering directory '/media/rainman/d/DM/DRAGON'
/bin/bash ./libtool  --tag=F77   --mode=compile  -DHAVE_CONFIG_H -I.  -I/media/rainman/d/DM/DRAGON/include -I/media/rainman/d/DM/DRAGON/cparamlib -DTIXML_USE_STL -I/media/rainman/d/DM/gsl/scalar/include -I/media/rainman/d/DM/cfitsio/scalar//include   -c -o dmspec.lo dmspec.F
Usage: /media/rainman/d/DM/DRAGON/libtool [OPTION]... [MODE-ARG]..
``Try 'libtool --help' for more information.
libtool:   error: unrecognised option: '-DHAVE_CONFIG_H'
Makefile:756: recipe for target 'dmspec.lo' failed
make[2]: *** [dmspec.lo] Error 1
make[2]: Leaving directory '/media/rainman/d/DM/DRAGON'
Makefile:806: recipe for target 'all-recursive' failed
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory '/media/rainman/d/DM/DRAGON'
Makefile:573: recipe for target 'all' failed
make: *** [all] Error 2

on the other computer i successfully pass 'make' while did the same thing but failed. i do not know how to deal with this. could anyone help me out? thanks

Body:
The most relevant part of the error you recieved is

libtool:   error: unrecognised option: '-DHAVE_CONFIG_H'

This error looks similar to that encountered in Error installing PCRE, which was due to not having the 'g++' c++ compiler installed.

Try installing build-essential (apt-get install build-essential) which should ensure you have the bare minimum necessary installed to compile most software, including g++.


问:
How does one compute a single finite differences in Matlab efficiently?
I wanted to compute a finite difference with respect to the change of the function in Matlab. In other words

```<code>f(x+e_i) - f(x)
</code>```

is what I want to compute. Note that its very similar to the first order numerical partial differentiation (forward differentiation in this case) :

```<code>(f(x+e_i) - f(x)) / (e_i)
</code>```

Currently I am using for loops to compute it but it seems that Matlab is much slower than I thought. I am doing it as follows:

```<code>function [ dU ] = numerical_gradient(W,f,eps)
%compute gradient or finite difference update numerically
[D1, D2] = size(W);
dU = zeros(D1, D2);
for d1=1:D1
    for d2=1:D2
        e = zeros([D1,D2]);
        e(d1,d2) = eps;
        f_e1 = f(W+e);
        f_e2 = f(W-e);
        %numerical_derivative = (f_e1 - f_e2)/(2*eps);
        %dU(d1,d2) = numerical_derivative
        numerical_difference = f_e1 - f_e2;
        dU(d1,d2) = numerical_difference;
    end
end
</code>```

it seems that its really difficult to vectorize the above code because for numerical differences follow the definition of the gradient and partial derivatives which is:

```<code>df_dW = [ ..., df_dWi, ...]
</code>```

where df_dWi assumes the other coordinates are fixed and it only worries about the change of the variable Wi. Thus, I can't just change all the coordinates at once.

Is there a better way to do this? My intuition tells me that the best way to do this is to implement this not in matlab but in some other language, say C and then have matlab call that library. Is that true? Does it mean that the best solution is some Matlab library that does this for me?

I did see:

https://www.mathworks.com/matlabcentral/answers/332414-what-is-the-quickest-way-to-find-a-gradient-or-finite-difference-in-matlab-of-a-real-function-in-hig

but unfortunately, it computes exact derivatives, which isn't what I am looking for. I am explicitly looking for differences or "bad approximation" to the gradient.

Since it seems this code is not easy to vectorize (in fact my intuition tells me its not possible to do so) my only other idea is to implement this finite difference function in C and then have C call the function. Is this a good idea? Anyone know how to do this?

I did try reading the following:
https://www.mathworks.com/help/matlab/matlab_external/standalone-example.html

but it was too difficult to understand for me because I have no idea what a mex file is, if I need to have a arrayProduct.c file as well as a mex.h file, if I also needed a matlab file, etc. If there just existed a way to simply download a working example with all the functions they suggest there and some instructions to compile it, then it would be super helpful. But just reading the hmtl/article like that its impossible for me to infer what they want me to do.

For the sake of completness it seems reddit has some comments in its discussion of this:

https://www.reddit.com/r/matlab/comments/623m7i/how_does_one_compute_a_single_finite_differences/

答:
Here is a more efficient doing so:

```<code>function [ vNumericalGrad ] = CalcNumericalGradient( hInputFunc, vInputPoint, epsVal )

numElmnts       = size(vInputPoint, 1);
vNumericalGrad  = zeros([numElmnts, 1]);

refVal = hInputFunc(vInputPoint);

for ii = 1:numElmnts
    % Set the perturbation vector
    refInVal = vInputPoint(ii);
    vInputPoint(ii) = refInVal + epsVal;

    % Compute Numerical Gradient
    vNumericalGrad(ii) = (hInputFunc(vInputPoint) - refVal) / epsVal;

    % Reset the perturbation vector
    vInputPoint(ii) = refInVal;
end

end
</code>```

This code allocate less memory.

The above code performance will be totally controlled by the speed of the hInputFunction.

The small tricks compared to original code are:

No memory reallocation of e each iteration.

Instead of addition of vectors W + e there are 2 assignments to the array.

Decreasing the calls to hInputFunction() by half by defining the reference value outside the loop (This only works for Forward / Backward difference).

Probably this will be very close to C code unless you can code in C more efficiently the function which computes the value (hInputFunction).

A full implementation can be found in StackOverflow Q44984132 Repository (It was Posted in StackOverflow Q44984132).

See CalcFunGrad( vX, hObjFun, difMode, epsVal ).

Body:
I wanted to compute a finite difference with respect to the change of the function in Matlab. In other words

f(x+e_i) - f(x)

is what I want to compute. Note that its very similar to the first order numerical partial differentiation (forward differentiation in this case) :

(f(x+e_i) - f(x)) / (e_i)

Currently I am using for loops to compute it but it seems that Matlab is much slower than I thought. I am doing it as follows:

function [ dU ] = numerical_gradient(W,f,eps)
%compute gradient or finite difference update numerically
[D1, D2] = size(W);
dU = zeros(D1, D2);
for d1=1:D1
    for d2=1:D2
        e = zeros([D1,D2]);
        e(d1,d2) = eps;
        f_e1 = f(W+e);
        f_e2 = f(W-e);
        %numerical_derivative = (f_e1 - f_e2)/(2*eps);
        %dU(d1,d2) = numerical_derivative
        numerical_difference = f_e1 - f_e2;
        dU(d1,d2) = numerical_difference;
    end
end

it seems that its really difficult to vectorize the above code because for numerical differences follow the definition of the gradient and partial derivatives which is:

df_dW = [ ..., df_dWi, ...]

where df_dWi assumes the other coordinates are fixed and it only worries about the change of the variable Wi. Thus, I can't just change all the coordinates at once.

Is there a better way to do this? My intuition tells me that the best way to do this is to implement this not in matlab but in some other language, say C and then have matlab call that library. Is that true? Does it mean that the best solution is some Matlab library that does this for me?

I did see:

https://www.mathworks.com/matlabcentral/answers/332414-what-is-the-quickest-way-to-find-a-gradient-or-finite-difference-in-matlab-of-a-real-function-in-hig

but unfortunately, it computes exact derivatives, which isn't what I am looking for. I am explicitly looking for differences or "bad approximation" to the gradient.

Since it seems this code is not easy to vectorize (in fact my intuition tells me its not possible to do so) my only other idea is to implement this finite difference function in C and then have C call the function. Is this a good idea? Anyone know how to do this?

I did try reading the following:
https://www.mathworks.com/help/matlab/matlab_external/standalone-example.html

but it was too difficult to understand for me because I have no idea what a mex file is, if I need to have a arrayProduct.c file as well as a mex.h file, if I also needed a matlab file, etc. If there just existed a way to simply download a working example with all the functions they suggest there and some instructions to compile it, then it would be super helpful. But just reading the hmtl/article like that its impossible for me to infer what they want me to do.

For the sake of completness it seems reddit has some comments in its discussion of this:

https://www.reddit.com/r/matlab/comments/623m7i/how_does_one_compute_a_single_finite_differences/

Body:
Here is a more efficient doing so:

function [ vNumericalGrad ] = CalcNumericalGradient( hInputFunc, vInputPoint, epsVal )

numElmnts       = size(vInputPoint, 1);
vNumericalGrad  = zeros([numElmnts, 1]);

refVal = hInputFunc(vInputPoint);

for ii = 1:numElmnts
    % Set the perturbation vector
    refInVal = vInputPoint(ii);
    vInputPoint(ii) = refInVal + epsVal;

    % Compute Numerical Gradient
    vNumericalGrad(ii) = (hInputFunc(vInputPoint) - refVal) / epsVal;

    % Reset the perturbation vector
    vInputPoint(ii) = refInVal;
end

end

This code allocate less memory.

The above code performance will be totally controlled by the speed of the hInputFunction.

The small tricks compared to original code are:

No memory reallocation of e each iteration.

Instead of addition of vectors W + e there are 2 assignments to the array.

Decreasing the calls to hInputFunction() by half by defining the reference value outside the loop (This only works for Forward / Backward difference).

Probably this will be very close to C code unless you can code in C more efficiently the function which computes the value (hInputFunction).

A full implementation can be found in StackOverflow Q44984132 Repository (It was Posted in StackOverflow Q44984132).

See CalcFunGrad( vX, hObjFun, difMode, epsVal ).


问:
Performance benchmark: PHP Generated content VS. javascript and DOM over AJAX
For the following pretty straightforward task: query a list of products from a DB and present it on a webpage,
consider 2 setups:

Setup1: PHP script queries. All content is built on the server and the entire page is served back to the client.

Setup2: Static HTML "page skeleton" requesting content using AJAX. Received content is parsed on the client side using Javascript and rendered using innerHTML or similar.

Of course the second setup only makes sense when you have pages, categories and tags for the client user to choose from.

I need to compare these two, at least by means of:

time it will take content to be served

user experience (setup1 is delivered as a whole, setup2 is delivered in "two parts")

scalability - how do the setups compare when I have 100,000 queries daily

Any thoughts on the issue will be much appreciated.

答:
Unless the implementation sucks, AJAX should win hands down. Among the benefits are:

parallelism due to parallel requests on the client side (i.e. you can use multiple server CPU cores to serve parts of one served web page, that can't be done easily using PHP)

refreshing only small parts of the page is faster (less data to transfer, generate ...)

it scales much better since the server has less work to do (esp. if you can offload some of the processing needed for generating html to the client instead of just delivering it)

Dynamic pages like http://www.startpagina.nl/ have been doing this successfully since way before the recent AJAX fad (1 static file delivered, all customization done on the client side - last time I checked anyway).

Of course you can screw things up with either method so that it becomes slower than the other.

OwnerDisplayName: gnosis

OwnerDisplayName: mjy


问:
Google Analytics API service object - no management attribute
I am trying to set up an application that uses the Google Analytics API. I have all the authorization steps working correctly and can pull all the data as expected. However, at the moment, it is working because I have hardcoded my own view ID into the queries to the API. E.g:

```<code>response = analytics.reports().batchGet(
    body={
        "reportRequests":
            [
                {
                    "viewId": "ga:12345678",
                    ...
</code>```

From what I understand, what I need to do is before I start querying the data, is use the service object to first get a view Id (or list of View Ids), then use that in the data queries. I have been attempting to do just that and have been failing miserably. Basically, I have the following (just to get the first step of a list of accounts):

```<code>credentials = client.OAuth2Credentials.from_json(session['credentials'])
http = credentials.authorize(httplib2.Http())
analytics = build('analytics', 'v4', http=http) #create the service object
data = analytics.management().accounts().list().execute()
</code>```

The error I am getting is 'Resource' object has no attribute 'management'. What am I missing here??

答:
Ok, so for those who come across this question, the issue seemed to be that, as of the time of writing, version 4 of the API does not have the management features. What I/we did instead was build a second service to get the account details using version 3:

```<code>service = build('analytics', 'v3', http=http)
accounts = service.management().accounts().list().execute()
</code>```

That seemed to do the trick, although there was some additional fiddling required in order to get the view ID(s). However, once you have the object, it is just a matter of manipulating the object as needed.

Body:
I am trying to set up an application that uses the Google Analytics API. I have all the authorization steps working correctly and can pull all the data as expected. However, at the moment, it is working because I have hardcoded my own view ID into the queries to the API. E.g:

response = analytics.reports().batchGet(
    body={
        "reportRequests":
            [
                {
                    "viewId": "ga:12345678",
                    ...

From what I understand, what I need to do is before I start querying the data, is use the service object to first get a view Id (or list of View Ids), then use that in the data queries. I have been attempting to do just that and have been failing miserably. Basically, I have the following (just to get the first step of a list of accounts):

credentials = client.OAuth2Credentials.from_json(session['credentials'])
http = credentials.authorize(httplib2.Http())
analytics = build('analytics', 'v4', http=http) #create the service object
data = analytics.management().accounts().list().execute()

The error I am getting is 'Resource' object has no attribute 'management'. What am I missing here??

Body:
Ok, so for those who come across this question, the issue seemed to be that, as of the time of writing, version 4 of the API does not have the management features. What I/we did instead was build a second service to get the account details using version 3:

service = build('analytics', 'v3', http=http)
accounts = service.management().accounts().list().execute()

That seemed to do the trick, although there was some additional fiddling required in order to get the view ID(s). However, once you have the object, it is just a matter of manipulating the object as needed.


问:
PHP - Compare associative array with custom list and sort to match
I need to sort an array in a particular order, sorting by the keys.  I know I need to do something like the below but have tried many different variations of the it and cannot get the result I need.

Can anyone help?

An example of the array is below and th results I am looking for for this would be:  Connectivity | Contact Centres | Cloud & Hosting | Business Continuity

$solutions = Array ( [Business Continuity] => business-continuity
  [Connectivity] => connectivity [Cloud & Hosting] => cloud-hosting
  [Contact Centres] => contact-centres )

```<code>    function reorder_solutions($a, $b){
                    $custom = array('Lines & Calls', 'Mobile', 'Connectivity', 'Wifi', 'LAN', 'UC&C', 'Contact Centres', 'Cloud & Hosting', 'Managed Services', 'Security', 'Business Continuity');

                    foreach ($custom as $k => $v){
                        if ($k == $b) {
                            return 0;
                        }
                        return ($k < $b) ? -1 : 1;
                    }
                }

                uasort($solutions, "reorder_solutions");
</code>```

答:
Here's one way to do it:

```<code>// loop over the $custom array
foreach ($custom as $key) {

    // add each key found in $solutions into a $sorted array
    // (they'll be added in custom order)
    if (isset($solutions[$key])) {
        $sorted[$key] = $solutions[$key];

        // remove the item from $solutions after adding it to $sorted
        unset($solutions[$key]);
    }
}

// merge the $sorted array with any remaining items in $solutions
$solutions = array_merge($sorted, $solutions);
</code>```

Another way:

```<code>// create an array using the values of $custom as keys with all empty values
$custom = array_fill_keys($custom, null);

// merge that array with $solutions (same keys will be overwritten with values
//  from $solutions, then remove the empty values with array_filter
$solutions = array_filter(array_merge($custom, $solutions));
</code>```

You can make it a one-liner if you like.

```<code>$solutions = array_filter(array_merge(array_fill_keys($custom, null), $solutions));
</code>```

Body:
I need to sort an array in a particular order, sorting by the keys.  I know I need to do something like the below but have tried many different variations of the it and cannot get the result I need.

Can anyone help?

An example of the array is below and th results I am looking for for this would be:  Connectivity | Contact Centres | Cloud & Hosting | Business Continuity

$solutions = Array ( [Business Continuity] => business-continuity
  [Connectivity] => connectivity [Cloud & Hosting] => cloud-hosting
  [Contact Centres] => contact-centres )

    function reorder_solutions($a, $b){
                    $custom = array('Lines & Calls', 'Mobile', 'Connectivity', 'Wifi', 'LAN', 'UC&C', 'Contact Centres', 'Cloud & Hosting', 'Managed Services', 'Security', 'Business Continuity');

                    foreach ($custom as $k => $v){
                        if ($k == $b) {
                            return 0;
                        }
                        return ($k < $b) ? -1 : 1;
                    }
                }

                uasort($solutions, "reorder_solutions");

Body:
Here's one way to do it:

// loop over the $custom array
foreach ($custom as $key) {

    // add each key found in $solutions into a $sorted array
    // (they'll be added in custom order)
    if (isset($solutions[$key])) {
        $sorted[$key] = $solutions[$key];

        // remove the item from $solutions after adding it to $sorted
        unset($solutions[$key]);
    }
}

// merge the $sorted array with any remaining items in $solutions
$solutions = array_merge($sorted, $solutions);

Another way:

// create an array using the values of $custom as keys with all empty values
$custom = array_fill_keys($custom, null);

// merge that array with $solutions (same keys will be overwritten with values
//  from $solutions, then remove the empty values with array_filter
$solutions = array_filter(array_merge($custom, $solutions));

You can make it a one-liner if you like.

$solutions = array_filter(array_merge(array_fill_keys($custom, null), $solutions));


问:
pyparsing: skip to the next token ignoring everything in between
I am trying to parse a log file that contains multiple entries with the following format:

```<code>ITEM_BEGIN item_name
  some_text
</code>```

some_text may optionally contain an expression matched by my_expr anywhere within itself. I am only interested in item_name and my_expr (or None if it is missing). Ideally, what I want is a list of (item_name, my_expr) pairs. What is the best way to extract this information using pyparsing?

答:
If you are not trying to define a parser for the entire input text, but only some pieces of it, look into using pyparsing's searchString or scanString methods - something along these lines:

```<code>import pyparsing as pp
ident = Word(alphas, alphanums+'_')
item_header = pp.Keyword("ITEM_BEGIN") + ident("name")
other_expr = ... whatever ...

search_expr = item_header | other_expr

found = {}
current_name = ''
for result in search_expr.searchString(input_text):
    result = result[0]
    if result[0] == "ITEM_BEGIN":
        print("found an item header with name {name}".format_map(result))
        current_name = result.name
        found[result.name] = []
    else:
        # found an other expr
        found[current_name].append(result.asList())
</code>```

Body:
I am trying to parse a log file that contains multiple entries with the following format:

ITEM_BEGIN item_name
  some_text

some_text may optionally contain an expression matched by my_expr anywhere within itself. I am only interested in item_name and my_expr (or None if it is missing). Ideally, what I want is a list of (item_name, my_expr) pairs. What is the best way to extract this information using pyparsing?

Body:
If you are not trying to define a parser for the entire input text, but only some pieces of it, look into using pyparsing's searchString or scanString methods - something along these lines:

import pyparsing as pp
ident = Word(alphas, alphanums+'_')
item_header = pp.Keyword("ITEM_BEGIN") + ident("name")
other_expr = ... whatever ...

search_expr = item_header | other_expr

found = {}
current_name = ''
for result in search_expr.searchString(input_text):
    result = result[0]
    if result[0] == "ITEM_BEGIN":
        print("found an item header with name {name}".format_map(result))
        current_name = result.name
        found[result.name] = []
    else:
        # found an other expr
        found[current_name].append(result.asList())


问:
Make a div span two rows in a grid
I have a page full of blocks which pile up with display: inline-block. I want to make some four or two times bigger, so I used float: left or right to put other blocks around.

My problem is if I have a five-element row, how could I put a bigger element in the middle of it? (as float put it naturally on the side).

Here's an example snippet:

#wrapper{
  width: 516px;
}
.block{
  display: inline-block;
  width: 90px;
  height: 50px;
  margin: 5px;
  background-color: red;
}
.bigger{
  height: 110px;
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><div id="wrapper">
  <div class="block"></div>
  <div class="block"></div>
  <div class="block"></div>
  <div class="block"></div>
  <div class="block"></div>
  <div class="block"></div>
  <div class="block bigger"></div>
  <div class="block"></div>
  <div class="block"></div>
</div></code>```

Here's what I would like to have from the snippet above

答:
Flexbox Solution

You have fixed heights on your child elements (.block). Based on that information, we can extrapolate the height of the container (#wrapper).

By knowing the height of the container, your layout can be achieved using CSS Flexbox with flex-direction: column and flex-wrap: wrap.

A fixed height on the container serves as a breakpoint, telling flex items where to wrap.

#wrapper {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  height: 120px;
  width: 516px;
}
.block {
  width: 90px;
  flex: 0 0 50px;
  margin: 5px;
  background-color: red;
}
.bigger {
  flex-basis: 110px;
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><div id="wrapper">
  <div class="block"></div>
  <div class="block"></div>
  <div class="block bigger"></div>
  <div class="block"></div>
  <div class="block"></div>
  <div class="block"></div>
  <div class="block"></div>
  <div class="block"></div>
  <div class="block"></div>
</div></code>```

jsFiddle

Here's an explanation why flex items can't wrap unless there's a fixed height/width on the container: https://stackoverflow.com/a/43897663/3597276

Title: Make a div span two rows in a grid

Body:
I have a page full of blocks which pile up with display: inline-block. I want to make some four or two times bigger, so I used float: left or right to put other blocks around.

My problem is if I have a five-element row, how could I put a bigger element in the middle of it? (as float put it naturally on the side).

Here's an example snippet:

#wrapper{
  width: 516px;
}
.block{
  display: inline-block;
  width: 90px;
  height: 50px;
  margin: 5px;
  background-color: red;
}
.bigger{
  height: 110px;
}

Here's what I would like to have from the snippet above

Body:
Flexbox Solution

You have fixed heights on your child elements (.block). Based on that information, we can extrapolate the height of the container (#wrapper).

By knowing the height of the container, your layout can be achieved using CSS Flexbox with flex-direction: column and flex-wrap: wrap.

A fixed height on the container serves as a breakpoint, telling flex items where to wrap.

#wrapper {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  height: 120px;
  width: 516px;
}
.block {
  width: 90px;
  flex: 0 0 50px;
  margin: 5px;
  background-color: red;
}
.bigger {
  flex-basis: 110px;
}

<div id="wrapper">
  <div class="block"></div>
  <div class="block"></div>
  <div class="block bigger"></div>
  <div class="block"></div>
  <div class="block"></div>
  <div class="block"></div>
  <div class="block"></div>
  <div class="block"></div>
  <div class="block"></div>
</div>

jsFiddle

Here's an explanation why flex items can't wrap unless there's a fixed height/width on the container: https://stackoverflow.com/a/43897663/3597276

ContentLicense: CC BY-SA 4.0


问:
Recommend non GPL XMPP server in java
It seems nearly all the XMPP java servers such as OpenFire, Tigrese are GPL.</p>

答: Open IM is the only one I could find that isn't GPLed - it's BSDed.</p>

Title: Recommend non GPL XMPP server in java


问:
Error "The input device is not a TTY"
I am running the following command from my Jenkinsfile. However, I get the error "The input device is not a TTY".

```<code>docker run -v $PWD:/foobar -it cloudfoundry/cflinuxfs2 /foobar/script.sh
</code>```

Is there a way to run the script from the Jenkinsfile without doing interactive mode?

I basically have a file called script.sh that I would like to run inside the Docker container.

答:
Remove the -it from your cli to make it non interactive and remove the TTY. If you don't need either, e.g. running your command inside of a Jenkins or cron script, you should do this.

Or you can change it to -i if you have input piped into the docker command that doesn't come from a TTY. If you have something like xyz | docker ... or docker ... <input in your command line, do this.

Or you can change it to -t if you want TTY support but don't have it available on the input device. Do this for apps that check for a TTY to enable color formatting of the output in your logs, or for when you later attach to the container with a proper terminal.

Or if you need an interactive terminal and aren't running in a terminal on Linux or MacOS, use a different command line interface. PowerShell is reported to include this support on Windows.

What is a TTY? It's a terminal interface that supports escape sequences, moving the cursor around, etc, that comes from the old days of dumb terminals attached to mainframes. Today it is provided by the Linux command terminals and ssh interfaces. See the wikipedia article for more details.

To see the difference of running a container with and without a TTY, run a container without one: docker run --rm -i ubuntu bash. From inside that container, install vim with apt-get update; apt-get install vim. Note the lack of a prompt. When running vim against a file, try to move the cursor around within the file.

Title: Error "The input device is not a TTY"

Body:
I am running the following command from my Jenkinsfile. However, I get the error "The input device is not a TTY".

docker run -v $PWD:/foobar -it cloudfoundry/cflinuxfs2 /foobar/script.sh

Is there a way to run the script from the Jenkinsfile without doing interactive mode?

I basically have a file called script.sh that I would like to run inside the Docker container.


问:
Excel IFS formula - range of number
I have a value in cell AB8 and I want to conditionally return a status depending on which value it takes between 1 and 30.

I want to save time and rather than type the scores 1-9 indivdually to turn into "EMERGING" (etc) is there a way I can put a range in?

```<code>IFS(AB8=1,"EMERGING",AB8=2,"EMERGING",AB8=3,"EMERGING",AB8=4,"EMERGING",AB8=5,"EMERGING",AB8=6,"EMERGING",AB8=7,"EMERGING",AB8=8,"EMERGING",AB8=9,"DEVELOPING",AB8=10,"DEVELOPING",AB8=11,"DEVELOPING",AB8=12,"DEVELOPING",AB8=13,"DEVELOPING",AB8=14,"DEVELOPING",AB8=15,"DEVELOPING",AB8=16,"DEVELOPING",AB8=17,"SECURING",AB8=18,"SECURING",AB8=19,"SECURING",AB8=20,"SECURING",AB8=21,"SECURING",AB8=22,"SECURING",AB8=23,"SECURING",AB8=24,"SECURING",AB8=25,"READY",AB8=26,"READY",AB8=27,"READY",AB8=28,"READY",AB8=29,"READY",AB8=30,"READY")
</code>```

答:
This one might also work,</p>

```<code>=IF(AND(AB8>=1,AB8<=8),"EMERGING",IF(AND(AB8>=9,AB8<=16),"DEVELOPING",IF(AND(AB8>=17,AB8<=24),"SECURING",IF(AB8<=30,"READY","INVALID NUMBER"))))
</code>```

Title: Excel IFS formula - range of number

Body:
I have a value in cell AB8 and I want to conditionally return a status depending on which value it takes between 1 and 30.

I want to save time and rather than type the scores 1-9 indivdually to turn into "EMERGING" (etc) is there a way I can put a range in?

IFS(AB8=1,"EMERGING",AB8=2,"EMERGING",AB8=3,"EMERGING",AB8=4,"EMERGING",AB8=5,"EMERGING",AB8=6,"EMERGING",AB8=7,"EMERGING",AB8=8,"EMERGING",AB8=9,"DEVELOPING",AB8=10,"DEVELOPING",AB8=11,"DEVELOPING",AB8=12,"DEVELOPING",AB8=13,"DEVELOPING",AB8=14,"DEVELOPING",AB8=15,"DEVELOPING",AB8=16,"DEVELOPING",AB8=17,"SECURING",AB8=18,"SECURING",AB8=19,"SECURING",AB8=20,"SECURING",AB8=21,"SECURING",AB8=22,"SECURING",AB8=23,"SECURING",AB8=24,"SECURING",AB8=25,"READY",AB8=26,"READY",AB8=27,"READY",AB8=28,"READY",AB8=29,"READY",AB8=30,"READY")

Body:
This one might also work,

=IF(AND(AB8>=1,AB8<=8),"EMERGING",IF(AND(AB8>=9,AB8<=16),"DEVELOPING",IF(AND(AB8>=17,AB8<=24),"SECURING",IF(AB8<=30,"READY","INVALID NUMBER"))))


问:
VB.NET - counting days between two dates with exclusions
I'm trying to count the days between two dates, excluding Saturdays and Sundays. I've written this code so far

```<code>Dim startDay As Integer
Dim endDay As Integer
Dim days As Integer
Dim count As Integer

startDay = dtpStartDate.Value.DayOfWeek
endDay = dtpEndDate.Value.DayOfWeek

For days = startDay To endDay
    If days = 0 Or days = 6 Then           'Sunday = 0, Saturday = 6
        count += 1
    End If
Next

    lblNoOfDays.Text = count
</code>```

It works fine if you choose the two dates within the same week. (ex: 23rd Jan to 27th Jan, gives the result 5)
But if I set them to dates in different weeks, (ex : 23rd Jan to 30th Jan, gives the result 1), it gives incorrect results.

I know it happens because of the loop but I can't think of a way to overcome this. Can anyone give me a suggestion, solution??

Thank you

答:
This function calculates the non-weekend days between two dates:</p>

```<code>    Public Shared Function WorkingDaysElapsed(ByVal pFromDate As Date, ByVal pToDate As Date) As Integer

        Dim _elapsedDays As Integer = 0
        Dim _weekendDays As DayOfWeek() = {DayOfWeek.Saturday, DayOfWeek.Sunday}

        For i = 0 To (pToDate - pFromDate).Days
            If Not _weekendDays.Contains(pFromDate.AddDays(i).DayOfWeek) Then _elapsedDays += 1
        Next

        Return _elapsedDays

    End Function
</code>```

Body:
I'm trying to count the days between two dates, excluding Saturdays and Sundays. I've written this code so far

Dim startDay As Integer
Dim endDay As Integer
Dim days As Integer
Dim count As Integer

startDay = dtpStartDate.Value.DayOfWeek
endDay = dtpEndDate.Value.DayOfWeek

For days = startDay To endDay
    If days = 0 Or days = 6 Then           'Sunday = 0, Saturday = 6
        count += 1
    End If
Next

    lblNoOfDays.Text = count

It works fine if you choose the two dates within the same week. (ex: 23rd Jan to 27th Jan, gives the result 5)
But if I set them to dates in different weeks, (ex : 23rd Jan to 30th Jan, gives the result 1), it gives incorrect results.

I know it happens because of the loop but I can't think of a way to overcome this. Can anyone give me a suggestion, solution??

Thank you

Body:
This function calculates the non-weekend days between two dates:

    Public Shared Function WorkingDaysElapsed(ByVal pFromDate As Date, ByVal pToDate As Date) As Integer

        Dim _elapsedDays As Integer = 0
        Dim _weekendDays As DayOfWeek() = {DayOfWeek.Saturday, DayOfWeek.Sunday}

        For i = 0 To (pToDate - pFromDate).Days
            If Not _weekendDays.Contains(pFromDate.AddDays(i).DayOfWeek) Then _elapsedDays += 1
        Next

        Return _elapsedDays

    End Function


问:
Efficiently store list of matrices
I have a large list of images stored as numpy matrices. The images have different sizes e.g.

```<code>import numpy as np
from numpy.random import rand

data = [rand(100,200), rand(1024, 768)]
</code>```

I am looking for a way to store this list of matrices such that it can be read fast (writing the data can be slow). I tried pickle/numpy.savez, but reading the data was slower than loading the raw images again.

I think hdf5 may be fast, however I cannot figure out how to store this list. Not mandatory, but useful would data format which allows to append data such that the list does not have to in memory as a whole.

Edit:
Based on the answers so far I tried to time some suggestions

```<code>data = [rand(1024, 768) for i in np.arange(100)]
def timenp():
    np.savez("test.npz",*data)
    d=np.load('test.npz')
    loaded = [d[f] for f in d]

def timebinary():
    with file("tmp.bin","wb") as f:
        np.save(f, len(data))
        for img in data:
            np.save(f,img)

    with file("tmp.bin","rb") as f:
        n = np.load(f)
        loaded = []
        for i in np.arange(n):
            loaded.append(np.load(f))

import h5py
def timeh5py():
    with h5py.File('foo.hdf5','w') as f:
        dt = h5py.special_dtype(vlen=np.dtype('float32'))
        dset = f.create_dataset('data', (len(data),), dtype=dt)
        shapes = f.create_dataset('shapes', (len(data), 2), dtype='int32')
        dset[...] = [img.flatten() for img in data]
        shapes[...] = [img.shape for img in data]

    with h5py.File('foo.hdf5','r') as f:
        loaded=[]
        for (img, shape) in zip(f['data'],f['shapes']):
            loaded.append(np.reshape(img,shape))
</code>```

python -m cProfile timenp.py

           452906 function calls (451141 primitive calls) in 9.256 seconds

python -m cProfile timebinary.py
          73085 function calls (71340 primitive calls) in 4.945 seconds

python -m cProfile timeh5py.py

          33151 function calls (32568 primitive calls) in 4.384 seconds

答:
```<code>In [276]: alist=[np.arange(10), np.arange(3), np.arange(100)]
</code>```

If I save this as np.savez('test',alist), it saves the list as one object.  If instead I expand the list with *, then it puts each list element is a separate file in the archive.

```<code>In [277]: np.savez('test',*alist)
In [278]: d=np.load('test.npz')
In [279]: list(d.keys())
Out[279]: ['arr_2', 'arr_1', 'arr_0']
In [280]: d['arr_0']
Out[280]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
</code>```

With np.save (and by extension savez), arrays are stored in their own compact format, which consists of a header block with shape information, and the data block, which is essentially a byte copy of its data buffer.  So a np.save of an array should be as efficient any other method.

If you give np.save an non-array object it will use that object's pickle method.  But note that the pickle method for an array is the save method I just described.  So a pickle of an array should still be efficient.

Keep in mind that npz files are lazy-loaded.

With h5py, arrays are saved to named data sets.  In sense it is like the savez above - the elements of the list have to have names, whether generated automatically or by your code.

I don't know how h5py speeds compare with a save(z).

h5py can handle arrays that are ragged in one dimension.  I've explored that in previous SO questions.  Storing multidimensional variable length array with h5py

How to save list of numpy.arrays of different shape with h5py?

Title: Efficiently store list of matrices

Body:
I have a large list of images stored as numpy matrices. The images have different sizes e.g.

import numpy as np
from numpy.random import rand

data = [rand(100,200), rand(1024, 768)]

I am looking for a way to store this list of matrices such that it can be read fast (writing the data can be slow). I tried pickle/numpy.savez, but reading the data was slower than loading the raw images again.

I think hdf5 may be fast, however I cannot figure out how to store this list. Not mandatory, but useful would data format which allows to append data such that the list does not have to in memory as a whole.

Edit:
Based on the answers so far I tried to time some suggestions

data = [rand(1024, 768) for i in np.arange(100)]
def timenp():
    np.savez("test.npz",*data)
    d=np.load('test.npz')
    loaded = [d[f] for f in d]

def timebinary():
    with file("tmp.bin","wb") as f:
        np.save(f, len(data))
        for img in data:
            np.save(f,img)

    with file("tmp.bin","rb") as f:
        n = np.load(f)
        loaded = []
        for i in np.arange(n):
            loaded.append(np.load(f))

import h5py
def timeh5py():
    with h5py.File('foo.hdf5','w') as f:
        dt = h5py.special_dtype(vlen=np.dtype('float32'))
        dset = f.create_dataset('data', (len(data),), dtype=dt)
        shapes = f.create_dataset('shapes', (len(data), 2), dtype='int32')
        dset[...] = [img.flatten() for img in data]
        shapes[...] = [img.shape for img in data]

    with h5py.File('foo.hdf5','r') as f:
        loaded=[]
        for (img, shape) in zip(f['data'],f['shapes']):
            loaded.append(np.reshape(img,shape))

python -m cProfile timenp.py

           452906 function calls (451141 primitive calls) in 9.256 seconds

python -m cProfile timebinary.py
          73085 function calls (71340 primitive calls) in 4.945 seconds

python -m cProfile timeh5py.py

          33151 function calls (32568 primitive calls) in 4.384 seconds

Body:
In [276]: alist=[np.arange(10), np.arange(3), np.arange(100)]

If I save this as np.savez('test',alist), it saves the list as one object.  If instead I expand the list with *, then it puts each list element is a separate file in the archive.

In [277]: np.savez('test',*alist)
In [278]: d=np.load('test.npz')
In [279]: list(d.keys())
Out[279]: ['arr_2', 'arr_1', 'arr_0']
In [280]: d['arr_0']
Out[280]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

With np.save (and by extension savez), arrays are stored in their own compact format, which consists of a header block with shape information, and the data block, which is essentially a byte copy of its data buffer.  So a np.save of an array should be as efficient any other method.

If you give np.save an non-array object it will use that object's pickle method.  But note that the pickle method for an array is the save method I just described.  So a pickle of an array should still be efficient.

Keep in mind that npz files are lazy-loaded.

With h5py, arrays are saved to named data sets.  In sense it is like the savez above - the elements of the list have to have names, whether generated automatically or by your code.

I don't know how h5py speeds compare with a save(z).

h5py can handle arrays that are ragged in one dimension.  I've explored that in previous SO questions.  Storing multidimensional variable length array with h5py

How to save list of numpy.arrays of different shape with h5py?


问:
Using UIDynamics gravity to animate CGRects
My goal is to have a bunch of UIViews that have a property of CGRect falling from offscreen and then piling up. Right now, I have one circle working, but every time I try to add another circle, they won't animate anymore. I am using UIDynamics and its gravity function.  Here is my code:

```<code>func addBox(location: CGRect) -> UIView {
    let newBox = UIView(frame: location)
    newBox.backgroundColor = UIColor(red: 186.0/255.0, green: 216.0/255.0, blue: 242.0/255.0, alpha: 1.0)
    newBox.layer.cornerRadius = newBox.frame.width/2

    hView.insertSubview(newBox, at: 0)

    return newBox
}

addBox(location: CGRect(x: 100, y: -100, width: 100, height: 100))

var animator: UIDynamicAnimator? = nil;
let gravity = UIGravityBehavior()
var collision: UICollisionBehavior!
animator = UIDynamicAnimator(referenceView: hView)

func createAnimatorStuff(box: UIView) {
    collision = UICollisionBehavior(items: [box])
    collision.translatesReferenceBoundsIntoBoundary = true
    animator?.addBehavior(collision)

    gravity.addItem(box)
    gravity.gravityDirection = CGVector(dx: 0, dy: 0.8)
    animator?.addBehavior(gravity)

}

func dropDown() {
    let xVal = 100
    let xVal2 = 700

    let box = addBox(location: CGRect(x: xVal, y: 100, width: 100, height: 100))
    let box2 = addBox(location: CGRect(x: xVal2, y: 100, width: 100, height: 100))
    createAnimatorStuff(box: box)
    createAnimatorStuff(box: box2)
}
dropDown()
</code>```

If anyone could help me and adjust my code to create more circles falling and then piling up I would immensely appreciate it. Truly. Please ask any questions and thank you so much in advance.

答:
(NOTE: the question was updated with these suggestions, so that's why it appears to suggest things that were already done)

The general problem you are having is that you made box a global variable and you keep reinitializing the UIDynamicAnimator. So ...

Remove the box var, then

Change:

```<code>func addBox(location: CGRect)
</code>```

to:

```<code>func addBox(location: CGRect) -> UIView
</code>```

and return newBox at the end.

Then, change:

```<code>func createAnimatorStuff() {
</code>```

to:

```<code>func createAnimatorStuff(box: UIView) {
</code>```

and

```<code>addBox(location: CGRect(x: xVal, y: 100, width: 100, height: 100))
createAnimatorStuff()
</code>```

To

```<code>let box = addBox(location: CGRect(x: xVal, y: 100, width: 100, height: 100))
createAnimatorStuff(box: box)
</code>```

Finally, don't create a new UIDynamicAnimator for every call -- make one and share it.  You can do this with

```<code>let animator = UIDynamicAnimator(referenceView: hView)
</code>```

(also, you don't need semicolons in Swift)

Body:
My goal is to have a bunch of UIViews that have a property of CGRect falling from offscreen and then piling up. Right now, I have one circle working, but every time I try to add another circle, they won't animate anymore. I am using UIDynamics and its gravity function.  Here is my code:

func addBox(location: CGRect) -> UIView {
    let newBox = UIView(frame: location)
    newBox.backgroundColor = UIColor(red: 186.0/255.0, green: 216.0/255.0, blue: 242.0/255.0, alpha: 1.0)
    newBox.layer.cornerRadius = newBox.frame.width/2

    hView.insertSubview(newBox, at: 0)

    return newBox
}

addBox(location: CGRect(x: 100, y: -100, width: 100, height: 100))

var animator: UIDynamicAnimator? = nil;
let gravity = UIGravityBehavior()
var collision: UICollisionBehavior!
animator = UIDynamicAnimator(referenceView: hView)

func createAnimatorStuff(box: UIView) {
    collision = UICollisionBehavior(items: [box])
    collision.translatesReferenceBoundsIntoBoundary = true
    animator?.addBehavior(collision)

    gravity.addItem(box)
    gravity.gravityDirection = CGVector(dx: 0, dy: 0.8)
    animator?.addBehavior(gravity)

}

func dropDown() {
    let xVal = 100
    let xVal2 = 700

    let box = addBox(location: CGRect(x: xVal, y: 100, width: 100, height: 100))
    let box2 = addBox(location: CGRect(x: xVal2, y: 100, width: 100, height: 100))
    createAnimatorStuff(box: box)
    createAnimatorStuff(box: box2)
}
dropDown()

If anyone could help me and adjust my code to create more circles falling and then piling up I would immensely appreciate it. Truly. Please ask any questions and thank you so much in advance.

Body:
(NOTE: the question was updated with these suggestions, so that's why it appears to suggest things that were already done)

The general problem you are having is that you made box a global variable and you keep reinitializing the UIDynamicAnimator. So ...

Remove the box var, then

Change:

func addBox(location: CGRect)

to:

func addBox(location: CGRect) -> UIView

and return newBox at the end.

Then, change:

func createAnimatorStuff() {

to:

func createAnimatorStuff(box: UIView) {

and

addBox(location: CGRect(x: xVal, y: 100, width: 100, height: 100))
createAnimatorStuff()

To

let box = addBox(location: CGRect(x: xVal, y: 100, width: 100, height: 100))
createAnimatorStuff(box: box)

Finally, don't create a new UIDynamicAnimator for every call -- make one and share it.  You can do this with

let animator = UIDynamicAnimator(referenceView: hView)

(also, you don't need semicolons in Swift)


问:
Device number in stat command output
```<code>  stat test.log
  File: `test.log'
  Size: 573         Blocks: 8          IO Block: 4096   regular file
Device: 804h/2052d  Inode: 7091301     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1001/   abc)   Gid: ( 1001/   abc)
Access: 2010-11-29 17:56:22.000000000 -0800
Modify: 2010-11-29 17:56:22.000000000 -0800
Change: 2010-11-29 17:56:22.000000000 -0800
</code>```

In the stat o/p above what does the Device entry signify ?

答:
The stat command is simply a frontend to the stat() system call.

From the stat(2) manual page (man 2 stat)

The st_dev field describes the device
  on which this file resides.  (The
  major(3) and minor(3) macros may be
  useful to decompose the device ID in
  this field.)

From the 0804 hex notation you get major=8 (/dev/sd*) minor=4. i.e. /dev/sda4

Title: Device number in stat command output

Body:
  stat test.log
  File: `test.log'
  Size: 573         Blocks: 8          IO Block: 4096   regular file
Device: 804h/2052d  Inode: 7091301     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1001/   abc)   Gid: ( 1001/   abc)
Access: 2010-11-29 17:56:22.000000000 -0800
Modify: 2010-11-29 17:56:22.000000000 -0800
Change: 2010-11-29 17:56:22.000000000 -0800

In the stat o/p above what does the Device entry signify ?


问:
Return certain record based on criteria
Take this list of results

```<code>  Date     | YESorNO
---------------------
01/01/2001 | NO
01/01/2002 | NO
01/01/2003 | YES
01/01/2004 | NO
01/01/2005 | NO
01/01/2006 | NO
01/01/2007 | YES
01/01/2008 | YES
01/01/2009 | YES
</code>```

The list is ordered and I cannot sort this is any other way other than descending / ascending.

I want to be able to return the first 'YES' after all 'NO's have been accounted for.

In the above example, row 7 is the record I want returned (on 01/01/2007)

My code is as follows

```<code>var query =
(
    from m in db.MyTable
    where m.Criteria == XYZ
    select new
    {
      Date = m.Date,
      YESorNO = m.YESorNO
    }
).OrderBy(x => x.Date);
</code>```

Using .FirstOrDefault(x => x.YesOrNO == "YES") returns the 3rd record.

Thanks for any help.

答:
You can do this by ordering descending and take the last element of the first group of "YES":

```<code>var query =
(
    from m in db.MyTable
    where m.Criteria == XYZ
    select new
    {
      Date = m.Date,
      YESorNO = m.YESorNO
    }
).OrderByDescending(x => x.Date);

var result = query.AsEnumerable()
                  .TakeWhile(x => x.YESorNO == "YES")
                  .LastOrDefault();
</code>```

I use AsEnumerable() here, because I'm not sure which operators are translatable into SQL, TakeWhile is surely not.

If there is no "YES" after the latest "NO", this returns null.

Title: Return certain record based on criteria

Body:
Take this list of results

  Date     | YESorNO
---------------------
01/01/2001 | NO
01/01/2002 | NO
01/01/2003 | YES
01/01/2004 | NO
01/01/2005 | NO
01/01/2006 | NO
01/01/2007 | YES
01/01/2008 | YES
01/01/2009 | YES

The list is ordered and I cannot sort this is any other way other than descending / ascending.

I want to be able to return the first 'YES' after all 'NO's have been accounted for.

In the above example, row 7 is the record I want returned (on 01/01/2007)

My code is as follows

var query =
(
    from m in db.MyTable
    where m.Criteria == XYZ
    select new
    {
      Date = m.Date,
      YESorNO = m.YESorNO
    }
).OrderBy(x => x.Date);

Using .FirstOrDefault(x => x.YesOrNO == "YES") returns the 3rd record.

Thanks for any help.

Body:
You can do this by ordering descending and take the last element of the first group of "YES":

var query =
(
    from m in db.MyTable
    where m.Criteria == XYZ
    select new
    {
      Date = m.Date,
      YESorNO = m.YESorNO
    }
).OrderByDescending(x => x.Date);

var result = query.AsEnumerable()
                  .TakeWhile(x => x.YESorNO == "YES")
                  .LastOrDefault();

I use AsEnumerable() here, because I'm not sure which operators are translatable into SQL, TakeWhile is surely not.

If there is no "YES" after the latest "NO", this returns null.


问:
Evaluation of list-comprehensions in Haskell
I am wondering exactly how list-comprehensions are evaluated in Haskell. After reading this Removing syntactic sugar: List comprehension in Haskell and this: Haskell Lazy Evaluation and Reuse I still don't really understand if

```<code>[<function> x|x <- <someList>, <somePredicate>]
</code>```

is actually exactly equivalent (not just in outcome but in evaluation) to

```<code>filter (<somePredicate>) . map (<function>) $ <someList>
</code>```

and if so, does this mean it can potentially reduce time complexity drastically to build up the desired list with only desired elements?
Also, how does this work in terms of infinite lists? To be specific: I assume something like:

```<code>[x|x <- [1..], x < 20]
</code>```

will be evaluated in finite time, but how "obvious" does the fact that there are no more elements above some value which satisfy the predicate need to be, for the compiler to consider it? Would

```<code>[x|x <- [1..], (sum.map factorial $ digits x) == x]
</code>```

work (see project Euler problem 34 https://projecteuler.net/problem=34). There is obviously an upper bound because from some x on x*9! < 10^n -1 always holds, but do I need to supply that bound or will the compiler find it?

答:
```<code>[<function> x|x <- <someList>, <somePredicate>]
</code>```

should always evaluate to the same result as

```<code>filter (<somePredicate>) . map (<function>) $ <someList>
</code>```

However, there is no guarantee that this is how the compiler will actually do it. The section on list comprehensions in the Haskell Report only mentions what list comprehensions should do, not how they should work. So each compiler is free to do as its developers find best. Therefore, you should not assume anything about the performance of list comprehensions or that the compiler will do any optimizations.

Body:
I am wondering exactly how list-comprehensions are evaluated in Haskell. After reading this Removing syntactic sugar: List comprehension in Haskell and this: Haskell Lazy Evaluation and Reuse I still don't really understand if

[<function> x|x <- <someList>, <somePredicate>]

is actually exactly equivalent (not just in outcome but in evaluation) to

filter (<somePredicate>) . map (<function>) $ <someList>

and if so, does this mean it can potentially reduce time complexity drastically to build up the desired list with only desired elements?
Also, how does this work in terms of infinite lists? To be specific: I assume something like:

[x|x <- [1..], x < 20]

will be evaluated in finite time, but how "obvious" does the fact that there are no more elements above some value which satisfy the predicate need to be, for the compiler to consider it? Would

[x|x <- [1..], (sum.map factorial $ digits x) == x]

work (see project Euler problem 34 https://projecteuler.net/problem=34). There is obviously an upper bound because from some x on x*9! < 10^n -1 always holds, but do I need to supply that bound or will the compiler find it?

Body:
[<function> x|x <- <someList>, <somePredicate>]

should always evaluate to the same result as

filter (<somePredicate>) . map (<function>) $ <someList>

However, there is no guarantee that this is how the compiler will actually do it. The section on list comprehensions in the Haskell Report only mentions what list comprehensions should do, not how they should work. So each compiler is free to do as its developers find best. Therefore, you should not assume anything about the performance of list comprehensions or that the compiler will do any optimizations.