问:
AttributeError: 'tuple' object has no attribute 'decode' on Heroku logs
When running heroku logs --tail I get this error.

I'm implementing RedisToGo on my heroku app and I noticed that when I added "REDISTOGO_URL" to my code, it's returning this error. I don`t really know why when I add this it breaks my whole code. I was running it without the "REDISTOGO_URL" but realized I needed it on my app in order to work Thanks in advance.

This is my code:

```<code>redis_url = ('REDISTOGO_URL','redis://localhost:6379')
redis = redis.from_url(redis_url)

app.config["SESSION_FILE_DIR"] = mkdtemp()
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "redis"
app.config["SESSION_REDIS"] = redis

sess = Session()
sess.init_app(app)
</code>```

And this is the whole error code:

```<code>2020-06-02T21:29:55.662911+00:00 app[web.1]: connection_pool = ConnectionPool.from_url(url, db=db, **kwargs)
2020-06-02T21:29:55.662912+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/redis/connection.py", line 995, in from_url
2020-06-02T21:29:55.662912+00:00 app[web.1]: url = urlparse(url)
2020-06-02T21:29:55.662912+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/urllib/parse.py", line 372, in urlparse
2020-06-02T21:29:55.662913+00:00 app[web.1]: url, scheme, _coerce_result = _coerce_args(url, scheme)
2020-06-02T21:29:55.662913+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/urllib/parse.py", line 124, in _coerce_args
2020-06-02T21:29:55.662913+00:00 app[web.1]: return _decode_args(args) + (_encode_result,)
2020-06-02T21:29:55.662914+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/urllib/parse.py", line 108, in _decode_args
2020-06-02T21:29:55.662914+00:00 app[web.1]: return tuple(x.decode(encoding, errors) if x else '' for x in args)
2020-06-02T21:29:55.662914+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/urllib/parse.py", line 108, in <genexpr>
2020-06-02T21:29:55.662915+00:00 app[web.1]: return tuple(x.decode(encoding, errors) if x else '' for x in args)
2020-06-02T21:29:55.662916+00:00 app[web.1]: AttributeError: 'tuple' object has no attribute 'decode'
</code>```

答:
That is appropriate only with a call to os.getenv, e.g.:

```<code>redis_url = os.getenv('REDISTOGO_URL', 'redis://localhost:6379')
#           ^^^^^^^^^
redis = redis.from_url(redis_url)
</code>```

This is per: https://devcenter.heroku.com/articles/redistogo

The second value, 'redis://localhost:6379', is used when the environment does not have a variable called REDISTOGO_URL.

You can run heroku config -a your_app_name_here to see if REDISTOGO_URL is set.

Body:
When running heroku logs --tail I get this error.

I'm implementing RedisToGo on my heroku app and I noticed that when I added "REDISTOGO_URL" to my code, it's returning this error. I don`t really know why when I add this it breaks my whole code. I was running it without the "REDISTOGO_URL" but realized I needed it on my app in order to work Thanks in advance.

This is my code:

redis_url = ('REDISTOGO_URL','redis://localhost:6379')
redis = redis.from_url(redis_url)

app.config["SESSION_FILE_DIR"] = mkdtemp()
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "redis"
app.config["SESSION_REDIS"] = redis

sess = Session()
sess.init_app(app)

And this is the whole error code:

2020-06-02T21:29:55.662911+00:00 app[web.1]: connection_pool = ConnectionPool.from_url(url, db=db, **kwargs)
2020-06-02T21:29:55.662912+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/redis/connection.py", line 995, in from_url
2020-06-02T21:29:55.662912+00:00 app[web.1]: url = urlparse(url)
2020-06-02T21:29:55.662912+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/urllib/parse.py", line 372, in urlparse
2020-06-02T21:29:55.662913+00:00 app[web.1]: url, scheme, _coerce_result = _coerce_args(url, scheme)
2020-06-02T21:29:55.662913+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/urllib/parse.py", line 124, in _coerce_args
2020-06-02T21:29:55.662913+00:00 app[web.1]: return _decode_args(args) + (_encode_result,)
2020-06-02T21:29:55.662914+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/urllib/parse.py", line 108, in _decode_args
2020-06-02T21:29:55.662914+00:00 app[web.1]: return tuple(x.decode(encoding, errors) if x else '' for x in args)
2020-06-02T21:29:55.662914+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/urllib/parse.py", line 108, in <genexpr>
2020-06-02T21:29:55.662915+00:00 app[web.1]: return tuple(x.decode(encoding, errors) if x else '' for x in args)
2020-06-02T21:29:55.662916+00:00 app[web.1]: AttributeError: 'tuple' object has no attribute 'decode'

Body:
That is appropriate only with a call to os.getenv, e.g.:

redis_url = os.getenv('REDISTOGO_URL', 'redis://localhost:6379')
#           ^^^^^^^^^
redis = redis.from_url(redis_url)

This is per: https://devcenter.heroku.com/articles/redistogo

The second value, 'redis://localhost:6379', is used when the environment does not have a variable called REDISTOGO_URL.

You can run heroku config -a your_app_name_here to see if REDISTOGO_URL is set.


来源: stackexchange

ContentLicense: CC BY-SA 4.0

Title: Python - Compare TS in 2 col and 2 rows

Body:
I've a Df with multiple engine, a start and end Dt, and an info code. (exemple here)

   engine               start                 end  duration info  energy

20      a 2020-04-16 09:40:00 2020-04-17 00:00:00       860    1    1982
21      a 2020-04-17 00:01:00 2020-04-18 00:00:00      1439    1    3254
22      a 2020-04-18 00:01:00 2020-04-19 00:00:00      1439    1    3258
23      a 2020-04-19 00:01:00 2020-04-20 00:00:00      1439    1    3353
24      a 2020-04-20 00:01:00 2020-04-21 00:00:00      1439    1    3253
25      a 2020-04-21 00:01:00 2020-04-22 00:00:00      1439    1    3053
26      a 2020-04-22 00:01:00 2020-04-22 06:27:00       386    1     820
27      a 2020-04-23 15:26:00 2020-04-24 00:00:00       514    1    1836
28      a 2020-04-24 00:01:00 2020-04-25 00:00:00      1439    1    4797
29      a 2020-04-25 00:01:00 2020-04-26 00:00:00      1439    1    4265

And i'd like to groupby my df to know for each engine when there are a break with the code or with the date.

I'd like to compare like

if dt.loc[i+1, 'start'] - dt.loc[i, 'end'] <= day:
        dt['diff']=  False
    else:
        dt['diff'] = True

Then I can cumsum df['diff']

For exemple, here, i'd like to get

engine a form 2020-04-16 9:40 to 2020-04-22 6:27 the sum of energy and the info code and engine a form 2020-04-23 15:26 to 2020-04-26 0:00

I try this but it's doesn't work

df = df.sort_values(['engine','start','code'])

dt = df[['start','end']]

diff = df['codea']

day = pd.Timedelta('1M')

breaks = dt.start.diff().apply(lambda x: False if x <= day else True)

breaks2 = diff.diff() != 0

groups = breaks.cumsum()

groups2 = breaks2.cumsum()

df['follow'] = groups

df['follow2'] = groups2

dfend = (df.groupby(['engine','follow','follow2','code'])
           .agg(
                   begin=('start','first'),

                end = ('end','last'),
                ensum = ('energy','sum')
                )
         )

Thx a lot

Body:
You can do this using groupby as follows:

import pandas as pd
import numpy as np
df = pd.DataFrame({"engine": ["a"]*10,
                    "start": pd.to_datetime(pd.Series(["2020-04-16 09:40:00", "2020-04-17 00:01:00",
                                                       "2020-04-18 00:01:00", "2020-04-19 00:01:00",
                                                       "2020-04-20 00:01:00", "2020-04-21 00:01:00",
                                                       "2020-04-22 00:01:00", "2020-04-23 15:26:00",
                                                       "2020-04-24 00:01:00", "2020-04-25 00:01:00"])),
                   "end": pd.to_datetime(pd.Series(["2020-04-17 00:00:00", "2020-04-18 00:00:00",
                                                    "2020-04-19 00:00:00", "2020-04-20 00:00:00",
                                                    "2020-04-21 00:00:00", "2020-04-22 00:00:00",
                                                    "2020-04-22 06:27:00", "2020-04-24 00:00:00",
                                                    "2020-04-25 00:00:00", "2020-04-26 00:00:00"])),
                   "duration": [860, 1439, 1439, 1439, 1439, 1439, 386, 514, 1439, 1439],
                   "info": [1]*10,
                   "energy": [1982, 3254, 3258, 3353, 3253, 3053, 820, 1838, 4797, 4265]})

minute = pd.Timedelta('1M')

df.groupby(["engine", "info"])\
   .apply(lambda x:\
          x.groupby(((x["start"] - x["end"].shift(1)) > minute).cumsum())
   .agg({"start": min, "end": max, "energy": np.sum}))

The output is:

                            start                 end  energy
engine info
a      1    0 2020-04-16 09:40:00 2020-04-22 06:27:00   18973
            1 2020-04-23 15:26:00 2020-04-26 00:00:00   10900


问:
passing a tuple as variable
I am passing a tuple by argument, i get the following error.
I searched on the internet and the working method was correct. Can someone have a look?

Person class

```<code>from datetime import datetime
from sqldata import Sqldata

class Person:
    def __init__(self, addr, client):
        self.addr = "192.12"
        self.client = "demorun"
        self.name = none

    def set_name(self, name):
        self.name = name
        values = (self.addr, self.name, str(datetime.now()))
        Sqldata.create_connclient(values)
</code>```

Sqldata class to write data to the database,

```<code>import sqlite3
from sqlite3 import Error
import os

fileDir = os.path.dirname(os.path.realpath(__file__))
filename = "dbConnClients.db"
db_file = os.path.join(fileDir,  "database\\" + filename)

class Sqldata:
    def __init__(self):
        self.conn = None
        try:
            self.conn = sqlite3.connect(db_file)
        except Error as e:
            print(e)

    def create_connclient(self, vdata):
        print(vdata)
        conn = self.conn
        sqlquery = ''' SELECT id FROM ConnClients WHERE name = ? '''
        cur = conn.cursor()
        cur.execute(sqlquery, vdata[1])
</code>```

Error:

```<code>File "...person.py", line 23, in set_name
    Sqldata.create_connclient(vdata)
TypeError: create_connclient() missing 1 required positional argument: 'vdata'
</code>```

答:
I am assuming Sqldata is a class you have created (hesitant due to the lack of that particular code and proper indentation).  Because it is a class, it needs to be instantiated prior to calling it's methods.

I am not sure why you made it a class if you are going to call it anonymously but the below is the solution:

```<code>Sqldata().create_connclient(values)
# if your __init__ function has args,
# you will need to input it in the instantiation
</code>```

What this means is that we have just created an instance of the class object Sqldata. create_connclient has two parameters, self and vdata. By calling Sqldata(), you just created a self value when you call create_connclient and you satisfy all required parameters (as shown in the error).

The Sqldata class is correctly initiated.

```<code>class Sqldata:
    def __init__(self):
        self.conn = None
        ...
</code>```

It is the TypeError which indicates my class method does not accept the tuple. Or am I wrong?

```<code>
</code>```

Title: passing a tuple as variable

Body:
I am passing a tuple by argument, i get the following error.
I searched on the internet and the working method was correct. Can someone have a look?

Person class

from datetime import datetime
from sqldata import Sqldata

class Person:
    def __init__(self, addr, client):
        self.addr = "192.12"
        self.client = "demorun"
        self.name = none

    def set_name(self, name):
        self.name = name
        values = (self.addr, self.name, str(datetime.now()))
        Sqldata.create_connclient(values)

Sqldata class to write data to the database,

import sqlite3
from sqlite3 import Error
import os

fileDir = os.path.dirname(os.path.realpath(__file__))
filename = "dbConnClients.db"
db_file = os.path.join(fileDir,  "database\\" + filename)

class Sqldata:
    def __init__(self):
        self.conn = None
        try:
            self.conn = sqlite3.connect(db_file)
        except Error as e:
            print(e)

    def create_connclient(self, vdata):
        print(vdata)
        conn = self.conn
        sqlquery = ''' SELECT id FROM ConnClients WHERE name = ? '''
        cur = conn.cursor()
        cur.execute(sqlquery, vdata[1])

Error:

File "...person.py", line 23, in set_name
    Sqldata.create_connclient(vdata)
TypeError: create_connclient() missing 1 required positional argument: 'vdata'

Body:
I am assuming Sqldata is a class you have created (hesitant due to the lack of that particular code and proper indentation).  Because it is a class, it needs to be instantiated prior to calling it's methods.

I am not sure why you made it a class if you are going to call it anonymously but the below is the solution:

Sqldata().create_connclient(values)
# if your __init__ function has args,
# you will need to input it in the instantiation

What this means is that we have just created an instance of the class object Sqldata. create_connclient has two parameters, self and vdata. By calling Sqldata(), you just created a self value when you call create_connclient and you satisfy all required parameters (as shown in the error).

The Sqldata class is correctly initiated.

class Sqldata:
    def __init__(self):
        self.conn = None
        ...

It is the TypeError which indicates my class method does not accept the tuple. Or am I wrong?


问:
This function converts a decimal to a hexadecimal, however it only prints two digits. How do I get it to print more?
This only takes numbers up to 255 hence it only prints two digits. Why does it only print two digits? It looks like the while loop only runs once or something. It should run once or twice based on what I think. The hexadecimal for 5368 should be 14F8, the function gets close, but as you can see it only prints the 1 and 4, not the F or 8. Also, how can I get it to convert float numbers?</p>

```<code>import math

def hexConvert(dec):

  while(math.floor(dec/16) > 0):
    x = ""
    rem = dec/16 - math.floor(dec/16)
    myHex = rem*16
    if myHex > 9 :
      if myHex == 10 :
        x += "A"

      if myHex == 11 :
        x += "B"

      if myHex == 12 :
        x += "C"

      if myHex == 13 :
        x += "D"

      if myHex == 14 :
        x += "E"

      if myHex == 15 :
        x += "F"

    else :
      myHex = str(int(myHex))
      x += myHex
    dec = math.floor(dec/16)

  remainder = dec/16 - math.floor(dec/16)
  myHex2 = remainder*16
  if myHex2 > 9 :
    if myHex2 == 10 :
      x += "A"

    if myHex2 == 11 :
      x += "B"

    if myHex2 == 12 :
      x += "C"

    if myHex2 == 13 :
      x += "D"

    if myHex2 == 14 :
      x += "E"

    if myHex2 == 15 :
      x += "F"

  else :
    myHex2 = str(int(myHex2))
    x += str(myHex2)

  x = x[::-1]
  print ("Hex: " + x)

hexConvert(5368)
</code>```

答:
You need to move the assignment of x outside of your while loop. Define x using x = "" immediately after the function definition, then delete the redefinition. The first four lines should read:

```<code>import math

def hexConvert(dec):
  x = ""
  while(math.floor(dec/16) > 0):
    rem = dec/16 - math.floor(dec/16)
</code>```

As of right now, you're clearing out the X value every two digits, causing you to lose the F8 that should be at the end.

Body:
This only takes numbers up to 255 hence it only prints two digits. Why does it only print two digits? It looks like the while loop only runs once or something. It should run once or twice based on what I think. The hexadecimal for 5368 should be 14F8, the function gets close, but as you can see it only prints the 1 and 4, not the F or 8. Also, how can I get it to convert float numbers?

import math

def hexConvert(dec):

  while(math.floor(dec/16) > 0):
    x = ""
    rem = dec/16 - math.floor(dec/16)
    myHex = rem*16
    if myHex > 9 :
      if myHex == 10 :
        x += "A"

      if myHex == 11 :
        x += "B"

      if myHex == 12 :
        x += "C"

      if myHex == 13 :
        x += "D"

      if myHex == 14 :
        x += "E"

      if myHex == 15 :
        x += "F"

    else :
      myHex = str(int(myHex))
      x += myHex
    dec = math.floor(dec/16)

  remainder = dec/16 - math.floor(dec/16)
  myHex2 = remainder*16
  if myHex2 > 9 :
    if myHex2 == 10 :
      x += "A"

    if myHex2 == 11 :
      x += "B"

    if myHex2 == 12 :
      x += "C"

    if myHex2 == 13 :
      x += "D"

    if myHex2 == 14 :
      x += "E"

    if myHex2 == 15 :
      x += "F"

  else :
    myHex2 = str(int(myHex2))
    x += str(myHex2)

  x = x[::-1]
  print ("Hex: " + x)

hexConvert(5368)

Body:
You need to move the assignment of x outside of your while loop. Define x using x = "" immediately after the function definition, then delete the redefinition. The first four lines should read:

import math

def hexConvert(dec):
  x = ""
  while(math.floor(dec/16) > 0):
    rem = dec/16 - math.floor(dec/16)

As of right now, you're clearing out the X value every two digits, causing you to lose the F8 that should be at the end.


问:
Iterate through files, divide two variables and extract the results, including identifier column using name of the original file
I am stuck with a for loop and would need some help please. I use R for certain statistics, but what I need to do now exceeds what I know and can learn without some help. I really appreciate your time.

I edited the question and hope I made it clearer now.

I need to loop over every file in a folder and perform following actions on each file:

Load the data

Identify rows that correspond to variable A (in my case, the coverage of the chromosome, ChromosomeCoverage, numerical)

Identify rows that correspond to variable B (in my case, the coverage
of the phage, PhageCoverage, numerical)

Estimate the mode value (ChromosomeModeEstimate) of variable A (ChromosomeCoverage) which I will use in the next step

Calculate variable C (normalized values of B, C <- PhageCoverage/ChromosomeModeEstimate)

Save C into separate document

Save file name also in this separate document, to know for which file the normalized values are created

Each file in my folder has same dimensions; 4 columns and 45808 rows. The first and the last 5000 rows contain values that should be allocated to ChromosomeCoverage and in between are the values for the PhageCoverage. Files look like this (the 3. and 4. column need to be summed up first before proceeding to next steps, see below):

```<code>position    ref_base    unique_top_cov  unique_bot_cov
661401  T   26  23
661402  T   26  23
661403  C   26  23
661404  T   26  23
661405  C   26  23
661406  C   25  23
661407  G   25  23
661408  T   25  23
661409  G   23  25
</code>```

These are the raw data and I first need to sum unique_top_cov and unique_bot_cov, to obtain the total coverage values, and then perform the other steps.
My script so far goes like this:

```<code>library(modeest) #estimate mode
data_files <- Sys.glob("*reg1.tab") # list all tab files that need to be iterated through
results = list() # In my latest tries I attempted to put the normalized values in a list or vector but couldn’t make it to work. I also tried making an empty dataframe, but similarly unsuccessful (results <- data.frame(ID=character(), normalized=numeric()))

for (i in data_files) {

Data <- read.table(i, header = TRUE, sep = "\t") #read the files
ChromosomeCoverage <- c(Data$unique_bot_cov[1:5000]+Data$unique_top_cov[1:5000],
Data$unique_bot_cov[(length(Data$unique_bot_cov)5000):length(Data$unique_bot_cov)]+
Data$unique_top_cov[(length(Data$unique_top_cov)-5000):length(Data$unique_top_cov)]) #identify chromosome coverage

PhageCoverage <- Data$unique_bot_cov[5001:(length(Data$unique_top_cov)-5001)]+Data$unique_top_cov[5001:(length(Data$unique_top_cov)-5001)] #identify phage coverage

ChromosomeModeEstimate <- mlv(ChromosomeCoverage[ChromosomeCoverage>0]) #consider only values above 0

norm <- PhageCoverage/ChromosomeModeEstimate # here I made my normalized variable and this is where my problems begin. I tried multiple lines of code to somehow try and extract the filenames and normalized values into another dataframe, here only some examples:

filename = data_files[i]
normalized = norm[i]
append(results, c(filename, normalized)) # my results were empty

#When instead of the list, I made my output as an empty dataframe, I tried for example
#results$ID[i] <- paste(unique(data_files))
results$ID[i] <- data_files[i]
results$normalised[i] <- norm
}
</code>```

I kind of don’t know what I am doing here. I don’t know how to append data to the output that needs to grow with each loop. I found how to add a separate column within each of the files that contains file name as identifier for each row, but then again I don’t know how to extract only the part of the column which covers phage region (so first and last 5000 rows removed).

Many thanks for looking at this, I appreciate any suggestion.

答:
To keep things simple, I am assuming that filename contains a string and that normalized contains a number:

```<code>results <- list()
filename <- "myfile.tab"
normalized <- 1
</code>```

Doing what you are doing:

```<code>append(results, c(filename, normalized))
</code>```

gives me two issues. Combining a character and numeric into a vector coerces the numeric to character:

```<code>> c(filename, normalized)
[1] "myfile.tab" "1"
</code>```

Not too hard to deal with but you need to be aware of this.
The other issues is that append does not reassign the value to your object. Your code only results in printing the appended object to the console:

```<code>> x <- c(1, 2)
> append(x, 3)
[1] 1 2 3
> x
[1] 1 2
</code>```

So you have to do instead:

```<code>> x <- c(1, 2)
> x <- append(x, 3)
> x
[1] 1 2 3
</code>```

In your case:

```<code>results <- list()
filename <- "myfile.tab"
normalized <- 1

results <- append(results, c(filename, normalized))
</code>```

Body:
I am stuck with a for loop and would need some help please. I use R for certain statistics, but what I need to do now exceeds what I know and can learn without some help. I really appreciate your time.

I edited the question and hope I made it clearer now.

I need to loop over every file in a folder and perform following actions on each file:

Load the data

Identify rows that correspond to variable A (in my case, the coverage of the chromosome, ChromosomeCoverage, numerical)

Identify rows that correspond to variable B (in my case, the coverage
of the phage, PhageCoverage, numerical)

Estimate the mode value (ChromosomeModeEstimate) of variable A (ChromosomeCoverage) which I will use in the next step

Calculate variable C (normalized values of B, C <- PhageCoverage/ChromosomeModeEstimate)

Save C into separate document

Save file name also in this separate document, to know for which file the normalized values are created

Each file in my folder has same dimensions; 4 columns and 45808 rows. The first and the last 5000 rows contain values that should be allocated to ChromosomeCoverage and in between are the values for the PhageCoverage. Files look like this (the 3. and 4. column need to be summed up first before proceeding to next steps, see below):

position    ref_base    unique_top_cov  unique_bot_cov
661401  T   26  23
661402  T   26  23
661403  C   26  23
661404  T   26  23
661405  C   26  23
661406  C   25  23
661407  G   25  23
661408  T   25  23
661409  G   23  25

These are the raw data and I first need to sum unique_top_cov and unique_bot_cov, to obtain the total coverage values, and then perform the other steps.
My script so far goes like this:

library(modeest) #estimate mode
data_files <- Sys.glob("*reg1.tab") # list all tab files that need to be iterated through
results = list() # In my latest tries I attempted to put the normalized values in a list or vector but couldn’t make it to work. I also tried making an empty dataframe, but similarly unsuccessful (results <- data.frame(ID=character(), normalized=numeric()))

for (i in data_files) {

Data <- read.table(i, header = TRUE, sep = "\t") #read the files
ChromosomeCoverage <- c(Data$unique_bot_cov[1:5000]+Data$unique_top_cov[1:5000],
Data$unique_bot_cov[(length(Data$unique_bot_cov)5000):length(Data$unique_bot_cov)]+
Data$unique_top_cov[(length(Data$unique_top_cov)-5000):length(Data$unique_top_cov)]) #identify chromosome coverage

PhageCoverage <- Data$unique_bot_cov[5001:(length(Data$unique_top_cov)-5001)]+Data$unique_top_cov[5001:(length(Data$unique_top_cov)-5001)] #identify phage coverage

ChromosomeModeEstimate <- mlv(ChromosomeCoverage[ChromosomeCoverage>0]) #consider only values above 0

norm <- PhageCoverage/ChromosomeModeEstimate # here I made my normalized variable and this is where my problems begin. I tried multiple lines of code to somehow try and extract the filenames and normalized values into another dataframe, here only some examples:

filename = data_files[i]
normalized = norm[i]
append(results, c(filename, normalized)) # my results were empty

#When instead of the list, I made my output as an empty dataframe, I tried for example
#results$ID[i] <- paste(unique(data_files))
results$ID[i] <- data_files[i]
results$normalised[i] <- norm
}

I kind of don’t know what I am doing here. I don’t know how to append data to the output that needs to grow with each loop. I found how to add a separate column within each of the files that contains file name as identifier for each row, but then again I don’t know how to extract only the part of the column which covers phage region (so first and last 5000 rows removed).

Many thanks for looking at this, I appreciate any suggestion.

Body:
To keep things simple, I am assuming that filename contains a string and that normalized contains a number:

results <- list()
filename <- "myfile.tab"
normalized <- 1

Doing what you are doing:

append(results, c(filename, normalized))

gives me two issues. Combining a character and numeric into a vector coerces the numeric to character:

> c(filename, normalized)
[1] "myfile.tab" "1"

Not too hard to deal with but you need to be aware of this.
The other issues is that append does not reassign the value to your object. Your code only results in printing the appended object to the console:

> x <- c(1, 2)
> append(x, 3)
[1] 1 2 3
> x
[1] 1 2

So you have to do instead:

> x <- c(1, 2)
> x <- append(x, 3)
> x
[1] 1 2 3

In your case:

results <- list()
filename <- "myfile.tab"
normalized <- 1

results <- append(results, c(filename, normalized))


问:
Getting "hexadecimal value 0x3C, is an invalid attribute character" in correct Japanese XML
All, I'm getting

hexadecimal value 0x3C, is an invalid attribute character

error while trying to read XML on computer with Japanese locale and Japanese characters in the file:

```<code>  $xml = [xml]@(Get-Content $file)
</code>```

Specific place in the XML is pretty benign:

```<code><Control type="select">
</code>```

As far as I can tell almost every error like that is caused by "<" somewhere in the string. So I checked my XML and did not see any extra "<" where it shouldn't be.
But then I've found one hotfix description that says this:

Consider the following scenario: You have a computer that is running Windows Server 2012. The system locale setting of the computer is set
  to Japanese. You install the Network Policy and Access Services (NPAS)
  server role with default settings on the computer. You try to run the
  Best Practices Analyzer tool to scan Network Access Protection (NAP)
  components.

Kinda sounds familiar, so I suspect it's due to Japanese locale or due to Japanese characters in some of the values for the XML.

The file itself starts like this:

```<code><?xml version="1.0" encoding="utf-8"?>
</code>```

Any ideas?

答:
Here's a demo of how this can happen.  This file.xml has one japanese character encoded in utf8 no bom:

```<code><こ/>
</code>```

That character is unicode 0x3053:

```<code>[int][char]'こ' | % tostring x

3053

</code>```

```<code>'こ' | format-hex -Encoding bigendianunicode

   Label: String (System.String) <32532FE4>

          Offset Bytes                                           Ascii
                 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
          ------ ----------------------------------------------- -----
0000000000000000 30 53                                           0S

</code>```

Powershell 5 will assume the file is ascii:

```<code>[xml]$xml = get-content file.xml

Cannot convert value "<ã“/>" to type "System.Xml.XmlDocument". Error: "The '' character,
hexadecimal value 0x81, cannot be included in a name. Line 1, position 3."
At line:1 char:1
+ [xml]$xml = get-content file.xml
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : MetadataError: (:) [], ArgumentTransformationMetadataException
    + FullyQualifiedErrorId : RuntimeException
</code>```

Unicode character 0x3053 will get encoded in utf8 as 3 bytes, "E3 81 93", because of the range it falls in.  The utf8 bit prefixes look like this (https://en.wikipedia.org/wiki/UTF-8).  Three bytes always begin with '1110' or 'E'.  The other '10' bits continue it.

```<code>1110xxxx    10xxxxxx    10xxxxxx
</code>```

Xml doesn't mind the E3 because it's an a with a squiggly line above it, but the 81 just looks like an empty box (stackoverflow can't display it), so it causes an error.  Here are the raw bytes of the file.xml.  "0D 0A" is just carriage return and linefeed.

```<code>format-hex file.xml -encoding utf8

           Path: C:\Users\admin\foo\file.xml

           00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

00000000   3C E3 81 93 2F 3E 0D 0A                          <ã“/>..
</code>```

I don't know how exactly to reproduce the op's error, but it's something like that.

Body:
All, I'm getting

hexadecimal value 0x3C, is an invalid attribute character

error while trying to read XML on computer with Japanese locale and Japanese characters in the file:

  $xml = [xml]@(Get-Content $file)

Specific place in the XML is pretty benign:

<Control type="select">

As far as I can tell almost every error like that is caused by "<" somewhere in the string. So I checked my XML and did not see any extra "<" where it shouldn't be.
But then I've found one hotfix description that says this:

Consider the following scenario: You have a computer that is running Windows Server 2012. The system locale setting of the computer is set
  to Japanese. You install the Network Policy and Access Services (NPAS)
  server role with default settings on the computer. You try to run the
  Best Practices Analyzer tool to scan Network Access Protection (NAP)
  components.

Kinda sounds familiar, so I suspect it's due to Japanese locale or due to Japanese characters in some of the values for the XML.

The file itself starts like this:

<?xml version="1.0" encoding="utf-8"?>

Any ideas?

Body:
Here's a demo of how this can happen.  This file.xml has one japanese character encoded in utf8 no bom:

<こ/>

That character is unicode 0x3053:

[int][char]'こ' | % tostring x

3053

'こ' | format-hex -Encoding bigendianunicode

   Label: String (System.String) <32532FE4>

          Offset Bytes                                           Ascii
                 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
          ------ ----------------------------------------------- -----
0000000000000000 30 53                                           0S

Powershell 5 will assume the file is ascii:

[xml]$xml = get-content file.xml

Cannot convert value "<ã“/>" to type "System.Xml.XmlDocument". Error: "The '' character,
hexadecimal value 0x81, cannot be included in a name. Line 1, position 3."
At line:1 char:1
+ [xml]$xml = get-content file.xml
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : MetadataError: (:) [], ArgumentTransformationMetadataException
    + FullyQualifiedErrorId : RuntimeException

Unicode character 0x3053 will get encoded in utf8 as 3 bytes, "E3 81 93", because of the range it falls in.  The utf8 bit prefixes look like this (https://en.wikipedia.org/wiki/UTF-8).  Three bytes always begin with '1110' or 'E'.  The other '10' bits continue it.

1110xxxx    10xxxxxx    10xxxxxx

Xml doesn't mind the E3 because it's an a with a squiggly line above it, but the 81 just looks like an empty box (stackoverflow can't display it), so it causes an error.  Here are the raw bytes of the file.xml.  "0D 0A" is just carriage return and linefeed.

format-hex file.xml -encoding utf8

           Path: C:\Users\admin\foo\file.xml

           00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

00000000   3C E3 81 93 2F 3E 0D 0A                          <ã“/>..

I don't know how exactly to reproduce the op's error, but it's something like that.


问:
Pass my just created VM's IP as an extension's parameter of the same VM
I created an ARM template which deploys a virtual machine called 'VM1' and then an extension for that virtual machine; that extension calls a PowerShell script on some repo, and I am passing some parameters to it successfully.

My question is: how do I pass the IP of the virtual machine 'VM1' as a parameter of this extension correctly within the same template?

```<code>{
          "type": "Microsoft.Compute/virtualMachines/extensions",
          "apiVersion": "2019-12-01",
          "name": "[concat(parameters('virtualMachines_VM_name'),'/', 'Mypowershell')]",
          "location": "[parameters('location')]",
         "dependsOn": [
            "[resourceId('Microsoft.Compute/virtualMachines', parameters('virtualMachines_VM_name'))]"
        ],
          "properties": {
              "publisher": "Microsoft.Compute",
              "type": "CustomScriptExtension",
              "typeHandlerVersion": "1.7",
              "autoUpgradeMinorVersion":true,
              "settings": {
                "fileUris": [
                  "https://raw.githubusercontent.com/********/********/master/powershell.ps1"
                ],
                "commandToExecute": "[concat('powershell.exe -ExecutionPolicy Unrestricted -File powershell.ps1', ' -database ', parameters('DataBaseName'),
                ' -uid ', parameters('adminUsername'), ' -pwd ', parameters('adminPassword'), ' -dbserver ', parameters('servers_dbserver_name'), ' -vmip ', parameters('VM_IP'))]"
              }
          }}
</code>```

Just for the record, in the code above I tried to pass the VM's IP as an ARM parameter but it did not work.

答:
I realized that my powershell file is in my repo but runs within the VM, that is what an extension does, so i was able to put the following line in my powershwll file:

```<code>$publicIp = (Invoke-RestMethod http://ipinfo.io/json).ip
</code>```

So i did not have to pass this as a parameter in my ARM template in order to use it in my powershell.

Body:
I created an ARM template which deploys a virtual machine called 'VM1' and then an extension for that virtual machine; that extension calls a PowerShell script on some repo, and I am passing some parameters to it successfully.

My question is: how do I pass the IP of the virtual machine 'VM1' as a parameter of this extension correctly within the same template?

{
          "type": "Microsoft.Compute/virtualMachines/extensions",
          "apiVersion": "2019-12-01",
          "name": "[concat(parameters('virtualMachines_VM_name'),'/', 'Mypowershell')]",
          "location": "[parameters('location')]",
         "dependsOn": [
            "[resourceId('Microsoft.Compute/virtualMachines', parameters('virtualMachines_VM_name'))]"
        ],
          "properties": {
              "publisher": "Microsoft.Compute",
              "type": "CustomScriptExtension",
              "typeHandlerVersion": "1.7",
              "autoUpgradeMinorVersion":true,
              "settings": {
                "fileUris": [
                  "https://raw.githubusercontent.com/********/********/master/powershell.ps1"
                ],
                "commandToExecute": "[concat('powershell.exe -ExecutionPolicy Unrestricted -File powershell.ps1', ' -database ', parameters('DataBaseName'),
                ' -uid ', parameters('adminUsername'), ' -pwd ', parameters('adminPassword'), ' -dbserver ', parameters('servers_dbserver_name'), ' -vmip ', parameters('VM_IP'))]"
              }
          }}

Just for the record, in the code above I tried to pass the VM's IP as an ARM parameter but it did not work.

Body:
I realized that my powershell file is in my repo but runs within the VM, that is what an extension does, so i was able to put the following line in my powershwll file:

$publicIp = (Invoke-RestMethod http://ipinfo.io/json).ip

So i did not have to pass this as a parameter in my ARM template in order to use it in my powershell.


问:
debug=true in web.config = BAD thing?
We're seeing lots of virtual memory fragmentation and out of memory errors and then it hits the 3GB limit.

The compilation debug is set to true in the web.config but I get different answers from everyone i ask, does debug set to true cause each aspx to compile into random areas of ram thus fragmenting that ram and eventually causing out of memory problems?

答:
The debug flag should be set to false in web.config, unless you actually need to debug the application.

Running in debug mode can increase the memory usage somewhat, but it's not likely case as severe problems as you are talking about. However, you should set it to false to elliminate the effect that it has, and see if you can notice any improvement.

When run in debug mode, the garbage collection works differently. The life time of variables is expanded from its actual usage to the scope of the variable (to be able to show the value in the debugger). This makes some objects live longer before they are garbage collected.

The compiler doesn't optimize the code when compiling in debug mode, and also some extra nop instructions are added so that each code line has at least one instruction where a break point can be placed.

Throwing an exception takes considerably longer in debug mode. (However, normally the code should not throw exceptions that often.)

OwnerDisplayName: MateloT

LastEditorDisplayName: ScarletGarden

ContentLicense: CC BY-SA 2.5

Title: debug=true in web.config = BAD thing?

OwnerDisplayName: Guffa


问:
Swing thread safety boilerplate
For the sake of simplicity, imagine an application that downloads a file. There is a simple GUI with one label that displays progress. To avoid EDT violations, like every lawful citizen I download the file in one thread (main), and update GUI in another (EDT). So, here's the relevant chunk of pseudcode:

```<code>class Downloader {
    download() {
        progress.startDownload();
        while(hasMoreChunks()) {
            downloadChunk();
            progress.downloadedBytes(n);
        }
        progress.finishDownload();
    }
}

class ProgressDisplay extends JPanel {
    JLabel label;
    startDownload() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                label.setText("Download started");
            }
        });
    }
    downloadedBytes(int n) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                label.setText("Downloaded bytes: " + n);
            }
        });
    }
    finishDownload() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                label.setText("Download finished");
            }
        });
    }
}
</code>```

I love the fact that Java does not support closures and the code is crystal clear to me. Jokes aside, I'm wondering... Am I doing it wrong? Is it possible to eliminate all this ugly boilerplate with SwingUtilities, anonymous implementation of Runnable in every method etc.?

My case is slightly more complicated than this, but I'm trying not to overengineer by implementing proxies or something like that.

答: There is special class for that tasks: SwingWorker. It allows you yo run code in a separate thread and update UI on the end of the work.

ContentLicense: CC BY-SA 3.0

Title: Swing thread safety boilerplate

Body:
For the sake of simplicity, imagine an application that downloads a file. There is a simple GUI with one label that displays progress. To avoid EDT violations, like every lawful citizen I download the file in one thread (main), and update GUI in another (EDT). So, here's the relevant chunk of pseudcode:

class Downloader {
    download() {
        progress.startDownload();
        while(hasMoreChunks()) {
            downloadChunk();
            progress.downloadedBytes(n);
        }
        progress.finishDownload();
    }
}

class ProgressDisplay extends JPanel {
    JLabel label;
    startDownload() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                label.setText("Download started");
            }
        });
    }
    downloadedBytes(int n) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                label.setText("Downloaded bytes: " + n);
            }
        });
    }
    finishDownload() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                label.setText("Download finished");
            }
        });
    }
}

I love the fact that Java does not support closures and the code is crystal clear to me. Jokes aside, I'm wondering... Am I doing it wrong? Is it possible to eliminate all this ugly boilerplate with SwingUtilities, anonymous implementation of Runnable in every method etc.?

My case is slightly more complicated than this, but I'm trying not to overengineer by implementing proxies or something like that.


问:
How to squash all commits except the n most recent?
I see you can do git rebase -i HEAD~3 to handle the latest 3 commits. Is there a way to simply squash every commit before, say, the latest 3 commits without manually going through all the commits and specifying squash?

For context, I created a branch (testBranch) off a branch (originalBranch). I made a bunch of commits, and now I want to merge this branch to the master branch. The problem is, I have all the commits from originalBranch.
I've even tried rebasing by doing git rebase -i HEAD~70 (yes, there were a lot of commits in originalBranch), and editing the file as so:

```<code>r 14133a8 first commit message in originalBranch
s 3cb3a90 message2
s ea61cc4 message3
# ... etc
#commits from testBranch. These should remain after rebase
p a62bff7 first commit message in testBranch
p 4364e1f last commit message in testBranch
</code>```

I though everything from originalBranch would squash into one message, but I get file conflicts when I do this. How can I simply squash every commit from originalBranch into 1 commit message while keeping the commit messages from testBranch, without file conflicts?

答:
This is what filter-branch is for.

```<code>git replace --graft HEAD~3
git filter-branch --all
</code>```

is the meat of it, every ref that traces back to HEAD~3 will get a new history tracing back to the newly-amnesiac replacement commit.  See the filter-branch docs, you might want to rewrite tag names and such.  To back out of a filter-branch whose results you don't like, git fetch . +refs/original/*:*; to clean up after a filter-branch whose results you're sure you want to keep, git filter-branch -f --setup exit.

Body:
I see you can do git rebase -i HEAD~3 to handle the latest 3 commits. Is there a way to simply squash every commit before, say, the latest 3 commits without manually going through all the commits and specifying squash?

For context, I created a branch (testBranch) off a branch (originalBranch). I made a bunch of commits, and now I want to merge this branch to the master branch. The problem is, I have all the commits from originalBranch.
I've even tried rebasing by doing git rebase -i HEAD~70 (yes, there were a lot of commits in originalBranch), and editing the file as so:

r 14133a8 first commit message in originalBranch
s 3cb3a90 message2
s ea61cc4 message3
# ... etc
#commits from testBranch. These should remain after rebase
p a62bff7 first commit message in testBranch
p 4364e1f last commit message in testBranch

I though everything from originalBranch would squash into one message, but I get file conflicts when I do this. How can I simply squash every commit from originalBranch into 1 commit message while keeping the commit messages from testBranch, without file conflicts?

Body:
This is what filter-branch is for.

git replace --graft HEAD~3
git filter-branch --all

is the meat of it, every ref that traces back to HEAD~3 will get a new history tracing back to the newly-amnesiac replacement commit.  See the filter-branch docs, you might want to rewrite tag names and such.  To back out of a filter-branch whose results you don't like, git fetch . +refs/original/*:*; to clean up after a filter-branch whose results you're sure you want to keep, git filter-branch -f --setup exit.


问:
React-Native Components in ReactJS Web
Is it possible to use native react components (View, Text, TouchableOpacity etc.) in a ReactJS web project?</p>

答: It is possible, but not using standard React or React Native. Here is a popular library which was designed to accomplish this: https://github.com/necolas/react-native-web</p>

Title: React-Native Components in ReactJS Web


问:
Enable Azure AD SSO on Web Server which has no internet connection
I implement my ASP.Net Web Application SSO with Azure AD. Everything work fine my testing server which has an internet connection.
My customer policy does not allow internet connection on their server and the SSO failed when validate the token from the Microsoft after user authenticated. The error is "IDX20803: Unable to obtain configuration from: [PII is Hidden]".
I add the Microsoft.IdentityModel.Logging.IdentityModelEventSource.ShowPII = true; to my Startup.cs class, the error more detail: "IDX20803: Unable to obtain configuration from: 'https://login.microsoftonline.com/MYTENANTID/.well-known/openid-configuration'".

My question is: I will request my customer open their firewall to allow only login.mirosoftonline.com. Does it enough? Does it need to allow another requests? And what are another requests?

答:
according to microsoft, https://learn.microsoft.com/en-ca/office365/enterprise/urls-and-ip-address-ranges#microsoft-365-common-and-office-online

I believe category 56 is for identity and authentication, whether you need them all, I'm not sure, but Microsoft seems to think so?

56  Allow
Required    Yes     *.msappproxy.net, *.msftidentity.com, *.msidentity.com, account.activedirectory.windowsazure.com, accounts.accesscontrol.windows.net, adminwebservice.microsoftonline.com, api.passwordreset.microsoftonline.com, autologon.microsoftazuread-sso.com, becws.microsoftonline.com, clientconfig.microsoftonline-p.net, companymanager.microsoftonline.com, device.login.microsoftonline.com, graph.microsoft.com, graph.windows.net, login.microsoft.com, login.microsoftonline.com, login.microsoftonline-p.com, login.windows.net, logincert.microsoftonline.com, loginex.microsoftonline.com, login-us.microsoftonline.com, nexus.microsoftonline-p.com, passwordreset.microsoftonline.com, provisioningapi.microsoftonline.com
20.190.128.0/18, 40.126.0.0/18, 2603:1006:2000::/48, 2603:1007:200::/48, 2603:1016:1400::/48, 2603:1017::/48, 2603:1026:3000::/48, 2603:1027:1::/48, 2603:1036:3000::/48, 2603:1037:1::/48, 2603:1046:2000::/48, 2603:1047:1::/48, 2603:1056:2000::/48, 2603:1057:2::/48


问:
How can I increase the client_max_body_size in Elastic Beanstalk
I am using Golang with elastic Beanstalk and I find that I am able to upload files up to 1 MB and the ones bigger than that fail with the error client intended to send too large body: 1749956 bytes the bytes obviously depend on the file size . I have been reading this post Increasing client_max_body_size in Nginx conf on AWS Elastic Beanstalk and I changed my code created a file 01_nginx.config under the ebExtensions and put the following in it and I try to upload a video of 3 MB and it still gives that error, any suggestions ? I am new to this

```<code>files:
    "/etc/nginx/conf.d/proxy.conf" :
        mode: "000755"
        owner: root
        group: root
        content: |
           client_max_body_size 20M;
</code>```

答:
I have tried all .ebextensions method of adding implementation level configuration for rails and it didn't help me in the latest Amazon Linux AMI. For the latest Amazon Linux AMI You need to follow this structure to increase the upload size. You need to follow this structure to increase the upload size limit.

Add the below folder setup in the root level of your project folder.

Folder structure (.platform/nginx/conf.d/proxy.conf)

```<code>.platform/
         nginx/
              conf.d/
                    proxy.conf
</code>```

Add this line to proxy.conf (Inside .platform/nginx/conf.d/ folder)

```<code>client_max_body_size 50M;
</code>```

commit this file and deploy again using eb deploy.

Body:
I am using Golang with elastic Beanstalk and I find that I am able to upload files up to 1 MB and the ones bigger than that fail with the error client intended to send too large body: 1749956 bytes the bytes obviously depend on the file size . I have been reading this post Increasing client_max_body_size in Nginx conf on AWS Elastic Beanstalk and I changed my code created a file 01_nginx.config under the ebExtensions and put the following in it and I try to upload a video of 3 MB and it still gives that error, any suggestions ? I am new to this

files:
    "/etc/nginx/conf.d/proxy.conf" :
        mode: "000755"
        owner: root
        group: root
        content: |
           client_max_body_size 20M;

Body:
I have tried all .ebextensions method of adding implementation level configuration for rails and it didn't help me in the latest Amazon Linux AMI. For the latest Amazon Linux AMI You need to follow this structure to increase the upload size. You need to follow this structure to increase the upload size limit.

Add the below folder setup in the root level of your project folder.

Folder structure (.platform/nginx/conf.d/proxy.conf)

.platform/
         nginx/
              conf.d/
                    proxy.conf

Add this line to proxy.conf (Inside .platform/nginx/conf.d/ folder)

client_max_body_size 50M;

commit this file and deploy again using eb deploy.


问:
HTTP GET request to get binary content of an XML file
I'm trying to get the binary content of an XML file through HTTP but the server just want to give me kind of interpreted format (wich removes \r for every \r\n).

My HTTP GET request is simple : "GET http://%s%s%s HTTP/1.0\r\nConnection: Keep-Alive\r\n\r\n"

It works for extention except .xml. And if I change the .xml in .notxml, it works.

So, I'm looking for a way to the binary content without any change in the file.

Thank you in advance.

答:
This is almost definitely a Mime-Type issue. If you're using IIS I would suggest checking out:

http://technet.microsoft.com/en-us/library/bb742440.aspx


问:
Data organisation in graph database
This is more of a logical question rather than technical one. I am asking for data organisation guidance for my requirements. Please keep in mind that I am willing to use a graph database for this purpose (though I am pretty new at that). So guidance in graph database context would be much appreciated.

Let me provide an overview of the scenario. There are two entities in the app, User and House. User can owns a house or rents a house. If an user rents a house, there should be time period mentioned for which the user has rented the house. An user may rent same house for different periods.

Demo Dataset:

```<code>A (User) -owns-> H1, H2, H3 (House) - one-liner for brevity
X -rents-> H2 (start=DATE1, end=DATE2)
Y -rents-> H2 (start=DATE3, end=DATE4)
X -rents-> H2 (start=DATE5, end=DATE6) - user rents same house again
</code>```

I am assuming that User and House would be nodes and owns and rents would be edges. Rent period would be properties of rents edges. Please point out if there is any better way.

Questions:

Is this possible in graph database in general to have multiple edges of same type between two nodes? Should I keep just one edge for rent of a specific user to specific house and add periods? Or should I maintain multiple edges for multiple periods?

Is it possible to query for something like: "fetch all the houses that were empty for a period of 3 months"? This should fetch the houses that have a gap of 3 months between consecutive end and next start dates in rents. These houses may not be empty now.

I have checked neo4j, cayley, dgraph etc. Which may be better with this scenario?

Any guidance of how I should keep the data with relationships would be much appreciated. Have a nice day.

答:
There could be a better answer than this, still posting my experience with graph on the given requirement if this of any help to you.

I think it is the best fit for the graph DB for your requirement and to answer your questions.

It is more of designing your graph model to suit the purpose and I think you can have multiple rent edges with different periods from node user to node house.
Which way you can maintain the history and you can later delete the older/expired period edges if you want.

[Just to avoid duplicates] Assume here you need to make sure the edge would be created between nodes (user & house) only if the period slot is free.
You can add logic to the query while creating the edge between nodes.

With the given demo data set, here is the sample graph I have created based on the scenario you have described.

http://console.neo4j.org/?id=bxu3sp

Click on the above console link and you can run the below cypher query in the query window at the bottom

```<code>    MATCH (user:User)-[rent:RENT]->(house:House)
    WITH house, rent
    ORDER BY rent.startDate
    WITH collect(rent) as rents, house
    UNWIND range(0, size(rents)-1) as index
    WITH rents, index, house
    WHERE duration.inDays(date(rents[index].endDate), date(rents[index+1].startDate)).days > 30
    RETURN house
</code>```

This would get the list of houses that was with no allocation for a given period range.

I'm not an expert and never used other than neo4j and so far with my experience on neo4j, documentation is really good and it is powerful with additions like Kafka integrations, GraphQL, Halin monitoring, APOC, etc.

I'd say it is a learning curve, just explore and play around with it to get yourself into the graph DB world.

Update:
In case of the same user renting the same house for different periods then the graph would look something like below as said you should avoid creating duplicates by not allowing edges for the same/overlapping window period between any user node and any house node. here in this graph, I have created edges for the different and non-overlapping start/end date so which is valid and not a duplicate.

Title: Data organisation in graph database

Body:
This is more of a logical question rather than technical one. I am asking for data organisation guidance for my requirements. Please keep in mind that I am willing to use a graph database for this purpose (though I am pretty new at that). So guidance in graph database context would be much appreciated.

Let me provide an overview of the scenario. There are two entities in the app, User and House. User can owns a house or rents a house. If an user rents a house, there should be time period mentioned for which the user has rented the house. An user may rent same house for different periods.

Demo Dataset:

A (User) -owns-> H1, H2, H3 (House) - one-liner for brevity
X -rents-> H2 (start=DATE1, end=DATE2)
Y -rents-> H2 (start=DATE3, end=DATE4)
X -rents-> H2 (start=DATE5, end=DATE6) - user rents same house again

I am assuming that User and House would be nodes and owns and rents would be edges. Rent period would be properties of rents edges. Please point out if there is any better way.

Questions:

Is this possible in graph database in general to have multiple edges of same type between two nodes? Should I keep just one edge for rent of a specific user to specific house and add periods? Or should I maintain multiple edges for multiple periods?

Is it possible to query for something like: "fetch all the houses that were empty for a period of 3 months"? This should fetch the houses that have a gap of 3 months between consecutive end and next start dates in rents. These houses may not be empty now.

I have checked neo4j, cayley, dgraph etc. Which may be better with this scenario?

Any guidance of how I should keep the data with relationships would be much appreciated. Have a nice day.

Body:
There could be a better answer than this, still posting my experience with graph on the given requirement if this of any help to you.

I think it is the best fit for the graph DB for your requirement and to answer your questions.

It is more of designing your graph model to suit the purpose and I think you can have multiple rent edges with different periods from node user to node house.
Which way you can maintain the history and you can later delete the older/expired period edges if you want.

[Just to avoid duplicates] Assume here you need to make sure the edge would be created between nodes (user & house) only if the period slot is free.
You can add logic to the query while creating the edge between nodes.

With the given demo data set, here is the sample graph I have created based on the scenario you have described.

http://console.neo4j.org/?id=bxu3sp

Click on the above console link and you can run the below cypher query in the query window at the bottom

    MATCH (user:User)-[rent:RENT]->(house:House)
    WITH house, rent
    ORDER BY rent.startDate
    WITH collect(rent) as rents, house
    UNWIND range(0, size(rents)-1) as index
    WITH rents, index, house
    WHERE duration.inDays(date(rents[index].endDate), date(rents[index+1].startDate)).days > 30
    RETURN house

This would get the list of houses that was with no allocation for a given period range.

I'm not an expert and never used other than neo4j and so far with my experience on neo4j, documentation is really good and it is powerful with additions like Kafka integrations, GraphQL, Halin monitoring, APOC, etc.

I'd say it is a learning curve, just explore and play around with it to get yourself into the graph DB world.

Update:
In case of the same user renting the same house for different periods then the graph would look something like below as said you should avoid creating duplicates by not allowing edges for the same/overlapping window period between any user node and any house node. here in this graph, I have created edges for the different and non-overlapping start/end date so which is valid and not a duplicate.


问:
When I get the row index from numpy array: only size-1 arrays can be converted to Python scalars
I trying to implement "Brute Force" for Knapsack problem, I randomly generating table of values and costs. My BruteForce returns corrects answers but sometimes it returns TypeError: only size-1 arrays can be converted to Python scalars. The problem is in that line "bestItems.append(int(np.where(np.all(table==element, axis=1))[0]))"

```<code>def BruteForce(table):
    wholeWeight = table[:,0].sum()
    bagWeight = 0.5 * wholeWeight
    bestScore = 0
    bestItems = []
    for num in range(len(table)):
        for com in combinations(table,num+1):
            WEIGHT = sum([elem[0] for elem in com])
            COST = sum([elem[1] for elem in com])
            if WEIGHT <= bagWeight and bestScore < COST:
                bestScore = COST
                bestItems = []
                for element in com:
                    bestItems.append(int(np.where(np.all(table==element, axis=1))[0]))
    return bestScore, bestItems
</code>```

For example:

```<code>np.array([[2, 8],
 [2, 8],
 [6 ,7],
 [6, 5],
 [6, 1]])
</code>```

Returns:
TypeError: only size-1 arrays can be converted to Python scalars

But if table =:

```<code>np.array([[9, 6],
 [4, 7],
 [8 ,9],
 [9, 7],
 [3, 4]])
</code>```

Returns: (20, [1, 2, 4])

答:
```<code>In [45]: table = np.array([[2, 8],
    ...:  [2, 8],
    ...:  [6 ,7],
    ...:  [6, 5],
    ...:  [6, 1]])

In [47]: np.all(table==[2,8], axis=1)
Out[47]: array([ True,  True, False, False, False])
In [48]: np.where(np.all(table==[2,8], axis=1))
Out[48]: (array([0, 1]),)
In [49]: np.where(np.all(table==[2,8], axis=1))[0]
Out[49]: array([0, 1])
In [50]: int(np.where(np.all(table==[2,8], axis=1))[0])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-50-cb698d9f7e05> in <module>
----> 1 int(np.where(np.all(table==[2,8], axis=1))[0])

TypeError: only size-1 arrays can be converted to Python scalars
</code>```

The where produces a tuple of integer arrays.  The int() wrapper only works when there is exactly one match.

```<code>In [51]: int(np.where(np.all(table==[2,7], axis=1))[0])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-51-294e3139501e> in <module>
----> 1 int(np.where(np.all(table==[2,7], axis=1))[0])

TypeError: only size-1 arrays can be converted to Python scalars
In [52]: int(np.where(np.all(table==[6,7], axis=1))[0])
Out[52]: 2
</code>```

You need to handle the matches more robustly.

Body:
I trying to implement "Brute Force" for Knapsack problem, I randomly generating table of values and costs. My BruteForce returns corrects answers but sometimes it returns TypeError: only size-1 arrays can be converted to Python scalars. The problem is in that line "bestItems.append(int(np.where(np.all(table==element, axis=1))[0]))"

def BruteForce(table):
    wholeWeight = table[:,0].sum()
    bagWeight = 0.5 * wholeWeight
    bestScore = 0
    bestItems = []
    for num in range(len(table)):
        for com in combinations(table,num+1):
            WEIGHT = sum([elem[0] for elem in com])
            COST = sum([elem[1] for elem in com])
            if WEIGHT <= bagWeight and bestScore < COST:
                bestScore = COST
                bestItems = []
                for element in com:
                    bestItems.append(int(np.where(np.all(table==element, axis=1))[0]))
    return bestScore, bestItems

For example:

np.array([[2, 8],
 [2, 8],
 [6 ,7],
 [6, 5],
 [6, 1]])

Returns:
TypeError: only size-1 arrays can be converted to Python scalars

But if table =:

np.array([[9, 6],
 [4, 7],
 [8 ,9],
 [9, 7],
 [3, 4]])

Returns: (20, [1, 2, 4])

Body:
In [45]: table = np.array([[2, 8],
    ...:  [2, 8],
    ...:  [6 ,7],
    ...:  [6, 5],
    ...:  [6, 1]])

In [47]: np.all(table==[2,8], axis=1)
Out[47]: array([ True,  True, False, False, False])
In [48]: np.where(np.all(table==[2,8], axis=1))
Out[48]: (array([0, 1]),)
In [49]: np.where(np.all(table==[2,8], axis=1))[0]
Out[49]: array([0, 1])
In [50]: int(np.where(np.all(table==[2,8], axis=1))[0])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-50-cb698d9f7e05> in <module>
----> 1 int(np.where(np.all(table==[2,8], axis=1))[0])

TypeError: only size-1 arrays can be converted to Python scalars

The where produces a tuple of integer arrays.  The int() wrapper only works when there is exactly one match.

In [51]: int(np.where(np.all(table==[2,7], axis=1))[0])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-51-294e3139501e> in <module>
----> 1 int(np.where(np.all(table==[2,7], axis=1))[0])

TypeError: only size-1 arrays can be converted to Python scalars
In [52]: int(np.where(np.all(table==[6,7], axis=1))[0])
Out[52]: 2

You need to handle the matches more robustly.


问:
Image to take the height and with of parent element
so today i did good with advancing in learning to make an app with flutter , i made a ListView , but the image didnt take the full height and with of the parent element , i tried some solutions but it didnt workout

this is the class

```<code>class FlowersItem extends StatelessWidget {

  final String imgAssetPath;

  FlowersItem({this.imgAssetPath});

  @override
  Widget build(BuildContext context) {
    return Container(
     width: 200,
      alignment: Alignment.center,
      padding: EdgeInsets.symmetric(horizontal: 30),
      margin: EdgeInsets.only(right: 16),
      decoration: BoxDecoration(
        color: Color(0xff29404E),
        borderRadius: BorderRadius.circular(12)
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
        Column(children: <Widget>[
            ClipRect(
                child: FittedBox(
  child:    Image.asset(imgAssetPath),
  fit: BoxFit.fill,

),
       )
        ],)

        ],
      ),
    );
  }
}

List<FlowersItem> flowers = [
 FlowersItem(
  imgAssetPath: 'assets/images/flower1.jpeg',

  ),
FlowersItem(
   imgAssetPath: 'assets/images/flower-pot.png',

  ),
FlowersItem(
     imgAssetPath: 'assets/images/flower-pot.png',
  ),
  FlowersItem(
      imgAssetPath: 'assets/images/flower-pot.png',
  ),
FlowersItem(
     imgAssetPath: 'assets/images/flower-pot.png',

  ),
];
</code>```

答:
Have you tried BoxFit.cover? I think that's what you are looking for.
It generally works for me when I want to fill out and entire box with an image.

Body:
so today i did good with advancing in learning to make an app with flutter , i made a ListView , but the image didnt take the full height and with of the parent element , i tried some solutions but it didnt workout

this is the class

class FlowersItem extends StatelessWidget {

  final String imgAssetPath;

  FlowersItem({this.imgAssetPath});

  @override
  Widget build(BuildContext context) {
    return Container(
     width: 200,
      alignment: Alignment.center,
      padding: EdgeInsets.symmetric(horizontal: 30),
      margin: EdgeInsets.only(right: 16),
      decoration: BoxDecoration(
        color: Color(0xff29404E),
        borderRadius: BorderRadius.circular(12)
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
        Column(children: <Widget>[
            ClipRect(
                child: FittedBox(
  child:    Image.asset(imgAssetPath),
  fit: BoxFit.fill,

),
       )
        ],)

        ],
      ),
    );
  }
}

List<FlowersItem> flowers = [
 FlowersItem(
  imgAssetPath: 'assets/images/flower1.jpeg',

  ),
FlowersItem(
   imgAssetPath: 'assets/images/flower-pot.png',

  ),
FlowersItem(
     imgAssetPath: 'assets/images/flower-pot.png',
  ),
  FlowersItem(
      imgAssetPath: 'assets/images/flower-pot.png',
  ),
FlowersItem(
     imgAssetPath: 'assets/images/flower-pot.png',

  ),
];


问:
Why does sys.getsizeof() return a value that is 33 bytes more than the actual size?
The following code returns 35 as the answer:

```<code>print(sys.getsizeof((1024).to_bytes(2, byteorder='big')))
</code>```

Shouldn't the int.to_bytes() return a byte representation of 1024 that is 2 bytes long because the documentation mentions:

```<code>The integer is represented using length bytes. An OverflowError is raised if the integer is not representable with the given number of bytes.
</code>```

What am I understanding incorrectly?

答: I believe that you neglected to account for the garbage collector overhead that comes with each python object on creation. Additionally take note that sys.getsizeof() doesn't count the sizes of the objects that an object refers to.

Body:
The following code returns 35 as the answer:

print(sys.getsizeof((1024).to_bytes(2, byteorder='big')))

Shouldn't the int.to_bytes() return a byte representation of 1024 that is 2 bytes long because the documentation mentions:

The integer is represented using length bytes. An OverflowError is raised if the integer is not representable with the given number of bytes.

What am I understanding incorrectly?


问:
How can I check the existence of a GitHub repo?
I coding a PHP script that has to check if exists a GitHub repo that the user enters. But I don't know how can I do it. I think the main matter is to know which HTTP code gives GitHub, but I can't find anything like that in cURL PHP documentation. How can I do it?</p>

答: The 404 check is bad because it will return the same 404 and error message if the request straight up fails due to, eg auth issues. Therefore it could incorrectly tell your program that the repo doesn't exist, when it does, but you just couldn't access github API.</p>


问:
If span contains exact text, add class to certain element on page
I have this code below.

```<code>    $("span.number").filter(function() {
    if (return ($(this).text() === 'PENDING')) {
        $(".property-thumb").addClass("pending");
    }
    if (return ($(this).text() === 'PENDING INSPECTION')) {
        $(".property-thumb").addClass("pendinginspection");
    }
});
</code>```

Simply speaking, if "span.number" contains PENDING or PENDING INSPECTION, I want certain classes to be added to ".property-thumb". Why isn't this working?

答:
You can't use the return keyword in an if statement.That is invalid syntax.

Instead of filter use an each loop and remove the return statement.

In the loop get the select the current element being looped over with $(this), find the parent in which it is in with parents() (this is to create a context of which thumbnail it should select), and select the thumbnail element from the parent with find().

Check with each item in $("span.number") if the text is either PENDING or PENDING INSPECTION with an if...else if statement and then add the classes accordingly to the thumbnail.

Tip: the $ before a variable can be a way to indicate that the value is a jQuery object so you know what kind of value it is and what you can do with it.

```<code>$("span.number").each(function() {
  var $this = $(this);
  var text = $this.text();
  var $property = $this.parents('.property');
  var $propertyThumb = $property.find('.property-thumb');
  if (text === 'PENDING') {
    $propertyThumb.addClass("pending");
  } else if (text === 'PENDING INSPECTION') {
    $propertyThumb.addClass("pendinginspection");
  }
}
</code>```

Body:
I have this code below.

    $("span.number").filter(function() {
    if (return ($(this).text() === 'PENDING')) {
        $(".property-thumb").addClass("pending");
    }
    if (return ($(this).text() === 'PENDING INSPECTION')) {
        $(".property-thumb").addClass("pendinginspection");
    }
});

Simply speaking, if "span.number" contains PENDING or PENDING INSPECTION, I want certain classes to be added to ".property-thumb". Why isn't this working?

Body:
You can't use the return keyword in an if statement.That is invalid syntax.

Instead of filter use an each loop and remove the return statement.

In the loop get the select the current element being looped over with $(this), find the parent in which it is in with parents() (this is to create a context of which thumbnail it should select), and select the thumbnail element from the parent with find().

Check with each item in $("span.number") if the text is either PENDING or PENDING INSPECTION with an if...else if statement and then add the classes accordingly to the thumbnail.

Tip: the $ before a variable can be a way to indicate that the value is a jQuery object so you know what kind of value it is and what you can do with it.

$("span.number").each(function() {
  var $this = $(this);
  var text = $this.text();
  var $property = $this.parents('.property');
  var $propertyThumb = $property.find('.property-thumb');
  if (text === 'PENDING') {
    $propertyThumb.addClass("pending");
  } else if (text === 'PENDING INSPECTION') {
    $propertyThumb.addClass("pendinginspection");
  }
}


问:
Is there a way to start a program N times at once?
I am making a python bot that makes twitter accounts, just for educational purposes with throw-away emails (catchalls) and russian phonenumbers. I managed to get through both email and phone verification and was wondering if I can create accounts at a large scale by starting N webdrivers at once.

Right now I made a code that only loops the program N times. I removed the code but it looked like this:

```<code>amount = input....
for i in range(amount)
       App.run()
</code>```

This was my only hope in actually doing this. Does anyone know how I can do this and if a computer can actually handle 10 or 100 headless webdrivers from selenium at once?

答:
Well, you need to create multiple threads instead of looping, then you can start each upload in parallel threads. You are on the right track. You don't need a selenium grid to achieve this.

lookup about multithreading. You can start with this answer(Threads in Java)

It's not right you need grid for executing multiple browser sessions. You can invoke multiple browser sessions by just creating multiple driver objects, and managing them. Each session will be separate if you want them to be.

Grid is for scaling as there is a limitation on the no of browser instances you can run keeping your machine performance intact and tests stable. Like more than 5 chrome instances in a single machine. If you want to do more than that then you have to use selenium Grid.

Body:
I am making a python bot that makes twitter accounts, just for educational purposes with throw-away emails (catchalls) and russian phonenumbers. I managed to get through both email and phone verification and was wondering if I can create accounts at a large scale by starting N webdrivers at once.

Right now I made a code that only loops the program N times. I removed the code but it looked like this:

amount = input....
for i in range(amount)
       App.run()

This was my only hope in actually doing this. Does anyone know how I can do this and if a computer can actually handle 10 or 100 headless webdrivers from selenium at once?


问:
Is there a function in R that splits a matrix along a margin using a factor or categorical vector?
I am trying to split a matrix in the same way that you might split a data.frame using split. Is there a function that does that? For example, I have matrix m and I am trying to split it into a list of matrices using vector g.

```<code>m <- matrix(rnorm(50), ncol = 5)
groups <- c('A', 'B', 'C')
g <- sample(groups, 10, replace = T)
</code>```

split doesn't seem to work with matrices so we could convert it into a data.frame:

```<code>split(data.frame(m), f = g)
</code>```

This works but I'd like to keep it as a matrix. The following loop works:

```<code>lapply(groups, function(x) m[g == x,])
</code>```

But is there a dedicated function, or a better way?

答:
We can split on the sequence of rows of 'm', and use that index to subset the rows of 'm'

```<code>lapply(split(seq_len(nrow(m)), g), function(i) m[i,])
</code>```

Body:
I am trying to split a matrix in the same way that you might split a data.frame using split. Is there a function that does that? For example, I have matrix m and I am trying to split it into a list of matrices using vector g.

m <- matrix(rnorm(50), ncol = 5)
groups <- c('A', 'B', 'C')
g <- sample(groups, 10, replace = T)

split doesn't seem to work with matrices so we could convert it into a data.frame:

split(data.frame(m), f = g)

This works but I'd like to keep it as a matrix. The following loop works:

lapply(groups, function(x) m[g == x,])

But is there a dedicated function, or a better way?

Body:
We can split on the sequence of rows of 'm', and use that index to subset the rows of 'm'

lapply(split(seq_len(nrow(m)), g), function(i) m[i,])


问:
What is the best way to cache database query on asp.net-mvc website for performance?

Possible Duplicate:

  Caching in asp.net-mvc

i have an asp.net-mvc site and i am running an expensive database query, where the results rarely change, so i wanted to adding caching,  so when other users bring up the same web page it doesn't go out to the db but just grabs from this cache. (and maybe forced a db get on some expiration timeframe . .)

i have used syscache before to do this 2nd level caching across different users, when i was using nhibernate but (for other reasons not worth discussing) i can't use nhibernate in this case.

I wanted to replicate that same behavior from what i am getting with nhibernate syscache (2nd level cache) in a scenario without nhiberate.

Is this possible and are there recommended solutions for this.  I only have a single web server so i don't need to deal with any distributed issues.

答: Memcached is a good solution. It basically is a key/value dictionary that runs in memory as a separate process. You can check the cache first, then grab from the database if it has expired.</p>


问:
Unable to insert data into sqlite3 database using PHP
I've been trying to get my PHP code to insert data into my sqlite3 database. I have read the documentation for using sqlite3 with PHP, and have tried to follow it carefully. Here is the specific syntax on how to do insertion queries in PHP for sqlite3: https://www.php.net/manual/en/sqlite3.exec.php

Here is some insertion code I am trying to execute. Each variable value in the query is gathered from an html form.

$db->exec("
   INSERT INTO BOOK(Title,Author,Genre,ISBN_number,Age_rating,Language,Book_review,Price)
   VALUES (
'$Btitle', '$BAuthor', '$BGenre', '$BISBN', '$age', '$lan', '$bookRev', '$pr')
");
```

This code does not throw any errors. However, when I do a selection query through PHP to display the books, the entry from this line of code is not in the table.

答: Finally found the problem. The form's submit tag in the html had the value "Submit" instead of "Submit Action".</p>

Body:
I've been trying to get my PHP code to insert data into my sqlite3 database. I have read the documentation for using sqlite3 with PHP, and have tried to follow it carefully. Here is the specific syntax on how to do insertion queries in PHP for sqlite3: https://www.php.net/manual/en/sqlite3.exec.php

Here is some insertion code I am trying to execute. Each variable value in the query is gathered from an html form.

$db->exec("
   INSERT INTO BOOK(Title,Author,Genre,ISBN_number,Age_rating,Language,Book_review,Price)
   VALUES (
'$Btitle', '$BAuthor', '$BGenre', '$BISBN', '$age', '$lan', '$bookRev', '$pr')
");

This code does not throw any errors. However, when I do a selection query through PHP to display the books, the entry from this line of code is not in the table.


问:
bootstrap vue what does the site use for the search results popup
Which bootstrap vue component does the bootstrap vue site use?

答: https://github.com/algolia/docsearch it sadly looks not to be a bootstrap-vue element</p>


问:
How can I use an href link within a "data" attribute - or rewrite it with jQuery?
I'm using the data attribute to show a credit for a photo on a site:

```<code><div id="test" data-credit="name">
</code>```

But how do I do use a link, like this?:

```<code><div id="test" data-credit="<a href="http://mydomain.com">name</a>">
</code>```

Escaping the html, like this, doesn't work:

```<code><a href="http://mydomain.com" >name</a>
</code>```

It that can't be done, how would I use jQuery to rewrite the data-credit "name" into "name" on document ready?

JSfiddle here: http://jsfiddle.net/8muBm/58/

答:
This should perform the rewrite with jQuery:

```<code>$("div[data-credit]").each(function(){
    $(this).attr("data-credit",'<a href="http://mydomain.com">'+ $(this).attr("data-credit")+'</a>');
});
</code>```

It looks for all div elements with data-credit attribute, gets that attribute and wraps it around that url and assigns it back to the attribute. But as Felix already asked, why would you want to store the full HTML there?

Example: http://jsfiddle.net/niklasvh/fBW4V/

Body:
I'm using the data attribute to show a credit for a photo on a site:

But how do I do use a link, like this?:

<div id="test" data-credit="name">

Escaping the html, like this, doesn't work:

name

It that can't be done, how would I use jQuery to rewrite the data-credit "name" into "name" on document ready?

JSfiddle here: http://jsfiddle.net/8muBm/58/

Body:
This should perform the rewrite with jQuery:

$("div[data-credit]").each(function(){
    $(this).attr("data-credit",'<a href="http://mydomain.com">'+ $(this).attr("data-credit")+'</a>');
});

It looks for all div elements with data-credit attribute, gets that attribute and wraps it around that url and assigns it back to the attribute. But as Felix already asked, why would you want to store the full HTML there?

Example: http://jsfiddle.net/niklasvh/fBW4V/


问:
Detect when dialog sets focus to various combo controls using PreTranslateMessage
I have a CDialog and I am trying to override PreTranslateMessage like this:

```<code>BOOL CWeekendMeetingDlg::PreTranslateMessage(MSG* pMsg)
{

    if (pMsg->message == WM_SETFOCUS)
    {
        if (::GetDlgCtrlID(pMsg->hwnd) != IDC_COMBO_PT_CHAIRMAN &&
            ::GetDlgCtrlID(pMsg->hwnd) != IDC_COMBO_PT_OPEN_PRAYER &&
            ::GetDlgCtrlID(pMsg->hwnd) != IDC_COMBO_WT_CLOSE_PRAYER &&
            ::GetDlgCtrlID(pMsg->hwnd) != IDC_COMBO_WEEKEND_HOST &&
            ::GetDlgCtrlID(pMsg->hwnd) != IDC_COMBO_WEEKEND_COHOST &&
            ::GetDlgCtrlID(pMsg->hwnd) != IDC_COMBO_PT_READER)
        {
            if (m_gridAssignHist.GetRowCount() != 1)
            {
                m_gridAssignHist.SetRowCount(1);
            }
        }
    }

    return CDialog::PreTranslateMessage(pMsg);
}
</code>```

It is not working. What I want to do is reset my CGridCtrl row count to 1 when other controls gain focus (eg: edit combo). But if I put a breakpoint inside the first if it is never intercepted.

At the moment the only thing I can think of is to renumber all of the combo IDs on the dialog so they are sequential and then use a command range for OnSetFocus and detect in that handler. But there are also some CEdit controls.

Can't I use PTM though and avoid that? For me it seems it would be the easiest.

Here is the dialog:

At the moment I have 7 combo OnSetFocus handlers. When they fire the display specific assignment history in the grid on the right.

So if the user moves to any other control on the dialog, the assignment history control will not apply. And that was why i wanted to reset the history to just the header row. And I was hoping to do this with PTM.

答:
I have added a 2 second timer to the dialog:

```<code>void CWeekendMeetingDlg::OnTimer(UINT_PTR nIDEvent)
{
    if (nIDEvent == 1)
    {
        CWnd* pControl = GetFocus();
        if (pControl != nullptr)
        {
            CWnd* pParent = pControl->GetParent();
            if (pParent != nullptr)
            {
                int iCtrlID = pParent->GetDlgCtrlID();

                if (iCtrlID != IDC_COMBO_PT_CHAIRMAN &&
                    iCtrlID != IDC_COMBO_PT_OPEN_PRAYER &&
                    iCtrlID != IDC_COMBO_WT_CLOSE_PRAYER &&
                    iCtrlID != IDC_COMBO_WEEKEND_HOST &&
                    iCtrlID != IDC_COMBO_WEEKEND_COHOST &&
                    iCtrlID != IDC_COMBO_PT_READER)
                {
                    if (m_gridAssignHist.GetRowCount() != 1)
                    {
                        m_gridAssignHist.SetRowCount(1);

                        UpdateData(TRUE);
                        m_strAssignHistLabel = _T("Assignment:");
                        UpdateData(FALSE);
                    }
                }

            }

        }

    }

    CDialog::OnTimer(nIDEvent);
}
</code>```

This seems to work fine.

Body:
I have a CDialog and I am trying to override PreTranslateMessage like this:

BOOL CWeekendMeetingDlg::PreTranslateMessage(MSG* pMsg)
{

    if (pMsg->message == WM_SETFOCUS)
    {
        if (::GetDlgCtrlID(pMsg->hwnd) != IDC_COMBO_PT_CHAIRMAN &&
            ::GetDlgCtrlID(pMsg->hwnd) != IDC_COMBO_PT_OPEN_PRAYER &&
            ::GetDlgCtrlID(pMsg->hwnd) != IDC_COMBO_WT_CLOSE_PRAYER &&
            ::GetDlgCtrlID(pMsg->hwnd) != IDC_COMBO_WEEKEND_HOST &&
            ::GetDlgCtrlID(pMsg->hwnd) != IDC_COMBO_WEEKEND_COHOST &&
            ::GetDlgCtrlID(pMsg->hwnd) != IDC_COMBO_PT_READER)
        {
            if (m_gridAssignHist.GetRowCount() != 1)
            {
                m_gridAssignHist.SetRowCount(1);
            }
        }
    }

    return CDialog::PreTranslateMessage(pMsg);
}

It is not working. What I want to do is reset my CGridCtrl row count to 1 when other controls gain focus (eg: edit combo). But if I put a breakpoint inside the first if it is never intercepted.

At the moment the only thing I can think of is to renumber all of the combo IDs on the dialog so they are sequential and then use a command range for OnSetFocus and detect in that handler. But there are also some CEdit controls.

Can't I use PTM though and avoid that? For me it seems it would be the easiest.

Here is the dialog:

At the moment I have 7 combo OnSetFocus handlers. When they fire the display specific assignment history in the grid on the right.

So if the user moves to any other control on the dialog, the assignment history control will not apply. And that was why i wanted to reset the history to just the header row. And I was hoping to do this with PTM.

Body:
I have added a 2 second timer to the dialog:

void CWeekendMeetingDlg::OnTimer(UINT_PTR nIDEvent)
{
    if (nIDEvent == 1)
    {
        CWnd* pControl = GetFocus();
        if (pControl != nullptr)
        {
            CWnd* pParent = pControl->GetParent();
            if (pParent != nullptr)
            {
                int iCtrlID = pParent->GetDlgCtrlID();

                if (iCtrlID != IDC_COMBO_PT_CHAIRMAN &&
                    iCtrlID != IDC_COMBO_PT_OPEN_PRAYER &&
                    iCtrlID != IDC_COMBO_WT_CLOSE_PRAYER &&
                    iCtrlID != IDC_COMBO_WEEKEND_HOST &&
                    iCtrlID != IDC_COMBO_WEEKEND_COHOST &&
                    iCtrlID != IDC_COMBO_PT_READER)
                {
                    if (m_gridAssignHist.GetRowCount() != 1)
                    {
                        m_gridAssignHist.SetRowCount(1);

                        UpdateData(TRUE);
                        m_strAssignHistLabel = _T("Assignment:");
                        UpdateData(FALSE);
                    }
                }

            }

        }

    }

    CDialog::OnTimer(nIDEvent);
}

This seems to work fine.


问:
Android GSON custom deserialization
I am trying to create custom deserializationg using GSON with Kotlin. Here is the sample code, but the variable test is always null. Any idea why?

```<code>    // simple class with x property
    class TestClass(var x: Int = 1)

    // simple deserializer that always returns object with value x = 4444
    class TestClassDeserializer : JsonDeserializer<TestClass> {
        override fun deserialize(json: JsonElement?, typeOfT: Type?, context: JsonDeserializationContext?): TestClass {
            return TestClass(4444)
        }
    }

    // simple gson object with register adapter
    val gson = GsonBuilder()
        .registerTypeAdapter(::TestClass.javaClass, TestClassDeserializer())
        .create()

    // init object using json string
    val json = """ {"x": 9} """.trimIndent()
    val test = gson.fromJson(json, ::TestClass.javaClass)
</code>```

Any idea why test is always null, and not the expected object TestClass(4444). I am using latest gson version 2.8.6.

答:
The problem is in line ::TestClass.javaClass
Type of this expression is KFunction1<Int, TestClass> because it is a TestClass constructor method reference.
Correct syntax is TestClass::class.java

Title: Android GSON custom deserialization

Body:
I am trying to create custom deserializationg using GSON with Kotlin. Here is the sample code, but the variable test is always null. Any idea why?

    // simple class with x property
    class TestClass(var x: Int = 1)

    // simple deserializer that always returns object with value x = 4444
    class TestClassDeserializer : JsonDeserializer<TestClass> {
        override fun deserialize(json: JsonElement?, typeOfT: Type?, context: JsonDeserializationContext?): TestClass {
            return TestClass(4444)
        }
    }

    // simple gson object with register adapter
    val gson = GsonBuilder()
        .registerTypeAdapter(::TestClass.javaClass, TestClassDeserializer())
        .create()

    // init object using json string
    val json = """ {"x": 9} """.trimIndent()
    val test = gson.fromJson(json, ::TestClass.javaClass)

Any idea why test is always null, and not the expected object TestClass(4444). I am using latest gson version 2.8.6.


问:
Group by Date and sum the count
I'm trying to figure out this issue but unable to solve it.
I have an array of objects below and what I am trying to do is to group it by date and sum the count.

var arr = [
  {CaptureTime: "2020-05-19 10:00:00.0", Division: "PC    ", Count: "1"},
{CaptureTime: "2020-06-01 18:00:00.0", Division: "CONGSO", Count: "1"},
{CaptureTime: "2020-05-14 20:00:00.0", Division: "CUA5  ", Count: "9"},
{CaptureTime: "2020-05-28 14:00:00.0", Division: "HR    ", Count: "9"},
{CaptureTime: "2020-05-19 11:00:00.0", Division: "SERK  ", Count: "20"},
{CaptureTime: "2020-05-22 17:00:00.0", Division: "WWCBS ", Count: "5"},
{CaptureTime: "2020-05-13 08:00:00.0", Division: "ECEADM", Count: "3"},
{CaptureTime: "2020-05-27 09:00:00.0", Division: "BRIDGE", Count: "1"},
{CaptureTime: "2020-05-14 09:00:00.0", Division: "WFI   ", Count: "1"},
{CaptureTime: "2020-06-01 13:00:00.0", Division: null, Count: "156"},
{CaptureTime: "2020-05-20 16:00:00.0", Division: "BRIDGE", Count: "2"},
{CaptureTime: "2020-05-22 14:00:00.0", Division: "TQM   ", Count: "9"},
{CaptureTime: "2020-05-22 17:00:00.0", Division: "PHC   ", Count: "3"}
];```
</div>
</div>
</p>
<p>The expected output would be like below:</p>
<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>var output = [
 {2020-05-13: 3},
 {2020-05-14: 10},
 {2020-05-19: 21},
 {2020-05-20: 2},
 {2020-05-22: 17},
 {2020-05-27: 1},
 {2020-05-28: 9},
 {2020-06-01: 157}
];</code>```

Thanks.

答:
You can use an object to store the dates that have already been added to the result array and their index in it. When looping over the array, check if the date is a key in the object and if it is not, add the entry to the array; otherwise, increment the count of the entry already in the array.

var arr = [
  {CaptureTime: "2020-05-19 10:00:00.0", Division: "PC    ", Count: "1"},
{CaptureTime: "2020-06-01 18:00:00.0", Division: "CONGSO", Count: "1"},
{CaptureTime: "2020-05-14 20:00:00.0", Division: "CUA5  ", Count: "9"},
{CaptureTime: "2020-05-28 14:00:00.0", Division: "HR    ", Count: "9"},
{CaptureTime: "2020-05-19 11:00:00.0", Division: "SERK  ", Count: "20"},
{CaptureTime: "2020-05-22 17:00:00.0", Division: "WWCBS ", Count: "5"},
{CaptureTime: "2020-05-13 08:00:00.0", Division: "ECEADM", Count: "3"},
{CaptureTime: "2020-05-27 09:00:00.0", Division: "BRIDGE", Count: "1"},
{CaptureTime: "2020-05-14 09:00:00.0", Division: "WFI   ", Count: "1"},
{CaptureTime: "2020-06-01 13:00:00.0", Division: null, Count: "156"},
{CaptureTime: "2020-05-20 16:00:00.0", Division: "BRIDGE", Count: "2"},
{CaptureTime: "2020-05-22 14:00:00.0", Division: "TQM   ", Count: "9"},
{CaptureTime: "2020-05-22 17:00:00.0", Division: "PHC   ", Count: "3"}
];
const res = [];
const mapping = {};
arr.forEach(a=>{
    const date = a.CaptureTime.split(" ")[0];
    if(!(date in mapping)){
     mapping[date] = res.length;
        res.push({[date]: +a.Count})
    } else {
     res[mapping[date]][date] += +a.Count;
    }
});
console.log(res);```

Title: Group by Date and sum the count

Body:
I'm trying to figure out this issue but unable to solve it.
I have an array of objects below and what I am trying to do is to group it by date and sum the count.

var arr = [
  {CaptureTime: "2020-05-19 10:00:00.0", Division: "PC    ", Count: "1"},
{CaptureTime: "2020-06-01 18:00:00.0", Division: "CONGSO", Count: "1"},
{CaptureTime: "2020-05-14 20:00:00.0", Division: "CUA5  ", Count: "9"},
{CaptureTime: "2020-05-28 14:00:00.0", Division: "HR    ", Count: "9"},
{CaptureTime: "2020-05-19 11:00:00.0", Division: "SERK  ", Count: "20"},
{CaptureTime: "2020-05-22 17:00:00.0", Division: "WWCBS ", Count: "5"},
{CaptureTime: "2020-05-13 08:00:00.0", Division: "ECEADM", Count: "3"},
{CaptureTime: "2020-05-27 09:00:00.0", Division: "BRIDGE", Count: "1"},
{CaptureTime: "2020-05-14 09:00:00.0", Division: "WFI   ", Count: "1"},
{CaptureTime: "2020-06-01 13:00:00.0", Division: null, Count: "156"},
{CaptureTime: "2020-05-20 16:00:00.0", Division: "BRIDGE", Count: "2"},
{CaptureTime: "2020-05-22 14:00:00.0", Division: "TQM   ", Count: "9"},
{CaptureTime: "2020-05-22 17:00:00.0", Division: "PHC   ", Count: "3"}
];

The expected output would be like below:

var output = [
 {2020-05-13: 3},
 {2020-05-14: 10},
 {2020-05-19: 21},
 {2020-05-20: 2},
 {2020-05-22: 17},
 {2020-05-27: 1},
 {2020-05-28: 9},
 {2020-06-01: 157}
];

Thanks.


问:
FileNotFoundError: No such file or directory for spark-submit encountered when running pyspark commands on Heroku
Background: I built an XGBClassifier model for content-based filtering and an ALS model for collaborative filtering (for ALS, I imported from pyspark.ml) and took the weighted sum of rating predictions from both to yield the final rating predictions, which are sorted in descending order (and top 5 rows are shown for user as top 5 recommendations) for a hybrid recommendation system which was built on scraped Yelp data containing Singapore's coffee-drinking outlets - basically, I have built a hybrid recommender to recommend coffee-drinking outlets to coffee lovers in Singapore based on Yelp data.

I have built and run it successfully in a local jupyter notebook as well as in a virtual environment as a Flask app (code from jupyter notebook was copied and pasted into a flaskr.py and together with its accompanying static stylesheets and html templates, constitute the flask app).

In preparing for deployment with Heroku, I have also prepared a requirements.txt based on pip freeze command, a Procfile that contains gunicorn and the various arguments such as --timeout 1800 for instance (as my flask app took 20 mins to churn out the recommendations so I thought of lengthening the worker timeout to 20 mins (1800s)), and even copied and pasted my .bash_profile into the flaskr folder (within this flaskr folder, there is another flaskr folder containing flaskr.py, requirements.txt, Procfile, and the relevant datasets used).

In my flaskr.py, I did not use SparkContext nor spark-submit but only SparkSession and the flask app worked both in a local virtual environment and my local jupyter notebook but when I tried to deploy on Heroku with gunicorn in the Procfile, the FileNotFoundError [ErrNo2] where spark-submit is not found was raised...

I tried running heroku run .bin/pyspark(or spark-shell) -a  on Terminal with virtual environment activated and the pyspark command generated the following output:

While for the spark-shell command, only spark-submit was not found but the issue is, both files are very much present in the respective paths when I checked!

The following is the error log encountered when I click "submit" in the deployed app: coffee-recsys.herokuapp.com , where the main problem (I think) is the stuff located inside the red box...

Would really appreciate if anyone can enlighten me on how I can possibly resolve this issue as I have been researching online and permutating my google search terms for the past few days to no avail. Or should I try other search engines like bing or yahoo instead?

Any help rendered is appreciated, even if it does not result in the successful deployment of my app on heroku (eg. due to possible incompatibility issues between spark-2.4.5 and heroku)..

答: Probably you moved your Spark location, check if the $SPARK_HOME environment variable is reachable/point to the intended installation</p>