问:
Are list-comprehensions and functional functions faster than "for loops"?
In terms of performance in Python, is a list-comprehension, or functions like map(), filter() and reduce() faster than a for loop? Why, technically, they run in a C speed, while the for loop runs in the python virtual machine speed?.

Suppose that in a game that I'm developing I need to draw complex and huge maps using for loops. This question would be definitely relevant, for if a list-comprehension, for example, is indeed faster, it would be a much better option in order to avoid lags (Despite the visual complexity of the code).

答:
I modified @Alisa's code and used cProfile to show why list comprehension is faster:

```<code>from functools import reduce
import datetime

def reduce_(numbers):
    return reduce(lambda sum, next: sum + next * next, numbers, 0)

def for_loop(numbers):
    a = []
    for i in numbers:
        a.append(i*2)
    a = sum(a)
    return a

def map_(numbers):
    sqrt = lambda x: x*x
    return sum(map(sqrt, numbers))

def list_comp(numbers):
    return(sum([i*i for i in numbers]))

funcs = [
        reduce_,
        for_loop,
        map_,
        list_comp
        ]

if __name__ == "__main__":
    # [1, 2, 5, 3, 1, 2, 5, 3]
    import cProfile
    for f in funcs:
        print('=' * 25)
        print("Profiling:", f.__name__)
        print('=' * 25)
        pr = cProfile.Profile()
        for i in range(10**6):
            pr.runcall(f, [1, 2, 5, 3, 1, 2, 5, 3])
        pr.create_stats()
        pr.print_stats()
</code>```

Here's the results:

```<code>=========================
Profiling: reduce_
=========================
         11000000 function calls in 1.501 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
  1000000    0.162    0.000    1.473    0.000 profiling.py:4(reduce_)
  8000000    0.461    0.000    0.461    0.000 profiling.py:5(<lambda>)
  1000000    0.850    0.000    1.311    0.000 {built-in method _functools.reduce}
  1000000    0.028    0.000    0.028    0.000 {method 'disable' of '_lsprof.Profiler' objects}

=========================
Profiling: for_loop
=========================
         11000000 function calls in 1.372 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
  1000000    0.879    0.000    1.344    0.000 profiling.py:7(for_loop)
  1000000    0.145    0.000    0.145    0.000 {built-in method builtins.sum}
  8000000    0.320    0.000    0.320    0.000 {method 'append' of 'list' objects}
  1000000    0.027    0.000    0.027    0.000 {method 'disable' of '_lsprof.Profiler' objects}

=========================
Profiling: map_
=========================
         11000000 function calls in 1.470 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
  1000000    0.264    0.000    1.442    0.000 profiling.py:14(map_)
  8000000    0.387    0.000    0.387    0.000 profiling.py:15(<lambda>)
  1000000    0.791    0.000    1.178    0.000 {built-in method builtins.sum}
  1000000    0.028    0.000    0.028    0.000 {method 'disable' of '_lsprof.Profiler' objects}

=========================
Profiling: list_comp
=========================
         4000000 function calls in 0.737 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
  1000000    0.318    0.000    0.709    0.000 profiling.py:18(list_comp)
  1000000    0.261    0.000    0.261    0.000 profiling.py:19(<listcomp>)
  1000000    0.131    0.000    0.131    0.000 {built-in method builtins.sum}
  1000000    0.027    0.000    0.027    0.000 {method 'disable' of '_lsprof.Profiler' objects}

</code>```

IMHO:

reduce and map are in general pretty slow. Not only that, using sum on the iterators that map returned is slow, compared to suming a list

for_loop uses append, which is of course slow to some extent

list-comprehension not only spent the least time building the list, it also makes sum much quicker, in contrast to map

Body:
I modified @Alisa's code and used cProfile to show why list comprehension is faster:

from functools import reduce
import datetime

def reduce_(numbers):
    return reduce(lambda sum, next: sum + next * next, numbers, 0)

def for_loop(numbers):
    a = []
    for i in numbers:
        a.append(i*2)
    a = sum(a)
    return a

def map_(numbers):
    sqrt = lambda x: x*x
    return sum(map(sqrt, numbers))

def list_comp(numbers):
    return(sum([i*i for i in numbers]))

funcs = [
        reduce_,
        for_loop,
        map_,
        list_comp
        ]

if __name__ == "__main__":
    # [1, 2, 5, 3, 1, 2, 5, 3]
    import cProfile
    for f in funcs:
        print('=' * 25)
        print("Profiling:", f.__name__)
        print('=' * 25)
        pr = cProfile.Profile()
        for i in range(10**6):
            pr.runcall(f, [1, 2, 5, 3, 1, 2, 5, 3])
        pr.create_stats()
        pr.print_stats()

Here's the results:

=========================
Profiling: reduce_
=========================
         11000000 function calls in 1.501 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
  1000000    0.162    0.000    1.473    0.000 profiling.py:4(reduce_)
  8000000    0.461    0.000    0.461    0.000 profiling.py:5(<lambda>)
  1000000    0.850    0.000    1.311    0.000 {built-in method _functools.reduce}
  1000000    0.028    0.000    0.028    0.000 {method 'disable' of '_lsprof.Profiler' objects}

=========================
Profiling: for_loop
=========================
         11000000 function calls in 1.372 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
  1000000    0.879    0.000    1.344    0.000 profiling.py:7(for_loop)
  1000000    0.145    0.000    0.145    0.000 {built-in method builtins.sum}
  8000000    0.320    0.000    0.320    0.000 {method 'append' of 'list' objects}
  1000000    0.027    0.000    0.027    0.000 {method 'disable' of '_lsprof.Profiler' objects}

=========================
Profiling: map_
=========================
         11000000 function calls in 1.470 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
  1000000    0.264    0.000    1.442    0.000 profiling.py:14(map_)
  8000000    0.387    0.000    0.387    0.000 profiling.py:15(<lambda>)
  1000000    0.791    0.000    1.178    0.000 {built-in method builtins.sum}
  1000000    0.028    0.000    0.028    0.000 {method 'disable' of '_lsprof.Profiler' objects}

=========================
Profiling: list_comp
=========================
         4000000 function calls in 0.737 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
  1000000    0.318    0.000    0.709    0.000 profiling.py:18(list_comp)
  1000000    0.261    0.000    0.261    0.000 profiling.py:19(<listcomp>)
  1000000    0.131    0.000    0.131    0.000 {built-in method builtins.sum}
  1000000    0.027    0.000    0.027    0.000 {method 'disable' of '_lsprof.Profiler' objects}

IMHO:

reduce and map are in general pretty slow. Not only that, using sum on the iterators that map returned is slow, compared to suming a list

for_loop uses append, which is of course slow to some extent

list-comprehension not only spent the least time building the list, it also makes sum much quicker, in contrast to map


来源: stackexchange

问:
routing with model and variable
I am trying to test a custom email verification code with route model binding, when 2 wildcards are used, laravel always returns a 404.

this is my route in api.php

```<code>Route::get('/verify_contact_email/{id}/{hashed_key}', 'CustomEmailVerifyController@verifyContactEmail');
</code>```

this is the controller with verifyContactEmail

```<code>public function verifyContactEmail(UserContactEmailVerify $id, $hashed_key) { return $id; }
</code>```

when I remove the wildcard {hashed_key} and the $hashed_key, the model shows. I read up on laravel routing documentation, there is no mention of multiple wildcards or passing variable thru URL. Am I doing it wrong? Any help is much appreciated.

答: There is no problem with the route model binding or controller functions. The problem was discovered that routes do not work with hashing or bcrypt, str_random(20) has replaced the verification code instead of bcrypt.</p>

ContentLicense: CC BY-SA 4.0

Title: routing with model and variable

Body:
I am trying to test a custom email verification code with route model binding, when 2 wildcards are used, laravel always returns a 404.

this is my route in api.php

Route::get('/verify_contact_email/{id}/{hashed_key}', 'CustomEmailVerifyController@verifyContactEmail');

this is the controller with verifyContactEmail

public function verifyContactEmail(UserContactEmailVerify $id, $hashed_key) { return $id; }

when I remove the wildcard {hashed_key} and the $hashed_key, the model shows. I read up on laravel routing documentation, there is no mention of multiple wildcards or passing variable thru URL. Am I doing it wrong? Any help is much appreciated.


问:
Select a DataRow based on two column conditions
I have a datatable that I know to be empty at the beginning, but as I populate it, I am unable to get the datarow back out of it based on two column conditions, so it is doubling (or more) the number of rows.

I may be going about this wrong, but my aim is to check if the row exists in the datatable if the 2 conditions are met and then get the row if it does exist and modify it.

```<code>Dim qryB = From drw As DataRow In oDT_Signals.Rows
           Where drw("SG").Equals(sgrp) AndAlso drw("EP").Equals(srchPt)
           Select drw

Dim rslt = qryB.SingleOrDefault

If IsNothing(rslt) Then
    ed.WriteMessage("Row does not exist")
    oDT_Signals.Rows.Add(0, 0, sgrp, rw.sPt, PtList.IndexOf(rw.sPt), rw.ePt, PtList.IndexOf(rw.ePt), "NIL", rw.Len, 0, rw.Phases, rw.Size, rw.Core, rw.Ins, rw.Temp, rw.Table, "NIL",
                            "NA", "NA", rw.mVAm, ldVal, 0, 0, rw.Zs, 0, 0, rw.ePt.X, rw.ePt.Y, rw.mVtest, 0, 0, rw.Ztest, 0, 0)

Else
    ed.WriteMessage("Row does exist")
    Dim dr As DataRow = qryB.First
    dr("LOAD") = Math.Round(ldVal + dr("LOAD"), 2, MidpointRounding.AwayFromZero)
End If
</code>```

The issue is rslt is always nothing, when I know it is not the case. Is anyone able to help me determine what I am doing wrong that is is always adding the row? The console just keeps saying "Row does not Exist".

答:
I haven't tested to be sure but I'm fairly certain that the issue is your use of the Object.Equals method, which will test for reference equality. If value equality is what you're interested in, test for that.  Change this:

```<code>Where drw("SG").Equals(sgrp) AndAlso drw("EP").Equals(srchPt)
</code>```

to something like this:

```<code>Where drw.Field(Of String)("SG") = sgrp AndAlso drw.Field(Of String)("EP") = srchPt
</code>```

If the columns don't contain Strings, change the generic type of the Field calls.

Body:
I have a datatable that I know to be empty at the beginning, but as I populate it, I am unable to get the datarow back out of it based on two column conditions, so it is doubling (or more) the number of rows.

I may be going about this wrong, but my aim is to check if the row exists in the datatable if the 2 conditions are met and then get the row if it does exist and modify it.

Dim qryB = From drw As DataRow In oDT_Signals.Rows
           Where drw("SG").Equals(sgrp) AndAlso drw("EP").Equals(srchPt)
           Select drw

Dim rslt = qryB.SingleOrDefault

If IsNothing(rslt) Then
    ed.WriteMessage("Row does not exist")
    oDT_Signals.Rows.Add(0, 0, sgrp, rw.sPt, PtList.IndexOf(rw.sPt), rw.ePt, PtList.IndexOf(rw.ePt), "NIL", rw.Len, 0, rw.Phases, rw.Size, rw.Core, rw.Ins, rw.Temp, rw.Table, "NIL",
                            "NA", "NA", rw.mVAm, ldVal, 0, 0, rw.Zs, 0, 0, rw.ePt.X, rw.ePt.Y, rw.mVtest, 0, 0, rw.Ztest, 0, 0)

Else
    ed.WriteMessage("Row does exist")
    Dim dr As DataRow = qryB.First
    dr("LOAD") = Math.Round(ldVal + dr("LOAD"), 2, MidpointRounding.AwayFromZero)
End If

The issue is rslt is always nothing, when I know it is not the case. Is anyone able to help me determine what I am doing wrong that is is always adding the row? The console just keeps saying "Row does not Exist".

Body:
I haven't tested to be sure but I'm fairly certain that the issue is your use of the Object.Equals method, which will test for reference equality. If value equality is what you're interested in, test for that.  Change this:

Where drw("SG").Equals(sgrp) AndAlso drw("EP").Equals(srchPt)

to something like this:

Where drw.Field(Of String)("SG") = sgrp AndAlso drw.Field(Of String)("EP") = srchPt

If the columns don't contain Strings, change the generic type of the Field calls.


问:
Why does createNewFile() fail to create a file?
Following code fails and I'm at a complete loss.

```<code>File newFile = new File ("C:/users/user/desktop/fruit" + "apple" + ".csv");

if (!newFile.exists()) {
        System.out.println("file not found");
    if (newFile.createNewFile()) {
        System.out.println("file created");
    } else {
        System.out.println("file DOESN'T exist");
    }
}
FileWriter fw2 = new FileWriter(newFile);
</code>```

file not found
file DOESN'T exist

```<code>Exception in thread "main" java.io.FileNotFoundException: C:\users\user\desktop\fruit\apple.csv (The system cannot find the file specified)
    at java.base/java.io.FileOutputStream.open0(Native Method)
    at java.base/java.io.FileOutputStream.open(FileOutputStream.java:292)
    at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:235)
    at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:185)
    at java.base/java.io.FileWriter.<init>(FileWriter.java:96)
    at dataformatting/dataFormatting.formatting.main(formatting.java:47)
</code>```

答: You are making a file called fruitapple.csv on your desktop and trying to open a file called apple.csv in a folder called fruit on your desktop...

Body:
Following code fails and I'm at a complete loss.

File newFile = new File ("C:/users/user/desktop/fruit" + "apple" + ".csv");

if (!newFile.exists()) {
        System.out.println("file not found");
    if (newFile.createNewFile()) {
        System.out.println("file created");
    } else {
        System.out.println("file DOESN'T exist");
    }
}
FileWriter fw2 = new FileWriter(newFile);

file not found
file DOESN'T exist

Exception in thread "main" java.io.FileNotFoundException: C:\users\user\desktop\fruit\apple.csv (The system cannot find the file specified)
    at java.base/java.io.FileOutputStream.open0(Native Method)
    at java.base/java.io.FileOutputStream.open(FileOutputStream.java:292)
    at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:235)
    at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:185)
    at java.base/java.io.FileWriter.<init>(FileWriter.java:96)
    at dataformatting/dataFormatting.formatting.main(formatting.java:47)


问:
What is the alternative to rewritebase on IIS?
I have the following .htaccess file in my website - I'm having to relocate it to a windows IIS machine and having problems with using the URL-Rewrite module to import a rule and create a web.config file.

This is my .htaccess file

```<code># Rewrite URLs and direct to index.php

<IfModule mod_rewrite.c>
Options -Multiviews
RewriteEngine On
##
## If moving host, change the structure below to folder name of where public folder lives
##
RewriteBase /foldername/public
##
##
##
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule  ^(.+)$ index.php?url=$1 [QSA,L]
</IfModule>
</code>```

It fails on the RewriteBase but this I understand in the .htaccess file is crucial for my site to function.

Can anyone give me anyone pointers?

答:
The RewriteBase directive sets the URL-path for relative path substitutions (overriding the directory-prefix). So, instead of using the RewriteBase directive, you could simply hardcode the URL-path in the RewriteRule directive itself.

For example:

```<code>RewriteRule  ^(.+)$ /foldername/public/index.php?url=$1 [QSA,L]
</code>```

Body:
I have the following .htaccess file in my website - I'm having to relocate it to a windows IIS machine and having problems with using the URL-Rewrite module to import a rule and create a web.config file.

This is my .htaccess file

# Rewrite URLs and direct to index.php

<IfModule mod_rewrite.c>
Options -Multiviews
RewriteEngine On
##
## If moving host, change the structure below to folder name of where public folder lives
##
RewriteBase /foldername/public
##
##
##
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule  ^(.+)$ index.php?url=$1 [QSA,L]
</IfModule>

It fails on the RewriteBase but this I understand in the .htaccess file is crucial for my site to function.

Can anyone give me anyone pointers?

Body:
The RewriteBase directive sets the URL-path for relative path substitutions (overriding the directory-prefix). So, instead of using the RewriteBase directive, you could simply hardcode the URL-path in the RewriteRule directive itself.

For example:

RewriteRule  ^(.+)$ /foldername/public/index.php?url=$1 [QSA,L]


问:
How remove item by ID from localStorage?
Greeting. Do I need
Example:

```<code>items: [{id:1 , name: test} , {id: 2 , name: test2} , {id:3 , name: test3}];
</code>```

I want to delete example item where id 3?

答:
When you use getItem, you get back a string. Assuming what you've stored it valid JSON, you can convert it to an actual object by doing JSON.parse(string).

Once you have the object, you can do:

```<code>const newData = result.filter(x => x.id !== 3);
</code>```

Now you can save this result back to localStorage.

Body:
Greeting. Do I need
Example:

items: [{id:1 , name: test} , {id: 2 , name: test2} , {id:3 , name: test3}];

I want to delete example item where id 3?

Body:
When you use getItem, you get back a string. Assuming what you've stored it valid JSON, you can convert it to an actual object by doing JSON.parse(string).

Once you have the object, you can do:

const newData = result.filter(x => x.id !== 3);

Now you can save this result back to localStorage.


问:
Required field 'client_protocol' is unset
I am using Hive 0.12, and I'm trying the JDBC from apache. When I try to run the code, I get apache.thrift.TApplicationException.

```<code>import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;

public class HiveJdbcClient {
private static String driverName = "org.apache.hive.jdbc.HiveDriver";

/**
 * @param args
 * @throws SQLException
 */
public static void main(String[] args) throws SQLException {
    try {
        Class.forName(driverName);
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.exit(1);
    }
    //replace "hive" here with the name of the user the queries should run as
    Connection con =      DriverManager.getConnection("jdbc:hive2://localhost:10000/default", "hive", "");
    Statement stmt = con.createStatement();
    String tableName = "testHiveDriverTable";
    stmt.execute("drop table if exists " + tableName);
    stmt.execute("create table " + tableName + " (key int, value string)");
    // show tables
    String sql = "show tables '" + tableName + "'";
    System.out.println("Running: " + sql);
    ResultSet res = stmt.executeQuery(sql);
    if (res.next()) {
        System.out.println(res.getString(1));
    }
    // describe table
    sql = "describe " + tableName;
    System.out.println("Running: " + sql);
    res = stmt.executeQuery(sql);
    while (res.next()) {
        System.out.println(res.getString(1) + "\t" + res.getString(2));
    }

    // load data into table
    // NOTE: filepath has to be local to the hive server
    // NOTE: /tmp/a.txt is a ctrl-A separated file with two fields per line
    String filepath = "/tmp/a.txt";
    sql = "load data local inpath '" + filepath + "' into table " + tableName;
    System.out.println("Running: " + sql);
    stmt.execute(sql);

    // select * query
    sql = "select * from " + tableName;
    System.out.println("Running: " + sql);
    res = stmt.executeQuery(sql);
    while (res.next()) {
        System.out.println(String.valueOf(res.getInt(1)) + "\t" + res.getString(2));
    }

    // regular hive query
    sql = "select count(1) from " + tableName;
    System.out.println("Running: " + sql);
    res = stmt.executeQuery(sql);
    while (res.next()) {
        System.out.println(res.getString(1));
    }
}
</code>```

}

I imported all the necessary jars, and when I try to run my code, I get the following error:

```<code> org.apache.thrift.TApplicationException: Required field 'client_protocol' is unset!   Struct:TOpenSessionReq(client_protocol:null)
</code>```

How can I fix this?

答:
I have faces same issue. I fixed this by following below steps.

```<code><dependency>
    <groupId>org.apache.hive</groupId>
    <artifactId>hive-jdbc</artifactId>
    <version>1.1.0</version>
    <classifier>standalone</classifier>
    <exclusions>
        <exclusion>
            <groupId>org.eclipse.jetty.aggregate</groupId>
            <artifactId>*</artifactId>
        </exclusion>
    </exclusions>
</dependency>
</code>```

Make sure that, you add above dependency.

Class.forName("org.apache.hive.jdbc.HiveDriver");
Add this before calling DriverManager.getConnection(url);

Represent hive url by IP instead of hostname.

ContentLicense: CC BY-SA 3.0

Body:
I am using Hive 0.12, and I'm trying the JDBC from apache. When I try to run the code, I get apache.thrift.TApplicationException.

import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;

public class HiveJdbcClient {
private static String driverName = "org.apache.hive.jdbc.HiveDriver";

/**
 * @param args
 * @throws SQLException
 */
public static void main(String[] args) throws SQLException {
    try {
        Class.forName(driverName);
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.exit(1);
    }
    //replace "hive" here with the name of the user the queries should run as
    Connection con =      DriverManager.getConnection("jdbc:hive2://localhost:10000/default", "hive", "");
    Statement stmt = con.createStatement();
    String tableName = "testHiveDriverTable";
    stmt.execute("drop table if exists " + tableName);
    stmt.execute("create table " + tableName + " (key int, value string)");
    // show tables
    String sql = "show tables '" + tableName + "'";
    System.out.println("Running: " + sql);
    ResultSet res = stmt.executeQuery(sql);
    if (res.next()) {
        System.out.println(res.getString(1));
    }
    // describe table
    sql = "describe " + tableName;
    System.out.println("Running: " + sql);
    res = stmt.executeQuery(sql);
    while (res.next()) {
        System.out.println(res.getString(1) + "\t" + res.getString(2));
    }

    // load data into table
    // NOTE: filepath has to be local to the hive server
    // NOTE: /tmp/a.txt is a ctrl-A separated file with two fields per line
    String filepath = "/tmp/a.txt";
    sql = "load data local inpath '" + filepath + "' into table " + tableName;
    System.out.println("Running: " + sql);
    stmt.execute(sql);

    // select * query
    sql = "select * from " + tableName;
    System.out.println("Running: " + sql);
    res = stmt.executeQuery(sql);
    while (res.next()) {
        System.out.println(String.valueOf(res.getInt(1)) + "\t" + res.getString(2));
    }

    // regular hive query
    sql = "select count(1) from " + tableName;
    System.out.println("Running: " + sql);
    res = stmt.executeQuery(sql);
    while (res.next()) {
        System.out.println(res.getString(1));
    }
}

}

I imported all the necessary jars, and when I try to run my code, I get the following error:

 org.apache.thrift.TApplicationException: Required field 'client_protocol' is unset!   Struct:TOpenSessionReq(client_protocol:null)

How can I fix this?

Body:
I have faces same issue. I fixed this by following below steps.

<dependency>
    <groupId>org.apache.hive</groupId>
    <artifactId>hive-jdbc</artifactId>
    <version>1.1.0</version>
    <classifier>standalone</classifier>
    <exclusions>
        <exclusion>
            <groupId>org.eclipse.jetty.aggregate</groupId>
            <artifactId>*</artifactId>
        </exclusion>
    </exclusions>
</dependency>

Make sure that, you add above dependency.

Class.forName("org.apache.hive.jdbc.HiveDriver");
Add this before calling DriverManager.getConnection(url);

Represent hive url by IP instead of hostname.


答:
I think you can consider it as remove element on array, first you need find index of element has id = 3. Let's try

```<code>let items = [{id:1 , name: test} , {id: 2 , name: test2} , {id:3 , name: test3}];
let index = items.findIndex(element => element.id === 3)

items = items.splice(index, 1)
</code>```

After you changed items, you can update localStore with new data.

Title: How remove item by ID from localStorage?

Body:
I think you can consider it as remove element on array, first you need find index of element has id = 3. Let's try

let items = [{id:1 , name: test} , {id: 2 , name: test2} , {id:3 , name: test3}];
let index = items.findIndex(element => element.id === 3)

items = items.splice(index, 1)

After you changed items, you can update localStore with new data.


问:
How to delete a row from DOM on button click in JavaScript?
I have an HTML code as shown below. I have copied this from DOM and it is generated through php.

```<code><div class="house-senate-committee" style="text-align:center; margin-top:15px;">
    <button type="button" onclick="removeRow()" name="delete_row[]" style="margin-right:10px;" value="">Delete</button>
    <input type="text" name="code[]" style="margin-right:10px;" value="AEFA">
    <input type="text" name="en_desc[]" value="Foreign Affairs and International Trade">
</div>

<div class="house-senate-committee" style="text-align:center; margin-top:15px;">
    <button type="button" onclick="removeRow()" name="delete_row[]" style="margin-right:10px;" value="">Delete</button>
    <input type="text" name="code[]" style="margin-right:10px;" value="AGFO">
    <input type="text" name="en_desc[]" value="Agriculture and Forestry">
</div>
</code>```

The above HTML code belongs to the following screenshot:

Problem Statement:

I am wondering what JS code I need to add so that on clicking Delete button it deletes a row containing that button.

For example: If I click the 2nd row Delete button, it should delete the complete 2nd row and if I click the 1st row Delete
button, it should delete the complete 1st row.

This is what I have tried:

```<code>function removeRow() {
 document.queryselector(".house-senate-committee").remove();
}
</code>```

答:
You can use closest to achieve it like below

function removeRow(element) {
  $(element).closest('.house-senate-committee').remove();
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="house-senate-committee" style="text-align:center; margin-top:15px;">
    <button type="button" onclick="removeRow(this)" name="delete_row[]" style="margin-right:10px;" value="">Delete</button>
    <input type="text" name="code[]" style="margin-right:10px;" value="AEFA">
    <input type="text" name="en_desc[]" value="Foreign Affairs and International Trade">
</div>

<div class="house-senate-committee" style="text-align:center; margin-top:15px;">
    <button type="button" onclick="removeRow(this)" name="delete_row[]" style="margin-right:10px;" value="">Delete</button>
    <input type="text" name="code[]" style="margin-right:10px;" value="AGFO">
    <input type="text" name="en_desc[]" value="Agriculture and Forestry">
</div></code>```

Body:
I have an HTML code as shown below. I have copied this from DOM and it is generated through php.

    <button type="button" onclick="removeRow()" name="delete_row[]" style="margin-right:10px;" value="">Delete</button>
    <input type="text" name="code[]" style="margin-right:10px;" value="AEFA">
    <input type="text" name="en_desc[]" value="Foreign Affairs and International Trade">

    <button type="button" onclick="removeRow()" name="delete_row[]" style="margin-right:10px;" value="">Delete</button>
    <input type="text" name="code[]" style="margin-right:10px;" value="AGFO">
    <input type="text" name="en_desc[]" value="Agriculture and Forestry">

The above HTML code belongs to the following screenshot:

Problem Statement:

I am wondering what JS code I need to add so that on clicking Delete button it deletes a row containing that button.

For example: If I click the 2nd row Delete button, it should delete the complete 2nd row and if I click the 1st row Delete
button, it should delete the complete 1st row.

This is what I have tried:

function removeRow() {
 document.queryselector(".house-senate-committee").remove();
}

Body:
You can use closest to achieve it like below

function removeRow(element) {
  $(element).closest('.house-senate-committee').remove();
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="house-senate-committee" style="text-align:center; margin-top:15px;">
    <button type="button" onclick="removeRow(this)" name="delete_row[]" style="margin-right:10px;" value="">Delete</button>
    <input type="text" name="code[]" style="margin-right:10px;" value="AEFA">
    <input type="text" name="en_desc[]" value="Foreign Affairs and International Trade">
</div>

<div class="house-senate-committee" style="text-align:center; margin-top:15px;">
    <button type="button" onclick="removeRow(this)" name="delete_row[]" style="margin-right:10px;" value="">Delete</button>
    <input type="text" name="code[]" style="margin-right:10px;" value="AGFO">
    <input type="text" name="en_desc[]" value="Agriculture and Forestry">
</div>


问:
How can I organize my data into a new table
I am trying to organize my data with a new table that must contain three columns respectively: date, ticker and value. I have this code below:

```<code>import pandas as pd
import numpy as np
from pandas_datareader import data as wb
stocks = ['CTKA3.SA', 'PNVL3.SA', 'SHUL4.SA', 'SMTO3.SA']
initial_dt = '2019-01-01'
data = wb.DataReader(stocks, data_source='yahoo', start = initial_dt)['Adj Close']
</code>```

The code gives me a table that contains for each ticker a column and a date column with the value in that day. I want just one column of ticker and not for each ticker.

Thanks.

Body:
I am trying to organize my data with a new table that must contain three columns respectively: date, ticker and value. I have this code below:

import pandas as pd
import numpy as np
from pandas_datareader import data as wb
stocks = ['CTKA3.SA', 'PNVL3.SA', 'SHUL4.SA', 'SMTO3.SA']
initial_dt = '2019-01-01'
data = wb.DataReader(stocks, data_source='yahoo', start = initial_dt)['Adj Close']

The code gives me a table that contains for each ticker a column and a date column with the value in that day. I want just one column of ticker and not for each ticker.

Thanks.

Body:
Adding

data=data.stack()

produces

Date        Symbols
2019-01-02  CTKA3.SA      4.890000
            PNVL3.SA    306.722626
            SHUL4.SA      6.837185
            SMTO3.SA     17.927473
2019-01-03  CTKA3.SA      4.890000
                           ...
2020-02-13  SMTO3.SA     27.850000
2020-02-14  CTKA3.SA     12.680000
            PNVL3.SA    690.000000
            SHUL4.SA     14.390000
            SMTO3.SA     28.799999
Length: 1116, dtype: float64


问:
Finding most common occurrence of a character that follows another
I'm currently working on a small piece of code and I seem to have run into a roadblock. I was wondering if it's possible to (because I cannot, for the life of me, figure it out) find the most common occurrence of a character that follows a specific character or string?

For example, say I have the following sentence:

```<code>"this is a test sentence that happens to be short"
</code>```

How would could I determine, for example, the most common character that occurs after the letter h?

In this specific example, doing it by hand, I get something like this:

{"i": 1, "a": 2, "o": 1}

I'd then like to be able to get the key of the highest value--in this case, a.

Using Counter from collections, I've been able to find the most common occurrence of a specific word or character, but I'm not sure how to do this specific implementation of doing the most common occurrence after. Any help would be greatly appreciated, thanks!

(The code I wrote to find the most common occurrence of a letter in a file:
 Counter(text).most_common(1), which does include white spaces )

EDIT:

How would this be done with words? For example, if I had the sentence: "whales are super neat, but whales don't make good pets. whales are cool."
How would I find the most common character that occurs after the words whales?
In this instance, removing white spaces, the most common character would be a

答:
Just split them by your character and then get the letter after it</p>

```<code>import collections
sentence = "this is a test sentence that happens to be short"
character = 'h'
letters_after_some_character = [part[0] for part in str.split(character)[1:] if part[0].isalpha()]
print(collections.Counter(letters_after_some_character).most_common())
</code>```

Body:
I'm currently working on a small piece of code and I seem to have run into a roadblock. I was wondering if it's possible to (because I cannot, for the life of me, figure it out) find the most common occurrence of a character that follows a specific character or string?

For example, say I have the following sentence:

"this is a test sentence that happens to be short"

How would could I determine, for example, the most common character that occurs after the letter h?

In this specific example, doing it by hand, I get something like this:

{"i": 1, "a": 2, "o": 1}

I'd then like to be able to get the key of the highest value--in this case, a.

Using Counter from collections, I've been able to find the most common occurrence of a specific word or character, but I'm not sure how to do this specific implementation of doing the most common occurrence after. Any help would be greatly appreciated, thanks!

(The code I wrote to find the most common occurrence of a letter in a file:
 Counter(text).most_common(1), which does include white spaces )

EDIT:

How would this be done with words? For example, if I had the sentence: "whales are super neat, but whales don't make good pets. whales are cool."
How would I find the most common character that occurs after the words whales?
In this instance, removing white spaces, the most common character would be a

Body:
Just split them by your character and then get the letter after it

import collections
sentence = "this is a test sentence that happens to be short"
character = 'h'
letters_after_some_character = [part[0] for part in str.split(character)[1:] if part[0].isalpha()]
print(collections.Counter(letters_after_some_character).most_common())


问:
MySQL INSERT into with COUNT() and GROUP BY error `Invalid use of group function`
I am trying to do the following query and get an error that it is an invalid use of group by function.  Any ideas?

insert into table_a(id, date, product)
select id, date, count(product)
from table_b
group by id, date
on duplicate key update product = count(product)
```

答:
I don't kow what you try to accomplish, sanatizing query helps only marginaly, but you can't use count( as you notced but you can make a corect SELECT that gives you the desired result.

```<code>insert into table_a(id, date, product)
select id, date, count(product)
from table_b
group by id, date
on duplicate key update product =
    (SELECT COUNT(product) FROM table_b WHERE id = VALUES(id)  GROUP BY id, date LIMIt 1) t1
</code>```

This is of course only an example how you can do something leike you want, but of course you have to adept to your needs

Body:
I don't kow what you try to accomplish, sanatizing query helps only marginaly, but you can't use count( as you notced but you can make a corect SELECT that gives you the desired result.

insert into table_a(id, date, product)
select id, date, count(product)
from table_b
group by id, date
on duplicate key update product =
    (SELECT COUNT(product) FROM table_b WHERE id = VALUES(id)  GROUP BY id, date LIMIt 1) t1

This is of course only an example how you can do something leike you want, but of course you have to adept to your needs


问:
C and derived data types?
I know the fundamental data types in C - char, int, float etc. But What exactly are derived data types in C language?

答:
Data types that are derived from fundamental data types are called derived data types. Derived data types don't create a new data type but,instead they add some functionality to the basic data types.

In C, two derived data type are : Array & Pointer.

Array : An array is a collection of variables of same type. They are stored in contagious memory allocation.

e.g

```<code>int a[10];
char chi [20];
</code>```

Pointer :

A pointer is a special variable that holds a memory address (location in memory) of another variable.

```<code>int i=10;
int *j;
j=&i;
</code>```

Here, j is a integer pointer as it holds an address of an integer variable i.

Title: C and derived data types?

Body:
Data types that are derived from fundamental data types are called derived data types. Derived data types don't create a new data type but,instead they add some functionality to the basic data types.

In C, two derived data type are : Array & Pointer.

Array : An array is a collection of variables of same type. They are stored in contagious memory allocation.

e.g

int a[10];
char chi [20];

Pointer :

A pointer is a special variable that holds a memory address (location in memory) of another variable.

int i=10;
int *j;
j=&i;

Here, j is a integer pointer as it holds an address of an integer variable i.


问:
How does Angular detect changes and update the component view?
Using a new angular project:

Consider the following app.component.ts (notice the redSelected boolean)

```<code>import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  redSelected = false;

}
</code>```

It has the following app.component.html

```<code><h2>Choose:</h2>
<label>
  <input type="radio" name="color" value="red" (change)="redSelected = true">
  <span>Red</span>
</label>
<label>
  <input type="radio" name="color" value="blue" (change)="redSelected = false">
  <span>Blue</span>
</label>
<p *ngIf="redSelected">Whoooorraay! Red is here.</p>
</code>```

Whenever the Red radiobox is selected, the final paragraph appears. When the Blue radiobox is selected the paragraph disappears.

How is it possible that angular detects this change and updates the component html view?

Note: I tried replacing the final paragraph with

```<code><p [ngClass]="hideOrShow()">Whoooorraay! Red is here.</p>
</code>```

where hideOrShow() is defined (inside app.component.ts) as the following:

```<code>hideOrShow() {
  if (this.redSelected === false) return 'hidden';
}
</code>```

and hidden is a css class (inside app.component.css)

```<code>.hidden {
    display: none;
}
</code>```

The same behavior continues... Angular is able to detect the change in the value of redSelected and updates the component view html automatically.

My question is: what triggers this re-evaluation? what is being re-evaluated exactly?

Thanks in advance!

答: Each angular component has attached change detector that test changes in templates variables. You can read more about it here</p>

Body:
Using a new angular project:

Consider the following app.component.ts (notice the redSelected boolean)

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  redSelected = false;

}

It has the following app.component.html

Choose:

  <input type="radio" name="color" value="red" (change)="redSelected = true">
  Red

  <input type="radio" name="color" value="blue" (change)="redSelected = false">
  Blue

Whoooorraay! Red is here.

Whenever the Red radiobox is selected, the final paragraph appears. When the Blue radiobox is selected the paragraph disappears.

How is it possible that angular detects this change and updates the component html view?

Note: I tried replacing the final paragraph with

Whoooorraay! Red is here.

where hideOrShow() is defined (inside app.component.ts) as the following:

hideOrShow() {
  if (this.redSelected === false) return 'hidden';
}

and hidden is a css class (inside app.component.css)

.hidden {
    display: none;
}

The same behavior continues... Angular is able to detect the change in the value of redSelected and updates the component view html automatically.

My question is: what triggers this re-evaluation? what is being re-evaluated exactly?

Thanks in advance!


问:
Beautiful Soup and Table Scraping - lxml vs html parser
I'm trying to extract the HTML code of a table from a webpage using BeautifulSoup.

```<code><table class="facts_label" id="facts_table">...</table>
</code>```

I would like to know why the code bellow works with the "html.parser" and prints back none if I change "html.parser" for "lxml".

```<code>#! /usr/bin/python

from bs4 import BeautifulSoup
from urllib import urlopen

webpage = urlopen('http://www.thewebpage.com')
soup=BeautifulSoup(webpage, "html.parser")
table = soup.find('table', {'class' : 'facts_label'})
print table
</code>```

答:
Short answer.

If you already installed lxml, just use it.

html.parser - BeautifulSoup(markup, "html.parser")

Advantages: Batteries included, Decent speed, Lenient (as of Python
2.7.3 and 3.2.)

Disadvantages: Not very lenient (before Python 2.7.3 or 3.2.2)

lxml - BeautifulSoup(markup, "lxml")

Advantages: Very fast, Lenient

Disadvantages: External C dependency

html5lib - BeautifulSoup(markup, "html5lib")

Advantages: Extremely lenient, Parses pages the same way a web browser does, Creates valid HTML5

Disadvantages: Very slow, External Python dependency

Body:
I'm trying to extract the HTML code of a table from a webpage using BeautifulSoup.

...

I would like to know why the code bellow works with the "html.parser" and prints back none if I change "html.parser" for "lxml".

#! /usr/bin/python

from bs4 import BeautifulSoup
from urllib import urlopen

webpage = urlopen('http://www.thewebpage.com')
soup=BeautifulSoup(webpage, "html.parser")
table = soup.find('table', {'class' : 'facts_label'})
print table


问:
Recursion for my code doesn't seem to work even though I am returning everything
I am making code to check if a number is a prime or not, but I want the argument to be both list and integer. For that I'm using recursion but the recursion doesn't seem to work.</p>

```<code> def prime_checker(suspected_prime):
    if type(suspected_prime) == type(list()):
        result_list = list()
        for x in range(len(suspected_prime)):
            result_list.append(prime_checker(suspected_prime[x]))
        return(result_list)
    else:
        prime_factor, factors, suspected_prime = 2, 0, abs(suspected_prime)
        while factors < 1:
            if suspected_prime % prime_factor == 0:
                factors += 1
            if math.ceil(suspected_prime**0.5) == prime_factor:
                if factors == 0:
                    return True
                else:
                    return False
            prime_factor += 1
</code>```

答:
Both your list case and int case are too complicated if not broken, let's see what we can do.  First, use isinstance to test types.  Next, you don't need to loop over the indexes, you can more easily loop over the content.

As far as the int clause, you continue doing math after the answer is already known -- don't waste time this way when it comes to primes.  Also, this style of prime testing is always simpler if we treat numbers 2 and less, and evens, as special cases and focus on testing odd divisors only:

```<code>def prime_checker(suspected_primes):
    if isinstance(suspected_primes, list):
        return [prime_checker(entity) for entity in suspected_primes]

    suspected_prime = abs(suspected_primes)

    if suspected_prime < 2:
        return False

    if suspected_prime % 2 == 0:
        return suspected_prime == 2

    for divisor in range(3, int(suspected_prime**0.5) + 1, 2):
        if suspected_prime % divisor == 0:
            return False

    return True

print(prime_checker([1, [2, 3], 4, [5, 6, [7, 8], 9]]))
print(prime_checker(13))
</code>```

OUTPUT

```<code>> python3 test.py
[False, [True, True], False, [True, False, [True, False], False]]
True
>
</code>```

Body:
I am making code to check if a number is a prime or not, but I want the argument to be both list and integer. For that I'm using recursion but the recursion doesn't seem to work.

 def prime_checker(suspected_prime):
    if type(suspected_prime) == type(list()):
        result_list = list()
        for x in range(len(suspected_prime)):
            result_list.append(prime_checker(suspected_prime[x]))
        return(result_list)
    else:
        prime_factor, factors, suspected_prime = 2, 0, abs(suspected_prime)
        while factors < 1:
            if suspected_prime % prime_factor == 0:
                factors += 1
            if math.ceil(suspected_prime**0.5) == prime_factor:
                if factors == 0:
                    return True
                else:
                    return False
            prime_factor += 1

Body:
Both your list case and int case are too complicated if not broken, let's see what we can do.  First, use isinstance to test types.  Next, you don't need to loop over the indexes, you can more easily loop over the content.

As far as the int clause, you continue doing math after the answer is already known -- don't waste time this way when it comes to primes.  Also, this style of prime testing is always simpler if we treat numbers 2 and less, and evens, as special cases and focus on testing odd divisors only:

def prime_checker(suspected_primes):
    if isinstance(suspected_primes, list):
        return [prime_checker(entity) for entity in suspected_primes]

    suspected_prime = abs(suspected_primes)

    if suspected_prime < 2:
        return False

    if suspected_prime % 2 == 0:
        return suspected_prime == 2

    for divisor in range(3, int(suspected_prime**0.5) + 1, 2):
        if suspected_prime % divisor == 0:
            return False

    return True

print(prime_checker([1, [2, 3], 4, [5, 6, [7, 8], 9]]))
print(prime_checker(13))

OUTPUT

> python3 test.py
[False, [True, True], False, [True, False, [True, False], False]]
True
>


问:
How to find part numbers that have a certain string in their description
I have a dataset with two columns, "PartNumber" and "OperationDescription".

I am trying to find all of the part numbers that have the word "Debulk in their description. Then, I want to find the number of times that occurs for each part number. I am using R, any advice?

答:
Try this:</p>

```<code>
library(tidyverse)

df %>%
   filter(str_detect(OperationDescription, 'Debulk')) %>%
   group_by(PartNumber) %>%
   count()

</code>```

Body:
Try this:

library(tidyverse)

df %>%
   filter(str_detect(OperationDescription, 'Debulk')) %>%
   group_by(PartNumber) %>%
   count()


问:
Get the value of options in php Drupal Form

update

I have made a Drupal Form with the Form API. I used a the element select, my question how can I get the value that the user chose?
For the example: the following code. How can I know what the user chose for 'Titles plus teaser' (teaser). Thanks!

```<code>form ['planning_detail']['study_groep'] = array (
   '# type =' select ',
   '# title' = 'Pick a value',
   '# options' => array (
   'title' => t ('Titles only '),
   'teaser' => t ('Titles plus teaser'),
   'full text' => t ('Full text '),
   )
);
</code>```

答:
```<code>function my_form_submit(&$form, &$form_state) {
  $field_key = $form_state['values']['field_name']
  $value = $form['field_name']['#options'][$field_key];
  drupal_set_message($value);
}
</code>```

The correct attribute '#options' must in ''

Body:
update

I have made a Drupal Form with the Form API. I used a the element select, my question how can I get the value that the user chose?
For the example: the following code. How can I know what the user chose for 'Titles plus teaser' (teaser). Thanks!

form ['planning_detail']['study_groep'] = array (
   '# type =' select ',
   '# title' = 'Pick a value',
   '# options' => array (
   'title' => t ('Titles only '),
   'teaser' => t ('Titles plus teaser'),
   'full text' => t ('Full text '),
   )
);

Body:
function my_form_submit(&$form, &$form_state) {
  $field_key = $form_state['values']['field_name']
  $value = $form['field_name']['#options'][$field_key];
  drupal_set_message($value);
}

The correct attribute '#options' must in ''


问:
How to update single value inside specific array item in redux
I have an issue where re-rendering of state causes ui issues and was suggested to only update specific value inside my reducer to reduce amount of re-rendering on a page.

this is example of my state

```<code>{
 name: "some name",
 subtitle: "some subtitle",
 contents: [
   {title: "some title", text: "some text"},
   {title: "some other title", text: "some other text"}
 ]
}
</code>```

and I am currently updating it like this

```<code>case 'SOME_ACTION':
   return { ...state, contents: action.payload }
</code>```

where action.payload is a whole array containing new values. But now I actually just need to update text of second item in contents array, and something like this doesn't work

```<code>case 'SOME_ACTION':
   return { ...state, contents[1].text: action.payload }
</code>```

where action.payload is now a text I need for update.

答:
This is how I did it for one of my projects:</p>

```<code>const markdownSaveActionCreator = (newMarkdownLocation, newMarkdownToSave) => ({
  type: MARKDOWN_SAVE,
  saveLocation: newMarkdownLocation,
  savedMarkdownInLocation: newMarkdownToSave
});

const markdownSaveReducer = (state = MARKDOWN_SAVED_ARRAY_DEFAULT, action) => {
  let objTemp = {
    saveLocation: action.saveLocation,
    savedMarkdownInLocation: action.savedMarkdownInLocation
  };

  switch(action.type) {
    case MARKDOWN_SAVE:
      return(
        state.map(i => {
          if (i.saveLocation === objTemp.saveLocation) {
            return Object.assign({}, i, objTemp);
          }
          return i;
        })
      );
    default:
      return state;
  }
};
</code>```

Body:
I have an issue where re-rendering of state causes ui issues and was suggested to only update specific value inside my reducer to reduce amount of re-rendering on a page.

this is example of my state

{
 name: "some name",
 subtitle: "some subtitle",
 contents: [
   {title: "some title", text: "some text"},
   {title: "some other title", text: "some other text"}
 ]
}

and I am currently updating it like this

case 'SOME_ACTION':
   return { ...state, contents: action.payload }

where action.payload is a whole array containing new values. But now I actually just need to update text of second item in contents array, and something like this doesn't work

case 'SOME_ACTION':
   return { ...state, contents[1].text: action.payload }

where action.payload is now a text I need for update.

Body:
This is how I did it for one of my projects:

const markdownSaveActionCreator = (newMarkdownLocation, newMarkdownToSave) => ({
  type: MARKDOWN_SAVE,
  saveLocation: newMarkdownLocation,
  savedMarkdownInLocation: newMarkdownToSave
});

const markdownSaveReducer = (state = MARKDOWN_SAVED_ARRAY_DEFAULT, action) => {
  let objTemp = {
    saveLocation: action.saveLocation,
    savedMarkdownInLocation: action.savedMarkdownInLocation
  };

  switch(action.type) {
    case MARKDOWN_SAVE:
      return(
        state.map(i => {
          if (i.saveLocation === objTemp.saveLocation) {
            return Object.assign({}, i, objTemp);
          }
          return i;
        })
      );
    default:
      return state;
  }
};


问:
Why can I mutably borrow separate fields from a Box, but not other ref types?
With a simple object, I can get mutable references to individual fields:

```<code>struct MyObject {
    pub a: i32,
    pub b: i32,
}

fn func_1(obj: &mut MyObject) {
    let a = &mut obj.a;
    let b = &mut obj.b;

    *a += 1;
    *b *= 2;
}
</code>```

This doesn't work if obj is a MutexGuard or RefMut:

```<code>fn func_3(mtx: &Mutex<MyObject>) {
    let mut obj = mtx.lock().unwrap();
    let a = &mut obj.a;
    let b = &mut obj.b; // fails
    ...
}

fn func_4(rfc: &mut RefCell<MyObject>) {
    let mut obj = rfc.borrow_mut();
    let a = &mut obj.a;
    let b = &mut obj.b; // fails
    ...
}
</code>```

Both fail with:

```<code>error[E0499]: cannot borrow `obj` as mutable more than once at a time
  --> src/main.rs:28:18
   |
27 |     let a = &mut obj.a;
   |                  --- first mutable borrow occurs here
28 |     let b = &mut obj.b; // fails
   |                  ^^^ second mutable borrow occurs here
</code>```

However, this does work if obj is a Box:

```<code>fn func_2(obj: &mut Box<MyObject>) {
    let a = &mut obj.a;
    let b = &mut obj.b;

    *a += 1;
    *b *= 2;
}
</code>```

See it on the Rust Playground.

My main question is why. Why does the compiler know that this is okay for Box, but not for the others? Is Box special?

答:
Yes, this is one of the ways in which Box is still special, despite rather a lot of effort having been put into making it appear like any other type.

RefMut is implemented in plain Rust. The reason you can take references to a member like &mut obj.a is because RefMut implements Deref and DerefMut, and the compiler uses DerefMut to take an &mut RefMut<'_, MyObject> and turn it into a &mut MyObject to access the field. But the compiler doesn't know that the borrow of obj.a is disjoint from the borrow of obj.b because the implementation of DerefMut is opaque.

Box is different because dereferencing a Box<T> does not go through Deref or DerefMut (except in a generic context). Instead, the compiler has special code that knows what a Box<T> is and how to dereference it to get a T. This is how the compiler knows that obj.a and obj.b are disjoint for Box<T>, but not for RefMut<'_, T> or MutexGuard<'_, T>.

Box is the only type that is special in this particular way.

Other links about the specialness of Box

Rust Tidbits: Box Is Special

Why do I get "cannot move out of `item` because it is borrowed" for a custom type but not a Box?

Why can I not borrow a variable as mutable more than once at a time with a &mut Box<T> while &mut T works?

Body:
With a simple object, I can get mutable references to individual fields:

struct MyObject {
    pub a: i32,
    pub b: i32,
}

fn func_1(obj: &mut MyObject) {
    let a = &mut obj.a;
    let b = &mut obj.b;

    *a += 1;
    *b *= 2;
}

This doesn't work if obj is a MutexGuard or RefMut:

fn func_3(mtx: &Mutex<MyObject>) {
    let mut obj = mtx.lock().unwrap();
    let a = &mut obj.a;
    let b = &mut obj.b; // fails
    ...
}

fn func_4(rfc: &mut RefCell<MyObject>) {
    let mut obj = rfc.borrow_mut();
    let a = &mut obj.a;
    let b = &mut obj.b; // fails
    ...
}

Both fail with:

error[E0499]: cannot borrow `obj` as mutable more than once at a time
  --> src/main.rs:28:18
   |
27 |     let a = &mut obj.a;
   |                  --- first mutable borrow occurs here
28 |     let b = &mut obj.b; // fails
   |                  ^^^ second mutable borrow occurs here

However, this does work if obj is a Box:

fn func_2(obj: &mut Box<MyObject>) {
    let a = &mut obj.a;
    let b = &mut obj.b;

    *a += 1;
    *b *= 2;
}

See it on the Rust Playground.

My main question is why. Why does the compiler know that this is okay for Box, but not for the others? Is Box special?


问:
Access rightmost two columns of pandas dataframe, by number
I'm hoping to overwrite some values in a df when a condition is met. Using the df below, when col B is equal to values in lst, I want to replace values in col C,D with X.

This can achieved using the method below but I'm hoping to use indexing to select the last two columns, rather than using hard coded labels.

```<code>df = pd.DataFrame({
    'A' : [1,1,1,1,1,1,1,1],
    'B' : ['X','Foo','X','Cat','A','A','X','D'],
    'C' : [1,1,1,1,1,1,1,1],
    'D' : [1,1,1,1,1,1,1,1],
    })

lst = ['Foo','Cat']
df.loc[df.B.isin(lst), ['C','D']] = 'X'
</code>```

Attempt:

```<code>df.loc[df.B.isin(lst), df.loc[:-2]] = 'X'
</code>```

Intended:

```<code>   A    B  C  D
0  1    X  1  1
1  1  Foo  X  X
2  1    X  1  1
3  1  Cat  X  X
4  1    A  1  1
5  1    A  1  1
6  1    X  1  1
7  1    D  1  1
</code>```

答:
Here's my best attempt at a pandas-y approach, with the super-ugly and less performant apply method further down below:

```<code>import pandas as pd

df = pd.DataFrame({
    'A' : [1,1,1,1,1,1,1,1],
    'B' : ['X','Foo','X','Cat','A','A','X','D'],
    'C' : [1,1,1,1,1,1,1,1],
    'D' : [1,1,1,1,1,1,1,1],
})

# get working index once,
# where column "B" in lst,
# store
ind = df["B"].isin(lst)

# get working slice with index
df_slice = df[ind]

# set the "C" and "D" columns
df_slice["C"], df_slice["D"] = "X", "X"

# set the original df slice
# to our working slice
df[df.B.isin(lst)] = df_slice

print(df)

### PRINTS:

   A    B  C  D
0  1    X  1  1
1  1  Foo  X  X
2  1    X  1  1
3  1  Cat  X  X
4  1    A  1  1
5  1    A  1  1
6  1    X  1  1
7  1    D  1  1
</code>```

And here's the row-by-row apply approach. It's not the prettiest solution, but it gets the job done. Note that this just replaces the rest of the row with Xs.

```<code>import pandas as pd

df = pd.DataFrame({
    'A' : [1,1,1,1,1,1,1,1],
    'B' : ['X','Foo','X','Cat','A','A','X','D'],
    'C' : [1,1,1,1,1,1,1,1],
    'D' : [1,1,1,1,1,1,1,1],
})

def apply_function(row):
    lst = ['Foo','Cat']
    return row if row["B"] not in lst else [
        # first two rows
        *row[:2],
        # Xs for the rest of the row
        *["X" for r in range(len(row) - 2)]
    ]

df = df.apply(apply_function, axis=1)
print(df)

### PRINTS:

   A    B  C  D
0  1    X  1  1
1  1  Foo  X  X
2  1    X  1  1
3  1  Cat  X  X
4  1    A  1  1
5  1    A  1  1
6  1    X  1  1
7  1    D  1  1
</code>```

Body:
I'm hoping to overwrite some values in a df when a condition is met. Using the df below, when col B is equal to values in lst, I want to replace values in col C,D with X.

This can achieved using the method below but I'm hoping to use indexing to select the last two columns, rather than using hard coded labels.

df = pd.DataFrame({
    'A' : [1,1,1,1,1,1,1,1],
    'B' : ['X','Foo','X','Cat','A','A','X','D'],
    'C' : [1,1,1,1,1,1,1,1],
    'D' : [1,1,1,1,1,1,1,1],
    })

lst = ['Foo','Cat']
df.loc[df.B.isin(lst), ['C','D']] = 'X'

Attempt:

df.loc[df.B.isin(lst), df.loc[:-2]] = 'X'

Intended:

   A    B  C  D
0  1    X  1  1
1  1  Foo  X  X
2  1    X  1  1
3  1  Cat  X  X
4  1    A  1  1
5  1    A  1  1
6  1    X  1  1
7  1    D  1  1

Body:
Here's my best attempt at a pandas-y approach, with the super-ugly and less performant apply method further down below:

import pandas as pd

df = pd.DataFrame({
    'A' : [1,1,1,1,1,1,1,1],
    'B' : ['X','Foo','X','Cat','A','A','X','D'],
    'C' : [1,1,1,1,1,1,1,1],
    'D' : [1,1,1,1,1,1,1,1],
})

# get working index once,
# where column "B" in lst,
# store
ind = df["B"].isin(lst)

# get working slice with index
df_slice = df[ind]

# set the "C" and "D" columns
df_slice["C"], df_slice["D"] = "X", "X"

# set the original df slice
# to our working slice
df[df.B.isin(lst)] = df_slice

print(df)

### PRINTS:

   A    B  C  D
0  1    X  1  1
1  1  Foo  X  X
2  1    X  1  1
3  1  Cat  X  X
4  1    A  1  1
5  1    A  1  1
6  1    X  1  1
7  1    D  1  1

And here's the row-by-row apply approach. It's not the prettiest solution, but it gets the job done. Note that this just replaces the rest of the row with Xs.

import pandas as pd

df = pd.DataFrame({
    'A' : [1,1,1,1,1,1,1,1],
    'B' : ['X','Foo','X','Cat','A','A','X','D'],
    'C' : [1,1,1,1,1,1,1,1],
    'D' : [1,1,1,1,1,1,1,1],
})

def apply_function(row):
    lst = ['Foo','Cat']
    return row if row["B"] not in lst else [
        # first two rows
        *row[:2],
        # Xs for the rest of the row
        *["X" for r in range(len(row) - 2)]
    ]

df = df.apply(apply_function, axis=1)
print(df)

### PRINTS:

   A    B  C  D
0  1    X  1  1
1  1  Foo  X  X
2  1    X  1  1
3  1  Cat  X  X
4  1    A  1  1
5  1    A  1  1
6  1    X  1  1
7  1    D  1  1


问:
How can USB vendor ID and product ID values be spoofed on OSX?
We are considering using the vendor and product ID of a USB device (obtained via IOKit) to unlock certain features of an application. I'm aware that these values can be spoofed, but I'm not sure how easily it can be done on OSX. What is involved in spoofing the vendor and product ID? Is it something that a non-programmer can do fairly easily?</p>

答:
I can create A OCX in Delphi code to do that
And supply infomation to send/receive data to the USB with other states like:- is it still connected.

Natraly I need a little more detail than that in case that same chip is connected from another product.
example frendly name, device desription.............


问:
iOS - How to upload a video with uploadTask?
I need to upload an mp4 video file from iPhone/iPad to a server, also in the background, so I read that is possible with URLSession.uploadTask(with: URLRequest, fromFile: URL) method, but I don't understand how do I prepare the request before.I need to create a multipart/form-data request because I want to append other string parameters.

```<code>func requestBodyFor(video: URL) -> Data? {
    let url = URL(string: "url_of_upload_handler.php")!

    let parameters = ["type":"video", "user":"112"]

    do {

        let kBoundary = "Boundary-\(UUID().uuidString)"
        let kStartTag = "--%@\r\n"
        let kEndTag = "\r\n"
        let kContent = "Content-Disposition: form-data; name=\"%@\"\r\n\r\n"

        var body = Data()

        let videoData = try Data(contentsOf: video)

        // parameters
        for (key,value) in parameters {
            body.append(String(format: kStartTag, kBoundary).data(using: String.Encoding.utf8)!)
            body.append(String(format: kContent, key).data(using: String.Encoding.utf8)!)
            body.append(value.data(using: String.Encoding.utf8)!)
            body.append(String(format: kEndTag).data(using: String.Encoding.utf8)!)
        }

        //Video data
        body.append(String(format: kStartTag, boundary).data(using: String.Encoding.utf8)!)
        body.append(String(format: "Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", "file", video.lastPathComponent).data(using: String.Encoding.utf8)!)
        body.append("Content-Type: video/mp4\r\n\r\n".data(using: String.Encoding.utf8)!)
        body.append(videoData)
        body.append(String(format: kEndTag).data(using: String.Encoding.utf8)!)

        // close form
        body.append("--\(boundary)--\r\n".data(using: String.Encoding.utf8)!)

       return body
    } catch let error {
        print(error)
        return nil
    }
}

if let body = requestBodyFor(video: fileUrl) {
        let contentType = "multipart/form-data; boundary=\(kBoundary)"

        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        request.setValue(contentType, forHTTPHeaderField: "Content-Type")

        let task = URLSession.shared.uploadTask(with: request, from: body) { data, response, error in

        guard error == nil && data != nil else {
          return
        }

        if let data = String(data: data!, encoding: String.Encoding.utf8) {
            print(data)
        }

        }
        task.resume()
}
</code>```

How does the uploadTask work? maybe it appends the data of the file to the request body and then adds the boundary automatically? if I use this code, the upload doesn't work, what I have to change?

UPDATE: I've updated the code, now the upload works in foreground using the completionHandler of the uploadTask, but if I create a background session and using URLSessionDataDelegate instead of the completionHandler (because it doesn't work in the background), the transfer rate is very slow also with a 2 MB file, how can I solve this?

UPDATE 2: with the background session, the uploadTask restarts many times and it doesn't complete, never.

答:
Sometimes it is easier for the server to read file from form-data. For instance, the flask framework can read file uploaded in the format of form-data easily by request.files.
Alamofire provides an easy way to do this

```<code>AF.upload(multipartFormData: { multipartFormData in
                multipartFormData.append(FilePath, withName: FilePath.lastPathComponent)
            }, to: url).responseJSON { response in
                    debugPrint(response)
            }
</code>```

In this way, the file will be uploaded in the format of form-data.

Body:
I need to upload an mp4 video file from iPhone/iPad to a server, also in the background, so I read that is possible with URLSession.uploadTask(with: URLRequest, fromFile: URL) method, but I don't understand how do I prepare the request before.I need to create a multipart/form-data request because I want to append other string parameters.

func requestBodyFor(video: URL) -> Data? {
    let url = URL(string: "url_of_upload_handler.php")!

    let parameters = ["type":"video", "user":"112"]

    do {

        let kBoundary = "Boundary-\(UUID().uuidString)"
        let kStartTag = "--%@\r\n"
        let kEndTag = "\r\n"
        let kContent = "Content-Disposition: form-data; name=\"%@\"\r\n\r\n"

        var body = Data()

        let videoData = try Data(contentsOf: video)

        // parameters
        for (key,value) in parameters {
            body.append(String(format: kStartTag, kBoundary).data(using: String.Encoding.utf8)!)
            body.append(String(format: kContent, key).data(using: String.Encoding.utf8)!)
            body.append(value.data(using: String.Encoding.utf8)!)
            body.append(String(format: kEndTag).data(using: String.Encoding.utf8)!)
        }

        //Video data
        body.append(String(format: kStartTag, boundary).data(using: String.Encoding.utf8)!)
        body.append(String(format: "Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", "file", video.lastPathComponent).data(using: String.Encoding.utf8)!)
        body.append("Content-Type: video/mp4\r\n\r\n".data(using: String.Encoding.utf8)!)
        body.append(videoData)
        body.append(String(format: kEndTag).data(using: String.Encoding.utf8)!)

        // close form
        body.append("--\(boundary)--\r\n".data(using: String.Encoding.utf8)!)

       return body
    } catch let error {
        print(error)
        return nil
    }
}

if let body = requestBodyFor(video: fileUrl) {
        let contentType = "multipart/form-data; boundary=\(kBoundary)"

        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        request.setValue(contentType, forHTTPHeaderField: "Content-Type")

        let task = URLSession.shared.uploadTask(with: request, from: body) { data, response, error in

        guard error == nil && data != nil else {
          return
        }

        if let data = String(data: data!, encoding: String.Encoding.utf8) {
            print(data)
        }

        }
        task.resume()
}

How does the uploadTask work? maybe it appends the data of the file to the request body and then adds the boundary automatically? if I use this code, the upload doesn't work, what I have to change?

UPDATE: I've updated the code, now the upload works in foreground using the completionHandler of the uploadTask, but if I create a background session and using URLSessionDataDelegate instead of the completionHandler (because it doesn't work in the background), the transfer rate is very slow also with a 2 MB file, how can I solve this?

UPDATE 2: with the background session, the uploadTask restarts many times and it doesn't complete, never.

Body:
Sometimes it is easier for the server to read file from form-data. For instance, the flask framework can read file uploaded in the format of form-data easily by request.files.
Alamofire provides an easy way to do this

AF.upload(multipartFormData: { multipartFormData in
                multipartFormData.append(FilePath, withName: FilePath.lastPathComponent)
            }, to: url).responseJSON { response in
                    debugPrint(response)
            }

In this way, the file will be uploaded in the format of form-data.


问:
Run function with positional and optional arguments in parallel in python
I'm trying to compute various metrics on a Pandas DataFrame using the apply method. Since the DataFrame I'm working with is quiet big (1 million rows x 20 columns), I decided to parallelize the computation process.

In order to reproduce the issue I'm having, I'm going to use the iris dataset. Here are the steps:

```<code># Step 1: Import all required modules + load iris dataset to Pandas DataFrame

import pandas as pd
import numpy as np
import seaborn as sns
from multiprocessing import Pool

iris = pd.DataFrame(sns.load_dataset('iris'))

# Step 2: Define function that adds some metric to initial iris DataFrame

def add_metrics(data):
    data['x_1'] = data['species'].apply(lambda x: len(x))
    return data

# Step 3: Define parallelization function

num_partitions = 10 # number of partitions to split dataframe
num_cores = 4

def parallelize_dataframe(df, func):
    df_split = np.array_split(df, num_partitions)
    pool = Pool(num_cores)
    df = pd.concat(pool.map(func, df_split))
    pool.close()
    pool.join()
    return df

# Step 4: Add metrics to initial iris DataFrame using parallelization function

iris = parallelize_dataframe(iris, add_metrics)
</code>```

The above process works perfectly well as it is BUT I want to be able to have additional positional and/or optional arguments in my add_metrics function. For example, my add_metrics function might look like the following:

```<code>def add_metrics(data, num, keep = False):
    data['x_1'] = data['species'].apply(lambda x: len(x))
    data['x_2'] = data['sepal_length'].apply(lambda x: x * num)
    if keep == True:
        data['x_3'] = data['sepal_width'].apply(lambda x: x * num)
    return data
</code>```

Now, no matter how I try to call the parallelize_dataframe function I'm getting an error. For example:

iris = parallelize_dataframe(iris, add_metrics(iris, 2, keep = True)) throws a TypeError: 'DataFrame' object is not callable.

I'm fairly new to Python so I don't know what is going wrong here and how to fix my problem. I know the example I chose does not require parallel processing as the iris dataset only contains 150 observation. I used it to easily reproduce my problem.

Any help would be appreciated.

答:
You can use the functools.partial to set variables in your function before passing to map.

```<code>def add(x,y):
    return(x+y)

a = [1, 2, 3]
import functools
map(functools.partial(add, y=2), a) # map object [3, 4, 5]
</code>```

Body:
I'm trying to compute various metrics on a Pandas DataFrame using the apply method. Since the DataFrame I'm working with is quiet big (1 million rows x 20 columns), I decided to parallelize the computation process.

In order to reproduce the issue I'm having, I'm going to use the iris dataset. Here are the steps:

# Step 1: Import all required modules + load iris dataset to Pandas DataFrame

import pandas as pd
import numpy as np
import seaborn as sns
from multiprocessing import Pool

iris = pd.DataFrame(sns.load_dataset('iris'))

# Step 2: Define function that adds some metric to initial iris DataFrame

def add_metrics(data):
    data['x_1'] = data['species'].apply(lambda x: len(x))
    return data

# Step 3: Define parallelization function

num_partitions = 10 # number of partitions to split dataframe
num_cores = 4

def parallelize_dataframe(df, func):
    df_split = np.array_split(df, num_partitions)
    pool = Pool(num_cores)
    df = pd.concat(pool.map(func, df_split))
    pool.close()
    pool.join()
    return df

# Step 4: Add metrics to initial iris DataFrame using parallelization function

iris = parallelize_dataframe(iris, add_metrics)

The above process works perfectly well as it is BUT I want to be able to have additional positional and/or optional arguments in my add_metrics function. For example, my add_metrics function might look like the following:

def add_metrics(data, num, keep = False):
    data['x_1'] = data['species'].apply(lambda x: len(x))
    data['x_2'] = data['sepal_length'].apply(lambda x: x * num)
    if keep == True:
        data['x_3'] = data['sepal_width'].apply(lambda x: x * num)
    return data

Now, no matter how I try to call the parallelize_dataframe function I'm getting an error. For example:

iris = parallelize_dataframe(iris, add_metrics(iris, 2, keep = True)) throws a TypeError: 'DataFrame' object is not callable.

I'm fairly new to Python so I don't know what is going wrong here and how to fix my problem. I know the example I chose does not require parallel processing as the iris dataset only contains 150 observation. I used it to easily reproduce my problem.

Any help would be appreciated.

Body:
You can use the functools.partial to set variables in your function before passing to map.

def add(x,y):
    return(x+y)

a = [1, 2, 3]
import functools
map(functools.partial(add, y=2), a) # map object [3, 4, 5]


问:
Docker container date/time totally different to host PC
When I run a docker container on my PC it has a totally different date/time to the host PC. See commands below. The time on the container recognizerDev is for the previous day, different hour, different minutes to the host. Any idea what is going on?

```<code>PS C:\Users\Bobby> date
11 October 2016 19:51:38

PS C:\Users\Bobby> docker exec recognizerDev date
Mon Oct 10 21:43:54 UTC 2016
</code>```

When I try the same thing running on an AWS linux host the date/time is correct except for a 1 hour difference due to timezones.

Note that the first command returns the correct time/date in UTC+1 (London) as per my PC. The second command says it is in UTC but this cannot be right as if so it would return the same result less 1 hour.

答:
To some people looking on still how to correct the date and time, not sure if this will work to you but it happens on me using windows.

Just have to restart the locker, and once the docker booted up, it will sync time and time to the localhost machine :)

Body:
When I run a docker container on my PC it has a totally different date/time to the host PC. See commands below. The time on the container recognizerDev is for the previous day, different hour, different minutes to the host. Any idea what is going on?

PS C:\Users\Bobby> date
11 October 2016 19:51:38

PS C:\Users\Bobby> docker exec recognizerDev date
Mon Oct 10 21:43:54 UTC 2016

When I try the same thing running on an AWS linux host the date/time is correct except for a 1 hour difference due to timezones.

Note that the first command returns the correct time/date in UTC+1 (London) as per my PC. The second command says it is in UTC but this cannot be right as if so it would return the same result less 1 hour.


问:
Python delete file if it has a specific size in bytes
Basically trying to go over some 1000+ files in a folder, and delete any of duplicate sizes. My method was to loop through the whole folder in one go, deleting and files that have an exact byte size that has been seen in a previous file.

I've tried writing it in various ways to get it to work, but I keep ending up at the same error that it cannot find the file specified, and lists the name of the file it is on.

```<code>import os

def cleanup():
    sizes = [1,2,3] #in bytes

    for fileName in os.listdir(r"C:\Users\Jake\Desktop\testing"): #iterate through all items in directory
        fileDIR = (r"C:\Users\Jake\Desktop\testing" + "\\" + fileName)
        fileSize = os.path.getsize(fileDIR) # get integer value size in bytes of the file

        for i in sizes: #compare the size of the current focused file to the items in the list
            if fileSize == i:
                os.remove(fileName) #If the filesize has been seen before, delete the given file. If not, add the size and go on to next file.
            else:
                sizes.append(fileSize)

cleanup()
</code>```

答:
Seems like this is to do with how you're iterating over your sizes list.

For every file you find, you're iterating over the entire list, and appending the size multiple times.

for i in sizes: will cause you to iterate even after the file has matched and been deleted, if it encounters the size before the end of the list.

You can change this, by just checking if the size is found in sizes.

```<code>sizes = []

for filename in os.listdir(r"c:\users\admin\storage\\"):
    file = r"c:\users\admin\storage\\" + filename

    size = os.path.getsize(file)

    if size in sizes:
        print('same size', filename)
    else:
        sizes.append(size)
</code>```

The first time i run this on my own folder, it returns;

```<code>same size static
same size storage

sizes

#[0, 4096, 135168, 554, 102]
</code>```

This matches correctly, as static and storage are 0 bytes. When i run it again, without modifying the result in sizes every file matches.

```<code>same size .ebextensions
same size customer
same size db.sqlite3
same size manage.py
same size requirements.txt
same size static
same size storage
</code>```

Body:
Basically trying to go over some 1000+ files in a folder, and delete any of duplicate sizes. My method was to loop through the whole folder in one go, deleting and files that have an exact byte size that has been seen in a previous file.

I've tried writing it in various ways to get it to work, but I keep ending up at the same error that it cannot find the file specified, and lists the name of the file it is on.

import os

def cleanup():
    sizes = [1,2,3] #in bytes

    for fileName in os.listdir(r"C:\Users\Jake\Desktop\testing"): #iterate through all items in directory
        fileDIR = (r"C:\Users\Jake\Desktop\testing" + "\\" + fileName)
        fileSize = os.path.getsize(fileDIR) # get integer value size in bytes of the file

        for i in sizes: #compare the size of the current focused file to the items in the list
            if fileSize == i:
                os.remove(fileName) #If the filesize has been seen before, delete the given file. If not, add the size and go on to next file.
            else:
                sizes.append(fileSize)

cleanup()

Body:
Seems like this is to do with how you're iterating over your sizes list.

For every file you find, you're iterating over the entire list, and appending the size multiple times.

for i in sizes: will cause you to iterate even after the file has matched and been deleted, if it encounters the size before the end of the list.

You can change this, by just checking if the size is found in sizes.

sizes = []

for filename in os.listdir(r"c:\users\admin\storage\\"):
    file = r"c:\users\admin\storage\\" + filename

    size = os.path.getsize(file)

    if size in sizes:
        print('same size', filename)
    else:
        sizes.append(size)

The first time i run this on my own folder, it returns;

same size static
same size storage

sizes

#[0, 4096, 135168, 554, 102]

This matches correctly, as static and storage are 0 bytes. When i run it again, without modifying the result in sizes every file matches.

same size .ebextensions
same size customer
same size db.sqlite3
same size manage.py
same size requirements.txt
same size static
same size storage


问:
print modified web page without displaying it
I'm trying to implement "print this page" on an e-commerce product page. It will be without needless sections like top navigation, footer, add to cart button etc. I want it to work like target.com
http://www.target.com/Avalon-Vitamin-Moisture-Plus-Lotion/dp/B00120ZAYI/ref=sr_1_13?ie=UTF8&searchView=grid5&keywords=vitamins&fromGsearch=true&sr=1-13&qid=1305580954&rh=&searchRank=target104545&id=Avalon%20Vitamin%20Moisture%20Plus%20Lotion&node=1038576|1287991011&searchSize=30&searchPage=1&searchNodeID=1038576|1287991011&searchBinNameList=subjectbin%2Cprice%2Ctarget_com_primary_color-bin%2Ctarget_com_size-bin%2Ctarget_com_brand-bin&frombrowse=0

If you try to print it, only essential content gets printed. I'm confused how to implement it. Should I create a modified version of product page and print it without displaying it to a user? Please advise.

答:
You're able to set a custom CSS page that has the header, navigation, footer or whatever you like hidden so when the user clicks print, the web browser looks at the print css and acts how you want it to.

Example of this is:

In the HTML

```<code><link href="/css/main.css" rel="stylesheet" type="text/css">
<link href="/css/print.css" rel="stylesheet" type="text/css" media="print">
</code>```

Under the attribute "media", print will only be used when printing it out.

In the CSS for main.css, could look like..

```<code>header { font-size: 32px; }
nav { font-size: 14px; }
footer { font-size: 10px; text-align: center; }
</code>```

In the CSS for print.css, it can look like...

```<code>header { display: none; }
nav { display: none; }
footer { display: none; }
</code>```

Basically, make a duplicate of the CSS you're using and just set display: none; to what you don't want to have printed.

Body:
You're able to set a custom CSS page that has the header, navigation, footer or whatever you like hidden so when the user clicks print, the web browser looks at the print css and acts how you want it to.

Example of this is:

In the HTML

<link href="/css/main.css" rel="stylesheet" type="text/css">
<link href="/css/print.css" rel="stylesheet" type="text/css" media="print">

Under the attribute "media", print will only be used when printing it out.

In the CSS for main.css, could look like..

header { font-size: 32px; }
nav { font-size: 14px; }
footer { font-size: 10px; text-align: center; }

In the CSS for print.css, it can look like...

header { display: none; }
nav { display: none; }
footer { display: none; }

Basically, make a duplicate of the CSS you're using and just set display: none; to what you don't want to have printed.


问:
Functions on header not working - AngularJs & Javascript
I need to put some functions on the header of my site, but the functions just not working...

I'm building my site with AngularJs and Javascript. Next my index.html:

```<code><!DOCTYPE html>
<html ng-app="adminApp">

    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Admin</title>
    <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" />
    <link type="text/css" href="app/css/admin.css" rel="stylesheet" />
    <link type="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.7.1/css/bootstrap-datepicker3.standalone.min.css" rel="stylesheet"/>
    <link href="https://fonts.googleapis.com/css?family=Quicksand&display=swap" rel="stylesheet">

<head>

    <base href="/">

</head>
<body>

    <div ng-include="'app/components/include/header.html'"></div>

    <ng-view></ng-view>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.7.1/js/bootstrap-datepicker.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.7.1/js/bootstrap-datepicker.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular-resource.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular-route.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular-cookies.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular-sanitize.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.22/pdfmake.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.62/vfs_fonts.js"></script>
<script src="app/app.js"></script>
<script src="app/components/home/admin-home.js"></script>
<script src="app/components/test/test.js"></script>
<script src="app/components/include/header.js"></script>
<script src="app/factories/admin-factory.js"></script>
<script src="app/routes/admin-route.js"></script>

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

As you can see, i'm calling the header file on the ng-include div.

And the my header.html file:

```<code><section class="admin-header">
    <div class="row">
        <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">
            <a id="admin-logo"><img src="/logo.png" /></a>
        </div>
        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 hide" id="login" align="center">
            <button type="button" class="btn btn-user" id="jump-btn">
                <span class="glyphicon glyphicon-menu-hamburger" aria-hidden="true"></span>
            </button>
        </div>
    </div>
</section>
<div class="menu-out">
    <button type="button" class="btn btn-logout" id="logout">Log out</button>
</div>
</code>```

So, at this point, everything works fine: the header appears on every page. But if i need a function on the logout button, how can i do it?

I've tried creating a home.js file like next one:

```<code>(function(){

    var head = {
        templateUrl: '/app/components/include/header.html',
        controller: headCtrl
    };

    angular
    .module('adminApp')
    .component('adminHead', head);

    headCtrl.$inject = ["$http", "$scope"];
    function headCtrl($http, $scope){
        $scope.getOut = function(){
            alert('Log out');
        }
     }

})();
</code>```

And then, added the function on the button with ng-click:

```<code><div class="menu-out">
    <button type="button" class="btn btn-logout" id="logout" ng-click="getOut()">Log out</button>
</div>
</code>```

But the function is not working.

Someone knows why is not working and help me with and example if it's possible, please? If i add the function on the html between script tags works, but i need to find if it's possible with the $scope.

Hope you can help me. Thanx in advance

答:
Declare a controller:

```<code>angular
.module('adminApp')
.controller('adminHeadCtrl', headCtrl);

headCtrl.$inject = ["$http", "$scope"];
function headCtrl($http, $scope){
    $scope.getOut = function(){
        alert('Log out');
    }
}
</code>```

Instantiate the controller with the ng-controller directive:

    <button type="button" ng-click="getOut()" id="logout">Log out</button>

```

For more information, see

AngularJS Developer Guide - Controllers

Body:
I need to put some functions on the header of my site, but the functions just not working...

I'm building my site with AngularJs and Javascript. Next my index.html:

<!DOCTYPE html>

    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Admin</title>
    <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" />
    <link type="text/css" href="app/css/admin.css" rel="stylesheet" />
    <link type="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.7.1/css/bootstrap-datepicker3.standalone.min.css" rel="stylesheet"/>
    <link href="https://fonts.googleapis.com/css?family=Quicksand&display=swap" rel="stylesheet">

    <base href="/">

    <ng-view></ng-view>

As you can see, i'm calling the header file on the ng-include div.

And the my header.html file:

            <button type="button" class="btn btn-user" id="jump-btn">

            </button>

    <button type="button" class="btn btn-logout" id="logout">Log out</button>

So, at this point, everything works fine: the header appears on every page. But if i need a function on the logout button, how can i do it?

I've tried creating a home.js file like next one:

(function(){

    var head = {
        templateUrl: '/app/components/include/header.html',
        controller: headCtrl
    };

    angular
    .module('adminApp')
    .component('adminHead', head);

    headCtrl.$inject = ["$http", "$scope"];
    function headCtrl($http, $scope){
        $scope.getOut = function(){
            alert('Log out');
        }
     }

})();

And then, added the function on the button with ng-click:

    <button type="button" class="btn btn-logout" id="logout" ng-click="getOut()">Log out</button>

But the function is not working.

Someone knows why is not working and help me with and example if it's possible, please? If i add the function on the html between script tags works, but i need to find if it's possible with the $scope.

Hope you can help me. Thanx in advance

Body:
Declare a controller:

angular
.module('adminApp')
.controller('adminHeadCtrl', headCtrl);

headCtrl.$inject = ["$http", "$scope"];
function headCtrl($http, $scope){
    $scope.getOut = function(){
        alert('Log out');
    }
}

Instantiate the controller with the ng-controller directive:

<div class="menu-out" ng-controller="adminHeadCtrl">
    <button type="button" ng-click="getOut()" id="logout">Log out</button>
</div>

For more information, see

AngularJS Developer Guide - Controllers


问:
Gap Filling OHLCV (Open High Low Close Volume) in TimescaleDB
I have some OHLCV data stored in TimescaleDB which contains missing data during certain time ranges. This data needs to be resampled to a different time period (i.e. 1 day) and contain contiguous, ordered time buckets.

TimescaleDB provides the function time_bucket_gapfill to do this. My current query is:

SELECT
    time_bucket_gapfill(
        '1 day',
        "timestamp",
        '2017-07-25 00:00',
        '2018-01-01 00:00'
    ) as date,
    FIRST(open, "timestamp") as open,
    MAX(high) as high,
    MIN(low) as low,
    LAST(close, "timestamp") as close,
    SUM(volume) as volume
FROM ohlcv
WHERE "timestamp" > '2017-07-25'
GROUP BY date ORDER BY date ASC LIMIT 10
```

<p>which gives as the results</p>

```date                    open        high        low         close       volume
2017-07-25 00:00:00+00
2017-07-26 00:00:00+00
2017-07-27 00:00:00+00  0.00992     0.010184    0.009679    0.010039    65553.5299999999
2017-07-28 00:00:00+00  0.00999     0.010059    0.009225    0.009248    43049.93
2017-07-29 00:00:00+00
2017-07-30 00:00:00+00  0.009518    0.0098      0.009286    0.009457    40510.0599999999

...
```

<p><strong>Question:</strong> It looks like only the <code>date</code> column has been gapfilled. By modifying the SQL statement, is it possible to also gap-fill the columns <code>open</code>, <code>high</code>, <code>low</code>, <code>close</code> and <code>volume</code> such that we obtain the results:</p>

```date                    open        high        low         close       volume
2017-07-25 00:00:00+00  0           0           0           0           0
2017-07-26 00:00:00+00  0           0           0           0           0
2017-07-27 00:00:00+00  0.00992     0.010184    0.009679    0.010039    65553.5299999999
2017-07-28 00:00:00+00  0.00999     0.010059    0.009225    0.009248    43049.93
2017-07-29 00:00:00+00  0.009248    0.009248    0.009248    0.009248    0
2017-07-30 00:00:00+00  0.009518    0.0098      0.009286    0.009457    40510.0599999999

...
```

<p>Or is it recommended to perform this data inputation after receiving the query result, such as in Python/Nodejs?</p>

<hr>

<h2>Example of how it can be done using Python/pandas</h2>

<p><em>Prefer to perform this gapfilling/inputation using TimescaleDB instead of using my Nodejs app because... using Nodejs to do this will be much slower and I do not want to introduce Python into the app just to do this processing</em></p>

<pre class="lang-py prettyprint-override"><code>import pandas as pd

# Building the test dataset simulating missing values after time_bucket
data = [
    (pd.Timestamp('2020-01-01'), None, None, None, None, None),
    (pd.Timestamp('2020-01-02'), 100, 110, 90, 95, 3),
    (pd.Timestamp('2020-01-03'), None, None, None, None, None),
    (pd.Timestamp('2020-01-04'), 98, 150, 100, 100, 4),
]
df = pd.DataFrame(data, columns=['date', 'open' , 'high', 'low', 'close', 'volume']).set_index('date')

#              open   high    low  close  volume
# date
# 2020-01-01    NaN    NaN    NaN    NaN     NaN
# 2020-01-02  100.0  110.0   90.0   95.0     3.0
# 2020-01-03    NaN    NaN    NaN    NaN     NaN
# 2020-01-04   98.0  150.0  100.0  100.0     4.0

# Perform gap filling
df.close = df.close.fillna(method='ffill')
df.volume = df.volume.fillna(0)                 # fill missing volume with 0
df['open'] = df['open'].fillna(df['close'])     # fill missing open by forward-filling close
df['high'] = df['high'].fillna(df['close'])     # fill missing high by forward-filling close
df['low'] = df['low'].fillna(df['close'])       # fill missing low by forward-filling close
df = df.fillna(0)                               # fill missing OHLC with 0 if no previous values available

#               open   high    low  close  volume
# date
# 2020-01-01    0.0    0.0    0.0    0.0     0.0
# 2020-01-02  100.0  110.0   90.0   95.0     3.0
# 2020-01-03   95.0   95.0   95.0   95.0     0.0
# 2020-01-04   98.0  150.0  100.0  100.0     4.0
</code>```

答:
You need to specify in each column how to perform the gapfilling.  My guess is that you probably want to use locf.  See:

https://docs.timescale.com/latest/api#time_bucket_gapfill
https://docs.timescale.com/latest/api#locf

Body:
I have some OHLCV data stored in TimescaleDB which contains missing data during certain time ranges. This data needs to be resampled to a different time period (i.e. 1 day) and contain contiguous, ordered time buckets.

TimescaleDB provides the function time_bucket_gapfill to do this. My current query is:

SELECT
    time_bucket_gapfill(
        '1 day',
        "timestamp",
        '2017-07-25 00:00',
        '2018-01-01 00:00'
    ) as date,
    FIRST(open, "timestamp") as open,
    MAX(high) as high,
    MIN(low) as low,
    LAST(close, "timestamp") as close,
    SUM(volume) as volume
FROM ohlcv
WHERE "timestamp" > '2017-07-25'
GROUP BY date ORDER BY date ASC LIMIT 10

which gives as the results

date                    open        high        low         close       volume
2017-07-25 00:00:00+00
2017-07-26 00:00:00+00
2017-07-27 00:00:00+00  0.00992     0.010184    0.009679    0.010039    65553.5299999999
2017-07-28 00:00:00+00  0.00999     0.010059    0.009225    0.009248    43049.93
2017-07-29 00:00:00+00
2017-07-30 00:00:00+00  0.009518    0.0098      0.009286    0.009457    40510.0599999999

...

Question: It looks like only the date column has been gapfilled. By modifying the SQL statement, is it possible to also gap-fill the columns open, high, low, close and volume such that we obtain the results:

date                    open        high        low         close       volume
2017-07-25 00:00:00+00  0           0           0           0           0
2017-07-26 00:00:00+00  0           0           0           0           0
2017-07-27 00:00:00+00  0.00992     0.010184    0.009679    0.010039    65553.5299999999
2017-07-28 00:00:00+00  0.00999     0.010059    0.009225    0.009248    43049.93
2017-07-29 00:00:00+00  0.009248    0.009248    0.009248    0.009248    0
2017-07-30 00:00:00+00  0.009518    0.0098      0.009286    0.009457    40510.0599999999

...

Or is it recommended to perform this data inputation after receiving the query result, such as in Python/Nodejs?

Example of how it can be done using Python/pandas

Prefer to perform this gapfilling/inputation using TimescaleDB instead of using my Nodejs app because... using Nodejs to do this will be much slower and I do not want to introduce Python into the app just to do this processing

import pandas as pd

# Building the test dataset simulating missing values after time_bucket
data = [
    (pd.Timestamp('2020-01-01'), None, None, None, None, None),
    (pd.Timestamp('2020-01-02'), 100, 110, 90, 95, 3),
    (pd.Timestamp('2020-01-03'), None, None, None, None, None),
    (pd.Timestamp('2020-01-04'), 98, 150, 100, 100, 4),
]
df = pd.DataFrame(data, columns=['date', 'open' , 'high', 'low', 'close', 'volume']).set_index('date')

#              open   high    low  close  volume
# date
# 2020-01-01    NaN    NaN    NaN    NaN     NaN
# 2020-01-02  100.0  110.0   90.0   95.0     3.0
# 2020-01-03    NaN    NaN    NaN    NaN     NaN
# 2020-01-04   98.0  150.0  100.0  100.0     4.0

# Perform gap filling
df.close = df.close.fillna(method='ffill')
df.volume = df.volume.fillna(0)                 # fill missing volume with 0
df['open'] = df['open'].fillna(df['close'])     # fill missing open by forward-filling close
df['high'] = df['high'].fillna(df['close'])     # fill missing high by forward-filling close
df['low'] = df['low'].fillna(df['close'])       # fill missing low by forward-filling close
df = df.fillna(0)                               # fill missing OHLC with 0 if no previous values available

#               open   high    low  close  volume
# date
# 2020-01-01    0.0    0.0    0.0    0.0     0.0
# 2020-01-02  100.0  110.0   90.0   95.0     3.0
# 2020-01-03   95.0   95.0   95.0   95.0     0.0
# 2020-01-04   98.0  150.0  100.0  100.0     4.0