问:
Setting a variable from a string inside a TXT file with a batch file
I'm attempting to create a batch file for my company's users to use to migrate files from their older 'personal folder' network share to our new OneDrive implementation.

The problem is, some of our users have change the default sync folder for OneDrive to whatever drive and path they wanted, instead of keeping the default (*c:\users\username*)...for example, the one I'm using to test my BAT file against is using:

D:\OneDrive - Business

However, it does still have to work when it contains more or less delimiters, like C:\OneDrive or C:\This Is Where I Foolishly Store Stuff.

At this point, I've tried using REG QUERY to output to a text file from the UserFolder key in the registry to get the path, and now I'm trying to extract this path FROM the text file in question and set it to a variable.  The text file that is created looks like the following:

```<code>HKEY_CURRENT_USER\SOFTWARE\Microsoft\OneDrive\Accounts\Business1
    UserFolder    REG_SZ    D:\OneDrive - Business
</code>```

What I want to do is set the "D:\OneDrive - Business" to a variable to use in a scripted MOVE command.

My code is so awful, that I will refrain from posting it unless someone decides its necessary to answer my question.

I'm sure that for at least a DOZEN of you, this is child's play, so I would really appreciate any help that is provided me!!!

Thanks!!

答:
Here is one batch solution for this task:

```<code>@echo off
for /F "skip=1 tokens=1,2*" %%A in ('%SystemRoot%\System32\reg.exe QUERY HKCU\SOFTWARE\Microsoft\OneDrive\Accounts\Business1 /v UserFolder 2^>nul') do if /I "%%A" == "UserFolder" if not "%%C" == "" set "UserFolder=%%C" & goto UserFolderSet
echo No user folder for OneDrive found in Windows registry.
goto :EOF

:UserFolderSet
echo Found user folder: "%UserFolder%"
</code>```

Better readable is this version doing exactly the same:

```<code>@echo off
for /F "skip=1 tokens=1,2*" %%A in ('%SystemRoot%\System32\reg.exe QUERY HKCU\SOFTWARE\Microsoft\OneDrive\Accounts\Business1 /v UserFolder 2^>nul') do (
    if /I "%%A" == "UserFolder" (
        if not "%%C" == "" (
            set "UserFolder=%%C"
            goto UserFolderSet
        )
    )
)
echo No user folder for OneDrive found in Windows registry.
goto :EOF

:UserFolderSet
echo Found user folder: "%UserFolder%"
</code>```

The command FOR executes the command REG in a background command process with capturing its output written to handle STDOUT.

An error message output by REG to handle STDERR is suppressed by redirecting it to device NUL because of 2^>nul. The redirection operator > is escaped with caret character ^ which is necessary to interpret > first as literal character by Windows command interpreter on parsing the FOR command line. But later on execution of REG command line with 2>nul by FOR > is interpreted as redirection operator. An error message could occur if the registry value does not exist at all in Windows registry.

FOR processes each non empty line of captured output of REG command by splitting the line up into substrings (tokens) using space and tab as delimiter (default).

The option skip=1 instructs FOR to skip the first line of captured output.

The option tokens=1,2* instructs FOR that first space/tab delimited string being here value name UserFolder should be assigned to first loop variable A.

The second space/tab delimited string being here type REG_SZ should be assigned to loop variable B being the next character in ASCII table. Now it should be clear why loop variables are case-sensitive while environment variables are not case-sensitive. This loop variable is not further processed here although it might be good in case of type is REG_EXPAND_SZ instead of REG_SZ as in this case the directory path contains most likely also 1 or more environment variable references which must be expanded before having real directory path.

The string after the spaces/tabs after second space/tab delimited string being in your example D:\OneDrive - Business should be assigned without further splitting up on spaces/tabs to loop variable C because of * after 2 in options string tokens=1,2*.

The first IF condition makes sure the right line is processed as on Windows XP the output of REG starts with a header where only the first line would be skipped.

The second IF condition makes sure the user folder value has a non empty value.

The string of interest is finally assigned to environment variable UserFolder and the FOR loop is exited with a jump to the commands below label UserFolderSet.

The commands below the FOR loop are executed if the registry value was not found in Windows registry.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

echo /?

for /?

goto /?

if /?

set /?

Read also the Microsoft article about Using Command Redirection Operators.

Body:
I'm attempting to create a batch file for my company's users to use to migrate files from their older 'personal folder' network share to our new OneDrive implementation.

The problem is, some of our users have change the default sync folder for OneDrive to whatever drive and path they wanted, instead of keeping the default (*c:\users\username*)...for example, the one I'm using to test my BAT file against is using:

D:\OneDrive - Business

However, it does still have to work when it contains more or less delimiters, like C:\OneDrive or C:\This Is Where I Foolishly Store Stuff.

At this point, I've tried using REG QUERY to output to a text file from the UserFolder key in the registry to get the path, and now I'm trying to extract this path FROM the text file in question and set it to a variable.  The text file that is created looks like the following:

HKEY_CURRENT_USER\SOFTWARE\Microsoft\OneDrive\Accounts\Business1
    UserFolder    REG_SZ    D:\OneDrive - Business

What I want to do is set the "D:\OneDrive - Business" to a variable to use in a scripted MOVE command.

My code is so awful, that I will refrain from posting it unless someone decides its necessary to answer my question.

I'm sure that for at least a DOZEN of you, this is child's play, so I would really appreciate any help that is provided me!!!

Thanks!!

Body:
Here is one batch solution for this task:

@echo off
for /F "skip=1 tokens=1,2*" %%A in ('%SystemRoot%\System32\reg.exe QUERY HKCU\SOFTWARE\Microsoft\OneDrive\Accounts\Business1 /v UserFolder 2^>nul') do if /I "%%A" == "UserFolder" if not "%%C" == "" set "UserFolder=%%C" & goto UserFolderSet
echo No user folder for OneDrive found in Windows registry.
goto :EOF

:UserFolderSet
echo Found user folder: "%UserFolder%"

Better readable is this version doing exactly the same:

@echo off
for /F "skip=1 tokens=1,2*" %%A in ('%SystemRoot%\System32\reg.exe QUERY HKCU\SOFTWARE\Microsoft\OneDrive\Accounts\Business1 /v UserFolder 2^>nul') do (
    if /I "%%A" == "UserFolder" (
        if not "%%C" == "" (
            set "UserFolder=%%C"
            goto UserFolderSet
        )
    )
)
echo No user folder for OneDrive found in Windows registry.
goto :EOF

:UserFolderSet
echo Found user folder: "%UserFolder%"

The command FOR executes the command REG in a background command process with capturing its output written to handle STDOUT.

An error message output by REG to handle STDERR is suppressed by redirecting it to device NUL because of 2^>nul. The redirection operator > is escaped with caret character ^ which is necessary to interpret > first as literal character by Windows command interpreter on parsing the FOR command line. But later on execution of REG command line with 2>nul by FOR > is interpreted as redirection operator. An error message could occur if the registry value does not exist at all in Windows registry.

FOR processes each non empty line of captured output of REG command by splitting the line up into substrings (tokens) using space and tab as delimiter (default).

The option skip=1 instructs FOR to skip the first line of captured output.

The option tokens=1,2* instructs FOR that first space/tab delimited string being here value name UserFolder should be assigned to first loop variable A.

The second space/tab delimited string being here type REG_SZ should be assigned to loop variable B being the next character in ASCII table. Now it should be clear why loop variables are case-sensitive while environment variables are not case-sensitive. This loop variable is not further processed here although it might be good in case of type is REG_EXPAND_SZ instead of REG_SZ as in this case the directory path contains most likely also 1 or more environment variable references which must be expanded before having real directory path.

The string after the spaces/tabs after second space/tab delimited string being in your example D:\OneDrive - Business should be assigned without further splitting up on spaces/tabs to loop variable C because of * after 2 in options string tokens=1,2*.

The first IF condition makes sure the right line is processed as on Windows XP the output of REG starts with a header where only the first line would be skipped.

The second IF condition makes sure the user folder value has a non empty value.

The string of interest is finally assigned to environment variable UserFolder and the FOR loop is exited with a jump to the commands below label UserFolderSet.

The commands below the FOR loop are executed if the registry value was not found in Windows registry.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

echo /?

for /?

goto /?

if /?

set /?

Read also the Microsoft article about Using Command Redirection Operators.


来源: stackexchange

问:
Update a development team with rewritten Git repo history, removing big files
I have a git repo with some very large binaries in it. I no longer need them, and I don't care about being able to checkout the files from earlier commits. So, to reduce the repo size, I want to delete the binaries from the history altogether.

After a web search, I concluded that my best (only?) option is to use git-filter-branch:

```<code>git filter-branch --index-filter 'git rm --cached --ignore-unmatch big_1.zip big_2.zip etc.zip' HEAD
</code>```

Does this seem like a good approach so far?

Assuming the answer is yes, I have another problem to contend with. The git manual has this warning:

WARNING! The rewritten history will have different object names for all the objects and will not converge with the original branch. You will not be able to easily push and distribute the rewritten branch on top of the original branch. Please do not use this command if you do not know the full implications, and avoid using it anyway, if a simple single commit would suffice to fix your problem. (See the "RECOVERING FROM UPSTREAM REBASE" section in git-rebase(1) for further information about rewriting published history.)

We have a remote repo on our server. Each developer pushes to and pulls from it. Based on the warning above (and my understanding of how git-filter-branch works), I don't think I'll be able to run git-filter-branch on my local copy and then push the changes.

So, I'm tentatively planning to go through the following steps:

Tell all my developers to commit, push, and stop working for a bit.

Log into the server and run the filter on the central repo.

Have everyone delete their old copies and clone again from the server.

Does this sound right? Is this the best solution?

答:
Yes, your solution will work.  You also have another option: instead of doing this on the central repo, run the filter on your clone and then push it back with git push --force --all.  This will force the server to accept the new branches from your repository.  This replaces step 2 only; the other steps will be the same.

If your developers are pretty Git-savvy, then they might not have to delete their old copies; for example, they could fetch the new remotes and rebase their topic branches as appropriate.

ContentLicense: CC BY-SA 3.0

Body:
I have a git repo with some very large binaries in it. I no longer need them, and I don't care about being able to checkout the files from earlier commits. So, to reduce the repo size, I want to delete the binaries from the history altogether.

After a web search, I concluded that my best (only?) option is to use git-filter-branch:

git filter-branch --index-filter 'git rm --cached --ignore-unmatch big_1.zip big_2.zip etc.zip' HEAD

Does this seem like a good approach so far?

Assuming the answer is yes, I have another problem to contend with. The git manual has this warning:

WARNING! The rewritten history will have different object names for all the objects and will not converge with the original branch. You will not be able to easily push and distribute the rewritten branch on top of the original branch. Please do not use this command if you do not know the full implications, and avoid using it anyway, if a simple single commit would suffice to fix your problem. (See the "RECOVERING FROM UPSTREAM REBASE" section in git-rebase(1) for further information about rewriting published history.)

We have a remote repo on our server. Each developer pushes to and pulls from it. Based on the warning above (and my understanding of how git-filter-branch works), I don't think I'll be able to run git-filter-branch on my local copy and then push the changes.

So, I'm tentatively planning to go through the following steps:

Tell all my developers to commit, push, and stop working for a bit.

Log into the server and run the filter on the central repo.

Have everyone delete their old copies and clone again from the server.

Does this sound right? Is this the best solution?

ContentLicense: CC BY-SA 2.5


问:
Sequential file display "System.Windows.Forms.ListBox+SelectedObjectCollection" when trying to add selected listbox items
I'm working on a program where user has to select items from the listbox(lstCommercial) and save to access file, problem is that only the last selected items shows in access file, here's my code</p>

```<code>Public Class Form1

    Dim outfile As IO.StreamWriter
    Dim infile As IO.StreamReader

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        lstCommercial.Items.Add("Cheerios")
        lstCommercial.Items.Add("Doritos")
        lstCommercial.Items.Add("T-mobile")
        lstCommercial.Items.Add("Radioshack")
    End Sub

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click

        ' open the file to create
        outfile = IO.File.CreateText("commercial.txt")

        ' write the name on a separate line in the file
        outfile.WriteLine(lstCommercial.SelectedItems)

        ' close the file
        lstCommercial.Focus()
        outfile.Close()
    End Sub

    Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click

    End Sub
    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

End Class
</code>```

答:
Instead of this:

```<code>' write the name on a separate line in the file
outfile.WriteLine(lstCommercial.SelectedItems)
</code>```

...try this:

```<code>' write the name on a separate line in the file
For Each item In lstCommercial.SelectedItems
    outfile.WriteLine(item)
Next
</code>```

The problem is that you're not iterating over all of the elements in the SelectedItems collection. Also, make sure you have the SelectionMode property for your lstCommercial ListBox set to either MultiSimple or MultiExtended, and that you actually have multiple items selected when you click btnSave.

Body:
I'm working on a program where user has to select items from the listbox(lstCommercial) and save to access file, problem is that only the last selected items shows in access file, here's my code

Public Class Form1

    Dim outfile As IO.StreamWriter
    Dim infile As IO.StreamReader

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        lstCommercial.Items.Add("Cheerios")
        lstCommercial.Items.Add("Doritos")
        lstCommercial.Items.Add("T-mobile")
        lstCommercial.Items.Add("Radioshack")
    End Sub

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click

        ' open the file to create
        outfile = IO.File.CreateText("commercial.txt")

        ' write the name on a separate line in the file
        outfile.WriteLine(lstCommercial.SelectedItems)

        ' close the file
        lstCommercial.Focus()
        outfile.Close()
    End Sub

    Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click

    End Sub
    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

End Class

Body:
Instead of this:

' write the name on a separate line in the file
outfile.WriteLine(lstCommercial.SelectedItems)

...try this:

' write the name on a separate line in the file
For Each item In lstCommercial.SelectedItems
    outfile.WriteLine(item)
Next

The problem is that you're not iterating over all of the elements in the SelectedItems collection. Also, make sure you have the SelectionMode property for your lstCommercial ListBox set to either MultiSimple or MultiExtended, and that you actually have multiple items selected when you click btnSave.


问:
Merge two lists by key in object
I have seen _.zip which merges two lists based on index but instead I would like merge by key. The two lists are not necessarily sorted/indexed but will have same number of objects

for example

```<code>list1 = [{id:"1", field1:"field1val"},...]
list2 = [{id:"1", field2:"field2val"},...]
</code>```

merge would look like:

```<code> mergeOfList1List2 = [{id:"1", field1:"field1val", field2:"field2val"},...]
</code>```

答:
You can compare and merge like this. Though it is not generic, it would work in your case

list1 = [{id:"1", field1:"field1val"},{id:"2", field1:"abc"}]
list2 = [{id:"1", field2:"field2val"}]

for(var i=0; i<list2.length; i++){
  for(var j=0; j<list1.length; j++){
    if(list1[j].id === list2[i].id){
      list1[j].field2 = list2[i].field2
    }
  }
}

console.log(list1)```
</div>
</div>
</p>

<p>A better solution is this as it would combine objects completely(Union) even if there are more properties</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>list1 = [{id:"1", field1:"field1val"},{id:"2", field1:"abc"}]
list2 = [{id:"1", field2:"field2val"}]

for(var i=0; i<list2.length; i++){
  for(var j=0; j<list1.length; j++){
    if(list1[j].id === list2[i].id){
      Object.assign(list1[j],list2[i])    // to union two objects
    }
  }
}

console.log(list1)</code>```

Title: Merge two lists by key in object

Body:
I have seen _.zip which merges two lists based on index but instead I would like merge by key. The two lists are not necessarily sorted/indexed but will have same number of objects

for example

list1 = [{id:"1", field1:"field1val"},...]
list2 = [{id:"1", field2:"field2val"},...]

merge would look like:

 mergeOfList1List2 = [{id:"1", field1:"field1val", field2:"field2val"},...]

Body:
You can compare and merge like this. Though it is not generic, it would work in your case

list1 = [{id:"1", field1:"field1val"},{id:"2", field1:"abc"}]
list2 = [{id:"1", field2:"field2val"}]

for(var i=0; i<list2.length; i++){
  for(var j=0; j<list1.length; j++){
    if(list1[j].id === list2[i].id){
      list1[j].field2 = list2[i].field2
    }
  }
}

console.log(list1)

A better solution is this as it would combine objects completely(Union) even if there are more properties

list1 = [{id:"1", field1:"field1val"},{id:"2", field1:"abc"}]
list2 = [{id:"1", field2:"field2val"}]

for(var i=0; i<list2.length; i++){
  for(var j=0; j<list1.length; j++){
    if(list1[j].id === list2[i].id){
      Object.assign(list1[j],list2[i])    // to union two objects
    }
  }
}

console.log(list1)


问:
python inheritance : new class don't initialize properly
I am writing a class called XGB, which inherits from XGBClassifier (from the python library xgboost.sklearn). I wrote an init function and a fit one, shown here :

```<code>from xgboost.sklearn import XGBClassifier
from balanceSmote import BalanceSmote
from balance import Balance

class XGB(XGBClassifier):

def __init__(self,learning_rate=0.5, max_depth=3,colsample_bytree=0.5,n_estimators=300,
             frac=None,k_neighbors=None,m_neighbors=None,out_step=None):

    # These are the additional arguments that are not in XGBClassifier
    if k_neighbors:
        self.balancingStrategy = 'smote'
        self.k_neighbors = k_neighbors
        self.m_neighbors = m_neighbors
        self.out_step = out_step
    elif frac :
        self.balancingStrategy = 'normal'
        self.frac = frac
    else:
        self.balancingStrategy = 'false'

    # Utilize the motherClass
    super(XGB,self).__init__(seed=500,
                        learning_rate = learning_rate,
                        max_depth = max_depth,
                        colsample_bytree = colsample_bytree,
                        n_estimators = n_estimators)
</code>```

Here is my test code :

```<code>xgb4 = XGB(learning_rate = 0.1, max_depth = 3, colsample_bytree = 1, n_estimators = 1000)

xgb4.fit(trainData,trainLabel)
</code>```

The initialization seems to go well, but when I try to use fit() (which is a method inherited from XGBClassifier), I have an error message telling me a parameter is missing :

```<code>File "<ipython-input-3-47344b7fbc76>", line 1, in <module>
runfile('/Users/celsloaner/Project/SPUDS/code/testSpark.py', wdir='/Users/celsloaner/Project/SPUDS/code')

File "/anaconda/envs/SPUDS/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py", line 880, in runfile
execfile(filename, namespace)

File "/anaconda/envs/SPUDS/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "/Users/celsloaner/Project/SPUDS/code/testSpark.py", line 50, in <module>
xgb4.fit(predictor.trainData,predictor.trainLabel)

File "/anaconda/envs/SPUDS/lib/python3.5/site-packages/xgboost/sklearn.py", line 396, in fit
xgb_options = self.get_xgb_params()

File "/anaconda/envs/SPUDS/lib/python3.5/site-packages/xgboost/sklearn.py", line 177, in get_xgb_params
xgb_params = self.get_params()

File "/anaconda/envs/SPUDS/lib/python3.5/site-packages/xgboost/sklearn.py", line 169, in get_params
if params['missing'] is np.nan:

KeyError: 'missing'
</code>```

The problem comes from inside the mother class, which should have been initialized properly. Here is the mother class problematic function :

```<code>def get_params(self, deep=False):
    """Get parameter.s"""
    params = super(XGBModel, self).get_params(deep=deep)
    if params['missing'] is np.nan:
        params['missing'] = None  # sklearn doesn't handle nan. see #4725
    if not params.get('eval_metric', True):
        del params['eval_metric']  # don't give as None param to Booster
    return params
</code>```

The dictionary params is apparently not properly defined (the key 'missing' doesn't exist) when XGBClassifier initialization is called in XGB initialization.
Do you have any idea what is the problem or how to track it ?

Thanks

答:
Well, it worked when i initialized all of the mother class parameters, even if the mother class constructor is supposed to have default values for them :

```<code>class XGB(XGBClassifier):

    def __init__(self,max_depth=3, learning_rate=0.1,
                 n_estimators=100, silent=True,
                 objective="binary:logistic",
                 nthread=-1, gamma=0, min_child_weight=1,
                 max_delta_step=0, subsample=1, colsample_bytree=1, colsample_bylevel=1,
                 reg_alpha=0, reg_lambda=1, scale_pos_weight=1,
                 base_score=0.5, seed=0, missing=None,
                 frac=None,k_neighbors=None,m_neighbors=None,out_step=None):

        if k_neighbors:
            self.balancingStrategy = 'smote'
            self.k_neighbors = k_neighbors
            self.m_neighbors = m_neighbors
            self.out_step = out_step
        elif frac :
            self.balancingStrategy = 'normal'
            self.frac = frac
        else:
            self.balancingStrategy = 'false'

        super(XGB,self).__init__(max_depth, learning_rate,
                                n_estimators, silent, objective,
                                nthread, gamma, min_child_weight,
                                max_delta_step, subsample,
                                colsample_bytree, colsample_bylevel,
                                reg_alpha, reg_lambda,
                                scale_pos_weight, base_score, seed, missing)
</code>```

Not sure I understand the logic here, but it is always good to know :)

Body:
I am writing a class called XGB, which inherits from XGBClassifier (from the python library xgboost.sklearn). I wrote an init function and a fit one, shown here :

from xgboost.sklearn import XGBClassifier
from balanceSmote import BalanceSmote
from balance import Balance

class XGB(XGBClassifier):

def __init__(self,learning_rate=0.5, max_depth=3,colsample_bytree=0.5,n_estimators=300,
             frac=None,k_neighbors=None,m_neighbors=None,out_step=None):

    # These are the additional arguments that are not in XGBClassifier
    if k_neighbors:
        self.balancingStrategy = 'smote'
        self.k_neighbors = k_neighbors
        self.m_neighbors = m_neighbors
        self.out_step = out_step
    elif frac :
        self.balancingStrategy = 'normal'
        self.frac = frac
    else:
        self.balancingStrategy = 'false'

    # Utilize the motherClass
    super(XGB,self).__init__(seed=500,
                        learning_rate = learning_rate,
                        max_depth = max_depth,
                        colsample_bytree = colsample_bytree,
                        n_estimators = n_estimators)

Here is my test code :

xgb4 = XGB(learning_rate = 0.1, max_depth = 3, colsample_bytree = 1, n_estimators = 1000)

xgb4.fit(trainData,trainLabel)

The initialization seems to go well, but when I try to use fit() (which is a method inherited from XGBClassifier), I have an error message telling me a parameter is missing :

File "<ipython-input-3-47344b7fbc76>", line 1, in <module>
runfile('/Users/celsloaner/Project/SPUDS/code/testSpark.py', wdir='/Users/celsloaner/Project/SPUDS/code')

File "/anaconda/envs/SPUDS/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py", line 880, in runfile
execfile(filename, namespace)

File "/anaconda/envs/SPUDS/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "/Users/celsloaner/Project/SPUDS/code/testSpark.py", line 50, in <module>
xgb4.fit(predictor.trainData,predictor.trainLabel)

File "/anaconda/envs/SPUDS/lib/python3.5/site-packages/xgboost/sklearn.py", line 396, in fit
xgb_options = self.get_xgb_params()

File "/anaconda/envs/SPUDS/lib/python3.5/site-packages/xgboost/sklearn.py", line 177, in get_xgb_params
xgb_params = self.get_params()

File "/anaconda/envs/SPUDS/lib/python3.5/site-packages/xgboost/sklearn.py", line 169, in get_params
if params['missing'] is np.nan:

KeyError: 'missing'

The problem comes from inside the mother class, which should have been initialized properly. Here is the mother class problematic function :

def get_params(self, deep=False):
    """Get parameter.s"""
    params = super(XGBModel, self).get_params(deep=deep)
    if params['missing'] is np.nan:
        params['missing'] = None  # sklearn doesn't handle nan. see #4725
    if not params.get('eval_metric', True):
        del params['eval_metric']  # don't give as None param to Booster
    return params

The dictionary params is apparently not properly defined (the key 'missing' doesn't exist) when XGBClassifier initialization is called in XGB initialization.
Do you have any idea what is the problem or how to track it ?

Thanks

Body:
Well, it worked when i initialized all of the mother class parameters, even if the mother class constructor is supposed to have default values for them :

class XGB(XGBClassifier):

    def __init__(self,max_depth=3, learning_rate=0.1,
                 n_estimators=100, silent=True,
                 objective="binary:logistic",
                 nthread=-1, gamma=0, min_child_weight=1,
                 max_delta_step=0, subsample=1, colsample_bytree=1, colsample_bylevel=1,
                 reg_alpha=0, reg_lambda=1, scale_pos_weight=1,
                 base_score=0.5, seed=0, missing=None,
                 frac=None,k_neighbors=None,m_neighbors=None,out_step=None):

        if k_neighbors:
            self.balancingStrategy = 'smote'
            self.k_neighbors = k_neighbors
            self.m_neighbors = m_neighbors
            self.out_step = out_step
        elif frac :
            self.balancingStrategy = 'normal'
            self.frac = frac
        else:
            self.balancingStrategy = 'false'

        super(XGB,self).__init__(max_depth, learning_rate,
                                n_estimators, silent, objective,
                                nthread, gamma, min_child_weight,
                                max_delta_step, subsample,
                                colsample_bytree, colsample_bylevel,
                                reg_alpha, reg_lambda,
                                scale_pos_weight, base_score, seed, missing)

Not sure I understand the logic here, but it is always good to know :)


问:
Change id name of ImageView with a String
Suppose we have :

```<code>ImageView imageView_A = findViewById(R.id.imageview_dhze);
ImageView imageView_B = findViewById(R.id.imageview_dhhkjhkze);
ImageView imageView_C = findViewById(R.id.imageview_dhkhkjze);
ImageView imageView_D = findViewById(R.id.imageview_dhhuihuybze);
</code>```

I want to make a function like this :

```<code>changeImages(String NAME) {
    imageView_1_NAME.setImageResource(R.drawable.img);
    imageView_2_NAME.setImageResource(R.drawable.img);
    imageView_3_NAME.setImageResource(R.drawable.img);
}
</code>```

And for example, I could use it like this :

```<code>changeImage("A");
</code>```

to obtain :

```<code>imageView_1_A.setImageResource(R.drawable.img);
imageView_2_A.setImageResource(R.drawable.img);
imageView_3_A.setImageResource(R.drawable.img);
</code>```

It is possible ? How can I do that ?

答:
I think what you're looking for is something like this</p>

```<code>fun changeImage(which: String) {
    when(which) {
        "a" -> imageView_A.setImageResource(R.drawable.img)
        "b" -> imageView_B.setImageResource(R.drawable.img)
        "c" -> imageView_C.setImageResource(R.drawable.img)
        "d" -> imageView_D.setImageResource(R.drawable.img)

        else -> throw IllegalArgumentException("Unknown image view")
    }
}
</code>```

Body:
Suppose we have :

ImageView imageView_A = findViewById(R.id.imageview_dhze);
ImageView imageView_B = findViewById(R.id.imageview_dhhkjhkze);
ImageView imageView_C = findViewById(R.id.imageview_dhkhkjze);
ImageView imageView_D = findViewById(R.id.imageview_dhhuihuybze);

I want to make a function like this :

changeImages(String NAME) {
    imageView_1_NAME.setImageResource(R.drawable.img);
    imageView_2_NAME.setImageResource(R.drawable.img);
    imageView_3_NAME.setImageResource(R.drawable.img);
}

And for example, I could use it like this :

changeImage("A");

to obtain :

imageView_1_A.setImageResource(R.drawable.img);
imageView_2_A.setImageResource(R.drawable.img);
imageView_3_A.setImageResource(R.drawable.img);

It is possible ? How can I do that ?

Body:
I think what you're looking for is something like this

fun changeImage(which: String) {
    when(which) {
        "a" -> imageView_A.setImageResource(R.drawable.img)
        "b" -> imageView_B.setImageResource(R.drawable.img)
        "c" -> imageView_C.setImageResource(R.drawable.img)
        "d" -> imageView_D.setImageResource(R.drawable.img)

        else -> throw IllegalArgumentException("Unknown image view")
    }
}


问:
Catching Cascading Errors in PowerShell
I'm working with a wrapper script that calls a function which queries some databases inside specified servers and inserts metadata to a specific database on a separate server. I use the $error.count variable to determine if the script was successful or not. I anticipate some permission/extraction errors to happen and want these to be caught and ignored (no increase in the $error.count variable but having a warning written in the job log). I can confirm one permission error is happening and is being properly caught. The $error.count variable is not increased but a warning is printed from the catch showing the database that could not be accessed.

My problem occurs after the extraction/insertion function is finished running. Immediately after this function returns to the wrapper script, I have the $error.count variable print again. This time, it returns a 1 as if the error previously caught cascades into the wrapper script. As I mentioned previously, I do not want this to be included in the error count. I'm not sure how or why the $error.count is increased from this function.

Should I use a different variable to determine if the script "failed" or not? Is there some underlying reason why the $error.count would increase outside of the function that has the error while not increasing after the error is caught? Any guidance on this issue would be appreciated.

Code for reference:
Wrapper function:

```<code>$errorCount = $error.count
    Write-Warning ("$errorCount Before function")
    Extraction/Insertion_Function -serverList $serverList -insertionDB $insertionDB -ErrorAction SilentlyContinue
    $errorCount = $error.count
    Write-Warning ("$errorCount After function")
    } catch {
    Write-Error "Error caught by wrapper: $_"
    }
</code>```

Extraction/Insertion_Function:

```<code>ForEach ($db in $dbList) {
Write-Warning "$errorCount database
.
.
.
    try {
            $totalProperties = Get-ServerDBMetadata -DBConnectionString ($connStr) -DatabaseName $dbName -EA SilentlyContinue
            } catch {
                Write-Warning "Unable to extract metadata from $dbname in $server"
                }
}
</code>```

I then have the error count printing out inside the loop that extracts/inserts the metadata from each database to the insertion database, as well as in the loop for each server that contains the databases:

```<code>WARNING: 0 Before function
WARNING: 0 database
.
.
.
WARNING: 0 database
WARNING: Unable to extract metadata from *database* in *server*
WARNING: 0 database
.
.
.
WARNING: 0 database
**WARNING: 1 After function**
</code>```

The error (permission issue) is caught inside the function but cascades to my wrapper script. I want this particular error to be ignored while NOT ignoring other, more serious errors (like being unable to connect to the server I'm inserting the metadata into) so placing -EA Ignore on the driver function inside the wrapper script is out of the question.

答:
Your primary problem with the try-catch not catching the error (even though you don't supply all of the code) is that your cmdlet explicitly calls -ErrorAction SilentlyContinue.  Try/Catch blocks REQUIRE the use of terminating errors so in the case of your function/cmdlet, you need to change to -ErrorAction Stop for try/catch to appropriately handle an error from that function/cmdlet.

This needs to be updated for any other function/cmdlet in the code we can't see.

Edit described in comments below:

```<code> $n = New-Object PSObject -property @{
    'Test1' = ''
    'Test2' = ''
    'Test3' = ''
}

try {
    get-process someprocess -ErrorAction Stop
    $n.Test1 = $true
} catch {
    $n.Test1 = $false
}

try {
    Get-WmiObject win32_computersystem -ErrorAction Stop
    $n.Test2 = $true
} catch {
    $n.Test2 = $false
}

try {
    Get-Content somefile.ext -ErrorAction Stop
    $n.Test3 = $true
} catch {
    $n.Test3 = $false
}

if ($n.Test1 -and $n.Test2 -and $n.Test3) {
    ## All procedures completed successfully -- do something magical
} else {
    ## At least one test procedure failed.
}
</code>```

Title: Catching Cascading Errors in PowerShell

Body:
I'm working with a wrapper script that calls a function which queries some databases inside specified servers and inserts metadata to a specific database on a separate server. I use the $error.count variable to determine if the script was successful or not. I anticipate some permission/extraction errors to happen and want these to be caught and ignored (no increase in the $error.count variable but having a warning written in the job log). I can confirm one permission error is happening and is being properly caught. The $error.count variable is not increased but a warning is printed from the catch showing the database that could not be accessed.

My problem occurs after the extraction/insertion function is finished running. Immediately after this function returns to the wrapper script, I have the $error.count variable print again. This time, it returns a 1 as if the error previously caught cascades into the wrapper script. As I mentioned previously, I do not want this to be included in the error count. I'm not sure how or why the $error.count is increased from this function.

Should I use a different variable to determine if the script "failed" or not? Is there some underlying reason why the $error.count would increase outside of the function that has the error while not increasing after the error is caught? Any guidance on this issue would be appreciated.

Code for reference:
Wrapper function:

$errorCount = $error.count
    Write-Warning ("$errorCount Before function")
    Extraction/Insertion_Function -serverList $serverList -insertionDB $insertionDB -ErrorAction SilentlyContinue
    $errorCount = $error.count
    Write-Warning ("$errorCount After function")
    } catch {
    Write-Error "Error caught by wrapper: $_"
    }

Extraction/Insertion_Function:

ForEach ($db in $dbList) {
Write-Warning "$errorCount database
.
.
.
    try {
            $totalProperties = Get-ServerDBMetadata -DBConnectionString ($connStr) -DatabaseName $dbName -EA SilentlyContinue
            } catch {
                Write-Warning "Unable to extract metadata from $dbname in $server"
                }
}

I then have the error count printing out inside the loop that extracts/inserts the metadata from each database to the insertion database, as well as in the loop for each server that contains the databases:

WARNING: 0 Before function
WARNING: 0 database
.
.
.
WARNING: 0 database
WARNING: Unable to extract metadata from *database* in *server*
WARNING: 0 database
.
.
.
WARNING: 0 database
**WARNING: 1 After function**

The error (permission issue) is caught inside the function but cascades to my wrapper script. I want this particular error to be ignored while NOT ignoring other, more serious errors (like being unable to connect to the server I'm inserting the metadata into) so placing -EA Ignore on the driver function inside the wrapper script is out of the question.

Body:
Your primary problem with the try-catch not catching the error (even though you don't supply all of the code) is that your cmdlet explicitly calls -ErrorAction SilentlyContinue.  Try/Catch blocks REQUIRE the use of terminating errors so in the case of your function/cmdlet, you need to change to -ErrorAction Stop for try/catch to appropriately handle an error from that function/cmdlet.

This needs to be updated for any other function/cmdlet in the code we can't see.

Edit described in comments below:

 $n = New-Object PSObject -property @{
    'Test1' = ''
    'Test2' = ''
    'Test3' = ''
}

try {
    get-process someprocess -ErrorAction Stop
    $n.Test1 = $true
} catch {
    $n.Test1 = $false
}

try {
    Get-WmiObject win32_computersystem -ErrorAction Stop
    $n.Test2 = $true
} catch {
    $n.Test2 = $false
}

try {
    Get-Content somefile.ext -ErrorAction Stop
    $n.Test3 = $true
} catch {
    $n.Test3 = $false
}

if ($n.Test1 -and $n.Test2 -and $n.Test3) {
    ## All procedures completed successfully -- do something magical
} else {
    ## At least one test procedure failed.
}


问:
How to prevent Sql-Injection on User-Generated Sql Queries
I have a project (private, ASP.net website, password protected with https) where one of the requirements is that the user be able to enter Sql queries that will directly query the database. I need to be able to allow these queries, while preventing them from doing damage to the database itself, and from accessing or updating data that they shouldn't be able to access/update.

I have come up with the following rules for implementation:

Use a db user that only has permission for Select Table/View and Update Table (thus any other commands like drop/alter/truncate/insert/delete will just not run).

Verify that the statement begins with the words "Select" or "Update"

Verify (using Regex) that there are no instances of semi-colons in the statement that are not surrounded by single-quotes, white space and letters. (The thought here is that the only way that they could include a second query would be to end the first with a semi-colon that is not part of an input string).

Verify (using Regex) that the user has permission to access the tables being queried/updated, included in joins, etc. This includes any subqueries. (Part of the way that this will be accomplished is that the user will be using a set of table names that do not actually exist in the database, part of the query parsing will be to substitute in the correct corresponding table names into the query).

Am I missing anything?

The goal is that the users be able to query/update tables to which they have access in any way that they see fit, and to prevent any accidental or malicious attempts to damage the db. (And since a requirement is that the user generate the sql, I have no way to parametrize the query or sanitize it using any built-in tools that I know of).

答:
Although I agree with Joel Coehoorn and SQLMenace, some of us do have "requirements". Instead of having them send ad Hoc queries, why not create a visual query builder, like the ones found in the MS sample applications found at asp.net, or try this link.

I am not against the points made by Joel. He is correct. Having users (remember we are talking users here, they could care less about what you want to enforce) throw queries is like an app without a "Business Logic Layer", not to mention the additional questions to be answered when certain results does not match other supporting application results.

OwnerDisplayName: Yaakov Ellis

LastEditorDisplayName: Yaakov Ellis

OwnerDisplayName: Saif Khan


问:
How do I load XML into a List component in MXML Flash Builder 4?
How do I load XML from a local file into a List component in MXML Flash Builder 4?

I tried using Actionscript to create a loader, but I can't seem to add an event listener to the loader.  I've tried to create a local service but I couldn't get that to work either.

I'm making an XML driven AIR app. I plan on having multiple XML files being used - some for content and some for storing settings, state etc.

答:
Use HTTPService.  Check out an example in Tour de Flex:

http://www.adobe.com/devnet-archive/flex/tourdeflex/web/#sampleId=12700;illustIndex=0;docIndex=0


问:
Weird Spring MVC 4.2.x behaviour on controller method with ResponseBody and returning Model class instance
I have some legacy controller which puts some amount of data into the Model object (needed for Thymeleaf template).

Now I have to return the same data as JSON in REST service.

For these purposes I have wrapped the block of data preparation into separate method to use in two places: in the old method which is used for thymeleaf template and in the new one:

```<code>@RequestMapping(value = "/index", method = RequestMethod.GET)
public String index(Model model) {
    prepareIndexModel(model);
    return "index";
}

@RequestMapping(value = "/index/model", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
public Map<String, Object> indexModel(Model model) {
    prepareIndexModel(model);
    return model.asMap();
}

private void prepareIndexModel(Model model) {
    model.addAttribute("prop1", ...);
    ...
}
</code>```

However, when I try access via GET /index/model I receive the following error:

```<code>org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "/WEB-INF/templates/index/model.html")
</code>```

so it simply considers my method not as REST method. I guess, that it is because method is actually returns instance of ExtendedModelMap class which implements both interfaces: Model and Map.

Therefore, after chaning /index/model method to:

```<code>@RequestMapping(value = "/index/model", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
public Map<String, Object> indexModel(Model model) {
    prepareIndexModel(model);
    return new LinkedHashMap<>(model.asMap());
}
</code>```

everything started working expected: GET /index/model returns me desired JSON. So basically I just wrapped the model into LinkedHashMap.

My question is: is this a specified behaviour or simply a bug? I expected that by annotating a method with @ResponseBody annotation, Spring should ignore the fact that returning object is instance of Model. Shouldn't it?

UPDATE: @Sotirios Delimanolis provided the link to the very similar question, which was about version 3.2. But my question is not about why, but is it a bug of Spring or is it specified some where in the documentation?

UPDATE-2: Please, also note, that in the linked question a method has Model returning type, and its behaviour is described. In my case I have Map<...> returning type, what in my opinion makes this behaviour strange and inconsistent!

答:
is this a specified behaviour or simply a bug?

I would say it's undefined behavior.

The Spring Framework Reference defines that a return type of Model means a View with a Model. It also defines that a return type of Map means a View with the Map as the Model. These are both well-defined.

It also specifies that for a method annotated with @ResponseBody, the returned value is written to the response HTTP body. Again, this is well-defined.

What it doesn't specify, is what happens when a @ResponseBody returns a Model or a Map.

It is very common to return a Map to be encoded as JSON, and that works correctly, i.e. @ResponseBody takes precedence over return type Map. This makes sense.

However, a Model is specifically for the purpose of model attributes for a View, so the fact that return type Model takes precedence over @ResponseBody makes some sense. But as I said, it's not specified behavior, but neither is it a bug. It is undefined.

If you asked me for what it should do, I'd leave it undefined, since Model as @ResponseBody doesn't make any sense to me.

Also, the documentation doesn't make the distinction between declared return type and actual return type, so it is undefined which is used.

The implementation uses the actual return type, which has the advantage that your handler method can return Object and then return e.g. a ModelAndView or an HttpEntity, depending on conditions. This flexibility makes sense, but it isn't explicitly defined, as far as I can see.

So, result of the combination of @ResponseBody, declared return type of Map, but actual return type of Model, is undefined.

If you ask me (and you kind of did), I'd say that your code is bugged. If you want to return a Map to be sent as the response body, why not just create the Map yourself. Asking the Spring framework for a view Model makes no sense at all to me.

Even reading the code, I'd be unsure what you actually intended, given the mixed signals of using a Model and specifying @ResponseBody.

Conclusion: Don't be lazy, and create the Map yourself.

Body:
I have some legacy controller which puts some amount of data into the Model object (needed for Thymeleaf template).

Now I have to return the same data as JSON in REST service.

For these purposes I have wrapped the block of data preparation into separate method to use in two places: in the old method which is used for thymeleaf template and in the new one:

@RequestMapping(value = "/index", method = RequestMethod.GET)
public String index(Model model) {
    prepareIndexModel(model);
    return "index";
}

@RequestMapping(value = "/index/model", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
public Map<String, Object> indexModel(Model model) {
    prepareIndexModel(model);
    return model.asMap();
}

private void prepareIndexModel(Model model) {
    model.addAttribute("prop1", ...);
    ...
}

However, when I try access via GET /index/model I receive the following error:

org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "/WEB-INF/templates/index/model.html")

so it simply considers my method not as REST method. I guess, that it is because method is actually returns instance of ExtendedModelMap class which implements both interfaces: Model and Map.

Therefore, after chaning /index/model method to:

@RequestMapping(value = "/index/model", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
public Map<String, Object> indexModel(Model model) {
    prepareIndexModel(model);
    return new LinkedHashMap<>(model.asMap());
}

everything started working expected: GET /index/model returns me desired JSON. So basically I just wrapped the model into LinkedHashMap.

My question is: is this a specified behaviour or simply a bug? I expected that by annotating a method with @ResponseBody annotation, Spring should ignore the fact that returning object is instance of Model. Shouldn't it?

UPDATE: @Sotirios Delimanolis provided the link to the very similar question, which was about version 3.2. But my question is not about why, but is it a bug of Spring or is it specified some where in the documentation?

UPDATE-2: Please, also note, that in the linked question a method has Model returning type, and its behaviour is described. In my case I have Map<...> returning type, what in my opinion makes this behaviour strange and inconsistent!


问:
How can I filter/clip a raster provided by WMS - Geoserver
I'm trying to filter/clip a raster provided by a Web Map Service from Geoserver.

I dont want to clip based on a polygon; I want to filter based on a value such that all raster pixels below this value are black or transparent.

As far as I know I can not use cql_filter since its only for WFS Feature Requests and not for WMS Raster images.

Maybe you have an idea how to solve my question.

答:
A WMS does not return you data, it returns a picture of your data.

So there are two possibilities:

You use a Web Coverage Service (WCS) request which will return you the actual data in your raster. Think of it as like a WFS for rasters. The GeoServer manual covers the mechanics of making a request and there is a request builder under Demos for you to experiment. I'm pretty sure this will only allow you to subset by rectangular areas, though your client is free to do more complex operations of the returned data.

You can consider this a styling exercise in which case it should be possible to set up an SLD style that performs the operation that you need. You will need to use a spatial filter to clip the underlying raster and apply two rules depending on if it is true or false. You can even pass the polygon in as an environment variable from the request if you need.


答:
You can do that:</p>

```<code>void changeImage(String NAME) {
    int imageId = this.getResources().getIdentifier("imageView_" + NAME, "id", this.getPackageName());
    if (imageId != 0) {
        ImageView imageView = (ImageView) findViewById(imageId);
        imageView.setImageResource(R.drawable.img);
    }
}
</code>```

Title: Change id name of ImageView with a String

Body:
You can do that:

void changeImage(String NAME) {
    int imageId = this.getResources().getIdentifier("imageView_" + NAME, "id", this.getPackageName());
    if (imageId != 0) {
        ImageView imageView = (ImageView) findViewById(imageId);
        imageView.setImageResource(R.drawable.img);
    }
}


问:
Determine the complexity
Let say i have given an algorithmen X, which gets as an input an integer n.

Now consider  the X needs for  all input n which are smaller then x(  x is a fixed arbitrarily natural number)
 O(n2) steps. But for every input n > x it takes n steps.

Question: What is the (worstcase)runtime complexity of X?

Answer: The (worstcase) runtime complexity of X is O(n). For every fixed x, we can find numbers n such that n>(x2) and the (worst case) runtime for all n with n>(x2) is O(n). I am not sure if my answer is correct.

Edit: for better understanding T(n, x) \in if n <= x then O(n^2) else O(n). What is the worstcase runtime complexity?

答:
The runtime complexity of the algorithm X with the cost function (uniform cost criteria) T(n, x) which is a member of O(n^2) when n <= x, otherwise a member of O(n), i.e: T(n, x) \in if n <= x then O(n^2) else O(n) is, if we fix x \in Nat to an arbitrary number as you say, O(n).

This is the case because once x is fixed, and given that natural numbers have no bound, there is always a n >= x^2, wherefore we can disregard O(n^2) when dealing with the asymptotics of the algorithm.

Therefore, R(n) = T(n, x) \in O(n).

I find your reasoning to be sound.

Title: Determine the complexity


问:
ones complement of 0 - 0
For background,  I'm trying to calculate a checksum for an IP packet which is stored in constant memory (can't modify it in place).   Before doing the checksum, I'm supposed to pretend the existing checksum in the packet is 0.   Rather than copy all the data to temporary buffer, and store 0, I'd like to do a checksum of the entire packet, and then subtract the existing checksum from the result.

To do this, I was looking for a one's complement version of subtraction which I found here.  Unfortunately, if I use this, and subtract 0 from 0, I get 0x1111 instead of the expected 0:

Convert 0 to one's complement:  [1111]

Next, we add 0:   [1111]

no overflow bit, so we're done...

I would have expected 0 - 0 to be 0 -- what am I missing?

答:
You don't need to subtract.

If the checksum is valid, calculating the checksum over the entire packet, including the checksum, should give you 0.  If the result is not 0, the packet is corrupted.

This is how routers validate the checksum.

Title: ones complement of 0 - 0


问:
Best practices for making a queue of NSURLSessionTasks
What are the best practices for making a serial queue of NSURLSessionTasks ?

In my case, I need to:

Fetch a URL inside a JSON file (NSURLSessionDataTask)

Download the file at that URL (NSURLSessionDownloadTask)

Here’s what I have so far:

```<code>session = [NSURLSession sharedSession];

//Download the JSON:
NSURLRequest *dataRequest = [NSURLRequest requestWithURL:url];

NSURLSessionDataTask *task =
[session dataTaskWithRequest:dataRequest
           completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

               //Figure out the URL of the file I want to download:
               NSJSONSerialization *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
               NSURL *downloadURL = [NSURL urlWithString:[json objectForKey:@"download_url"]];

               NSURLSessionDownloadTask *fileDownloadTask =
               [session downloadTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:playlistURL]]
                              completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
                                  NSLog(@"completed!");
                              }];

               [fileDownloadTask resume];

           }
 ];
</code>```

Apart from the fact that writing a completion block within another completion looks messy, I am getting an EXC_BAD_ACCESS error when I call [fileDownloadTask resume]... Even though fileDownloadTask is not nil!

So, what is the best of way of sequencing NSURLSessionTasks?

答:
```<code>#import "SessionTaskQueue.h"

@interface SessionTaskQueue ()

@property (nonatomic, strong) NSMutableArray * sessionTasks;
@property (nonatomic, strong) NSURLSessionTask * currentTask;

@end

@implementation SessionTaskQueue

- (instancetype)init {

    self = [super init];
    if (self) {

        self.sessionTasks = [[NSMutableArray alloc] initWithCapacity:15];

    }
    return self;

}

- (void)addSessionTask:(NSURLSessionTask *)sessionTask {

    [self.sessionTasks addObject:sessionTask];
    [self resume];

}

// call in the completion block of the sessionTask
- (void)sessionTaskFinished:(NSURLSessionTask *)sessionTask {

    self.currentTask = nil;
    [self resume];

}

- (void)resume {

    if (self.currentTask) {
        return;
    }

    self.currentTask = [self.sessionTasks firstObject];
    if (self.currentTask) {
        [self.sessionTasks removeObjectAtIndex:0];
        [self.currentTask resume];
    }

}

@end
</code>```

and use like this

```<code>    __block __weak NSURLSessionTask * wsessionTask;
    use_wself();

    wsessionTask = [[CommonServices shared] doSomeStuffWithCompletion:^(NSError * _Nullable error) {
        use_sself();

        [self.sessionTaskQueue sessionTaskFinished:wsessionTask];

        ...

    }];

    [self.sessionTaskQueue addSessionTask:wsessionTask];
</code>```

Body:
What are the best practices for making a serial queue of NSURLSessionTasks ?

In my case, I need to:

Fetch a URL inside a JSON file (NSURLSessionDataTask)

Download the file at that URL (NSURLSessionDownloadTask)

Here’s what I have so far:

session = [NSURLSession sharedSession];

//Download the JSON:
NSURLRequest *dataRequest = [NSURLRequest requestWithURL:url];

NSURLSessionDataTask *task =
[session dataTaskWithRequest:dataRequest
           completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

               //Figure out the URL of the file I want to download:
               NSJSONSerialization *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
               NSURL *downloadURL = [NSURL urlWithString:[json objectForKey:@"download_url"]];

               NSURLSessionDownloadTask *fileDownloadTask =
               [session downloadTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:playlistURL]]
                              completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
                                  NSLog(@"completed!");
                              }];

               [fileDownloadTask resume];

           }
 ];

Apart from the fact that writing a completion block within another completion looks messy, I am getting an EXC_BAD_ACCESS error when I call [fileDownloadTask resume]... Even though fileDownloadTask is not nil!

So, what is the best of way of sequencing NSURLSessionTasks?

Body:
#import "SessionTaskQueue.h"

@interface SessionTaskQueue ()

@property (nonatomic, strong) NSMutableArray * sessionTasks;
@property (nonatomic, strong) NSURLSessionTask * currentTask;

@end

@implementation SessionTaskQueue

- (instancetype)init {

    self = [super init];
    if (self) {

        self.sessionTasks = [[NSMutableArray alloc] initWithCapacity:15];

    }
    return self;

}

- (void)addSessionTask:(NSURLSessionTask *)sessionTask {

    [self.sessionTasks addObject:sessionTask];
    [self resume];

}

// call in the completion block of the sessionTask
- (void)sessionTaskFinished:(NSURLSessionTask *)sessionTask {

    self.currentTask = nil;
    [self resume];

}

- (void)resume {

    if (self.currentTask) {
        return;
    }

    self.currentTask = [self.sessionTasks firstObject];
    if (self.currentTask) {
        [self.sessionTasks removeObjectAtIndex:0];
        [self.currentTask resume];
    }

}

@end

and use like this

    __block __weak NSURLSessionTask * wsessionTask;
    use_wself();

    wsessionTask = [[CommonServices shared] doSomeStuffWithCompletion:^(NSError * _Nullable error) {
        use_sself();

        [self.sessionTaskQueue sessionTaskFinished:wsessionTask];

        ...

    }];

    [self.sessionTaskQueue addSessionTask:wsessionTask];


问:
XML::Parser refusing to install
I've been trying to install XML::Simple on Ubuntu 12.10 for the past two hours or so and have officially run out of ideas. It's failing at the XML::Parser.

I've done an

```<code>sudo apt-get install expat
</code>```

and it still did not work. Here's the error I've been getting:

```<code>make[1]: Entering directory `/home/<user>/.cpan/build/XML-Parser-2.41-MCLo7T/Expat'
cp Expat.pm ../blib/lib/XML/Parser/Expat.pm
/usr/bin/perl /usr/share/perl/5.14.2/ExtUtils/xsubpp -noprototypes -typemap /usr/share /perl/5.14/ExtUtils/typemap -typemap typemap  Expat.xs > Expat.xsc && mv Expat.xsc Expat.c
cc -c   -D_REENTRANT -D_GNU_SOURCE -DDEBIAN -fstack-protector -fno-strict-aliasing -pipe -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -O2 -g   -DVERSION=\"2.41\" -DXS_VERSION=\"2.41\" -fPIC "-I/usr/lib/perl/5.14/CORE"   Expat.c
Expat.xs:12:19: fatal error: expat.h: No such file or directory
compilation terminated.
make[1]: *** [Expat.o] Error 1
make[1]: Leaving directory `/home/sclarawu/.cpan/build/XML-Parser-2.41-MCLo7T/Expat'
make: *** [subdirs] Error 2
TODDR/XML-Parser-2.41.tar.gz
/usr/bin/make -- NOT OK
Running make test
Can't test without successful make
Running make install
Make had returned bad status, install seems impossible
</code>```

Seems to be telling me that it's unable to find expat.h, but I'm not sure why or how, considering expat is installed? I tried to apt expat-devel, but it appears not to be a package in apt. Any ideas would be greatly appreciated. :)

EDIT: I seemed to have been simply trying to install the wrong package. -.- It's always the little things. Much thanks to those who answered!

答:
I only managed to install it by downloading it from cpan and manually editing the Makefile.pl inside to correctly pointing to the EXPATLIBPATH and EXPATINCPATH.

See this post for more detailed instructions.

Title: XML::Parser refusing to install

Body:
I've been trying to install XML::Simple on Ubuntu 12.10 for the past two hours or so and have officially run out of ideas. It's failing at the XML::Parser.

I've done an

sudo apt-get install expat

and it still did not work. Here's the error I've been getting:

make[1]: Entering directory `/home/<user>/.cpan/build/XML-Parser-2.41-MCLo7T/Expat'
cp Expat.pm ../blib/lib/XML/Parser/Expat.pm
/usr/bin/perl /usr/share/perl/5.14.2/ExtUtils/xsubpp -noprototypes -typemap /usr/share /perl/5.14/ExtUtils/typemap -typemap typemap  Expat.xs > Expat.xsc && mv Expat.xsc Expat.c
cc -c   -D_REENTRANT -D_GNU_SOURCE -DDEBIAN -fstack-protector -fno-strict-aliasing -pipe -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -O2 -g   -DVERSION=\"2.41\" -DXS_VERSION=\"2.41\" -fPIC "-I/usr/lib/perl/5.14/CORE"   Expat.c
Expat.xs:12:19: fatal error: expat.h: No such file or directory
compilation terminated.
make[1]: *** [Expat.o] Error 1
make[1]: Leaving directory `/home/sclarawu/.cpan/build/XML-Parser-2.41-MCLo7T/Expat'
make: *** [subdirs] Error 2
TODDR/XML-Parser-2.41.tar.gz
/usr/bin/make -- NOT OK
Running make test
Can't test without successful make
Running make install
Make had returned bad status, install seems impossible

Seems to be telling me that it's unable to find expat.h, but I'm not sure why or how, considering expat is installed? I tried to apt expat-devel, but it appears not to be a package in apt. Any ideas would be greatly appreciated. :)

EDIT: I seemed to have been simply trying to install the wrong package. -.- It's always the little things. Much thanks to those who answered!


问:
How do I set the text color of jQuery UI dialog title bars?
I inherited a project from another developer who used jQuery UI.

I've added a few pages that have jQuery UI dialogs and jQuery UI datepickers.

The problem is, the text of the title in the jQuery UI dialog title bar is the same color as the color of the title bar.  In the jQuery UI datepicker, the arrows that the user clicks to change the month on the calendar are the same color as the background there too!

I think this is set somewhere in the jQuery UI CSS.  Can someone please tell me where in the CSS I need to make the adjustment so that the text is visible?

My .ui-widget section is given below and I'm getting gray widgets and text on a gray background:

```<code>.ui-widget { font-family: Verdana,Arial,sans-serif/*{ffDefault}*/; font-size: 1.1em/*{fsDefault}*/; }
.ui-widget .ui-widget { font-size: 1em; }
.ui-widget input, .ui-widget select, .ui-widget textarea, .ui-widget button { font-family: Verdana,Arial,sans-serif/*{ffDefault}*/; font-size: 1em; }
.ui-widget-content { border: 1px solid #aaaaaa/*{borderColorContent}*/; background: #ffffff/*{bgColorContent}*/ url(images/ui-bg_flat_75_ffffff_40x100.png)/*{bgImgUrlContent}*/ 50%/*{bgContentXPos}*/ 50%/*{bgContentYPos}*/ repeat-x/*{bgContentRepeat}*/; color: #222222/*{fcContent}*/; }
.ui-widget-content a { color: #222222/*{fcContent}*/; }
.ui-widget-header { border: 1px solid #aaaaaa/*{borderColorHeader}*/;  background: #cccccc/*{bgColorHeader}*/ url(images/ui-bg_highlight-soft_75_cccccc_1x100.png)/*{bgImgUrlHeader}*/ 50%/*{bgHeaderXPos}*/ 50%/*{bgHeaderYPos}*/ repeat-x/*{bgHeaderRepeat}*/;   color: #222222/*{fcHeader}*/;    font-weight: bold; }
.ui-widget-header a { color: #222222 /*{fcHeader}*/; }
</code>```

UPDATE: I have awarded the points for the answer below.  I discovered that the reason that the widget is invisible is that the jQuery UI image files are missing from the copy of the program I'm working on!

答: The jQuery UI Theme Roller can help you roll out your own theme for jQuery. If a previous developer used jQuery, then he most likely also used theme roller to build his CSS. I would search through your files and look for a file with a name like jquery-ui-*.css where the * represents the version number (e.g. 1.8.4).

Body:
I inherited a project from another developer who used jQuery UI.

I've added a few pages that have jQuery UI dialogs and jQuery UI datepickers.

The problem is, the text of the title in the jQuery UI dialog title bar is the same color as the color of the title bar.  In the jQuery UI datepicker, the arrows that the user clicks to change the month on the calendar are the same color as the background there too!

I think this is set somewhere in the jQuery UI CSS.  Can someone please tell me where in the CSS I need to make the adjustment so that the text is visible?

My .ui-widget section is given below and I'm getting gray widgets and text on a gray background:

.ui-widget { font-family: Verdana,Arial,sans-serif/*{ffDefault}*/; font-size: 1.1em/*{fsDefault}*/; }
.ui-widget .ui-widget { font-size: 1em; }
.ui-widget input, .ui-widget select, .ui-widget textarea, .ui-widget button { font-family: Verdana,Arial,sans-serif/*{ffDefault}*/; font-size: 1em; }
.ui-widget-content { border: 1px solid #aaaaaa/*{borderColorContent}*/; background: #ffffff/*{bgColorContent}*/ url(images/ui-bg_flat_75_ffffff_40x100.png)/*{bgImgUrlContent}*/ 50%/*{bgContentXPos}*/ 50%/*{bgContentYPos}*/ repeat-x/*{bgContentRepeat}*/; color: #222222/*{fcContent}*/; }
.ui-widget-content a { color: #222222/*{fcContent}*/; }
.ui-widget-header { border: 1px solid #aaaaaa/*{borderColorHeader}*/;  background: #cccccc/*{bgColorHeader}*/ url(images/ui-bg_highlight-soft_75_cccccc_1x100.png)/*{bgImgUrlHeader}*/ 50%/*{bgHeaderXPos}*/ 50%/*{bgHeaderYPos}*/ repeat-x/*{bgHeaderRepeat}*/;   color: #222222/*{fcHeader}*/;    font-weight: bold; }
.ui-widget-header a { color: #222222 /*{fcHeader}*/; }

UPDATE: I have awarded the points for the answer below.  I discovered that the reason that the widget is invisible is that the jQuery UI image files are missing from the copy of the program I'm working on!


答:
You can use aggregate

```<code>aggregate(x = df$Value, by = list(df$TestNum), FUN = mean)
#  Group.1         x
#1       1 100.33333
#2       2 100.00000
#3       3  90.00000
#4       4  93.66667
#5       5 106.00000
</code>```

You could also split based on unique values of TestNum first and then summarize

```<code>data.frame(test_num = unique(df$TestNum), mean_value = sapply(split(df$Value, df$TestNum), mean))
#  test_num mean_value
#1        1  100.33333
#2        2  100.00000
#3        3   90.00000
#4        4   93.66667
#5        5  106.00000
</code>```

Title: Finding the average for certain unique categories in a data frame

Body:
If I have an R data frame that looks like this:

| Value | TestNum | RepNum |
|:-----:|:-------:|:------:|
| 104   |       1 |      1 |
| 101   |       1 |      2 |
| 101   |       1 |      3 |
| 100   |       2 |      1 |
| 100   |       2 |      2 |
| 100   |       2 |      3 |
| 90    |       3 |      1 |
| 90    |       3 |      2 |
| 90    |       3 |      3 |
| 91    |       4 |      1 |
| 94    |       4 |      2 |
| 94    |       4 |      3 |
| 105   |       5 |      1 |
| 105   |       5 |      2 |
| 108   |       5 |      3 |

Is there a way that I can modify this data frame to find the average for the 3 RepNum Values for each unique TestNum so that it looks like this:

| Mean | TestNum |
|:----:|:-------:|
| 102  |       1 |
| 100  |       2 |
| 90   |       3 |
| 93   |       4 |
| 106  |       5 |

You can create this example data frame in R by copying and pasting this code and executing it.

Value<-c(100,101,100,100,100,100,90,90,90,93,94,94,105,105,108)
TestNum<-c(1,1,1,2,2,2,3,3,3,4,4,4,5,5,5)
RepNum<-c(1,2,3,1,2,3,1,2,3,1,2,3,1,2,3)

df<-data.frame(Value,TestNum,RepNum)

EDIT: Here's a "more complete" example of the data frame I'm starting with an what I'm hoping to end up with:

| FileName | Version |  Category | Value | TestNum | RepNum |
|:--------:|:-------:|:---------:|:-----:|:-------:|:------:|
| File1    | 1.0.1   | Category1 |   104 |       1 |      1 |
| File1    | 1.0.1   | Category1 |   101 |       1 |      2 |
| File1    | 1.0.1   | Category1 |   101 |       1 |      3 |
| File1    | 1.0.2   | Category1 |   100 |       2 |      1 |
| File1    | 1.0.2   | Category1 |   100 |       2 |      2 |
| File1    | 1.0.2   | Category1 |   100 |       2 |      3 |
| File1    | 1.0.4   | Category1 |    90 |       3 |      1 |
| File1    | 1.0.4   | Category1 |    90 |       3 |      2 |
| File1    | 1.0.4   | Category1 |    90 |       3 |      3 |
| File1    | 1.0.5   | Category1 |    94 |       4 |      1 |
| File1    | 1.0.5   | Category1 |    91 |       4 |      2 |
| File1    | 1.0.5   | Category1 |    94 |       4 |      3 |
| File1    | 1.0.8   | Category1 |   105 |       5 |      1 |
| File1    | 1.0.8   | Category1 |   105 |       5 |      2 |
| File1    | 1.0.8   | Category1 |   108 |       5 |      3 |

And ending with this:

| FileName | Version |  Category | Mean_Value | TestNum |
|:--------:|:-------:|:---------:|:----------:|:-------:|
| File1    | 1.0.1   | Category1 |        102 |       1 |
| File1    | 1.0.2   | Category1 |        100 |       2 |
| File1    | 1.0.4   | Category1 |         90 |       3 |
| File1    | 1.0.5   | Category1 |         93 |       4 |
| File1    | 1.0.8   | Category1 |        106 |       5 |

As you've probably noticed, there is only 1 unique entry for both the FileName column and the Category column. The Version column is changing along with the TestNum column. So it seems like it might be easiest to simply add in these other columns after I've found the mean.

In the "full" code that I'm working on, I'm getting the mean values for several different files and many unique categories but I've been creating multiple data frames that are created by subsetting an original data frame  on the FileName and Category (and an additional "Case" column).

Body:
You can use aggregate

aggregate(x = df$Value, by = list(df$TestNum), FUN = mean)
#  Group.1         x
#1       1 100.33333
#2       2 100.00000
#3       3  90.00000
#4       4  93.66667
#5       5 106.00000

You could also split based on unique values of TestNum first and then summarize

data.frame(test_num = unique(df$TestNum), mean_value = sapply(split(df$Value, df$TestNum), mean))
#  test_num mean_value
#1        1  100.33333
#2        2  100.00000
#3        3   90.00000
#4        4   93.66667
#5        5  106.00000


问:
Can we use Markdown with Bot Framework for Skype for Business?
Can we use Markdown with Bot Framework for Skype for Business?

We implemented a code to send an image following the document below.
https://learn.microsoft.com/en-us/bot-framework/rest-api/bot-framework-rest-connector-create-messages

But it didn’t work. So we though Skype for Business may not be able to use Markdown feature.

答:
Markdown is not currently a supported text format for Bot Framework bots on the Skype for Business channel.

For more information on supported message formats see: Skype for Business Bot Framework (Preview) > Feature Support

Messages : Your bot can send plain text, HTML, and emoticons to a user or group. Users can send rich text, HTML, and emoticons to your bot.

Note: HTML support is planned for mid-to-late June release.


问:
Data gets garbled when writing to csv with fputcsv() / fgetcsv()
There seems to be an encoding issue or bug in PHP with fputcsv() and fgetcsv().

The following PHP code:

```<code>$row_before = ['A', json_encode(['a', '\\', 'b']), 'B'];

print "\nBEFORE:\n";
var_export($row_before);
print "\n";

$fh = fopen($file = 'php://temp', 'rb+');

fputcsv($fh, $row_before);

rewind($fh);

$row_after = fgetcsv($fh);

print "\nAFTER:\n";
var_export($row_after);
print "\n\n";

fclose($fh);
</code>```

Gives me this output:

```<code>BEFORE:
array (
  0 => 'A',
  1 => '["a","\\\\","b"]',
  2 => 'B',
)

AFTER:
array (
  0 => 'A',
  1 => '["a","\\\\',
  2 => 'b""]"',
  3 => 'B',
)
</code>```

So clearly, the data is damaged on the way. Originally there were just 3 cells in the row, afterwards there are 4 cells in the row. The middle cell is split thanks to the backslash that is also used as an escape character.

See also
https://3v4l.org/nc1oE
Or here, with explicit values for delimiter, enclosure, escape_char: https://3v4l.org/Svt7m

Is there any way I can sanitize / escape my data before writing to CSV, to guarantee that the data read from the file will be exactly the same?

Is CSV a fully reversible format?

EDIT: The goal would be a mechanism to properly write and read ANY data as csv, so that after one round trip the data is still the same.

EDIT: I realize that I do not really understand the $escape_char parameter. See also fgetcsv/fputcsv $escape parameter fundamentally broken Maybe an answer to this would also bring us closer to a solution.

答:
UPDATE Jan 2020

Since PHP 7.4, passing an empty string as escape char fixes the problem!
https://www.php.net/manual/en/function.fgetcsv.php

Demo https://3v4l.org/33Wja - see difference of PHP 7.4 vs older versions.
(this is the same snippet as below, just with empty string as escape char)

Original answer

Contrary to what others are saying, I claim that this is a PHP bug. I am going to report it, and update this answer.

EDIT: Now reported here, https://bugs.php.net/bug.php?id=74713

Discussed in this answer:

Does changing the delimiter help? -> Not really.

Could fputcsv() be fixed? -> Yes.

Does changing the delimiter help?

It can be shown that this is reproducible with any combination of delimiter, enclosure and escape character.

https://3v4l.org/a29kR

```<code>$delimiter = 'X';
$enclosure = 'Y';
$escape_char = "Z";

$row_before = [
  'A',
  "[{$enclosure}a{$enclosure}{$delimiter}{$enclosure}{$escape_char}{$escape_char}{$enclosure}{$delimiter}{$enclosure}b{$enclosure}]",
  'B',
];

print "\nBEFORE:\n";
var_export($row_before);
print "\n";

$fh = fopen($file = 'php://temp', 'rb+');

fputcsv($fh,$row_before,$delimiter,$enclosure, $escape_char);

rewind($fh);

$row_plain = fread($fh, 1000);

print "\nPLAIN:\n";
var_export($row_plain);
print "\n";

rewind($fh);

$row_after = fgetcsv($fh, 500,$delimiter,$enclosure, $escape_char);

print "\nAFTER:\n";
var_export($row_after);
print "\n\n";

fclose($fh);
</code>```

Output:

```<code>BEFORE:
array (
  0 => 'A',
  1 => '[YaYXYZZYXYbY]',
  2 => 'B',
)

PLAIN:
'AXY[YYaYYXYYZZYXYYbYY]YXB
'

AFTER:
array (
  0 => 'A',
  1 => '[YaYXYZZ',
  2 => 'bYY]Y',
  3 => 'B',
)
</code>```

Could fputcsv() be fixed?

For this let's turn back to more common and readable delimiter, enclosure and escape character.

```<code>$delimiter = ',';
$enclosure = '"';
$escape_char = "@";
</code>```

Here the result is:

```<code>BEFORE:
array (
  0 => 'A',
  1 => '["a","@@","b"]',
  2 => 'B',
)

PLAIN:
'A,"[""a"",""@@",""b""]",B
'

AFTER:
array (
  0 => 'A',
  1 => '["a","@@',
  2 => 'b""]"',
  3 => 'B',
)
</code>```

We see that the '"@@"' part is exported as '""@@"', while it SHOULD have been exported as '""@@""'.

In fact, doing this manually with fwrite() instead of fputcsv() does fix the problem: https://3v4l.org/4U1CQ

Body:
There seems to be an encoding issue or bug in PHP with fputcsv() and fgetcsv().

The following PHP code:

$row_before = ['A', json_encode(['a', '\\', 'b']), 'B'];

print "\nBEFORE:\n";
var_export($row_before);
print "\n";

$fh = fopen($file = 'php://temp', 'rb+');

fputcsv($fh, $row_before);

rewind($fh);

$row_after = fgetcsv($fh);

print "\nAFTER:\n";
var_export($row_after);
print "\n\n";

fclose($fh);

Gives me this output:

BEFORE:
array (
  0 => 'A',
  1 => '["a","\\\\","b"]',
  2 => 'B',
)

AFTER:
array (
  0 => 'A',
  1 => '["a","\\\\',
  2 => 'b""]"',
  3 => 'B',
)

So clearly, the data is damaged on the way. Originally there were just 3 cells in the row, afterwards there are 4 cells in the row. The middle cell is split thanks to the backslash that is also used as an escape character.

See also
https://3v4l.org/nc1oE
Or here, with explicit values for delimiter, enclosure, escape_char: https://3v4l.org/Svt7m

Is there any way I can sanitize / escape my data before writing to CSV, to guarantee that the data read from the file will be exactly the same?

Is CSV a fully reversible format?

EDIT: The goal would be a mechanism to properly write and read ANY data as csv, so that after one round trip the data is still the same.

EDIT: I realize that I do not really understand the $escape_char parameter. See also fgetcsv/fputcsv $escape parameter fundamentally broken Maybe an answer to this would also bring us closer to a solution.

Body:
UPDATE Jan 2020

Since PHP 7.4, passing an empty string as escape char fixes the problem!
https://www.php.net/manual/en/function.fgetcsv.php

Demo https://3v4l.org/33Wja - see difference of PHP 7.4 vs older versions.
(this is the same snippet as below, just with empty string as escape char)

Original answer

Contrary to what others are saying, I claim that this is a PHP bug. I am going to report it, and update this answer.

EDIT: Now reported here, https://bugs.php.net/bug.php?id=74713

Discussed in this answer:

Does changing the delimiter help? -> Not really.

Could fputcsv() be fixed? -> Yes.

Does changing the delimiter help?

It can be shown that this is reproducible with any combination of delimiter, enclosure and escape character.

https://3v4l.org/a29kR

$delimiter = 'X';
$enclosure = 'Y';
$escape_char = "Z";

$row_before = [
  'A',
  "[{$enclosure}a{$enclosure}{$delimiter}{$enclosure}{$escape_char}{$escape_char}{$enclosure}{$delimiter}{$enclosure}b{$enclosure}]",
  'B',
];

print "\nBEFORE:\n";
var_export($row_before);
print "\n";

$fh = fopen($file = 'php://temp', 'rb+');

fputcsv($fh,$row_before,$delimiter,$enclosure, $escape_char);

rewind($fh);

$row_plain = fread($fh, 1000);

print "\nPLAIN:\n";
var_export($row_plain);
print "\n";

rewind($fh);

$row_after = fgetcsv($fh, 500,$delimiter,$enclosure, $escape_char);

print "\nAFTER:\n";
var_export($row_after);
print "\n\n";

fclose($fh);

Output:

BEFORE:
array (
  0 => 'A',
  1 => '[YaYXYZZYXYbY]',
  2 => 'B',
)

PLAIN:
'AXY[YYaYYXYYZZYXYYbYY]YXB
'

AFTER:
array (
  0 => 'A',
  1 => '[YaYXYZZ',
  2 => 'bYY]Y',
  3 => 'B',
)

Could fputcsv() be fixed?

For this let's turn back to more common and readable delimiter, enclosure and escape character.

$delimiter = ',';
$enclosure = '"';
$escape_char = "@";

Here the result is:

BEFORE:
array (
  0 => 'A',
  1 => '["a","@@","b"]',
  2 => 'B',
)

PLAIN:
'A,"[""a"",""@@",""b""]",B
'

AFTER:
array (
  0 => 'A',
  1 => '["a","@@',
  2 => 'b""]"',
  3 => 'B',
)

We see that the '"@@"' part is exported as '""@@"', while it SHOULD have been exported as '""@@""'.

In fact, doing this manually with fwrite() instead of fputcsv() does fix the problem: https://3v4l.org/4U1CQ

ContentLicense: CC BY-SA 4.0


问:
OnEditorActionListener is not initiated for EditText inside popup window
I have a popUp window which is called on click of a button in activity, this popup has a edittext inside it, im trying to call search function on hitting enter after typing in EditText.But the control never comes inside OnEditorActionListener i have also tried setOnKeyListener.

This is the code for popup window:

```<code>private PopupWindow initiatePopupWindow(int i) {
try {
     mInflater = (LayoutInflater) getApplicationContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View layout = mInflater.inflate(R.layout.rowsearch, null);
View viewone = layout.findViewById(R.id.blankone);
View viewtwo = layout.findViewById(R.id.blanktwo);
searchedit = (EditText)layout.findViewById(R.id.search_edit);
//       searchedit.setImeActionLabel("Go", KeyEvent.KEYCODE_ENTER);
//       mDropdown.setFocusable(true);
//       mDropdown.update();
//       searchedit.setFocusableInTouchMode(true);
 searchedit.requestFocus();
 searchedit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if (actionId == EditorInfo.IME_ACTION_DONE) {
                        // Search function to be called here
                       Log.d("search","search");
                       searchfunc();

                        return true;
                    }
                    return false;
                }
            });
 layout.measure(View.MeasureSpec.UNSPECIFIED,
                    View.MeasureSpec.UNSPECIFIED);
            mDropdown = new PopupWindow(layout, FrameLayout.LayoutParams.MATCH_PARENT,
                    FrameLayout.LayoutParams.MATCH_PARENT,true);

            mDropdown.showAsDropDown(linearLayout,5,5);

    } catch (Exception e) {
        e.printStackTrace();
    }
    return mDropdown;

}
</code>```

This is the layout:

```<code><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>

<View
    android:layout_width="match_parent"
    android:layout_height="140dp"
    android:id="@+id/blankone">

</View>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:orientation="vertical"
    android:padding="20dp"
    android:background="@drawable/white_menu">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColorHint="#b5b5b5"
            android:id="@+id/search_edit"
            android:imeOptions="actionDone"
            android:hint="what can we help you find?"/>

    </LinearLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:id="@+id/blanktwo">

</View>

</LinearLayout>
</code>```

I tried many things like setfocusable(true) but nothing seems to work i want searchfunc(); to be called on hitting the enter key in the EditText.

答: Adding android:inputType="text" to the EditText solved the issue.

Body:
I have a popUp window which is called on click of a button in activity, this popup has a edittext inside it, im trying to call search function on hitting enter after typing in EditText.But the control never comes inside OnEditorActionListener i have also tried setOnKeyListener.

This is the code for popup window:

private PopupWindow initiatePopupWindow(int i) {
try {
     mInflater = (LayoutInflater) getApplicationContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View layout = mInflater.inflate(R.layout.rowsearch, null);
View viewone = layout.findViewById(R.id.blankone);
View viewtwo = layout.findViewById(R.id.blanktwo);
searchedit = (EditText)layout.findViewById(R.id.search_edit);
//       searchedit.setImeActionLabel("Go", KeyEvent.KEYCODE_ENTER);
//       mDropdown.setFocusable(true);
//       mDropdown.update();
//       searchedit.setFocusableInTouchMode(true);
 searchedit.requestFocus();
 searchedit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if (actionId == EditorInfo.IME_ACTION_DONE) {
                        // Search function to be called here
                       Log.d("search","search");
                       searchfunc();

                        return true;
                    }
                    return false;
                }
            });
 layout.measure(View.MeasureSpec.UNSPECIFIED,
                    View.MeasureSpec.UNSPECIFIED);
            mDropdown = new PopupWindow(layout, FrameLayout.LayoutParams.MATCH_PARENT,
                    FrameLayout.LayoutParams.MATCH_PARENT,true);

            mDropdown.showAsDropDown(linearLayout,5,5);

    } catch (Exception e) {
        e.printStackTrace();
    }
    return mDropdown;

}

This is the layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>

<View
    android:layout_width="match_parent"
    android:layout_height="140dp"
    android:id="@+id/blankone">

</View>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:orientation="vertical"
    android:padding="20dp"
    android:background="@drawable/white_menu">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColorHint="#b5b5b5"
            android:id="@+id/search_edit"
            android:imeOptions="actionDone"
            android:hint="what can we help you find?"/>

    </LinearLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:id="@+id/blanktwo">

</View>

</LinearLayout>

I tried many things like setfocusable(true) but nothing seems to work i want searchfunc(); to be called on hitting the enter key in the EditText.


问:
ionic2 push notification API taking a long time
In an Ionic2 project that I am working on I have configured the Ionic Push Cloud service. When I use the ionic.io dashboard to send a push notification, the notification registers on the target device immediately.

When I now use the ionic http push API to programmatically deploy the push notification, the push takes a few hours to register on my target device. Is this expected behavior? If not how can we reduce the push timing to have push notifications go out in an instantaneous manner (within a minute or two?).

Of note: A week or two ago I had also tested the http push API and observed instantaneous push notifications to my target device.

答:
It could have been a possible outage. Does the issue still persist?

Push notifications are expected to be instant or near-instant most of the time. I suppose your case on that day could have been affected by smaller undocumented backend procedures, like migration or maintenance of some sort, which is why your notifications.were not lost, but late.


问:
How do I install python 3 on google cloud console?
I'm just getting started in the Google Cloud Console. I've created a VM (Red Hat Enterprise Linux 7). Python 2.7 comes preinstalled. How do I update to Python 3? Also 'pip install' doesn't work; I'm assuming that will come with Python 3 once I get that updated. </p>

答:
RHEL 7 running on GCE VM is mostly identical to RHEL 7 running on your own physical server.

One thing to be aware is that by default on GCE Vms, passwordless sudo is enabled with the default account you ssh into the VM.

Once you ssh into the VM (either from the browser or using gcloud compute ssh), you can follow these steps:

```<code># Install Python 3.4
sudo yum -y install python34

# Install python-setuptools which will bring in easy_install
sudo yum -y install python34-setuptools

# Install pip using easy_install
sudo easy_install-3.4 pip
</code>```

Body:
RHEL 7 running on GCE VM is mostly identical to RHEL 7 running on your own physical server.

One thing to be aware is that by default on GCE Vms, passwordless sudo is enabled with the default account you ssh into the VM.

Once you ssh into the VM (either from the browser or using gcloud compute ssh), you can follow these steps:

# Install Python 3.4
sudo yum -y install python34

# Install python-setuptools which will bring in easy_install
sudo yum -y install python34-setuptools

# Install pip using easy_install
sudo easy_install-3.4 pip


问:
Select Direct Child of $(this) in Jquery
I know that I can use $('img', this) to select all img elements in $(this).

In my current case I am trying to manipulate only DIRECT child images of $(this).

Is there a selector for that??

答:
```<code>$(this).children('img');
</code>```

See also: http://api.jquery.com/children/

Body:
$(this).children('img');

See also: http://api.jquery.com/children/


问:
Line breaks in CSV row
I've the following csv

```<code>DATO;JOBSID;JOBNR;TEKST;AFSENDER;MODTAGER
02-06-2017;1234;4321;"MEI KONG ML KL. 00.45-13.00 TLF. NR 1023 0188
SØNDAG ML KL 24.00-11.00 TLF. NR. 6935 0188
Prags Boulevard 49D.  Tlf. 3134 5998
Bogpakker: Tlf. 1245 0880
TILLIDSREPRÆSENTANT GRETE WISE: TLF. 21200919";"DI";"ALLE"
</code>```

Initially I was using the newline character for row tokenization. But my data also contains line breaks, and I needed to maintain those line breaks. I've tried to replace the line break with a special character and later replace that with the line break. But in this scenario I'm not able to tokenize.

My XSLT is as following:

```<code>    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:f="foo/bar"
    exclude-result-prefixes="xs"
    version="2.0">
    <xsl:output encoding="iso-8859-1" method="xml" indent="yes"/>
    <xsl:include href="job-lib.xsl"/><!-- string normalization and other general functions in this library -->

    <!-- Parameters -->
    <xsl:param name="csv-file"/><!-- the input csv file -->
    <xsl:param name="text-encoding" as="xs:string" select="'iso-8859-1'"/>
    <xsl:param name="root-element"/><!-- root element name in output file -->

    <!-- The direct input to the transformation is an "empty" xml file with (empty) root element "null" -->
    <xsl:template match="null">
        <xsl:variable name="output-file" select="concat(substring-before($csv-file,'.csv'),'.xml')"/>
            <xsl:element name="{$root-element}">
                <xsl:variable name="csv-content">
                    <xsl:for-each select="tokenize(unparsed-text($csv-file,$text-encoding),'
')">
                        <xsl:element name="row">
                            <xsl:value-of select="."/>
                        </xsl:element>
                    </xsl:for-each>
                </xsl:variable>
                <xsl:variable name="column-names" select="tokenize(string($csv-content/row[1]),';')"/>
                <xsl:for-each select="$csv-content/row[position() > 1]">
                    <xsl:copy>
                        <xsl:for-each select="tokenize(string(.),';')">
                            <xsl:variable name="column-pos" select="position()"/>
                            <xsl:variable name="element-name" select="$column-names[$column-pos]"/>
                            <xsl:element name="{$element-name}">
                                <xsl:value-of select="f:normalize(.)"/>
                            </xsl:element>
                        </xsl:for-each>
                    </xsl:copy>
                </xsl:for-each>
            </xsl:element>
    </xsl:template>
</xsl:stylesheet>
</code>```

Required XML is

```<code><job>
  <row>
    <DATO>02-06-2017</DATO>
    <JOBSID>1234</JOBSID>
    <JOBNR>4321</JOBNR>
    <TEKST>
      "MEI KONG ML KL. 00.45-13.00 TLF. NR 1023 0188
      MEI KONG ML KL 24.00-11.00 TLF. NR. 6935 0188
      Prags Boulevard 49D.  Tlf. 3134 5998
      Bogpakker: Tlf. 1245 0880
      TILLIDSREPRÆSENTANT GRETE WISE: TLF. 21200919
    </TEKST>
    <AFSENDER>DI</AFSENDER>
    <MODTAGER>ALLE</MODTAGER>
   </row>
</job>
</code>```

Please help me, how can I convert my CSV into the required XML format which contains the line breaks as well.

I'm using calabash for the conversion.

答: It is unclear to me if your sample data is representative.  If your actual data is fully consistent with your sample data, then a simple fix might be possible -- your MODTAGER string is the last data field in each record, and that string ends in "\n.  Meanwhile, your TEKST string, the only data field that contains newlines, never has a newline immediately preceded by a double quote -- so "\n would never match anything in the TEKST string.  You could thus safely tokenize on "\n to break the file up by each individual record.

Title: Line breaks in CSV row

Body:
I've the following csv

DATO;JOBSID;JOBNR;TEKST;AFSENDER;MODTAGER
02-06-2017;1234;4321;"MEI KONG ML KL. 00.45-13.00 TLF. NR 1023 0188
SØNDAG ML KL 24.00-11.00 TLF. NR. 6935 0188
Prags Boulevard 49D.  Tlf. 3134 5998
Bogpakker: Tlf. 1245 0880
TILLIDSREPRÆSENTANT GRETE WISE: TLF. 21200919";"DI";"ALLE"

Initially I was using the newline character for row tokenization. But my data also contains line breaks, and I needed to maintain those line breaks. I've tried to replace the line break with a special character and later replace that with the line break. But in this scenario I'm not able to tokenize.

My XSLT is as following:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:f="foo/bar"
    exclude-result-prefixes="xs"
    version="2.0">
    <xsl:output encoding="iso-8859-1" method="xml" indent="yes"/>
    <xsl:include href="job-lib.xsl"/>

    <xsl:param name="csv-file"/>
    <xsl:param name="text-encoding" as="xs:string" select="'iso-8859-1'"/>
    <xsl:param name="root-element"/>

    <xsl:template match="null">
        <xsl:variable name="output-file" select="concat(substring-before($csv-file,'.csv'),'.xml')"/>
            <xsl:element name="{$root-element}">
                <xsl:variable name="csv-content">
                    <xsl:for-each select="tokenize(unparsed-text($csv-file,$text-encoding),'
')">
                        <xsl:element name="row">
                            <xsl:value-of select="."/>
                        </xsl:element>
                    </xsl:for-each>
                </xsl:variable>
                <xsl:variable name="column-names" select="tokenize(string($csv-content/row[1]),';')"/>
                <xsl:for-each select="$csv-content/row[position() > 1]">
                    <xsl:copy>
                        <xsl:for-each select="tokenize(string(.),';')">
                            <xsl:variable name="column-pos" select="position()"/>
                            <xsl:variable name="element-name" select="$column-names[$column-pos]"/>
                            <xsl:element name="{$element-name}">
                                <xsl:value-of select="f:normalize(.)"/>
                            </xsl:element>
                        </xsl:for-each>
                    </xsl:copy>
                </xsl:for-each>
            </xsl:element>
    </xsl:template>
</xsl:stylesheet>

Required XML is

<job>
  <row>
    <DATO>02-06-2017</DATO>
    <JOBSID>1234</JOBSID>
    <JOBNR>4321</JOBNR>
    <TEKST>
      "MEI KONG ML KL. 00.45-13.00 TLF. NR 1023 0188
      MEI KONG ML KL 24.00-11.00 TLF. NR. 6935 0188
      Prags Boulevard 49D.  Tlf. 3134 5998
      Bogpakker: Tlf. 1245 0880
      TILLIDSREPRÆSENTANT GRETE WISE: TLF. 21200919
    </TEKST>
    <AFSENDER>DI</AFSENDER>
    <MODTAGER>ALLE</MODTAGER>
   </row>
</job>

Please help me, how can I convert my CSV into the required XML format which contains the line breaks as well.

I'm using calabash for the conversion.


问:
Can I use localStorage to create a permanent database?
I'm currently learning about hybrid app development on my own. I need to create a local database on the phone in order to save some information, however, I don't know much about storage.

I've seen HTML5 Local Storage (https://www.w3schools.com/html/html5_webstorage.asp), and it's easy and confortable to apply, but I don't know if this kind of storage will be permanent on a hybrid app when the phone turns off or when the app closes.

Can you, please, help me with this doubt?

Thank you!

答:
It depends of what kind of information do you want to save.

If you need to save some app information (color, kind of font...) it's ok. But if you want to save some personal information (credit card, id) is better if you use a some database.

You just need to be aware the information that is in localStorage will be deleted for the user anytime so ask yourself, does it matter if all the information is deleted?