问:
Remove duplicate data from dynamic select option of HTMl form
In my website's database, there are a lot of posts inserted with the date. I am making a post shorting system where a user selects a year from the dynamic dropdown and after pressing the submit button only that year's post will be visible on the website. Everything is going good but on the dynamic dropdown, a year is showing multiple time.

For example, There are 3 posts in my database which date is 2/2/2019 , 21/2/2019 , 22/3/2019. In my dynamic dropdown there I find 2019 3 times, but I need to show 2019 only one time.

I have tried some jQuery and JavaScript code but it did not work.

My code :

```<code><?php
    $query = "SELECT year(date) FROM `press_release` WHERE del_status=0";
    $year = $conn->query($query);
?>

<form class="form-inline title" action="press_new.php" method="post">
    <div class="form-group">
        <label for="" style="color:#EB2227">Search by year</label>
        <select name="year">
            <option value="0">Please Select</option>

            <?php while($row = mysqli_fetch_array($year)){ ?>
                <option value="<?php echo($row['year(date)'])?>">
                    <?php echo($row['year(date)']) ?>
                </option>
            <?php } ?>
        </select>
        <button type="submit" name="submit" class="btn btn-default" style="background-color:#EB2227">Submit</button>
    </div>
</form>
</code>```

答:
Select unique/distinct years instead of just year in your sql query. </p>

```<code><?php

        $query = "SELECT distinct year(date) FROM `press_release` WHERE del_status=0";
        $year = $conn->query($query);
        ?>

        <form class="form-inline" class="title" action="press_new.php" method="post">
            <div class="form-group">
                <label for="" style="color:#EB2227">Search by year</label>
                <select name="year">
                    <option value="0">Please Select</option>
                    <?php
                        while($row = mysqli_fetch_array($year))
                        {
                    ?>
                    <option value="<?php echo($row['year(date)'])?>">
                        <?php echo($row['year(date)']) ?>
                    </option>
                    <?php
                        }
                    ?>
                </select>
                <button type="submit" name="submit" class="btn btn-default" style="background-color:#EB2227">Submit</button>
            </div>
        </form>
</code>```

Body:
In my website's database, there are a lot of posts inserted with the date. I am making a post shorting system where a user selects a year from the dynamic dropdown and after pressing the submit button only that year's post will be visible on the website. Everything is going good but on the dynamic dropdown, a year is showing multiple time.

For example, There are 3 posts in my database which date is 2/2/2019 , 21/2/2019 , 22/3/2019. In my dynamic dropdown there I find 2019 3 times, but I need to show 2019 only one time.

I have tried some jQuery and JavaScript code but it did not work.

My code :

<?php
    $query = "SELECT year(date) FROM `press_release` WHERE del_status=0";
    $year = $conn->query($query);
?>

<form class="form-inline title" action="press_new.php" method="post">

        Search by year
        <select name="year">
            <option value="0">Please Select</option>

            <?php while($row = mysqli_fetch_array($year)){ ?>
                <option value="<?php echo($row['year(date)'])?>">
                    <?php echo($row['year(date)']) ?>
                </option>
            <?php } ?>
        </select>
        <button type="submit" name="submit" class="btn btn-default" style="background-color:#EB2227">Submit</button>

</form>

Body:
Select unique/distinct years instead of just year in your sql query.

<?php

        $query = "SELECT distinct year(date) FROM `press_release` WHERE del_status=0";
        $year = $conn->query($query);
        ?>

        <form class="form-inline" class="title" action="press_new.php" method="post">
            <div class="form-group">
                <label for="" style="color:#EB2227">Search by year</label>
                <select name="year">
                    <option value="0">Please Select</option>
                    <?php
                        while($row = mysqli_fetch_array($year))
                        {
                    ?>
                    <option value="<?php echo($row['year(date)'])?>">
                        <?php echo($row['year(date)']) ?>
                    </option>
                    <?php
                        }
                    ?>
                </select>
                <button type="submit" name="submit" class="btn btn-default" style="background-color:#EB2227">Submit</button>
            </div>
        </form>


来源: stackexchange

问:
Differentiating between different USB accessory attached to phone
I am doing a project in android studio which requires me to check if a printer and a usb fingerprint scanner is attached to the device. How can I check if the device connected is printer or scanner ?

i have used USBManager and UsbDevice to get the list of all connected devices but not able to check the type of devices connected.

答:
From the official UsbConstants documentation

```<code>int Class = device.getDeviceClass();
</code>```

ex : if printer : USB_CLASS_PRINTER  : Constant Value: 7

ContentLicense: CC BY-SA 4.0

Body:
From the official UsbConstants documentation

int Class = device.getDeviceClass();

ex : if printer : USB_CLASS_PRINTER  : Constant Value: 7


问:
How to parse a text file by either unique identifying header or FF form feed char?
I have a text file that comes from a credit report agency.  The text files come with unique file numbers which i am able to search for using regex.  The problem is, the data i want to extract per file number is never in the exact location.

For instance, if in the text file i have a file number TP067283, the date of birth or social security number maybe be in one column one time or different for another file number.

What is constant is that for every unique file number there is a unique Header "TransUnion Credit Report" and it terminates with "END OF TRANSUNION REPORT".

And between these 2 headers the data will be in there.

For example:

```<code>RF_TP067283               TRANSUNION CREDIT REPORT

 <FOR>          <SUB NAME>          <MKT SUB>  <INFILE>   <DATE>      <TIME>
 (I) Y CH0001434                     06 CH     12/16      05/21/19    10:22CT

 <SUBJECT>                                          <SSN>
 TA****, K**                                        ###-##-####
 <CURRENT ADDRESS>                                               <DATE RPTD>
 1307 Blah CT., WHEELING IL. 66666                                12/16
 ----------------------------------------------------------------------------
 S P E C I A L   M E S S A G E S
 ****IDVISION ALERTS : CLEAR FOR ALL SEARCHES PERFORMED***
 ----------------------------------------------------------------------------
 M O D E L   P R O F I L E
 ***RECOVERY MODEL 1.0: NOT SCORED: INSUFFICIENT CREDIT***
 ----------------------------------------------------------------------------

                             END OF TRANSUNION REPORT

RF_TP067284               TRANSUNION CREDIT REPORT

 <FOR>          <SUB NAME>          <MKT SUB>  <INFILE>   <DATE>      <TIME>
 (I) Y CH0001434                     07 RK      4/05      05/21/19    10:22CT

 <SUBJECT>                                          <SSN>        <BIRTH DATE>
 P****, A*****  K.                                 ***-**-****   2/87

 <CURRENT ADDRESS>                                               <DATE RPTD>
 93 W. AUBURNDALE AV., CORTLAND IL. 66666                        10/06

 ----------------------------------------------------------------------------
 S P E C I A L   M E S S A G E S
 ****IDVISION ALERTS : CLEAR FOR ALL SEARCHES PERFORMED***
 ----------------------------------------------------------------------------
 M O D E L   P R O F I L E
 ***RECOVERY MODEL 1.0 SCORE +519  : ***
 ----------------------------------------------------------------------------
 C R E D I T   S U M M A R Y      * * *    T O T A L  F I L E  H I S T O R Y
 PR=0 COL=5  NEG=13 HSTNEG=0     TRD=27 RVL=11 INST=16 MTG=0  OPN=0  INQ=9
 C R E D I T  R E P O R T  S E R V I C E D  B Y :
 TRANSUNION                                                    800-888-4213
 2 BALDWIN PLACE, P.O. BOX 1000 CHESTER, PA 19016
 CONSUMER DISCLOSURES CAN BE OBTAINED ONLINE THROUGH TRANSUNION AT:
      HTTP://WWW.TRANSUNION.COM

                             END OF TRANSUNION REPORT
</code>```

The file numbers are always on the top left with the header row. The information i want to extract is always in between.

But say for instance like below, The first file has a  and the social directly below it. But it has no .  And sometimes they're never in the same position.

What i already tried was Streamreader but if a file number is missing a date of birth or social, i'll have uneven columns of data since data is missing.

Here's my code so far:

```<code>Dim textfile = "C:\Users\username\DeskTop\Fucked up sample data.txt"

Sub Main()
Dim foundfileNumbers = FindFileNumbers(textfile)
    For Each filenumber In foundfileNumbers
        getFileNumberData(filenumber.ToString, textfile)
        'Console.WriteLine(filenumber.ToString)
    Next
End Sub

    Public Function FindFileNumbers(ByVal textfile As String)
        Dim filereader As New System.IO.StreamReader(textfile)
    Dim pages As List(Of String) = New List(Of String)
    Do While filereader.Peek() <> -1

        Dim regexPattern = "TP[0-9]{6}"
        Dim reg = New Regex(regexPattern)

        Dim currenttext As String = Nothing

        Dim textline As String = filereader.ReadLine()
        currenttext = textline.Substring(0, 77)
        currenttext.IndexOf("", StringComparison.InvariantCultureIgnoreCase)
        Dim matches = reg.Matches(currenttext)

        For Each m In matches
            'Console.WriteLine(m.ToString)
            pages.Add(m.ToString)
        Next m

    Loop
    Return pages

End Function
</code>```

This is where i get stuck:

```<code>    Public Function getFileNumberData(ByVal filenumber As String, ByVal textfile As String) As String
            Dim returnElement As String = Nothing
            Dim filereader As New System.IO.StreamReader(textfile)

            Do While filereader.Peek() <> -1

                Dim textline = filereader.ReadLine()
                If textline.Substring(0, 77).Contains(filenumber) Then
                Do While filereader.Peek() <> -1
                    Dim textline2 = filereader.ReadLine()
                    If textline2.Substring(0, 77).Contains("BIRTH DATE")Then
                        Dim textline3 = filereader.ReadLine()
                        returnElement = textline3.Substring(0, 77)
                        Console.WriteLine(returnElement)
                    Else
                        returnElement = "No DOB"
                        Console.WriteLine(returnElement)
                    End If

                Loop
            End If

        Loop
        Return returnElement
    End Function
</code>```

So right there since it could not find "BIRTH DATE" it just gives me "No DOB" for every line that does not have it.....

Much Appreciated.

答:
So after thoroughly going over this, i found a solution by using a regex pattern, finding a keyword and adding the correct number of indices to it so that the code can find the value directly beneath the regex pattern keyword. </p>

<p>My code also separates each file till its footer row and makes a separate PDF, so this code includes all of that.  </p>

```<code>Imports System.IO
Imports System.IO.Path
Imports Microsoft.Office.Interop.Excel
Imports Microsoft.Office.Interop
Imports System.Text.RegularExpressions
Imports System.Text
Imports iTextSharp.text.pdf
Imports iTextSharp.text
Module Module1
    Dim xlApp As New Excel.Application
    Dim xlWorkbook As Excel.Workbook
    Dim xlWorksheet1 As Excel.Worksheet
    Dim counter As Integer = 0
    Dim CBR = Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "CBR")
    Dim textfile = "C:\Users\username\DeskTop\CBR large data.txt"

    Dim TextFileFolder = Combine(CBR, "Text Files")
    Sub Main()
        If Not Directory.Exists(CBR) Then
            Directory.CreateDirectory(CBR)
        End If
        If Not Directory.Exists(TextFileFolder) Then
            Directory.CreateDirectory(TextFileFolder)
        End If
        Dim rmt = "C:\Users\xborja\DeskTop\CBR large data.txt"

        xlWorkbook = xlApp.Workbooks.Add()
        xlWorksheet1 = CType(xlWorkbook.Sheets("Sheet1"), Excel.Worksheet)
        xlWorksheet1.Cells(1, 1) = "FILENO"
        xlWorksheet1.Cells(1, 2) = "NUMBER"
        xlWorksheet1.Cells(1, 3) = "DOB"
        xlWorksheet1.Cells(1, 4) = "HIGH RISK"
        xlWorksheet1.Cells(1, 5) = "SCORE"
        xlWorksheet1.Cells(1, 6) = "PR"
        xlWorksheet1.Cells(1, 7) = "MTG"
        xlWorksheet1.Range("C:C").NumberFormat = "m/d/yyyy"

        Dim reader = File.OpenText(textfile)
        Dim builder = New StringBuilder(9000)
        'Allocate a reasonable size buffer to avoid mem allocs
        Dim line As String = reader.ReadLine
        Dim counter = 0
        While (Not (line) Is Nothing)
            counter += 1

            builder.AppendLine(line)
            If line.Contains("END OF TRANSUNION REPORT") Then

                CheckAndProcess(builder.ToString)

                builder.Clear()
            End If

            line = reader.ReadLine

        End While

        xlWorkbook.SaveAs(CBR & "/" & System.DateTime.Now.ToString("yyyyMMdd") & " CBR.xlsx")
        xlWorkbook.Close(True)
        Process.Start("explorer.exe", CBR)

        xlApp.Quit()
        releaseObject(xlApp)
        releaseObject(xlWorkbook)
        releaseObject(xlWorksheet1)
    End Sub
    Public Sub CheckAndProcess(ByVal page As String)
        Dim filenumber = Nothing
        Dim foundfileNumbers = FindFileNumbers(textfile)
        Dim BirthDateidx = Nothing
        Dim SSNidx = Nothing
        Dim ScoreIdx = Nothing
        Dim PRidx = Nothing
        Dim MTGidx = Nothing
        Dim DOB = Nothing
        Dim SSN = Nothing
        Dim Risk = Nothing
        Dim PR = Nothing
        Dim MTG = Nothing
        Dim Score = Nothing

        Dim pdfDoc As New Document()

        For Each filenumber In foundfileNumbers

            If page.Contains((filenumber)) Then
                Dim pdfWrite As PdfWriter = PdfWriter.GetInstance(pdfDoc, New FileStream("C:\Users\xborja\Desktop\CBR\Text Files\" & filenumber & ".pdf", FileMode.Create))
                pdfDoc.Open()
                pdfDoc.Add(New Paragraph(page))

                counter += 1
                xlWorksheet1.Cells(counter + 1, 1) = filenumber
                If Not page.Contains("BIRTH DATE") Then
                    Console.WriteLine("no DOB")
                    DOB = " "
                    xlWorksheet1.Cells(counter + 1, 3) = DOB

                Else
                    BirthDateidx = page.IndexOf("BIRTH DATE") + 78 'returning birth date index plus enough characters to go to next line
                    Console.WriteLine(page.Substring(BirthDateidx, 5).Replace(" ", "0"))
                    DOB = page.Substring(BirthDateidx, 5).Replace(" ", "0")
                    xlWorksheet1.Cells(counter + 1, 3) = DOB

                End If

                If Not page.Contains("SSN") Then
                    Console.WriteLine("no ssn")
                    SSN = " "
                    xlWorksheet1.Cells(counter + 1, 8) = SSN

                Else
                    SSNidx = page.IndexOf("SSN") + 78
                    Console.WriteLine(page.Substring(SSNidx, 11))
                    SSN = page.Substring(SSNidx, 11)
                    xlWorksheet1.Cells(counter + 1, 8) = SSN

                End If

                If page.Contains("CLEAR FOR ALL SEARCHES PERFORMED") Then
                    Risk = "N"
                    xlWorksheet1.Cells(counter + 1, 4) = Risk
                    Console.WriteLine("N")

                Else
                    Risk = "Y"
                    xlWorksheet1.Cells(counter + 1, 4) = Risk
                    Console.WriteLine("Y")

                End If
                If Not page.Contains("RECOVERY MODEL 1.0 SCORE") Then
                    Console.WriteLine("no score")
                    Score = " "
                    xlWorksheet1.Cells(counter + 1, 5) = Score
                Else
                    ScoreIdx = page.IndexOf("RECOVERY MODEL 1.0 SCORE") + 26
                    Console.WriteLine(page.Substring(ScoreIdx, 3))
                    Score = page.Substring(ScoreIdx, 3)
                    xlWorksheet1.Cells(counter + 1, 5) = Score

                End If

                If page.Contains("PR=") Then
                    PRidx = page.IndexOf("PR=") + 3
                    Console.WriteLine(page.Substring(PRidx, 1))
                    PR = page.Substring(PRidx, 1)
                    xlWorksheet1.Cells(counter + 1, 6) = PR

                Else
                    Console.WriteLine("NO PR")
                    PR = "0"
                    xlWorksheet1.Cells(counter + 1, 6) = PR

                End If

                If page.Contains("MTG=") Then
                    MTGidx = page.IndexOf("MTG=") + 4
                    Console.WriteLine(page.Substring(MTGidx, 1))
                    MTG = page.Substring(MTGidx, 1)
                    xlWorksheet1.Cells(counter + 1, 7) = MTG

                Else
                    Console.WriteLine("NO MTG")
                    MTG = "0"
                    xlWorksheet1.Cells(counter + 1, 7) = MTG

                End If

            End If

            File.AppendAllText("C:\Users\xborja\Desktop\CBR\Text Files\" & filenumber & ".txt", page)

        Next
        pdfDoc.Close()

    End Sub
    Public Function FindFileNumbers(ByVal textfile As String)
        Dim filereader As New System.IO.StreamReader(textfile)
        Dim pages As List(Of String) = New List(Of String)
        Do While filereader.Peek() <> -1

            Dim regexPattern = "TP[0-9]{6}"
            Dim reg = New Regex(regexPattern)

            Dim currenttext As String = Nothing

            Dim textline As String = filereader.ReadLine()
            currenttext = textline.Substring(0, 77)
            currenttext.IndexOf("", StringComparison.InvariantCultureIgnoreCase)
            Dim matches = reg.Matches(currenttext)

            For Each m In matches

                pages.Add(m.ToString)
            Next m

        Loop
        Return pages

    End Function
    Private Sub releaseObject(ByVal aCell As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(aCell)
            aCell = Nothing
        Catch ex As Exception
            aCell = Nothing
        Finally
            GC.Collect()
        End Try
    End Sub

End Module
</code>```

Body:
I have a text file that comes from a credit report agency.  The text files come with unique file numbers which i am able to search for using regex.  The problem is, the data i want to extract per file number is never in the exact location.

For instance, if in the text file i have a file number TP067283, the date of birth or social security number maybe be in one column one time or different for another file number.

What is constant is that for every unique file number there is a unique Header "TransUnion Credit Report" and it terminates with "END OF TRANSUNION REPORT".

And between these 2 headers the data will be in there.

For example:

RF_TP067283               TRANSUNION CREDIT REPORT

 <FOR>                    <MKT SUB>  <INFILE>   <DATE>      <TIME>
 (I) Y CH0001434                     06 CH     12/16      05/21/19    10:22CT

 <SUBJECT>                                          <SSN>
 TA****, K**                                        ###-##-####
 <CURRENT ADDRESS>                                               <DATE RPTD>
 1307 Blah CT., WHEELING IL. 66666                                12/16
 ----------------------------------------------------------------------------
 S P E C I A L   M E S S A G E S
 ****IDVISION ALERTS : CLEAR FOR ALL SEARCHES PERFORMED***
 ----------------------------------------------------------------------------
 M O D E L   P R O F I L E
 ***RECOVERY MODEL 1.0: NOT SCORED: INSUFFICIENT CREDIT***
 ----------------------------------------------------------------------------

                             END OF TRANSUNION REPORT

RF_TP067284               TRANSUNION CREDIT REPORT

 <FOR>                    <MKT SUB>  <INFILE>   <DATE>      <TIME>
 (I) Y CH0001434                     07 RK      4/05      05/21/19    10:22CT

 <SUBJECT>                                          <SSN>        <BIRTH DATE>
 P****, A*****  K.                                 ***-**-****   2/87

 <CURRENT ADDRESS>                                               <DATE RPTD>
 93 W. AUBURNDALE AV., CORTLAND IL. 66666                        10/06

 ----------------------------------------------------------------------------
 S P E C I A L   M E S S A G E S
 ****IDVISION ALERTS : CLEAR FOR ALL SEARCHES PERFORMED***
 ----------------------------------------------------------------------------
 M O D E L   P R O F I L E
 ***RECOVERY MODEL 1.0 SCORE +519  : ***
 ----------------------------------------------------------------------------
 C R E D I T   S U M M A R Y      * * *    T O T A L  F I L E  H I S T O R Y
 PR=0 COL=5  NEG=13 HSTNEG=0     TRD=27 RVL=11 INST=16 MTG=0  OPN=0  INQ=9
 C R E D I T  R E P O R T  S E R V I C E D  B Y :
 TRANSUNION                                                    800-888-4213
 2 BALDWIN PLACE, P.O. BOX 1000 CHESTER, PA 19016
 CONSUMER DISCLOSURES CAN BE OBTAINED ONLINE THROUGH TRANSUNION AT:
      HTTP://WWW.TRANSUNION.COM

                             END OF TRANSUNION REPORT

The file numbers are always on the top left with the header row. The information i want to extract is always in between.

But say for instance like below, The first file has a  and the social directly below it. But it has no .  And sometimes they're never in the same position.

What i already tried was Streamreader but if a file number is missing a date of birth or social, i'll have uneven columns of data since data is missing.

Here's my code so far:

Dim textfile = "C:\Users\username\DeskTop\Fucked up sample data.txt"

Sub Main()
Dim foundfileNumbers = FindFileNumbers(textfile)
    For Each filenumber In foundfileNumbers
        getFileNumberData(filenumber.ToString, textfile)
        'Console.WriteLine(filenumber.ToString)
    Next
End Sub

    Public Function FindFileNumbers(ByVal textfile As String)
        Dim filereader As New System.IO.StreamReader(textfile)
    Dim pages As List(Of String) = New List(Of String)
    Do While filereader.Peek() <> -1

        Dim regexPattern = "TP[0-9]{6}"
        Dim reg = New Regex(regexPattern)

        Dim currenttext As String = Nothing

        Dim textline As String = filereader.ReadLine()
        currenttext = textline.Substring(0, 77)
        currenttext.IndexOf("", StringComparison.InvariantCultureIgnoreCase)
        Dim matches = reg.Matches(currenttext)

        For Each m In matches
            'Console.WriteLine(m.ToString)
            pages.Add(m.ToString)
        Next m

    Loop
    Return pages

End Function

This is where i get stuck:

    Public Function getFileNumberData(ByVal filenumber As String, ByVal textfile As String) As String
            Dim returnElement As String = Nothing
            Dim filereader As New System.IO.StreamReader(textfile)

            Do While filereader.Peek() <> -1

                Dim textline = filereader.ReadLine()
                If textline.Substring(0, 77).Contains(filenumber) Then
                Do While filereader.Peek() <> -1
                    Dim textline2 = filereader.ReadLine()
                    If textline2.Substring(0, 77).Contains("BIRTH DATE")Then
                        Dim textline3 = filereader.ReadLine()
                        returnElement = textline3.Substring(0, 77)
                        Console.WriteLine(returnElement)
                    Else
                        returnElement = "No DOB"
                        Console.WriteLine(returnElement)
                    End If

                Loop
            End If

        Loop
        Return returnElement
    End Function

So right there since it could not find "BIRTH DATE" it just gives me "No DOB" for every line that does not have it.....

Much Appreciated.

Body:
So after thoroughly going over this, i found a solution by using a regex pattern, finding a keyword and adding the correct number of indices to it so that the code can find the value directly beneath the regex pattern keyword.

My code also separates each file till its footer row and makes a separate PDF, so this code includes all of that.

Imports System.IO
Imports System.IO.Path
Imports Microsoft.Office.Interop.Excel
Imports Microsoft.Office.Interop
Imports System.Text.RegularExpressions
Imports System.Text
Imports iTextSharp.text.pdf
Imports iTextSharp.text
Module Module1
    Dim xlApp As New Excel.Application
    Dim xlWorkbook As Excel.Workbook
    Dim xlWorksheet1 As Excel.Worksheet
    Dim counter As Integer = 0
    Dim CBR = Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "CBR")
    Dim textfile = "C:\Users\username\DeskTop\CBR large data.txt"

    Dim TextFileFolder = Combine(CBR, "Text Files")
    Sub Main()
        If Not Directory.Exists(CBR) Then
            Directory.CreateDirectory(CBR)
        End If
        If Not Directory.Exists(TextFileFolder) Then
            Directory.CreateDirectory(TextFileFolder)
        End If
        Dim rmt = "C:\Users\xborja\DeskTop\CBR large data.txt"

        xlWorkbook = xlApp.Workbooks.Add()
        xlWorksheet1 = CType(xlWorkbook.Sheets("Sheet1"), Excel.Worksheet)
        xlWorksheet1.Cells(1, 1) = "FILENO"
        xlWorksheet1.Cells(1, 2) = "NUMBER"
        xlWorksheet1.Cells(1, 3) = "DOB"
        xlWorksheet1.Cells(1, 4) = "HIGH RISK"
        xlWorksheet1.Cells(1, 5) = "SCORE"
        xlWorksheet1.Cells(1, 6) = "PR"
        xlWorksheet1.Cells(1, 7) = "MTG"
        xlWorksheet1.Range("C:C").NumberFormat = "m/d/yyyy"

        Dim reader = File.OpenText(textfile)
        Dim builder = New StringBuilder(9000)
        'Allocate a reasonable size buffer to avoid mem allocs
        Dim line As String = reader.ReadLine
        Dim counter = 0
        While (Not (line) Is Nothing)
            counter += 1

            builder.AppendLine(line)
            If line.Contains("END OF TRANSUNION REPORT") Then

                CheckAndProcess(builder.ToString)

                builder.Clear()
            End If

            line = reader.ReadLine

        End While

        xlWorkbook.SaveAs(CBR & "/" & System.DateTime.Now.ToString("yyyyMMdd") & " CBR.xlsx")
        xlWorkbook.Close(True)
        Process.Start("explorer.exe", CBR)

        xlApp.Quit()
        releaseObject(xlApp)
        releaseObject(xlWorkbook)
        releaseObject(xlWorksheet1)
    End Sub
    Public Sub CheckAndProcess(ByVal page As String)
        Dim filenumber = Nothing
        Dim foundfileNumbers = FindFileNumbers(textfile)
        Dim BirthDateidx = Nothing
        Dim SSNidx = Nothing
        Dim ScoreIdx = Nothing
        Dim PRidx = Nothing
        Dim MTGidx = Nothing
        Dim DOB = Nothing
        Dim SSN = Nothing
        Dim Risk = Nothing
        Dim PR = Nothing
        Dim MTG = Nothing
        Dim Score = Nothing

        Dim pdfDoc As New Document()

        For Each filenumber In foundfileNumbers

            If page.Contains((filenumber)) Then
                Dim pdfWrite As PdfWriter = PdfWriter.GetInstance(pdfDoc, New FileStream("C:\Users\xborja\Desktop\CBR\Text Files\" & filenumber & ".pdf", FileMode.Create))
                pdfDoc.Open()
                pdfDoc.Add(New Paragraph(page))

                counter += 1
                xlWorksheet1.Cells(counter + 1, 1) = filenumber
                If Not page.Contains("BIRTH DATE") Then
                    Console.WriteLine("no DOB")
                    DOB = " "
                    xlWorksheet1.Cells(counter + 1, 3) = DOB

                Else
                    BirthDateidx = page.IndexOf("BIRTH DATE") + 78 'returning birth date index plus enough characters to go to next line
                    Console.WriteLine(page.Substring(BirthDateidx, 5).Replace(" ", "0"))
                    DOB = page.Substring(BirthDateidx, 5).Replace(" ", "0")
                    xlWorksheet1.Cells(counter + 1, 3) = DOB

                End If

                If Not page.Contains("SSN") Then
                    Console.WriteLine("no ssn")
                    SSN = " "
                    xlWorksheet1.Cells(counter + 1, 8) = SSN

                Else
                    SSNidx = page.IndexOf("SSN") + 78
                    Console.WriteLine(page.Substring(SSNidx, 11))
                    SSN = page.Substring(SSNidx, 11)
                    xlWorksheet1.Cells(counter + 1, 8) = SSN

                End If

                If page.Contains("CLEAR FOR ALL SEARCHES PERFORMED") Then
                    Risk = "N"
                    xlWorksheet1.Cells(counter + 1, 4) = Risk
                    Console.WriteLine("N")

                Else
                    Risk = "Y"
                    xlWorksheet1.Cells(counter + 1, 4) = Risk
                    Console.WriteLine("Y")

                End If
                If Not page.Contains("RECOVERY MODEL 1.0 SCORE") Then
                    Console.WriteLine("no score")
                    Score = " "
                    xlWorksheet1.Cells(counter + 1, 5) = Score
                Else
                    ScoreIdx = page.IndexOf("RECOVERY MODEL 1.0 SCORE") + 26
                    Console.WriteLine(page.Substring(ScoreIdx, 3))
                    Score = page.Substring(ScoreIdx, 3)
                    xlWorksheet1.Cells(counter + 1, 5) = Score

                End If

                If page.Contains("PR=") Then
                    PRidx = page.IndexOf("PR=") + 3
                    Console.WriteLine(page.Substring(PRidx, 1))
                    PR = page.Substring(PRidx, 1)
                    xlWorksheet1.Cells(counter + 1, 6) = PR

                Else
                    Console.WriteLine("NO PR")
                    PR = "0"
                    xlWorksheet1.Cells(counter + 1, 6) = PR

                End If

                If page.Contains("MTG=") Then
                    MTGidx = page.IndexOf("MTG=") + 4
                    Console.WriteLine(page.Substring(MTGidx, 1))
                    MTG = page.Substring(MTGidx, 1)
                    xlWorksheet1.Cells(counter + 1, 7) = MTG

                Else
                    Console.WriteLine("NO MTG")
                    MTG = "0"
                    xlWorksheet1.Cells(counter + 1, 7) = MTG

                End If

            End If

            File.AppendAllText("C:\Users\xborja\Desktop\CBR\Text Files\" & filenumber & ".txt", page)

        Next
        pdfDoc.Close()

    End Sub
    Public Function FindFileNumbers(ByVal textfile As String)
        Dim filereader As New System.IO.StreamReader(textfile)
        Dim pages As List(Of String) = New List(Of String)
        Do While filereader.Peek() <> -1

            Dim regexPattern = "TP[0-9]{6}"
            Dim reg = New Regex(regexPattern)

            Dim currenttext As String = Nothing

            Dim textline As String = filereader.ReadLine()
            currenttext = textline.Substring(0, 77)
            currenttext.IndexOf("", StringComparison.InvariantCultureIgnoreCase)
            Dim matches = reg.Matches(currenttext)

            For Each m In matches

                pages.Add(m.ToString)
            Next m

        Loop
        Return pages

    End Function
    Private Sub releaseObject(ByVal aCell As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(aCell)
            aCell = Nothing
        Catch ex As Exception
            aCell = Nothing
        Finally
            GC.Collect()
        End Try
    End Sub

End Module


问:
Are there any ways other than authentication to tell the users apart?
In my app, the user can select a Youtube video that will be downloaded to MEDIA_ROOT folder and then also made available for the user to download.

Whenever the user chooses another video to download, the previous one is deleted from MEDIA_ROOT. So at any given moment there is only one video sitting in the MEDIA_ROOT folder for a particular user.

Is there any way - apart from implementing user authentication and associating the downloaded files with a user through foreign key, which I feel is an overkill for only this task - of telling the users apart whenever such download request is being made, so that one user's request does not cause the deletion of the file downloaded by some other user (as all the files sit in the same MEDIA_ROOT folder)?

答: Assuming you have some sort of web server, you can create custom links that redirect through your web server and pass IP information, etc, so you can distinguish a user before one downloads a video. That is certainly one way of doing it without authentication and since the app/platform that tracks user data is in-house you don't have to worry about foreign keys, etc.</p>


问:
How to compare two @ character in clojure
Please, how can I write this in clojure? I have an @ character and I want to compare it to "@".

eg (= @ "@")

gives error (ClassCastException java.lang.String cannot be cast to java.util.concurrent.Future  clojure.core/deref-future (core.clj:2206))

答:
There's an inherent conflict in this comparison, in that "@" is a string sequence of characters and \@ is an individual character.  If you wanted to confirm that the string consisted of a single character which matched the \@ symbol, something like the following would be work:

```<code>(let [s "@"]
  (and (= \@ (first s)) (= (count s) 1)))
</code>```

However, if you want to detect if the string contains any \@ characters or if it just started with an \@ character, that requires different code.  This is the problem with comparing strings and characters -- it's not inherently obvious what you need from the comparison.

Body:
There's an inherent conflict in this comparison, in that "@" is a string sequence of characters and \@ is an individual character.  If you wanted to confirm that the string consisted of a single character which matched the \@ symbol, something like the following would be work:

(let [s "@"]
  (and (= \@ (first s)) (= (count s) 1)))

However, if you want to detect if the string contains any \@ characters or if it just started with an \@ character, that requires different code.  This is the problem with comparing strings and characters -- it's not inherently obvious what you need from the comparison.


问:
Checking that a filename matches a pattern within a tar --to-command
I have a TGZ file (compressed with GZIP) that contains a wide variety of other compressed files, some binary, some ascii text.  I would like to grep through the compressed ascii text files looking for string matches and print the filename.  Some research has brought me to the tar --to-command, which seems to almost do exactly what I want when executed with zgrep, however, I cannot filter out which file names to search for so task takes prohibitively long and prints out some garbage along the way.

This does what I want except it examines all files in the archive:

```<code>tar xaPf log.tgz --to-command='zgrep -l "mystring" && echo $TAR_FILENAME || true'
</code>```

This was my best guess at how to do it:

```<code>tar xaPf log.tgz --to-command='if [[ $TAR_FILENAME *asciiFile.gz ]]; then zgrep 'mystring'; fi'
</code>```

But all I get is this:

```<code>/bin/sh: 1: [[: not found
</code>```

For every file.  From what I have read, the arguments for the --to-command do not run in a full bash shell, so what conditional statement can I do so that I only evaluate files of a given suffix?  Thanks

Update:
Realizing that --to-command only executes /bin/sh shell commands, I revised my command:

```<code>tar xaPf log.tgz --to-command='[ "$TAR_FILENAME" = "*asciiFile.gz" ] && zgrep "mystring" || true'
</code>```

So now it execute without errors, but the conditional expression is never true.  How can I check for a pattern in /bin/sh?

答:
Here it is:</p>

```<code>tar xaPf log.tgz --to-command='echo "$TAR_FILENAME" | grep -q "asciiFile.gz" && zgrep "mystring" || true'
</code>```

Body:
I have a TGZ file (compressed with GZIP) that contains a wide variety of other compressed files, some binary, some ascii text.  I would like to grep through the compressed ascii text files looking for string matches and print the filename.  Some research has brought me to the tar --to-command, which seems to almost do exactly what I want when executed with zgrep, however, I cannot filter out which file names to search for so task takes prohibitively long and prints out some garbage along the way.

This does what I want except it examines all files in the archive:

tar xaPf log.tgz --to-command='zgrep -l "mystring" && echo $TAR_FILENAME || true'

This was my best guess at how to do it:

tar xaPf log.tgz --to-command='if [[ $TAR_FILENAME *asciiFile.gz ]]; then zgrep 'mystring'; fi'

But all I get is this:

/bin/sh: 1: [[: not found

For every file.  From what I have read, the arguments for the --to-command do not run in a full bash shell, so what conditional statement can I do so that I only evaluate files of a given suffix?  Thanks

Update:
Realizing that --to-command only executes /bin/sh shell commands, I revised my command:

tar xaPf log.tgz --to-command='[ "$TAR_FILENAME" = "*asciiFile.gz" ] && zgrep "mystring" || true'

So now it execute without errors, but the conditional expression is never true.  How can I check for a pattern in /bin/sh?

Body:
Here it is:

tar xaPf log.tgz --to-command='echo "$TAR_FILENAME" | grep -q "asciiFile.gz" && zgrep "mystring" || true'


答:
Have you tried DISTINCT ?

```<code>$query = "SELECT DISTINCT year(date) FROM `press_release` WHERE del_status=0";
</code>```

Title: Remove duplicate data from dynamic select option of HTMl form

Body:
Have you tried DISTINCT ?

$query = "SELECT DISTINCT year(date) FROM `press_release` WHERE del_status=0";


问:
how do i set a dynamic object in JS?
I want to keep a value from a form by a js function
with document.getElementById("form1")

but in the form there are dynamic inputs amount1 , amount2 ect... (i dont know how many - its from database)

how do i reach form1.amount (p)

when p is the index of the amount ?

thanks

答:
You can get all input elements of a form by use of "getElementsByTagName". Like this:

```<code>var form = document.getElementById("form1");
var inputs = form.getElementsByTagName("input");
</code>```

That way the array "inputs" contains all input elements contained in your form.

ContentLicense: CC BY-SA 3.0

Title: how do i set a dynamic object in JS?

Body:
You can get all input elements of a form by use of "getElementsByTagName". Like this:

var form = document.getElementById("form1");
var inputs = form.getElementsByTagName("input");

That way the array "inputs" contains all input elements contained in your form.


问:
How do I add padding between shapes in kivy?
I'm creating multiple shapes automatically, but I have a feeling that the shapes are just being overlapped by each other. I want to be able to add padding to the shapes so that this is not a problem.

Code:

```<code>...

with open("streak.json", "r+") as f:
                    data = json.load(f)

                get_score = data.get(key, {}).get('score')

                for x in range(get_score):
                    self.update_canvas()

    def update_canvas(self):
        can = self.root.get_screen("three")

        with can.ids.my_box.canvas.before:
            Color(0,0,0,1)
            Line(width=5)
            Rectangle(pos=can.pos, size=(30,30))
        with can.ids.my_box.canvas:
            Color(0, 1, 0, .95, mode='rgba')
            Rectangle(pos=can.pos, size=(30,30))
</code>```

EDIT

This question has been answered How do I create multiple shapes relative to each other in kivy?

答:
Rectangle(pos=can.pos, size=(30,30))

Your Rectangles all have the same position because that's exactly what you set for them. To give them different positions, just pass different values for the pos argument.

Body:
I'm creating multiple shapes automatically, but I have a feeling that the shapes are just being overlapped by each other. I want to be able to add padding to the shapes so that this is not a problem.

Code:

...

with open("streak.json", "r+") as f:
                    data = json.load(f)

                get_score = data.get(key, {}).get('score')

                for x in range(get_score):
                    self.update_canvas()

    def update_canvas(self):
        can = self.root.get_screen("three")

        with can.ids.my_box.canvas.before:
            Color(0,0,0,1)
            Line(width=5)
            Rectangle(pos=can.pos, size=(30,30))
        with can.ids.my_box.canvas:
            Color(0, 1, 0, .95, mode='rgba')
            Rectangle(pos=can.pos, size=(30,30))

EDIT

This question has been answered How do I create multiple shapes relative to each other in kivy?


问:
Is there a function in pandas to convert timestamps with CST as the time zone?
I have a data frame which has the time_zone and the date in different columns. I want to get the local time corresponding to the time_zone.

I have the data frame as below:

df = pd.DataFrame({'CREATED_DT':['2017-01-01 20:24:21','2017-01-01 21:10:54','2017-01-02 11:48:12','2017-01-02 19:30:53','2017-01-02 21:06:55'],
'Time_Zone':['EST','EST','CET','EST','CST']})

df['CREATED_DT'] = pd.to_datetime(df['CREATED_DT']).dt.tz_localize('UTC')

I have converted the created date to UTC and the time_zone is in a different column. I want to get the local time corresponding to the timezone. So the code for it is :

df['Local_Time'] = df.apply(lambda x: x['CREATED_DT'].tz_convert(x['timezone']), axis = 1)

Which works fine in case the time_zone is EST,CET but gives an error for CST which says:

UnknownTimeZoneError: ('CST', u'occurred at index 4')

I am not sure if there is a way to handle this other than hard coding ... please suggest if there is any other library or function that could be used?

答:
The reason why CST as a timezone throws an error is because it can refer to 3 different timezones: Central Standard Time, which is North America's Central Time Zone (UTC -0600), China Standard Time (UTC +0800) and Cuba Standard Time (UTC -0400). I'm assuming you want to use Central Standard Time.

An easy way to converge time, is the use the pytz library, which has a constantly updated database of most timezones.

In your case, the line that converts the date to the timezone could be changed to this line:

import pytz
df['Local_Time'] = df.apply(lambda x: pytz.timezone("US/Central" if x['Time_Zone'] == 'CST' else x['Time_Zone']).normalize(x['CREATED_DT']), axis = 1)
```

(sidenote: in pytz, Central Standard Time is 'US/Central', China Standard Time is 'Asia/Shanghai' and Cuba Standard Time is 'Cuba'. For any other times, you can check this stackoverflow question or this wikipedia page)

It might also make your code a bit more readable if you made it a function instead of a lambda at this point though.

Body:
The reason why CST as a timezone throws an error is because it can refer to 3 different timezones: Central Standard Time, which is North America's Central Time Zone (UTC -0600), China Standard Time (UTC +0800) and Cuba Standard Time (UTC -0400). I'm assuming you want to use Central Standard Time.

An easy way to converge time, is the use the pytz library, which has a constantly updated database of most timezones.

In your case, the line that converts the date to the timezone could be changed to this line:

import pytz
df['Local_Time'] = df.apply(lambda x: pytz.timezone("US/Central" if x['Time_Zone'] == 'CST' else x['Time_Zone']).normalize(x['CREATED_DT']), axis = 1)

(sidenote: in pytz, Central Standard Time is 'US/Central', China Standard Time is 'Asia/Shanghai' and Cuba Standard Time is 'Cuba'. For any other times, you can check this stackoverflow question or this wikipedia page)

It might also make your code a bit more readable if you made it a function instead of a lambda at this point though.


问:
Boggle type word checker using jquery
I am trying to create a variation of a Boggle type word checker using jQuery. The variation is that a letter can be used more than once and the board is always two lines.

I have been using a string divided in half as the 'board', and using a recursive function to search for a given word. I have looked at similar examples but cannot seem to code the function correctly.

For example, if the board string is OMRAWNLET. The board would look like:

```<code>O M R A W
N L E T
</code>```

It would return true if the word to search for is watermelon.  Can anyone please guide me or write the function? Thanks.

答:
I would use a two dimentional array for the board and an array of letters for the word.
Here's a quick prototype:

var isWordOnTheBoard = function(board, word) {
    var passedTest = true;
    var previousLetterCoordinates;

    //written as a loop; rewrite to use recursion
    for (let letter of word) {
        // console.log('current letter is ' + letter);
        var letterCoordinates = getCoordinatesOnTheBoard(board, letter);
        if(!letterCoordinates){
            passedTest = false;
            console.log('letter ' + letter + ' is not on the board');
            break;
        }
        if(previousLetterCoordinates && !areNeighbors(previousLetterCoordinates, letterCoordinates)){
            passedTest = false;
            console.log('letter ' + letter + ' is not a neighbor to the previous letter');
            break;
        }
        previousLetterCoordinates = letterCoordinates;
    }
    return passedTest;
};

//assumes letters on the board are unique with no repeated letters
var getCoordinatesOnTheBoard = function(board, letter) {
    //undefined means letter is not on the board
    var coordinates;

    var firstRowIndex = board[0].indexOf(letter);
    if(firstRowIndex >= 0){
        coordinates = {
            row: 0,
            column: firstRowIndex
        };
    }

    var secondRowIndex = board[1].indexOf(letter);
    if(secondRowIndex >= 0){
        coordinates = {
            row: 1,
            column: secondRowIndex
        };
    }

    return coordinates;
}
//neighbors of a:
// | NW | N | NE |
// | W  | a | E  |
// | SW | S | SE |
// where NW is North West, N is North, NE is North East and so on

var areNeighbors = function(a, b) {
    var nw_of_a = {
        row: a.row - 1,
        column: a.column - 1
    }
    if (JSON.stringify(b) === JSON.stringify(nw_of_a)){return true}
    var n_of_a = {
        row: a.row - 1,
        column: a.column
    }
    if (JSON.stringify(b) === JSON.stringify(n_of_a)){return true}

    var ne_of_a = {
        row: a.row - 1,
        column: a.column + 1
    }
    if (JSON.stringify(b) === JSON.stringify(ne_of_a)){return true}

    var e_of_a = {
        row: a.row,
        column: a.column + 1
    }
    if (JSON.stringify(b) === JSON.stringify(e_of_a)){return true}

    var se_of_a = {
        row: a.row + 1,
        column: a.column + 1
    }
    if (JSON.stringify(b) === JSON.stringify(se_of_a)){return true}

    var s_of_a = {
        row: a.row + 1,
        column: a.column
    }
    if (JSON.stringify(b) === JSON.stringify(s_of_a)){return true}

    var sw_of_a = {
        row: a.row + 1,
        column: a.column - 1
    }
    if (JSON.stringify(b) === JSON.stringify(sw_of_a)){return true}

    var w_of_a = {
        row: a.row,
        column: a.column - 1
    }
    if (JSON.stringify(b) === JSON.stringify(w_of_a)){return true}

    return false;
}

var omraw_nlet = [
    ['O','M','R','A','W'],
    ['N','L','E','T']
];
var watermelon = ['W', 'A', 'T', 'E', 'R', 'M', 'E', 'L', 'O', 'N'];
var lemon = ['L', 'E', 'M', 'O', 'N'];

console.log(isWordOnTheBoard(omraw_nlet, watermelon)); //true
console.log(isWordOnTheBoard(omraw_nlet, ['W', 'A', 'R'])); //true
console.log(isWordOnTheBoard(omraw_nlet, lemon)); //true
console.log(isWordOnTheBoard(omraw_nlet, lemon.concat(['S']))); //false
console.log(isWordOnTheBoard(omraw_nlet, ['T', 'E', 'N'])); //false```

Title: Boggle type word checker using jquery

Body:
I am trying to create a variation of a Boggle type word checker using jQuery. The variation is that a letter can be used more than once and the board is always two lines.

I have been using a string divided in half as the 'board', and using a recursive function to search for a given word. I have looked at similar examples but cannot seem to code the function correctly.

For example, if the board string is OMRAWNLET. The board would look like:

O M R A W
N L E T

It would return true if the word to search for is watermelon.  Can anyone please guide me or write the function? Thanks.


问:
HTTP Error 404: Not Found - Post request not working
Im trying to use postcode.io to get longitude and latitude from a bunch of postcodes by using the "Bulk lookup postcodes" that they have shown on the website https://postcodes.io/.

Im trying to pass a json object to the api point as they've shown in the documentation but keep getting the error shown in the title.

Here is my code;

```<code>allpostcodes = [i['postcode'] for i in _sortedstores()] # Getting postcodes from json file

data = {}
data['postcodes'] = allpostcodes # creating a dictionary and keeping `postcodes` and key and the postcode's as the value as shown in the example

_postcodearray = 'https://api.postcodes.io/%s' % json.dumps(data) # creating a json object and passing in the data
#_postcodearray = 'https://api.postcodes.io/%s' % data['postcodes'] #Not working

try:
    with urllib.request.urlopen(_postcodearray) as _postcodearray:
        a = _postcodearray.read()
        a = json.loads(a.decode('utf-8'))
        print(a)
except Exception as e:
    print(e)
</code>```

How my data varible looks before i use json.dump on it {'postcodes': ['GU34 2QS', 'TN23 7DH', 'HP20 1DH']}

I have also tried to regularly pass an array which resulted in the same error. Any help/suggestion on what i could be doing wrong would be appreciated.

答:
```<code>#!/usr/bin/env python3

import json
import urllib.request

allpostcodes = ["OX49 5NU", "M32 0JG", "NE30 1DP"]
#allpostcodes = [i['postcode'] for i in sortedstores] # Getting postcodes from json file

values = {}
# creating a dictionary and keeping `postcodes` and key and
# the postcode's as the value as shown in the example
values['postcodes'] = allpostcodes

api_endpoint = 'https://api.postcodes.io/postcodes/'
data = json.dumps(values).encode('utf8')
req = urllib.request.Request(api_endpoint, data,
        headers={'content-type': 'application/json'})
try:
    with urllib.request.urlopen(req) as response:
        result = json.loads(response.read())
        print(json.dumps(result, indent=2))
except Exception as e:
    print(e)
</code>```

Body:
Im trying to use postcode.io to get longitude and latitude from a bunch of postcodes by using the "Bulk lookup postcodes" that they have shown on the website https://postcodes.io/.

Im trying to pass a json object to the api point as they've shown in the documentation but keep getting the error shown in the title.

Here is my code;

allpostcodes = [i['postcode'] for i in _sortedstores()] # Getting postcodes from json file

data = {}
data['postcodes'] = allpostcodes # creating a dictionary and keeping `postcodes` and key and the postcode's as the value as shown in the example

_postcodearray = 'https://api.postcodes.io/%s' % json.dumps(data) # creating a json object and passing in the data
#_postcodearray = 'https://api.postcodes.io/%s' % data['postcodes'] #Not working

try:
    with urllib.request.urlopen(_postcodearray) as _postcodearray:
        a = _postcodearray.read()
        a = json.loads(a.decode('utf-8'))
        print(a)
except Exception as e:
    print(e)

How my data varible looks before i use json.dump on it {'postcodes': ['GU34 2QS', 'TN23 7DH', 'HP20 1DH']}

I have also tried to regularly pass an array which resulted in the same error. Any help/suggestion on what i could be doing wrong would be appreciated.


问:
Reset screen point to the top of screen in Windows & Linux console
I have a little routine that's run under Linux and Windows written in C and displays output on the console. I'm not linking in any form of curses or anything like that.

Currently I clear the screen using

```<code>#ifdef __WIN32
  system( "cls" );
#else
  system( "clear" );
#endif
</code>```

Then I have a bunch of printf statements to update the status. What I'd like just reset the screenpointer to 0,0 so I can then just overlay my printfs. I'd rather avoid compiling in any more extensions especially since I'm coding for 2 different OS'.

答: For Unix-like platforms, the usual way to do this is using the curses library.</p>

OwnerDisplayName: Paul Hargreaves

ContentLicense: CC BY-SA 2.5

Body:
I have a little routine that's run under Linux and Windows written in C and displays output on the console. I'm not linking in any form of curses or anything like that.

Currently I clear the screen using

#ifdef __WIN32
  system( "cls" );
#else
  system( "clear" );
#endif

Then I have a bunch of printf statements to update the status. What I'd like just reset the screenpointer to 0,0 so I can then just overlay my printfs. I'd rather avoid compiling in any more extensions especially since I'm coding for 2 different OS'.

OwnerDisplayName: Greg Hewgill


问:
Why does ps only return one line of output in my Perl script when I call it with Nagios?
I have this running:

```<code>if (open(PS_ELF, "/bin/ps -eLf|")) {
  while (<PS_ELF>) {
    if ($_ =~ m/some regex/) {
      # do some stuff
    }
  }
}
</code>```

If called locally, the loop runs just fine, once for every output line of ps -eLf

Now if the same script is called from Nagios via NRPE, PS_ELF does only contain one line (the first line output by ps).

This puzzles me; what could be the reason?

Maybe this is not limited to/caused by Nagios at all, I just included it for the sake of completeness.

I'm on SUSE Enterprise Linux 10 SP2 and perl v5.8.8.

答:
Although this problem is very old, I experienced the exact same problem today.
So I thought I share what I found.
The problem is that processes created by the NRPE daemon (can) have a different environment than processes you execute directly in the shell as the NRPE daemon user.

I created the following bash script:

```<code>#!/bin/bash
echo `env | grep COLUMNS`
</code>```

This gives me the environment variable COLUMN of the current process, which has the same environment as the parent process (the process forked by the NRPE daemon).

When I execute this script as the NRPE daemon user

```<code>$ /tmp/check_env.sh
COLUMNS=174
</code>```

it gives me the value of my current shell window.
But when I execute this script via NRPE, I get:

```<code>nagios-server $ check_nrpe -H client -c check_env
COLUMNS=80
</code>```

Which is why ps -eaf output is limited to 80 characters unless you use the ww parameter for unlimited width, which ignores the COLUMNS environment variable.

OwnerDisplayName: Node

Body:
I have this running:

if (open(PS_ELF, "/bin/ps -eLf|")) {
  while (<PS_ELF>) {
    if ($_ =~ m/some regex/) {
      # do some stuff
    }
  }
}

If called locally, the loop runs just fine, once for every output line of ps -eLf

Now if the same script is called from Nagios via NRPE, PS_ELF does only contain one line (the first line output by ps).

This puzzles me; what could be the reason?

Maybe this is not limited to/caused by Nagios at all, I just included it for the sake of completeness.

I'm on SUSE Enterprise Linux 10 SP2 and perl v5.8.8.

LastEditorDisplayName: brian d foy

Body:
Although this problem is very old, I experienced the exact same problem today.
So I thought I share what I found.
The problem is that processes created by the NRPE daemon (can) have a different environment than processes you execute directly in the shell as the NRPE daemon user.

I created the following bash script:

#!/bin/bash
echo `env | grep COLUMNS`

This gives me the environment variable COLUMN of the current process, which has the same environment as the parent process (the process forked by the NRPE daemon).

When I execute this script as the NRPE daemon user

$ /tmp/check_env.sh
COLUMNS=174

it gives me the value of my current shell window.
But when I execute this script via NRPE, I get:

nagios-server $ check_nrpe -H client -c check_env
COLUMNS=80

Which is why ps -eaf output is limited to 80 characters unless you use the ww parameter for unlimited width, which ignores the COLUMNS environment variable.


问:
Initializing a char* and std::string with bytes
Is the following implementation-defined:

```<code>char *cp = "\x96\xA0\xB4\xBE\xC8";
</code>```

and as well as:

```<code>std::string = "\x96\xA0\xB4\xBE\xC8";
</code>```

The char means signed char on my compiler (MSVC 2015).

I figured that I can't do the following:

```<code>unsigned char *cp = "\x96\xA0\xB4\xBE\xC8";
</code>```

"\x96\xA0\xB4\xBE\xC8" are bytes in range 0 - 255, hence my question is:

Does the above depend on the compiler?

答:
Is the following implementation-defined:

```<code>signed char *cp = "\x96\xA0\xB4\xBE\xC8";
</code>```

and as well as:

```<code>std::string = "\x96\xA0\xB4\xBE\xC8";
</code>```

On systems with 8-bits wide signed char, yes. A hex escape sequence in a narrow string literal has an implementation-defined value if it falls outside of the implementation-defined range defined for char. Assuming 8-bit signed char, any hex value greater than 7F is outside the range of representable values.

Whether that literal is used to initialise a std::string or a pointer to character is irrelevant in this regard.

You can use an array of unsigned char instead of a string literal:

```<code>static constexpr unsigned char cp[] = {
    0x96,
    0xA0,
    0xB4,
    0xC8,
};
</code>```

You can use this array to initialise a std::basic_string<unsigned char> if you need it:

```<code> std::basic_string<unsigned char> s = {std::begin(cp), std::end(cp)};
</code>```

P.S. Conversion from string literal to non-const char pointer is ill-formed (since C++11; prior the conversion was well-formed but deprecated).

P.P.S char, unsigned char and signed char are always three distinct types whether char is signed or not.

Body:
Is the following implementation-defined:

char *cp = "\x96\xA0\xB4\xBE\xC8";

and as well as:

std::string = "\x96\xA0\xB4\xBE\xC8";

The char means signed char on my compiler (MSVC 2015).

I figured that I can't do the following:

unsigned char *cp = "\x96\xA0\xB4\xBE\xC8";

"\x96\xA0\xB4\xBE\xC8" are bytes in range 0 - 255, hence my question is:

Does the above depend on the compiler?

Body:
Is the following implementation-defined:

signed char *cp = "\x96\xA0\xB4\xBE\xC8";

and as well as:

std::string = "\x96\xA0\xB4\xBE\xC8";

On systems with 8-bits wide signed char, yes. A hex escape sequence in a narrow string literal has an implementation-defined value if it falls outside of the implementation-defined range defined for char. Assuming 8-bit signed char, any hex value greater than 7F is outside the range of representable values.

Whether that literal is used to initialise a std::string or a pointer to character is irrelevant in this regard.

You can use an array of unsigned char instead of a string literal:

static constexpr unsigned char cp[] = {
    0x96,
    0xA0,
    0xB4,
    0xC8,
};

You can use this array to initialise a std::basic_string<unsigned char> if you need it:

 std::basic_string<unsigned char> s = {std::begin(cp), std::end(cp)};

P.S. Conversion from string literal to non-const char pointer is ill-formed (since C++11; prior the conversion was well-formed but deprecated).

P.P.S char, unsigned char and signed char are always three distinct types whether char is signed or not.


问:
Angular 6 pass data to component on router menu click
I have a navmenu component as under:

```<code>     <li class="dropdown">
        <a href="#" class="dropdown-toggle" data-toggle="dropdown"><span class='glyphicon glyphicon-wrench'></span> Configuration<span class="caret"></span></a>
        <ul class="dropdown-menu" role="menu">
            <li class="dropdown-header">Options</li>
            <li [routerLinkActive]="['link-active']">
                <a [routerLink]="['config']" [queryParams]="{optionFilter: 'LOB' }">
                    <span class='glyphicon glyphicon-arrow-right'></span> Line of Business
                </a>
            </li>
            <li [routerLinkActive]="['link-active']">
                <a [routerLink]="['config']" [queryParams]="{optionFilter: 'RG' }">
                    <span class='glyphicon glyphicon-arrow-right'></span> Report Groups
                </a>
            </li>
        </ul>
    </li>
</code>```

There are dropdown menus under Configuration. All menus lead to the same component 'config'. However, based on the routerLink selected I need to write logic. How can I pass the param 'Line of Business', or 'Report Group' to the config component and retrieve in ngOnInit?

答:
You can pass that in query parameters:

```<code><a [routerLink]="['config'] [queryParams]="{ someProperty: 'ReportGroup' }">
</code>```

Inject ActivatedRoute in the component's constructor, and subscribe queryParams or queryParamsMap.

Or

You can make it two separate routes, pointing to the same component. Again, inject ActivatedRoute, but subscribe route params instead of query params.

Body:
I have a navmenu component as under:

         Configuration

Options

                     Line of Business

                     Report Groups

There are dropdown menus under Configuration. All menus lead to the same component 'config'. However, based on the routerLink selected I need to write logic. How can I pass the param 'Line of Business', or 'Report Group' to the config component and retrieve in ngOnInit?

Body:
You can pass that in query parameters:

<a [routerLink]="['config'] [queryParams]="{ someProperty: 'ReportGroup' }">

Inject ActivatedRoute in the component's constructor, and subscribe queryParams or queryParamsMap.

Or

You can make it two separate routes, pointing to the same component. Again, inject ActivatedRoute, but subscribe route params instead of query params.


问:
How do I checkout a branch added after a Github clone?
I have cloned a Github repository with the "--recursive" qualifier.  I then checked out the latest branch in that repository.

Later, the author of the repository added a new branch.  When I attempted to checkout that new branch with git checkout branch-name, git reported that the branch-name was unknown.

Is there a way to get the new branch-name without again cloning the repository?

答:
Use git fetch to retrieve new work done by other people including the newly added branch.

Fetching from a repository grabs all the new remote-tracking branches and tags without merging those changes into your own branches.

If you already have a local repository with a remote URL set up for the desired project, you can grab all the new information by using git fetch remotename

After fetching you can list all the branches including the newly added one by git branch -a

Then, you can select the required branch and checkout by git checkout requiredbranch-name


问:
Pandas Pivot Table Weighted Average of Numerical Column Name and Numerical Row Index
How does one calculate the "weighted average axis" value for each axis of "pot" and "graft", weighted by the "rez" values (summed) which make up the belly of the Pivot Table? In this case the row index and column names are not strings, but numbers.

```<code>import pandas as pd
import numpy as np
import random

# CREATE DATA
pot = np.linspace(0,80,num=9)
graft = np.linspace(0.30,0.80,num=11)

pots = pd.Series([])
grafts = pd.Series([])
rezs = pd.Series([])

for i in range (1,5):
    for pott in pot:
        for graftt in graft:
            rez = random.uniform(-2,4)
            rez= round(rez, 2)
            pots = pots.append(pd.Series(pott))
            grafts = grafts.append(pd.Series(graftt))
            rezs = rezs.append(pd.Series(rez))

# CREATE DATA FRAME
df = pd.DataFrame({'pot' : pots, 'graft' : grafts, 'rez': rezs })

# CREATE PIVOT TABLE
pivot = pd.pivot_table(data = df, values = 'rez', index = ['pot'], columns = ['graft'], aggfunc = np.sum)

# REPLACE NEGATIVE VALUES WITH ZEROES
pivot[pivot < 0] = 0

# Calculate Weights
weights = pivot / (pivot.sum().sum())
pivotsum = pivot.sum().sum()
</code>```

Pivot Table:

```<code>graft  0.30   0.35  0.40  0.45   0.50   0.55  0.60  0.65  0.70  0.75   0.80
pot
0.0    5.66   0.37  3.92  4.99   1.38   7.89  7.04  3.83  3.88  5.48   4.08
10.0   5.44   4.34  2.26  4.55   4.89   4.50  2.07  3.94  2.66  3.77  11.26
20.0   0.00   4.47  7.15  1.20   4.55  11.40  3.33  0.41  2.34  4.20   7.17
30.0   2.88   1.88  8.55  4.60   4.07  13.58  6.06  3.79  9.25  4.21   1.63
40.0   5.06   0.63  4.20  6.68   5.15   7.93  2.03  7.92  6.94  0.00   1.99
50.0   6.57  11.63  3.80  6.69   6.74   5.71  3.48  4.48  0.00  3.20   4.18
60.0   6.46   5.69  5.72  0.00  13.71   5.03  3.82  9.91  4.02  1.12   1.81
70.0   0.82   8.50  4.79  3.82   1.50   5.66  2.57  0.00  6.91  6.12   4.55
80.0   8.90   4.58  5.01  3.47   4.42   0.08  4.63  0.00  2.77  0.96   3.30
</code>```

I would like to calculate the weighted average "graft" and "pot", weighted by the (summed) "rez" values which make up the belly of the dataframe.

I've gotten as far as to create weights but do not know how to proceed in accessing the indices and column names to calculate the weighted average.

Actual data will not be uniformly distributed.

Desired output:

Weighted Average Graft = (0.30*5.66/pivotsum) + (0.30* 5.44/pivotsum) + ...
+ (0.80*3.30/pivotsum)

Weighted Average Pot = (0 * 5.66/pivotsum) + (10*5.44/pivotsum) + ... +(80*3.30/pivotsum)

These two weighted averages together then describe a "point" in the pivot table which represents where the pivot table would balance.

答:
Pandas.dataframe.reset_index and pandas.melt work. We can eliminate the last two lines in the original code.  We collapse the pivot table multi index and then melt the remaining dataframe columns while preserving one as the id variable for the melt method.  We can then calculate the weighted averages on the fully melted pivot table.  There may be a more direct solution out there, but this does get to the right numbers.</p>

```<code>import pandas as pd
import numpy as np
import random

# CREATE DATA
pot = np.linspace(0,80,num=9)
graft = np.linspace(0.30,0.80,num=11)

pots = pd.Series([])
grafts = pd.Series([])
rezs = pd.Series([])

for i in range (1,5):
    for pott in pot:
        for graftt in graft:
            rez = random.uniform(-2,4)
            rez= round(rez, 2)
            pots = pots.append(pd.Series(pott))
            grafts = grafts.append(pd.Series(graftt))
            rezs = rezs.append(pd.Series(rez))

# CREATE DATA FRAME
df = pd.DataFrame({'pot' : pots, 'graft' : grafts, 'rez': rezs })

# CREATE PIVOT TABLE
pivot = pd.pivot_table(data = df, values = 'rez', index = ['pot'], columns = ['graft'], aggfunc = np.sum)

# PRINT OUT INITIAL PIVOT TABLE
print("\nOriginal Pivot:\n",pivot)

# REPLACE NEGATIVE VALUES WITH ZEROES
pivot[pivot < 0] = 0
print("\nPost negative filtered Pivot:\n",pivot)

# RESET INDEX ON PIVOT TABLE
unpack = pivot.reset_index()

# MELT DATAFRAME COLUMNS
unpack = pd.melt(unpack, id_vars=['pot'], value_name='Rez')

# CALCULATE AND DISPLAY WEIGHTED AVERAGES
totrez = unpack['Rez'].sum()
unpack['Weight'] = unpack['Rez'] / totrez
wavpot = (unpack['pot'] * unpack['Weight']).sum()
wavpot = round(wavpot, 2)
print("\nWeighted Average pot:", wavpot)
wavgraft = (unpack['graft'] * unpack['Weight']).sum()
wavgraft = round(wavgraft, 2)
print("Weighted Average graft:", wavgraft)
</code>```

Body:
How does one calculate the "weighted average axis" value for each axis of "pot" and "graft", weighted by the "rez" values (summed) which make up the belly of the Pivot Table? In this case the row index and column names are not strings, but numbers.

import pandas as pd
import numpy as np
import random

# CREATE DATA
pot = np.linspace(0,80,num=9)
graft = np.linspace(0.30,0.80,num=11)

pots = pd.Series([])
grafts = pd.Series([])
rezs = pd.Series([])

for i in range (1,5):
    for pott in pot:
        for graftt in graft:
            rez = random.uniform(-2,4)
            rez= round(rez, 2)
            pots = pots.append(pd.Series(pott))
            grafts = grafts.append(pd.Series(graftt))
            rezs = rezs.append(pd.Series(rez))

# CREATE DATA FRAME
df = pd.DataFrame({'pot' : pots, 'graft' : grafts, 'rez': rezs })

# CREATE PIVOT TABLE
pivot = pd.pivot_table(data = df, values = 'rez', index = ['pot'], columns = ['graft'], aggfunc = np.sum)

# REPLACE NEGATIVE VALUES WITH ZEROES
pivot[pivot < 0] = 0

# Calculate Weights
weights = pivot / (pivot.sum().sum())
pivotsum = pivot.sum().sum()

Pivot Table:

graft  0.30   0.35  0.40  0.45   0.50   0.55  0.60  0.65  0.70  0.75   0.80
pot
0.0    5.66   0.37  3.92  4.99   1.38   7.89  7.04  3.83  3.88  5.48   4.08
10.0   5.44   4.34  2.26  4.55   4.89   4.50  2.07  3.94  2.66  3.77  11.26
20.0   0.00   4.47  7.15  1.20   4.55  11.40  3.33  0.41  2.34  4.20   7.17
30.0   2.88   1.88  8.55  4.60   4.07  13.58  6.06  3.79  9.25  4.21   1.63
40.0   5.06   0.63  4.20  6.68   5.15   7.93  2.03  7.92  6.94  0.00   1.99
50.0   6.57  11.63  3.80  6.69   6.74   5.71  3.48  4.48  0.00  3.20   4.18
60.0   6.46   5.69  5.72  0.00  13.71   5.03  3.82  9.91  4.02  1.12   1.81
70.0   0.82   8.50  4.79  3.82   1.50   5.66  2.57  0.00  6.91  6.12   4.55
80.0   8.90   4.58  5.01  3.47   4.42   0.08  4.63  0.00  2.77  0.96   3.30

I would like to calculate the weighted average "graft" and "pot", weighted by the (summed) "rez" values which make up the belly of the dataframe.

I've gotten as far as to create weights but do not know how to proceed in accessing the indices and column names to calculate the weighted average.

Actual data will not be uniformly distributed.

Desired output:

Weighted Average Graft = (0.30*5.66/pivotsum) + (0.30* 5.44/pivotsum) + ...
+ (0.80*3.30/pivotsum)

Weighted Average Pot = (0 * 5.66/pivotsum) + (10*5.44/pivotsum) + ... +(80*3.30/pivotsum)

These two weighted averages together then describe a "point" in the pivot table which represents where the pivot table would balance.

Body:
Pandas.dataframe.reset_index and pandas.melt work. We can eliminate the last two lines in the original code.  We collapse the pivot table multi index and then melt the remaining dataframe columns while preserving one as the id variable for the melt method.  We can then calculate the weighted averages on the fully melted pivot table.  There may be a more direct solution out there, but this does get to the right numbers.

import pandas as pd
import numpy as np
import random

# CREATE DATA
pot = np.linspace(0,80,num=9)
graft = np.linspace(0.30,0.80,num=11)

pots = pd.Series([])
grafts = pd.Series([])
rezs = pd.Series([])

for i in range (1,5):
    for pott in pot:
        for graftt in graft:
            rez = random.uniform(-2,4)
            rez= round(rez, 2)
            pots = pots.append(pd.Series(pott))
            grafts = grafts.append(pd.Series(graftt))
            rezs = rezs.append(pd.Series(rez))

# CREATE DATA FRAME
df = pd.DataFrame({'pot' : pots, 'graft' : grafts, 'rez': rezs })

# CREATE PIVOT TABLE
pivot = pd.pivot_table(data = df, values = 'rez', index = ['pot'], columns = ['graft'], aggfunc = np.sum)

# PRINT OUT INITIAL PIVOT TABLE
print("\nOriginal Pivot:\n",pivot)

# REPLACE NEGATIVE VALUES WITH ZEROES
pivot[pivot < 0] = 0
print("\nPost negative filtered Pivot:\n",pivot)

# RESET INDEX ON PIVOT TABLE
unpack = pivot.reset_index()

# MELT DATAFRAME COLUMNS
unpack = pd.melt(unpack, id_vars=['pot'], value_name='Rez')

# CALCULATE AND DISPLAY WEIGHTED AVERAGES
totrez = unpack['Rez'].sum()
unpack['Weight'] = unpack['Rez'] / totrez
wavpot = (unpack['pot'] * unpack['Weight']).sum()
wavpot = round(wavpot, 2)
print("\nWeighted Average pot:", wavpot)
wavgraft = (unpack['graft'] * unpack['Weight']).sum()
wavgraft = round(wavgraft, 2)
print("Weighted Average graft:", wavgraft)


问:
C# laziness question
What's the common approach to design applications, which strongly rely on lazy evaluation in C# (LINQ, IEnumerable, IQueryable, ...)?

Right now I usually attempt to make every query as lazy as possible, using yield return and LINQ queries, but in runtime this could usually lead to "too lazy" behavior, when every query gets builts from it's beginning obviously resulting in severe visual performance degradation.

What I usually do means putting ToList() projection operators somewhere to cache the data, but I suspect this approach might be incorrect.

What's the appropriate / common ways to design this sort of applications from the very beginning?

答:
A few random thoughts - as the question itself is loosely defined:

Lazy is good only when the result might not be used hence loaded only when needed. Most operations, however, would need the data to be loaded so laziness is not good in that term.

Laziness can cause difficult bugs. We have seen it all with data contexts in ORMs

Lazy is good when it comes to MEF

Title: C# laziness question


问:
How to extract objects from a csv / pandas dataframe of JSON files?
I have a csv (which I turned into a pandas dataframe) in which each row consists of a different JSON file, each JSON file has the exact same format and objects as the others, and each one represents a unique transaction (purchase) I would like to take this dataframe and convert it into a dataframe or excel file in which each column would represent an object from the JSON file and each row would represent each transaction.

The JSON also contains arrays, in which case I would like to be able to retrieve each element of the array. Ideally I would like to be able to retrieve all possible objects from the JSON files and turn them into columns.

A simplified version of a row would be:

```<code>    {
       "source":{
          "analyze":true,
          "billing":{
             "gender":null,
             "name":"xxxxx",
             "phones":[
                {
                   "area_code":"xxxxx",
                   "country_code":"xxxxx",
                   "number":"xxxxx",
                   "phone_type":"xxxxx"
                }
             ]
          },
          "created_at":"xxxxx",
          "customer":{
             "address":{
                "city":"xxxxx",
                "complement":"xxxxx",
                "country":"xxxxx",
                "neighborhood":"xxxxx",
                "number":"xxxxx",
                "state":"xxxxx",
                "street":"xxxxx",
                "zip_code":"xxxxx"
             },
             "date_of_birth":"xxxxx",
             "documents":[
                {
                   "document_type":"xxxxx",
                   "number":"xxxxx"
                }
             ],
             "email":"xxxxx",
             "gender":xxxxx,
             "name":"xxxxx",
             "number_of_previous_orders":xxxxx,
             "phones":[
                {
                   "area_code":"xxxxx",
                   "country_code":"xxxxx",
                   "number":"xxxxx",
                   "phone_type":"xxxxx"
                }
             ],
             "register_date":xxxxx,
             "register_id":"xxxxx"
          },
          "device":{
             "ip":"xxxxx",
             "lat":"xxxxx",
             "lng":"xxxxx",
             "platform":xxxxx,
             "session_id":xxxxx
          }
    }
    }
</code>```

And my python code,,,

```<code>import csv
import json
import pandas as pd
df = pd.read_csv(r"<name of csv file in which each row is a JSON file>")
</code>```

A simplified of my expected output would be something like

Expected Output

答:
You mean something like this as the output, for example to get area_code:

```<code>        A_col                                          area_code
0   {"source":{"analyze":true,"billing":{"gender":...   xxxxx
</code>```

first:

"gender":xxxxx, "number_of_previous_orders":xxxxx, "register_date":xxxxx,              "platform":xxxxx, "session_id":xxxxx, should be double quoted

get the json document:

```<code>newjson = []
with open('./example.json', 'r') as f:
    for line in f:
        line = line.strip()
        newjson.append(line)
</code>```

format it to string:

```<code>jsonString = ''.join(newjson)
</code>```

turn into python object:

```<code>jsonData = json.loads(jsonString)
</code>```

extract the fields using dictionary operations and turn into pandas dataframe:

```<code>newDF = pd.DataFrame({"A_col": jsonString, "area_code": jsonData['source']['billing']['phones'][0]['area_code']}, index=[0])
</code>```

Body:
I have a csv (which I turned into a pandas dataframe) in which each row consists of a different JSON file, each JSON file has the exact same format and objects as the others, and each one represents a unique transaction (purchase) I would like to take this dataframe and convert it into a dataframe or excel file in which each column would represent an object from the JSON file and each row would represent each transaction.

The JSON also contains arrays, in which case I would like to be able to retrieve each element of the array. Ideally I would like to be able to retrieve all possible objects from the JSON files and turn them into columns.

A simplified version of a row would be:

    {
       "source":{
          "analyze":true,
          "billing":{
             "gender":null,
             "name":"xxxxx",
             "phones":[
                {
                   "area_code":"xxxxx",
                   "country_code":"xxxxx",
                   "number":"xxxxx",
                   "phone_type":"xxxxx"
                }
             ]
          },
          "created_at":"xxxxx",
          "customer":{
             "address":{
                "city":"xxxxx",
                "complement":"xxxxx",
                "country":"xxxxx",
                "neighborhood":"xxxxx",
                "number":"xxxxx",
                "state":"xxxxx",
                "street":"xxxxx",
                "zip_code":"xxxxx"
             },
             "date_of_birth":"xxxxx",
             "documents":[
                {
                   "document_type":"xxxxx",
                   "number":"xxxxx"
                }
             ],
             "email":"xxxxx",
             "gender":xxxxx,
             "name":"xxxxx",
             "number_of_previous_orders":xxxxx,
             "phones":[
                {
                   "area_code":"xxxxx",
                   "country_code":"xxxxx",
                   "number":"xxxxx",
                   "phone_type":"xxxxx"
                }
             ],
             "register_date":xxxxx,
             "register_id":"xxxxx"
          },
          "device":{
             "ip":"xxxxx",
             "lat":"xxxxx",
             "lng":"xxxxx",
             "platform":xxxxx,
             "session_id":xxxxx
          }
    }
    }

And my python code,,,

import csv
import json
import pandas as pd
df = pd.read_csv(r"<name of csv file in which each row is a JSON file>")

A simplified of my expected output would be something like

Expected Output

Body:
You mean something like this as the output, for example to get area_code:

        A_col                                          area_code
0   {"source":{"analyze":true,"billing":{"gender":...   xxxxx

first:

"gender":xxxxx, "number_of_previous_orders":xxxxx, "register_date":xxxxx,              "platform":xxxxx, "session_id":xxxxx, should be double quoted

get the json document:

newjson = []
with open('./example.json', 'r') as f:
    for line in f:
        line = line.strip()
        newjson.append(line)

format it to string:

jsonString = ''.join(newjson)

turn into python object:

jsonData = json.loads(jsonString)

extract the fields using dictionary operations and turn into pandas dataframe:

newDF = pd.DataFrame({"A_col": jsonString, "area_code": jsonData['source']['billing']['phones'][0]['area_code']}, index=[0])


问:
Detecting Endianness
I'm currently trying to create a C source code which properly handles I/O whatever the endianness of the target system.

I've selected "little endian" as my I/O convention, which means that, for big endian CPU, I need to convert data while writing or reading.

Conversion is not the issue. The problem I face is to detect endianness, preferably at compile time (since CPU do not change endianness in the middle of execution...).

Up to now, I've been using this :

```<code>#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
...
#else
...
#endif
</code>```

It's documented as a GCC pre-defined macro, and Visual seems to understand it too.

However, I've received report that the check fails for some big_endian systems (PowerPC).

So, I'm looking for a foolproof solution, which ensures that endianess is correctly detected, whatever the compiler and the target system. well, most of them at least...

[Edit] : Most of the solutions proposed rely on "run-time tests". These tests may sometimes be properly evaluated by compilers during compilation, and therefore cost no real runtime performance.

However, branching with some kind of << if (0) { ... } else { ... } >> is not enough. In the current code implementation, variable and functions declaration depend on big_endian detection. These cannot be changed with an if statement.

Well, obviously, there is fall back plan, which is to rewrite the code...

I would prefer to avoid that, but, well, it looks like a diminishing hope...

[Edit 2] : I have tested "run-time tests", by deeply modifying the code. Although they do their job correctly, these tests also impact performance.

I was expecting that, since the tests have predictable output, the compiler could eliminate bad branches. But unfortunately, it doesn't work all the time. MSVC is good compiler, and is successful in eliminating bad branches, but GCC has mixed results, depending on versions, kind of tests, and with greater impact on 64 bits than on 32 bits.

It's strange. And it also means that the run-time tests cannot be ensured to be dealt with by the compiler.

Edit 3 : These days, I'm using a compile-time constant union, expecting the compiler to solve it to a clear yes/no signal.
And it works pretty well :
https://godbolt.org/g/DAafKo

答:
As most have mentioned, compile time is your best bet. Assuming you do not do cross compilations and you use cmake (it will also work with other tools such as a configure script, of course) then you can use a pre-test which is a compiled .c or .cpp file and that gives you the actual verified endianness of the processor you're running on.

With cmake you use the TestBigEndian macro. It sets a variable which you can then pass to your software. Something like this (untested):

```<code>TestBigEndian(IS_BIG_ENDIAN)
...
set(CFLAGS ${CFLAGS} -DIS_BIG_ENDIAN=${IS_BIG_ENDIAN}) // C
set(CXXFLAGS ${CXXFLAGS} -DIS_BIG_ENDIAN=${IS_BIG_ENDIAN}) // C++
</code>```

Then in your C/C++ code you can check that IS_BIG_ENDIAN define:

```<code>#if IS_BIG_ENDIAN
    ...do big endian stuff here...
#else
    ...do little endian stuff here...
#endif
</code>```

So the main problem with such a test is cross compiling since you may be on a completely different CPU with a different endianness... but at least it gives you the endianness at time of compiling the rest of your code and will work for most projects.

Title: Detecting Endianness

Body:
I'm currently trying to create a C source code which properly handles I/O whatever the endianness of the target system.

I've selected "little endian" as my I/O convention, which means that, for big endian CPU, I need to convert data while writing or reading.

Conversion is not the issue. The problem I face is to detect endianness, preferably at compile time (since CPU do not change endianness in the middle of execution...).

Up to now, I've been using this :

#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
...
#else
...
#endif

It's documented as a GCC pre-defined macro, and Visual seems to understand it too.

However, I've received report that the check fails for some big_endian systems (PowerPC).

So, I'm looking for a foolproof solution, which ensures that endianess is correctly detected, whatever the compiler and the target system. well, most of them at least...

[Edit] : Most of the solutions proposed rely on "run-time tests". These tests may sometimes be properly evaluated by compilers during compilation, and therefore cost no real runtime performance.

However, branching with some kind of << if (0) { ... } else { ... } >> is not enough. In the current code implementation, variable and functions declaration depend on big_endian detection. These cannot be changed with an if statement.

Well, obviously, there is fall back plan, which is to rewrite the code...

I would prefer to avoid that, but, well, it looks like a diminishing hope...

[Edit 2] : I have tested "run-time tests", by deeply modifying the code. Although they do their job correctly, these tests also impact performance.

I was expecting that, since the tests have predictable output, the compiler could eliminate bad branches. But unfortunately, it doesn't work all the time. MSVC is good compiler, and is successful in eliminating bad branches, but GCC has mixed results, depending on versions, kind of tests, and with greater impact on 64 bits than on 32 bits.

It's strange. And it also means that the run-time tests cannot be ensured to be dealt with by the compiler.

Edit 3 : These days, I'm using a compile-time constant union, expecting the compiler to solve it to a clear yes/no signal.
And it works pretty well :
https://godbolt.org/g/DAafKo

Body:
As most have mentioned, compile time is your best bet. Assuming you do not do cross compilations and you use cmake (it will also work with other tools such as a configure script, of course) then you can use a pre-test which is a compiled .c or .cpp file and that gives you the actual verified endianness of the processor you're running on.

With cmake you use the TestBigEndian macro. It sets a variable which you can then pass to your software. Something like this (untested):

TestBigEndian(IS_BIG_ENDIAN)
...
set(CFLAGS ${CFLAGS} -DIS_BIG_ENDIAN=${IS_BIG_ENDIAN}) // C
set(CXXFLAGS ${CXXFLAGS} -DIS_BIG_ENDIAN=${IS_BIG_ENDIAN}) // C++

Then in your C/C++ code you can check that IS_BIG_ENDIAN define:

#if IS_BIG_ENDIAN
    ...do big endian stuff here...
#else
    ...do little endian stuff here...
#endif

So the main problem with such a test is cross compiling since you may be on a completely different CPU with a different endianness... but at least it gives you the endianness at time of compiling the rest of your code and will work for most projects.


问:
Generating a hierarchy view in angular with a list of json objects
I am currently in the process of trying to build out a hierarchy view of users. My end goal is to generate the view using this hierarchy view
or something similar.

The difficulty lies in how the JSON objects used to generate the hierarchy are given. This is a sample response (this response can be much bigger), where pid is the parent id and depth is the distance from the first parent.

```<code>response = [
    {uid: "abc", pid: null, depth: 1, parent: true},
    {uid: "def", pid: "abc", depth: 2, parent: false},
    {uid: "ghi", pid: "abc", depth: 2, parent: true},
    {uid: "jkl", pid: "ghi", depth: 3, parent: false},
    {uid: "mno", pid: "ghi", depth: 3, parent: false},
]
</code>```

To explain the above response better, here is the visual hierarchy view of it:
image

A lot of the answers and solutions I've seen so far utilize JSON with children nested in each one. Is it possible to generate the view using the json model above?

Any help or insight would be much appreciated! Thanks!

答:
You can use a reducer to convert the flat array into UID map of nodes, and once you have the map you can populate the children easily. You can then just pick out the root node and use that to render the HTML.

const map = [
   {uid: "abc", pid: null, depth: 1, parent: true},
   {uid: "def", pid: "abc", depth: 2, parent: true},
   {uid: "ghi", pid: "abc", depth: 2, parent: false},
   {uid: "jkl", pid: "ghi", depth: 3, parent: false},
   {uid: "mno", pid: "ghi", depth: 3, parent: false},
].reduce((acc, node) => (node.children = [], acc[node.uid] = node, acc), {});

const [root] =  Object.values(map)
                      .map(node => (node.pid && map[node.pid].children.push(node), node))
                      .filter(node => node.pid === null);

console.log(root);```
</div>
</div>
</p>

<p>You can render the tree by using the same component recursively, and have the template render the children.</p>

```@Component({
      selector: 'app-node',
      template: `
          Node
          <app-node [node]="child" *ngFor="let child of node.children"></app-node>
      `
})
export class NodeComponent {
    @Input()
    public node: any;
}
```

It's not hard to modify the above to match the HTML/CSS you linked to in your question.

Body:
I am currently in the process of trying to build out a hierarchy view of users. My end goal is to generate the view using this hierarchy view
or something similar.

The difficulty lies in how the JSON objects used to generate the hierarchy are given. This is a sample response (this response can be much bigger), where pid is the parent id and depth is the distance from the first parent.

response = [
    {uid: "abc", pid: null, depth: 1, parent: true},
    {uid: "def", pid: "abc", depth: 2, parent: false},
    {uid: "ghi", pid: "abc", depth: 2, parent: true},
    {uid: "jkl", pid: "ghi", depth: 3, parent: false},
    {uid: "mno", pid: "ghi", depth: 3, parent: false},
]

To explain the above response better, here is the visual hierarchy view of it:
image

A lot of the answers and solutions I've seen so far utilize JSON with children nested in each one. Is it possible to generate the view using the json model above?

Any help or insight would be much appreciated! Thanks!

Body:
You can use a reducer to convert the flat array into UID map of nodes, and once you have the map you can populate the children easily. You can then just pick out the root node and use that to render the HTML.

const map = [
   {uid: "abc", pid: null, depth: 1, parent: true},
   {uid: "def", pid: "abc", depth: 2, parent: true},
   {uid: "ghi", pid: "abc", depth: 2, parent: false},
   {uid: "jkl", pid: "ghi", depth: 3, parent: false},
   {uid: "mno", pid: "ghi", depth: 3, parent: false},
].reduce((acc, node) => (node.children = [], acc[node.uid] = node, acc), {});

const [root] =  Object.values(map)
                      .map(node => (node.pid && map[node.pid].children.push(node), node))
                      .filter(node => node.pid === null);

console.log(root);

You can render the tree by using the same component recursively, and have the template render the children.

@Component({
      selector: 'app-node',
      template: `
          <span>Node</span>
          <app-node [node]="child" *ngFor="let child of node.children"></app-node>
      `
})
export class NodeComponent {
    @Input()
    public node: any;
}

It's not hard to modify the above to match the HTML/CSS you linked to in your question.


问:
is SQLiteDatabase syncronous or async?
I have an app that opens a new intent. The new intent inserts some rows on the datadabase, then finish(); it and come back to the first screen where the data should have refreshed with the new row. The code I have do not work on execution but works when debuging... so maybe there is some lag somewhere... maybe? how to wait for the query to finish?

Here is my code:

on the first screen:

setting the adapter and onResume

```<code>@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    v= inflater.inflate(R.layout.filters_fragment,container,false);

    createFragmentWidgets();

    myrecyclerview = (RecyclerView) v.findViewById(R.id.contact_recycleview);
    recyclerAdapter = new RecyclerViewAdapterFilters(getContext(), lstFilters);
    myrecyclerview.setLayoutManager(new LinearLayoutManager((getActivity())));
    myrecyclerview.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
    myrecyclerview.setAdapter(recyclerAdapter);

    return v;
}

@Override
public void onResume()
{
// After a pause OR at startup
    super.onResume();

    lstFilters = mydb.getAllFilters("BL");
    recyclerAdapter.notifyDataSetChanged();
}
</code>```

the getAllFilters method on the DB class

```<code>public List<Filter> getAllFilters(String type) {
    List<Filter> lstFilter  = new ArrayList<>();

    //hp = new HashMap();
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res =  db.rawQuery( "select * from " + FILTERS_TABLE_NAME+" where type='"+type+"'", null );
    res.moveToFirst();

    while(res.isAfterLast() == false){
        lstFilter.add(new Filter (res.getInt(res.getColumnIndex(FILTERS_COLUMN_ID)),
                res.getString(res.getColumnIndex(FILTERS_COLUMN_NAME)),
                res.getString(res.getColumnIndex(FILTERS_COLUMN_NUMBER)),
                res.getString(res.getColumnIndex(FILTERS_COLUMN_PHOTO)),
                res.getString(res.getColumnIndex(FILTERS_COLUMN_TYPE))));
        res.moveToNext();
    }

    res.close();
    db.close();

    return lstFilter;
}
</code>```

any ideas?

答:
After you refresh your list, it should be enough to call notifyDataSetChanged(), however, I usually just reset the adapter:</p>

```<code>    adapter = new ClaimListAdapter(this, thisContext, claimItems);
    mClaimList.setAdapter(adapter);
</code>```

Title: is SQLiteDatabase syncronous or async?

Body:
I have an app that opens a new intent. The new intent inserts some rows on the datadabase, then finish(); it and come back to the first screen where the data should have refreshed with the new row. The code I have do not work on execution but works when debuging... so maybe there is some lag somewhere... maybe? how to wait for the query to finish?

Here is my code:

on the first screen:

setting the adapter and onResume

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    v= inflater.inflate(R.layout.filters_fragment,container,false);

    createFragmentWidgets();

    myrecyclerview = (RecyclerView) v.findViewById(R.id.contact_recycleview);
    recyclerAdapter = new RecyclerViewAdapterFilters(getContext(), lstFilters);
    myrecyclerview.setLayoutManager(new LinearLayoutManager((getActivity())));
    myrecyclerview.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
    myrecyclerview.setAdapter(recyclerAdapter);

    return v;
}

@Override
public void onResume()
{
// After a pause OR at startup
    super.onResume();

    lstFilters = mydb.getAllFilters("BL");
    recyclerAdapter.notifyDataSetChanged();
}

the getAllFilters method on the DB class

public List<Filter> getAllFilters(String type) {
    List<Filter> lstFilter  = new ArrayList<>();

    //hp = new HashMap();
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res =  db.rawQuery( "select * from " + FILTERS_TABLE_NAME+" where type='"+type+"'", null );
    res.moveToFirst();

    while(res.isAfterLast() == false){
        lstFilter.add(new Filter (res.getInt(res.getColumnIndex(FILTERS_COLUMN_ID)),
                res.getString(res.getColumnIndex(FILTERS_COLUMN_NAME)),
                res.getString(res.getColumnIndex(FILTERS_COLUMN_NUMBER)),
                res.getString(res.getColumnIndex(FILTERS_COLUMN_PHOTO)),
                res.getString(res.getColumnIndex(FILTERS_COLUMN_TYPE))));
        res.moveToNext();
    }

    res.close();
    db.close();

    return lstFilter;
}

any ideas?

Body:
After you refresh your list, it should be enough to call notifyDataSetChanged(), however, I usually just reset the adapter:

    adapter = new ClaimListAdapter(this, thisContext, claimItems);
    mClaimList.setAdapter(adapter);