问:
Accounting for the margin space in RecyclerView when using onSwipe
I have a RecyclerView that displays names. That looks like this

The white space between the items is the margin. I followed a guide to allow the user to swipe to delete the names. The only problem is that the red background when deleting overlaps the margin also where I only want it to be equal with the blue item. This Gif explains it better:

Recyler view layout

```<code><androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/myCoordinatorLayoutStats"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".StatisticsActivity">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_playerNamesStats"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_selectPlayerStats" />

    <TextView
        android:id="@+id/tv_selectPlayerStats"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:gravity="center"
        android:text="@string/selectPlayerStats"
        android:textAlignment="center"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
</code>```

Deleting code

And here is the code where the bounds is set for deleting an item:

```<code>@Override
public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
    super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);

    View itemView = viewHolder.itemView;
    int itemHeight = itemView.getHeight();

    boolean isCancelled = dX == 0 && !isCurrentlyActive;

    if (isCancelled) {
        clearCanvas(c, itemView.getRight() + dX, (float) itemView.getTop(), (float) itemView.getRight(), (float) itemView.getBottom());
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
        return;
    }

    mBackground.setColor(backgroundColor);
    mBackground.setBounds(itemView.getRight() + (int) dX, itemView.getTop(), itemView.getRight(), itemView.getBottom());
    mBackground.draw(c);

    int deleteIconTop = itemView.getTop() + (itemHeight - intrinsicHeight) / 2;
    int deleteIconMargin = (itemHeight - intrinsicHeight) / 2;
    int deleteIconLeft = itemView.getRight() - deleteIconMargin - intrinsicWidth;
    int deleteIconRight = itemView.getRight() - deleteIconMargin;
    int deleteIconBottom = deleteIconTop + intrinsicHeight;

    deleteDrawable.setBounds(deleteIconLeft, deleteIconTop, deleteIconRight, deleteIconBottom - 6);
    deleteDrawable.draw(c);

    super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);

}
</code>```

What do I need to adjust to avoid the margin getting  overlapped?

EDIT:

Recyler view item

```<code><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
    android:id="@+id/tv_usernameStatsAdapter"
    style="@style/users_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorAccent"
    android:padding="8dp"
    android:maxLines="1"
    android:textColor="@color/whiteText"/>
</LinearLayout>
</code>```

Updated gif

答:
The white space is not the margin but the padding of your TextView.

So, you can do it like this

```<code>@Override
public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
    super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);

    View itemView = viewHolder.itemView;
    //**********************
    TextView txt = itemView.findViewById(R.id.tv_usernameStatsAdapter);
    //*******************************
    int itemHeight = itemView.getHeight();

    boolean isCancelled = dX == 0 && !isCurrentlyActive;

    if (isCancelled) {
        clearCanvas(c, itemView.getRight() + dX, (float) itemView.getTop(), (float) itemView.getRight(), (float) itemView.getBottom());
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
        return;
    }

    mBackground.setColor(backgroundColor);
    // ******************
    mBackground.setBounds(itemView.getRight() + (int) dX + txt.getPaddingStart(), itemView.getTop() + txt.getPaddingTop(), itemView.getRight() - txt.getPaddingEnd(), itemView.getBottom() - txt.getPaddingBottom());
    //**************************
    mBackground.draw(c);

    int deleteIconTop = itemView.getTop() + (itemHeight - intrinsicHeight) / 2;
    int deleteIconMargin = (itemHeight - intrinsicHeight) / 2;
    int deleteIconLeft = itemView.getRight() - deleteIconMargin - intrinsicWidth;
    int deleteIconRight = itemView.getRight() - deleteIconMargin;
    int deleteIconBottom = deleteIconTop + intrinsicHeight;

    deleteDrawable.setBounds(deleteIconLeft, deleteIconTop, deleteIconRight, deleteIconBottom - 6);
    deleteDrawable.draw(c);

    super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);

}
</code>```

Of course, this solution is only valid if using with your particular item layout

Body:
I have a RecyclerView that displays names. That looks like this

The white space between the items is the margin. I followed a guide to allow the user to swipe to delete the names. The only problem is that the red background when deleting overlaps the margin also where I only want it to be equal with the blue item. This Gif explains it better:

Recyler view layout

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/myCoordinatorLayoutStats"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".StatisticsActivity">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_playerNamesStats"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_selectPlayerStats" />

    <TextView
        android:id="@+id/tv_selectPlayerStats"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:gravity="center"
        android:text="@string/selectPlayerStats"
        android:textAlignment="center"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Deleting code

And here is the code where the bounds is set for deleting an item:

@Override
public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
    super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);

    View itemView = viewHolder.itemView;
    int itemHeight = itemView.getHeight();

    boolean isCancelled = dX == 0 && !isCurrentlyActive;

    if (isCancelled) {
        clearCanvas(c, itemView.getRight() + dX, (float) itemView.getTop(), (float) itemView.getRight(), (float) itemView.getBottom());
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
        return;
    }

    mBackground.setColor(backgroundColor);
    mBackground.setBounds(itemView.getRight() + (int) dX, itemView.getTop(), itemView.getRight(), itemView.getBottom());
    mBackground.draw(c);

    int deleteIconTop = itemView.getTop() + (itemHeight - intrinsicHeight) / 2;
    int deleteIconMargin = (itemHeight - intrinsicHeight) / 2;
    int deleteIconLeft = itemView.getRight() - deleteIconMargin - intrinsicWidth;
    int deleteIconRight = itemView.getRight() - deleteIconMargin;
    int deleteIconBottom = deleteIconTop + intrinsicHeight;

    deleteDrawable.setBounds(deleteIconLeft, deleteIconTop, deleteIconRight, deleteIconBottom - 6);
    deleteDrawable.draw(c);

    super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);

}

What do I need to adjust to avoid the margin getting  overlapped?

EDIT:

Recyler view item

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
    android:id="@+id/tv_usernameStatsAdapter"
    style="@style/users_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorAccent"
    android:padding="8dp"
    android:maxLines="1"
    android:textColor="@color/whiteText"/>
</LinearLayout>

Updated gif

Body:
The white space is not the margin but the padding of your TextView.

So, you can do it like this

@Override
public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
    super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);

    View itemView = viewHolder.itemView;
    //**********************
    TextView txt = itemView.findViewById(R.id.tv_usernameStatsAdapter);
    //*******************************
    int itemHeight = itemView.getHeight();

    boolean isCancelled = dX == 0 && !isCurrentlyActive;

    if (isCancelled) {
        clearCanvas(c, itemView.getRight() + dX, (float) itemView.getTop(), (float) itemView.getRight(), (float) itemView.getBottom());
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
        return;
    }

    mBackground.setColor(backgroundColor);
    // ******************
    mBackground.setBounds(itemView.getRight() + (int) dX + txt.getPaddingStart(), itemView.getTop() + txt.getPaddingTop(), itemView.getRight() - txt.getPaddingEnd(), itemView.getBottom() - txt.getPaddingBottom());
    //**************************
    mBackground.draw(c);

    int deleteIconTop = itemView.getTop() + (itemHeight - intrinsicHeight) / 2;
    int deleteIconMargin = (itemHeight - intrinsicHeight) / 2;
    int deleteIconLeft = itemView.getRight() - deleteIconMargin - intrinsicWidth;
    int deleteIconRight = itemView.getRight() - deleteIconMargin;
    int deleteIconBottom = deleteIconTop + intrinsicHeight;

    deleteDrawable.setBounds(deleteIconLeft, deleteIconTop, deleteIconRight, deleteIconBottom - 6);
    deleteDrawable.draw(c);

    super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);

}

Of course, this solution is only valid if using with your particular item layout


来源: stackexchange

问:
How to read filtered rows from excel using POI library
I am reading excel file using POI library in my java code. So far fine. But now I have one requirement. The excel file contains many records (e.g. 1000 rows). It also has column headers (1st row). Now I am doing excel filtering on it. Say I have one 'year' column and I am filtering all rows for year=2019. I get 15 rows.
Question: I want to process only these 15 rows in my java code. Is there any method in poi library or way to know if the row being read is filtered or (the other way i.e. not filtered).
Thanks.

I already have working code but right now I am looking for how to read only filtered row. Nothing new tried yet other than searching in library and forums.

The below code is inside a method. I am not used to formatting with stackoverflow so kindly ignore any formatting issue.

```<code>    // For storing data into CSV files
    StringBuffer data = new StringBuffer();
    try {
        SimpleDateFormat dtFormat = new SimpleDateFormat(CommonConstants.YYYY_MM_DD); // "yyyy-MM-dd"
        String doubleQuotes = "\"";
        FileOutputStream fos = new FileOutputStream(outputFile);
        // Get the workbook object for XLSX file
        XSSFWorkbook wBook = new XSSFWorkbook(new FileInputStream(inputFile));
        wBook.setMissingCellPolicy(Row.RETURN_BLANK_AS_NULL);

        // Get first sheet from the workbook
        //XSSFSheet sheet = wBook.getSheetAt(0);
        XSSFSheet sheet = wBook.getSheet(CommonConstants.METADATA_WORKSHEET);
        //Row row;
        //Cell cell;
        // Iterate through each rows from first sheet
        int rows = sheet.getLastRowNum();
        int totalRows = 0;
        int colTitelNumber = 0;
        Row firstRowRecord = sheet.getRow(1);
        for (int cn = 0; cn < firstRowRecord.getLastCellNum(); cn++) {
            Cell cellObj = firstRowRecord.getCell(cn);
            if(cellObj != null) {
                String str = cellObj.toString();
                if(CommonConstants.COLUMN_TITEL.equalsIgnoreCase(str)) {
                    colTitelNumber = cn;
                    break;
                }
            }
        }
        // Start with row Number 1. We don't need 0th number row as it is for Humans to read but not required for processing.
        for (int rowNumber = 1; rowNumber <= rows; rowNumber++) {
            StringBuffer rowData = new StringBuffer();
            boolean skipRow = false;
            Row rowRecord = sheet.getRow(rowNumber);
            if (rowRecord == null) {
                LOG.error("Empty/Null record found");
            } else {
                for (int cn = 0; cn < rowRecord.getLastCellNum(); cn++) {
                    Cell cellObj = rowRecord.getCell(cn);
                    if(cellObj == null) {
                        if(cn == colTitelNumber) {
                            skipRow = true;
                            break; // The first column cell value is empty/null. Which means Titel column cell doesn't have value so don't add this row in csv.
                        }
                        rowData.append(CommonConstants.CSV_SEPARTOR);
                        continue;
                    }
                    switch (cellObj.getCellType()) {
                        case Cell.CELL_TYPE_BOOLEAN:
                            rowData.append(cellObj.getBooleanCellValue() + CommonConstants.CSV_SEPARTOR);
                            //LOG.error("Boolean:" + cellObj.getBooleanCellValue());
                            break;

                        case Cell.CELL_TYPE_NUMERIC:
                            if (DateUtil.isCellDateFormatted(cellObj)) {
                                Date date = cellObj.getDateCellValue();
                                rowData.append(dtFormat.format(date).toString() + CommonConstants.CSV_SEPARTOR);
                                //LOG.error("Date:" + cellObj.getDateCellValue());
                            } else {
                                rowData.append(cellObj.getNumericCellValue() + CommonConstants.CSV_SEPARTOR);
                                //LOG.error("Numeric:" + cellObj.getNumericCellValue());
                            }
                            break;

                        case Cell.CELL_TYPE_STRING:
                            String cellValue = cellObj.getStringCellValue();
                            // If string contains double quotes then replace it with pair of double quotes.
                            cellValue = cellValue.replaceAll(doubleQuotes, doubleQuotes + doubleQuotes);
                            // If string contains comma then surround the string with double quotes.
                            rowData.append(doubleQuotes + cellValue + doubleQuotes + CommonConstants.CSV_SEPARTOR);
                            //LOG.error("String:" + cellObj.getStringCellValue());
                            break;

                        case Cell.CELL_TYPE_BLANK:
                            rowData.append("" + CommonConstants.CSV_SEPARTOR);
                            //LOG.error("Blank:" + cellObj.toString());
                            break;

                        default:
                            rowData.append(cellObj + CommonConstants.CSV_SEPARTOR);
                    }
                }
                if(!skipRow) {
                    rowData.append("\r\n");
                    data.append(rowData); // Appending one entire row to main data string buffer.
                    totalRows++;
                }
            }
        }
        pTransferObj.put(CommonConstants.TOTAL_ROWS, (totalRows));
        fos.write(data.toString().getBytes());
        fos.close();
        wBook.close();
    } catch (Exception ex) {
        LOG.error("Exception Caught while generating CSV file", ex);
    }
</code>```

答:
You have to use auto filter provided in Apache Poi library and also you have set the freezing. I provide below the brief code snippet, you can use accordingly.</p>

```<code>XSSFSheet sheet = wBook.getSheet(CommonConstants.METADATA_WORKSHEET);
sheet.setAutoFilter(new CellRangeAddress(0, 0, 0, numColumns));
sheet.createFreezePane(0, 1);
</code>```

ContentLicense: CC BY-SA 4.0

Body:
I am reading excel file using POI library in my java code. So far fine. But now I have one requirement. The excel file contains many records (e.g. 1000 rows). It also has column headers (1st row). Now I am doing excel filtering on it. Say I have one 'year' column and I am filtering all rows for year=2019. I get 15 rows.
Question: I want to process only these 15 rows in my java code. Is there any method in poi library or way to know if the row being read is filtered or (the other way i.e. not filtered).
Thanks.

I already have working code but right now I am looking for how to read only filtered row. Nothing new tried yet other than searching in library and forums.

The below code is inside a method. I am not used to formatting with stackoverflow so kindly ignore any formatting issue.

    // For storing data into CSV files
    StringBuffer data = new StringBuffer();
    try {
        SimpleDateFormat dtFormat = new SimpleDateFormat(CommonConstants.YYYY_MM_DD); // "yyyy-MM-dd"
        String doubleQuotes = "\"";
        FileOutputStream fos = new FileOutputStream(outputFile);
        // Get the workbook object for XLSX file
        XSSFWorkbook wBook = new XSSFWorkbook(new FileInputStream(inputFile));
        wBook.setMissingCellPolicy(Row.RETURN_BLANK_AS_NULL);

        // Get first sheet from the workbook
        //XSSFSheet sheet = wBook.getSheetAt(0);
        XSSFSheet sheet = wBook.getSheet(CommonConstants.METADATA_WORKSHEET);
        //Row row;
        //Cell cell;
        // Iterate through each rows from first sheet
        int rows = sheet.getLastRowNum();
        int totalRows = 0;
        int colTitelNumber = 0;
        Row firstRowRecord = sheet.getRow(1);
        for (int cn = 0; cn < firstRowRecord.getLastCellNum(); cn++) {
            Cell cellObj = firstRowRecord.getCell(cn);
            if(cellObj != null) {
                String str = cellObj.toString();
                if(CommonConstants.COLUMN_TITEL.equalsIgnoreCase(str)) {
                    colTitelNumber = cn;
                    break;
                }
            }
        }
        // Start with row Number 1. We don't need 0th number row as it is for Humans to read but not required for processing.
        for (int rowNumber = 1; rowNumber <= rows; rowNumber++) {
            StringBuffer rowData = new StringBuffer();
            boolean skipRow = false;
            Row rowRecord = sheet.getRow(rowNumber);
            if (rowRecord == null) {
                LOG.error("Empty/Null record found");
            } else {
                for (int cn = 0; cn < rowRecord.getLastCellNum(); cn++) {
                    Cell cellObj = rowRecord.getCell(cn);
                    if(cellObj == null) {
                        if(cn == colTitelNumber) {
                            skipRow = true;
                            break; // The first column cell value is empty/null. Which means Titel column cell doesn't have value so don't add this row in csv.
                        }
                        rowData.append(CommonConstants.CSV_SEPARTOR);
                        continue;
                    }
                    switch (cellObj.getCellType()) {
                        case Cell.CELL_TYPE_BOOLEAN:
                            rowData.append(cellObj.getBooleanCellValue() + CommonConstants.CSV_SEPARTOR);
                            //LOG.error("Boolean:" + cellObj.getBooleanCellValue());
                            break;

                        case Cell.CELL_TYPE_NUMERIC:
                            if (DateUtil.isCellDateFormatted(cellObj)) {
                                Date date = cellObj.getDateCellValue();
                                rowData.append(dtFormat.format(date).toString() + CommonConstants.CSV_SEPARTOR);
                                //LOG.error("Date:" + cellObj.getDateCellValue());
                            } else {
                                rowData.append(cellObj.getNumericCellValue() + CommonConstants.CSV_SEPARTOR);
                                //LOG.error("Numeric:" + cellObj.getNumericCellValue());
                            }
                            break;

                        case Cell.CELL_TYPE_STRING:
                            String cellValue = cellObj.getStringCellValue();
                            // If string contains double quotes then replace it with pair of double quotes.
                            cellValue = cellValue.replaceAll(doubleQuotes, doubleQuotes + doubleQuotes);
                            // If string contains comma then surround the string with double quotes.
                            rowData.append(doubleQuotes + cellValue + doubleQuotes + CommonConstants.CSV_SEPARTOR);
                            //LOG.error("String:" + cellObj.getStringCellValue());
                            break;

                        case Cell.CELL_TYPE_BLANK:
                            rowData.append("" + CommonConstants.CSV_SEPARTOR);
                            //LOG.error("Blank:" + cellObj.toString());
                            break;

                        default:
                            rowData.append(cellObj + CommonConstants.CSV_SEPARTOR);
                    }
                }
                if(!skipRow) {
                    rowData.append("\r\n");
                    data.append(rowData); // Appending one entire row to main data string buffer.
                    totalRows++;
                }
            }
        }
        pTransferObj.put(CommonConstants.TOTAL_ROWS, (totalRows));
        fos.write(data.toString().getBytes());
        fos.close();
        wBook.close();
    } catch (Exception ex) {
        LOG.error("Exception Caught while generating CSV file", ex);
    }

Body:
You have to use auto filter provided in Apache Poi library and also you have set the freezing. I provide below the brief code snippet, you can use accordingly.

XSSFSheet sheet = wBook.getSheet(CommonConstants.METADATA_WORKSHEET);
sheet.setAutoFilter(new CellRangeAddress(0, 0, 0, numColumns));
sheet.createFreezePane(0, 1);


问:
Best UML designing tool in Linux?
Urgently want to know best UML designing tool in Linux?
Which Also Support ERD
Is it Possible to use in NetBEANs IDE 6.9.1</p>

答:
I use and prefer Umbrello.

ArgoUML is also quite good but its GUI bugs will drive you mad...

ContentLicense: CC BY-SA 3.0

Title: Best UML designing tool in Linux?


问:
not getting file while routing
I am getting an error while routing file as Error: file not found

File structure

```<code>Controller
--orgaizationController
--blogesController
--pagesController

Views
--organization.ejs
--bloges.ejs
--pages.ejs
--index.ejs
</code>```

organizationController.js

```<code>const express = require('express');
const router = express.Router();

router.get('/',async (req,res) => {

    res.render('index');
});

router.get('/addedOrganization', async (req,res) => {
    res.render('addedOrganization' , {});

});
</code>```

server.js

```<code>const blogsController = require('./Controllers/campaignController');
const organizationController = require('./Controllers/campaignController');
const pagesController = require('./Controllers/campaignController');
app.use('/', organizationController)
app.use('/', blogsController)
app.use('/', pagesController)
</code>```

Image

But when I change app.use('/', organizationController) to app.use('/organiztion', organizationController) I get the index page i.e my homepage
Please anyone help?

答:
You need to set this middleware to render views without mentioning the path</p>

```<code>// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
</code>```

Title: not getting file while routing

Body:
I am getting an error while routing file as Error: file not found

File structure

Controller
--orgaizationController
--blogesController
--pagesController

Views
--organization.ejs
--bloges.ejs
--pages.ejs
--index.ejs

organizationController.js

const express = require('express');
const router = express.Router();

router.get('/',async (req,res) => {

    res.render('index');
});

router.get('/addedOrganization', async (req,res) => {
    res.render('addedOrganization' , {});

});

server.js

const blogsController = require('./Controllers/campaignController');
const organizationController = require('./Controllers/campaignController');
const pagesController = require('./Controllers/campaignController');
app.use('/', organizationController)
app.use('/', blogsController)
app.use('/', pagesController)

Image

But when I change app.use('/', organizationController) to app.use('/organiztion', organizationController) I get the index page i.e my homepage
Please anyone help?

Body:
You need to set this middleware to render views without mentioning the path

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');


问:
input() error - NameError: name '...' is not defined
I am getting an error when I try to run this simple script:

```<code>input_variable = input("Enter your name: ")
print("your name is" + input_variable)
</code>```

Let's say I type in "dude", the error I am getting is:

  line 1, in <module>
    input_variable = input("Enter your name: ")
  File "<string>", line 1, in <module>
NameError: name 'dude' is not defined
```

I am running Mac OS X 10.9.1 and I am using the Python Launcher app that came with the install of Python 3.3 to run the script.

答:
I also encountered this issue with a module that was supposed to be compatible for python 2.7 and 3.7

what i found to fix the issue was importing:

```<code>from six.moves import input
</code>```

this fixed the usability for both interpreters

you can read more about the six library here

Body:
I am getting an error when I try to run this simple script:

input_variable = input("Enter your name: ")
print("your name is" + input_variable)

Let's say I type in "dude", the error I am getting is:

  line 1, in <module>
    input_variable = input("Enter your name: ")
  File "<string>", line 1, in <module>
NameError: name 'dude' is not defined

I am running Mac OS X 10.9.1 and I am using the Python Launcher app that came with the install of Python 3.3 to run the script.

Body:
I also encountered this issue with a module that was supposed to be compatible for python 2.7 and 3.7

what i found to fix the issue was importing:

from six.moves import input

this fixed the usability for both interpreters

you can read more about the six library here


问:
Calling list in a list of list of dictionary not working

Logic

There are different lists with different function names in each of them like this:

```<code>md5 = [gamma, alpha, beta, theta, delta]
sha1 = [alpha, beta, theta, delta]
sha256 = [alpha, beta, theta]
sha384 = [alpha, beta, theta]
sha512 = [alpha, beta, theta]
</code>```

That means, if a function for is to call md5 list, then all the functions inside the md5 list will run.

One of these lists will be called according to a dictionary of list. But in order to determine which list of functions to take, there is a dictionary created like this:

```<code>hashdict = {}
hashdict['md5']     =  [ 'md5'    , 32  , md5    ]
hashdict['sha1']    =  [ 'sha1'   , 40  , sha1   ]
hashdict['sha256']  =  [ 'sha256' , 64  , sha256 ]
hashdict['sha384']  =  [ 'sha384' , 96  , sha384 ]
hashdict['sha512']  =  [ 'sha512' , 128 , sha512 ]
</code>```

Function

This function takes the hash submitted by the user, checks if the string length macthes the length in any dictionary lists inside hashdict{}. If the length matches for example: 32 characters, then it will be md5 hash.

After the script knows the correct list in the dictionary, it will take the function (the third list item - second index of the list) from the perticular list in hashdict and pass it into a function call named api.

```<code>def crack(hashvalue):
    result = False
    hashInDict = False

    for hashList in list(hashdict.values()):
        if len(hashvalue) in hashList:
            hashInDict = True
            if not file:
                print(f'{info} Hash Function : {hashList[0].upper()}')
            for api in hashList[2]:
                r = api(hashvalue, hashList[0])
                if r:
                    return r
        else:
            if hashInDict == False:
                if not file:
                    print(f'{bad} This hash type is not supported.')
                    quit()
                else:
                    return False
</code>```

Problem

It works only if the hash is md5 and any other hash passed results in the message "This hash type is not supported". Somehow. if I remove the dictionary and use if-else instead to compare the hash string length, it all works fine. But using dictionary lists has resulted in all other hashes ignored and only the first dicitionary list working.

If I replace the first dictionary list with for example sha1, then only sha1 hashes will be detected and all others will result in hash not supported print.

I have no idea why it's ignoring anything after the first dictionary list item.

答:
It seems you’re really over complicating this, all you seem to be doing is if the length of the input matches a predefined number then call api passing the inputed string and a list of funcs then returning it’s value otherwise returning False.

Just make a dict that looks like this:

```<code>hash_dict = {
    32: [gamma, alpha, beta, theta, delta],
    40: [alpha, beta, theta, delta],
    64: [alpha, beta, theta],
    96: [alpha, beta, theta],
    128: [alpha, beta, theta]
}
</code>```

Then in the crack function just call .get with len(hash_value) and of its in the dict it returns the value, otherwise returns None or a specified default.

```<code>def crack(hash_value):
    hash_list = hash_dict.get(len(hash_value))
    if hash_list:
        return api(hash_value, hash_list)
    return False
</code>```

I’m making an assumption here as to what api does but if it’s just piping the data through each function in the list then it’s easy to do this in crack. Instead of return api(hash_value, hash_list) use:

```<code>for func in hash_list:
    hash_value = func(hash_value)
return hash_value
</code>```

Or even better just put actual methods in the dict like this (I’m using lambdas here for the sake of space, although, I’d suggest actual functions for readability):

```<code>hashdict = {
    32: lambda x: gamma(alpha(beta(theta(delta(x)))))
    40: lambda x: alpha(beta(theta, delta(x))),
    64: lambda x: alpha(beta(theta(x))),
    96: lambda x: alpha(beta(theta(x))),
    128: lambda x: alpha(beta(theta(x)))
}
</code>```

Then just do something like this:

```<code>def crack(hash_value):
    def _default(_): False
    hash_func = hash_dict.get(len(hash_value), _default)
    return hash_func(hash_value)
</code>```

Body:
Logic

There are different lists with different function names in each of them like this:

md5 = [gamma, alpha, beta, theta, delta]
sha1 = [alpha, beta, theta, delta]
sha256 = [alpha, beta, theta]
sha384 = [alpha, beta, theta]
sha512 = [alpha, beta, theta]

That means, if a function for is to call md5 list, then all the functions inside the md5 list will run.

One of these lists will be called according to a dictionary of list. But in order to determine which list of functions to take, there is a dictionary created like this:

hashdict = {}
hashdict['md5']     =  [ 'md5'    , 32  , md5    ]
hashdict['sha1']    =  [ 'sha1'   , 40  , sha1   ]
hashdict['sha256']  =  [ 'sha256' , 64  , sha256 ]
hashdict['sha384']  =  [ 'sha384' , 96  , sha384 ]
hashdict['sha512']  =  [ 'sha512' , 128 , sha512 ]

Function

This function takes the hash submitted by the user, checks if the string length macthes the length in any dictionary lists inside hashdict{}. If the length matches for example: 32 characters, then it will be md5 hash.

After the script knows the correct list in the dictionary, it will take the function (the third list item - second index of the list) from the perticular list in hashdict and pass it into a function call named api.

def crack(hashvalue):
    result = False
    hashInDict = False

    for hashList in list(hashdict.values()):
        if len(hashvalue) in hashList:
            hashInDict = True
            if not file:
                print(f'{info} Hash Function : {hashList[0].upper()}')
            for api in hashList[2]:
                r = api(hashvalue, hashList[0])
                if r:
                    return r
        else:
            if hashInDict == False:
                if not file:
                    print(f'{bad} This hash type is not supported.')
                    quit()
                else:
                    return False

Problem

It works only if the hash is md5 and any other hash passed results in the message "This hash type is not supported". Somehow. if I remove the dictionary and use if-else instead to compare the hash string length, it all works fine. But using dictionary lists has resulted in all other hashes ignored and only the first dicitionary list working.

If I replace the first dictionary list with for example sha1, then only sha1 hashes will be detected and all others will result in hash not supported print.

I have no idea why it's ignoring anything after the first dictionary list item.

Body:
It seems you’re really over complicating this, all you seem to be doing is if the length of the input matches a predefined number then call api passing the inputed string and a list of funcs then returning it’s value otherwise returning False.

Just make a dict that looks like this:

hash_dict = {
    32: [gamma, alpha, beta, theta, delta],
    40: [alpha, beta, theta, delta],
    64: [alpha, beta, theta],
    96: [alpha, beta, theta],
    128: [alpha, beta, theta]
}

Then in the crack function just call .get with len(hash_value) and of its in the dict it returns the value, otherwise returns None or a specified default.

def crack(hash_value):
    hash_list = hash_dict.get(len(hash_value))
    if hash_list:
        return api(hash_value, hash_list)
    return False

I’m making an assumption here as to what api does but if it’s just piping the data through each function in the list then it’s easy to do this in crack. Instead of return api(hash_value, hash_list) use:

for func in hash_list:
    hash_value = func(hash_value)
return hash_value

Or even better just put actual methods in the dict like this (I’m using lambdas here for the sake of space, although, I’d suggest actual functions for readability):

hashdict = {
    32: lambda x: gamma(alpha(beta(theta(delta(x)))))
    40: lambda x: alpha(beta(theta, delta(x))),
    64: lambda x: alpha(beta(theta(x))),
    96: lambda x: alpha(beta(theta(x))),
    128: lambda x: alpha(beta(theta(x)))
}

Then just do something like this:

def crack(hash_value):
    def _default(_): False
    hash_func = hash_dict.get(len(hash_value), _default)
    return hash_func(hash_value)


问:
Error: At least one of the class levels is not a valid R variable name
I am trying to implement Random Forest on a dataset using the caret package in  R. Looking at the previous examples on this site I changed the column names and the factor levels. Nothing seems to working. Been getting the same error over and over again. Here is my code, structure of my dataset and the error:

```<code>model_rf = train(Promoted ~ Department + Region+ Education+Gender+ RecruitmentChannel+TrainingNumber+Age+LengthOfService +EmployeePerformance+AvgTrainingPerformance, data=train, method='rf', tuneLength=5, trControl = fitControl)
model_rf
predicteds_rf <- predict(model_rf, newdata=test)
</code>```

Error: At least one of the class levels is not a valid
  R variable name; This will cause errors when class probabilities are
  generated because the variables names will be converted to
  Not.Promoted, Promoted . Please use factor levels that can be used as
  valid R variable names  (see ?make.names for help).

```<code>> str(trainData)
'data.frame':   54808 obs. of  12 variables:
 $ EmployeeID            : int  65438 65141 7513 2542 48945 58896 20379 16290 73202 28911 ...
 $ Department            : Factor w/ 9 levels "Analytics","Finance",..: 8 5 8 8 9 1 5 5 1 8 ...
 $ Region                : Factor w/ 34 levels "region_1","region_10",..: 32 15 11 16 19 12 13 28 13 1 ...
 $ Education             : Factor w/ 4 levels "","Bachelor's",..: 4 2 2 2 2 2 2 4 2 4 ...
 $ Gender                : Factor w/ 2 levels "f","m": 1 2 2 2 2 2 1 2 2 2 ...
 $ RecruitmentChannel    : Factor w/ 3 levels "other","referred",..: 3 1 3 1 1 3 1 3 1 3 ...
 $ TrainingNumber        : Factor w/ 5 levels "Average training",..: 5 5 5 3 5 3 5 5 5 5 ...
 $ Age                   : Factor w/ 3 levels "Middle Age","Old",..: 1 3 1 1 1 1 1 1 3 1 ...
 $ LengthOfService       : Factor w/ 6 levels "Junior","Mid Level",..: 6 2 6 6 1 6 2 2 2 2 ...
 $ EmployeePerformance   : Factor w/ 7 levels "Average Performer",..: 4 3 5 7 5 5 5 5 3 4 ...
 $ AvgTrainingPerformance: Factor w/ 6 levels "Average","Below Average",..: 5 1 2 2 4 6 2 1 6 2 ...
 $ Promoted              : Factor w/ 2 levels "Not Promoted",..: 1 1 1 1 1 1 1 1 1 1 ...
</code>```

Would be a big help if someone could provide me with some suggestion. Thanks a lot.

答:
Valid names for factor levels should not

contain spaces

start with a number

be an empty string

As you can see, your dataframe contains all three.

These requirements exist because whenever caret::train() runs a tree-based model (here random forests), it converts the factor levels into variables which are used to split the tree. Hence factor level labels become variable names.

So for each of these variables, you can convert the level names to valid labels with the following code.

```<code>trainData %>%
  mutate(Education = factor(Education,
          labels = make.names(levels(Education))))
</code>```

Instead of labels = you can do levels = - the latter changes the data whereas the former changes the display (e.g. by print(dataframe)).

Body:
I am trying to implement Random Forest on a dataset using the caret package in  R. Looking at the previous examples on this site I changed the column names and the factor levels. Nothing seems to working. Been getting the same error over and over again. Here is my code, structure of my dataset and the error:

model_rf = train(Promoted ~ Department + Region+ Education+Gender+ RecruitmentChannel+TrainingNumber+Age+LengthOfService +EmployeePerformance+AvgTrainingPerformance, data=train, method='rf', tuneLength=5, trControl = fitControl)
model_rf
predicteds_rf <- predict(model_rf, newdata=test)

Error: At least one of the class levels is not a valid
  R variable name; This will cause errors when class probabilities are
  generated because the variables names will be converted to
  Not.Promoted, Promoted . Please use factor levels that can be used as
  valid R variable names  (see ?make.names for help).

> str(trainData)
'data.frame':   54808 obs. of  12 variables:
 $ EmployeeID            : int  65438 65141 7513 2542 48945 58896 20379 16290 73202 28911 ...
 $ Department            : Factor w/ 9 levels "Analytics","Finance",..: 8 5 8 8 9 1 5 5 1 8 ...
 $ Region                : Factor w/ 34 levels "region_1","region_10",..: 32 15 11 16 19 12 13 28 13 1 ...
 $ Education             : Factor w/ 4 levels "","Bachelor's",..: 4 2 2 2 2 2 2 4 2 4 ...
 $ Gender                : Factor w/ 2 levels "f","m": 1 2 2 2 2 2 1 2 2 2 ...
 $ RecruitmentChannel    : Factor w/ 3 levels "other","referred",..: 3 1 3 1 1 3 1 3 1 3 ...
 $ TrainingNumber        : Factor w/ 5 levels "Average training",..: 5 5 5 3 5 3 5 5 5 5 ...
 $ Age                   : Factor w/ 3 levels "Middle Age","Old",..: 1 3 1 1 1 1 1 1 3 1 ...
 $ LengthOfService       : Factor w/ 6 levels "Junior","Mid Level",..: 6 2 6 6 1 6 2 2 2 2 ...
 $ EmployeePerformance   : Factor w/ 7 levels "Average Performer",..: 4 3 5 7 5 5 5 5 3 4 ...
 $ AvgTrainingPerformance: Factor w/ 6 levels "Average","Below Average",..: 5 1 2 2 4 6 2 1 6 2 ...
 $ Promoted              : Factor w/ 2 levels "Not Promoted",..: 1 1 1 1 1 1 1 1 1 1 ...

Would be a big help if someone could provide me with some suggestion. Thanks a lot.

Body:
Valid names for factor levels should not

contain spaces

start with a number

be an empty string

As you can see, your dataframe contains all three.

These requirements exist because whenever caret::train() runs a tree-based model (here random forests), it converts the factor levels into variables which are used to split the tree. Hence factor level labels become variable names.

So for each of these variables, you can convert the level names to valid labels with the following code.

trainData %>%
  mutate(Education = factor(Education,
          labels = make.names(levels(Education))))

Instead of labels = you can do levels = - the latter changes the data whereas the former changes the display (e.g. by print(dataframe)).


问:
Memory Leak Using JSON-C
I am new to JSON-C, Please see my sample code and let me know of it will create any memory leak, if yes then how to free JSON-C object.

```<code>    struct json_object *new_obj         = NULL;
    new_obj = json_tokener_parse(strRawJSON);
    new_obj = json_object_object_get(new_obj, "FUU");
    if(NULL == new_obj){
        SYS_OUT("\nFUU not found in JSON");
        return NO;
    }
    new_obj = json_object_object_get(new_obj, "FOO"); // I m re-using new_obj, without free it?
    if(NULL == new_obj){
        SYS_OUT("\nFOO not found in JSON");
        return NO;
    }
    // DO I need to clean new_obj, if yes then how ??
</code>```

Do I need to clean new_obj, if yes then how. Can some one help to understand how to do memory management JSON-C.

Thanks in Advance

答:
json_tokener_parse() will create an object which must be deleted.
in this case

json_object_put(new_obj);

is required.

Title: Memory Leak Using JSON-C

Body:
I am new to JSON-C, Please see my sample code and let me know of it will create any memory leak, if yes then how to free JSON-C object.

    struct json_object *new_obj         = NULL;
    new_obj = json_tokener_parse(strRawJSON);
    new_obj = json_object_object_get(new_obj, "FUU");
    if(NULL == new_obj){
        SYS_OUT("\nFUU not found in JSON");
        return NO;
    }
    new_obj = json_object_object_get(new_obj, "FOO"); // I m re-using new_obj, without free it?
    if(NULL == new_obj){
        SYS_OUT("\nFOO not found in JSON");
        return NO;
    }
    // DO I need to clean new_obj, if yes then how ??

Do I need to clean new_obj, if yes then how. Can some one help to understand how to do memory management JSON-C.

Thanks in Advance


问:
How do I find out how many records of 1 model have associated objects of an association?
I have two models - Property & Photos.

```<code>class Property < ActiveRecord::Base
  has_many :photos, dependent: :destroy
end

class Photo < ActiveRecord::Base
  belongs_to :property
end
</code>```

All I want to do is create a scope that returns only the Properties that actually have photos (i.e. photos.count > 0).

I have tried a million iterations of queries, and they don't work for one reason or another.

See some examples of what I have tried and the results:

```<code>[32] pry(main)> Property.includes(:photos).where('photos.count > 0').count
   (4.1ms)  SELECT COUNT(DISTINCT "properties"."id") FROM "properties" LEFT OUTER JOIN "photos" ON "photos"."property_id" = "properties"."id" WHERE (photos.count > 0)
ActiveRecord::StatementInvalid: PG::GroupingError: ERROR:  aggregate functions are not allowed in WHERE
LINE 1: ..."photos"."property_id" = "properties"."id" WHERE (photos.cou...
                                                             ^
[33] pry(main)> Property.joins(:photos).where('photos.count > 0').count
   (11.1ms)  SELECT COUNT(*) FROM "properties" INNER JOIN "photos" ON "photos"."property_id" = "properties"."id" WHERE (photos.count > 0)
ActiveRecord::StatementInvalid: PG::GroupingError: ERROR:  aggregate functions are not allowed in WHERE
LINE 1: ..."photos"."property_id" = "properties"."id" WHERE (photos.cou...
                                                             ^

[38] pry(main)> Property.joins(:photos).count("properties.id").count
   (158.2ms)  SELECT COUNT(properties.id) FROM "properties" INNER JOIN "photos" ON "photos"."property_id" = "properties"."id"
NoMethodError: undefined method `count' for 72604:Fixnum
from (pry):38:in `__pry__'

[39] pry(main)> Property.joins(:photos).count("properties.id")
   (50.6ms)  SELECT COUNT(properties.id) FROM "properties" INNER JOIN "photos" ON "photos"."property_id" = "properties"."id"
=> 72604

[40] pry(main)> Photo.joins(:properties).count("photos.id")
ActiveRecord::ConfigurationError: Association named 'properties' was not found on Photo; perhaps you misspelled it?
from /.rvm/gems/ruby-2.3.7@myapp/gems/activerecord-3.2.22.5/lib/active_record/associations/join_dependency.rb:112:in `build'

[41] pry(main)> Photo.joins(:property).count("photos.id")
   (65.0ms)  SELECT COUNT(photos.id) FROM "photos" INNER JOIN "properties" ON "properties"."id" = "photos"."property_id"
=> 72604

[42] pry(main)> Photo.joins(:property).count("photos.id")
   (60.5ms)  SELECT COUNT(photos.id) FROM "photos" INNER JOIN "properties" ON "properties"."id" = "photos"."property_id"
=> 72604

[43] pry(main)> Property.joins(:photos).count("properties.id").distinct
   (46.4ms)  SELECT COUNT(properties.id) FROM "properties" INNER JOIN "photos" ON "photos"."property_id" = "properties"."id"
NoMethodError: undefined method `distinct' for 72604:Fixnum
from (pry):43:in `__pry__'

[44] pry(main)> Property.joins(:photos).distinct.count("properties.id")
=> 0
</code>```

This should be simple, but for some reason it's much more difficult than I expected.

Thoughts?

答: Using Property.joins(:photos) (which performs an inner join) will return the properties that have related photos. Meaning photos that have a property_id. While the answer using left_outer_joins, LEFT JOIN or LEFT OUTER JOIN will work, it's using the left join incorrectly by having the .where(photos: { id: nil }). INNER JOIN, JOIN, or joins between Property and Photo will result in what I have stated at the beginning of this answer. The left joins will return all properties with or without related photos.

Body:
I have two models - Property & Photos.

class Property < ActiveRecord::Base
  has_many :photos, dependent: :destroy
end

class Photo < ActiveRecord::Base
  belongs_to :property
end

All I want to do is create a scope that returns only the Properties that actually have photos (i.e. photos.count > 0).

I have tried a million iterations of queries, and they don't work for one reason or another.

See some examples of what I have tried and the results:

[32] pry(main)> Property.includes(:photos).where('photos.count > 0').count
   (4.1ms)  SELECT COUNT(DISTINCT "properties"."id") FROM "properties" LEFT OUTER JOIN "photos" ON "photos"."property_id" = "properties"."id" WHERE (photos.count > 0)
ActiveRecord::StatementInvalid: PG::GroupingError: ERROR:  aggregate functions are not allowed in WHERE
LINE 1: ..."photos"."property_id" = "properties"."id" WHERE (photos.cou...
                                                             ^
[33] pry(main)> Property.joins(:photos).where('photos.count > 0').count
   (11.1ms)  SELECT COUNT(*) FROM "properties" INNER JOIN "photos" ON "photos"."property_id" = "properties"."id" WHERE (photos.count > 0)
ActiveRecord::StatementInvalid: PG::GroupingError: ERROR:  aggregate functions are not allowed in WHERE
LINE 1: ..."photos"."property_id" = "properties"."id" WHERE (photos.cou...
                                                             ^

[38] pry(main)> Property.joins(:photos).count("properties.id").count
   (158.2ms)  SELECT COUNT(properties.id) FROM "properties" INNER JOIN "photos" ON "photos"."property_id" = "properties"."id"
NoMethodError: undefined method `count' for 72604:Fixnum
from (pry):38:in `__pry__'

[39] pry(main)> Property.joins(:photos).count("properties.id")
   (50.6ms)  SELECT COUNT(properties.id) FROM "properties" INNER JOIN "photos" ON "photos"."property_id" = "properties"."id"
=> 72604

[40] pry(main)> Photo.joins(:properties).count("photos.id")
ActiveRecord::ConfigurationError: Association named 'properties' was not found on Photo; perhaps you misspelled it?
from /.rvm/gems/ruby-2.3.7@myapp/gems/activerecord-3.2.22.5/lib/active_record/associations/join_dependency.rb:112:in `build'

[41] pry(main)> Photo.joins(:property).count("photos.id")
   (65.0ms)  SELECT COUNT(photos.id) FROM "photos" INNER JOIN "properties" ON "properties"."id" = "photos"."property_id"
=> 72604

[42] pry(main)> Photo.joins(:property).count("photos.id")
   (60.5ms)  SELECT COUNT(photos.id) FROM "photos" INNER JOIN "properties" ON "properties"."id" = "photos"."property_id"
=> 72604

[43] pry(main)> Property.joins(:photos).count("properties.id").distinct
   (46.4ms)  SELECT COUNT(properties.id) FROM "properties" INNER JOIN "photos" ON "photos"."property_id" = "properties"."id"
NoMethodError: undefined method `distinct' for 72604:Fixnum
from (pry):43:in `__pry__'

[44] pry(main)> Property.joins(:photos).distinct.count("properties.id")
=> 0

This should be simple, but for some reason it's much more difficult than I expected.

Thoughts?


问:
KafkaUtils class not found in Spark streaming
I have just began with Spark Streaming and I am trying to build a sample application that counts words from a Kafka stream. Although it compiles with sbt package, when I run it, I get NoClassDefFoundError. This post seems to have the same problem, but the solution is for Maven and I have not been able to reproduce it with sbt.

KafkaApp.scala:

import org.apache.spark._
import org.apache.spark.streaming._
import org.apache.spark.streaming.kafka._

object KafkaApp {
  def main(args: Array[String]) {

    val conf = new SparkConf().setAppName("kafkaApp").setMaster("local[*]")
    val ssc = new StreamingContext(conf, Seconds(1))
    val kafkaParams = Map(
        "zookeeper.connect" -> "localhost:2181",
        "zookeeper.connection.timeout.ms" -> "10000",
        "group.id" -> "sparkGroup"
    )

    val topics = Map(
        "test" -> 1
    )

    // stream of (topic, ImpressionLog)
    val messages = KafkaUtils.createStream(ssc, kafkaParams, topics, storage.StorageLevel.MEMORY_AND_DISK)
    println(s"Number of words: %{messages.count()}")
  }
}
```

<p><code>build.sbt</code>:</p>

<pre class="lang-scala prettyprint-override"><code>name := "Simple Project"

version := "1.1"

scalaVersion := "2.10.4"

libraryDependencies ++= Seq(
    "org.apache.spark" %% "spark-core" % "1.1.1",
    "org.apache.spark" %% "spark-streaming" % "1.1.1",
    "org.apache.spark" %% "spark-streaming-kafka" % "1.1.1"
)

resolvers += "Akka Repository" at "http://repo.akka.io/releases/"
</code>```

And I submit it with:

```<code>bin/spark-submit \
  --class "KafkaApp" \
  --master local[4] \
  target/scala-2.10/simple-project_2.10-1.1.jar
</code>```

Error:

```<code>14/12/30 19:44:57 INFO AkkaUtils: Connecting to HeartbeatReceiver: akka.tcp://sparkDriver@192.168.5.252:65077/user/HeartbeatReceiver
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/spark/streaming/kafka/KafkaUtils$
    at KafkaApp$.main(KafkaApp.scala:28)
    at KafkaApp.main(KafkaApp.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.spark.deploy.SparkSubmit$.launch(SparkSubmit.scala:329)
    at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:75)
    at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala)
Caused by: java.lang.ClassNotFoundException: org.apache.spark.streaming.kafka.KafkaUtils$
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
</code>```

答: use --packages argument on spark-submit, it takes mvn package in the format group:artifact:version,...

Body:
I have just began with Spark Streaming and I am trying to build a sample application that counts words from a Kafka stream. Although it compiles with sbt package, when I run it, I get NoClassDefFoundError. This post seems to have the same problem, but the solution is for Maven and I have not been able to reproduce it with sbt.

KafkaApp.scala:

import org.apache.spark._
import org.apache.spark.streaming._
import org.apache.spark.streaming.kafka._

object KafkaApp {
  def main(args: Array[String]) {

    val conf = new SparkConf().setAppName("kafkaApp").setMaster("local[*]")
    val ssc = new StreamingContext(conf, Seconds(1))
    val kafkaParams = Map(
        "zookeeper.connect" -> "localhost:2181",
        "zookeeper.connection.timeout.ms" -> "10000",
        "group.id" -> "sparkGroup"
    )

    val topics = Map(
        "test" -> 1
    )

    // stream of (topic, ImpressionLog)
    val messages = KafkaUtils.createStream(ssc, kafkaParams, topics, storage.StorageLevel.MEMORY_AND_DISK)
    println(s"Number of words: %{messages.count()}")
  }
}

build.sbt:

name := "Simple Project"

version := "1.1"

scalaVersion := "2.10.4"

libraryDependencies ++= Seq(
    "org.apache.spark" %% "spark-core" % "1.1.1",
    "org.apache.spark" %% "spark-streaming" % "1.1.1",
    "org.apache.spark" %% "spark-streaming-kafka" % "1.1.1"
)

resolvers += "Akka Repository" at "http://repo.akka.io/releases/"

And I submit it with:

bin/spark-submit \
  --class "KafkaApp" \
  --master local[4] \
  target/scala-2.10/simple-project_2.10-1.1.jar

Error:

14/12/30 19:44:57 INFO AkkaUtils: Connecting to HeartbeatReceiver: akka.tcp://sparkDriver@192.168.5.252:65077/user/HeartbeatReceiver
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/spark/streaming/kafka/KafkaUtils$
    at KafkaApp$.main(KafkaApp.scala:28)
    at KafkaApp.main(KafkaApp.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.spark.deploy.SparkSubmit$.launch(SparkSubmit.scala:329)
    at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:75)
    at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala)
Caused by: java.lang.ClassNotFoundException: org.apache.spark.streaming.kafka.KafkaUtils$
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)


问:
Stop navigation WKWebview
This is my situation:

I have a view controller within a WKWebView. This webview starts with a page "A". In this page there are some links (href) and I want that for some of these links must open in the external browser.

For this reason I set the WKWebView delegate:

```<code>func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {

        if let url = webView.url?.absoluteString
        {

                if(self.isExternalURL(url))
                {
                    let urlT = URL(string: url)!
                    decisionHandler(.cancel)
                    UIApplication.shared.open(urlT, options: [:], completionHandler: nil)
                }
                else
                {
                    decisionHandler(.allow)
                }
         }
         else
         {
                decisionHandler(.allow)
         }
}

private func isExternalURL(url:String) -> Bool
{
    //......check link
}
</code>```

My problem is that if I select an external link the external browser opens, but the webview does not remain on page A, but it also loads the external link while I would like it to remain on page A.

I dont know why

答:
You should use navigationAction.request.url instead of webView.url

webView.url - Already loaded url

navigationAction.request.url - New url to load

Change

```<code>func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
    if let url = navigationAction.request.url?.absoluteString
    {
        if self.isExternalURL(url)
        {
            decisionHandler(.cancel)
            UIApplication.shared.open(navigationAction.request.url, options: [:], completionHandler: nil)
        }
        else
        {
            decisionHandler(.allow)
        }
    }
    else
    {
        decisionHandler(.allow)
    }
}
</code>```

Title: Stop navigation WKWebview

Body:
This is my situation:

I have a view controller within a WKWebView. This webview starts with a page "A". In this page there are some links (href) and I want that for some of these links must open in the external browser.

For this reason I set the WKWebView delegate:

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {

        if let url = webView.url?.absoluteString
        {

                if(self.isExternalURL(url))
                {
                    let urlT = URL(string: url)!
                    decisionHandler(.cancel)
                    UIApplication.shared.open(urlT, options: [:], completionHandler: nil)
                }
                else
                {
                    decisionHandler(.allow)
                }
         }
         else
         {
                decisionHandler(.allow)
         }
}

private func isExternalURL(url:String) -> Bool
{
    //......check link
}

My problem is that if I select an external link the external browser opens, but the webview does not remain on page A, but it also loads the external link while I would like it to remain on page A.

I dont know why

Body:
You should use navigationAction.request.url instead of webView.url

webView.url - Already loaded url

navigationAction.request.url - New url to load

Change

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
    if let url = navigationAction.request.url?.absoluteString
    {
        if self.isExternalURL(url)
        {
            decisionHandler(.cancel)
            UIApplication.shared.open(navigationAction.request.url, options: [:], completionHandler: nil)
        }
        else
        {
            decisionHandler(.allow)
        }
    }
    else
    {
        decisionHandler(.allow)
    }
}


问:
In TensorFlow 2.0, how can I see the number of elements in a dataset?
When I load a dataset, I wonder if there is any quick way to find the number of samples or batches in that dataset. I know that if I load a dataset with with_info=True, I can see for example total_num_examples=6000, but this information is not available if I split a dataset.

Currently, I count the number of samples as follows, but wondering if there is any better solution:

```<code>train_subsplit_1, train_subsplit_2, train_subsplit_3 = tfds.Split.TRAIN.subsplit(3)

cifar10_trainsub3 = tfds.load("cifar10", split=train_subsplit_3)

cifar10_trainsub3 = cifar10_trainsub3.batch(1000)

n = 0
for i, batch in enumerate(cifar10_trainsub3.take(-1)):
    print(i, n, batch['image'].shape)
    n += len(batch['image'])

print(i, n)
</code>```

答:
If it's possible to know the length then you could use:

tf.data.experimental.cardinality(dataset)
```

<p>but the problem is that a TF dataset is inherently lazily loaded. So we might not know the size of the dataset up front. Indeed, it's perfectly possible to have a dataset represent an infinite set of data!</p>

<p>If it is a small enough dataset you could also just iterate over it to get the length. I've used the following ugly little construct before but it depends on the dataset being small enough for us to be happy to load into memory and it's really not an improvement over your <code>for</code> loop above! </p>

<pre class="lang-py prettyprint-override"><code>dataset_length = [i for i,_ in enumerate(dataset)][-1] + 1
</code>```

Body:
When I load a dataset, I wonder if there is any quick way to find the number of samples or batches in that dataset. I know that if I load a dataset with with_info=True, I can see for example total_num_examples=6000, but this information is not available if I split a dataset.

Currently, I count the number of samples as follows, but wondering if there is any better solution:

train_subsplit_1, train_subsplit_2, train_subsplit_3 = tfds.Split.TRAIN.subsplit(3)

cifar10_trainsub3 = tfds.load("cifar10", split=train_subsplit_3)

cifar10_trainsub3 = cifar10_trainsub3.batch(1000)

n = 0
for i, batch in enumerate(cifar10_trainsub3.take(-1)):
    print(i, n, batch['image'].shape)
    n += len(batch['image'])

print(i, n)

Body:
If it's possible to know the length then you could use:

tf.data.experimental.cardinality(dataset)

but the problem is that a TF dataset is inherently lazily loaded. So we might not know the size of the dataset up front. Indeed, it's perfectly possible to have a dataset represent an infinite set of data!

If it is a small enough dataset you could also just iterate over it to get the length. I've used the following ugly little construct before but it depends on the dataset being small enough for us to be happy to load into memory and it's really not an improvement over your for loop above!

dataset_length = [i for i,_ in enumerate(dataset)][-1] + 1


问:
Read and process data from URL in python
I am trying to get the data from URL.below is the URL Format.

What I am trying to do

1)read line by line and find if the line contains the desired keyword.
3)If yes then store the previous line's content "GETCONTENT" in a list

```<code><http://www.example.com/XYZ/a-b-c/w#>DONTGETCONTENT
 a       <http://www.example.com/XYZ/mount/v1#NNNN> ,
<http://www.w3.org/2002/w#Individual> ;
        <http://www.w3.org/2000/01/rdf-schema#label>
                "some content , "some url content ;
        <http://www.example.com/XYZ/log/v1#hasRelation>
                <http://www.example.com/XYZ/data/v1#Change> ;
        <http://www.example.com/XYZ/log/v1#ServicePage>
                <https://dev.org.net/apis/someLabel> ;
        <http://www.example.com/XYZ/log/v1#Description>
                "Some API Content .

<http://www.example.com/XYZ/model/v1#GETBBBBBB>
a       <http://www.w3.org/01/07/w#BBBBBB> ;
        <http://www.w3.org/2000/01/schema#domain>
                <http://www.example.com/XYZ/data/v1#xyz> ;
        <http://www.w3.org/2000/01/schema#label1>
               "some content , "some url content ;
        <http://www.w3.org/2000/01/schema#range>
                <http://www.w3.org/2001/XMLSchema#boolean> ;
       <http://www.example.com/XYZ/log/v1#Description>
            "Some description .

<http://www.example.com/XYZ/datamodel-ee/v1#GETAAAAAA>
 a       <http://www.w3.org/01/07/w#AAAAAA> ;
        <http://www.w3.org/2000/01/schema#domain>
                <http://www.example.com/XYZ/data/v1#Version> ;
        <http://www.w3.org/2000/01/schema#label>
                "some content ;
        <http://www.w3.org/2000/01/schema#range>
            <http://www.example.com/XYZ/data/v1#uuu> .

<http://www.example.com/XYZ/datamodel/v1#GETCCCCCC>
 a       <http://www.w3.org/01/07/w#CCCCCC ,
<http://www.w3.org/2002/07/w#Name>
        <http://www.w3.org/2000/01/schema#domain>
                <http://www.example.com/XYZ/data/v1#xyz> ;
        <http://www.w3.org/2000/01/schema#label1>
              "some content , "some url content ;
        <http://www.w3.org/2000/01/schema#range>
               <http://www.w3.org/2001/XMLSchema#boolean> ;
        <http://www.example.com/XYZ/log/v1#Description>
               "Some description .
</code>```

below is the code i tried so far but it is printing all the content of the file

```<code>  import re
        def read_from_url():
            try:
                from urllib.request import urlopen
            except ImportError:
                from urllib2 import urlopen
            url_link = "examle.com"
            html = urlopen(url_link)
            previous=None
            for line in html:
                previous=line
                line = re.search(r"^(\s*a\s*)|\#GETBBBBBB|#GETAAAAAA|#GETCCCCCC\b",
        line.decode('UTF-8'))
                print(previous)
        if __name__ == '__main__':
        read_from_url()
</code>```

Expected output:

```<code>GETBBBBBB , GETAAAAAA , GETCCCCCC
</code>```

Thanks in advance!!

答:
I'm puzzled about a few things-- answering which may help the community better assist you. Specifically, I can't tell what form the file is in (ie. is it a txt file or a url you're making a request to and parsing the response of). I also can't tell if you're trying to get the entire line, just the url, or just the bit that follows the hash symbol.

Nonetheless, you stated you were looking for the program to output GETBBBBBB , GETAAAAAA , GETCCCCCC, and here's a quick way to get those specific values (assuming the values are in the form of a string):

```<code>search = re.findall(r'#(GET[ABC]{6})>', string)
</code>```

Otherwise, if you're reading from a txt file, this may help:

```<code>with open('example_file.txt', 'r') as file:
    lst = []
    for line in file:
        search = re.findall(r'#(GET[ABC]{6})', line)
        if search != []:
            lst += search
    print(lst)
</code>```

Of course, these are just some quick suggestions in case they may be of help. Otherwise, please answer the questions I mentioned at the beginning of my response and maybe it can help someone on SO better understand what you're looking to get.

Body:
I am trying to get the data from URL.below is the URL Format.

What I am trying to do

1)read line by line and find if the line contains the desired keyword.
3)If yes then store the previous line's content "GETCONTENT" in a list

<http://www.example.com/XYZ/a-b-c/w#>DONTGETCONTENT
 a       <http://www.example.com/XYZ/mount/v1#NNNN> ,
<http://www.w3.org/2002/w#Individual> ;
        <http://www.w3.org/2000/01/rdf-schema#label>
                "some content , "some url content ;
        <http://www.example.com/XYZ/log/v1#hasRelation>
                <http://www.example.com/XYZ/data/v1#Change> ;
        <http://www.example.com/XYZ/log/v1#ServicePage>
                <https://dev.org.net/apis/someLabel> ;
        <http://www.example.com/XYZ/log/v1#Description>
                "Some API Content .

<http://www.example.com/XYZ/model/v1#GETBBBBBB>
a       <http://www.w3.org/01/07/w#BBBBBB> ;
        <http://www.w3.org/2000/01/schema#domain>
                <http://www.example.com/XYZ/data/v1#xyz> ;
        <http://www.w3.org/2000/01/schema#label1>
               "some content , "some url content ;
        <http://www.w3.org/2000/01/schema#range>
                <http://www.w3.org/2001/XMLSchema#boolean> ;
       <http://www.example.com/XYZ/log/v1#Description>
            "Some description .

<http://www.example.com/XYZ/datamodel-ee/v1#GETAAAAAA>
 a       <http://www.w3.org/01/07/w#AAAAAA> ;
        <http://www.w3.org/2000/01/schema#domain>
                <http://www.example.com/XYZ/data/v1#Version> ;
        <http://www.w3.org/2000/01/schema#label>
                "some content ;
        <http://www.w3.org/2000/01/schema#range>
            <http://www.example.com/XYZ/data/v1#uuu> .

<http://www.example.com/XYZ/datamodel/v1#GETCCCCCC>
 a       <http://www.w3.org/01/07/w#CCCCCC ,
<http://www.w3.org/2002/07/w#Name>
        <http://www.w3.org/2000/01/schema#domain>
                <http://www.example.com/XYZ/data/v1#xyz> ;
        <http://www.w3.org/2000/01/schema#label1>
              "some content , "some url content ;
        <http://www.w3.org/2000/01/schema#range>
               <http://www.w3.org/2001/XMLSchema#boolean> ;
        <http://www.example.com/XYZ/log/v1#Description>
               "Some description .

below is the code i tried so far but it is printing all the content of the file

  import re
        def read_from_url():
            try:
                from urllib.request import urlopen
            except ImportError:
                from urllib2 import urlopen
            url_link = "examle.com"
            html = urlopen(url_link)
            previous=None
            for line in html:
                previous=line
                line = re.search(r"^(\s*a\s*)|\#GETBBBBBB|#GETAAAAAA|#GETCCCCCC\b",
        line.decode('UTF-8'))
                print(previous)
        if __name__ == '__main__':
        read_from_url()

Expected output:

GETBBBBBB , GETAAAAAA , GETCCCCCC

Thanks in advance!!

Body:
I'm puzzled about a few things-- answering which may help the community better assist you. Specifically, I can't tell what form the file is in (ie. is it a txt file or a url you're making a request to and parsing the response of). I also can't tell if you're trying to get the entire line, just the url, or just the bit that follows the hash symbol.

Nonetheless, you stated you were looking for the program to output GETBBBBBB , GETAAAAAA , GETCCCCCC, and here's a quick way to get those specific values (assuming the values are in the form of a string):

search = re.findall(r'#(GET[ABC]{6})>', string)

Otherwise, if you're reading from a txt file, this may help:

with open('example_file.txt', 'r') as file:
    lst = []
    for line in file:
        search = re.findall(r'#(GET[ABC]{6})', line)
        if search != []:
            lst += search
    print(lst)

Of course, these are just some quick suggestions in case they may be of help. Otherwise, please answer the questions I mentioned at the beginning of my response and maybe it can help someone on SO better understand what you're looking to get.


问:
Change the sequence using SQL Query
Want to sequence with SQL query.
Is there any shorter way to update it, or should I update the whole table.

My table as follows:

```<code>DECLARE @tab TABLE (ID INT IDENTITY, Name VARCHAR(10), Seq INT)

INSERT INTO @tab VALUES('A',1),('B',1),('C',1),('D',3),('E',4),('F',5),('G',6),('H',7),('I',8)

SELECT * FROM @tab ORDER BY Seq
</code>```

I want to change the sequence show column with ID 7, 8, 9 at the top.
My desired output should be

```<code>DECLARE @tab TABLE (ID INT IDENTITY, Name VARCHAR(10), Seq INT)

INSERT INTO @tab VALUES('A',4),('B',5),('C',6),('D',7),('E',8),('F',9),('G',1),('H',2),('I',3)
SELECT * FROM @tab ORDER BY Seq
</code>```

答:
ROW_NUMBER is your friend here, just have to compute 2 different ones depending on the name breaking point.

```<code>;WITH RowNumbers AS
(
    SELECT
        T.ID,
        RegularRowNumber = 3 + ROW_NUMBER() OVER (ORDER BY T.Name),
        AfterGRowNumber = -6 + ROW_NUMBER() OVER (ORDER BY T.Name),
        T.Seq,
        T.Name
    FROM
        @tab AS T
)
UPDATE R SET
    Seq = CASE WHEN R.Name >= 'G' THEN AfterGRowNumber ELSE RegularRowNumber END
FROM
    RowNumbers AS R
</code>```

Result:

```<code>ID  Name    Seq
1   A       4
2   B       5
3   C       6
4   D       7
5   E       8
6   F       9
7   G       1
8   H       2
9   I       3
</code>```

Might want to consider using ID to order instead of Name, if appropriate.

Title: Change the sequence using SQL Query

Body:
Want to sequence with SQL query.
Is there any shorter way to update it, or should I update the whole table.

My table as follows:

DECLARE @tab TABLE (ID INT IDENTITY, Name VARCHAR(10), Seq INT)

INSERT INTO @tab VALUES('A',1),('B',1),('C',1),('D',3),('E',4),('F',5),('G',6),('H',7),('I',8)

SELECT * FROM @tab ORDER BY Seq

I want to change the sequence show column with ID 7, 8, 9 at the top.
My desired output should be

DECLARE @tab TABLE (ID INT IDENTITY, Name VARCHAR(10), Seq INT)

INSERT INTO @tab VALUES('A',4),('B',5),('C',6),('D',7),('E',8),('F',9),('G',1),('H',2),('I',3)
SELECT * FROM @tab ORDER BY Seq

Body:
ROW_NUMBER is your friend here, just have to compute 2 different ones depending on the name breaking point.

;WITH RowNumbers AS
(
    SELECT
        T.ID,
        RegularRowNumber = 3 + ROW_NUMBER() OVER (ORDER BY T.Name),
        AfterGRowNumber = -6 + ROW_NUMBER() OVER (ORDER BY T.Name),
        T.Seq,
        T.Name
    FROM
        @tab AS T
)
UPDATE R SET
    Seq = CASE WHEN R.Name >= 'G' THEN AfterGRowNumber ELSE RegularRowNumber END
FROM
    RowNumbers AS R

Result:

ID  Name    Seq
1   A       4
2   B       5
3   C       6
4   D       7
5   E       8
6   F       9
7   G       1
8   H       2
9   I       3

Might want to consider using ID to order instead of Name, if appropriate.


问:
i am webscraping with bs4 and the urls wont show up
i am new to webscaping and wanted to scape all the charackterportraits from the lol site and when i examined one of the pictures in the browser it was in a "img scr="url" tag and i want to get the url to download the picture but when i do soup.select('img[src]') or soup.select('img') it returns an empty list and i dont know why

here is the code:

```<code>data=requests.get(website)
data.raise_for_status()

soup = bs4.BeautifulSoup(data.text,"lxml")
print(soup)
#soup returns html

elems = soup.select('img[src]')
print(elems)
#elems returns an empty list
</code>```

答:
Here is your answer </p>

```<code>import requests
from bs4 import BeautifulSoup

soup = BeautifulSoup(requests.get("https://na.leagueoflegends.com/en/game-info/champions/").text, 'lxml')
soup.find_all('link')    #these are your tags eg: a , script link

</code>```

```<code>OUTPUT:
Out[21]:
[<a href="/en/game-info/get-started/">Get Started</a>,
 <a href="/en/game-info/get-started/what-is-lol/">What is League of Legends?</a>,
 <a href="https://na.leagueoflegends.com/en/site/guide/index.html">New Player Guide</a>,
 <a href="/en/game-info/get-started/chat-commands/">Chat Commands</a>,
 <a href="/en/game-info/get-started/community-interaction/">Community Interaction</a>,
 <a href="/en/featured/summoners-code">The Summoner's Code</a>,
 <a href="/en/game-info/champions/">Champions</a>,
 <a href="/en/game-info/items/">Items</a>,
 <a href="/en/game-info/summoners/">Summoners</a>,
 <a href="/en/game-info/summoners/spells/">Summoner Spells</a>,
 <a href="/en/game-info/game-modes/">Game Modes</a>,
 <a href="/en/game-info/game-modes/summoners-rift/">Summoner's Rift</a>,
 <a href="/en/game-info/game-modes/the-twisted-treeline/">The Twisted Treeline</a>,
 <a href="/en/game-info/game-modes/howling-abyss/">Howling Abyss</a>,
 <a href="//na.leagueoflegends.com/en/">Home</a>,
 <a href="/en/game-info/">Game Info</a>]

soup = BeautifulSoup(requests.get("https://na.leagueoflegends.com/en/game-info/champions/").text, 'lxml')
soup.find_all('script')
Out[22]:
soup = BeautifulSoup(requests.get("https://na.leagueoflegends.com/en/game-info/champions/").text, 'lxml')
soup.find_all('a')
[<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src='//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);})(window,document,'script','dataLayer','GTM-N98J');</script>,
 <script>window.ga = window.ga || function(){(ga.q=ga.q||[]).push(arguments)};ga.l = +new Date;</script>,
 <script src="https://lolstatic-a.akamaihd.net/lolkit/1.1.6/modernizr.js" type="text/javascript"></script>,
 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>,
 <script src="https://lolstatic-a.akamaihd.net/lolkit/1.1.6/riot-all.js" type="text/javascript"></script>,
 <script src="https://lolstatic-a.akamaihd.net/lolkit/1.1.6/riot-kit-all.js" type="text/javascript"></script>,
 <script type="text/javascript">rg_force_language = 'en_US';rg_force_manifest = 'https://ddragon.leagueoflegends.com/realms/na.js';rg_assets = 'https://lolstatic-a.akamaihd.net/game-info/1.1.9';</script>,
 <script type="text/javascript">window.riotBarConfig = {touchpoints: {activeTouchpoint: 'game'},locale: {landingUrlPattern : 'https://na.leagueoflegends.com//game-info/'},footer: {enabled: true,container: {renderFooterInto: '#footer'}}};</script>,
 <script async="" src="https://lolstatic-a.akamaihd.net/riotbar/prod/latest/en_US.js"></script>,
 <script src="https://ddragon.leagueoflegends.com/cdn/dragonhead.js" type="text/javascript"></script>,
 <script src="https://lolstatic-a.akamaihd.net/frontpage/apps/prod/LolGameInfo-Harbinger/en_US/0d258ed5be6806b967afaf2b4b9817406912a7ac/assets/assets/js/riot-dd-utils.js" type="text/javascript"></script>,
 <script src="https://lolstatic-a.akamaihd.net/frontpage/apps/prod/LolGameInfo-Harbinger/en_US/0d258ed5be6806b967afaf2b4b9817406912a7ac/assets/assets/js/riot-dd-i18n.js" type="text/javascript"></script>,
 <script src="https://lolstatic-a.akamaihd.net/frontpage/apps/prod/LolGameInfo-Harbinger/en_US/0d258ed5be6806b967afaf2b4b9817406912a7ac/assets/assets/js/external/jquery.lazy-load.js" type="text/javascript"></script>,
 <script src="https://lolstatic-a.akamaihd.net/frontpage/apps/prod/LolGameInfo-Harbinger/en_US/0d258ed5be6806b967afaf2b4b9817406912a7ac/assets/assets/js/DDFilterApp.js" type="text/javascript"></script>,
 <script src="https://lolstatic-a.akamaihd.net/frontpage/apps/prod/LolGameInfo-Harbinger/en_US/0d258ed5be6806b967afaf2b4b9817406912a7ac/assets/assets/js/DDMarkupItem.js" type="text/javascript"></script>,
 <script src="https://lolstatic-a.akamaihd.net/frontpage/apps/prod/LolGameInfo-Harbinger/en_US/0d258ed5be6806b967afaf2b4b9817406912a7ac/assets/assets/js/DDMarkupContainer.js" type="text/javascript"></script>,
 <script src="https://lolstatic-a.akamaihd.net/frontpage/apps/prod/LolGameInfo-Harbinger/en_US/0d258ed5be6806b967afaf2b4b9817406912a7ac/assets/assets/js/champions/ChampionsListGridItem.js" type="text/javascript"></script>,
 <script src="https://lolstatic-a.akamaihd.net/frontpage/apps/prod/LolGameInfo-Harbinger/en_US/0d258ed5be6806b967afaf2b4b9817406912a7ac/assets/assets/js/champions/ChampionsListGridView.js" type="text/javascript"></script>,
 <script src="https://lolstatic-a.akamaihd.net/frontpage/apps/prod/LolGameInfo-Harbinger/en_US/0d258ed5be6806b967afaf2b4b9817406912a7ac/assets/assets/js/champions/ChampionsListApp.js" type="text/javascript"></script>]

soup = BeautifulSoup(requests.get("https://na.leagueoflegends.com/en/game-info/champions/").text, 'lxml')
soup.find_all('link')
Out[23]:
[<link href="https://lolstatic-a.akamaihd.net/lolkit/1.1.6/lol-kit.css" rel="stylesheet"/>,
 <link href="https://lolstatic-a.akamaihd.net/frontpage/apps/prod/LolGameInfo-Harbinger/en_US/0d258ed5be6806b967afaf2b4b9817406912a7ac/assets/assets/css/base-styles.css" rel="stylesheet"/>,
 <link href="https://lolstatic-a.akamaihd.net/lolkit/1.1.6/resources/images/favicon.ico" rel="SHORTCUT ICON"/>]
</code>```

Body:
i am new to webscaping and wanted to scape all the charackterportraits from the lol site and when i examined one of the pictures in the browser it was in a "img scr="url" tag and i want to get the url to download the picture but when i do soup.select('img[src]') or soup.select('img') it returns an empty list and i dont know why

here is the code:

data=requests.get(website)
data.raise_for_status()

soup = bs4.BeautifulSoup(data.text,"lxml")
print(soup)
#soup returns html

elems = soup.select('img[src]')
print(elems)
#elems returns an empty list

Body:
Here is your answer

import requests
from bs4 import BeautifulSoup

soup = BeautifulSoup(requests.get("https://na.leagueoflegends.com/en/game-info/champions/").text, 'lxml')
soup.find_all('link')    #these are your tags eg: a , script link

OUTPUT:
Out[21]:
[<a href="/en/game-info/get-started/">Get Started</a>,
 <a href="/en/game-info/get-started/what-is-lol/">What is League of Legends?</a>,
 <a href="https://na.leagueoflegends.com/en/site/guide/index.html">New Player Guide</a>,
 <a href="/en/game-info/get-started/chat-commands/">Chat Commands</a>,
 <a href="/en/game-info/get-started/community-interaction/">Community Interaction</a>,
 <a href="/en/featured/summoners-code">The Summoner's Code</a>,
 <a href="/en/game-info/champions/">Champions</a>,
 <a href="/en/game-info/items/">Items</a>,
 <a href="/en/game-info/summoners/">Summoners</a>,
 <a href="/en/game-info/summoners/spells/">Summoner Spells</a>,
 <a href="/en/game-info/game-modes/">Game Modes</a>,
 <a href="/en/game-info/game-modes/summoners-rift/">Summoner's Rift</a>,
 <a href="/en/game-info/game-modes/the-twisted-treeline/">The Twisted Treeline</a>,
 <a href="/en/game-info/game-modes/howling-abyss/">Howling Abyss</a>,
 <a href="//na.leagueoflegends.com/en/">Home</a>,
 <a href="/en/game-info/">Game Info</a>]

soup = BeautifulSoup(requests.get("https://na.leagueoflegends.com/en/game-info/champions/").text, 'lxml')
soup.find_all('script')
Out[22]:
soup = BeautifulSoup(requests.get("https://na.leagueoflegends.com/en/game-info/champions/").text, 'lxml')
soup.find_all('a')
[<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src='//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);})(window,document,'script','dataLayer','GTM-N98J');</script>,
 <script>window.ga = window.ga || function(){(ga.q=ga.q||[]).push(arguments)};ga.l = +new Date;</script>,
 <script src="https://lolstatic-a.akamaihd.net/lolkit/1.1.6/modernizr.js" type="text/javascript"></script>,
 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>,
 <script src="https://lolstatic-a.akamaihd.net/lolkit/1.1.6/riot-all.js" type="text/javascript"></script>,
 <script src="https://lolstatic-a.akamaihd.net/lolkit/1.1.6/riot-kit-all.js" type="text/javascript"></script>,
 <script type="text/javascript">rg_force_language = 'en_US';rg_force_manifest = 'https://ddragon.leagueoflegends.com/realms/na.js';rg_assets = 'https://lolstatic-a.akamaihd.net/game-info/1.1.9';</script>,
 <script type="text/javascript">window.riotBarConfig = {touchpoints: {activeTouchpoint: 'game'},locale: {landingUrlPattern : 'https://na.leagueoflegends.com//game-info/'},footer: {enabled: true,container: {renderFooterInto: '#footer'}}};</script>,
 <script async="" src="https://lolstatic-a.akamaihd.net/riotbar/prod/latest/en_US.js"></script>,
 <script src="https://ddragon.leagueoflegends.com/cdn/dragonhead.js" type="text/javascript"></script>,
 <script src="https://lolstatic-a.akamaihd.net/frontpage/apps/prod/LolGameInfo-Harbinger/en_US/0d258ed5be6806b967afaf2b4b9817406912a7ac/assets/assets/js/riot-dd-utils.js" type="text/javascript"></script>,
 <script src="https://lolstatic-a.akamaihd.net/frontpage/apps/prod/LolGameInfo-Harbinger/en_US/0d258ed5be6806b967afaf2b4b9817406912a7ac/assets/assets/js/riot-dd-i18n.js" type="text/javascript"></script>,
 <script src="https://lolstatic-a.akamaihd.net/frontpage/apps/prod/LolGameInfo-Harbinger/en_US/0d258ed5be6806b967afaf2b4b9817406912a7ac/assets/assets/js/external/jquery.lazy-load.js" type="text/javascript"></script>,
 <script src="https://lolstatic-a.akamaihd.net/frontpage/apps/prod/LolGameInfo-Harbinger/en_US/0d258ed5be6806b967afaf2b4b9817406912a7ac/assets/assets/js/DDFilterApp.js" type="text/javascript"></script>,
 <script src="https://lolstatic-a.akamaihd.net/frontpage/apps/prod/LolGameInfo-Harbinger/en_US/0d258ed5be6806b967afaf2b4b9817406912a7ac/assets/assets/js/DDMarkupItem.js" type="text/javascript"></script>,
 <script src="https://lolstatic-a.akamaihd.net/frontpage/apps/prod/LolGameInfo-Harbinger/en_US/0d258ed5be6806b967afaf2b4b9817406912a7ac/assets/assets/js/DDMarkupContainer.js" type="text/javascript"></script>,
 <script src="https://lolstatic-a.akamaihd.net/frontpage/apps/prod/LolGameInfo-Harbinger/en_US/0d258ed5be6806b967afaf2b4b9817406912a7ac/assets/assets/js/champions/ChampionsListGridItem.js" type="text/javascript"></script>,
 <script src="https://lolstatic-a.akamaihd.net/frontpage/apps/prod/LolGameInfo-Harbinger/en_US/0d258ed5be6806b967afaf2b4b9817406912a7ac/assets/assets/js/champions/ChampionsListGridView.js" type="text/javascript"></script>,
 <script src="https://lolstatic-a.akamaihd.net/frontpage/apps/prod/LolGameInfo-Harbinger/en_US/0d258ed5be6806b967afaf2b4b9817406912a7ac/assets/assets/js/champions/ChampionsListApp.js" type="text/javascript"></script>]

soup = BeautifulSoup(requests.get("https://na.leagueoflegends.com/en/game-info/champions/").text, 'lxml')
soup.find_all('link')
Out[23]:
[<link href="https://lolstatic-a.akamaihd.net/lolkit/1.1.6/lol-kit.css" rel="stylesheet"/>,
 <link href="https://lolstatic-a.akamaihd.net/frontpage/apps/prod/LolGameInfo-Harbinger/en_US/0d258ed5be6806b967afaf2b4b9817406912a7ac/assets/assets/css/base-styles.css" rel="stylesheet"/>,
 <link href="https://lolstatic-a.akamaihd.net/lolkit/1.1.6/resources/images/favicon.ico" rel="SHORTCUT ICON"/>]


问:
Can't create iPad XIB with Xcode 4
Alright, I have a problem on my hands.  I'm trying to convert my project to a universal app for the iPhone and the iPad.  I'm trying to make iPad-specific versions of my XIB files.  As it's not possible to Create iPad version using autoresizing masks in Xcode 4, I assumed I'd just be able to pop my XIB into Xcode 3's version of IB, and let that do the magic.  No dice.  Here's the error I get when i try to open the XIB file in IB:

To be honest I don't know where to proceed from here.  I guess I could just resize my views manually, but that's quite tedious and I don't really have the time to be doing that.  Any ideas?

答:
Alright, I figured out a rather convoluted workaround.

First of all, duplicate your original project and then select your project info.  Right-click on your target, and select "Duplicate".

Xcode will then pop up this message.  Select "Duplicate and Transition to iPad".

Now Xcode converts the XIB files for you, and presents you with an iPad resources folder.

Now open these XIB files in the Finder, and append ~ipad after the file name.

After doing so, copy these XIB files back into your original project, and then turn your project into a universal project by selecting from the dropdown in your target settings.

And you're done.  I found that really convoluted, and I'm hoping Apple can fix this huge usability problem soon.

Title: Can't create iPad XIB with Xcode 4


问:
How to find data length and index length of particular tables in a postgresql schema?
I am migrating my application from MySQL to PostgreSQL. In MySQL, I have used a query to calculate memory size of particular tables as per my requirement using the below Query,

```<code>SELECT SUM(ROUND(((DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024),2)) 'Size in MB' FROM  INFORMATION_SCHEMA.TABLES where TABLE_NAME like 'table_name';
</code>```

But I can't do the same in PostgreSQL. If I run the same query it is displaying as,

```<code>ERROR:  type "sum" does not exist
</code>```

And I have tried the solutions in various sites but I can't find the exact solution for my requirement. Please suggest me a solution. Thanks in advance.

答:
Your query  query is failing because you're using single quotes for alias - 'Size in MB'.

Further, there's no DATA_LENGTH and INDEX_LENGTH available in INFORMATION_SCHEMA.TABLES

In Postgres, you may use pg_total_relation_size function.

```<code>SELECT ROUND(pg_total_relation_size (oid) / ( 1024.0 * 1024.0 ), 2) AS
       "Size in MB"
FROM   pg_class c
WHERE  relname = 'table_name';
</code>```

Have a look at this answer to know the  functions available to get various sizes.

DEMO

Body:
I am migrating my application from MySQL to PostgreSQL. In MySQL, I have used a query to calculate memory size of particular tables as per my requirement using the below Query,

SELECT SUM(ROUND(((DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024),2)) 'Size in MB' FROM  INFORMATION_SCHEMA.TABLES where TABLE_NAME like 'table_name';

But I can't do the same in PostgreSQL. If I run the same query it is displaying as,

ERROR:  type "sum" does not exist

And I have tried the solutions in various sites but I can't find the exact solution for my requirement. Please suggest me a solution. Thanks in advance.

Body:
Your query  query is failing because you're using single quotes for alias - 'Size in MB'.

Further, there's no DATA_LENGTH and INDEX_LENGTH available in INFORMATION_SCHEMA.TABLES

In Postgres, you may use pg_total_relation_size function.

SELECT ROUND(pg_total_relation_size (oid) / ( 1024.0 * 1024.0 ), 2) AS
       "Size in MB"
FROM   pg_class c
WHERE  relname = 'table_name';

Have a look at this answer to know the  functions available to get various sizes.

DEMO