问:
java.util.NoSuchElementException while getting ArrayList from HashMap using Iterator
My code goes like this:

```<code>HashMap<Integer, ArrayList<DebtCollectionReport>> mapOfAccounts = new HashMap<Integer, ArrayList<DebtCollectionReport>>();
  Set<String> agencyNames = agencyWiseAccountMap.keySet();
  Iterator iter = agencyNames.iterator();
  while (iter.hasNext()) {
    String agency = (String) iter.next();
    HashMap<Integer, ArrayList<DebtCollectionReport>> tempAccountsMap = agencyWiseAccountMap.get(agency);
    Set<Integer> accountSet = tempAccountsMap.keySet();
    Iterator itr = accountSet.iterator();
    while (itr.hasNext()) {
      mapOfAccounts.put((Integer) itr.next(), tempAccountsMap.get((Integer) itr.next()));
    }
  }
</code>```

I am getting exception trace:

>

```<code> java.util.NoSuchElementException
    at java.util.HashMap$HashIterator.nextNode(Unknown Source)
    at java.util.HashMap$KeyIterator.next(Unknown Source)
    at com.cerillion.debtcollection.collector.CollectionExecutor.execute(CollectionExecutor.java:56)
    at com.cerillion.debtcollection.collector.CollectionExecutor.main(CollectionExecutor.java:24)
2017-11-14 05:00:43,733 ERROR  CollectionExecutor             [main      ] Exception occurred while executing Debt Collection java.util.NoSuchElementException
java.util.NoSuchElementException
    at java.util.HashMap$HashIterator.nextNode(Unknown Source)
    at java.util.HashMap$KeyIterator.next(Unknown Source)
    at com.cerillion.debtcollection.collector.CollectionExecutor.execute(CollectionExecutor.java:56)
    at com.cerillion.debtcollection.collector.CollectionExecutor.main(CollectionExecutor.java:24)
</code>```

This is occuring for line:

```<code>mapOfAccounts.put((Integer) itr.next(), tempAccountsMap.get((Integer) itr.next()));
</code>```

What can be the possible reason and how can i resolve it ?

答:
```<code>mapOfAccounts.put((Integer) itr.next(), tempAccountsMap.get((Integer) itr.next()));
</code>```

The problem should be with itr.next(), every time you called itr.next(), the index of the iterator would move a step forward. So the code move 2 steps at this line...
You should with a var to accept the value, then use the var:

```<code>int accountIdTemp = itr.next();
mapOfAccounts.put((Integer) accountIdTemp , tempAccountsMap.get((Integer) accountIdTemp ));
</code>```

Hope this help.

Body:
My code goes like this:

HashMap<Integer, ArrayList<DebtCollectionReport>> mapOfAccounts = new HashMap<Integer, ArrayList<DebtCollectionReport>>();
  Set<String> agencyNames = agencyWiseAccountMap.keySet();
  Iterator iter = agencyNames.iterator();
  while (iter.hasNext()) {
    String agency = (String) iter.next();
    HashMap<Integer, ArrayList<DebtCollectionReport>> tempAccountsMap = agencyWiseAccountMap.get(agency);
    Set<Integer> accountSet = tempAccountsMap.keySet();
    Iterator itr = accountSet.iterator();
    while (itr.hasNext()) {
      mapOfAccounts.put((Integer) itr.next(), tempAccountsMap.get((Integer) itr.next()));
    }
  }

I am getting exception trace:

>

 java.util.NoSuchElementException
    at java.util.HashMap$HashIterator.nextNode(Unknown Source)
    at java.util.HashMap$KeyIterator.next(Unknown Source)
    at com.cerillion.debtcollection.collector.CollectionExecutor.execute(CollectionExecutor.java:56)
    at com.cerillion.debtcollection.collector.CollectionExecutor.main(CollectionExecutor.java:24)
2017-11-14 05:00:43,733 ERROR  CollectionExecutor             [main      ] Exception occurred while executing Debt Collection java.util.NoSuchElementException
java.util.NoSuchElementException
    at java.util.HashMap$HashIterator.nextNode(Unknown Source)
    at java.util.HashMap$KeyIterator.next(Unknown Source)
    at com.cerillion.debtcollection.collector.CollectionExecutor.execute(CollectionExecutor.java:56)
    at com.cerillion.debtcollection.collector.CollectionExecutor.main(CollectionExecutor.java:24)

This is occuring for line:

mapOfAccounts.put((Integer) itr.next(), tempAccountsMap.get((Integer) itr.next()));

What can be the possible reason and how can i resolve it ?

Body:
mapOfAccounts.put((Integer) itr.next(), tempAccountsMap.get((Integer) itr.next()));

The problem should be with itr.next(), every time you called itr.next(), the index of the iterator would move a step forward. So the code move 2 steps at this line...
You should with a var to accept the value, then use the var:

int accountIdTemp = itr.next();
mapOfAccounts.put((Integer) accountIdTemp , tempAccountsMap.get((Integer) accountIdTemp ));

Hope this help.


来源: stackexchange

问:
Validation on deleting record ActiveJDBC
What would be the best approach if I want to put validation upon record deletion?
Here is the reason why.

I have two tables, Store and Staff.

Structure of Store:

ID

Store Code

Store Name

Structure of Staff:

ID

Staff Code

Staff Name

Store Code

As you can see, table store is related to table staff.

What if the user tries to delete a store that is already used on table staff?
If no validation, my data would be broken. How can I prevent it?

Thanks.

答:
The best way to implement it is through the referential integrity in the database, not in framework.

Structure of STORE:

id

code

name

Structure of STAFF:

id

code

name

store_id

As you can see, the STAFF table "belongs" to STORE because it contains the ID of a parent record. This is a classical One-to-many association: http://javalite.io/one_to_many_associations

You can do one of two things:

Throw exception from DB in case you are deleting a parent without deleting a child

Cascade deleting a child in case a parent is deleted automatically (in DB)

A few links suggested by Google:

https://dev.mysql.com/doc/refman/5.7/en/create-table-foreign-keys.html
http://www.mysqltutorial.org/mysql-on-delete-cascade/

ContentLicense: CC BY-SA 3.0


问:
Python - How to dynamically exclude a column name from a list of columns of a Panda Dataframe
So far I am able to get the list of all column names present in the dataframe or to get a specific column names based on its datatype, starting letters, etc...

Now my requirement is to get the whole list of column names or a sublist and to exclude one column from it (i.e Target variable / Label Column. This is a part of Machine Learning. So I am using the terms that are used in machine learning)

Please note I am not speaking about the data present in those columns. I am just taking the column names and want to exclude a particular column by its name

Please see below example for better understanding :

```<code># Get all the column names from a Dataframe
df.columns

Index(['transactionID', 'accountID', 'transactionAmountUSD',
       'transactionAmount', 'transactionCurrencyCode',
       'accountAge', 'validationid', 'LABEL'],
      dtype='object')

# Get only the Numeric Variables (Columns with numeric values in it)
df._get_numeric_data().columns

Index(['transactionAmountUSD', 'transactionAmount', 'accountAge', 'LABEL'],
      dtype='object')
</code>```

Now inorder to get remaining column names I am subtracting both the above commands

```<code>string_cols = list(set(list(df.columns))-set(df._get_numeric_data().columns))
</code>```

Ok everything goes well until I hit this.

I have found out that Label column though it has numeric values it should not be present in the list of numeric variables. It should be excluded.

(i.e) I want to exclude a particular column name (not using its index in the list but using its name explicitly)

I tried similar statements like the following ones but in vain. Any inputs on this will be helpful

```<code>set(df._get_numeric_data().columns-set(df.LABEL)
set(df._get_numeric_data().columns-set(df.LABEL.column)
set(df._get_numeric_data().columns-set(df['LABEL'])
</code>```

I am sure I am missing a very basic thing but not able to figure it out.

答:
Pandas' index supports set operations, so to exclude one column from column index you can just write something like

```<code>import pandas as pd
df = pd.DataFrame(columns=list('abcdef'))
print(df.columns.difference({'b'}))
</code>```

which will return to you

```<code>Index(['a', 'c', 'd', 'e', 'f'], dtype='object')
</code>```

I hope this is what you want :)

Body:
So far I am able to get the list of all column names present in the dataframe or to get a specific column names based on its datatype, starting letters, etc...

Now my requirement is to get the whole list of column names or a sublist and to exclude one column from it (i.e Target variable / Label Column. This is a part of Machine Learning. So I am using the terms that are used in machine learning)

Please note I am not speaking about the data present in those columns. I am just taking the column names and want to exclude a particular column by its name

Please see below example for better understanding :

# Get all the column names from a Dataframe
df.columns

Index(['transactionID', 'accountID', 'transactionAmountUSD',
       'transactionAmount', 'transactionCurrencyCode',
       'accountAge', 'validationid', 'LABEL'],
      dtype='object')

# Get only the Numeric Variables (Columns with numeric values in it)
df._get_numeric_data().columns

Index(['transactionAmountUSD', 'transactionAmount', 'accountAge', 'LABEL'],
      dtype='object')

Now inorder to get remaining column names I am subtracting both the above commands

string_cols = list(set(list(df.columns))-set(df._get_numeric_data().columns))

Ok everything goes well until I hit this.

I have found out that Label column though it has numeric values it should not be present in the list of numeric variables. It should be excluded.

(i.e) I want to exclude a particular column name (not using its index in the list but using its name explicitly)

I tried similar statements like the following ones but in vain. Any inputs on this will be helpful

set(df._get_numeric_data().columns-set(df.LABEL)
set(df._get_numeric_data().columns-set(df.LABEL.column)
set(df._get_numeric_data().columns-set(df['LABEL'])

I am sure I am missing a very basic thing but not able to figure it out.

Body:
Pandas' index supports set operations, so to exclude one column from column index you can just write something like

import pandas as pd
df = pd.DataFrame(columns=list('abcdef'))
print(df.columns.difference({'b'}))

which will return to you

Index(['a', 'c', 'd', 'e', 'f'], dtype='object')

I hope this is what you want :)


问:
MVCBuildViews not working correctly
So I edited my csproj file on an MVC 3 RTM application to set the following property:

```<code><MvcBuildViews>true</MvcBuildViews>
</code>```

This should cause my views to be complied during build and force a build error if my view is broken. This is the only change I made, however, when I try to build the application, I get the following error:

It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level.  This error can be caused by a virtual directory not being configured as an application in IIS.

The project compiles and runs successfully if I change back to  false,

The following are the build tasks configured in the csproj file (these were never manually edited, they were added by Visual Studio 2010)

```<code><Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target> -->
<Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
  <AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
</Target>
</code>```

Am I missing something here? How do I get MVC 3 / Visual Studio 2010 configured correctly to validate my views at build time?

答:
I had this problem a few days ago and I fixed it by deleting obj/Debug folder. Cleaning the project also works. I have no idea about the cause of the issue, though.

See Joe Cartano's answer for a more permanent solution.

Title: MVCBuildViews not working correctly

Body:
So I edited my csproj file on an MVC 3 RTM application to set the following property:

<MvcBuildViews>true</MvcBuildViews>

This should cause my views to be complied during build and force a build error if my view is broken. This is the only change I made, however, when I try to build the application, I get the following error:

It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level.  This error can be caused by a virtual directory not being configured as an application in IIS.

The project compiles and runs successfully if I change back to  false,

The following are the build tasks configured in the csproj file (these were never manually edited, they were added by Visual Studio 2010)

<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target> -->
<Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
  <AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
</Target>

Am I missing something here? How do I get MVC 3 / Visual Studio 2010 configured correctly to validate my views at build time?


问:
Redirect sys.stdout from ping results to a wx.TextCtrl widget
I'm trying to redirect the sys.stdout stream to a wx.TextCtrl widget. In this code, i used ping but how to redirect results to wx.textctrl or statictext widgets. I can send variable from my input to wx.statictext but not able to get the stdout from ping results. hope someone here can help.

I'm using python 3.6.0 with wx 2.8.12.1

```<code>import wx
import os

class RandomPanel(wx.Panel):

    def __init__(self, parent, color):

        wx.Panel.__init__(self, parent)
        self.SetBackgroundColour(color)

class MainPanel(wx.Panel):

    def __init__(self, parent):

        wx.Panel.__init__(self, parent)

        topSplitter = wx.SplitterWindow(self)
        vSplitter = wx.SplitterWindow(topSplitter)

        panelOne = RandomPanel(vSplitter, "white")

        self.txt = wx.TextCtrl(panelOne,
                    style=wx.TE_PROCESS_ENTER,
                    pos=(7, 8), size=(330, 30))
        self.txt.SetFocus()
        self.txt.Bind(wx.EVT_TEXT_ENTER, self.ping)

        panelTwo = RandomPanel(vSplitter, "white")
        vSplitter.SplitVertically(panelOne, panelTwo)
        vSplitter.SetSashGravity(0.5)

        panelThree = RandomPanel(topSplitter, "black")
        topSplitter.SplitHorizontally(vSplitter, panelThree)
        topSplitter.SetSashGravity(0.5)
        self.term = wx.StaticText(panelThree, -1, '', pos=(6, 100))
        self.term.SetForegroundColour((255, 255, 255))  #set font color
        self.term.SetBackgroundColour((0, 0, 0)) #set background color

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(topSplitter, 1, wx.EXPAND)
        self.SetSizer(sizer)

    def ping(self, event):
        put = self.txt.GetValue()
        self.p = os.system("ping -n 1 "+put)
        self.term.SetLabel(put)
        self.p.AppendText('>>> ')
        self.p.AppendText(event)

class MainFrame(wx.Frame):

    def __init__(self):
           wx.Frame.__init__(self, None, title="ping tool",
                          size=(800, 600))
        panel = MainPanel(self)
        self.Show()

if __name__ == "__main__":
    app = wx.App(False)
    frame = MainFrame()
    app.MainLoop()
</code>```

答:
@Rolf of Saxony, i have updated the code thanks for your help.</p>

```<code>def ping(self, event):
        a = []
        put = self.txt.GetValue()
        comm = Popen(['ping', put], stdout=PIPE, stderr=STDOUT, universal_newlines=True)
        for i in iter(comm.stdout.readline, b''):
            if i != '':
                pass
            else:
                break
            a.append(str(i))
            self.term.SetLabelText("start pinging......")
        self.term.SetLabelText("\n".join(a))
        wx.Yield()
</code>```

Body:
I'm trying to redirect the sys.stdout stream to a wx.TextCtrl widget. In this code, i used ping but how to redirect results to wx.textctrl or statictext widgets. I can send variable from my input to wx.statictext but not able to get the stdout from ping results. hope someone here can help.

I'm using python 3.6.0 with wx 2.8.12.1

import wx
import os

class RandomPanel(wx.Panel):

    def __init__(self, parent, color):

        wx.Panel.__init__(self, parent)
        self.SetBackgroundColour(color)

class MainPanel(wx.Panel):

    def __init__(self, parent):

        wx.Panel.__init__(self, parent)

        topSplitter = wx.SplitterWindow(self)
        vSplitter = wx.SplitterWindow(topSplitter)

        panelOne = RandomPanel(vSplitter, "white")

        self.txt = wx.TextCtrl(panelOne,
                    style=wx.TE_PROCESS_ENTER,
                    pos=(7, 8), size=(330, 30))
        self.txt.SetFocus()
        self.txt.Bind(wx.EVT_TEXT_ENTER, self.ping)

        panelTwo = RandomPanel(vSplitter, "white")
        vSplitter.SplitVertically(panelOne, panelTwo)
        vSplitter.SetSashGravity(0.5)

        panelThree = RandomPanel(topSplitter, "black")
        topSplitter.SplitHorizontally(vSplitter, panelThree)
        topSplitter.SetSashGravity(0.5)
        self.term = wx.StaticText(panelThree, -1, '', pos=(6, 100))
        self.term.SetForegroundColour((255, 255, 255))  #set font color
        self.term.SetBackgroundColour((0, 0, 0)) #set background color

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(topSplitter, 1, wx.EXPAND)
        self.SetSizer(sizer)

    def ping(self, event):
        put = self.txt.GetValue()
        self.p = os.system("ping -n 1 "+put)
        self.term.SetLabel(put)
        self.p.AppendText('>>> ')
        self.p.AppendText(event)

class MainFrame(wx.Frame):

    def __init__(self):
           wx.Frame.__init__(self, None, title="ping tool",
                          size=(800, 600))
        panel = MainPanel(self)
        self.Show()

if __name__ == "__main__":
    app = wx.App(False)
    frame = MainFrame()
    app.MainLoop()

Body:
@Rolf of Saxony, i have updated the code thanks for your help.

def ping(self, event):
        a = []
        put = self.txt.GetValue()
        comm = Popen(['ping', put], stdout=PIPE, stderr=STDOUT, universal_newlines=True)
        for i in iter(comm.stdout.readline, b''):
            if i != '':
                pass
            else:
                break
            a.append(str(i))
            self.term.SetLabelText("start pinging......")
        self.term.SetLabelText("\n".join(a))
        wx.Yield()


问:
Group by with distinct query
Am using Oracle 12c, below is the column:

```<code>CENTER_ID     UNIT      EMPLOYEEID    LESSON  MINS_STUDIED  SECTION
I234          4         G01234         4.1        5            EX
I234          4         G01234         4.1        5            LN
I234          4         G01234         4.1        5            VO

I234          4         G0123          4.2        5            EX
I234          4         G0123          4.2        5            LN
I234          4         G0123          4.2        5            VO

I2345         5         G023           5.2       12            EX
I2345         5         G023           5.2       12            LN
I2345         5         G023           5.2       12            VO
</code>```

From record 1 to 6, it contains 2 distinct employee_id who studied unit 4. In this case, I need total minutes spent per unit (center_id wise).

But I want to add only distinct MINS_STUDIED based on EMPLOYEE_ID.

So, total minutes spent for center_id I234 for unit 4 is (5+5)=10 and for center_id I2345 for unit 5 is 12.

I used the below query

```<code>    SELECT
         COUNT(DISTINCT EMPLOYEE_ID),CENTER_ID,UNIT,
         SUM(DISTINCT MINS_STUDIED)
    FROM
         TEST_DATA
    GROUP BY
          CENTER_ID,DBD.UNIT;
</code>```

The above query returns

```<code>center_id    UNIT  DISTINCT(EMPLOYEE_ID) TOTAL_MINS_STUDIED
I234          4      2                       5
I2345         5      1                       12
</code>```

But, I need the output like below:

```<code>center_id    UNIT  DISTINCT(EMPLOYEE_ID) TOTAL_MINS_STUDIED
I234          4      2                       10
I2345         5      1                       12
</code>```

答:
You can do that with analytics functions, without subselects and group by:

Edit, added Employee_id to the partititon by clause of the SUM function

```<code>SELECT
    CENTER_ID, UNIT,
    COUNT(EMPLOYEE_ID) OVER (PARTITION BY CENTER_ID, UNIT) EMPLOYEES,
    SUM(MIN_STUDIED) OVER (PARTITION BY CENTER_ID, UNIT, EMPLOYEE_ID) TOTAL_MINS_STUDIED
FROM
    TESTS_DATA
ORDER BY
    CENTER_ID, UNIT;
</code>```

Title: Group by with distinct query

Body:
Am using Oracle 12c, below is the column:

CENTER_ID     UNIT      EMPLOYEEID    LESSON  MINS_STUDIED  SECTION
I234          4         G01234         4.1        5            EX
I234          4         G01234         4.1        5            LN
I234          4         G01234         4.1        5            VO

I234          4         G0123          4.2        5            EX
I234          4         G0123          4.2        5            LN
I234          4         G0123          4.2        5            VO

I2345         5         G023           5.2       12            EX
I2345         5         G023           5.2       12            LN
I2345         5         G023           5.2       12            VO

From record 1 to 6, it contains 2 distinct employee_id who studied unit 4. In this case, I need total minutes spent per unit (center_id wise).

But I want to add only distinct MINS_STUDIED based on EMPLOYEE_ID.

So, total minutes spent for center_id I234 for unit 4 is (5+5)=10 and for center_id I2345 for unit 5 is 12.

I used the below query

    SELECT
         COUNT(DISTINCT EMPLOYEE_ID),CENTER_ID,UNIT,
         SUM(DISTINCT MINS_STUDIED)
    FROM
         TEST_DATA
    GROUP BY
          CENTER_ID,DBD.UNIT;

The above query returns

center_id    UNIT  DISTINCT(EMPLOYEE_ID) TOTAL_MINS_STUDIED
I234          4      2                       5
I2345         5      1                       12

But, I need the output like below:

center_id    UNIT  DISTINCT(EMPLOYEE_ID) TOTAL_MINS_STUDIED
I234          4      2                       10
I2345         5      1                       12

Body:
You can do that with analytics functions, without subselects and group by:

Edit, added Employee_id to the partititon by clause of the SUM function

SELECT
    CENTER_ID, UNIT,
    COUNT(EMPLOYEE_ID) OVER (PARTITION BY CENTER_ID, UNIT) EMPLOYEES,
    SUM(MIN_STUDIED) OVER (PARTITION BY CENTER_ID, UNIT, EMPLOYEE_ID) TOTAL_MINS_STUDIED
FROM
    TESTS_DATA
ORDER BY
    CENTER_ID, UNIT;


问:
AVR 8 bit timer - what to do when compare value doesn't fit in register?
Recently, I started programming the Atmega328P in pure c/c++, without all the arduino libraries and arduino IDE. I want to blink an LED at a rate of 1Hz (1 sec. on, 1 sec. off). Unfortunately, I can only use Timer0, an 8-bit timer. The mcu has a clock frequency of 16MHz and I chose a prescaler of 1024 to reduce to number of overflows as much as possible to reduce jitter, as an interrupt routine always has overhead (this makes more sense if you read the rest of my question). Using simple maths, I came to conclusion that after 1 second, Timer0 has overflowed 61 times and that the TCNT0 register equals 8.

Then, I came up with this solution:

```<code>#define F_CPU 16000000ul

#include <avr/io.h>
#include <avr/interrupt.h>

#define bit(b) (1 << (b))

void initBlinkTimer()
{
    //Timer0 with CTC Mode
    TCCR0A |= bit(WGM01) | bit(WGM00);

    //Compare TCNT0 with 8
    OCR0B = 8;

    //Interrupt at OCR0B compare match aka: execute interrupt when TCNT0 equals 8
    TIMSK0 |= bit(OCIE0B);

    //Set PB5 as output
    DDRB |= bit(PB5);

    //Set the prescaler to 1024
    TCCR0B |= bit(CS02) | bit(CS00);

    //Enable global interrupts, so that the interrupt routine can be executed upon the OCR0B compare match
    sei();
}

//Keeps track of the number of overflows
volatile unsigned char nOverflows = 0;

ISR(TIMER0_COMPB_vect)
{
    if(nOverflows >= 61)
    {
        //Toggle PB5
        PORTB ^= bit(PB5);

        //Reset timer
        nOverflows = 0;
        TCNT0 = 0;
    }
    else
    {
        nOverflows++;
    }
}

int main()
{
    initBlinkTimer();
    while(1){}
}
</code>```

This code initializes an 8-bit CTC timer with a prescaler of 1024. An interrupt service routine (ISR) is executed when TCNT0 equals OCR0B, which I set to 8 in my code. In the ISR, the nOverflows variable is compared to 61. If nOverflows equals 61, one second has passed and the PB5 pin is to be toggled. It also performs a greater-than check, in case the mcu missed the 61th overflow (if that's somehow possible in this case). The timer and nOverflows variable are cleared after toggling the pin and the timer then starts again from zero.

My question is: Is this a good way to blink an LED at 1Hz, when only an 8-bit timer is available? Can/should a part of this be implemented in the hardware instead of in the software?

答:
You don't need super high precision in your LED blinking; no one will be able to tell with their eyes if it is slightly off.

I'd say you should configure the timer to overflow about one to ten times per millisecond.  Let's say it overflows every 251 microseconds (the exact number doesn't matter).  Then every time it overflows, you add 251 to a uint16_t variable for counting microseconds.  If the microsecond counter is greater than or equal to 1000, you subtract 1000 from it and increment your millisecond counter, which should probably be a uint32_t so you can use it to time long periods of time.

But what if the overflow period is not an exact number of microseconds?  That's still fine, because it will probably be expressible in the form "N/M milliseconds", where N and M are whole numbers.  So every time the overflow happens, you increment your counter by N.  If the counter reaches M, you subtract M from the counter and add 1 to your millisecond counter.  (In the preivous paragraph, we only considered the M=1000 case, and I think by starting with that case it is easier to see what I'm talking about.)

You can either use an ISR to update your millisecond counter or do it in your main-line code by frequently checking for an overflow flag.  Either way would work.

So now you have a variable that counts up once per millisecond, like the Arduino millis() function, and you can use it for all sorts of millisecond-scale timing, including blinking LEDs.

The least-significant bit (bit 0) of the millisecond counter toggles once per millisecond, so its period is 2 ms.  The next bit up from that (bit 1) has a period of 4 ms.  Bit 10 has a period of 2048 ms.

So a simple way to blink your LED with a period of 2048 ms would be:

```<code>led_state = millisecond_counter >> 10 & 1;
</code>```

(The code above uses bitwise operations to set led_state to be a copy of bit 10 from the millisecond counter.  Assuming you run this code frequently and write led_state out to the LED using the I/O registers, your LED would then have a period of 2048 ms.)

If you really care that your LED is slow by 2.4%, you could do something more complicated like this:

```<code>static uint16_t last_toggle = 0;
if ((uint16_t)(millisecond_counter - last_toggle) >= 1000)
{
  last_toggle += 1000;
  toggle_the_led();
}
</code>```

Body:
Recently, I started programming the Atmega328P in pure c/c++, without all the arduino libraries and arduino IDE. I want to blink an LED at a rate of 1Hz (1 sec. on, 1 sec. off). Unfortunately, I can only use Timer0, an 8-bit timer. The mcu has a clock frequency of 16MHz and I chose a prescaler of 1024 to reduce to number of overflows as much as possible to reduce jitter, as an interrupt routine always has overhead (this makes more sense if you read the rest of my question). Using simple maths, I came to conclusion that after 1 second, Timer0 has overflowed 61 times and that the TCNT0 register equals 8.

Then, I came up with this solution:

#define F_CPU 16000000ul

#include <avr/io.h>
#include <avr/interrupt.h>

#define bit(b) (1 << (b))

void initBlinkTimer()
{
    //Timer0 with CTC Mode
    TCCR0A |= bit(WGM01) | bit(WGM00);

    //Compare TCNT0 with 8
    OCR0B = 8;

    //Interrupt at OCR0B compare match aka: execute interrupt when TCNT0 equals 8
    TIMSK0 |= bit(OCIE0B);

    //Set PB5 as output
    DDRB |= bit(PB5);

    //Set the prescaler to 1024
    TCCR0B |= bit(CS02) | bit(CS00);

    //Enable global interrupts, so that the interrupt routine can be executed upon the OCR0B compare match
    sei();
}

//Keeps track of the number of overflows
volatile unsigned char nOverflows = 0;

ISR(TIMER0_COMPB_vect)
{
    if(nOverflows >= 61)
    {
        //Toggle PB5
        PORTB ^= bit(PB5);

        //Reset timer
        nOverflows = 0;
        TCNT0 = 0;
    }
    else
    {
        nOverflows++;
    }
}

int main()
{
    initBlinkTimer();
    while(1){}
}

This code initializes an 8-bit CTC timer with a prescaler of 1024. An interrupt service routine (ISR) is executed when TCNT0 equals OCR0B, which I set to 8 in my code. In the ISR, the nOverflows variable is compared to 61. If nOverflows equals 61, one second has passed and the PB5 pin is to be toggled. It also performs a greater-than check, in case the mcu missed the 61th overflow (if that's somehow possible in this case). The timer and nOverflows variable are cleared after toggling the pin and the timer then starts again from zero.

My question is: Is this a good way to blink an LED at 1Hz, when only an 8-bit timer is available? Can/should a part of this be implemented in the hardware instead of in the software?

Body:
You don't need super high precision in your LED blinking; no one will be able to tell with their eyes if it is slightly off.

I'd say you should configure the timer to overflow about one to ten times per millisecond.  Let's say it overflows every 251 microseconds (the exact number doesn't matter).  Then every time it overflows, you add 251 to a uint16_t variable for counting microseconds.  If the microsecond counter is greater than or equal to 1000, you subtract 1000 from it and increment your millisecond counter, which should probably be a uint32_t so you can use it to time long periods of time.

But what if the overflow period is not an exact number of microseconds?  That's still fine, because it will probably be expressible in the form "N/M milliseconds", where N and M are whole numbers.  So every time the overflow happens, you increment your counter by N.  If the counter reaches M, you subtract M from the counter and add 1 to your millisecond counter.  (In the preivous paragraph, we only considered the M=1000 case, and I think by starting with that case it is easier to see what I'm talking about.)

You can either use an ISR to update your millisecond counter or do it in your main-line code by frequently checking for an overflow flag.  Either way would work.

So now you have a variable that counts up once per millisecond, like the Arduino millis() function, and you can use it for all sorts of millisecond-scale timing, including blinking LEDs.

The least-significant bit (bit 0) of the millisecond counter toggles once per millisecond, so its period is 2 ms.  The next bit up from that (bit 1) has a period of 4 ms.  Bit 10 has a period of 2048 ms.

So a simple way to blink your LED with a period of 2048 ms would be:

led_state = millisecond_counter >> 10 & 1;

(The code above uses bitwise operations to set led_state to be a copy of bit 10 from the millisecond counter.  Assuming you run this code frequently and write led_state out to the LED using the I/O registers, your LED would then have a period of 2048 ms.)

If you really care that your LED is slow by 2.4%, you could do something more complicated like this:

static uint16_t last_toggle = 0;
if ((uint16_t)(millisecond_counter - last_toggle) >= 1000)
{
  last_toggle += 1000;
  toggle_the_led();
}


问:
NoMethodError (undefined method `val' for #<Arel::Nodes::BindParam:0x007fe5554268b8>): app/controllers/posts_controller.rb:10:in `new'
I am new to rails. Everything is working fine locally but after deploying on heroku its giving me the above error.

Here is the posts_controllers action:

```<code> def new
   @post = current_user.posts.build
 end
</code>```

and here is the schema.rb file content:

```<code>  ActiveRecord::Schema.define(version: 20160516214156) do

  create_table "comments", force: :cascade do |t|
    t.text     "comment"
    t.integer  "post_id"
    t.integer  "user_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  add_index "comments", ["post_id"], name:    "index_comments_on_post_id"
  add_index "comments", ["user_id"], name: "index_comments_on_user_id"

  create_table "posts", force: :cascade do |t|
    t.text     "description"
    t.datetime "created_at",         null: false
    t.datetime "updated_at",         null: false
    t.string   "image_file_name"
    t.string   "image_content_type"
    t.integer  "image_file_size"
    t.datetime "image_updated_at"
    t.integer  "user_id"
  end

  create_table "users", force: :cascade do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
 end

 add_index "users", ["email"], name: "index_users_on_email", unique:   true
 add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

end
</code>```

答:
I came across this as well on Rails 4.2.0, in my case everything seemed to be ok, since I had the foreign_key specified, what fixed the issue was when the foreign_key and foreign_type were specified as a string, not as a symbol:</p>

```<code>has_many :answers, foreign_key: :assay_attempt_id  #this was not working
has_many :answers, foreign_key: 'assay_attempt_id' #this was ok
</code>```

Body:
I am new to rails. Everything is working fine locally but after deploying on heroku its giving me the above error.

Here is the posts_controllers action:

 def new
   @post = current_user.posts.build
 end

and here is the schema.rb file content:

  ActiveRecord::Schema.define(version: 20160516214156) do

  create_table "comments", force: :cascade do |t|
    t.text     "comment"
    t.integer  "post_id"
    t.integer  "user_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  add_index "comments", ["post_id"], name:    "index_comments_on_post_id"
  add_index "comments", ["user_id"], name: "index_comments_on_user_id"

  create_table "posts", force: :cascade do |t|
    t.text     "description"
    t.datetime "created_at",         null: false
    t.datetime "updated_at",         null: false
    t.string   "image_file_name"
    t.string   "image_content_type"
    t.integer  "image_file_size"
    t.datetime "image_updated_at"
    t.integer  "user_id"
  end

  create_table "users", force: :cascade do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
 end

 add_index "users", ["email"], name: "index_users_on_email", unique:   true
 add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

end

Body:
I came across this as well on Rails 4.2.0, in my case everything seemed to be ok, since I had the foreign_key specified, what fixed the issue was when the foreign_key and foreign_type were specified as a string, not as a symbol:

has_many :answers, foreign_key: :assay_attempt_id  #this was not working
has_many :answers, foreign_key: 'assay_attempt_id' #this was ok


问:
Spring Configuration creating complex data structure with yaml
Using yaml in my Spring-boot application (with snakeyaml dependency 1.16) I am attempting to create a @ConfigurationProperties based off of my application.yml file. I want to create a data structure like the json below which is a Map with String Keys and Array values.

```<code>mapName: {
    "key1": ["elem0","elem1"],
    "key2": ["hello","world"]
}
</code>```

Attempting to create a Spring configuration class as follows

```<code>@Component
@ConfigurationProperties(prefix = "channel-broker")
@EnableConfigurationProperties
public class BrokerConfiguration {

    private Map<String, Set<String>> broker = new HashMap<>();

    public Map<String, Set<String>> getBroker() {
        return broker;
    }
}
</code>```

I have tried the following for my yaml

```<code>channel-broker:
    broker: {message-delivery: ['all'], facebook: ['client1']}
</code>```

Attempt two

```<code>channel-broker:
      message-delivery: ['all']
      facebook: ['client1']
</code>```

Attempt three

```<code>channel-broker:
      message-delivery:
        - ['all']
      facebook:
        - ['client1']
</code>```

I have also tried initializing the HashMap in the @ConfigurationProperties  class as such ... new HashMap<String, Set<String>> this didn't work either

All attempts result in this error which makes me believe its an error when converting to the object not that there is anything wrong with the yaml syntax.

Caused by: org.springframework.beans.InvalidPropertyException: Invalid
  property 'brokerTest[message-delivery][0]' of bean class
  [my.classpackage.clasname]:
  Property referenced in indexed property path
  'brokerTest[message-delivery][0]' is neither an array nor a List nor a
  Map; returned value was [all]

Is it possible to create such an object? How would I accomplish this

-UPDATE-

If I change the Set to an ArrayList (or List interface) this works but that isn't what I'm looking for. changed to this

```<code>private Map<String, ArrayList<String>> brokerTest = new HashMap<>();
</code>```

but need this doesn't work with Set interface either:

```<code>private Map<String, HashSet<String>> brokerTest = new HashMap<>();
</code>```

答:
If you use [] then it's an array so arraylist works, for hashset/hashmap you need to use {} brackets.

```<code>channel-broker: {
    broker: {
        message-delivery:{'all', '123'},
        facebook:{'client1', 'cleant2'}
    }
}
</code>```

will work for hashset.

(hashmap example)

Body:
Using yaml in my Spring-boot application (with snakeyaml dependency 1.16) I am attempting to create a @ConfigurationProperties based off of my application.yml file. I want to create a data structure like the json below which is a Map with String Keys and Array values.

mapName: {
    "key1": ["elem0","elem1"],
    "key2": ["hello","world"]
}

Attempting to create a Spring configuration class as follows

@Component
@ConfigurationProperties(prefix = "channel-broker")
@EnableConfigurationProperties
public class BrokerConfiguration {

    private Map<String, Set<String>> broker = new HashMap<>();

    public Map<String, Set<String>> getBroker() {
        return broker;
    }
}

I have tried the following for my yaml

channel-broker:
    broker: {message-delivery: ['all'], facebook: ['client1']}

Attempt two

channel-broker:
      message-delivery: ['all']
      facebook: ['client1']

Attempt three

channel-broker:
      message-delivery:
        - ['all']
      facebook:
        - ['client1']

I have also tried initializing the HashMap in the @ConfigurationProperties  class as such ... new HashMap<String, Set<String>> this didn't work either

All attempts result in this error which makes me believe its an error when converting to the object not that there is anything wrong with the yaml syntax.

Caused by: org.springframework.beans.InvalidPropertyException: Invalid
  property 'brokerTest[message-delivery][0]' of bean class
  [my.classpackage.clasname]:
  Property referenced in indexed property path
  'brokerTest[message-delivery][0]' is neither an array nor a List nor a
  Map; returned value was [all]

Is it possible to create such an object? How would I accomplish this

-UPDATE-

If I change the Set to an ArrayList (or List interface) this works but that isn't what I'm looking for. changed to this

private Map<String, ArrayList<String>> brokerTest = new HashMap<>();

but need this doesn't work with Set interface either:

private Map<String, HashSet<String>> brokerTest = new HashMap<>();

Body:
If you use [] then it's an array so arraylist works, for hashset/hashmap you need to use {} brackets.

channel-broker: {
    broker: {
        message-delivery:{'all', '123'},
        facebook:{'client1', 'cleant2'}
    }
}

will work for hashset.

(hashmap example)


问:
How can I create an Array of ArrayLists?
I am wanting to create an array of arraylist like below:

```<code>ArrayList<Individual>[] group = new ArrayList<Individual>()[4];
</code>```

But it's not compiling. How can I do this?

答:
You can create like this
 ArrayList<Individual>[] group = (ArrayList<Individual>[])new ArrayList[4];

You have to create array of non generic type and then cast it into generic one.

ContentLicense: CC BY-SA 4.0

Body:
I am wanting to create an array of arraylist like below:

ArrayList<Individual>[] group = new ArrayList<Individual>()[4];

But it's not compiling. How can I do this?


问:
Converting a list into JSON with timestamp
I have a list of dictionaries like this:

```<code>  [{'accounts': [{'account_number': 'xx6747',
  'bank_name': 'Fargo Bank',
  'bankid': 85,
  'created_date_y': Timestamp('2015-05-20 08:08:33'),
  'item_account_id': 20086385.0,
  'item_id': 17764259,
  'user_id': 18},
  {'account_number': '*819',
 'bank_name': 'Central Alabama - Bank',
 'bankid': 88,
 'created_date_y': Timestamp('2015-05-20 08:08:33'),
 'item_account_id': 20637127.0,
 'item_id': 17761624,
 'user_id': 18},
 {'account_number': '*056',
'bank_name': 'Central Alabama - Bank',
'bankid': 88,
'created_date_y': Timestamp('2015-05-20 08:08:33'),
'item_account_id': 20087284.0,
'item_id': 17761624,
'user_id': 18},
'created_date_x': Timestamp('2014-03-11 10:48:42'),
'id': 18,
'password': 'NjgrUW9URGZz09',
'status': 1,
'uid': 97560,
'username': 'pepass@yahoo.com'},
{'accounts': [{'account_number': 791,
'bank_name': 'Chase - Bank',
'bankid': 855,
'created_date_y': Timestamp('2015-05-20 08:03:37'),
'item_account_id': 20090104.0,
'item_id': 17764260,
'user_id': 20},
{'account_number': 883,
'bank_name': 'Chase - Bank',
'bankid': 852,
'created_date_y': Timestamp('2015-05-20 08:03:37'),
'item_account_id': 20090105.0,
'item_id': 17764260,
'user_id': 20}],
'created_date_x': Timestamp('2014-03-11 13:42:20'),
'id': 20,
'password': 'bmJlbTN5dFo5QT09',
'status': 1,
'uid': 99408,
'username': 'hor@sanstore.com'}]
</code>```

It contains other items as well. The created_date_x and created_date_y are timestamps. I want to convert this list into a nested dictionary something like this:

```<code> {'id': 18,
  'password': 'NjgrUW9URGZz09',
  'status': 1,
  'uid': 97560,
  'username': 'pepass@yahoo.com'},
  'created_date_x': Timestamp('2014-03-11 10:48:42')
{'accounts':
       [{'account_number': 'xx6747',
       'bank_name': 'Fargo Bank',
       'bankid': 85,
       'created_date_y': Timestamp('2015-05-20 08:08:33'),
       'item_account_id': 20086385.0,
       'item_id': 17764259,
       'user_id': 18},
      {'account_number': '*819',
       'bank_name': 'Central Alabama - Bank',
       'bankid': 88,
       'created_date_y': Timestamp('2015-05-20 08:08:33'),
       'item_account_id': 20637127.0,
       'item_id': 17761624,
       'user_id': 18},
      {'account_number': '*056',
       'bank_name': 'Central Alabama - Bank',
       'bankid': 88,
       'created_date_y': Timestamp('2015-05-20 08:08:33'),
       'item_account_id': 20087284.0,
       'item_id': 17761624,
       'user_id': 18}]}}
</code>```

After converting it into a dictionary, I want to convert this into a JSON. The problem in converting it into json is that the timestamp fields give an error as timestamp is not json serializale. The code I have so far which does not work as I want is:

```<code> import pandas as pd
 #importing files
 df1 = pd.read_excel("C:\\Users\\Desktop\\yod_user.xlsx")
 df2 = pd.read_excel("C:\\Users\\Desktop\\yod_bank.xlsx")

 df1['created_date'] = df1['created_date'].astype(str)
 df2['created_date'] = df2['created_date'].astype(str)

 df1.dtypes
 #id               int64
 #username        object
 #password        object
 #uid              int64
 #created_date    object
 #status           int64
 #dtype: object
 df2.dtypes
 #user_id              int64
 #bankid                      int64
 #account_number             object
 #item_id              int64
 #item_account_id    float64
 #created_date               object
 #bank_name                  object
 #dtype: object

 df_merge = pd.merge(df1, df2, left_on = 'id', right_on ='user_id', how ='inner')
 df_merge.dtypes
 #id                        object
 #username                  object
 #password                  object
 #uid                       object
 #created_date_x            object
 #status                    object
 #user_id                   object
 #bankid                    object
 #account_number            object
 #item_id                   object
 #item_account_id           object
 #created_date_y            object
 #bank_name                 object
 #dtype: object
j = df_merge.groupby(['id', 'username', 'password',   'uid',created_date_x',  'status'], as_index=False)\
         .apply(lambda x: x[['account_number','user_id','bankid', 'item_id', 'item_account_id','created_date_y', 'bank_name' ]].to_dict('r'))\
         .reset_index()\
         .rename(columns={0:'accounts'})\

 j.dtypes
 #id                 int64
 #username          object
 #password          object
 #uid                int64
 #created_date_x    object
 #status             int64
 #accounts          object
 #dtype: object
 jdict = j.to_dict('records')

 import json
 json.dumps(jdict) #This gives an error: TypeError: Object of type     'Timestamp' is not JSON serializable
</code>```

If the initial list can be converted into a json like a dictionary that will also do. Please Help.

答:
Using astype() will work.  In your code, you're not actually changing the dtype of your date columns.

You have:

```<code> df1['created_date'].astype(str)
</code>```

You need:

```<code> df1['created_date'] = df1['created_date'].astype(str)
</code>```

Example:

```<code>dates = pd.date_range("2000","2002",freq="A")
data = {"created_date":dates, "value":range(len(dates))}
df = pd.DataFrame(data)

df
  created_date  value
0   2000-12-31      0
1   2001-12-31      1

(df["created_date"].dtype
# datetime64[ns]

df["created_date"] = df["created_date"].astype(str)

df["created_date"].dtype
# object

json.dumps(df.to_dict('records'))
# '[{"created_date": "2000-12-31", "value": 0},
    {"created_date": "2001-12-31", "value": 1}]'
</code>```

Body:
I have a list of dictionaries like this:

  [{'accounts': [{'account_number': 'xx6747',
  'bank_name': 'Fargo Bank',
  'bankid': 85,
  'created_date_y': Timestamp('2015-05-20 08:08:33'),
  'item_account_id': 20086385.0,
  'item_id': 17764259,
  'user_id': 18},
  {'account_number': '*819',
 'bank_name': 'Central Alabama - Bank',
 'bankid': 88,
 'created_date_y': Timestamp('2015-05-20 08:08:33'),
 'item_account_id': 20637127.0,
 'item_id': 17761624,
 'user_id': 18},
 {'account_number': '*056',
'bank_name': 'Central Alabama - Bank',
'bankid': 88,
'created_date_y': Timestamp('2015-05-20 08:08:33'),
'item_account_id': 20087284.0,
'item_id': 17761624,
'user_id': 18},
'created_date_x': Timestamp('2014-03-11 10:48:42'),
'id': 18,
'password': 'NjgrUW9URGZz09',
'status': 1,
'uid': 97560,
'username': 'pepass@yahoo.com'},
{'accounts': [{'account_number': 791,
'bank_name': 'Chase - Bank',
'bankid': 855,
'created_date_y': Timestamp('2015-05-20 08:03:37'),
'item_account_id': 20090104.0,
'item_id': 17764260,
'user_id': 20},
{'account_number': 883,
'bank_name': 'Chase - Bank',
'bankid': 852,
'created_date_y': Timestamp('2015-05-20 08:03:37'),
'item_account_id': 20090105.0,
'item_id': 17764260,
'user_id': 20}],
'created_date_x': Timestamp('2014-03-11 13:42:20'),
'id': 20,
'password': 'bmJlbTN5dFo5QT09',
'status': 1,
'uid': 99408,
'username': 'hor@sanstore.com'}]

It contains other items as well. The created_date_x and created_date_y are timestamps. I want to convert this list into a nested dictionary something like this:

 {'id': 18,
  'password': 'NjgrUW9URGZz09',
  'status': 1,
  'uid': 97560,
  'username': 'pepass@yahoo.com'},
  'created_date_x': Timestamp('2014-03-11 10:48:42')
{'accounts':
       [{'account_number': 'xx6747',
       'bank_name': 'Fargo Bank',
       'bankid': 85,
       'created_date_y': Timestamp('2015-05-20 08:08:33'),
       'item_account_id': 20086385.0,
       'item_id': 17764259,
       'user_id': 18},
      {'account_number': '*819',
       'bank_name': 'Central Alabama - Bank',
       'bankid': 88,
       'created_date_y': Timestamp('2015-05-20 08:08:33'),
       'item_account_id': 20637127.0,
       'item_id': 17761624,
       'user_id': 18},
      {'account_number': '*056',
       'bank_name': 'Central Alabama - Bank',
       'bankid': 88,
       'created_date_y': Timestamp('2015-05-20 08:08:33'),
       'item_account_id': 20087284.0,
       'item_id': 17761624,
       'user_id': 18}]}}

After converting it into a dictionary, I want to convert this into a JSON. The problem in converting it into json is that the timestamp fields give an error as timestamp is not json serializale. The code I have so far which does not work as I want is:

 import pandas as pd
 #importing files
 df1 = pd.read_excel("C:\\Users\\Desktop\\yod_user.xlsx")
 df2 = pd.read_excel("C:\\Users\\Desktop\\yod_bank.xlsx")

 df1['created_date'] = df1['created_date'].astype(str)
 df2['created_date'] = df2['created_date'].astype(str)

 df1.dtypes
 #id               int64
 #username        object
 #password        object
 #uid              int64
 #created_date    object
 #status           int64
 #dtype: object
 df2.dtypes
 #user_id              int64
 #bankid                      int64
 #account_number             object
 #item_id              int64
 #item_account_id    float64
 #created_date               object
 #bank_name                  object
 #dtype: object

 df_merge = pd.merge(df1, df2, left_on = 'id', right_on ='user_id', how ='inner')
 df_merge.dtypes
 #id                        object
 #username                  object
 #password                  object
 #uid                       object
 #created_date_x            object
 #status                    object
 #user_id                   object
 #bankid                    object
 #account_number            object
 #item_id                   object
 #item_account_id           object
 #created_date_y            object
 #bank_name                 object
 #dtype: object
j = df_merge.groupby(['id', 'username', 'password',   'uid',created_date_x',  'status'], as_index=False)\
         .apply(lambda x: x[['account_number','user_id','bankid', 'item_id', 'item_account_id','created_date_y', 'bank_name' ]].to_dict('r'))\
         .reset_index()\
         .rename(columns={0:'accounts'})\

 j.dtypes
 #id                 int64
 #username          object
 #password          object
 #uid                int64
 #created_date_x    object
 #status             int64
 #accounts          object
 #dtype: object
 jdict = j.to_dict('records')

 import json
 json.dumps(jdict) #This gives an error: TypeError: Object of type     'Timestamp' is not JSON serializable

If the initial list can be converted into a json like a dictionary that will also do. Please Help.

Body:
Using astype() will work.  In your code, you're not actually changing the dtype of your date columns.

You have:

 df1['created_date'].astype(str)

You need:

 df1['created_date'] = df1['created_date'].astype(str)

Example:

dates = pd.date_range("2000","2002",freq="A")
data = {"created_date":dates, "value":range(len(dates))}
df = pd.DataFrame(data)

df
  created_date  value
0   2000-12-31      0
1   2001-12-31      1

(df["created_date"].dtype
# datetime64[ns]

df["created_date"] = df["created_date"].astype(str)

df["created_date"].dtype
# object

json.dumps(df.to_dict('records'))
# '[{"created_date": "2000-12-31", "value": 0},
    {"created_date": "2001-12-31", "value": 1}]'


问:
Javascript dynamic nested objects
I was looking into how to create a dynamic nested objects from a string, for example, a string "obj1.obj2.obj3.obj4", how do you turn that into nested objects with the same order.

I googled around and i found this nice and clean code, but i did not understand how it worked, and there was no explanation on the page where it was written, can someone explain how this works please?

here is the code:

var s = "key1.key2.key3.key4";

var a = s.split('.');

var obj = {};
var temp = obj;

for (var k =0; k < a.length; k++) {
 temp = temp[a[k]] = {};
  console.log(temp);
}

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

<p>why is there a <code>var temp = obj</code>?</p>

<p>what does this line do?:</p>

```temp = temp[a[k]] = {};
```

also, the console.log(temp) inside the loop always logs an empty object {}, why?

Thanks for the feedback!

答:
Have added comments in code, hope it will be useful</p>

```<code>var s = "key1.key2.key3.key4";
//split the string by dot(.) and create an array
var a = s.split('.');

//Creating an empty object
var obj = {};
var temp = obj;

//looping over the array
for (var k = 0; k < a.length; k++) {
  //a[k] will be key1,key2,key3....
  // temp is an object,square bracket is use to create a object key
  // using a variable name
  // variable name here is key1,key2....
  //temp[a[k]] will initialize an empty object
  temp = temp[a[k]] = {};
  console.log(temp);
}

console.log(obj);
</code>```

Title: Javascript dynamic nested objects

Body:
I was looking into how to create a dynamic nested objects from a string, for example, a string "obj1.obj2.obj3.obj4", how do you turn that into nested objects with the same order.

I googled around and i found this nice and clean code, but i did not understand how it worked, and there was no explanation on the page where it was written, can someone explain how this works please?

here is the code:

var s = "key1.key2.key3.key4";

var a = s.split('.');

var obj = {};
var temp = obj;

for (var k =0; k < a.length; k++) {
 temp = temp[a[k]] = {};
  console.log(temp);
}

console.log(obj);

why is there a var temp = obj?

what does this line do?:

temp = temp[a[k]] = {};

also, the console.log(temp) inside the loop always logs an empty object {}, why?

Thanks for the feedback!

Body:
Have added comments in code, hope it will be useful

var s = "key1.key2.key3.key4";
//split the string by dot(.) and create an array
var a = s.split('.');

//Creating an empty object
var obj = {};
var temp = obj;

//looping over the array
for (var k = 0; k < a.length; k++) {
  //a[k] will be key1,key2,key3....
  // temp is an object,square bracket is use to create a object key
  // using a variable name
  // variable name here is key1,key2....
  //temp[a[k]] will initialize an empty object
  temp = temp[a[k]] = {};
  console.log(temp);
}

console.log(obj);


问:
How to take image as input in reactnative
Just the way as TextInput is used I need the user to upload an image in the field where it is asked for

For eg:  Upload Image: ___________ +

and user clicks the button to upload an image from gallery or take a photo from camera app of the phone

答:
This library do exactly what you want </p>

<p>React Image picker library </p>

Body:
This library do exactly what you want

React Image picker library


问:
How to validate a field's input value using regular expression in autoform in meteor on client side?
i'm new to meteor framework and i'm building a form which needs validation of fields using regular expression in client side.

schema.js

```<code>   PRODUCT_BUILD:{
    type:String,
    label:' PRODUCT_BUILD',
    defaultValue:"PRODUCT:latest",
    regEx: /^(PRODUCT)((\/(([0-9]+\.)+[0-9]+))|(\:(latest)))/
  },
</code>```

I have written the message as

```<code>SimpleSchema.messages({
 'regEx PRODUCT_BUILD': "Can have eg: PRODUCT/10.X.X.1234 or PRODUCT:latest",
});
</code>```

test.html

```<code> {{> afQuickField name='PRODUCT_BUILD'}}
</code>```

I have entered all the fields which regex needs, but when I give the invalid input it dosen't check the regex and also the error message.

Can anyone tell me what i can do to correct it?

答:
As for as I remember, the way simple schema messages are handled have been changed in the recent versions of simple-schema / autoform.

Ideally, it used to be something like this (which I'm not sure if it's going to work on your version):

```<code>SimpleSchema.messages({
  "regEx PRODUCT_BUILD": [
    { msg: "Can have eg: PRODUCT/10.X.X.1234 or PRODUCT:latest" }
  ]
});
</code>```

There is an alternate method using custom field validation as below:

```<code>  PRODUCT_BUILD:{
    type:String,
    label:' PRODUCT_BUILD',
    defaultValue:"PRODUCT:latest",
    custom: function(){
            if(this.value){
               // do the regex validation using normal JS
              //if validation fails,
               return "regexError"
             }
           }
  },

SimpleSchema.messages({
  "regexError":  "Can have eg: PRODUCT/10.X.X.1234 or PRODUCT:latest"

});
</code>```

Body:
i'm new to meteor framework and i'm building a form which needs validation of fields using regular expression in client side.

schema.js

   PRODUCT_BUILD:{
    type:String,
    label:' PRODUCT_BUILD',
    defaultValue:"PRODUCT:latest",
    regEx: /^(PRODUCT)((\/(([0-9]+\.)+[0-9]+))|(\:(latest)))/
  },

I have written the message as

SimpleSchema.messages({
 'regEx PRODUCT_BUILD': "Can have eg: PRODUCT/10.X.X.1234 or PRODUCT:latest",
});

test.html

 {{> afQuickField name='PRODUCT_BUILD'}}

I have entered all the fields which regex needs, but when I give the invalid input it dosen't check the regex and also the error message.

Can anyone tell me what i can do to correct it?

Body:
As for as I remember, the way simple schema messages are handled have been changed in the recent versions of simple-schema / autoform.

Ideally, it used to be something like this (which I'm not sure if it's going to work on your version):

SimpleSchema.messages({
  "regEx PRODUCT_BUILD": [
    { msg: "Can have eg: PRODUCT/10.X.X.1234 or PRODUCT:latest" }
  ]
});

There is an alternate method using custom field validation as below:

  PRODUCT_BUILD:{
    type:String,
    label:' PRODUCT_BUILD',
    defaultValue:"PRODUCT:latest",
    custom: function(){
            if(this.value){
               // do the regex validation using normal JS
              //if validation fails,
               return "regexError"
             }
           }
  },

SimpleSchema.messages({
  "regexError":  "Can have eg: PRODUCT/10.X.X.1234 or PRODUCT:latest"

});


问:
database datatype performance: int or string
I'm storing phone country codes. They range from 1 to about 300. What's going to be more performant for datatype: int or string? I'm using SQL server 2008 and linq-to-sql.

Thanks.

答: If you're really worried about space and performance then you could use a smallint (which equates to a int16). This will mean that the data will only take 2 bytes of storage (and 2 bytes in memory).</p>

ContentLicense: CC BY-SA 2.5


问:
How can I use array using "row index" in python?
I made code in python

There's so many arrays, what I want to do is
extract value of arrays and add it to array_matrix in row
and make a label too
and every circle row = row + 1

This is some part of my code,
I know the form of
array_matrix[row,0:3] = array1(u) and label_matrix(row,:) =[1 0]
isn't correct.

Please let me know what to change.

```<code>row = 0
array_matrix = []

for n in range(0,64,8):
  array1.append(dct_temp[0,1])
  array2.append(dct_temp[1,0])
  array3.append(dct_temp[0,2])
  array4.append(dct_temp[1,1])

  u = (index-1,index+1,1)  # index = index of max value of array
  array_matrix[row,0:3] = array1(u)
  array_matrix[row,4:7] = array2(u)
  array_matrix[row,8:11] = array3(u)
  array_matrix[row,12:15] = array4(u)

  label_matrix(row,:) =[0 1]

  row = row + 1
</code>```

答:
You could use a dict

```<code>dict = {"labelA",arrayVal}
list1 = [1, 2, 3, 4, 5]
list2 = [123, 234, 456]
d = {'a': [], 'b': []}
d['a'].append(list1)
d['a'].append(list2)
print d['a']
</code>```

For your array you can use a notation like

```<code>array[FROM INDEX ROW:TOINDEX ROW, :]
</code>```

The : means take all cols
FROM INDEX ROW:TOINDEX ROW means take all values from the FROM INDEX upto the TOINDEX.

Body:
I made code in python

There's so many arrays, what I want to do is
extract value of arrays and add it to array_matrix in row
and make a label too
and every circle row = row + 1

This is some part of my code,
I know the form of
array_matrix[row,0:3] = array1(u) and label_matrix(row,:) =[1 0]
isn't correct.

Please let me know what to change.

row = 0
array_matrix = []

for n in range(0,64,8):
  array1.append(dct_temp[0,1])
  array2.append(dct_temp[1,0])
  array3.append(dct_temp[0,2])
  array4.append(dct_temp[1,1])

  u = (index-1,index+1,1)  # index = index of max value of array
  array_matrix[row,0:3] = array1(u)
  array_matrix[row,4:7] = array2(u)
  array_matrix[row,8:11] = array3(u)
  array_matrix[row,12:15] = array4(u)

  label_matrix(row,:) =[0 1]

  row = row + 1

Body:
You could use a dict

dict = {"labelA",arrayVal}
list1 = [1, 2, 3, 4, 5]
list2 = [123, 234, 456]
d = {'a': [], 'b': []}
d['a'].append(list1)
d['a'].append(list2)
print d['a']

For your array you can use a notation like

array[FROM INDEX ROW:TOINDEX ROW, :]

The : means take all cols
FROM INDEX ROW:TOINDEX ROW means take all values from the FROM INDEX upto the TOINDEX.


问:
how to modify json Object in javascript
I am trying to load sample csv files into project read the content and display according to the filed. I done uploading and file writing but could not optimize the json structure properly for future use case

Here is my sample code :

```<code>let csvToJson = require('convert-csv-to-json');
let json = csvToJson.getJsonFromCsv(filepath);
var lines = [];
for(let i=0; i<json.length;i++){
console.log(json[i]);
lines.push(json[i])
}
console.log(lines);
var result3=JSON.stringify(lines);
result3 = result3.replace(/\r?\n|\r/g, " ");
console.log(result3);
</code>```

My output:

console.log(json[i]) =

```<code>Object {name,age,address: "asd,20,"12/76, 11th cross"
"}
browser.js:5358 Object {name,age,address: "dff,30,"33, 11th cross"
"}
browser.js:5358 Object {name,age,address: "f,22,"7g/22, 12th cross"
"}
browser.js:5358 Object {name,age,address: "ghth,55,"4h, 13th cross"
"}
</code>```

console.log(lines) = [Object, Object, Object, Object, Object]

console.log(result3)

```<code>[{"name,age,address":"asd,20,\"12/76, 11th cross\"\r"},
{"name,age,address":"dff,30,\"33, 11th cross\"\r"},
{"name,age,address":"f,22,\"7g/22, 12th cross\"\r"},
{"name,age,address":"ghth,55,\"4h, 13th cross\"\r"},
{"name,age,address":"fhg,44,\"6t, 10th cross\"\r"}]
</code>```

My expected output:

```<code>{name:[],age:[],address:[]}
</code>```

i.e I want all "name" values should store in the  name key similar to "age" and "address". Can anybody help me how to do this?

答:
This should work</p>

```<code>let csvToJson = require('convert-csv-to-json');
let json = csvToJson.getJsonFromCsv(filepath);
let obj = JSON.parse(json);
let finalresult = obj.reduce((result, item) => {
    Object.entries(item).forEach(([key, value]) => {
       result[key] = result[key] || [];
       result[key].push(value);
    });
    return result;
}, {});
</code>```

Title: how to modify json Object in javascript

Body:
I am trying to load sample csv files into project read the content and display according to the filed. I done uploading and file writing but could not optimize the json structure properly for future use case

Here is my sample code :

let csvToJson = require('convert-csv-to-json');
let json = csvToJson.getJsonFromCsv(filepath);
var lines = [];
for(let i=0; i<json.length;i++){
console.log(json[i]);
lines.push(json[i])
}
console.log(lines);
var result3=JSON.stringify(lines);
result3 = result3.replace(/\r?\n|\r/g, " ");
console.log(result3);

My output:

console.log(json[i]) =

Object {name,age,address: "asd,20,"12/76, 11th cross"
"}
browser.js:5358 Object {name,age,address: "dff,30,"33, 11th cross"
"}
browser.js:5358 Object {name,age,address: "f,22,"7g/22, 12th cross"
"}
browser.js:5358 Object {name,age,address: "ghth,55,"4h, 13th cross"
"}

console.log(lines) = [Object, Object, Object, Object, Object]

console.log(result3)

[{"name,age,address":"asd,20,\"12/76, 11th cross\"\r"},
{"name,age,address":"dff,30,\"33, 11th cross\"\r"},
{"name,age,address":"f,22,\"7g/22, 12th cross\"\r"},
{"name,age,address":"ghth,55,\"4h, 13th cross\"\r"},
{"name,age,address":"fhg,44,\"6t, 10th cross\"\r"}]

My expected output:

{name:[],age:[],address:[]}

i.e I want all "name" values should store in the  name key similar to "age" and "address". Can anybody help me how to do this?

Body:
This should work

let csvToJson = require('convert-csv-to-json');
let json = csvToJson.getJsonFromCsv(filepath);
let obj = JSON.parse(json);
let finalresult = obj.reduce((result, item) => {
    Object.entries(item).forEach(([key, value]) => {
       result[key] = result[key] || [];
       result[key].push(value);
    });
    return result;
}, {});


问:
Why is "std::is_pointer<std::nullptr_t>::value" equal to false?
I read about std::is_pointer in C++.

Then I wrote the program and check whether T is a pointer type or not using std::is_pointer.

```<code>#include <iostream>

int main()
{
    std::cout<<std::boolalpha;
    std::cout<<"char : " <<std::is_pointer<char>::value<<std::endl; // OK
    std::cout<<"char * : "<<std::is_pointer<char *>::value<<std::endl; // OK
    std::cout<<"char ** : "<<std::is_pointer<char **>::value<<std::endl; // OK
    std::cout<<"char *** : "<<std::is_pointer<char ***>::value<<std::endl; // OK
    std::cout<<"std::nullptr_t : "<<std::is_pointer<std::nullptr_t>::value<<std::endl;  // Not ok, Why false??
}
</code>```

Output : [Wandbox Demo]

```<code>char : false
char * : true
char ** : true
char *** : true
std::nullptr_t : false
</code>```

Why is std::is_pointer<std::nullptr_t>::value equal to false?

答:
As unintuitive as it sounds, std::nullptr_t is not a pointer type. For instance, you cannot dereference a std::nullptr_t, so it'd be pretty weird if is_pointer_type<nullptr_t>::value was true.

It is merely convertible to any pointer type.

Body:
I read about std::is_pointer in C++.

Then I wrote the program and check whether T is a pointer type or not using std::is_pointer.

#include <iostream>

int main()
{
    std::cout<<std::boolalpha;
    std::cout<<"char : " <<std::is_pointer<char>::value<<std::endl; // OK
    std::cout<<"char * : "<<std::is_pointer<char *>::value<<std::endl; // OK
    std::cout<<"char ** : "<<std::is_pointer<char **>::value<<std::endl; // OK
    std::cout<<"char *** : "<<std::is_pointer<char ***>::value<<std::endl; // OK
    std::cout<<"std::nullptr_t : "<<std::is_pointer<std::nullptr_t>::value<<std::endl;  // Not ok, Why false??
}

Output : [Wandbox Demo]

char : false
char * : true
char ** : true
char *** : true
std::nullptr_t : false

Why is std::is_pointer<std::nullptr_t>::value equal to false?


问:
Cloning an array in Javascript/Typescript
I have array of two objects:

```<code>genericItems: Item[] = [];
backupData: Item[] = [];
</code>```

I am populating my HTML table with genericItemsdata. The table is modifiable. There is a reset button to undo all changes done with backUpData. This array is populated by a service:

```<code>getGenericItems(selected: Item) {
this.itemService.getGenericItems(selected).subscribe(
  result => {
     this.genericItems = result;
  });
     this.backupData = this.genericItems.slice();
  }
</code>```

My idea was that, the user changes will get reflected in first array and second array can be used as backup for reset operation. The issue I am facing here is when the user modifies the table (genericItems[]) the second array backupData also gets modified.

How is this happening and how to prevent this?

答:
the easiest way to clone an array is

```<code>backUpData = genericItems.concat();
</code>```

This will create a new memory for the array indexes

Body:
I have array of two objects:

genericItems: Item[] = [];
backupData: Item[] = [];

I am populating my HTML table with genericItemsdata. The table is modifiable. There is a reset button to undo all changes done with backUpData. This array is populated by a service:

getGenericItems(selected: Item) {
this.itemService.getGenericItems(selected).subscribe(
  result => {
     this.genericItems = result;
  });
     this.backupData = this.genericItems.slice();
  }

My idea was that, the user changes will get reflected in first array and second array can be used as backup for reset operation. The issue I am facing here is when the user modifies the table (genericItems[]) the second array backupData also gets modified.

How is this happening and how to prevent this?

Body:
the easiest way to clone an array is

backUpData = genericItems.concat();

This will create a new memory for the array indexes


答:
why is there a var temp = obj?

obj is a variable to hold the completed object. temp is a variable to hold each intermediate step of the object being built. temp starts out with the same value as obj so that the first iteration of the loop adds on to obj.

what does this line do?:

```<code>temp = temp[a[k]] = {};
</code>```

Assign an empty object to a property in temp with the name a[k], where a[k] is one of the values in a.

Assign that new empty object to temp.

This could be written separately as two lines:

```<code>temp[a[k]] = {};
temp = temp[a[k]];
</code>```

also, the console.log(temp) inside the loop always logs an empty object {}, why?

Because the previous line assigns an empty object to temp.

Title: Javascript dynamic nested objects

Body:
why is there a var temp = obj?

obj is a variable to hold the completed object. temp is a variable to hold each intermediate step of the object being built. temp starts out with the same value as obj so that the first iteration of the loop adds on to obj.

what does this line do?:

temp = temp[a[k]] = {};

Assign an empty object to a property in temp with the name a[k], where a[k] is one of the values in a.

Assign that new empty object to temp.

This could be written separately as two lines:

temp[a[k]] = {};
temp = temp[a[k]];

also, the console.log(temp) inside the loop always logs an empty object {}, why?

Because the previous line assigns an empty object to temp.


问:
how to do pagination with elasticsearch? from vs scroll API
I'm using elasticsearch as DB to store a large batch of log data.
I know there are 2 ways to do pagination:

Use size and from API

Use scroll API

Now I'm using 'from' to do pagination.Get page and size parameters from front end,and at back end(Java)

```<code>searchSourceBuilder.size(size);
searchSourceBuilder.from(page * size);
</code>```

However, if page*size > 10000, an exception thrown from ES.

Can I use scroll API to do pagination?

I know that if I use scroll API, the searchResponse object will return me a _scroll_id, which looks like a base64 string.

How can I control page and size?

It seems Scroll API only support successive page number?

答:
What you said is correct!

You can't do traditional pagination by using scroll API.

I might suggest you to look the Search After API

This might not help you to cover your requirement!

The From / Size default max result size is 10,000.

As it is mentioned here

Note that from + size can not be more than the index.max_result_window index setting which defaults to 10,000

So if you somehow increase the index.max_result_window setting persistent then it will increase the max number of search result! But this might not be the solution but decrease the limitation.

Remember this above solution might hamper the performance of your ES server. Read through all the posts here.

My suggestion is to use Scroll API and change the pagination style

Body:
I'm using elasticsearch as DB to store a large batch of log data.
I know there are 2 ways to do pagination:

Use size and from API

Use scroll API

Now I'm using 'from' to do pagination.Get page and size parameters from front end,and at back end(Java)

searchSourceBuilder.size(size);
searchSourceBuilder.from(page * size);

However, if page*size > 10000, an exception thrown from ES.

Can I use scroll API to do pagination?

I know that if I use scroll API, the searchResponse object will return me a _scroll_id, which looks like a base64 string.

How can I control page and size?

It seems Scroll API only support successive page number?


问:
Xamarin.Forms Entry.Height is bigger than Label.Height
I have a Xamarin.Forms grid that I'm setting up and I've noticed that the Entry fields are a lot higher (3 - 4 times) than label fields. I can't see any obvious reason why this would be the case.

```<code><Grid RowSpacing="0" ColumnSpacing="0">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>

    <Label Grid.Row="0" Grid.Column="0" Text="G/C" FontSize="Small"/>
    <Entry Grid.Row="1" Grid.Column="0" FontSize="Small" BackgroundColor="Red"/>
</Grid>
</code>```

This results in something like this (on the default Android Emulator, installed 2 days ago);

Does anyone have any suggestions as to why the Entry height is so much bigger and what I can do to reduce it (obviously HeightRequest, but I'd rather avoid if possible)?

答: Use Star (*) as the height for the rows, this will make all rows have the same height. Auto will set the rows to a height that the control is just fitting in.

Body:
I have a Xamarin.Forms grid that I'm setting up and I've noticed that the Entry fields are a lot higher (3 - 4 times) than label fields. I can't see any obvious reason why this would be the case.

<Grid RowSpacing="0" ColumnSpacing="0">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>

    <Entry Grid.Row="1" Grid.Column="0" FontSize="Small" BackgroundColor="Red"/>
</Grid>

This results in something like this (on the default Android Emulator, installed 2 days ago);

Does anyone have any suggestions as to why the Entry height is so much bigger and what I can do to reduce it (obviously HeightRequest, but I'd rather avoid if possible)?


问:
PCIE region not aligned, and not consistent
I am developing a PCIE device driver for openwrt, and I met a data bus error when trying to access the io-memory in timer interrupt, which I mentioned in my last question. After lots of research I think I might have found the reason, but I am unable to solve it. Below are my troubles.

Last week I found out that the pcie region size might have changed during system startup. The region size of bar0 is 4096 in my driver (return from pci_resource_len) and the region size is 4097 in lspci -vv, which breaks the page size of linux kernel. By reading the source code of pciutil, I find that lspci command fetch the pcie information from /sys/devices/pci0000:00/0000:00:00.0/resouce file. So I remove all my custom components and run the original openwrt on my router. By cat /sys/devices/pci0000:00/0000:00:00.0/resouce, the first line of the result (bar0) is

```<code>0x0000000010008000 0x0000000010009000 0x0000000000040200
</code>```

Moreover, I also check the content of /proc/iomem, and the content related to PCIE is

```<code>10000000-13ffffff : mem_base
    10000000-13ffffff : PCI memory space
        10000000-10007fff : 0000:00:00.0
        10008000-10008fff : 0000:00:00.0
</code>```

It is super weird that the region size of bar0 indicated by the two files above is different! According to the mechanism of PCIE, the region size should always be the power of 2. How come the region size becomes 4097?

答:
After spending weeks reading source code of linux kernel, I find out that this is a bug of linux kernel 4.4.14.

The content of /sys/devices/pci0000:00/0000:00:00.0/resouce is generated through function resource_show in file drivers/pci/pci-sysfs.c. The related code is

```<code>for (i = 0; i < max; i++) {
    struct resource *res =  &pci_dev->resource[i];
    pci_resource_to_user(pci_dev, i, res, &start, &end);
    str += sprintf(str, "0x%016llx 0x%016llx 0x%016llx\n",
               (unsigned long long)start,
               (unsigned long long)end,
               (unsigned long long)res->flags);
}
</code>```

The function pci_resource_to_user actually invoked is located in arch/mips/include/asm/pci.h

```<code>static inline void pci_resource_to_user(const struct pci_dev *dev, int bar,
        const struct resource *rsrc, resource_size_t *start,
        resource_size_t *end)
{
    phys_addr_t size = resource_size(rsrc);

    *start = fixup_bigphys_addr(rsrc->start, size);
    *end = rsrc->start + size;
}
</code>```

The calculation of *end is wrong and should be replace by

```<code>*end = rsrc->start + size - (size ? 1 : 0)
</code>```

Body:
I am developing a PCIE device driver for openwrt, and I met a data bus error when trying to access the io-memory in timer interrupt, which I mentioned in my last question. After lots of research I think I might have found the reason, but I am unable to solve it. Below are my troubles.

Last week I found out that the pcie region size might have changed during system startup. The region size of bar0 is 4096 in my driver (return from pci_resource_len) and the region size is 4097 in lspci -vv, which breaks the page size of linux kernel. By reading the source code of pciutil, I find that lspci command fetch the pcie information from /sys/devices/pci0000:00/0000:00:00.0/resouce file. So I remove all my custom components and run the original openwrt on my router. By cat /sys/devices/pci0000:00/0000:00:00.0/resouce, the first line of the result (bar0) is

0x0000000010008000 0x0000000010009000 0x0000000000040200

Moreover, I also check the content of /proc/iomem, and the content related to PCIE is

10000000-13ffffff : mem_base
    10000000-13ffffff : PCI memory space
        10000000-10007fff : 0000:00:00.0
        10008000-10008fff : 0000:00:00.0

It is super weird that the region size of bar0 indicated by the two files above is different! According to the mechanism of PCIE, the region size should always be the power of 2. How come the region size becomes 4097?

Body:
After spending weeks reading source code of linux kernel, I find out that this is a bug of linux kernel 4.4.14.

The content of /sys/devices/pci0000:00/0000:00:00.0/resouce is generated through function resource_show in file drivers/pci/pci-sysfs.c. The related code is

for (i = 0; i < max; i++) {
    struct resource *res =  &pci_dev->resource[i];
    pci_resource_to_user(pci_dev, i, res, &start, &end);
    str += sprintf(str, "0x%016llx 0x%016llx 0x%016llx\n",
               (unsigned long long)start,
               (unsigned long long)end,
               (unsigned long long)res->flags);
}

The function pci_resource_to_user actually invoked is located in arch/mips/include/asm/pci.h

static inline void pci_resource_to_user(const struct pci_dev *dev, int bar,
        const struct resource *rsrc, resource_size_t *start,
        resource_size_t *end)
{
    phys_addr_t size = resource_size(rsrc);

    *start = fixup_bigphys_addr(rsrc->start, size);
    *end = rsrc->start + size;
}

The calculation of *end is wrong and should be replace by

*end = rsrc->start + size - (size ? 1 : 0)