问:
Get hardware information from /proc filesytem in Linux
I use execv to run lshw command to get the CPU, disk, and memory in C code. But I would like to search another solution to get these information from /proc or any other existed data. Have any suggestion? Here is my code:

```<code>char *params[9]  = {"/usr/bin/lshw", "-short", "-c", "disk",
                  "-c", "memory", "-c", "processor", 0}; //cmd params filled
execv(params[0], params);
</code>```

Linux command: $ sudo lshw -short -c disk -c processor -c memory

```<code>$ sudo lshw -short -c disk -c processor -c memory
H/W path         Device     Class          Description
======================================================
/0/0                        memory         64KiB BIOS
/0/22                       memory         16GiB System Memory
/0/22/0                     memory         DIMM Synchronous [empty]
/0/22/1                     memory         DIMM Synchronous [empty]
/0/22/2                     memory         8GiB DIMM Synchronous 2133 MHz (0.5 ns)
/0/22/3                     memory         8GiB DIMM Synchronous 2133 MHz (0.5 ns)
/0/2a                       memory         256KiB L1 cache
/0/2b                       memory         1MiB L2 cache
/0/2c                       memory         6MiB L3 cache
/0/2d                       processor      Intel(R) Xeon(R) CPU D-1521 @ 2.40GHz
/0/1/0.0.0       /dev/sda   disk           16GB SATADOM-SH 3IE3
/0/2/0.0.0       /dev/sdb   disk           120GB Patriot Blaze
</code>```

I have two questions:

Where to find a guide to parse the files in /proc to get
these hardware information?

Do I need to trace the source code of lshw to find what does lshw do?

Edit:

Chapter 7 of Advanced Linux Programming is a guide to parse the /proc filesystem.

答:
The best way to get hardware information is using sysconf() and sysctl*() functions (Mac OS X, freebsd, openbsd), and sysconf() and sysinfo() on Linux.

Parsing /proc/* is slower and more involved than calling sysinfo( ) or sysconf( )

Below is a small example giving you some information about processor and memory on Mac OS X:

```<code>#include <sys/types.h>
#include <sys/sysctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

int main()
{
 char *p = NULL;
 size_t len;
 sysctlbyname("hw.model", NULL, &len, NULL, 0);
 p = malloc(len);
 sysctlbyname("hw.model", p, &len, NULL, 0);
 printf("%s\n", p);
 /* CTL_MACHDEP variables are architecture dependent so doesn't work
 for every one */
 sysctlbyname("machdep.cpu.brand_string", NULL, &len, NULL, 0);
 p = malloc(len);
 sysctlbyname("machdep.cpu.brand_string", p, &len, NULL, 0);
 printf("%s\n", p);
 int64_t mem;
 len = sizeof(mem);
 sysctlbyname("hw.memsize", &mem, &len, NULL, 0);
 printf("System Memory : %lld\n", mem);
 return (0);
}
</code>```

You have to read man 3 sysctl, or on Linux man 2 sysconf and man 2 sysinfo.

An interesting link : http://nadeausoftware.com/articles/2012/09/c_c_tip_how_get_physical_memory_size_system#Other

You can calculate the CPU load and usage retrieving some sysctl variables, and doing the math by yourself (you can find the formulas to do it on google).

But where to find the physical DIMM information as the report from $ sudo lshw -short -c memory ?

You can execute your command inside your C program to save it as a string like :

```<code>#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/wait.h>
#include <string.h>
#include <stdlib.h>

char *strjoin(char *s1, char *s2, int n)
{
    int i = strlen(s2);
    int j = 0;
    if ((s2 = realloc(s2, (i + n + 1))) == NULL)
            perror(0);
    while (j < n && s1[j])
    {
            s2[i] = s1[j];
            i++;
            j++;
    }
    s2[i] = 0;
    return (s2);
}

int main()
{
    pid_t father;
    char buf[500] = {0};
    char *str;
    char *argv[5] = {"/usr/bin/lshw", "-short", "-c", "memory"};
    int fd[2];
    int ret;

    if (pipe(fd) == -1)
    {
            perror(NULL);
            return -1;
    }
    father = fork();
    if (father == 0)
    {
            close(fd[1]);
            while ((ret = read(fd[0], buf, 500)))
            {
                    str = strjoin(buf, str, ret);
            }
            close(fd[0]);
    }
    else
    {
            close(fd[0]);
            execv(argv[0], argv);
            close(fd[1]);
            wait(0);
    }
    wait(0);
    printf("%s", str);
    return 0;
}
</code>```

(I don't check all the function's return in this code, not to have a too long one, but you should do it in your program).

Here is an example of parsing the file /proc/meminfo to save in a double array 2 strings I want, and then printing them out :

```<code>#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    FILE *f;
    char *line = NULL;
    ssize_t read;
    size_t len = 0;
    char    **info;
    int i = 0;

    info = malloc(3 * sizeof(char*));
    f = fopen("/proc/meminfo", "r");
    while ((read = getline(&line, &len, f)) != -1)
    {
            if (strstr(line, "MemTotal") != NULL)
                    info[i] = strdup(line);
            else if (strstr(line, "MemFree") != NULL)
                    info[i] = strdup(line);
            i++;
    }
    info[i] = 0;
    fclose(f);
    i = 0;
    while (info[i])
    {
            printf("%s", info[i]);
            free (info[i]);
            i++;
    }
    free (info);
    return 0;
}
</code>```

If you want to save more strings, malloc more space in the double array info, and add them with else if inside the read loop. You can do that with any files from /proc/ to get the information you need.

ContentLicense: CC BY-SA 4.0

Body:
I use execv to run lshw command to get the CPU, disk, and memory in C code. But I would like to search another solution to get these information from /proc or any other existed data. Have any suggestion? Here is my code:

char *params[9]  = {"/usr/bin/lshw", "-short", "-c", "disk",
                  "-c", "memory", "-c", "processor", 0}; //cmd params filled
execv(params[0], params);

Linux command: $ sudo lshw -short -c disk -c processor -c memory

$ sudo lshw -short -c disk -c processor -c memory
H/W path         Device     Class          Description
======================================================
/0/0                        memory         64KiB BIOS
/0/22                       memory         16GiB System Memory
/0/22/0                     memory         DIMM Synchronous [empty]
/0/22/1                     memory         DIMM Synchronous [empty]
/0/22/2                     memory         8GiB DIMM Synchronous 2133 MHz (0.5 ns)
/0/22/3                     memory         8GiB DIMM Synchronous 2133 MHz (0.5 ns)
/0/2a                       memory         256KiB L1 cache
/0/2b                       memory         1MiB L2 cache
/0/2c                       memory         6MiB L3 cache
/0/2d                       processor      Intel(R) Xeon(R) CPU D-1521 @ 2.40GHz
/0/1/0.0.0       /dev/sda   disk           16GB SATADOM-SH 3IE3
/0/2/0.0.0       /dev/sdb   disk           120GB Patriot Blaze

I have two questions:

Where to find a guide to parse the files in /proc to get
these hardware information?

Do I need to trace the source code of lshw to find what does lshw do?

Edit:

Chapter 7 of Advanced Linux Programming is a guide to parse the /proc filesystem.

Body:
The best way to get hardware information is using sysconf() and sysctl*() functions (Mac OS X, freebsd, openbsd), and sysconf() and sysinfo() on Linux.

Parsing /proc/* is slower and more involved than calling sysinfo( ) or sysconf( )

Below is a small example giving you some information about processor and memory on Mac OS X:

#include <sys/types.h>
#include <sys/sysctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

int main()
{
 char *p = NULL;
 size_t len;
 sysctlbyname("hw.model", NULL, &len, NULL, 0);
 p = malloc(len);
 sysctlbyname("hw.model", p, &len, NULL, 0);
 printf("%s\n", p);
 /* CTL_MACHDEP variables are architecture dependent so doesn't work
 for every one */
 sysctlbyname("machdep.cpu.brand_string", NULL, &len, NULL, 0);
 p = malloc(len);
 sysctlbyname("machdep.cpu.brand_string", p, &len, NULL, 0);
 printf("%s\n", p);
 int64_t mem;
 len = sizeof(mem);
 sysctlbyname("hw.memsize", &mem, &len, NULL, 0);
 printf("System Memory : %lld\n", mem);
 return (0);
}

You have to read man 3 sysctl, or on Linux man 2 sysconf and man 2 sysinfo.

An interesting link : http://nadeausoftware.com/articles/2012/09/c_c_tip_how_get_physical_memory_size_system#Other

You can calculate the CPU load and usage retrieving some sysctl variables, and doing the math by yourself (you can find the formulas to do it on google).

But where to find the physical DIMM information as the report from $ sudo lshw -short -c memory ?

You can execute your command inside your C program to save it as a string like :

#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/wait.h>
#include <string.h>
#include <stdlib.h>

char *strjoin(char *s1, char *s2, int n)
{
    int i = strlen(s2);
    int j = 0;
    if ((s2 = realloc(s2, (i + n + 1))) == NULL)
            perror(0);
    while (j < n && s1[j])
    {
            s2[i] = s1[j];
            i++;
            j++;
    }
    s2[i] = 0;
    return (s2);
}

int main()
{
    pid_t father;
    char buf[500] = {0};
    char *str;
    char *argv[5] = {"/usr/bin/lshw", "-short", "-c", "memory"};
    int fd[2];
    int ret;

    if (pipe(fd) == -1)
    {
            perror(NULL);
            return -1;
    }
    father = fork();
    if (father == 0)
    {
            close(fd[1]);
            while ((ret = read(fd[0], buf, 500)))
            {
                    str = strjoin(buf, str, ret);
            }
            close(fd[0]);
    }
    else
    {
            close(fd[0]);
            execv(argv[0], argv);
            close(fd[1]);
            wait(0);
    }
    wait(0);
    printf("%s", str);
    return 0;
}

(I don't check all the function's return in this code, not to have a too long one, but you should do it in your program).

Here is an example of parsing the file /proc/meminfo to save in a double array 2 strings I want, and then printing them out :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    FILE *f;
    char *line = NULL;
    ssize_t read;
    size_t len = 0;
    char    **info;
    int i = 0;

    info = malloc(3 * sizeof(char*));
    f = fopen("/proc/meminfo", "r");
    while ((read = getline(&line, &len, f)) != -1)
    {
            if (strstr(line, "MemTotal") != NULL)
                    info[i] = strdup(line);
            else if (strstr(line, "MemFree") != NULL)
                    info[i] = strdup(line);
            i++;
    }
    info[i] = 0;
    fclose(f);
    i = 0;
    while (info[i])
    {
            printf("%s", info[i]);
            free (info[i]);
            i++;
    }
    free (info);
    return 0;
}

If you want to save more strings, malloc more space in the double array info, and add them with else if inside the read loop. You can do that with any files from /proc/ to get the information you need.


来源: stackexchange

问:
Google Analytics API V4: includeEmptyRows: 'true', Not Working
I'm migrating my app to V4, the thing is that you need setting includeEmptyRows to TRUE, and that's what I have just done. However the empty rows are not populating at all.

```<code>function queryReports() {
    gapi.client.request({
        path: '/v4/reports:batchGet',
        root: 'https://analyticsreporting.googleapis.com/',
        method: 'POST',
        body: {
            reportRequests: [
                {
                    **includeEmptyRows: 'true',**
                    //**this above for empty rows

                    viewId: VIEW_ID,
                    dateRanges: [
                        {
                            startDate: startDate0,
                            endDate: endDate0
                        }
                    ],
                    //**below samplimg levels
                    samplingLevel: 'LARGE',
                    //request metrics here
                    metrics: [
                        {
                            expression: 'ga:sessions',
                        }
                        ,
                        {
                            expression:'ga:goalCompletionsAll',
                        },
                        {
                            expression:'ga:transactionRevenue',
                        },
                        {
                            expression:'ga:transactions',
                        },
                    ],
                    //request dimensions here
                    dimensions: [
                        {
                            name:'ga:channelGrouping',
                        },

                        {
                            name:'ga:yearMonth',
                        },
                    ],
                }
            ]
        }
    }).then(displayResults, console.error.bind(console));
}
</code>```

I'm getting only not empty rows values, so for months where there is no data about any specific channel is simply skipped :/

Not sure what is wrong here, I followed the specification in the documentation but it's just not working at all.

Hope someone can give me a hand,

Many thanks
J.

答:
The documentation could be a lot better - unfortunately it leads people to believe that you will get rows returned for each combination of dimensions that have zero values.  Unfortunately this is not true.  If you were to exclude ChannelGrouping from your query, you will indeed get rows for all yearMonth values in your date range, even when there were 0 sessions.

In other words, this works as you'd expect when you ONLY include Date dimensions (and not time dimensions) in your query.

I believe this is because the cardinality of each dimension over each date period is unknown.  Would you expect to have a row with 0 sessions for each yearMonth and ChannelGrouping combination?  What if you added country and city as a dimension?

It is possible to get the results you're after, just not from a single query against the API.  Here is an outline of the steps using Analytics Canvas, but you can perform the same steps on your own using JavaScript (or any language to make the API calls) and SQL.

To get the results you're after make the following 3 queries:

1) yearMonth and sessions with empty rows:

2) channelGrouping and sessions over the time period

3) yearMonth, channelGrouping, and sessions (your query as shown above):

Now perform a cross join on queries 1 and 2, dropping sessions from your output.  You will have a table with all combinations of yearMonth and channelGrouping for your time period.  Let's call this Table 4.

Next perform a left outer join on Table 4 and Query 3, dropping yearMonth and channelGrouping from Query 3.

You now have Table 5, which has every combination of yearMonth and channelGrouping, values for sessions where there were sessions in that combination, and null values for the rest.

Finally replace the null values with zeros and you've got the dataset you're after.

```<code>isnull([Original.sessions], 0)
</code>```

ContentLicense: CC BY-SA 3.0

Body:
I'm migrating my app to V4, the thing is that you need setting includeEmptyRows to TRUE, and that's what I have just done. However the empty rows are not populating at all.

function queryReports() {
    gapi.client.request({
        path: '/v4/reports:batchGet',
        root: 'https://analyticsreporting.googleapis.com/',
        method: 'POST',
        body: {
            reportRequests: [
                {
                    **includeEmptyRows: 'true',**
                    //**this above for empty rows

                    viewId: VIEW_ID,
                    dateRanges: [
                        {
                            startDate: startDate0,
                            endDate: endDate0
                        }
                    ],
                    //**below samplimg levels
                    samplingLevel: 'LARGE',
                    //request metrics here
                    metrics: [
                        {
                            expression: 'ga:sessions',
                        }
                        ,
                        {
                            expression:'ga:goalCompletionsAll',
                        },
                        {
                            expression:'ga:transactionRevenue',
                        },
                        {
                            expression:'ga:transactions',
                        },
                    ],
                    //request dimensions here
                    dimensions: [
                        {
                            name:'ga:channelGrouping',
                        },

                        {
                            name:'ga:yearMonth',
                        },
                    ],
                }
            ]
        }
    }).then(displayResults, console.error.bind(console));
}

I'm getting only not empty rows values, so for months where there is no data about any specific channel is simply skipped :/

Not sure what is wrong here, I followed the specification in the documentation but it's just not working at all.

Hope someone can give me a hand,

Many thanks
J.

Body:
The documentation could be a lot better - unfortunately it leads people to believe that you will get rows returned for each combination of dimensions that have zero values.  Unfortunately this is not true.  If you were to exclude ChannelGrouping from your query, you will indeed get rows for all yearMonth values in your date range, even when there were 0 sessions.

In other words, this works as you'd expect when you ONLY include Date dimensions (and not time dimensions) in your query.

I believe this is because the cardinality of each dimension over each date period is unknown.  Would you expect to have a row with 0 sessions for each yearMonth and ChannelGrouping combination?  What if you added country and city as a dimension?

It is possible to get the results you're after, just not from a single query against the API.  Here is an outline of the steps using Analytics Canvas, but you can perform the same steps on your own using JavaScript (or any language to make the API calls) and SQL.

To get the results you're after make the following 3 queries:

1) yearMonth and sessions with empty rows:

2) channelGrouping and sessions over the time period

3) yearMonth, channelGrouping, and sessions (your query as shown above):

Now perform a cross join on queries 1 and 2, dropping sessions from your output.  You will have a table with all combinations of yearMonth and channelGrouping for your time period.  Let's call this Table 4.

Next perform a left outer join on Table 4 and Query 3, dropping yearMonth and channelGrouping from Query 3.

You now have Table 5, which has every combination of yearMonth and channelGrouping, values for sessions where there were sessions in that combination, and null values for the rest.

Finally replace the null values with zeros and you've got the dataset you're after.

isnull([Original.sessions], 0)


问:
monolithic or microservice concept
I have a very large django project with many features that uses django as backend framework. My project lets users use both a website and a iOS app.

I am researching using a monolithic app (currently using monolithic) vs micro services, I watched this video but one part really throws me off. At 1:05, he previews his 'monolithic' app before he changes to micro services, which to me looks like a single project with a bunch of different apps.

1) Are these technically just folders and not apps? These (what i would assume he calls folders) all have a models.py and views.py and most have a admin.py.

2) What makes this a monolithic app? Is it just because he doesn't simply use django-admin startapp in the terminal to create these 'folders'?

3) Or are microservices multiple projects connected and not simply multiple apps in a single project?

My biggest confusion is with the previewed project in the video because before then I thought I had a good grasp on these concepts. I was simply looking to change to microservices, after this part in the video I'm not sure I even know what a monolithic app really is.

答:
This is just a conflation of terminology.

In the context of monolithic apps vs. microservices, "application" refers to a web application, or in this case more specifically a WSGI application. A Django project is usually deployed as a WSGI application. So a monolithic app would be a deployment of a huge Django project, while microservices would be multiple smaller Django projects that are deployed separately.

"Monolithic app" may sometimes be used to refer to a Django application, i.e. a python module that is in INSTALLED_APPS. However, in that case you wouldn't be talking about microservices.

Title: monolithic or microservice concept


问:
Rails: How to delete a pending migration
I'm currently following the ruby on rails tutorial: http://guides.rubyonrails.org/getting_started.html.

I am trying to save data into the database. However, when I run: rails server I get the following error:

```<code>Migrations are pending. To resolve this issue, run: bin/rake db:migrate RAILS_ENV=development
</code>```

I've looked at the other articles and when I run:

```<code>bin/rake db:migrate
</code>```

I get a rake aborted!

After running:

```<code>rake db:abort_if_pending_migrations....
</code>```

I see that:

```<code>You have 1 pending migration:
20150805200129 CreateDatabases

SQLite3::SQLException: table "databases" already exists:
</code>```

and it just tells me to run rake db:migrate to start again.

It seems that it already exists. Is there a way to cancel the pending migration?

答:
If you are like me and maintain your database structure outside of Rails, you can just delete the migration file from db/migration. I got the error in the OP's question when I used the rails generate command to create a model class, forgetting that it also creates a migration file.

Do not use this method if you rely on Rails to maintain your database structure!

I keep my Rails structure file up to date by building it from the database using:

```<code>bundle exec rake db:structure:dump
</code>```

Body:
I'm currently following the ruby on rails tutorial: http://guides.rubyonrails.org/getting_started.html.

I am trying to save data into the database. However, when I run: rails server I get the following error:

Migrations are pending. To resolve this issue, run: bin/rake db:migrate RAILS_ENV=development

I've looked at the other articles and when I run:

bin/rake db:migrate

I get a rake aborted!

After running:

rake db:abort_if_pending_migrations....

I see that:

You have 1 pending migration:
20150805200129 CreateDatabases

SQLite3::SQLException: table "databases" already exists:

and it just tells me to run rake db:migrate to start again.

It seems that it already exists. Is there a way to cancel the pending migration?

Body:
If you are like me and maintain your database structure outside of Rails, you can just delete the migration file from db/migration. I got the error in the OP's question when I used the rails generate command to create a model class, forgetting that it also creates a migration file.

Do not use this method if you rely on Rails to maintain your database structure!

I keep my Rails structure file up to date by building it from the database using:

bundle exec rake db:structure:dump


问:
"open/close" SqlConnection or keep open?
I have my business-logic implemented in simple static classes with static methods. Each of these methods opens/closes SQL connection when called:

```<code>public static void DoSomething()
{
    using (SqlConnection connection = new SqlConnection("..."))
    {
        connection.Open();

        // ...

        connection.Close();
    }
}
</code>```

But I think passing the connection object around and avoiding opening and closing a connection saves performance. I made some tests long time ago with OleDbConnection class (not sure about SqlConnection), and it definitely helped to work like this (as far as I remember):

```<code>//pass around the connection object into the method
public static void DoSomething(SqlConnection connection)
{
    bool openConn = (connection.State == ConnectionState.Open);
    if (!openConn)
    {
        connection.Open();
    }

    // ....

    if (openConn)
    {
        connection.Close();
    }
}
</code>```

So the question is - should I choose the method (a) or method (b) ? I read in another stackoverflow question that connection pooling saved performance for me, I don't have to bother at all...

PS. It's an ASP.NET app - connections exist only during a web-request. Not a win-app or service.

答:
Use Method (a), every time. When you start scaling your application, the logic that deals with the state will become a real pain if you do not.

Connection pooling does what it says on the tin. Just think of what happens when the application scales, and how hard would it be to manually manage the connection open/close state. The connection pool does a fine job of automatically handling this. If you're worried about performance think about some sort of memory cache mechanism so that nothing gets blocked.

Body:
I have my business-logic implemented in simple static classes with static methods. Each of these methods opens/closes SQL connection when called:

public static void DoSomething()
{
    using (SqlConnection connection = new SqlConnection("..."))
    {
        connection.Open();

        // ...

        connection.Close();
    }
}

But I think passing the connection object around and avoiding opening and closing a connection saves performance. I made some tests long time ago with OleDbConnection class (not sure about SqlConnection), and it definitely helped to work like this (as far as I remember):

//pass around the connection object into the method
public static void DoSomething(SqlConnection connection)
{
    bool openConn = (connection.State == ConnectionState.Open);
    if (!openConn)
    {
        connection.Open();
    }

    // ....

    if (openConn)
    {
        connection.Close();
    }
}

So the question is - should I choose the method (a) or method (b) ? I read in another stackoverflow question that connection pooling saved performance for me, I don't have to bother at all...

PS. It's an ASP.NET app - connections exist only during a web-request. Not a win-app or service.


问:
Employing parallelism in pipelined execution
I am trying to develop a pipeline in which data is first read and processed, manipulated once, manipulated in a different way, and then displayed. I have a design in mind in which the data IO feeds into a buffer that is read by the first manipulator. Subsequently that first manipulator writes to another buffer which is read when possible by the second manipulator. Lastly, the output of the second manipulator is written to a display buffer which is read by the visualizer and displayed using OpenGL.

In my mind this is a fairly straightforward parallel problem in which each task has its own thread and they communicate via data buffers. However, all the tutorials I've come across for threaded programs seem to suggest that multithreading is something to be left up to some middleware (like OpenMP) that decides how to divide the workload.

I'm new to developing multithreaded applications, so this may be a dumb question, but is what I've described feasible and can it be done with middleware like OpenMP? I realize the obvious answer is "try it," and I want to, but the tutorials don't shed any light on *how* to try it.

答:
have you implemented single-threaded version? profiled?

they are crucial steps, w/o them you can get the optimal implementation of your highly-parallel design just to realise that the bottleneck is I/O of your buffers and/or thread synchronisation and/or false sharing and/or cache misses or similar issues.

I'd first try a simple thread pool with tasks that do all steps sequentially. Then after analysing how it works, what is CPU consumption etc. I'd experiment with more sophisticated tools always comparing their performance with the first simple edition


问:
GROUP BY clause and contains nonaggregated column when attempting to order by multiple records in relationship
Introduction

So the idea is that I have a players table, all we need to know about this table is that it has an id and username fields.

I also have a financials table, all we need to know about this table is that it references the players table through the player_id foreign key. It also has another two fields, staked and won.

A player can have many financials. So for example, a player could have 5 financials records associated with themselves.

The issue

I'm attempting to return a list of players which are sorted by their financial records. So for example, I'd like to retrieve the players who have made the most net revenue for the company. Here is a query I have written against Laravel's
Query Builder (Illuminate\Support\Facades\DB).

```<code>    DB::table('financials AS f')
        ->select(['*', DB::raw('SUM(staked - (won)) AS net_revenue')])
        ->join('players AS p', function ($join) use ($param) {
            $join->on('f.player_id', '=', 'p.id')
                ->where('p.id', 'like', '%' . $param . '%')
                ->orWhere('p.username', 'like', '%' . $param . '%');
            })
        ->orderBy('net_revenue')
        ->groupBy(['f.id'])
        ->paginate($perPage);
</code>```

The Error

```<code>SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #9 of
SELECT list is not in GROUP BY clause and contains nonaggregated column
'master.p.id' which is not functionally dependent on columns in GROUP BY
clause; this is incompatible with sql_mode=only_full_group_by (SQL: select
*, SUM(staked - (won)) AS net_revenue from `financials` as `f` inner join
`players` as `p` on `f`.`player_id` = `p`.`id` and `p`.`id` like %% or
`p`.`username` like %% group by `f`.`id` order by `net_revenue` asc limit 15
offset 0)
</code>```

Could someone please point me in the right direction to fixing this issue? I'd greatly appreciate it.

EDIT (08/06/2017):

```<code>      $query = DB::table('financials AS f1')
      ->select(['f1.id', 'f1.plays', 'f1.game_id', 'f1.staked', 'f1.won', 'f1.player_id', 'f1.date', 'f1.type', DB::raw('SUM(f1.staked - (f1.won)) AS net_revenue')])
      ->leftJoin('financials AS f2', 'f1.player_id', '=', 'f2.player_id')
      ->groupBy(['f1.id', 'f1.player_id', 'f1.game_id'])
      ->orderBy($orderBy)
      ->paginate($perPage);
</code>```

So my query now looks like this, and it works, it generates the net_revenue for each record. However it returns me multiple records for the same player_id.

To elaborate, if I have two records for the same player, it calculates the net_revenue for both records and orders them from highest to lowest, however what I want is only a single record for the player containing the calculation of all net_revenue.

答: Change the order by as SUM(staked - (won)) instead of the field name.</p>

Body:
Introduction

So the idea is that I have a players table, all we need to know about this table is that it has an id and username fields.

I also have a financials table, all we need to know about this table is that it references the players table through the player_id foreign key. It also has another two fields, staked and won.

A player can have many financials. So for example, a player could have 5 financials records associated with themselves.

The issue

I'm attempting to return a list of players which are sorted by their financial records. So for example, I'd like to retrieve the players who have made the most net revenue for the company. Here is a query I have written against Laravel's
Query Builder (Illuminate\Support\Facades\DB).

    DB::table('financials AS f')
        ->select(['*', DB::raw('SUM(staked - (won)) AS net_revenue')])
        ->join('players AS p', function ($join) use ($param) {
            $join->on('f.player_id', '=', 'p.id')
                ->where('p.id', 'like', '%' . $param . '%')
                ->orWhere('p.username', 'like', '%' . $param . '%');
            })
        ->orderBy('net_revenue')
        ->groupBy(['f.id'])
        ->paginate($perPage);

The Error

SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #9 of
SELECT list is not in GROUP BY clause and contains nonaggregated column
'master.p.id' which is not functionally dependent on columns in GROUP BY
clause; this is incompatible with sql_mode=only_full_group_by (SQL: select
*, SUM(staked - (won)) AS net_revenue from `financials` as `f` inner join
`players` as `p` on `f`.`player_id` = `p`.`id` and `p`.`id` like %% or
`p`.`username` like %% group by `f`.`id` order by `net_revenue` asc limit 15
offset 0)

Could someone please point me in the right direction to fixing this issue? I'd greatly appreciate it.

EDIT (08/06/2017):

      $query = DB::table('financials AS f1')
      ->select(['f1.id', 'f1.plays', 'f1.game_id', 'f1.staked', 'f1.won', 'f1.player_id', 'f1.date', 'f1.type', DB::raw('SUM(f1.staked - (f1.won)) AS net_revenue')])
      ->leftJoin('financials AS f2', 'f1.player_id', '=', 'f2.player_id')
      ->groupBy(['f1.id', 'f1.player_id', 'f1.game_id'])
      ->orderBy($orderBy)
      ->paginate($perPage);

So my query now looks like this, and it works, it generates the net_revenue for each record. However it returns me multiple records for the same player_id.

To elaborate, if I have two records for the same player, it calculates the net_revenue for both records and orders them from highest to lowest, however what I want is only a single record for the player containing the calculation of all net_revenue.


问:
Fibonacci sequence works in python, but not in c?
I have the following python code:

```<code>a, b = 1, 1
for i in range(0, 100):
    print a
    a, b = b, a + b
</code>```

It generates this:
1
1
2
3
5
8
etc

I wrote the same in c:

```<code>#include <stdio.h>
long long unsigned int a = 1, b = 1;
void main(){
    for(int i = 0; i < 100; i++){
        printf("%llu \n", a);
        a = b, b = a + b;
    }
}
</code>```

It generates this:
1
1
2
4
8
16
32
etc

Why does the c program generate powers of 2 when it is using the exact same operations?

答:
```<code>a, b = b, a + b
</code>```

in python packs the values b and a + b into a tuple, then unpacks it back into a and b.

C does not supports that feature, but rather use the comma to separate between assignments, so a = b, b = a + b get translated as

```<code>a = b;
b = a + b;
</code>```

where b gets doubled every time because the assignment is not simultaneous.

To fix that you'd have to assign each variable separately:

```<code>b = a + b;
a = b - a; // a + b - a = b
</code>```

Body:
I have the following python code:

a, b = 1, 1
for i in range(0, 100):
    print a
    a, b = b, a + b

It generates this:
1
1
2
3
5
8
etc

I wrote the same in c:

#include <stdio.h>
long long unsigned int a = 1, b = 1;
void main(){
    for(int i = 0; i < 100; i++){
        printf("%llu \n", a);
        a = b, b = a + b;
    }
}

It generates this:
1
1
2
4
8
16
32
etc

Why does the c program generate powers of 2 when it is using the exact same operations?

Body:
a, b = b, a + b

in python packs the values b and a + b into a tuple, then unpacks it back into a and b.

C does not supports that feature, but rather use the comma to separate between assignments, so a = b, b = a + b get translated as

a = b;
b = a + b;

where b gets doubled every time because the assignment is not simultaneous.

To fix that you'd have to assign each variable separately:

b = a + b;
a = b - a; // a + b - a = b


问:
Are numbered headings in Markdown / Rdiscount possible?
I'm trying to produce html with section / subsection headings resembling the following:

My top-level topic

1.1 My first subtopic

1.2 Another subtopic

      1.2.1 A sub-subtopic

Another top-level topic

Are there any implementations of Markdown capable of producing these kinds of numbered section headings?

答: As @adam-monsen points out 'pandoc --number-sections' does the trick. You can also simply add numbersections: true to th YAML-Header to activate  numbered headings for your file.


问:
"Requested allocation is too large" error while running MIT Scheme on windows 7
Error:

"Requested allocation is too large.
Try with smaller argument to --heap"

I downloaded the windows binary from http://www.gnu.org/software/mit-scheme/.

How do I fix this?

答:
Create a new system variable called

```<code>MITSCHEME_HEAP_SIZE
</code>```

and set its value to 512

Body:
Create a new system variable called

MITSCHEME_HEAP_SIZE

and set its value to 512


问:
Concat multiple C++ strings at once?
I'm new to C++ strings. Is there a way to concatenate more than two strings at once? Or I have to concat two strings at the time? My concern is that it probably needs to allocate memory on each operation, instead of only once for the final result.

Remember something like that in java, wonder if there is a method for that somewhere in std. Actually, std::stringstream might be it, but I don't know how exactly it works.

答:
The only way to avoid multiple allocations is to tell the string how big it needs to become before concatenating.

For example, to join together all the strings stored in a vector (like a list):

```<code>std::string join(std::vector<std::string> const& v)
{
    std::string r; // return string

    // add up all the string sizes
    std::size_t size = 0;
    for(auto const& s: v)
        size += s.size();

    // reserve that much space all at once (one allocation)
    r.reserve(size);

    // now do the concatenations
    for(auto const& s: v)
        r += s;

    return r;
}
</code>```

Title: Concat multiple C++ strings at once?

Body:
The only way to avoid multiple allocations is to tell the string how big it needs to become before concatenating.

For example, to join together all the strings stored in a vector (like a list):

std::string join(std::vector<std::string> const& v)
{
    std::string r; // return string

    // add up all the string sizes
    std::size_t size = 0;
    for(auto const& s: v)
        size += s.size();

    // reserve that much space all at once (one allocation)
    r.reserve(size);

    // now do the concatenations
    for(auto const& s: v)
        r += s;

    return r;
}


问:
`pickle`: yet another `ImportError: No module named my_module`
I have a class MyClass defined in my_module. MyClass has a method pickle_myself which pickles the instance of the class in question:

```<code>def pickle_myself(self, pkl_file_path):
    with open(pkl_file_path, 'w+') as f:
        pkl.dump(self, f, protocol=2)
</code>```

I have made sure that my_module is in PYTHONPATH. In the interpreter, executing __import__('my_module') works fine:

```<code>>>> __import__('my_module')
<module 'my_module' from 'A:\my_stuff\my_module.pyc'>
</code>```

However, when eventually loading the file, I get:

```<code>File "A:\Anaconda\lib\pickle.py", line 1128, in find_class
  __import__(module)
ImportError: No module named my_module
</code>```

Some things I have made sure of:

I have not changed the location of my_module.py (Python pickling after changing a module's directory)

I have tried to use dill instead, but still get the same error (More on python ImportError No module named)

EDIT -- A toy example that reproduces the error:

The example itself is spread over a bunch of files.

First, we have the module ball (stored in a file called ball.py):

```<code>class Ball():
    def __init__(self, ball_radius):
        self.ball_radius = ball_radius

    def say_hello(self):
        print "Hi, I'm a ball with radius {}!".format(self.ball_radius)
</code>```

Then, we have the module test_environment:

```<code>import os
import ball
#import dill as pkl
import pickle as pkl

class Environment():
    def __init__(self, store_dir, num_balls, default_ball_radius):
        self.store_dir = store_dir
        self.balls_in_environment = [ball.Ball(default_ball_radius) for x in range(num_balls)]

    def persist(self):
        pkl_file_path = os.path.join(self.store_dir, "test_stored_env.pkl")

        with open(pkl_file_path, 'w+') as f:
            pkl.dump(self, f, protocol=2)
</code>```

Then, we have a module that has functions to make environments, persist them, and load them, called make_persist_load:

```<code>import os
import test_environment
#import pickle as pkl
import dill as pkl

def make_env_and_persist():
    cwd = os.getcwd()

    my_env = test_environment.Environment(cwd, 5, 5)

    my_env.persist()

def load_env(store_path):
    stored_env = None

    with open(store_path, 'rb') as pkl_f:
        stored_env = pkl.load(pkl_f)

    return stored_env
</code>```

Then we have a script to put it all together, in test_serialization.py:

```<code>import os
import make_persist_load

MAKE_AND_PERSIST = True
LOAD = (not MAKE_AND_PERSIST)

cwd = os.getcwd()
store_path = os.path.join(cwd, "test_stored_env.pkl")

if MAKE_AND_PERSIST == True:
    make_persist_load.make_env_and_persist()

if LOAD == True:
    loaded_env = make_persist_load.load_env(store_path)
</code>```

In order to make it easy to use this toy example, I have put it all up on in a Github repository that simply needs to be cloned into your directory of choice.. Please see the README containing instructions, which I also reproduce here:

Instructions:

1) Clone repository into a directory.

2) Add repository directory to PYTHONPATH.

3) Open up test_serialization.py, and set the variable MAKE_AND_PERSIST to True. Run the script in an interpreter.

4) Close the previous interpreter instance, and start up a new one. In test_serialization.py, change MAKE_AND_PERSIST to False, and this will programmatically set LOAD to True. Run the script in an interpreter, causing ImportError: No module named test_environment.

5) By default, the test is set to use dill, instead of pickle. In order to change this, go into test_environment.py and make_persist_load.py, to change imports as required.

EDIT: after switching to dill '0.2.5.dev0', dill.detect.trace(True) output

```<code>C2: test_environment.Environment
# C2
D2: <dict object at 0x000000000A9BDAE8>
C2: ball.Ball
# C2
D2: <dict object at 0x000000000AA25048>
# D2
D2: <dict object at 0x000000000AA25268>
# D2
D2: <dict object at 0x000000000A9BD598>
# D2
D2: <dict object at 0x000000000A9BD9D8>
# D2
D2: <dict object at 0x000000000A9B0BF8>
# D2
# D2
</code>```

EDIT: the toy example works perfectly well when run on Mac/Ubuntu (i.e. Unix-like systems?). It only fails on Windows.

答:
In case someone is having same problem, I had the same problem running Python 2.7 and the problem was the pickle file created on windows while I am running Linux, what I had to do is running dos2unix which has to be downloaded first using

```<code>sudo yum install dos2unix
</code>```

And then you need to convert the pickle file example

```<code>dos2unix data.p
</code>```

Body:
I have a class MyClass defined in my_module. MyClass has a method pickle_myself which pickles the instance of the class in question:

def pickle_myself(self, pkl_file_path):
    with open(pkl_file_path, 'w+') as f:
        pkl.dump(self, f, protocol=2)

I have made sure that my_module is in PYTHONPATH. In the interpreter, executing __import__('my_module') works fine:

>>> __import__('my_module')
<module 'my_module' from 'A:\my_stuff\my_module.pyc'>

However, when eventually loading the file, I get:

File "A:\Anaconda\lib\pickle.py", line 1128, in find_class
  __import__(module)
ImportError: No module named my_module

Some things I have made sure of:

I have not changed the location of my_module.py (Python pickling after changing a module's directory)

I have tried to use dill instead, but still get the same error (More on python ImportError No module named)

EDIT -- A toy example that reproduces the error:

The example itself is spread over a bunch of files.

First, we have the module ball (stored in a file called ball.py):

class Ball():
    def __init__(self, ball_radius):
        self.ball_radius = ball_radius

    def say_hello(self):
        print "Hi, I'm a ball with radius {}!".format(self.ball_radius)

Then, we have the module test_environment:

import os
import ball
#import dill as pkl
import pickle as pkl

class Environment():
    def __init__(self, store_dir, num_balls, default_ball_radius):
        self.store_dir = store_dir
        self.balls_in_environment = [ball.Ball(default_ball_radius) for x in range(num_balls)]

    def persist(self):
        pkl_file_path = os.path.join(self.store_dir, "test_stored_env.pkl")

        with open(pkl_file_path, 'w+') as f:
            pkl.dump(self, f, protocol=2)

Then, we have a module that has functions to make environments, persist them, and load them, called make_persist_load:

import os
import test_environment
#import pickle as pkl
import dill as pkl

def make_env_and_persist():
    cwd = os.getcwd()

    my_env = test_environment.Environment(cwd, 5, 5)

    my_env.persist()

def load_env(store_path):
    stored_env = None

    with open(store_path, 'rb') as pkl_f:
        stored_env = pkl.load(pkl_f)

    return stored_env

Then we have a script to put it all together, in test_serialization.py:

import os
import make_persist_load

MAKE_AND_PERSIST = True
LOAD = (not MAKE_AND_PERSIST)

cwd = os.getcwd()
store_path = os.path.join(cwd, "test_stored_env.pkl")

if MAKE_AND_PERSIST == True:
    make_persist_load.make_env_and_persist()

if LOAD == True:
    loaded_env = make_persist_load.load_env(store_path)

In order to make it easy to use this toy example, I have put it all up on in a Github repository that simply needs to be cloned into your directory of choice.. Please see the README containing instructions, which I also reproduce here:

Instructions:

1) Clone repository into a directory.

2) Add repository directory to PYTHONPATH.

3) Open up test_serialization.py, and set the variable MAKE_AND_PERSIST to True. Run the script in an interpreter.

4) Close the previous interpreter instance, and start up a new one. In test_serialization.py, change MAKE_AND_PERSIST to False, and this will programmatically set LOAD to True. Run the script in an interpreter, causing ImportError: No module named test_environment.

5) By default, the test is set to use dill, instead of pickle. In order to change this, go into test_environment.py and make_persist_load.py, to change imports as required.

EDIT: after switching to dill '0.2.5.dev0', dill.detect.trace(True) output

C2: test_environment.Environment
# C2
D2: <dict object at 0x000000000A9BDAE8>
C2: ball.Ball
# C2
D2: <dict object at 0x000000000AA25048>
# D2
D2: <dict object at 0x000000000AA25268>
# D2
D2: <dict object at 0x000000000A9BD598>
# D2
D2: <dict object at 0x000000000A9BD9D8>
# D2
D2: <dict object at 0x000000000A9B0BF8>
# D2
# D2

EDIT: the toy example works perfectly well when run on Mac/Ubuntu (i.e. Unix-like systems?). It only fails on Windows.

Body:
In case someone is having same problem, I had the same problem running Python 2.7 and the problem was the pickle file created on windows while I am running Linux, what I had to do is running dos2unix which has to be downloaded first using

sudo yum install dos2unix

And then you need to convert the pickle file example

dos2unix data.p


答:
Because , has different meanings in C and python. In python:

```<code>a, b = b, a + b
</code>```

means modify a and b (simultaneously) with respective values b and a+b.

While in C:

```<code> a = b, b = a + b;
</code>```

means do a=b and then after b=a+b.

Title: Fibonacci sequence works in python, but not in c?

Body:
Because , has different meanings in C and python. In python:

a, b = b, a + b

means modify a and b (simultaneously) with respective values b and a+b.

While in C:

 a = b, b = a + b;

means do a=b and then after b=a+b.


问:
Why is it impossible to call static methods on Nullable<T> shorthands?
I thought T? is just a compiler shorthand for Nullable<T>. According to MSDN:

The syntax T? is shorthand for Nullable<T>, where T is a value type.
  The two forms are interchangeable.

However, there is a little (insignificant) difference: Visual Studio doesn't allow me to call static methods on shorthands:

```<code>bool b1 = Nullable<int>.Equals(1, 2); //no error
bool b2 = int?.Equals(1, 2); //syntax error "Invalid expression term 'int'"
</code>```

Why? Is there any reason for this limitation?

答:
Your MSDN quote echoes §4.1.10 of the C# 5.0 specification:

A nullable type is written T?, where T is the underlying type. This syntax is shorthand for System.Nullable<T>, and the two forms can be used interchangeably.

But “interchangeably” is an oversimplification. It’s true that T? means System.Nullable<T>, but as you’ve discovered, you can’t use T? everywhere that you can use System.Nullable<T>. In particular, the kind of member-access (§7.6.4) in your example requires a simple-name (§7.6.2):

[§7.6] Primary expressions include the simplest forms of expressions.

primary-expression:

    primary-no-array-creation-expression

    array-creation-expression

primary-no-array-creation-expression

    literal

    simple-name

    parenthesized-expression

    member-access

    ...

[§7.6.2] A simple-name is either of the form I or of the form I<A1, ..., AK>, where I is a single identifier and <A1, ..., AK> is an optional type-argument-list.

[§7.6.4] A member-access is either of the form E.I or of the form E.I<A1, ..., AK>, where E is a primary-expression, I is a single identifier and <A1, ..., AK> is an optional type-argument-list.

Nullable<T> is a simple-name and T? isn’t, so the former compiles whereas the latter doesn’t.

Why did the C# language designers require a member-access expression to use a simple-name as opposed to any type? I suppose only they can say for sure, but maybe this requirement simplified the grammar: In an expression, the compiler can assume that ? is always the conditional (ternary) operator instead of possibly a nullable type specifier.

In hindsight though, this was a fortunate choice that allowed C# 6.0 to add the ?. operator without possibly breaking existing programs. For example, consider this pathological example:

```<code>struct S
{
    public bool Equals(int x, int y) { return false; }
}

class C
{
    public static void Main()
    {
        S? S = new S();
        Console.WriteLine(S?.Equals(1, 1)); // "True" or "False"?
    }
}
</code>```

Should S?.Equals be parsed as Nullable<S> . Equals, a call to the Equals static method of class Object? Or should it be parsed as S ?. Equals, a null-conditional call to the Equals instance method of variable S? Because S? isn’t a simple-name, it’s unambiguously the latter.

Body:
I thought T? is just a compiler shorthand for Nullable<T>. According to MSDN:

The syntax T? is shorthand for Nullable<T>, where T is a value type.
  The two forms are interchangeable.

However, there is a little (insignificant) difference: Visual Studio doesn't allow me to call static methods on shorthands:

bool b1 = Nullable<int>.Equals(1, 2); //no error
bool b2 = int?.Equals(1, 2); //syntax error "Invalid expression term 'int'"

Why? Is there any reason for this limitation?

Body:
Your MSDN quote echoes §4.1.10 of the C# 5.0 specification:

A nullable type is written T?, where T is the underlying type. This syntax is shorthand for System.Nullable<T>, and the two forms can be used interchangeably.

But “interchangeably” is an oversimplification. It’s true that T? means System.Nullable<T>, but as you’ve discovered, you can’t use T? everywhere that you can use System.Nullable<T>. In particular, the kind of member-access (§7.6.4) in your example requires a simple-name (§7.6.2):

[§7.6] Primary expressions include the simplest forms of expressions.

primary-expression:

    primary-no-array-creation-expression

    array-creation-expression

primary-no-array-creation-expression

    literal

    simple-name

    parenthesized-expression

    member-access

    ...

[§7.6.2] A simple-name is either of the form I or of the form I<A1, ..., AK>, where I is a single identifier and <A1, ..., AK> is an optional type-argument-list.

[§7.6.4] A member-access is either of the form E.I or of the form E.I<A1, ..., AK>, where E is a primary-expression, I is a single identifier and <A1, ..., AK> is an optional type-argument-list.

Nullable<T> is a simple-name and T? isn’t, so the former compiles whereas the latter doesn’t.

Why did the C# language designers require a member-access expression to use a simple-name as opposed to any type? I suppose only they can say for sure, but maybe this requirement simplified the grammar: In an expression, the compiler can assume that ? is always the conditional (ternary) operator instead of possibly a nullable type specifier.

In hindsight though, this was a fortunate choice that allowed C# 6.0 to add the ?. operator without possibly breaking existing programs. For example, consider this pathological example:

struct S
{
    public bool Equals(int x, int y) { return false; }
}

class C
{
    public static void Main()
    {
        S? S = new S();
        Console.WriteLine(S?.Equals(1, 1)); // "True" or "False"?
    }
}

Should S?.Equals be parsed as Nullable<S> . Equals, a call to the Equals static method of class Object? Or should it be parsed as S ?. Equals, a null-conditional call to the Equals instance method of variable S? Because S? isn’t a simple-name, it’s unambiguously the latter.


问:
applying function to vector wise to a matrix
I have a 4 x 2 matrix, i.e. a numpy vector(of length 4) of numpy vectors of length two. For example a = [[1,1],[1,2],[3,5],[8,3]]
I want to subtract the vector  b = [3,6] from each row.
I tried to do the following:

```<code>np.vectorize(lamda x: x-b)(a)
</code>```

but i get the error ValueError:

```<code>setting an array element with a sequence.
</code>```

Can somebody explain me why and how to do this the right way?

答:
first convert them to numpy array and then subtract b from a:</p>

```<code>a = np.asarray(a)
b = np.asarray(b)
print a - b
</code>```

Body:
I have a 4 x 2 matrix, i.e. a numpy vector(of length 4) of numpy vectors of length two. For example a = [[1,1],[1,2],[3,5],[8,3]]
I want to subtract the vector  b = [3,6] from each row.
I tried to do the following:

np.vectorize(lamda x: x-b)(a)

but i get the error ValueError:

setting an array element with a sequence.

Can somebody explain me why and how to do this the right way?

Body:
first convert them to numpy array and then subtract b from a:

a = np.asarray(a)
b = np.asarray(b)
print a - b


问:
git-svn rebase and dcommit problem
```<code>git version 1.7.1
svn, version 1.6.12
Ubuntu 10.10
</code>```

I have just git and I have used svn. But I haven't used then together. I had a git repository, and I had to more my repository to a subversion one. So I have been using git-svn. Which works ok, most of the time. However, I find going round in circles.

I am the only one working on this project.

I make some changes to my branch. I then stage them:

```<code>git stage gateway.c
</code>```

Then commit them locally to git:

```<code>git commit m"Made some changes"
</code>```

Then I want to commit them to subversion. Get the latest updates:

```<code>git svn rebase
</code>```

Then I get the following message:

```<code>It seems that I cannot create a rebase-apply directory, and
I wonder if you are in the middle of patch application or another
rebase.  If that is not the case, please
        rm -fr /home/joe/projects/gateway/.git/rebase-apply
and run me again.  I am stopping in case you still have something
valuable there.
rebase refs/remotes/trunk: command returned error: 1

I then doing the following:
rm -fr /home/joe/projects/gateway/.git/rebase-apply
</code>```

Then I do a rebase again:

```<code>git svn rebase
</code>```

The message is this:

```<code>First, rewinding head to replay your work on top of it...
Applying: Issue with getting the port from the user context.
Using index info to reconstruct a base tree...
Falling back to patching base and 3-way merge...
Auto-merging driver.c
Auto-merging gateway.c
CONFLICT (content): Merge conflict in gateway.c
Failed to merge in the changes.
Patch failed at 0001 Issue with getting the port from the user context.

When you have resolved this problem run "git rebase --continue".
If you would prefer to skip this patch, instead run "git rebase --skip".
To restore the original branch and stop rebasing run "git rebase --abort".

rebase refs/remotes/trunk: command returned error: 1
</code>```

Which puts me on a branch called, and what is this branch and what is it for?:

```<code>*(no branch)
</code>```

I then resolve the conflicts to that branch. I then checkout my play_video branch. I try to make a another:

```<code>svn git dcommit
</code>```

And I end up going round in circles again.

Before I start pulling my hair out, can some offer me some advice,

Many thanks for any suggestions,

答:
No a complete answer, but what seems certain is that:

```<code>*(no branch)
</code>```

means you end up in a DETACHED HEAD mode which you see in a git svn context in "Can I recover lost commits in a SVN repository using a local tracking git-svn branch?".

See also "Why did git detach my head?".

So make sure:

at each step you are not in a detached HEAD (no branch)

that you dcommit a branch which exists in your SVN repo (and not a pure local Git branch)

ContentLicense: CC BY-SA 2.5

Title: git-svn rebase and dcommit problem

Body:
git version 1.7.1
svn, version 1.6.12
Ubuntu 10.10

I have just git and I have used svn. But I haven't used then together. I had a git repository, and I had to more my repository to a subversion one. So I have been using git-svn. Which works ok, most of the time. However, I find going round in circles.

I am the only one working on this project.

I make some changes to my branch. I then stage them:

git stage gateway.c

Then commit them locally to git:

git commit m"Made some changes"

Then I want to commit them to subversion. Get the latest updates:

git svn rebase

Then I get the following message:

It seems that I cannot create a rebase-apply directory, and
I wonder if you are in the middle of patch application or another
rebase.  If that is not the case, please
        rm -fr /home/joe/projects/gateway/.git/rebase-apply
and run me again.  I am stopping in case you still have something
valuable there.
rebase refs/remotes/trunk: command returned error: 1

I then doing the following:
rm -fr /home/joe/projects/gateway/.git/rebase-apply

Then I do a rebase again:

git svn rebase

The message is this:

First, rewinding head to replay your work on top of it...
Applying: Issue with getting the port from the user context.
Using index info to reconstruct a base tree...
Falling back to patching base and 3-way merge...
Auto-merging driver.c
Auto-merging gateway.c
CONFLICT (content): Merge conflict in gateway.c
Failed to merge in the changes.
Patch failed at 0001 Issue with getting the port from the user context.

When you have resolved this problem run "git rebase --continue".
If you would prefer to skip this patch, instead run "git rebase --skip".
To restore the original branch and stop rebasing run "git rebase --abort".

rebase refs/remotes/trunk: command returned error: 1

Which puts me on a branch called, and what is this branch and what is it for?:

*(no branch)

I then resolve the conflicts to that branch. I then checkout my play_video branch. I try to make a another:

svn git dcommit

And I end up going round in circles again.

Before I start pulling my hair out, can some offer me some advice,

Many thanks for any suggestions,

Body:
No a complete answer, but what seems certain is that:

*(no branch)

means you end up in a DETACHED HEAD mode which you see in a git svn context in "Can I recover lost commits in a SVN repository using a local tracking git-svn branch?".

See also "Why did git detach my head?".

So make sure:

at each step you are not in a detached HEAD (no branch)

that you dcommit a branch which exists in your SVN repo (and not a pure local Git branch)


问:
Displaying Text and Images In CSS
I have a database set up for Weapons for Sale and I have my PHP code here:

```<code><?php

echo '<link rel="stylesheet" href="display.css" type="text/css">';

function FindPhoto($name)
{
    $dir_path = "http://www.chemicalzero.com/FireArms_Bis/Guns/";
    $extensions_array = array('jpg','png','jpeg');
    echo "<img src='$dir_path$name' style='width:100px;height:150px'>";
    echo "TESTING";
    echo "</div>";
}

$connection = mysql_connect('localhost', 'USER', 'PASSWORD');
mysql_select_db('DATABASE');

if(mysqli_connect_errno()){
    die("connection failed: "
    . mysqli_connect_error()
    . " (" . mysqli_connect_errno()
    . ")");
}

$query = "SELECT * FROM Guns"; //You don't need a ; like you do in SQL
$result = mysql_query($query);

echo "<table>"; // start a table tag in the HTML

while($row = mysql_fetch_array($result))
{

   print

   "<div id= 'item'>" .
   "<p>Make : ".$row["Make"]."</p>".
   "<p>Model: ".$row["Model"]."</p>".
   "<p>$".$row["Price"]."</p>".
   FindPhoto($row['Photo']);

}
echo "</table>"; //Close the table in HTML

mysql_close(); //Make sure to close out the database connection
</code>```

My PHP displays all information from my database correctly and in the way I want it. The task comes when I incorporate the CSS code:

#item {
  width:100px;
  text-align:center;
  border: 5px solid #D9D9D9;
  padding: 0px;
  list-style: none;
  width: 150px;
  float: left;
  margin-right: 15px;
  margin-bottom: 15px;
  border-radius: 10px;
  -moz-border-radius: 10px; /* firefox rounded corners */
  -webkit-border-radius: 10px; /* Safari rounded corners */
  min-height: 220px;
}

#item li h1 {
  text-align:center;
}

#item li#white {
   min-height: 220px;
}
```

The CSS code works fine except when I combine the two, I get one product that looks great and the other does not get included in the CSS. See picture:

How do I display the images correctly?

答:
You have not closed the div in print statement.</p>

```<code>while($row = mysql_fetch_array($result))
{

   print

   "<div id= 'item'>" .
   "<p>Make : ".$row["Make"]."</p>".
   "<p>Model: ".$row["Model"]."</p>".
   "<p>$".$row["Price"]."</p>".
   FindPhoto($row['Photo']).
   // Here
   "</div>";

}`
</code>```

Title: Displaying Text and Images In CSS

Body:
I have a database set up for Weapons for Sale and I have my PHP code here:

<?php

echo '<link rel="stylesheet" href="display.css" type="text/css">';

function FindPhoto($name)
{
    $dir_path = "http://www.chemicalzero.com/FireArms_Bis/Guns/";
    $extensions_array = array('jpg','png','jpeg');
    echo "";
    echo "TESTING";
    echo "
";
}

$connection = mysql_connect('localhost', 'USER', 'PASSWORD');
mysql_select_db('DATABASE');

if(mysqli_connect_errno()){
    die("connection failed: "
    . mysqli_connect_error()
    . " (" . mysqli_connect_errno()
    . ")");
}

$query = "SELECT * FROM Guns"; //You don't need a ; like you do in SQL
$result = mysql_query($query);

echo "
"; // start a table tag in the HTML

while($row = mysql_fetch_array($result))
{

   print

   "
" .
   "
Make : ".$row["Make"]."
".
   "
Model: ".$row["Model"]."
".
   "
$".$row["Price"]."
".
   FindPhoto($row['Photo']);

}
echo "
"; //Close the table in HTML

mysql_close(); //Make sure to close out the database connection

My PHP displays all information from my database correctly and in the way I want it. The task comes when I incorporate the CSS code:

#item {
  width:100px;
  text-align:center;
  border: 5px solid #D9D9D9;
  padding: 0px;
  list-style: none;
  width: 150px;
  float: left;
  margin-right: 15px;
  margin-bottom: 15px;
  border-radius: 10px;
  -moz-border-radius: 10px; /* firefox rounded corners */
  -webkit-border-radius: 10px; /* Safari rounded corners */
  min-height: 220px;
}

#item li h1 {
  text-align:center;
}

#item li#white {
   min-height: 220px;
}

The CSS code works fine except when I combine the two, I get one product that looks great and the other does not get included in the CSS. See picture:

How do I display the images correctly?

Body:
You have not closed the div in print statement.

while($row = mysql_fetch_array($result))
{

   print

   "<div id= 'item'>" .
   "<p>Make : ".$row["Make"]."</p>".
   "<p>Model: ".$row["Model"]."</p>".
   "<p>$".$row["Price"]."</p>".
   FindPhoto($row['Photo']).
   // Here
   "</div>";

}`


问:
Can't get jQuery CDN to work for WordPress plugin
I'm building my first WordPress plugin and I've run into some trouble. I first created a custom page template with a contact form I created. It used Bootstrap JS, Bootstrap CSS, jQuery, and then jQuery UI for a datepicker. It also used an external stylesheet. Everything worked really well. I'm now turning this page template into a plugin so that I can use it across several websites easily, and to easier pull the theme's default design. I originally added Bootstrap JS, Bootstrap CSS, jQuery, and jQuery UI in the head of the .php file like this:

```<code><head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <link rel="stylesheet" type="text/css" href="/chem-dry-contact/style.css" media="screen">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
    <link rel="stylesheet" type="text/css" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
</head>
</code>```

I now understand this isn't the best way to link to external stylesheets and scripts for WordPress, and especially not for a plugin. But it did work before.

I was able to get all my html to show up as a plugin with a shortcode and everything, but it doesn't look like all of the external files are working. Here's how I set it up with WordPress's enqueue function:

```<code>function add_plugin_scripts() {
  wp_enqueue_script( 'jquery-3.2.1', '/wp-content/plugins/ChemDry-PriceQuote/jquery-3.2.1.js', array(), null, true);
  wp_enqueue_script('bootstrap-js', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js', array(), null, true);
}
add_action( 'wp_enqueue_scripts', 'add_plugin_scripts' );

function add_plugin_styles () {
    wp_enqueue_style('style', '/wp-content/plugins/ChemDry-PriceQuote/style.css');
    wp_enqueue_style('bootstrap', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css');
}
add_action( 'wp_enqueue_scripts', 'add_plugin_styles' );
</code>```

My external CSS is working, and I think my Bootstrap CSS is working, but I don't think jquery and Bootstrap JS are working. The functionality it provided when I had it set up as a page template is no longer working, and when I tested it out by commenting out the jQuery line in my page template version, I got the exact same result as I'm getting now. I did already try to deregister jquery first and then try to reregister it...but that didn't work either.

Now I've read that WordPress comes with jQuery...but if I don't add it, I don't get the functionality I need. I've looked all over Stack Overflow for an answer but I can't quite seem to get this to work.

Again, this is my first plugin I've developed so I'd appreciate any help. Thank you.

答:
Try this, order matters, usually you want to load style first. Then you need jQuery loaded, then load bootstrap since bootstrap require jQuery.

// A $( document ).ready() block.
$(document).ready(function() {
  console.log("jQuery ready!");
});```
<pre class="snippet-code-html lang-html prettyprint-override"><code><head>
  <link rel="stylesheet" type="text/css" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <link rel="stylesheet" type="text/css" href="/chem-dry-contact/style.css" media="screen">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head></code>```

Body:
I'm building my first WordPress plugin and I've run into some trouble. I first created a custom page template with a contact form I created. It used Bootstrap JS, Bootstrap CSS, jQuery, and then jQuery UI for a datepicker. It also used an external stylesheet. Everything worked really well. I'm now turning this page template into a plugin so that I can use it across several websites easily, and to easier pull the theme's default design. I originally added Bootstrap JS, Bootstrap CSS, jQuery, and jQuery UI in the head of the .php file like this:

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

    <link rel="stylesheet" type="text/css" href="/chem-dry-contact/style.css" media="screen">

    <link rel="stylesheet" type="text/css" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">

I now understand this isn't the best way to link to external stylesheets and scripts for WordPress, and especially not for a plugin. But it did work before.

I was able to get all my html to show up as a plugin with a shortcode and everything, but it doesn't look like all of the external files are working. Here's how I set it up with WordPress's enqueue function:

function add_plugin_scripts() {
  wp_enqueue_script( 'jquery-3.2.1', '/wp-content/plugins/ChemDry-PriceQuote/jquery-3.2.1.js', array(), null, true);
  wp_enqueue_script('bootstrap-js', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js', array(), null, true);
}
add_action( 'wp_enqueue_scripts', 'add_plugin_scripts' );

function add_plugin_styles () {
    wp_enqueue_style('style', '/wp-content/plugins/ChemDry-PriceQuote/style.css');
    wp_enqueue_style('bootstrap', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css');
}
add_action( 'wp_enqueue_scripts', 'add_plugin_styles' );

My external CSS is working, and I think my Bootstrap CSS is working, but I don't think jquery and Bootstrap JS are working. The functionality it provided when I had it set up as a page template is no longer working, and when I tested it out by commenting out the jQuery line in my page template version, I got the exact same result as I'm getting now. I did already try to deregister jquery first and then try to reregister it...but that didn't work either.

Now I've read that WordPress comes with jQuery...but if I don't add it, I don't get the functionality I need. I've looked all over Stack Overflow for an answer but I can't quite seem to get this to work.

Again, this is my first plugin I've developed so I'd appreciate any help. Thank you.

Body:
Try this, order matters, usually you want to load style first. Then you need jQuery loaded, then load bootstrap since bootstrap require jQuery.

// A $( document ).ready() block.
$(document).ready(function() {
  console.log("jQuery ready!");
});

<head>
  <link rel="stylesheet" type="text/css" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <link rel="stylesheet" type="text/css" href="/chem-dry-contact/style.css" media="screen">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>


问:
Flask Postgres client on Azure: No module named 'psycopg2' but it is installed
I've build a Flask app that connects with Azure PostgreSQL database and uploads data from API to that database. Everything is working fine on my PC but when I open my webpage I get an error:

```<code>The page cannot be displayed because an internal server error has occurred.
</code>```

Digging deeper I get:

```<code>StdErr:
2017-05-30 16:15:48.465025: Unhandled exception in wfastcgi.py: Traceback (most recent call last):
  File "D:\Python34\Scripts\wfastcgi.py", line 711, in main
    env, handler = read_wsgi_handler(response.physical_path)
  File "D:\Python34\Scripts\wfastcgi.py", line 568, in read_wsgi_handler
    return env, get_wsgi_handler(handler_name)
  File "D:\Python34\Scripts\wfastcgi.py", line 541, in get_wsgi_handler
    handler = handler()
  File ".\virtualenv_proxy.py", line 93, in get_venv_handler
    handler = get_wsgi_handler(os.getenv('WSGI_ALT_VIRTUALENV_HANDLER'))
  File ".\virtualenv_proxy.py", line 62, in get_wsgi_handler
    raise ValueError('"%s" could not be imported%s' % (handler_name, last_tb))
ValueError: "main.app" could not be imported: Traceback (most recent call last):
  File ".\virtualenv_proxy.py", line 46, in get_wsgi_handler
    handler = __import__(module_name, fromlist=[name_list[0][0]])
  File "D:\home\site\wwwroot\main.py", line 8, in <module>
    import psycopg2
ImportError: No module named 'psycopg2'

2017-05-30 16:15:48.480649: wfastcgi.py 2.1.1 closed
</code>```

When I used regular psycopg2 package I got error even during deployment on Azure. After changing my requirements.txt to this, deployment is successful, but the stated problem appears.

```<code>Flask==0.12.1
requests==2.17.3
git+https://github.com/nwcell/psycopg2-windows.git@win32-py34#egg=psycopg2
</code>```

Deployment log:

```<code>Command: "D:\home\site\deployments\tools\deploy.cmd"
Handling python deployment.
KuduSync.NET from: 'D:\home\site\repository' to: 'D:\home\site\wwwroot'
Copying file: 'requirements.txt'
Detected requirements.txt.  You can skip Python specific steps with a .skipPythonDeployment file.
Detecting Python runtime from site configuration
Detected python-3.4
Found compatible virtual environment.
Pip install requirements.
Requirement already satisfied (use --upgrade to upgrade): Flask==0.12.1 in d:\home\site\wwwroot\env\lib\site-packages (from -r requirements.txt (line 1))
Requirement already satisfied (use --upgrade to upgrade): requests==2.17.3 in d:\home\site\wwwroot\env\lib\site-packages (from -r requirements.txt (line 2))
Requirement already satisfied (use --upgrade to upgrade): psycopg2 from git+https://github.com/nwcell/psycopg2-windows.git@win32-py34 in d:\home\site\wwwroot\env\lib\site-packages (from -r requirements.txt (line 3))
Cleaning up...
Overwriting web.config with web.3.4.config
        1 file(s) copied.
Finished successfully.
</code>```

There is also top of my file main.py:

```<code>#!/usr/bin/env python
import requests
import csv
from os import listdir
from os.path import isfile, join, splitext
from datetime import datetime
import time
import psycopg2
from flask import Flask
app = Flask(__name__)

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

I tried to run command

```<code>pip install git+https://github.com/nwcell/psycopg2-windows.git@win32-py34#egg=psycopg2
</code>```

In my folder D:\Python34\Scripts> on Kudu but I get the following exception:

```<code>Exception:
Traceback (most recent call last):
  File "D:\Python34\lib\shutil.py", line 370, in _rmtree_unsafe
    os.unlink(fullname)
PermissionError: [WinError 5] Access is denied: 'D:\\local\\Temp\\pip_build_RD00155D83D9EE$\\psycopg2\\.git\\objects\\pack\\pack-31a2b4dba04af9763e01b5b017870e20de3710c3.idx'
</code>```

But it succeed in my env folder:

```<code>D:\home\site\wwwroot\env\Scripts>pip install git+https://github.com/nwcell/psycopg2-windows.git@win32-py34#egg=psycopg2
Requirement already satisfied (use --upgrade to upgrade): psycopg2 from git+https://github.com/nwcell/psycopg2-windows.git@win32-py34 in d:\home\site\wwwroot\env\lib\site-packages
Cleaning up...
</code>```

答:
According to the offical document about Troubleshooting - Package Installation, it explained this case reason as below.

Some packages may not install using pip when run on Azure. It may simply be that the package is not available on the Python Package Index. It could be that a compiler is required (a compiler is not available on the machine running the web app in Azure App Service).

So the solution to deal with this issue is shown the explaination of the document section.

Create a directory wheelhouse on your project.

Download the psycopg package wheel from here, and put it to wheelhouse.

Edit the requirements.txt file for the package as below, then re-deploy your project to Azure.

```<code>--find-links wheelhouse
psycopg2==2.7.1
</code>```

Or you also can upload the wheel file to the path wwwroot of your webapp, and run the command pip install psycopy2‑2.7.1‑cp27‑cp27m‑win32.whl via Kudu console https://<your webapp name>.scm.azurewebsites.net/DebugConsole.

Body:
I've build a Flask app that connects with Azure PostgreSQL database and uploads data from API to that database. Everything is working fine on my PC but when I open my webpage I get an error:

The page cannot be displayed because an internal server error has occurred.

Digging deeper I get:

StdErr:
2017-05-30 16:15:48.465025: Unhandled exception in wfastcgi.py: Traceback (most recent call last):
  File "D:\Python34\Scripts\wfastcgi.py", line 711, in main
    env, handler = read_wsgi_handler(response.physical_path)
  File "D:\Python34\Scripts\wfastcgi.py", line 568, in read_wsgi_handler
    return env, get_wsgi_handler(handler_name)
  File "D:\Python34\Scripts\wfastcgi.py", line 541, in get_wsgi_handler
    handler = handler()
  File ".\virtualenv_proxy.py", line 93, in get_venv_handler
    handler = get_wsgi_handler(os.getenv('WSGI_ALT_VIRTUALENV_HANDLER'))
  File ".\virtualenv_proxy.py", line 62, in get_wsgi_handler
    raise ValueError('"%s" could not be imported%s' % (handler_name, last_tb))
ValueError: "main.app" could not be imported: Traceback (most recent call last):
  File ".\virtualenv_proxy.py", line 46, in get_wsgi_handler
    handler = __import__(module_name, fromlist=[name_list[0][0]])
  File "D:\home\site\wwwroot\main.py", line 8, in <module>
    import psycopg2
ImportError: No module named 'psycopg2'

2017-05-30 16:15:48.480649: wfastcgi.py 2.1.1 closed

When I used regular psycopg2 package I got error even during deployment on Azure. After changing my requirements.txt to this, deployment is successful, but the stated problem appears.

Flask==0.12.1
requests==2.17.3
git+https://github.com/nwcell/psycopg2-windows.git@win32-py34#egg=psycopg2

Deployment log:

Command: "D:\home\site\deployments\tools\deploy.cmd"
Handling python deployment.
KuduSync.NET from: 'D:\home\site\repository' to: 'D:\home\site\wwwroot'
Copying file: 'requirements.txt'
Detected requirements.txt.  You can skip Python specific steps with a .skipPythonDeployment file.
Detecting Python runtime from site configuration
Detected python-3.4
Found compatible virtual environment.
Pip install requirements.
Requirement already satisfied (use --upgrade to upgrade): Flask==0.12.1 in d:\home\site\wwwroot\env\lib\site-packages (from -r requirements.txt (line 1))
Requirement already satisfied (use --upgrade to upgrade): requests==2.17.3 in d:\home\site\wwwroot\env\lib\site-packages (from -r requirements.txt (line 2))
Requirement already satisfied (use --upgrade to upgrade): psycopg2 from git+https://github.com/nwcell/psycopg2-windows.git@win32-py34 in d:\home\site\wwwroot\env\lib\site-packages (from -r requirements.txt (line 3))
Cleaning up...
Overwriting web.config with web.3.4.config
        1 file(s) copied.
Finished successfully.

There is also top of my file main.py:

#!/usr/bin/env python
import requests
import csv
from os import listdir
from os.path import isfile, join, splitext
from datetime import datetime
import time
import psycopg2
from flask import Flask
app = Flask(__name__)

[...]

I tried to run command

pip install git+https://github.com/nwcell/psycopg2-windows.git@win32-py34#egg=psycopg2

In my folder D:\Python34\Scripts> on Kudu but I get the following exception:

Exception:
Traceback (most recent call last):
  File "D:\Python34\lib\shutil.py", line 370, in _rmtree_unsafe
    os.unlink(fullname)
PermissionError: [WinError 5] Access is denied: 'D:\\local\\Temp\\pip_build_RD00155D83D9EE$\\psycopg2\\.git\\objects\\pack\\pack-31a2b4dba04af9763e01b5b017870e20de3710c3.idx'

But it succeed in my env folder:

D:\home\site\wwwroot\env\Scripts>pip install git+https://github.com/nwcell/psycopg2-windows.git@win32-py34#egg=psycopg2
Requirement already satisfied (use --upgrade to upgrade): psycopg2 from git+https://github.com/nwcell/psycopg2-windows.git@win32-py34 in d:\home\site\wwwroot\env\lib\site-packages
Cleaning up...

Body:
According to the offical document about Troubleshooting - Package Installation, it explained this case reason as below.

Some packages may not install using pip when run on Azure. It may simply be that the package is not available on the Python Package Index. It could be that a compiler is required (a compiler is not available on the machine running the web app in Azure App Service).

So the solution to deal with this issue is shown the explaination of the document section.

Create a directory wheelhouse on your project.

Download the psycopg package wheel from here, and put it to wheelhouse.

Edit the requirements.txt file for the package as below, then re-deploy your project to Azure.

--find-links wheelhouse
psycopg2==2.7.1

Or you also can upload the wheel file to the path wwwroot of your webapp, and run the command pip install psycopy2‑2.7.1‑cp27‑cp27m‑win32.whl via Kudu console https://<your webapp name>.scm.azurewebsites.net/DebugConsole.


问:
Filter out rows with NaN values for certain column
I have a dataset and in some of the rows an attribute value is NaN. This data is loaded into a dataframe and I would like to only use the rows which consist of rows where all attribute have values. I tried doing it via sql:

```<code>val df_data = sqlContext.sql("SELECT * FROM raw_data WHERE attribute1 != NaN")
</code>```

I tried several variants on this, but I can't seem to get it working.

Another option would be to transform it to a RDD and then filter it, since filtering this dataframe to check if a attribute isNaN , does not work.

答:
This works:

```<code>where isNaN(tau_doc) = false
</code>```

e.g.

```<code>val df_data = sqlContext.sql("SELECT * FROM raw_data where isNaN(attribute1) = false")
</code>```

Body:
I have a dataset and in some of the rows an attribute value is NaN. This data is loaded into a dataframe and I would like to only use the rows which consist of rows where all attribute have values. I tried doing it via sql:

val df_data = sqlContext.sql("SELECT * FROM raw_data WHERE attribute1 != NaN")

I tried several variants on this, but I can't seem to get it working.

Another option would be to transform it to a RDD and then filter it, since filtering this dataframe to check if a attribute isNaN , does not work.

Body:
This works:

where isNaN(tau_doc) = false

e.g.

val df_data = sqlContext.sql("SELECT * FROM raw_data where isNaN(attribute1) = false")


问:
REST API: adapt a curl post to powershell
I try to adapt a working curl command to powershell

this is the working curl from postman:

```<code>curl -X POST \
  https://test.portal.com/api/v1/login \
  -H 'authorization: Basic ADSFws343==' \
  -H 'cache-control: no-cache' \
  -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
  -H 'postman-token: 06944eab-3634-dd34-4634-0966bd8872c5' \
  -F 'user[email]=xxx@xxx.com' \
  -F 'user[password]=xxx'
</code>```

This creates the following raw data

```<code>POST /api/v1/login HTTP/1.1
cache-control: no-cache
Postman-Token: 80b2b345-6f5d-4c63-bedf-c65038b1e5df
Authorization: Basic ADSFws343==
User-Agent: PostmanRuntime/3.0.11-hotfix.2
Accept: */*
Host: test.portal.com
accept-encoding: gzip, deflate
content-type: multipart/form-data; boundary=--------------------------266295771457397823506834
content-length: 309
Connection: close

----------------------------266295771457397823506834
Content-Disposition: form-data; name="user[email]"

xxx@xxx.com
----------------------------266295771457397823506834
Content-Disposition: form-data; name="user[password]"

xxx
----------------------------266295771457397823506834--
</code>```

and I get the following response:

```<code>HTTP/1.1 201 Created
Server: nginx/1.8.0
Date: Tue, 06 Jun 2017 13:42:54 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: close
Status: 201 Created
Cache-Control: max-age=0, private, must-revalidate
X-XSS-Protection: 1; mode=block
X-Request-Id: d1dd86e7-325a-43d5-bfda-46a9df4cc660
ETag: W/"3b793a3a1971dac89b48633e0e031237"
X-Frame-Options: SAMEORIGIN
X-Runtime: 0.115464
X-Content-Type-Options: nosniff
X-Powered-By: Phusion Passenger 5.1.4
</code>```

with the expected data in the body.

Now I struggle to adapt the code to powershell

```<code>[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$boundary = "--------------------------210183530616764504332035"
$URL = "https://test.portal.com/api/v1/login"
$email = "xxx@xxx.com"
$password= "xxx"

$headers = @{
    "cache-control"="no-cache";
    "Postman-Token"="3cf33ce8-716e-4ac1-b2da-c46df65d5307"
    Authorization="Basic ADSFws343==";
    "content-type"="multipart/form-data; boundary=$boundary"
    "User-Agent"="PostmanRuntime/3.0.11-hotfix.2"
    "Accept"="*/*"
    "accept-encoding"="gzip, deflate"
}

$LF = "`n"
$bodyLines = (
    "--$boundary",
    "Content-Disposition: form-data; name=`"user[email]`"$LF",
    $email,
    "--$boundary",
    "Content-Disposition: form-data; name=`"user[password]`"$LF",
    $password,
    "--$boundary--$LF"
    ) -join $LF

$response = Invoke-RestMethod -Uri $URL -Headers $headers -Method Post -TimeoutSec 20 -Body $bodyLines -DisableKeepAlive
</code>```

This throws an exception:

```<code>Invoke-RestMethod : Incomplete response received from application
</code>```

and creates the following raw data

```<code>POST https://test.portal.com/api/v1/login HTTP/1.1
Content-Type: multipart/form-data; boundary=--------------------------210183530616764504332065
Accept: */*
Postman-Token: 3cf33ce8-716e-4ac1-b2da-c46df65d5307
Authorization: Basic ADSFws343==
accept-encoding: gzip, deflate
cache-control: no-cache
User-Agent: PostmanRuntime/3.0.11-hotfix.2
Host: test.portal.com
Content-Length: 300
Connection: Close

----------------------------210183530616764504332065
Content-Disposition: form-data; name="user[email]"

xxx@xxx.com
----------------------------210183530616764504332065
Content-Disposition: form-data; name="user[password]"

xxx
----------------------------210183530616764504332065--
</code>```

and this response

```<code>HTTP/1.1 502 Bad Gateway
Server: nginx/1.8.0
Date: Tue, 06 Jun 2017 14:02:23 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 54
Connection: close
Status: 502 Bad Gateway
cache-control: no-cache, no-store, must-revalidate

<h2>Incomplete response received from application</h2>
</code>```

Where is my fault or what is missing? The only difference in the Raw Data is the somehow shorter content-length. even the boundarys have the same length as the postman curl

Thank you for taking your time

Edit: Changed the Headers to hash-table without effect and changed the powershell code to simulate the postman curl more accurate

Final Solution: Problem was the wrong format of the body. Changing

```<code>$LF = "`n"
</code>```

to

```<code>$LF = "`r`n"
</code>```

fixed the problem!

答:
Use hashtables for your headers. Sometimes you need to wrap them in a array</p>

```<code>$headers = @{
    authorization="Basic ADSFws343==";
    "cache-control"="no-cache";
    "content-type"="multipart/form-data";
    boundary="----WebKitFormBoundary7MA4YWxkTrZu0gW";
    "postman-token"="06944eab-3634-dd34-4634-0966bd8872c5"
}

$post =@{
    email="xxx@xxx.com"
    password="xxxxxxxx"
}

Invoke-WebRequest -Uri "https://test.portal.com/api/v1/login" -Method POST -Headers $headers -Body $Post -SessionVariable WebSession
$websession
</code>```

Body:
I try to adapt a working curl command to powershell

this is the working curl from postman:

curl -X POST \
  https://test.portal.com/api/v1/login \
  -H 'authorization: Basic ADSFws343==' \
  -H 'cache-control: no-cache' \
  -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
  -H 'postman-token: 06944eab-3634-dd34-4634-0966bd8872c5' \
  -F 'user[email]=xxx@xxx.com' \
  -F 'user[password]=xxx'

This creates the following raw data

POST /api/v1/login HTTP/1.1
cache-control: no-cache
Postman-Token: 80b2b345-6f5d-4c63-bedf-c65038b1e5df
Authorization: Basic ADSFws343==
User-Agent: PostmanRuntime/3.0.11-hotfix.2
Accept: */*
Host: test.portal.com
accept-encoding: gzip, deflate
content-type: multipart/form-data; boundary=--------------------------266295771457397823506834
content-length: 309
Connection: close

----------------------------266295771457397823506834
Content-Disposition: form-data; name="user[email]"

xxx@xxx.com
----------------------------266295771457397823506834
Content-Disposition: form-data; name="user[password]"

xxx
----------------------------266295771457397823506834--

and I get the following response:

HTTP/1.1 201 Created
Server: nginx/1.8.0
Date: Tue, 06 Jun 2017 13:42:54 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: close
Status: 201 Created
Cache-Control: max-age=0, private, must-revalidate
X-XSS-Protection: 1; mode=block
X-Request-Id: d1dd86e7-325a-43d5-bfda-46a9df4cc660
ETag: W/"3b793a3a1971dac89b48633e0e031237"
X-Frame-Options: SAMEORIGIN
X-Runtime: 0.115464
X-Content-Type-Options: nosniff
X-Powered-By: Phusion Passenger 5.1.4

with the expected data in the body.

Now I struggle to adapt the code to powershell

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$boundary = "--------------------------210183530616764504332035"
$URL = "https://test.portal.com/api/v1/login"
$email = "xxx@xxx.com"
$password= "xxx"

$headers = @{
    "cache-control"="no-cache";
    "Postman-Token"="3cf33ce8-716e-4ac1-b2da-c46df65d5307"
    Authorization="Basic ADSFws343==";
    "content-type"="multipart/form-data; boundary=$boundary"
    "User-Agent"="PostmanRuntime/3.0.11-hotfix.2"
    "Accept"="*/*"
    "accept-encoding"="gzip, deflate"
}

$LF = "`n"
$bodyLines = (
    "--$boundary",
    "Content-Disposition: form-data; name=`"user[email]`"$LF",
    $email,
    "--$boundary",
    "Content-Disposition: form-data; name=`"user[password]`"$LF",
    $password,
    "--$boundary--$LF"
    ) -join $LF

$response = Invoke-RestMethod -Uri $URL -Headers $headers -Method Post -TimeoutSec 20 -Body $bodyLines -DisableKeepAlive

This throws an exception:

Invoke-RestMethod : Incomplete response received from application

and creates the following raw data

POST https://test.portal.com/api/v1/login HTTP/1.1
Content-Type: multipart/form-data; boundary=--------------------------210183530616764504332065
Accept: */*
Postman-Token: 3cf33ce8-716e-4ac1-b2da-c46df65d5307
Authorization: Basic ADSFws343==
accept-encoding: gzip, deflate
cache-control: no-cache
User-Agent: PostmanRuntime/3.0.11-hotfix.2
Host: test.portal.com
Content-Length: 300
Connection: Close

----------------------------210183530616764504332065
Content-Disposition: form-data; name="user[email]"

xxx@xxx.com
----------------------------210183530616764504332065
Content-Disposition: form-data; name="user[password]"

xxx
----------------------------210183530616764504332065--

and this response

HTTP/1.1 502 Bad Gateway
Server: nginx/1.8.0
Date: Tue, 06 Jun 2017 14:02:23 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 54
Connection: close
Status: 502 Bad Gateway
cache-control: no-cache, no-store, must-revalidate

Incomplete response received from application

Where is my fault or what is missing? The only difference in the Raw Data is the somehow shorter content-length. even the boundarys have the same length as the postman curl

Thank you for taking your time

Edit: Changed the Headers to hash-table without effect and changed the powershell code to simulate the postman curl more accurate

Final Solution: Problem was the wrong format of the body. Changing

$LF = "`n"

to

$LF = "`r`n"

fixed the problem!

Body:
Use hashtables for your headers. Sometimes you need to wrap them in a array

$headers = @{
    authorization="Basic ADSFws343==";
    "cache-control"="no-cache";
    "content-type"="multipart/form-data";
    boundary="----WebKitFormBoundary7MA4YWxkTrZu0gW";
    "postman-token"="06944eab-3634-dd34-4634-0966bd8872c5"
}

$post =@{
    email="xxx@xxx.com"
    password="xxxxxxxx"
}

Invoke-WebRequest -Uri "https://test.portal.com/api/v1/login" -Method POST -Headers $headers -Body $Post -SessionVariable WebSession
$websession


问:
How do I efficiently apply a transform to a multi-channel numpy ndarray?
I have three channels of data. The channels represent x, y and z coordinates. For example, the x position for a point is stored in channel 1, the y position in channel 2 etc.

I would like to perform a transform on each point represented in these channels. That is, I would like to multiply x, y and z at each u, v position by a transformation matrix. Sure, I could manually iterate through each u, v position and extract the positional data. I feel like that is not the most efficient approach.

My question is, what is the most efficient approach to transforming a collection of points, where positional data is stored in channels corresponding to each dimension?

答:
What you describe is simply a matrix multiplication:

```<code>raw = numpy.array([ [1, 10, 100], [2, 20, 200], [3, 30, 300], [4, 40, 400] ])
transformation = numpy.matrix([ [1, 0, 0], [0, 0, 1], [0, -1, 0] ])
transformed = raw * transformation
</code>```

Perhaps your data are higher-dimensional than this M-datapoints-by-3-"channels" example? It's unclear what you mean by u and v but they sort-of-suggest your problem might be higher-dimensional.  If so, you could either investigate numpy.tensordot and numpy.einsum which allow generalization of matrix multiplication to higher dimensions,  or you could set raw to a view of your actual raw data that you then reshape down to M-by-3 (and undo that at the end by reshaping transformed back the way you want it).

Body:
What you describe is simply a matrix multiplication:

raw = numpy.array([ [1, 10, 100], [2, 20, 200], [3, 30, 300], [4, 40, 400] ])
transformation = numpy.matrix([ [1, 0, 0], [0, 0, 1], [0, -1, 0] ])
transformed = raw * transformation

Perhaps your data are higher-dimensional than this M-datapoints-by-3-"channels" example? It's unclear what you mean by u and v but they sort-of-suggest your problem might be higher-dimensional.  If so, you could either investigate numpy.tensordot and numpy.einsum which allow generalization of matrix multiplication to higher dimensions,  or you could set raw to a view of your actual raw data that you then reshape down to M-by-3 (and undo that at the end by reshaping transformed back the way you want it).


问:
Why does Matrix#[] return nil for non existent colum and an exeption for non existent row?
I'm having trouble understanding this behaviour of the Ruby Matrix class, is it just an (ugly) implementation detail or am I missing something?</p>

```<code>>irb
irb(main):001:0> require 'matrix'
=> true
irb(main):003:0> m = Matrix[[1,2,3], [4,5,6], [7,8,9]]
=> Matrix[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
irb(main):004:0> m[1,1]
=> 5
irb(main):005:0> m[1,10]
=> nil
irb(main):006:0> m[10,1]
NoMethodError: undefined method `[]' for nil:NilClass
        from c:/ruby/lib/ruby/1.9.1/matrix.rb:261:in `[]'
        from (irb):6
        from c:/ruby/bin/irb:12:in `<main>'
</code>```

答: Because in first case (1,10) the first index is applied to a Matrix (the same as to an Array) and the second is applied to row (again the same as to array). But in the second case (10,1) the first index is applied to array, but the second here is applied to nil (because there is no 10th row) - this raises exception.

Body:
I'm having trouble understanding this behaviour of the Ruby Matrix class, is it just an (ugly) implementation detail or am I missing something?

>irb
irb(main):001:0> require 'matrix'
=> true
irb(main):003:0> m = Matrix[[1,2,3], [4,5,6], [7,8,9]]
=> Matrix[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
irb(main):004:0> m[1,1]
=> 5
irb(main):005:0> m[1,10]
=> nil
irb(main):006:0> m[10,1]
NoMethodError: undefined method `[]' for nil:NilClass
        from c:/ruby/lib/ruby/1.9.1/matrix.rb:261:in `[]'
        from (irb):6
        from c:/ruby/bin/irb:12:in `'


问:
Should I adapt loss weights for misclassified samples during epochs?
I am using FCN (Fully Convolutional Networks) and trying to do image segmentation. When training, there are some areas which are mislabeled, however further training doesn't help much to make them go away. I believe this is because network learns about some features which might not be completely correct ones, but because there are enough correctly classified examples, it is stuck in local minimum and can't get out.

One solution I can think of is to train for an epoch, then validate the network on training images, and then adjust weights for mismatched parts to penalize mismatch more there in next epoch.

Intuitively, this makes sense to me - but I haven't found any writing on this. Is this a known technique? If yes, how is it called? If no, what am I missing (what are the downsides)?

答:
It highly depends on your network structure. If you are using the original FCN, due to the pooling operations, the segmentation performance on the boundary of your objects is degraded. There have been quite some variants over the original FCN for image segmentation, although they didn't go the route you're proposing.

Just name a couple of examples here. One approach is to use Conditional Random Field (CRF) on top of the FCN output to refine the segmentation. You may search for the relevant papers to get more idea on that. In some sense, it is close to your idea but the difference is that CRF is separated from the network as a post-processing approach.

Another very interesting work is U-net. It employs some idea from the residual network (RES-net), which enables high resolution features from lower levels can be integrated into high levels to achieve more accurate segmentation.

This is still a very active research area. So you may bring the next break-through with your own idea. Who knows! Have fun!