问:
Exception Handling in Apache Beam pipelines when writing to database using Java
When writing simple records to a table in Postgres (could be any db) at the end of a pipeline, some of the potential records violate uniqueness constraints and trigger an exception. As far as I can tell, there's no straight forward way to handle these gracefully - the pipeline either errors out completely, or depending on the runner, enters an interminable death spiral.

There doesn't appear to be any mention of error handling for this case in the beam docs. The medium posts on error handling don't seem to apply to this particular type of PTransform which returns PDone.

This answer isn't comprehensible and is devoid of examples.

In my example, I'm reading from a file with 2 duplicate lines and trying to write them into a table.

```<code>CREATE TABLE foo (
    field CHARACTER VARYING(100) UNIQUE
);
</code>```

foo.txt contains:

```<code>a
a
</code>```

The pipeline looks like this:

```<code>        Pipeline p = Pipeline.create();
        p.apply(TextIO.read().from("/path/to/foo.txt"))
        .apply(
                JdbcIO.<String>write()
                .withDataSourceConfiguration(JdbcIO.DataSourceConfiguration.create("org.postgresql.Driver", "jdbc:postgresql://localhost:5432/somedb"))
                .withStatement("INSERT INTO foo (field) VALUES (?)")
                .withPreparedStatementSetter(new JdbcIO.PreparedStatementSetter<String>() {
                    private static final long serialVersionUID = 1L;
                    public void setParameters(String element, PreparedStatement query) throws SQLException {
                        query.setString(1, element);
                    }
                }))
        ;
        p.run();
</code>```

Here is the output from the simple example above:

```<code>[WARNING]
org.apache.beam.sdk.Pipeline$PipelineExecutionException: java.sql.BatchUpdateException: Batch entry 0 INSERT INTO foo (field) VALUES ('a') was aborted: ERROR: duplicate key value violates unique constraint "foo_field_key"
  Detail: Key (field)=(a) already exists.  Call getNextException to see other errors in the batch.
    at org.apache.beam.runners.direct.DirectRunner$DirectPipelineResult.waitUntilFinish (DirectRunner.java:332)
    at org.apache.beam.runners.direct.DirectRunner$DirectPipelineResult.waitUntilFinish (DirectRunner.java:302)
    at org.apache.beam.runners.direct.DirectRunner.run (DirectRunner.java:197)
    at org.apache.beam.runners.direct.DirectRunner.run (DirectRunner.java:64)
    at org.apache.beam.sdk.Pipeline.run (Pipeline.java:313)
    at org.apache.beam.sdk.Pipeline.run (Pipeline.java:299)
    at com.thing.Main.main (Main.java:105)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run (ExecJavaMojo.java:282)
    at java.lang.Thread.run (Thread.java:748)
Caused by: java.sql.BatchUpdateException: Batch entry 0 INSERT INTO foo (field) VALUES ('a') was aborted: ERROR: duplicate key value violates unique constraint "foo_field_key"
  Detail: Key (field)=(a) already exists.  Call getNextException to see other errors in the batch.
    at org.postgresql.jdbc.BatchResultHandler.handleError (BatchResultHandler.java:148)
    at org.postgresql.core.ResultHandlerDelegate.handleError (ResultHandlerDelegate.java:50)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults (QueryExecutorImpl.java:2184)
    at org.postgresql.core.v3.QueryExecutorImpl.execute (QueryExecutorImpl.java:481)
    at org.postgresql.jdbc.PgStatement.executeBatch (PgStatement.java:840)
    at org.postgresql.jdbc.PgPreparedStatement.executeBatch (PgPreparedStatement.java:1538)
    at org.apache.commons.dbcp2.DelegatingStatement.executeBatch (DelegatingStatement.java:345)
    at org.apache.commons.dbcp2.DelegatingStatement.executeBatch (DelegatingStatement.java:345)
    at org.apache.commons.dbcp2.DelegatingStatement.executeBatch (DelegatingStatement.java:345)
    at org.apache.commons.dbcp2.DelegatingStatement.executeBatch (DelegatingStatement.java:345)
    at org.apache.beam.sdk.io.jdbc.JdbcIO$Write$WriteFn.executeBatch (JdbcIO.java:846)
    at org.apache.beam.sdk.io.jdbc.JdbcIO$Write$WriteFn.finishBundle (JdbcIO.java:819)
Caused by: org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "foo_field_key"
  Detail: Key (field)=(a) already exists.
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse (QueryExecutorImpl.java:2440)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults (QueryExecutorImpl.java:2183)
    at org.postgresql.core.v3.QueryExecutorImpl.execute (QueryExecutorImpl.java:481)
    at org.postgresql.jdbc.PgStatement.executeBatch (PgStatement.java:840)
    at org.postgresql.jdbc.PgPreparedStatement.executeBatch (PgPreparedStatement.java:1538)
    at org.apache.commons.dbcp2.DelegatingStatement.executeBatch (DelegatingStatement.java:345)
    at org.apache.commons.dbcp2.DelegatingStatement.executeBatch (DelegatingStatement.java:345)
    at org.apache.commons.dbcp2.DelegatingStatement.executeBatch (DelegatingStatement.java:345)
    at org.apache.commons.dbcp2.DelegatingStatement.executeBatch (DelegatingStatement.java:345)
    at org.apache.beam.sdk.io.jdbc.JdbcIO$Write$WriteFn.executeBatch (JdbcIO.java:846)
    at org.apache.beam.sdk.io.jdbc.JdbcIO$Write$WriteFn.finishBundle (JdbcIO.java:819)
    at org.apache.beam.sdk.io.jdbc.JdbcIO$Write$WriteFn$DoFnInvoker.invokeFinishBundle (Unknown Source)
    at org.apache.beam.repackaged.beam_runners_direct_java.runners.core.SimpleDoFnRunner.finishBundle (SimpleDoFnRunner.java:285)
    at org.apache.beam.repackaged.beam_runners_direct_java.runners.core.SimplePushbackSideInputDoFnRunner.finishBundle (SimplePushbackSideInputDoFnRunner.java:118)
    at org.apache.beam.runners.direct.ParDoEvaluator.finishBundle (ParDoEvaluator.java:223)
    at org.apache.beam.runners.direct.DoFnLifecycleManagerRemovingTransformEvaluator.finishBundle (DoFnLifecycleManagerRemovingTransformEvaluator.java:73)
    at org.apache.beam.runners.direct.DirectTransformExecutor.finishBundle (DirectTransformExecutor.java:188)
    at org.apache.beam.runners.direct.DirectTransformExecutor.run (DirectTransformExecutor.java:126)
    at java.util.concurrent.Executors$RunnableAdapter.call (Executors.java:511)
    at java.util.concurrent.FutureTask.run (FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:624)
    at java.lang.Thread.run (Thread.java:748)
</code>```

I'd like to be able to arrest that exception and divert it to some dead letter construct.

答:
There is no general way of doing it in Beam yet. There are discussions from time to time about modifying the IOs to not return PDone but to my knowledge there is nothing readily available.

At the moment I can think of couple of workarounds, all of them are far from ideal:

in the driver program handle the restart of the pipeline when it fails;

copy-paste JdbcIO, parts of it, or implement your own Jdbc ParDo with custom exception handling;

add an exception handling feature to JdbcIO and contribute it to Beam, it will be appreciated;

Body:
When writing simple records to a table in Postgres (could be any db) at the end of a pipeline, some of the potential records violate uniqueness constraints and trigger an exception. As far as I can tell, there's no straight forward way to handle these gracefully - the pipeline either errors out completely, or depending on the runner, enters an interminable death spiral.

There doesn't appear to be any mention of error handling for this case in the beam docs. The medium posts on error handling don't seem to apply to this particular type of PTransform which returns PDone.

This answer isn't comprehensible and is devoid of examples.

In my example, I'm reading from a file with 2 duplicate lines and trying to write them into a table.

CREATE TABLE foo (
    field CHARACTER VARYING(100) UNIQUE
);

foo.txt contains:

a
a

The pipeline looks like this:

        Pipeline p = Pipeline.create();
        p.apply(TextIO.read().from("/path/to/foo.txt"))
        .apply(
                JdbcIO.<String>write()
                .withDataSourceConfiguration(JdbcIO.DataSourceConfiguration.create("org.postgresql.Driver", "jdbc:postgresql://localhost:5432/somedb"))
                .withStatement("INSERT INTO foo (field) VALUES (?)")
                .withPreparedStatementSetter(new JdbcIO.PreparedStatementSetter<String>() {
                    private static final long serialVersionUID = 1L;
                    public void setParameters(String element, PreparedStatement query) throws SQLException {
                        query.setString(1, element);
                    }
                }))
        ;
        p.run();

Here is the output from the simple example above:

[WARNING]
org.apache.beam.sdk.Pipeline$PipelineExecutionException: java.sql.BatchUpdateException: Batch entry 0 INSERT INTO foo (field) VALUES ('a') was aborted: ERROR: duplicate key value violates unique constraint "foo_field_key"
  Detail: Key (field)=(a) already exists.  Call getNextException to see other errors in the batch.
    at org.apache.beam.runners.direct.DirectRunner$DirectPipelineResult.waitUntilFinish (DirectRunner.java:332)
    at org.apache.beam.runners.direct.DirectRunner$DirectPipelineResult.waitUntilFinish (DirectRunner.java:302)
    at org.apache.beam.runners.direct.DirectRunner.run (DirectRunner.java:197)
    at org.apache.beam.runners.direct.DirectRunner.run (DirectRunner.java:64)
    at org.apache.beam.sdk.Pipeline.run (Pipeline.java:313)
    at org.apache.beam.sdk.Pipeline.run (Pipeline.java:299)
    at com.thing.Main.main (Main.java:105)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run (ExecJavaMojo.java:282)
    at java.lang.Thread.run (Thread.java:748)
Caused by: java.sql.BatchUpdateException: Batch entry 0 INSERT INTO foo (field) VALUES ('a') was aborted: ERROR: duplicate key value violates unique constraint "foo_field_key"
  Detail: Key (field)=(a) already exists.  Call getNextException to see other errors in the batch.
    at org.postgresql.jdbc.BatchResultHandler.handleError (BatchResultHandler.java:148)
    at org.postgresql.core.ResultHandlerDelegate.handleError (ResultHandlerDelegate.java:50)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults (QueryExecutorImpl.java:2184)
    at org.postgresql.core.v3.QueryExecutorImpl.execute (QueryExecutorImpl.java:481)
    at org.postgresql.jdbc.PgStatement.executeBatch (PgStatement.java:840)
    at org.postgresql.jdbc.PgPreparedStatement.executeBatch (PgPreparedStatement.java:1538)
    at org.apache.commons.dbcp2.DelegatingStatement.executeBatch (DelegatingStatement.java:345)
    at org.apache.commons.dbcp2.DelegatingStatement.executeBatch (DelegatingStatement.java:345)
    at org.apache.commons.dbcp2.DelegatingStatement.executeBatch (DelegatingStatement.java:345)
    at org.apache.commons.dbcp2.DelegatingStatement.executeBatch (DelegatingStatement.java:345)
    at org.apache.beam.sdk.io.jdbc.JdbcIO$Write$WriteFn.executeBatch (JdbcIO.java:846)
    at org.apache.beam.sdk.io.jdbc.JdbcIO$Write$WriteFn.finishBundle (JdbcIO.java:819)
Caused by: org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "foo_field_key"
  Detail: Key (field)=(a) already exists.
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse (QueryExecutorImpl.java:2440)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults (QueryExecutorImpl.java:2183)
    at org.postgresql.core.v3.QueryExecutorImpl.execute (QueryExecutorImpl.java:481)
    at org.postgresql.jdbc.PgStatement.executeBatch (PgStatement.java:840)
    at org.postgresql.jdbc.PgPreparedStatement.executeBatch (PgPreparedStatement.java:1538)
    at org.apache.commons.dbcp2.DelegatingStatement.executeBatch (DelegatingStatement.java:345)
    at org.apache.commons.dbcp2.DelegatingStatement.executeBatch (DelegatingStatement.java:345)
    at org.apache.commons.dbcp2.DelegatingStatement.executeBatch (DelegatingStatement.java:345)
    at org.apache.commons.dbcp2.DelegatingStatement.executeBatch (DelegatingStatement.java:345)
    at org.apache.beam.sdk.io.jdbc.JdbcIO$Write$WriteFn.executeBatch (JdbcIO.java:846)
    at org.apache.beam.sdk.io.jdbc.JdbcIO$Write$WriteFn.finishBundle (JdbcIO.java:819)
    at org.apache.beam.sdk.io.jdbc.JdbcIO$Write$WriteFn$DoFnInvoker.invokeFinishBundle (Unknown Source)
    at org.apache.beam.repackaged.beam_runners_direct_java.runners.core.SimpleDoFnRunner.finishBundle (SimpleDoFnRunner.java:285)
    at org.apache.beam.repackaged.beam_runners_direct_java.runners.core.SimplePushbackSideInputDoFnRunner.finishBundle (SimplePushbackSideInputDoFnRunner.java:118)
    at org.apache.beam.runners.direct.ParDoEvaluator.finishBundle (ParDoEvaluator.java:223)
    at org.apache.beam.runners.direct.DoFnLifecycleManagerRemovingTransformEvaluator.finishBundle (DoFnLifecycleManagerRemovingTransformEvaluator.java:73)
    at org.apache.beam.runners.direct.DirectTransformExecutor.finishBundle (DirectTransformExecutor.java:188)
    at org.apache.beam.runners.direct.DirectTransformExecutor.run (DirectTransformExecutor.java:126)
    at java.util.concurrent.Executors$RunnableAdapter.call (Executors.java:511)
    at java.util.concurrent.FutureTask.run (FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:624)
    at java.lang.Thread.run (Thread.java:748)

I'd like to be able to arrest that exception and divert it to some dead letter construct.


来源: stackexchange

问:
How to correctly ensure type safety on onChange method?
```<code>interface Cat {
  type: string;
  Age: number;
}

type CatValue = Cat[keyof Cat];

const onChange(key: keyof Cat, value: CatValue) {
   //
}
</code>```

How can I ensure that type safety happens dependant on the key that is passed?

For example:

```<code>onChange('type', 'Short Haired Domestic'); // correct
onChange('type', 3); // correct, BUT it shouldnt be as the 'type' key should only accept strings.
</code>```

答:
A generic can help us establish a relationship between key and value:

```<code>interface Cat {
  type: string;
  Age: number;
}

declare const cat: Cat;

const onChange = <K extends keyof Cat>(key: K, value: Cat[K]) => {
   cat[key] = value;
}
</code>```

ContentLicense: CC BY-SA 4.0

Body:
interface Cat {
  type: string;
  Age: number;
}

type CatValue = Cat[keyof Cat];

const onChange(key: keyof Cat, value: CatValue) {
   //
}

How can I ensure that type safety happens dependant on the key that is passed?

For example:

onChange('type', 'Short Haired Domestic'); // correct
onChange('type', 3); // correct, BUT it shouldnt be as the 'type' key should only accept strings.

Body:
A generic can help us establish a relationship between key and value:

interface Cat {
  type: string;
  Age: number;
}

declare const cat: Cat;

const onChange = <K extends keyof Cat>(key: K, value: Cat[K]) => {
   cat[key] = value;
}


问:
Function to determine if two numbers are nearly equal when rounded to n significant decimal digits
I have been asked to test a library provided by a 3rd party. The library is known to be accurate to n significant figures. Any less-significant errors can safely be ignored. I want to write a function to help me compare the results:

```<code>def nearlyequal(a, b, sigfig=5):
</code>```

The purpose of this function is to determine if two floating-point numbers (a and b) are approximately equal. The function will return True if a==b (exact match) or if a and b have the same value when rounded to sigfig significant-figures when written in decimal.

Can anybody suggest a good implementation? I've written a mini unit-test. Unless you can see a bug in my tests then a good implementation should pass the following:

```<code>assert nearlyequal(1, 1, 5)
assert nearlyequal(1.0, 1.0, 5)
assert nearlyequal(1.0, 1.0, 5)
assert nearlyequal(-1e-9, 1e-9, 5)
assert nearlyequal(1e9, 1e9 + 1 , 5)
assert not nearlyequal(1e4, 1e4 + 1, 5)
assert nearlyequal(0.0, 1e-15, 5)
assert not nearlyequal(0.0, 1e-4, 6)
</code>```

Additional notes:

Values a and b might be of type int, float or numpy.float64. Values a and b will always be of the same type. It's vital that conversion does not introduce additional error into the function.

Lets keep this numerical, so functions that convert to strings or use non-mathematical tricks are not ideal. This program will be audited by somebody who is a mathematician who will want to be able to prove that the function does what it is supposed to do.

Speed... I've got to compare a lot of numbers so the faster the better.

I've got numpy, scipy and the standard-library. Anything else will be hard for me to get, especially for such a small part of the project.

答:
Oren Shemesh got part of the problem with the problem as stated but there's more:

```<code>assert nearlyequal(0.0, 1e-15, 5)
</code>```

also fails the second definition (and that's the definition I learned in school.)

No matter how many digits you are looking at, 0 will not equal a not-zero.  This could prove to be a headache for such tests if you have a case whose correct answer is zero.

OwnerDisplayName: Salim Fadhley

Body:
I have been asked to test a library provided by a 3rd party. The library is known to be accurate to n significant figures. Any less-significant errors can safely be ignored. I want to write a function to help me compare the results:

def nearlyequal(a, b, sigfig=5):

The purpose of this function is to determine if two floating-point numbers (a and b) are approximately equal. The function will return True if a==b (exact match) or if a and b have the same value when rounded to sigfig significant-figures when written in decimal.

Can anybody suggest a good implementation? I've written a mini unit-test. Unless you can see a bug in my tests then a good implementation should pass the following:

assert nearlyequal(1, 1, 5)
assert nearlyequal(1.0, 1.0, 5)
assert nearlyequal(1.0, 1.0, 5)
assert nearlyequal(-1e-9, 1e-9, 5)
assert nearlyequal(1e9, 1e9 + 1 , 5)
assert not nearlyequal(1e4, 1e4 + 1, 5)
assert nearlyequal(0.0, 1e-15, 5)
assert not nearlyequal(0.0, 1e-4, 6)

Additional notes:

Values a and b might be of type int, float or numpy.float64. Values a and b will always be of the same type. It's vital that conversion does not introduce additional error into the function.

Lets keep this numerical, so functions that convert to strings or use non-mathematical tricks are not ideal. This program will be audited by somebody who is a mathematician who will want to be able to prove that the function does what it is supposed to do.

Speed... I've got to compare a lot of numbers so the faster the better.

I've got numpy, scipy and the standard-library. Anything else will be hard for me to get, especially for such a small part of the project.

LastEditorDisplayName: starblue

Body:
Oren Shemesh got part of the problem with the problem as stated but there's more:

assert nearlyequal(0.0, 1e-15, 5)

also fails the second definition (and that's the definition I learned in school.)

No matter how many digits you are looking at, 0 will not equal a not-zero.  This could prove to be a headache for such tests if you have a case whose correct answer is zero.

OwnerDisplayName: Loren Pechtel


问:
Array of tuples to JSON (TypeScript) not recognized by API entry point
I have an angular application that call my back-end written in Asp.net c#. The back-end works correctly (already used by a mobile application) but one entry point does not behave correctly while being hit by the angular Application.

I Serialize a tupple array of  and send it to the API, but when API entry point gets hit, it seems like the data of the tuple array is not being received.

I've already looked to my tuple array, the data is correctly serialized to json client-side but not received server-side.

Here is the entry point definition in my c# API :

```<code> public HttpResponseMessage Xamarin_UpdateStatut([FromRoute] int idEvent,          [System.Web.Http.FromBody] List<Tuple<int, bool>> listUsersStatut)
    {
    }
</code>```

And here is the tuple sended client-side (get via chrome console) :
       "[[2102,false],[1096,false],[73,false]]"

Here is the code used to serialized the tuple array to Json (client-side) :

```<code> var listUpdate = new Array<[number, boolean]>();
arr.forEach(re => {
  let el: [number, boolean] = [re.userId, re.wasThere];
  listUpdate.push(el);
});
let body = JSON.stringify(listUpdate);
</code>```

I debugged a few times server side, the list length is 0. I tried to change the type received from List to Tuple array (server side, in the method definition) but it didn't change anything.

It seems to me like the json output is correct so I don't understand why it does not work.

A little help would be very much appreciated :)

Have a nice day and thanks in advance,

Lio

答:
Tuples in C# are treated like classes, whereas you're setting up your client-side model as if they're arrays. Try something like this:

```<code> var listUpdate = new Array<{item1: number, item2: boolean}>();
arr.forEach(re => {
  let el: {item1: number, item2: boolean} = {item1: re.userId, item2: re.wasThere};
  listUpdate.push(el);
});
</code>```

I'd personally suggest using a strongly-typed class instead of tuples, so you have more meaningful property names than Item1 and Item2.

You might also consider posting an object that has the list as one of its properties rather than posting the list itself: this will give you the flexibility to add more data to the object in the future, should the need arise, without needing to change as much code.

Body:
I have an angular application that call my back-end written in Asp.net c#. The back-end works correctly (already used by a mobile application) but one entry point does not behave correctly while being hit by the angular Application.

I Serialize a tupple array of  and send it to the API, but when API entry point gets hit, it seems like the data of the tuple array is not being received.

I've already looked to my tuple array, the data is correctly serialized to json client-side but not received server-side.

Here is the entry point definition in my c# API :

 public HttpResponseMessage Xamarin_UpdateStatut([FromRoute] int idEvent,          [System.Web.Http.FromBody] List<Tuple<int, bool>> listUsersStatut)
    {
    }

And here is the tuple sended client-side (get via chrome console) :
       "[[2102,false],[1096,false],[73,false]]"

Here is the code used to serialized the tuple array to Json (client-side) :

 var listUpdate = new Array<[number, boolean]>();
arr.forEach(re => {
  let el: [number, boolean] = [re.userId, re.wasThere];
  listUpdate.push(el);
});
let body = JSON.stringify(listUpdate);

I debugged a few times server side, the list length is 0. I tried to change the type received from List to Tuple array (server side, in the method definition) but it didn't change anything.

It seems to me like the json output is correct so I don't understand why it does not work.

A little help would be very much appreciated :)

Have a nice day and thanks in advance,

Lio

Body:
Tuples in C# are treated like classes, whereas you're setting up your client-side model as if they're arrays. Try something like this:

 var listUpdate = new Array<{item1: number, item2: boolean}>();
arr.forEach(re => {
  let el: {item1: number, item2: boolean} = {item1: re.userId, item2: re.wasThere};
  listUpdate.push(el);
});

I'd personally suggest using a strongly-typed class instead of tuples, so you have more meaningful property names than Item1 and Item2.

You might also consider posting an object that has the list as one of its properties rather than posting the list itself: this will give you the flexibility to add more data to the object in the future, should the need arise, without needing to change as much code.


问:
Array of Integers to Arrays of Ranges
My Task:
I need to divide the Array into several Arrays of Arrays with the following properties:

every subarray is a range of continuous integer. As example [1,2,3,4,5] will be [[1,5]].

When there are no contiguous integer create a new subarray. As example [1,2,4,5] will be [[1,2], [4,5]]

Example:

If I have this Array of Integers - [0, 1, 5, 6, 3, 7]

Expected Result - [[0, 1], [3], [5, 7]]

I already tried this:

```<code>let array: [Int] = [0, 1, 3, 4, 5, 6, 7]

var group: [[Int]] = []
var temp: [Int] = [Int]()

for (index, element) in array.enumerated() {
    if index + 1 < array.count {
        let nextElement = array[index + 1]
        let step = nextElement - element

//        temp.append(element)

        if(step) == 1 { // Until it's in range
            temp.append(element)
        } else { // One-by-one
            temp.append(element)
            group.append(temp)

            temp = [Int]()
            group.append([nextElement])
        }
    } else {
        print(index)
    }
}

print(group)
</code>```

From my code, I get this result - [[0, 1], [3]]

答:
There is an API, IndexSet:

It's not clear what you want, your examples are ambiguous.

If you want an array of ranges

```<code>let indexSet = IndexSet(array)
let rangeView = indexSet.rangeView
let group = rangeView.map { $0.indices.startIndex..<$0.indices.endIndex }
</code>```

If you want a grouped array by ranges

```<code>let indexSet = IndexSet(array)
let rangeView = indexSet.rangeView
let group = rangeView.map { Array($0.indices) }
</code>```

Title: Array of Integers to Arrays of Ranges

Body:
My Task:
I need to divide the Array into several Arrays of Arrays with the following properties:

every subarray is a range of continuous integer. As example [1,2,3,4,5] will be [[1,5]].

When there are no contiguous integer create a new subarray. As example [1,2,4,5] will be [[1,2], [4,5]]

Example:

If I have this Array of Integers - [0, 1, 5, 6, 3, 7]

Expected Result - [[0, 1], [3], [5, 7]]

I already tried this:

let array: [Int] = [0, 1, 3, 4, 5, 6, 7]

var group: [[Int]] = []
var temp: [Int] = [Int]()

for (index, element) in array.enumerated() {
    if index + 1 < array.count {
        let nextElement = array[index + 1]
        let step = nextElement - element

//        temp.append(element)

        if(step) == 1 { // Until it's in range
            temp.append(element)
        } else { // One-by-one
            temp.append(element)
            group.append(temp)

            temp = [Int]()
            group.append([nextElement])
        }
    } else {
        print(index)
    }
}

print(group)

From my code, I get this result - [[0, 1], [3]]

Body:
There is an API, IndexSet:

It's not clear what you want, your examples are ambiguous.

If you want an array of ranges

let indexSet = IndexSet(array)
let rangeView = indexSet.rangeView
let group = rangeView.map { $0.indices.startIndex..<$0.indices.endIndex }

If you want a grouped array by ranges

let indexSet = IndexSet(array)
let rangeView = indexSet.rangeView
let group = rangeView.map { Array($0.indices) }


问:
How to output attributes of resources created?
I'm executing a GCP module to create a service account.

main.tf:

```<code>resource "google_service_account" "gsvc_account" {
    account_id   = "xxx"
    display_name = ""
    project      = "proj-yyy"
}

output "account_id" {
    value = "${google_service_account.gsvc_account.account_id}"
}
</code>```

Once the account is created, a terraform.tfstate file is created containing all details of the account.

terraform.tfstate

```<code>{
    "version": 4,
    "terraform_version": "0.12.0",
    "serial": 3,
    "lineage": "aaaa-bbbb-cccc",
    "outputs": {
        "xxx": {
            "value": "xxx",
            "type": "string"
        }
    },
    "resources": [
      {
            "module": "module.gsvc_tf",
            "mode": "managed",
            "type": "google_service_account",
            "name": "gsvc_account",
            "provider": "provider.google",
            "instances": [
                {
                    "schema_version": 0,
                    "attributes": {
                        "account_id": "xxx",
                        "display_name": "",
                        "email": "xxx@yyy.com",
                        "id": "projects/proj-yyy/serviceAccounts/xxx@yyy.com",
                        "name": "projects/proj-yyy/serviceAccounts/xxx@yyy.com",
                        "policy_data": null,
                        "project": "proj-xxx",
                        "unique_id": "10891885"
                    }
                }
            ]
        }
    ]
}
</code>```

As you can see above, in the module, I'm outputing the account_id input variable. Is there a way to output the attributes viz. id, name etc. so that they can be accessed by another module? The attributes are computed after the resource is created.

答:
From the docs for the google_service_account resource:

the following computed attributes are exported:

email - The e-mail address of the service account. This value should be referenced from any google_iam_policy data sources that would grant the service account privileges.

name - The fully-qualified name of the service account.

unique_id - The unique id of the service account.

You can declare outputs using these attributes in the same way as you declared your account_id output. For example:

```<code>   output "id" {
     value = "${google_service_account.gsvc_account.unique_id}"
   }

   output "email" {
     value = "${google_service_account.gsvc_account.email}"
   }

</code>```

Re this: "so that they can be accessed by another module" ... if the "other module" uses the same state file then the above outputs are addressable using ...

${google_service_account.gsvc_account.account_id}

${google_service_account.gsvc_account.email}

etc

... i.e. you don't need outputs at all. So, I'm guessing that the "other module" is in a separate project / workspace / repo and hence is using a different state file. If so, then you would access these outputs via remote state. For example, you would declare a remote state data source to point at whatever state contains your outputs:

```<code>resource "terraform_remote_state" "the_other_state" {
  backend = "..."
  config {
    ...
  }
}

</code>```

And then refer to the outputs within that state like so:

${terraform_remote_state.the_other_state.output.account_id}

${terraform_remote_state.the_other_state.output.email}

etc

Body:
I'm executing a GCP module to create a service account.

main.tf:

resource "google_service_account" "gsvc_account" {
    account_id   = "xxx"
    display_name = ""
    project      = "proj-yyy"
}

output "account_id" {
    value = "${google_service_account.gsvc_account.account_id}"
}

Once the account is created, a terraform.tfstate file is created containing all details of the account.

terraform.tfstate

{
    "version": 4,
    "terraform_version": "0.12.0",
    "serial": 3,
    "lineage": "aaaa-bbbb-cccc",
    "outputs": {
        "xxx": {
            "value": "xxx",
            "type": "string"
        }
    },
    "resources": [
      {
            "module": "module.gsvc_tf",
            "mode": "managed",
            "type": "google_service_account",
            "name": "gsvc_account",
            "provider": "provider.google",
            "instances": [
                {
                    "schema_version": 0,
                    "attributes": {
                        "account_id": "xxx",
                        "display_name": "",
                        "email": "xxx@yyy.com",
                        "id": "projects/proj-yyy/serviceAccounts/xxx@yyy.com",
                        "name": "projects/proj-yyy/serviceAccounts/xxx@yyy.com",
                        "policy_data": null,
                        "project": "proj-xxx",
                        "unique_id": "10891885"
                    }
                }
            ]
        }
    ]
}

As you can see above, in the module, I'm outputing the account_id input variable. Is there a way to output the attributes viz. id, name etc. so that they can be accessed by another module? The attributes are computed after the resource is created.

Body:
From the docs for the google_service_account resource:

the following computed attributes are exported:

email - The e-mail address of the service account. This value should be referenced from any google_iam_policy data sources that would grant the service account privileges.

name - The fully-qualified name of the service account.

unique_id - The unique id of the service account.

You can declare outputs using these attributes in the same way as you declared your account_id output. For example:

   output "id" {
     value = "${google_service_account.gsvc_account.unique_id}"
   }

   output "email" {
     value = "${google_service_account.gsvc_account.email}"
   }

Re this: "so that they can be accessed by another module" ... if the "other module" uses the same state file then the above outputs are addressable using ...

${google_service_account.gsvc_account.account_id}

${google_service_account.gsvc_account.email}

etc

... i.e. you don't need outputs at all. So, I'm guessing that the "other module" is in a separate project / workspace / repo and hence is using a different state file. If so, then you would access these outputs via remote state. For example, you would declare a remote state data source to point at whatever state contains your outputs:

resource "terraform_remote_state" "the_other_state" {
  backend = "..."
  config {
    ...
  }
}

And then refer to the outputs within that state like so:

${terraform_remote_state.the_other_state.output.account_id}

${terraform_remote_state.the_other_state.output.email}

etc


问:
Passion in CouchBase Programming
I would like to get the single element in the Couchbase document that is in the array of objects, but i am able to fetch the array of objects

i tried to fetch the array using the following query, 'select countryDetails from test';

```<code>{
   "type":"countries",
   "docName":"CountryData",
   "countryDetails":[
      {
         "name":"US",
         "code":"+1",
         "stateInfo":[
            {
               "name":"Florida",
               "id":"1212"
            },
            {
               "name":"NewYork",
               "id":"1214"
            }
         ]
      },
       {
         "name":"France",
         "code":"+33",
         "stateInfo":[
            {
               "name":"Grand Est",
               "id":"5212"
            },
            {
               "name":"Brittany",
               "id":"5214"
            }
         ]
      }
   ]
}
</code>```

i tried fetching array using, select countryDetails from test;

i like to fetch the result as [ {"name" : "US", "code" : "+1" }, {"name" : "France", "code" : "+33"}]

答:
If you project countryDetails it projects whole sub object.
If you need to part of sub object you need to explicitly project that.

The following ARRAY construction will provide the data representation you are expecting.

```<code>SELECT ARRAY {v.name,v.code} FOR v IN t.countryDetails END AS contryDetails
FROM test AS t
WHERE t.type = "countries";
</code>```

Title: Passion in CouchBase Programming

Body:
I would like to get the single element in the Couchbase document that is in the array of objects, but i am able to fetch the array of objects

i tried to fetch the array using the following query, 'select countryDetails from test';

{
   "type":"countries",
   "docName":"CountryData",
   "countryDetails":[
      {
         "name":"US",
         "code":"+1",
         "stateInfo":[
            {
               "name":"Florida",
               "id":"1212"
            },
            {
               "name":"NewYork",
               "id":"1214"
            }
         ]
      },
       {
         "name":"France",
         "code":"+33",
         "stateInfo":[
            {
               "name":"Grand Est",
               "id":"5212"
            },
            {
               "name":"Brittany",
               "id":"5214"
            }
         ]
      }
   ]
}

i tried fetching array using, select countryDetails from test;

i like to fetch the result as [ {"name" : "US", "code" : "+1" }, {"name" : "France", "code" : "+33"}]

Body:
If you project countryDetails it projects whole sub object.
If you need to part of sub object you need to explicitly project that.

The following ARRAY construction will provide the data representation you are expecting.

SELECT ARRAY {v.name,v.code} FOR v IN t.countryDetails END AS contryDetails
FROM test AS t
WHERE t.type = "countries";


问:
Is cursor over element (on page load)?
I have a .hover() action assigned to an element to hide/show something depending on the cursor being over it. The problem I am running into is that when the page loads and the cursor is OVER the element it doesn't register as being over because its not firing the mouseenter event.

Is there a another way to tell if the cursor is over a desired element?

答:
Using the mouse position on page load you could call

```<code>var currentElement = document.elementFromPoint(x, y);
</code>```

then if the element found at that position is the one with the hover event.  You can trigger that hover event manually

```<code>$(currentElement).trigger('hover');
</code>```

ContentLicense: CC BY-SA 3.0

Title: Is cursor over element (on page load)?

Body:
Using the mouse position on page load you could call

var currentElement = document.elementFromPoint(x, y);

then if the element found at that position is the one with the hover event.  You can trigger that hover event manually

$(currentElement).trigger('hover');


问:
Use line break in mPDF when data is from mysql table
I'm using mPDF to display data from mysql database. It happens the mPDF can't display the line break.

In database data is like :

one

two

three

the mPDF displays like this : one two three

I found an answer in this page :[How to put line break in mpdf when data is extracted from mysql database

But this cannot solve my problem.

My PHP CODE

```<code>$mpdf = new \Mpdf\Mpdf(['tempDir' => __DIR__ . '/custom/temp/dir/path','format' => 'A5']);

$sql ="SELECT * FROM  table";
$this->data['dataTable']= $this->db->query($sql);

$html = $this->load->view('admin/print/cirugia_toracia_report',$this->data,true);

$mpdf->WriteHTML($html);

$mpdf->Output();
</code>```

Some helps is greatly appreciated

答:
Try this please (TRUE big)

```<code>$data = $this->db->query($sql);
$html = $this->load->view('admin/print/cirugia_toracia_report',$data, TRUE);
</code>```

Or

```<code>$html = $this->load->view('admin/print/cirugia_toracia_report',$this->data, TRUE);
</code>```

Body:
I'm using mPDF to display data from mysql database. It happens the mPDF can't display the line break.

In database data is like :

one

two

three

the mPDF displays like this : one two three

I found an answer in this page :[How to put line break in mpdf when data is extracted from mysql database

But this cannot solve my problem.

My PHP CODE

$mpdf = new \Mpdf\Mpdf(['tempDir' => __DIR__ . '/custom/temp/dir/path','format' => 'A5']);

$sql ="SELECT * FROM  table";
$this->data['dataTable']= $this->db->query($sql);

$html = $this->load->view('admin/print/cirugia_toracia_report',$this->data,true);

$mpdf->WriteHTML($html);

$mpdf->Output();

Some helps is greatly appreciated

Body:
Try this please (TRUE big)

$data = $this->db->query($sql);
$html = $this->load->view('admin/print/cirugia_toracia_report',$data, TRUE);

Or

$html = $this->load->view('admin/print/cirugia_toracia_report',$this->data, TRUE);


问:
selecting whole partitions uses index - why?
I am selecting all the data from some partitions of a partitioned table (Oracle 11g - actually updating in the real case, but for the example a select shows the same behaviour that I would like to understand). Can you explain to me why Oracle decides to use the index instead of a full scan? In my understanding a full scan would be the smarter access method. Why does Oracle think an index range scan + table access by index rowid batched is smarter than a full scan of the partitions?

Test table:

```<code>CREATE TABLE t_test
    (ID                NUMBER                 NOT NULL ENABLE,
    PARTITION_NUMBER   NUMBER                 NOT NULL ENABLE,
    CREATION_TIMESTAMP DATE   DEFAULT SYSDATE NOT NULL ENABLE,
    CONSTRAINT PK_t_test PRIMARY KEY (PARTITION_NUMBER, ID) USING INDEX LOCAL
    )
PARTITION BY LIST (PARTITION_NUMBER)
    (
    PARTITION P1  VALUES (1) SEGMENT CREATION IMMEDIATE,
    PARTITION P2  VALUES (2) SEGMENT CREATION IMMEDIATE,
    PARTITION P3  VALUES (3) SEGMENT CREATION IMMEDIATE,
    PARTITION P4  VALUES (4) SEGMENT CREATION IMMEDIATE,
    PARTITION P5  VALUES (5) SEGMENT CREATION IMMEDIATE,
    PARTITION P6  VALUES (6) SEGMENT CREATION IMMEDIATE,
    PARTITION P7  VALUES (7) SEGMENT CREATION IMMEDIATE,
    PARTITION P8  VALUES (8) SEGMENT CREATION IMMEDIATE,
    PARTITION P9  VALUES (9) SEGMENT CREATION IMMEDIATE
    );
</code>```

Selecting the data from some partitions (nothing was inserted for this example, but behaviour is the same with data in the partitions):

```<code>explain plan for select * from t_test where PARTITION_NUMBER in (2,3,4,5,6,7);
SELECT * FROM TABLE(dbms_xplan.display);

Plan hash value: 3284178661

-------------------------------------------------------------------------------------------------------------------------
| Id  | Operation                                   | Name      | Rows  | Bytes | Cost (%CPU)| Time     | Pstart| Pstop |
-------------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                            |           |     1 |    35 |     1   (0)| 00:00:01 |       |       |
|   1 |  INLIST ITERATOR                            |           |       |       |            |          |       |       |
|   2 |   PARTITION LIST ITERATOR                   |           |     1 |    35 |     1   (0)| 00:00:01 |KEY(I) |KEY(I) |
|   3 |    TABLE ACCESS BY LOCAL INDEX ROWID BATCHED| T_TEST    |     1 |    35 |     1   (0)| 00:00:01 |KEY(I) |KEY(I) |
|*  4 |     INDEX RANGE SCAN                        | PK_T_TEST |     1 |       |     2   (0)| 00:00:01 |KEY(I) |KEY(I) |
-------------------------------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   4 - access("PARTITION_NUMBER"=2 OR "PARTITION_NUMBER"=3 OR "PARTITION_NUMBER"=4 OR "PARTITION_NUMBER"=5 OR
              "PARTITION_NUMBER"=6 OR "PARTITION_NUMBER"=7)

Note
-----
   - dynamic statistics used: dynamic sampling (level=2)
</code>```

I don't understand why it does the range scan... Why not just like this (same query but with FULL-hint)? Why does it ever think using the index is the better approach here? (Even an ALL_ROWS hint does not change the behavior) :

```<code>explain plan for select /*+ full(t_test) */ * from t_test where PARTITION_NUMBER in (2,3,4,5,6,7);
SELECT * FROM TABLE(dbms_xplan.display);

Plan hash value: 3335595461

------------------------------------------------------------------------------------------------
| Id  | Operation             | Name   | Rows  | Bytes | Cost (%CPU)| Time     | Pstart| Pstop |
------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT      |        |     1 |    35 |     2   (0)| 00:00:01 |       |       |
|   1 |  PARTITION LIST INLIST|        |     1 |    35 |     2   (0)| 00:00:01 |KEY(I) |KEY(I) |
|   2 |   TABLE ACCESS FULL   | T_TEST |     1 |    35 |     2   (0)| 00:00:01 |KEY(I) |KEY(I) |
------------------------------------------------------------------------------------------------

Note
-----
   - dynamic statistics used: dynamic sampling (level=2)
</code>```

答:
Because the index tells oracle what partitions your data are in.  When you say "full scan" do you mean full scan of the table,  or read all the rows in the specified partitions?  The "TABLE ACCESS FULL" row in second explain plan has KEY(I) in the Pstart and Pstop columns, so Oracle is only going to scan all rows in those partitions, not the whole table.

So the partitioning is limiting reading just the partitions in the where clause, and it is reading all rows in the specific partitions and using the index to figure out what partitions to read.  It is using a range scan to look for the leading column value in your primary key.  In either explain plan, it is using the index to look just at the partitions that contain the specified PARTITION_NUMBER -- in the first explain plan you see 4 = access meaning it is using the index.

Body:
I am selecting all the data from some partitions of a partitioned table (Oracle 11g - actually updating in the real case, but for the example a select shows the same behaviour that I would like to understand). Can you explain to me why Oracle decides to use the index instead of a full scan? In my understanding a full scan would be the smarter access method. Why does Oracle think an index range scan + table access by index rowid batched is smarter than a full scan of the partitions?

Test table:

CREATE TABLE t_test
    (ID                NUMBER                 NOT NULL ENABLE,
    PARTITION_NUMBER   NUMBER                 NOT NULL ENABLE,
    CREATION_TIMESTAMP DATE   DEFAULT SYSDATE NOT NULL ENABLE,
    CONSTRAINT PK_t_test PRIMARY KEY (PARTITION_NUMBER, ID) USING INDEX LOCAL
    )
PARTITION BY LIST (PARTITION_NUMBER)
    (
    PARTITION P1  VALUES (1) SEGMENT CREATION IMMEDIATE,
    PARTITION P2  VALUES (2) SEGMENT CREATION IMMEDIATE,
    PARTITION P3  VALUES (3) SEGMENT CREATION IMMEDIATE,
    PARTITION P4  VALUES (4) SEGMENT CREATION IMMEDIATE,
    PARTITION P5  VALUES (5) SEGMENT CREATION IMMEDIATE,
    PARTITION P6  VALUES (6) SEGMENT CREATION IMMEDIATE,
    PARTITION P7  VALUES (7) SEGMENT CREATION IMMEDIATE,
    PARTITION P8  VALUES (8) SEGMENT CREATION IMMEDIATE,
    PARTITION P9  VALUES (9) SEGMENT CREATION IMMEDIATE
    );

Selecting the data from some partitions (nothing was inserted for this example, but behaviour is the same with data in the partitions):

explain plan for select * from t_test where PARTITION_NUMBER in (2,3,4,5,6,7);
SELECT * FROM TABLE(dbms_xplan.display);

Plan hash value: 3284178661

-------------------------------------------------------------------------------------------------------------------------
| Id  | Operation                                   | Name      | Rows  | Bytes | Cost (%CPU)| Time     | Pstart| Pstop |
-------------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                            |           |     1 |    35 |     1   (0)| 00:00:01 |       |       |
|   1 |  INLIST ITERATOR                            |           |       |       |            |          |       |       |
|   2 |   PARTITION LIST ITERATOR                   |           |     1 |    35 |     1   (0)| 00:00:01 |KEY(I) |KEY(I) |
|   3 |    TABLE ACCESS BY LOCAL INDEX ROWID BATCHED| T_TEST    |     1 |    35 |     1   (0)| 00:00:01 |KEY(I) |KEY(I) |
|*  4 |     INDEX RANGE SCAN                        | PK_T_TEST |     1 |       |     2   (0)| 00:00:01 |KEY(I) |KEY(I) |
-------------------------------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   4 - access("PARTITION_NUMBER"=2 OR "PARTITION_NUMBER"=3 OR "PARTITION_NUMBER"=4 OR "PARTITION_NUMBER"=5 OR
              "PARTITION_NUMBER"=6 OR "PARTITION_NUMBER"=7)

Note
-----
   - dynamic statistics used: dynamic sampling (level=2)

I don't understand why it does the range scan... Why not just like this (same query but with FULL-hint)? Why does it ever think using the index is the better approach here? (Even an ALL_ROWS hint does not change the behavior) :

explain plan for select /*+ full(t_test) */ * from t_test where PARTITION_NUMBER in (2,3,4,5,6,7);
SELECT * FROM TABLE(dbms_xplan.display);

Plan hash value: 3335595461

------------------------------------------------------------------------------------------------
| Id  | Operation             | Name   | Rows  | Bytes | Cost (%CPU)| Time     | Pstart| Pstop |
------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT      |        |     1 |    35 |     2   (0)| 00:00:01 |       |       |
|   1 |  PARTITION LIST INLIST|        |     1 |    35 |     2   (0)| 00:00:01 |KEY(I) |KEY(I) |
|   2 |   TABLE ACCESS FULL   | T_TEST |     1 |    35 |     2   (0)| 00:00:01 |KEY(I) |KEY(I) |
------------------------------------------------------------------------------------------------

Note
-----
   - dynamic statistics used: dynamic sampling (level=2)


问:
Rails active-record array gives 0 on count but record exists in db?
can anyone help me to solve this mystery?

checkout on image you will understand it

Ruby version - 2.6.0
Rails - 5.2.2

```<code>(byebug) volume_prices
#<ActiveRecord::AssociationRelation [#<Spree::VoumePrice id: 5, variant_id: 4, name: nil, range: "(1..5)", amount: 0.109e3, position: 2, created_at: "2019-05-30 12:49:56", updated_at: "2019-05-30 12:49:56", discount_type: "price", role_id: nil, volume_price_model_id: nil, user_id: nil, supplier_id: 3, pricing_tier_id: 5>]>
(byebug) volume_prices.count
0
(byebug) volume_prices.length
1
(byebug) Spree::VolumePrice.find(5)
#<Spree::VolumePrice id: 5, variant_id: 4, name: nil, range: "(1..5)", amount: 0.1e2, position: 1, created_at: "2019-05-30 12:54:00", updated_at: "2019-05-30 12:54:00", discount_type: "price", role_id: nil, volume_price_model_id: nil, user_id: nil, supplier_id: 4, pricing_tier_id: 5>
</code>```

答:
An ActiveRecord::Relation can be loaded in memory. When a relation is loaded, you can work on it without hitting the database with queries.

The mystery in your case is that volume_prices is a relation that has been loaded in memory, so .length method will not perform any query on the database. However, .count will perform a query, because it is a calculation method of ActiveRecord.

I think that the source code of Activerecord size method is the perfect example of this approach, because it checks if the relation is loaded before decide which method will use between count and length.

You can check all of this by running this lines in your very example:

```<code>volume_prices.loaded?       # should return true
volume_prices.count         # should return 0
volume_prices.length        # should return 1
volume_prices.reload.length # should return 0
</code>```

Body:
can anyone help me to solve this mystery?

checkout on image you will understand it

Ruby version - 2.6.0
Rails - 5.2.2

(byebug) volume_prices
#<ActiveRecord::AssociationRelation [#<Spree::VoumePrice id: 5, variant_id: 4, name: nil, range: "(1..5)", amount: 0.109e3, position: 2, created_at: "2019-05-30 12:49:56", updated_at: "2019-05-30 12:49:56", discount_type: "price", role_id: nil, volume_price_model_id: nil, user_id: nil, supplier_id: 3, pricing_tier_id: 5>]>
(byebug) volume_prices.count
0
(byebug) volume_prices.length
1
(byebug) Spree::VolumePrice.find(5)
#<Spree::VolumePrice id: 5, variant_id: 4, name: nil, range: "(1..5)", amount: 0.1e2, position: 1, created_at: "2019-05-30 12:54:00", updated_at: "2019-05-30 12:54:00", discount_type: "price", role_id: nil, volume_price_model_id: nil, user_id: nil, supplier_id: 4, pricing_tier_id: 5>

Body:
An ActiveRecord::Relation can be loaded in memory. When a relation is loaded, you can work on it without hitting the database with queries.

The mystery in your case is that volume_prices is a relation that has been loaded in memory, so .length method will not perform any query on the database. However, .count will perform a query, because it is a calculation method of ActiveRecord.

I think that the source code of Activerecord size method is the perfect example of this approach, because it checks if the relation is loaded before decide which method will use between count and length.

You can check all of this by running this lines in your very example:

volume_prices.loaded?       # should return true
volume_prices.count         # should return 0
volume_prices.length        # should return 1
volume_prices.reload.length # should return 0


问:
Powershell to move Excel files based on Initial part of the file name
I have hundreds of excel files like the following in directory called D:\TestARC_Source

```<code>5020190429.dat
5120190429.dat
602019111121.dat
702019050926.dat
etc.
</code>```

I need a script to move them to respective folders based on first two characters of file name. Folders are already created on target directory.

```<code>D:\TestARC_Destination\file_50
D:\TestARC_Destination\file_51
D:\TestARC_Destination\file_60
D:\TestARC_Destination\file_70
</code>```

Files should move to respective folders based on first two characters of file name. For example 5020190429.dat moves to 50_file folder.

I am trying to edit below script but it's not working. I have a very little knowledge in PS script and I'll appreciate if you can help me out.

```<code>$SourceFolder = "C:\Users\Administrator\Desktop\ARC_Testing\TestARC_Source\"
$targetFolder = "C:\Users\Administrator\Desktop\ARC_Testing\TestARC_Destination\"

# Find all files matching *.sql in the folder specified
Get-ChildItem -Path $SourceFolder -Filter *.dat | ForEach-Object {

    # Combine the source filename and target directory
    # The source filename has all instances of _ replaced with \
    # Cast the resulting string to a FileInfo object to take advantage of extra methods
    [System.IO.FileInfo]$destination = (Join-Path -Path $targetFolder -ChildPath $_.Name.replace("_","\"))

    # Create the directory if it doesn't already exits
    if (!(Test-Path) $destination.Directory.FullName)
    {
        New-item -Path $destination.Directory.FullName -ItemType Directory
    }

    # Copy the source to the target directory
    copy-item -Path $_.FullName -Destination $Destination.FullName
}
</code>```

答:
An efficient approach would be to group the files by the 1st two digits,

check if the folder exists (and create if necessary) and move the files in one go.

```<code>## Q:\Test\2019\05\31\SO_56397901.ps1
$SourceFolder = "C:\Users\Administrator\Desktop\ARC_Testing\TestARC_Source\"
$targetFolder = "C:\Users\Administrator\Desktop\ARC_Testing\TestARC_Destination\"

Get-ChildItem -Path "$SourceFolder[0-9][0-9]*" -Filter *.dat |
   Group-Object {"file_"+$_.BaseName.Substring(0,2)}| ForEach-Object {
      $TargetDir = Join-Path $targetFolder $_.Name
      if(!(Test-Path $TargetDir)){MD $TargetDir | Out-Null}
      $_.Group.FullName | Move-Item -Destination $TargetDir #-WhatIf
}
</code>```

Tree after running with your above testdata:

```<code>> TREE /f
├───FILE_50
│       5020190429.DAT
│
├───FILE_51
│       5120190429.DAT
│
├───FILE_60
│       602019111121.DAT
│
└───FILE_70
        702019050926.DAT
</code>```

EDIT: Changing Requirements usually afford a new question, nevertheless the changes are minimal

```<code>## Q:\Test\2019\05\31\SO_56397901.ps1
$SourceFolder = "C:\Users\Administrator\Desktop\ARC_Testing\TestARC_Source\"
$targetFolder = "C:\Users\Administrator\Desktop\ARC_Testing\TestARC_Destination\"

Get-ChildItem -Path "$SourceFolder[0-9][0-9]*" -Filter *.dat |
   Group-Object {$_.BaseName.Substring(0,2)+"_file"}| ForEach-Object {
      $TargetDir = Join-Path $targetFolder $_.Name
      if(Test-Path $TargetDir){
          $_.Group.FullName | Move-Item -Destination $TargetDir #-WhatIf
      } else {
          "{0} doesn't exist, files not moved: {1}" -f $TargetDir,($_.Group.FullName -join ';')
      }
}
</code>```

Body:
I have hundreds of excel files like the following in directory called D:\TestARC_Source

5020190429.dat
5120190429.dat
602019111121.dat
702019050926.dat
etc.

I need a script to move them to respective folders based on first two characters of file name. Folders are already created on target directory.

D:\TestARC_Destination\file_50
D:\TestARC_Destination\file_51
D:\TestARC_Destination\file_60
D:\TestARC_Destination\file_70

Files should move to respective folders based on first two characters of file name. For example 5020190429.dat moves to 50_file folder.

I am trying to edit below script but it's not working. I have a very little knowledge in PS script and I'll appreciate if you can help me out.

$SourceFolder = "C:\Users\Administrator\Desktop\ARC_Testing\TestARC_Source\"
$targetFolder = "C:\Users\Administrator\Desktop\ARC_Testing\TestARC_Destination\"

# Find all files matching *.sql in the folder specified
Get-ChildItem -Path $SourceFolder -Filter *.dat | ForEach-Object {

    # Combine the source filename and target directory
    # The source filename has all instances of _ replaced with \
    # Cast the resulting string to a FileInfo object to take advantage of extra methods
    [System.IO.FileInfo]$destination = (Join-Path -Path $targetFolder -ChildPath $_.Name.replace("_","\"))

    # Create the directory if it doesn't already exits
    if (!(Test-Path) $destination.Directory.FullName)
    {
        New-item -Path $destination.Directory.FullName -ItemType Directory
    }

    # Copy the source to the target directory
    copy-item -Path $_.FullName -Destination $Destination.FullName
}

Body:
An efficient approach would be to group the files by the 1st two digits,

check if the folder exists (and create if necessary) and move the files in one go.

## Q:\Test\2019\05\31\SO_56397901.ps1
$SourceFolder = "C:\Users\Administrator\Desktop\ARC_Testing\TestARC_Source\"
$targetFolder = "C:\Users\Administrator\Desktop\ARC_Testing\TestARC_Destination\"

Get-ChildItem -Path "$SourceFolder[0-9][0-9]*" -Filter *.dat |
   Group-Object {"file_"+$_.BaseName.Substring(0,2)}| ForEach-Object {
      $TargetDir = Join-Path $targetFolder $_.Name
      if(!(Test-Path $TargetDir)){MD $TargetDir | Out-Null}
      $_.Group.FullName | Move-Item -Destination $TargetDir #-WhatIf
}

Tree after running with your above testdata:

> TREE /f
├───FILE_50
│       5020190429.DAT
│
├───FILE_51
│       5120190429.DAT
│
├───FILE_60
│       602019111121.DAT
│
└───FILE_70
        702019050926.DAT

EDIT: Changing Requirements usually afford a new question, nevertheless the changes are minimal

## Q:\Test\2019\05\31\SO_56397901.ps1
$SourceFolder = "C:\Users\Administrator\Desktop\ARC_Testing\TestARC_Source\"
$targetFolder = "C:\Users\Administrator\Desktop\ARC_Testing\TestARC_Destination\"

Get-ChildItem -Path "$SourceFolder[0-9][0-9]*" -Filter *.dat |
   Group-Object {$_.BaseName.Substring(0,2)+"_file"}| ForEach-Object {
      $TargetDir = Join-Path $targetFolder $_.Name
      if(Test-Path $TargetDir){
          $_.Group.FullName | Move-Item -Destination $TargetDir #-WhatIf
      } else {
          "{0} doesn't exist, files not moved: {1}" -f $TargetDir,($_.Group.FullName -join ';')
      }
}


问:
How can I clip the center column of text during a table resize but leave 2 side columns alone?
I have a table with three columns of text that I'm having trouble getting to resize properly. The middle column holds a lot of non-crucial text that should be clipped (with an ellipsis) when the page is resized, but the two edge columns hold text that cannot be clipped. None of the text should be wrapped. Here's some example HTML:

```<code><table id="resize-table">
<thead>
    <tr>
        <th class="col-1">Column 1</th>
        <th class="col-2">Column 2</th>
        <th class="col-3">Column 3</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td class="col-1">Unclippable text</td>
        <td class="col-2">Clippable text that is very long. Clippable text that is very long.</td>
        <td class="col-3">Unclippable text</td>
    </tr>
</tbody>
</code>```

And here's the css that I'm using:

```<code>#resize-table{
    width:50%;
    border-collapse:collapse;
}
#resize-table th{
    padding:0.2em;
    border:1px solid black;
}
#resize-table td{
    padding:0.2em;
    border:1px solid black;
    white-space:nowrap;
}
#resize-table .col-2{
    overflow: hidden;
    text-overflow: ellipsis;
}
</code>```

When I resize the page the middle column is never clipped and the ellipsis is never shown. Instead, the table cells just stops shrinking when they reach the width of the content. I've played around a lot with different width settings, but haven't gotten anything to work properly. I've also tried table-layout:fixed but that just results in all the columns being resized equally. Any ideas?

EDIT: I'm viewing this in Chrome, but a cross-browser solution would be ideal.

答:
I can't say if this could be done exclusively in CSS but if you would consider some jQuery you might be able to achieve something like it. Consider the following.

```<code> $(window).resize(function() {
  //if the table gets less that 300 say
  if ($("#resize-table").width() < 300) {
      //class small added so we do not repeat the operation unnecessarily
      if (!$(".col-2").hasClass("small")) {
        $(".col-2").each(function(){
            //put the content in a hidden div for later and only display the ellipses
            $(this).html("..." + "<div class='content' style='display:none'>" + $(this).html() + "</div>").addClass("small");
        });
    }
} else {
    //else the table is large enough so
    if ($(".col-2").hasClass("small")) {
        $(".col-2").each(function() {
            //copy the td content back out of the hidden div
            $(this).html($(this).find(".content").html()).removeClass("small");
        });
    }
 }
});
</code>```

Now, I haven't tried this in any rigorous way but it does the job. So its a rudimentary demonstration for feasibility.

Body:
I have a table with three columns of text that I'm having trouble getting to resize properly. The middle column holds a lot of non-crucial text that should be clipped (with an ellipsis) when the page is resized, but the two edge columns hold text that cannot be clipped. None of the text should be wrapped. Here's some example HTML:

        Column 1
        Column 2
        Column 3

        Unclippable text
        Clippable text that is very long. Clippable text that is very long.
        Unclippable text

And here's the css that I'm using:

#resize-table{
    width:50%;
    border-collapse:collapse;
}
#resize-table th{
    padding:0.2em;
    border:1px solid black;
}
#resize-table td{
    padding:0.2em;
    border:1px solid black;
    white-space:nowrap;
}
#resize-table .col-2{
    overflow: hidden;
    text-overflow: ellipsis;
}

When I resize the page the middle column is never clipped and the ellipsis is never shown. Instead, the table cells just stops shrinking when they reach the width of the content. I've played around a lot with different width settings, but haven't gotten anything to work properly. I've also tried table-layout:fixed but that just results in all the columns being resized equally. Any ideas?

EDIT: I'm viewing this in Chrome, but a cross-browser solution would be ideal.

Body:
I can't say if this could be done exclusively in CSS but if you would consider some jQuery you might be able to achieve something like it. Consider the following.

 $(window).resize(function() {
  //if the table gets less that 300 say
  if ($("#resize-table").width() < 300) {
      //class small added so we do not repeat the operation unnecessarily
      if (!$(".col-2").hasClass("small")) {
        $(".col-2").each(function(){
            //put the content in a hidden div for later and only display the ellipses
            $(this).html("..." + "<div class='content' style='display:none'>" + $(this).html() + "</div>").addClass("small");
        });
    }
} else {
    //else the table is large enough so
    if ($(".col-2").hasClass("small")) {
        $(".col-2").each(function() {
            //copy the td content back out of the hidden div
            $(this).html($(this).find(".content").html()).removeClass("small");
        });
    }
 }
});

Now, I haven't tried this in any rigorous way but it does the job. So its a rudimentary demonstration for feasibility.


问:
google.api_core.exceptions.ServiceUnavailable: 503 Name resolution failure
I'm trying to iterate through images in a directory and get their labels through goodle_api_vision. I am getting the error in google.api_core.exceptions.ServiceUnavailable: 503 Name resolution failure not sure what the issue here is?
This is my code:

```<code>import io
import os

# Imports the Google Cloud client library
from google.cloud import vision
from google.cloud.vision import types

# Instantiates a client
client = vision.ImageAnnotatorClient()

# The name of the image file to annotate
file_name = os.path.join(
    os.path.dirname(__file__),
    'IronMan.PNG')

# Loads the image into memory
with io.open(file_name, 'rb') as image_file:
    content = image_file.read()

image = types.Image(content=content)

# Performs label detection on the image file
response = client.label_detection(image=image)
labels = response.label_annotations

print('Labels:')
for label in labels:
    print(label.description)
</code>```

the error that I get is at this part of code:

```<code> File "geo.py", line 23, in <module>
    response = client.label_detection(image=image)
  File "C:\Users\MyDrive\AppData\Local\Continuum\anaconda3\lib\site-packages\google\cloud\vision_helpers\decorators.py", line 111, in inner
    response = self.annotate_image(request, retry=retry, timeout=timeout)
  File "C:\Users\shubhami\AppData\Local\Continuum\anaconda3\lib\site-packages\google\cloud\vision_helpers\__init__.py", line 71, in annotate_image
    r = self.batch_annotate_images([request], retry=retry, timeout=timeout)
  File "C:\Users\MyDrive\AppData\Local\Continuum\anaconda3\lib\site-packages\google\cloud\vision_v1\gapic\image_annotator_client.py", line 233, in batch_annotate_images

    request, retry=retry, timeout=timeout, metadata=metadata)
  File "C:\Users\shubhami\AppData\Local\Continuum\anaconda3\lib\site-packages\google\api_core\gapic_v1\method.py", line 139, in __call__
    return wrapped_func(*args, **kwargs)
  File "C:\Users\MyDrive\AppData\Local\Continuum\anaconda3\lib\site-packages\google\api_core\grpc_helpers.py", line 61, in error_remapped_callable
    six.raise_from(exceptions.from_grpc_error(exc), exc)
  File "<string>", line 3, in raise_from
google.api_core.exceptions.ServiceUnavailable: 503 Name resolution failure
</code>```

答: Had the same issue while trying to FCMNotification from pyfcm package. Slove if by adding: os.environ['GRPC_DNS_RESOLVER'] = 'native'

Body:
I'm trying to iterate through images in a directory and get their labels through goodle_api_vision. I am getting the error in google.api_core.exceptions.ServiceUnavailable: 503 Name resolution failure not sure what the issue here is?
This is my code:

import io
import os

# Imports the Google Cloud client library
from google.cloud import vision
from google.cloud.vision import types

# Instantiates a client
client = vision.ImageAnnotatorClient()

# The name of the image file to annotate
file_name = os.path.join(
    os.path.dirname(__file__),
    'IronMan.PNG')

# Loads the image into memory
with io.open(file_name, 'rb') as image_file:
    content = image_file.read()

image = types.Image(content=content)

# Performs label detection on the image file
response = client.label_detection(image=image)
labels = response.label_annotations

print('Labels:')
for label in labels:
    print(label.description)

the error that I get is at this part of code:

 File "geo.py", line 23, in <module>
    response = client.label_detection(image=image)
  File "C:\Users\MyDrive\AppData\Local\Continuum\anaconda3\lib\site-packages\google\cloud\vision_helpers\decorators.py", line 111, in inner
    response = self.annotate_image(request, retry=retry, timeout=timeout)
  File "C:\Users\shubhami\AppData\Local\Continuum\anaconda3\lib\site-packages\google\cloud\vision_helpers\__init__.py", line 71, in annotate_image
    r = self.batch_annotate_images([request], retry=retry, timeout=timeout)
  File "C:\Users\MyDrive\AppData\Local\Continuum\anaconda3\lib\site-packages\google\cloud\vision_v1\gapic\image_annotator_client.py", line 233, in batch_annotate_images

    request, retry=retry, timeout=timeout, metadata=metadata)
  File "C:\Users\shubhami\AppData\Local\Continuum\anaconda3\lib\site-packages\google\api_core\gapic_v1\method.py", line 139, in __call__
    return wrapped_func(*args, **kwargs)
  File "C:\Users\MyDrive\AppData\Local\Continuum\anaconda3\lib\site-packages\google\api_core\grpc_helpers.py", line 61, in error_remapped_callable
    six.raise_from(exceptions.from_grpc_error(exc), exc)
  File "<string>", line 3, in raise_from
google.api_core.exceptions.ServiceUnavailable: 503 Name resolution failure


问:
sum and store data from for statement
I need to memorize the data that comes out of this FOR statement

```<code>for (UTEModelSportWalkRun *model in arraySport) {
    NSLog(@"sport *** time = %@, Total step = %ld, walkDistance = %f , walkCalorie = %f, runDistance = %f, runCalorie = %f",
    model.time, (long)model.stepsTotal, model.walkDistances, model.walkCalories, model.runDistances, model.runCalories);
}
</code>```

I need to add up all the model.stepTotal and memorize the sum in some way. Now it comes out like this:

sport *** time = 2019-05-30-17, Total step = 14, walkDistance = 9.764355, walkCalorie = 0.346086, runDistance = 0.000000, runCalories = 0.000000

  sport *** time = 2019-05-31-10, Total step = 67, walkDistance = 46.729411, walkCalorie = 1.656267, runDistance = 0.000000, runCalories = 0.000000

  sport *** time = 2019-05-31-11, Total step = 520, walkDistance = 362.676028, walkCalorie = 12.854608,
  runDistance = 0.000000, runCalories = 0.000000

.....

答:
```<code>long count = 0;
for (UTEModelSportWalkRun *model in arraySport) {
      count += (long)model.stepsTotal;
}
</code>```

Declare the variable before the loop and enjoy accessing it after. It's called Scoping.

Title: sum and store data from for statement

Body:
I need to memorize the data that comes out of this FOR statement

for (UTEModelSportWalkRun *model in arraySport) {
    NSLog(@"sport *** time = %@, Total step = %ld, walkDistance = %f , walkCalorie = %f, runDistance = %f, runCalorie = %f",
    model.time, (long)model.stepsTotal, model.walkDistances, model.walkCalories, model.runDistances, model.runCalories);
}

I need to add up all the model.stepTotal and memorize the sum in some way. Now it comes out like this:

sport *** time = 2019-05-30-17, Total step = 14, walkDistance = 9.764355, walkCalorie = 0.346086, runDistance = 0.000000, runCalories = 0.000000

  sport *** time = 2019-05-31-10, Total step = 67, walkDistance = 46.729411, walkCalorie = 1.656267, runDistance = 0.000000, runCalories = 0.000000

  sport *** time = 2019-05-31-11, Total step = 520, walkDistance = 362.676028, walkCalorie = 12.854608,
  runDistance = 0.000000, runCalories = 0.000000

.....

Body:
long count = 0;
for (UTEModelSportWalkRun *model in arraySport) {
      count += (long)model.stepsTotal;
}

Declare the variable before the loop and enjoy accessing it after. It's called Scoping.


问:
Cannot trigger data selected in vue-select when show record
I am using vue-select. I select province_id then create record success, but when show record, I cannot trigger data selected to select2.</p>

```<code><v-select
  v-model="form.province_id"
  name="province_id"
  :options="provinces"
  :reduce="province => province.province_id"
  label="name"
  :clearable="false"
  :searchable="true" />
</code>```

```<code>import vSelect from 'vue-select'

export default {
  components: { 'v-select': vSelect },
  data () {
    const form = {
      province_id: ''
    }
    return {
      provinces: []
    }
  },
}```
</code>```

答:
Your data declaration is wrong, you need to return form inside the returned object.

```<code>import vSelect from 'vue-select'

export default {
  components: { 'v-select': vSelect },
  data () {
    const
    return {
      provinces: [],
      form: {
          province_id: undefined
      }
    }
  },
}
</code>```

Vue.component('v-select', VueSelect.VueSelect);

new Vue({
  el: '#app',
  data() {
    return {
      provinces: [
        { name: 'province 1', province_id: 'p_1' },
        { name: 'province 2', province_id: 'p_2' },
        { name: 'province 3', province_id: 'p_3' },
      ],
      form: {
        province_id: ''
      }
    }
  },
})```
<pre class="snippet-code-html lang-html prettyprint-override"><code><script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<!-- use the latest vue-select release -->
<script src="https://unpkg.com/vue-select@latest"></script>
<link rel="stylesheet" href="https://unpkg.com/vue-select@latest/dist/vue-select.css">

<div id="app">
   Selected {{ form.province_id }}
  <v-select
    v-model="form.province_id"
    name="province_id"
    :options="provinces"
    :reduce="province => province.province_id"
    label="name"
    :clearable="false"
    :searchable="true" />
</div></code>```

Body:
I am using vue-select. I select province_id then create record success, but when show record, I cannot trigger data selected to select2.

<v-select
  v-model="form.province_id"
  name="province_id"
  :options="provinces"
  :reduce="province => province.province_id"
  label="name"
  :clearable="false"
  :searchable="true" />

import vSelect from 'vue-select'

export default {
  components: { 'v-select': vSelect },
  data () {
    const form = {
      province_id: ''
    }
    return {
      provinces: []
    }
  },
}```

Body:
Your data declaration is wrong, you need to return form inside the returned object.

import vSelect from 'vue-select'

export default {
  components: { 'v-select': vSelect },
  data () {
    const
    return {
      provinces: [],
      form: {
          province_id: undefined
      }
    }
  },
}

Vue.component('v-select', VueSelect.VueSelect);

new Vue({
  el: '#app',
  data() {
    return {
      provinces: [
        { name: 'province 1', province_id: 'p_1' },
        { name: 'province 2', province_id: 'p_2' },
        { name: 'province 3', province_id: 'p_3' },
      ],
      form: {
        province_id: ''
      }
    }
  },
})

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<!-- use the latest vue-select release -->
<script src="https://unpkg.com/vue-select@latest"></script>
<link rel="stylesheet" href="https://unpkg.com/vue-select@latest/dist/vue-select.css">

<div id="app">
   Selected {{ form.province_id }}
  <v-select
    v-model="form.province_id"
    name="province_id"
    :options="provinces"
    :reduce="province => province.province_id"
    label="name"
    :clearable="false"
    :searchable="true" />
</div>


问:
CSS transform does not get applied on elements selected by :nth-child()
Consider the following HTML:

```<code><p>
  <span> A </span>
  <span> A </span>
  <span> A </span>
</p>
</code>```

Now try applying this CSS on it:

```<code>p span:nth-of-type(2) {transform: rotateX( 180deg ) ; color: blue }
</code>```

You will see that the color has been changed on the second span, but the transform property has not. Could anyone explain why this is the case?

答: It seems the reason is that css transform works only on "block" elements. Since  is an "inline"element, it will not be affected.</p>

Body:
Consider the following HTML:

   A
   A
   A

Now try applying this CSS on it:

p span:nth-of-type(2) {transform: rotateX( 180deg ) ; color: blue }

You will see that the color has been changed on the second span, but the transform property has not. Could anyone explain why this is the case?


问:
how to get http content with Sharppcap
For my school project we have to do a man in the middle. But we don't succeed in getting HTTP content. I wrote a little thing to reassemble tcp payload data but I only get the HTTP Header and not the source code. I see that on wireshark data are uncompressed but I don't know what this mean.

This is my code
TcpSession class

 public class TcpSession : IDisposable
    {
        private MemoryStream _clientBuffer;
        private MemoryStream _serverBuffer;

        public TcpSession()
        {
            _clientBuffer = new MemoryStream();
            _serverBuffer = new MemoryStream();
        }

        public void WritePacket(TcpPacket packet, bool toServer)
        {
            if (toServer)
            {
                _clientBuffer.Write(packet.PayloadData, 0, packet.PayloadData.Length);
            }
            else
            {
                _serverBuffer.Write(packet.PayloadData, 0, packet.PayloadData.Length);
            }
        }

        public byte[] GetBuffer(bool toServer)
        {
            if (toServer)
            {
                return  _clientBuffer.ToArray();
            }

            return _serverBuffer.ToArray();
        }

        public void Dispose()
        {
            _clientBuffer?.Dispose();
            _serverBuffer?.Dispose();
        }

    }
```

<p>PacketAssembler</p>

<pre class="lang-cs prettyprint-override"><code>  public class PacketAssembler
    {
        public delegate void OnPacketReassembledDelegate(TcpSession session);

        public OnPacketReassembledDelegate OnPacketReassembled;

        private Dictionary<int, TcpSession> _tcpSessions;
        public PacketAssembler()
        {
            _tcpSessions = new Dictionary<int, TcpSession>();
        }

        public void AddPacket(TcpPacket packet, bool toServer)
        {
            // Client port (unique) is used for the dictionary.
            int port = toServer ? packet.SourcePort : packet.DestinationPort;

            if (packet.Syn) // Connection established
            {
                if (!_tcpSessions.ContainsKey(port))
                {
                    _tcpSessions.Add(port, new TcpSession());
                }
            }
            else if (packet.Psh) // Push data
            {
                _tcpSessions[port].WritePacket(packet, toServer);
            }
            else if (packet.Rst) // reset the session
            {
                _tcpSessions.Remove(port);
            }
            else if (packet.Fin) // End the session
            {
                OnPacketReassembled(_tcpSessions[port]);
                _tcpSessions.Remove(port);
            }

        }

    }
</code>```

答: Have you checked out packetnet-connections? https://github.com/chmorgan/packetnet-connections</p>

Title: how to get http content with Sharppcap

Body:
For my school project we have to do a man in the middle. But we don't succeed in getting HTTP content. I wrote a little thing to reassemble tcp payload data but I only get the HTTP Header and not the source code. I see that on wireshark data are uncompressed but I don't know what this mean.

This is my code
TcpSession class

 public class TcpSession : IDisposable
    {
        private MemoryStream _clientBuffer;
        private MemoryStream _serverBuffer;

        public TcpSession()
        {
            _clientBuffer = new MemoryStream();
            _serverBuffer = new MemoryStream();
        }

        public void WritePacket(TcpPacket packet, bool toServer)
        {
            if (toServer)
            {
                _clientBuffer.Write(packet.PayloadData, 0, packet.PayloadData.Length);
            }
            else
            {
                _serverBuffer.Write(packet.PayloadData, 0, packet.PayloadData.Length);
            }
        }

        public byte[] GetBuffer(bool toServer)
        {
            if (toServer)
            {
                return  _clientBuffer.ToArray();
            }

            return _serverBuffer.ToArray();
        }

        public void Dispose()
        {
            _clientBuffer?.Dispose();
            _serverBuffer?.Dispose();
        }

    }

PacketAssembler

  public class PacketAssembler
    {
        public delegate void OnPacketReassembledDelegate(TcpSession session);

        public OnPacketReassembledDelegate OnPacketReassembled;

        private Dictionary<int, TcpSession> _tcpSessions;
        public PacketAssembler()
        {
            _tcpSessions = new Dictionary<int, TcpSession>();
        }

        public void AddPacket(TcpPacket packet, bool toServer)
        {
            // Client port (unique) is used for the dictionary.
            int port = toServer ? packet.SourcePort : packet.DestinationPort;

            if (packet.Syn) // Connection established
            {
                if (!_tcpSessions.ContainsKey(port))
                {
                    _tcpSessions.Add(port, new TcpSession());
                }
            }
            else if (packet.Psh) // Push data
            {
                _tcpSessions[port].WritePacket(packet, toServer);
            }
            else if (packet.Rst) // reset the session
            {
                _tcpSessions.Remove(port);
            }
            else if (packet.Fin) // End the session
            {
                OnPacketReassembled(_tcpSessions[port]);
                _tcpSessions.Remove(port);
            }

        }

    }


问:
How should I fetch firebase data in my app
Hey there hope your fine I want to develope my first react native app so I need to get data from firebase but the problem my data look like that
{id : {name:"" , image:""}} with deferent id for every line I mean after getting data and store it in a state.

So how can I take every line and put it in a flat list for exemple ?
this is my real result:

```<code>{001: {image: "http://itgsmgroup.com/dev/foo/Activites-socio-culturelles.png", name: "Activités socioculturels"}
002: {image: "http://itgsmgroup.com/dev/foo/Accompagnement-juridique.png", name: "Accopagnement juridique"}
003: {image: "http://itgsmgroup.com/dev/foo/Sante.png", name: "santé"}
004: {image: "http://itgsmgroup.com/dev/foo/Formation.png", name: "formation et inseriton"}
005: {image: "", name: "information"}}
and this how i get data
getData() {
        firebase.database().ref('categories/').on('value', (snapshot) => {
            console.log(snapshot.val());
            this.setState({
                data: snapshot.val()
            });
        });
    }
</code>```

答:
Check this snack that i made for a working sample with your question on expo.io snack.

```<code>    export default class App extends React.Component {
      constructor(props){
       super(props)
       /**
       * This next line is used to convert the object that you receive from Firebase to an Array
       * which is what a FlatList expects as data
       */
      var tempListData = Object.entries(rawData).map(([key, value]) => value)
      this.state = {
      listData: tempListData
    }
  }

  componentDidMount() {
    //Normally this lifecycle method is where you would dispatch a call to firebase but for
    //demostration purposes am using static data defined above.
  }

  render() {
    return (
      <View style={styles.container}>
        <FlatList
          style={{flex: 1, padding: 10}}
          data={this.state.listData}
          renderItem={({item})=>(<View style={{height: 40}}><Text>{item.name}</Text></View>)}
        />
      </View>
    );
  }
}
</code>```

In summary you will need to convert the json object that you get from Firebase to an Array that you can pass to Flatlist.

Body:
Hey there hope your fine I want to develope my first react native app so I need to get data from firebase but the problem my data look like that
{id : {name:"" , image:""}} with deferent id for every line I mean after getting data and store it in a state.

So how can I take every line and put it in a flat list for exemple ?
this is my real result:

{001: {image: "http://itgsmgroup.com/dev/foo/Activites-socio-culturelles.png", name: "Activités socioculturels"}
002: {image: "http://itgsmgroup.com/dev/foo/Accompagnement-juridique.png", name: "Accopagnement juridique"}
003: {image: "http://itgsmgroup.com/dev/foo/Sante.png", name: "santé"}
004: {image: "http://itgsmgroup.com/dev/foo/Formation.png", name: "formation et inseriton"}
005: {image: "", name: "information"}}
and this how i get data
getData() {
        firebase.database().ref('categories/').on('value', (snapshot) => {
            console.log(snapshot.val());
            this.setState({
                data: snapshot.val()
            });
        });
    }

Body:
Check this snack that i made for a working sample with your question on expo.io snack.

    export default class App extends React.Component {
      constructor(props){
       super(props)
       /**
       * This next line is used to convert the object that you receive from Firebase to an Array
       * which is what a FlatList expects as data
       */
      var tempListData = Object.entries(rawData).map(([key, value]) => value)
      this.state = {
      listData: tempListData
    }
  }

  componentDidMount() {
    //Normally this lifecycle method is where you would dispatch a call to firebase but for
    //demostration purposes am using static data defined above.
  }

  render() {
    return (
      <View style={styles.container}>
        <FlatList
          style={{flex: 1, padding: 10}}
          data={this.state.listData}
          renderItem={({item})=>(<View style={{height: 40}}><Text>{item.name}</Text></View>)}
        />
      </View>
    );
  }
}

In summary you will need to convert the json object that you get from Firebase to an Array that you can pass to Flatlist.


问:
Using appscript how can I accept self-signed SSL cert in Safari
I'm trying to automate some stuff which involves browsing to a https url. I'm using py-appscript with Safari on Mac OS X. But when Safari navigates to the https URL, since I have a self-signed certificate, it throws up a popup window which asks you to accept it to continue. I could not figure out a way to do this. Sure using signed certificates will solve the problem but I was curious if it is even possible via appscript to accept or click on the popup dialog boxes on Safari?</p>

答:
Well, answering my own question. Hopefully it'll be useful to someone...</p>

```<code>from appscript import *

app(u'Safari').activate()
title = app(u'Safari').do_JavaScript(u'document.title', in_=app.documents[1])
app(u'Safari').documents[1].URL.set(u'https://localhost/')
app(u'System Events').application_processes[u'Safari'].windows[title].sheets[1].groups[2].UI_elements[u'Continue'].click()
</code>```

ContentLicense: CC BY-SA 2.5

Body:
Well, answering my own question. Hopefully it'll be useful to someone...

from appscript import *

app(u'Safari').activate()
title = app(u'Safari').do_JavaScript(u'document.title', in_=app.documents[1])
app(u'Safari').documents[1].URL.set(u'https://localhost/')
app(u'System Events').application_processes[u'Safari'].windows[title].sheets[1].groups[2].UI_elements[u'Continue'].click()


问:
What is Pythonic way to assign empty list to variables?
I want to assign empty list to multiple variables. I have tried these:

```<code># method 1
a = b = []

# method 2
c, d = [], []

print('\nBefore')
print(f'a: id={id(a)} value={a}')
print(f'b: id={id(b)} value={b}')
print(f'c: id={id(c)} value={c}')
print(f'd: id={id(d)} value={d}')

b = [1,2,5]
c = [6,4,2]

print('\nAfter')
print(f'a: id={id(a)} value={a}')
print(f'b: id={id(b)} value={b}')
print(f'c: id={id(c)} value={c}')
print(f'd: id={id(d)} value={d}')
</code>```

The difference, that I can see, is that both a and b point to the same location at initialize but changes after assignments. What is the Pythonic way to assign empty list to multiple variables?

Edit: Question is not about how to assign multiple variables but what is the Pythonic way(better way) of assigning multiple variables.

答:
Simplest solution:

```<code>a = b = c = d = []
</code>```

Rather pythonic in my opinion, although this will be the same reference o a singular list object (a.append("foo") also mutates b, c, and d).

To initialize completely separate list objects, as pointed out in the comments:

```<code>a, b, c, d = ([] for _ in range(4))
</code>```

or

```<code>a, b, c, d = [], [], [], []     # or list(), list(), list(), list()
</code>```

It's not hugely pretty, I agree. But do you really need four separate list variables? Here's what I think you should do:

```<code>lists = ([] for _ in range(4))
</code>```

Then you can access the different lists using lists[0] to lists[3].

Body:
I want to assign empty list to multiple variables. I have tried these:

# method 1
a = b = []

# method 2
c, d = [], []

print('\nBefore')
print(f'a: id={id(a)} value={a}')
print(f'b: id={id(b)} value={b}')
print(f'c: id={id(c)} value={c}')
print(f'd: id={id(d)} value={d}')

b = [1,2,5]
c = [6,4,2]

print('\nAfter')
print(f'a: id={id(a)} value={a}')
print(f'b: id={id(b)} value={b}')
print(f'c: id={id(c)} value={c}')
print(f'd: id={id(d)} value={d}')

The difference, that I can see, is that both a and b point to the same location at initialize but changes after assignments. What is the Pythonic way to assign empty list to multiple variables?

Edit: Question is not about how to assign multiple variables but what is the Pythonic way(better way) of assigning multiple variables.

Body:
Simplest solution:

a = b = c = d = []

Rather pythonic in my opinion, although this will be the same reference o a singular list object (a.append("foo") also mutates b, c, and d).

To initialize completely separate list objects, as pointed out in the comments:

a, b, c, d = ([] for _ in range(4))

or

a, b, c, d = [], [], [], []     # or list(), list(), list(), list()

It's not hugely pretty, I agree. But do you really need four separate list variables? Here's what I think you should do:

lists = ([] for _ in range(4))

Then you can access the different lists using lists[0] to lists[3].