问:
.NET Core build in docker linux container fails due to SSL authentication to Nuget
I was given a .NET Core project to run in a Linux Docker container to do the build, everything seems to be okay on the docker configuration side, but when I run this command: dotnet publish -c Release -o out, I get the SSL authentication error below.

The SSL connection could not be established, see inner exception. Authentication failed because the remote party has closed the transport stream.

I did my research and apparently it seemed that I was missing:

the environment variables Kestrel for ASPNET (as per https://github.com/aspnet/AspNetCore.Docs/issues/6199), which I add to my docker-compose, but I don't think it is the issue.

a Developer .pfx certificate, so I updated my docker-compose with the Kestrel Path to the certs file as seen below.

```<code>version: '3'
services:
  netcore:
    container_name: test_alerting_comp
    tty: true
    stdin_open: true
    image: alerting_netcore
    environment:
      - http_proxy=http://someproxy:8080
      - https_proxy=http://someproxy:8080
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=https://+;http://+
      - ASPNETCORE_HTTPS_PORT=443
      - ASPNETCORE_Kestrel__Certificates__Default__Password="ABC"
      - ASPNETCORE_Kestrel__Certificates__Default__Path=/root/.dotnet/corefx/cryptography/x509stores/my
    ports:
      - "8080:80"
      - "443:443"
    build: .
      #context: .
    security_opt:
      - seccomp:unconfined
    volumes:
      - "c:/FakePath/git/my_project/src:/app"
      - "c:/TEMP/nuget:/root"
    networks:
      - net
networks:
  net:
</code>```

I re-run the docker container and executed dotnet publish -c Release -o out with the same results.

From my host I can do this to my local NuGet:

A) wget https://nuget.local.com/api/v2 without issues,

B) but from the container I can't.

C) However from the container I can do this to official NuGet wget https://api.nuget.org/v3/index.json, so definetely my proxy is working okay.

Debugging SSL issue:

The given .pfx certificate is a self-signed one, and it is working okay from Windows OS (at least I was told that).

strace shows me from where the certs are being pulled from as below

root@9b98d5447904:/app# strace wget https://nuget.local.com/api/v2 |& grep certs open("/etc/ssl/certs/ca-certificates.crt", O_RDONLY) = 3

I exported the .pfx as follows:
openssl pkcs12 -in ADPRootCertificate.pfx -out my_adp_dev.crt then moved it to /usr/local/share/ca-certificates/, removed the private part, just left in the file public part (-----BEGIN CERTIFICATE----- -----END CERTIFICATE----- ) executed update-ca-certificates and I could see 1 added, double checked in file /etc/ssl/certs/ca-certificates.crt and the new cert was in there.

Executed this again wget https://nuget.local.com/api/v2 and failed.

I used OpenSSL to get more info and as you can see it is not working, the cert has a weird CN, because they used a wildcard for the subject and to me this is wrong, but they state that .pfx is working in Windows OS.

```<code>root@ce21098e9643:/usr/local/share/ca-certificates# openssl s_client -connect nuget.local.com:443 -CApath /etc/ssl/certs
CONNECTED(00000003)
depth=0 CN = *.local.com
verify error:num=20:unable to get local issuer certificate
verify return:1
depth=0 CN = *.local.com
verify error:num=21:unable to verify the first certificate
verify return:1
---
Certificate chain
 0 s:/CN=\x00*\x00l\x00o\x00c\x00a\x00l\x00.\x00c\x00o\x00m
   i:/C=ES/ST=SomeCity/L=SomeCity/OU=DEV/O=ASD/CN=Development CA
---
Server certificate
-----BEGIN CERTIFICATE-----
XXXXXXXXXXX
XXXXXXXXXXX
-----END CERTIFICATE-----
subject=s:/CN=\x00*\x00l\x00o\x00c\x00a\x00l\x00.\x00c\x00o\x00m
issuer=i:/C=ES/ST=SomeCity/L=SomeCity/OU=DEV/O=ASD/CN=Development CA
---
No client certificate CA names sent
Peer signing digest: SHA1
Server Temp Key: ECDH, P-256, 256 bits
---
SSL handshake has read 1284 bytes and written 358 bytes
Verification error: unable to verify the first certificate
---
New, TLSv1.2, Cipher is ECDHE-RSA-AES256-SHA384
Server public key is 1024 bit
Secure Renegotiation IS supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
SSL-Session:
    Protocol  : TLSv1.2
    Cipher    : ECDHE-RSA-AES256-SHA384
    Session-ID: 95410000753146AAE1D313E8538972244C7B79A60DAF3AA14206417490E703F3
    Session-ID-ctx:
    Master-Key: B09214XXXXXXX0007D126D24D306BB763673EC52XXXXXXB153D310B22C341200EF013BC991XXXXXXX888C08A954265623
    PSK identity: None
    PSK identity hint: None
    SRP username: None
    Start Time: 1558993408
    Timeout   : 7200 (sec)
    Verify return code: 21 (unable to verify the first certificate)
    Extended master secret: yes
---
</code>```

I don't know what issue I'm facing, but it appears to be that:

A) the self-signed .pfx was wrongly configured, and now that it is being used in Linux it doesn't work as it should.

B) I need some more config in the container, which I'm not aware of.

What else should I do?

I'm thinking on probaly create other cert to use from Linux hosts.

Is it feasible to create another self-signed cert with OpenSSL for IIS ver 8 and import it to IIS?.

Any ideas are welcome, cheers.

答:
ANSWERING TO MYSELF

It was not a Linux container issue, it is a certificate issue in the web server (IIS), because we are using self-signed certificates and in this way the cert will be always an invalid certificate. Self-signed certs works okay on Windows OS side, doesn't matter the invalid error. Of course self-signed certs are just for a test environment or so.

From Linux OS when you are trying to pull packages from NuGet you will get the error below, because:

1) The cert is indeed invalid, and

2) because apparently there is not an option to ignore an invalid certificate from Linux side.

```<code>The SSL connection could not be established, see inner exception.
The remote certificate is invalid according to the validation procedure.
</code>```

The solution is you are working in a corporate environment, is to request to System Administrator a proper signed certificate, for that you generate a CSR from your web server, in my case IIS, then pass it to them, so they will send you back a .cer file to install in that web server.

The other option that I was trying to do but I couldn't due to the limitations of my corporate environment, is to create a fake CA (with OpenSSL), then you sign the CSR's yourself to have some valid certificates for your Dev or test environment.

Apologizes for answering this myself, but I believe it is worth to share my findings.

Hope it helps.

Body:
I was given a .NET Core project to run in a Linux Docker container to do the build, everything seems to be okay on the docker configuration side, but when I run this command: dotnet publish -c Release -o out, I get the SSL authentication error below.

The SSL connection could not be established, see inner exception. Authentication failed because the remote party has closed the transport stream.

I did my research and apparently it seemed that I was missing:

the environment variables Kestrel for ASPNET (as per https://github.com/aspnet/AspNetCore.Docs/issues/6199), which I add to my docker-compose, but I don't think it is the issue.

a Developer .pfx certificate, so I updated my docker-compose with the Kestrel Path to the certs file as seen below.

version: '3'
services:
  netcore:
    container_name: test_alerting_comp
    tty: true
    stdin_open: true
    image: alerting_netcore
    environment:
      - http_proxy=http://someproxy:8080
      - https_proxy=http://someproxy:8080
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=https://+;http://+
      - ASPNETCORE_HTTPS_PORT=443
      - ASPNETCORE_Kestrel__Certificates__Default__Password="ABC"
      - ASPNETCORE_Kestrel__Certificates__Default__Path=/root/.dotnet/corefx/cryptography/x509stores/my
    ports:
      - "8080:80"
      - "443:443"
    build: .
      #context: .
    security_opt:
      - seccomp:unconfined
    volumes:
      - "c:/FakePath/git/my_project/src:/app"
      - "c:/TEMP/nuget:/root"
    networks:
      - net
networks:
  net:

I re-run the docker container and executed dotnet publish -c Release -o out with the same results.

From my host I can do this to my local NuGet:

A) wget https://nuget.local.com/api/v2 without issues,

B) but from the container I can't.

C) However from the container I can do this to official NuGet wget https://api.nuget.org/v3/index.json, so definetely my proxy is working okay.

Debugging SSL issue:

The given .pfx certificate is a self-signed one, and it is working okay from Windows OS (at least I was told that).

strace shows me from where the certs are being pulled from as below

root@9b98d5447904:/app# strace wget https://nuget.local.com/api/v2 |& grep certs open("/etc/ssl/certs/ca-certificates.crt", O_RDONLY) = 3

I exported the .pfx as follows:
openssl pkcs12 -in ADPRootCertificate.pfx -out my_adp_dev.crt then moved it to /usr/local/share/ca-certificates/, removed the private part, just left in the file public part (-----BEGIN CERTIFICATE----- -----END CERTIFICATE----- ) executed update-ca-certificates and I could see 1 added, double checked in file /etc/ssl/certs/ca-certificates.crt and the new cert was in there.

Executed this again wget https://nuget.local.com/api/v2 and failed.

I used OpenSSL to get more info and as you can see it is not working, the cert has a weird CN, because they used a wildcard for the subject and to me this is wrong, but they state that .pfx is working in Windows OS.

root@ce21098e9643:/usr/local/share/ca-certificates# openssl s_client -connect nuget.local.com:443 -CApath /etc/ssl/certs
CONNECTED(00000003)
depth=0 CN = *.local.com
verify error:num=20:unable to get local issuer certificate
verify return:1
depth=0 CN = *.local.com
verify error:num=21:unable to verify the first certificate
verify return:1
---
Certificate chain
 0 s:/CN=\x00*\x00l\x00o\x00c\x00a\x00l\x00.\x00c\x00o\x00m
   i:/C=ES/ST=SomeCity/L=SomeCity/OU=DEV/O=ASD/CN=Development CA
---
Server certificate
-----BEGIN CERTIFICATE-----
XXXXXXXXXXX
XXXXXXXXXXX
-----END CERTIFICATE-----
subject=s:/CN=\x00*\x00l\x00o\x00c\x00a\x00l\x00.\x00c\x00o\x00m
issuer=i:/C=ES/ST=SomeCity/L=SomeCity/OU=DEV/O=ASD/CN=Development CA
---
No client certificate CA names sent
Peer signing digest: SHA1
Server Temp Key: ECDH, P-256, 256 bits
---
SSL handshake has read 1284 bytes and written 358 bytes
Verification error: unable to verify the first certificate
---
New, TLSv1.2, Cipher is ECDHE-RSA-AES256-SHA384
Server public key is 1024 bit
Secure Renegotiation IS supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
SSL-Session:
    Protocol  : TLSv1.2
    Cipher    : ECDHE-RSA-AES256-SHA384
    Session-ID: 95410000753146AAE1D313E8538972244C7B79A60DAF3AA14206417490E703F3
    Session-ID-ctx:
    Master-Key: B09214XXXXXXX0007D126D24D306BB763673EC52XXXXXXB153D310B22C341200EF013BC991XXXXXXX888C08A954265623
    PSK identity: None
    PSK identity hint: None
    SRP username: None
    Start Time: 1558993408
    Timeout   : 7200 (sec)
    Verify return code: 21 (unable to verify the first certificate)
    Extended master secret: yes
---

I don't know what issue I'm facing, but it appears to be that:

A) the self-signed .pfx was wrongly configured, and now that it is being used in Linux it doesn't work as it should.

B) I need some more config in the container, which I'm not aware of.

What else should I do?

I'm thinking on probaly create other cert to use from Linux hosts.

Is it feasible to create another self-signed cert with OpenSSL for IIS ver 8 and import it to IIS?.

Any ideas are welcome, cheers.

Body:
ANSWERING TO MYSELF

It was not a Linux container issue, it is a certificate issue in the web server (IIS), because we are using self-signed certificates and in this way the cert will be always an invalid certificate. Self-signed certs works okay on Windows OS side, doesn't matter the invalid error. Of course self-signed certs are just for a test environment or so.

From Linux OS when you are trying to pull packages from NuGet you will get the error below, because:

1) The cert is indeed invalid, and

2) because apparently there is not an option to ignore an invalid certificate from Linux side.

The SSL connection could not be established, see inner exception.
The remote certificate is invalid according to the validation procedure.

The solution is you are working in a corporate environment, is to request to System Administrator a proper signed certificate, for that you generate a CSR from your web server, in my case IIS, then pass it to them, so they will send you back a .cer file to install in that web server.

The other option that I was trying to do but I couldn't due to the limitations of my corporate environment, is to create a fake CA (with OpenSSL), then you sign the CSR's yourself to have some valid certificates for your Dev or test environment.

Apologizes for answering this myself, but I believe it is worth to share my findings.

Hope it helps.


来源: stackexchange

问:
ASP.NET Core [Require] non-nullable types
Here, the question was posed how to validate non-nullable required types.

The provided solution to make the field nullable like the following is not desirable in my case.

```<code>[Required]
public int? Data { get; set; }
</code>```

How can you change the behavior to instead make the following fail validation in the cases where the field is omitted from the request.

```<code>[Required]
public int Data { get; set; }
</code>```

I have tried a custom validator, but these do not have information about the raw value and only see the default 0 value. I have also tried a custom model binder but it seems to work at the level of the entire request model instead of the integer fields which a want. My binder experiment looks like this:

```<code>public class RequiredIntBinder : IModelBinder
{
    public Task BindModelAsync(ModelBindingContext bindingContext)
    {
        if (bindingContext.ModelType != typeof(int))
            throw new InvalidOperationException($"{nameof(RequiredIntBinder)} can only be applied to integer properties");

        var value = bindingContext.ValueProvider.GetValue(bindingContext.BinderModelName);
        if (value == ValueProviderResult.None)
        {
            bindingContext.Result = ModelBindingResult.Failed();
            return Task.CompletedTask;
        }

        return new SimpleTypeModelBinder(bindingContext.ModelType).BindModelAsync(bindingContext);
    }
}

public class RequiredIntBinderProvider : IModelBinderProvider
{
    public IModelBinder GetBinder(ModelBinderProviderContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException(nameof(context));
        }

        if (context.Metadata.ModelType == typeof(int))
        {
            return new BinderTypeModelBinder(typeof(RequiredIntBinder));
        }

        return null;
    }
}
</code>```

and is registered with mvc like this

```<code>options.ModelBinderProviders.Insert(0, new RequiredIntBinderProvider());
</code>```

but the model binder is never used. I feel like I might be close but cannot connect the last dots.

答:
Solution working with json requests

You cannot validate an already created model instance, because a non-nullable property has always a value (no matter whether it was assigned from json or is a default value). The solution is to report the missing value already during deserialization.

Create a contract resolver

```<code>public class RequiredPropertiesContractResolver : DefaultContractResolver
{
    protected override JsonObjectContract CreateObjectContract(Type objectType)
    {
        var contract = base.CreateObjectContract(objectType);

        foreach (var contractProperty in contract.Properties)
        {
            if (contractProperty.PropertyType.IsValueType
                && contractProperty.AttributeProvider.GetAttributes(typeof(RequiredAttribute), inherit: true).Any())
            {
                contractProperty.Required = Required.Always;
            }
        }

        return contract;
    }
}
</code>```

and then assign it to SerializerSettings:

```<code>services.AddMvc()
        .AddJsonOptions(jsonOptions =>
        {
            jsonOptions.SerializerSettings.ContractResolver = new RequiredPropertiesContractResolver();
        });
</code>```

The ModelState is then invalid for non-nullable properties with the [Required] attribute if the value is missing from json.

Example

Json body

```<code>var jsonBody = @"{ Data2=123 }"
</code>```

is invalid for model

```<code>class Model
{
    [Required]
    public int Data { get; set; }

    public int Data2 { get; set; }
}
</code>```

ContentLicense: CC BY-SA 4.0

Body:
Here, the question was posed how to validate non-nullable required types.

The provided solution to make the field nullable like the following is not desirable in my case.

[Required]
public int? Data { get; set; }

How can you change the behavior to instead make the following fail validation in the cases where the field is omitted from the request.

[Required]
public int Data { get; set; }

I have tried a custom validator, but these do not have information about the raw value and only see the default 0 value. I have also tried a custom model binder but it seems to work at the level of the entire request model instead of the integer fields which a want. My binder experiment looks like this:

public class RequiredIntBinder : IModelBinder
{
    public Task BindModelAsync(ModelBindingContext bindingContext)
    {
        if (bindingContext.ModelType != typeof(int))
            throw new InvalidOperationException($"{nameof(RequiredIntBinder)} can only be applied to integer properties");

        var value = bindingContext.ValueProvider.GetValue(bindingContext.BinderModelName);
        if (value == ValueProviderResult.None)
        {
            bindingContext.Result = ModelBindingResult.Failed();
            return Task.CompletedTask;
        }

        return new SimpleTypeModelBinder(bindingContext.ModelType).BindModelAsync(bindingContext);
    }
}

public class RequiredIntBinderProvider : IModelBinderProvider
{
    public IModelBinder GetBinder(ModelBinderProviderContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException(nameof(context));
        }

        if (context.Metadata.ModelType == typeof(int))
        {
            return new BinderTypeModelBinder(typeof(RequiredIntBinder));
        }

        return null;
    }
}

and is registered with mvc like this

options.ModelBinderProviders.Insert(0, new RequiredIntBinderProvider());

but the model binder is never used. I feel like I might be close but cannot connect the last dots.

Body:
Solution working with json requests

You cannot validate an already created model instance, because a non-nullable property has always a value (no matter whether it was assigned from json or is a default value). The solution is to report the missing value already during deserialization.

Create a contract resolver

public class RequiredPropertiesContractResolver : DefaultContractResolver
{
    protected override JsonObjectContract CreateObjectContract(Type objectType)
    {
        var contract = base.CreateObjectContract(objectType);

        foreach (var contractProperty in contract.Properties)
        {
            if (contractProperty.PropertyType.IsValueType
                && contractProperty.AttributeProvider.GetAttributes(typeof(RequiredAttribute), inherit: true).Any())
            {
                contractProperty.Required = Required.Always;
            }
        }

        return contract;
    }
}

and then assign it to SerializerSettings:

services.AddMvc()
        .AddJsonOptions(jsonOptions =>
        {
            jsonOptions.SerializerSettings.ContractResolver = new RequiredPropertiesContractResolver();
        });

The ModelState is then invalid for non-nullable properties with the [Required] attribute if the value is missing from json.

Example

Json body

var jsonBody = @"{ Data2=123 }"

is invalid for model

class Model
{
    [Required]
    public int Data { get; set; }

    public int Data2 { get; set; }
}


问:
Getting error with basic trim() function in U-SQL script
I want to apply .trim() function on a column but getting the error.
Sample data:
Product_ID,Product_Name
1,   Office Supplies
2,Personal Care

I have to do some data manipulation but can't get the basic trim() function right.

```<code>@productlog =
EXTRACT Product_ID string,
    Prduct_Name string
FROM "/Staging/Products.csv"
USING Extractors.Csv();
@output = Select Product_ID, Product_Name.trim() from @productlog;
OUTPUT @output
TO "/Output/Products.csv"
USING Outputters.Csv();
</code>```

Error:
Activity U-SQL1 failed: Error Id: E_CSC_USER_SYNTAXERROR, Error Message: syntax error. Expected one of: '.' ALL ANTISEMIJOIN ANY AS BEGIN BROADCASTLEFT BROADCASTRIGHT CROSS DISTINCT EXCEPT FULL FULLCROSS GROUP HASH HAVING INDEXLOOKUP INNER INTERSECT JOIN LEFT LOOP MERGE ON OPTION ORDER OUTER OUTER UNION PAIR PIVOT PRESORT PRODUCE READONLY REQUIRED RIGHT SAMPLE SEMIJOIN SERIAL TO UNIFORM UNION UNIVERSE UNPIVOT USING WHERE WITH ';' '(' ')' ',' .

答:
Try the below, afaik you need to alias trimmed fields</p>

```<code>@productlog =
EXTRACT Product_ID string,
    Prduct_Name string
FROM "/Staging/Products.csv"
USING Extractors.Csv();
@output = Select Product_ID, Product_Name.trim() as Trimmed_Product_Name from @productlog;
OUTPUT @output
TO "/Output/Products.csv"
USING Outputters.Csv();
</code>```

Body:
I want to apply .trim() function on a column but getting the error.
Sample data:
Product_ID,Product_Name
1,   Office Supplies
2,Personal Care

I have to do some data manipulation but can't get the basic trim() function right.

@productlog =
EXTRACT Product_ID string,
    Prduct_Name string
FROM "/Staging/Products.csv"
USING Extractors.Csv();
@output = Select Product_ID, Product_Name.trim() from @productlog;
OUTPUT @output
TO "/Output/Products.csv"
USING Outputters.Csv();

Error:
Activity U-SQL1 failed: Error Id: E_CSC_USER_SYNTAXERROR, Error Message: syntax error. Expected one of: '.' ALL ANTISEMIJOIN ANY AS BEGIN BROADCASTLEFT BROADCASTRIGHT CROSS DISTINCT EXCEPT FULL FULLCROSS GROUP HASH HAVING INDEXLOOKUP INNER INTERSECT JOIN LEFT LOOP MERGE ON OPTION ORDER OUTER OUTER UNION PAIR PIVOT PRESORT PRODUCE READONLY REQUIRED RIGHT SAMPLE SEMIJOIN SERIAL TO UNIFORM UNION UNIVERSE UNPIVOT USING WHERE WITH ';' '(' ')' ',' .

Body:
Try the below, afaik you need to alias trimmed fields

@productlog =
EXTRACT Product_ID string,
    Prduct_Name string
FROM "/Staging/Products.csv"
USING Extractors.Csv();
@output = Select Product_ID, Product_Name.trim() as Trimmed_Product_Name from @productlog;
OUTPUT @output
TO "/Output/Products.csv"
USING Outputters.Csv();


问:
How to find whether an element exists in std::map?
My use case:

```<code>map<string, Car> cars;
bool exists(const string& name) {
  // somehow I should find whether my MAP has a car
  // with the name provided
  return false;
}
</code>```

Could you please suggest the best and the most elegant way to do it in C++? Thanks.

答:
C++20:</p>

```<code>return cars.contains(name);
</code>```

ContentLicense: CC BY-SA 2.5

Body:
My use case:

map<string, Car> cars;
bool exists(const string& name) {
  // somehow I should find whether my MAP has a car
  // with the name provided
  return false;
}

Could you please suggest the best and the most elegant way to do it in C++? Thanks.

Body:
C++20:

return cars.contains(name);


问:
tf.estimator.Estimator gives different test accuracy when trianed epoch by epoch vs over all epochs
I have defined a straightforward CNN as my model_fn for a tf.estimator.Estimator and feed it with this input_fn:

```<code>def input_fn(features, labels, batch_size, epochs):
    dataset = tf.data.Dataset.from_tensor_slices((features))
    dataset = dataset.map(lambda x: tf.cond(tf.random_uniform([], 0, 1) > 0.5, lambda: dataset_augment(x), lambda: x),
                          num_parallel_calls=16).cache()
    dataset_labels = tf.data.Dataset.from_tensor_slices((labels))
    dataset = dataset.zip((dataset, dataset_labels))
    dataset = dataset.shuffle(30000)
    dataset = dataset.repeat(epochs)
    dataset = dataset.batch(batch_size)
    dataset = dataset.prefetch(-1)
    return dataset
</code>```

when I train the estimator this way, I get 43% test accuracy after 10 epochs:

```<code>steps_per_epoch = data_train.shape[0] // batch_size
for epoch in range(1, epochs + 1):
    cifar100_classifier.train(lambda: input_fn(data_train, labels_train, batch_size, epochs=1), steps=steps_per_epoch)
</code>```

But when I train it this way I get 32% test accuracy after 10 epochs:

```<code>steps_per_epoch = data_train.shape[0] // batch_size
max_steps = epochs * steps_per_epoch
cifar100_classifier.train(steps=max_steps,
                              input_fn=lambda: input_fn(data_train, labels_train, batch_size, epochs=epochs))
</code>```

I just cannot understand why these two methods produce different results. Can anyone please explain?

答:
Since you are calling the input_fn multiple times in the first example it seems like you would be generating more augmented data through dataset_augment(x) as you're doing an augmentation coin-toss for every x every epoch.

In the second example you only do these coin-tosses once and then train multiple epochs on that same data. So here your train set is effectively ``smaller''.

The .cache() doesn't really save you from this in the first example.

Body:
I have defined a straightforward CNN as my model_fn for a tf.estimator.Estimator and feed it with this input_fn:

def input_fn(features, labels, batch_size, epochs):
    dataset = tf.data.Dataset.from_tensor_slices((features))
    dataset = dataset.map(lambda x: tf.cond(tf.random_uniform([], 0, 1) > 0.5, lambda: dataset_augment(x), lambda: x),
                          num_parallel_calls=16).cache()
    dataset_labels = tf.data.Dataset.from_tensor_slices((labels))
    dataset = dataset.zip((dataset, dataset_labels))
    dataset = dataset.shuffle(30000)
    dataset = dataset.repeat(epochs)
    dataset = dataset.batch(batch_size)
    dataset = dataset.prefetch(-1)
    return dataset

when I train the estimator this way, I get 43% test accuracy after 10 epochs:

steps_per_epoch = data_train.shape[0] // batch_size
for epoch in range(1, epochs + 1):
    cifar100_classifier.train(lambda: input_fn(data_train, labels_train, batch_size, epochs=1), steps=steps_per_epoch)

But when I train it this way I get 32% test accuracy after 10 epochs:

steps_per_epoch = data_train.shape[0] // batch_size
max_steps = epochs * steps_per_epoch
cifar100_classifier.train(steps=max_steps,
                              input_fn=lambda: input_fn(data_train, labels_train, batch_size, epochs=epochs))

I just cannot understand why these two methods produce different results. Can anyone please explain?


问:
Get the values from the "GET" parameters (JavaScript)
I have a URL with some GET parameters as follows:

```<code>www.test.com/t.html?a=1&b=3&c=m2-m3-m4-m5
</code>```

I need to get the whole value of c. I tried to read the URL, but I got only m2. How do I do this using JavaScript?

答:
Get a single param value:</p>

```<code>function getQueryParameter(query, parameter) {
return (window.location.href.split(parameter + '=')[1].split('&')[0]);}
</code>```

Body:
I have a URL with some GET parameters as follows:

www.test.com/t.html?a=1&b=3&c=m2-m3-m4-m5

I need to get the whole value of c. I tried to read the URL, but I got only m2. How do I do this using JavaScript?

Body:
Get a single param value:

function getQueryParameter(query, parameter) {
return (window.location.href.split(parameter + '=')[1].split('&')[0]);}


问:
How to slice np array's certain dimention with different start point?
The sample code is as below.

I want to get dataNew(h, w, length) according to data(h, w, c) and ind(h, w). Here length < c, it means dataNew is sliced from data.

Here, length and ind[i, j] is made sure to suit the c value.

I have realize it through for loops, but I wnat the python way. Please help, thanks!

import numpy as np

h, w, c = 3, 4, 5

data = np.arange(60).reshape((h, w, c))
print(data)

length = 3
ind = np.random.randint(0, 3, 12).reshape(h, w)
print(ind)

dataNew = np.empty((h, w, length), np.int16)
for i in range(h):
    for j in range(w):
        st = ind[i, j]
        dataNew[i, j] = data[i, j][st : st + length]

print(dataNew)
```

答:
We can leverage np.lib.stride_tricks.as_strided based scikit-image's view_as_windows to get sliding windows. More info on use of as_strided based view_as_windows.

```<code>from skimage.util.shape import view_as_windows

# Get all sliding windows along the last axis
w = view_as_windows(data,(1,1,length))[...,0,0,:]

# Index into windows with start indices and slice out singleton dims
out = np.take_along_axis(w,ind[...,None,None],axis=-1)[...,0]
</code>```

Last step is basically using advanced-indexing into the windows with those start indices. This could be made a bit simpler and might be easier to understand. So, alternatively, we could do -

```<code>m,n = ind.shape
I,J = np.ogrid[:m,:n]
out = w[I,J,ind]
</code>```

Body:
We can leverage np.lib.stride_tricks.as_strided based scikit-image's view_as_windows to get sliding windows. More info on use of as_strided based view_as_windows.

from skimage.util.shape import view_as_windows

# Get all sliding windows along the last axis
w = view_as_windows(data,(1,1,length))[...,0,0,:]

# Index into windows with start indices and slice out singleton dims
out = np.take_along_axis(w,ind[...,None,None],axis=-1)[...,0]

Last step is basically using advanced-indexing into the windows with those start indices. This could be made a bit simpler and might be easier to understand. So, alternatively, we could do -

m,n = ind.shape
I,J = np.ogrid[:m,:n]
out = w[I,J,ind]


问:
Android Studio - Install failed. Installation failed Rerun
I am unable to run my android project on any android device.

I am getting error on android studio. My project is building and compiling without any error.

Attaching screenshot

答:
Wiping emulator may help, but what if I'm using physical, configured for use device? This bug is weird and a bit random... for me restarting IDE and device helped, also used Android Studio -> Files -> Invalidate Caches / Restart and manual killing/restarting daemon with adb kill-server and adb start-server commands

also: note that some devices have additional toggles to turn on for developing purposes, e.g. Xiaomi have separated option in dev prefs for allowing installing apks through USB, USB debugging turned on isn't sufficient


问:
How to create custom themes in Anycharts Qlik extension
I am trying to create custom themes in our Anycharts Qlik sense extension. I used the following article as a guide, https://www.anychart.com/blog/2019/01/16/qlik-data-visualization-custom-drawing/

The article names these files as the key to adding themes:

"To add a custom theme to the AnyChart Qlik Extension, edit the file at ..\Users\Documents\Qlik\Sense\Extensions\AnyChart-Qlik\lib\themes-combined.js by putting the code of your custom theme at the end.

To use the theme by default in the future, edit the config.js file at ..\Users\Documents\Qlik\Sense\Extensions\AnyChart-Qlik"

I cannot find these files (or even the "lib" folder mentioned) in either a desktop or enterprise installation.

has anyone:
1) found these files/folders ?
2) successfully created a custom theme for teh Anycharts qlik sense extension ?

Thanks

Already searched Qlik installation for the files.

答: The thing is that the article is related to the 2.4 version of Qlik. But the current version is 3.x, and the feature is not supported there. Perhaps it'll be restored in the future.</p>


问:
Why are Angular apps called Single-Page Applications when they still make use of hyperlinks to different pages?
This is a question I've found myself asking a number of times since starting to work with Angular. I've heard time after time that Angular is a great framework for developing Single-Page Applications.

Now to me, the idea that an application is single-page suggests to me that all aspects of the web app are loaded in a single page on the internet, at a single address. Surely then this would mean that an app loaded at http://myapp.com/index.html/ should never need to navigate away from index.html?

But in reality within Angular we use a RoutingModule which allows the user to navigate to different hyperlinks using html elements present within the webpage. The router enables access to other links such as contact.html, about.html etc.

So why exactly are SPAs named as such when they don't have a single page within their application, but actually have multiple?

The same can be said of Vue or React apps.

答:
In Angular, when you redirect  the links, a new page doesn't get loaded. Only the existing content changes dynamically through routing. It is basically a single page, on every route change, the content gets replaced.

Fore more info, read this article


问:
Stopping a service after Intent -Android
In my android application, I have an activity called A .From A, I call a background service.In that services onStartCommand I will do some AsyncTask and Provided an Intent to move to another activity called B. Where I am stuck is,

1. How can I stop the service itself after calling the Intent?

EDIT: I tried calling StopSelf(); but it not ending my service.

2. After reaching B the back button should not work. Currently if press backbutton I can still go to Activity A.

My Intent

```<code>Intent dialogIntent = new Intent(UserLoginValidationService.this,SplashScreen.class);
                        dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        dialogIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        stopSelf();
                        startActivity(dialogIntent);
</code>```

EDIT: How to call finishAffinity(); in service?

Any help is appreciated.

答:
This could be the best way to do it.

Create an Intent Service.

Register a Local BroadcastReceiver in your A Activity from where you started
the Service.

Broadcast from your Intent Service and Receive it in you A Activity.

Do the needful in that BroadcastReceiver code.

NOTE: unregister BroadcastReceiver in A Activity onDestroy() method.

stopSelf() do stop the service but it could take little time. You can cross check it by putting log in onDestroy() Method of Service.

Body:
In my android application, I have an activity called A .From A, I call a background service.In that services onStartCommand I will do some AsyncTask and Provided an Intent to move to another activity called B. Where I am stuck is,

1. How can I stop the service itself after calling the Intent?

EDIT: I tried calling StopSelf(); but it not ending my service.

2. After reaching B the back button should not work. Currently if press backbutton I can still go to Activity A.

My Intent

Intent dialogIntent = new Intent(UserLoginValidationService.this,SplashScreen.class);
                        dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        dialogIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        stopSelf();
                        startActivity(dialogIntent);

EDIT: How to call finishAffinity(); in service?

Any help is appreciated.


问:
How to create a stacked bar chart with gaps between values of each variables in Python
I have a problem with creating a stacked bar chart in Python.
I have data with 3 variables as below:

```<code>A=[3,5,7]

B=[4,5,7]

C=[2,3,4,5,6,7]
</code>```

I would like to create a bar chart with gaps of values of each variable as below

bar chart with gaps between values of each variable :

Could anyone help me with this graph? Thank you very much.

答:
Actually you want to draw a color table or something close to a Gantt diagram. I don't know really convenience ways to do this in Python.

One solution is to use matplotlib.pyplot.grid (doc). This discussion provides one solution.

Another solution is to study the plotly package. It provides really pretty outputs for table and gantt graphs (doc).

Here, I will show you a similar output using hbar from matplotlib. The main idea is to rebuild a grid of cells. Each row represent a class (e.g. A, B or C). Each row is composed by the same number of cells. Each cell has the same width. The color of a cell is defined by the binary translation of your data.

To adjust the x-axis labels, they are just shifted manually.

```<code># Import module
import seaborn
import matplotlib.pyplot as plt
import numpy as np

##########################
#       Your input       #
##########################

A = [3, 5, 7]
B = [4, 5, 7]
C = [2, 3, 4, 5, 6, 7]

##########################
# Prepra the data        #
##########################
my_list = [A, B, C]
segments = 8
cell_width = 1000
nb_x_range = [i * cell_width for i in range(2, segments)]
classes = ["A", "B", "C"]
colors_def = {'A': {0: "w", 1: "b"},
              'B': {0: "w", 1: "g"},
              'C': {0: "w", 1: "y"},
              }

def create_data(my_list):
    data = np.zeros((segments + 1, len(classes)))
    for i, sub_list in enumerate(my_list):
        for elt in sub_list:
            data[elt, i] = 1
    return data

data = create_data(my_list)
print(data)
# [[0. 0. 0.]
#  [0. 0. 0.]
#  [0. 0. 1.]
#  [1. 0. 1.]
#  [0. 1. 1.]
#  [1. 1. 1.]
#  [0. 0. 1.]
#  [1. 1. 1.]
#  [0. 0. 0.]]

y_pos = np.arange(len(classes))
# left alignment of data starts at zero
left = np.zeros(len(my_list)) - cell_width/2

##########################
#       Create plot      #
##########################
# Set sea born for prettier graph
seaborn.set()
# create figure
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111)
# Set X axis (shifted)
ax.set_xlim((min(nb_x_range)-cell_width/2, max(nb_x_range)+cell_width/2))

# For each cell
for d in data:
    # Define color for each row
    colors = [colors_def[classes[i]][bi] for i, bi in enumerate(d)]
    # e.g. colors = [colors_def["A"][d[0]], colors_def["B"][d[1]], colors_def["C"][d[2]]]

    # Draw cell
    ax.barh(y_pos, cell_width,
            height=1,           # Heights of horizontal bars
            color=colors,       # Colors
            left=left)          # Left padd from y-axis
    # Update margin
    left += cell_width

# Axis graduation
ax.set_yticks(y_pos)
ax.set_xticks(nb_x_range)
ax.set_yticklabels(classes)
ax.set_xlabel('Stacked bar')

plt.show()
</code>```

The output looks like this:

Body:
I have a problem with creating a stacked bar chart in Python.
I have data with 3 variables as below:

A=[3,5,7]

B=[4,5,7]

C=[2,3,4,5,6,7]

I would like to create a bar chart with gaps of values of each variable as below

bar chart with gaps between values of each variable :

Could anyone help me with this graph? Thank you very much.

Body:
Actually you want to draw a color table or something close to a Gantt diagram. I don't know really convenience ways to do this in Python.

One solution is to use matplotlib.pyplot.grid (doc). This discussion provides one solution.

Another solution is to study the plotly package. It provides really pretty outputs for table and gantt graphs (doc).

Here, I will show you a similar output using hbar from matplotlib. The main idea is to rebuild a grid of cells. Each row represent a class (e.g. A, B or C). Each row is composed by the same number of cells. Each cell has the same width. The color of a cell is defined by the binary translation of your data.

To adjust the x-axis labels, they are just shifted manually.

# Import module
import seaborn
import matplotlib.pyplot as plt
import numpy as np

##########################
#       Your input       #
##########################

A = [3, 5, 7]
B = [4, 5, 7]
C = [2, 3, 4, 5, 6, 7]

##########################
# Prepra the data        #
##########################
my_list = [A, B, C]
segments = 8
cell_width = 1000
nb_x_range = [i * cell_width for i in range(2, segments)]
classes = ["A", "B", "C"]
colors_def = {'A': {0: "w", 1: "b"},
              'B': {0: "w", 1: "g"},
              'C': {0: "w", 1: "y"},
              }

def create_data(my_list):
    data = np.zeros((segments + 1, len(classes)))
    for i, sub_list in enumerate(my_list):
        for elt in sub_list:
            data[elt, i] = 1
    return data

data = create_data(my_list)
print(data)
# [[0. 0. 0.]
#  [0. 0. 0.]
#  [0. 0. 1.]
#  [1. 0. 1.]
#  [0. 1. 1.]
#  [1. 1. 1.]
#  [0. 0. 1.]
#  [1. 1. 1.]
#  [0. 0. 0.]]

y_pos = np.arange(len(classes))
# left alignment of data starts at zero
left = np.zeros(len(my_list)) - cell_width/2

##########################
#       Create plot      #
##########################
# Set sea born for prettier graph
seaborn.set()
# create figure
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111)
# Set X axis (shifted)
ax.set_xlim((min(nb_x_range)-cell_width/2, max(nb_x_range)+cell_width/2))

# For each cell
for d in data:
    # Define color for each row
    colors = [colors_def[classes[i]][bi] for i, bi in enumerate(d)]
    # e.g. colors = [colors_def["A"][d[0]], colors_def["B"][d[1]], colors_def["C"][d[2]]]

    # Draw cell
    ax.barh(y_pos, cell_width,
            height=1,           # Heights of horizontal bars
            color=colors,       # Colors
            left=left)          # Left padd from y-axis
    # Update margin
    left += cell_width

# Axis graduation
ax.set_yticks(y_pos)
ax.set_xticks(nb_x_range)
ax.set_yticklabels(classes)
ax.set_xlabel('Stacked bar')

plt.show()

The output looks like this:


问:
Specify quality parameter in node pdf-poppler
I am trying to convert PDF to image.

https://www.npmjs.com/package/pdf-poppler

```<code>const path = require('path');
const pdf = require('pdf-poppler');

let file = 'C:\\tmp\\convertme.pdf'

let opts = {
    format: 'jpeg',
    out_dir: path.dirname(file),
    out_prefix: path.baseName(file, path.extname(file)),
    page: null
}

pdf.convert(file, opts)
    .then(res => {
        console.log('Successfully converted');
    })
    .catch(error => {
        console.error(error);
    })
</code>```

However the images generated doesn't have good quality. How I can specify the quality parameter.

答:
Good question. You can use 'scale' to increase the size of the image, default is 1024.</p>

```<code>let opts = {
    format: 'jpeg',
    out_dir: path.dirname(file)+'/images',
    out_prefix: 'test_',
    page: null,
    scale: 4096
}
</code>```

Body:
I am trying to convert PDF to image.

https://www.npmjs.com/package/pdf-poppler

const path = require('path');
const pdf = require('pdf-poppler');

let file = 'C:\\tmp\\convertme.pdf'

let opts = {
    format: 'jpeg',
    out_dir: path.dirname(file),
    out_prefix: path.baseName(file, path.extname(file)),
    page: null
}

pdf.convert(file, opts)
    .then(res => {
        console.log('Successfully converted');
    })
    .catch(error => {
        console.error(error);
    })

However the images generated doesn't have good quality. How I can specify the quality parameter.

Body:
Good question. You can use 'scale' to increase the size of the image, default is 1024.

let opts = {
    format: 'jpeg',
    out_dir: path.dirname(file)+'/images',
    out_prefix: 'test_',
    page: null,
    scale: 4096
}


问:
function from javascript was working for datepicker and listbox, but now it is not working
before few days function for datepicker and multiselect was woking but now it is not working. cant detect problem</p>

```<code>$(function ()
    {

        $("#txtBirthDate").datepicker({ dateFormat: 'dd/mm/yy' , changeMonth: true, changeYear: true, yearRange: '1945:' + (new Date).getFullYear() });

        $('#lstFinancialArea').multiselect({
            includeSelectAllOption: true
        });
    });

<asp:TextBox ID="txtBirthDate" runat="server" CssClass="form-control datepickerCompleted" MaxLength="20" ReadOnly="true"></asp:TextBox>
</code>```

答: Yes please inspect your code in the browser and there you will notice that during run time text box id would have change actually. So now you can use $('#<%=txtBirthdate.ClientID%>').datepicker(). It would work for you my friend.</p>

Body:
before few days function for datepicker and multiselect was woking but now it is not working. cant detect problem

$(function ()
    {

        $("#txtBirthDate").datepicker({ dateFormat: 'dd/mm/yy' , changeMonth: true, changeYear: true, yearRange: '1945:' + (new Date).getFullYear() });

        $('#lstFinancialArea').multiselect({
            includeSelectAllOption: true
        });
    });

<asp:TextBox ID="txtBirthDate" runat="server" CssClass="form-control datepickerCompleted" MaxLength="20" ReadOnly="true"></asp:TextBox>


问:
Sonar : Replace this lambda with a method reference
This code sample

```<code>Collection<Number> values = transform(
        getValuatedObjects(),
        input -> getValueProvider().apply(input).getValue());
</code>```

violates the Sonarqube rule:

Lambdas should be replaced with method references

Is it a sonar bug?
Or can I really use a method reference?

答:
if you are coding in java 8 you can use method reference in place of lambda expression for code readable

```<code>List<Integer> list = Arrays.asList(1,2,3,4,5);
</code>```

replace this lambda with a method reference

```<code>strList.stream().sorted().collect(Collectors.toList()).forEach(s -> System.out.println(s));
</code>```

Replace

```<code>strList.stream().sorted().collect(Collectors.toList()).forEach(System.out::println);
</code>```

Body:
This code sample

Collection<Number> values = transform(
        getValuatedObjects(),
        input -> getValueProvider().apply(input).getValue());

violates the Sonarqube rule:

Lambdas should be replaced with method references

Is it a sonar bug?
Or can I really use a method reference?

Body:
if you are coding in java 8 you can use method reference in place of lambda expression for code readable

List<Integer> list = Arrays.asList(1,2,3,4,5);

replace this lambda with a method reference

strList.stream().sorted().collect(Collectors.toList()).forEach(s -> System.out.println(s));

Replace

strList.stream().sorted().collect(Collectors.toList()).forEach(System.out::println);


问:
Find Critical Chi Square Value using MathNet.Numerics
So I want to get Critical Chi-Square Value using Significance level and Degrees of Freedom. I tried using MathNet.Numerics but couldn't find which method to use to get the Critical Chi-Square Value

This was the documentation I'm referring, any help on redirecting me to correct documentation would help.

How I calculate the value in Excel is by using the formula =CHISQ.INV.RT(A2,B2)

答:
The function you require is InvCDF(), it is used as follows:</p>

```<code>MathNet.Numerics.Distributions.ChiSquared.InvCDF(degreesOfFreedom, probability);
</code>```

ContentLicense: CC BY-SA 3.0

Body:
The function you require is InvCDF(), it is used as follows:

MathNet.Numerics.Distributions.ChiSquared.InvCDF(degreesOfFreedom, probability);


问:
Read data from memory
I have an API to read data:

ROM_READ_PAGE(BLOCK_NO, PAGE_NO, *READ_BUFFER);

The Page size is 2048 and you can ONLY read WHOLE page at a time.

I know it is just about using C, but my system doesn't run using either of the methods I tried.

For example:

```<code>char *readPtr;
char readBuff[128]; //(Can not use 2048 due to stack overflow)

readPtr = readBuff;

ROM_READ_PAGE(BLOCK_NO, PAGE_NO, readPtr); // SMX doesn't like this and shows some exception
</code>```

Any other idea or if I am missing something important.

答:
If your stack can't be 2048, that doesn't mean that you are excused from providing a 2048 byte block</p>

```<code>char *readPtr = malloc(2048);
ROM_READ_PAGE(BLOCK_NO, PAGE_NO, readPtr);
// do whatever you need
free(readPtr);
</code>```

Title: Read data from memory

Body:
I have an API to read data:

ROM_READ_PAGE(BLOCK_NO, PAGE_NO, *READ_BUFFER);

The Page size is 2048 and you can ONLY read WHOLE page at a time.

I know it is just about using C, but my system doesn't run using either of the methods I tried.

For example:

char *readPtr;
char readBuff[128]; //(Can not use 2048 due to stack overflow)

readPtr = readBuff;

ROM_READ_PAGE(BLOCK_NO, PAGE_NO, readPtr); // SMX doesn't like this and shows some exception

Any other idea or if I am missing something important.

Body:
If your stack can't be 2048, that doesn't mean that you are excused from providing a 2048 byte block

char *readPtr = malloc(2048);
ROM_READ_PAGE(BLOCK_NO, PAGE_NO, readPtr);
// do whatever you need
free(readPtr);


问:
access key request from guzzle for oauth2 in laravel5.8
i want to get access token from mailchimp oauth2 but in every try i was fail. Here i used guzzle and my actual request is

curl --request POST \
--url 'https://login.mailchimp.com/oauth2/token' \
--data "grant_type=authorization_code&client_id={client_id}&client_secret={client_secret}&redirect_uri={encoded_url}&code={code}" \
--include

i already try curl and javascript but javascript request is not accepted by mailchimp
this is code i used:

```<code>$http = new Client(['base_uri' => 'https://login.mailchimp.com/oauth2',
 'defaults' => [
                'exceptions' => false ],
    'header' => [
        'Accept' => 'application/json',
        'Content-Type' => 'application/x-www-form-urlencoded'
    ],
    'verify' => false
    ]);
    $result = $http->request('POST','/token', [
    'form_params' => [
        'grant_type' => 'authorization_code',
        'client_id' => $this->client_id,
        'client_secret' => $this->client_secret,
        'redirect_uri' => $this->redirect_uri,
        'code' => $code,
        ],
    ]);
    $response = $result->send();
    $responseBody = $response->getBody(true);
    var_dump($responseBody);
    die();
</code>```

the expected actual result is:
"access_token":"5c6ccc561059aa386da9d112215bae55","expires_in":0,"scope":null
but my error Client error:
 POST https://login.mailchimp.com/token resulted in a 404 Not Found response:

答:
Due to the way Guzzle works, you are using an absolute URI.

First you set the base_uri:

```<code>$http = new Client(['base_uri' => 'https://login.mailchimp.com/oauth2']);
</code>```

Then make the request:

```<code>$http->request('POST', '/token')
</code>```

Because you've added the leading slash, Guzzle will treat it has an absolute URI and hit https://login.mailchimp.com/token, as it is doing. My guess is the desired endpoint to hit is https://login.mailchimp.com/oauth2/token

This can be fixed by adding a trailing slash to the base_uri and removing the leading slash when performing the request.

Info from the docs: http://docs.guzzlephp.org/en/stable/quickstart.html#creating-a-client

It follows RFC3986

Body:
i want to get access token from mailchimp oauth2 but in every try i was fail. Here i used guzzle and my actual request is

curl --request POST \
--url 'https://login.mailchimp.com/oauth2/token' \
--data "grant_type=authorization_code&client_id={client_id}&client_secret={client_secret}&redirect_uri={encoded_url}&code={code}" \
--include

i already try curl and javascript but javascript request is not accepted by mailchimp
this is code i used:

$http = new Client(['base_uri' => 'https://login.mailchimp.com/oauth2',
 'defaults' => [
                'exceptions' => false ],
    'header' => [
        'Accept' => 'application/json',
        'Content-Type' => 'application/x-www-form-urlencoded'
    ],
    'verify' => false
    ]);
    $result = $http->request('POST','/token', [
    'form_params' => [
        'grant_type' => 'authorization_code',
        'client_id' => $this->client_id,
        'client_secret' => $this->client_secret,
        'redirect_uri' => $this->redirect_uri,
        'code' => $code,
        ],
    ]);
    $response = $result->send();
    $responseBody = $response->getBody(true);
    var_dump($responseBody);
    die();

the expected actual result is:
"access_token":"5c6ccc561059aa386da9d112215bae55","expires_in":0,"scope":null
but my error Client error:
 POST https://login.mailchimp.com/token resulted in a 404 Not Found response:

Body:
Due to the way Guzzle works, you are using an absolute URI.

First you set the base_uri:

$http = new Client(['base_uri' => 'https://login.mailchimp.com/oauth2']);

Then make the request:

$http->request('POST', '/token')

Because you've added the leading slash, Guzzle will treat it has an absolute URI and hit https://login.mailchimp.com/token, as it is doing. My guess is the desired endpoint to hit is https://login.mailchimp.com/oauth2/token

This can be fixed by adding a trailing slash to the base_uri and removing the leading slash when performing the request.

Info from the docs: http://docs.guzzlephp.org/en/stable/quickstart.html#creating-a-client

It follows RFC3986


问:
Make post preview empty if excerpt is blank with Timber
I've tackled this issue before in vanilla Wordpress however I can't seem to find a way to keep the post.preview object blank if the excerpt field is empty. Despite searching around no one else seems to find this an issue!

Anyway, I've come up with a work around based on my previous methods to set a variable in my logic files for the twig template to use which is simple enough and someone else may find useful. Just intrigued to see if I'm missing something that would be possible from within Timber.

Here's the solutions I used and it was placed in page.php

```<code>$excerpt = '';
if ( ! has_excerpt() ) {
    $excerpt = '';
} else {
    $excerpt = get_the_excerpt();
}
$context['post_excerpt'] = $excerpt;
</code>```

答:
Since PHP 7 this should also work as expected and is a much cleaner solution:

$context['post_excerpt'] = get_the_excerpt() ?? '';
```

<p>The <code>??</code> operator is called the 'Null Coalescing Operator'. You can find more about it in the official PHP docs.</p>

<p>An alternative solution is to use the timber "preview"-method on the post class directly in your twig template:</p>

```{{ post.preview }}
```

You can find more info about this in the official Timber docs.

Body:
I've tackled this issue before in vanilla Wordpress however I can't seem to find a way to keep the post.preview object blank if the excerpt field is empty. Despite searching around no one else seems to find this an issue!

Anyway, I've come up with a work around based on my previous methods to set a variable in my logic files for the twig template to use which is simple enough and someone else may find useful. Just intrigued to see if I'm missing something that would be possible from within Timber.

Here's the solutions I used and it was placed in page.php

$excerpt = '';
if ( ! has_excerpt() ) {
    $excerpt = '';
} else {
    $excerpt = get_the_excerpt();
}
$context['post_excerpt'] = $excerpt;

Body:
Since PHP 7 this should also work as expected and is a much cleaner solution:

$context['post_excerpt'] = get_the_excerpt() ?? '';

The ?? operator is called the 'Null Coalescing Operator'. You can find more about it in the official PHP docs.

An alternative solution is to use the timber "preview"-method on the post class directly in your twig template:

{{ post.preview }}

You can find more info about this in the official Timber docs.


问:
Extracting json field from string in Hive using dataset
I am trying a very basic hive query. I am trying to extract a json field from a dataset but I always get

\N

for the json field, however some_string comes okay

Here is my query :

```<code>WITH dataset AS (
SELECT
CAST(
   '{ "traceId": "abc", "additionalData": "{\"Star Rating\":\"3\"}",  "locale": "en_US", "content": { "contentType": "PB", "content": "T S", "bP": { "mD": { "S R": "3" }, "cType": "T_S", "sType": "unknown-s", "bTimestamp": 0, "title": "T S" } }
    }' AS STRING) AS some_string
)
SELECT some_string, get_json_object(dataset.some_string, '$.traceId') FROM dataset
</code>```

Question :  How can I get the json field here ?

答:
The problem is in backslash. Single backslashes treated as escape character for " and removed by Hive:

```<code>hive> select '\"';
OK
"
Time taken: 0.069 seconds, Fetched: 1 row(s)
</code>```

When you have two backslashes, Hive removes one:

```<code>hive> select '\\"';
OK
\"
Time taken: 0.061 seconds, Fetched: 1 row(s)
</code>```

With two backslashes it works fine:

```<code>WITH dataset AS (
  SELECT
  CAST(
     '{ "traceId": "abc", "additionalData": "{\\"Star Rating\\":\\"3\\"}",  "locale": "en_US", "content": { "contentType": "PB", "content": "T S", "bP": { "mD": { "S R": "3" }, "cType": "T_S", "sType": "unknown-s", "bTimestamp": 0, "title": "T S" } }
       }' AS STRING) AS some_string
   )
   SELECT some_string,  get_json_object(dataset.some_string, '$.traceId') FROM dataset;
OK
{ "traceId": "abc", "additionalData": "{\"Star Rating\":\"3\"}",  "locale": "en_US", "content": { "contentType": "PB", "content": "T S", "bP": { "mD": { "S R": "3" }, "cType": "T_S", "sType": "unknown-s", "bTimestamp": 0, "title": "T S" } }
    }   abc
Time taken: 0.788 seconds, Fetched: 1 row(s)
</code>```

You can also easily remove double-quotes before { and after } in additionalData:

```<code>WITH dataset AS (
SELECT
regexp_replace(regexp_replace(
   '{ "traceId": "abc", "additionalData": "{\"Star Rating\":\"3\"}",  "locale": "en_US", "content": { "contentType": "PB", "content": "T S", "bP": { "mD": { "S R": "3" }, "cType": "T_S", "sType": "unknown-s", "bTimestamp": 0, "title": "T S" } }
    }' ,'\\"\\{','\\{') ,'\\}\\"','\\}' )AS some_string
)
SELECT some_string,  get_json_object(dataset.some_string, '$.traceId') FROM dataset;
</code>```

Returns:

```<code>OK
{ "traceId": "abc", "additionalData": {"Star Rating":"3"},  "locale": "en_US", "content": { "contentType": "PB", "content": "T S", "bP": { "mD": { "S R": "3" }, "cType": "T_S", "sType": "unknown-s", "bTimestamp": 0, "title": "T S" } }
    }   abc
Time taken: 7.035 seconds, Fetched: 1 row(s)
</code>```

Body:
I am trying a very basic hive query. I am trying to extract a json field from a dataset but I always get

\N

for the json field, however some_string comes okay

Here is my query :

WITH dataset AS (
SELECT
CAST(
   '{ "traceId": "abc", "additionalData": "{\"Star Rating\":\"3\"}",  "locale": "en_US", "content": { "contentType": "PB", "content": "T S", "bP": { "mD": { "S R": "3" }, "cType": "T_S", "sType": "unknown-s", "bTimestamp": 0, "title": "T S" } }
    }' AS STRING) AS some_string
)
SELECT some_string, get_json_object(dataset.some_string, '$.traceId') FROM dataset

Question :  How can I get the json field here ?

Body:
The problem is in backslash. Single backslashes treated as escape character for " and removed by Hive:

hive> select '\"';
OK
"
Time taken: 0.069 seconds, Fetched: 1 row(s)

When you have two backslashes, Hive removes one:

hive> select '\\"';
OK
\"
Time taken: 0.061 seconds, Fetched: 1 row(s)

With two backslashes it works fine:

WITH dataset AS (
  SELECT
  CAST(
     '{ "traceId": "abc", "additionalData": "{\\"Star Rating\\":\\"3\\"}",  "locale": "en_US", "content": { "contentType": "PB", "content": "T S", "bP": { "mD": { "S R": "3" }, "cType": "T_S", "sType": "unknown-s", "bTimestamp": 0, "title": "T S" } }
       }' AS STRING) AS some_string
   )
   SELECT some_string,  get_json_object(dataset.some_string, '$.traceId') FROM dataset;
OK
{ "traceId": "abc", "additionalData": "{\"Star Rating\":\"3\"}",  "locale": "en_US", "content": { "contentType": "PB", "content": "T S", "bP": { "mD": { "S R": "3" }, "cType": "T_S", "sType": "unknown-s", "bTimestamp": 0, "title": "T S" } }
    }   abc
Time taken: 0.788 seconds, Fetched: 1 row(s)

You can also easily remove double-quotes before { and after } in additionalData:

WITH dataset AS (
SELECT
regexp_replace(regexp_replace(
   '{ "traceId": "abc", "additionalData": "{\"Star Rating\":\"3\"}",  "locale": "en_US", "content": { "contentType": "PB", "content": "T S", "bP": { "mD": { "S R": "3" }, "cType": "T_S", "sType": "unknown-s", "bTimestamp": 0, "title": "T S" } }
    }' ,'\\"\\{','\\{') ,'\\}\\"','\\}' )AS some_string
)
SELECT some_string,  get_json_object(dataset.some_string, '$.traceId') FROM dataset;

Returns:

OK
{ "traceId": "abc", "additionalData": {"Star Rating":"3"},  "locale": "en_US", "content": { "contentType": "PB", "content": "T S", "bP": { "mD": { "S R": "3" }, "cType": "T_S", "sType": "unknown-s", "bTimestamp": 0, "title": "T S" } }
    }   abc
Time taken: 7.035 seconds, Fetched: 1 row(s)


问:
How to make an ASP.Net page with a C# code behind that will work in Sharepoint
I need to develop a page for Sharepoint.

Basically it is a 3 step process that takes some inputs, gets some data and shows a preview of it, then allows the data to be sent off attached to an email.

I know ASP.Net and C#.Net, and the problem should be reasonably easy to solve with an ASP.Net page and a C# code behind.

However I've never had to work with Sharepoint before, and I don't know what is required to make the page 'work in Sharepoint'.

Is there anything special that needs to be done to make it work in Sharepoint? Is an ASPX page the right approach for this sort of problem?

(I'm developing on an XP machine using Visual Studio 2008.)

答:
The answer here:

ASP.net page without a codebehind

has your answer for here.

OwnerDisplayName: Sophia

OwnerDisplayName: GregD


问:
How to keep GDB tracing in the same emacs frame
I use GDB in Emacs 23.2 and while I mostly like it there is one thing that I find very annoying.  When stepping through code if I trace into a function that is located in a source module that is already in an Emacs buffer and happens to be visible in a different frame then that frame is raised and stepping continues there.  Is there a way to have gud just make that buffer visible in the current gdb frame and keep it as the active frame? </p>

答:
I've had the exact same problem.  The only way I've dealt with it to date is to close out other frames, so everything appears in the GDB frame, but that's an awful workaround.  I've done some pretty serious searching through the docs and stackoverflow and found these customizations...

pop-up-windows

sticky windows

dedicated windows

Hope they help.


问:
How to format DataGrid rows based on data's units
I have a DataGrid with a collection of parameters, each of which have properties for name and a value. Each parameters' value can be one of a multitude of units (ft, btuh, kw, currency, string, unit less double, etc.) How do I format the data grid rows to display the value with the units and a unit dependent rounding factor? See below for an example.

The DataGrid can reference different object classes with different collections of parameters so the solution has to be dynamic. Maybe add a Units enum to the parameter class and then somehow read that from the DataGrid and format the row accordingly?

答:
One approach I would take would be make a converter. I've made you one just to pitch the idea but you should probably modify it to suit your needs.

```<code>public class CellUnitConverter : MarkupExtension, IValueConverter
{
    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }

    public string Unit { get; set; }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        //Checks both if the value is null or if the converter somehow gets used ahead of time
        if (value == null || value == DependencyProperty.UnsetValue)
        {
            return string.Empty;
        }

        // You can place some rounding rules here

        return $"{value} {Unit}";
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return null;
    }
}
</code>```

As it uses the MarkupExtension, you can just place it in the binding of the cell

```<code>Binding="{Binding Results, Converter={converters:CellUnitConverter Unit=CFM}}"
</code>```

However, if you want different units depending on the value, you could also Bind the unit to another property such of each element.

Let's you are displaying a collection of DataItem where each one has a Title, Result,Unit. Then, you bind one column to the Title and the other one to the Result, such like

```<code>Binding="{Binding Result, Converter={converters:CellUnitConverter Unit={Binding Unit}}}"
</code>```

Hope this points to the solution.

Body:
One approach I would take would be make a converter. I've made you one just to pitch the idea but you should probably modify it to suit your needs.

public class CellUnitConverter : MarkupExtension, IValueConverter
{
    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }

    public string Unit { get; set; }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        //Checks both if the value is null or if the converter somehow gets used ahead of time
        if (value == null || value == DependencyProperty.UnsetValue)
        {
            return string.Empty;
        }

        // You can place some rounding rules here

        return $"{value} {Unit}";
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return null;
    }
}

As it uses the MarkupExtension, you can just place it in the binding of the cell

Binding="{Binding Results, Converter={converters:CellUnitConverter Unit=CFM}}"

However, if you want different units depending on the value, you could also Bind the unit to another property such of each element.

Let's you are displaying a collection of DataItem where each one has a Title, Result,Unit. Then, you bind one column to the Title and the other one to the Result, such like

Binding="{Binding Result, Converter={converters:CellUnitConverter Unit={Binding Unit}}}"

Hope this points to the solution.


问:
How can I code a Javascript Alert to only populate once per web session?
I am creating a website for my final and have coded an "adult content" alert on my homepage. I am trying to get the site to populate that message only once per session but right now it populates every time you go to the homepage.

The class is HTML and CSS but we were asked to include one basic javascript code and the "alert" seemed most appropriate. I have tried googling this question different ways to get help but it's mostly providing information that is above a very very basic level of javascript.

This is my current code:

```<code><script>
alert("WARNING! Adult Language and Content! The following is intended for
immature adults only. Viewer Discretion is Advised.")
</script>
</code>```

This is the site:
http://mysite.du.edu/~mdarienz/index.html

Since this is for a final, I don't need the code to fix it but if you could maybe steer me in the right direction for how to approach it I would greatly appreciate it.

答:
Using sessionStore we can make sure that per tab/session alert is shown rather than showing each time we load the page.</p>

```<code>!sessionStorage.getItem('alert') &&
(!alert('WARNING! Adult Language and Content! The following is intended for immature adults only. Viewer Discretion is Advised.') &&
sessionStorage.setItem('alert', true));
</code>```

Body:
I am creating a website for my final and have coded an "adult content" alert on my homepage. I am trying to get the site to populate that message only once per session but right now it populates every time you go to the homepage.

The class is HTML and CSS but we were asked to include one basic javascript code and the "alert" seemed most appropriate. I have tried googling this question different ways to get help but it's mostly providing information that is above a very very basic level of javascript.

This is my current code:

This is the site:
http://mysite.du.edu/~mdarienz/index.html

Since this is for a final, I don't need the code to fix it but if you could maybe steer me in the right direction for how to approach it I would greatly appreciate it.

Body:
Using sessionStore we can make sure that per tab/session alert is shown rather than showing each time we load the page.

!sessionStorage.getItem('alert') &&
(!alert('WARNING! Adult Language and Content! The following is intended for immature adults only. Viewer Discretion is Advised.') &&
sessionStorage.setItem('alert', true));


问:
Beautiful Soup [Python] and the extracting of text in a table
i am new to Python and to Beatiful Soup also! I heard about BS. It is told to be a great tool to parse and extract content. So here i am...:

I want to take the content of the first td of a table in a html
document. For example, i have this table

```<code><table class="bp_ergebnis_tab_info">
    <tr>
            <td>
                     This is a sample text
            </td>

            <td>
                     This is the second sample text
            </td>
    </tr>
</table>
</code>```

How can i use beautifulsoup to take the text "This is a sample text"?
I use soup.findAll('table' ,attrs={'class':'bp_ergebnis_tab_info'}) to get
the whole table.

Thanks... or should i try to get  the whole stuff with Perl ... which i am not so familiar with. Another soltion would be a regex in PHP.

See the target  [1]: http://www.schulministerium.nrw.de/BP/SchuleSuchen?action=799.601437941842&SchulAdresseMapDO=142323

Note; since the html is a  bit invalid - i think that we have to do some cleaning. That can cause a lot of PHP code - since we want to solve the job in PHP. Perl would be a good solution too.

Many thanks for some hints and ideas for a starting point
zero

答:
Use "text" to get text between "td"

1) First read table DOM using tag or ID

```<code>soup = BeautifulSoup(self.driver.page_source, "html.parser")
htnm_migration_table = soup.find("table", {'id':'htnm_migration_table'})
</code>```

2) Read tbody

```<code>tbody = htnm_migration_table.find('tbody')
</code>```

3) Read all tr from tbody tag

```<code>trs = tbody.find_all('tr')
</code>```

4) get all tds using tr

```<code>for tr in trs:
      tds = tr.find_all('td')
      for td in tds:
      print(td.text)
</code>```

Body:
i am new to Python and to Beatiful Soup also! I heard about BS. It is told to be a great tool to parse and extract content. So here i am...:

I want to take the content of the first td of a table in a html
document. For example, i have this table

                     This is a sample text

                     This is the second sample text

How can i use beautifulsoup to take the text "This is a sample text"?
I use soup.findAll('table' ,attrs={'class':'bp_ergebnis_tab_info'}) to get
the whole table.

Thanks... or should i try to get  the whole stuff with Perl ... which i am not so familiar with. Another soltion would be a regex in PHP.

See the target  [1]: http://www.schulministerium.nrw.de/BP/SchuleSuchen?action=799.601437941842&SchulAdresseMapDO=142323

Note; since the html is a  bit invalid - i think that we have to do some cleaning. That can cause a lot of PHP code - since we want to solve the job in PHP. Perl would be a good solution too.

Many thanks for some hints and ideas for a starting point
zero

Body:
Use "text" to get text between "td"

1) First read table DOM using tag or ID

soup = BeautifulSoup(self.driver.page_source, "html.parser")
htnm_migration_table = soup.find("table", {'id':'htnm_migration_table'})

2) Read tbody

tbody = htnm_migration_table.find('tbody')

3) Read all tr from tbody tag

trs = tbody.find_all('tr')

4) get all tds using tr

for tr in trs:
      tds = tr.find_all('td')
      for td in tds:
      print(td.text)


问:
Close JqueryUI dialog from inside an asp.net mvc partial view form
I have the following partial view:

```<code>@using (Ajax.BeginForm("EditUserRole", "Administrator", null, new AjaxOptions{ OnSuccess = "CloseWnd()"))
{
    <input type="submit" class="jqBtn" />
}
</code>```

I get this by ajax and insert it into a dialog like this:

```<code>@Ajax.ActionLink(" ", "GetEditUserRoleControl", new { userId = x.UserId }, new AjaxOptions { UpdateTargetId = "WndContent", OnSuccess="OpenWnd('Edit User Roles')" }, new { @class = "ui-icon ui-icon-person BtnIcon" })

<div id="WndContent" style="display: none;">

</div>

<script>
function OpenWnd(title) {
   var $dialog = $("#WndContent").dialog({ title: title, modal: true });
   //get the widget element
   var $widg = $dialog.dialog('widget');
   //find all buttons and apply button:
   $("input:submit.jqBtn", $widg).button();

}
</script>
</code>```

I need after I submit the form, I close the box. But it seems I'll be writing code in the partial control that depends on the dialog. I also do not want to use the built in jquery buttons because I'll have to remove the button from my partial form. Any help?

答:
Well you could look in the content for a form, and bind to the submit function.

```<code>$("form", $widg).submit(function(){
    /* submit callback */
});
</code>```

This will tell you when the form is submitted however, not when the ajax submit status.

My suggestion would be to use the dialog buttons (since that is how this dialog is designed to be used) and modify your view to optionally render the buttons. You could pass another key modalView=true through the action link.

Body:
I have the following partial view:

@using (Ajax.BeginForm("EditUserRole", "Administrator", null, new AjaxOptions{ OnSuccess = "CloseWnd()"))
{
    <input type="submit" class="jqBtn" />
}

I get this by ajax and insert it into a dialog like this:

@Ajax.ActionLink(" ", "GetEditUserRoleControl", new { userId = x.UserId }, new AjaxOptions { UpdateTargetId = "WndContent", OnSuccess="OpenWnd('Edit User Roles')" }, new { @class = "ui-icon ui-icon-person BtnIcon" })

I need after I submit the form, I close the box. But it seems I'll be writing code in the partial control that depends on the dialog. I also do not want to use the built in jquery buttons because I'll have to remove the button from my partial form. Any help?

Body:
Well you could look in the content for a form, and bind to the submit function.

$("form", $widg).submit(function(){
    /* submit callback */
});

This will tell you when the form is submitted however, not when the ajax submit status.

My suggestion would be to use the dialog buttons (since that is how this dialog is designed to be used) and modify your view to optionally render the buttons. You could pass another key modalView=true through the action link.


问:
Declaring foreign key constraint for two foreign keys from two different tables
I want to declare constraint "fk" for two foreign keys student_id and subject_id from two different tables student and subject respectively.

I've alredy tried the following but it doesn't work

constraint fk foreign key(subject_id) and foreign key(student_id) references subject(subject_id) and student(student_id)

I cannot use two constraints that's why I am facing this problem. It has to be "fk".

答:
You need to use two foreign keys:

MySQL / Oracle Setup:

```<code>CREATE TABLE student ( student_id INT PRIMARY KEY );
CREATE TABLE subject ( subject_id INT PRIMARY KEY );
</code>```

Create Table:

```<code>CREATE TABLE your_table2 (
  subject_id INT,
  student_id INT,
  CONSTRAINT your_table2__subject_id__fk FOREIGN KEY ( subject_id ) REFERENCES subject ( subject_id ),
  CONSTRAINT your_table2__student_if__fk FOREIGN KEY ( student_id ) REFERENCES student ( student_id )
)
</code>```

or, in Oracle, you can also use inline constraints:

```<code>CREATE TABLE your_table (
  subject_id INT CONSTRAINT your_table__subject_id__fk REFERENCES subject ( subject_id ),
  student_id INT CONSTRAINT your_table__student_if__fk REFERENCES student ( student_id )
)
</code>```

I want to declare constraint "fk" for two foreign keys student_id and subject_id from two different tables student and subject respectively.

You can't declare a single foreign key constraint which references two tables; the syntax requires that you only reference a single table.

I suppose, in Oracle, you could create a materialized view on the cross join of the two tables you want to reference and create a composite primary key on the student_id and subject_id columns and then reference the material view in the foreign key rather than the underlying tables but this seems to be a far too convoluted method to solve the issue. Instead, you should be pushing back against the requirement that you only use a single foreign key when the expected solution is to use two.

Body:
You need to use two foreign keys:

MySQL / Oracle Setup:

CREATE TABLE student ( student_id INT PRIMARY KEY );
CREATE TABLE subject ( subject_id INT PRIMARY KEY );

Create Table:

CREATE TABLE your_table2 (
  subject_id INT,
  student_id INT,
  CONSTRAINT your_table2__subject_id__fk FOREIGN KEY ( subject_id ) REFERENCES subject ( subject_id ),
  CONSTRAINT your_table2__student_if__fk FOREIGN KEY ( student_id ) REFERENCES student ( student_id )
)

or, in Oracle, you can also use inline constraints:

CREATE TABLE your_table (
  subject_id INT CONSTRAINT your_table__subject_id__fk REFERENCES subject ( subject_id ),
  student_id INT CONSTRAINT your_table__student_if__fk REFERENCES student ( student_id )
)

I want to declare constraint "fk" for two foreign keys student_id and subject_id from two different tables student and subject respectively.

You can't declare a single foreign key constraint which references two tables; the syntax requires that you only reference a single table.

I suppose, in Oracle, you could create a materialized view on the cross join of the two tables you want to reference and create a composite primary key on the student_id and subject_id columns and then reference the material view in the foreign key rather than the underlying tables but this seems to be a far too convoluted method to solve the issue. Instead, you should be pushing back against the requirement that you only use a single foreign key when the expected solution is to use two.


问:
How to update ListView entries and let the user continue and scrolling the listview values?
I am trying to  update the list of listView while the user is scrolling the list and give the user the option to continue and scrolling the new entries but with no success and i need an help.

In my application, i have a chat and i am saving history of the chat at SQLite. when the user is entered to the chat, the application is reading only the last 25 message and present the newest message at the bottom of the list.
In order that do that i am using the following options at the XML:

```<code>    android:transcriptMode="alwaysScroll"
    android:stackFromBottom="true"
</code>```

and it is working fine.
Now , when the user is starting to scroll up and checking the history of the chat, when he is arriving to the top of the list, i am accessing the database and retrieving another 25 messages and add them to the top of the list and i am calling to:
        chatAdaptor.notifyDataSetChanged();

but when the user is trying to scroll up, nothing is presented, only after scrolling down and up again the new entries are presented.

I have try to set the position of the list view using setSelection and smoothScrollToPosition but it is not really work.

how can it update the list with new entries and give the user the option to continue and scroll up?

here is the relevant code:

```<code><ListView
    android:id="@+id/chatList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="bottom"
    android:layout_margin="2dp"
    android:layout_weight="1"
    android:transcriptMode="alwaysScroll"
    android:stackFromBottom="true"
    android:background="@drawable/frame_tab"></ListView>

public void showChatLines() {

    chatLinesView = chatTextDialog.findViewById(R.id.chatList);

    chatAdaptor = new GenericAdapter<ChatLine>(activity, chatLines) {
        @Override
        public View getMyView(int position, View convertView, ViewGroup parent, ChatLine chatLine) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.chat_line, null);

            TextView chatLineText = view.findViewById(R.id.chatLine);
            chatLineText.setText(chatLine.messageText);
            TextView chatLineTime = view.findViewById(R.id.chatLineTime);
            chatLineTime.setText(chatLine.displayDateTime);
            TextView chatLineUserName = view.findViewById(R.id.userName);
            chatLineUserName.setText(chatLine.userDisplayName);
            chatLineUserName.setTextColor(activity.getColor(Constants.colors[getColorBy(chatLine.userName)]));

            LinearLayout chatLineLayout = view.findViewById(R.id.chatLineLayout);
            LinearLayout.LayoutParams chatLineLayoutParams = (LinearLayout.LayoutParams) chatLineLayout.getLayoutParams();
            CardView chatLineBackground = view.findViewById(R.id.chatLineBackground);
            LinearLayout.LayoutParams chatLineBackgroundParams = (LinearLayout.LayoutParams) chatLineBackground.getLayoutParams();
            LinearLayout chatLineSubLayout = view.findViewById(R.id.chatLineSubLayout);
            FrameLayout.LayoutParams chatLineSubLayoutParams = (FrameLayout.LayoutParams) chatLineSubLayout.getLayoutParams();

            if (chatLine.userName.equals(logonUserName)) {
                CardView charLineCard = view.findViewById(R.id.chatLineBackground);
                charLineCard.setCardBackgroundColor(activity.getColor(R.color.focusedColor));
                ((ViewGroup) chatLineUserName.getParent()).removeView(chatLineUserName);
            } else {
                chatLineLayoutParams.gravity = Gravity.RIGHT;
                chatLineLayout.setLayoutParams(chatLineLayoutParams);
                chatLineSubLayoutParams.gravity = Gravity.RIGHT;
                chatLineSubLayout.setLayoutParams(chatLineSubLayoutParams);
                chatLineBackgroundParams.gravity = Gravity.RIGHT;
                chatLineBackground.setLayoutParams(chatLineBackgroundParams);
            }

            if (position==0 & fromLine!=0) {
                getNextChatLinesFromDb(position);
            }
            return view;
        }
    };

    chatLinesView.setAdapter(chatAdaptor);

}

private void getNextChatLinesFromDb(int position) {
    toLine = fromLine - 1;
    fromLine -= pageSize;
    if (fromLine < 0) {
        fromLine = 0;
    }
    ArrayList<ChatLine> newChatLines = db.getChatLines(chatRoomId, fromLine, toLine);
    chatLines.addAll(0, newChatLines);
    chatAdaptor.notifyDataSetChanged();
}
</code>```

答:
Try below sample code:

activity_main.xml

```<code><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<ListView
    android:id="@+id/listView"
    android:transcriptMode="alwaysScroll"
    android:stackFromBottom="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></ListView>

</LinearLayout>
</code>```

MainActivity.java

```<code>public class MainActivity extends AppCompatActivity implements AbsListView.OnScrollListener{

final static private int ADD_DATA_SIZE = 10;

ArrayList<String> dataList;
ListView listView;
ArrayAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    dataList = new ArrayList<>();
    for(int i=0; i<30; i++){
        dataList.add("Item " + i);
    }
    listView = findViewById(R.id.listView);
    adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, dataList);
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Toast.makeText(getApplicationContext(), adapterView.getItemAtPosition(i) + " Clicked!", Toast.LENGTH_SHORT).show();
        }
    });
    listView.setOnScrollListener(this);
}

private void addNewData() {
    ArrayList<String> tmpList = new ArrayList<>();
    for(int i=0; i<ADD_DATA_SIZE; i++){
        tmpList.add("Add " + (dataList.size() - 20)/10 + ": Item " + i);
    }
    dataList.addAll(0, tmpList);

    // Try this:
    //listView.setSelection(ADD_DATA_SIZE);
    //adapter.notifyDataSetInvalidated();
    // or this:
    adapter.notifyDataSetChanged();
    listView.setSelection(ADD_DATA_SIZE);

}

@Override
public void onScrollStateChanged(AbsListView absListView, int i) {}

@Override
public void onScroll(AbsListView absListView, int firstVisibleItem,
                     int visibleItemCount, int totalItemCount) {
    if(firstVisibleItem == 0){
        View view = absListView.getChildAt(0);
        if(view != null){
            if (view.getTop() == absListView.getTop()) {
                absListView.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_DISABLED);
                Toast.makeText(getApplicationContext(), "TRANSCRIPT MODE CHANGED TO: "
                        + absListView.getTranscriptMode(), Toast.LENGTH_SHORT).show();
                addNewData();
            }
        }
    } else if (totalItemCount == (firstVisibleItem + visibleItemCount)){
        View view = absListView.getChildAt(visibleItemCount - 1);
        if (view.getBottom() == absListView.getBottom()) {
            absListView.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
            Toast.makeText(getApplicationContext(), "TRANSCRIPT MODE CHANGED TO: "
                    + absListView.getTranscriptMode(), Toast.LENGTH_SHORT).show();
        }
    }
}
}
</code>```

Hope it helps!

Body:
I am trying to  update the list of listView while the user is scrolling the list and give the user the option to continue and scrolling the new entries but with no success and i need an help.

In my application, i have a chat and i am saving history of the chat at SQLite. when the user is entered to the chat, the application is reading only the last 25 message and present the newest message at the bottom of the list.
In order that do that i am using the following options at the XML:

    android:transcriptMode="alwaysScroll"
    android:stackFromBottom="true"

and it is working fine.
Now , when the user is starting to scroll up and checking the history of the chat, when he is arriving to the top of the list, i am accessing the database and retrieving another 25 messages and add them to the top of the list and i am calling to:
        chatAdaptor.notifyDataSetChanged();

but when the user is trying to scroll up, nothing is presented, only after scrolling down and up again the new entries are presented.

I have try to set the position of the list view using setSelection and smoothScrollToPosition but it is not really work.

how can it update the list with new entries and give the user the option to continue and scroll up?

here is the relevant code:

<ListView
    android:id="@+id/chatList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="bottom"
    android:layout_margin="2dp"
    android:layout_weight="1"
    android:transcriptMode="alwaysScroll"
    android:stackFromBottom="true"
    android:background="@drawable/frame_tab"></ListView>

public void showChatLines() {

    chatLinesView = chatTextDialog.findViewById(R.id.chatList);

    chatAdaptor = new GenericAdapter<ChatLine>(activity, chatLines) {
        @Override
        public View getMyView(int position, View convertView, ViewGroup parent, ChatLine chatLine) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.chat_line, null);

            TextView chatLineText = view.findViewById(R.id.chatLine);
            chatLineText.setText(chatLine.messageText);
            TextView chatLineTime = view.findViewById(R.id.chatLineTime);
            chatLineTime.setText(chatLine.displayDateTime);
            TextView chatLineUserName = view.findViewById(R.id.userName);
            chatLineUserName.setText(chatLine.userDisplayName);
            chatLineUserName.setTextColor(activity.getColor(Constants.colors[getColorBy(chatLine.userName)]));

            LinearLayout chatLineLayout = view.findViewById(R.id.chatLineLayout);
            LinearLayout.LayoutParams chatLineLayoutParams = (LinearLayout.LayoutParams) chatLineLayout.getLayoutParams();
            CardView chatLineBackground = view.findViewById(R.id.chatLineBackground);
            LinearLayout.LayoutParams chatLineBackgroundParams = (LinearLayout.LayoutParams) chatLineBackground.getLayoutParams();
            LinearLayout chatLineSubLayout = view.findViewById(R.id.chatLineSubLayout);
            FrameLayout.LayoutParams chatLineSubLayoutParams = (FrameLayout.LayoutParams) chatLineSubLayout.getLayoutParams();

            if (chatLine.userName.equals(logonUserName)) {
                CardView charLineCard = view.findViewById(R.id.chatLineBackground);
                charLineCard.setCardBackgroundColor(activity.getColor(R.color.focusedColor));
                ((ViewGroup) chatLineUserName.getParent()).removeView(chatLineUserName);
            } else {
                chatLineLayoutParams.gravity = Gravity.RIGHT;
                chatLineLayout.setLayoutParams(chatLineLayoutParams);
                chatLineSubLayoutParams.gravity = Gravity.RIGHT;
                chatLineSubLayout.setLayoutParams(chatLineSubLayoutParams);
                chatLineBackgroundParams.gravity = Gravity.RIGHT;
                chatLineBackground.setLayoutParams(chatLineBackgroundParams);
            }

            if (position==0 & fromLine!=0) {
                getNextChatLinesFromDb(position);
            }
            return view;
        }
    };

    chatLinesView.setAdapter(chatAdaptor);

}

private void getNextChatLinesFromDb(int position) {
    toLine = fromLine - 1;
    fromLine -= pageSize;
    if (fromLine < 0) {
        fromLine = 0;
    }
    ArrayList<ChatLine> newChatLines = db.getChatLines(chatRoomId, fromLine, toLine);
    chatLines.addAll(0, newChatLines);
    chatAdaptor.notifyDataSetChanged();
}

Body:
Try below sample code:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<ListView
    android:id="@+id/listView"
    android:transcriptMode="alwaysScroll"
    android:stackFromBottom="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></ListView>

</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity implements AbsListView.OnScrollListener{

final static private int ADD_DATA_SIZE = 10;

ArrayList<String> dataList;
ListView listView;
ArrayAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    dataList = new ArrayList<>();
    for(int i=0; i<30; i++){
        dataList.add("Item " + i);
    }
    listView = findViewById(R.id.listView);
    adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, dataList);
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Toast.makeText(getApplicationContext(), adapterView.getItemAtPosition(i) + " Clicked!", Toast.LENGTH_SHORT).show();
        }
    });
    listView.setOnScrollListener(this);
}

private void addNewData() {
    ArrayList<String> tmpList = new ArrayList<>();
    for(int i=0; i<ADD_DATA_SIZE; i++){
        tmpList.add("Add " + (dataList.size() - 20)/10 + ": Item " + i);
    }
    dataList.addAll(0, tmpList);

    // Try this:
    //listView.setSelection(ADD_DATA_SIZE);
    //adapter.notifyDataSetInvalidated();
    // or this:
    adapter.notifyDataSetChanged();
    listView.setSelection(ADD_DATA_SIZE);

}

@Override
public void onScrollStateChanged(AbsListView absListView, int i) {}

@Override
public void onScroll(AbsListView absListView, int firstVisibleItem,
                     int visibleItemCount, int totalItemCount) {
    if(firstVisibleItem == 0){
        View view = absListView.getChildAt(0);
        if(view != null){
            if (view.getTop() == absListView.getTop()) {
                absListView.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_DISABLED);
                Toast.makeText(getApplicationContext(), "TRANSCRIPT MODE CHANGED TO: "
                        + absListView.getTranscriptMode(), Toast.LENGTH_SHORT).show();
                addNewData();
            }
        }
    } else if (totalItemCount == (firstVisibleItem + visibleItemCount)){
        View view = absListView.getChildAt(visibleItemCount - 1);
        if (view.getBottom() == absListView.getBottom()) {
            absListView.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
            Toast.makeText(getApplicationContext(), "TRANSCRIPT MODE CHANGED TO: "
                    + absListView.getTranscriptMode(), Toast.LENGTH_SHORT).show();
        }
    }
}
}

Hope it helps!