问:
caffe - network produce zero gradient and not learning
I'm training caffenet with multilabel data. However the lost is not decreasing during training phase. I'm now trying to check if the backward() is not working properly. I have this code to check if there is a gradient.

```<code>    import numpy as np
    import os.path as osp
    import matplotlib.pyplot as plt

    from pprint import pprint
    from copy import copy

    % matplotlib inline

    plt.rcParams['figure.figsize'] = (6, 6)

    caffe_root = '../'  # this file is expected to be in {caffe_root}/examples
    sys.path.append(caffe_root + 'python')
    import caffe # If you get "No module named _caffe", either you have not built pycaffe or you have the wrong path.

    from caffe import layers as L, params as P # Shortcuts to define the net prototxt.

    sys.path.append("pycaffe/layers") # the datalayers we will use are in this directory.
    sys.path.append("pycaffe") # the tools file is in this folder

    import tools #this contains some tools that we need

    # set data root directory, e.g:
    peta_root = osp.join('/root/data/PETA/')

    # these are the PASCAL classes, we'll need them later.
    #classes = np.asarray(['aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor'])

# make sure we have the caffenet weight downloaded.
if not os.path.isfile(caffe_root + 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel'):
    print("Downloading pre-trained CaffeNet model...")
    !../scripts/download_model_binary.py ../models/bvlc_reference_caffenet

# initialize caffe for gpu mode
caffe.set_mode_gpu()
caffe.set_device(1)

# helper function for common structures
def conv_relu(bottom, ks, nout, stride=1, pad=0, group=1):
    conv = L.Convolution(bottom, kernel_size=ks, stride=stride,
                                num_output=nout, pad=pad, group=group)#,weight_filler=dict(type='xavier'))
    return conv, L.ReLU(conv, in_place=True)

# another helper function
def fc_relu(bottom, nout):
    fc = L.InnerProduct(bottom, num_output=nout)
    return fc, L.ReLU(fc, in_place=True)

# yet another helper function
def max_pool(bottom, ks, stride=1):
    return L.Pooling(bottom, pool=P.Pooling.MAX, kernel_size=ks, stride=stride)

# main netspec wrapper
def caffenet_multilabel(data_layer_params, datalayer):
    # setup the python data layer
    n = caffe.NetSpec()
    n.data, n.label = L.Python(module = 'peta_multilabel_datalayers', layer = datalayer,
                               ntop = 2, param_str=str(data_layer_params))

    # the net itself
    n.conv1, n.relu1 = conv_relu(n.data, 11, 96, stride=4)
    n.pool1 = max_pool(n.relu1, 3, stride=2)
    n.norm1 = L.LRN(n.pool1, local_size=5, alpha=1e-4, beta=0.75)
    n.conv2, n.relu2 = conv_relu(n.norm1, 5, 256, pad=2, group=2)
    n.pool2 = max_pool(n.relu2, 3, stride=2)
    n.norm2 = L.LRN(n.pool2, local_size=5, alpha=1e-4, beta=0.75)
    n.conv3, n.relu3 = conv_relu(n.norm2, 3, 384, pad=1)
    n.conv4, n.relu4 = conv_relu(n.relu3, 3, 384, pad=1, group=2)
    n.conv5, n.relu5 = conv_relu(n.relu4, 3, 256, pad=1, group=2)
    n.pool5 = max_pool(n.relu5, 3, stride=2)
    n.fc6, n.relu6 = fc_relu(n.pool5, 4096)
    n.drop6 = L.Dropout(n.relu6, in_place=True)
    n.fc7, n.relu7 = fc_relu(n.drop6, 4096)
    n.drop7 = L.Dropout(n.relu7, in_place=True)
    n.score = L.InnerProduct(n.drop7, num_output=2)
    n.loss = L.SigmoidCrossEntropyLoss(n.score, n.label)

    return str(n.to_proto())

workdir = './peta_multilabel_with_datalayer'
if not os.path.isdir(workdir):
    os.makedirs(workdir)

solverprototxt = tools.CaffeSolver(trainnet_prototxt_path = osp.join(workdir, "trainnet.prototxt"), testnet_prototxt_path = osp.join(workdir, "valnet.prototxt"))
solverprototxt.sp['display'] = "1"
solverprototxt.sp['base_lr'] = "0.0001"
solverprototxt.write(osp.join(workdir, 'solver.prototxt'))

# write train net.
with open(osp.join(workdir, 'trainnet.prototxt'), 'w') as f:
    # provide parpeta_multilabel_with_datalayerameters to the data layer as a python dictionary. Easy as pie!
    data_layer_params = dict(batch_size = 128, im_shape = [227, 227], split = 'train', peta_root = peta_root)
    f.write(caffenet_multilabel(data_layer_params, 'PetaMultilabelDataLayerSync'))

# write validation net.
with open(osp.join(workdir, 'valnet.prototxt'), 'w') as f:
    data_layer_params = dict(batch_size = 128, im_shape = [227, 227], split = 'val', peta_root = peta_root)
    f.write(caffenet_multilabel(data_layer_params, 'PetaMultilabelDataLayerSync'))

solver = caffe.SGDSolver(osp.join(workdir, 'solver.prototxt'))
#solver.net.copy_from(caffe_root + 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel')
solver.test_nets[0].share_with(solver.net)
#solver.step(1)

solver.net.top_names
solver.net.backward()
solver.step(1)
print solver.net.params['fc6'][0].data[...]
print solver.net.blobs['fc6'].data[...]
print solver.net.blobs['fc6'].diff[...]
</code>```

However the output of gradient seems to be zero and the weight is not update at all.

```<code>[[ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 ...,
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]]
[[ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 ...,
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]]
[[ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 ...,
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]]
</code>```

Anyone knows what happened?

答:
Looking at the values of your params: they are all zeros. You did not define fillers for your layers, thus all you get is zeros.

Define random initializers for the weights and run again.

Running only backward() is meaningless - the loss is computed during forward() pass, there is no loss information propagated through the net for the backward pass to use.

Call forward() before backward() to make one complete forward-backward pass.

Body:
I'm training caffenet with multilabel data. However the lost is not decreasing during training phase. I'm now trying to check if the backward() is not working properly. I have this code to check if there is a gradient.

    import numpy as np
    import os.path as osp
    import matplotlib.pyplot as plt

    from pprint import pprint
    from copy import copy

    % matplotlib inline

    plt.rcParams['figure.figsize'] = (6, 6)

    caffe_root = '../'  # this file is expected to be in {caffe_root}/examples
    sys.path.append(caffe_root + 'python')
    import caffe # If you get "No module named _caffe", either you have not built pycaffe or you have the wrong path.

    from caffe import layers as L, params as P # Shortcuts to define the net prototxt.

    sys.path.append("pycaffe/layers") # the datalayers we will use are in this directory.
    sys.path.append("pycaffe") # the tools file is in this folder

    import tools #this contains some tools that we need

    # set data root directory, e.g:
    peta_root = osp.join('/root/data/PETA/')

    # these are the PASCAL classes, we'll need them later.
    #classes = np.asarray(['aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor'])

# make sure we have the caffenet weight downloaded.
if not os.path.isfile(caffe_root + 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel'):
    print("Downloading pre-trained CaffeNet model...")
    !../scripts/download_model_binary.py ../models/bvlc_reference_caffenet

# initialize caffe for gpu mode
caffe.set_mode_gpu()
caffe.set_device(1)

# helper function for common structures
def conv_relu(bottom, ks, nout, stride=1, pad=0, group=1):
    conv = L.Convolution(bottom, kernel_size=ks, stride=stride,
                                num_output=nout, pad=pad, group=group)#,weight_filler=dict(type='xavier'))
    return conv, L.ReLU(conv, in_place=True)

# another helper function
def fc_relu(bottom, nout):
    fc = L.InnerProduct(bottom, num_output=nout)
    return fc, L.ReLU(fc, in_place=True)

# yet another helper function
def max_pool(bottom, ks, stride=1):
    return L.Pooling(bottom, pool=P.Pooling.MAX, kernel_size=ks, stride=stride)

# main netspec wrapper
def caffenet_multilabel(data_layer_params, datalayer):
    # setup the python data layer
    n = caffe.NetSpec()
    n.data, n.label = L.Python(module = 'peta_multilabel_datalayers', layer = datalayer,
                               ntop = 2, param_str=str(data_layer_params))

    # the net itself
    n.conv1, n.relu1 = conv_relu(n.data, 11, 96, stride=4)
    n.pool1 = max_pool(n.relu1, 3, stride=2)
    n.norm1 = L.LRN(n.pool1, local_size=5, alpha=1e-4, beta=0.75)
    n.conv2, n.relu2 = conv_relu(n.norm1, 5, 256, pad=2, group=2)
    n.pool2 = max_pool(n.relu2, 3, stride=2)
    n.norm2 = L.LRN(n.pool2, local_size=5, alpha=1e-4, beta=0.75)
    n.conv3, n.relu3 = conv_relu(n.norm2, 3, 384, pad=1)
    n.conv4, n.relu4 = conv_relu(n.relu3, 3, 384, pad=1, group=2)
    n.conv5, n.relu5 = conv_relu(n.relu4, 3, 256, pad=1, group=2)
    n.pool5 = max_pool(n.relu5, 3, stride=2)
    n.fc6, n.relu6 = fc_relu(n.pool5, 4096)
    n.drop6 = L.Dropout(n.relu6, in_place=True)
    n.fc7, n.relu7 = fc_relu(n.drop6, 4096)
    n.drop7 = L.Dropout(n.relu7, in_place=True)
    n.score = L.InnerProduct(n.drop7, num_output=2)
    n.loss = L.SigmoidCrossEntropyLoss(n.score, n.label)

    return str(n.to_proto())

workdir = './peta_multilabel_with_datalayer'
if not os.path.isdir(workdir):
    os.makedirs(workdir)

solverprototxt = tools.CaffeSolver(trainnet_prototxt_path = osp.join(workdir, "trainnet.prototxt"), testnet_prototxt_path = osp.join(workdir, "valnet.prototxt"))
solverprototxt.sp['display'] = "1"
solverprototxt.sp['base_lr'] = "0.0001"
solverprototxt.write(osp.join(workdir, 'solver.prototxt'))

# write train net.
with open(osp.join(workdir, 'trainnet.prototxt'), 'w') as f:
    # provide parpeta_multilabel_with_datalayerameters to the data layer as a python dictionary. Easy as pie!
    data_layer_params = dict(batch_size = 128, im_shape = [227, 227], split = 'train', peta_root = peta_root)
    f.write(caffenet_multilabel(data_layer_params, 'PetaMultilabelDataLayerSync'))

# write validation net.
with open(osp.join(workdir, 'valnet.prototxt'), 'w') as f:
    data_layer_params = dict(batch_size = 128, im_shape = [227, 227], split = 'val', peta_root = peta_root)
    f.write(caffenet_multilabel(data_layer_params, 'PetaMultilabelDataLayerSync'))

solver = caffe.SGDSolver(osp.join(workdir, 'solver.prototxt'))
#solver.net.copy_from(caffe_root + 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel')
solver.test_nets[0].share_with(solver.net)
#solver.step(1)

solver.net.top_names
solver.net.backward()
solver.step(1)
print solver.net.params['fc6'][0].data[...]
print solver.net.blobs['fc6'].data[...]
print solver.net.blobs['fc6'].diff[...]

However the output of gradient seems to be zero and the weight is not update at all.

[[ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 ...,
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]]
[[ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 ...,
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]]
[[ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 ...,
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]]

Anyone knows what happened?


来源: stackexchange

问:
WARNING in Circular dependency detected Compiled with warnings
When I am trying to my build project it throws error-

WARNING in Circular dependency detected:
  src\app\own-filter\own.filter.module.ts -> src\app\own-filter\own.filter.component.ts -> src\app\own-filter\own.filter.module.t

Thanks in Advance. Let me know if any more details are required.

答:
You have an import inside your OwnFilterComponent which imports the OwnFilterModule. I cannot think of a reason that you should need that. But without any actual code from you it's hard to see, why you want to import a module into a component.

To turn off this warning you have to put this line in your .angular-cli.json:

```<code>{
  ...,
  "defaults": {
    ...,
    "build": {
      "showCircularDependencies": false
    }
  }
}
</code>```

Only do this if you really really have no other way to resolve this circular dependency. But like I stated before, I'm pretty sure you can get around this one.

The reason it warns for this, is because a circular dependency will cause issues using the closure compiler

ContentLicense: CC BY-SA 3.0

Body:
You have an import inside your OwnFilterComponent which imports the OwnFilterModule. I cannot think of a reason that you should need that. But without any actual code from you it's hard to see, why you want to import a module into a component.

To turn off this warning you have to put this line in your .angular-cli.json:

{
  ...,
  "defaults": {
    ...,
    "build": {
      "showCircularDependencies": false
    }
  }
}

Only do this if you really really have no other way to resolve this circular dependency. But like I stated before, I'm pretty sure you can get around this one.

The reason it warns for this, is because a circular dependency will cause issues using the closure compiler


问:
spring security keycloak adapter
I'm trying to use Keycloak with my spring boot application.

I want to restrict access to a particular URL based on the REST method and user's role.

In the example below, users with any of the roles view-all or calendar can perform a GET, while those with manage-all or calendar_manage can do a POST, PUT, or DELETE.

Unfortunately, this configuration is allowing any authenticated user access to the /api/calendar URL.  What am I doing wrong?

```<code>@Configuration
@EnableWebSecurity
class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter {
...
    @Override
    protected void configure(HttpSecurity http) throws Exception
    {
        super.configure(http);
        http
                .csrf().disable()
                .antMatcher("/api/**")
                .authorizeRequests()
                .antMatchers(HttpMethod.GET, "/api/calendar/*").hasAnyRole("view-all", "calendar")
                .antMatchers(HttpMethod.POST, "/api/calendar/*").hasAnyRole("manage-all", "calendar_manage")
                .antMatchers(HttpMethod.PUT, "/api/calendar/*").hasAnyRole("manage-all", "calendar_manage")
                .antMatchers(HttpMethod.DELETE, "/api/calendar/*").hasAnyRole("manage-all", "calendar_manage");
    }
}
</code>```

答:
try with isAuthenticated and hasAnyAuthority and permission to  ("/api/**")

```<code>       @Configuration
        @EnableWebSecurity
        class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter {
        ...
            @Override
            protected void configure(HttpSecurity http) throws Exception
            {
                super.configure(http);
                http    .authorizeRequests()
                        .csrf().disable()
                        .antMatcher("/api/**").permitAll()
                        .isAuthenticated()
                        .antMatchers(HttpMethod.GET, "/api/calendar/*").hasAnyAuthority("view-all", "calendar")
                        .antMatchers(HttpMethod.POST, "/api/calendar/*").hasAnyAuthority("manage-all", "calendar_manage")
                        .antMatchers(HttpMethod.PUT, "/api/calendar/*").hasAnyAuthority("manage-all", "calendar_manage")
                        .antMatchers(HttpMethod.DELETE, "/api/calendar/*").hasAnyAuthority("manage-all", "calendar_manage");
            }
        }
</code>```

Title: spring security keycloak adapter

Body:
I'm trying to use Keycloak with my spring boot application.

I want to restrict access to a particular URL based on the REST method and user's role.

In the example below, users with any of the roles view-all or calendar can perform a GET, while those with manage-all or calendar_manage can do a POST, PUT, or DELETE.

Unfortunately, this configuration is allowing any authenticated user access to the /api/calendar URL.  What am I doing wrong?

@Configuration
@EnableWebSecurity
class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter {
...
    @Override
    protected void configure(HttpSecurity http) throws Exception
    {
        super.configure(http);
        http
                .csrf().disable()
                .antMatcher("/api/**")
                .authorizeRequests()
                .antMatchers(HttpMethod.GET, "/api/calendar/*").hasAnyRole("view-all", "calendar")
                .antMatchers(HttpMethod.POST, "/api/calendar/*").hasAnyRole("manage-all", "calendar_manage")
                .antMatchers(HttpMethod.PUT, "/api/calendar/*").hasAnyRole("manage-all", "calendar_manage")
                .antMatchers(HttpMethod.DELETE, "/api/calendar/*").hasAnyRole("manage-all", "calendar_manage");
    }
}

Body:
try with isAuthenticated and hasAnyAuthority and permission to  ("/api/**")

       @Configuration
        @EnableWebSecurity
        class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter {
        ...
            @Override
            protected void configure(HttpSecurity http) throws Exception
            {
                super.configure(http);
                http    .authorizeRequests()
                        .csrf().disable()
                        .antMatcher("/api/**").permitAll()
                        .isAuthenticated()
                        .antMatchers(HttpMethod.GET, "/api/calendar/*").hasAnyAuthority("view-all", "calendar")
                        .antMatchers(HttpMethod.POST, "/api/calendar/*").hasAnyAuthority("manage-all", "calendar_manage")
                        .antMatchers(HttpMethod.PUT, "/api/calendar/*").hasAnyAuthority("manage-all", "calendar_manage")
                        .antMatchers(HttpMethod.DELETE, "/api/calendar/*").hasAnyAuthority("manage-all", "calendar_manage");
            }
        }


问:
JOIN instead of subquery for JSON array data?
I have database design (in PostgreSQL 9.6) where in one table company data is stored. Each company can have one or more contact persons whose details are layed out in another table.
The (simplified) schema being something like that:

```<code>DROP TABLE IF EXISTS test_company;
CREATE TABLE test_company (id integer, company_name text, contact_person integer[]);

DROP TABLE IF EXISTS test_contact_person;
CREATE TABLE test_contact_person (id integer, person_name text);
</code>```

Now consider data like this:

```<code>INSERT INTO test_company (id, company_name, contact_person) VALUES (1, 'Foo Ldt.', '{1,2}');
INSERT INTO test_company (id, company_name, contact_person) VALUES (2, 'Foo Sub Inc.', '{1,2}');
INSERT INTO test_company (id, company_name, contact_person) VALUES (3, 'Foo Sub Sub Inc.', '{1}');
INSERT INTO test_company (id, company_name, contact_person) VALUES (4, 'Bar Inc.', '{3,4}');
INSERT INTO test_company (id, company_name, contact_person) VALUES (5, 'Foo-Bar Joint-Venture', '{2,3,4}');

INSERT INTO test_contact_person(id, person_name) VALUES (1,'John');
INSERT INTO test_contact_person(id, person_name) VALUES (2,'Maria');
INSERT INTO test_contact_person(id, person_name) VALUES (3,'Bill');
INSERT INTO test_contact_person(id, person_name) VALUES (4,'Jane');
</code>```

You see, one person could be the contact for multiple companies, even the "pairs" (like '{1,2}' could be the same).

Now the requirements, that when querying companies are:

One row per company

The details of the contact persons should be returned all in one column as JSON array like [{"id":1,"person_name":"John"}]

Right now, I'm solving that problem with a subquery like so:

```<code>SELECT
id,
company_name,
 (
  SELECT json_agg(my_subquery) FROM
   (
     SELECT id, person_name FROM test_contact_person
     WHERE id = ANY(test_company.contact_person)
   )
  AS my_subquery
)
contact_person_expanded
FROM test_company;
</code>```

That gives me the expected result. However (as always) performance is not satisfying. BTW: There are currently no indexes on either table. I'm wondering now:

Would using JOINs make the query faster? If yes: How exactly would I make a JOIN return the JSON array here?

Would using indexes improve the performance? If yes: What kind of index on which column?

Update

Just for reference I'd like to point out that the solution suggested by Radim Bača seems to work in terms of gaining performance.

First I put in much more data with an ugly plv8-loop

```<code>DROP TABLE IF EXISTS test_company;
CREATE TABLE test_company (id integer, company_name text, contact_person integer[]);

DROP TABLE IF EXISTS test_contact_person;
CREATE TABLE test_contact_person (id integer, person_name text);

DO $$
 for(var i = 1; i < 20000; i++) {
   plv8.execute('INSERT INTO test_contact_person(id, person_name) VALUES ($1,$2)',[i,'SomePerson' + i]);
 }

for(var i = 1; i < 10000; i++) {
   plv8.execute('INSERT INTO test_company (id, company_name, contact_person) VALUES ($1,$2,$3)',[i,'SomeCompany' + i,[i,(20 -i)]]);
 }
$$ LANGUAGE plv8;
</code>```

I then tried my version of the query again:

```<code>SELECT
id,
company_name,
 (
  SELECT json_agg(my_subquery) FROM
   (
     SELECT id, person_name FROM test_contact_person
     WHERE id = ANY(test_company.contact_person)
   )
  AS my_subquery
)
contact_person_expanded
FROM test_company;
</code>```

That gives me (always measured on my local machine in pgAdmin 3) about 23 seconds execution time, compared to

```<code>SELECT
  comp.id,
  comp.company_name,
  json_agg(json_build_object('id', pers.id, 'person_name', pers.person_name)) AS contact_person_expanded
FROM test_company comp
JOIN test_contact_person pers ON comp.contact_person @> ARRAY[pers.id]
GROUP BY comp.id, comp.company_name
</code>```

Which takes about 47 seconds - without index.

Finally, I added an index:

```<code>DROP INDEX IF EXISTS idx_testcompany_contactperson;
CREATE INDEX idx_testcompany_contactperson on test_company USING GIN ("contact_person");
</code>```

The execution time of the version with subquery doesn't change, however when using the JOIN the effect is dramatic: 1.1 seconds!

BTW: I once heard that in subquery test_company.contact_person @> ARRAY[id] is faster than id = ANY(test_company.contact_person). As far as I tested, that is not true. In my case, the latter version returned all rows in 23 seconds, while the first took 46 seconds.

答:
I would use common relational approach for M:N cardinality

```<code>CREATE TABLE company (cid integer primary key, company_name text);
CREATE TABLE contact_person (pid integer primary key, person_name text);
CREATE TABLE contact(
    cid integer references company,
    pid integer references contact_person,
    primary key(cid, pid)
);
</code>```

For the first person, you simply add the following values

```<code>INSERT INTO contact VALUES (1, 1);
INSERT INTO contact VALUES (1, 2);
-- and so on
</code>```

If you then need companies together with their contacts you simply use the following JOIN and JSON aggregation

```<code>SELECT c.cid,
   c.company_name,
   json_agg(json_build_object('id', cp.pid, 'person_name', cp.person_name))
FROM company c
JOIN contact ct ON c.cid = ct.cid
JOIN contact_person cp ON cp.pid = ct.pid
GROUP BY c.cid, c.company_name
</code>```

demo

Indexes are automatically created with primary keys, therefore, the performance should be ok. The question is: you really want all companies and all their contacts? No filter at all?

EDIT based on your comments I would at least rewrite your query using the JOIN instead of a correlated subquery. It may help the optimizer to find a better plan.

```<code>SELECT
  comp.id,
  comp.company_name,
  json_agg(json_build_object('id', pers.id, 'person_name', pers.person_name))
FROM test_company comp
JOIN test_contact_person pers ON comp.contact_person @> ARRAY[pers.id]
GROUP BY comp.id, comp.company_name
</code>```

This notation should allow Postgresql to use an GIN index like this

```<code>CREATE INDEX idx_testcompany_contactperson on test_company USING GIN ("contact_person");
</code>```

Body:
I have database design (in PostgreSQL 9.6) where in one table company data is stored. Each company can have one or more contact persons whose details are layed out in another table.
The (simplified) schema being something like that:

DROP TABLE IF EXISTS test_company;
CREATE TABLE test_company (id integer, company_name text, contact_person integer[]);

DROP TABLE IF EXISTS test_contact_person;
CREATE TABLE test_contact_person (id integer, person_name text);

Now consider data like this:

INSERT INTO test_company (id, company_name, contact_person) VALUES (1, 'Foo Ldt.', '{1,2}');
INSERT INTO test_company (id, company_name, contact_person) VALUES (2, 'Foo Sub Inc.', '{1,2}');
INSERT INTO test_company (id, company_name, contact_person) VALUES (3, 'Foo Sub Sub Inc.', '{1}');
INSERT INTO test_company (id, company_name, contact_person) VALUES (4, 'Bar Inc.', '{3,4}');
INSERT INTO test_company (id, company_name, contact_person) VALUES (5, 'Foo-Bar Joint-Venture', '{2,3,4}');

INSERT INTO test_contact_person(id, person_name) VALUES (1,'John');
INSERT INTO test_contact_person(id, person_name) VALUES (2,'Maria');
INSERT INTO test_contact_person(id, person_name) VALUES (3,'Bill');
INSERT INTO test_contact_person(id, person_name) VALUES (4,'Jane');

You see, one person could be the contact for multiple companies, even the "pairs" (like '{1,2}' could be the same).

Now the requirements, that when querying companies are:

One row per company

The details of the contact persons should be returned all in one column as JSON array like [{"id":1,"person_name":"John"}]

Right now, I'm solving that problem with a subquery like so:

SELECT
id,
company_name,
 (
  SELECT json_agg(my_subquery) FROM
   (
     SELECT id, person_name FROM test_contact_person
     WHERE id = ANY(test_company.contact_person)
   )
  AS my_subquery
)
contact_person_expanded
FROM test_company;

That gives me the expected result. However (as always) performance is not satisfying. BTW: There are currently no indexes on either table. I'm wondering now:

Would using JOINs make the query faster? If yes: How exactly would I make a JOIN return the JSON array here?

Would using indexes improve the performance? If yes: What kind of index on which column?

Update

Just for reference I'd like to point out that the solution suggested by Radim Bača seems to work in terms of gaining performance.

First I put in much more data with an ugly plv8-loop

DROP TABLE IF EXISTS test_company;
CREATE TABLE test_company (id integer, company_name text, contact_person integer[]);

DROP TABLE IF EXISTS test_contact_person;
CREATE TABLE test_contact_person (id integer, person_name text);

DO $$
 for(var i = 1; i < 20000; i++) {
   plv8.execute('INSERT INTO test_contact_person(id, person_name) VALUES ($1,$2)',[i,'SomePerson' + i]);
 }

for(var i = 1; i < 10000; i++) {
   plv8.execute('INSERT INTO test_company (id, company_name, contact_person) VALUES ($1,$2,$3)',[i,'SomeCompany' + i,[i,(20 -i)]]);
 }
$$ LANGUAGE plv8;

I then tried my version of the query again:

SELECT
id,
company_name,
 (
  SELECT json_agg(my_subquery) FROM
   (
     SELECT id, person_name FROM test_contact_person
     WHERE id = ANY(test_company.contact_person)
   )
  AS my_subquery
)
contact_person_expanded
FROM test_company;

That gives me (always measured on my local machine in pgAdmin 3) about 23 seconds execution time, compared to

SELECT
  comp.id,
  comp.company_name,
  json_agg(json_build_object('id', pers.id, 'person_name', pers.person_name)) AS contact_person_expanded
FROM test_company comp
JOIN test_contact_person pers ON comp.contact_person @> ARRAY[pers.id]
GROUP BY comp.id, comp.company_name

Which takes about 47 seconds - without index.

Finally, I added an index:

DROP INDEX IF EXISTS idx_testcompany_contactperson;
CREATE INDEX idx_testcompany_contactperson on test_company USING GIN ("contact_person");

The execution time of the version with subquery doesn't change, however when using the JOIN the effect is dramatic: 1.1 seconds!

BTW: I once heard that in subquery test_company.contact_person @> ARRAY[id] is faster than id = ANY(test_company.contact_person). As far as I tested, that is not true. In my case, the latter version returned all rows in 23 seconds, while the first took 46 seconds.

Body:
I would use common relational approach for M:N cardinality

CREATE TABLE company (cid integer primary key, company_name text);
CREATE TABLE contact_person (pid integer primary key, person_name text);
CREATE TABLE contact(
    cid integer references company,
    pid integer references contact_person,
    primary key(cid, pid)
);

For the first person, you simply add the following values

INSERT INTO contact VALUES (1, 1);
INSERT INTO contact VALUES (1, 2);
-- and so on

If you then need companies together with their contacts you simply use the following JOIN and JSON aggregation

SELECT c.cid,
   c.company_name,
   json_agg(json_build_object('id', cp.pid, 'person_name', cp.person_name))
FROM company c
JOIN contact ct ON c.cid = ct.cid
JOIN contact_person cp ON cp.pid = ct.pid
GROUP BY c.cid, c.company_name

demo

Indexes are automatically created with primary keys, therefore, the performance should be ok. The question is: you really want all companies and all their contacts? No filter at all?

EDIT based on your comments I would at least rewrite your query using the JOIN instead of a correlated subquery. It may help the optimizer to find a better plan.

SELECT
  comp.id,
  comp.company_name,
  json_agg(json_build_object('id', pers.id, 'person_name', pers.person_name))
FROM test_company comp
JOIN test_contact_person pers ON comp.contact_person @> ARRAY[pers.id]
GROUP BY comp.id, comp.company_name

This notation should allow Postgresql to use an GIN index like this

CREATE INDEX idx_testcompany_contactperson on test_company USING GIN ("contact_person");


问:
How to test if BufferedImage using ImageIO picks up a 'null'
I am trying to set my program (java using netbeans) to do one thing if it finds the image its looking for, and another thing if it doesn't...

this is what I have got so far however it never completes the else statement. I believe this is because "image" is not technically null, as it still has corresponds to the filename entered, however I am not sure how to set java to do something based on if the filename is not found within the directory.

```<code>public void displayImage(String strfilename, JLabel JLlabel) {

    try {
        JLabel label = JLlabel;

        String FileName = strfilename;
        BufferedImage image = ImageIO.read(new File(FileName + ".jpg"));
        if(image!=null){
        ImageIcon icon = new ImageIcon(image);

        label.setIcon(icon);}
        else{
        BufferedImage image2 = ImageIO.read(new File("NOIMAGE.jpg"));
        ImageIcon icon2 = new ImageIcon(image2);
        label.setIcon(icon2);
        }
    } catch (IOException ioe) {
    }
}
</code>```

If anyone could help me with this I would be very grateful

答:
You're swallowing the exception. You don't need an if{}else{} loop since you already have a try{}catch{}.

```<code>String FileName = file;
try {
    BufferedImage image = ImageIO.read(new File(FileName + ".jpg"));
    // Code for when the image is found
} catch (IOException ex) {
    // Code for when the image is not found
}
</code>```

EDIT:
As @haraldK pointed out, you can have a file that exists but is unreadable, in which case a NullPointerException will be thrown.

You can handle them both in the catch clause.

```<code>public void displayImage(String strfilename, JLabel label) {
    try {
        BufferedImage image = ImageIO.read(new File(strfilename + ".jpg"));
        ImageIcon icon = new ImageIcon(image); // Can throw NullPointerException if the file is found but is unreadable
        label.setIcon(icon);

    } catch (IOException | NullPointerException ex) {
        ImageIcon icon = new ImageIcon("NOIMAGE.jpg");
        label.setIcon(icon);
    }
    // You're probably going to have to pack or validate your container here
}
</code>```

One thing worth noting is that this does not check exceptions for the NOIMAGE, you might want to add that.

This is better than just calling File.exists() as it also handles files that exist but are unreadable (text file and whatnot).

Body:
I am trying to set my program (java using netbeans) to do one thing if it finds the image its looking for, and another thing if it doesn't...

this is what I have got so far however it never completes the else statement. I believe this is because "image" is not technically null, as it still has corresponds to the filename entered, however I am not sure how to set java to do something based on if the filename is not found within the directory.

public void displayImage(String strfilename, JLabel JLlabel) {

    try {
        JLabel label = JLlabel;

        String FileName = strfilename;
        BufferedImage image = ImageIO.read(new File(FileName + ".jpg"));
        if(image!=null){
        ImageIcon icon = new ImageIcon(image);

        label.setIcon(icon);}
        else{
        BufferedImage image2 = ImageIO.read(new File("NOIMAGE.jpg"));
        ImageIcon icon2 = new ImageIcon(image2);
        label.setIcon(icon2);
        }
    } catch (IOException ioe) {
    }
}

If anyone could help me with this I would be very grateful

Body:
You're swallowing the exception. You don't need an if{}else{} loop since you already have a try{}catch{}.

String FileName = file;
try {
    BufferedImage image = ImageIO.read(new File(FileName + ".jpg"));
    // Code for when the image is found
} catch (IOException ex) {
    // Code for when the image is not found
}

EDIT:
As @haraldK pointed out, you can have a file that exists but is unreadable, in which case a NullPointerException will be thrown.

You can handle them both in the catch clause.

public void displayImage(String strfilename, JLabel label) {
    try {
        BufferedImage image = ImageIO.read(new File(strfilename + ".jpg"));
        ImageIcon icon = new ImageIcon(image); // Can throw NullPointerException if the file is found but is unreadable
        label.setIcon(icon);

    } catch (IOException | NullPointerException ex) {
        ImageIcon icon = new ImageIcon("NOIMAGE.jpg");
        label.setIcon(icon);
    }
    // You're probably going to have to pack or validate your container here
}

One thing worth noting is that this does not check exceptions for the NOIMAGE, you might want to add that.

This is better than just calling File.exists() as it also handles files that exist but are unreadable (text file and whatnot).


Title: Group array of objects by string property value in JavaScript?

Body:
I hate this array of objects, each object has a date, I want to  be able to group these objects into months. Is there a way to convert this,

var data  = [
  { date: "2016-08-13",...},
  { date: "2016-07-23",...},
  { date: "2016-08-11",...},
  { date: "2016-08-10",...},
  { date: "2016-07-20",...},
  { date: "2016-07-21",...},
]

into something like this

var data  = [
  [{ date: "2016-08-13",...},
  { date: "2016-08-11",...},
  { date: "2016-08-10",...}],
  [{ date: "2016-07-20",...},
  { date: "2016-07-21",...},
  { date: "2016-07-23",...}[
]

Body:
var data  = [
{ date: "2016-08-13"},
{ date: "2016-07-23"},
{ date: "2016-08-11"},
{ date: "2016-08-10"},
{ date: "2016-07-20"},
{ date: "2016-07-21"}
];

var monthDateIndex = {
 "01": "Jan",
 "02" : "Feb",
 "03" : "mar",
 "04" : "april",
 "05" : "may",
 "06" : "june",
 "07" : "july",
 "08" : "august",
 "09" : "sept",
 "10" : "oct",
 "11" : "nov",
 "12" : "dec"
}

let newDataObj = [];
Object.keys(monthDateIndex).map(monthNum => {
        let monthObj = [];
   data.map(dateObj => {
       if(monthNum == dateObj.date.split("-")[1]) {
        monthObj.push(dateObj);
     }
   });
   if(monthObj.length != 0) newDataObj.push(monthObj);
});


问:
Bootstrap 4 Grid issue with Angular 4
I'm facing weird issue with the grid, I tried the same code here which is Bootstrap 3, I used same in Bootstrap 4, but I'm getting below grid structure.

答: The Bootply code you have shared is using Bootstrap 3 version and in your case you are using Bootstrap 4 There is difference between these two. Please check Documentation for bootstrap 4 here</p>

Title: Bootstrap 4 Grid issue with Angular 4


问:
How to use Excel formula combining replace and match
I have these sets of values in Excel and I want them to be replaced, please see the attached screenshot:

答:
you can use vlookup

follow this tutorial


问:
Is there a way to make inline-block elements behave like table cells?
I am trying to mimic an HTML table with css classes, but the last "cell" element always gets pushed to the next line.

The way I am currently getting around this is to decrease the width of the last element by a few percent, so that the total width of the elements adds up to ~ 97%. This protects the last element from being pushed to the next line, but it doesn't look as nice because there's unused space at the end.

Is there a way to get the table cells to line up nicely and use 100% of the containing width?

.cell {
  display: inline-block;
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><div class="table" style="width: 100%">
  <div class="row" style="width: 100%">
    <div class="cell" style="width:40%">Last Name</div>
    <div class="cell" style="width:40%">First Name</div>
    <div class="cell" style="width:20%">Age</div>
  </div>
</div></code>```

答:
I hvae 2 solutions.

The first solution is that

.row{
  font-size: 0;
}

.cell {
  display: inline-block;
  font-size: 14px;
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><div class="table" style="width: 100%">
  <div class="row" style="width: 100%">
    <div class="cell" style="width:40%">Last Name</div>
    <div class="cell" style="width:40%">First Name</div>
    <div class="cell" style="width:20%">Age</div>
  </div>
</div></code>```

Just set font-size:0 at parent div.

But I don't like this solution.

The second solution is that

.row{
  display: flex;
  flex-direction: row;
  justify-content:flex-start;
}
.cell {
  /* display: inline-block; It's not require */
  font-size: 14px;
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><div class="table" style="width: 100%">
    <div class="row" style="width: 100%">
        <div class="cell" style="width:40%">Last Name</div>
        <div class="cell" style="width:40%">First Name</div>
        <div class="cell" style="width:20%">Age</div>
    </div>
</div></code>```

I recommend you display:flex.

If you want to learn about flex, you can find it in Google easily.

Body:
I am trying to mimic an HTML table with css classes, but the last "cell" element always gets pushed to the next line.

The way I am currently getting around this is to decrease the width of the last element by a few percent, so that the total width of the elements adds up to ~ 97%. This protects the last element from being pushed to the next line, but it doesn't look as nice because there's unused space at the end.

Is there a way to get the table cells to line up nicely and use 100% of the containing width?

.cell {
  display: inline-block;
}

Last Name

First Name

Age

Body:
I hvae 2 solutions.

The first solution is that

.row{
  font-size: 0;
}

.cell {
  display: inline-block;
  font-size: 14px;
}

<div class="table" style="width: 100%">
  <div class="row" style="width: 100%">
    <div class="cell" style="width:40%">Last Name</div>
    <div class="cell" style="width:40%">First Name</div>
    <div class="cell" style="width:20%">Age</div>
  </div>
</div>

Just set font-size:0 at parent div.

But I don't like this solution.

The second solution is that

.row{
  display: flex;
  flex-direction: row;
  justify-content:flex-start;
}
.cell {
  /* display: inline-block; It's not require */
  font-size: 14px;
}

<div class="table" style="width: 100%">
    <div class="row" style="width: 100%">
        <div class="cell" style="width:40%">Last Name</div>
        <div class="cell" style="width:40%">First Name</div>
        <div class="cell" style="width:20%">Age</div>
    </div>
</div>

I recommend you display:flex.

If you want to learn about flex, you can find it in Google easily.


问:
Compare three different array and arrange them in same order
I tried to arrange three different arrays in the same order, all three arrays as same size but values are not in the same order. How can I arrange them into the same order?

First array:

```<code>Array(
    [0] => Array( [id] => 1 )
    [1] => Array( [id] => 12 )
    [2] => Array( [id] => 25 )
    [3] => Array( [id] => 54 )
)
</code>```

Second array:

```<code>Array(
    [0] => Array( [id] => 24 )
    [1] => Array( [id] => 12 )
    [2] => Array( [id] => 54 )
    [3] => Array( [id] => 1 )
)
</code>```

Third array:

```<code>Array(
    [0] => Array( [id] => 54 )
    [1] => Array( [id] => 25 )
    [2] => Array( [id] => 1 )
    [3] => Array( [id] => 12 )
)
</code>```

Expected final array result:

```<code>Array(
    [0] => Array( [id] => 1 )
    [1] => Array( [id] => 12 )
    [2] => Array( [id] => 25 )
    [3] => Array( [id] => 54 )
)
</code>```

All three arrays are arranged like the first array. I tried a lot but it's not working.

答:
As you want the result to be in the order of the first array, try multisort the arrays with the first array
array_multisort($arr2,$arrone);
array_multisort($arr3,$arrone);</p>

Body:
I tried to arrange three different arrays in the same order, all three arrays as same size but values are not in the same order. How can I arrange them into the same order?

First array:

Array(
    [0] => Array( [id] => 1 )
    [1] => Array( [id] => 12 )
    [2] => Array( [id] => 25 )
    [3] => Array( [id] => 54 )
)

Second array:

Array(
    [0] => Array( [id] => 24 )
    [1] => Array( [id] => 12 )
    [2] => Array( [id] => 54 )
    [3] => Array( [id] => 1 )
)

Third array:

Array(
    [0] => Array( [id] => 54 )
    [1] => Array( [id] => 25 )
    [2] => Array( [id] => 1 )
    [3] => Array( [id] => 12 )
)

Expected final array result:

Array(
    [0] => Array( [id] => 1 )
    [1] => Array( [id] => 12 )
    [2] => Array( [id] => 25 )
    [3] => Array( [id] => 54 )
)

All three arrays are arranged like the first array. I tried a lot but it's not working.


问:
Does not work loop while in bash
I have logs in redmine, about users, that have connected from ip of my server. I can do this via command:

tail -n 100000 /usr/share/redmine/log/production.log | grep -A 3 "192.168.110.50" | grep "Current user" | awk '{print $3}' | head -n 1 | tail -n 1

I need to write this parameter in variable.

No problem:

temp=$(tail -n 100000 /usr/share/redmine/log/production.log | grep -A 3 "192.168.110.50" | grep "Current user" | awk '{print $3}' | head -n 1 | tail -n 1)

It works. But it can return user name anonymous. To return other user name i should write head -n 2. If it still anonymous, i can change in my formula to head -n 3.

So ... of course i can do this work

```<code>#!/bin/bash
temp=$(tail -n 100000 /usr/share/redmine/log/production.log | grep -A 3 "192.168.110.50" | grep "Current user" | awk '{print $3}' | head -n 1 | tail -n 1)

if [[ $temp == "anonymous" ]]; then
temp=$(tail -n 100000 /usr/share/redmine/log/production.log | grep -A 3 "192.168.110.50" | grep "Current user" | awk '{print $3}' | head -n 2 | tail -n 1)
fi
</code>```

But it will work for one iteration. I tried to:

```<code>while [ $tmp != "anonymous" ]; do
temp=$(tail -n 100000 /usr/share/redmine/log/production.log | grep -A 3 "192.168.110.50" | grep "Current user" | awk '{print $3}' | head -n ((var=var+1)) | tail -n 1)
done
</code>```

But of course it does not work. I can't understand just logically, how can i do this ? Can you help me ? Thanks for your attention.

答:
The immediate problem is that you're setting the variable temp, but checking tmp. You also need double-quotes around the variable in case it's blank, a wildcard, or something else troublesome.

But there's an easier way, because awk can handle most of the work by itself:

```<code>tail -n 100000 /usr/share/redmine/log/production.log |
    grep -A 3 "192.168.110.50" |
    awk '/Current user/ {if ($3 != "anonymous") {print $3; exit}}'
</code>```

It'd actually be possible to have awk handle looking at the lines after "192.168.110.50" too, but that'd be a little more complicated. BTW, since I don't have any redmine logs (let alone yours) to test with, this has not been properly tested.

Title: Does not work loop while in bash

Body:
I have logs in redmine, about users, that have connected from ip of my server. I can do this via command:

tail -n 100000 /usr/share/redmine/log/production.log | grep -A 3 "192.168.110.50" | grep "Current user" | awk '{print $3}' | head -n 1 | tail -n 1

I need to write this parameter in variable.

No problem:

temp=$(tail -n 100000 /usr/share/redmine/log/production.log | grep -A 3 "192.168.110.50" | grep "Current user" | awk '{print $3}' | head -n 1 | tail -n 1)

It works. But it can return user name anonymous. To return other user name i should write head -n 2. If it still anonymous, i can change in my formula to head -n 3.

So ... of course i can do this work

#!/bin/bash
temp=$(tail -n 100000 /usr/share/redmine/log/production.log | grep -A 3 "192.168.110.50" | grep "Current user" | awk '{print $3}' | head -n 1 | tail -n 1)

if [[ $temp == "anonymous" ]]; then
temp=$(tail -n 100000 /usr/share/redmine/log/production.log | grep -A 3 "192.168.110.50" | grep "Current user" | awk '{print $3}' | head -n 2 | tail -n 1)
fi

But it will work for one iteration. I tried to:

while [ $tmp != "anonymous" ]; do
temp=$(tail -n 100000 /usr/share/redmine/log/production.log | grep -A 3 "192.168.110.50" | grep "Current user" | awk '{print $3}' | head -n ((var=var+1)) | tail -n 1)
done

But of course it does not work. I can't understand just logically, how can i do this ? Can you help me ? Thanks for your attention.

Body:
The immediate problem is that you're setting the variable temp, but checking tmp. You also need double-quotes around the variable in case it's blank, a wildcard, or something else troublesome.

But there's an easier way, because awk can handle most of the work by itself:

tail -n 100000 /usr/share/redmine/log/production.log |
    grep -A 3 "192.168.110.50" |
    awk '/Current user/ {if ($3 != "anonymous") {print $3; exit}}'

It'd actually be possible to have awk handle looking at the lines after "192.168.110.50" too, but that'd be a little more complicated. BTW, since I don't have any redmine logs (let alone yours) to test with, this has not been properly tested.


问:
PHPexcel get the data from another sheet and display it on activeSheet
I am attempting to display a set of data from a excel sheet using PHPExcel which has a name of sheet1 but the reference data is located on sheet2 i got an output of #REF! which i only see on excel if the formula is wrong or the data is missing.

So a got a hint that this is because i am referencing only at sheet1 and the formula cannot get the data from sheet2 is there a away to get the data from the other sheet?

Or just to be simple just disregard the formula and display the output as a plain text.

```<code>require_once 'PHPExcel/IOFactory.php';

$inputFileType = 'Excel5';
$inputFileName = './data/myExcelData.xls';
$sheetname = 'Sheet1';

/**  Create a new Reader of the type defined in $inputFileType  **/
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
/**  Advise the Reader of which WorkSheets we want to load  **/
$objReader->setLoadSheetsOnly($sheetname);
/**  Load $inputFileName to a PHPExcel Object  **/
$objPHPExcel = $objReader->load($inputFileName);

$myDataArray = $objPHPExcel->getActiveSheet()->rangeToArray('A1:K31' ,NULL, FALSE, TRUE, false);
</code>```

I set the calculateFormula as FALSE to see the formula my sheet2 name is ML

答:
You can't see data from a second sheet if you don't actually load that second sheet....

```<code>$objReader->setLoadSheetsOnly($sheetname);
</code>```

PHPExcel doesn't have a magical, telepathic link that allows it to see what's on sheet2 if you've told it to load only sheet1.... load sheet2 as well if you expect PHPExcel to calculate formula values that reference sheet2

Body:
I am attempting to display a set of data from a excel sheet using PHPExcel which has a name of sheet1 but the reference data is located on sheet2 i got an output of #REF! which i only see on excel if the formula is wrong or the data is missing.

So a got a hint that this is because i am referencing only at sheet1 and the formula cannot get the data from sheet2 is there a away to get the data from the other sheet?

Or just to be simple just disregard the formula and display the output as a plain text.

require_once 'PHPExcel/IOFactory.php';

$inputFileType = 'Excel5';
$inputFileName = './data/myExcelData.xls';
$sheetname = 'Sheet1';

/**  Create a new Reader of the type defined in $inputFileType  **/
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
/**  Advise the Reader of which WorkSheets we want to load  **/
$objReader->setLoadSheetsOnly($sheetname);
/**  Load $inputFileName to a PHPExcel Object  **/
$objPHPExcel = $objReader->load($inputFileName);

$myDataArray = $objPHPExcel->getActiveSheet()->rangeToArray('A1:K31' ,NULL, FALSE, TRUE, false);

I set the calculateFormula as FALSE to see the formula my sheet2 name is ML

Body:
You can't see data from a second sheet if you don't actually load that second sheet....

$objReader->setLoadSheetsOnly($sheetname);

PHPExcel doesn't have a magical, telepathic link that allows it to see what's on sheet2 if you've told it to load only sheet1.... load sheet2 as well if you expect PHPExcel to calculate formula values that reference sheet2


问:
Spring 5 Reporting
On the changelog for Spring Framework 5, it states that they removed the compatibility for JasperReports. What reporting tool can I use, that will be compatible with Spring FW 5? </p>

答:
https://jira.spring.io/browse/SPR-13294
From the ticket

we rather recommend native use of the JasperReports API in Spring MVC
  handler methods, generating reports from specifically designed RESTful
  endpoints. We are dropping our now semi-useless JasperReports view
  class hierarchy as of Spring Framework 5.0. Note that our existing
  support against the deprecated JRExporter API remains around in the
  Spring Framework 4.3.x line until 2019, in particular for existing
  applications. However, even with 4.3, native use of the JasperReports
  API is worth considering.

You should use JasperReport but avoid using JRExporter. So just manually add all the JasperReport libraries and create reports when you need them. The Jasper Report generation code could be called by your own endpoints.

Title: Spring 5 Reporting


问:
Only once data.sql file has to be execute
I Don't want to execute data.sql file every time I want data.sql file to be execute once and after that when ever I run my application file it has to by pass this file. because when ever I am running the application it executes the data.sql file and for that duplicate entries are in the table.

答:
Why don't you use some database migration tool such as Flyway or Liquidbase, then each time when start the app, it will know if the changes have already been applied.

Eg:
data.sql will be renamed to something like initial_data_V100.

Then this will be stored under a schema table (Generated and used by Flyway/Liquidbase) like:
Id, Schema_version, script_name, checksum
1, V100, initial_data_V100,234234

Each time you start the app, it will see if you have already applied that schema changes.

Next time you want to add more changes, all you have to do is create a new file with and increment the version eg: V101.sql. Flyway or Liquidbase will pick it up and apply the schema changes


问:
TCP port 80 not shown in listening list after adding Inbound and outbound policies on Azure cloud Machine
I logged in into cloud machine and added the inbound and outbound rules for TCP port 80 and 443.

After that when i check from the command prompt netstat -an command. The TCP port 80 and 443 was not listed.

Can any one help me in resolving this issue.

Regards
Mohd Shoeb

答:
After that when i check from the command prompt netstat -an command.
  The TCP port 80 and 443 was not listed.

According to your description, we should run some application, like IIS or apache to listen on port 80 or 443, after the application running correct, we can use netstat -an to list them.


问:
Missing images in FlowDocument saved as XPS document
I am having some difficulties getting images contained in a FlowDocument to show when the FlowDocument is saved as an XPS document.

Here is what I do:

Create an image using the Image control of WPF. I set the image source bracketed by calls to BeginInit/EndInit.

Add the image to the FlowDocument wrapping it in a BlockUIContainer.

Save the FlowDocument object to an XPS file using a modified version of this code.

If I then view the saved file in the XPS viewer, the image is not shown. The problem is that the images are not loaded until actually shown on the screen by WPF so they are not saved to the XPS file.  Hence, there is a workaround: If I first show the document on screen using the FlowDocumentPageViewer and then save the XPS file afterwards, the image is loaded and shows up in the XPS file. This works even if the FlowDocumentPageViewer is hidden. But that gives me another challenge.  Here is what I wish to do (in pseudocode):

```<code>void SaveDocument()
{
    AddFlowDocumentToFlowDocumentPageViewer();
    SaveFlowDocumentToXpsFile();
}
</code>```

This of course does not work since the FlowDocumentPageViewer never gets a chance to show its contents before the document is saved to the XPS file. I tried wrapping SaveFlowDocumentToXpsFile in a call to Dispatcher.BeginInvoke but it did not help.

My questions are:

Can I somehow force the images to load before saving the XPS file without actually showing the document on screen? (I tried fiddling with BitmapImage.CreateOptions with no luck).

If there is no solution to question #1, is there a way to tell when FlowDocumentPageViewer has finished loading its contents so that I know when it is save to create the XPS file?

答:
Couple things...
You sure the image is sized before its written?  Usually you have to call Measure on the control so that it may size itself accordingly (infinity lets the control expand to its Width and Height)

```<code>image.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
</code>```

Also, sometimes you have to bump the UI thread so that everything gets updated in the control

```<code>Dispatcher.Invoke(DispatcherPriority.Render, new Action(() =>{}));
</code>```

ContentLicense: CC BY-SA 2.5

Body:
I am having some difficulties getting images contained in a FlowDocument to show when the FlowDocument is saved as an XPS document.

Here is what I do:

Create an image using the Image control of WPF. I set the image source bracketed by calls to BeginInit/EndInit.

Add the image to the FlowDocument wrapping it in a BlockUIContainer.

Save the FlowDocument object to an XPS file using a modified version of this code.

If I then view the saved file in the XPS viewer, the image is not shown. The problem is that the images are not loaded until actually shown on the screen by WPF so they are not saved to the XPS file.  Hence, there is a workaround: If I first show the document on screen using the FlowDocumentPageViewer and then save the XPS file afterwards, the image is loaded and shows up in the XPS file. This works even if the FlowDocumentPageViewer is hidden. But that gives me another challenge.  Here is what I wish to do (in pseudocode):

void SaveDocument()
{
    AddFlowDocumentToFlowDocumentPageViewer();
    SaveFlowDocumentToXpsFile();
}

This of course does not work since the FlowDocumentPageViewer never gets a chance to show its contents before the document is saved to the XPS file. I tried wrapping SaveFlowDocumentToXpsFile in a call to Dispatcher.BeginInvoke but it did not help.

My questions are:

Can I somehow force the images to load before saving the XPS file without actually showing the document on screen? (I tried fiddling with BitmapImage.CreateOptions with no luck).

If there is no solution to question #1, is there a way to tell when FlowDocumentPageViewer has finished loading its contents so that I know when it is save to create the XPS file?

Body:
Couple things...
You sure the image is sized before its written?  Usually you have to call Measure on the control so that it may size itself accordingly (infinity lets the control expand to its Width and Height)

image.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));

Also, sometimes you have to bump the UI thread so that everything gets updated in the control

Dispatcher.Invoke(DispatcherPriority.Render, new Action(() =>{}));


问:
Kafka - consuming from spark
I followed this document, and It works well. Now I tried to consuming connector data from spark. Is there any reference I can use? Since I use confluent, It's much different from original kafka reference document.

It's some code that I've used so far. The problem is cannot convert record data to java.String. (and not sure that it's right way to consume)

val brokers = "http://127.0.0.1:9092"
val topics = List("postgres-accounts2")
val sparkConf = new SparkConf().setAppName("KafkaWordCount")
//sparkConf.setMaster("spark://sda1:7077,sda2:7077")
sparkConf.setMaster("local[2]")
sparkConf.set("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
sparkConf.registerKryoClasses(Array(classOf[org.apache.avro.generic.GenericData$Record]))

val ssc = new StreamingContext(sparkConf, Seconds(2))
ssc.checkpoint("checkpoint")

 // Create direct kafka stream with brokers and topics
//val topicsSet = topics.split(",")

val kafkaParams = Map[String, Object](
  "schema.registry.url" -> "http://127.0.0.1:8081",
  "bootstrap.servers" -> "http://127.0.0.1:9092",
  "key.deserializer" -> "io.confluent.kafka.serializers.KafkaAvroDeserializer",
   "value.deserializer" -> "io.confluent.kafka.serializers.KafkaAvroDeserializer",
  "group.id" -> "use_a_separate_group_id_for_each_stream",
  "auto.offset.reset" -> "earliest",
  "enable.auto.commit" -> (false: java.lang.Boolean)
)

val messages = KafkaUtils.createDirectStream[String, String](
  ssc,
  PreferConsistent,
  Subscribe[String, String](topics, kafkaParams)
)

val data = messages.map(record => {
    println( record)
    println( "value : " + record.value().toString() ) // error  java.lang.ClassCastException: org.apache.avro.generic.GenericData$Record cannot be cast to java.lang.String
    //println( Json.parse( record.value() + ""))

    (record.key, record.value)
})
```

答:
Do sync my value deserializer to below. It will provide proper function and type.</p>

```<code>KafkaUtils.createDirectStream[String, record]
</code>```

ContentLicense: CC BY-SA 4.0

Title: Kafka - consuming from spark

Body:
Do sync my value deserializer to below. It will provide proper function and type.

KafkaUtils.createDirectStream[String, record]


问:
Angular Routing:Call different function on same module(or component) from different router links
I have a module named Items and i navigate to its items.Component.ts form many router links.To track from which page we came an id can be passed along with router path.

```<code>this.router.navigate(['/items',id passed along with);
</code>```

But i want to call different functions as per the page i came from before ngOnit().
Ex. I want to call function A in itemscomponent if i navigated from Menu
or B if i navigated from Category

Also required navigation can be done from both module(or component) html file or component.ts(view model)
How can i achieve this in angular?

答:
you can use queryParams and send 1 from menu page and 2 from category, and after navigate you can execute functions related to you query params

```<code>   this.router.navigate(['/items'],id, { queryParams: { functionToexecute: 1  } });
</code>```

and in your itemsComponent:

```<code> constructor(
   private route: ActivatedRoute,
   private router: Router) {}

  ngOnInit() {
   this.route
    .queryParams
    .subscribe(params => {
     if (params['functionToexecute'] === 1){ call functionA} else{call
      function B}
   });
 }
</code>```

Body:
I have a module named Items and i navigate to its items.Component.ts form many router links.To track from which page we came an id can be passed along with router path.

this.router.navigate(['/items',id passed along with);

But i want to call different functions as per the page i came from before ngOnit().
Ex. I want to call function A in itemscomponent if i navigated from Menu
or B if i navigated from Category

Also required navigation can be done from both module(or component) html file or component.ts(view model)
How can i achieve this in angular?

Body:
you can use queryParams and send 1 from menu page and 2 from category, and after navigate you can execute functions related to you query params

   this.router.navigate(['/items'],id, { queryParams: { functionToexecute: 1  } });

and in your itemsComponent:

 constructor(
   private route: ActivatedRoute,
   private router: Router) {}

  ngOnInit() {
   this.route
    .queryParams
    .subscribe(params => {
     if (params['functionToexecute'] === 1){ call functionA} else{call
      function B}
   });
 }


问:
PHP Contact Form POST_Method
I am trying to build a really simple Contact form for my personal website

```<code><?php
if(isset($_POST["submit"])){
  echo $_POST["name"];
  echo $_POST["email"];
}
?>

<html>
<body>
   <form action="" method="post">
    <p>
      <label for="name"> Name: </label><br>
      <input type="text" name="name" id="name" />
    </p>

     <p>
      <label for="email"> Email: </label><br>
      <input type="text" name="email" id="email" />
     </p>

  <p>
      <label for="message"> Message: </label><br>
       <textarea name="message" id="message"></textarea>
   </p>

   <p>
      <input type="submit" value="Submit" />
    </p>
 </form>

 </body>
 </html>
</code>```

However, nothing appears when I try to echo the post value after pressing submit. I tried using get instead of post and I can see the data from the input field appearing in my URL.

What went wrong?

答:
You should add a name attribute in your submit button. Because PHP uses the name attribute as the variable identifier in $_POST array.

So that it would look like: <input type="submit" value="Submit" name="submit" />

Finally your code would look like:

```<code><?php
if(isset($_POST["submit"])){
  echo $_POST["name"];
  echo $_POST["email"];
}
?>

<html>
<body>
   <form action="" method="post">
    <p>
      <label for="name"> Name: </label><br>
      <input type="text" name="name" id="name" />
    </p>

     <p>
      <label for="email"> Email: </label><br>
      <input type="text" name="email" id="email" />
     </p>

  <p>
      <label for="message"> Message: </label><br>
       <textarea name="message" id="message"></textarea>
   </p>

   <p>
      <input type="submit" value="Submit" name="submit" />
    </p>
 </form>

 </body>
 </html>
</code>```

Title: PHP Contact Form POST_Method

Body:
I am trying to build a really simple Contact form for my personal website

<?php
if(isset($_POST["submit"])){
  echo $_POST["name"];
  echo $_POST["email"];
}
?>

   <form action="" method="post">

       Name:

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

       Email:

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

       Message:

       <textarea name="message" id="message"></textarea>

      <input type="submit" value="Submit" />

 </form>

However, nothing appears when I try to echo the post value after pressing submit. I tried using get instead of post and I can see the data from the input field appearing in my URL.

What went wrong?

Body:
You should add a name attribute in your submit button. Because PHP uses the name attribute as the variable identifier in $_POST array.

So that it would look like: <input type="submit" value="Submit" name="submit" />

Finally your code would look like:

<?php
if(isset($_POST["submit"])){
  echo $_POST["name"];
  echo $_POST["email"];
}
?>

<html>
<body>
   <form action="" method="post">
    <p>
      <label for="name"> Name: </label><br>
      <input type="text" name="name" id="name" />
    </p>

     <p>
      <label for="email"> Email: </label><br>
      <input type="text" name="email" id="email" />
     </p>

  <p>
      <label for="message"> Message: </label><br>
       <textarea name="message" id="message"></textarea>
   </p>

   <p>
      <input type="submit" value="Submit" name="submit" />
    </p>
 </form>

 </body>
 </html>


问:
How to find issue Android SDK 3.0 Error:(9, 5) error: resource android:attr/colorError not found
How to find issue to this error when I did a make I have got this error message :
Error:(9, 5) error: resource android:attr/colorError not found

Thing strange I've 2 build.gradle files :
Here my build.gradle (Project:Projectname) File :

```<code>buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        maven {
            url "https://maven.google.com"
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
</code>```

Here my build.gradle (Module:app) File :

```<code>apply plugin: 'com.android.application'
android {
    compileSdkVersion 22
    defaultConfig {
        applicationId "org.acme.nfcedit"
        minSdkVersion 22
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
</code>```

This file appears /home/users/.gradle/caches/transforms-1/files-1.1/appcompat-v7-26.1.0.aar/c41e5bc4d98504dc222d4eca88ab6d1b/res/values-v26/values-v26.xml content

```<code><?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="Base.Theme.AppCompat" parent="Base.V26.Theme.AppCompat"/>
    <style name="Base.Theme.AppCompat.Light" parent="Base.V26.Theme.AppCompat.Light"/>
    <style name="Base.V26.Theme.AppCompat" parent="Base.V26.Theme.AppCompat">
        <!-- We can use the platform styles on API 26+ -->
        <item name="colorError">?android:attr/colorError</item>
    </style>
    <style name="Base.V26.Theme.AppCompat.Light" parent="Base.V23.Theme.AppCompat.Light">
        <!-- We can use the platform styles on API 26+ -->
        <item name="colorError">?android:attr/colorError</item>
    </style>
    <style name="Base.V26.Widget.AppCompat.Toolbar" parent="Base.V7.Widget.AppCompat.Toolbar">
        <item name="android:touchscreenBlocksFocus">true</item>
        <item name="android:keyboardNavigationCluster">true</item>
    </style>
    <style name="Base.Widget.AppCompat.Toolbar" parent="Base.V26.Widget.AppCompat.Toolbar"/>
</resources>
</code>```

I don't know what's mmeaning ?android:attr/colorError

Thanks

答: Change compileSdkVersion 22 to compileSdkVersion 26.

Body:
How to find issue to this error when I did a make I have got this error message :
Error:(9, 5) error: resource android:attr/colorError not found

Thing strange I've 2 build.gradle files :
Here my build.gradle (Project:Projectname) File :

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        maven {
            url "https://maven.google.com"
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Here my build.gradle (Module:app) File :

apply plugin: 'com.android.application'
android {
    compileSdkVersion 22
    defaultConfig {
        applicationId "org.acme.nfcedit"
        minSdkVersion 22
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

This file appears /home/users/.gradle/caches/transforms-1/files-1.1/appcompat-v7-26.1.0.aar/c41e5bc4d98504dc222d4eca88ab6d1b/res/values-v26/values-v26.xml content

<?xml version="1.0" encoding="utf-8"?>
<resources>


问:
Try to validate a xml-file by more than one xsd-file
I try to validate a xml-file aganist two xsd-files, but I get every time an error on validation. I use the "xml copy editor" to validate. I do not know, if this is a problem of my xml/xsd or the program (which maybe can not validate aganist multiple xsd!?). Can anybody help?

I try to follow the example on page.

XML-file:

```<code><?xml version="1.0" encoding="UTF-8"?>
<PostAdress xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="D:\XML\XSD1.xsd">
    <Meta>
        <Name>Hallo</Name>
    </Meta>
</PostAdress>
</code>```

XSD1.xsd:

```<code><?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" xmlns:xs2="http://aa.bb.cc">
    <xs:import namespace="http://aa.bb.cc" schemaLocation="D:\XML\XSD0.xsd" />
    <xs:element name="PostAdress">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Meta" type="xs2:Adress" minOccurs="0" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
</code>```

XSD0.xsd:

```<code><?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://aa.bb.cc">
  <xs:element name="Adress">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Name" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
</code>```

PS: I get the error: "Error at line 7: column 96: type 'http://aa.bb.cc:Adress' not found." I think the program means Line 7 in XSD1.xsd.

PS2: I try also to run a small VB.net-App for validation. I get nearly the same error message. So, I think there is a problem with the files.

答:
I think you should use a file uri when importing the other xsd scheme.
Try to replace the row

```<code><xs:import namespace="http://aa.bb.cc" schemaLocation="D:\XML\XSD0.xsd" />
</code>```

with this one

```<code><xs:import namespace="http://aa.bb.cc" schemaLocation="file:D:/XML/XSD0.xsd" />
</code>```

in your XSD1.xsd scheme

Body:
I try to validate a xml-file aganist two xsd-files, but I get every time an error on validation. I use the "xml copy editor" to validate. I do not know, if this is a problem of my xml/xsd or the program (which maybe can not validate aganist multiple xsd!?). Can anybody help?

I try to follow the example on page.

XML-file:

<?xml version="1.0" encoding="UTF-8"?>
<PostAdress xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="D:\XML\XSD1.xsd">
    <Meta>
        <Name>Hallo</Name>
    </Meta>
</PostAdress>

XSD1.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" xmlns:xs2="http://aa.bb.cc">
    <xs:import namespace="http://aa.bb.cc" schemaLocation="D:\XML\XSD0.xsd" />
    <xs:element name="PostAdress">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Meta" type="xs2:Adress" minOccurs="0" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

XSD0.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://aa.bb.cc">
  <xs:element name="Adress">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Name" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

PS: I get the error: "Error at line 7: column 96: type 'http://aa.bb.cc:Adress' not found." I think the program means Line 7 in XSD1.xsd.

PS2: I try also to run a small VB.net-App for validation. I get nearly the same error message. So, I think there is a problem with the files.

Body:
I think you should use a file uri when importing the other xsd scheme.
Try to replace the row

<xs:import namespace="http://aa.bb.cc" schemaLocation="D:\XML\XSD0.xsd" />

with this one

<xs:import namespace="http://aa.bb.cc" schemaLocation="file:D:/XML/XSD0.xsd" />

in your XSD1.xsd scheme


问:
How can I synchronize two processes accessing a file on a NAS?
Here's the thing: I have two applications, written in C++ and running on two machines with different OS (one Linux and one Windows). One of this process is in charge of updating an XML file on a NAS (Network Attached Storage) while the other one reads this file.

Is it possible to synchronize these two processes in order to avoid reading of the file at the same time it's being modified?

答:
OK you need some form of locking mechanism to control accesses.

Most *nix File systems provide this. I suspect it is also available on Windows File System (as this mechanism is used by perl) but it may have another name.

Take a look at the flock().

This is a file locking mechanism. It is an advisory lock so it does not actually lock the file and prevent usage but it provides a mechanism for marking the file. If both applications use the mechanism then you can control accesses to the file.

flock() provides both shared locks (or READ Lock) and exclusive locks (or WRITE Lock). flock will block your thread (in a non busy way) until the file has been unlocked by the user (it also provides NON blocking checks so you can do other things while waiting).

Check out flock in section 2 of the man pages.

```<code>int     flock(int fd, int operation);

Flock() applies or removes an advisory lock on the file associated with the file
descriptor fd.  A lock is applied by specifying an operation parameter that is
one of LOCK_SH or LOCK_EX with the optional addition of LOCK_NB.  To unlock an
existing lock operation should be LOCK_UN.
</code>```

OwnerDisplayName: Luc Touraille

Body:
OK you need some form of locking mechanism to control accesses.

Most *nix File systems provide this. I suspect it is also available on Windows File System (as this mechanism is used by perl) but it may have another name.

Take a look at the flock().

This is a file locking mechanism. It is an advisory lock so it does not actually lock the file and prevent usage but it provides a mechanism for marking the file. If both applications use the mechanism then you can control accesses to the file.

flock() provides both shared locks (or READ Lock) and exclusive locks (or WRITE Lock). flock will block your thread (in a non busy way) until the file has been unlocked by the user (it also provides NON blocking checks so you can do other things while waiting).

Check out flock in section 2 of the man pages.

int     flock(int fd, int operation);

Flock() applies or removes an advisory lock on the file associated with the file
descriptor fd.  A lock is applied by specifying an operation parameter that is
one of LOCK_SH or LOCK_EX with the optional addition of LOCK_NB.  To unlock an
existing lock operation should be LOCK_UN.

OwnerDisplayName: Martin York


问:
django media url tag
Does django have media tag similar to static and url and how to setup it?

```<code>{% static 'styles/boo.css' %}
{% url 'some_app:some_name' %}

Is this possible: {% media 'what here' %}?
</code>```

How to setup it?

答:
For media files I use django-imagekit

Basic use:

```<code>from django.db import models
from imagekit.models import ProcessedImageField
from imagekit.processors import ResizeToFill
</code>```

models.py

```<code>class Photo(models.Model):
    owner = models.ForeignKey(Project, on_delete=models.CASCADE)
    photos = ProcessedImageField(upload_to='pagename/images',
                                       processors=[ResizeToFill(900, 600)],
                                       format='JPEG',
                                       options={'quality': 90})
</code>```

settings.py

```<code>INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'imagekit',
]
</code>```

.html

```<code>{% load imagekit %}

{% thumbnail '100x50' source_file %}
</code>```

Read documentation - https://pypi.python.org/pypi/django-imagekit

Title: django media url tag

Body:
Does django have media tag similar to static and url and how to setup it?

{% static 'styles/boo.css' %}
{% url 'some_app:some_name' %}

Is this possible: {% media 'what here' %}?

How to setup it?

Body:
For media files I use django-imagekit

Basic use:

from django.db import models
from imagekit.models import ProcessedImageField
from imagekit.processors import ResizeToFill

models.py

class Photo(models.Model):
    owner = models.ForeignKey(Project, on_delete=models.CASCADE)
    photos = ProcessedImageField(upload_to='pagename/images',
                                       processors=[ResizeToFill(900, 600)],
                                       format='JPEG',
                                       options={'quality': 90})

settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'imagekit',
]

.html

{% load imagekit %}

{% thumbnail '100x50' source_file %}

Read documentation - https://pypi.python.org/pypi/django-imagekit


问:
How to stick div to bottom page no scrolling
I have the following html struct:

```<code><body>
<div class="content">
   ...here long content
</div>
<div class="footer">
    ...here some rows for footer
</div>
</body>
</code>```

i want to strick the footer part to bottom page and the content page need to be in all the other place in page with scroll only in his part. !important! not scrolling in all page - only in content part.

something like this:

Is it possible?

Thanks!

答:
Make your content div into a scroll-able div so you can keep the footer at the bottom anytime

```<code><div class="content" style="overflow:scroll; height:400px;">
</code>```

If you want only vertical scrolling try this

```<code><div class="content" style="overflow-y: scroll; height:400px;">
</code>```

Body:
I have the following html struct:

   ...here long content

    ...here some rows for footer

i want to strick the footer part to bottom page and the content page need to be in all the other place in page with scroll only in his part. !important! not scrolling in all page - only in content part.

something like this:

Is it possible?

Thanks!

Body:
Make your content div into a scroll-able div so you can keep the footer at the bottom anytime

<div class="content" style="overflow:scroll; height:400px;">

If you want only vertical scrolling try this

<div class="content" style="overflow-y: scroll; height:400px;">


问:
(Windows batch) Goto within if block behaves very strangely
If I take the following Windows batch code snippet and run it:

```<code>echo foo
if 1 == 1 (
    echo bar
    goto asdf
    :asdf
    echo baz
) else (
    echo quux
)</code>```

The output I would expect is:

```<code>foo
bar
baz</code>```

But instead I get:

```<code>foo
bar
baz
quux</code>```

If I comment out the goto asdf line, it gives the output I expect. The echo quux line should never be exectuted, so why is the existence of the goto causing that to happen?

UPDATE: For what it's worth, here's a workaround that correctly does what I originally intended:

```<code>goto BEGIN

:doit
    echo bar
    goto asdf
    :asdf
    echo baz
    goto :EOF

:BEGIN

echo foo
if 1 == 1 (
    call :doit
) else (
    echo quux
)</code>```

However, this doesn't answer my original question.

答:
In this case it turns out to be related to the poor nesting and structure of a loop inside an IF statement, as clearly explained by https://stackoverflow.com/users/1012053/dbenham above.

That said, I figured out another similar consideration due to this problem...
Please take a look a the following problem and subsequent solution here:
". was unexpected at this time" generated from batch script line 'if exist [file] (...

The solution was simply the treatment of '(' and ')' on ECHO lines inside an IF statement block.

The point is, do consider treatment of special characters as a possible source of a problem when troubleshooting IF (and possibly FOR) statements.

HTH

Body:
If I take the following Windows batch code snippet and run it:

echo foo
if 1 == 1 (
    echo bar
    goto asdf
    :asdf
    echo baz
) else (
    echo quux
)

The output I would expect is:

foo
bar
baz

But instead I get:

foo
bar
baz
quux

If I comment out the goto asdf line, it gives the output I expect. The echo quux line should never be exectuted, so why is the existence of the goto causing that to happen?

UPDATE: For what it's worth, here's a workaround that correctly does what I originally intended:

goto BEGIN

:doit
    echo bar
    goto asdf
    :asdf
    echo baz
    goto :EOF

:BEGIN

echo foo
if 1 == 1 (
    call :doit
) else (
    echo quux
)

However, this doesn't answer my original question.


问:
How to display Current date in HTML?
How to display Current date (DD mon YYYY) in HTML, this has to be implemented in Php code   </p>

```<code><div class="container-fluid" style="padding: 0rem 1rem;">
  <h3>Attendance : {current_date} </h3>
  <hr>
  <p id="Page_MsgBox" style="padding: 0rem 1rem;"></p>
  <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 col-xl-12">
    {$attendance_table}
  </div>
</div>
</code>```

答:
A bit more strait and simple solution:

```<code><h3>Attendance : <?=date('d F Y')?></h3>
</code>```

F - Full month name

M - Short (3 symbols) month name

m - month number

d - day of month

Y - full year

Title: How to display Current date in HTML?

Body:
How to display Current date (DD mon YYYY) in HTML, this has to be implemented in Php code

Attendance : {current_date}

    {$attendance_table}

Body:
A bit more strait and simple solution:

<h3>Attendance : <?=date('d F Y')?></h3>

F - Full month name

M - Short (3 symbols) month name

m - month number

d - day of month

Y - full year


问:
Reset scroll position on component re-render
Have a look at this pen: https://codepen.io/segmentationfaulter/pen/XzgJyV

I am also pasting the code here for convenience. Upon opening the pen above, you will see that a slider is rendered. Scroll the slider right, and the click on change slides button, you will see that slider is re-rendered with different numbers, but the scroll position of the slider is not reset. Why is the scroll position not reset? Thanks

function SliderElement({
  elementContent
}) {
  return (
 {
      elementContent
    }

  )
}

class Slider extends React.Component {
    render() {
        return ( <
          div className = 'slider' > {
            this.props.sliderContentArray.map(elementContent => < SliderElement elementContent = {
                elementContent
              }
              />)} <
              /div>
            )
          }
        }

        class SliderContainer extends React.Component {
          constructor() {
            super()
            this.slidersContent = [
              [1, 2, 3, 4, 5, 6, 7, 8],
              [9, 10, 11, 12, 13, 14, 15, 16]
            ]

            this.state = {
              currentSliderIndex: 0
            }
          }
          switchSlider() {
            this.setState((prevState) => {
              if (prevState.currentSliderIndex === 0) {
                return {
                  currentSliderIndex: 1
                }
              } else {
                return {
                  currentSliderIndex: 0
                }
              }
            })
          }
          render() {
            return ( <
              div >
              <
              Slider sliderContentArray = {
                this.slidersContent[this.state.currentSliderIndex]
              }
              /> <
              button className = 'slider-switch-button'
              onClick = {
                this.switchSlider.bind(this)
              } >
              change slides <
              /button> <
              /div>
            )
          }
        }

        ReactDOM.render( <
          SliderContainer / > ,
          document.getElementById('root')
        );```
<pre class="snippet-code-css lang-css prettyprint-override"><code>.slider {
  display: flex;
  overflow: scroll;
}

.slider-element {
  flex-shrink: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 20%;
  height: 100px;
  background: yellow;
}

.slider-element+.slider-element {
  margin-left: 10px;
}

.slider-switch-button {
  margin-top: 10px;
}</code>```

```

答:
Your Slider Content rerenders but the scrollbar is not aware of it. You need to manually change the scrollLeft position to 0 for the div which has overflow: scroll property which in your case is the div with slider className.

You can attach a ref to it and set scrollLeft = 0 like

```<code>class Slider extends React.Component {
  render () {
    return (
      <div ref={this.props.innerRef} className='slider'>
        {this.props.sliderContentArray.map(elementContent => <SliderElement elementContent={elementContent} />)}
      </div>
    )
  }
}

class SliderContainer extends React.Component {
  constructor () {
    super()
    this.slidersContent = [
      [1, 2, 3, 4, 5, 6, 7, 8],
      [9, 10, 11, 12, 13, 14, 15, 16]
    ]

    this.state = {
      currentSliderIndex: 0
    }
  }
  switchSlider () {
    this.setState((prevState) => {
      if (prevState.currentSliderIndex === 0) {
        return { currentSliderIndex: 1 }
      } else {
        return { currentSliderIndex: 0 }
      }
    }, () => {this.slider.scrollLeft = 0})
  }
  render () {
    return (
      <div>
        <Slider innerRef={(ref) => this.slider = ref} sliderContentArray={this.slidersContent[this.state.currentSliderIndex]} />
        <button
          className='slider-switch-button'
          onClick={this.switchSlider.bind(this)}
        >
          change slides
        </button>
      </div>
    )
  }
}
</code>```

CodePen

Body:
Have a look at this pen: https://codepen.io/segmentationfaulter/pen/XzgJyV

I am also pasting the code here for convenience. Upon opening the pen above, you will see that a slider is rendered. Scroll the slider right, and the click on change slides button, you will see that slider is re-rendered with different numbers, but the scroll position of the slider is not reset. Why is the scroll position not reset? Thanks

function SliderElement({
  elementContent
}) {
  return (
 {
      elementContent
    }

  )
}

class Slider extends React.Component {
    render() {
        return ( <
          div className = 'slider' > {
            this.props.sliderContentArray.map(elementContent => < SliderElement elementContent = {
                elementContent
              }
              />)} <
              /div>
            )
          }
        }

        class SliderContainer extends React.Component {
          constructor() {
            super()
            this.slidersContent = [
              [1, 2, 3, 4, 5, 6, 7, 8],
              [9, 10, 11, 12, 13, 14, 15, 16]
            ]

            this.state = {
              currentSliderIndex: 0
            }
          }
          switchSlider() {
            this.setState((prevState) => {
              if (prevState.currentSliderIndex === 0) {
                return {
                  currentSliderIndex: 1
                }
              } else {
                return {
                  currentSliderIndex: 0
                }
              }
            })
          }
          render() {
            return ( <
              div >
              <
              Slider sliderContentArray = {
                this.slidersContent[this.state.currentSliderIndex]
              }
              /> <
              button className = 'slider-switch-button'
              onClick = {
                this.switchSlider.bind(this)
              } >
              change slides <
              /button> <
              /div>
            )
          }
        }

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

.slider {
  display: flex;
  overflow: scroll;
}

.slider-element {
  flex-shrink: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 20%;
  height: 100px;
  background: yellow;
}

.slider-element+.slider-element {
  margin-left: 10px;
}

.slider-switch-button {
  margin-top: 10px;
}

Body:
Your Slider Content rerenders but the scrollbar is not aware of it. You need to manually change the scrollLeft position to 0 for the div which has overflow: scroll property which in your case is the div with slider className.

You can attach a ref to it and set scrollLeft = 0 like

class Slider extends React.Component {
  render () {
    return (
      <div ref={this.props.innerRef} className='slider'>
        {this.props.sliderContentArray.map(elementContent => <SliderElement elementContent={elementContent} />)}
      </div>
    )
  }
}

class SliderContainer extends React.Component {
  constructor () {
    super()
    this.slidersContent = [
      [1, 2, 3, 4, 5, 6, 7, 8],
      [9, 10, 11, 12, 13, 14, 15, 16]
    ]

    this.state = {
      currentSliderIndex: 0
    }
  }
  switchSlider () {
    this.setState((prevState) => {
      if (prevState.currentSliderIndex === 0) {
        return { currentSliderIndex: 1 }
      } else {
        return { currentSliderIndex: 0 }
      }
    }, () => {this.slider.scrollLeft = 0})
  }
  render () {
    return (
      <div>
        <Slider innerRef={(ref) => this.slider = ref} sliderContentArray={this.slidersContent[this.state.currentSliderIndex]} />
        <button
          className='slider-switch-button'
          onClick={this.switchSlider.bind(this)}
        >
          change slides
        </button>
      </div>
    )
  }
}

CodePen


问:
error_cb in confluent-kafka-python producers and consumers
I'm wondering what is the proper reaction to evens that lead to the error_cb callback being called.

Initially our code was always throwing an Exception from the error_cb regardless of anything. We're running our stuff in Kubernetes, so restarting a consumer/producer is (technically) not a big deal. But the number of restart were quite significant, so we added a couple of exceptions, which we just log without quitting:

KafkaError._MSG_TIMED_OUT (both consumer and producer)

KafkaError._TRANSPORT (consumer)

These are the ones that we see a lot, and confluent-kafka-python seems to be able to recover from them without any extra help.

Now I'm wondering if we were right to throw any exceptions in error_cb to begin with. Should we start treating error_cb just as a logging function, and only react to exceptions thrown explicitly by poll and flush?

答:
librdkafka will do its best to automatically recover from any errors it hits, so the error_cb is mostly informational and it is generally not advisable for the application to do anything drastic upon such an error.

_MSG_TIMED_OUT and _TIMED_OUT- Kafka protocol requests timed out, typically due to network or broker issues. The requests will be retried according to the retry configuration, or the corresponding API / functionality willl propagate a more detailed error (e.g., failure to commit offsets). This error can safely be ignored.

_TRANSPORT - the broker connection went down or could not be established, again this is a temporary network or broker problem and may too be safely ignored.


问:
To shuffle two list and merge into one list
I want to merge two list into one
  eg:

```<code>    A  B
    3  7
    1  8
    9  2
    31 4
</code>```

Desired answer:

```<code>    A
    3
    8
    9
    4
</code>```

It would be helpful if you anyone can tell me Code of this query?

Body:
I want to merge two list into one
  eg:

    A  B
    3  7
    1  8
    9  2
    31 4

Desired answer:

    A
    3
    8
    9
    4

It would be helpful if you anyone can tell me Code of this query?

Body: A[seq(1:(length(A)/2))*2] <- B[seq(1:(length(A)/2))*2]


问:
Regex to insert content just before mediawiki category links
I'm trying to use a regex to insert a template into a page, before all category or interwiki links, but after everything else.
So if you have a page that ends like this:

```<code>== See Also ==
* [[Link one]]
* [[more link]]
* [//external.link external link]

[[Category:Pages]]
[[de:Spezial Page]]
</code>```

I want the template {{template}} to be inserted before the [[Category:Pages]] but after everything else.

Note: The last section is not necessarily a list - it could be

```<code>== References ==
<references/>
</code>```

or even something else. The point is to insert it before all category/interwiki links at the end, but after the last section.

What regex can help me do this? I've tried (?P```[\s\S]+)(?P<cats>(?:\[\[[^]]:[^]]\]\])*$)</code> as the matching expression with <code>\g```{{template}}\n\g<cats> as the substituting expression, but that simply inserts it at the very end.

Regex flavor: Python 2

答:
Actually regexes are powerful enough for this specific task, although in general it is indeed a bad idea to use them for parsing wikitext. Something like

```<code>(\[\[(Category|\w{2,3}(-\w+){0,2}):[^\[\]<>]+\]\]\s*)*$
</code>```

would work.

Body:
I'm trying to use a regex to insert a template into a page, before all category or interwiki links, but after everything else.
So if you have a page that ends like this:

== See Also ==
* [[Link one]]
* [[more link]]
* [//external.link external link]

[[Category:Pages]]
[[de:Spezial Page]]

I want the template {{template}} to be inserted before the [[Category:Pages]] but after everything else.

Note: The last section is not necessarily a list - it could be

== References ==
<references/>

or even something else. The point is to insert it before all category/interwiki links at the end, but after the last section.

What regex can help me do this? I've tried (?P
[\s\S]+)(?P<cats>(?:\[\[[^]]:[^]]\]\])*$) as the matching expression with \g
{{template}}\n\g<cats> as the substituting expression, but that simply inserts it at the very end.

Regex flavor: Python 2

Body:
Actually regexes are powerful enough for this specific task, although in general it is indeed a bad idea to use them for parsing wikitext. Something like

(\[\[(Category|\w{2,3}(-\w+){0,2}):[^\[\]<>]+\]\]\s*)*$

would work.


问:
Mapping of Blob type in C++Driver
According to "datastax.github.io/cpp-driver/topics/basics/",  the blob data type of Cassandra is matched to 'const cass_byte_t*'.  Suppose the database,  contains a structure saved in the form of blob; how can I fetch it.
Somebody had suggested me using cass_value_get_bytes().  Can anybody give example of how to use this function to fetch the structure? </p>

答:
What is the problem with cass_value_get_bytes()? You simply need to perform casting of data read to your structure, something like:

```<code>const cass_byte_t* outPtr = null;
size_t outSize = 0;
if (cass_value_get_bytes(casValue, &outPtr, &outSize) == CASS_OK) {
   if (outSize == sizeof(YourStruct)) {
       const YourStruct* yourStruct = reinterpret_cast<const YourStruct*>(outPtr);
   }
}
</code>```

Title: Mapping of Blob type in C++Driver

Body:
What is the problem with cass_value_get_bytes()? You simply need to perform casting of data read to your structure, something like:

const cass_byte_t* outPtr = null;
size_t outSize = 0;
if (cass_value_get_bytes(casValue, &outPtr, &outSize) == CASS_OK) {
   if (outSize == sizeof(YourStruct)) {
       const YourStruct* yourStruct = reinterpret_cast<const YourStruct*>(outPtr);
   }
}