问:
Bug in my for-loop to iterate over data frame
I am working on a data frame and have extracted on the of the columns with hour data from 0 t0 23.  I am adding one more column as type of the day based on hour.  I had executed below for loop but getting error.  Can somebody help me what is wrong with below syntax and how to correct the same.</p>

```<code>for(i in data$Requesthours) {
   if(data$Requesthours>=0 & data$Requesthours<3) {
     data$Partoftheday <- "Midnight"
   } else if(data$Requesthours>=3 & data$Requesthours<6) {
     data$Partoftheday <- "Early Morning"
   } else if(data$Requesthours>=6 & data$Requesthours<12) {
     data$Partoftheday <- "Morning"
   } else if(data$Requesthours>=12 & data$Requesthours<16) {
     data$Partoftheday <- "Afternoon"
   } else if(data$Requesthours>=16 & data$Requesthours<20) {
     data$Partoftheday <- "Evening"
   } else if(data$Requesthours>=20 & data$Requesthours<=23) {
     data$Partoftheday <- "Night"
   }
}
</code>```

答:
if(data$Requesthours>=0 & data$Requesthours<3) (and other similar ifs) make no sense since data$Requesthours is a vector.  You should try either of the following:

Solution 1:

```<code>for(i in seq(length(data$Requesthours))) {
    if(data$Requesthours[i]>=0 & data$Requesthours[i]<3)
        data$Partoftheday[i] <- "Midnight"
    ....
}
</code>```

This solution is slow like hell and really ugly, but it would work.

Solution 2:

```<code>data$Partoftheday[data$Requesthours>=0 & data$Requesthours<3] <- "Midnight"
...
</code>```

Solution 3 = what was proposed by smci

Body:
I am working on a data frame and have extracted on the of the columns with hour data from 0 t0 23.  I am adding one more column as type of the day based on hour.  I had executed below for loop but getting error.  Can somebody help me what is wrong with below syntax and how to correct the same.

for(i in data$Requesthours) {
   if(data$Requesthours>=0 & data$Requesthours<3) {
     data$Partoftheday <- "Midnight"
   } else if(data$Requesthours>=3 & data$Requesthours<6) {
     data$Partoftheday <- "Early Morning"
   } else if(data$Requesthours>=6 & data$Requesthours<12) {
     data$Partoftheday <- "Morning"
   } else if(data$Requesthours>=12 & data$Requesthours<16) {
     data$Partoftheday <- "Afternoon"
   } else if(data$Requesthours>=16 & data$Requesthours<20) {
     data$Partoftheday <- "Evening"
   } else if(data$Requesthours>=20 & data$Requesthours<=23) {
     data$Partoftheday <- "Night"
   }
}

Body:
if(data$Requesthours>=0 & data$Requesthours<3) (and other similar ifs) make no sense since data$Requesthours is a vector.  You should try either of the following:

Solution 1:

for(i in seq(length(data$Requesthours))) {
    if(data$Requesthours[i]>=0 & data$Requesthours[i]<3)
        data$Partoftheday[i] <- "Midnight"
    ....
}

This solution is slow like hell and really ugly, but it would work.

Solution 2:

data$Partoftheday[data$Requesthours>=0 & data$Requesthours<3] <- "Midnight"
...

Solution 3 = what was proposed by smci


来源: stackexchange

问:
dataframe manipulation with python
so I'm really new to python and the whole dataframe manipulation topic. What I'm trying to do is to ask the user for a min year and a max year, then list the name and year for the provided range.
I have tried many different approaches, the last one I tried and though was going to be successful was this:

```<code> yearInputMin = int(input("Please enter min year: "))
    yearInputMax = int(input("Please enter max year: "))

    for row in my_data:
        year = int(row['year'])
        if year <= yearInputMax and year >= yearInputMin:
            print
            year
</code>```

I'm focusing more on the year aspect because that is the area I'm having most difficulty in.

Any ideas of how to achieve this?

答:
dataframes are very easy to search through with ".loc", try this:

```<code>yearInputMin = int(input("Please enter min year: "))
yearInputMax = int(input("Please enter max year: "))

my_data.loc[(my_data['year']<=yearInputMax)&(my_data['year']>=yearInputMin)]
</code>```

more details on: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.loc.html

ContentLicense: CC BY-SA 3.0

Title: dataframe manipulation with python

Body:
so I'm really new to python and the whole dataframe manipulation topic. What I'm trying to do is to ask the user for a min year and a max year, then list the name and year for the provided range.
I have tried many different approaches, the last one I tried and though was going to be successful was this:

 yearInputMin = int(input("Please enter min year: "))
    yearInputMax = int(input("Please enter max year: "))

    for row in my_data:
        year = int(row['year'])
        if year <= yearInputMax and year >= yearInputMin:
            print
            year

I'm focusing more on the year aspect because that is the area I'm having most difficulty in.

Any ideas of how to achieve this?

Body:
dataframes are very easy to search through with ".loc", try this:

yearInputMin = int(input("Please enter min year: "))
yearInputMax = int(input("Please enter max year: "))

my_data.loc[(my_data['year']<=yearInputMax)&(my_data['year']>=yearInputMin)]

more details on: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.loc.html


问:
I don't know how to get every single result of my function list out
RStudio Version 1.0.143

Windows Ver: Windows10 Pro

I have 300+ files which has the same struction, and I want to create a loop, so it can calculate the correlation index of the required files. I can get the right files and calculate the correlation index, but I can't get them all showed as a result. I tried to save them to a vector, but it tells me "the object not found". and if it can work, I also worried about whether the content of the vector will stay if I run the function for several times. Here's the loop:

for(i in ind_larg){
+         specdata_i <- read.csv(i)
+         com_case_ind <- complete.cases(specdata_i)
+         sulfate_i <- specdata_i[,2][com_case_ind]
+         nitrate_i <- specdata_i[,3][com_case_ind]
+         ou[i] <- cor(sulfate_i, nitrate_i)
+ }

and  the result

Error: object 'ou' not found

I'm not sure if you need the rest of the code before this, so I attach them at the end here.

```<code>> setwd("C:/Users/sunxi/Coursera/specdata")
> ind <- dir(path = "C:/Users/sunxi/Coursera/specdata", pattern = ".csv") #Save the index of the files to a vector.
> specdata_ful <- lapply(ind, read.csv) #combine all the files to a data frame.
> specdat_recon_ful <- do.call(rbind, specdata_ful) #Reconstruct the data frame to put the same variable in one column.
> com_case_ful <- complete.cases(specdat_recon_ful) #Filter the complete cases.
> id_ful <- specdat_recon_ful[,4][com_case_ful] #The ID of the complete cases.
> sulfate_ful <- specdat_recon_ful[,2][com_case_ful] #The sulfate value of the complete cases.
> nitrate_ful <- specdat_recon_ful[,3][com_case_ful] #The nitrate value of the complete cases.
> id_freq_ful <- table(id_ful) #Summary the frequency in each id
> id_freq_mat_ful <- as.data.frame(id_freq_ful) #transfer the table into the data.frame.
> good <- id_freq_mat_ful[["Freq"]] > 1000 #Filter the freqency larger than threshold.
> id_good <- id_freq_mat_ful[["id_ful"]][good] #Filter the id has the frequency of complete cases larger than the threshold.
> ind_larg <- ind[id_good] #Create an index for the id has required requency.
</code>```

答:
You have to create the variable ou before you access it with ou[i]:

```<code>ou <- c()
for(i in ind_larg){
    # your loop here...
    ou[i] <- cor(sulfate_i, nitrate_i)
}
</code>```

Body:
RStudio Version 1.0.143

Windows Ver: Windows10 Pro

I have 300+ files which has the same struction, and I want to create a loop, so it can calculate the correlation index of the required files. I can get the right files and calculate the correlation index, but I can't get them all showed as a result. I tried to save them to a vector, but it tells me "the object not found". and if it can work, I also worried about whether the content of the vector will stay if I run the function for several times. Here's the loop:

for(i in ind_larg){
+         specdata_i <- read.csv(i)
+         com_case_ind <- complete.cases(specdata_i)
+         sulfate_i <- specdata_i[,2][com_case_ind]
+         nitrate_i <- specdata_i[,3][com_case_ind]
+         ou[i] <- cor(sulfate_i, nitrate_i)
+ }

and  the result

Error: object 'ou' not found

I'm not sure if you need the rest of the code before this, so I attach them at the end here.

> setwd("C:/Users/sunxi/Coursera/specdata")
> ind <- dir(path = "C:/Users/sunxi/Coursera/specdata", pattern = ".csv") #Save the index of the files to a vector.
> specdata_ful <- lapply(ind, read.csv) #combine all the files to a data frame.
> specdat_recon_ful <- do.call(rbind, specdata_ful) #Reconstruct the data frame to put the same variable in one column.
> com_case_ful <- complete.cases(specdat_recon_ful) #Filter the complete cases.
> id_ful <- specdat_recon_ful[,4][com_case_ful] #The ID of the complete cases.
> sulfate_ful <- specdat_recon_ful[,2][com_case_ful] #The sulfate value of the complete cases.
> nitrate_ful <- specdat_recon_ful[,3][com_case_ful] #The nitrate value of the complete cases.
> id_freq_ful <- table(id_ful) #Summary the frequency in each id
> id_freq_mat_ful <- as.data.frame(id_freq_ful) #transfer the table into the data.frame.
> good <- id_freq_mat_ful[["Freq"]] > 1000 #Filter the freqency larger than threshold.
> id_good <- id_freq_mat_ful[["id_ful"]][good] #Filter the id has the frequency of complete cases larger than the threshold.
> ind_larg <- ind[id_good] #Create an index for the id has required requency.

Body:
You have to create the variable ou before you access it with ou[i]:

ou <- c()
for(i in ind_larg){
    # your loop here...
    ou[i] <- cor(sulfate_i, nitrate_i)
}


问:
How to use timeit module
How do I use timeit to compare the performance of my own functions such as "insertion_sort" and "tim_sort"?

答:
The built-in timeit module works best from the IPython command line.

To time functions from within a module:

```<code>from timeit import default_timer as timer
import sys

def timefunc(func, *args, **kwargs):
    """Time a function.

    args:
        iterations=3

    Usage example:
        timeit(myfunc, 1, b=2)
    """
    try:
        iterations = kwargs.pop('iterations')
    except KeyError:
        iterations = 3
    elapsed = sys.maxsize
    for _ in range(iterations):
        start = timer()
        result = func(*args, **kwargs)
        elapsed = min(timer() - start, elapsed)
    print(('Best of {} {}(): {:.9f}'.format(iterations, func.__name__, elapsed)))
    return result
</code>```

ContentLicense: CC BY-SA 4.0

Title: How to use timeit module

Body:
The built-in timeit module works best from the IPython command line.

To time functions from within a module:

from timeit import default_timer as timer
import sys

def timefunc(func, *args, **kwargs):
    """Time a function.

    args:
        iterations=3

    Usage example:
        timeit(myfunc, 1, b=2)
    """
    try:
        iterations = kwargs.pop('iterations')
    except KeyError:
        iterations = 3
    elapsed = sys.maxsize
    for _ in range(iterations):
        start = timer()
        result = func(*args, **kwargs)
        elapsed = min(timer() - start, elapsed)
    print(('Best of {} {}(): {:.9f}'.format(iterations, func.__name__, elapsed)))
    return result


问:
Changing default encoding of Python?
I have many "can't encode" and "can't decode" problems with Python when I run my applications from the console. But in the Eclipse PyDev IDE, the default character encoding is set to UTF-8, and I'm fine.

I searched around for setting the default encoding, and people say that Python deletes the sys.setdefaultencoding function on startup, and we can not use it.

So what's the best solution for it?

答:
This is a quick hack for anyone who is (1) On a Windows platform (2) running Python 2.7 and (3) annoyed because a nice piece of software (i.e., not written by you so not immediately a candidate for encode/decode printing maneuvers) won't display the "pretty unicode characters" in the IDLE environment (Pythonwin prints unicode fine), For example, the neat First Order Logic symbols that Stephan Boyer uses in the output from his pedagogic prover at First Order Logic Prover.

I didn't like the idea of forcing a sys reload and I couldn't get the system to cooperate with setting environment variables like PYTHONIOENCODING (tried direct Windows environment variable and also dropping that in a sitecustomize.py in site-packages as a one liner ='utf-8').

So, if you are willing to hack your way to success, go to your IDLE directory, typically:
"C:\Python27\Lib\idlelib"
Locate the file IOBinding.py. Make a copy of that file and store it somewhere else so you can revert to original behavior when you choose. Open the file in the idlelib with an editor (e.g., IDLE). Go to this code area:

```<code># Encoding for file names
filesystemencoding = sys.getfilesystemencoding()

encoding = "ascii"
if sys.platform == 'win32':
    # On Windows, we could use "mbcs". However, to give the user
    # a portable encoding name, we need to find the code page
    try:
        # --> 6/5/17 hack to force IDLE to display utf-8 rather than cp1252
        # --> encoding = locale.getdefaultlocale()[1]
        encoding = 'utf-8'
        codecs.lookup(encoding)
    except LookupError:
        pass
</code>```

In other words, comment out the original code line following the 'try' that was making the encoding variable equal to locale.getdefaultlocale (because that will give you cp1252 which you don't want) and instead brute force it to 'utf-8' (by adding the line 'encoding = 'utf-8' as shown).

I believe this only affects IDLE display to stdout and not the encoding used for file names etc. (that is obtained in the filesystemencoding prior). If you have a problem with any other code you run in IDLE later, just replace the IOBinding.py file with the original unmodified file.

Title: Changing default encoding of Python?

Body:
This is a quick hack for anyone who is (1) On a Windows platform (2) running Python 2.7 and (3) annoyed because a nice piece of software (i.e., not written by you so not immediately a candidate for encode/decode printing maneuvers) won't display the "pretty unicode characters" in the IDLE environment (Pythonwin prints unicode fine), For example, the neat First Order Logic symbols that Stephan Boyer uses in the output from his pedagogic prover at First Order Logic Prover.

I didn't like the idea of forcing a sys reload and I couldn't get the system to cooperate with setting environment variables like PYTHONIOENCODING (tried direct Windows environment variable and also dropping that in a sitecustomize.py in site-packages as a one liner ='utf-8').

So, if you are willing to hack your way to success, go to your IDLE directory, typically:
"C:\Python27\Lib\idlelib"
Locate the file IOBinding.py. Make a copy of that file and store it somewhere else so you can revert to original behavior when you choose. Open the file in the idlelib with an editor (e.g., IDLE). Go to this code area:

# Encoding for file names
filesystemencoding = sys.getfilesystemencoding()

encoding = "ascii"
if sys.platform == 'win32':
    # On Windows, we could use "mbcs". However, to give the user
    # a portable encoding name, we need to find the code page
    try:
        # --> 6/5/17 hack to force IDLE to display utf-8 rather than cp1252
        # --> encoding = locale.getdefaultlocale()[1]
        encoding = 'utf-8'
        codecs.lookup(encoding)
    except LookupError:
        pass

In other words, comment out the original code line following the 'try' that was making the encoding variable equal to locale.getdefaultlocale (because that will give you cp1252 which you don't want) and instead brute force it to 'utf-8' (by adding the line 'encoding = 'utf-8' as shown).

I believe this only affects IDLE display to stdout and not the encoding used for file names etc. (that is obtained in the filesystemencoding prior). If you have a problem with any other code you run in IDLE later, just replace the IOBinding.py file with the original unmodified file.


问:
How would I check how busy the HDD is with PHP?
I've noticed that some cloud hosting solutions have really poor Disk IO. This causes a few problems that could be solved by having the script wait until the disk was less busy.

With PHP is it possible to monitor the busy (or not so busy) state of the filesystem without making things worse?

答:
If this is a Linux system, you can calculate disk usage yourself - the language you choose to implement it in will use the same concepts.

Your kernel is most likely using sysfs which makes a lot of information about your system available at /sys; we can grab information about our desired disks at a regular interval, and calculate usage based on the differences between them.

On my system I will be looking at the disk, sda, yours may differ.

```<code>$ cat /sys/class/block/sda/stat
   42632       25  2045318   247192  6956543  7362278 123236256 23878974        0  3703033 24119492
</code>```

Now if we look at the Kernel documentation for /sys/class/block/<dev>/stat we can see the following descriptions for each column of the output.

```<code>Name            units         description
----            -----         -----------
read I/Os       requests      number of read I/Os processed
read merges     requests      number of read I/Os merged with in-queue I/O
read sectors    sectors       number of sectors read
read ticks      milliseconds  total wait time for read requests
write I/Os      requests      number of write I/Os processed
write merges    requests      number of write I/Os merged with in-queue I/O
write sectors   sectors       number of sectors written
write ticks     milliseconds  total wait time for write requests
in_flight       requests      number of I/Os currently in flight
io_ticks        milliseconds  total time this block device has been active
time_in_queue   milliseconds  total wait time for all requests
</code>```

If we run this on a cron schedule, and diff some of the wait times, we can see just how long we are waiting on each operation. You will also have other stats about total IOPS, and RW bandwidth. The documentation goes more in depth on each field.

Whatever language is chosen, the file descriptor to open to get information about the disk will be

```<code>/sys/class/block/<dev>/stat
</code>```

If we do this on a schedule, we can draw fancy graphs ;)

Body:
If this is a Linux system, you can calculate disk usage yourself - the language you choose to implement it in will use the same concepts.

Your kernel is most likely using sysfs which makes a lot of information about your system available at /sys; we can grab information about our desired disks at a regular interval, and calculate usage based on the differences between them.

On my system I will be looking at the disk, sda, yours may differ.

$ cat /sys/class/block/sda/stat
   42632       25  2045318   247192  6956543  7362278 123236256 23878974        0  3703033 24119492

Now if we look at the Kernel documentation for /sys/class/block/<dev>/stat we can see the following descriptions for each column of the output.

Name            units         description
----            -----         -----------
read I/Os       requests      number of read I/Os processed
read merges     requests      number of read I/Os merged with in-queue I/O
read sectors    sectors       number of sectors read
read ticks      milliseconds  total wait time for read requests
write I/Os      requests      number of write I/Os processed
write merges    requests      number of write I/Os merged with in-queue I/O
write sectors   sectors       number of sectors written
write ticks     milliseconds  total wait time for write requests
in_flight       requests      number of I/Os currently in flight
io_ticks        milliseconds  total time this block device has been active
time_in_queue   milliseconds  total wait time for all requests

If we run this on a cron schedule, and diff some of the wait times, we can see just how long we are waiting on each operation. You will also have other stats about total IOPS, and RW bandwidth. The documentation goes more in depth on each field.

Whatever language is chosen, the file descriptor to open to get information about the disk will be

/sys/class/block/<dev>/stat

If we do this on a schedule, we can draw fancy graphs ;)


问:
finding change between records in MySQL
I have a table where I am storing the stored number of barrels inside of many tanks.  I am storing values here every night at midnight, and at the beggining and end of any operator initiated transfer.

What I want to return is the number of barrels difference since the previous event record for that specific tank.  I have the correct ID for the self join to get the previous record number, however the barrels is incorrect.

Here is what I currently have.

```<code>SELECT
    inventory.id,
    MAX(inventory2.id) AS id2,
    inventory.tankname,
    inventory.barrels,
    inventory.eventstamp,
    inventory2.barrels
FROM
    inventory
LEFT JOIN
    inventory inventory2 ON inventory2.tankname = inventory.tankname AND inventory2.eventstamp < inventory.eventstamp
GROUP BY
    inventory.id,
    inventory.tankname,
    inventory.barrels,
    inventory.eventstamp
ORDER BY
    inventory.tankname,
    inventory.eventstamp
</code>```

That returns the following

答:
Just use correlated subqueries:

```<code>SELECT i.*,
       (SELECT i2.id
        FROM inventory i2
        WHERE i2.tankname = i.tankname AND
              i2.eventstamp < i.eventstamp
        ORDER BY i2.eventstamp DESC
        LIMIT 1
       ) as prev_id,
       (SELECT i2.barrels
        FROM inventory i2
        WHERE i2.tankname = i.tankname AND
              i2.eventstamp < i.eventstamp
        ORDER BY i2.eventstamp DESC
        LIMIT 1
       ) as prev_barrels
FROM inventory i
ORDER BY i.tankname, i.eventstamp;
</code>```

Your query doesn't work because you have columns in the SELECT that are not in the GROUP BY and are not aggregated.  That shouldn't be allowed in any database; it is unfortunate that MySQL does allow it.

Title: finding change between records in MySQL

Body:
I have a table where I am storing the stored number of barrels inside of many tanks.  I am storing values here every night at midnight, and at the beggining and end of any operator initiated transfer.

What I want to return is the number of barrels difference since the previous event record for that specific tank.  I have the correct ID for the self join to get the previous record number, however the barrels is incorrect.

Here is what I currently have.

SELECT
    inventory.id,
    MAX(inventory2.id) AS id2,
    inventory.tankname,
    inventory.barrels,
    inventory.eventstamp,
    inventory2.barrels
FROM
    inventory
LEFT JOIN
    inventory inventory2 ON inventory2.tankname = inventory.tankname AND inventory2.eventstamp < inventory.eventstamp
GROUP BY
    inventory.id,
    inventory.tankname,
    inventory.barrels,
    inventory.eventstamp
ORDER BY
    inventory.tankname,
    inventory.eventstamp

That returns the following

Body:
Just use correlated subqueries:

SELECT i.*,
       (SELECT i2.id
        FROM inventory i2
        WHERE i2.tankname = i.tankname AND
              i2.eventstamp < i.eventstamp
        ORDER BY i2.eventstamp DESC
        LIMIT 1
       ) as prev_id,
       (SELECT i2.barrels
        FROM inventory i2
        WHERE i2.tankname = i.tankname AND
              i2.eventstamp < i.eventstamp
        ORDER BY i2.eventstamp DESC
        LIMIT 1
       ) as prev_barrels
FROM inventory i
ORDER BY i.tankname, i.eventstamp;

Your query doesn't work because you have columns in the SELECT that are not in the GROUP BY and are not aggregated.  That shouldn't be allowed in any database; it is unfortunate that MySQL does allow it.


问:
SQL update column twice
I have a string column (Col1) in table (Tab1). I want to write a script that removes '%' or '&' characters from the string values.

The select for this is the following:

```<code>SELECT REPLACE( Tab1.Col1, '&', ''),
       REPLACE(Tab1.Col1, '%', '')
FROM Table1 Tab1
WHERE Tab1.Col1 like '%[&]%'
      OR Tab1.Col1 like '%[%]%'
</code>```

Does this seem correct? If it is how would I convert this to any update statement? I tried the following:

```<code>UPDATE Tab1
SET Tab1.Col1 =  REPLACE( Tab1.Col1, '&', ''),
    Tab1.Col1 = REPLACE (Tab1.Col1, '%', '')
FROM Table1 Tab1
WHERE Tab1.Col1 like '%[&]%'
      OR Tab1.Col1 like '%[%]%'
</code>```

This doesn't work as you cant update column twice in SET.
Is there any other way I can do this? I know I'm probably doing something silly here so apologies for my ignorance.

Thanks in advance.

答:
```<code> UPDATE Tab1
 SET Tab1.Col1 =  REPLACE(REPLACE( Tab1.Col1, '&', ''), '%', '')
 FROM Table1 Tab1
 WHERE Tab1.Col1 like '%[&]%'
  OR Tab1.Col1 like '%[%]%'
</code>```

Title: SQL update column twice

Body:
I have a string column (Col1) in table (Tab1). I want to write a script that removes '%' or '&' characters from the string values.

The select for this is the following:

SELECT REPLACE( Tab1.Col1, '&', ''),
       REPLACE(Tab1.Col1, '%', '')
FROM Table1 Tab1
WHERE Tab1.Col1 like '%[&]%'
      OR Tab1.Col1 like '%[%]%'

Does this seem correct? If it is how would I convert this to any update statement? I tried the following:

UPDATE Tab1
SET Tab1.Col1 =  REPLACE( Tab1.Col1, '&', ''),
    Tab1.Col1 = REPLACE (Tab1.Col1, '%', '')
FROM Table1 Tab1
WHERE Tab1.Col1 like '%[&]%'
      OR Tab1.Col1 like '%[%]%'

This doesn't work as you cant update column twice in SET.
Is there any other way I can do this? I know I'm probably doing something silly here so apologies for my ignorance.

Thanks in advance.

ContentLicense: CC BY-SA 2.5


问:
Can't Detect Object with a Specific Color (RGB/HSV/HSL)
My aim is to detect the lanes on GTA Vice City.

When I analyse this picture on paint, the lines are like [120,100,45] in RGB color space. When I apply this with cv2.inRange, I can't get result strangely. I do not know what to do and do not know why it didn't show me any signs of this yellow (actually it looks like yellow) lanes.

edit1:

I find the values to obtain only these lines, they are:
lower: 0,110,0
upper: 160,195,80

And here's the photo of it,

However, when I tried to obtain this line on live playing, with ImageGrab module (after canny and gaussblur), I get:

My aim is to draw lane with HoughProbabilistic, however, I can't see continuous lines, even there is no line every time on live playing. I'm confused, here's the codes:

```<code>   def process_img(image):
    lower_yellow = np.array([0, 110, 0])
    upper_yellow = np.array([160, 195, 80])
    # yellow color mask
    processimagehsl = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    yellow_mask = cv2.inRange(processimagehsl, lower_yellow, upper_yellow)  # and we are masking it
    masked = cv2.bitwise_and(image, image, mask=yellow_mask)  # and then we combine it with original image
    # turned into gray
    processimagecanny = cv2.Canny(masked, threshold1=150,
                                  threshold2=300)  # with canny edge detection method, we detect edges
    # of only our yellow lines' edges. We used masking
    # at the beginning of the code because of this.
    processimagegauss = cv2.GaussianBlur(processimagecanny, (5, 5), 0)  # This'Ll fix some in order to avoid noises
    processedimage = regionofinterest(processimagegauss)  # Let's get back to our predetermined region
    lines = cv2.HoughLinesP(processedimage, 1, np.pi / 180, 180, 0, 0)

    return processedimage
</code>```

Solved

You can pick the range with this application on OpenCv, here's the link: http://answers.opencv.org/question/134248/how-to-define-the-lower-and-upper-range-of-a-color/

And here's what it looks like with explanation:

答:
You are missing the fifth argument lines in cv2.HoughLinesP(). The positional arguments expect the order:

```<code>cv2.HoughLinesP(image, rho, theta, threshold[, lines[, minLineLength[, maxLineGap]]])
</code>```

You can solve this one of two ways; either use None where the argument is expected for lines:

```<code>lines = cv2.HoughLinesP(image, rho, theta, threshold, None, minLineLength, maxLineGap)
</code>```

or call all the optional arguments you want to use with their key:

```<code>lines = cv2.HoughLinesP(image, rho, theta, threshold=..., minLineLength=..., maxLineGap=...)
</code>```

Body:
My aim is to detect the lanes on GTA Vice City.

When I analyse this picture on paint, the lines are like [120,100,45] in RGB color space. When I apply this with cv2.inRange, I can't get result strangely. I do not know what to do and do not know why it didn't show me any signs of this yellow (actually it looks like yellow) lanes.

edit1:

I find the values to obtain only these lines, they are:
lower: 0,110,0
upper: 160,195,80

And here's the photo of it,

However, when I tried to obtain this line on live playing, with ImageGrab module (after canny and gaussblur), I get:

My aim is to draw lane with HoughProbabilistic, however, I can't see continuous lines, even there is no line every time on live playing. I'm confused, here's the codes:

   def process_img(image):
    lower_yellow = np.array([0, 110, 0])
    upper_yellow = np.array([160, 195, 80])
    # yellow color mask
    processimagehsl = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    yellow_mask = cv2.inRange(processimagehsl, lower_yellow, upper_yellow)  # and we are masking it
    masked = cv2.bitwise_and(image, image, mask=yellow_mask)  # and then we combine it with original image
    # turned into gray
    processimagecanny = cv2.Canny(masked, threshold1=150,
                                  threshold2=300)  # with canny edge detection method, we detect edges
    # of only our yellow lines' edges. We used masking
    # at the beginning of the code because of this.
    processimagegauss = cv2.GaussianBlur(processimagecanny, (5, 5), 0)  # This'Ll fix some in order to avoid noises
    processedimage = regionofinterest(processimagegauss)  # Let's get back to our predetermined region
    lines = cv2.HoughLinesP(processedimage, 1, np.pi / 180, 180, 0, 0)

    return processedimage

Solved

You can pick the range with this application on OpenCv, here's the link: http://answers.opencv.org/question/134248/how-to-define-the-lower-and-upper-range-of-a-color/

And here's what it looks like with explanation:

Body:
You are missing the fifth argument lines in cv2.HoughLinesP(). The positional arguments expect the order:

cv2.HoughLinesP(image, rho, theta, threshold[, lines[, minLineLength[, maxLineGap]]])

You can solve this one of two ways; either use None where the argument is expected for lines:

lines = cv2.HoughLinesP(image, rho, theta, threshold, None, minLineLength, maxLineGap)

or call all the optional arguments you want to use with their key:

lines = cv2.HoughLinesP(image, rho, theta, threshold=..., minLineLength=..., maxLineGap=...)


问:
Enable TLS 1.2 on Mac OSX
I am pulling my hair out trying to get a Django application working with Stripe on my local machine with OSX 10.12.3. The error I get when trying to run a test payment is:

PermissionError at /register Request req_ADIwntj3xGaqUF: Stripe no
  longer supports API requests made with TLS 1.0. Please initiate HTTPS
  connections with TLS 1.2 or later. You can learn more about this at
  https://stripe.com/blog/upgrading-tls.

I successfully upgraded openssl using brew. When I run openssl version the output is:

```<code>OpenSSL 1.0.2k  26 Jan 2017
</code>```

When I run which openssl the output is:

```<code>/usr/local/opt/openssl/bin/openssl
</code>```

I found instructions on stack overflow that said to run brew link --force openssl. When I do that the error is:

Warning: Refusing to link: openssl Linking keg-only openssl means you
  may end up linking against the insecure, deprecated system OpenSSL
  while using the headers from Homebrew's openssl. Instead, pass the
  full include/library paths to your compiler e.g.:

  -I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib

Can anybody help me get this working?

答:
For anyone using brew + pyenv + pyenv-virtualenv this is what fixed my issue with missing TLS 1.2 on my Mac:

```<code># Update XCode
# Restart machine
Type:
$ brew update
$ brew upgrade pyenv
$ pyenv install 3.4.6 # the latest version of Python 3.4
$ pyenv global 3.4.6  # make default python
$ pyenv virtualenv 3.4.6 myenv
$ pyenv activate myenv
$ pip install -r requirements.txt
</code>```

Hope that's useful to someone someday.

Title: Enable TLS 1.2 on Mac OSX

Body:
I am pulling my hair out trying to get a Django application working with Stripe on my local machine with OSX 10.12.3. The error I get when trying to run a test payment is:

PermissionError at /register Request req_ADIwntj3xGaqUF: Stripe no
  longer supports API requests made with TLS 1.0. Please initiate HTTPS
  connections with TLS 1.2 or later. You can learn more about this at
  https://stripe.com/blog/upgrading-tls.

I successfully upgraded openssl using brew. When I run openssl version the output is:

OpenSSL 1.0.2k  26 Jan 2017

When I run which openssl the output is:

/usr/local/opt/openssl/bin/openssl

I found instructions on stack overflow that said to run brew link --force openssl. When I do that the error is:

Warning: Refusing to link: openssl Linking keg-only openssl means you
  may end up linking against the insecure, deprecated system OpenSSL
  while using the headers from Homebrew's openssl. Instead, pass the
  full include/library paths to your compiler e.g.:

  -I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib

Can anybody help me get this working?

Body:
For anyone using brew + pyenv + pyenv-virtualenv this is what fixed my issue with missing TLS 1.2 on my Mac:

# Update XCode
# Restart machine
Type:
$ brew update
$ brew upgrade pyenv
$ pyenv install 3.4.6 # the latest version of Python 3.4
$ pyenv global 3.4.6  # make default python
$ pyenv virtualenv 3.4.6 myenv
$ pyenv activate myenv
$ pip install -r requirements.txt

Hope that's useful to someone someday.


问:
IllegalMutationException from Beam PTransform
This is the Apache Beam PTransform that I wrote:

```<code>public class NormalizeTransform
  extends PTransform<PCollection<String>, PCollection<SimpleTable>> {

@Override
public PCollection<SimpleTable> expand(PCollection<String> lines) {
  ExtractFields extract_i = new ExtractFields();
  PCollection<SimpleTable> table = lines
    .apply("Extracting data model fields from lines",
           ParDo.of(extract_i));
}

public class ExtractFields extends DoFn<String, SimpleTable> {

@ProcessElement
public void processElement(ProcessContext c){
  try {
    String line = c.element();
    // fill table
    for (Table_Struct st: this.struct){
      String o = line.substring(st.pos_1, st.pos_2));
      this.table.getClass().getField(st.Field_Name).set(
        this.table, o);
    }
    c.output(this.table);
  }
}
</code>```

and sporadically I will get the following error IllegalMutationException, which means I repeated the running of the code, sometimes it works, sometimes it doesn't.

```<code>org.apache.beam.sdk.util.IllegalMutationException: PTransform Transform/Extracting data model fields from lines/ParMultiDo(ExtractFields) mutated value  after it was output (new value was ). Values must not be mutated in any way after being output.

at org.apache.beam.runners.direct.ImmutabilityCheckingBundleFactory$ImmutabilityEnforcingBundle.commit(ImmutabilityCheckingBundleFactory.java:135)
at org.apache.beam.runners.direct.EvaluationContext.commitBundles(EvaluationContext.java:214)
at org.apache.beam.runners.direct.EvaluationContext.handleResult(EvaluationContext.java:163)
at org.apache.beam.runners.direct.ExecutorServiceParallelExecutor$TimerIterableCompletionCallback.handleResult(ExecutorServiceParallelExecutor.java:268)
at org.apache.beam.runners.direct.TransformExecutor.finishBundle(TransformExecutor.java:168)
at org.apache.beam.runners.direct.TransformExecutor.run(TransformExecutor.java:109)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
</code>```

I don't think I specifically changed any output of the value anywhere in my codes.  The MutationDetectors will compare two values: previousValue and newValue. In my case, the previousValue is usually one input value and the newValue is another input value. How come the Transform will try using one input value to modify another input value?

答:
I'm not sure where this.table is coming from.

But to help you understand the error message, keep in mind that processElement may be called on multiple inputs. The first call will output this.table. The next call will mutate this.table before outputting it.

If this mutation happens after the first call has output this.table and before downstream code has had the chance to read this.table, you will have incorrect results. So, this error is indicating that you have mutated the content of this.table after the reference was output -- something you should not do.

Consider instead either (1) outputting a copy of this.table or (2) creating the table as a local field. For example:

```<code>@ProcessElement
public void processElement(ProcessContext c){
  try {
    String line = c.element();
    Table table = /* create the table */;
    // fill table
    for (Table_Struct st: this.struct){
      String o = line.substring(st.pos_1, st.pos_2));
      this.table.getClass().getField(st.Field_Name)
        .set(table, o);
    }
    c.output(table);
  }
}
</code>```

Also note that performing reflection within each processElement may be slower than desired. If you can modify the fields directly, that will likely be better.

Body:
This is the Apache Beam PTransform that I wrote:

public class NormalizeTransform
  extends PTransform<PCollection<String>, PCollection<SimpleTable>> {

@Override
public PCollection<SimpleTable> expand(PCollection<String> lines) {
  ExtractFields extract_i = new ExtractFields();
  PCollection<SimpleTable> table = lines
    .apply("Extracting data model fields from lines",
           ParDo.of(extract_i));
}

public class ExtractFields extends DoFn<String, SimpleTable> {

@ProcessElement
public void processElement(ProcessContext c){
  try {
    String line = c.element();
    // fill table
    for (Table_Struct st: this.struct){
      String o = line.substring(st.pos_1, st.pos_2));
      this.table.getClass().getField(st.Field_Name).set(
        this.table, o);
    }
    c.output(this.table);
  }
}

and sporadically I will get the following error IllegalMutationException, which means I repeated the running of the code, sometimes it works, sometimes it doesn't.

org.apache.beam.sdk.util.IllegalMutationException: PTransform Transform/Extracting data model fields from lines/ParMultiDo(ExtractFields) mutated value  after it was output (new value was ). Values must not be mutated in any way after being output.

at org.apache.beam.runners.direct.ImmutabilityCheckingBundleFactory$ImmutabilityEnforcingBundle.commit(ImmutabilityCheckingBundleFactory.java:135)
at org.apache.beam.runners.direct.EvaluationContext.commitBundles(EvaluationContext.java:214)
at org.apache.beam.runners.direct.EvaluationContext.handleResult(EvaluationContext.java:163)
at org.apache.beam.runners.direct.ExecutorServiceParallelExecutor$TimerIterableCompletionCallback.handleResult(ExecutorServiceParallelExecutor.java:268)
at org.apache.beam.runners.direct.TransformExecutor.finishBundle(TransformExecutor.java:168)
at org.apache.beam.runners.direct.TransformExecutor.run(TransformExecutor.java:109)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

I don't think I specifically changed any output of the value anywhere in my codes.  The MutationDetectors will compare two values: previousValue and newValue. In my case, the previousValue is usually one input value and the newValue is another input value. How come the Transform will try using one input value to modify another input value?

Body:
I'm not sure where this.table is coming from.

But to help you understand the error message, keep in mind that processElement may be called on multiple inputs. The first call will output this.table. The next call will mutate this.table before outputting it.

If this mutation happens after the first call has output this.table and before downstream code has had the chance to read this.table, you will have incorrect results. So, this error is indicating that you have mutated the content of this.table after the reference was output -- something you should not do.

Consider instead either (1) outputting a copy of this.table or (2) creating the table as a local field. For example:

@ProcessElement
public void processElement(ProcessContext c){
  try {
    String line = c.element();
    Table table = /* create the table */;
    // fill table
    for (Table_Struct st: this.struct){
      String o = line.substring(st.pos_1, st.pos_2));
      this.table.getClass().getField(st.Field_Name)
        .set(table, o);
    }
    c.output(table);
  }
}

Also note that performing reflection within each processElement may be slower than desired. If you can modify the fields directly, that will likely be better.


问:
Js implement something like protocol delegate in swift
Is there something like the protocol delegate in js??  something like:</p>

```<code>class a{
    somefunction();//<<== Is called from here
}

class b{
    class_A = new a();

    somefunction(){//Code is implemented here
        //Do something
    }
}
</code>```

答:
You mean like this

class a {

  constructor() {
    this.b = 100000;

    this.a = {
      *[Symbol.iterator]() {
        let pre = 0,
          cur = 1
        for (;;) {
          [pre, cur] = [cur, pre + cur]
          yield cur
        }
      }
    }

  }

  get more() {
    var items = new b(this.a, this.b);
    return items.some;
  }

};

class b {

  constructor(a, b) {
    this.a = a;
    this.b = b;
    let k = 1;
  }

  get some() {
    for (let n of this.a) {
      if (n > this.b)
        break
      console.log(n)

    }
  }
}

var other = new a();
console.log(other.more);```

Body:
Is there something like the protocol delegate in js??  something like:

class a{
    somefunction();//<<== Is called from here
}

class b{
    class_A = new a();

    somefunction(){//Code is implemented here
        //Do something
    }
}


问:
HttpListener.Start() AccessDenied error on Vista
Running this code as a regular user throws HttpListenerException (access denied).  Snippet runs ok as an administator

```<code>class Program
{
    static void Main(string[] args)
    {
        HttpListener listener = new HttpListener();
        listener.Prefixes.Add("http://myip:8080/app/");
        listener.Start();
        //.... and so on
     }
}
</code>```

i went ahead and added the uri using netsh (netsh http show lists the uri)

```<code>netsh http add urlacl url=http://+:8080/app user=domain\user
</code>```

still getting the same error.  Adding ACLs did work for other projects (they didn't use HttpListener though).  I tried multiple port/application name combinations, nothing works.

Any ideas what might be the cause?

Running .Net 3.5 SP1 on Vista

答:
Is that URI already registered on the system?

http://msdn.microsoft.com/en-us/library/system.net.httplistenerexception.aspx says that would be one cause.

Body:
Running this code as a regular user throws HttpListenerException (access denied).  Snippet runs ok as an administator

class Program
{
    static void Main(string[] args)
    {
        HttpListener listener = new HttpListener();
        listener.Prefixes.Add("http://myip:8080/app/");
        listener.Start();
        //.... and so on
     }
}

i went ahead and added the uri using netsh (netsh http show lists the uri)

netsh http add urlacl url=http://+:8080/app user=domain\user

still getting the same error.  Adding ACLs did work for other projects (they didn't use HttpListener though).  I tried multiple port/application name combinations, nothing works.

Any ideas what might be the cause?

Running .Net 3.5 SP1 on Vista

OwnerDisplayName: BQ


问:
Will Android receivers unregister automatically on application exit?
When my Android application needs to register a receiver, I've been registering in Activity.onCreate() and unregistering in Activity.onDestroy() (I think I'm supposed to be using onPause() and onResume() instead, but that's not really the point):

```<code>public class Foo extends Activity {
    private Receiver bar = null;

    @Override public void onCreate(Bundle bundle) {
        this.registerReceiver(this.bar = new Receiver(), new IntentFilter());
    }

    @Override public void onDestroy(Bundle bundle) {
        this.unregisterReceiver(this.bar);
    }
}
</code>```

Is unregistering the receiver actually necessary, or will the framework handle it when my application exits?

答: If registering a receiver in your Activity.onResume() implementation, you should unregister it in Activity.onPause(). (You won't receive intents when paused, and this will cut down on unnecessary system overhead)</p>

Body:
When my Android application needs to register a receiver, I've been registering in Activity.onCreate() and unregistering in Activity.onDestroy() (I think I'm supposed to be using onPause() and onResume() instead, but that's not really the point):

public class Foo extends Activity {
    private Receiver bar = null;

    @Override public void onCreate(Bundle bundle) {
        this.registerReceiver(this.bar = new Receiver(), new IntentFilter());
    }

    @Override public void onDestroy(Bundle bundle) {
        this.unregisterReceiver(this.bar);
    }
}

Is unregistering the receiver actually necessary, or will the framework handle it when my application exits?


问:
How to manage configuration for Webpack/Electron app?
I am using Webpack 2 and Electron to build nodejs application on Mac.

In my project in the root I have directory 'data' where I store configuration in a json like data/configurations/files.json (in practices there are different files with dynamic names)

After webpackaing though when I call: fs.readdirSync(remote.app.getAppPath()); to get files in the root I get only these packed: [ "default_app.js", "icon.png", "index.html", "main.js", "package.json", "renderer.js" ]

path.join(remote.app.getAppPath(), 'data/tests/groups.json'); called with FS ReadSync leads to an issue Error: ENOENT, data/tests/groups.json not found in /Users/myuser/myproject/node_modules/electron/dist/Electron.app/Contents/Resources/default_app.asar. So it seems that the whole data folder is not picked up by webpacker.

Webpack config is using json-loader and I did not find any documentation mentioning anything special about including specific files or jsons. Or do I have to reference json files in my code differently as they might be packed under main.js.

What is the best practice for Electron/Webpack for managing JSON config files? Am I doing something wrong when webpacking the project?

My project is based of https://github.com/SimulatedGREG/electron-vue using webpack/electron/vue

答:
The Webpack Misconception

One thing to understand upfront is that webpack does not bundle files required through fs or other modules that ask for a path to a file. These type of assets are commonly labeled as Static Assets, as they are not bundled in any way. webpack will only bundle files that are required or imported (ES6). Furthermore, depending on your webpack configuration, your project root may not always match what is output within your production builds.

Based on the electron-vue documentation's Project Structure/File Tree, you will find that only webpack bundles and the static/ directory are made available in production builds. electron-vue also has a handy __static global variable that can provide a path to that static/ folder within both development and production. You can use this variable similar to how one would with __dirname and path.join to access your JSON files, or really any files.

A Solution to Static Assets

It seems the current version of the electron-vue boilerplate already has this solved for you, but I'm going to describe how this is setup with webpack as it can apply to not only JSON files and how it can also apply for any webpack + electron setup. The following solution assumes your webpack build outputs to a separate folder, which we'll use dist/ in this case, assumes your webpack configuration is located in your project's root directory, and assumes process.env.NODE_ENV is set to development during development.

The static/ directory

During development we need a place to store our static assets, so let's place them in a directory called static/. Here we can put files, such as JSONs, that we know we will need to read with fs or some other module that requires a full path to the file.

Now we need to make that static/ assets directory available in production builds.

But webpack isn't handling this folder at all, what can we do?

Let's use the simple copy-webpack-plugin. Within our webpack configuration file we can add this plugin when building for production and configure it to copy the static/ folder into our dist/ folder.

```<code>new CopyWebpackPlugin([
    {
      from: path.join(__dirname, '/static'),
      to: path.join(__dirname, '/dist/static'),
      ignore: ['.*']
    }
])
</code>```

Okay so the assets are in production, but how do I get a path to this folder in both development and production?

Creating a global __static variable

What's the point of making this __static variable?

Using __dirname is not reliable in webpack + electron setups. During development __dirname could be in reference to a directory that exists in your src/ files. In production, since webpack bundles our src/ files into one script, that path you formed to get to static/ doesn't exist anymore. Furthermore, those files you put inside src/ that were not required or imported never make it to your production build.

When handling the project structure differences from development and production, trying to get a path to static/ will be highly annoying during development having to always check your process.env.NODE_ENV.

So let's simplify this by creating one source of truth.

Using the webpack.DefinePlugin we can set our __static variable only in development to yield a path that points to <projectRoot>/static/. Depending if you have multiple webpack configurations, you can apply this for both a main and renderer process configuration.

```<code>new webpack.DefinePlugin({
    '__static': `"${path.join(__dirname, '/static').replace(/\\/g, '\\\\')}"`
})
</code>```

In production, we need to set the __static variable manually in our code. Here's what we can do...

index.html (renderer process)

```<code><!-- Set `__static` path to static files in production -->
<script>
    if (process.env.NODE_ENV !== 'development') window.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
</script>
<!-- import webpack bundle -->
</code>```

main.js (main process)

```<code>// Set `__static` path to static files in production
if (process.env.NODE_ENV !== 'development') {
    global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
}

// rest of application code below
</code>```

Now start using your __static variable

Let's say we have a simple JSON file we need to read with fs, here's what we can accomplish now...

static/someFile.json

```<code>{"foo":"bar"}
</code>```

someScript.js (renderer or main process)

```<code>import fs from 'fs'
import path from 'path'

const someFile = fs.readFileSync(path.join(__static, '/someFile.json'), 'utf8')

console.log(JSON.parse(someFile))
// => { foo: bar }
</code>```

Conclusion

webpack was made to bundle assets together that are required or imported into one nice bundle. Assets referenced with fs or other modules that need a file path are considered Static Assets, and webpack does not directly handle these. Using copy-webpack-plugin and webpack.DefinePlugin we can setup a reliable __static variable that yields a path to our static/ assets directory in both development and production.

To end, I personally haven't seen any other webpack + electron boilerplates handle this situation as it isn't a very common situation, but I think we can all agree that having one source of truth to a static assets directory is a wonderful approach to alleviate developer fatigue.

Body:
The Webpack Misconception

One thing to understand upfront is that webpack does not bundle files required through fs or other modules that ask for a path to a file. These type of assets are commonly labeled as Static Assets, as they are not bundled in any way. webpack will only bundle files that are required or imported (ES6). Furthermore, depending on your webpack configuration, your project root may not always match what is output within your production builds.

Based on the electron-vue documentation's Project Structure/File Tree, you will find that only webpack bundles and the static/ directory are made available in production builds. electron-vue also has a handy __static global variable that can provide a path to that static/ folder within both development and production. You can use this variable similar to how one would with __dirname and path.join to access your JSON files, or really any files.

A Solution to Static Assets

It seems the current version of the electron-vue boilerplate already has this solved for you, but I'm going to describe how this is setup with webpack as it can apply to not only JSON files and how it can also apply for any webpack + electron setup. The following solution assumes your webpack build outputs to a separate folder, which we'll use dist/ in this case, assumes your webpack configuration is located in your project's root directory, and assumes process.env.NODE_ENV is set to development during development.

The static/ directory

During development we need a place to store our static assets, so let's place them in a directory called static/. Here we can put files, such as JSONs, that we know we will need to read with fs or some other module that requires a full path to the file.

Now we need to make that static/ assets directory available in production builds.

But webpack isn't handling this folder at all, what can we do?

Let's use the simple copy-webpack-plugin. Within our webpack configuration file we can add this plugin when building for production and configure it to copy the static/ folder into our dist/ folder.

new CopyWebpackPlugin([
    {
      from: path.join(__dirname, '/static'),
      to: path.join(__dirname, '/dist/static'),
      ignore: ['.*']
    }
])

Okay so the assets are in production, but how do I get a path to this folder in both development and production?

Creating a global __static variable

What's the point of making this __static variable?

Using __dirname is not reliable in webpack + electron setups. During development __dirname could be in reference to a directory that exists in your src/ files. In production, since webpack bundles our src/ files into one script, that path you formed to get to static/ doesn't exist anymore. Furthermore, those files you put inside src/ that were not required or imported never make it to your production build.

When handling the project structure differences from development and production, trying to get a path to static/ will be highly annoying during development having to always check your process.env.NODE_ENV.

So let's simplify this by creating one source of truth.

Using the webpack.DefinePlugin we can set our __static variable only in development to yield a path that points to <projectRoot>/static/. Depending if you have multiple webpack configurations, you can apply this for both a main and renderer process configuration.

new webpack.DefinePlugin({
    '__static': `"${path.join(__dirname, '/static').replace(/\\/g, '\\\\')}"`
})

In production, we need to set the __static variable manually in our code. Here's what we can do...

index.html (renderer process)

<!-- Set `__static` path to static files in production -->
<script>
    if (process.env.NODE_ENV !== 'development') window.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
</script>
<!-- import webpack bundle -->

main.js (main process)

// Set `__static` path to static files in production
if (process.env.NODE_ENV !== 'development') {
    global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
}

// rest of application code below

Now start using your __static variable

Let's say we have a simple JSON file we need to read with fs, here's what we can accomplish now...

static/someFile.json

{"foo":"bar"}

someScript.js (renderer or main process)

import fs from 'fs'
import path from 'path'

const someFile = fs.readFileSync(path.join(__static, '/someFile.json'), 'utf8')

console.log(JSON.parse(someFile))
// => { foo: bar }

Conclusion

webpack was made to bundle assets together that are required or imported into one nice bundle. Assets referenced with fs or other modules that need a file path are considered Static Assets, and webpack does not directly handle these. Using copy-webpack-plugin and webpack.DefinePlugin we can setup a reliable __static variable that yields a path to our static/ assets directory in both development and production.

To end, I personally haven't seen any other webpack + electron boilerplates handle this situation as it isn't a very common situation, but I think we can all agree that having one source of truth to a static assets directory is a wonderful approach to alleviate developer fatigue.


问:
Adding css/js files to the home site in the admin's panel django
I am trying to add a simple script to my admin's panel in django. I am following this example from the doc, which works when I do this:

```<code>    class ArticleAdmin(admin.ModelAdmin):
        class Media:
            js = ("my_code.js",)

admin.site.register(Article, ArticleAdmin)
</code>```

my_code.js (which lives in STATICFILES_DIRS) gets executed when I open the "Article" link in the admin's panel (so this would be the example url: localhost:8000/admin/news/article/)

However, I want it to be executed in the home site, as in right after I login to the admin's panel (this would be the url:localhost:8000/admin/) .  Where do I have to insert the class Media then?

答:
Are you trying to make your custom javascript appear on all admin pages? Then the best thing to do is to override the default admin templatte.

Look in your django installation and find django/contrib/admin/templates/admin/base.html make a copy of it in templates/admin in your project folder. Then simply add the following to the html.

```<code><script scr="my_code.js" type="text/javascript"></script>
</code>```

Then the js will be available on all admin pages.

Body:
I am trying to add a simple script to my admin's panel in django. I am following this example from the doc, which works when I do this:

    class ArticleAdmin(admin.ModelAdmin):
        class Media:
            js = ("my_code.js",)

admin.site.register(Article, ArticleAdmin)

my_code.js (which lives in STATICFILES_DIRS) gets executed when I open the "Article" link in the admin's panel (so this would be the example url: localhost:8000/admin/news/article/)

However, I want it to be executed in the home site, as in right after I login to the admin's panel (this would be the url:localhost:8000/admin/) .  Where do I have to insert the class Media then?

Body:
Are you trying to make your custom javascript appear on all admin pages? Then the best thing to do is to override the default admin templatte.

Look in your django installation and find django/contrib/admin/templates/admin/base.html make a copy of it in templates/admin in your project folder. Then simply add the following to the html.

<script scr="my_code.js" type="text/javascript"></script>

Then the js will be available on all admin pages.


问:
Why should(n't) I use S3 instead Lambda for static content
Let's imagine situation like that:

We have node.js app, which is rendering view on server-side and sends html to browser. In generated html we have few static assets (like images, stylesheets etc.).

Why should I (or not) choose S3 over Lambda to serve this content?

Here are pros & cons which I see:

Performance

I was quite sure that providing content from S3 is much more faster then from Lambda (there is no script which need to be executed)...

...Until I performed some tests (file size ~44kB) average of 10 requests:

API GW + S3: 285ms

API GW + Lambda: 290ms

S3: 135ms

As you can see there is no difference between providing content from Lambda via API GW then from S3. The only significant difference is between direct link to s3 and two previous tests.

Lambda 1 : S3 1

Cost

And here Lambda wins definetely.

First of all we have free triage of 1 000 000 requests,
Second here pricing comes:

S3: $0.004 per 10,000 requests

Lambda: around 0,002000624 per 10,000 requests:

($0.20 per 1 million requests + $0.000000208$ per every 100ms)

So in pricing Lambda wins.

Summarizing

My observations shows that Lambda is better way to serve even static content (speed is similar to S3, and pricing is twice cheaper).

Is there anything what I am missing?

答:
I believe you've made a couple of errors.

S3 request pricing is $0.004 per 10,000 requests, which is $0.40 per million.  That's correct.

Lambda is $0.20 per million invocations, plus CPU time.  Agreed.

But I believe you've overlooked the fact that you can't invoke Lambda functions from the Internet without API Gateway, which is an additional $3.50 per million requests.

Net cost for serving static content from Lambda is $3.70 per million requests, plus CPU time.¹

This makes S3 substantially less expensive.

Then, consider bandwidth costs: CloudFront, when coupled with S3, is faster than S3 alone, has a higher per-request cost, but is also slightly less expensive for bandwidth.  If you constrain your CloudFront distribution to Price Class 100 then you will actually pay less under some circumstances than just using S3 alone.

S3 download bandwidth in the least expensive regions is $0.09/GB.

CloudFront download bandwidth in the least expensive class is $0.085/GB.

Bandwidth from S3 to CloudFront is free (e.g. for cache misses).

The cost per GB downloaded is $0.005 less when using CloudFront with S3 than when using S3 alone.  CloudFront charges $0.0075 per 10,000 requests, or $0.0035 more than S3... but, if we assume a 50% cache hit rate, the numbers look like this:

Per 10,000 objects $0.0075 [CF] + ($0.004 [S3] * 0.5 [hit rate]) = $0.0095... for simplicity, let's just round that up to $0.01.

Now, we can see that the request cost for 10K objects is exactly offset by the savings on 2GB of download, so if your objects are larger than 2G/10K = 2M/10 = 200KB/each then using CloudFront with S3 is actually slightly cheaper than using S3 alone.  If not, the cost is still too close to be significant and, as mentioned, the download turnaround time is much shorter.

Additionally, CloudFront supports HTTP/2.

¹ This assumes API Gateway + Lambda.  Since this answer was written, there are now two more ways to allow a Lambda function to return static (or dynamic) content: CloudFront's Lambda@Edge feature supports generating HTTP responses from a Lambda function, but the function runs in a special, lightweight "edge" container that only supports Node.js.  However, minimum runtime here is 50ms rather than the standard 100ms.  Application Load Balancers also support using a Lambda function as a target, and these are standard Lambda invocations in standard containers, so all runtimes are supported.  Both of these can be more cost-effective than API Gateway, although the baseline cost of the ALB itself also has to be considered unless you already have an ALB.   Both are also limited to a 1MB response body (on Lambda@Edge, this requires an "origin request" trigger), which is a smaller limit than API Gateway.


问:
F# - splitting list into tuple of odd-even lists (by element, not position)
Example: split [1;3;2;4;7;9];;

Output: ([1;3;7;9], [2;4])

I'm new to F# and I can't figure it out.

Can't use the partition built in function.

This is what I have so far:

```<code>let rec split xs =
    match xs with
    | [] -> [], []
    | xs -> xs, []
    | xh::xt -> let odds, evens = split xt
                if (xh % 2) = 0 then xh::odds, xh::evens
                else xh::odds, evens
</code>```

Fixed code:

```<code>let rec split xs =
    match xs with
    | [] -> [], []
    | xh::xt -> let odds, evens = split xt
                if (xh % 2) = 0 then odds, xh::evens
                else xh::odds, evens
</code>```

*Thanks to @TheInnerLight for pointing out my errors: unreachable case and unnecessarily modifying odds

答:
You can use the built-in List.partition function

```<code>let splitOddEven xs =
    xs |> List.partition (fun x -> x % 2 <> 0)
</code>```

```<code>splitOddEven [1;3;2;4;7;9];;
val it : int list * int list = ([1; 3; 7; 9], [2; 4])
</code>```

If you want a recursive implementation, I'd probably go for a tail recursive implementation like this:

```<code>let splitOddEven xs =
    let rec splitOddEvenRec oddAcc evenAcc xs =
        match xs with
        | [] -> oddAcc, evenAcc
        | xh::xt ->
            if (xh % 2) = 0 then splitOddEvenRec oddAcc (xh :: evenAcc) xt
            else splitOddEvenRec (xh :: oddAcc) evenAcc xt
    splitOddEvenRec [] [] xs

splitOddEven  [1;3;2;4;7;9]
</code>```

Note that this will give you the two resulting lists in reverse order so you might wish to reverse them yourself.

Body:
Example: split [1;3;2;4;7;9];;

Output: ([1;3;7;9], [2;4])

I'm new to F# and I can't figure it out.

Can't use the partition built in function.

This is what I have so far:

let rec split xs =
    match xs with
    | [] -> [], []
    | xs -> xs, []
    | xh::xt -> let odds, evens = split xt
                if (xh % 2) = 0 then xh::odds, xh::evens
                else xh::odds, evens

Fixed code:

let rec split xs =
    match xs with
    | [] -> [], []
    | xh::xt -> let odds, evens = split xt
                if (xh % 2) = 0 then odds, xh::evens
                else xh::odds, evens

*Thanks to @TheInnerLight for pointing out my errors: unreachable case and unnecessarily modifying odds

Body:
You can use the built-in List.partition function

let splitOddEven xs =
    xs |> List.partition (fun x -> x % 2 <> 0)

splitOddEven [1;3;2;4;7;9];;
val it : int list * int list = ([1; 3; 7; 9], [2; 4])

If you want a recursive implementation, I'd probably go for a tail recursive implementation like this:

let splitOddEven xs =
    let rec splitOddEvenRec oddAcc evenAcc xs =
        match xs with
        | [] -> oddAcc, evenAcc
        | xh::xt ->
            if (xh % 2) = 0 then splitOddEvenRec oddAcc (xh :: evenAcc) xt
            else splitOddEvenRec (xh :: oddAcc) evenAcc xt
    splitOddEvenRec [] [] xs

splitOddEven  [1;3;2;4;7;9]

Note that this will give you the two resulting lists in reverse order so you might wish to reverse them yourself.


问:
Searching for numbers/product codes in Solr
I have a text-field to which i'm feeding normal text that contains product codes (e.g. FI302010, RR220011, etc).
I would like to be able to search for these product numbers.
I have tried it out in the ANALYSIS-tool - And here it looks like it should be possible. A standard text-field does some word delimiting that splits the product codes into "FI" and "302010" - but as long as the same happens in both query and index it shouldn't be a problem.
When i look at the schema browser i cant find the product codes, but i can find something that looks like a stemmed version of the code (i.e. 30201 - the last 0 is missing).

I have tried using different text-types (text_ws, textTight and text) - but none will allow me to do the search.

How should I enable text-search in text?

Thanks

答:
I found the solution here: http://search-with-fast-and-solr.blogspot.com/2010/12/maxfieldlength-in-solr.html

It turned out that solr was truncating the document at 10000 words - So I had to increase the maxFieldLength setting in solrconfig.xml

Thanks for your input!


问:
removing .php but now the folder structure doesn't work and trailing slash
I was to remove .php extension by using

```<code>RewriteEngine On
RewriteRule ^([^\.]+)$ $1.php [NC,L]
</code>```

but this introduced a problem which is i can't access to folder directories and their internal files and server is not even accepting trailing slash at the end.

so lets i visit this page

```<code>www.example.com/family
</code>```

url will load fine

if i visit this page with / at the end

```<code>www.example.com/family/
</code>```

note i have trailing slash / at the end, it won't work

now if i have a directory or folder name family

```<code>www.example/family
</code>```

which is directory instead of my page then page will not work nor the directory or folder will be accessible.

答:
Add a Rewrite Rule to Redirect to a URL with a slash:

```<code>RewriteRule (.*)$ /$1 [L,R=301]
</code>```

Input URL: http://www.example.com/family

Output URL: http://www.example.com/family/

Body:
I was to remove .php extension by using

RewriteEngine On
RewriteRule ^([^\.]+)$ $1.php [NC,L]

but this introduced a problem which is i can't access to folder directories and their internal files and server is not even accepting trailing slash at the end.

so lets i visit this page

www.example.com/family

url will load fine

if i visit this page with / at the end

www.example.com/family/

note i have trailing slash / at the end, it won't work

now if i have a directory or folder name family

www.example/family

which is directory instead of my page then page will not work nor the directory or folder will be accessible.

Body:
Add a Rewrite Rule to Redirect to a URL with a slash:

RewriteRule (.*)$ /$1 [L,R=301]

Input URL: http://www.example.com/family

Output URL: http://www.example.com/family/


问:
Compare name variable against a string value in Drupal 8 twig template
I'm working with a taxonomy term template page in Drupal 8 and I need to be able to show specific content based solely on the taxonomy term that is populating the content of the page. I'm using the taxonomy-term.twig.html template as a starting point, but I'm not having any luck comparing a string value against the {{ name }} variable. Here's the code I have so far:

```<code><section{{ attributes.addClass(classes)}}>
    {% if 'general' in name %}
       <img class="promo-image" src="http://placehold.it/150x150" alt="promo">
       <h3 class="promo-title">Promo title text</h3>
       <p class="promo-copy">lorem ipsum dolor set amet</p>
       <div class="links">
          <a href="/resources" class="btn outline-black-transparent">Learn more</a>
       </div>
    {% endif %}
</section>
</code>```

If I output the name variable like normal {{ name }} it prints the tag name onto the page, but I can't figure out how to compare the value against anything. I've tried a straight if equals route as well, but it seems like the name variable is an array.

答:
Since the name variable is an array, and I only needed the first value I added:

```<code>{%
set tag = name['#items'].0.value
%}
</code>```

Then changed the if statement to:

```<code>{% if tag == 'text' %}
<section{{ attributes.addClass(classes)}}>
   <img class="promo-image" src="http://placehold.it/150x150" alt="promo">
   <h3 class="promo-title">Promo title text</h3>
   <p class="promo-copy">lorem ipsum dolor set amet</p>
   <div class="links">
      <a href="/resources" class="btn outline-black-transparent">Learn more</a>
   </div>
 </section>
{% endif %}
</code>```

Now the template is working the way I intended.

Body:
I'm working with a taxonomy term template page in Drupal 8 and I need to be able to show specific content based solely on the taxonomy term that is populating the content of the page. I'm using the taxonomy-term.twig.html template as a starting point, but I'm not having any luck comparing a string value against the {{ name }} variable. Here's the code I have so far:

    {% if 'general' in name %}

Promo title text

lorem ipsum dolor set amet

          Learn more

    {% endif %}

If I output the name variable like normal {{ name }} it prints the tag name onto the page, but I can't figure out how to compare the value against anything. I've tried a straight if equals route as well, but it seems like the name variable is an array.

Body:
Since the name variable is an array, and I only needed the first value I added:

{%
set tag = name['#items'].0.value
%}

Then changed the if statement to:

{% if tag == 'text' %}
<section{{ attributes.addClass(classes)}}>
   <img class="promo-image" src="http://placehold.it/150x150" alt="promo">
   <h3 class="promo-title">Promo title text</h3>
   <p class="promo-copy">lorem ipsum dolor set amet</p>
   <div class="links">
      <a href="/resources" class="btn outline-black-transparent">Learn more</a>
   </div>
 </section>
{% endif %}

Now the template is working the way I intended.


问:
Tensorflow saver.restore() not restoring network
I am completely lost on the the tensorflow saver method.

I'm trying to follow the basic tensorflow deep neural network model tutorial. I want to figure out how to train the network for a few iterations, then load the model in another session.

```<code>with tf.Session() as sess:
    graph = tf.Graph()
    x = tf.placeholder(tf.float32,shape=[None,784])
    y_ = tf.placeholder(tf.float32, shape=[None,10])

    sess.run(global_variables_initializer())

    #Define the Network
    #(This part is all copied from the tutorial - not copied for brevity)
    #See here: https://www.tensorflow.org/versions/r0.12/tutorials/mnist/pros/
</code>```

Skipping ahead to training.

```<code>    #Train the Network
    train_step = tf.train.AdamOptimizer(1e-4).minimize(
                     cross_entropy,global_step=global_step)
    correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

    saver = tf.train.Saver()

    for i in range(101):
        batch = mnist.train.next_batch(50)
        if i%100 == 0:
        train_accuracy = accuracy.eval(feed_dict=
                           {x:batch[0],y_:batch[1]})
        print 'Step %d, training accuracy %g'%(i,train_accuracy)
            train_step.run(feed_dict={x:batch[0], y_: batch[1]})
        if i%100 == 0:
            print 'Test accuracy %g'%accuracy.eval(feed_dict={x:
                       mnist.test.images, y_: mnist.test.labels})

        saver.save(sess,'./mnist_model')
</code>```

The console prints out:

Step 0, training accuracy 0.16

Test accuracy 0.0719

Step 100, training accuracy 0.88

Test accuracy 0.8734

Next I want to load the model

```<code>with tf.Session() as sess:
    saver = tf.train.import_meta_graph('mnist_model.meta')
    saver.restore(sess,tf.train.latest_checkpoint('./'))
    sess.run(tf.global_variables_initializer())
</code>```

Now I want to re-test to see if the model loaded

```<code>print 'Test accuracy %g'%accuracy.eval(feed_dict={x:
                       mnist.test.images, y_: mnist.test.labels})
</code>```

The console prints out:

Test accuracy 0.1151

It doesn't appear that the model is saving any of the data? What am I doing wrong?

答:
When you save your models, typically all global variables are saved in external files whereas local variables are not. You can have a look at this answer to understand the difference.

The error in your restoration code is calling tf.global_variable_initializer() after saver.restore(). The saver.restore docs mention,

The variables to restore do not have to have been initialized, as restoring is itself a way to initialize variables.

Hence, try removing the line,

```<code>sess.run(tf.global_variables_initializer())
</code>```

You should ideally replace it with,

```<code>sess.run(tf.local_variables_initializer())
</code>```

Body:
I am completely lost on the the tensorflow saver method.

I'm trying to follow the basic tensorflow deep neural network model tutorial. I want to figure out how to train the network for a few iterations, then load the model in another session.

with tf.Session() as sess:
    graph = tf.Graph()
    x = tf.placeholder(tf.float32,shape=[None,784])
    y_ = tf.placeholder(tf.float32, shape=[None,10])

    sess.run(global_variables_initializer())

    #Define the Network
    #(This part is all copied from the tutorial - not copied for brevity)
    #See here: https://www.tensorflow.org/versions/r0.12/tutorials/mnist/pros/

Skipping ahead to training.

    #Train the Network
    train_step = tf.train.AdamOptimizer(1e-4).minimize(
                     cross_entropy,global_step=global_step)
    correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

    saver = tf.train.Saver()

    for i in range(101):
        batch = mnist.train.next_batch(50)
        if i%100 == 0:
        train_accuracy = accuracy.eval(feed_dict=
                           {x:batch[0],y_:batch[1]})
        print 'Step %d, training accuracy %g'%(i,train_accuracy)
            train_step.run(feed_dict={x:batch[0], y_: batch[1]})
        if i%100 == 0:
            print 'Test accuracy %g'%accuracy.eval(feed_dict={x:
                       mnist.test.images, y_: mnist.test.labels})

        saver.save(sess,'./mnist_model')

The console prints out:

Step 0, training accuracy 0.16

Test accuracy 0.0719

Step 100, training accuracy 0.88

Test accuracy 0.8734

Next I want to load the model

with tf.Session() as sess:
    saver = tf.train.import_meta_graph('mnist_model.meta')
    saver.restore(sess,tf.train.latest_checkpoint('./'))
    sess.run(tf.global_variables_initializer())

Now I want to re-test to see if the model loaded

print 'Test accuracy %g'%accuracy.eval(feed_dict={x:
                       mnist.test.images, y_: mnist.test.labels})

The console prints out:

Test accuracy 0.1151

It doesn't appear that the model is saving any of the data? What am I doing wrong?

Body:
When you save your models, typically all global variables are saved in external files whereas local variables are not. You can have a look at this answer to understand the difference.

The error in your restoration code is calling tf.global_variable_initializer() after saver.restore(). The saver.restore docs mention,

The variables to restore do not have to have been initialized, as restoring is itself a way to initialize variables.

Hence, try removing the line,

sess.run(tf.global_variables_initializer())

You should ideally replace it with,

sess.run(tf.local_variables_initializer())


问:
How to correctly get the weights using spark for synthetic dataset?
I'm doing LogisticRegressionWithSGD on spark for synthetic dataset. I've calculated the error on matlab using vanilla gradient descent and on R which is ~5%. I got similar weight that was used in the model that I used to generate y. The dataset was generated using this example.

While I am able to get very close error rate at the end with different stepsize tuning, the weights for individual feature isn't the same. In fact, it varies a lot. I tried LBFGS for spark and it's able to predict both error and weight correctly in few iterations. My problem is with logistic regression with SGD on spark.

The weight I'm getting:

```<code>[0.466521045342,0.699614292387,0.932673108363,0.464446310304,0.231458578991,0.464372487994,0.700369689073,0.928407671516,0.467131704168,0.231629845549,0.46465456877,0.700207596219,0.935570594833,0.465697758292,0.230127949916]
</code>```

The weight I want:

```<code>[2,3,4,2,1,2,3,4,2,1,2,3,4,2,1]
</code>```

Intercept I'm getting: 0.2638102010832128
Intercept I want: 1

Q.1. Is it the problem with synthetic dataset. I have tried tuning with minBatchFraction, stepSize, iteration and intercept. I couldn't get it right.

Q.2. Why is spark giving me this weird weights? Would it be wrong to expect similar weights from Spark's model?

Please let me know if extra details is needed to answer my question.

答: It  actually did converge, your weights are normalized between 0 and 1, while expected max value is for, multiply everything you got from SGD with 4, you can see the correlation even for intercept value.</p>

Body:
I'm doing LogisticRegressionWithSGD on spark for synthetic dataset. I've calculated the error on matlab using vanilla gradient descent and on R which is ~5%. I got similar weight that was used in the model that I used to generate y. The dataset was generated using this example.

While I am able to get very close error rate at the end with different stepsize tuning, the weights for individual feature isn't the same. In fact, it varies a lot. I tried LBFGS for spark and it's able to predict both error and weight correctly in few iterations. My problem is with logistic regression with SGD on spark.

The weight I'm getting:

[0.466521045342,0.699614292387,0.932673108363,0.464446310304,0.231458578991,0.464372487994,0.700369689073,0.928407671516,0.467131704168,0.231629845549,0.46465456877,0.700207596219,0.935570594833,0.465697758292,0.230127949916]

The weight I want:

[2,3,4,2,1,2,3,4,2,1,2,3,4,2,1]

Intercept I'm getting: 0.2638102010832128
Intercept I want: 1

Q.1. Is it the problem with synthetic dataset. I have tried tuning with minBatchFraction, stepSize, iteration and intercept. I couldn't get it right.

Q.2. Why is spark giving me this weird weights? Would it be wrong to expect similar weights from Spark's model?

Please let me know if extra details is needed to answer my question.


问:
Magento - Fetch current domain and not base domain
We are running one Magento install over multiple domains and are using a script to fetch information from the database to help users filter the products.

In our coding we have the $_SERVER function calling the store which seems to be store 1 (which is on domain 1), I was wondering what I would need to change to fetch the information from store 2 (which is on domain 2).

答:
First of all, $_SERVER is not a function, but a predefined variable (associative array). Once u access the site from domain 2, $_SERVER['SERVER_NAME'] will give u the right thing. It all depends on which domain you access the server from. I currently use this for a reseller website i have that has a lot of parked domains.

Hope that helps.

Cheers!


问:
How XPath axes preceding:: and following:: work?
I have always believed that the preceding axis contains all nodes that are before the context node in document order, excluding any ancestors of the context node, and also excluding attribute nodes and namespace nodes.

Recently I came across two published books on XML. On chapters about XPath and preceding and following axes, those authors didn't mention the nodes that I believed should be excluded from the definition of those two axes. They simply defined them as ALL nodes before and after contexts nodes, respectively.

I get confused, naturally. I don't (dare) normally doubt published books, especially things that I found on two separate books.

I undrrstand XML is an evolving technology. Has there been a change in the official or recommended definition that I missed?

Thanks for any help clarifying it.

答:
This isn't a matter of what you believe, it's a matter of what the specs say. The latest (3.1) version of XPath defines these axes at

https://www.w3.org/TR/xpath-31/#axes

as follows:

the following axis contains all nodes that are descendants of the root
  of the tree in which the context node is found, are not descendants of
  the context node, and occur after the context node in document order

the preceding axis contains all nodes that are descendants of the root
  of the tree in which the context node is found, are not ancestors of
  the context node, and occur before the context node in document order

The definition has not changed over time.

Sadly, it's not unusual for books to simplify. On the other hand, you may have missed something in the books you were reading, you haven't cited them specifically, so I can't tell.


问:
Lua - associate function with object's parameter
I'm trying to do an easy exercise for WoW's client.

I have the next object:

```<code>local name = detailsFrame:CreateFontString("$name", "ARTWORK", "Fancy24Font")
name.SetFontObjects = MySetFontObjects;
</code>```

Where the function MySetFontObjects is the next one:

```<code>function MySetFontObjects (self, ...) {
    self.fontObjects = { ... };
    --more stuff
}
</code>```

If I try to call that function it returns attempt to call a method 'MySetFontObjects' (a nil value)

I'm calling it like this:

```<code>name:SetFontObjects(Font1, Font2, Font3);
</code>```

What am I doing wrong? I just don't see it.

答:
Are you doing something like this?

```<code>local name = {}
name.SetFontObjects = MySetFontObjects

function MySetFontObjects (self, ...)
    self.fontObjects = { ... }
    --more stuff
end

name:SetFontObjects("foo", "bar", "baz")
</code>```

This produces the "attempt to call method 'SetFontObjects' (a nil value)" error that you seem to be reporting.

This happens because when you set name.SetFontObjects = MySetFontObjects, the MySetFontObjects function is not yet defined, so MySetFontObjects defaults to a global variable with a value of nil. To fix this, just move the function definition above that statement:

```<code>function MySetFontObjects (self, ...)
    self.fontObjects = { ... }
    --more stuff
end

local name = {}
name.SetFontObjects = MySetFontObjects

name:SetFontObjects("foo", "bar", "baz")
</code>```

Does that work for you?

Body:
I'm trying to do an easy exercise for WoW's client.

I have the next object:

local name = detailsFrame:CreateFontString("$name", "ARTWORK", "Fancy24Font")
name.SetFontObjects = MySetFontObjects;

Where the function MySetFontObjects is the next one:

function MySetFontObjects (self, ...) {
    self.fontObjects = { ... };
    --more stuff
}

If I try to call that function it returns attempt to call a method 'MySetFontObjects' (a nil value)

I'm calling it like this:

name:SetFontObjects(Font1, Font2, Font3);

What am I doing wrong? I just don't see it.

Body:
Are you doing something like this?

local name = {}
name.SetFontObjects = MySetFontObjects

function MySetFontObjects (self, ...)
    self.fontObjects = { ... }
    --more stuff
end

name:SetFontObjects("foo", "bar", "baz")

This produces the "attempt to call method 'SetFontObjects' (a nil value)" error that you seem to be reporting.

This happens because when you set name.SetFontObjects = MySetFontObjects, the MySetFontObjects function is not yet defined, so MySetFontObjects defaults to a global variable with a value of nil. To fix this, just move the function definition above that statement:

function MySetFontObjects (self, ...)
    self.fontObjects = { ... }
    --more stuff
end

local name = {}
name.SetFontObjects = MySetFontObjects

name:SetFontObjects("foo", "bar", "baz")

Does that work for you?


问:
How do I prompt for an MFA key to generate and use credentials for AWS CLI access?
I have several Bash scripts that invoke AWS CLI commands for which permissions have changed to require MFA, and I want to be able to prompt for a code generated by my MFA device in these scripts so that they can run with the necessary authentication.

But there seems to be no simple built in way to do this. The only documentation I can find involves a complicated process of using aws sts get-session-token and then saving each value in a configuration, which it is then unclear how to use.

To be clear what I'd like is that when I run one of my scripts that that contains AWS CLI commands that require MFA, I'm simply prompted for the code, so that providing it allows the AWS CLI operations to complete. Something like:

```<code>#!/usr/bin/env bash

# (1) prompt for generated MFA code
# ???

# (2) use entered code to generate necessary credentials
aws sts get-session-token ... --token-code $ENTERED_VALUE

# (3) perform my AWS CLI commands requiring MFA
# ....
</code>```

It's not clear to me how to prompt for this when needed (which is probably down to not being proficient with bash) or how to use the output of get-session-token once I have it.

Is there a way to do what I'm looking for?

I've tried to trigger a prompt by specifying a --profile with a mfa_serial entry; but that doesn't work either.

答:
For bash you could read in the value, then set those values from the sts output

```<code>echo "Type the mfa code that you want to use (4 digits), followed by [ENTER]:"

read ENTERED_VALUE

aws sts get-session-token ... --token-code $ENTERED_VALUE
</code>```

then you'll have to parse the output of the sts call which has the access key, secret and session token.

```<code>{
  Credentials: {
    AccessKeyId: "ASIAJPC6D7SKHGHY47IA",
    Expiration: 2016-06-05 22:12:07 +0000 UTC,
    SecretAccessKey: "qID1YUDHaMPet5xw/vpw1Wk8SKPilFihdiMSdSIj",
    SessionToken: "FQoDYXdzEB4aDLwmzouEQ3eckfqJxyLOARbBGasdCaAXkZ7ABOcOCNx2/7sS8N7A6Dpcax/t2G8KNTcUkRLdxI0gTvPoKQeZrH8wUrL4UxFFP6kCWEasdVIBAoUfuhdeUa1a7H216Mrfbbv3rMGsVKUoJT2Ar3r0pYgsYxizOWzH5VaA4rmd5gaQvfSFmasdots3WYrZZRjN5nofXJBOdcRd6J94k8m5hY6ClfGzUJEqKcMZTrYkCyUu3xza2S73CuykGM2sePVNH9mGZCWpTBcjO8MrawXjXj19UHvdJ6dzdl1FRuKdKKeS18kF"
  }
}
</code>```

then set them

```<code>aws configure set aws_access_key_id default_access_key --profile NAME_PROFILE
aws configure set aws_secret_access_key default_secret_key --profile NAME_PROFILE
aws configure set default.region us-west-2 --profile

aws some_commmand --profile NAME_PROFILE
</code>```

http://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_08_02.html

AWS STS API Reference
http://docs.aws.amazon.com/STS/latest/APIReference/API_GetSessionToken.html

AWS CLI STS Command
http://docs.aws.amazon.com/cli/latest/reference/sts/get-session-token.html

I wrote something very similar to what you are trying to in Go, here but this is for the sts assumerole not get-session-token.

Body:
I have several Bash scripts that invoke AWS CLI commands for which permissions have changed to require MFA, and I want to be able to prompt for a code generated by my MFA device in these scripts so that they can run with the necessary authentication.

But there seems to be no simple built in way to do this. The only documentation I can find involves a complicated process of using aws sts get-session-token and then saving each value in a configuration, which it is then unclear how to use.

To be clear what I'd like is that when I run one of my scripts that that contains AWS CLI commands that require MFA, I'm simply prompted for the code, so that providing it allows the AWS CLI operations to complete. Something like:

#!/usr/bin/env bash

# (1) prompt for generated MFA code
# ???

# (2) use entered code to generate necessary credentials
aws sts get-session-token ... --token-code $ENTERED_VALUE

# (3) perform my AWS CLI commands requiring MFA
# ....

It's not clear to me how to prompt for this when needed (which is probably down to not being proficient with bash) or how to use the output of get-session-token once I have it.

Is there a way to do what I'm looking for?

I've tried to trigger a prompt by specifying a --profile with a mfa_serial entry; but that doesn't work either.

Body:
For bash you could read in the value, then set those values from the sts output

echo "Type the mfa code that you want to use (4 digits), followed by [ENTER]:"

read ENTERED_VALUE

aws sts get-session-token ... --token-code $ENTERED_VALUE

then you'll have to parse the output of the sts call which has the access key, secret and session token.

{
  Credentials: {
    AccessKeyId: "ASIAJPC6D7SKHGHY47IA",
    Expiration: 2016-06-05 22:12:07 +0000 UTC,
    SecretAccessKey: "qID1YUDHaMPet5xw/vpw1Wk8SKPilFihdiMSdSIj",
    SessionToken: "FQoDYXdzEB4aDLwmzouEQ3eckfqJxyLOARbBGasdCaAXkZ7ABOcOCNx2/7sS8N7A6Dpcax/t2G8KNTcUkRLdxI0gTvPoKQeZrH8wUrL4UxFFP6kCWEasdVIBAoUfuhdeUa1a7H216Mrfbbv3rMGsVKUoJT2Ar3r0pYgsYxizOWzH5VaA4rmd5gaQvfSFmasdots3WYrZZRjN5nofXJBOdcRd6J94k8m5hY6ClfGzUJEqKcMZTrYkCyUu3xza2S73CuykGM2sePVNH9mGZCWpTBcjO8MrawXjXj19UHvdJ6dzdl1FRuKdKKeS18kF"
  }
}

then set them

aws configure set aws_access_key_id default_access_key --profile NAME_PROFILE
aws configure set aws_secret_access_key default_secret_key --profile NAME_PROFILE
aws configure set default.region us-west-2 --profile

aws some_commmand --profile NAME_PROFILE

http://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_08_02.html

AWS STS API Reference
http://docs.aws.amazon.com/STS/latest/APIReference/API_GetSessionToken.html

AWS CLI STS Command
http://docs.aws.amazon.com/cli/latest/reference/sts/get-session-token.html

I wrote something very similar to what you are trying to in Go, here but this is for the sts assumerole not get-session-token.


问:
How to ssr to replace all string in kdb?
table dd

```<code>dd:([]a:("account=abcde;cash=123";"account=abc;cash=345");1 2)
</code>```

result:

```<code>"account=abcde;cash=123"    1
"account=abc;cash=345"      2
</code>```

I want to replace everything in account:
so the result should be

```<code>"replace;cash=123"  1
"replace;cash=345"  2
</code>```

I tried this

```<code>update ssr[;"account=* ;";""] each a from dd
</code>```

but it didn't work.

答:
can't use wild card * with ssr information on http://code.kx.com/q/cookbook/regex/#regex-in-q
for integrating with regex libraries.

alternative solution to ssr:

```<code>update a:{v:(" *";";")0:x;y,/:v 0}[a;"replacestring;"] from dd where a like "account=\*"
</code>```

Body:
table dd

dd:([]a:("account=abcde;cash=123";"account=abc;cash=345");1 2)

result:

"account=abcde;cash=123"    1
"account=abc;cash=345"      2

I want to replace everything in account:
so the result should be

"replace;cash=123"  1
"replace;cash=345"  2

I tried this

update ssr[;"account=* ;";""] each a from dd

but it didn't work.

Body:
can't use wild card * with ssr information on http://code.kx.com/q/cookbook/regex/#regex-in-q
for integrating with regex libraries.

alternative solution to ssr:

update a:{v:(" *";";")0:x;y,/:v 0}[a;"replacestring;"] from dd where a like "account=\*"


问:
I cannot figure out why my variable is out of range
I am getting an error in my python code. This is the code

```<code>binary=[]
bits=int(input("How many bits do you want to have?   >>"))
numOfCombos=2**bits
lenOfPart=numOfCombos/2
x=0
y=0
z=0
while x!=numOfCombos:
    binary.insert(x,[])
    x+=1
x=0
while x!=lenOfPart:
    binary[x].insert(0,"0")
    x+=1
while x!=lenOfPart*2:
    binary[x].insert(0,"1")
    x+=1
x=0
p=0
while x!=bits:
    lenOfPart=lenOfPart/2
    while z!=len(binary)-1:
        while p!=lenOfPart:
            binary[y].insert(len(binary[y]),"0")
            y+=1
            p+=1
        p=0
        while p!=lenOfPart:
            binary[y].insert(len(binary[y]),"1")
            y+=1
            p+=1
        p=0
        z+=1
    y=0
    x+=1

print(numOfCombos)
print(len(binary))
print(binary)
</code>```

I am getting an error that reads

```<code>How many bits do you want to have?   >>3
Traceback (most recent call last):
  File "C:/Users/#####/AppData/Local/Programs/Python/Python36-32/binarygen.py", line 24, in <module>
    binary[y].insert(len(binary[y]),"0")
IndexError: list index out of range
</code>```

I do not understand why my variables are not working. I have tested all the things I can think of. If you have any advice, please respond as soon as possible. If you think that the entire program is wrong, please insert a working version into your answer. The purpose of the program is to generate all the possible binary combinations for a given number of bits. Thank you for your time and help.

答:
```<code>from itertools import product

def generate_combinations(n):
    return [''.join(p) for p in product('10', repeat=n)]

if __name__ == '__main__':
    bits = int(input('How many bits do you want to have? '))
    print(generate_combinations(bits))
</code>```

<hr>

```<code>>>> generate_combinations(1)
['1', '0']
>>> generate_combinations(2)
['11', '10', '01', '00']
>>> generate_combinations(3)
['111', '110', '101', '100', '011', '010', '001', '000']
</code>```

Body:
I am getting an error in my python code. This is the code

binary=[]
bits=int(input("How many bits do you want to have?   >>"))
numOfCombos=2**bits
lenOfPart=numOfCombos/2
x=0
y=0
z=0
while x!=numOfCombos:
    binary.insert(x,[])
    x+=1
x=0
while x!=lenOfPart:
    binary[x].insert(0,"0")
    x+=1
while x!=lenOfPart*2:
    binary[x].insert(0,"1")
    x+=1
x=0
p=0
while x!=bits:
    lenOfPart=lenOfPart/2
    while z!=len(binary)-1:
        while p!=lenOfPart:
            binary[y].insert(len(binary[y]),"0")
            y+=1
            p+=1
        p=0
        while p!=lenOfPart:
            binary[y].insert(len(binary[y]),"1")
            y+=1
            p+=1
        p=0
        z+=1
    y=0
    x+=1

print(numOfCombos)
print(len(binary))
print(binary)

I am getting an error that reads

How many bits do you want to have?   >>3
Traceback (most recent call last):
  File "C:/Users/#####/AppData/Local/Programs/Python/Python36-32/binarygen.py", line 24, in <module>
    binary[y].insert(len(binary[y]),"0")
IndexError: list index out of range

I do not understand why my variables are not working. I have tested all the things I can think of. If you have any advice, please respond as soon as possible. If you think that the entire program is wrong, please insert a working version into your answer. The purpose of the program is to generate all the possible binary combinations for a given number of bits. Thank you for your time and help.

Body:
from itertools import product

def generate_combinations(n):
    return [''.join(p) for p in product('10', repeat=n)]

if __name__ == '__main__':
    bits = int(input('How many bits do you want to have? '))
    print(generate_combinations(bits))

>>> generate_combinations(1)
['1', '0']
>>> generate_combinations(2)
['11', '10', '01', '00']
>>> generate_combinations(3)
['111', '110', '101', '100', '011', '010', '001', '000']