问:
Codeigniter SQL Query does not work. Its return false. But if we run the same query directly into database its returns result
```<code>  $this->db->select('sum(commission_amount) as myamount');
  $this->db->from('forex_commissions');
  $this->db->where('createdDate_sql = `2020-02-16`  and userRef = `MXkIP8z0vs5J`');
  $result = $this->db->get();
</code>```

Codeigniter SQL Query does not work. Its return false. But if we run the same query directly into database its returns result.

答:
You are not using ->db->where() properly. The correct codeigniter way of your example is:

```<code>  $this->db->select('sum(commission_amount) as myamount');
  $this->db->from('forex_commissions');
  $this->db->where('createdDate_sql', '2020-02-16');
  $this->db->where('userRef', 'MXkIP8z0vs5J');
  $result = $this->db->get();
</code>```

using this syntax also escapes all values automatically, producing safer queries. see here

note: you can also echo your latest query executed with: echo $this->db->last_query();

Body:
  $this->db->select('sum(commission_amount) as myamount');
  $this->db->from('forex_commissions');
  $this->db->where('createdDate_sql = `2020-02-16`  and userRef = `MXkIP8z0vs5J`');
  $result = $this->db->get();

Codeigniter SQL Query does not work. Its return false. But if we run the same query directly into database its returns result.

Body:
You are not using ->db->where() properly. The correct codeigniter way of your example is:

  $this->db->select('sum(commission_amount) as myamount');
  $this->db->from('forex_commissions');
  $this->db->where('createdDate_sql', '2020-02-16');
  $this->db->where('userRef', 'MXkIP8z0vs5J');
  $result = $this->db->get();

using this syntax also escapes all values automatically, producing safer queries. see here

note: you can also echo your latest query executed with: echo $this->db->last_query();


来源: stackexchange

问:
Add class when (...) with jQuery
I've this simple code:

HTML:

```<code><input type="text" name="name" class="required" />

<select id="options" name="options" size="1">
  <option value="0"> YES </option>
  <option value="1"> NO </option>
</select>

<input type="text" name="email" />
<input type="text" name="city" />
</code>```

Question:

I want to add class = "required"  to email and city fields when options is equal to 1

答:
I've added Id's to your input elements to make the selector quicker:

```<code> <input type="text" name="email" id="email" />
 <input type="text" name="city" id="city" />
</code>```

jQuery:

```<code>$("#options").change(function() {
    if ($(this).val() == "1") {
        $("#email, #city").addClass("required");
    }
    else {
        $("#email, #city").removeClass("required");
    }
});
</code>```

ContentLicense: CC BY-SA 3.0

Title: Add class when (...) with jQuery

Body:
I've this simple code:

HTML:

<input type="text" name="name" class="required" />

<select id="options" name="options" size="1">
  <option value="0"> YES </option>
  <option value="1"> NO </option>
</select>

<input type="text" name="email" />
<input type="text" name="city" />

Question:

I want to add class = "required"  to email and city fields when options is equal to 1

Body:
I've added Id's to your input elements to make the selector quicker:

 <input type="text" name="email" id="email" />
 <input type="text" name="city" id="city" />

jQuery:

$("#options").change(function() {
    if ($(this).val() == "1") {
        $("#email, #city").addClass("required");
    }
    else {
        $("#email, #city").removeClass("required");
    }
});


问:
How do I keep track of time before executing any function?
I have this service which calls Notification() function every second. What this function does is it checks the time difference between lastupdated and DateTime.UtcNow and check if the Countdiff is greater than 1

If the above conditions is true then it checks if the AppGuid is already in the DB if it's not it inserts it and calls DoAction() function which sends a notification. Everything works fine till here.

The problem starts when the AppGuid is already in DB.

What I need is if AppGuid is already in DB then send notification every 15 Min, here I'm struggling with keeping track last time I send notification, I tried to compare last timestamp when it got updated and tried to compare the difference but it keeps sending notifications resulting spamming the pipeline.

I tried to keep elasped time with stopwatch class but the problem it waits for 15 min when app starts initially to send anything.

How do I send notification only once every 15 min?

```<code>public class DataGetService : DelegatingHandler, IHostedService
{
    private Timer _timer;
    Stopwatch stopwatch = new Stopwatch();

    public Task StartAsync(CancellationToken cancellationToken)
    {
        stopwatch.Start();
        _timer = new Timer(Heartbeat, null, 1000, 1000);
        return Task.CompletedTask;
    }

    public Task StopAsync(CancellationToken cancellationToken)
    {
        //Timer does not have a stop.
        _timer?.Change(Timeout.Infinite, 0);
        return Task.CompletedTask;
    }

    public void Heartbeat(object state)
    {
        _ = Notifications(TimeSpan.FromMinutes(15));
    }

    public async Task Notifications(TimeSpan period)
    {
        // Result array have: LastUpdated, CountDiff, AppGuid, AppName, Timestamp
        foreach (var d in result)
        {
            var x = _DBcontext.TestEvents.FirstOrDefault(o => o.AppGuid == (Guid)d.AppGuid);

            if (DateTime.UtcNow - d.LastUpdated <= TimeSpan.FromMinutes(30) && d.CountDiff > 1)
            {
                if (x == null)
                {
                    DoAction();
                    _DBcontext.TestEvents.Add(new TestEvents
                    {
                        AppGuid = (Guid)d.AppGuid,
                        AppName = d.AppName,
                        Timestamp = DateTime.UtcNow
                    });
                }
                else
                {   //This is useful if app have crashed or was not able to update in db, it will update the current timestamp.
                    if (DateTime.UtcNow - x.Timestamp >= TimeSpan.FromMinutes(30))
                    {
                        x.Timestamp = DateTime.UtcNow;
                    }
                    else
                    {
                        if (stopwatch.Elapsed <= period)
                            return;

                        DoAction();
                        x.Timestamp = DateTime.UtcNow;
                    }
                }
            }
        }

        await _DBcontext.SaveChangesAsync();

        stopwatch.Restart();
    }
}
</code>```

答:
I'd do it the same way I would lock a user out for 15 minutes if they tried their password too many times: rather than trying to use a date field that tracks when a record was last updated, have a dedicated field for when the next event should be raised. At the time you send the first notification, set it to a future date and only send a new notification when the date falls into the past (whereupon you set the date future again)

By using a "last updated" field you risk another notification if something else about the record changes. If you feel that it's not relevant to put the date in the table concerned, consider having a table just for notification event dates; all it needs is a GUID and a date, and then it can function for any object ID in any table in the database (if the GUIDs are unique) - "no sending information about entity with GUID x until time y" is an easy thing to code for in that case and the eventing system doesnt need to know anything about the entity it is reporting on. You can make the subsystems naively raise events every second if they want to, but the sending of notifications can only happen every X minutes, so all the interim notifications are quenched. This simplifies the system raising the messages too; it can just raise them and not care for the logic of whether they should actually be notified or not

ContentLicense: CC BY-SA 4.0

Body:
I have this service which calls Notification() function every second. What this function does is it checks the time difference between lastupdated and DateTime.UtcNow and check if the Countdiff is greater than 1

If the above conditions is true then it checks if the AppGuid is already in the DB if it's not it inserts it and calls DoAction() function which sends a notification. Everything works fine till here.

The problem starts when the AppGuid is already in DB.

What I need is if AppGuid is already in DB then send notification every 15 Min, here I'm struggling with keeping track last time I send notification, I tried to compare last timestamp when it got updated and tried to compare the difference but it keeps sending notifications resulting spamming the pipeline.

I tried to keep elasped time with stopwatch class but the problem it waits for 15 min when app starts initially to send anything.

How do I send notification only once every 15 min?

public class DataGetService : DelegatingHandler, IHostedService
{
    private Timer _timer;
    Stopwatch stopwatch = new Stopwatch();

    public Task StartAsync(CancellationToken cancellationToken)
    {
        stopwatch.Start();
        _timer = new Timer(Heartbeat, null, 1000, 1000);
        return Task.CompletedTask;
    }

    public Task StopAsync(CancellationToken cancellationToken)
    {
        //Timer does not have a stop.
        _timer?.Change(Timeout.Infinite, 0);
        return Task.CompletedTask;
    }

    public void Heartbeat(object state)
    {
        _ = Notifications(TimeSpan.FromMinutes(15));
    }

    public async Task Notifications(TimeSpan period)
    {
        // Result array have: LastUpdated, CountDiff, AppGuid, AppName, Timestamp
        foreach (var d in result)
        {
            var x = _DBcontext.TestEvents.FirstOrDefault(o => o.AppGuid == (Guid)d.AppGuid);

            if (DateTime.UtcNow - d.LastUpdated <= TimeSpan.FromMinutes(30) && d.CountDiff > 1)
            {
                if (x == null)
                {
                    DoAction();
                    _DBcontext.TestEvents.Add(new TestEvents
                    {
                        AppGuid = (Guid)d.AppGuid,
                        AppName = d.AppName,
                        Timestamp = DateTime.UtcNow
                    });
                }
                else
                {   //This is useful if app have crashed or was not able to update in db, it will update the current timestamp.
                    if (DateTime.UtcNow - x.Timestamp >= TimeSpan.FromMinutes(30))
                    {
                        x.Timestamp = DateTime.UtcNow;
                    }
                    else
                    {
                        if (stopwatch.Elapsed <= period)
                            return;

                        DoAction();
                        x.Timestamp = DateTime.UtcNow;
                    }
                }
            }
        }

        await _DBcontext.SaveChangesAsync();

        stopwatch.Restart();
    }
}


问:
Migrating code to tensorflow 2.0 gives Invalid argument error: Default MaxPoolingOp only supports NHWC on device type CPU
I am migrating some code from Keras API + Tensorflow 1.14 to Tensorflow 2.0 and tf.keras. Where the code previously ran fine; now it throws the following error.

```<code>2020-02-18 21:38:17.678879: E tensorflow/core/common_runtime/executor.cc:642] Executor failed to create kernel. Invalid argument: Default MaxPoolingOp only supports NHWC on device type CPU
     [[{{node sequential/max_pooling2d/MaxPool}}]]
Traceback (most recent call last):
  File "C:/Users/ymeli/MEGA/Machine Learning/9. Deep learning for Python/5. Convolutional neural networks/CH19_Digits.py", line 70, in <module>
    model.fit(X_train, y_train, validation_data=(X_test, y_test), batch_size=200, epochs=10, verbose=2)
  File "C:\Users\ymeli\Anaconda3\envs\Anaconda3.7\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 728, in fit
    use_multiprocessing=use_multiprocessing)
  File "C:\Users\ymeli\Anaconda3\envs\Anaconda3.7\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 324, in fit
    total_epochs=epochs)
  File "C:\Users\ymeli\Anaconda3\envs\Anaconda3.7\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 123, in run_one_epoch
    batch_outs = execution_function(iterator)
  File "C:\Users\ymeli\Anaconda3\envs\Anaconda3.7\lib\site-packages\tensorflow_core\python\keras\engine\training_v2_utils.py", line 86, in execution_function
    distributed_function(input_fn))
  File "C:\Users\ymeli\Anaconda3\envs\Anaconda3.7\lib\site-packages\tensorflow_core\python\eager\def_function.py", line 457, in __call__
    result = self._call(*args, **kwds)
  File "C:\Users\ymeli\Anaconda3\envs\Anaconda3.7\lib\site-packages\tensorflow_core\python\eager\def_function.py", line 520, in _call
    return self._stateless_fn(*args, **kwds)
  File "C:\Users\ymeli\Anaconda3\envs\Anaconda3.7\lib\site-packages\tensorflow_core\python\eager\function.py", line 1823, in __call__
    return graph_function._filtered_call(args, kwargs)  # pylint: disable=protected-access
  File "C:\Users\ymeli\Anaconda3\envs\Anaconda3.7\lib\site-packages\tensorflow_core\python\eager\function.py", line 1141, in _filtered_call
    self.captured_inputs)
  File "C:\Users\ymeli\Anaconda3\envs\Anaconda3.7\lib\site-packages\tensorflow_core\python\eager\function.py", line 1224, in _call_flat
    ctx, args, cancellation_manager=cancellation_manager)
  File "C:\Users\ymeli\Anaconda3\envs\Anaconda3.7\lib\site-packages\tensorflow_core\python\eager\function.py", line 511, in call
    ctx=ctx)
  File "C:\Users\ymeli\Anaconda3\envs\Anaconda3.7\lib\site-packages\tensorflow_core\python\eager\execute.py", line 67, in quick_execute
    six.raise_from(core._status_to_exception(e.code, message), None)
  File "<string>", line 3, in raise_from
tensorflow.python.framework.errors_impl.InvalidArgumentError:  Default MaxPoolingOp only supports NHWC on device type CPU
     [[node sequential/max_pooling2d/MaxPool (defined at \Users\ymeli\Anaconda3\envs\Anaconda3.7\lib\site-packages\tensorflow_core\python\framework\ops.py:1751) ]] [Op:__inference_distributed_function_1139]

Function call stack:
distributed_function

Process finished with exit code 1
</code>```

The script I am running is relatively simple. It is a CNN classifying digits. My CPU is an AMD ryzen 9 3900X.

```<code>from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Convolution2D
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras.utils import to_categorical
from tensorflow.keras import backend as K

K.set_image_data_format("channels_first")

# Load Data
(X_train, y_train), (X_test, y_test) = mnist.load_data()

# Reshape to be [# of samples][channels][width][height]
X_train = X_train.reshape(X_train.shape[0], 1, 28, 28).astype('float32')
X_test = X_test.reshape(X_test.shape[0], 1, 28, 28).astype('float32')

# normalize inputs from 0-255 to 0-1
X_train = X_train / 255
X_test = X_test / 255

# One hot encode outputs
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
num_classes = y_test.shape[1]

# define baseline model
def baseline_model():
    model = Sequential()
    model.add(Convolution2D(30, kernel_size=(5, 5), kernel_initializer='normal', padding='valid', input_shape=(1, 28, 28), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(
        Convolution2D(15, kernel_size=(3, 3), kernel_initializer='normal', padding='valid', input_shape=(1, 28, 28)))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.2))
    model.add(Flatten())
    model.add(Dense(128, activation='relu'))
    model.add(Dense(50, activation='relu'))
    model.add(Dense(num_classes, activation='softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model

model = baseline_model()
model.fit(X_train, y_train, validation_data=(X_test, y_test), batch_size=200, epochs=10, verbose=2)
scores = model.evaluate(X_test, y_test, verbose=2)
print("Large CNN Error: {}%".format(100 - scores[1] * 100))
</code>```

答:
I managed to solve the issue myself. The problem was that this new version of tensorflow only supports NHCW format on CPU (NHCW standing for Number of examples, Height, Width, Channels). So by changing

```<code>K.set_image_data_format("channels_first")
</code>```

to

```<code>K.set_image_data_format("channels_last")
</code>```

and

```<code>X_train = X_train.reshape(X_train.shape[0],1, 28, 28).astype('float32')
X_test = X_test.reshape(X_test.shape[0],1, 28, 28).astype('float32')
</code>```

to

```<code>X_train = X_train.reshape(X_train.shape[0] 28, 28, 1).astype('float32')
X_test = X_test.reshape(X_test.shape[0],1, 28, 28, 1).astype('float32')
</code>```

and lastly

```<code>model.add(Convolution2D(30, kernel_size=(5, 5), kernel_initializer='normal', padding='valid', input_shape=(1, 28, 28),activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(15, kernel_size=(3, 3), kernel_initializer='normal', padding='valid', input_shape=(1, 28, 28)))
</code>```

to

```<code>model.add(Convolution2D(30, kernel_size=(5, 5), kernel_initializer='normal', padding='valid', input_shape=(28, 28, 1),activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(15, kernel_size=(3, 3), kernel_initializer='normal', padding='valid', input_shape=(28, 28, 1)))
</code>```

The dimensions are now correct again, and the problem is solved. Everything works fine once more =)

Body:
I am migrating some code from Keras API + Tensorflow 1.14 to Tensorflow 2.0 and tf.keras. Where the code previously ran fine; now it throws the following error.

2020-02-18 21:38:17.678879: E tensorflow/core/common_runtime/executor.cc:642] Executor failed to create kernel. Invalid argument: Default MaxPoolingOp only supports NHWC on device type CPU
     [[{{node sequential/max_pooling2d/MaxPool}}]]
Traceback (most recent call last):
  File "C:/Users/ymeli/MEGA/Machine Learning/9. Deep learning for Python/5. Convolutional neural networks/CH19_Digits.py", line 70, in <module>
    model.fit(X_train, y_train, validation_data=(X_test, y_test), batch_size=200, epochs=10, verbose=2)
  File "C:\Users\ymeli\Anaconda3\envs\Anaconda3.7\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 728, in fit
    use_multiprocessing=use_multiprocessing)
  File "C:\Users\ymeli\Anaconda3\envs\Anaconda3.7\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 324, in fit
    total_epochs=epochs)
  File "C:\Users\ymeli\Anaconda3\envs\Anaconda3.7\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 123, in run_one_epoch
    batch_outs = execution_function(iterator)
  File "C:\Users\ymeli\Anaconda3\envs\Anaconda3.7\lib\site-packages\tensorflow_core\python\keras\engine\training_v2_utils.py", line 86, in execution_function
    distributed_function(input_fn))
  File "C:\Users\ymeli\Anaconda3\envs\Anaconda3.7\lib\site-packages\tensorflow_core\python\eager\def_function.py", line 457, in __call__
    result = self._call(*args, **kwds)
  File "C:\Users\ymeli\Anaconda3\envs\Anaconda3.7\lib\site-packages\tensorflow_core\python\eager\def_function.py", line 520, in _call
    return self._stateless_fn(*args, **kwds)
  File "C:\Users\ymeli\Anaconda3\envs\Anaconda3.7\lib\site-packages\tensorflow_core\python\eager\function.py", line 1823, in __call__
    return graph_function._filtered_call(args, kwargs)  # pylint: disable=protected-access
  File "C:\Users\ymeli\Anaconda3\envs\Anaconda3.7\lib\site-packages\tensorflow_core\python\eager\function.py", line 1141, in _filtered_call
    self.captured_inputs)
  File "C:\Users\ymeli\Anaconda3\envs\Anaconda3.7\lib\site-packages\tensorflow_core\python\eager\function.py", line 1224, in _call_flat
    ctx, args, cancellation_manager=cancellation_manager)
  File "C:\Users\ymeli\Anaconda3\envs\Anaconda3.7\lib\site-packages\tensorflow_core\python\eager\function.py", line 511, in call
    ctx=ctx)
  File "C:\Users\ymeli\Anaconda3\envs\Anaconda3.7\lib\site-packages\tensorflow_core\python\eager\execute.py", line 67, in quick_execute
    six.raise_from(core._status_to_exception(e.code, message), None)
  File "<string>", line 3, in raise_from
tensorflow.python.framework.errors_impl.InvalidArgumentError:  Default MaxPoolingOp only supports NHWC on device type CPU
     [[node sequential/max_pooling2d/MaxPool (defined at \Users\ymeli\Anaconda3\envs\Anaconda3.7\lib\site-packages\tensorflow_core\python\framework\ops.py:1751) ]] [Op:__inference_distributed_function_1139]

Function call stack:
distributed_function

Process finished with exit code 1

The script I am running is relatively simple. It is a CNN classifying digits. My CPU is an AMD ryzen 9 3900X.

from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Convolution2D
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras.utils import to_categorical
from tensorflow.keras import backend as K

K.set_image_data_format("channels_first")

# Load Data
(X_train, y_train), (X_test, y_test) = mnist.load_data()

# Reshape to be [# of samples][channels][width][height]
X_train = X_train.reshape(X_train.shape[0], 1, 28, 28).astype('float32')
X_test = X_test.reshape(X_test.shape[0], 1, 28, 28).astype('float32')

# normalize inputs from 0-255 to 0-1
X_train = X_train / 255
X_test = X_test / 255

# One hot encode outputs
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
num_classes = y_test.shape[1]

# define baseline model
def baseline_model():
    model = Sequential()
    model.add(Convolution2D(30, kernel_size=(5, 5), kernel_initializer='normal', padding='valid', input_shape=(1, 28, 28), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(
        Convolution2D(15, kernel_size=(3, 3), kernel_initializer='normal', padding='valid', input_shape=(1, 28, 28)))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.2))
    model.add(Flatten())
    model.add(Dense(128, activation='relu'))
    model.add(Dense(50, activation='relu'))
    model.add(Dense(num_classes, activation='softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model

model = baseline_model()
model.fit(X_train, y_train, validation_data=(X_test, y_test), batch_size=200, epochs=10, verbose=2)
scores = model.evaluate(X_test, y_test, verbose=2)
print("Large CNN Error: {}%".format(100 - scores[1] * 100))

Body:
I managed to solve the issue myself. The problem was that this new version of tensorflow only supports NHCW format on CPU (NHCW standing for Number of examples, Height, Width, Channels). So by changing

K.set_image_data_format("channels_first")

to

K.set_image_data_format("channels_last")

and

X_train = X_train.reshape(X_train.shape[0],1, 28, 28).astype('float32')
X_test = X_test.reshape(X_test.shape[0],1, 28, 28).astype('float32')

to

X_train = X_train.reshape(X_train.shape[0] 28, 28, 1).astype('float32')
X_test = X_test.reshape(X_test.shape[0],1, 28, 28, 1).astype('float32')

and lastly

model.add(Convolution2D(30, kernel_size=(5, 5), kernel_initializer='normal', padding='valid', input_shape=(1, 28, 28),activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(15, kernel_size=(3, 3), kernel_initializer='normal', padding='valid', input_shape=(1, 28, 28)))

to

model.add(Convolution2D(30, kernel_size=(5, 5), kernel_initializer='normal', padding='valid', input_shape=(28, 28, 1),activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(15, kernel_size=(3, 3), kernel_initializer='normal', padding='valid', input_shape=(28, 28, 1)))

The dimensions are now correct again, and the problem is solved. Everything works fine once more =)


问:
How to retry fetch in a loop when it throws an error
I have the following JS function:

```<code>func() {
  return fetch({
    ...
  }).then({
    ...
  })catch({
    ...
  })
}
</code>```

In it I return a promise returned by fetch(). In the event that it fails (ie calls catch() block) I want to repeat the whole thing. Something like having the whole thing in a while (true) loop, but I can't figure out how to do this with promises.

Any suggestions?

答:
you should have a close look to promises and async await.

```<code>async function fetchUntilSucceeded() {
  let success = false;
  while(!success) {
    try {
      let result = await fetch(...);
      success = true;
      //do your stuff with your result here
    } catch {
      //do your catch stuff here
    }
  }
}
</code>```

If you just need the results:

```<code>async function fetchUntilSucceeded() {
  while(true) {
    try {
      return await fetch(...);
    }
  }
}
</code>```

But be careful with such code as it might never resolve! also it can send a lot of requests without any waittime in between.

Body:
I have the following JS function:

func() {
  return fetch({
    ...
  }).then({
    ...
  })catch({
    ...
  })
}

In it I return a promise returned by fetch(). In the event that it fails (ie calls catch() block) I want to repeat the whole thing. Something like having the whole thing in a while (true) loop, but I can't figure out how to do this with promises.

Any suggestions?

Body:
you should have a close look to promises and async await.

async function fetchUntilSucceeded() {
  let success = false;
  while(!success) {
    try {
      let result = await fetch(...);
      success = true;
      //do your stuff with your result here
    } catch {
      //do your catch stuff here
    }
  }
}

If you just need the results:

async function fetchUntilSucceeded() {
  while(true) {
    try {
      return await fetch(...);
    }
  }
}

But be careful with such code as it might never resolve! also it can send a lot of requests without any waittime in between.


问:
Geometry has too many edges (Google Earth Engine). Help me

I have problem during working in Google Earth Engine. I was processing some vector files. And i am getting below code:

答:
The geometry has too many vertices. You can try to simplify it using:

// Get a feature collection and subset the first feature.
var feature = ee.FeatureCollection("TIGER/2018/States").first();

// Simplify the feature - think of max error as resolution.
// Setting to 100 means that the geometry is accurate to
// within 100 meters, for example.
var featureSimple = ee.Feature(feature).simplify({maxError: 100});
```

<p>or for a <code>ee.FeatureCollection</code>:</p>

<pre class="lang-js prettyprint-override"><code>// Get a feature collection.
var featureCol = ee.FeatureCollection("TIGER/2018/States");

// Simplify each feature in the collection, by mapping the
// .simplify() function over it.
var simplifiedCol = featureCol.map(function(feature) {
  return feature.simplify({maxError: 100});
});
</code>```

Body:
The geometry has too many vertices. You can try to simplify it using:

// Get a feature collection and subset the first feature.
var feature = ee.FeatureCollection("TIGER/2018/States").first();

// Simplify the feature - think of max error as resolution.
// Setting to 100 means that the geometry is accurate to
// within 100 meters, for example.
var featureSimple = ee.Feature(feature).simplify({maxError: 100});

or for a ee.FeatureCollection:

// Get a feature collection.
var featureCol = ee.FeatureCollection("TIGER/2018/States");

// Simplify each feature in the collection, by mapping the
// .simplify() function over it.
var simplifiedCol = featureCol.map(function(feature) {
  return feature.simplify({maxError: 100});
});


问:
How to install Ruby on docker?
I am trying to install ruby on docker. I could install the 1.9 versions but it is not possible to install the latest version such as 2.2.0 and above. I am actually trying to set up calabash on docker. Have tried this. Whenever I try to install calabash-android in it getting the error </p>

```<code>ERROR:  Error installing calabash-android:
luffa requires Ruby version >= 2.0.
</code>```

答:
Low reputation so I can't comment inline (all those years of lurking, sigh), but in case anyone else happens across this while searching for ways to install old ruby versions to docker, I found @grosser's answer very helpful - it worked where trying to install via RVM simply wouldn't, at least for me.

I would, however, recommend using the recommended approach for installing ruby-build - the following worked for me:

```<code><prior steps>
RUN git clone https://github.com/rbenv/ruby-build.git && \
  PREFIX=/usr/local ./ruby-build/install.sh && \
  ruby-build -v 2.4.1 /usr/local && \
  gem install bundler -v <VERSION HERE> --no-ri --no-rdoc && bundle install
<following steps>
</code>```

Key point here is that this keeps you up to date with ruby-build instead of being hard-coded to the 2018-03-29 version as in a previous @grosser's comment.

Title: How to install Ruby on docker?

Body:
I am trying to install ruby on docker. I could install the 1.9 versions but it is not possible to install the latest version such as 2.2.0 and above. I am actually trying to set up calabash on docker. Have tried this. Whenever I try to install calabash-android in it getting the error

ERROR:  Error installing calabash-android:
luffa requires Ruby version >= 2.0.

Body:
Low reputation so I can't comment inline (all those years of lurking, sigh), but in case anyone else happens across this while searching for ways to install old ruby versions to docker, I found @grosser's answer very helpful - it worked where trying to install via RVM simply wouldn't, at least for me.

I would, however, recommend using the recommended approach for installing ruby-build - the following worked for me:

<prior steps>
RUN git clone https://github.com/rbenv/ruby-build.git && \
  PREFIX=/usr/local ./ruby-build/install.sh && \
  ruby-build -v 2.4.1 /usr/local && \
  gem install bundler -v <VERSION HERE> --no-ri --no-rdoc && bundle install
<following steps>

Key point here is that this keeps you up to date with ruby-build instead of being hard-coded to the 2018-03-29 version as in a previous @grosser's comment.


问:
Skip top N lines in snowflake load
My actual data in csv extracts starts from line 10. How can I skip top few lines in snowflake load using copy or any other utility. Do we have anything similar to SKIP_HEADER ?

I have files on S3 and its my stage. I would be creating a snowpipe later on this datasource.

答:
yes there is a skip_header option for CSV, allowing you to skip a specified number of rows, when defining a file format. Please have a look here:

https://docs.snowflake.net/manuals/sql-reference/sql/create-file-format.html#type-csv

So you create a file format associated with the csv files you have in mind and then use this when calling the copy commands.

Title: Skip top N lines in snowflake load


问:
Javascript date formats customizing
I'm trying to test an API to check if the dates are present in the comment. The date is given in ISO format in the comment like 2020-02-18 21:30:13

When I compare with the Date() and convert it to ISO format, the format is slightly different from my date which makes my test to fail. How do I make the format the same as the one is my API response?

Below is my code:

```<code>  var dateobj = new Date();
var B = dateobj.toISOString();
pm.test("Comment has Date", function (){
    pm.expect(responseBody.split("*/")[0]).to.include(B)
})
</code>```

答:
Something Like This?

Javascript is not very flexible with Dates. But I think creating a formatting function shouldn't be a problem at all, try this:

var date = new Date();

var formattedDate = (date)=>{
  return (`${date.getFullYear()}-${date.getMonth()}-${date.getDay()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`);
}

alert(formattedDate(date));```

Title: Javascript date formats customizing

Body:
I'm trying to test an API to check if the dates are present in the comment. The date is given in ISO format in the comment like 2020-02-18 21:30:13

When I compare with the Date() and convert it to ISO format, the format is slightly different from my date which makes my test to fail. How do I make the format the same as the one is my API response?

Below is my code:

  var dateobj = new Date();
var B = dateobj.toISOString();
pm.test("Comment has Date", function (){
    pm.expect(responseBody.split("*/")[0]).to.include(B)
})


问:
Is there an R function that will pull the day of year from POSIXct?
I have a data table that looks roughly like the following:

```<code>Date_Time               Water_Level        Date
6-28-2019 15:00:00         184             6-28-2019
6-28-2019 15:15:00         186             6-28-2019
6-28-2019 15:30:00         180             6-28-2019
6-29-2019 10:15:00         179             6-29-2019
6-29-2019 10:30:00         188             6-29-2019
6-29-2019 10:45:00         190             6-29-2019
</code>```

I'd like to be able to use the aggregate function to find the daily average water level for the entire data set so it looks something like this:

```<code>Date_Time       Water_Level
6-28-2019          183.3
6-29-2019          185.6
</code>```

I used the following code, but for some reason it only aggregated SOME of the data, not all of it. If I switch the Date_Time column out for the Date column (second code below), it does the job, but the dates are out of order resulting in me needing to run a line to reorder them...

```<code>LSUP1<-aggregate(Water_Level_m~Date_Time, LSUP, mean)
LSUP1<-LSUP1[order(as.Date(LSUP1$Date, format="%m/%d/%Y")),]
</code>```

I'm also having some trouble extracting the "day of year" (DOY) from the POSIXct format of the Date_Time column. I aggregated some similar data based on DOY and that worked swimmingly...

Any suggestions would be lovely, even if it's a different function.

答:
Sure:

```<code>R> now
[1] "2020-02-18 15:58:10.226131 CST"
R> class(now)
[1] "POSIXct" "POSIXt"
R> unclass(as.POSIXlt(now))
$sec
[1] 10.2261

$min
[1] 58

$hour
[1] 15

$mday
[1] 18

$mon
[1] 1

$year
[1] 120

$wday
[1] 2

$yday
[1] 48

$isdst
[1] 0

$zone
[1] "CST"

$gmtoff
[1] -21600

attr(,"tzone")
[1] "America/Chicago" "CST"             "CDT"
R>
</code>```

The preferred way, though, is through the standard time formatter functions.  Day of the year is %j per help(sptrftime):

```<code>R> format(now, "%j")
[1] "049"
R>
</code>```

These functions are all vectorized, and it is easy to call as.integer() or as numeric on them:

```<code>R> as.integer(format(as.Date(now) + cumsum(runif(5)*20), "%j"))
[1] 61 65 84 96 99
R>
</code>```

Body:
I have a data table that looks roughly like the following:

Date_Time               Water_Level        Date
6-28-2019 15:00:00         184             6-28-2019
6-28-2019 15:15:00         186             6-28-2019
6-28-2019 15:30:00         180             6-28-2019
6-29-2019 10:15:00         179             6-29-2019
6-29-2019 10:30:00         188             6-29-2019
6-29-2019 10:45:00         190             6-29-2019

I'd like to be able to use the aggregate function to find the daily average water level for the entire data set so it looks something like this:

Date_Time       Water_Level
6-28-2019          183.3
6-29-2019          185.6

I used the following code, but for some reason it only aggregated SOME of the data, not all of it. If I switch the Date_Time column out for the Date column (second code below), it does the job, but the dates are out of order resulting in me needing to run a line to reorder them...

LSUP1<-aggregate(Water_Level_m~Date_Time, LSUP, mean)
LSUP1<-LSUP1[order(as.Date(LSUP1$Date, format="%m/%d/%Y")),]

I'm also having some trouble extracting the "day of year" (DOY) from the POSIXct format of the Date_Time column. I aggregated some similar data based on DOY and that worked swimmingly...

Any suggestions would be lovely, even if it's a different function.

Body:
Sure:

R> now
[1] "2020-02-18 15:58:10.226131 CST"
R> class(now)
[1] "POSIXct" "POSIXt"
R> unclass(as.POSIXlt(now))
$sec
[1] 10.2261

$min
[1] 58

$hour
[1] 15

$mday
[1] 18

$mon
[1] 1

$year
[1] 120

$wday
[1] 2

$yday
[1] 48

$isdst
[1] 0

$zone
[1] "CST"

$gmtoff
[1] -21600

attr(,"tzone")
[1] "America/Chicago" "CST"             "CDT"
R>

The preferred way, though, is through the standard time formatter functions.  Day of the year is %j per help(sptrftime):

R> format(now, "%j")
[1] "049"
R>

These functions are all vectorized, and it is easy to call as.integer() or as numeric on them:

R> as.integer(format(as.Date(now) + cumsum(runif(5)*20), "%j"))
[1] 61 65 84 96 99
R>


问:
How do I get statistics on Azure Devops usage?
I am currently administering a project on DevOps and wish to verify how many users are using the platform actively.

I want the following information -

```<code>        1. Unique visitors per month
        2. All visits per month
        3. All visits by roles
</code>```

答: The best you're going to be able to get is from the usage page (https://dev.azure.com/your_organization/_settings/usage) or audit REST API. It doesn't include everything you want, but you can probably play with the data once you have it to aggregate it into a format that's more to your liking and extract some of the information you're after.</p>

Body:
I am currently administering a project on DevOps and wish to verify how many users are using the platform actively.

I want the following information -

        1. Unique visitors per month
        2. All visits per month
        3. All visits by roles


问:
Testing javascript that updates DOM
I am new to web testing and would like to know how can I test javascript that reads and updates the DOM.

```<code>function otherFunction(string) {
// do some processing and return processed data
}

function domUpdater() {
  var string = document.getElementById("input").value;
  document.getElementById("output").innerHTML = otherFunction(string);
}
</code>```

I can test easily the otherFunction which accepts and input and returns an output.

```<code>expect(otherFunction("input1")).to.be("expected1");
expect(otherFunction("input2")).to.be("expected2");
</code>```

I am not clear on how can I test the domUpdater function which modifies the DOM?

答:
I usually use jsdom, and in my case since I'm using mocha it would look like this (using jsdom-mocha):

```<code>var jsdom = require('mocha-jsdom')
var expect = require('chai').expect

describe('mocha tests', function () {

  jsdom()

  it('has document', function () {
    var div = document.createElement('div')
    expect(div.nodeName).eql('DIV')
  })

})
</code>```

However if you can afford it (the tests tend to take a bit longer to run) i would recommend looking into running your tests in a headless browser, using something like Puppeteer.

A quick google search yielded this for how to run jest with puppeteer:

First install jest-puppeteer

```<code>yarn add --dev jest-puppeteer
</code>```

Specify preset in your Jest configuration:

```<code>{
 "preset": "jest-puppeteer"
}
</code>```

Write your test

describe('Google', () => {
  beforeAll(async () => {
    await page.goto('https://google.com');
  });

  it('should be titled "Google"', async () => {
    await expect(page.title()).resolves.toMatch('Google');
  });
});
```

Title: Testing javascript that updates DOM

Body:
I am new to web testing and would like to know how can I test javascript that reads and updates the DOM.

function otherFunction(string) {
// do some processing and return processed data
}

function domUpdater() {
  var string = document.getElementById("input").value;
  document.getElementById("output").innerHTML = otherFunction(string);
}

I can test easily the otherFunction which accepts and input and returns an output.

expect(otherFunction("input1")).to.be("expected1");
expect(otherFunction("input2")).to.be("expected2");

I am not clear on how can I test the domUpdater function which modifies the DOM?

Body:
I usually use jsdom, and in my case since I'm using mocha it would look like this (using jsdom-mocha):

var jsdom = require('mocha-jsdom')
var expect = require('chai').expect

describe('mocha tests', function () {

  jsdom()

  it('has document', function () {
    var div = document.createElement('div')
    expect(div.nodeName).eql('DIV')
  })

})

However if you can afford it (the tests tend to take a bit longer to run) i would recommend looking into running your tests in a headless browser, using something like Puppeteer.

A quick google search yielded this for how to run jest with puppeteer:

First install jest-puppeteer

yarn add --dev jest-puppeteer

Specify preset in your Jest configuration:

{
 "preset": "jest-puppeteer"
}

Write your test

describe('Google', () => {
  beforeAll(async () => {
    await page.goto('https://google.com');
  });

  it('should be titled "Google"', async () => {
    await expect(page.title()).resolves.toMatch('Google');
  });
});


问:
Is there a way I can plot this density graph
I am trying to plot a density chart for two lifts, small and a large and they hold the data of the median weight of 5 people in the small lift, and median weight of 10 people in the large lift, however, I am confused on how to plot the density chart so i can compare the median weight of both the lifts

The dataset that I am using is here;

500 Person Gender-Height-Weight-Body Mass Index

Height and Weight random generated, Body Mass Index Calculated

However, i deleted the height and the index as that is irrelevant for my research

I created a two samples which are here;

```<code>  Large <- replicate(n=1000, mean(sample(Weight$Weight, size = 10)))
    Small <- replicate(n=1000, mean(sample(Weight$Weight, size = 10)))
</code>```

and then I put them into one dataframe called Lifts;

```<code>  Lifts<-data.frame(Large, Small)
</code>```

I have tried to come up with a solution myself by plotting the following density chart;

```<code>ggplot(Lifts, aes(Large)) + geom_density(fill = "blue") + labs(x = "Median of Weight", y =     "Distribution of Data")
</code>```

Any help would be appreciated

答:
If you'd like two groups to compare between you can use pivot_longer to make them, and then set a grouping aesthetic in your plot

```<code>library(ggplot)
library(tidyr)

Large <- replicate(n=1000, mean(sample(Weight$Weight, size = 10)))
Small <- replicate(n=1000, mean(sample(Weight$Weight, size = 10)))
Lifts<-data.frame(Large, Small)

Lifts_long <- pivot_longer(Lifts, cols = c(Small, Large), names_to = "name", values_to = "value")

ggplot(Lifts_long, aes(value)) +
  geom_density(aes(group = name, fill = name), alpha = 0.5) +
  labs(x = "Median of Weight",
       y = "Distribution of Data",
       fill = "Group Name")
</code>```

Body:
I am trying to plot a density chart for two lifts, small and a large and they hold the data of the median weight of 5 people in the small lift, and median weight of 10 people in the large lift, however, I am confused on how to plot the density chart so i can compare the median weight of both the lifts

The dataset that I am using is here;

500 Person Gender-Height-Weight-Body Mass Index

Height and Weight random generated, Body Mass Index Calculated

However, i deleted the height and the index as that is irrelevant for my research

I created a two samples which are here;

  Large <- replicate(n=1000, mean(sample(Weight$Weight, size = 10)))
    Small <- replicate(n=1000, mean(sample(Weight$Weight, size = 10)))

and then I put them into one dataframe called Lifts;

  Lifts<-data.frame(Large, Small)

I have tried to come up with a solution myself by plotting the following density chart;

ggplot(Lifts, aes(Large)) + geom_density(fill = "blue") + labs(x = "Median of Weight", y =     "Distribution of Data")

Any help would be appreciated

Body:
If you'd like two groups to compare between you can use pivot_longer to make them, and then set a grouping aesthetic in your plot

library(ggplot)
library(tidyr)

Large <- replicate(n=1000, mean(sample(Weight$Weight, size = 10)))
Small <- replicate(n=1000, mean(sample(Weight$Weight, size = 10)))
Lifts<-data.frame(Large, Small)

Lifts_long <- pivot_longer(Lifts, cols = c(Small, Large), names_to = "name", values_to = "value")

ggplot(Lifts_long, aes(value)) +
  geom_density(aes(group = name, fill = name), alpha = 0.5) +
  labs(x = "Median of Weight",
       y = "Distribution of Data",
       fill = "Group Name")


问:
How to freeze a layer's weights for some number of iterations?
I'm trying to train two MLP's jointly, each one to predict a different real-valued variable. I want to minimize a loss over these two outputs, but I want to fix one of them for some number of "warm-up" iterations.

I'm new to tensorflow, but basically I'm looking for the equivalent of something like this in Pytorch:

```<code>def loss(self, *args, **kwargs) -> torch.Tensor:
        # Extract data
        data, target, probability = args

        # Iterate through each model and sum nll
        nll = []
        for index in range(self.num_models):
            # Extract mean and variance from prediction
            if self._current_it < self.warm_start_it:
                predictive_mean = self.mean[index](data)
                with torch.no_grad():
                    predictive_variance = softplus(self.variance[index](data))
            else:
                with torch.no_grad():
                    predictive_mean = self.mean[index](data)
                predictive_variance = softplus(self.variance[index](data))

            # Calculate the loss
            nll.append(self.calculate_nll(target, predictive_mean, predictive_variance))

        mean_nll = torch.stack(nll).mean()

        # Update current iteration
        if self.training:
            self._current_it += 1

        return mean_nll
</code>```

I'm thinking I can do something similar inside my model's call() function, i.e.:

```<code>    def call(self, step, inputs, training=None, mask=None):

        if step < self.warmup:
            with tf.GradientTape() as t:
                mean_predictions = self.mean(inputs)
            var_predictions = self.variance(inputs)
        else:
            mean_predictions = self.mean(inputs)
            with tf.GradientTape() as t:
                var_predictions = self.variance(inputs)
        return mean_predictions, var_predictions
</code>```

Is this the correct way of getting the above Pytorch equivalent?

答:
I ended up doing the following:

In a main loop,

```<code>mlp = UncertaintyMLP(805, 1)
    loss_fn = GaussianNLL()
    optimizer = tf.keras.optimizers.Adam(learning_rate=1e-3)
    epochs = 1000
    for epoch in range(epochs):
        for step, (x_batch, y_batch) in enumerate(train_dataset):

            if epoch > mlp.warmup:
                for layer in mlp.mean.layers:
                    layer.trainable = False
                for layer in mlp.variance.layers:
                    layer.trainable = True
            with tf.GradientTape() as tape:
                output = mlp(step, x_batch)
                loss = loss_fn(y_batch, output)
            grads = tape.gradient(loss, mlp.trainable_weights)
            optimizer.apply_gradients(zip(grads, mlp.trainable_weights))
</code>```

and in the model class:

```<code>def call(self, step, inputs, training=None, mask=None):
    mean_predictions = self.mean(inputs)
    var_predictions = tf.math.softplus(self.variance(inputs)
    return mean_predictions, var_predictions
</code>```

However I'm still curious to see what, if any, would be Tensorflow's equivalent of Pytorch's torch.no_grad().

Body:
I'm trying to train two MLP's jointly, each one to predict a different real-valued variable. I want to minimize a loss over these two outputs, but I want to fix one of them for some number of "warm-up" iterations.

I'm new to tensorflow, but basically I'm looking for the equivalent of something like this in Pytorch:

def loss(self, *args, **kwargs) -> torch.Tensor:
        # Extract data
        data, target, probability = args

        # Iterate through each model and sum nll
        nll = []
        for index in range(self.num_models):
            # Extract mean and variance from prediction
            if self._current_it < self.warm_start_it:
                predictive_mean = self.mean[index](data)
                with torch.no_grad():
                    predictive_variance = softplus(self.variance[index](data))
            else:
                with torch.no_grad():
                    predictive_mean = self.mean[index](data)
                predictive_variance = softplus(self.variance[index](data))

            # Calculate the loss
            nll.append(self.calculate_nll(target, predictive_mean, predictive_variance))

        mean_nll = torch.stack(nll).mean()

        # Update current iteration
        if self.training:
            self._current_it += 1

        return mean_nll

I'm thinking I can do something similar inside my model's call() function, i.e.:

    def call(self, step, inputs, training=None, mask=None):

        if step < self.warmup:
            with tf.GradientTape() as t:
                mean_predictions = self.mean(inputs)
            var_predictions = self.variance(inputs)
        else:
            mean_predictions = self.mean(inputs)
            with tf.GradientTape() as t:
                var_predictions = self.variance(inputs)
        return mean_predictions, var_predictions

Is this the correct way of getting the above Pytorch equivalent?

Body:
I ended up doing the following:

In a main loop,

mlp = UncertaintyMLP(805, 1)
    loss_fn = GaussianNLL()
    optimizer = tf.keras.optimizers.Adam(learning_rate=1e-3)
    epochs = 1000
    for epoch in range(epochs):
        for step, (x_batch, y_batch) in enumerate(train_dataset):

            if epoch > mlp.warmup:
                for layer in mlp.mean.layers:
                    layer.trainable = False
                for layer in mlp.variance.layers:
                    layer.trainable = True
            with tf.GradientTape() as tape:
                output = mlp(step, x_batch)
                loss = loss_fn(y_batch, output)
            grads = tape.gradient(loss, mlp.trainable_weights)
            optimizer.apply_gradients(zip(grads, mlp.trainable_weights))

and in the model class:

def call(self, step, inputs, training=None, mask=None):
    mean_predictions = self.mean(inputs)
    var_predictions = tf.math.softplus(self.variance(inputs)
    return mean_predictions, var_predictions

However I'm still curious to see what, if any, would be Tensorflow's equivalent of Pytorch's torch.no_grad().


问:
How to implement Python's multiprocessing Pool to convert thousands of files
I have a Python script that reads in a directory containing over 10,000 DBF files so they can be converted to CSV. I would like to parellelize this task rather than converting each file individually. I have read up on Python's multiprocessing module, though I am having a little bit of trouble implementing it for this task. Specifically, I wanted to use the Pool class to distribute the workload across the CPU cores.

Here is my code thus far:

```<code>import os
from dbfread import DBF
import pandas as pd
import multiprocessing

directory = 'C:\\Path_to_DBF_Files' #define file directory

files_in = os.listdir(directory) #store files in directory to list

def convert():

    for file in files_in:

        if file.startswith('D') and file.endswith('.DBF'): #define parameters of filenames to convert
            file_path = os.path.join(files_in, file)
            print(f'\nReading in {file}...')
            dbf = DBF(file_path) #create DBF object
            dbf.encoding = 'utf-8' #set encoding attribute to utf-8 instead of acsii
            dbf.char_decode_errors = 'ignore' #set decoding errors attribute to ignore any errors and read in DBF file as is
            print('\nConverting to DataFrame...')
            df = pd.DataFrame(iter(dbf)) #convert to Pandas dataframe
            df.columns.astype(str) #convert column datatypes to string
            print(df)
            print('\nWriting to CSV...')
            dest_directory = 'C:\\Path_to_output_directory\\%s.csv' % ('D' + file.strip('.DBF')) #define destination directory and names for output files
            df.to_csv(dest_directory, index = False)
            print(f'\nConverted {file} to CSV. Moving to next file...')

        elif file.startswith('B') and file.endswith('.DBF'): #define parameters for unnecessary files
            print('\nB file not needed.')
            continue

        elif file.endswith('.FPT'): #skip FPT files
            print('Skipping FPT file.')
            continue

        elif file.startswith('ID') and file.endswith('.DB~'): #stop iteration when this file is reached in the directory
            print('All files converted to CSV.')
            break

        else:
            print('\nFile not found or error.')
            print(f'Last file read in was {file}.')

pool = multiprocessing.Pool(processes = len(in_files)) #create Pool to run across the length of the input directory
result = pool.map(convert, files_in) #reference convert function and list of DBF files to be passed through
print(result)
</code>```

I've read a few answers here on StackOverflow that are somewhat similar to my question; however, I've not seen anything that applies to my specific task. How could I improve my code so that instead of reading and converting just one file at a time, the script will process multiple files simultaneously?

Thank you for any help provided.

答:
Some general guidance:

You are creating a pool. The pool size should depend on machine, not the size of your job. For example, you want 4 processes in the pool instead of 10000 processes even you have 10000 files to process

The job to run on each process should be simple but parameterized. In your case, create a function to take a filename as input and do the conversion. Then map input files into it. The filtering should be done before map is called.

So I would convert your code into something like the follows:

import os
from dbfread import DBF
import pandas as pd
import multiprocessing

directory = 'C:\\Path_to_DBF_Files' #define file directory

files_in = os.listdir(directory) #store files in directory to list

def convert(file):
    file_path = os.path.join(files_in, file)
    print(f'\nReading in {file}...')
    dbf = DBF(file_path) #create DBF object
    dbf.encoding = 'utf-8' #set encoding attribute to utf-8 instead of acsii
    dbf.char_decode_errors = 'ignore' #set decoding errors attribute to ignore any errors and read in DBF file as is
    print('\nConverting to DataFrame...')
    df = pd.DataFrame(iter(dbf)) #convert to Pandas dataframe
    df.columns.astype(str) #convert column datatypes to string
    print(df)
    print('\nWriting to CSV...')
    dest_directory = 'C:\\Path_to_output_directory\\%s.csv' % ('D' + file.strip('.DBF')) #define destination directory and names for output files
    df.to_csv(dest_directory, index = False)
    print(f'\nConverted {file} to CSV. Moving to next file...')

pool = multiprocessing.Pool(processes = 4)
pool.map(convert, [file for file in files_in if file.startswith('D') and file.endswith('.DBF')])
```

Body:
I have a Python script that reads in a directory containing over 10,000 DBF files so they can be converted to CSV. I would like to parellelize this task rather than converting each file individually. I have read up on Python's multiprocessing module, though I am having a little bit of trouble implementing it for this task. Specifically, I wanted to use the Pool class to distribute the workload across the CPU cores.

Here is my code thus far:

import os
from dbfread import DBF
import pandas as pd
import multiprocessing

directory = 'C:\\Path_to_DBF_Files' #define file directory

files_in = os.listdir(directory) #store files in directory to list

def convert():

    for file in files_in:

        if file.startswith('D') and file.endswith('.DBF'): #define parameters of filenames to convert
            file_path = os.path.join(files_in, file)
            print(f'\nReading in {file}...')
            dbf = DBF(file_path) #create DBF object
            dbf.encoding = 'utf-8' #set encoding attribute to utf-8 instead of acsii
            dbf.char_decode_errors = 'ignore' #set decoding errors attribute to ignore any errors and read in DBF file as is
            print('\nConverting to DataFrame...')
            df = pd.DataFrame(iter(dbf)) #convert to Pandas dataframe
            df.columns.astype(str) #convert column datatypes to string
            print(df)
            print('\nWriting to CSV...')
            dest_directory = 'C:\\Path_to_output_directory\\%s.csv' % ('D' + file.strip('.DBF')) #define destination directory and names for output files
            df.to_csv(dest_directory, index = False)
            print(f'\nConverted {file} to CSV. Moving to next file...')

        elif file.startswith('B') and file.endswith('.DBF'): #define parameters for unnecessary files
            print('\nB file not needed.')
            continue

        elif file.endswith('.FPT'): #skip FPT files
            print('Skipping FPT file.')
            continue

        elif file.startswith('ID') and file.endswith('.DB~'): #stop iteration when this file is reached in the directory
            print('All files converted to CSV.')
            break

        else:
            print('\nFile not found or error.')
            print(f'Last file read in was {file}.')

pool = multiprocessing.Pool(processes = len(in_files)) #create Pool to run across the length of the input directory
result = pool.map(convert, files_in) #reference convert function and list of DBF files to be passed through
print(result)

I've read a few answers here on StackOverflow that are somewhat similar to my question; however, I've not seen anything that applies to my specific task. How could I improve my code so that instead of reading and converting just one file at a time, the script will process multiple files simultaneously?

Thank you for any help provided.


问:
CGImage from images stored on iCloud look wrong
I'm extracting pixel colors from a CGImage using the code described in this answer.

However, I just realized that if I load an image that was created on another device, the pixels values look wrong. The first obvious problem is that the alpha is gone. The CGImageAlphaInfo reports .noneSkipLast, but I know the image is RGBA. If I read it from the same device it was created, it looks fine. The second problem is that there is some color bleeding, as if the image had been resized. Perhaps is being compressed or something.

Here's an example:

Source image is this watermelon, 12x12

It was created on my iPad. But if I load it on my iPhone through iCloud using that code I linked, I get this:

The alpha channel is gone, and colors bleed.

If from the same iPhone I send the little watermelon to my Mac using Airdrop, and send it back using Airdrop again (so it is supposedly the same image!), and load it now, I get the correct image:

(dark brown areas is where alpha is 0)

If you have a couple of iOS device with iCloud enabled, you can reproduce this behavior in this app: http://swiftpixels.endavid.com
where I'm using that code to read pixel colors.

What could be the difference between those images? How can I read the correct image from iCloud? Should I look for hints in UIImage instead of CGImage?

Any clues? Thanks!

Update

For reference, I'm reading the image using a UIImagePickerController, using this code:

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
            loadImageInMemory(image)
        }
        picker.dismiss(animated: true, completion: nil)
        self.presentingViewController?.dismiss(animated: true, completion: nil)
    }

    fileprivate func loadImageInMemory(_ image: UIImage) {
       /// skipping some preparation
       guard let cgImage = image.cgImage else {
          return
       }
       /// getImageData is a function like getPixelColor linked above,
       /// but for a Rect
       self.imageData = cgImage.getImageData(rect, width, height)
    }
```

I also found this question which may be related: UIImagePickerController and iCloud photos

As Rob suggested in the comments below, I changed the Photos settings in my phone to "Download and Keep Originals" (instead of "Optimize iPhone Storage"), and that fixes the problem. So I guess the question is why iCloud tries to compress a PNG image that it's just 1,355 bytes, and whether it's possible to access the original image from the UIImagePickerController

答:
The reason why the image looked blurry and without alpha seems to be that Photos streams Jpeg-compressed images from iCloud by default. Even if your original image would be smaller without compression, like in this pixel art example.

As pointed out by Rob, a way to verify that this is the case is to change your Photos settings. From the Settings app:

```<code>Settings -> Photos -> Download and Keep Originals
</code>```

This would fix the issue, but of course it's not desirable. If you want to keep using Photos, instead of implementing your own iCloud solution, while keeping the Optimize iPhone Storage setting, you can use PhotoKit to retrieve the original image.

Replace this code,

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
            loadImageInMemory(image)
        }
        picker.dismiss(animated: true, completion: nil)
        self.presentingViewController?.dismiss(animated: true, completion: nil)
    }
```
<p>by this other code:</p>
<pre class="lang-swift prettyprint-override"><code>import Photos

// ...

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        // it will be loaded asynchronously
        loadImageFromPicker(info: info)
        picker.dismiss(animated: true, completion: nil)
        self.presentingViewController?.dismiss(animated: true, completion: nil)
    }

    private func loadImageFromPicker(info: [UIImagePickerController.InfoKey : Any]) {
        var phAsset: PHAsset?
        if #available(iOS 11.0, *) {
            phAsset = info[UIImagePickerController.InfoKey.phAsset] as? PHAsset
        } else {
            // Fallback on earlier versions
            if let referenceURL = info[UIImagePickerController.InfoKey.referenceURL] as? URL {
                let fetchResult = PHAsset.fetchAssets(withALAssetURLs: [referenceURL], options: nil)
                phAsset = fetchResult.firstObject
            }
        }
        guard let asset = phAsset else {
            return
        }
        // size doesn't matter, because resizeMode = .none
        let size = CGSize(width: 32, height: 32)
        let options = PHImageRequestOptions()
        options.version = .original
        options.deliveryMode = .highQualityFormat
        options.resizeMode = .none
        options.isNetworkAccessAllowed = true
        PHImageManager.default().requestImage(for: asset, targetSize: size, contentMode: .aspectFit, options: options) { [weak self] (image, info) in
            if let s = self, let image = image {
                s.loadImageInMemory(image)
            }
        }
    }
</code>```

This code will work with both local images and iCloud images.

Body:
I'm extracting pixel colors from a CGImage using the code described in this answer.

However, I just realized that if I load an image that was created on another device, the pixels values look wrong. The first obvious problem is that the alpha is gone. The CGImageAlphaInfo reports .noneSkipLast, but I know the image is RGBA. If I read it from the same device it was created, it looks fine. The second problem is that there is some color bleeding, as if the image had been resized. Perhaps is being compressed or something.

Here's an example:

Source image is this watermelon, 12x12

It was created on my iPad. But if I load it on my iPhone through iCloud using that code I linked, I get this:

The alpha channel is gone, and colors bleed.

If from the same iPhone I send the little watermelon to my Mac using Airdrop, and send it back using Airdrop again (so it is supposedly the same image!), and load it now, I get the correct image:

(dark brown areas is where alpha is 0)

If you have a couple of iOS device with iCloud enabled, you can reproduce this behavior in this app: http://swiftpixels.endavid.com
where I'm using that code to read pixel colors.

What could be the difference between those images? How can I read the correct image from iCloud? Should I look for hints in UIImage instead of CGImage?

Any clues? Thanks!

Update

For reference, I'm reading the image using a UIImagePickerController, using this code:

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
            loadImageInMemory(image)
        }
        picker.dismiss(animated: true, completion: nil)
        self.presentingViewController?.dismiss(animated: true, completion: nil)
    }

    fileprivate func loadImageInMemory(_ image: UIImage) {
       /// skipping some preparation
       guard let cgImage = image.cgImage else {
          return
       }
       /// getImageData is a function like getPixelColor linked above,
       /// but for a Rect
       self.imageData = cgImage.getImageData(rect, width, height)
    }

I also found this question which may be related: UIImagePickerController and iCloud photos

As Rob suggested in the comments below, I changed the Photos settings in my phone to "Download and Keep Originals" (instead of "Optimize iPhone Storage"), and that fixes the problem. So I guess the question is why iCloud tries to compress a PNG image that it's just 1,355 bytes, and whether it's possible to access the original image from the UIImagePickerController

Body:
The reason why the image looked blurry and without alpha seems to be that Photos streams Jpeg-compressed images from iCloud by default. Even if your original image would be smaller without compression, like in this pixel art example.

As pointed out by Rob, a way to verify that this is the case is to change your Photos settings. From the Settings app:

Settings -> Photos -> Download and Keep Originals

This would fix the issue, but of course it's not desirable. If you want to keep using Photos, instead of implementing your own iCloud solution, while keeping the Optimize iPhone Storage setting, you can use PhotoKit to retrieve the original image.

Replace this code,

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
            loadImageInMemory(image)
        }
        picker.dismiss(animated: true, completion: nil)
        self.presentingViewController?.dismiss(animated: true, completion: nil)
    }

by this other code:

import Photos

// ...

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        // it will be loaded asynchronously
        loadImageFromPicker(info: info)
        picker.dismiss(animated: true, completion: nil)
        self.presentingViewController?.dismiss(animated: true, completion: nil)
    }

    private func loadImageFromPicker(info: [UIImagePickerController.InfoKey : Any]) {
        var phAsset: PHAsset?
        if #available(iOS 11.0, *) {
            phAsset = info[UIImagePickerController.InfoKey.phAsset] as? PHAsset
        } else {
            // Fallback on earlier versions
            if let referenceURL = info[UIImagePickerController.InfoKey.referenceURL] as? URL {
                let fetchResult = PHAsset.fetchAssets(withALAssetURLs: [referenceURL], options: nil)
                phAsset = fetchResult.firstObject
            }
        }
        guard let asset = phAsset else {
            return
        }
        // size doesn't matter, because resizeMode = .none
        let size = CGSize(width: 32, height: 32)
        let options = PHImageRequestOptions()
        options.version = .original
        options.deliveryMode = .highQualityFormat
        options.resizeMode = .none
        options.isNetworkAccessAllowed = true
        PHImageManager.default().requestImage(for: asset, targetSize: size, contentMode: .aspectFit, options: options) { [weak self] (image, info) in
            if let s = self, let image = image {
                s.loadImageInMemory(image)
            }
        }
    }

This code will work with both local images and iCloud images.


问:
How to use HttpsURLConnection through proxy by setProperty?
Network environment:

Https Client<=============>Proxy Server<==============>Https Server

                                                    192.168.17.11<-----extranet------>192.168.17.22

  10.100.21.10<----intranet----->10.100.21.11

ps: Http Client without default gateway, but it can ping to 10.100.21.11

Description:

OS: Ubuntu 12.04 on 3 hosts

  Https Client: Implement with java(openjdk-6).Have one network-interface.

  Proxy Server: Apache2.2.Have two network-interfaces.

  Https Server: Tomcat6.Have one network-interface.

I use two method to implement httpsurlconnection through proxy:

(For facilitate I do not write down about ssl handle function for checking serverTrusted and hostnameVerifier issue.If need I will update.)

1.Proxy class

InetSocketAddress proxyInet = new InetSocketAddress("10.100.21.11",80);
Proxy proxy = new Proxy(Proxy.Type.HTTP, proxyInet);
URL httpsUrl = new URL("https://192.168.17.22:8443/test");
HttpsURLConnection httpsCon = (HttpsURLConnection) httpsUrl.openConnection(proxy);

httpsCon.setDoOutput(true);
httpsCon.setDoInput(true);
httpsCon.setRequestMethod("POST");
OutputStream out = httpsCon.getOutputStream();
OutputStreamWriter owriter = new OutputStreamWriter(out);

owriter.write("<request>test</request>");
owriter.flush();
owriter.close();
...
```

<p>This method workable and I observed packets flow also met my expectation.<br/>
<em>HttpClient ---> ProxyServer ---> HttpServer</em><br/></p>

<p>But when I use set Property method:<br/></p>

<h1>2.setProperty</h1>

<pre class="lang-java prettyprint-override"><code>System.setProperty("http.proxySet", "true");
System.setProperty("http.proxyHost",10.100.21.11);
System.setProperty("http.proxyPort","80");

URL httpsUrl = new URL("https://192.168.17.22:8443/test");
HttpsURLConnection httpsCon = (HttpsURLConnection)httpsUrl.openConnection();

httpsCon.setDoOutput(true);
httpsCon.setDoInput(true);
httpsCon.setRequestMethod("POST");
OutputStream out = httpsCon.getOutputStream();
OutputStreamWriter owriter = new OutputStreamWriter(out);

owriter.write("<request>test</request>");
owriter.flush();
owriter.close();
...
</code>```

I got a NoRouteToHostException: Network is unreachable.

It make me confused.I did not see any packets between HttpClient and ProxyServer.

But HttpClient can ping to ProxyServer(10.100.12.10 ping 10.100.21.11)

So I remove proxy setting(as without using proxy):

Also got NoRouteToHostException: Network is unreachable.

I thought this is reasonable.Because there is no route to extranet.

I guess it seems like to setProperty method that the inner function of httpsUrlConnection will to check this url can be reachable or not.

But it is weird. 1st method can be success.

Have any idea? Or what are different between 1st and 2nd method?

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Update

```<code>System.setProperty("https.proxyHost",10.100.21.11);
System.setProperty("https.proxyPort","80");
</code>```

It can work and packets flow are correct what I expect for.

But set https.proxyPort=443 is not workable for me

```<code>System.setProperty("https.proxyPort","443");
</code>```

It will thorow a exception as bellow:

```<code>java.net.SocketException: Unexpected end of file from server
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:770)
....
</code>```

So I thought Apache Proxy have also to be modified to the right configuration.

答:
thank you @divinedragon!

Same code on kotlin:

```<code> fun testProxy(login: String, pass: String, proxyData: ProxyData): String {
    val url = URL("http://api.ipify.org")
    val proxy = Proxy(Proxy.Type.HTTP, InetSocketAddress(proxyData.ip, proxyData.port))
    val connection = url.openConnection(proxy) as HttpURLConnection

    val loginPass = "$login:$pass"
    val encodedLoginPass = Base64.getEncoder().encodeToString(loginPass.toByteArray())
    val authString = "Basic $encodedLoginPass"
    connection.setRequestProperty("Proxy-Authorization", authString);
    with(connection) {
        requestMethod = "GET"  // optional default is GET
        connectTimeout = 2000
        readTimeout = 2000
        return inputStream.bufferedReader().readText()
    }
}
</code>```

Body:
Network environment:

Https Client<=============>Proxy Server<==============>Https Server

                                                    192.168.17.11<-----extranet------>192.168.17.22

  10.100.21.10<----intranet----->10.100.21.11

ps: Http Client without default gateway, but it can ping to 10.100.21.11

Description:

OS: Ubuntu 12.04 on 3 hosts

  Https Client: Implement with java(openjdk-6).Have one network-interface.

  Proxy Server: Apache2.2.Have two network-interfaces.

  Https Server: Tomcat6.Have one network-interface.

I use two method to implement httpsurlconnection through proxy:

(For facilitate I do not write down about ssl handle function for checking serverTrusted and hostnameVerifier issue.If need I will update.)

1.Proxy class

InetSocketAddress proxyInet = new InetSocketAddress("10.100.21.11",80);
Proxy proxy = new Proxy(Proxy.Type.HTTP, proxyInet);
URL httpsUrl = new URL("https://192.168.17.22:8443/test");
HttpsURLConnection httpsCon = (HttpsURLConnection) httpsUrl.openConnection(proxy);

httpsCon.setDoOutput(true);
httpsCon.setDoInput(true);
httpsCon.setRequestMethod("POST");
OutputStream out = httpsCon.getOutputStream();
OutputStreamWriter owriter = new OutputStreamWriter(out);

owriter.write("<request>test</request>");
owriter.flush();
owriter.close();
...

This method workable and I observed packets flow also met my expectation.

HttpClient ---> ProxyServer ---> HttpServer

But when I use set Property method:

2.setProperty

System.setProperty("http.proxySet", "true");
System.setProperty("http.proxyHost",10.100.21.11);
System.setProperty("http.proxyPort","80");

URL httpsUrl = new URL("https://192.168.17.22:8443/test");
HttpsURLConnection httpsCon = (HttpsURLConnection)httpsUrl.openConnection();

httpsCon.setDoOutput(true);
httpsCon.setDoInput(true);
httpsCon.setRequestMethod("POST");
OutputStream out = httpsCon.getOutputStream();
OutputStreamWriter owriter = new OutputStreamWriter(out);

owriter.write("<request>test</request>");
owriter.flush();
owriter.close();
...

I got a NoRouteToHostException: Network is unreachable.

It make me confused.I did not see any packets between HttpClient and ProxyServer.

But HttpClient can ping to ProxyServer(10.100.12.10 ping 10.100.21.11)

So I remove proxy setting(as without using proxy):

Also got NoRouteToHostException: Network is unreachable.

I thought this is reasonable.Because there is no route to extranet.

I guess it seems like to setProperty method that the inner function of httpsUrlConnection will to check this url can be reachable or not.

But it is weird. 1st method can be success.

Have any idea? Or what are different between 1st and 2nd method?

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Update

System.setProperty("https.proxyHost",10.100.21.11);
System.setProperty("https.proxyPort","80");

It can work and packets flow are correct what I expect for.

But set https.proxyPort=443 is not workable for me

System.setProperty("https.proxyPort","443");

It will thorow a exception as bellow:

java.net.SocketException: Unexpected end of file from server
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:770)
....

So I thought Apache Proxy have also to be modified to the right configuration.

Body:
thank you @divinedragon!

Same code on kotlin:

 fun testProxy(login: String, pass: String, proxyData: ProxyData): String {
    val url = URL("http://api.ipify.org")
    val proxy = Proxy(Proxy.Type.HTTP, InetSocketAddress(proxyData.ip, proxyData.port))
    val connection = url.openConnection(proxy) as HttpURLConnection

    val loginPass = "$login:$pass"
    val encodedLoginPass = Base64.getEncoder().encodeToString(loginPass.toByteArray())
    val authString = "Basic $encodedLoginPass"
    connection.setRequestProperty("Proxy-Authorization", authString);
    with(connection) {
        requestMethod = "GET"  // optional default is GET
        connectTimeout = 2000
        readTimeout = 2000
        return inputStream.bufferedReader().readText()
    }
}


问:
How can I add content:"NEW" to paragraphs 2 & 3 using only CSS?

help me put content="NEW" with css, but only in paragraf 2 and 3.

i mustn't change html code.

if possible pls wiht pseudo classes.

```<code><div id="first-div">
  <h2>Subheading</h2>
  <p>Paragraph 2</p> <!-- i need to add content here-->
  <p>Paragraph 3</p> <!--and here -->
  <div id="second-div">
     <h3>Sub subheading</h3>
     <p>Paragraph 4</p>
     <p>Paragraph 5</p>
     <p>Paragraph 6</p>
     <p>Paragraph 7</p>
     <p>Paragraph 8</p>
  </div>
</div>
<p>Paragraph 9</p>
<p>Paragraph 10</p>
</code>```

答:
You can accomplish this using the immediate child selector, also referred to as the child combinator (>), which will target only direct p children of #first-div.

From MDN:

The child combinator (>) is placed between two CSS selectors. It
  matches only those elements matched by the second selector that are
  the direct children of elements matched by the first.

#first-div>p::after {
  content: 'NEW';
  color: red;
  padding-left: 10px;
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><div id="first-div">
  <h2>Subheading</h2>
  <p>Paragraph 2</p> <!-- i need to add content here -->
  <p>Paragraph 3</p> <!--and here -->
  <div id="second-div">
    <h3>Sub subheading</h3>
    <p>Paragraph 4</p>
    <p>Paragraph 5</p>
    <p>Paragraph 6</p>
    <p>Paragraph 7</p>
    <p>Paragraph 8</p>
  </div>
</div>
<p>Paragraph 9</p>
<p>Paragraph 10</p></code>```

jsFiddle

Body:
help me put content="NEW" with css, but only in paragraf 2 and 3.

i mustn't change html code.

if possible pls wiht pseudo classes.

Subheading

Paragraph 2

Paragraph 3

Sub subheading

Paragraph 4

Paragraph 5

Paragraph 6

Paragraph 7

Paragraph 8

Paragraph 9

Paragraph 10

Body:
You can accomplish this using the immediate child selector, also referred to as the child combinator (>), which will target only direct p children of #first-div.

From MDN:

The child combinator (>) is placed between two CSS selectors. It
  matches only those elements matched by the second selector that are
  the direct children of elements matched by the first.

#first-div>p::after {
  content: 'NEW';
  color: red;
  padding-left: 10px;
}

<div id="first-div">
  <h2>Subheading</h2>
  <p>Paragraph 2</p> <!-- i need to add content here -->
  <p>Paragraph 3</p> <!--and here -->
  <div id="second-div">
    <h3>Sub subheading</h3>
    <p>Paragraph 4</p>
    <p>Paragraph 5</p>
    <p>Paragraph 6</p>
    <p>Paragraph 7</p>
    <p>Paragraph 8</p>
  </div>
</div>
<p>Paragraph 9</p>
<p>Paragraph 10</p>

jsFiddle


问:
How to setRequiredLevel specific control or attribute?
My mission is to check if a control is visible and if it is then to set it as required.
On my form I have the same field twice (in different sections) so to check my control visibility I use this code:

```<code>if(Xrm.Page.ui.tabs.get("tab1").sections.get("section").controls.get("attrname").getVisible()) //works
{
              Xrm.Page.ui.tabs.get("tab1").sections.get("section").controls.get("attrname").setRequiredLevel("required"); //doesn't work - error
              Xrm.Page.ui.tabs.get("tab1").sections.get("section").controls.get("attrname").getAttribute("attrname").setRequiredLevel("required");  //doesn't work - error
}
</code>```

How do I set the required level in this case?

答:
When you add the same attribute in multiple different sections, you will get a new name appended by sequence number. For ex. attrname, attrname1, attrname2 and so on.

So you have to get the right attribute to set the property when you go through the tab/section. Anyway the result will be, all the attribute copies will get the property set.

```<code>Xrm.Page.ui.tabs.get("tab1").sections.get("section2").controls.get("new_rep1").getAttribute("new_rep1").setRequiredLevel("required");
</code>```

Result:

Body:
My mission is to check if a control is visible and if it is then to set it as required.
On my form I have the same field twice (in different sections) so to check my control visibility I use this code:

if(Xrm.Page.ui.tabs.get("tab1").sections.get("section").controls.get("attrname").getVisible()) //works
{
              Xrm.Page.ui.tabs.get("tab1").sections.get("section").controls.get("attrname").setRequiredLevel("required"); //doesn't work - error
              Xrm.Page.ui.tabs.get("tab1").sections.get("section").controls.get("attrname").getAttribute("attrname").setRequiredLevel("required");  //doesn't work - error
}

How do I set the required level in this case?

Body:
When you add the same attribute in multiple different sections, you will get a new name appended by sequence number. For ex. attrname, attrname1, attrname2 and so on.

So you have to get the right attribute to set the property when you go through the tab/section. Anyway the result will be, all the attribute copies will get the property set.

Xrm.Page.ui.tabs.get("tab1").sections.get("section2").controls.get("new_rep1").getAttribute("new_rep1").setRequiredLevel("required");

Result:


问:
Reading the ID from a file and auto increment it by 1
Currently I'm working on this:

I have a windows form with a text file. I use StreamReader with a while loop in order to read the file line by line and then to display it in a listview but then I don't know how to auto-increment the ID in the text box by one so for example if the last id of a person was 1005, the code should automatically add 1 so i'll get 1006

```<code>using (StreamReader sr = new StreamReader("INCIDENT.txt"))
{
    int iCount = 0;

    while (sr.Peek() != -1)
    {
        ListViewItem lvRead = new ListViewItem();
        lvRead.Text = sr.ReadLine();
        lvRead.SubItems.Add(sr.ReadLine());
        lvRead.SubItems.Add(sr.ReadLine());
        lvRead.SubItems.Add(sr.ReadLine());
        lvRead.SubItems.Add(sr.ReadLine());
        lvRead.SubItems.Add(sr.ReadLine());
        lvRead.SubItems.Add(sr.ReadLine());
        lvRead.SubItems.Add(sr.ReadLine());
        lvRead.SubItems.Add(sr.ReadLine());
        lstViewData.Items.Add(lvRead);

        iCount++;
    }
}
</code>```

Image

答: try code after edit you should after opening any stream close it</p>

Body:
Currently I'm working on this:

I have a windows form with a text file. I use StreamReader with a while loop in order to read the file line by line and then to display it in a listview but then I don't know how to auto-increment the ID in the text box by one so for example if the last id of a person was 1005, the code should automatically add 1 so i'll get 1006

using (StreamReader sr = new StreamReader("INCIDENT.txt"))
{
    int iCount = 0;

    while (sr.Peek() != -1)
    {
        ListViewItem lvRead = new ListViewItem();
        lvRead.Text = sr.ReadLine();
        lvRead.SubItems.Add(sr.ReadLine());
        lvRead.SubItems.Add(sr.ReadLine());
        lvRead.SubItems.Add(sr.ReadLine());
        lvRead.SubItems.Add(sr.ReadLine());
        lvRead.SubItems.Add(sr.ReadLine());
        lvRead.SubItems.Add(sr.ReadLine());
        lvRead.SubItems.Add(sr.ReadLine());
        lvRead.SubItems.Add(sr.ReadLine());
        lstViewData.Items.Add(lvRead);

        iCount++;
    }
}

Image


问:
Using discard in Sourcetree
I would like to know if I right click on an unstaged file and click on discard, will that remove the file from my folder or will it just "undo" the changes I made but leave the file alone. Also will it affect my files on bitbucket. I'm worried that if I discard the file it will delete it and then I would have problems pulling my files down from bitbucket.

I'm sorry if these are silly questions.

答: discard will only remove the local file changes that you have made to a specific file. This will put the file back to the state it was in the last commit. remove completely removes the file from version control which will result in git no longer watching the file. This would result in changes made to the file not being reflected in your Sourcetree.

Title: Using discard in Sourcetree


问:
Trying to check for a certain field inside a firebase firestore document but struggling with the logic behind it
So I am using this currently to check and see if a doc exists for a user

```<code>let user = firebase.auth().currentUser;
                        let userInfo = db.collection("stripe_customers").doc(user.uid);

</code>```

and then if it does, it runs a script I want

```<code>
userInfo.get().then(function (doc) {
                            if (doc.exists) {

</code>```

However, instead of checking for a specific doc, I need to try and check for a field value inside some documents.

For example I have a collection called "stripe_customers" > that has a document per user via their UID > then inside the document is the collection "charges" > then inside "charges" is a document under a random string of numbers and letters "89nzVNrfQCOVqogDaGvo" for example that is generated by stripe for their charge after they purchase (there may be multiple of these if they have an older charge which is why I need to find the most recent one) > then inside the most recent charge document, I need to check for the field "status" and that is has the value "succeeded". That way I can check to see who has a succeeded payment and if they do it will run the script I want. I am just so confused on how to achieve this. I know how to do basic queries but this is somewhat complex. I need to be able to make sure the current UID has that field with that value so I can see if the current UID paid or not and if they did the script runs, which sets a custom claim.

Here is a visual of my db storage flow for what im trying to do so its easier to understand https://imgur.com/a/NE1x6sU

答:
So, you want to get the most recent document in a charges (sub)collection based on a created field which contains a timestamp value, and check the value of the status field of this doc.

The following should do the trick:

```<code>const user = firebase.auth().currentUser;
const subCollRef = db.collection("stripe_customers").doc(user.uid).collection("charges");

const query = subCollRef.orderBy('created', 'desc').limit(1);

query.get()
  .then(snapshot => {
    if (snapshot.size > 0 && snapshot.docs[0].data().status === "succeeded") {
         //payment was succeeded, you can "run your script"
    });
  })
  .catch(err => {
    console.log('Error getting documents', err);
  });
</code>```

Body:
So I am using this currently to check and see if a doc exists for a user

let user = firebase.auth().currentUser;
                        let userInfo = db.collection("stripe_customers").doc(user.uid);

and then if it does, it runs a script I want

userInfo.get().then(function (doc) {
                            if (doc.exists) {

However, instead of checking for a specific doc, I need to try and check for a field value inside some documents.

For example I have a collection called "stripe_customers" > that has a document per user via their UID > then inside the document is the collection "charges" > then inside "charges" is a document under a random string of numbers and letters "89nzVNrfQCOVqogDaGvo" for example that is generated by stripe for their charge after they purchase (there may be multiple of these if they have an older charge which is why I need to find the most recent one) > then inside the most recent charge document, I need to check for the field "status" and that is has the value "succeeded". That way I can check to see who has a succeeded payment and if they do it will run the script I want. I am just so confused on how to achieve this. I know how to do basic queries but this is somewhat complex. I need to be able to make sure the current UID has that field with that value so I can see if the current UID paid or not and if they did the script runs, which sets a custom claim.

Here is a visual of my db storage flow for what im trying to do so its easier to understand https://imgur.com/a/NE1x6sU

Body:
So, you want to get the most recent document in a charges (sub)collection based on a created field which contains a timestamp value, and check the value of the status field of this doc.

The following should do the trick:

const user = firebase.auth().currentUser;
const subCollRef = db.collection("stripe_customers").doc(user.uid).collection("charges");

const query = subCollRef.orderBy('created', 'desc').limit(1);

query.get()
  .then(snapshot => {
    if (snapshot.size > 0 && snapshot.docs[0].data().status === "succeeded") {
         //payment was succeeded, you can "run your script"
    });
  })
  .catch(err => {
    console.log('Error getting documents', err);
  });


答:
#first-div > p::before {
  content : 'NEW ';
  color   : red;
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><div id="first-div">
  <h2>Subheading</h2>
  <p>Paragraph 2</p> <!-- i need to add content here-->
  <p>Paragraph 3</p> <!--and here -->
  <div id="second-div">
     <h3>Sub subheading</h3>
     <p>Paragraph 4</p>
     <p>Paragraph 5</p>
     <p>Paragraph 6</p>
     <p>Paragraph 7</p>
     <p>Paragraph 8</p>
  </div>
</div>
<p>Paragraph 9</p>
<p>Paragraph 10</p></code>```

Title: How can I add content:"NEW" to paragraphs 2 & 3 using only CSS?

Body:
#first-div > p::before {
  content : 'NEW ';
  color   : red;
}

<div id="first-div">
  <h2>Subheading</h2>
  <p>Paragraph 2</p> <!-- i need to add content here-->
  <p>Paragraph 3</p> <!--and here -->
  <div id="second-div">
     <h3>Sub subheading</h3>
     <p>Paragraph 4</p>
     <p>Paragraph 5</p>
     <p>Paragraph 6</p>
     <p>Paragraph 7</p>
     <p>Paragraph 8</p>
  </div>
</div>
<p>Paragraph 9</p>
<p>Paragraph 10</p>


问:
Multi provider relational user table in laravel
I am new in laravel and trying to create an auth system where user can login/register using multiple providers and whenever user logins using any of providers the user avatar and profile data must be shown accordingly. When using Auth::user()->providers the result is array of all registered providers for that user.

What I want is to fetch only provider data for current login method (i.e local account, facebook account provider, google account provider etc) so I can show data from that exact provider for user.

User.php

```<code>...
    public function providers()
    {
        return $this->hasMany("App\UserProvider");
    }
...
</code>```

UserProvider.php

```<code>class UserProvider Extends Model
{
    protected $table = 'user_providers';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'user_id', 'provider', 'provider_id','avatar_url', 'token', 'token_secret',
    ];

}
</code>```

Please let me know if I am on the right way or is there any other way to do this.
Thanks!

答:
You can use your relation to build a query

```<code>$provider = Auth::user()->providers()->where('provider', 'facebook')->first();
</code>```

This will return single UserProvider class object

Body:
I am new in laravel and trying to create an auth system where user can login/register using multiple providers and whenever user logins using any of providers the user avatar and profile data must be shown accordingly. When using Auth::user()->providers the result is array of all registered providers for that user.

What I want is to fetch only provider data for current login method (i.e local account, facebook account provider, google account provider etc) so I can show data from that exact provider for user.

User.php

...
    public function providers()
    {
        return $this->hasMany("App\UserProvider");
    }
...

UserProvider.php

class UserProvider Extends Model
{
    protected $table = 'user_providers';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'user_id', 'provider', 'provider_id','avatar_url', 'token', 'token_secret',
    ];

}

Please let me know if I am on the right way or is there any other way to do this.
Thanks!

Body:
You can use your relation to build a query

$provider = Auth::user()->providers()->where('provider', 'facebook')->first();

This will return single UserProvider class object


问:
AWS S3 Lambda Access Denied
Trying to follow this tutorial and I keep getting "Access Denied" when running my Lambda. The Lambda is the default s3-python-get-object.

The role for the lambda is

```<code>{
"Version": "2012-10-17",
"Statement": [
    {
        "Effect": "Allow",
        "Action": [
            "s3:GetObject",
            "s3:DeleteObject"
        ],
        "Resource": [
            "arn:aws:s3:::*"
        ]
    }
]
}
</code>```

The user has admin privileges. I just don't get why it's going wrong.

答:
From the docs:

```<code>If the object you request does not exist, the error Amazon S3 returns depends on whether you also have the s3:ListBucket permission.

If you have the s3:ListBucket permission on the bucket, Amazon S3 returns an HTTP status code 404 ("no such key") error.
If you don’t have the s3:ListBucket permission, Amazon S3 returns an HTTP status code 403 ("access denied") error.
</code>```

The code above seems right for the operation you do.
Please make sure you have the key you are calling or add s3:ListBucket permission to be sure of the kind of error.

Title: AWS S3 Lambda Access Denied

Body:
Trying to follow this tutorial and I keep getting "Access Denied" when running my Lambda. The Lambda is the default s3-python-get-object.

The role for the lambda is

{
"Version": "2012-10-17",
"Statement": [
    {
        "Effect": "Allow",
        "Action": [
            "s3:GetObject",
            "s3:DeleteObject"
        ],
        "Resource": [
            "arn:aws:s3:::*"
        ]
    }
]
}

The user has admin privileges. I just don't get why it's going wrong.

Body:
From the docs:

If the object you request does not exist, the error Amazon S3 returns depends on whether you also have the s3:ListBucket permission.

If you have the s3:ListBucket permission on the bucket, Amazon S3 returns an HTTP status code 404 ("no such key") error.
If you don’t have the s3:ListBucket permission, Amazon S3 returns an HTTP status code 403 ("access denied") error.

The code above seems right for the operation you do.
Please make sure you have the key you are calling or add s3:ListBucket permission to be sure of the kind of error.