问:
When use getOne and findOne methods Spring Data JPA
I have an use case where it calls the following:

```<code>@Override
@Transactional(propagation=Propagation.REQUIRES_NEW)
public UserControl getUserControlById(Integer id){
    return this.userControlRepository.getOne(id);
}
</code>```

Observe the @Transactional has Propagation.REQUIRES_NEW and the repository uses getOne. When I run the app, I receive the following error message:

```<code>Exception in thread "main" org.hibernate.LazyInitializationException:
could not initialize proxy - no Session
...
</code>```

But If I change the getOne(id) by findOne(id) all works fine.

BTW, just before the use case calls the getUserControlById method, it already has called the insertUserControl method

```<code>@Override
@Transactional(propagation=Propagation.REQUIRES_NEW)
public UserControl insertUserControl(UserControl userControl) {
    return this.userControlRepository.save(userControl);
}
</code>```

Both methods are Propagation.REQUIRES_NEW because I am doing a simple audit control.

I use the getOne method because it is defined in the JpaRepository interface and my Repository interface extends from there, I am working with JPA of course.

The JpaRepository interface extends from CrudRepository.
The findOne(id) method is defined in CrudRepository.

My questions are:

Why fail the getOne(id) method?

When I should use the getOne(id) method?

I am working with other repositories and all use the getOne(id) method and all work fine, only when I use the Propagation.REQUIRES_NEW it fails.

According with the getOne API:

Returns a reference to the entity with the given identifier.

According with the findOne API:

Retrieves an entity by its id.

When I should use the findOne(id) method?

What method is recommended to be used?

答:
I really find very difficult from the above answers.
From debugging perspective i almost spent 8 hours to know the silly mistake.

I have testing spring+hibernate+dozer+Mysql project. To be clear.

I have User entity, Book Entity. You do the calculations of mapping.

Were the Multiple books tied to One user. But in UserServiceImpl i was trying to find it by getOne(userId);

```<code>public UserDTO getById(int userId) throws Exception {

    final User user = userDao.getOne(userId);

    if (user == null) {
        throw new ServiceException("User not found", HttpStatus.NOT_FOUND);
    }
    userDto = mapEntityToDto.transformBO(user, UserDTO.class);

    return userDto;
}
</code>```

The Rest result is

```<code>{
"collection": {
    "version": "1.0",
    "data": {
        "id": 1,
        "name": "TEST_ME",
        "bookList": null
    },
    "error": null,
    "statusCode": 200
},
"booleanStatus": null
</code>```

}

The above code did not fetch the books which is read by the user let say.

The bookList was always null because of getOne(ID). After changing to findOne(ID). The result is

```<code>{
"collection": {
    "version": "1.0",
    "data": {
        "id": 0,
        "name": "Annama",
        "bookList": [
            {
                "id": 2,
                "book_no": "The karma of searching",
            }
        ]
    },
    "error": null,
    "statusCode": 200
},
"booleanStatus": null
</code>```

}

Body:
I have an use case where it calls the following:

@Override
@Transactional(propagation=Propagation.REQUIRES_NEW)
public UserControl getUserControlById(Integer id){
    return this.userControlRepository.getOne(id);
}

Observe the @Transactional has Propagation.REQUIRES_NEW and the repository uses getOne. When I run the app, I receive the following error message:

Exception in thread "main" org.hibernate.LazyInitializationException:
could not initialize proxy - no Session
...

But If I change the getOne(id) by findOne(id) all works fine.

BTW, just before the use case calls the getUserControlById method, it already has called the insertUserControl method

@Override
@Transactional(propagation=Propagation.REQUIRES_NEW)
public UserControl insertUserControl(UserControl userControl) {
    return this.userControlRepository.save(userControl);
}

Both methods are Propagation.REQUIRES_NEW because I am doing a simple audit control.

I use the getOne method because it is defined in the JpaRepository interface and my Repository interface extends from there, I am working with JPA of course.

The JpaRepository interface extends from CrudRepository.
The findOne(id) method is defined in CrudRepository.

My questions are:

Why fail the getOne(id) method?

When I should use the getOne(id) method?

I am working with other repositories and all use the getOne(id) method and all work fine, only when I use the Propagation.REQUIRES_NEW it fails.

According with the getOne API:

Returns a reference to the entity with the given identifier.

According with the findOne API:

Retrieves an entity by its id.

When I should use the findOne(id) method?

What method is recommended to be used?

Body:
I really find very difficult from the above answers.
From debugging perspective i almost spent 8 hours to know the silly mistake.

I have testing spring+hibernate+dozer+Mysql project. To be clear.

I have User entity, Book Entity. You do the calculations of mapping.

Were the Multiple books tied to One user. But in UserServiceImpl i was trying to find it by getOne(userId);

public UserDTO getById(int userId) throws Exception {

    final User user = userDao.getOne(userId);

    if (user == null) {
        throw new ServiceException("User not found", HttpStatus.NOT_FOUND);
    }
    userDto = mapEntityToDto.transformBO(user, UserDTO.class);

    return userDto;
}

The Rest result is

{
"collection": {
    "version": "1.0",
    "data": {
        "id": 1,
        "name": "TEST_ME",
        "bookList": null
    },
    "error": null,
    "statusCode": 200
},
"booleanStatus": null

}

The above code did not fetch the books which is read by the user let say.

The bookList was always null because of getOne(ID). After changing to findOne(ID). The result is

{
"collection": {
    "version": "1.0",
    "data": {
        "id": 0,
        "name": "Annama",
        "bookList": [
            {
                "id": 2,
                "book_no": "The karma of searching",
            }
        ]
    },
    "error": null,
    "statusCode": 200
},
"booleanStatus": null

}


来源: stackexchange

问:
Composer is not getting my new PHP version
I'm trying to install a package that requires PHP ^7.0. So I changed my host PHP version from 5.6 to 7.0.25. Running php -v return:

PHP 7.0.25 (cli) (built: Oct 30 2017 17:10:45) ( NTS )

But composer still relating version problem:

spatie/laravel-backup 4.19.2 requires php ^7.0 -> your PHP version (5.6.30) does not satisfy that requirement.

答:
You probably still have php5.6-cli installed on your server (which php)
It should work if you call

```<code>/path/to/php7 composer.phar update
</code>```

Or

Considering php7 executable is /usr/bin/php7.0, create an alias in your ~/.bash_profile

```<code>alias composer='/usr/bin/php7.0 /usr/local/bin/composer'
</code>```

Or

By default, composer (composer.phar generally located in /usr/local/bin/composer) uses the executable specified by env :

```<code>#!/usr/bin/env php
</code>```

You can also specify a targeted version of php to use :

```<code>#!/usr/bin/php7.0
</code>```

Or

completely remove php5.6-cli :)

ContentLicense: CC BY-SA 3.0

Body:
You probably still have php5.6-cli installed on your server (which php)
It should work if you call

/path/to/php7 composer.phar update

Or

Considering php7 executable is /usr/bin/php7.0, create an alias in your ~/.bash_profile

alias composer='/usr/bin/php7.0 /usr/local/bin/composer'

Or

By default, composer (composer.phar generally located in /usr/local/bin/composer) uses the executable specified by env :

#!/usr/bin/env php

You can also specify a targeted version of php to use :

#!/usr/bin/php7.0

Or

completely remove php5.6-cli :)


问:
Map Recyclerview inside scrollview display black flickering background while scrolling
I have used map view inside recyclerview row.
I have attached screenshot.
While scrolling maps display black flickering background.

Adapter code:

```<code> @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TASK_VIEW) {
            return new TasksViewHolder(LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_task_row, parent, false));
        } else if (viewType == CHECKIN_VIEW) {
            return new CheckInViewHolder(LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_checkin_row, parent, false));
        } else {
            return null;
        }
    }

@Override
private void onBindCheckInViewHolder(CheckInViewHolder holder, int position) {
    final JobCheckIn singleCheckIn = (JobCheckIn) items.get(position);
    holder.location.setText(singleCheckIn.getAddress());

    holder.lastUpdated.setText(String.valueOf("Created " +
            activity.getDateTime(singleCheckIn.getCreatedDateTime())));

    holder.deleteTask.setOnClickListener(view -> jobInterface.deleteCheckIn(singleCheckIn.getId()));

    float f = 0.0f;
    if (holder.mapView != null && Float.compare(singleCheckIn.getLatitude(), f) != 0 &&
            Float.compare(singleCheckIn.getLongitude(), f) != 0) {
        holder.mapView.setVisibility(View.VISIBLE);
        holder.initializeMapView();
        if (holder.gMap != null) {
            // The map is already ready to be used
            moveMap(holder.gMap, singleCheckIn.getLatitude(), singleCheckIn.getLongitude());
        }
    }

}
   public class CheckInViewHolder extends RecyclerView.ViewHolder implements OnMapReadyCallback {

    MapView mapView;

    GoogleMap gMap;

    public CheckInViewHolder(View view) {
        super(view);
        mapView = view.findViewById(R.id.checkInMapView);

    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        MapsInitializer.initialize(context);
        gMap = googleMap;
        googleMap.getUiSettings().setMapToolbarEnabled(false);
        if(items.get(getLayoutPosition())!=null )
            moveMap(gMap,((JobCheckIn) items.get(getLayoutPosition())).getLatitude(), ((JobCheckIn) items.get(getLayoutPosition())).getLongitude());
    }
    public void initializeMapView() {
        if (mapView != null) {
            // Initialise the MapView
            mapView.onCreate(null);
            // Set the map ready callback to receive the GoogleMap object
            mapView.getMapAsync(this);

        }
    }
}
 public void moveMap(GoogleMap gMap, double latitude, double longitude) {
    Log.v(TAG, "mapMoved: " + gMap);
    LatLng latlng = new LatLng(latitude, longitude);
    CameraUpdate cu = CameraUpdateFactory.newLatLngZoom(latlng, 6);
    gMap.addMarker(new MarkerOptions().position(latlng));
    gMap.moveCamera(cu);
}
</code>```

I refer this link: Link
  but in this demo they used array adapter in my code we have used recyclerview adapter.

xml code:

```<code> <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="150dp">

            <com.google.android.gms.maps.MapView
                android:id="@+id/checkInMapView"
                android:layout_width="0dp"
                android:layout_height="150dp"
                android:layout_weight="1"
                android:enabled="true"
                android:visibility="gone"
                map:cameraZoom="15"
                map:liteMode="true"
                map:mapType="normal" />

         <!--   I have also tried adding transparent view as below but still no solution.

        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/transparent"/>-->

        </LinearLayout>
</code>```

I am not performing any zoom-In, Zoom-out or drag.
  Thanks in advance.

Google Issue Track

Update

Previously i was initializing map in viewHolder instead of mentioned as above scenario.

```<code> onBindCheckInViewHolder(...){
 holder.mapView.onCreate(null);
            holder.mapView.getMapAsync(googleMap -> {
                MapsInitializer.initialize(context);
                gMap = googleMap;
                googleMap.getUiSettings().setMapToolbarEnabled(false);
                moveMap(gMap, singleCheckIn.getLatitude(), singleCheckIn.getLongitude());
            });
      }
</code>```

答:
So I dug around a bit, and here are some suggestions to make the performance a little better, it's not perfect, but it is a lot faster.

Map type

This basically determines how the map looks like (normal/hybrid... etc), you need to set the map type to GoogleMap.MAP_TYPE_NONE in these cases.

XML where you define the view, so this is the default type

Override onViewRecycled in your adapter and if your holder has a GoogleMap set its type to none

Only set your type back to GoogleMap.MAP_TYPE_NORMAL or whatever type you want, when your onBindViewHolder is called.

Pre-initialization

Call MapsInitializer.initialize(context) from your onMapReady callback.

Initialization

Call GoogleMap.onCreate() and GoogleMap.getMapAsync() from your onCreateViewHolder, maybe in your ViewHolder constructor.

RecyclerView Prefetch

You can also use a neat new feature of LinearLayoutManager, you can call LinearLayoutManager.setInitialPrefetchItemCount, to start preparing off screen items.

Not that this can cause other performance issues, tweak it till you get a nice balance.

Optimize layout

Try to have the layout where your RecyclerView to be flat as
possible, don't nest too much layouts

Make sure you set setHasFixedSize(true) for your RecyclerView

Make sure to avoid weights or RelativeLayouts as parents of your RecyclerView or your children list items.

Body:
I have used map view inside recyclerview row.
I have attached screenshot.
While scrolling maps display black flickering background.

Adapter code:

 @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TASK_VIEW) {
            return new TasksViewHolder(LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_task_row, parent, false));
        } else if (viewType == CHECKIN_VIEW) {
            return new CheckInViewHolder(LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_checkin_row, parent, false));
        } else {
            return null;
        }
    }

@Override
private void onBindCheckInViewHolder(CheckInViewHolder holder, int position) {
    final JobCheckIn singleCheckIn = (JobCheckIn) items.get(position);
    holder.location.setText(singleCheckIn.getAddress());

    holder.lastUpdated.setText(String.valueOf("Created " +
            activity.getDateTime(singleCheckIn.getCreatedDateTime())));

    holder.deleteTask.setOnClickListener(view -> jobInterface.deleteCheckIn(singleCheckIn.getId()));

    float f = 0.0f;
    if (holder.mapView != null && Float.compare(singleCheckIn.getLatitude(), f) != 0 &&
            Float.compare(singleCheckIn.getLongitude(), f) != 0) {
        holder.mapView.setVisibility(View.VISIBLE);
        holder.initializeMapView();
        if (holder.gMap != null) {
            // The map is already ready to be used
            moveMap(holder.gMap, singleCheckIn.getLatitude(), singleCheckIn.getLongitude());
        }
    }

}
   public class CheckInViewHolder extends RecyclerView.ViewHolder implements OnMapReadyCallback {

    MapView mapView;

    GoogleMap gMap;

    public CheckInViewHolder(View view) {
        super(view);
        mapView = view.findViewById(R.id.checkInMapView);

    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        MapsInitializer.initialize(context);
        gMap = googleMap;
        googleMap.getUiSettings().setMapToolbarEnabled(false);
        if(items.get(getLayoutPosition())!=null )
            moveMap(gMap,((JobCheckIn) items.get(getLayoutPosition())).getLatitude(), ((JobCheckIn) items.get(getLayoutPosition())).getLongitude());
    }
    public void initializeMapView() {
        if (mapView != null) {
            // Initialise the MapView
            mapView.onCreate(null);
            // Set the map ready callback to receive the GoogleMap object
            mapView.getMapAsync(this);

        }
    }
}
 public void moveMap(GoogleMap gMap, double latitude, double longitude) {
    Log.v(TAG, "mapMoved: " + gMap);
    LatLng latlng = new LatLng(latitude, longitude);
    CameraUpdate cu = CameraUpdateFactory.newLatLngZoom(latlng, 6);
    gMap.addMarker(new MarkerOptions().position(latlng));
    gMap.moveCamera(cu);
}

I refer this link: Link
  but in this demo they used array adapter in my code we have used recyclerview adapter.

xml code:

 <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="150dp">

            <com.google.android.gms.maps.MapView
                android:id="@+id/checkInMapView"
                android:layout_width="0dp"
                android:layout_height="150dp"
                android:layout_weight="1"
                android:enabled="true"
                android:visibility="gone"
                map:cameraZoom="15"
                map:liteMode="true"
                map:mapType="normal" />

        </LinearLayout>

I am not performing any zoom-In, Zoom-out or drag.
  Thanks in advance.

Google Issue Track

Update

Previously i was initializing map in viewHolder instead of mentioned as above scenario.

 onBindCheckInViewHolder(...){
 holder.mapView.onCreate(null);
            holder.mapView.getMapAsync(googleMap -> {
                MapsInitializer.initialize(context);
                gMap = googleMap;
                googleMap.getUiSettings().setMapToolbarEnabled(false);
                moveMap(gMap, singleCheckIn.getLatitude(), singleCheckIn.getLongitude());
            });
      }


问:
Casting a expression result on SQLite, from float to decimal
I have the following expression on a query that I'm running against a SQLite database, using SQLite.NET.

```<code>SUM(Detail.Qty * Products.Weight) AS MovedWeight
</code>```

The query works, but as Detail.Qty is decimal, and Weight is decimal too, it returns a float number. I'm trying to cast it to something like DECIMAL(10,2).
I know SQLite is special about datatypes, and I did try to use CAST with no sucess.

```<code>CAST(SUM(Detail.Qty * Products.Weight) AS DECIMAL(10,2)) AS MovedWeight
</code>```

It works too, but takes no notice about the precision (10,2), and gives me the same result that the first example.

Anyone has ideas? Converting the result to decimal in the DataTable would be acceptable too, but I have no idea of how to do that without an ugly, resource-consuming loop (it's expected that the query, at some point, will return 3000 or more results).

答: In SQLite, the numbers in parentheses after a type name don't do anything; they're just allowed for compatibility with other databases.  VARCHAR(100) means nothing more than "text affinity" and DECIMAL(10, 2) means nothing more than "numeric affinity".

ContentLicense: CC BY-SA 2.5

Body:
I have the following expression on a query that I'm running against a SQLite database, using SQLite.NET.

SUM(Detail.Qty * Products.Weight) AS MovedWeight

The query works, but as Detail.Qty is decimal, and Weight is decimal too, it returns a float number. I'm trying to cast it to something like DECIMAL(10,2).
I know SQLite is special about datatypes, and I did try to use CAST with no sucess.

CAST(SUM(Detail.Qty * Products.Weight) AS DECIMAL(10,2)) AS MovedWeight

It works too, but takes no notice about the precision (10,2), and gives me the same result that the first example.

Anyone has ideas? Converting the result to decimal in the DataTable would be acceptable too, but I have no idea of how to do that without an ugly, resource-consuming loop (it's expected that the query, at some point, will return 3000 or more results).


问:
Stacked bar chart bars dimension is not coralated to Y axis
I am trying to build a stacked bar chart using c3.js.

The issue I have is that the bars that get generated are not coralated to the Y axis.

This is what I have so far: https://codepen.io/anon/pen/dZLMoY?editors=1010

And this is the code:

```<code>c3.generate({
  bindto:'#chart',
     data: {
        type: 'bar',
        columns: [
           ['Column 1', 2, 10, 22, 34, 9, 60],
          ['Column 2', 6, 15, 43, 36, 45, 75],
          ['Column 3', 10, 20, 79, 39, 50, 97],
          ['Column 4', 12, 27, 87, 76, 55, 150]
        ],
        groups: [
          ['Column 1', 'Column 2', 'Column 3', 'Column 4']
        ],
      },
      bar: {
        width: 15,
      },
      axis: {
        x: {
          show: true
        },
        y: {
          show: true,
          padding: {
            top: 0,
            bottom: 0
          },
          tick: {
            //count: 6
          },
          min: 0,
          max: 150
        }
      },
      grid: {
        x: {
          show: true
        },
        y: {
          show: true
        }
      }
});
</code>```

As you can see the bar chart is generated but it is not generated correctly. The last 4 bars are all equal even if the values that are provided are not.

Taking into consideration that the Y axis has the maximum value set to 150 then the 2nd, 3rd and 4th bars should not ocupy the same height as the 5th bar

What am I doing wrong?

答:
Your axis.y.max value is less than stacked sum, so chart is clipped at the top.

Try this:

```<code>c3.generate({
     ...
      axis: {
        ...
        y: {
          ...
          min: 0,
          max: 500
        }
      }
   }
</code>```

Or see it in action.

Body:
I am trying to build a stacked bar chart using c3.js.

The issue I have is that the bars that get generated are not coralated to the Y axis.

This is what I have so far: https://codepen.io/anon/pen/dZLMoY?editors=1010

And this is the code:

c3.generate({
  bindto:'#chart',
     data: {
        type: 'bar',
        columns: [
           ['Column 1', 2, 10, 22, 34, 9, 60],
          ['Column 2', 6, 15, 43, 36, 45, 75],
          ['Column 3', 10, 20, 79, 39, 50, 97],
          ['Column 4', 12, 27, 87, 76, 55, 150]
        ],
        groups: [
          ['Column 1', 'Column 2', 'Column 3', 'Column 4']
        ],
      },
      bar: {
        width: 15,
      },
      axis: {
        x: {
          show: true
        },
        y: {
          show: true,
          padding: {
            top: 0,
            bottom: 0
          },
          tick: {
            //count: 6
          },
          min: 0,
          max: 150
        }
      },
      grid: {
        x: {
          show: true
        },
        y: {
          show: true
        }
      }
});

As you can see the bar chart is generated but it is not generated correctly. The last 4 bars are all equal even if the values that are provided are not.

Taking into consideration that the Y axis has the maximum value set to 150 then the 2nd, 3rd and 4th bars should not ocupy the same height as the 5th bar

What am I doing wrong?

Body:
Your axis.y.max value is less than stacked sum, so chart is clipped at the top.

Try this:

c3.generate({
     ...
      axis: {
        ...
        y: {
          ...
          min: 0,
          max: 500
        }
      }
   }

Or see it in action.


问:
"Python for Data Analysis" tutorial code runs unpredictably - US Baby Names proportions
I'm working through Wes McKinney's Python for Data Analysis. While I'm working in Python 3 and the book is written in Python 2, this is generally not an issue, and if anything a good exercise.

However, I've reached an impasse on Chapter 2, example 3: US Baby Names 1880 - 2010 (pg. 34). The purpose of the following code is to insert a column titled 'prop' that contains the fraction of babies given a name for each year and gender into the dataframe:

```<code>def add_prop(group):
    births = group.births.astype(float)
    group['prop']=births/births.sum()
    return group

names=names.groupby(['year','sex']).apply(add_prop)
</code>```

'names' is a dataframe with five columns ('name', 'sex', 'births', 'year', and this adds 'prop') and approximately 1.7 million rows. In order to test whether prop was added correctly, you then test when the proportions sum to approximately 1 with np.allclose(names.groupby(['year','sex']).prop.sum(), 1).

My problem is that the function runs unpredictably. Perhaps once out of every 15 or 20 runs np.allclose will be true, and the function will have been applied to the dataframe correctly. Otherwise np.allclose is false. Additionally, it's wrong in different ways. Later you use this dataframe to graph the proportion of births represented in the top 1000 names by sex, and the shape of that graph changes constantly. Some examples of graph change:  I know the problem is in how proportion is being calculated and added because the rest of the dataframe doesn't vary.

What is introducing unpredictability into this example? While I suspect it's the .apply() command, I'm not sure and don't know how to test my hypothesis. It's been suggested to me that part of the code block is deprecated, but Jupyter Notebook doesn't come up with a warning and I haven't been able to find anything online. I've gone over my code twice, and overall it's virtually identical to the book's, and is identical in the case of this block. Thanks in advance.

答:
I think the issue is in using a float data type for prop.  Floats are bad where you need accuracy. That's why, in the book, he says the sum of the props should add to 1 or are "sufficiently close to (but perhaps not exactly equal to) 1"

I'm new to python myself so I don't know the best solution.  In databases I avoid floats and use the decimal data type.  Regardless, if we're going to find the proportion of each of over a million records, it's going to be tough to maintain any single accuracy.

Body:
I'm working through Wes McKinney's Python for Data Analysis. While I'm working in Python 3 and the book is written in Python 2, this is generally not an issue, and if anything a good exercise.

However, I've reached an impasse on Chapter 2, example 3: US Baby Names 1880 - 2010 (pg. 34). The purpose of the following code is to insert a column titled 'prop' that contains the fraction of babies given a name for each year and gender into the dataframe:

def add_prop(group):
    births = group.births.astype(float)
    group['prop']=births/births.sum()
    return group

names=names.groupby(['year','sex']).apply(add_prop)

'names' is a dataframe with five columns ('name', 'sex', 'births', 'year', and this adds 'prop') and approximately 1.7 million rows. In order to test whether prop was added correctly, you then test when the proportions sum to approximately 1 with np.allclose(names.groupby(['year','sex']).prop.sum(), 1).

My problem is that the function runs unpredictably. Perhaps once out of every 15 or 20 runs np.allclose will be true, and the function will have been applied to the dataframe correctly. Otherwise np.allclose is false. Additionally, it's wrong in different ways. Later you use this dataframe to graph the proportion of births represented in the top 1000 names by sex, and the shape of that graph changes constantly. Some examples of graph change:  I know the problem is in how proportion is being calculated and added because the rest of the dataframe doesn't vary.

What is introducing unpredictability into this example? While I suspect it's the .apply() command, I'm not sure and don't know how to test my hypothesis. It's been suggested to me that part of the code block is deprecated, but Jupyter Notebook doesn't come up with a warning and I haven't been able to find anything online. I've gone over my code twice, and overall it's virtually identical to the book's, and is identical in the case of this block. Thanks in advance.


问:
Parallel Cosine similarity of two large files with each other
I have two files: A and B

```<code>A has 400,000 lines each having 50 float values
B has 40,000 lines having 50 float values.
</code>```

For every line in B, I need to find corresponding lines in A which have >90% similarity (cosine).

For linear search and computation, the code takes ginormous computing time. (40-50 hours)

Reaching out to the community for suggestions on how to fasten the process (link of blogs/resources such as AWS/Cloud to be used to achieve it). Have been stuck with this for quite a while!

[There were mentions of rpud/rpudplus to do it, but can't seem to perform them on cloud resources]

N.B. as requested, the code for cosine similarity is:

```<code>for line1, line2 in zip(f1, f2):
    line1 = line1[:-1]
    cnt = cnt + 1
    l2X = [float(i) for i in line2.split()]
    f3 = open(enLabelValues, 'r')
    f4 = open(enVectorValues, 'r')
    print cnt
    cnt_j = 0
    for line3, line4 in zip(f3, f4):
        line3 = line3[:-1]
        l4X = [float(i) for i in line4.split()]
        ########This is the line for spatial cosine similarity
        result = 1 - spatial.distance.cosine(l2X, l4X)
        cnt_j = cnt_j + 1
        if(result > float(0.95)):
            if line3 not in a.keys():
                a[line3] = True
                fLabel_2.write(line3+"\n")
                fX_2.write(line4)
        fLabel_2.flush()
        fX_2.flush()
        os.fsync(fLabel_2.fileno())
        os.fsync(fX_2.fileno())
</code>```

答:
I can generate synthetic files of 40,000 and 400,000 lines with 50 samples per line and process them in around 2 minutes 18 seconds on a reasonable 4 core (+hyperthreading) desktop iMac in my clumsy style of C++ without any SIMD optimisation (by me) using GNU Parallel.

Here is the top-level script. You can see it generates the test data in "a.txt" and "b.txt". Then it "compresses" "b.txt" to an identical binary representation, with the pre-computed magnitude appended to each line. Finally, it numbers the lines in "a.txt" and passes them into GNU Parallel which splits the lines into groups of around 5,200 lines and starts a group of 8 parallel processes to compare each of those lines with the 40,000 lines in B.

```<code>#!/bin/bash

# Generate test data - a.txt b.txt
./generate

# Preprocess b.txt into binary with precomputed magitudes save as B
./preprocess

# Process file A in batches
cat -n a.txt | parallel --block-size 2M --line-buffer --pipe ./process {#}
</code>```

Here is the generate.cpp program to synthesise data:

```<code>#include <iostream>
#include <cstdlib>
#include <fstream>
#include "common.h"

using namespace std;

int main()
{
   int line,sample;
   ofstream a("a.txt");
   if (!a.is_open()){
      cerr << "ERROR: Unable to open output file";
      exit(EXIT_FAILURE);
   }
   for(line=0;line<ALINES;line++){
      for(sample=0;sample<SAMPLESPERLINE;sample++){
         a << (float)rand()*100/RAND_MAX << " ";
      }
      a << endl;
   }
   a.close();
   ofstream b("b.txt");
   if (!b.is_open()){
      cerr << "ERROR: Unable to open output file";
      exit(EXIT_FAILURE);
   }
   for(line=0;line<BLINES;line++){
      for(sample=0;sample<SAMPLESPERLINE;sample++){
         b << (float)rand()*100/RAND_MAX << " ";
      }
      b << endl;
   }
   b.close();
}
</code>```

Here is the preprocess.cpp code:

```<code>#include <sstream>
#include <fstream>
#include <string>
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <cmath>
#include "common.h"

int main(int argc, char* argv[]){

   std::ifstream btxt("b.txt");
   std::ofstream bbin("B",std::ios::out|std::ios::binary);
   if (!btxt.is_open()){
      std::cerr << "ERROR: Unable to open input file";
      exit(EXIT_FAILURE);
   }
   if (!bbin.is_open()){
      std::cerr << "ERROR: Unable to open output file";
      exit(EXIT_FAILURE);
   }

   int l=0;
   std::string line;
   std::vector<float> v;
   v.resize(SAMPLESPERLINE+1);
   while (std::getline(btxt,line)){
      std::istringstream iss(line);
      v.clear();
      float f;
      double magnitude;
      magnitude=0.0;
      int s=0;
      while (iss >> f){
         v[s]=(f);
         magnitude+=(double)f*f;
         s++;
      }
      // Append the magnitude to the end of the "line"
      v[s]=(float)sqrt(magnitude);
      // Write the samples and magnitide in binary to the output file
      bbin.write(reinterpret_cast<char*>(&v[0]),(SAMPLESPERLINE+1)*sizeof(float));
      l++;
   }
   btxt.close();
   bbin.close();

   return EXIT_SUCCESS;
}
</code>```

Here is the common.h file:

```<code>const int ALINES=400000;
const int BLINES=40000;
const int SAMPLESPERLINE=50;
</code>```

And here is the process.cpp code:

```<code>#include <sstream>
#include <fstream>
#include <string>
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <array>
#include <cmath>
#include "common.h"

int main(int argc, char* argv[]){

   if(argc!=2){
      std::cerr << "Usage: process JOBNUM" << std::endl;
      exit(1);
   }
   int JobNum=std::atoi(argv[1]);
   std::cerr << "Starting job: " << JobNum << std::endl;

   // Load B
   std::ifstream bbin("B",std::ios::binary);
   if (!bbin.is_open()){
      std::cerr << "ERROR: Unable to open B";
      exit(EXIT_FAILURE);
   }

   int l=0;
   std::array<float,SAMPLESPERLINE+1> record;
   std::vector<std::array<float,SAMPLESPERLINE+1>> B;
   B.resize(BLINES);
   for(l=0;l<BLINES;l++){
      // Read one record of 50 floats and their magnitude
      bbin.read(reinterpret_cast<char*>(&B[l][0]),sizeof(float)*(SAMPLESPERLINE+1));
   }
   bbin.close();

   // Process all lines read from stdin, each line prepended by its line number
   // Format is:
   // <line number in file "a.txt"> <SAMPLE0> <SAMPLE1> ... <SAMPLE49>
   int nLines=0;
   std::string line;
   while (std::getline(std::cin,line)){
      nLines++;
      std::istringstream iss(line);
      std::vector<float> A;
      A.resize(SAMPLESPERLINE);
      float f;
      int Alineno;
      int s=0;
      iss >> Alineno;
      double dMag=0.0;
      while (iss >> f){
         A[s++]=f;
         dMag+=(double)f*f;
      }
      // Root magnitude
      float AMagnitude=(float)sqrt(dMag);

      // At this point we have in B, 40,000 records each of 50 samples followed by the magnitude
      // ... and we have a single record from "a.txt" with 50 samples and its magnitude in AMagnitude
      // ... and Alineno is the absolute line number in "a.txt" of this line
      // Time to do the actual calculation: compare this record to all records in B
      for(int brec=0;brec<BLINES;brec++){
         float BMagnitude=B[brec][SAMPLESPERLINE];
         double dotproduct=0.0;
         float *a = &A[0];
         float *b = &B[brec][0];
         for(s=0;s<SAMPLESPERLINE;s++){
            dotproduct += (*a++) * (*b++);
         }
         float similarity = dotproduct/(AMagnitude*BMagnitude);
         if(similarity>0.99){
            std::cout << "Line A: " << Alineno << ", line B: " << brec << ", similarity:" << similarity << std::endl;
         }
      }
   }
   std::cerr << "Ending job: " << JobNum << ", processed " << nLines << " lines" << std::endl;

   return EXIT_SUCCESS;
}
</code>```

The Makefile is pretty simple:

```<code>CFLAGS= -std=c++11 -O3 -march=native

all:    generate preprocess process

generate:   generate.cpp
        clang++ ${CFLAGS} generate.cpp -o generate

preprocess: preprocess.cpp
        clang++ ${CFLAGS} preprocess.cpp -o preprocess

process:    process.cpp
        clang++ ${CFLAGS} process.cpp -o process
</code>```

When you run it, it pegs the CPU for 2 minutes and looks like this:

```<code>time ./go
Starting job: 3
Starting job: 7
Starting job: 8
Starting job: 2
Starting job: 5
Starting job: 1
Starting job: 4
Starting job: 6
Ending job: 1, processed 5204 lines
Starting job: 9
Ending job: 2, processed 5203 lines
Ending job: 3, processed 5204 lines
Starting job: 11
Starting job: 10
Ending job: 4, processed 5204 lines
Starting job: 12
Ending job: 5, processed 5203 lines
Ending job: 6, processed 5203 lines
Starting job: 14
Starting job: 13
...
...
Starting job: 75
Ending job: 68, processed 5204 lines
Ending job: 69, processed 5203 lines
Starting job: 76
Starting job: 77
Ending job: 70, processed 5203 lines
Ending job: 71, processed 5204 lines
Ending job: 72, processed 5203 lines
Ending job: 77, processed 4535 lines
Ending job: 74, processed 5204 lines
Ending job: 73, processed 5205 lines
Ending job: 75, processed 5204 lines
Ending job: 76, processed 5203 lines

real    2m17.510s
user    16m24.533s
sys     0m4.426s
</code>```

Note that I have not done any explicit SIMD or loop-unrolling or used any intrinsics to form the dot-product. I suspect if you asked a question about forming a dot-product and tagged it with simd or avx, someone would help you optimise it.

Note also that you could easily run this code across multiple computers with GNU Parallel, assuming you have ssh login to them, just using:

```<code>parallel -S host1,host2,host3 ....
</code>```

For example, I have a 6-core Debian PC on my network, so I ran the above code parallelised across my 4-core Mac and 6-core Debian machine with:

```<code>parallel -S :,debian ...
</code>```

and it then takes 1 minute 8 seconds.

Body:
I have two files: A and B

A has 400,000 lines each having 50 float values
B has 40,000 lines having 50 float values.

For every line in B, I need to find corresponding lines in A which have >90% similarity (cosine).

For linear search and computation, the code takes ginormous computing time. (40-50 hours)

Reaching out to the community for suggestions on how to fasten the process (link of blogs/resources such as AWS/Cloud to be used to achieve it). Have been stuck with this for quite a while!

[There were mentions of rpud/rpudplus to do it, but can't seem to perform them on cloud resources]

N.B. as requested, the code for cosine similarity is:

for line1, line2 in zip(f1, f2):
    line1 = line1[:-1]
    cnt = cnt + 1
    l2X = [float(i) for i in line2.split()]
    f3 = open(enLabelValues, 'r')
    f4 = open(enVectorValues, 'r')
    print cnt
    cnt_j = 0
    for line3, line4 in zip(f3, f4):
        line3 = line3[:-1]
        l4X = [float(i) for i in line4.split()]
        ########This is the line for spatial cosine similarity
        result = 1 - spatial.distance.cosine(l2X, l4X)
        cnt_j = cnt_j + 1
        if(result > float(0.95)):
            if line3 not in a.keys():
                a[line3] = True
                fLabel_2.write(line3+"\n")
                fX_2.write(line4)
        fLabel_2.flush()
        fX_2.flush()
        os.fsync(fLabel_2.fileno())
        os.fsync(fX_2.fileno())

Body:
I can generate synthetic files of 40,000 and 400,000 lines with 50 samples per line and process them in around 2 minutes 18 seconds on a reasonable 4 core (+hyperthreading) desktop iMac in my clumsy style of C++ without any SIMD optimisation (by me) using GNU Parallel.

Here is the top-level script. You can see it generates the test data in "a.txt" and "b.txt". Then it "compresses" "b.txt" to an identical binary representation, with the pre-computed magnitude appended to each line. Finally, it numbers the lines in "a.txt" and passes them into GNU Parallel which splits the lines into groups of around 5,200 lines and starts a group of 8 parallel processes to compare each of those lines with the 40,000 lines in B.

#!/bin/bash

# Generate test data - a.txt b.txt
./generate

# Preprocess b.txt into binary with precomputed magitudes save as B
./preprocess

# Process file A in batches
cat -n a.txt | parallel --block-size 2M --line-buffer --pipe ./process {#}

Here is the generate.cpp program to synthesise data:

#include <iostream>
#include <cstdlib>
#include <fstream>
#include "common.h"

using namespace std;

int main()
{
   int line,sample;
   ofstream a("a.txt");
   if (!a.is_open()){
      cerr << "ERROR: Unable to open output file";
      exit(EXIT_FAILURE);
   }
   for(line=0;line<ALINES;line++){
      for(sample=0;sample<SAMPLESPERLINE;sample++){
         a << (float)rand()*100/RAND_MAX << " ";
      }
      a << endl;
   }
   a.close();
   ofstream b("b.txt");
   if (!b.is_open()){
      cerr << "ERROR: Unable to open output file";
      exit(EXIT_FAILURE);
   }
   for(line=0;line<BLINES;line++){
      for(sample=0;sample<SAMPLESPERLINE;sample++){
         b << (float)rand()*100/RAND_MAX << " ";
      }
      b << endl;
   }
   b.close();
}

Here is the preprocess.cpp code:

#include <sstream>
#include <fstream>
#include <string>
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <cmath>
#include "common.h"

int main(int argc, char* argv[]){

   std::ifstream btxt("b.txt");
   std::ofstream bbin("B",std::ios::out|std::ios::binary);
   if (!btxt.is_open()){
      std::cerr << "ERROR: Unable to open input file";
      exit(EXIT_FAILURE);
   }
   if (!bbin.is_open()){
      std::cerr << "ERROR: Unable to open output file";
      exit(EXIT_FAILURE);
   }

   int l=0;
   std::string line;
   std::vector<float> v;
   v.resize(SAMPLESPERLINE+1);
   while (std::getline(btxt,line)){
      std::istringstream iss(line);
      v.clear();
      float f;
      double magnitude;
      magnitude=0.0;
      int s=0;
      while (iss >> f){
         v[s]=(f);
         magnitude+=(double)f*f;
         s++;
      }
      // Append the magnitude to the end of the "line"
      v[s]=(float)sqrt(magnitude);
      // Write the samples and magnitide in binary to the output file
      bbin.write(reinterpret_cast<char*>(&v[0]),(SAMPLESPERLINE+1)*sizeof(float));
      l++;
   }
   btxt.close();
   bbin.close();

   return EXIT_SUCCESS;
}

Here is the common.h file:

const int ALINES=400000;
const int BLINES=40000;
const int SAMPLESPERLINE=50;

And here is the process.cpp code:

#include <sstream>
#include <fstream>
#include <string>
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <array>
#include <cmath>
#include "common.h"

int main(int argc, char* argv[]){

   if(argc!=2){
      std::cerr << "Usage: process JOBNUM" << std::endl;
      exit(1);
   }
   int JobNum=std::atoi(argv[1]);
   std::cerr << "Starting job: " << JobNum << std::endl;

   // Load B
   std::ifstream bbin("B",std::ios::binary);
   if (!bbin.is_open()){
      std::cerr << "ERROR: Unable to open B";
      exit(EXIT_FAILURE);
   }

   int l=0;
   std::array<float,SAMPLESPERLINE+1> record;
   std::vector<std::array<float,SAMPLESPERLINE+1>> B;
   B.resize(BLINES);
   for(l=0;l<BLINES;l++){
      // Read one record of 50 floats and their magnitude
      bbin.read(reinterpret_cast<char*>(&B[l][0]),sizeof(float)*(SAMPLESPERLINE+1));
   }
   bbin.close();

   // Process all lines read from stdin, each line prepended by its line number
   // Format is:
   // <line number in file "a.txt"> <SAMPLE0> <SAMPLE1> ... <SAMPLE49>
   int nLines=0;
   std::string line;
   while (std::getline(std::cin,line)){
      nLines++;
      std::istringstream iss(line);
      std::vector<float> A;
      A.resize(SAMPLESPERLINE);
      float f;
      int Alineno;
      int s=0;
      iss >> Alineno;
      double dMag=0.0;
      while (iss >> f){
         A[s++]=f;
         dMag+=(double)f*f;
      }
      // Root magnitude
      float AMagnitude=(float)sqrt(dMag);

      // At this point we have in B, 40,000 records each of 50 samples followed by the magnitude
      // ... and we have a single record from "a.txt" with 50 samples and its magnitude in AMagnitude
      // ... and Alineno is the absolute line number in "a.txt" of this line
      // Time to do the actual calculation: compare this record to all records in B
      for(int brec=0;brec<BLINES;brec++){
         float BMagnitude=B[brec][SAMPLESPERLINE];
         double dotproduct=0.0;
         float *a = &A[0];
         float *b = &B[brec][0];
         for(s=0;s<SAMPLESPERLINE;s++){
            dotproduct += (*a++) * (*b++);
         }
         float similarity = dotproduct/(AMagnitude*BMagnitude);
         if(similarity>0.99){
            std::cout << "Line A: " << Alineno << ", line B: " << brec << ", similarity:" << similarity << std::endl;
         }
      }
   }
   std::cerr << "Ending job: " << JobNum << ", processed " << nLines << " lines" << std::endl;

   return EXIT_SUCCESS;
}

The Makefile is pretty simple:

CFLAGS= -std=c++11 -O3 -march=native

all:    generate preprocess process

generate:   generate.cpp
        clang++ ${CFLAGS} generate.cpp -o generate

preprocess: preprocess.cpp
        clang++ ${CFLAGS} preprocess.cpp -o preprocess

process:    process.cpp
        clang++ ${CFLAGS} process.cpp -o process

When you run it, it pegs the CPU for 2 minutes and looks like this:

time ./go
Starting job: 3
Starting job: 7
Starting job: 8
Starting job: 2
Starting job: 5
Starting job: 1
Starting job: 4
Starting job: 6
Ending job: 1, processed 5204 lines
Starting job: 9
Ending job: 2, processed 5203 lines
Ending job: 3, processed 5204 lines
Starting job: 11
Starting job: 10
Ending job: 4, processed 5204 lines
Starting job: 12
Ending job: 5, processed 5203 lines
Ending job: 6, processed 5203 lines
Starting job: 14
Starting job: 13
...
...
Starting job: 75
Ending job: 68, processed 5204 lines
Ending job: 69, processed 5203 lines
Starting job: 76
Starting job: 77
Ending job: 70, processed 5203 lines
Ending job: 71, processed 5204 lines
Ending job: 72, processed 5203 lines
Ending job: 77, processed 4535 lines
Ending job: 74, processed 5204 lines
Ending job: 73, processed 5205 lines
Ending job: 75, processed 5204 lines
Ending job: 76, processed 5203 lines

real    2m17.510s
user    16m24.533s
sys     0m4.426s

Note that I have not done any explicit SIMD or loop-unrolling or used any intrinsics to form the dot-product. I suspect if you asked a question about forming a dot-product and tagged it with simd or avx, someone would help you optimise it.

Note also that you could easily run this code across multiple computers with GNU Parallel, assuming you have ssh login to them, just using:

parallel -S host1,host2,host3 ....

For example, I have a 6-core Debian PC on my network, so I ran the above code parallelised across my 4-core Mac and 6-core Debian machine with:

parallel -S :,debian ...

and it then takes 1 minute 8 seconds.


问:
Convert column into object in R
I wanna convert first column into object in R so if the input looks like:

```<code>#name of df - KPIF
    namePill KPIPill
1: pill_tech     0.3
2:  pill_EUX     0.2
3:  pill_Bus     0.3
</code>```

the output should looks like:

```<code>#name of df - KPIF
           KPIPill
pill_tech    0.3
pill_EUX     0.2
pill_Bus     0.3
</code>```

Anyone could help ?
Regards,
Aleksandra

答:
use first rownames()and then you can just get rid off the first column:

I was assuming you made your df like this:

df=as.data.frame(matrix(c('pill_tech','pill_EUX','pill_Bus',0.3,0.2,0.3),3,2))

otherwise you can just convert it to a data frame with as.data.frame(df)

then assign the column namePill to rownames()

rownames(df)=df$namePill

and then you can get rid off the column namePill if it bothers you

df=df[2]

Title: Convert column into object in R

Body:
I wanna convert first column into object in R so if the input looks like:

#name of df - KPIF
    namePill KPIPill
1: pill_tech     0.3
2:  pill_EUX     0.2
3:  pill_Bus     0.3

the output should looks like:

#name of df - KPIF
           KPIPill
pill_tech    0.3
pill_EUX     0.2
pill_Bus     0.3

Anyone could help ?
Regards,
Aleksandra


问:
Split collection into multiple collections
I want to create new list of list on basis of a single list.
I have a list like

```<code>public class OfficeLocator
{
    public String id{ get; set; }
    public string Geography{ get; set; }
    public string Country{ get; set; }
    public string State{ get; set; }
    public string OfficeName{ get; set; }
}
</code>```

I am trying to prepare an tree structured list

```<code>GeographyName="Asia",
{
   Country = "China",
   {
      State = "Hunan",
        {
            {
                OfficeId = "1",
                OfficeName = "Office 1"
            },
            {
                OfficeId = "2",
                OfficeName = "Office 2"
            }
        },
      State = "Hubei"
        {
            {
                OfficeId = "3",
                OfficeName = "Office 3"
            }
        }
    },
    Country = "India",
    {
      State = "Maharashtra",
        {
            {
                OfficeId = "4",
                OfficeName = "Office 4"
            },
            {
                OfficeId = "5",
                OfficeName = "Office 5"
            }
        },
      State = "Punjab"
        {
            {
                OfficeId = "6",
                OfficeName = "Office 6"
            }
        }
    },
},
GeographyName="Europe",
{
   Country = "UK",
   {
      State = "York",
        {
            {
                OfficeId = "7",
                OfficeName = "Office 7"
            },
            {
                OfficeId = "8",
                OfficeName = "Office 8"
            }
        }
    }
}
</code>```

I tried using some group by on Geography and Country.
But I am not getting the required output.

I can use looping logic to get the result, but I want to avoid it and try something with linq.

答:
GroupBy, has some overload that comes handy in those case:

```<code>var query = locations.GroupBy(
            x => x.Geography,
            (a, aGroup) => new
            {
                A = a,
                Items = aGroup.GroupBy(
                    x => x.Country,
                    (b, bGroup) => new
                    {
                        B = b,
                        Items = bGroup.GroupBy(
                            x => x.State,
                            (c, cGroup) => new
                            {
                                B = c,
                                Items = cGroup.Select(i => new {i.Id, i.OfficeName})
                            })
                    })
            });
</code>```

It's a little bit clearer to read and write.

The Anonimised object can be replace by your own class if needed.

Body:
I want to create new list of list on basis of a single list.
I have a list like

public class OfficeLocator
{
    public String id{ get; set; }
    public string Geography{ get; set; }
    public string Country{ get; set; }
    public string State{ get; set; }
    public string OfficeName{ get; set; }
}

I am trying to prepare an tree structured list

GeographyName="Asia",
{
   Country = "China",
   {
      State = "Hunan",
        {
            {
                OfficeId = "1",
                OfficeName = "Office 1"
            },
            {
                OfficeId = "2",
                OfficeName = "Office 2"
            }
        },
      State = "Hubei"
        {
            {
                OfficeId = "3",
                OfficeName = "Office 3"
            }
        }
    },
    Country = "India",
    {
      State = "Maharashtra",
        {
            {
                OfficeId = "4",
                OfficeName = "Office 4"
            },
            {
                OfficeId = "5",
                OfficeName = "Office 5"
            }
        },
      State = "Punjab"
        {
            {
                OfficeId = "6",
                OfficeName = "Office 6"
            }
        }
    },
},
GeographyName="Europe",
{
   Country = "UK",
   {
      State = "York",
        {
            {
                OfficeId = "7",
                OfficeName = "Office 7"
            },
            {
                OfficeId = "8",
                OfficeName = "Office 8"
            }
        }
    }
}

I tried using some group by on Geography and Country.
But I am not getting the required output.

I can use looping logic to get the result, but I want to avoid it and try something with linq.

Body:
GroupBy, has some overload that comes handy in those case:

var query = locations.GroupBy(
            x => x.Geography,
            (a, aGroup) => new
            {
                A = a,
                Items = aGroup.GroupBy(
                    x => x.Country,
                    (b, bGroup) => new
                    {
                        B = b,
                        Items = bGroup.GroupBy(
                            x => x.State,
                            (c, cGroup) => new
                            {
                                B = c,
                                Items = cGroup.Select(i => new {i.Id, i.OfficeName})
                            })
                    })
            });

It's a little bit clearer to read and write.

The Anonimised object can be replace by your own class if needed.


问:
Problems in sending email through gmail since past few days php codeigniter
I had a working code which was sending emails through the email->send function on Codeigniter.
Since yesterday, this code has just stopped working.

Following is the code that I am using

```<code>$config = Array(
        'protocol'  => 'smtp',
        'smtp_host' => 'smtp.gmail.com',
        'smtp_port' =>  465,
        'smtp_user' => 'xxxx@gmail.com',
        'smtp_pass' => 'xxxx',
        'mailtype'  => 'html',
        'charset'   => 'utf-8',
        'smtp_crypto' => "ssl"
    );

    $data = array(
   'userName'=> $firstName,
   'link'=>$url
     );

    $this->load->library('email', $config);
    $this->email->set_newline("\r\n");
    $this->email->set_mailtype("html");
    $this->email->from('finstlerforum@gmail.com', 'Finstler Support');
    $this->email->to($email);
    $this->email->subject('Reset Password requested');
    $body = $this->load->view('email-template/forgotpassword',$data,TRUE);
    $this->email->message($body);
    if (!$this->email->send())
    {
        // show_error($this->email->print_debugger());
        return false;
    }
    else
    {
        return true;
    }
</code>```

I am receiving the below error since yesterday

Severity: Warning Message: fsockopen(): SSL: Handshake timed out
  Filename: libraries/Email.php Line Number: 2055
Message:
  fsockopen(): Failed to enable crypto
 Message: fsockopen(): unable
  to connect to ssl://smtp.gmail.com:465 (Unknown error)

I have the openssl enabled on my machine and it correctly shows as enabled.
Any help is highly appreciated !

答:
Try using below config:  </p>

```<code>$config = Array(
    'protocol'  => 'smtp',
    'smtp_host' => 'smtp.gmail.com',
    'smtp_port' =>  587,
    'smtp_user' => 'xxxx@gmail.com',
    'smtp_pass' => 'xxxx',
    'mailtype'  => 'html',
    'charset'   => 'utf-8',
    'smtp_crypto' => "tls"
);
</code>```

Body:
I had a working code which was sending emails through the email->send function on Codeigniter.
Since yesterday, this code has just stopped working.

Following is the code that I am using

$config = Array(
        'protocol'  => 'smtp',
        'smtp_host' => 'smtp.gmail.com',
        'smtp_port' =>  465,
        'smtp_user' => 'xxxx@gmail.com',
        'smtp_pass' => 'xxxx',
        'mailtype'  => 'html',
        'charset'   => 'utf-8',
        'smtp_crypto' => "ssl"
    );

    $data = array(
   'userName'=> $firstName,
   'link'=>$url
     );

    $this->load->library('email', $config);
    $this->email->set_newline("\r\n");
    $this->email->set_mailtype("html");
    $this->email->from('finstlerforum@gmail.com', 'Finstler Support');
    $this->email->to($email);
    $this->email->subject('Reset Password requested');
    $body = $this->load->view('email-template/forgotpassword',$data,TRUE);
    $this->email->message($body);
    if (!$this->email->send())
    {
        // show_error($this->email->print_debugger());
        return false;
    }
    else
    {
        return true;
    }

I am receiving the below error since yesterday

Severity: Warning Message: fsockopen(): SSL: Handshake timed out
  Filename: libraries/Email.php Line Number: 2055
Message:
  fsockopen(): Failed to enable crypto
 Message: fsockopen(): unable
  to connect to ssl://smtp.gmail.com:465 (Unknown error)

I have the openssl enabled on my machine and it correctly shows as enabled.
Any help is highly appreciated !

Body:
Try using below config:

$config = Array(
    'protocol'  => 'smtp',
    'smtp_host' => 'smtp.gmail.com',
    'smtp_port' =>  587,
    'smtp_user' => 'xxxx@gmail.com',
    'smtp_pass' => 'xxxx',
    'mailtype'  => 'html',
    'charset'   => 'utf-8',
    'smtp_crypto' => "tls"
);


问:
mean squared error greater zero even though input equals output and weights initialized by one
I try to train a network where input = output, or more mathematical: f(x) = x <==> y = x <==> x = x

Therefore I have a input of size; [1, 500, 500, 3] and my network looks like this:

```<code>logits = tf.layers.conv2d(inputs=x, filters=3, kernel_size=1, padding='SAME', name ='logits', kernel_initializer=tf.ones_initializer(), trainable=True)
</code>```

Then I calculate the loss:

```<code>loss = tf.losses.mean_squared_error(x, logits) > 0
</code>```

but the network does not show the loss of 0.

In my opinion I thought if I set x = y the loss should be 0 when initializing the weights with ones.

Is there something wrong in my mind?

答:
The conv2d operation sums over the channels of your input. Thus, initialising the kernel with ones will give you identical output channels, each of which is the sum over your input channels.

Note that unless you set the bias_initializer to zero or set the use_bias parameter to False, the randomly-initialised bias will also affect your output.

```<code>import tensorflow as tf
import numpy as np

with tf.Graph().as_default():
    x = tf.placeholder(tf.float32, [None, 500, 500, 3])
    y = tf.layers.conv2d(x, 3, 1, padding='same', use_bias=False, kernel_initializer=tf.ones_initializer())
    sess = tf.Session()
    sess.run(tf.global_variables_initializer())

# Evaluate
np.random.seed(1)
_x = np.random.uniform(0, 1, (1, 500, 500, 3))
_y = sess.run(y, {x: _x})

# Check that the channels are identical
np.testing.assert_allclose(_y[..., 0], _y[..., 1])

# Check that each channel is the sum over channels
np.testing.assert_allclose(_y[..., 0], _x.sum(axis=-1), rtol=1e-5)
</code>```

Body:
I try to train a network where input = output, or more mathematical: f(x) = x <==> y = x <==> x = x

Therefore I have a input of size; [1, 500, 500, 3] and my network looks like this:

logits = tf.layers.conv2d(inputs=x, filters=3, kernel_size=1, padding='SAME', name ='logits', kernel_initializer=tf.ones_initializer(), trainable=True)

Then I calculate the loss:

loss = tf.losses.mean_squared_error(x, logits) > 0

but the network does not show the loss of 0.

In my opinion I thought if I set x = y the loss should be 0 when initializing the weights with ones.

Is there something wrong in my mind?

Body:
The conv2d operation sums over the channels of your input. Thus, initialising the kernel with ones will give you identical output channels, each of which is the sum over your input channels.

Note that unless you set the bias_initializer to zero or set the use_bias parameter to False, the randomly-initialised bias will also affect your output.

import tensorflow as tf
import numpy as np

with tf.Graph().as_default():
    x = tf.placeholder(tf.float32, [None, 500, 500, 3])
    y = tf.layers.conv2d(x, 3, 1, padding='same', use_bias=False, kernel_initializer=tf.ones_initializer())
    sess = tf.Session()
    sess.run(tf.global_variables_initializer())

# Evaluate
np.random.seed(1)
_x = np.random.uniform(0, 1, (1, 500, 500, 3))
_y = sess.run(y, {x: _x})

# Check that the channels are identical
np.testing.assert_allclose(_y[..., 0], _y[..., 1])

# Check that each channel is the sum over channels
np.testing.assert_allclose(_y[..., 0], _x.sum(axis=-1), rtol=1e-5)


问:
Text is not displaying as password
I have four EditTexts for password and when I enter the value it automatically moves to the next edittext.  I am following the How to change the focus to next edit text in android? , its working fine but after entering the digit in second text it is not changing as password , It displays like digit untill i touch the EditText field.
I have set android:inputType="numberPassword".

But it is showing the digit and when I touch the screen or when I back press all the fields display as password, it works as password.

But before touching or back pressing the edit text field it always displays the digit.

My requirement is that when I enter it it should display characters as password not digits, but before backpressing it displays as digits.

Please guide me.

where my xml is for one EditText is given below and there is four edit text.

```<code><EditText
  android:id="@+id/cnf_pin_first_edittext"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:contentDescription="@string/pin_content_desc"
  android:inputType="numberPassword"
  android:layout_marginRight="5dp"
 android:layout_marginLeft="5dp         android:background="@drawable/text_box_underline_selector"
 android:textColor="@color/black" />
</code>```

and my code for three Edit text is given below

```<code> final StringBuilder sbForFirstText = new StringBuilder();
    setPinFirstDigitEditText.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
            if (sbForFirstText.length() == 0 & setPinFirstDigitEditText.length() == 1) {
                sbForFirstText.append(s);
                setPinFirstDigitEditText.clearFocus();
                setPinSecondDigitEditText.requestFocus();
                setPinSecondDigitEditText.setCursorVisible(true);
            }
        }
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            if (sbForFirstText.length() == 1) {
                sbForFirstText.deleteCharAt(0);
            }
        }
        public void afterTextChanged(Editable s) {
            if (sbForFirstText.length() == 0) {
                setPinFirstDigitEditText.requestFocus();
            }
        }
    });

    final StringBuilder sbSecondText = new StringBuilder();
    setPinSecondDigitEditText.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
            if (sbSecondText.length() == 0 & setPinSecondDigitEditText.length() == 1) {
                sbSecondText.append(s);
                setPinSecondDigitEditText.clearFocus();
                setPinThirdDigitEditText.requestFocus();
                setPinThirdDigitEditText.setCursorVisible(true);
            }
        }
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            if (sbSecondText.length() == 1) {
                sbSecondText.deleteCharAt(0);
            }
        }
        public void afterTextChanged(Editable s) {
            if (sbSecondText.length() == 0) {
                setPinSecondDigitEditText.requestFocus();

            }
        }
    });

    final StringBuilder sbThirdText = new StringBuilder();
    setPinThirdDigitEditText.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
            if (sbThirdText.length() == 0 & setPinThirdDigitEditText.length() == 1) {
                sbThirdText.append(s);
                setPinThirdDigitEditText.clearFocus();
                setPinForthDigitEditText.requestFocus();
                setPinForthDigitEditText.setCursorVisible(true);
            }
        }
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            if (sbThirdText.length() == 1) {
                sbThirdText.deleteCharAt(0);
            }
        }
        public void afterTextChanged(Editable s) {
            if (sbThirdText.length() == 0) {
                setPinThirdDigitEditText.requestFocus();
            }
        }
    });
</code>```

答: Set android:inputType="textPassword"

Title: Text is not displaying as password

Body:
I have four EditTexts for password and when I enter the value it automatically moves to the next edittext.  I am following the How to change the focus to next edit text in android? , its working fine but after entering the digit in second text it is not changing as password , It displays like digit untill i touch the EditText field.
I have set android:inputType="numberPassword".

But it is showing the digit and when I touch the screen or when I back press all the fields display as password, it works as password.

But before touching or back pressing the edit text field it always displays the digit.

My requirement is that when I enter it it should display characters as password not digits, but before backpressing it displays as digits.

Please guide me.

where my xml is for one EditText is given below and there is four edit text.

<EditText
  android:id="@+id/cnf_pin_first_edittext"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:contentDescription="@string/pin_content_desc"
  android:inputType="numberPassword"
  android:layout_marginRight="5dp"
 android:layout_marginLeft="5dp         android:background="@drawable/text_box_underline_selector"
 android:textColor="@color/black" />

and my code for three Edit text is given below

 final StringBuilder sbForFirstText = new StringBuilder();
    setPinFirstDigitEditText.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
            if (sbForFirstText.length() == 0 & setPinFirstDigitEditText.length() == 1) {
                sbForFirstText.append(s);
                setPinFirstDigitEditText.clearFocus();
                setPinSecondDigitEditText.requestFocus();
                setPinSecondDigitEditText.setCursorVisible(true);
            }
        }
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            if (sbForFirstText.length() == 1) {
                sbForFirstText.deleteCharAt(0);
            }
        }
        public void afterTextChanged(Editable s) {
            if (sbForFirstText.length() == 0) {
                setPinFirstDigitEditText.requestFocus();
            }
        }
    });

    final StringBuilder sbSecondText = new StringBuilder();
    setPinSecondDigitEditText.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
            if (sbSecondText.length() == 0 & setPinSecondDigitEditText.length() == 1) {
                sbSecondText.append(s);
                setPinSecondDigitEditText.clearFocus();
                setPinThirdDigitEditText.requestFocus();
                setPinThirdDigitEditText.setCursorVisible(true);
            }
        }
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            if (sbSecondText.length() == 1) {
                sbSecondText.deleteCharAt(0);
            }
        }
        public void afterTextChanged(Editable s) {
            if (sbSecondText.length() == 0) {
                setPinSecondDigitEditText.requestFocus();

            }
        }
    });

    final StringBuilder sbThirdText = new StringBuilder();
    setPinThirdDigitEditText.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
            if (sbThirdText.length() == 0 & setPinThirdDigitEditText.length() == 1) {
                sbThirdText.append(s);
                setPinThirdDigitEditText.clearFocus();
                setPinForthDigitEditText.requestFocus();
                setPinForthDigitEditText.setCursorVisible(true);
            }
        }
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            if (sbThirdText.length() == 1) {
                sbThirdText.deleteCharAt(0);
            }
        }
        public void afterTextChanged(Editable s) {
            if (sbThirdText.length() == 0) {
                setPinThirdDigitEditText.requestFocus();
            }
        }
    });

Body: Set android:inputType="textPassword"


问:
Angularjs save variables $routeProvider
There's a way to save variables inside the $routeProvider?

Ex: I was trying to set the aside menu when a reach a certain page.
The idea is do like this:

```<code>$routeProvider.when('/', {
    title: 'Home',
    controller: 'Home',
    variable: 'active'
});
</code>```

And in the HTML:

```<code><a href="/Home" class="{{$route.variable}}">
</code>```

Thanks!

答:
You said you are already setting a static property in the route object.

All you need to do is in your HomeController make a variable which can be access in html, i.e.

```<code>$scope.route = $route;
//or
vm.route = $route;
</code>```

So the HomeController will now look like :

```<code>app.controller('HomeController', ['$scope', '$routeParams', function ($scope, $route) {
 $scope.route = $route;
}
</code>```

and the HTML would just have a small change, i.e.

```<code><a href="/Home" class="{{route.current.variable}}">
</code>```

and its done!

Title: Angularjs save variables $routeProvider

Body:
There's a way to save variables inside the $routeProvider?

Ex: I was trying to set the aside menu when a reach a certain page.
The idea is do like this:

$routeProvider.when('/', {
    title: 'Home',
    controller: 'Home',
    variable: 'active'
});

And in the HTML:

Thanks!

Body:
You said you are already setting a static property in the route object.

All you need to do is in your HomeController make a variable which can be access in html, i.e.

$scope.route = $route;
//or
vm.route = $route;

So the HomeController will now look like :

app.controller('HomeController', ['$scope', '$routeParams', function ($scope, $route) {
 $scope.route = $route;
}

and the HTML would just have a small change, i.e.

<a href="/Home" class="{{route.current.variable}}">

and its done!


问:
Magento Product rating calculation performance
If my web pages show many products (include title and rating), then web page needs to calculate rating percentage by every products with following code.
I am sure this kinds of calculation will slow down my web page.
Is this the only choice that i can choose, or is there any better performance code that i can use?</p>

```<code>            if (count($reviews) > 0)
            {
                foreach ($reviews->getItems() as $review)
                {
                    foreach( $review->getRatingVotes() as $vote )
                    {
                        $ratings[] = $vote->getPercent();
                    }
                }
                $avg = array_sum($ratings)/count($ratings);
            }
</code>```

答:
you can used this code, magento use same code on category list page.

```<code><?php if($_product->getRatingSummary()): ?>
      <?php echo $this->getReviewsSummaryHtml($_product) ?>
<?php endif; ?>
</code>```

You need to just pass your $product object in this code.

Body:
If my web pages show many products (include title and rating), then web page needs to calculate rating percentage by every products with following code.
I am sure this kinds of calculation will slow down my web page.
Is this the only choice that i can choose, or is there any better performance code that i can use?

            if (count($reviews) > 0)
            {
                foreach ($reviews->getItems() as $review)
                {
                    foreach( $review->getRatingVotes() as $vote )
                    {
                        $ratings[] = $vote->getPercent();
                    }
                }
                $avg = array_sum($ratings)/count($ratings);
            }

Body:
you can used this code, magento use same code on category list page.

<?php if($_product->getRatingSummary()): ?>
      <?php echo $this->getReviewsSummaryHtml($_product) ?>
<?php endif; ?>

You need to just pass your $product object in this code.


问:
adding same class to dynamic created elements with javascript or php

Hello!

So for each category in an online shop i have a page with every subcategory included and a table with products.
I have this table with sorting buttons on top for each subcategory of products. What I want is to put the same color as the buttons to each row(product) in the table.
So where the product is from the Display subcategory the color should be green and so on.

This needs to be done dynamically because I have more pages like that, created from the database.
Another page

To add the subcategory colors I'm using a simple

```<code>$class++
</code>```

in the while loop. with css classes created like

```<code>.col1{
   color: #3e8f3e;
}
.col2{
   color: #e38d13;
}
.col3{
   color: #b92c28;
}
.col4{
   color: #DC4C46;
}
.col5{
   color: #672E3B;
}
.col6{
   color: #F3D6E4;
}
.col7{
   color: #C48F65;
}
.col8{
   color: #223A5E;
}
.col9{
   color: #898E8C;
}
.col10{
   color: #005960;
}
.col11{
   color: #9C9A40;
}
.col12{
   color: #4F84C4;
}
</code>```

I used the subcatergory name as the id of the button.
Code for button:

```<code><li>
    <a type="button" class="btn-filter" id="'.$row['subcat_pro'].'" href="#go" title="'.$row['subcat_pro'].'" data-target="'.$row['subcat_pro'].'">'.$row['subcat_pro'].'</a><hr class="btn'.$class++.'">
</li>';
</code>```

Code for the  where the color should be:

```<code><span id="sub'.$row['subcat_pro'] .'" class="pull-right">('. $row['subcat_pro'] .') (SKU: '. $row['sku'] .')</span>
</code>```

I really have no idea of what can be done.

Thanks in advance for any help!

答:
honestly, i would put that color IN your "categories" or "subcategories" table, like, i would create a colunm called "color" and then when you are creating your table you could do something like this:

```<code><?php
    $categories = $db ... // get your categories here using a query

    foreach($categories as $category) {
          <span name="category_{$category['id']}" style="color:{$category['color']}" ><span>
    }
?>
</code>```

But, if you don't like styles in-line on your HTML, like i don't, you will have to define all those colors as classes on your CSS file, then, you would build that html using those classes.

Let me know if you want a example for the second option (which is pretty hard work).

Body:
Hello!

So for each category in an online shop i have a page with every subcategory included and a table with products.
I have this table with sorting buttons on top for each subcategory of products. What I want is to put the same color as the buttons to each row(product) in the table.
So where the product is from the Display subcategory the color should be green and so on.

This needs to be done dynamically because I have more pages like that, created from the database.
Another page

To add the subcategory colors I'm using a simple

$class++

in the while loop. with css classes created like

.col1{
   color: #3e8f3e;
}
.col2{
   color: #e38d13;
}
.col3{
   color: #b92c28;
}
.col4{
   color: #DC4C46;
}
.col5{
   color: #672E3B;
}
.col6{
   color: #F3D6E4;
}
.col7{
   color: #C48F65;
}
.col8{
   color: #223A5E;
}
.col9{
   color: #898E8C;
}
.col10{
   color: #005960;
}
.col11{
   color: #9C9A40;
}
.col12{
   color: #4F84C4;
}

I used the subcatergory name as the id of the button.
Code for button:

    '.$row['subcat_pro'].'

';

Code for the  where the color should be:

('. $row['subcat_pro'] .') (SKU: '. $row['sku'] .')

I really have no idea of what can be done.

Thanks in advance for any help!

Body:
honestly, i would put that color IN your "categories" or "subcategories" table, like, i would create a colunm called "color" and then when you are creating your table you could do something like this:

<?php
    $categories = $db ... // get your categories here using a query

    foreach($categories as $category) {
          <span name="category_{$category['id']}" style="color:{$category['color']}" ><span>
    }
?>

But, if you don't like styles in-line on your HTML, like i don't, you will have to define all those colors as classes on your CSS file, then, you would build that html using those classes.

Let me know if you want a example for the second option (which is pretty hard work).


问:
How to show a specific unit of data from a record queried in rails?
I am really stuck with dealing with querys from multiple tables in rails. If this is my controller:

```<code> def show
    @user = User.find(params[:id])
    @entry = Entry.joins(:event, :user).where(users: { id: '2' })
    @event = Event.find(1)
  end
</code>```

and this is my models

```<code>class Entry < ApplicationRecord
  belongs_to :user
  belongs_to :event

class User < ApplicationRecord
   has_many :entries, dependent: :destroy

class Event < ApplicationRecord
    has_many :entries, dependent: :destroy
</code>```

The query runs happily in rails console and returns the records but I don't know how to access specific units like entry.course in my embeded ruby.

Update

To clarify On a page I woulk like to output somthing for all the entries assosiated with the user logged in to the page.

eventName eventLocation eventDate course siCard

The schema:

```<code>ActiveRecord::Schema.define(version: 20171204183458) do

    # These are extensions that must be enabled in order to support this
  database
  enable_extension "plpgsql"

create_table "entries", force: :cascade do |t|
  t.string "course"
  t.string "siCard"
  t.bigint "user_id"
  t.bigint "event_id"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.index ["event_id"], name: "index_entries_on_event_id"
  t.index ["user_id", "created_at"], name:
  "index_entries_on_user_id_and_created_at"
  t.index ["user_id"], name: "index_entries_on_user_id"
 end

create_table "events", force: :cascade do |t|
 t.string "eventName"
 t.string "location"
 t.date "eventDate"
 t.text "description"
 t.datetime "created_at", null: false
 t.datetime "updated_at", null: false
 t.string "eventLink"
 t.string "eventCoordinates"
 t.index ["eventName"], name: "index_events_on_eventName", unique: true
end

create_table "users", force: :cascade do |t|
  t.string "name"
  t.string "email"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.string "password_digest"
  t.string "firstName"
  t.string "remember_digest"
  t.boolean "admin"
  t.index ["email"], name: "index_users_on_email", unique: true
end

add_foreign_key "entries", "events"
add_foreign_key "entries", "users"
end
</code>```

答:
Why are you using such a complicated query with joins? If you are looking for the Entrys of @user just use @user.entries. ActiveRecord knows from the has_many how to deal with that.

If you posted a simplified example, you should try to find your Entry with something like .where(user_id: user.id) as long as id is the foreign key. With that you don't need that join, too.

Body:
I am really stuck with dealing with querys from multiple tables in rails. If this is my controller:

 def show
    @user = User.find(params[:id])
    @entry = Entry.joins(:event, :user).where(users: { id: '2' })
    @event = Event.find(1)
  end

and this is my models

class Entry < ApplicationRecord
  belongs_to :user
  belongs_to :event

class User < ApplicationRecord
   has_many :entries, dependent: :destroy

class Event < ApplicationRecord
    has_many :entries, dependent: :destroy

The query runs happily in rails console and returns the records but I don't know how to access specific units like entry.course in my embeded ruby.

Update

To clarify On a page I woulk like to output somthing for all the entries assosiated with the user logged in to the page.

eventName eventLocation eventDate course siCard

The schema:

ActiveRecord::Schema.define(version: 20171204183458) do

    # These are extensions that must be enabled in order to support this
  database
  enable_extension "plpgsql"

create_table "entries", force: :cascade do |t|
  t.string "course"
  t.string "siCard"
  t.bigint "user_id"
  t.bigint "event_id"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.index ["event_id"], name: "index_entries_on_event_id"
  t.index ["user_id", "created_at"], name:
  "index_entries_on_user_id_and_created_at"
  t.index ["user_id"], name: "index_entries_on_user_id"
 end

create_table "events", force: :cascade do |t|
 t.string "eventName"
 t.string "location"
 t.date "eventDate"
 t.text "description"
 t.datetime "created_at", null: false
 t.datetime "updated_at", null: false
 t.string "eventLink"
 t.string "eventCoordinates"
 t.index ["eventName"], name: "index_events_on_eventName", unique: true
end

create_table "users", force: :cascade do |t|
  t.string "name"
  t.string "email"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.string "password_digest"
  t.string "firstName"
  t.string "remember_digest"
  t.boolean "admin"
  t.index ["email"], name: "index_users_on_email", unique: true
end

add_foreign_key "entries", "events"
add_foreign_key "entries", "users"
end


问:
django rest framework, not serializable
I am new at using the django REST framework and I am having trouble to import a list of the members of a team

my query is

```<code>team_name_list = Project.objects.get(id=kwargs['pk1']).team_id.members.all()
</code>```

which give an output :

```<code><QuerySet [<MyUser: johndoe@gmail.com>, <MyUser: johndoe2@gmail.com>, <MyUser: johndoe3@gmail.com>, <MyUser: johndoe4@gmail.com>]>
</code>```

The thing is when I try to import it in my context I get an error

```<code>Object of type 'MyUser' is not JSON serializable
</code>```

I guess because the output of my query should be something ["johndoe@gmail.com","johndoe2@gmail.com","johndoe3@gmail.com","johndoe4@gmail.com"]

How can I make it work

Edited work: using serializer

I created serializer.py file that I imported in my views so now I do not get the error "not serializable" but my data does not showed up in my endpoint or in my inspect/console

i am I missing something ?

```<code>from rest_framework.serializers import ModelSerializer

from registration.models import MyUser
from website.models import Team,Project

class MyUserSerializer(ModelSerializer):
    class Meta:
        model = MyUser
        fields = [
            'email',
            'first_name',
            'last_name'
        ]
</code>```

Views:

from website.serializers import MyUserSerializer

I have a view that is linked my employe_detail_page.html

```<code>class EmployeeDetailView(generic.DetailView, LoginRequiredMixin):
    #import pdb; pdb.set_trace()
    model = MyUser
    template_name = 'Employee_Details.html'

    def get_object(self, queryset=None):
        return get_object_or_404(MyUser, pk=self.kwargs['pk2'], members__project=self.kwargs['pk1'])

    def get_context_data(self, **kwargs):
        context = super(EmployeeDetailView, self).get_context_data(**kwargs)
        employee_name = MyUser.objects.get(id=self.kwargs['pk2'])
        team_list = Project.objects.get(id=self.kwargs['pk1']).team_id.members.all()
        team_list_pop = Project.objects.get(id=self.kwargs['pk1']).team_id.members.all().exclude(id=self.kwargs['pk2'])
        context={
            'employee_name' : employee_name,
            'team_list' : team_list,
            'team_list_pop' : team_list_pop
        }
        return context
</code>```

and in the template I include a dashboard.html using chartJS and this view

```<code>class EmployeeChartData(APIView):
    #import pdb; pdb.set_trace()
    authentication_classes = []
    permission_classes = []

    serializer_class = MyUserSerializer

    def get(self, request, format=None, *args, **kwargs):

        project_name = Project.objects.get(id=kwargs['pk1']).name
        team_name_list = Project.objects.get(id=kwargs['pk1']).team_id.members.all()
        serializer = self.get_serializer_class()
        team_name_data = serializer(team_name_list)
        team_member_count = Project.objects.get(id=kwargs['pk1']).team_id.members.count()
        main_items2 = [1,2,3,-4,5,6,-7,8,9,-10,-11,12,13,14,15,16]
        info_process_data = [4,6,2,8,4]
        info_process_data2 = [8,2,5,2,4]
        action_process_data = [5,3,9]
        motivation_data = [4,5,1,8]
        behaviour_data = [6,3,9,1]

        data = {
            #labels

            "labels_main_graph":labels_main_graph,
            "information_processing_label": information_processing_label,
            "action_process_label": action_process_label,
            "motivation_label": motivation_label,
            "behaviour_label":behaviour_label,
            #data

            "main2": main_items2,
            "info_process_data": info_process_data,
            "info_process_data2": info_process_data2,
            "action_process_data": action_process_data,
            "motivation_data":motivation_data,
            "behaviour_data":behaviour_data,
            #other
            "project_name":project_name,
            "team_member_count":team_member_count,
            "team_name_list":team_name_data

        }
        return Response(data)
</code>```

答:
before returning response serialize your team_name_list

```<code>    team_name_list = Project.objects.get(id=kwargs['pk1']).team_id.members.all()
    serializer = self.get_serializer_class()
    team_name_data = serializer(team_name_list)
</code>```

then in data dict:

```<code>    "team_name_list":team_name_data
</code>```

Title: django rest framework, not serializable

Body:
I am new at using the django REST framework and I am having trouble to import a list of the members of a team

my query is

team_name_list = Project.objects.get(id=kwargs['pk1']).team_id.members.all()

which give an output :

<QuerySet [<MyUser: johndoe@gmail.com>, <MyUser: johndoe2@gmail.com>, <MyUser: johndoe3@gmail.com>, <MyUser: johndoe4@gmail.com>]>

The thing is when I try to import it in my context I get an error

Object of type 'MyUser' is not JSON serializable

I guess because the output of my query should be something ["johndoe@gmail.com","johndoe2@gmail.com","johndoe3@gmail.com","johndoe4@gmail.com"]

How can I make it work

Edited work: using serializer

I created serializer.py file that I imported in my views so now I do not get the error "not serializable" but my data does not showed up in my endpoint or in my inspect/console

i am I missing something ?

from rest_framework.serializers import ModelSerializer

from registration.models import MyUser
from website.models import Team,Project

class MyUserSerializer(ModelSerializer):
    class Meta:
        model = MyUser
        fields = [
            'email',
            'first_name',
            'last_name'
        ]

Views:

from website.serializers import MyUserSerializer

I have a view that is linked my employe_detail_page.html

class EmployeeDetailView(generic.DetailView, LoginRequiredMixin):
    #import pdb; pdb.set_trace()
    model = MyUser
    template_name = 'Employee_Details.html'

    def get_object(self, queryset=None):
        return get_object_or_404(MyUser, pk=self.kwargs['pk2'], members__project=self.kwargs['pk1'])

    def get_context_data(self, **kwargs):
        context = super(EmployeeDetailView, self).get_context_data(**kwargs)
        employee_name = MyUser.objects.get(id=self.kwargs['pk2'])
        team_list = Project.objects.get(id=self.kwargs['pk1']).team_id.members.all()
        team_list_pop = Project.objects.get(id=self.kwargs['pk1']).team_id.members.all().exclude(id=self.kwargs['pk2'])
        context={
            'employee_name' : employee_name,
            'team_list' : team_list,
            'team_list_pop' : team_list_pop
        }
        return context

and in the template I include a dashboard.html using chartJS and this view

class EmployeeChartData(APIView):
    #import pdb; pdb.set_trace()
    authentication_classes = []
    permission_classes = []

    serializer_class = MyUserSerializer

    def get(self, request, format=None, *args, **kwargs):

        project_name = Project.objects.get(id=kwargs['pk1']).name
        team_name_list = Project.objects.get(id=kwargs['pk1']).team_id.members.all()
        serializer = self.get_serializer_class()
        team_name_data = serializer(team_name_list)
        team_member_count = Project.objects.get(id=kwargs['pk1']).team_id.members.count()
        main_items2 = [1,2,3,-4,5,6,-7,8,9,-10,-11,12,13,14,15,16]
        info_process_data = [4,6,2,8,4]
        info_process_data2 = [8,2,5,2,4]
        action_process_data = [5,3,9]
        motivation_data = [4,5,1,8]
        behaviour_data = [6,3,9,1]

        data = {
            #labels

            "labels_main_graph":labels_main_graph,
            "information_processing_label": information_processing_label,
            "action_process_label": action_process_label,
            "motivation_label": motivation_label,
            "behaviour_label":behaviour_label,
            #data

            "main2": main_items2,
            "info_process_data": info_process_data,
            "info_process_data2": info_process_data2,
            "action_process_data": action_process_data,
            "motivation_data":motivation_data,
            "behaviour_data":behaviour_data,
            #other
            "project_name":project_name,
            "team_member_count":team_member_count,
            "team_name_list":team_name_data

        }
        return Response(data)

Body:
before returning response serialize your team_name_list

    team_name_list = Project.objects.get(id=kwargs['pk1']).team_id.members.all()
    serializer = self.get_serializer_class()
    team_name_data = serializer(team_name_list)

then in data dict:

    "team_name_list":team_name_data


问:
Spring Boot Security not ignoring directory using WebSecurity

I am trying to ignore directory using websecurity.ignore. I have the following configuration.
</p>

```<code>    @Override
    protected void configure(HttpSecurity http) throws Exception {
            http
                .authorizeRequests()
                    .antMatchers("/", "/home").permitAll()
                    .antMatchers("/product_save").hasAuthority("ADMIN")
                    .anyRequest().authenticated()
                    .and()
                .formLogin()
                    .loginPage("/login")
                    .permitAll()
                    .and()
                .logout()
                    .permitAll().and().csrf().disable()
                    .exceptionHandling().and()
                    .rememberMe()
                        .key("uniqueAndSecret")
                        .rememberMeCookieName("broman-remember-me")
                        .tokenValiditySeconds(60 * 60 * 24).and()
                    .sessionManagement().maximumSessions(1);

    }

    @Override
    public void configure(WebSecurity web)throws Exception {
        web.ignoring().antMatchers("/static/**");
    }
}
</code>```

<p>The above configuration doesn't ignore.
</p>

答:
Your code:

```<code>@Override
public void configure(WebSecurity web)throws Exception {
    web.ignoring().antMatchers("/static/**");
}
</code>```

will ignore all URLs that start with /static/** like http://localhost:8080/static/...

Spring Boot serves the content of the static folder by default. So if you have some folders or files in the static folder (like static resources related to customers and managers, etc..) you can ignore them one by one:

```<code>@Override
public void configure(WebSecurity web)throws Exception {
    web.ignoring().antMatchers("/customers/**", "/managers/**", "/monitor.html");
}
</code>```

This will ignore:

http://localhost:8080/customers/ - and all sub-URLs

http://localhost:8080/managers/ - and all sub-URLs

http://localhost:8080/monitor.html

Body:
I am trying to ignore directory using websecurity.ignore. I have the following configuration.

    @Override
    protected void configure(HttpSecurity http) throws Exception {
            http
                .authorizeRequests()
                    .antMatchers("/", "/home").permitAll()
                    .antMatchers("/product_save").hasAuthority("ADMIN")
                    .anyRequest().authenticated()
                    .and()
                .formLogin()
                    .loginPage("/login")
                    .permitAll()
                    .and()
                .logout()
                    .permitAll().and().csrf().disable()
                    .exceptionHandling().and()
                    .rememberMe()
                        .key("uniqueAndSecret")
                        .rememberMeCookieName("broman-remember-me")
                        .tokenValiditySeconds(60 * 60 * 24).and()
                    .sessionManagement().maximumSessions(1);

    }

    @Override
    public void configure(WebSecurity web)throws Exception {
        web.ignoring().antMatchers("/static/**");
    }
}

The above configuration doesn't ignore.

Body:
Your code:

@Override
public void configure(WebSecurity web)throws Exception {
    web.ignoring().antMatchers("/static/**");
}

will ignore all URLs that start with /static/** like http://localhost:8080/static/...

Spring Boot serves the content of the static folder by default. So if you have some folders or files in the static folder (like static resources related to customers and managers, etc..) you can ignore them one by one:

@Override
public void configure(WebSecurity web)throws Exception {
    web.ignoring().antMatchers("/customers/**", "/managers/**", "/monitor.html");
}

This will ignore:

http://localhost:8080/customers/ - and all sub-URLs

http://localhost:8080/managers/ - and all sub-URLs

http://localhost:8080/monitor.html