问:
Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead
i am new to react , it would be great if someone can explain the problem , this question has been answered but i am still confused .

```<code>import React from 'react' ;
import Card from './Card.js';
const CardList = ({Friends}) =>{
    const Cardcomponent = Friends.map((user , i) => {
        return(
            <div>
                 <Card id={Friends[i].id} name={Friends[i].name} username={Friends[i].username} instagram={Friends[i].instagram}  />
            </div>
              )
    })
return(
          <div>
                {Cardcomponent}
          </div>
      )
}
export default CardList ;
</code>```

Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead.

答:
You are not thinking about react correctly.

The friends data comes from some external source. You are going to consume it in your components.

You have 2 main components here.

Card: Displays data about a single user

CardList: Displays Card components

Each of your components needs different data to work properly. When you give the array of friends to CardList you just want it to render one Card component for each friend element in the list.

React takes care of all of the DOM manipulations behind the scenes so it needs to be sure that every element on the screen can be uniquely identified. This is why we sometimes need to help it by telling it what key to use for a component when we know that there could be a lot of them.

The Card component only needs information about 1 friend, so that's all we give it from the CardList.

Notice that Card is not defined inside CardList and that CardList is not defined in App. This allows us to reuse these components in other places if we want to.

There's a lot more to be said about this, but hopefully this helps a little.

Here is a sample app:

```<code>import React from "react";
import ReactDOM from "react-dom";

const Card = ({ friend, id, name, username, instagram }) => (
  <div>
    <ul>
      <li>id: {id}</li>
      <li>name: {name}</li>
      <li>username: {username}</li>
      <li>instagram: {instagram}</li>
    </ul>
  </div>
);

const CardList = ({ friends }) => (
  <div>
    {friends.map(friend => <Card key={friend.id} {...friend} />)}
  </div>
);

function App() {
  const friends = [
    { id: 1, name: "John Smith", username: "jsmith", instagram: "N/A" },
    { id: 2, name: "Mary Jane", username: "mjane", instagram: "puppyLover" },
    {
      id: 3,
      name: "Harry Potter",
      username: "hpotter",
      instagram: "best_wizard"
    }
  ];
  return (
    <div className="App">
      <CardList friends={friends} />
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
</code>```

Body:
i am new to react , it would be great if someone can explain the problem , this question has been answered but i am still confused .

import React from 'react' ;
import Card from './Card.js';
const CardList = ({Friends}) =>{
    const Cardcomponent = Friends.map((user , i) => {
        return(

                 <Card id={Friends[i].id} name={Friends[i].name} username={Friends[i].username} instagram={Friends[i].instagram}  />

              )
    })
return(

                {Cardcomponent}

      )
}
export default CardList ;

Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead.

Body:
You are not thinking about react correctly.

The friends data comes from some external source. You are going to consume it in your components.

You have 2 main components here.

Card: Displays data about a single user

CardList: Displays Card components

Each of your components needs different data to work properly. When you give the array of friends to CardList you just want it to render one Card component for each friend element in the list.

React takes care of all of the DOM manipulations behind the scenes so it needs to be sure that every element on the screen can be uniquely identified. This is why we sometimes need to help it by telling it what key to use for a component when we know that there could be a lot of them.

The Card component only needs information about 1 friend, so that's all we give it from the CardList.

Notice that Card is not defined inside CardList and that CardList is not defined in App. This allows us to reuse these components in other places if we want to.

There's a lot more to be said about this, but hopefully this helps a little.

Here is a sample app:

import React from "react";
import ReactDOM from "react-dom";

const Card = ({ friend, id, name, username, instagram }) => (
  <div>
    <ul>
      <li>id: {id}</li>
      <li>name: {name}</li>
      <li>username: {username}</li>
      <li>instagram: {instagram}</li>
    </ul>
  </div>
);

const CardList = ({ friends }) => (
  <div>
    {friends.map(friend => <Card key={friend.id} {...friend} />)}
  </div>
);

function App() {
  const friends = [
    { id: 1, name: "John Smith", username: "jsmith", instagram: "N/A" },
    { id: 2, name: "Mary Jane", username: "mjane", instagram: "puppyLover" },
    {
      id: 3,
      name: "Harry Potter",
      username: "hpotter",
      instagram: "best_wizard"
    }
  ];
  return (
    <div className="App">
      <CardList friends={friends} />
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);


来源: stackexchange

问:
Ansible: docker related command doesn't work inside playbook, but does work once run directly
I have following task in my playbook:

```<code>- name: install pg_stat_statements extension in the postgres container
  shell: docker exec octopus_postgres_{{ group_id }} /bin/bash -c 'psql -h localhost -U postgres -p 5433 -c "CREATE EXTENSION pg_stat_statements;"' #  && service postgres restart')"
  async: 10
  poll: 0
</code>```

Once I run the playbook this task seems to successfully finished, but if I check the postgres database - there are no changes to it. The task didn't actually worked.

If I run the above mentioned command manually on the host via bash, everything works fine and the databased gets updated, like this:

```<code>docker exec octopus_postgres_iaa /bin/bash -c 'psql -h localhost -U postgres -p 5433 -c "CREATE EXTENSION pg_stat_statements;"'
</code>```

While trying to check what is wrong with the task, I tried the following:

```<code>- name: install pg_stat_statements extension in the postgres container
  shell: docker exec octopus_postgres_{{ group_id }} /bin/bash -c 'touch /1 && psql -h localhost -U postgres -p 5433 -c "CREATE EXTENSION pg_stat_statements;" && touch /2' #  && service postgres restart')"
  async: 10
  poll: 0
</code>```

I've noticed that file /1 indeed has been created inside of the container, but the file /2 hasn't...

What is wrong with the command?

答:
Using docker exec at all isn't right here.  In general you want to avoid it for tasks like this: if the container gets deleted and recreated, any local setup you've done with docker exec will be lost.  When you're trying to make a change to some sort of server using its API, you usually just call its API, instead of getting a root shell on the server host and then doing things, but this latter step is what docker exec does.

The standard postgres image supports putting SQL fragments in a container-side /docker-entrypoint-initdb.d directory, which will get processed the very first time the container is started (only).  A very typical use is to mount a host-system directory with initialization scripts on to that directory.  In Ansible this might look like:

```<code>- name: create pg_stat_statements extension file
  copy:
    dest: /docker/postgres/initdb/create-stat-statements.sql
    content: |-
      CREATE EXTENSION pg_stat_statements;

- name: start postgres container
  docker_container:
    image: 'postgres:11'
    name: octopus_postgres_{{ group_id }}
    published_ports: ['5433:5432']
    volumes:
      - '/docker/postgres/initdb:/docker-entrypoint-initdb.d'
      - '/docker/postgres/data:/var/lib/postgresql/data'
</code>```

Alternatively, you can manage the database like any other PostgreSQL database (local, cloud-hosted, remote, ...) using Ansible's built-in tools; in this case, the postgresql_ext module for creating extensions.

```<code>- name: enable pg_stat_statements PostgreSQL extension
  postgresql_ext:
    name: pg_stat_statements
    port: 5433
</code>```

In terms of your original statement, there are probably two things going on.  First of all, if you do use the docker exec path to interact with a container, you always need to use the port the server thinks it's running on and not any remapped ports from the docker run -p option or equivalents: in your statement you need to use the default port 5432 and not 5433.  Secondly, since you run the task with async: 10, poll: 0, Ansible launches the task and immediately moves on to the next one, without ever checking to see whether it succeeds (see Asynchronous Actions and Polling), so you don't actually know whether or not the docker exec task succeeded.  My guess is that nothing is happening because it's failing to connect to the database, but you never see this error.

ContentLicense: CC BY-SA 4.0

Body:
I have following task in my playbook:

- name: install pg_stat_statements extension in the postgres container
  shell: docker exec octopus_postgres_{{ group_id }} /bin/bash -c 'psql -h localhost -U postgres -p 5433 -c "CREATE EXTENSION pg_stat_statements;"' #  && service postgres restart')"
  async: 10
  poll: 0

Once I run the playbook this task seems to successfully finished, but if I check the postgres database - there are no changes to it. The task didn't actually worked.

If I run the above mentioned command manually on the host via bash, everything works fine and the databased gets updated, like this:

docker exec octopus_postgres_iaa /bin/bash -c 'psql -h localhost -U postgres -p 5433 -c "CREATE EXTENSION pg_stat_statements;"'

While trying to check what is wrong with the task, I tried the following:

- name: install pg_stat_statements extension in the postgres container
  shell: docker exec octopus_postgres_{{ group_id }} /bin/bash -c 'touch /1 && psql -h localhost -U postgres -p 5433 -c "CREATE EXTENSION pg_stat_statements;" && touch /2' #  && service postgres restart')"
  async: 10
  poll: 0

I've noticed that file /1 indeed has been created inside of the container, but the file /2 hasn't...

What is wrong with the command?

Body:
Using docker exec at all isn't right here.  In general you want to avoid it for tasks like this: if the container gets deleted and recreated, any local setup you've done with docker exec will be lost.  When you're trying to make a change to some sort of server using its API, you usually just call its API, instead of getting a root shell on the server host and then doing things, but this latter step is what docker exec does.

The standard postgres image supports putting SQL fragments in a container-side /docker-entrypoint-initdb.d directory, which will get processed the very first time the container is started (only).  A very typical use is to mount a host-system directory with initialization scripts on to that directory.  In Ansible this might look like:

- name: create pg_stat_statements extension file
  copy:
    dest: /docker/postgres/initdb/create-stat-statements.sql
    content: |-
      CREATE EXTENSION pg_stat_statements;

- name: start postgres container
  docker_container:
    image: 'postgres:11'
    name: octopus_postgres_{{ group_id }}
    published_ports: ['5433:5432']
    volumes:
      - '/docker/postgres/initdb:/docker-entrypoint-initdb.d'
      - '/docker/postgres/data:/var/lib/postgresql/data'

Alternatively, you can manage the database like any other PostgreSQL database (local, cloud-hosted, remote, ...) using Ansible's built-in tools; in this case, the postgresql_ext module for creating extensions.

- name: enable pg_stat_statements PostgreSQL extension
  postgresql_ext:
    name: pg_stat_statements
    port: 5433

In terms of your original statement, there are probably two things going on.  First of all, if you do use the docker exec path to interact with a container, you always need to use the port the server thinks it's running on and not any remapped ports from the docker run -p option or equivalents: in your statement you need to use the default port 5432 and not 5433.  Secondly, since you run the task with async: 10, poll: 0, Ansible launches the task and immediately moves on to the next one, without ever checking to see whether it succeeds (see Asynchronous Actions and Polling), so you don't actually know whether or not the docker exec task succeeded.  My guess is that nothing is happening because it's failing to connect to the database, but you never see this error.


问:
How can I map lists of simple datatypes without having to stringify them?
I'm making a API in C# .NET Core 2.2 and I'm trying to figure out how to properly map Lists of Enums and Strings.

Right now I have the following solution:

```<code>        [NotMapped]
        public virtual IList<string> OtherNames { get; set; } = new List<string>();
        public string OtherName
        {
            get
            {
                return JsonConvert.SerializeObject(OtherNames);
            }
            set
            {
                OtherNames = JsonConvert.DeserializeObject<List<string>>(value);
            }
        }
</code>```

and this works 100% like I want it to. BUT when I do an API call of a HttpGet for the object that has OtherNames as a property it returns OtherNames AND OtherName. Here a shortened visualization for context:
"otherNames":[],"otherName":"[]".

I can see the solution to this going two ways:

An easier mapping without needing a second property.

A way to exclude either one of the properties when calling the HttpGet. ==> For this solution I'll add the responsible code for the HttpGet down here:

This are all the properties that get returned.

```<code>        public int Id { get; set; }
        public string Image { get; private set; }
        public string FirstName { get; private set; }
        public string LastName { get;  private set; }
        public Countries PlaceBorn { get; private set; }
        public DateTime WhenBorn { get; private set; }
        public DateTime? WhenDied { get; private set;}
        public string Description { get; set; }
        public Book Favorite { get; private set; }

        public IList<CelebrityRead> Read { get; set; } = new List<CelebrityRead>();
        public IList<Relationship> Relationships { get; set; } = new List<Relationship>();
        [NotMapped]
        public virtual IList<Countries> Nationalities { get; set; } = new List<Countries>();

        public string Nationality
        {
            get
            {
                return JsonConvert.SerializeObject(Nationalities);
            }
            set
            {
                Nationalities = JsonConvert.DeserializeObject<List<Countries>>(value);
            }
        }
        [NotMapped]
        public virtual IList<string> OtherNames { get; set; } = new List<string>();
        public string OtherName
        {
            get
            {
                return JsonConvert.SerializeObject(OtherNames);
            }
            set
            {
                OtherNames = JsonConvert.DeserializeObject<List<string>>(value);
            }
        }
        [NotMapped]
        public virtual IList<Jobs> Jobs { get; set; } = new List<Jobs>();
        public string Job
        {
            get
            {
                return JsonConvert.SerializeObject(Jobs);
            }
            set
            {
                Jobs = JsonConvert.DeserializeObject<List<Jobs>>(value);
            }
        }
</code>```

Controller method, calls repository method

```<code>        [HttpGet("{id}")]
        public ActionResult<Celebrity> GetCelebrity(int id)
        {
            var celeb = _celebrityRepository.GetBy(id);
            if (celeb == null) return NotFound();
            return celeb;
        }
</code>```

Repository method, called by controller method

```<code>        public Celebrity GetBy(int id)
        {
            return _celebrities
              .Include(c => c.Favorite)
              .Include(c => c.Read)
              .Include(c => c.Relationships)
                .ThenInclude(r => r.With)
              .SingleOrDefault(c => c.Id == id);
        }
</code>```

Controller returns

```<code>{"id":-1,"image":".png","firstName":"Louis","lastName":"Sachar","placeBorn":249,"whenBorn":"1954-03-20T00:00:00","whenDied":null,"description":"...","favorite":null,"read":[],"relationships":[],"nationalities":[],"nationality":"[]","otherNames":[],"otherName":"[]","jobs":[],"job":"[]"}
</code>```

Current mapping of Lists of simple datatypes is auto mapped by fluent API since it can automap the string property OtherName.

OtherNames is not mapped.

答:
Option 1 ("An easier mapping without needing a second property") could be achieved with EF Core Value Conversions:

Value converters allow property values to be converted when reading from or writing to the database. This conversion can be from one value to another of the same type (for example, encrypting strings) or from a value of one type to a value of another type (for example, converting enum values to and from strings in the database.)

Basically keep just this in the entity class:

```<code>public IList<string> OtherNames { get; set; } = new List<string>();
</code>```

and associate value converter with it inside model configuration:

```<code>modelBuilder.Entity<Celebrity>()
    .Property(e => e.OtherNames)
    .HasConversion(
        value => JsonConvert.SerializeObject(value),
        dbValue => JsonConvert.DeserializeObject<List<string>>(dbValue));
</code>```

The only potential problem could be the usage of OtherNames property inside LINQ to Entities query (for instance, to perform filtering) as mentioned in the current Value Conversion Limitations:

Use of value conversions may impact the ability of EF Core to translate expressions to SQL. A warning will be logged for such cases. Removal of these limitations is being considered for a future release.

Body:
I'm making a API in C# .NET Core 2.2 and I'm trying to figure out how to properly map Lists of Enums and Strings.

Right now I have the following solution:

        [NotMapped]
        public virtual IList<string> OtherNames { get; set; } = new List<string>();
        public string OtherName
        {
            get
            {
                return JsonConvert.SerializeObject(OtherNames);
            }
            set
            {
                OtherNames = JsonConvert.DeserializeObject<List<string>>(value);
            }
        }

and this works 100% like I want it to. BUT when I do an API call of a HttpGet for the object that has OtherNames as a property it returns OtherNames AND OtherName. Here a shortened visualization for context:
"otherNames":[],"otherName":"[]".

I can see the solution to this going two ways:

An easier mapping without needing a second property.

A way to exclude either one of the properties when calling the HttpGet. ==> For this solution I'll add the responsible code for the HttpGet down here:

This are all the properties that get returned.

        public int Id { get; set; }
        public string Image { get; private set; }
        public string FirstName { get; private set; }
        public string LastName { get;  private set; }
        public Countries PlaceBorn { get; private set; }
        public DateTime WhenBorn { get; private set; }
        public DateTime? WhenDied { get; private set;}
        public string Description { get; set; }
        public Book Favorite { get; private set; }

        public IList<CelebrityRead> Read { get; set; } = new List<CelebrityRead>();
        public IList<Relationship> Relationships { get; set; } = new List<Relationship>();
        [NotMapped]
        public virtual IList<Countries> Nationalities { get; set; } = new List<Countries>();

        public string Nationality
        {
            get
            {
                return JsonConvert.SerializeObject(Nationalities);
            }
            set
            {
                Nationalities = JsonConvert.DeserializeObject<List<Countries>>(value);
            }
        }
        [NotMapped]
        public virtual IList<string> OtherNames { get; set; } = new List<string>();
        public string OtherName
        {
            get
            {
                return JsonConvert.SerializeObject(OtherNames);
            }
            set
            {
                OtherNames = JsonConvert.DeserializeObject<List<string>>(value);
            }
        }
        [NotMapped]
        public virtual IList<Jobs> Jobs { get; set; } = new List<Jobs>();
        public string Job
        {
            get
            {
                return JsonConvert.SerializeObject(Jobs);
            }
            set
            {
                Jobs = JsonConvert.DeserializeObject<List<Jobs>>(value);
            }
        }

Controller method, calls repository method

        [HttpGet("{id}")]
        public ActionResult<Celebrity> GetCelebrity(int id)
        {
            var celeb = _celebrityRepository.GetBy(id);
            if (celeb == null) return NotFound();
            return celeb;
        }

Repository method, called by controller method

        public Celebrity GetBy(int id)
        {
            return _celebrities
              .Include(c => c.Favorite)
              .Include(c => c.Read)
              .Include(c => c.Relationships)
                .ThenInclude(r => r.With)
              .SingleOrDefault(c => c.Id == id);
        }

Controller returns

{"id":-1,"image":".png","firstName":"Louis","lastName":"Sachar","placeBorn":249,"whenBorn":"1954-03-20T00:00:00","whenDied":null,"description":"...","favorite":null,"read":[],"relationships":[],"nationalities":[],"nationality":"[]","otherNames":[],"otherName":"[]","jobs":[],"job":"[]"}

Current mapping of Lists of simple datatypes is auto mapped by fluent API since it can automap the string property OtherName.

OtherNames is not mapped.

Body:
Option 1 ("An easier mapping without needing a second property") could be achieved with EF Core Value Conversions:

Value converters allow property values to be converted when reading from or writing to the database. This conversion can be from one value to another of the same type (for example, encrypting strings) or from a value of one type to a value of another type (for example, converting enum values to and from strings in the database.)

Basically keep just this in the entity class:

public IList<string> OtherNames { get; set; } = new List<string>();

and associate value converter with it inside model configuration:

modelBuilder.Entity<Celebrity>()
    .Property(e => e.OtherNames)
    .HasConversion(
        value => JsonConvert.SerializeObject(value),
        dbValue => JsonConvert.DeserializeObject<List<string>>(dbValue));

The only potential problem could be the usage of OtherNames property inside LINQ to Entities query (for instance, to perform filtering) as mentioned in the current Value Conversion Limitations:

Use of value conversions may impact the ability of EF Core to translate expressions to SQL. A warning will be logged for such cases. Removal of these limitations is being considered for a future release.


问:
custom spark does not find hive databases when running on yarn
Starting a custom version of spark on yarn in HDP works fine following the tutorial from https://georgheiler.com/2019/05/01/headless-spark-on-yarn/ i.e. following:

```<code># download a current headless version of spark
export SPARK_DIST_CLASSPATH=$(hadoop classpath)
export HADOOP_CONF_DIR=/usr/hdp/current/spark2-client/conf
export SPARK_HOME=<<path/to>>/spark-2.4.3-bin-without-hadoop/
<<path/to>>/spark-2.4.3-bin-without-hadoop/bin/spark-shell --master yarn --deploy-mode client --queue <<my_queue>> --conf spark.driver.extraJavaOptions='-Dhdp.version=2.6.<<version>>' --conf spark.yarn.am.extraJavaOptions='-Dhdp.version=2.6.<<version>>'

Welcome to
      ____              __
     / __/__  ___ _____/ /__
    _\ \/ _ \/ _ `/ __/  '_/
   /___/ .__/\_,_/_/ /_/\_\   version 2.4.3
      /_/
</code>```

However, a:

```<code>spark.sql("show databases").show
</code>```

only returns:

```<code>+------------+
|databaseName|
+------------+
|     default|
+------------+
</code>```

Now trying to pass the original HDP configuration (which is apparently not read in by my custom version of spark) like:

one:

```<code>--files /usr/hdp/current/spark2-client/conf/hive-site.xml
</code>```

two:

```<code>--conf spark.hive.metastore.uris='thrift://master001.my.corp.com:9083,thrift://master002.my.corp.com:9083,thrift://master003.my.corp.com:9083' --conf spark.hive.metastore.sasl.enabled='true' --conf hive.metastore.uris='thrift://master001.my.corp.com:9083,thrift://master002.my.corp.com:9083,thrift://master003.my.corp.com:9083' --conf hive.metastore.sasl.enabled='true'
</code>```

three:

```<code>--conf spark.yarn.dist.files='/usr/hdp/current/spark2-client/conf/hive-site.xml'
</code>```

four:

```<code>--conf spark.sql.warehouse.dir='/apps/hive/warehouse'
</code>```

all does not help to solve the issue.
How can I get spark to recognize the hive databases?

答:
Hive jars need to be in the classpath of Spark for hive support to be enabled.
if the hive jars are not present in classpath, the catalog implementation
 used is in-memory

In spark-shell we can confirm this by executing

```<code>sc.getConf.get("spark.sql.catalogImplementation")
</code>```

which will give in-memory

Why are hive classes required

```<code>    def enableHiveSupport(): Builder = synchronized {
      if (hiveClassesArePresent) {
        config(CATALOG_IMPLEMENTATION.key, "hive")
      } else {
        throw new IllegalArgumentException(
          "Unable to instantiate SparkSession with Hive support because " +
            "Hive classes are not found.")
      }
    }
</code>```

SparkSession.scala

```<code>  private[spark] def hiveClassesArePresent: Boolean = {
    try {
      Utils.classForName(HIVE_SESSION_STATE_BUILDER_CLASS_NAME)
      Utils.classForName("org.apache.hadoop.hive.conf.HiveConf")
      true
    } catch {
      case _: ClassNotFoundException | _: NoClassDefFoundError => false
    }
  }
</code>```

If the classes are not present, Hive support is not enabled.
Link to the code where the above checks happen as part of Spark shell initialization.

In the above code pasted as part of question, SPARK_DIST_CLASSPATH is populated only with the Hadoop classpath and the paths to Hive jars missing.

Body:
Starting a custom version of spark on yarn in HDP works fine following the tutorial from https://georgheiler.com/2019/05/01/headless-spark-on-yarn/ i.e. following:

# download a current headless version of spark
export SPARK_DIST_CLASSPATH=$(hadoop classpath)
export HADOOP_CONF_DIR=/usr/hdp/current/spark2-client/conf
export SPARK_HOME=<<path/to>>/spark-2.4.3-bin-without-hadoop/
<<path/to>>/spark-2.4.3-bin-without-hadoop/bin/spark-shell --master yarn --deploy-mode client --queue <<my_queue>> --conf spark.driver.extraJavaOptions='-Dhdp.version=2.6.<<version>>' --conf spark.yarn.am.extraJavaOptions='-Dhdp.version=2.6.<<version>>'

Welcome to
      ____              __
     / __/__  ___ _____/ /__
    _\ \/ _ \/ _ `/ __/  '_/
   /___/ .__/\_,_/_/ /_/\_\   version 2.4.3
      /_/

However, a:

spark.sql("show databases").show

only returns:

+------------+
|databaseName|
+------------+
|     default|
+------------+

Now trying to pass the original HDP configuration (which is apparently not read in by my custom version of spark) like:

one:

--files /usr/hdp/current/spark2-client/conf/hive-site.xml

two:

--conf spark.hive.metastore.uris='thrift://master001.my.corp.com:9083,thrift://master002.my.corp.com:9083,thrift://master003.my.corp.com:9083' --conf spark.hive.metastore.sasl.enabled='true' --conf hive.metastore.uris='thrift://master001.my.corp.com:9083,thrift://master002.my.corp.com:9083,thrift://master003.my.corp.com:9083' --conf hive.metastore.sasl.enabled='true'

three:

--conf spark.yarn.dist.files='/usr/hdp/current/spark2-client/conf/hive-site.xml'

four:

--conf spark.sql.warehouse.dir='/apps/hive/warehouse'

all does not help to solve the issue.
How can I get spark to recognize the hive databases?

Body:
Hive jars need to be in the classpath of Spark for hive support to be enabled.
if the hive jars are not present in classpath, the catalog implementation
 used is in-memory

In spark-shell we can confirm this by executing

sc.getConf.get("spark.sql.catalogImplementation")

which will give in-memory

Why are hive classes required

    def enableHiveSupport(): Builder = synchronized {
      if (hiveClassesArePresent) {
        config(CATALOG_IMPLEMENTATION.key, "hive")
      } else {
        throw new IllegalArgumentException(
          "Unable to instantiate SparkSession with Hive support because " +
            "Hive classes are not found.")
      }
    }

SparkSession.scala

  private[spark] def hiveClassesArePresent: Boolean = {
    try {
      Utils.classForName(HIVE_SESSION_STATE_BUILDER_CLASS_NAME)
      Utils.classForName("org.apache.hadoop.hive.conf.HiveConf")
      true
    } catch {
      case _: ClassNotFoundException | _: NoClassDefFoundError => false
    }
  }

If the classes are not present, Hive support is not enabled.
Link to the code where the above checks happen as part of Spark shell initialization.

In the above code pasted as part of question, SPARK_DIST_CLASSPATH is populated only with the Hadoop classpath and the paths to Hive jars missing.


问:
Why is this coodinate-returning function sometimes returning a coordinate outside its supposed restrictions?
As an exercise I'm building a clone of the classic Nibbles/Snake game.

The game area consists of a two-dimensional array (rows and columns), and the player snake consists of another two-dimensional array containing coordinates for each cell in the game area that the snake is currently occupying.

I then have a function to create the apples that the snake is supposed to eat. This function returns a simple array containing an x- and a y-coordinate specifying the game area cell to put the apple. This cell should of course never be one of the cells that the player snake is occupying at the moment when the apple is created.

However, the function I've constructed sometimes creates an apple in a cell that the player snake IS currently occupying.

Can anyone spot the bug?

This is the relevant code:

```<code>const rows = 20;
const cols = 26;

const cells = createCells(rows, cols);
let player = [[1,1], [1,2], [1,3], [1,4]];
let apple = createApple(cells, player);

function createCells(rows, cols) {
    let cells = new Array(rows);
    for (let r = 0; r < cells.length; r++) {
        cells[r] = new Array(cols);
    }
    return cells;
}

function createApple(cells, player) {
    let positions = new Array();
    for (let r = 0; r < cells.length; r++) {
        for (let c = 0; c < cells[r].length; c++) {
            for (let p = 0; p < player.length; p++) {
                if (!(player[p][0] === r && player[p][1] === c)) {
                    positions.push([r, c]);
                }
            }
        }
    }
    let random = Math.floor(Math.random() * positions.length - 1);
    let apple = positions[random];
    return apple;
}
</code>```

答:
The bug is in this part of the code:

```<code>        for (let p = 0; p < player.length; p++) {
            if (!(player[p][0] === r && player[p][1] === c)) {
                positions.push([r, c]);
            }
        }
</code>```

Even for the cells that are occupied by the snake, you are pushing them (player.length - 1) times. So, for the initial setup where player.length is 4, you are pushing every unoccupied cell 4 times, and every occupied cell 3 times. Do you see the bug now?

What you should change in the code? For a given cell (r,c), you must check it with ALL the cells of the player, and push it to positions only and only if there is no match with any of them. Take some time, and try to code it yourself without seeing further below.

```<code>        bool free=true;
        for (let p = 0; p < player.length; p++) {
            if (player[p][0] === r && player[p][1] === c) {
                free = false;
                break;
            }
        }
        if (free) {
            positions.push([r, c]);
        }
</code>```

Body:
As an exercise I'm building a clone of the classic Nibbles/Snake game.

The game area consists of a two-dimensional array (rows and columns), and the player snake consists of another two-dimensional array containing coordinates for each cell in the game area that the snake is currently occupying.

I then have a function to create the apples that the snake is supposed to eat. This function returns a simple array containing an x- and a y-coordinate specifying the game area cell to put the apple. This cell should of course never be one of the cells that the player snake is occupying at the moment when the apple is created.

However, the function I've constructed sometimes creates an apple in a cell that the player snake IS currently occupying.

Can anyone spot the bug?

This is the relevant code:

const rows = 20;
const cols = 26;

const cells = createCells(rows, cols);
let player = [[1,1], [1,2], [1,3], [1,4]];
let apple = createApple(cells, player);

function createCells(rows, cols) {
    let cells = new Array(rows);
    for (let r = 0; r < cells.length; r++) {
        cells[r] = new Array(cols);
    }
    return cells;
}

function createApple(cells, player) {
    let positions = new Array();
    for (let r = 0; r < cells.length; r++) {
        for (let c = 0; c < cells[r].length; c++) {
            for (let p = 0; p < player.length; p++) {
                if (!(player[p][0] === r && player[p][1] === c)) {
                    positions.push([r, c]);
                }
            }
        }
    }
    let random = Math.floor(Math.random() * positions.length - 1);
    let apple = positions[random];
    return apple;
}

Body:
The bug is in this part of the code:

        for (let p = 0; p < player.length; p++) {
            if (!(player[p][0] === r && player[p][1] === c)) {
                positions.push([r, c]);
            }
        }

Even for the cells that are occupied by the snake, you are pushing them (player.length - 1) times. So, for the initial setup where player.length is 4, you are pushing every unoccupied cell 4 times, and every occupied cell 3 times. Do you see the bug now?

What you should change in the code? For a given cell (r,c), you must check it with ALL the cells of the player, and push it to positions only and only if there is no match with any of them. Take some time, and try to code it yourself without seeing further below.

        bool free=true;
        for (let p = 0; p < player.length; p++) {
            if (player[p][0] === r && player[p][1] === c) {
                free = false;
                break;
            }
        }
        if (free) {
            positions.push([r, c]);
        }


问:
how to not display duplicated data?
i want to show the categorie list without showing duplicated ones

my controller code :

public function showActualite(){

```<code>    $actualites=DB::table('actualites')->distinct('categorie')->orderBy('created_at', 'DESC')->paginate(6);
    return view ('AllActualite',['actualites' => $actualites]);
}
</code>```

My view :

@foreach ($actualites as $act)

```<code>      <span style="text-transform: capitalize;" > <a href="/actualites/{{$act->categorie}}"> {{$act->categorie}} </a></span>

     <span style="text-transform: capitalize;">  </span>
    @endforeach
    </div>
    <div class="row">

        @foreach($actualites as $act)
      <div class="col-lg-6">
        <div class="box wow fadeInLeft">
          <div class="icon"><i class="fa fa-newspaper-o"></i></div>
          <h4 class="title"><a href="/actualites/consulter/{{$act->id}}">{{$act->categorie}}</a></h4>
          <p class="description">{{$act->titre}}</p>

        </div>
      </div>

      @endforeach
</code>```

答:
I believe your code should work, but in general i would not use DB logic. A more Laravel version of this would be. Latest is a syntax sugar for the same order by logic. See of this solves the problem.

```<code>$actualites = Actualites::query()->distinct('categorie')->latest()->paginate(6);
</code>```

Title: how to not display duplicated data?

Body:
i want to show the categorie list without showing duplicated ones

my controller code :

public function showActualite(){

    $actualites=DB::table('actualites')->distinct('categorie')->orderBy('created_at', 'DESC')->paginate(6);
    return view ('AllActualite',['actualites' => $actualites]);
}

My view :

@foreach ($actualites as $act)

       categorie}}"> {{$act->categorie}}

    @endforeach

        @foreach($actualites as $act)

id}}">{{$act->categorie}}

{{$act->titre}}

      @endforeach

Body:
I believe your code should work, but in general i would not use DB logic. A more Laravel version of this would be. Latest is a syntax sugar for the same order by logic. See of this solves the problem.

$actualites = Actualites::query()->distinct('categorie')->latest()->paginate(6);


问:
Can I check built-in type at runtime?
For example,

If I write:

```<code>char c = CHAR_MAX;
c++;
</code>```

Can I know if 'c++' results in int or char so I know for sure if its not an overflow?

答:
I don't know what you mean by "check at runtime", but I can tell you for sure that c++ results in a prvalue of type char, and c is always char. c is never converted to int.

Per [expr.post.incr]/1:

The value of a postfix ++ expression is the value of its operand.
  [ Note: The value obtained is a copy of the original value
  — end note ] The operand shall be a modifiable lvalue. The
  type of the operand shall be an arithmetic type other than cv
  bool, or a pointer to a complete object type. The value of the
  operand object is modified by adding 1 to it. The value computation
  of the ++ expression is sequenced before the modification of the
  operand object. With respect to an indeterminately-sequenced function
  call, the operation of postfix ++ is a single evaluation. [ Note:
  Therefore, a function call shall not intervene between the
  lvalue-to-rvalue conversion and the side effect associated with any
  single postfix ++ operator. — end note ] The result is a
  prvalue. The type of the result is the cv-unqualified version of the
  type of the operand. If the operand is a bit-field that cannot
  represent the incremented value, the resulting value of the bit-field
  is implementation-defined. See also [expr.add] and [expr.ass].

As mentioned by Nikos C. in comment, you should check if c == CHAR_MAX prior to incrementing. For more about checking for signed overflow, see Detecting signed overflow in C/C++.

Title: Can I check built-in type at runtime?

Body:
For example,

If I write:

char c = CHAR_MAX;
c++;

Can I know if 'c++' results in int or char so I know for sure if its not an overflow?


问:
How can I add a user to login as a user and not as a root Yocto project sama5d27 board
I am using sama5d27 som1 ek1 board and I build a Linux kernel for it using yocto project. I open minicom and the system boot. I want to remove debug tweaks from my image features but I must now set a user to my board to login and stop login as a root. How can I add a user to my board ?

答:
You have to use one of the useradd classes. The documentation is there:
https://www.yoctoproject.org/docs/current/ref-manual/ref-manual.html#ref-classes-useradd

The meta-skeleton/recipes-skeleton/useradd/useradd-example.bb recipe show you how to do that dynamically.

You can also provide the uids and gids statically by using USERADDEXTENSION = "useradd-staticids" in local.conf or, preferably in your distro and then having files/passwd and files/group anywhere in your BBPATH.


问:
How to get Discord.js bot to log variable for every user
I am creating a discord bot similar to Pokecord and would like to log 1 or more variables for every user. I am using Discord.js, JavaScript, and Node.js.
Is this possible?</p>

答:
well this isn't very easy, for this you must create a database i suggest you
Mysql install that and then watch the guide another cool guide here
then if you installed and watched the guides
`

```<code>const mysql = require('mysql');
const connection = mysql.createConnection({
  host     : 'myhost',
  port     : '3306',
  user     : 'myuser'
  password : 'mypassword',
  database : 'mydatabase',
  charset : 'utf8mb4'
});
</code>```

this to connect and then you have to create a value(money) in a database and assign a the value you want

```<code>bot.on('!money add 1', message => {
 //get the database and add a variable with the username and the value of money you want

});
</code>```

Body:
well this isn't very easy, for this you must create a database i suggest you
Mysql install that and then watch the guide another cool guide here
then if you installed and watched the guides
`

const mysql = require('mysql');
const connection = mysql.createConnection({
  host     : 'myhost',
  port     : '3306',
  user     : 'myuser'
  password : 'mypassword',
  database : 'mydatabase',
  charset : 'utf8mb4'
});

this to connect and then you have to create a value(money) in a database and assign a the value you want

bot.on('!money add 1', message => {
 //get the database and add a variable with the username and the value of money you want

});


问:
How to make pagination in the REST service?
I have to write some REST service which should handle a million entries and return a response to the user in JSON format. I'm writing some REST-controller using Spring and make pagination.

```<code>public List<ContactDto> getAllContacts() {

        double countItems = contactRepo.count();
        int pages = (int) Math.ceil(countItems / totalItemsPerPage);

        List<Contact> contacts = new ArrayList<>();
        for (int i = 0; i < pages; i++) {
            Page<Contact> page = contactRepo.findAll(PageRequest.of(i, totalItemsPerPage));
            contacts.addAll(page.stream().collect(Collectors.toList()));
        }

        return contacts.stream()
                .map(entity -> new ContactDto(entity.getId(), entity.getName()))
                .collect(Collectors.toList());
    }
</code>```

I'm new in spring and pagination.

In this approach is have a sense or I'm doing something wrong?

I mean I want to know I'm using pagination write or wrong?

Thanks for the help!

答: You should take a look at Spring Data Rest.</p>

Body:
I have to write some REST service which should handle a million entries and return a response to the user in JSON format. I'm writing some REST-controller using Spring and make pagination.

public List<ContactDto> getAllContacts() {

        double countItems = contactRepo.count();
        int pages = (int) Math.ceil(countItems / totalItemsPerPage);

        List<Contact> contacts = new ArrayList<>();
        for (int i = 0; i < pages; i++) {
            Page<Contact> page = contactRepo.findAll(PageRequest.of(i, totalItemsPerPage));
            contacts.addAll(page.stream().collect(Collectors.toList()));
        }

        return contacts.stream()
                .map(entity -> new ContactDto(entity.getId(), entity.getName()))
                .collect(Collectors.toList());
    }

I'm new in spring and pagination.

In this approach is have a sense or I'm doing something wrong?

I mean I want to know I'm using pagination write or wrong?

Thanks for the help!


问:
Mask rcnn not working for images with large resolution
I used Mask-Rcnn for training an image set (Note with high resolution Eg:2400*1920 ) with VIAtool following this reference article Mask rcnn usage. Here, I have edited the Ballon.py and the code is as follows:

```<code>import os
import sys
import json
import datetime
import numpy as np
import skimage.draw

# Root directory of the project
ROOT_DIR = os.path.abspath("../../")

# Import Mask RCNN
sys.path.append(ROOT_DIR)  # To find local version of the library
from mrcnn.config import Config
from mrcnn import model as modellib, utils

# Path to trained weights file
COCO_WEIGHTS_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")

if COCO_WEIGHTS_PATH is None:
    print('weights not available')
else:
    print('weights available')

DEFAULT_LOGS_DIR = os.path.join(ROOT_DIR, "logs")

#  Configurations
class NeuralCodeConfig(Config):
    NAME = "screens"

    # We use a GPU with 12GB memory, which can fit two images.
    # Adjust down if you use a smaller GPU.
    IMAGES_PER_GPU = 1

    # Number of classes (including background)
    NUM_CLASSES = 1 + 10 # Background + other region classes

    # Number of training steps per epoch
    STEPS_PER_EPOCH = 30

    # Skip detections with < 90% confidence
    DETECTION_MIN_CONFIDENCE = 0.9

#  Dataset
class NeuralCodeDataset(utils.Dataset):
    def load_screen(self, dataset_dir, subset):
        """Load a subset of the screens dataset.
        dataset_dir: Root directory of the dataset.
        subset: Subset to load: train or val
        """
        # Add classes.
        self.add_class("screens",1,"logo")
        self.add_class("screens",2,"slider")
        self.add_class("screens",3,"navigation")
        self.add_class("screens",4,"forms")
        self.add_class("screens",5,"social_media_icons")
        self.add_class("screens",6,"video")
        self.add_class("screens",7,"map")
        self.add_class("screens",8,"pagination")
        self.add_class("screens",9,"pricing_table_block")
        self.add_class("screens",10,"gallery")

        # Train or validation dataset?
        assert subset in ["train", "val"]
        dataset_dir = os.path.join(dataset_dir, subset)

         # Load annotations
        # VGG Image Annotator saves each image in the form:
        # { 'filename': '28503151_5b5b7ec140_b.jpg',
        #   'regions': {
        #       '0': {
        #           'region_attributes': {},
        #           'shape_attributes': {
        #               'all_points_x': [...],
        #               'all_points_y': [...],
        #               'name': 'polygon'}},
        #       ... more regions ...
        #   },
        #   'size': 100202
        # }
        # We mostly care about the x and y coordinates of each region
        annotations = json.load(open(os.path.join(dataset_dir, "via_region_data.json")))
        if annotations is None:
            print ("region data json not loaded")
        else:
            print("region data json loaded")
        # print(annotations)
        annotations = list(annotations.values())  # don't need the dict keys

        # The VIA tool saves images in the JSON even if they don't have any
        # annotations. Skip unannotated images.
        annotations = [a for a in annotations if a['regions']]

        # Add images
        for a in annotations:
            # Get the x, y coordinaets of points of the polygons that make up
            # the outline of each object instance. There are stores in the
            # shape_attributes and region_attributes (see json format above)
            polygons = [r['shape_attributes'] for r in a['regions']]
            screens = [r['region_attributes']for r in a['regions']]
            #getting the filename by spliting
            class_name = screens[0]['html']
            file_name = a['filename'].split("/")
            file_name = file_name[len(file_name)-1]

            #getting class_ids with file_name
            class_ids = class_name+"_"+file_name
            # #getting width an height of the images
            # height = [h['height'] for h in polygons]
            # width = [w['width'] for w in polygons]
            # print(height,'height')
            # print('polygons',polygons)

            # load_mask() needs the image size to convert polygons to masks.
            # Unfortunately, VIA doesn't include it in JSON, so we must readpath
            # the image. This is only managable since the dataset is tiny.
            image_path = os.path.join(dataset_dir,file_name)
            image = skimage.io.imread(image_path)
             #resizing images
            # image = utils.resize_image(image, min_dim=800, max_dim=1000, min_scale=None, mode="square")
            # print('image',image)
            height,width = image.shape[:2]
            # print('height',height)
            # print('width',width)
            # height = 800
            # width = 800

            self.add_image(
                "screens",
                image_id=file_name,  # use file name as a unique image id
                path=image_path,
                width=width, height=height,
                polygons=polygons,
                class_ids=class_ids)

    def load_mask(self, image_id):
        """Generate instance masks for an image.
       Returns:
        masks: A bool array of shape [height, width, instance count] with
            one mask per instance.
        class_ids: a 1D array of class IDs of the instance masks.
        """
        # If not a screens dataset image, delegate to parent class.
        image_info = self.image_info[image_id]
        if image_info["source"] != "screens":
            return super(self.__class__, self).load_mask(image_id)

        # Convert polygons to a bitmap mask of shape
        # [height, width, instance_count]
        info = self.image_info[image_id]
        mask = np.zeros([info["height"], info["width"], len(info["polygons"])],
                        dtype=np.uint8)
        for i, p in enumerate(info["polygons"]):
            # Get indexes of pixels inside the polygon and set them to 1
            rr, cc = skimage.draw.polygon(p['y'], p['x'])
            mask[rr, cc, i] = 1

        # Return mask, and array of class IDs of each instance. Since we have
        # one class ID only, we return an array of 1s
        # return mask.astype(np.bool), np.ones([mask.shape[-1]], dtype=np.int32)
        # class_ids = np.array(class_ids,dtype=np.int32)
        return mask,class_ids

    def image_reference(self, image_id):
        """Return the path of the image."""
        info = self.image_info[image_id]
        if info["source"] == "screens":
            return info["path"]
        else:
            super(self.__class__, self).image_reference(image_id)

def train(model):
    # Train the model.
    # Training dataset.

    dataset_train = NeuralCodeDataset()
    dataset_train.load_screen(args.dataset, "train")
    dataset_train.prepare()

    # Validation dataset
    dataset_val = NeuralCodeDataset()
    dataset_val.load_screen(args.dataset, "val")
    dataset_val.prepare()

    # *** This training schedule is an example. Update to your needs ***
    # Since we're using a very small dataset, and starting from
    # COCO trained weights, we don't need to train too long. Also,
    # no need to train all layers, just the heads should do it.
    print("Training network heads")
    model.train(dataset_train, dataset_val,
                learning_rate=config.LEARNING_RATE,
                epochs=30,
                layers='heads')

#  Training
if __name__ == '__main__':
    import argparse

# Parse command line arguments
parser = argparse.ArgumentParser(
    description='Train Mask R-CNN to detect screens.')
parser.add_argument("command",
                    metavar="<command>",
                    help="'train' or 'splash'")
parser.add_argument('--dataset', required='True',
                    metavar="../../datasets/screens",
                    help='Directory of the screens dataset')
parser.add_argument('--weights', required=True,
                    metavar="/weights.h5",
                    help="Path to weights .h5 file or 'coco'")
parser.add_argument('--logs', required=False,
                    default=DEFAULT_LOGS_DIR,
                    metavar="../../logs/",
                    help='Logs and checkpoints directory (default=logs/)')
parser.add_argument('--image', required=False,
                    metavar="path or URL to image",
                    help='Image to apply the color splash effect on')
parser.add_argument('--video', required=False,
                    metavar="path or URL to video",
                    help='Video to apply the color splash effect on')
args = parser.parse_args()

# Validate arguments
if args.command == "train":
    assert args.dataset, "Argument --dataset is required for training"
elif args.command == "splash":
    assert args.image or args.video,\
           "Provide --image or --video to apply color splash"

print("Weights: ", args.weights)
print("Dataset: ", args.dataset)
print("Logs: ", args.logs)

# Configurations
if args.command == "train":
    config = NeuralCodeConfig()
else:
    class InferenceConfig(NeuralCodeConfig):
        # Set batch size to 1 since we'll be running inference on
        # one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
        GPU_COUNT = 1
        IMAGES_PER_GPU = 1
    config = InferenceConfig()
config.display()

# Create model
if args.command == "train":
    model = modellib.MaskRCNN(mode="training", config=config,
                              model_dir=args.logs)
else:
    model = modellib.MaskRCNN(mode="inference", config=config,
                              model_dir=args.logs)

# Select weights file to load
if args.weights.lower() == "coco":
    weights_path = COCO_WEIGHTS_PATH
    # Download weights file
    if not os.path.exists(weights_path):
        utils.download_trained_weights(weights_path)
elif args.weights.lower() == "last":
    # Find last trained weights
    weights_path = model.find_last()
elif args.weights.lower() == "imagenet":
    # Start from ImageNet trained weights
    weights_path = model.get_imagenet_weights()
else:
    weights_path = args.weights

# Load weights
print("Loading weights ", weights_path)
if args.weights.lower() == "coco":
    # Exclude the last layers because they require a matching
    # number of classes
    model.load_weights(weights_path, by_name=True, exclude=[
        "mrcnn_class_logits", "mrcnn_bbox_fc",
        "mrcnn_bbox", "mrcnn_mask"])
else:
    model.load_weights(weights_path, by_name=True)

# Train or evaluate
if args.command == "train":
    train(model)
# elif args.command == "splash":
#     detect_and_color_splash(model, image_path=args.image,
#                             video_path=args.video)
else:
    print("'{}' is not recognized. "
          "Use 'train' or 'splash'".format(args.command))
</code>```

And I am getting the following error when training the data set with pretrained COCO dataset:

UserWarning: Converting sparse IndexedSlices to a dense Tensor of unknown shape. This may consume a large amount of memory.
  "Converting sparse IndexedSlices to a dense Tensor of unknown shape. "
2018-08-09 13:52:27.993239: W tensorflow/core/framework/allocator.cc:108] Allocation of 51380224 exceeds 10% of system memory.
2018-08-09 13:52:28.037704: W tensorflow/core/framework/allocator.cc:108] Allocation of 51380224 exceeds 10% of system memory.
/home/scit/anaconda3/lib/python3.6/site-packages/keras/engine/training.py:2022: UserWarning: Using a generator with use_multiprocessing=True` and multiple workers may duplicate your data. Please consider using the`keras.utils.Sequence class.
  UserWarning('Using a generator with `use_multiprocessing=True`'

ERROR:root:Error processing image {'id': '487.jpg', 'source': 'screens', 'path': '../../datasets/screens/train/487.jpg', 'width': 1920, 'height': 7007, 'polygons': [{'name': 'rect', 'x': 384, 'y': 5, 'width': 116, 'height': 64}, {'name': 'rect', 'x': 989, 'y': 17, 'width': 516, 'height': 42}, {'name': 'rect', 'x': 984, 'y': 5933, 'width': 565, 'height': 273}, {'name': 'rect', 'x': 837, 'y': 6793, 'width': 238, 'height': 50}], 'class_ids': 'logo_487.jpg'}
    Traceback (most recent call last):
      File "/home/scit/Desktop/My_work/object_detection/mask_rcnn/mrcnn/model.py", line 1717, in data_generator
        use_mini_mask=config.USE_MINI_MASK)
      File "/home/scit/Desktop/My_work/object_detection/mask_rcnn/mrcnn/model.py", line 1219, in load_image_gt
        mask, class_ids = dataset.load_mask(image_id)
      File "neural_code.py", line 235, in load_mask
        rr, cc = skimage.draw.polygon(p['y'], p['x'])
      File "/home/scit/anaconda3/lib/python3.6/site-packages/skimage/draw/draw.py", line 441, in polygon
        return _polygon(r, c, shape)
      File "skimage/draw/_draw.pyx", line 217, in skimage.draw._draw._polygon (skimage/draw/_draw.c:4402)
    OverflowError: Python int too large to convert to C ssize_t
```

My laptop graphics specs are follows:

Nvidia GeForce 830M (2 GB) with 250 CUDA cores

CPU specs:

Intel Core i5 (4th gen), 8 GB RAM

What may be the case here? Is it the resolution of the images or the incapability of my GPU. Shall I proceed with CPU?

答:
I am sharing my observations with Mask RCNN while training my custom dataset.

My dataset comprises of images of various dimension (i.e. smallest image has approx 1700 x 1600 pixels and the largest image has approx 8500 x 4600 pixels).

I am training on nVIDIA RTX 2080Ti, 32 GB DDR4 RAM and while training I get the below mentioned warnings; but the training process completes.

UserWarning: Converting sparse IndexedSlices to a dense Tensor of unknown shape. This may consume a large amount of memory.
  "Converting sparse IndexedSlices to a dense Tensor of unknown shape. "

2019-05-23 15:25:23.433774: W T:\src\github\tensorflow\tensorflow\core\common_runtime\bfc_allocator.cc:219] Allocator (GPU_0_bfc) ran out of memory trying to allocate 3.14GiB. The caller indicates that this is not a failure, but may mean that there could be performance gains if more memory were available.

Few months back, I tried the Matterport Splash of Color Example on my Laptop which has 12 GB RAM and nVIDIA 920M (2GB GPU); and have encountered similar Memory Errors.

So, we can suspect that size of the GPU Memory is a contributing factor in this error.

Additionally, batch size is another contributing factor; but I see that you have set the IMAGE_PER_GPU=1. If you search for the BATCH_SIZE in the config.py file present in the mrcnn folder, you will find –

self.BATCH_SIZE = self.IMAGES_PER_GPU * self.GPU_COUNT

So, in your case the batch_size is 1.

In conclusion, I would suggest to please try the same code on a more powerful GPU.

Body:
I used Mask-Rcnn for training an image set (Note with high resolution Eg:2400*1920 ) with VIAtool following this reference article Mask rcnn usage. Here, I have edited the Ballon.py and the code is as follows:

import os
import sys
import json
import datetime
import numpy as np
import skimage.draw

# Root directory of the project
ROOT_DIR = os.path.abspath("../../")

# Import Mask RCNN
sys.path.append(ROOT_DIR)  # To find local version of the library
from mrcnn.config import Config
from mrcnn import model as modellib, utils

# Path to trained weights file
COCO_WEIGHTS_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")

if COCO_WEIGHTS_PATH is None:
    print('weights not available')
else:
    print('weights available')

DEFAULT_LOGS_DIR = os.path.join(ROOT_DIR, "logs")

#  Configurations
class NeuralCodeConfig(Config):
    NAME = "screens"

    # We use a GPU with 12GB memory, which can fit two images.
    # Adjust down if you use a smaller GPU.
    IMAGES_PER_GPU = 1

    # Number of classes (including background)
    NUM_CLASSES = 1 + 10 # Background + other region classes

    # Number of training steps per epoch
    STEPS_PER_EPOCH = 30

    # Skip detections with < 90% confidence
    DETECTION_MIN_CONFIDENCE = 0.9

#  Dataset
class NeuralCodeDataset(utils.Dataset):
    def load_screen(self, dataset_dir, subset):
        """Load a subset of the screens dataset.
        dataset_dir: Root directory of the dataset.
        subset: Subset to load: train or val
        """
        # Add classes.
        self.add_class("screens",1,"logo")
        self.add_class("screens",2,"slider")
        self.add_class("screens",3,"navigation")
        self.add_class("screens",4,"forms")
        self.add_class("screens",5,"social_media_icons")
        self.add_class("screens",6,"video")
        self.add_class("screens",7,"map")
        self.add_class("screens",8,"pagination")
        self.add_class("screens",9,"pricing_table_block")
        self.add_class("screens",10,"gallery")

        # Train or validation dataset?
        assert subset in ["train", "val"]
        dataset_dir = os.path.join(dataset_dir, subset)

         # Load annotations
        # VGG Image Annotator saves each image in the form:
        # { 'filename': '28503151_5b5b7ec140_b.jpg',
        #   'regions': {
        #       '0': {
        #           'region_attributes': {},
        #           'shape_attributes': {
        #               'all_points_x': [...],
        #               'all_points_y': [...],
        #               'name': 'polygon'}},
        #       ... more regions ...
        #   },
        #   'size': 100202
        # }
        # We mostly care about the x and y coordinates of each region
        annotations = json.load(open(os.path.join(dataset_dir, "via_region_data.json")))
        if annotations is None:
            print ("region data json not loaded")
        else:
            print("region data json loaded")
        # print(annotations)
        annotations = list(annotations.values())  # don't need the dict keys

        # The VIA tool saves images in the JSON even if they don't have any
        # annotations. Skip unannotated images.
        annotations = [a for a in annotations if a['regions']]

        # Add images
        for a in annotations:
            # Get the x, y coordinaets of points of the polygons that make up
            # the outline of each object instance. There are stores in the
            # shape_attributes and region_attributes (see json format above)
            polygons = [r['shape_attributes'] for r in a['regions']]
            screens = [r['region_attributes']for r in a['regions']]
            #getting the filename by spliting
            class_name = screens[0]['html']
            file_name = a['filename'].split("/")
            file_name = file_name[len(file_name)-1]

            #getting class_ids with file_name
            class_ids = class_name+"_"+file_name
            # #getting width an height of the images
            # height = [h['height'] for h in polygons]
            # width = [w['width'] for w in polygons]
            # print(height,'height')
            # print('polygons',polygons)

            # load_mask() needs the image size to convert polygons to masks.
            # Unfortunately, VIA doesn't include it in JSON, so we must readpath
            # the image. This is only managable since the dataset is tiny.
            image_path = os.path.join(dataset_dir,file_name)
            image = skimage.io.imread(image_path)
             #resizing images
            # image = utils.resize_image(image, min_dim=800, max_dim=1000, min_scale=None, mode="square")
            # print('image',image)
            height,width = image.shape[:2]
            # print('height',height)
            # print('width',width)
            # height = 800
            # width = 800

            self.add_image(
                "screens",
                image_id=file_name,  # use file name as a unique image id
                path=image_path,
                width=width, height=height,
                polygons=polygons,
                class_ids=class_ids)

    def load_mask(self, image_id):
        """Generate instance masks for an image.
       Returns:
        masks: A bool array of shape [height, width, instance count] with
            one mask per instance.
        class_ids: a 1D array of class IDs of the instance masks.
        """
        # If not a screens dataset image, delegate to parent class.
        image_info = self.image_info[image_id]
        if image_info["source"] != "screens":
            return super(self.__class__, self).load_mask(image_id)

        # Convert polygons to a bitmap mask of shape
        # [height, width, instance_count]
        info = self.image_info[image_id]
        mask = np.zeros([info["height"], info["width"], len(info["polygons"])],
                        dtype=np.uint8)
        for i, p in enumerate(info["polygons"]):
            # Get indexes of pixels inside the polygon and set them to 1
            rr, cc = skimage.draw.polygon(p['y'], p['x'])
            mask[rr, cc, i] = 1

        # Return mask, and array of class IDs of each instance. Since we have
        # one class ID only, we return an array of 1s
        # return mask.astype(np.bool), np.ones([mask.shape[-1]], dtype=np.int32)
        # class_ids = np.array(class_ids,dtype=np.int32)
        return mask,class_ids

    def image_reference(self, image_id):
        """Return the path of the image."""
        info = self.image_info[image_id]
        if info["source"] == "screens":
            return info["path"]
        else:
            super(self.__class__, self).image_reference(image_id)

def train(model):
    # Train the model.
    # Training dataset.

    dataset_train = NeuralCodeDataset()
    dataset_train.load_screen(args.dataset, "train")
    dataset_train.prepare()

    # Validation dataset
    dataset_val = NeuralCodeDataset()
    dataset_val.load_screen(args.dataset, "val")
    dataset_val.prepare()

    # *** This training schedule is an example. Update to your needs ***
    # Since we're using a very small dataset, and starting from
    # COCO trained weights, we don't need to train too long. Also,
    # no need to train all layers, just the heads should do it.
    print("Training network heads")
    model.train(dataset_train, dataset_val,
                learning_rate=config.LEARNING_RATE,
                epochs=30,
                layers='heads')

#  Training
if __name__ == '__main__':
    import argparse

# Parse command line arguments
parser = argparse.ArgumentParser(
    description='Train Mask R-CNN to detect screens.')
parser.add_argument("command",
                    metavar="<command>",
                    help="'train' or 'splash'")
parser.add_argument('--dataset', required='True',
                    metavar="../../datasets/screens",
                    help='Directory of the screens dataset')
parser.add_argument('--weights', required=True,
                    metavar="/weights.h5",
                    help="Path to weights .h5 file or 'coco'")
parser.add_argument('--logs', required=False,
                    default=DEFAULT_LOGS_DIR,
                    metavar="../../logs/",
                    help='Logs and checkpoints directory (default=logs/)')
parser.add_argument('--image', required=False,
                    metavar="path or URL to image",
                    help='Image to apply the color splash effect on')
parser.add_argument('--video', required=False,
                    metavar="path or URL to video",
                    help='Video to apply the color splash effect on')
args = parser.parse_args()

# Validate arguments
if args.command == "train":
    assert args.dataset, "Argument --dataset is required for training"
elif args.command == "splash":
    assert args.image or args.video,\
           "Provide --image or --video to apply color splash"

print("Weights: ", args.weights)
print("Dataset: ", args.dataset)
print("Logs: ", args.logs)

# Configurations
if args.command == "train":
    config = NeuralCodeConfig()
else:
    class InferenceConfig(NeuralCodeConfig):
        # Set batch size to 1 since we'll be running inference on
        # one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
        GPU_COUNT = 1
        IMAGES_PER_GPU = 1
    config = InferenceConfig()
config.display()

# Create model
if args.command == "train":
    model = modellib.MaskRCNN(mode="training", config=config,
                              model_dir=args.logs)
else:
    model = modellib.MaskRCNN(mode="inference", config=config,
                              model_dir=args.logs)

# Select weights file to load
if args.weights.lower() == "coco":
    weights_path = COCO_WEIGHTS_PATH
    # Download weights file
    if not os.path.exists(weights_path):
        utils.download_trained_weights(weights_path)
elif args.weights.lower() == "last":
    # Find last trained weights
    weights_path = model.find_last()
elif args.weights.lower() == "imagenet":
    # Start from ImageNet trained weights
    weights_path = model.get_imagenet_weights()
else:
    weights_path = args.weights

# Load weights
print("Loading weights ", weights_path)
if args.weights.lower() == "coco":
    # Exclude the last layers because they require a matching
    # number of classes
    model.load_weights(weights_path, by_name=True, exclude=[
        "mrcnn_class_logits", "mrcnn_bbox_fc",
        "mrcnn_bbox", "mrcnn_mask"])
else:
    model.load_weights(weights_path, by_name=True)

# Train or evaluate
if args.command == "train":
    train(model)
# elif args.command == "splash":
#     detect_and_color_splash(model, image_path=args.image,
#                             video_path=args.video)
else:
    print("'{}' is not recognized. "
          "Use 'train' or 'splash'".format(args.command))

And I am getting the following error when training the data set with pretrained COCO dataset:

UserWarning: Converting sparse IndexedSlices to a dense Tensor of unknown shape. This may consume a large amount of memory.
  "Converting sparse IndexedSlices to a dense Tensor of unknown shape. "
2018-08-09 13:52:27.993239: W tensorflow/core/framework/allocator.cc:108] Allocation of 51380224 exceeds 10% of system memory.
2018-08-09 13:52:28.037704: W tensorflow/core/framework/allocator.cc:108] Allocation of 51380224 exceeds 10% of system memory.
/home/scit/anaconda3/lib/python3.6/site-packages/keras/engine/training.py:2022: UserWarning: Using a generator with use_multiprocessing=True` and multiple workers may duplicate your data. Please consider using the`keras.utils.Sequence class.
  UserWarning('Using a generator with `use_multiprocessing=True`'

ERROR:root:Error processing image {'id': '487.jpg', 'source': 'screens', 'path': '../../datasets/screens/train/487.jpg', 'width': 1920, 'height': 7007, 'polygons': [{'name': 'rect', 'x': 384, 'y': 5, 'width': 116, 'height': 64}, {'name': 'rect', 'x': 989, 'y': 17, 'width': 516, 'height': 42}, {'name': 'rect', 'x': 984, 'y': 5933, 'width': 565, 'height': 273}, {'name': 'rect', 'x': 837, 'y': 6793, 'width': 238, 'height': 50}], 'class_ids': 'logo_487.jpg'}
    Traceback (most recent call last):
      File "/home/scit/Desktop/My_work/object_detection/mask_rcnn/mrcnn/model.py", line 1717, in data_generator
        use_mini_mask=config.USE_MINI_MASK)
      File "/home/scit/Desktop/My_work/object_detection/mask_rcnn/mrcnn/model.py", line 1219, in load_image_gt
        mask, class_ids = dataset.load_mask(image_id)
      File "neural_code.py", line 235, in load_mask
        rr, cc = skimage.draw.polygon(p['y'], p['x'])
      File "/home/scit/anaconda3/lib/python3.6/site-packages/skimage/draw/draw.py", line 441, in polygon
        return _polygon(r, c, shape)
      File "skimage/draw/_draw.pyx", line 217, in skimage.draw._draw._polygon (skimage/draw/_draw.c:4402)
    OverflowError: Python int too large to convert to C ssize_t

My laptop graphics specs are follows:

Nvidia GeForce 830M (2 GB) with 250 CUDA cores

CPU specs:

Intel Core i5 (4th gen), 8 GB RAM

What may be the case here? Is it the resolution of the images or the incapability of my GPU. Shall I proceed with CPU?


问:
Use JWT-Extended to produce JSON Web Signatures for user confirmation
I am building a RESTful API for an upcoming project. This needs some kind of user account verification. I implemented a token based confirmation procedure previously using itsdangerous. But I wonder wether I can accomplish the same by using JWT-Extended, as its already part of my app and I want to keep the number of dependencies as low as possible.

Could I just use a normal access_token for that?

I appreciate your help!

Edit:

I implemented the following two methods and they seem to work. I am just not sure, if this is considered good practice.

```<code>@blueprint.route('/gen_confirmation_token', methods=['GET'])
@jwt_required
def gen_confirmation_token():
    access_token = create_access_token(identity=get_jwt_identity(), user_claims={"confirm": True}, expires_delta=dt.timedelta(seconds=3600))
    # TODO send a link to mail
    return jsonify({"message": "confirmation token sent"}), 200

@blueprint.route('/confirm/<string:token>', methods=['GET'])
@jwt_required
def confirm_user(token):
    user_identity = get_jwt_identity()
    current_user = User.query.get(user_identity)
    decoded_token = decode_token(token)
    if decoded_token['identity'] == user_identity and decoded_token['user_claims'].get('confirm', False):
        current_user.confirm()
        return jsonify({"message": "user confirmed"}), 200
    return jsonify({"message": "invalid confirmation token"}), 400

</code>```

答:
EDIT

Seeing the code you've added, which seems to be working, the nature of my answer changes. I think your solution to the problem would be considered good practice. The main problems you have are security, i.e. no one should be able to create their own token, which the hash value confirms, and the tokens should be personalised in such a way that one and only one person can use them, which the user identity guarantees.

Since you can encode as much information as you want in a JWT token, you should be able to store the information you need in it as well. I'm not sure what format you were thinking of, but if you were to, for example, store the confirmation step someone still has to reach, you can store something like the following:

```<code>@jwt.user_claims_loader
def add_confirmation_status_to_token(user):
  """Given an identity, add the confirmation status to the token"""
  return dict(status=user.confirmed_email)
</code>```

For more information, have a look here

Body:
I am building a RESTful API for an upcoming project. This needs some kind of user account verification. I implemented a token based confirmation procedure previously using itsdangerous. But I wonder wether I can accomplish the same by using JWT-Extended, as its already part of my app and I want to keep the number of dependencies as low as possible.

Could I just use a normal access_token for that?

I appreciate your help!

Edit:

I implemented the following two methods and they seem to work. I am just not sure, if this is considered good practice.

@blueprint.route('/gen_confirmation_token', methods=['GET'])
@jwt_required
def gen_confirmation_token():
    access_token = create_access_token(identity=get_jwt_identity(), user_claims={"confirm": True}, expires_delta=dt.timedelta(seconds=3600))
    # TODO send a link to mail
    return jsonify({"message": "confirmation token sent"}), 200

@blueprint.route('/confirm/<string:token>', methods=['GET'])
@jwt_required
def confirm_user(token):
    user_identity = get_jwt_identity()
    current_user = User.query.get(user_identity)
    decoded_token = decode_token(token)
    if decoded_token['identity'] == user_identity and decoded_token['user_claims'].get('confirm', False):
        current_user.confirm()
        return jsonify({"message": "user confirmed"}), 200
    return jsonify({"message": "invalid confirmation token"}), 400

Body:
EDIT

Seeing the code you've added, which seems to be working, the nature of my answer changes. I think your solution to the problem would be considered good practice. The main problems you have are security, i.e. no one should be able to create their own token, which the hash value confirms, and the tokens should be personalised in such a way that one and only one person can use them, which the user identity guarantees.

Since you can encode as much information as you want in a JWT token, you should be able to store the information you need in it as well. I'm not sure what format you were thinking of, but if you were to, for example, store the confirmation step someone still has to reach, you can store something like the following:

@jwt.user_claims_loader
def add_confirmation_status_to_token(user):
  """Given an identity, add the confirmation status to the token"""
  return dict(status=user.confirmed_email)

For more information, have a look here


问:
In UISplitViewController, how to make master open detail view controller for iPhone?
I have a bit of a unique UISplitViewController setup (I think). My master view controller is a table view controller within a navigation controller, which is normal. However, my detail view controller is a UITabBarController. Each tab has a navigation controller to wrap the content within.

When the user selects a table row within the master view controller, I select a tab in the detail view controller and start pushing a view controller:

```<code>let detailViewController = masterViewController?.splitViewController
    .viewControllers[1] as? UITabBarController

let selectIndex = 2
let viewController = (detailViewController.viewControllers?[selectIndex] as? UINavigationController)?.viewControllers.first
detailViewController.selectIndex = selectIndex
viewController.show(myCustomViewController)
</code>```

This works great on iPad.. selecting an item in the master view controller table selects the tab in the detail view controller and pushes view controllers to it.

On iPhone, I have the master view controller shown on initial load using this post:

```<code>class MasterViewController: UIViewController {
  private var collapseDetailViewController = true

  override func viewDidLoad() {
    super.viewDidLoad()
    splitViewController?.delegate = self
  }
  // ...
}

extension MasterViewController: UISplitViewControllerDelegate {
  func splitViewController(_ splitViewController: UISplitViewController, collapseSecondary secondaryViewController: UIViewController, onto primaryViewController: UIViewController) -> Bool {
    return collapseDetailViewController
  }
}

extension MasterViewController {

  override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    collapseDetailViewController = false
    selectTabAndPush(index: 2, vc: myCustomViewController)
    // Tried swapping the order of these but still no luck
  }
}
</code>```

However, selecting a row in the master view controller table literally does nothing. How can I get the detail view controller to show when a row in the master table is selected?

答:
At very first, why you need both functionalities : Master-Detail and Tab. Because if you are implementing master detail with tab then you must have equal number of tab with the equal number of rows in Master, then only you can navigate to the tab when you select row. In iPhone there would be max 5 tabs only and if you have 7 rows in Master then what happened if user will click on row 6 and 7?

Because as per design concept : You should choose only one as per requirements.

Master-Detail -> Provides facilities like side menu, so user can access many more option from these.

TabBar -> Provide facilities of more option with having menu at bottom.

Anyway, you might have some unique requirement.

As you said on iPad it is working fine. So for iPhone you can go with below way :

UI in storyboard should be like :

Create or add UITabBarController in your project. (I have created tabbarcontroller named CustomTabController).

Then in didSelectRowAt, push tabbarcontroller :

```<code>override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.phone {
        let tabController = self.storyboard?.instantiateViewController(withIdentifier: "CustomTabController") as! CustomTabController
        self.navigationController?.pushViewController(tabController, animated: true)
        tab.selectedIndex = indexPath.row
    }
    else {
        // iPad code here
    }
}
</code>```

But make sure, that you have enough number of tabs same as number of rows.

Body:
I have a bit of a unique UISplitViewController setup (I think). My master view controller is a table view controller within a navigation controller, which is normal. However, my detail view controller is a UITabBarController. Each tab has a navigation controller to wrap the content within.

When the user selects a table row within the master view controller, I select a tab in the detail view controller and start pushing a view controller:

let detailViewController = masterViewController?.splitViewController
    .viewControllers[1] as? UITabBarController

let selectIndex = 2
let viewController = (detailViewController.viewControllers?[selectIndex] as? UINavigationController)?.viewControllers.first
detailViewController.selectIndex = selectIndex
viewController.show(myCustomViewController)

This works great on iPad.. selecting an item in the master view controller table selects the tab in the detail view controller and pushes view controllers to it.

On iPhone, I have the master view controller shown on initial load using this post:

class MasterViewController: UIViewController {
  private var collapseDetailViewController = true

  override func viewDidLoad() {
    super.viewDidLoad()
    splitViewController?.delegate = self
  }
  // ...
}

extension MasterViewController: UISplitViewControllerDelegate {
  func splitViewController(_ splitViewController: UISplitViewController, collapseSecondary secondaryViewController: UIViewController, onto primaryViewController: UIViewController) -> Bool {
    return collapseDetailViewController
  }
}

extension MasterViewController {

  override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    collapseDetailViewController = false
    selectTabAndPush(index: 2, vc: myCustomViewController)
    // Tried swapping the order of these but still no luck
  }
}

However, selecting a row in the master view controller table literally does nothing. How can I get the detail view controller to show when a row in the master table is selected?

Body:
At very first, why you need both functionalities : Master-Detail and Tab. Because if you are implementing master detail with tab then you must have equal number of tab with the equal number of rows in Master, then only you can navigate to the tab when you select row. In iPhone there would be max 5 tabs only and if you have 7 rows in Master then what happened if user will click on row 6 and 7?

Because as per design concept : You should choose only one as per requirements.

Master-Detail -> Provides facilities like side menu, so user can access many more option from these.

TabBar -> Provide facilities of more option with having menu at bottom.

Anyway, you might have some unique requirement.

As you said on iPad it is working fine. So for iPhone you can go with below way :

UI in storyboard should be like :

Create or add UITabBarController in your project. (I have created tabbarcontroller named CustomTabController).

Then in didSelectRowAt, push tabbarcontroller :

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.phone {
        let tabController = self.storyboard?.instantiateViewController(withIdentifier: "CustomTabController") as! CustomTabController
        self.navigationController?.pushViewController(tabController, animated: true)
        tab.selectedIndex = indexPath.row
    }
    else {
        // iPad code here
    }
}

But make sure, that you have enough number of tabs same as number of rows.


问:
How to require at compile time that a type parameter be a trait (and not a class or other type value)?
I'm looking for some kind of upper bound on a generic parameter T that ensures that T is a trait.

```<code>class Foo
trait Bar

def f[A ??? IsATrait] = ???

// f[Foo] Won't compile
f[Bar] // this is fine
</code>```

答:
Try

```<code>typeOf[Bar].typeSymbol.asClass.isTrait // true
typeOf[Foo].typeSymbol.asClass.isTrait // false
</code>```

At compile time

```<code>import scala.language.experimental.macros
import scala.reflect.macros.whitebox

trait IsATrait[A]

object IsATrait {
  implicit def materialize[A]: IsATrait[A] = macro impl[A]

  def impl[A: c.WeakTypeTag](c: whitebox.Context): c.Tree = {
    import c.universe._
    val tpA = weakTypeOf[A]
    if (tpA.typeSymbol.asClass.isTrait)
      q"new IsATrait[$tpA] {}"
    else c.abort(c.enclosingPosition, s"$tpA is not a trait")
  }
}

def f[A: IsATrait] = ???

f[Bar] // compiles

f[Foo]
//Information: IsATrait.materialize is not a valid implicit value for IsATrait[Foo] because:
//             hasMatchingSymbol reported error: Foo is not a trait
//
//Error: could not find implicit value for evidence parameter of type IsATrait[Foo]
//
//Error: not enough arguments for method f: (implicit evidence$1: IsATrait[Foo])Nothing.
//       Unspecified value parameter evidence$1.
</code>```

Body:
I'm looking for some kind of upper bound on a generic parameter T that ensures that T is a trait.

class Foo
trait Bar

def f[A ??? IsATrait] = ???

// f[Foo] Won't compile
f[Bar] // this is fine

Body:
Try

typeOf[Bar].typeSymbol.asClass.isTrait // true
typeOf[Foo].typeSymbol.asClass.isTrait // false

At compile time

import scala.language.experimental.macros
import scala.reflect.macros.whitebox

trait IsATrait[A]

object IsATrait {
  implicit def materialize[A]: IsATrait[A] = macro impl[A]

  def impl[A: c.WeakTypeTag](c: whitebox.Context): c.Tree = {
    import c.universe._
    val tpA = weakTypeOf[A]
    if (tpA.typeSymbol.asClass.isTrait)
      q"new IsATrait[$tpA] {}"
    else c.abort(c.enclosingPosition, s"$tpA is not a trait")
  }
}

def f[A: IsATrait] = ???

f[Bar] // compiles

f[Foo]
//Information: IsATrait.materialize is not a valid implicit value for IsATrait[Foo] because:
//             hasMatchingSymbol reported error: Foo is not a trait
//
//Error: could not find implicit value for evidence parameter of type IsATrait[Foo]
//
//Error: not enough arguments for method f: (implicit evidence$1: IsATrait[Foo])Nothing.
//       Unspecified value parameter evidence$1.


问:
How to insert ellipsis into a large dataset in HTML with the least latency
Insert ellipsis (...) into HTML tag if content too wide mentions how to insert ellipsis into HTML content. This solution works, but I find that when it is applied to a large dataset (imagine 1,000 div's) it tends to get pretty slow.

My JSFiddle demonstrates this issue. Changing the variable I set (numberOfDivs) from 10 to 100 dramatically increases the time it takes to output the results. Changing this number from 100 to 1000, causes Chrome to want to kill the page.

It seems like Jquery's solution, and the others I have seen, all involve either populating an element in the DOM with the text (I believe the Jquery plugin I used does something of the sort, it seems expensive for large datasets) or using a feature not supported by IE7 and other older browsers (text-wrap:ellipsis).

It also seems like any calls to get element.offsetWidth or element.clientWidth get expensive when dealing with large sets of data.

Has anyone found any optimizations or robust server-side solutions? When I say robust server-side solution, I mean one that takes into account the text's size and size of the containing div (or other element). Simply doing  a substring of the text is one solution, but I would not consider that robust.

答:
I've solved similar issue in the past and following optimization performed better for our purpose. It performs better especially for texts much longer than required space. But 1000 div's... don't know...

Instead of the while loop in the mentioned solution, try something like this:

```<code>var diff = t.width() - el.width();
if (diff <= 0)
    return;
else {
    var avgWidth = t.width() / text.length;
    var redundantChars = diff / avgWidth;
    text = text.substr(0, text.length - redundantChars);
    t.html(text + "...");
}

while (text.length > 0 && t.width() > el.width()) {
    text = text.substr(0, text.length - 1);
    t.html(text + "...");
}

while (text.length < originalText.length && (t.width() + avgWidth) < el.width()) {
    text = originalText.substr(0, text.length + 1);
    t.html(text + "...");
}
</code>```

ContentLicense: CC BY-SA 3.0

Body:
I've solved similar issue in the past and following optimization performed better for our purpose. It performs better especially for texts much longer than required space. But 1000 div's... don't know...

Instead of the while loop in the mentioned solution, try something like this:

var diff = t.width() - el.width();
if (diff <= 0)
    return;
else {
    var avgWidth = t.width() / text.length;
    var redundantChars = diff / avgWidth;
    text = text.substr(0, text.length - redundantChars);
    t.html(text + "...");
}

while (text.length > 0 && t.width() > el.width()) {
    text = text.substr(0, text.length - 1);
    t.html(text + "...");
}

while (text.length < originalText.length && (t.width() + avgWidth) < el.width()) {
    text = originalText.substr(0, text.length + 1);
    t.html(text + "...");
}


问:
Get sum of selected/ unselected checkbox values and populate div
I have a set of checkboxes inside their respective labels and trying to get the values of selected labels/ checkboxes, add their values and populate the sum in a Div underneath.

The sum will keep changing depending upon select and unselect of the labels.

I have gotten to the part where the value of each selected label can be fetched on click, added the sum to its DIV and displayed. But here is the tricky part which I havent been able to figure out, how to subtract from that sum when an option is unselected after it was selected.

This is the function I have written to generate the labeled inputs from the database.

```<code>function getLabTestListCheck($color){
        global $db;
        $getLabTestListCheck = $db->prepare("SELECT * from lab_tests WHERE enabled = true ORDER BY test_name ASC");
        $getLabTestListCheck->execute();
        $testType = '';
        if($getLabTestListCheck->rowCount() > 0){
            while($showLabTestListCheck = $getLabTestListCheck->fetch(PDO::FETCH_ASSOC)){
                $id = $showLabTestListCheck['id'];
                $test_name = $showLabTestListCheck['test_name'];
                $test_rate = $showLabTestListCheck['test_rate'];

                $testType .=  "<label id='testType_label' name='test_label_list' class='btn btn-secondary {$color} test_type' data-name='{$test_name}' style='border-radius:0;'>";
                $testType .= "<input type='checkbox' id='test_type_{$test_name}' name='test_name[]' value='{$id},{$test_name},{$test_rate}' data-price='{$test_rate}'autocomplete='off'>{$test_name}";
                $testType .= "</label>";
            }
        return $testType;
        }
    }
</code>```

The above code will generate the labels something like this

```<code><div class="btn-group btn-group-toggle" style="flex-flow: row wrap;" data-toggle="buttons">
    <label id="testType_label_28" name="test_label_list" class="btn btn-secondary success test_type" data-name="A.S.O Titter">
        <input type="checkbox" id="test_type_28" name="test_name[]" value="28,A.S.O Titter,200.00" data-price="200.00">A.S.O Titter
    </label>
    <label id="testType_label_1" name="test_label_list" class="btn btn-secondary success test_type" data-name="Albumin">
        <input type="checkbox" id="test_type_1" name="test_name[]" value="1,Albumin,100.00" data-price="100.00">Albumin
    </label>
    <label id="testType_label_2" name="test_label_list" class="btn btn-secondary success test_type" data-name="ALP">
        <input type="checkbox" id="test_type_2" name="test_name[]" value="2,ALP,100.00" data-price="100.00">ALP
    </label>
    <label id="testType_label_6" name="test_label_list" class="btn btn-secondary success test_type" data-name="APTT">
        <input type="checkbox" id="test_type_6" name="test_name[]" value="6,APTT,200.00" data-price="200.00">APTT
    </label>
    <label id="testType_label_7" name="test_label_list" class="btn btn-secondary success test_type" data-name="B.S.R">
        <input type="checkbox" id="test_type_7" name="test_name[]" value="7,B.S.R,50.00" data-price="50.00">B.S.R
    </label>
    <label id="testType_label_20" name="test_label_list" class="btn btn-secondary success test_type" data-name="B.T+C.T">
        <input type="checkbox" id="test_type_20" name="test_name[]" value="20,B.T+C.T,100.00" data-price="100.00">B.T+C.T
    </label>
    <label id="testType_label_25" name="test_label_list" class="btn btn-secondary success test_type" data-name="Calcium">
        <input type="checkbox" id="test_type_25" name="test_name[]" value="25,Calcium,120.00" data-price="120.00">Calcium
    </label>
    <label id="testType_label_8" name="test_label_list" class="btn btn-secondary success test_type" data-name="CBC">
        <input type="checkbox" id="test_type_8" name="test_name[]" value="8,CBC,200.00" data-price="200.00">CBC
    </label>
</div>
</code>```

Below is the div which will contain the (add/subtract on-call) sum of values inside data-price attribute

```<code><div class='priceDiv'></div>
</code>```

The jQuery I have written is as follows,

```<code>    // Test Type fetch price from data attr and add to rate list
    var sum = [];
    $('.test_type').on('click', function(){

            var id = $(this).data("id");
            var val = $('#test_type_'+id).data("price");
            sum.push(val);

            var total=0;
            for(var i = 0; i < sum.length; i++) {
                total += sum[i] << 0;
            }
            $('#priceDiv').html(total);
    });
</code>```

I want to subtract the value of later ... "unselected" checkbox from the sum.
For now clicking on any label just keeps adding to the sum regardless of selection or unselection

Please help

答:
Nevermind, I figured it out myself.
Though it might not be the best solution out there, but it still works so I think I should be fine with it.

Just added a line of code in my jQuery and it worked.

```<code>// Test Type fetch price from data attr and add to rate list
    var sum = [];
    $('.test_type').on('change', function(){

            var id = $(this).data("id");

            /* Added this if statement to manipulate on class of selected label */
            if($(this).hasClass('active')){
                var val = '-'+$('#test_type_'+id).data("price");
            } else {
                val = $('#test_type_'+id).data("price");
            }
            /* -------------------------------- */

            sum.push(val);

            var total=0;
            for(var i = 0; i < sum.length; i++) {
                total += sum[i] << 0;
            }
            $('#priceDiv').html(total);
    });
</code>```

Body:
I have a set of checkboxes inside their respective labels and trying to get the values of selected labels/ checkboxes, add their values and populate the sum in a Div underneath.

The sum will keep changing depending upon select and unselect of the labels.

I have gotten to the part where the value of each selected label can be fetched on click, added the sum to its DIV and displayed. But here is the tricky part which I havent been able to figure out, how to subtract from that sum when an option is unselected after it was selected.

This is the function I have written to generate the labeled inputs from the database.

function getLabTestListCheck($color){
        global $db;
        $getLabTestListCheck = $db->prepare("SELECT * from lab_tests WHERE enabled = true ORDER BY test_name ASC");
        $getLabTestListCheck->execute();
        $testType = '';
        if($getLabTestListCheck->rowCount() > 0){
            while($showLabTestListCheck = $getLabTestListCheck->fetch(PDO::FETCH_ASSOC)){
                $id = $showLabTestListCheck['id'];
                $test_name = $showLabTestListCheck['test_name'];
                $test_rate = $showLabTestListCheck['test_rate'];

                $testType .=  "";
                $testType .= "<input type='checkbox' id='test_type_{$test_name}' name='test_name[]' value='{$id},{$test_name},{$test_rate}' data-price='{$test_rate}'autocomplete='off'>{$test_name}";
                $testType .= "";
            }
        return $testType;
        }
    }

The above code will generate the labels something like this

        <input type="checkbox" id="test_type_28" name="test_name[]" value="28,A.S.O Titter,200.00" data-price="200.00">A.S.O Titter

        <input type="checkbox" id="test_type_1" name="test_name[]" value="1,Albumin,100.00" data-price="100.00">Albumin

        <input type="checkbox" id="test_type_2" name="test_name[]" value="2,ALP,100.00" data-price="100.00">ALP

        <input type="checkbox" id="test_type_6" name="test_name[]" value="6,APTT,200.00" data-price="200.00">APTT

        <input type="checkbox" id="test_type_7" name="test_name[]" value="7,B.S.R,50.00" data-price="50.00">B.S.R

        <input type="checkbox" id="test_type_20" name="test_name[]" value="20,B.T+C.T,100.00" data-price="100.00">B.T+C.T

        <input type="checkbox" id="test_type_25" name="test_name[]" value="25,Calcium,120.00" data-price="120.00">Calcium

        <input type="checkbox" id="test_type_8" name="test_name[]" value="8,CBC,200.00" data-price="200.00">CBC

Below is the div which will contain the (add/subtract on-call) sum of values inside data-price attribute

The jQuery I have written is as follows,

    // Test Type fetch price from data attr and add to rate list
    var sum = [];
    $('.test_type').on('click', function(){

            var id = $(this).data("id");
            var val = $('#test_type_'+id).data("price");
            sum.push(val);

            var total=0;
            for(var i = 0; i < sum.length; i++) {
                total += sum[i] << 0;
            }
            $('#priceDiv').html(total);
    });

I want to subtract the value of later ... "unselected" checkbox from the sum.
For now clicking on any label just keeps adding to the sum regardless of selection or unselection

Please help

Body:
Nevermind, I figured it out myself.
Though it might not be the best solution out there, but it still works so I think I should be fine with it.

Just added a line of code in my jQuery and it worked.

// Test Type fetch price from data attr and add to rate list
    var sum = [];
    $('.test_type').on('change', function(){

            var id = $(this).data("id");

            /* Added this if statement to manipulate on class of selected label */
            if($(this).hasClass('active')){
                var val = '-'+$('#test_type_'+id).data("price");
            } else {
                val = $('#test_type_'+id).data("price");
            }
            /* -------------------------------- */

            sum.push(val);

            var total=0;
            for(var i = 0; i < sum.length; i++) {
                total += sum[i] << 0;
            }
            $('#priceDiv').html(total);
    });