问:
Laravel Repository caching and pagination
I have created a base repository and am now extending it to add caching, but the problem I seem to be experiencing more than most is pagination

in my all() method, I do the following without caching:

```<code>public function all($pagination = 20)
{
    try
    {
        $query = $this->model->newQuery();
        return $this->handlePagination($query, $pagination);
    }
    catch (Exception $e)
    {
        throw $e;
    }
}

protected function handlePagination($query, $pagination)
{
    if (is_null($pagination))
    {
        return $query;
    }
    $collection = $query->paginate($pagination);
    return $collection;
}
</code>```

This is working well, but when I try to implement caching, I want to cache each model individually and store the keys for each collection, so, if I was paginating all of the entries, I would store each paginated collection in a cache:

```<code>cache set.1 [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
cache set.2 [21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40]
</code>```

etc...

the problem is that is seems impossible to use the actual pagination class to return the results since you are paginating only the id

I could return the data and the paginator separately but that seems very hacky.

Is there a way to repopulate the Paginator class with model data without over-writing the whole thing?

EDIT

I was thinking about something like this:

```<code>public function all($pagination = 20)
{
    try
    {
        $cache_key = $this->cache_key . '.all';
        $ids = Cache::get($cache_key);
        if (! $ids)
        {
            $query = $this->model->newQuery();
            $ids = $query->pluck('id');
            Cache::put($cache_key, $ids, $this->cache_ttl);
        }

        $ids = $this->handlePagination($ids, $pagination);
        $collection = new Collection();
        foreach ($ids as $id)
        {
            if ($model = $this->find($id))
            {
                $collection->put($id, $model);
            }
        }
        return $collection;
    }
    catch (Exception $e)
    {
        throw $e;
    }
}

/**
 * @var \Illuminate\Pagination\Paginator
 */
protected $paginator;
public function handlePagination($array, $pagination = 20)
{
    if (!is_null($pagination))
    {
        $this->paginator = Paginator::make($array, count($array), $pagination);
        return $this->paginator->getItems();
    }
    return $array;
}

public function getPaginator()
{
    return $this->paginator;
}
</code>```

答:
Pass the page number along as well for caching. Sonething like this

$currentPg = Input::get('page') ? Input::get('page') : '1';
$boards = Cache::remember('boards'.$currentPg, 60, function(){ return WhatEverModel::paginate(15); });```

Body:
I have created a base repository and am now extending it to add caching, but the problem I seem to be experiencing more than most is pagination

in my all() method, I do the following without caching:

public function all($pagination = 20)
{
    try
    {
        $query = $this->model->newQuery();
        return $this->handlePagination($query, $pagination);
    }
    catch (Exception $e)
    {
        throw $e;
    }
}

protected function handlePagination($query, $pagination)
{
    if (is_null($pagination))
    {
        return $query;
    }
    $collection = $query->paginate($pagination);
    return $collection;
}

This is working well, but when I try to implement caching, I want to cache each model individually and store the keys for each collection, so, if I was paginating all of the entries, I would store each paginated collection in a cache:

cache set.1 [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
cache set.2 [21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40]

etc...

the problem is that is seems impossible to use the actual pagination class to return the results since you are paginating only the id

I could return the data and the paginator separately but that seems very hacky.

Is there a way to repopulate the Paginator class with model data without over-writing the whole thing?

EDIT

I was thinking about something like this:

public function all($pagination = 20)
{
    try
    {
        $cache_key = $this->cache_key . '.all';
        $ids = Cache::get($cache_key);
        if (! $ids)
        {
            $query = $this->model->newQuery();
            $ids = $query->pluck('id');
            Cache::put($cache_key, $ids, $this->cache_ttl);
        }

        $ids = $this->handlePagination($ids, $pagination);
        $collection = new Collection();
        foreach ($ids as $id)
        {
            if ($model = $this->find($id))
            {
                $collection->put($id, $model);
            }
        }
        return $collection;
    }
    catch (Exception $e)
    {
        throw $e;
    }
}

/**
 * @var \Illuminate\Pagination\Paginator
 */
protected $paginator;
public function handlePagination($array, $pagination = 20)
{
    if (!is_null($pagination))
    {
        $this->paginator = Paginator::make($array, count($array), $pagination);
        return $this->paginator->getItems();
    }
    return $array;
}

public function getPaginator()
{
    return $this->paginator;
}


来源: stackexchange

问:
symbol or html entities code did not complied in jsx
How to compile symbol in jsx? I have a checking like this

{this.state.something && &#8226}
 this won't work it print the string.

答:
Answering this question based on my assumption of your question.Please correct me if I didn't get your question.

If you want to have a conditional check: {this.state.something && &#8226}

In current scenario this.state.something evaluates to true and then it will print the string '&#8226'. It is commonJS behaviour. You can check this in browser console.

If you want to have a condition to which you can do something when it evaluates to true try this:

```<code>{this.state.check && '&#8226' ? 'print this' : 'do not print this'}
</code>```

if '&#8226' does not evaluate the bullet(which is most likely), try '\u2022' instead.

P.S. Here is browser console result:

```<code>var text = ' asdjk ' + '&#8226'; ---> *text= " asdjk &#8226"*

var text = ' asdjk ' + '\u2022'; ----> *text = " asdjk •"*
</code>```

Checking whether bullet is present in text or not:

```<code>text.includes('&#8226') --->  *false*
text.includes('\u2022')--->*true*
</code>```

ContentLicense: CC BY-SA 3.0

Body:
Answering this question based on my assumption of your question.Please correct me if I didn't get your question.

If you want to have a conditional check: {this.state.something && &#8226}

In current scenario this.state.something evaluates to true and then it will print the string '&#8226'. It is commonJS behaviour. You can check this in browser console.

If you want to have a condition to which you can do something when it evaluates to true try this:

{this.state.check && '&#8226' ? 'print this' : 'do not print this'}

if '&#8226' does not evaluate the bullet(which is most likely), try '\u2022' instead.

P.S. Here is browser console result:

var text = ' asdjk ' + '&#8226'; ---> *text= " asdjk &#8226"*

var text = ' asdjk ' + '\u2022'; ----> *text = " asdjk •"*

Checking whether bullet is present in text or not:

text.includes('&#8226') --->  *false*
text.includes('\u2022')--->*true*


问:
Drag and drop on Win7 machine to my app
I have a user that is currently running my Winforms app on Win7.  My app allows users to select rows from an open Excel spreadsheet and drag-n-drop them onto the app.  However, this user cannot do the drag-n-drop.  The cursor changes to the "no" cursor (little circle with line through it) and the operation won't complete.

I was researching drag-n-drop and Win7 and everything I found points to UAC and/or UIPI.  I was looking for some solutions and am not sure if any of the below would work:

If the user logs in as admin (and as a result runs my app as admin) would that allow drag-n-drop to work?

Does the user need to turn off or change the settings of UAC/UIPI in order to be able to drag-n-drop?

I am not sure what the issue is.  My app usually runs from C:/Documents and Settings/... (C:/Users/... on Win7).  Does where it is running from matter?  Does drag-n-drop not work because the user is not running my app with enough permissions? Are his Excel and my app on different permission levels?  If so, what can be done about that?  Note that even though my app allows users to just drag the file directly, that doesn't work either.

Also, is there any way I can have the user reproduce this issue with other apps?  Are there apps that come with Win7 that he can see the same problem with.  For example, can this be reproduced using Notepad?

Thanks.

答:
Explaining this problem away by UIPI is a very long stretch.  It doesn't have anything to do with whether or not the user is logged-in as an admin, that doesn't affect UAC and your program will be running with that same account anyway.  The only way UIPI could kick in to stop a D+D is when your program is elevated and Excel is not.

To get yourself elevated requires work and doesn't happen by accident.  You'd have to include a manifest so that the user gets the UAC prompt, you'd know about that.  Or the user would have to change the desktop shortcut and tick the "Run this program as an administrator" option, she's know about that.  While UIPI can be bypassed for Windows messages (ChangeWindowMessageFilter), it cannot for Drag and Drop so if any elevation is going on then your stuck.  The ultimate test is to simply ask the user to put the UAC slider all the way down.

The much more likely scenario is that your DragEnter event handler simply isn't happy with the data it sees and therefore doesn't assign the e.Effect property.  If you can't get a debugger on-site then write a little test program that logs the values of e.Data.GetFormats() plus whatever else you use to check if the drop is acceptable.  And don't forget the obvious: the user simply fumbling the drag somehow.

ContentLicense: CC BY-SA 2.5

Title: Drag and drop on Win7 machine to my app


问:
Why do all browsers' user agents start with "Mozilla/"?
All popular browsers' user agent strings, even Internet Explorer's, start with Mozilla/. Why is this the case?

答:
Mozilla/5.0 is the general token that says the browser is Mozilla compatible, and is common to almost every browser today.
for more detail click here


问:
Split Strings into values in long dataframe format
I have a dataframe that looks like the following example df which consist of a character variable VAR.

```<code>df<-data.frame(ID = 1:2,
               VAR = c("VAL1\r\nVAL2\r\nVAL8","VAL2\r\nVAL5"),
               stringsAsFactors = FALSE)
#     ID                  VAR
# 1    1 VAL1\r\nVAL2\r\nVAL8
# 2    2         VAL2\r\nVAL5
</code>```

I would like to split the character variable by the return carriage - newline \r\n and obtain the desired dataframe below:

```<code>#    ID   VAR
# 1    1 VAL1
# 2    1 VAL2
# 3    1 VAL8
# 4    2 VAL2
# 5    2 VAL5
</code>```

I wrote the code as follows, but I somehow got lost in the gather function while trying to change the format of the data frame into a long format.

```<code>library(tidyverse)
df <- df %>%
  bind_cols(as.data.frame(str_split(df$VAR,"\r\n",simplify = TRUE))) %>%
  select(-VAR) %>%
  gather(key,value)
</code>```

Please advise.

答:
We can do this with separate_rows

```<code>library(tidyr)
separate_rows(df, VAR, sep='\\s+')
</code>```

Body:
I have a dataframe that looks like the following example df which consist of a character variable VAR.

df<-data.frame(ID = 1:2,
               VAR = c("VAL1\r\nVAL2\r\nVAL8","VAL2\r\nVAL5"),
               stringsAsFactors = FALSE)
#     ID                  VAR
# 1    1 VAL1\r\nVAL2\r\nVAL8
# 2    2         VAL2\r\nVAL5

I would like to split the character variable by the return carriage - newline \r\n and obtain the desired dataframe below:

#    ID   VAR
# 1    1 VAL1
# 2    1 VAL2
# 3    1 VAL8
# 4    2 VAL2
# 5    2 VAL5

I wrote the code as follows, but I somehow got lost in the gather function while trying to change the format of the data frame into a long format.

library(tidyverse)
df <- df %>%
  bind_cols(as.data.frame(str_split(df$VAR,"\r\n",simplify = TRUE))) %>%
  select(-VAR) %>%
  gather(key,value)

Please advise.

Body:
We can do this with separate_rows

library(tidyr)
separate_rows(df, VAR, sep='\\s+')


问:
Need to change date format in angular 2
Currently using Date() gives it in format

```<code>Wed Jul 05 2017 15:14:53 GMT+0530 (India Standard Time)
</code>```

i want to change it so that I get it in this format (want GMT time not IST)

```<code>Jul 5 15:14:53 2017 GMT
</code>```

I also want to know how I can get the time say 2 years from now.

EDIT:

I have fixed the format but now having problems in setting it up to gmt time instead of ist fixed it by :

```<code>let d = new Date();
let month = d.toLocaleString("en-us", { month: "short" });
let datestring = month + " " + d.getDate() + " " + (d.getHours()) + ":" + (d.getMinutes())+ ":" +d.getSeconds() + " " + (d.getFullYear()+2) + " GMT";
</code>```

but naturally that does not actauly show the gmt, its just IST so now trying to fix that.

答:
I would advise you to use a library as momentjs.com.

```<code>moment(mydate).add(1, 'year');
</code>```

Moment provide method to compare 2 dates...etc. I think i will fit your needs.

Title: Need to change date format in angular 2

Body:
Currently using Date() gives it in format

Wed Jul 05 2017 15:14:53 GMT+0530 (India Standard Time)

i want to change it so that I get it in this format (want GMT time not IST)

Jul 5 15:14:53 2017 GMT

I also want to know how I can get the time say 2 years from now.

EDIT:

I have fixed the format but now having problems in setting it up to gmt time instead of ist fixed it by :

let d = new Date();
let month = d.toLocaleString("en-us", { month: "short" });
let datestring = month + " " + d.getDate() + " " + (d.getHours()) + ":" + (d.getMinutes())+ ":" +d.getSeconds() + " " + (d.getFullYear()+2) + " GMT";

but naturally that does not actauly show the gmt, its just IST so now trying to fix that.

Body:
I would advise you to use a library as momentjs.com.

moment(mydate).add(1, 'year');

Moment provide method to compare 2 dates...etc. I think i will fit your needs.


问:
Is there a built-in function to sort and filter a python list in one step?
Given a directory of files all with numeric names, I currently sort and filter the directory list in two steps.

```<code>#files = os.listdir(path)
files = ["0", "1", "10", "5", "2", "11", "4", "15", "18", "14", "7", "8", "9"]

firstFile =  5
lastFile  = 15

#filter out any files that are not in the desired range
files = filter(lambda f: int(f) >= firstFile and int(f) < lastFile, files)

#sort the remaining files by timestamp
files.sort(lambda a,b: cmp(int(a), int(b)))
</code>```

Is there a python function that combines the filter and sort operations so the list only needs to be iterated over once?

答:
Those are orthogonal tasks, I don't think they should be mixed. Besides, it's easy to filter and sort separately in one line with generator expressions</p>

```<code>files = sorted( (f for f in files if firstFile <= int(f) < lastFile), key=int)
</code>```

Body:
Given a directory of files all with numeric names, I currently sort and filter the directory list in two steps.

#files = os.listdir(path)
files = ["0", "1", "10", "5", "2", "11", "4", "15", "18", "14", "7", "8", "9"]

firstFile =  5
lastFile  = 15

#filter out any files that are not in the desired range
files = filter(lambda f: int(f) >= firstFile and int(f) < lastFile, files)

#sort the remaining files by timestamp
files.sort(lambda a,b: cmp(int(a), int(b)))

Is there a python function that combines the filter and sort operations so the list only needs to be iterated over once?

Body:
Those are orthogonal tasks, I don't think they should be mixed. Besides, it's easy to filter and sort separately in one line with generator expressions

files = sorted( (f for f in files if firstFile <= int(f) < lastFile), key=int)


问:
PHP Regex explode the string using ',' except ',' inside quote and double qoute
How to do explode except using ',' but not inside quote and double quote?

This is the string i want to explode:

```<code>`ot_request_id` int(11) NOT NULL,`ot_hours` int(11) NOT NULL,`ot_timelog_id` int(11) NOT NULL,`ot_user_id` int(11) NOT NULL,`ot_filing_date` datetime NOT NULL,`ot_approveby_id` int(11) NOT NULL,`final_approved` int(11) NOT NULL DEFAULT '0' , `ot_token` varchar(100) DEFAULT 'Your, name, here' NOT NULL,`startTime` datetime NOT NULL,`endTime` datetime NOT NULL
</code>```

I want the output to become

```<code>`ot_request_id` int(11) NOT NULL `ot_request_id` int(11) NOT NULL,
`ot_hours` int(11) NOT NULL ,
`ot_timelog_id` int(11) NOT NULL,
`ot_user_id` int(11) NOT NULL,
`ot_filing_date` datetime NOT NULL,
`ot_approveby_id` int(11) NOT NULL,
`final_approved` int(11) NOT NULL DEFAULT '0' ,
`ot_token` varchar(100) DEFAULT 'Your, name, here' NOT NULL,
`startTime` datetime NOT NULL,
`endTime` datetime NOT NULL
</code>```

here is my code:

```<code>$array = explode(",",$str);// change to Regex
foreach($array as $m){
    echo "<br>$m";
}
</code>```

答:
A bit different from your question about "between quotes": it seems de comma to break on, is always before a backtic (or white space followed by backtic)

```<code><?php
$s = "`ot_request_id` int(11) NOT NULL,`ot_hours` int(11) NOT NULL,`ot_timelog_id` int(11) NOT NULL,`ot_user_id` int(11) NOT NULL,`ot_filing_date` datetime NOT NULL,`ot_approveby_id` int(11) NOT NULL,`final_approved` int(11) NOT NULL DEFAULT '0' , `ot_token` varchar(100) DEFAULT 'Your, name, here' NOT NULL,`startTime` datetime NOT NULL,`endTime` datetime NOT NULL";

$b = preg_replace('#,\s*`#', '[BR]`', $s);

$answer = explode('[BR]', $b);
print_r($answer);
</code>```

would be easier when the whitespace would not be there (at ot_token)

note: preg_split() would avoid the extra step inserting a [BR] only to explode on it the next line: better use preg_split as already answered

Body:
How to do explode except using ',' but not inside quote and double quote?

This is the string i want to explode:

`ot_request_id` int(11) NOT NULL,`ot_hours` int(11) NOT NULL,`ot_timelog_id` int(11) NOT NULL,`ot_user_id` int(11) NOT NULL,`ot_filing_date` datetime NOT NULL,`ot_approveby_id` int(11) NOT NULL,`final_approved` int(11) NOT NULL DEFAULT '0' , `ot_token` varchar(100) DEFAULT 'Your, name, here' NOT NULL,`startTime` datetime NOT NULL,`endTime` datetime NOT NULL

I want the output to become

`ot_request_id` int(11) NOT NULL `ot_request_id` int(11) NOT NULL,
`ot_hours` int(11) NOT NULL ,
`ot_timelog_id` int(11) NOT NULL,
`ot_user_id` int(11) NOT NULL,
`ot_filing_date` datetime NOT NULL,
`ot_approveby_id` int(11) NOT NULL,
`final_approved` int(11) NOT NULL DEFAULT '0' ,
`ot_token` varchar(100) DEFAULT 'Your, name, here' NOT NULL,
`startTime` datetime NOT NULL,
`endTime` datetime NOT NULL

here is my code:

$array = explode(",",$str);// change to Regex
foreach($array as $m){
    echo "
$m";
}

Body:
A bit different from your question about "between quotes": it seems de comma to break on, is always before a backtic (or white space followed by backtic)

<?php
$s = "`ot_request_id` int(11) NOT NULL,`ot_hours` int(11) NOT NULL,`ot_timelog_id` int(11) NOT NULL,`ot_user_id` int(11) NOT NULL,`ot_filing_date` datetime NOT NULL,`ot_approveby_id` int(11) NOT NULL,`final_approved` int(11) NOT NULL DEFAULT '0' , `ot_token` varchar(100) DEFAULT 'Your, name, here' NOT NULL,`startTime` datetime NOT NULL,`endTime` datetime NOT NULL";

$b = preg_replace('#,\s*`#', '[BR]`', $s);

$answer = explode('[BR]', $b);
print_r($answer);

would be easier when the whitespace would not be there (at ot_token)

note: preg_split() would avoid the extra step inserting a [BR] only to explode on it the next line: better use preg_split as already answered


问:
How to convert java project to Maven project or similar
I am currently creating an application for compiling multiple java projects in one go for programming assignments. As there are many different ways to submit java projects(e.g. as eclipse project, as netbeans project, as jar file) is there a plugin of something similar that you can easily import or use in a project that will convert a java project to a maven project so they can all be compiled in the same way rather then having to create functionality to allow the program to compile all different types of java projects?

If not to maven projects, is there another way to accomplish what i am trying to do?

答:
I have done the same for Selenium Project. Hope will be useful

Please install TestNG and Maven in your system. Follow all Step 1 to Step 8, and Run your Project

Step:1 Right click on Java Project, click on Configure navigate to Convert to Maven Project.

Follow, figure 1.1 and figure 1.2

Step:2 Add all dependencies (required jars and plugins surefire-plugin for testing.xml)

How to add jars,

Way:1 Search in “https://mvnrepository.com/ “and add the repository

Way:2 Add jars from Local system (Chose either way1 or way2 or both if needed)
Create a folder (Say: lib) and place in the same directory

```<code><dependency>
<groupId>selenium-server-standalone-3.3.1</groupId>
<artifactId>selenium-server-standalone-3.3.1</artifactId>
<version>3.3.1</version>
<systemPath>${basedir}/lib/selenium-server-standalone-3.3.1.jar</systemPath>
<scope>system</scope>
</dependency>
</code>```

Step:3 Convert your class to testing class

Step:4 Edit testing.xml.

Provide the class Name save it

```<code><suite name="Suite">
<test name="Test">
<classes>
<class name="test.java.CurseraTesting" />
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
</code>```

Step:5 Your class must contain Annotation @Test

If not, please add. See the below program,

```<code>package test.java;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class URL {
@Test
public void openUrl() throws InterruptedException{
System.setProperty("webdriver.chrome.driver","C:\\Users\\shivendra.pandey\\Downloads\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.co.in/?gfe_rd=cr&ei=EjlbWbnFPOSK8QeylZ7ACw");
Thread.sleep(1000);
driver.manage().window().maximize();
driver.findElement(By.xpath("//*[@title='Search']")).sendKeys("sanfoundry");
Thread.sleep(1000);
driver.close();
}
}
</code>```

Step:7 At the last verify all the setup’s TestNG, JDK

Step:8 Run your Project, click on Maven Test

Body:
I have done the same for Selenium Project. Hope will be useful

Please install TestNG and Maven in your system. Follow all Step 1 to Step 8, and Run your Project

Step:1 Right click on Java Project, click on Configure navigate to Convert to Maven Project.

Follow, figure 1.1 and figure 1.2

Step:2 Add all dependencies (required jars and plugins surefire-plugin for testing.xml)

How to add jars,

Way:1 Search in “https://mvnrepository.com/ “and add the repository

Way:2 Add jars from Local system (Chose either way1 or way2 or both if needed)
Create a folder (Say: lib) and place in the same directory

<dependency>
<groupId>selenium-server-standalone-3.3.1</groupId>
<artifactId>selenium-server-standalone-3.3.1</artifactId>
<version>3.3.1</version>
<systemPath>${basedir}/lib/selenium-server-standalone-3.3.1.jar</systemPath>
<scope>system</scope>
</dependency>

Step:3 Convert your class to testing class

Step:4 Edit testing.xml.

Provide the class Name save it

<suite name="Suite">
<test name="Test">
<classes>
<class name="test.java.CurseraTesting" />
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->

Step:5 Your class must contain Annotation @Test

If not, please add. See the below program,

package test.java;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class URL {
@Test
public void openUrl() throws InterruptedException{
System.setProperty("webdriver.chrome.driver","C:\\Users\\shivendra.pandey\\Downloads\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.co.in/?gfe_rd=cr&ei=EjlbWbnFPOSK8QeylZ7ACw");
Thread.sleep(1000);
driver.manage().window().maximize();
driver.findElement(By.xpath("//*[@title='Search']")).sendKeys("sanfoundry");
Thread.sleep(1000);
driver.close();
}
}

Step:7 At the last verify all the setup’s TestNG, JDK

Step:8 Run your Project, click on Maven Test


答:
Although answered and accepted (because I worked on it for some time) I'll submit my answer, since it handles some things the accepted answer doesn't.

Using

(?<=,)(?!\R)(?=(?:[^'"]*(['"])(?:\\.|(?!\1).)*\1)*[^'"]*$)

to explode your string should do it. It fi

It Matches the position after a comma, that isn't followed by a linefeed (the negative look-ahead (?!\R)), and is followed by

a sequence of zero or more characters that doesn't contain a quote

[^'"]*

a quote character that is captured

(['"])

either an escaped character

\\.

or a character that doesn't match the captured quote, tested with a negative look-ahead.

(?!\1).

1 to 3 are in a non capturing group that can repeat any number of times (including zero).

finally it's ended by a sequence of zero or more characters that doesn't contain a quote. $ (matches end of line) to make sure the whole string is matched.

[^'"]*$

See it here at regex101.

Note that this solution assumes opening/closing quotes are balanced in the string (unless in a quoted string).

The things handled that aren't in the accepted answer is

single quoted strings inside a double quoted string, and vice versa.

e.g. "It's a solution"

escaped characters in a string

e.g. 'It\'s a solution'

This may be completely unnecessary, but it was in my mindset when I started fiddling with this, so :)

Title: PHP Regex explode the string using ',' except ',' inside quote and double qoute


问:
Retrieving specific fields in a Solr query?
I am running a Solr instance on Jetty and when I search using the Solr admin panel, it returns the entire document. What should I do to get only specified fields from each Solr document returned by the search?</p>

答:
The best way is to run the query from Admin concole. When we run it, it also provides the actuall SQL query executed. Just copy the query and use it.

About the question: select specific fields from the table. In the admin console look for 'FL' text box. write the field names you want to retrieve, comma sapereted. Hit the 'Execute Query' button.
Top right side the SQL will be available.

Generated Query: ......select?fl=FIELDNAME&indent=on&q=:&wt=json

OwnerDisplayName: Mohit Ranka

LastEditorDisplayName: mdb


问:
How to index twitter data in elasticsearch?
I have fetched some data from twitter using streaming api. The file has json lines corresponding to the data. I would like to index this data in elasticsearch so that it could be searched easily.

I used following command to for index

```<code>cat file.json | jq -c '.[] | {"index": {"_index": "bookmarks", "_type": "bookmark", "_id": .id}}, .' | curl -XPOST localhost:9200/_bulk --data-binary @-
</code>```

but it results in following error.

```<code>jq: error (at <stdin>:127687): Cannot index string with string "id"
</code>```

答:
Instead of doing all that manually, you can simply use the Logstash Twitter Input.

I wrote a blog post about it.

Body:
I have fetched some data from twitter using streaming api. The file has json lines corresponding to the data. I would like to index this data in elasticsearch so that it could be searched easily.

I used following command to for index

cat file.json | jq -c '.[] | {"index": {"_index": "bookmarks", "_type": "bookmark", "_id": .id}}, .' | curl -XPOST localhost:9200/_bulk --data-binary @-

but it results in following error.

jq: error (at <stdin>:127687): Cannot index string with string "id"


问:
Save kafka broker ids (zookeeper-shell.sh result) to variable in bash script
I want to check if broker id is already in use, but I want to do this within a script.

```<code>./bin/zookeeper-shell.sh localhost:2181 <<< "ls /brokers/ids"
</code>```

Gives the result I want, but how would I save this to a variable for later use? What I'm interested in is the actual result of the "ls ..". The whole output includes some fodder:

```<code>Connecting to localhost:2181
Welcome to ZooKeeper!
JLine support is disabled

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
Node does not exist: /brokers/ids
</code>```

If I save the result to a variable I actually only get this fodder and the last line is only printed to the screen.

```<code>var=$(./opt/kafka/bin/zookeeper-shell.sh localhost:2181 <<< "ls /brokers/ids")
</code>```

And then echo $var gives

```<code>Connecting to localhost:2181 Welcome to ZooKeeper! JLine support is disabled WATCHER:: WatchedEvent state:SyncConnected type:None path:null
</code>```

How would I get the actual result of the request to a variable for later use?

答:
I ended up not using the script, if someone has a way to make use of the script still, that would be appreciated, but how I ended up doing it:

```<code>brokerIDs=$(echo dump | nc localhost 2181 | grep brokers/ids | sed 's/\/brokers\/ids\///')
</code>```

And then to check

```<code>if echo ${brokerIDs[@]} | grep -q -w $newNodeBrokerId;
then
    echo "Broker with that id already up";
else
    echo "Starting Kafka";
    # Start kafka service here
fi
</code>```

Body:
I want to check if broker id is already in use, but I want to do this within a script.

./bin/zookeeper-shell.sh localhost:2181 <<< "ls /brokers/ids"

Gives the result I want, but how would I save this to a variable for later use? What I'm interested in is the actual result of the "ls ..". The whole output includes some fodder:

Connecting to localhost:2181
Welcome to ZooKeeper!
JLine support is disabled

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
Node does not exist: /brokers/ids

If I save the result to a variable I actually only get this fodder and the last line is only printed to the screen.

var=$(./opt/kafka/bin/zookeeper-shell.sh localhost:2181 <<< "ls /brokers/ids")

And then echo $var gives

Connecting to localhost:2181 Welcome to ZooKeeper! JLine support is disabled WATCHER:: WatchedEvent state:SyncConnected type:None path:null

How would I get the actual result of the request to a variable for later use?

Body:
I ended up not using the script, if someone has a way to make use of the script still, that would be appreciated, but how I ended up doing it:

brokerIDs=$(echo dump | nc localhost 2181 | grep brokers/ids | sed 's/\/brokers\/ids\///')

And then to check

if echo ${brokerIDs[@]} | grep -q -w $newNodeBrokerId;
then
    echo "Broker with that id already up";
else
    echo "Starting Kafka";
    # Start kafka service here
fi


问:
How do I add corner radius and icons to buttons?
How do I make a similar view like the one below

So technically both will be a button, and Currently I'm using 2 buttons and I use stack view to group it together

The questions are

How do I add corner radius to stack view

How do I add icons and the line between them

答:
If you want to add corner Radius, Border Line, Image in UIButton you can use Our Custom Controls.
you have not add single line of code you can set from storyboard.
Just visit this link.
let me know if you have any doubts.

https://github.com/ZetrixWeb-Swift-iOS/ZWButton


问:
AWS Lex and Facebook - content type
I am working on AWS Lex, where I integrated it with FB Messenger successfully.

As I know so far, "responseCard" has only one "contentType"

```<code> "responseCard": {
   "version": integer-value,
   "contentType": "application/vnd.amazonaws.card.generic",
</code>```

In FB Messenger, there are 4 different content types: audio, file, image, video
https://developers.facebook.com/docs/messenger-platform/send-api-reference/contenttypes

My question: how many are content types of "responseCard"?

What I want to achieve is bot replies gif file which will be played automatically. Example: GIPHY bot

Thanks in advance

答:
The possible contentType value is only generic.
Based on this source http://docs.aws.amazon.com/lex/latest/dg/API_runtime_ResponseCard.html

The result in messenger will be just an static image and not GIF

Title: AWS Lex and Facebook - content type

Body:
I am working on AWS Lex, where I integrated it with FB Messenger successfully.

As I know so far, "responseCard" has only one "contentType"

 "responseCard": {
   "version": integer-value,
   "contentType": "application/vnd.amazonaws.card.generic",

In FB Messenger, there are 4 different content types: audio, file, image, video
https://developers.facebook.com/docs/messenger-platform/send-api-reference/contenttypes

My question: how many are content types of "responseCard"?

What I want to achieve is bot replies gif file which will be played automatically. Example: GIPHY bot

Thanks in advance


问:
How to use Docker in Windows ?
I have few questions about using Docker in Win 10 Pro.
I have already installed Docker, but I need help to create environment to develop using php.

If my idea is wrong, please, correct it.

I need a any linux OS as image base, on it I need php, nginx, git, composer, mysql etc. Do I will use this image to create containers for my projects.

Where I can download this image/configuration ?

Or I don't need linux OS ?

答:
You should take a look to official Docker documentation.

Here's some documentation that I think it can help you about this particular subject :

Create a base image

Best practices for writing Dockerfiles

Title: How to use Docker in Windows ?


问:
open on the same window form submit

var form = document.createElement("form");
form.setAttribute("hidden", "true");
form.setAttribute("method", "post");
form.setAttribute("target", "_blank");
form.setAttribute("action", "target.php");

document.body.appendChild(form); // Not entirely sure if this is necessary
form.submit();
return;```

I have the above code inside the success of an ajax succes. On success of saving data in the database I send back the said data to be print. I am using FPDF for printing and I have create a template in target.php and I am posting data into that page. So in order to send the data through post I create a form with target set as _blank expecting that when I save another set of data the and print the said data it will open on the previous tab. What is happening now is every time I save data it opens new tab.

What I expect is every time I save and print the data it will open to the previous tab that was opened on first print. Basically I dont want to open new tab every time I print. On first print open new tab then the rest will just load on that tab

答: I think you need to use _self  instead of _blank.</p>

Title: open on the same window form submit

Body:
var form = document.createElement("form");
form.setAttribute("hidden", "true");
form.setAttribute("method", "post");
form.setAttribute("target", "_blank");
form.setAttribute("action", "target.php");

document.body.appendChild(form); // Not entirely sure if this is necessary
form.submit();
return;

I have the above code inside the success of an ajax succes. On success of saving data in the database I send back the said data to be print. I am using FPDF for printing and I have create a template in target.php and I am posting data into that page. So in order to send the data through post I create a form with target set as _blank expecting that when I save another set of data the and print the said data it will open on the previous tab. What is happening now is every time I save data it opens new tab.

What I expect is every time I save and print the data it will open to the previous tab that was opened on first print. Basically I dont want to open new tab every time I print. On first print open new tab then the rest will just load on that tab


问:
Soapenv error when trying to send XML file to a webservice via cURL
I've a problem with soapenv, webservice and webdav.

I send pdf files & a xml file which list all pdf files of an enveloppe.
Then I call a webservice, to "launch" pdf files listed in the xml (in the webdav) by sending a technical xml with cURL.

here an example of the XML which are in webdav :

```<code><?xml version='1.0' encoding='ISO-8859-1' ?>
<actes:Acte xmlns:actes='http://' xmlns:insee='http://' xmlns:xsi='http://' xsi:schemaLocation='http://' actes:Date='2017-04-20' actes:NumeroInterne='lmc117M235' actes:CodeNatureActe='4'>
<actes:CodeMatiere1 actes:CodeMatiere="1"/>
<actes:CodeMatiere2 actes:CodeMatiere="4"/>
<actes:Objet>TRAVAUX DE REMPLACEMENT DES RESEAUX EAUX PLUVIALES ET RESEAUX EAUX USEES - RUE ALBERT CAMUS, RUE ANDRE MALRAUX ET CHEMIN BARRIEU - COMMUNE DE BLAGNAC - 17M024PA</actes:Objet>
<actes:ClassificationDateVersion>2003-06-25</actes:ClassificationDateVersion><actes:Document><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_1.pdf</actes:NomFichier></actes:Document>
<actes:Annexes actes:Nombre="22">
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_02.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_03.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_04.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_05.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_06.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_07.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_08.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_09.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_10.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_11.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_12.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_13.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_14.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_15.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_16.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_17.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_18.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_19.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_20.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_21.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_22.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_23.pdf</actes:NomFichier></actes:Annexe>
</actes:Annexes></actes:Acte>
</code>```

(I delete link data but they are right.)

Here an example of technical xml which are send by cURL :

```<code><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v20="http://dev.cdcfast.fr/connecteur/V20">
<soapenv:Header/><soapenv:Body>
<v20:traiterACTES>
<v20:typeTraitement>TELETRANSMISSION</v20:typeTraitement>
<v20:DNUtilisateur>E = assistant@toulouse.fr, CN = Assistant TOULOUSE, OU = 0002 999100046, OU = FAST UTILISATEUR, O = CU TOULOUSE, L = TOULOUSE, C = FR</v20:DNUtilisateur>
<v20:SIREN>999100046</v20:SIREN>
<v20:fichierACTES>031-999100046-20170420-lmc116M244-CC-1-1_0.xml</v20:fichierACTES>
</v20:traiterACTES>
</soapenv:Body>
</soapenv:Envelope>
</code>```

Here my cURL function in PHP :

```<code>function cdcDemandeEnvoi($cheminFichierXML)
    {
        $ch = curl_init();

        $url_decoupe = explode("/", $cheminFichierXML);
        $fichierXML = end($url_decoupe);
        $nomFichierXML = pathinfo($fichierXML, PATHINFO_FILENAME);

        $urlPut = "https://recette.efast.fr/ascl/services/FASTConnecteur/".$nomFichierXML;
        $file = '@'.$cheminFichierXML;

        //Proxy config
        $proxyAdresse = "http://";
        $proxyIdentification = "";

        $header = array("Content-Type:text/plain; charset=UTF-8");
        $curl_post_data = array("file" => $file);

        curl_setopt($ch, CURLOPT_URL, $urlPut);
        curl_setopt($ch, CURLOPT_PROXY, $proxyAdresse);
        curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyIdentification);
        curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_NTLM);
        curl_setopt($ch, CURLOPT_USERAGENT, "curl/7.29.0");
        curl_setopt($ch, CURLOPT_SSLCERT, "/var/www/html/teletransmission/certificats/test/test.crt");
        curl_setopt($ch, CURLOPT_SSLKEY, "/var/www/html/teletransmission/certificats/test/test.privkey");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curl_post_data);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);

        $output = curl_exec($ch);
        $error = curl_error($ch);
        $http_code = curl_getinfo($ch ,CURLINFO_HTTP_CODE);

        curl_close($ch);

    }
</code>```

But when I send the cURL request I've got his message error :

```<code>soapenv:Receivercom.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '-' (code 45) in prolog; expected '<'
 at [row,col {unknown-source}]: [1,1]org.apache.axis2.AxisFault: com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '-' (code 45) in prolog; expected '<'
 at [row,col {unknown-source}]: [1,1]
    at org.apache.axis2.AxisFault.makeFault(AxisFault.java:417)
    at org.apache.axis2.transport.http.HTTPTransportUtils.processHTTPPostRequest(HTTPTransportUtils.java:283)
    at org.apache.axis2.transport.http.AxisServlet.doPost(AxisServlet.java:120)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:102)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:567)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:190)
    at org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:291)
    at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:769)
    at org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:698)
    at org.apache.jk.common.ChannelSocket$SocketConnection.runIt(ChannelSocket.java:891)
    at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:690)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.axiom.om.OMException: com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '-' (code 45) in prolog; expected '<'
 at [row,col {unknown-source}]: [1,1]
    at org.apache.axiom.om.impl.builder.StAXOMBuilder.next(StAXOMBuilder.java:239)
    at org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder.getSOAPEnvelope(StAXSOAPModelBuilder.java:161)
    at org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder.<init>(StAXSOAPModelBuilder.java:110)
    at org.apache.axis2.builder.BuilderUtil.getSOAPBuilder(BuilderUtil.java:609)
    at org.apache.axis2.transport.TransportUtils.createDocumentElement(TransportUtils.java:178)
    at org.apache.axis2.transport.TransportUtils.createSOAPMessage(TransportUtils.java:111)
    at org.apache.axis2.transport.http.HTTPTransportUtils.processHTTPPostRequest(HTTPTransportUtils.java:270)
    ... 22 more
Caused by: com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '-' (code 45) in prolog; expected '<'
 at [row,col {unknown-source}]: [1,1]
    at com.ctc.wstx.sr.StreamScanner.throwUnexpectedChar(StreamScanner.java:639)
    at com.ctc.wstx.sr.BasicStreamReader.nextFromProlog(BasicStreamReader.java:2017)
    at com.ctc.wstx.sr.BasicStreamReader.next(BasicStreamReader.java:1102)
    at org.apache.axiom.om.impl.builder.StAXOMBuilder.next(StAXOMBuilder.java:153)
    ... 28 more
</code>```

So I looked in the xml which are in webdav & I see that when I put the xml it added these lines before my xml content :

```<code>------------------------------1b9a7ef8734e
Content-Disposition: form-data; name="file"; filename="/mnt/test_dematmarche/depot/ACTES_17M235-PA-444/enveloppe1/031-999100046-20170420-lmc117M235-CC-1-1_0.xml"
Content-Type: application/octet-stream
</code>```

& this after my xml content :

```<code>------------------------------1b9a7ef8734e--
</code>```

I don't understand where is my problem. Why it added these lines ?
Anyone can help me please ?
Thanks

--- EDIT ---

Here my function which upload files into webdav :

```<code>function cdcDepotXML($fileActe)
    {

        $ch = curl_init();

        $url_decoupe = explode("/", $fileActe);
        $nomActe = end($url_decoupe);

        $urlPut = "https://url.fr/to/webdav/999100046/lmc1/".$nomActe;
        $file = '@'.$fileActe;

        $proxyAdresse = "http://proxyurl:80";
        $proxyIdentification = "proxyuser:proxypassword";

        $header = array("Content-Type:multipart/form-data");
        $curl_post_data = array("file" => $file);

        curl_setopt($ch, CURLOPT_URL, $urlPut);
        curl_setopt($ch, CURLOPT_PROXY, $proxyAdresse);
        curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyIdentification);
        curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_NTLM);
        curl_setopt($ch, CURLOPT_USERAGENT, "curl/7.29.0");
        curl_setopt($ch, CURLOPT_SSLCERT, "/var/www/html/teletransmission/certificats/test/test.crt");
        curl_setopt($ch, CURLOPT_SSLKEY, "/var/www/html/teletransmission/certificats/test/test.privkey");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curl_post_data);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);

        $output = curl_exec($ch);
        $error = curl_error($ch);
        $http_code = curl_getinfo($ch ,CURLINFO_HTTP_CODE);

        curl_close($ch);

}
</code>```

答:
Finally, I found the solution !!

Here's the curl function:

```<code>function cdcDepotXML($fileActe)
{

    $ch = curl_init();

    $url_decoupe = explode("/", $fileActe);
    $nomActe = end($url_decoupe);

    $urlPut = "https://url.fr/to/webdav/999100046/lmc1/".$nomActe;
    $file = '@'.$fileActe;

    $proxyAdresse = "http://proxyurl:80";
    $proxyIdentification = "proxyuser:proxypassword";

    $header = array("Content-Type:multipart/form-data");

    $fp = fopen($fileActe, 'r');

    curl_setopt($ch, CURLOPT_URL, $urlPut);
    curl_setopt($ch, CURLOPT_PROXY, $proxyAdresse);
    curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyIdentification);
    curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_NTLM);
    curl_setopt($ch, CURLOPT_USERAGENT, "curl/7.29.0");
    curl_setopt($ch, CURLOPT_SSLCERT, "/var/www/html/teletransmission/certificats/test/test.crt");
    curl_setopt($ch, CURLOPT_SSLKEY, "/var/www/html/teletransmission/certificats/test/test.privkey");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch,CURLOPT_HTTPGET, true);
    curl_setopt($ch, CURLOPT_UPLOAD, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_INFILE, $fp);
    curl_setopt($ch, CURLOPT_INFILESIZE, filesize($fileActe));

    $output = curl_exec($ch);
    $error = curl_error($ch);
    $http_code = curl_getinfo($ch ,CURLINFO_HTTP_CODE);

    print "erreur : " .$error ." " .$http_code;

    curl_close($ch);
}
</code>```

Body:
I've a problem with soapenv, webservice and webdav.

I send pdf files & a xml file which list all pdf files of an enveloppe.
Then I call a webservice, to "launch" pdf files listed in the xml (in the webdav) by sending a technical xml with cURL.

here an example of the XML which are in webdav :

<?xml version='1.0' encoding='ISO-8859-1' ?>
<actes:Acte xmlns:actes='http://' xmlns:insee='http://' xmlns:xsi='http://' xsi:schemaLocation='http://' actes:Date='2017-04-20' actes:NumeroInterne='lmc117M235' actes:CodeNatureActe='4'>
<actes:CodeMatiere1 actes:CodeMatiere="1"/>
<actes:CodeMatiere2 actes:CodeMatiere="4"/>
<actes:Objet>TRAVAUX DE REMPLACEMENT DES RESEAUX EAUX PLUVIALES ET RESEAUX EAUX USEES - RUE ALBERT CAMUS, RUE ANDRE MALRAUX ET CHEMIN BARRIEU - COMMUNE DE BLAGNAC - 17M024PA</actes:Objet>
<actes:ClassificationDateVersion>2003-06-25</actes:ClassificationDateVersion><actes:Document><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_1.pdf</actes:NomFichier></actes:Document>
<actes:Annexes actes:Nombre="22">
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_02.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_03.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_04.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_05.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_06.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_07.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_08.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_09.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_10.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_11.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_12.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_13.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_14.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_15.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_16.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_17.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_18.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_19.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_20.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_21.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_22.pdf</actes:NomFichier></actes:Annexe>
<actes:Annexe><actes:NomFichier>031-999100046-20170420-lmc117M235-CC-1-1_23.pdf</actes:NomFichier></actes:Annexe>
</actes:Annexes></actes:Acte>

(I delete link data but they are right.)

Here an example of technical xml which are send by cURL :

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v20="http://dev.cdcfast.fr/connecteur/V20">
<soapenv:Header/><soapenv:Body>
<v20:traiterACTES>
<v20:typeTraitement>TELETRANSMISSION</v20:typeTraitement>
<v20:DNUtilisateur>E = assistant@toulouse.fr, CN = Assistant TOULOUSE, OU = 0002 999100046, OU = FAST UTILISATEUR, O = CU TOULOUSE, L = TOULOUSE, C = FR</v20:DNUtilisateur>
<v20:SIREN>999100046</v20:SIREN>
<v20:fichierACTES>031-999100046-20170420-lmc116M244-CC-1-1_0.xml</v20:fichierACTES>
</v20:traiterACTES>
</soapenv:Body>
</soapenv:Envelope>

Here my cURL function in PHP :

function cdcDemandeEnvoi($cheminFichierXML)
    {
        $ch = curl_init();

        $url_decoupe = explode("/", $cheminFichierXML);
        $fichierXML = end($url_decoupe);
        $nomFichierXML = pathinfo($fichierXML, PATHINFO_FILENAME);

        $urlPut = "https://recette.efast.fr/ascl/services/FASTConnecteur/".$nomFichierXML;
        $file = '@'.$cheminFichierXML;

        //Proxy config
        $proxyAdresse = "http://";
        $proxyIdentification = "";

        $header = array("Content-Type:text/plain; charset=UTF-8");
        $curl_post_data = array("file" => $file);

        curl_setopt($ch, CURLOPT_URL, $urlPut);
        curl_setopt($ch, CURLOPT_PROXY, $proxyAdresse);
        curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyIdentification);
        curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_NTLM);
        curl_setopt($ch, CURLOPT_USERAGENT, "curl/7.29.0");
        curl_setopt($ch, CURLOPT_SSLCERT, "/var/www/html/teletransmission/certificats/test/test.crt");
        curl_setopt($ch, CURLOPT_SSLKEY, "/var/www/html/teletransmission/certificats/test/test.privkey");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curl_post_data);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);

        $output = curl_exec($ch);
        $error = curl_error($ch);
        $http_code = curl_getinfo($ch ,CURLINFO_HTTP_CODE);

        curl_close($ch);

    }

But when I send the cURL request I've got his message error :

soapenv:Receivercom.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '-' (code 45) in prolog; expected '<'
 at [row,col {unknown-source}]: [1,1]org.apache.axis2.AxisFault: com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '-' (code 45) in prolog; expected '<'
 at [row,col {unknown-source}]: [1,1]
    at org.apache.axis2.AxisFault.makeFault(AxisFault.java:417)
    at org.apache.axis2.transport.http.HTTPTransportUtils.processHTTPPostRequest(HTTPTransportUtils.java:283)
    at org.apache.axis2.transport.http.AxisServlet.doPost(AxisServlet.java:120)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:102)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:567)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:190)
    at org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:291)
    at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:769)
    at org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:698)
    at org.apache.jk.common.ChannelSocket$SocketConnection.runIt(ChannelSocket.java:891)
    at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:690)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.axiom.om.OMException: com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '-' (code 45) in prolog; expected '<'
 at [row,col {unknown-source}]: [1,1]
    at org.apache.axiom.om.impl.builder.StAXOMBuilder.next(StAXOMBuilder.java:239)
    at org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder.getSOAPEnvelope(StAXSOAPModelBuilder.java:161)
    at org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder.<init>(StAXSOAPModelBuilder.java:110)
    at org.apache.axis2.builder.BuilderUtil.getSOAPBuilder(BuilderUtil.java:609)
    at org.apache.axis2.transport.TransportUtils.createDocumentElement(TransportUtils.java:178)
    at org.apache.axis2.transport.TransportUtils.createSOAPMessage(TransportUtils.java:111)
    at org.apache.axis2.transport.http.HTTPTransportUtils.processHTTPPostRequest(HTTPTransportUtils.java:270)
    ... 22 more
Caused by: com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '-' (code 45) in prolog; expected '<'
 at [row,col {unknown-source}]: [1,1]
    at com.ctc.wstx.sr.StreamScanner.throwUnexpectedChar(StreamScanner.java:639)
    at com.ctc.wstx.sr.BasicStreamReader.nextFromProlog(BasicStreamReader.java:2017)
    at com.ctc.wstx.sr.BasicStreamReader.next(BasicStreamReader.java:1102)
    at org.apache.axiom.om.impl.builder.StAXOMBuilder.next(StAXOMBuilder.java:153)
    ... 28 more

So I looked in the xml which are in webdav & I see that when I put the xml it added these lines before my xml content :

------------------------------1b9a7ef8734e
Content-Disposition: form-data; name="file"; filename="/mnt/test_dematmarche/depot/ACTES_17M235-PA-444/enveloppe1/031-999100046-20170420-lmc117M235-CC-1-1_0.xml"
Content-Type: application/octet-stream

& this after my xml content :

------------------------------1b9a7ef8734e--

I don't understand where is my problem. Why it added these lines ?
Anyone can help me please ?
Thanks

--- EDIT ---

Here my function which upload files into webdav :

function cdcDepotXML($fileActe)
    {

        $ch = curl_init();

        $url_decoupe = explode("/", $fileActe);
        $nomActe = end($url_decoupe);

        $urlPut = "https://url.fr/to/webdav/999100046/lmc1/".$nomActe;
        $file = '@'.$fileActe;

        $proxyAdresse = "http://proxyurl:80";
        $proxyIdentification = "proxyuser:proxypassword";

        $header = array("Content-Type:multipart/form-data");
        $curl_post_data = array("file" => $file);

        curl_setopt($ch, CURLOPT_URL, $urlPut);
        curl_setopt($ch, CURLOPT_PROXY, $proxyAdresse);
        curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyIdentification);
        curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_NTLM);
        curl_setopt($ch, CURLOPT_USERAGENT, "curl/7.29.0");
        curl_setopt($ch, CURLOPT_SSLCERT, "/var/www/html/teletransmission/certificats/test/test.crt");
        curl_setopt($ch, CURLOPT_SSLKEY, "/var/www/html/teletransmission/certificats/test/test.privkey");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curl_post_data);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);

        $output = curl_exec($ch);
        $error = curl_error($ch);
        $http_code = curl_getinfo($ch ,CURLINFO_HTTP_CODE);

        curl_close($ch);

}

Body:
Finally, I found the solution !!

Here's the curl function:

function cdcDepotXML($fileActe)
{

    $ch = curl_init();

    $url_decoupe = explode("/", $fileActe);
    $nomActe = end($url_decoupe);

    $urlPut = "https://url.fr/to/webdav/999100046/lmc1/".$nomActe;
    $file = '@'.$fileActe;

    $proxyAdresse = "http://proxyurl:80";
    $proxyIdentification = "proxyuser:proxypassword";

    $header = array("Content-Type:multipart/form-data");

    $fp = fopen($fileActe, 'r');

    curl_setopt($ch, CURLOPT_URL, $urlPut);
    curl_setopt($ch, CURLOPT_PROXY, $proxyAdresse);
    curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyIdentification);
    curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_NTLM);
    curl_setopt($ch, CURLOPT_USERAGENT, "curl/7.29.0");
    curl_setopt($ch, CURLOPT_SSLCERT, "/var/www/html/teletransmission/certificats/test/test.crt");
    curl_setopt($ch, CURLOPT_SSLKEY, "/var/www/html/teletransmission/certificats/test/test.privkey");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch,CURLOPT_HTTPGET, true);
    curl_setopt($ch, CURLOPT_UPLOAD, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_INFILE, $fp);
    curl_setopt($ch, CURLOPT_INFILESIZE, filesize($fileActe));

    $output = curl_exec($ch);
    $error = curl_error($ch);
    $http_code = curl_getinfo($ch ,CURLINFO_HTTP_CODE);

    print "erreur : " .$error ." " .$http_code;

    curl_close($ch);
}


问:
How to automate image slicing process in Linux?
Input image

I need as following,

output explanation
Output scheme

Following steps needed to be done.

Process the input image

Split the image into more images based on the gap between the horizontal text line.

then store that image in consecutive numbering.

Again process each image and split into more images based on space between the each word.

then store it in each image.

If possible i need individual characters image from the input image, i dont need text extraction. Since all Tamil OCR are not deciphering properly.

答:
Here is an approach - it is probably not perfect but you can maybe tweak it if you have more images available to test with.

The basic idea is to fatten up the individual letters so they touch each other but hopefully without bridging across to adjacent words. Then do a "Connected Components Analysis" to find the individual words of your original text as blobs.

Here is the first step - the fattening of the letters with ImageMagick

```<code>convert text.png -threshold 50% -morphology erode diamond:4 step1.png
</code>```

I am using morphology techniques above, but you could equally try blurring and thresholding techniques instead.

Now find the "blobs":

```<code>convert step1.png \
    -define connected-components:verbose=true        \
    -define connected-components:area-threshold=100  \
    -connected-components 8 -auto-level output.png
</code>```

Sample Output

```<code>Objects (id: bounding-box centroid area mean-color):
  0: 1086x188+0+0 556.4,83.0 155156 gray(255)
  7: 364x65+128+118 281.8,142.6 9206 gray(0)
  6: 212x34+817+115 919.3,131.1 4691 gray(0)
  4: 231x33+73+76 184.4,92.3 4645 gray(0)
  2: 181x42+494+8 578.4,27.7 4399 gray(0)
  9: 209x31+608+118 713.1,132.9 3892 gray(0)
  17: 148x34+826+148 903.0,165.1 2932 gray(0)
  22: 132x34+20+153 84.1,169.1 2453 gray(0)
  20: 126x27+384+151 443.4,165.9 2404 gray(0)
  1: 91x42+396+8 440.3,29.0 2390 gray(0)
  18: 117x34+708+149 764.2,165.5 2350 gray(0)
  21: 104x33+509+151 560.3,167.6 2245 gray(0)
  23: 112x27+271+158 325.8,169.3 2159 gray(0)
  8: 100x33+507+118 558.2,134.6 1982 gray(0)
  19: 91x33+615+150 659.4,166.2 1888 gray(0)
  10: 55x25+73+121 100.0,134.4 920 gray(0)
  3: 28x29+361+12 373.8,27.5 456 gray(0)
</code>```

Each line above corresponds to one blob, or hopefully one word of your original text. The first line is a header line which tells you what the fields are. The second field on each subsequent line is the WIDTH X HEIGHT and X Y OFFSET (from top left corner of the image) of a blob followed by its centroid and its area and the last field is the mean colour.

You don't need this next step, as I guess the lines of text describing each word are what you need, but by way of illustration, I can draw in the boxes on the original image:

```<code>convert "text.png" -stroke red -fill none -strokewidth 1 \
   -draw "rectangle 128,118 492,183"  \
   -draw "rectangle 817,115 1029,149" \
   -draw "rectangle 73,76 304,109"    \
   -draw "rectangle 494,8 675,50"     \
   -draw "rectangle 608,118 817,149"  \
   -draw "rectangle 826,148 974,182"  \
   -draw "rectangle 20,153 152,187"   \
   -draw "rectangle 384,151 510,178"  \
   -draw "rectangle 396,8 487,50"     \
   -draw "rectangle 708,149 825,183"  \
   -draw "rectangle 509,151 613,184"  \
   -draw "rectangle 271,158 383,185"  \
   -draw "rectangle 507,118 607,151"  \
   -draw "rectangle 615,150 706,183"  \
   -draw "rectangle 73,121 128,146"   \
   -draw "rectangle 361,12 389,41" result.png
</code>```

You don't actually need to create the intermediate images like I did above, you can do it all in one go as follows, but I wanted to explain my technique:

```<code>convert text.png -threshold 50% -morphology erode diamond:4 \
    -define connected-components:verbose=true               \
    -define connected-components:area-threshold=100         \
    -connected-components 8 -auto-level output.png
</code>```

The output image (output.png) is actually "labelled" by which I mean that all the pixels of each identified blob are coloured in a successively lighter shade of white.

Note that there are other structuring element shapes and sizes that may give better results with your images, e.g.:

```<code>convert text.png -threshold 50% -morphology erode disk:3 result.png
</code>```

See Anthony Thyssen's excellent introduction to morphology here.

Regarding your further question of splitting the image into its constituent parts, each containing a word, you could do the following...

Pipe the output of the previous convert command into awk to find all the lines that contain the word gray and print out the geometry of the box in field 2.

```<code>convert ... as above ... | awk '/gray/{print $2}'
</code>```

Sample Output

```<code>1086x188+0+0
364x65+128+118
212x34+817+115
231x33+73+76
181x42+494+8
209x31+608+118
148x34+826+148
132x34+20+153
126x27+384+151
91x42+396+8
117x34+708+149
104x33+509+151
112x27+271+158
100x33+507+118
91x33+615+150
55x25+73+121
28x29+361+12
</code>```

Now split that on the plus sign to separate the X and Y:

```<code>convert ... | awk '/gray/{split($2,a,"+");print a[1],a[2],a[3]}'
</code>```

Sample Output

```<code>1086x188 0 0
364x65 128 118
212x34 817 115
231x33 73 76
181x42 494 8
209x31 608 118
148x34 826 148
132x34 20 153
126x27 384 151
91x42 396 8
117x34 708 149
104x33 509 151
112x27 271 158
100x33 507 118
91x33 615 150
55x25 73 121
28x29 361 12
</code>```

Now sort by Y then X so that words come out in line order (Y counts down from the top) then word order (X counts across from the left):

```<code>convert step1.png \
    -define connected-components:verbose=true        \
    -define connected-components:area-threshold=100  \
    -connected-components 8 -auto-level output.png
</code>```0

Sample Output

```<code>convert step1.png \
    -define connected-components:verbose=true        \
    -define connected-components:area-threshold=100  \
    -connected-components 8 -auto-level output.png
</code>```1

Now, pass the geometries into a loop and read them into bash variables, then crop the original image and name the individual words with a simple index (i).

```<code>convert step1.png \
    -define connected-components:verbose=true        \
    -define connected-components:area-threshold=100  \
    -connected-components 8 -auto-level output.png
</code>```2

Note that if your text is even slightly rotated, say 1 degree anticlockwise, the words on the right end of any given line will have a smaller Y coordinate than those on the left and so may come out before them in the image order when sorted. As such, you may need to round the Y coordinate to the nearest 10, or 20 or 40 in awk so that if one word is 168 pixels from the top and another is 169 pixels from the top, they both get sorted as though they were say 170 pixels from the top and come out in the same line.

Body:
Here is an approach - it is probably not perfect but you can maybe tweak it if you have more images available to test with.

The basic idea is to fatten up the individual letters so they touch each other but hopefully without bridging across to adjacent words. Then do a "Connected Components Analysis" to find the individual words of your original text as blobs.

Here is the first step - the fattening of the letters with ImageMagick

convert text.png -threshold 50% -morphology erode diamond:4 step1.png

I am using morphology techniques above, but you could equally try blurring and thresholding techniques instead.

Now find the "blobs":

convert step1.png \
    -define connected-components:verbose=true        \
    -define connected-components:area-threshold=100  \
    -connected-components 8 -auto-level output.png

Sample Output

Objects (id: bounding-box centroid area mean-color):
  0: 1086x188+0+0 556.4,83.0 155156 gray(255)
  7: 364x65+128+118 281.8,142.6 9206 gray(0)
  6: 212x34+817+115 919.3,131.1 4691 gray(0)
  4: 231x33+73+76 184.4,92.3 4645 gray(0)
  2: 181x42+494+8 578.4,27.7 4399 gray(0)
  9: 209x31+608+118 713.1,132.9 3892 gray(0)
  17: 148x34+826+148 903.0,165.1 2932 gray(0)
  22: 132x34+20+153 84.1,169.1 2453 gray(0)
  20: 126x27+384+151 443.4,165.9 2404 gray(0)
  1: 91x42+396+8 440.3,29.0 2390 gray(0)
  18: 117x34+708+149 764.2,165.5 2350 gray(0)
  21: 104x33+509+151 560.3,167.6 2245 gray(0)
  23: 112x27+271+158 325.8,169.3 2159 gray(0)
  8: 100x33+507+118 558.2,134.6 1982 gray(0)
  19: 91x33+615+150 659.4,166.2 1888 gray(0)
  10: 55x25+73+121 100.0,134.4 920 gray(0)
  3: 28x29+361+12 373.8,27.5 456 gray(0)

Each line above corresponds to one blob, or hopefully one word of your original text. The first line is a header line which tells you what the fields are. The second field on each subsequent line is the WIDTH X HEIGHT and X Y OFFSET (from top left corner of the image) of a blob followed by its centroid and its area and the last field is the mean colour.

You don't need this next step, as I guess the lines of text describing each word are what you need, but by way of illustration, I can draw in the boxes on the original image:

convert "text.png" -stroke red -fill none -strokewidth 1 \
   -draw "rectangle 128,118 492,183"  \
   -draw "rectangle 817,115 1029,149" \
   -draw "rectangle 73,76 304,109"    \
   -draw "rectangle 494,8 675,50"     \
   -draw "rectangle 608,118 817,149"  \
   -draw "rectangle 826,148 974,182"  \
   -draw "rectangle 20,153 152,187"   \
   -draw "rectangle 384,151 510,178"  \
   -draw "rectangle 396,8 487,50"     \
   -draw "rectangle 708,149 825,183"  \
   -draw "rectangle 509,151 613,184"  \
   -draw "rectangle 271,158 383,185"  \
   -draw "rectangle 507,118 607,151"  \
   -draw "rectangle 615,150 706,183"  \
   -draw "rectangle 73,121 128,146"   \
   -draw "rectangle 361,12 389,41" result.png

You don't actually need to create the intermediate images like I did above, you can do it all in one go as follows, but I wanted to explain my technique:

convert text.png -threshold 50% -morphology erode diamond:4 \
    -define connected-components:verbose=true               \
    -define connected-components:area-threshold=100         \
    -connected-components 8 -auto-level output.png

The output image (output.png) is actually "labelled" by which I mean that all the pixels of each identified blob are coloured in a successively lighter shade of white.

Note that there are other structuring element shapes and sizes that may give better results with your images, e.g.:

convert text.png -threshold 50% -morphology erode disk:3 result.png

See Anthony Thyssen's excellent introduction to morphology here.

Regarding your further question of splitting the image into its constituent parts, each containing a word, you could do the following...

Pipe the output of the previous convert command into awk to find all the lines that contain the word gray and print out the geometry of the box in field 2.

convert ... as above ... | awk '/gray/{print $2}'

Sample Output

1086x188+0+0
364x65+128+118
212x34+817+115
231x33+73+76
181x42+494+8
209x31+608+118
148x34+826+148
132x34+20+153
126x27+384+151
91x42+396+8
117x34+708+149
104x33+509+151
112x27+271+158
100x33+507+118
91x33+615+150
55x25+73+121
28x29+361+12

Now split that on the plus sign to separate the X and Y:

convert ... | awk '/gray/{split($2,a,"+");print a[1],a[2],a[3]}'

Sample Output

1086x188 0 0
364x65 128 118
212x34 817 115
231x33 73 76
181x42 494 8
209x31 608 118
148x34 826 148
132x34 20 153
126x27 384 151
91x42 396 8
117x34 708 149
104x33 509 151
112x27 271 158
100x33 507 118
91x33 615 150
55x25 73 121
28x29 361 12

Now sort by Y then X so that words come out in line order (Y counts down from the top) then word order (X counts across from the left):

convert ... | awk '/gray/{split($2,a,"+");print a[1],a[2],a[3]}' |
    sort -n -k3 -k2

Sample Output

1086x188,0,0
91x42,396,8
181x42,494,8
28x29,361,12
231x33,73,76
212x34,817,115
364x65,128,118
100x33,507,118
209x31,608,118
55x25,73,121
148x34,826,148
117x34,708,149
91x33,615,150
126x27,384,151
104x33,509,151
132x34,20,153
112x27,271,158

Now, pass the geometries into a loop and read them into bash variables, then crop the original image and name the individual words with a simple index (i).

convert ... | awk '/gray/{ split($2,a,"+");print a[1],a[2],a[3] }' |
   sort -n -k3 -k2 |
 { i=0;
   while read g x y; do
          convert text.png -crop ${g}+${x}+${y} word-${i}.png
          ((i+=1))
   done }

Note that if your text is even slightly rotated, say 1 degree anticlockwise, the words on the right end of any given line will have a smaller Y coordinate than those on the left and so may come out before them in the image order when sorted. As such, you may need to round the Y coordinate to the nearest 10, or 20 or 40 in awk so that if one word is 168 pixels from the top and another is 169 pixels from the top, they both get sorted as though they were say 170 pixels from the top and come out in the same line.


问:
How to omit module prefix?
I am very new to python, I have a simple question.

For example

If I do

```<code>import A
</code>```

then I can use A.b().
I am wondering, how to omit A in A.b()?

答:
Since A is a module, you can do:

```<code>from A import b

b()
</code>```

Title: How to omit module prefix?

Body:
I am very new to python, I have a simple question.

For example

If I do

import A

then I can use A.b().
I am wondering, how to omit A in A.b()?

Body:
Since A is a module, you can do:

from A import b

b()


问:
Center align the absolute div without using left:0, right:0, width:100%
How to center a absolute div without using left, right and width:100%? And the width should be auto

```<code>div{
    position: absolute;
    width: 100%;
    margin: 0 auto;
    display: table;
}
</code>```

答:
You can use margin to position your div element. For example:

```<code>margin-left: 50%;
</code>```

or viceversa.

EDIT: Here is a snippet to explain the above text. I hope it helps.

body {
margin: 0;
padding: 0;
background-color: #000;
}

h1 {
color: #fff;
margin-top: 100px;
margin-left: 25%;
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><html>
<body>
<h1>This is an example</h1>
</body>
</html></code>```

Body:
How to center a absolute div without using left, right and width:100%? And the width should be auto

div{
    position: absolute;
    width: 100%;
    margin: 0 auto;
    display: table;
}

Body:
You can use margin to position your div element. For example:

margin-left: 50%;

or viceversa.

EDIT: Here is a snippet to explain the above text. I hope it helps.

body {
margin: 0;
padding: 0;
background-color: #000;
}

h1 {
color: #fff;
margin-top: 100px;
margin-left: 25%;
}

<html>
<body>
<h1>This is an example</h1>
</body>
</html>


问:
Close a batch window only by typing "exit"?? disable the close "X" button on the top right of batch window
I run a batch file which carries out some task for hours. BY mistake users happen to close the terminal window in which the batch file is executing by pressing the top right "X" close button.

I want to disable it and want to close the batch file window only by typing "exit" inside the window..is this possible?

答:
This can't be done using only one batch file. But there might be a really dirty way for this. You will need three batch files:

EDIT: Here is the solution. Put these three files into the same folder.

controller.bat:

```<code>@ECHO OFF
TITLE CONTROLLER
IF NOT EXIST hiddenrunner.vbs (
    echo CreateObject^("WScript.Shell"^).Run Wscript.Arguments^(0^), 0, False>hiddenrunner.vbs
)
tasklist /V|findstr "MONITOR"
IF %ERRORLEVEL%==1 hiddenrunner.vbs monitor.bat
:INPUTLOOP
CLS
SET /P input=Type 'exit' to exit:
IF /i NOT %input%==exit GOTO INPUTLOOP
ECHO ok, exiting
taskkill /F /FI "WindowTitle eq Administrator:  WORK" /T
taskkill /F /FI "WindowTitle eq Administrator:  MONITOR" /T
ping localhost -n 2
del /Q hiddenrunner.vbs
</code>```

monitor.bat:

```<code>@ECHO OFF
SETLOCAL EnableDelayedExpansion
TITLE MONITOR
hiddenrunner.vbs work.bat
:LOOP
tasklist /V|findstr "CONTROLLER"
IF !errorlevel!==1 START controller.bat
ping localhost -n 2
GOTO LOOP
</code>```

work.bat:

```<code>@ECHO OFF
title WORK
ping -t localhost
</code>```

In your case, the work-file will be your original batch you want to execute. You'll have to adjust the window titles (like TITLE CONTROLLER and WindowTitle eq Administrator:  WORK) to fit your setup. However, this works for me. If the user closes the input console, it is being restarted after one second. All processes are being terminated after the user inputs exit in the input console.

Body:
This can't be done using only one batch file. But there might be a really dirty way for this. You will need three batch files:

EDIT: Here is the solution. Put these three files into the same folder.

controller.bat:

@ECHO OFF
TITLE CONTROLLER
IF NOT EXIST hiddenrunner.vbs (
    echo CreateObject^("WScript.Shell"^).Run Wscript.Arguments^(0^), 0, False>hiddenrunner.vbs
)
tasklist /V|findstr "MONITOR"
IF %ERRORLEVEL%==1 hiddenrunner.vbs monitor.bat
:INPUTLOOP
CLS
SET /P input=Type 'exit' to exit:
IF /i NOT %input%==exit GOTO INPUTLOOP
ECHO ok, exiting
taskkill /F /FI "WindowTitle eq Administrator:  WORK" /T
taskkill /F /FI "WindowTitle eq Administrator:  MONITOR" /T
ping localhost -n 2
del /Q hiddenrunner.vbs

monitor.bat:

@ECHO OFF
SETLOCAL EnableDelayedExpansion
TITLE MONITOR
hiddenrunner.vbs work.bat
:LOOP
tasklist /V|findstr "CONTROLLER"
IF !errorlevel!==1 START controller.bat
ping localhost -n 2
GOTO LOOP

work.bat:

@ECHO OFF
title WORK
ping -t localhost

In your case, the work-file will be your original batch you want to execute. You'll have to adjust the window titles (like TITLE CONTROLLER and WindowTitle eq Administrator:  WORK) to fit your setup. However, this works for me. If the user closes the input console, it is being restarted after one second. All processes are being terminated after the user inputs exit in the input console.


问:
RubyMine: Rails server launcher wasn't found in the project
After importing an existing project into RubyMine, I encountered "Rails server launcher wasn't found in the project" when I Run/Edit Configuration.

I've checked this Cannot start the debugger in Rubymine. Rails server launcher wasn't found in project, but deleting the .idea directory and re-open is not helpful.

I can rails server in the Terminal successfully and the rails server runs. So I guess it's likely a RubyMine related thing.

Thanks,
Allen

答:
I upgraded from RubyMine to IntelliJ Ultimate IDE 2017.1.5 and I encountered the same error "Run Configuration Error: Rails server launcher wasn't found in project". I was using Ruby v2.4.0 with Rails 5.0.2.

It took me an entire day to solve the issue, so I've written a detailed explanation with screenshots to guide others.

It occurred when I tried to Run or Debug my Rails server from within IntelliJ.
Other solutions also didn't fix the error. I tried deleting my .idea/ folder, trying different Ruby or Rails versions (i.e. Ruby v2.3.0 or latest Rails 5.1.2), and even starting a brand new project from scratch. But I could run the Rails server fine using Terminal (outside the IDE) with rails s.

SOLUTION approach:

First I opened Run > Edit Configurations but "Choose SDK from module" didn't have any Project Modules available to select from the drop-down, so I had to create one as follows:

I went to File > Project Structure

Then I clicked Modules, and clicked the icon "+", and selected "Import Module".

Then I navigated to my Rails project's root directory "'/Users/my_username/code/apps/_murmur/skag_server_rails" and clicked Open, which displayed the following.

I then selected "Create module from existing sources" and clicked "Next", and it showed the following, with a blue checkbox shown next to my Rails project root directory that it found, so I clicked "Next" again:

It then showed the following window, and I clicked "Finish".

I'd now finished importing the Rails project I was working on as a Module, and it showed the following, so I clicked "Ok", as I was now ready to try and run my Rails server again:

So I went back to Run > Edit Configurations window and click on "Rails > skag" where I could now use "Choose SDK from module" since there was now my "skag_server_rails" Module available in the drop-down that I just created, and no errors were apparent, as shown below:

I was then able to click Ok to save the Run/Edit Config

I was then able to run the server Run > Run, which displayed:

I was then able to successfully open the Rails app in the browser open http://localhost:3000

But then when I tried to Run > Debug so I could debug using breakpoints, it gave me the following errors:

I solved this by running the following in the Terminal (outside the IDE):

```<code>gem install debase --pre
gem install ruby-debug-ide
</code>```

Then finally I could run Run > Debug and it allowed me to trigger breakpoints as shown below:

Important Note:

The initial approach that I took that DID NOT end up fixing the error involved choosing to use "Use other SDK" within the Run > Edit Configurations window to load my Ruby Gemset into the debug configuration (instead of using a Module).

UPDATE: I encountered the same error again more recently on 2nd September 2017 and followed these steps to overcome it:

Go to Menu > File > Project Structure > Project Settings > Modules > Click "+" and select "New Module"

Select "Ruby on Rails" from the left hand side list

Choose for "Module SDK" label your "RVM: ruby-2.4.1 []"

Click for "Rails Version" label the "Install Rails Gem..." drop-down

Select a Rails version for it to download and wait (i.e. 5.0.0)

Do not select a Rails version that causes the tooltip "This version is not fully supported" to appear below (i.e. selecting a Rails version >5.1.2 causes this to occur)

Click for "Rails Version" label the version that finished downloading from the drop-down (i.e. 5.0.0)

Enter "Working Directory" label value of "../"

Enter a "Module Name" (i.e. videotube-5.0.0)

Click "Apply" and "Save"

Go to Menu > Run > Edit Configurations

Change any directories that do not point to root folder of app

Go to Menu > Run > Debug

Body:
I upgraded from RubyMine to IntelliJ Ultimate IDE 2017.1.5 and I encountered the same error "Run Configuration Error: Rails server launcher wasn't found in project". I was using Ruby v2.4.0 with Rails 5.0.2.

It took me an entire day to solve the issue, so I've written a detailed explanation with screenshots to guide others.

It occurred when I tried to Run or Debug my Rails server from within IntelliJ.
Other solutions also didn't fix the error. I tried deleting my .idea/ folder, trying different Ruby or Rails versions (i.e. Ruby v2.3.0 or latest Rails 5.1.2), and even starting a brand new project from scratch. But I could run the Rails server fine using Terminal (outside the IDE) with rails s.

SOLUTION approach:

First I opened Run > Edit Configurations but "Choose SDK from module" didn't have any Project Modules available to select from the drop-down, so I had to create one as follows:

I went to File > Project Structure

Then I clicked Modules, and clicked the icon "+", and selected "Import Module".

Then I navigated to my Rails project's root directory "'/Users/my_username/code/apps/_murmur/skag_server_rails" and clicked Open, which displayed the following.

I then selected "Create module from existing sources" and clicked "Next", and it showed the following, with a blue checkbox shown next to my Rails project root directory that it found, so I clicked "Next" again:

It then showed the following window, and I clicked "Finish".

I'd now finished importing the Rails project I was working on as a Module, and it showed the following, so I clicked "Ok", as I was now ready to try and run my Rails server again:

So I went back to Run > Edit Configurations window and click on "Rails > skag" where I could now use "Choose SDK from module" since there was now my "skag_server_rails" Module available in the drop-down that I just created, and no errors were apparent, as shown below:

I was then able to click Ok to save the Run/Edit Config

I was then able to run the server Run > Run, which displayed:

I was then able to successfully open the Rails app in the browser open http://localhost:3000

But then when I tried to Run > Debug so I could debug using breakpoints, it gave me the following errors:

I solved this by running the following in the Terminal (outside the IDE):

gem install debase --pre
gem install ruby-debug-ide

Then finally I could run Run > Debug and it allowed me to trigger breakpoints as shown below:

Important Note:

The initial approach that I took that DID NOT end up fixing the error involved choosing to use "Use other SDK" within the Run > Edit Configurations window to load my Ruby Gemset into the debug configuration (instead of using a Module).

UPDATE: I encountered the same error again more recently on 2nd September 2017 and followed these steps to overcome it:

Go to Menu > File > Project Structure > Project Settings > Modules > Click "+" and select "New Module"

Select "Ruby on Rails" from the left hand side list

Choose for "Module SDK" label your "RVM: ruby-2.4.1 []"

Click for "Rails Version" label the "Install Rails Gem..." drop-down

Select a Rails version for it to download and wait (i.e. 5.0.0)

Do not select a Rails version that causes the tooltip "This version is not fully supported" to appear below (i.e. selecting a Rails version >5.1.2 causes this to occur)

Click for "Rails Version" label the version that finished downloading from the drop-down (i.e. 5.0.0)

Enter "Working Directory" label value of "../"

Enter a "Module Name" (i.e. videotube-5.0.0)

Click "Apply" and "Save"

Go to Menu > Run > Edit Configurations

Change any directories that do not point to root folder of app

Go to Menu > Run > Debug


问:
Laravel Pagination links not including other GET parameters
I am using Eloquent together with Laravel 4's Pagination class.

Problem: When there are some GET parameters in the URL, eg: http://site.example/users?gender=female&body=hot, the pagination links produced only contain the page parameter and nothing else.

Blade Template

```<code>{{ $users->link() }}
</code>```

There's a ->append() function for this, but when we don't know how many of the GET parameters are there, how can we use append() to include the other GET parameters in the paginated links without a whole chunk of if code messing up our blade template?

答:
Pass the page number for pagination as well. Some thing like this

$currentPg = Input::get('page') ? Input::get('page') : '1';
$boards = Cache::remember('boards' . $currentPg, 60, function() {
    return WhatEverModel::paginate(15);
});
```

ContentLicense: CC BY-SA 4.0

Body:
I am using Eloquent together with Laravel 4's Pagination class.

Problem: When there are some GET parameters in the URL, eg: http://site.example/users?gender=female&body=hot, the pagination links produced only contain the page parameter and nothing else.

Blade Template

{{ $users->link() }}

There's a ->append() function for this, but when we don't know how many of the GET parameters are there, how can we use append() to include the other GET parameters in the paginated links without a whole chunk of if code messing up our blade template?


问:
Is salting with a hashed version of the user's password just as secure as salting with a pre-defined string?
Basically, what I'm asking is if salting a password by appending a hashed version of it to the end of it is just as secure as salting with a different string. So:

Is this:

```<code>$pass = "pass";
$salt = sha1(md5($pass));
$pass = md5($pass.$salt);
</code>```

As secure as this?

```<code>$pass = "pass";
$salt = "4D9ds*^dkrUI45^#dkd*3fD8&!dlvd*";
$pass = md5($pass.$salt);
</code>```

答:
You shouldn't use md5 anyway as it's been cracked.  sha256 is more secure and just as easy to implement.
How about using, when storing the data:

```<code>$salt = mt_rand();
$pass = hash('sha256',$_POST['userPassword'] . $salt);
</code>```

Therefore $salt is not based on any user entered data, but random data. You then store $salt in the db as it is, or reverse the string for obfuscation.

Body:
Basically, what I'm asking is if salting a password by appending a hashed version of it to the end of it is just as secure as salting with a different string. So:

Is this:

$pass = "pass";
$salt = sha1(md5($pass));
$pass = md5($pass.$salt);

As secure as this?

$pass = "pass";
$salt = "4D9ds*^dkrUI45^#dkd*3fD8&!dlvd*";
$pass = md5($pass.$salt);

Body:
You shouldn't use md5 anyway as it's been cracked.  sha256 is more secure and just as easy to implement.
How about using, when storing the data:

$salt = mt_rand();
$pass = hash('sha256',$_POST['userPassword'] . $salt);

Therefore $salt is not based on any user entered data, but random data. You then store $salt in the db as it is, or reverse the string for obfuscation.


问:
Pandas fillna with DataFrame of values
Accordingly to the docs, the fillna value parameter can be one among the following:

```<code>value : scalar, dict, Series, or DataFrame
Value to use to fill holes (e.g. 0), alternately a dict/Series/DataFrame of values specifying which value to use for each index (for a Series) or column (for a DataFrame). (values not in the dict/Series/DataFrame will not be filled). This value cannot be a list.
</code>```

I have a data frame that looks like:

```<code>PassengerId Pclass  Name    Sex Age SibSp   Parch   Ticket  Fare    Cabin   Embarked
0   892 3   Kelly, Mr. James    male    34.5    0   0   330911  7.8292  NaN Q
1   893 3   Wilkes, Mrs. James (Ellen Needs)    female  47.0    1   0   363272  7.0000  NaN S
2   894 2   Myles, Mr. Thomas Francis   male    62.0    0   0   240276  9.6875  NaN Q
3   895 3   Wirz, Mr. Albert    male    27.0    0   0   315154  8.6625  NaN S
4   896 3   Hirvonen, Mrs. Alexander (Helga E Lindqvist)    female  22.0    1   1   3101298 12.2875 NaN S
</code>```

And that is what I want to do:

NaN Cabin will be filled according to the median value given the Pclass feature value

NaN Age will be filled according to its median value across the data set

NaN Embarked will be filled according to the median value given the Pclass feature value

So after some data manipulation, I got this data frame:

```<code>    Pclass  Cabin   Embarked    Ticket
0   1   C   S   50
1   2   F   S   13
2   3   G   S   5
</code>```

What it says is that for the Pclass == 1 the most common Cabin is C. Given that, in my original data frame df I want to fill every df['Cabin'] == null with C.

This is a small example and I could treat each possible null combination by hand with something as:

```<code>df_both[df_both['Pclass'] == 1 & df_both['Cabin'] == np.NaN] = 'C'
</code>```

However, I wonder if I can use this derived data frame to do all this filling automatic.

Thank you.

答:
If you want to fill all Nan's with something like the median or the mean of the specific column you can do the following.

for median:

```<code>df.fillna(df.median())
</code>```

for mean

```<code>df.fillna(df.mean())
</code>```

see https://pandas.pydata.org/pandas-docs/stable/missing_data.html#filling-with-a-pandasobject for more information.

Edit:

Alternatively you can use a dictionary with specified values. The keys need to map to column names. This way you can also impute for missing values in strings.

```<code>df.fillna({'col1':'a','col2': 1})
</code>```

Title: Pandas fillna with DataFrame of values

Body:
Accordingly to the docs, the fillna value parameter can be one among the following:

value : scalar, dict, Series, or DataFrame
Value to use to fill holes (e.g. 0), alternately a dict/Series/DataFrame of values specifying which value to use for each index (for a Series) or column (for a DataFrame). (values not in the dict/Series/DataFrame will not be filled). This value cannot be a list.

I have a data frame that looks like:

PassengerId Pclass  Name    Sex Age SibSp   Parch   Ticket  Fare    Cabin   Embarked
0   892 3   Kelly, Mr. James    male    34.5    0   0   330911  7.8292  NaN Q
1   893 3   Wilkes, Mrs. James (Ellen Needs)    female  47.0    1   0   363272  7.0000  NaN S
2   894 2   Myles, Mr. Thomas Francis   male    62.0    0   0   240276  9.6875  NaN Q
3   895 3   Wirz, Mr. Albert    male    27.0    0   0   315154  8.6625  NaN S
4   896 3   Hirvonen, Mrs. Alexander (Helga E Lindqvist)    female  22.0    1   1   3101298 12.2875 NaN S

And that is what I want to do:

NaN Cabin will be filled according to the median value given the Pclass feature value

NaN Age will be filled according to its median value across the data set

NaN Embarked will be filled according to the median value given the Pclass feature value

So after some data manipulation, I got this data frame:

    Pclass  Cabin   Embarked    Ticket
0   1   C   S   50
1   2   F   S   13
2   3   G   S   5

What it says is that for the Pclass == 1 the most common Cabin is C. Given that, in my original data frame df I want to fill every df['Cabin'] == null with C.

This is a small example and I could treat each possible null combination by hand with something as:

df_both[df_both['Pclass'] == 1 & df_both['Cabin'] == np.NaN] = 'C'

However, I wonder if I can use this derived data frame to do all this filling automatic.

Thank you.

Body:
If you want to fill all Nan's with something like the median or the mean of the specific column you can do the following.

for median:

df.fillna(df.median())

for mean

df.fillna(df.mean())

see https://pandas.pydata.org/pandas-docs/stable/missing_data.html#filling-with-a-pandasobject for more information.

Edit:

Alternatively you can use a dictionary with specified values. The keys need to map to column names. This way you can also impute for missing values in strings.

df.fillna({'col1':'a','col2': 1})