问:
Airflow/Luigi for AWS EMR automatic cluster creation and pyspark deployment
I am new to airflow automation, i dont now if it is possible to do this with apache airflow(or luigi etc) or should i just make a long bash file to do this.

I want to build dag for this

Create/clone a cluster on AWS EMR

Install python requirements

Install pyspark related libararies

Get latest code from github

Submit spark job

Terminate cluster on finish

for individual steps, i can make .sh files like below(not sure if it is good to do this or not) but dont know how to do it in airflow

1) creating a cluser with cluster.sh

```<code> aws emr create-cluster \
    --name "1-node dummy cluster" \
    --instance-type m3.xlarge \
    --release-label emr-4.1.0 \
    --instance-count 1 \
    --use-default-roles \
    --applications Name=Spark \
    --auto-terminate
</code>```

2 & 3 & 4) clone git and install requirements codesetup.sh

```<code>git clone some-repo.git
pip install -r requirements.txt
mv xyz.jar /usr/lib/spark/xyz.jar
</code>```

5) Running spark job sparkjob.sh

```<code>aws emr add-steps --cluster-id <Your EMR cluster id> --steps Type=spark,Name=TestJob,Args=[--deploy-mode,cluster,--master,yarn,--conf,spark.yarn.submit.waitAppCompletion=true,pythonjob.py,s3a://your-source-bucket/data/data.csv,s3a://your-destination-bucket/test-output/],ActionOnFailure=CONTINUE
</code>```

6) Not sure, may be this

```<code>  terminate-clusters
--cluster-ids <value> [<value>...]
</code>```

Finally this all can be executed as one .sh file. I need to know the good approach to this with airflow/luigi.

What i found:

I find this post to be close but its outdated(2016) and misses the connections and code for playbooks

https://www.agari.com/email-security-blog/automated-model-building-emr-spark-airflow/

答:
I figured out that, There can be two option to do this

1) we can make a bash script with the help of emr create-cluster and addstep and then use airflow Bashoperator to schedule it

Alternatively, there is wrapper around these two, called sparksteps

An example from their documentation

```<code>sparksteps examples/episodes.py \
  --s3-bucket $AWS_S3_BUCKET \
  --aws-region us-east-1 \
  --release-label emr-4.7.0 \
  --uploads examples/lib examples/episodes.avro \
  --submit-args="--deploy-mode client --jars /home/hadoop/lib/spark-avro_2.10-2.0.2-custom.jar" \
  --app-args="--input /home/hadoop/episodes.avro" \
  --tags Application="Spark Steps" \
  --debug
</code>```

you can make a .sh script with default option of your choice. After preparing this script you can call this from airflow bashoperator as below

```<code>create_command = "sparkstep_custom.sh "

t1 = BashOperator(
        task_id= 'create_file',
        bash_command=create_command,
        dag=dag
   )
</code>```

2) You can use airflow's own operators for aws  to do this.

EmrCreateJobFlowOperator (for launching cluster)  EmrAddStepsOperator(for submitting spark job)
 EmrStepSensor (to track when step finishes)
EmrTerminateJobFlowOperator (to terminate clluster when step finishes)

Basic example to create cluster and submit step

```<code>my_step=[

    {
        'Name': 'setup - copy files',
        'ActionOnFailure': 'CANCEL_AND_WAIT',
        'HadoopJarStep': {
            'Jar': 'command-runner.jar',
            'Args': ['aws', 's3', 'cp', S3_URI + 'test.py', '/home/hadoop/']
        }
    },
{
        'Name': 'setup - copy files 3',
        'ActionOnFailure': 'CANCEL_AND_WAIT',
        'HadoopJarStep': {
            'Jar': 'command-runner.jar',
            'Args': ['aws', 's3', 'cp', S3_URI + 'myfiledependecy.py', '/home/hadoop/']
        }
    },
 {
        'Name': 'Run Spark',
        'ActionOnFailure': 'CANCEL_AND_WAIT',
        'HadoopJarStep': {
            'Jar': 'command-runner.jar',
            'Args': ['spark-submit','--jars', "jar1.jar,jar2.jar", '--py-files','/home/hadoop/myfiledependecy.py','/home/hadoop/test.py']
        }
    }
    ]

cluster_creator = EmrCreateJobFlowOperator(
    task_id='create_job_flow2',
    job_flow_overrides=JOB_FLOW_OVERRIDES,
    aws_conn_id='aws_default',
    emr_conn_id='emr_default',
    dag=dag
)

step_adder_pre_step = EmrAddStepsOperator(
    task_id='pre_step',
    job_flow_id="{{ task_instance.xcom_pull('create_job_flow2', key='return_value') }}",
    aws_conn_id='aws_default',
    steps=my_steps,
    dag=dag
)
step_checker = EmrStepSensor(
    task_id='watch_step',
    job_flow_id="{{ task_instance.xcom_pull('create_job_flow2', key='return_value') }}",
    step_id="{{ task_instance.xcom_pull('pre_step', key='return_value')[0] }}",
    aws_conn_id='aws_default',
    dag=dag
)

cluster_remover = EmrTerminateJobFlowOperator(
    task_id='remove_cluster',
    job_flow_id="{{ task_instance.xcom_pull('create_job_flow2', key='return_value') }}",
    aws_conn_id='aws_default',
    dag=dag
)
</code>```

Also, to upload code to s3 (where i was curious to get latest code from github_ it can be done with s3, boto3 and Pythonoperator

Simple example

```<code>S3_BUCKET = 'you_bucket_name'
S3_URI = 's3://{bucket}/'.format(bucket=S3_BUCKET)
def upload_file_to_S3(filename, key, bucket_name):
    s3.Bucket(bucket_name).upload_file(filename, key)

upload_to_S3_task = PythonOperator(
    task_id='upload_to_S3',
    python_callable=upload_file_to_S3,
    op_kwargs={
        'filename': configdata['project_path']+'test.py',
        'key': 'test.py',
        'bucket_name': 'dep-buck',
    },
    dag=dag)
</code>```

Body:
I am new to airflow automation, i dont now if it is possible to do this with apache airflow(or luigi etc) or should i just make a long bash file to do this.

I want to build dag for this

Create/clone a cluster on AWS EMR

Install python requirements

Install pyspark related libararies

Get latest code from github

Submit spark job

Terminate cluster on finish

for individual steps, i can make .sh files like below(not sure if it is good to do this or not) but dont know how to do it in airflow

1) creating a cluser with cluster.sh

 aws emr create-cluster \
    --name "1-node dummy cluster" \
    --instance-type m3.xlarge \
    --release-label emr-4.1.0 \
    --instance-count 1 \
    --use-default-roles \
    --applications Name=Spark \
    --auto-terminate

2 & 3 & 4) clone git and install requirements codesetup.sh

git clone some-repo.git
pip install -r requirements.txt
mv xyz.jar /usr/lib/spark/xyz.jar

5) Running spark job sparkjob.sh

aws emr add-steps --cluster-id <Your EMR cluster id> --steps Type=spark,Name=TestJob,Args=[--deploy-mode,cluster,--master,yarn,--conf,spark.yarn.submit.waitAppCompletion=true,pythonjob.py,s3a://your-source-bucket/data/data.csv,s3a://your-destination-bucket/test-output/],ActionOnFailure=CONTINUE

6) Not sure, may be this

  terminate-clusters
--cluster-ids <value> [<value>...]

Finally this all can be executed as one .sh file. I need to know the good approach to this with airflow/luigi.

What i found:

I find this post to be close but its outdated(2016) and misses the connections and code for playbooks

https://www.agari.com/email-security-blog/automated-model-building-emr-spark-airflow/

Body:
I figured out that, There can be two option to do this

1) we can make a bash script with the help of emr create-cluster and addstep and then use airflow Bashoperator to schedule it

Alternatively, there is wrapper around these two, called sparksteps

An example from their documentation

sparksteps examples/episodes.py \
  --s3-bucket $AWS_S3_BUCKET \
  --aws-region us-east-1 \
  --release-label emr-4.7.0 \
  --uploads examples/lib examples/episodes.avro \
  --submit-args="--deploy-mode client --jars /home/hadoop/lib/spark-avro_2.10-2.0.2-custom.jar" \
  --app-args="--input /home/hadoop/episodes.avro" \
  --tags Application="Spark Steps" \
  --debug

you can make a .sh script with default option of your choice. After preparing this script you can call this from airflow bashoperator as below

create_command = "sparkstep_custom.sh "

t1 = BashOperator(
        task_id= 'create_file',
        bash_command=create_command,
        dag=dag
   )

2) You can use airflow's own operators for aws  to do this.

EmrCreateJobFlowOperator (for launching cluster)  EmrAddStepsOperator(for submitting spark job)
 EmrStepSensor (to track when step finishes)
EmrTerminateJobFlowOperator (to terminate clluster when step finishes)

Basic example to create cluster and submit step

my_step=[

    {
        'Name': 'setup - copy files',
        'ActionOnFailure': 'CANCEL_AND_WAIT',
        'HadoopJarStep': {
            'Jar': 'command-runner.jar',
            'Args': ['aws', 's3', 'cp', S3_URI + 'test.py', '/home/hadoop/']
        }
    },
{
        'Name': 'setup - copy files 3',
        'ActionOnFailure': 'CANCEL_AND_WAIT',
        'HadoopJarStep': {
            'Jar': 'command-runner.jar',
            'Args': ['aws', 's3', 'cp', S3_URI + 'myfiledependecy.py', '/home/hadoop/']
        }
    },
 {
        'Name': 'Run Spark',
        'ActionOnFailure': 'CANCEL_AND_WAIT',
        'HadoopJarStep': {
            'Jar': 'command-runner.jar',
            'Args': ['spark-submit','--jars', "jar1.jar,jar2.jar", '--py-files','/home/hadoop/myfiledependecy.py','/home/hadoop/test.py']
        }
    }
    ]

cluster_creator = EmrCreateJobFlowOperator(
    task_id='create_job_flow2',
    job_flow_overrides=JOB_FLOW_OVERRIDES,
    aws_conn_id='aws_default',
    emr_conn_id='emr_default',
    dag=dag
)

step_adder_pre_step = EmrAddStepsOperator(
    task_id='pre_step',
    job_flow_id="{{ task_instance.xcom_pull('create_job_flow2', key='return_value') }}",
    aws_conn_id='aws_default',
    steps=my_steps,
    dag=dag
)
step_checker = EmrStepSensor(
    task_id='watch_step',
    job_flow_id="{{ task_instance.xcom_pull('create_job_flow2', key='return_value') }}",
    step_id="{{ task_instance.xcom_pull('pre_step', key='return_value')[0] }}",
    aws_conn_id='aws_default',
    dag=dag
)

cluster_remover = EmrTerminateJobFlowOperator(
    task_id='remove_cluster',
    job_flow_id="{{ task_instance.xcom_pull('create_job_flow2', key='return_value') }}",
    aws_conn_id='aws_default',
    dag=dag
)

Also, to upload code to s3 (where i was curious to get latest code from github_ it can be done with s3, boto3 and Pythonoperator

Simple example

S3_BUCKET = 'you_bucket_name'
S3_URI = 's3://{bucket}/'.format(bucket=S3_BUCKET)
def upload_file_to_S3(filename, key, bucket_name):
    s3.Bucket(bucket_name).upload_file(filename, key)

upload_to_S3_task = PythonOperator(
    task_id='upload_to_S3',
    python_callable=upload_file_to_S3,
    op_kwargs={
        'filename': configdata['project_path']+'test.py',
        'key': 'test.py',
        'bucket_name': 'dep-buck',
    },
    dag=dag)


来源: stackexchange

问:
PHP Laravel 5.3 questions
I have searched for information about two issues I am trying to solve, but I have not been able to get a clear idea of how to fix it. Specially, for Laravel 5.3 a lot of things have changed and there is less information than with previous versions.

My first question is: I need to show different views depending if I am using a PC or a mobile device. How I could solve it?

My second question is: How can I modify the routes and redirects in the web.php file? For example:

If a user is an "admin", first I need to be able to get that parameter (don't know how to do it in the routes) and in the case that it is true, redirect to the adminpanel view. In other case, redirect to the home view.

In the same way, if a non-admin person try to access to the view (for example, he knows the path) of the admin panel, he should be redirect to the home screen.

Thank you so much for your help.

Have a nice day and best regards.

Jose Lara.

答: your number 1 question has to do with the blade template(view) i guess. if am correct, then blade template (view) has to do with bootstrap integration. what you are asking, bootstrap mobile view can handle that for you. goto [https://www.w3schools.com/bootstrap/tryit.asp?filename=trybs_default&stacked=h][1] you will have a better understanding of what you want.</p>

ContentLicense: CC BY-SA 3.0

Title: PHP Laravel 5.3 questions

ContentLicense: CC BY-SA 4.0


问:
How to make a scalable ListView item background colour?
I want to make a scalable ListView item background colour from green to red that scales every day by one tick. For example 365 days, from day 0 it's green and with each day it get's more red but not totaly red and finally on day 365 it get's totaly red. How can I make it happen?</p>

答:
You can use ColorDrawable to create a color for each item, according to its color, and assign it as the item view's background. To get a color between two colors with a percentage, you can use ArgbEvaluator:

```<code>int color = new ArgbEvaluator().evaluate(getAdapterPosition()/365f, Color.GREEN, Color.RED);
ColorDrawable cd = new ColorDrawable(color);
view.setBackground(cd);
</code>```

Body:
You can use ColorDrawable to create a color for each item, according to its color, and assign it as the item view's background. To get a color between two colors with a percentage, you can use ArgbEvaluator:

int color = new ArgbEvaluator().evaluate(getAdapterPosition()/365f, Color.GREEN, Color.RED);
ColorDrawable cd = new ColorDrawable(color);
view.setBackground(cd);


问:
What's the best way of parsing strings?
We've got a scenario that requires us to parse lots of e-mail (plain text), each e-mail 'type' is the result of a script being run against various platforms. Some are tab delimited, some are space delimited, some we simply don't know yet.

We'll need to support more 'formats' in the future too.

Do we go for a solution using:

Regex

Simply string searching (using string.IndexOf etc)

Lex/ Yacc

Other

The overall solution will be developed in C# 2.0 (hopefully 3.5)

答:
Your best bet is RegEx because it provides a much greater degree of flexibility than any of the other options.

While you could use IndexOf to handle somethings, you may quickly find yourself writing code that looks like:

if(s.IndexOf("search1")>-1 || s.IndexOf("search2")>-1 ||...

That can be handled in one RegEx statement. Plus, there are a lot of place like RegExLib.com where you can find folks who have shared regular expressions to solve problems.

ContentLicense: CC BY-SA 2.5

Title: What's the best way of parsing strings?

LastEditorDisplayName: Torsten Marek

OwnerDisplayName: Josef


问:
Asynchronous function call in Flex
Is it possible to call a function asynchronously in Flex? I want to parse a file at regular intervals without blocking the rest of the application, what is the recommended approach for this?</p>

答:
Actionscript doesn't support multithreading, which I think is what you are really asking about here.

While the functionality isn't inherent in actionscript (or Flex) you could set up a mock system using events and timers.

I'm a little unclear on your exact question, so I'll give two answers:

1) You want to process a file every few seconds to act on any changes.

In this case all you need to do is set up a timer to check the file periodically:

```<code>var fileTimer:Timer = new Timer(5000);
fileTimer.addEventListener(TimerEvent.TIMER, checkFile);

...

private function checkFile(event:TimerEvent):void {
  // read the file and do whatever you need to do.
}
</code>```

2) You want to parse a very large file but don't want the application to hang whilst doing it.

If you want to process the file in the background, while keeping the main application responsive then I would probably create a function that would parse several lines of the file and then send an event and return. Listen for the event and start a timer that would wait a few milliseconds before calling the function again to parse the next set of lines.

This would break up the parsing of a large file with enough down time to keep the rest of your app running smoothly. You'd have to play with the timer interval and number of lines to parse at once to strike a good balance of responsiveness and the time needed to parse the file.

Hope that makes sense!

OwnerDisplayName: Abdullah Jibaly

Title: Asynchronous function call in Flex

LastEditorDisplayName: Christophe Herreman

Body:
Actionscript doesn't support multithreading, which I think is what you are really asking about here.

While the functionality isn't inherent in actionscript (or Flex) you could set up a mock system using events and timers.

I'm a little unclear on your exact question, so I'll give two answers:

1) You want to process a file every few seconds to act on any changes.

In this case all you need to do is set up a timer to check the file periodically:

var fileTimer:Timer = new Timer(5000);
fileTimer.addEventListener(TimerEvent.TIMER, checkFile);

...

private function checkFile(event:TimerEvent):void {
  // read the file and do whatever you need to do.
}

2) You want to parse a very large file but don't want the application to hang whilst doing it.

If you want to process the file in the background, while keeping the main application responsive then I would probably create a function that would parse several lines of the file and then send an event and return. Listen for the event and start a timer that would wait a few milliseconds before calling the function again to parse the next set of lines.

This would break up the parsing of a large file with enough down time to keep the rest of your app running smoothly. You'd have to play with the timer interval and number of lines to parse at once to strike a good balance of responsiveness and the time needed to parse the file.

Hope that makes sense!

OwnerDisplayName: Matt Guest

LastEditorDisplayName: Matt Guest


问:
Python wrapper not working: swig_import_helper() error
This is related to Reading Dicom files in Dlang

I am trying Python wrapper for Imebra as shown in above post.

Python_wrapper.so is created by following command:

```<code>$ g++ -shared -o python_wrapper.so -fPIC -I../../library/include/ -I/usr/include/python3.5m -L../../build -limebra python_wrapper.cxx
</code>```

I try to load it with following simple python file:

```<code>import sys
sys.path.append('../../build')
from imebra import *            # does not work;
# import imebra                 # also does not work;
</code>```

But I get following error:

```<code>$ python3 rnImebra.py
Traceback (most recent call last):
  File "..../0-Imebra-dicom/wrappers/pythonWrapper/imebra.py", line 14, in swig_import_helper
    return importlib.import_module(mname)
  File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
ImportError: No module named '_imebra'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "rnImebra.py", line 5, in <module>
    import imebra                               #  also does not work;
  File "..../0-Imebra-dicom/wrappers/pythonWrapper/imebra.py", line 17, in <module>
    _imebra = swig_import_helper()
  File "..../0-Imebra-dicom/wrappers/pythonWrapper/imebra.py", line 16, in swig_import_helper
    return importlib.import_module('_imebra')
  File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
ImportError: No module named '_imebra'
</code>```

Where is the problem and how can it be solved?

答:
After downloading the source distribution, just install the python library as explained in the user manual.

```<code>cd imebra
python setup.py install
</code>```

or

```<code>cd imebra
python setup.py install --user
</code>```

Body:
This is related to Reading Dicom files in Dlang

I am trying Python wrapper for Imebra as shown in above post.

Python_wrapper.so is created by following command:

$ g++ -shared -o python_wrapper.so -fPIC -I../../library/include/ -I/usr/include/python3.5m -L../../build -limebra python_wrapper.cxx

I try to load it with following simple python file:

import sys
sys.path.append('../../build')
from imebra import *            # does not work;
# import imebra                 # also does not work;

But I get following error:

$ python3 rnImebra.py
Traceback (most recent call last):
  File "..../0-Imebra-dicom/wrappers/pythonWrapper/imebra.py", line 14, in swig_import_helper
    return importlib.import_module(mname)
  File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
ImportError: No module named '_imebra'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "rnImebra.py", line 5, in <module>
    import imebra                               #  also does not work;
  File "..../0-Imebra-dicom/wrappers/pythonWrapper/imebra.py", line 17, in <module>
    _imebra = swig_import_helper()
  File "..../0-Imebra-dicom/wrappers/pythonWrapper/imebra.py", line 16, in swig_import_helper
    return importlib.import_module('_imebra')
  File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
ImportError: No module named '_imebra'

Where is the problem and how can it be solved?

Body:
After downloading the source distribution, just install the python library as explained in the user manual.

cd imebra
python setup.py install

or

cd imebra
python setup.py install --user


问:
Can't Free caller component
I'm trying to free a component when i click it. So, i've written the simplest code i could imagine to achieve this: a procedure that frees it's sender. But on Delphi 7 (Tried on Delphi XE 10 and it worked with no errors) it sometimes throws an Access Violation or Abstract Error randomly. The easiest way to replicate this is to insert like 30 Buttons and assign an onclick procedure with the code below, then click them.

I've tried the two codes below, both on onclick:

```<code>procedure FreeMe(Sender: TObject);
begin
    TButton(Sender).Free;
end;
</code>```

or

```<code>procedure FreeMe(Sender: TObject);
begin
    (Sender as TButton).Free;
end;
</code>```

答:
You need to delay the freeing until after the button's OnClick event handier has fully exited.  It is important that the freeing happens when the object being freed is idle and not in the middle of processing anything.

One way to do that is to use PostMessage(), eg:

var
  MyReleaseWnd: HWND;

procedure TMyMainForm.FormCreate(Sender: TObject);
begin
  MyReleaseWnd := AllocateHWnd(MyReleaseWndProc);
end;

procedure TMyMainForm.FormDestroy(Sender: TObject);
begin
   DeallocateHWnd(MyReleaseWnd);
end;

procedure TMyMainForm.MyReleaseWndProc(var Message: TMessage);
begin
  if Message.Msg = CM_RELEASE then
    TObject(Msg.LParam).Free
  else
    Message.Result := DefWindowProc(MyReleaseWnd, Message.Msg, Message.WParam, Message.LParam);
end;

procedure DelayFreeMe(Sender: TObject);
begin
  PostMessage(MyReleaseWnd, CM_RELEASE, 0, LPARAM(Sender));
end;
```

<p>Alternatively, in 10.2 Tokyo and later, you can use <code>TThread.ForceQueue()</code> instead:</p>

<pre class="lang-pascal prettyprint-override"><code>procedure DelayFreeMe(Sender: TObject);
begin
  TThread.ForceQueue(nil, Sender.Free);
end;
</code>```

Either way, you can then do this:

procedure TSomeForm.ButtonClick(Sender: TObject);
begin
  DelayFreeMe(Sender);
end;
```

Title: Can't Free caller component

Body:
I'm trying to free a component when i click it. So, i've written the simplest code i could imagine to achieve this: a procedure that frees it's sender. But on Delphi 7 (Tried on Delphi XE 10 and it worked with no errors) it sometimes throws an Access Violation or Abstract Error randomly. The easiest way to replicate this is to insert like 30 Buttons and assign an onclick procedure with the code below, then click them.

I've tried the two codes below, both on onclick:

procedure FreeMe(Sender: TObject);
begin
    TButton(Sender).Free;
end;

or

procedure FreeMe(Sender: TObject);
begin
    (Sender as TButton).Free;
end;

Body:
You need to delay the freeing until after the button's OnClick event handier has fully exited.  It is important that the freeing happens when the object being freed is idle and not in the middle of processing anything.

One way to do that is to use PostMessage(), eg:

var
  MyReleaseWnd: HWND;

procedure TMyMainForm.FormCreate(Sender: TObject);
begin
  MyReleaseWnd := AllocateHWnd(MyReleaseWndProc);
end;

procedure TMyMainForm.FormDestroy(Sender: TObject);
begin
   DeallocateHWnd(MyReleaseWnd);
end;

procedure TMyMainForm.MyReleaseWndProc(var Message: TMessage);
begin
  if Message.Msg = CM_RELEASE then
    TObject(Msg.LParam).Free
  else
    Message.Result := DefWindowProc(MyReleaseWnd, Message.Msg, Message.WParam, Message.LParam);
end;

procedure DelayFreeMe(Sender: TObject);
begin
  PostMessage(MyReleaseWnd, CM_RELEASE, 0, LPARAM(Sender));
end;

Alternatively, in 10.2 Tokyo and later, you can use TThread.ForceQueue() instead:

procedure DelayFreeMe(Sender: TObject);
begin
  TThread.ForceQueue(nil, Sender.Free);
end;

Either way, you can then do this:

procedure TSomeForm.ButtonClick(Sender: TObject);
begin
  DelayFreeMe(Sender);
end;


问:
Error while trying to pull azure key vault from azure function - Value cannot be null. Parameter name: authKeyOrResourceToken
I want to integrate my azure functions app with the key vault. I followed the steps shown here http://blogs.adatis.co.uk/benjarvis/post/Azure-Functions-Key-Vault-Integration

But facing this error- Value cannot be null.
Parameter name: authKeyOrResourceToken

Not sure where I'm going wrong. Any help would be greatly appreciated.

I tried to add an environmental variable named 'authKeyOrResourceToken' under the application settings, proving the value with default func key. But no luck.

This is how my function.json looks like

{
      "type": "CosmosDB",
      "name": "<>",
      "databaseName": "<>",
      "collectionName": "<>",
      "connectionStringSetting": "<>",
      "sqlQuery": "SELECT * FROM c where c.recordType = {recordType}",
      "direction": "in"
    }

While I expect the result of the query, I'm facing the exception.

答:
connectionStringSetting should have, as value, the name of a setting in your Function App that has a value in the format:

@Microsoft.KeyVault(SecretUri=secret_uri_with_version)

Where secret_uri_with_version is the secret url that contains your Cosmos DB Connection String (full connection string, not just the key).


问:
Draw ontop of all views
Is there a way to draw ontop of every view (like a watermark that shows everywhere for example)? sort of how the Dock4Droid app draws a a dockbar ontop of every screen.</p>

答: Your best bet is to have a relative layout as your top level container. The very last item in your markup should be an imageview containing your watermark which you can then position on the screen anywhere you like.</p>

Title: Draw ontop of all views


问:
jquery - Bind datepicker event
I have this code:

```<code>$('.datepicker').datepicker({ format: 'yyyy-mm-dd' });
</code>```

And I am trying to bind it to input text that will appear later on.

```<code>$(".table tbody").prepend("<tr><td><input type='text' name='Date' value='' class='datepicker' /></td></tr>");
</code>```

How would I do that?

答:
When you add the new datepicker, you have to initialize it.

```<code>var $newDatePicker = $("<tr><td><input type='text' name='Date' value='' class='datepicker' /></td></tr>");
$(".table tbody").prepend($newDatePicker);
$newDatePicker.find('.datepicker').datepicker(...);
</code>```

Storing the new element in a variable will let you initialize only the new one you added, without having to try to find in the DOM which one is new, vs the ones that are already initialized.

Title: jquery - Bind datepicker event

Body:
I have this code:

$('.datepicker').datepicker({ format: 'yyyy-mm-dd' });

And I am trying to bind it to input text that will appear later on.

$(".table tbody").prepend("
<input type='text' name='Date' value='' class='datepicker' />
");

How would I do that?

Body:
When you add the new datepicker, you have to initialize it.

var $newDatePicker = $("<tr><td><input type='text' name='Date' value='' class='datepicker' /></td></tr>");
$(".table tbody").prepend($newDatePicker);
$newDatePicker.find('.datepicker').datepicker(...);

Storing the new element in a variable will let you initialize only the new one you added, without having to try to find in the DOM which one is new, vs the ones that are already initialized.


问:
Can Ambassador handle CORS requests?
I'm sorry if this is a very ignorant question but is it possible for Ambassador to truly handle CORS headers and pre-flight OPTION responses?

The docs (https://www.getambassador.io/reference/cors) seem kind of ambiguous to me, if there are just hooks to prevent requests, or if it can truly respond on behalf of the services.

Here's my situation: I've got Ambassador in front of all the http requests to some microservices.  For [reasons] we now need a separate domain to make requests into the same Ambassador.

I have an AuthService configured, and according to the docs "When you use external authorization, each incoming request is authenticated before routing to its destination, including pre-flight OPTIONS requests." Which makes perfect sense, and that's what I'm seeing.  My AuthService is configured to allow things correctly and that seems to be working.  The AuthService responds with the appropriate headers, but Ambassador seems to just ignore that and only cares if the AuthService responds with a 200 or not. (Which seems totally reasonable.)

I have this annotated on my ambassador module:

```<code>getambassador.io/config: |
  ---
  apiVersion: ambassador/v1
  kind:  Module
  name:  ambassador
  config:
    service_port: 8080
    cors:
      origins: [my domain here]
      credentials: true
</code>```

And that doesn't seem to do what I'd expect, which is handle the CORS headers and pre-flight... instead it forwards it on to the service to handle all the CORS stuff.

答:
Turns out, by specifying headers: "Content-Type" in the cors configuration, things just started to work.  Apparently that's not as optional as I thought.

So this is now my module:

```<code>getambassador.io/config: |
---
apiVersion: ambassador/v1
kind:  Module
name:  ambassador
config:
  service_port: 8080
  cors:
    origins: [my domain here]
    headers: "Content-Type"
    credentials: true
</code>```

Title: Can Ambassador handle CORS requests?

Body:
I'm sorry if this is a very ignorant question but is it possible for Ambassador to truly handle CORS headers and pre-flight OPTION responses?

The docs (https://www.getambassador.io/reference/cors) seem kind of ambiguous to me, if there are just hooks to prevent requests, or if it can truly respond on behalf of the services.

Here's my situation: I've got Ambassador in front of all the http requests to some microservices.  For [reasons] we now need a separate domain to make requests into the same Ambassador.

I have an AuthService configured, and according to the docs "When you use external authorization, each incoming request is authenticated before routing to its destination, including pre-flight OPTIONS requests." Which makes perfect sense, and that's what I'm seeing.  My AuthService is configured to allow things correctly and that seems to be working.  The AuthService responds with the appropriate headers, but Ambassador seems to just ignore that and only cares if the AuthService responds with a 200 or not. (Which seems totally reasonable.)

I have this annotated on my ambassador module:

getambassador.io/config: |
  ---
  apiVersion: ambassador/v1
  kind:  Module
  name:  ambassador
  config:
    service_port: 8080
    cors:
      origins: [my domain here]
      credentials: true

And that doesn't seem to do what I'd expect, which is handle the CORS headers and pre-flight... instead it forwards it on to the service to handle all the CORS stuff.

Body:
Turns out, by specifying headers: "Content-Type" in the cors configuration, things just started to work.  Apparently that's not as optional as I thought.

So this is now my module:

getambassador.io/config: |
---
apiVersion: ambassador/v1
kind:  Module
name:  ambassador
config:
  service_port: 8080
  cors:
    origins: [my domain here]
    headers: "Content-Type"
    credentials: true


问:
Can't get table function to work with if statement
trying to create a table function that returns a two column table that must be conditioned with an if statement.

I have two other functions that do this separate but cant get it to work when combined.

```<code>create  function dbo.PDM_GetCategorytable (@Mystring varchar(200) ,@appid int)
returns table
as -- begin

--return
begin
-- declare @MyString varchar(200);
--set @MyString= 'Fred mtf is dead'--'RE: [Encrypt]]FW: MTF Military ---
--UPDATE URGENT'
--declare @AppId int =5

declare @Dubb Table (Col varchar(200));
insert into @dubb
(Col )
values ( @Mystring);

if ((select top 1 categoryid from @dubb a
left join
(SELECT        (pass.Recordid) as categoryid , pass.appid,
pass.Priority_Identifier, pass.PrioritySortorder, pass.Category,
ps.Search_String AS SrchVar
FROM            dbo.PDM_PriorityAssignments AS pass INNER JOIN
                     dbo.PDM_Priority_Search AS ps ON pass.Recordid =
ps.PriorityAssignmentid where pass.appid=@AppId )  b on a.col like '%' +
b.SrchVar + '%'
                     order by PrioritySortorder)  is not null)--'where
appid=@AppId
begin
select top 1 categoryid,Category from @dubb a
left join
(SELECT        (pass.Recordid) as categoryid , pass.appid,
pass.Priority_Identifier, pass.PrioritySortorder, pass.Category,
ps.Search_String AS SrchVar
FROM            dbo.PDM_PriorityAssignments AS pass INNER JOIN
                         dbo.PDM_Priority_Search AS ps ON pass.Recordid =
ps.PriorityAssignmentid where pass.appid=@AppId )  b on a.col like '%' +
b.SrchVar + '%'
                         order by PrioritySortorder;
                    end
else
begin
select recordid as categoryid,Category  FROM dbo.PDM_PriorityAssignments
AS pass  where appid=@AppId and Priority_Identifier like 'Routine'
end
return
end;
</code>```

expected results will be the returning of two columns , category id, and category.

答:
seems like a duplicate for Multi statement table valued function
Multi-statement Table Valued Function vs Inline Table Valued Function

here is an example how to achieve this.

```<code>CREATE FUNCTION foo
    (@param int)
RETURNS
    @T TABLE (id int, val varchar)
As
BEGIN

    IF (@param = 1)
        insert into @t Select 1, 'a';
    else
        insert into @t Select 0, 'a';

    return ;

END
GO
</code>```

Body:
trying to create a table function that returns a two column table that must be conditioned with an if statement.

I have two other functions that do this separate but cant get it to work when combined.

create  function dbo.PDM_GetCategorytable (@Mystring varchar(200) ,@appid int)
returns table
as -- begin

--return
begin
-- declare @MyString varchar(200);
--set @MyString= 'Fred mtf is dead'--'RE: [Encrypt]]FW: MTF Military ---
--UPDATE URGENT'
--declare @AppId int =5

declare @Dubb Table (Col varchar(200));
insert into @dubb
(Col )
values ( @Mystring);

if ((select top 1 categoryid from @dubb a
left join
(SELECT        (pass.Recordid) as categoryid , pass.appid,
pass.Priority_Identifier, pass.PrioritySortorder, pass.Category,
ps.Search_String AS SrchVar
FROM            dbo.PDM_PriorityAssignments AS pass INNER JOIN
                     dbo.PDM_Priority_Search AS ps ON pass.Recordid =
ps.PriorityAssignmentid where pass.appid=@AppId )  b on a.col like '%' +
b.SrchVar + '%'
                     order by PrioritySortorder)  is not null)--'where
appid=@AppId
begin
select top 1 categoryid,Category from @dubb a
left join
(SELECT        (pass.Recordid) as categoryid , pass.appid,
pass.Priority_Identifier, pass.PrioritySortorder, pass.Category,
ps.Search_String AS SrchVar
FROM            dbo.PDM_PriorityAssignments AS pass INNER JOIN
                         dbo.PDM_Priority_Search AS ps ON pass.Recordid =
ps.PriorityAssignmentid where pass.appid=@AppId )  b on a.col like '%' +
b.SrchVar + '%'
                         order by PrioritySortorder;
                    end
else
begin
select recordid as categoryid,Category  FROM dbo.PDM_PriorityAssignments
AS pass  where appid=@AppId and Priority_Identifier like 'Routine'
end
return
end;

expected results will be the returning of two columns , category id, and category.

Body:
seems like a duplicate for Multi statement table valued function
Multi-statement Table Valued Function vs Inline Table Valued Function

here is an example how to achieve this.

CREATE FUNCTION foo
    (@param int)
RETURNS
    @T TABLE (id int, val varchar)
As
BEGIN

    IF (@param = 1)
        insert into @t Select 1, 'a';
    else
        insert into @t Select 0, 'a';

    return ;

END
GO


问:
Is there a more pythonic way to merge the maximum number of a element of two lists into one
I'm writing a packet diff tool in python using scapy to find the differences in two pcap files and then output the difference in a human readable format. The script compares each packet on an individual basis, and splits up the different layers/protocols/im-not-that-much-of-a-networking-guy-sorry to compare individually. This is where my dilemma begins, you don't know what layers are present, or how many, or if there are multiple of the same layer, or how the two packets may have completely different layers. My plan to go through the differences was to pull out the names of the layers and then smash the two lists together and use that list to know what sort of layers i should be looking through the two packets for. I'm not quite sure if this is the best way to go about it, but its all I can really think of. If you have a better Idea on how to do it, PLEASE share

tl;dr
I need to figure out how to 'merge' two lists of layer names together so I can compare two packets

I tried to write it, but I couldn't quite get what I wanted. Then I was told that it looked like c written in python and to make it more pythonic, which I completely understand.

def get_common_layers(layers_1, layers_2):  # doesn't quite work
    layers_total = []
    i = 0
    while i < len(layers_1):
        temp_count = layers_1.count(layers_1[i])
        if layers_1[i] not in layers_total:
            if layers_1[i] not in layers_2:
                layers_total.extend(layers_1[i:i + temp_count])
            elif layers_1[i] in layers_2:
                if temp_count >= layers_2.count(layers_1[i]):
                    layers_total.extend(layers_1[i:i + temp_count])
        i = i + temp_count
    i = 0
    while i < len(layers_2):
        temp_count = layers_2.count(layers_2[i])
        if layers_2[i] not in layers_total:
            if layers_2[i] not in layers_1:
                layers_total.extend(layers_2[i:i + temp_count])
            elif layers_2[i] in layers_1:
                if temp_count >= layers_1.count(layers_2[i]):
                    layers_total.extend(layers_2[i:i + temp_count])
        i = i + temp_count
    return layers_total
```

<p>This is kind of close, but its a bit off.
I'm sorry that I haven't really been able to explain what I mean, but the unittest and the desired input and output should give a bit better of a picture</p>

<p>desired input and output:</p>

<pre class="lang-py prettyprint-override"><code>layers_1 = ['Ether', 'UDP', 'DNS', 'DNSQR', 'DNSQR', 'DNSQR', 'DNSRR'],
layers_2 = ['Ether', 'TCP', 'DNS', 'DNSRR', 'DNSRR', 'DNSQR'])

layers_total = ['Ether', 'UDP', 'TCP', 'DNS', 'DNSQR', 'DNSQR', 'DNSQR', 'DNSRR', 'DNSRR']
</code>```

screenshot of the error that unittest shows:
https://imgur.com/UFi92jY.png "unittest"

screenshot of what i'm trying to actually do:
https://imgur.com/eMZNX5V.png "example_output"

(would have had pics show up but new account)

答:
Are you looking for the union of the two lists? This should work:</p>

```<code>layers_total = list(set(layers_1).union(set(layers_2)))
</code>```

Body:
I'm writing a packet diff tool in python using scapy to find the differences in two pcap files and then output the difference in a human readable format. The script compares each packet on an individual basis, and splits up the different layers/protocols/im-not-that-much-of-a-networking-guy-sorry to compare individually. This is where my dilemma begins, you don't know what layers are present, or how many, or if there are multiple of the same layer, or how the two packets may have completely different layers. My plan to go through the differences was to pull out the names of the layers and then smash the two lists together and use that list to know what sort of layers i should be looking through the two packets for. I'm not quite sure if this is the best way to go about it, but its all I can really think of. If you have a better Idea on how to do it, PLEASE share

tl;dr
I need to figure out how to 'merge' two lists of layer names together so I can compare two packets

I tried to write it, but I couldn't quite get what I wanted. Then I was told that it looked like c written in python and to make it more pythonic, which I completely understand.

def get_common_layers(layers_1, layers_2):  # doesn't quite work
    layers_total = []
    i = 0
    while i < len(layers_1):
        temp_count = layers_1.count(layers_1[i])
        if layers_1[i] not in layers_total:
            if layers_1[i] not in layers_2:
                layers_total.extend(layers_1[i:i + temp_count])
            elif layers_1[i] in layers_2:
                if temp_count >= layers_2.count(layers_1[i]):
                    layers_total.extend(layers_1[i:i + temp_count])
        i = i + temp_count
    i = 0
    while i < len(layers_2):
        temp_count = layers_2.count(layers_2[i])
        if layers_2[i] not in layers_total:
            if layers_2[i] not in layers_1:
                layers_total.extend(layers_2[i:i + temp_count])
            elif layers_2[i] in layers_1:
                if temp_count >= layers_1.count(layers_2[i]):
                    layers_total.extend(layers_2[i:i + temp_count])
        i = i + temp_count
    return layers_total

This is kind of close, but its a bit off.
I'm sorry that I haven't really been able to explain what I mean, but the unittest and the desired input and output should give a bit better of a picture

desired input and output:

layers_1 = ['Ether', 'UDP', 'DNS', 'DNSQR', 'DNSQR', 'DNSQR', 'DNSRR'],
layers_2 = ['Ether', 'TCP', 'DNS', 'DNSRR', 'DNSRR', 'DNSQR'])

layers_total = ['Ether', 'UDP', 'TCP', 'DNS', 'DNSQR', 'DNSQR', 'DNSQR', 'DNSRR', 'DNSRR']

screenshot of the error that unittest shows:
https://imgur.com/UFi92jY.png "unittest"

screenshot of what i'm trying to actually do:
https://imgur.com/eMZNX5V.png "example_output"

(would have had pics show up but new account)

Body:
Are you looking for the union of the two lists? This should work:

layers_total = list(set(layers_1).union(set(layers_2)))


问:
Pyspark aggregate a StructType column as an Array of its elements for each line
I'm trying to do something that seems pretty much straightforward but somehow cannot figure how to do it with pyspark.

I have a df with two columns (to simplify) 'id' and 'strcol', with possible duplicates ids

I want to do a df.groupBy('id') that would return for each id an array of the strcol values

simple exemple :

```<code>|--id--|--strCol--|
|   a  |  {'a':1} |
|   a  |  {'a':2} |
|   b  |  {'b':3} |
|   b  |  {'b':4} |
|------|----------|
would become
|--id--|-------aggsStr------|
|   a  |  [{'a':1},{'a':2}] |
|   b  |  [{'b':3},{'b':4}] |
|------|--------------------|
</code>```

I tried to use apply with a pandas udf but it seems to refuse to return arrays. (or maybe I didn't use it correctly)

答:
You can use collect_list from the pyspark.sql.functions module:

from pyspark.sql import functions as F
agg = df.groupby("id").agg(F.collect_list("strCol"))
```

<p>A fully functional example:</p>

<pre class="lang-py prettyprint-override"><code>import pandas as pd
from pyspark.sql import functions as F

data =  {'id': ['a', 'a', 'b', 'b'], 'strCol': [{'a':1}, {'a':2}, {'b':3}, {'b':4}]}

df_aux = pd.DataFrame(data)

# df type: DataFrame[id: string, strCol: map<string,bigint>]
df = spark.createDataFrame(df_aux)

# agg type: # DataFrame[id: string, collect_list(strCol): array<map<string,bigint>>]
agg = df.groupby("id").agg(F.collect_list("strCol"))
</code>```

Hope this helped!

Body:
I'm trying to do something that seems pretty much straightforward but somehow cannot figure how to do it with pyspark.

I have a df with two columns (to simplify) 'id' and 'strcol', with possible duplicates ids

I want to do a df.groupBy('id') that would return for each id an array of the strcol values

simple exemple :

|--id--|--strCol--|
|   a  |  {'a':1} |
|   a  |  {'a':2} |
|   b  |  {'b':3} |
|   b  |  {'b':4} |
|------|----------|
would become
|--id--|-------aggsStr------|
|   a  |  [{'a':1},{'a':2}] |
|   b  |  [{'b':3},{'b':4}] |
|------|--------------------|

I tried to use apply with a pandas udf but it seems to refuse to return arrays. (or maybe I didn't use it correctly)

Body:
You can use collect_list from the pyspark.sql.functions module:

from pyspark.sql import functions as F
agg = df.groupby("id").agg(F.collect_list("strCol"))

A fully functional example:

import pandas as pd
from pyspark.sql import functions as F

data =  {'id': ['a', 'a', 'b', 'b'], 'strCol': [{'a':1}, {'a':2}, {'b':3}, {'b':4}]}

df_aux = pd.DataFrame(data)

# df type: DataFrame[id: string, strCol: map<string,bigint>]
df = spark.createDataFrame(df_aux)

# agg type: # DataFrame[id: string, collect_list(strCol): array<map<string,bigint>>]
agg = df.groupby("id").agg(F.collect_list("strCol"))

Hope this helped!


问:
Passing a command to custom command prompt from C#
I am trying to run a command in a custom .cmd from C#. The problem is that the command is not passed to the prompt when the command prompt is ready. Here is my code:

```<code>proc1.UseShellExecute = false;
string Command = @"Depanalyzer targets /n Dev\Tools\CleanupPerfCounters.exe";
proc1.FileName = @"C:\Users\xx\Desktop\MyCustom.cmd";
proc1.RedirectStandardOutput = true;
proc1.Verb = "runas";
proc1.Arguments ="/k " +  Command;
p.Start();
string output = p.StandardOutput.ReadToEnd();
</code>```

I see command window opened but the Command not passed to it and there is no output.

答:
Your Command is passed as an argument and that argument looks like it as whitespace. To pass an argument with whitespace you need to wrap that argument in quotes.

proc1.UseShellExecute = false;
//wrap Command in quotes
string Command = @"""Depanalyzer targets /n Dev\Tools\CleanupPerfCounters.exe""";
proc1.FileName = @"C:\Users\xx\Desktop\MyCustom.cmd";
proc1.RedirectStandardOutput = true;
proc1.Verb = "runas";
//add quotes
proc1.Arguments ="/k " +  Command;
p.Start();
string output = p.StandardOutput.ReadToEnd();
```

Body:
I am trying to run a command in a custom .cmd from C#. The problem is that the command is not passed to the prompt when the command prompt is ready. Here is my code:

proc1.UseShellExecute = false;
string Command = @"Depanalyzer targets /n Dev\Tools\CleanupPerfCounters.exe";
proc1.FileName = @"C:\Users\xx\Desktop\MyCustom.cmd";
proc1.RedirectStandardOutput = true;
proc1.Verb = "runas";
proc1.Arguments ="/k " +  Command;
p.Start();
string output = p.StandardOutput.ReadToEnd();

I see command window opened but the Command not passed to it and there is no output.


问:
How can i prevent recyclerview scrolling to start when i load more data
I am trying to build a news feed using cardviews listed in a recyclerview. Problem is, when I load more data by scrolling, it is automatically scrolling to top of page. I want to know how I can prevent this.

I have overrided the onScrolled method of Recyclerview's onScrollListener to load more data. I am adding new data to an Arraylist every time with my fetchingJSON() function. Then I am calling setupRecycler() function to setup Recyclerview with my updated Arraylist.

So let's say that I loaded 4 news at beginning, then I scrolled to bottom of page and loaded 4 more news. Its just adding new data to Arraylist and kind of re-creating recyclerview. I think that's why there's not much difference in installing 8 news at first.But I don't know what to do to avoid that.

```<code>/*Here I am controlling if user has reached to bottom of page, and calling fetchingJSON() function.*/

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int visibleItemCount = lManager.getChildCount();
                int totalItemCount = lManager.getItemCount();
                int firstVisibleItemPosition = lManager.findFirstVisibleItemPosition();

                if ((visibleItemCount + firstVisibleItemPosition) >= totalItemCount
                        && firstVisibleItemPosition >= 0
                        && totalItemCount >= PAGE_SIZE && !isLoading) {

                    newsFeed.this.postPage++;
                    fetchingJSON();

                }
            }
        });
</code>```

Then in fetchingJSON() function, i am calling parseJSONData function to parse response.
I'm not doing this in onResponse because volley's asynchronous operation causes some problems

```<code>private void fetchingJSON() {
        isLoading = true;
        String url = getResources().getString(R.string.haberUrl);
        StringRequest jsonStringRequest = new StringRequest(
                Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        parseJSONData(response);
                    }

                },
                new Response.ErrorListener() {
                    @Override
                .
                .
                .
</code>```

In parseJSONData function ; I am getting the data from JSON response, setting my object's attributes with that. Than adding my object to Arraylist. Finally I am calling setupRecycler function to re-create my Recyclerview with updated data .

```<code>public void parseJSONData(String response) {

        try {
            JSONObject obj = new JSONObject(response);
            JSONArray dataArray = obj.getJSONArray("data");

            for (int i = 0; i < dataArray.length(); i++) {

                JSONObject dataobj = dataArray.getJSONObject(i);

                haberModel1.setTitle(dataobj.getString("header"));
                haberModel1.setUrl(dataobj.getString("picture"));
                haberModel1.setId(dataobj.getString("id"));
                haberModelArrayList.add(haberModel1);
                setupRecycler();
            }

        } catch (JSONException e) {
            Log.d(""+TAG,e.printStackTrace() );
        }
    }
</code>```

```<code>private void setupRecycler() {
        lManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(lManager);
        rvAdapter = new RvAdapter(this, haberModelArrayList);
        recyclerView.setAdapter(rvAdapter);
        isLoading = false;
    }
</code>```

答:
If rvAdapter is non-null, then add items to it and call notifyItemRangeInserted() instead of creating a new adapter:

```<code>       for (int i = 0; i < dataArray.length(); i++) {

            JSONObject dataobj = dataArray.getJSONObject(i);

            haberModel1.setTitle(dataobj.getString("header"));
            haberModel1.setUrl(dataobj.getString("picture"));
            haberModel1.setId(dataobj.getString("id"));
            haberModelArrayList.add(haberModel1);
            // remove line: setupRecycler();
        }
        // Add these lines
        if (rvAdapter != null){
            int itemCount = rvAdapter.getItemCount();
            // TODO: add method setItems() to your adapter
            rvAdapter.setItems(haberModelArrayList);
            rvAdapter.notifyItemRangeInserted(itemCount, dataArray.length());
        } else {
            setupRecycler();
        }
</code>```

Body:
I am trying to build a news feed using cardviews listed in a recyclerview. Problem is, when I load more data by scrolling, it is automatically scrolling to top of page. I want to know how I can prevent this.

I have overrided the onScrolled method of Recyclerview's onScrollListener to load more data. I am adding new data to an Arraylist every time with my fetchingJSON() function. Then I am calling setupRecycler() function to setup Recyclerview with my updated Arraylist.

So let's say that I loaded 4 news at beginning, then I scrolled to bottom of page and loaded 4 more news. Its just adding new data to Arraylist and kind of re-creating recyclerview. I think that's why there's not much difference in installing 8 news at first.But I don't know what to do to avoid that.

/*Here I am controlling if user has reached to bottom of page, and calling fetchingJSON() function.*/

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int visibleItemCount = lManager.getChildCount();
                int totalItemCount = lManager.getItemCount();
                int firstVisibleItemPosition = lManager.findFirstVisibleItemPosition();

                if ((visibleItemCount + firstVisibleItemPosition) >= totalItemCount
                        && firstVisibleItemPosition >= 0
                        && totalItemCount >= PAGE_SIZE && !isLoading) {

                    newsFeed.this.postPage++;
                    fetchingJSON();

                }
            }
        });

Then in fetchingJSON() function, i am calling parseJSONData function to parse response.
I'm not doing this in onResponse because volley's asynchronous operation causes some problems

private void fetchingJSON() {
        isLoading = true;
        String url = getResources().getString(R.string.haberUrl);
        StringRequest jsonStringRequest = new StringRequest(
                Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        parseJSONData(response);
                    }

                },
                new Response.ErrorListener() {
                    @Override
                .
                .
                .

In parseJSONData function ; I am getting the data from JSON response, setting my object's attributes with that. Than adding my object to Arraylist. Finally I am calling setupRecycler function to re-create my Recyclerview with updated data .

public void parseJSONData(String response) {

        try {
            JSONObject obj = new JSONObject(response);
            JSONArray dataArray = obj.getJSONArray("data");

            for (int i = 0; i < dataArray.length(); i++) {

                JSONObject dataobj = dataArray.getJSONObject(i);

                haberModel1.setTitle(dataobj.getString("header"));
                haberModel1.setUrl(dataobj.getString("picture"));
                haberModel1.setId(dataobj.getString("id"));
                haberModelArrayList.add(haberModel1);
                setupRecycler();
            }

        } catch (JSONException e) {
            Log.d(""+TAG,e.printStackTrace() );
        }
    }

private void setupRecycler() {
        lManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(lManager);
        rvAdapter = new RvAdapter(this, haberModelArrayList);
        recyclerView.setAdapter(rvAdapter);
        isLoading = false;
    }

Body:
If rvAdapter is non-null, then add items to it and call notifyItemRangeInserted() instead of creating a new adapter:

       for (int i = 0; i < dataArray.length(); i++) {

            JSONObject dataobj = dataArray.getJSONObject(i);

            haberModel1.setTitle(dataobj.getString("header"));
            haberModel1.setUrl(dataobj.getString("picture"));
            haberModel1.setId(dataobj.getString("id"));
            haberModelArrayList.add(haberModel1);
            // remove line: setupRecycler();
        }
        // Add these lines
        if (rvAdapter != null){
            int itemCount = rvAdapter.getItemCount();
            // TODO: add method setItems() to your adapter
            rvAdapter.setItems(haberModelArrayList);
            rvAdapter.notifyItemRangeInserted(itemCount, dataArray.length());
        } else {
            setupRecycler();
        }


问:
NLP - How to add more features?
I want to use a sklearn classifier to train a model to classify data entries (yes,no) using a text feature (content), a numerical feature (population) and a categorical feature (location).

The model below is using only the text data to classify each entry. The text is converted with TF-IDF into a sparse matrix before being imported into the classifier.

Is there a way to add/use also the other features? These features are not in sparse matrix format so not sure how to combine them with the text sparse matrix.

    #import libraries
    import string, re, nltk
    import pandas as pd
    from pandas import Series, DataFrame
    from nltk.corpus import stopwords
    from nltk.stem.porter import PorterStemmer
    from sklearn.feature_extraction.text import CountVectorizer
    from sklearn.feature_extraction.text import TfidfTransformer
    from sklearn.model_selection import train_test_split
    from sklearn.metrics import classification_report
    from sklearn.pipeline import Pipeline

    # read data and remove empty lines
    dataset = pd.read_csv('sample_data.txt',
                           sep='\t',
                           names=['content','location','population','target'])
                           .dropna(how='all')
                           .dropna(subset=['target'])

    df = dataset[1:]

    #reset dataframe index
    df.reset_index(inplace = True)

    #add an extra column which is the length of text
    df['length'] = df['content'].apply(len)

    #create a dataframe that contains only two columns the text and the target class
    df_cont = df.copy()
    df_cont = df_cont.drop(
        ['location','population','length'],axis = 1)

    # function that takes in a string of text, removes all punctuation, stopwords and returns a list of cleaned text

    def text_process(mess):
        # lower case for string
        mess = mess.lower()

        # check characters and removes URLs
       nourl = re.sub(r'http\S+', ' ', mess)

        # check characters and removes punctuation
        nopunc = [char for char in nourl if char not in string.punctuation]

        # join the characters again to form the string and removes numbers
        nopunc = ''.join([i for i in nopunc if not i.isdigit()])

        # remove stopwords
        return [ps.stem(word) for word in nopunc.split() if word not in set(stopwords.words('english'))]

    #split the data in train and test set and train/test the model

    cont_train, cont_test, target_train, target_test = train_test_split(df_cont['content'],df_cont['target'],test_size = 0.2,shuffle = True, random_state = 1)

    pipeline = Pipeline([('bag_of_words',CountVectorizer(analyzer=text_process)),
                         ('tfidf',TfidfTransformer()),
                         ('classifier',MultinomialNB())])

    pipeline.fit(cont_train,target_train)
    predictions = pipeline.predict(cont_test)

    print(classification_report(predictions,target_test))

```

The model is expected to return the following: accuracy, precision, recall ,f1-score

答:
I believe you need to use one-hot vectoring for the 'location' feature.
One-hot vectors for the given data would be,

London - 100

Manchester - 010

Edinburg - 001

Vector length is the number of cities you have in there. Note that each bit here would be a feature.
Categorical data is usually converted to one-hot vectors before feeding to a machine learning algorithm.

Once this is done you can concat the whole row into a 1D array and then feed that to the classifier.

Title: NLP - How to add more features?

Body:
I want to use a sklearn classifier to train a model to classify data entries (yes,no) using a text feature (content), a numerical feature (population) and a categorical feature (location).

The model below is using only the text data to classify each entry. The text is converted with TF-IDF into a sparse matrix before being imported into the classifier.

Is there a way to add/use also the other features? These features are not in sparse matrix format so not sure how to combine them with the text sparse matrix.

    #import libraries
    import string, re, nltk
    import pandas as pd
    from pandas import Series, DataFrame
    from nltk.corpus import stopwords
    from nltk.stem.porter import PorterStemmer
    from sklearn.feature_extraction.text import CountVectorizer
    from sklearn.feature_extraction.text import TfidfTransformer
    from sklearn.model_selection import train_test_split
    from sklearn.metrics import classification_report
    from sklearn.pipeline import Pipeline

    # read data and remove empty lines
    dataset = pd.read_csv('sample_data.txt',
                           sep='\t',
                           names=['content','location','population','target'])
                           .dropna(how='all')
                           .dropna(subset=['target'])

    df = dataset[1:]

    #reset dataframe index
    df.reset_index(inplace = True)

    #add an extra column which is the length of text
    df['length'] = df['content'].apply(len)

    #create a dataframe that contains only two columns the text and the target class
    df_cont = df.copy()
    df_cont = df_cont.drop(
        ['location','population','length'],axis = 1)

    # function that takes in a string of text, removes all punctuation, stopwords and returns a list of cleaned text

    def text_process(mess):
        # lower case for string
        mess = mess.lower()

        # check characters and removes URLs
       nourl = re.sub(r'http\S+', ' ', mess)

        # check characters and removes punctuation
        nopunc = [char for char in nourl if char not in string.punctuation]

        # join the characters again to form the string and removes numbers
        nopunc = ''.join([i for i in nopunc if not i.isdigit()])

        # remove stopwords
        return [ps.stem(word) for word in nopunc.split() if word not in set(stopwords.words('english'))]

    #split the data in train and test set and train/test the model

    cont_train, cont_test, target_train, target_test = train_test_split(df_cont['content'],df_cont['target'],test_size = 0.2,shuffle = True, random_state = 1)

    pipeline = Pipeline([('bag_of_words',CountVectorizer(analyzer=text_process)),
                         ('tfidf',TfidfTransformer()),
                         ('classifier',MultinomialNB())])

    pipeline.fit(cont_train,target_train)
    predictions = pipeline.predict(cont_test)

    print(classification_report(predictions,target_test))

The model is expected to return the following: accuracy, precision, recall ,f1-score


问:
Histogram fill not representing all present levels of factor in certain bins
I am using a histogram  to visualize distribution of various prices of a beverage offered at stores and applied a fill to represent the proportion of stores that are in a certain status within each price level. Some levels are not appearing in the fill, and some bins aren't present at all even though I am pretty certain they are present in the data. What made me think this problem exists is that I displayed averages of another variable over each bin, and given the value of that label, the fill I end up with shouldn't be possible.

The set up should be fairly simple; I set a geom_histogram with x = price, assigned the fill (cpm.bins), and modified the x axis scale. As mentioned previously, I added a geom_text to display the average cpm for each bin. I noticed things were wrong and messed around with the data frame.

This is a small sample of the data frame I am using, but I believe it will be enough to demonstrate the problem.

library(lemon)
library(ggplot2)

df1 <- data.table::fread(
  "id   size price         cpm.bin int.ave.cpm p.int
    420  12ounce  2.39            Good       32.50   2.4
    629  12ounce  2.78 Underperforming       18.00   2.8
    940  12ounce  2.49  Non-purchasing       22.00   2.5
    1653 12ounce  2.45            Good       22.00   2.5
    1660 12ounce  2.45            Good       22.00   2.5
    2561 20ounce  2.59 Underperforming       13.65   2.6
    2578 20ounce  2.39 Underperforming       26.02   2.4
    2580 20ounce  2.39 Underperforming       26.02   2.4
    2581 20ounce  2.39            Good       26.02   2.4
    2582 20ounce  2.39            Good       26.02   2.4
    2583 20ounce  2.39            Good       26.02   2.4
    2584 20ounce  2.39            Good       26.02   2.4
    2587 20ounce  2.49  Non-purchasing       20.05   2.5
    2589 20ounce  2.99 Underperforming       18.13   3.0
    2599 20ounce  2.49  Non-purchasing       20.05   2.5
    2600 20ounce  2.49 Underperforming       20.05   2.5
    2606 20ounce  2.59  Non-purchasing       13.65   2.6
    2607 20ounce  2.39            Good       26.02   2.4
    2609 20ounce  2.39 Underperforming       26.02   2.4
    2629 20ounce  2.49  Non-purchasing       20.05   2.5
  "
)

df2 <- data.table::fread(
  "id size price         cpm.bin int.ave.cpm p.int
  629  12ounce  2.78 Underperforming       18.00   2.8
  940  12ounce  2.49  Non-purchasing       22.00   2.5
  1653 12ounce  2.45            Good       22.00   2.5
  1660 12ounce  2.45            Good       22.00   2.5
  2561 20ounce  2.59 Underperforming       13.65   2.6
  2587 20ounce  2.49  Non-purchasing       20.05   2.5
  2589 20ounce  2.99 Underperforming       18.13   3.0
  2599 20ounce  2.49  Non-purchasing       20.05   2.5
  2600 20ounce  2.49 Underperforming       20.05   2.5
  2606 20ounce  2.59  Non-purchasing       13.65   2.6
  2629 20ounce  2.49  Non-purchasing       20.05   2.5
  2634 20ounce  2.59  Non-purchasing       13.65   2.6
  2658 20ounce  2.49 Underperforming       20.05   2.5
  2665 20ounce  2.59  Non-purchasing       13.65   2.6
  2671 20ounce  2.69  Non-purchasing       21.18   2.7
  2673 20ounce  2.69            Good       21.18   2.7
  2674 20ounce  2.69            Good       21.18   2.7
  2675 20ounce  2.69 Underperforming       21.18   2.7
  2676 20ounce  2.69            Good       21.18   2.7
  2677 20ounce  2.69            Good       21.18   2.7"
)
```

<p>when using these data frames for the following ggplot, there is a different fill for the $2.50 bin in the "12 Ounce" plot.</p>

```ggplot(df1, aes(x = price)) +
  geom_histogram(aes(fill = cpm.bin), binwidth = 0.1, position = position_fill(), stat = "bin") +
  facet_rep_wrap(~size, nrow = 3, repeat.tick.labels = TRUE, scales = "free") +
  scale_x_continuous(breaks = seq(0, 10, by = 0.1), labels = scales::dollar) +
  geom_text(aes(x = p.int, y = 0.5, label=int.ave.cpm), size=4)
```

the only difference of these subsets is the minimum possible value for p.int. For df1 the minimum is 2.4, and for df2 the minimum is 2.5.

For the $2.50 bin in the "12 Ounce" section, the fill should be 2/3 "good" (blue), and 1/3 "Non-purchasing" (red) no matter what the minimum value of p.int is. What is going on, and how do I fix this so my plots are displaying values accurately and proportionally when I am using my entire data frame?

Thanks.

答:
I would use geom_bar() instead of geom_histogram() for this. I have modified the code to show how I would make the plot. You can still use the lemon package to do all the neat labeling, etc.

# plot_1 <-
ggplot(df1) +
  geom_bar(aes(x = round(price, 1), fill = cpm.bin), width = 0.09) +
  facet_grid(~size) +
  scale_fill_manual(values = c("dodgerblue2", "coral1", "mediumseagreen")) +
  xlim(2.3, 3.1) + ylim(0, 10) +
  #theme(legend.position = "none") +
  labs(title = "df1")

# plot_2 <-
ggplot(df2) +
  geom_bar(aes(x = round(price, 1), fill = cpm.bin), width = 0.09) +
  facet_grid(~size) +
  scale_fill_manual(values = c("dodgerblue2", "coral1", "mediumseagreen")) +
  xlim(2.3, 3.1) + ylim(0, 10) +
  labs(title = "df2")

# gridExtra::grid.arrange(plot_1, plot_2, nrow = 2)
```

Body:
I am using a histogram  to visualize distribution of various prices of a beverage offered at stores and applied a fill to represent the proportion of stores that are in a certain status within each price level. Some levels are not appearing in the fill, and some bins aren't present at all even though I am pretty certain they are present in the data. What made me think this problem exists is that I displayed averages of another variable over each bin, and given the value of that label, the fill I end up with shouldn't be possible.

The set up should be fairly simple; I set a geom_histogram with x = price, assigned the fill (cpm.bins), and modified the x axis scale. As mentioned previously, I added a geom_text to display the average cpm for each bin. I noticed things were wrong and messed around with the data frame.

This is a small sample of the data frame I am using, but I believe it will be enough to demonstrate the problem.

library(lemon)
library(ggplot2)

df1 <- data.table::fread(
  "id   size price         cpm.bin int.ave.cpm p.int
    420  12ounce  2.39            Good       32.50   2.4
    629  12ounce  2.78 Underperforming       18.00   2.8
    940  12ounce  2.49  Non-purchasing       22.00   2.5
    1653 12ounce  2.45            Good       22.00   2.5
    1660 12ounce  2.45            Good       22.00   2.5
    2561 20ounce  2.59 Underperforming       13.65   2.6
    2578 20ounce  2.39 Underperforming       26.02   2.4
    2580 20ounce  2.39 Underperforming       26.02   2.4
    2581 20ounce  2.39            Good       26.02   2.4
    2582 20ounce  2.39            Good       26.02   2.4
    2583 20ounce  2.39            Good       26.02   2.4
    2584 20ounce  2.39            Good       26.02   2.4
    2587 20ounce  2.49  Non-purchasing       20.05   2.5
    2589 20ounce  2.99 Underperforming       18.13   3.0
    2599 20ounce  2.49  Non-purchasing       20.05   2.5
    2600 20ounce  2.49 Underperforming       20.05   2.5
    2606 20ounce  2.59  Non-purchasing       13.65   2.6
    2607 20ounce  2.39            Good       26.02   2.4
    2609 20ounce  2.39 Underperforming       26.02   2.4
    2629 20ounce  2.49  Non-purchasing       20.05   2.5
  "
)

df2 <- data.table::fread(
  "id size price         cpm.bin int.ave.cpm p.int
  629  12ounce  2.78 Underperforming       18.00   2.8
  940  12ounce  2.49  Non-purchasing       22.00   2.5
  1653 12ounce  2.45            Good       22.00   2.5
  1660 12ounce  2.45            Good       22.00   2.5
  2561 20ounce  2.59 Underperforming       13.65   2.6
  2587 20ounce  2.49  Non-purchasing       20.05   2.5
  2589 20ounce  2.99 Underperforming       18.13   3.0
  2599 20ounce  2.49  Non-purchasing       20.05   2.5
  2600 20ounce  2.49 Underperforming       20.05   2.5
  2606 20ounce  2.59  Non-purchasing       13.65   2.6
  2629 20ounce  2.49  Non-purchasing       20.05   2.5
  2634 20ounce  2.59  Non-purchasing       13.65   2.6
  2658 20ounce  2.49 Underperforming       20.05   2.5
  2665 20ounce  2.59  Non-purchasing       13.65   2.6
  2671 20ounce  2.69  Non-purchasing       21.18   2.7
  2673 20ounce  2.69            Good       21.18   2.7
  2674 20ounce  2.69            Good       21.18   2.7
  2675 20ounce  2.69 Underperforming       21.18   2.7
  2676 20ounce  2.69            Good       21.18   2.7
  2677 20ounce  2.69            Good       21.18   2.7"
)

when using these data frames for the following ggplot, there is a different fill for the $2.50 bin in the "12 Ounce" plot.

ggplot(df1, aes(x = price)) +
  geom_histogram(aes(fill = cpm.bin), binwidth = 0.1, position = position_fill(), stat = "bin") +
  facet_rep_wrap(~size, nrow = 3, repeat.tick.labels = TRUE, scales = "free") +
  scale_x_continuous(breaks = seq(0, 10, by = 0.1), labels = scales::dollar) +
  geom_text(aes(x = p.int, y = 0.5, label=int.ave.cpm), size=4)

the only difference of these subsets is the minimum possible value for p.int. For df1 the minimum is 2.4, and for df2 the minimum is 2.5.

For the $2.50 bin in the "12 Ounce" section, the fill should be 2/3 "good" (blue), and 1/3 "Non-purchasing" (red) no matter what the minimum value of p.int is. What is going on, and how do I fix this so my plots are displaying values accurately and proportionally when I am using my entire data frame?

Thanks.


问:
How can i do a multi-gradient background with 3 colors and a lot of curves?
I need to realize a background like this in CSS:

For now I've done something similar, but of course is totally not the same thing:

body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
}

div.divbg {
  position: absolute;
  width: 100%;
  height: 100%;
}

#layer1 {
  background: #4F598A;
  z-index: 100;
}

#layer2 {
  z-index: 110;
  /*background-image: linear-gradient(90deg, rgba(186,52,41,0) 40%, rgba(186,52,41,1) 100%);*/
  background-image: linear-gradient(90deg, rgba(186, 52, 41, 0) 43%, rgba(186, 52, 41, 1) 51%);
  /*-webkit-clip-path: polygon(100% 0, 54% 1%, 48% 12%, 45% 24%, 45% 36%, 46% 52%, 48% 71%, 53% 93%, 75% 97%, 100% 81%, 100% 53%);*/
  /*clip-path: polygon(100% 0, 54% 1%, 48% 12%, 45% 24%, 45% 36%, 46% 52%, 48% 71%, 53% 93%, 75% 97%, 100% 81%, 100% 53%);*/
  /*-webkit-clip-path: polygon(51% 0, 50% 5%, 49% 12%, 49% 21%, 50% 35%, 52% 58%, 55% 82%, 59% 100%, 80% 100%, 100% 100%, 100% 0);*/
  /*clip-path: polygon(51% 0, 50% 5%, 49% 12%, 49% 21%, 50% 35%, 52% 58%, 55% 82%, 59% 100%, 80% 100%, 100% 100%, 100% 0);*/
  /*filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#BA3429', endColorstr='#BA3429',GradientType=1 );*/
  /*box-shadow: inset 943px -164px 69px 100px #4F598A;*/
  /*background: red;*/
  opacity: 0.8;
}

#layer3 {
  z-index: 120;
  width: 100%;
  height: 50%;
  /*background: #000 !important;*/
  bottom: 0;
  content: '';
  z-index: 99999 !important;
  position: absolute;
  background-image: -webkit-linear-gradient(270deg, rgba(0, 255, 255, 0) 0%, rgba(94, 151, 73, 1) 100%);
  /*clip-path: polygon(65% 7%, 49% 76%, 2% 66%, 8% 9%, 33% 3%);*/
  /*filter: blur(20px);*/
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><div class="divbg" id="layer1"> </div>
<div class="divbg" id="layer2"> </div>
<div class="divbg" id="layer3"> </div></code>```

codepen

Then, I tried to "cut" my gradient elements with clip-path, but of course this is totally worse:

body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
}

div.divbg {
  position: absolute;
  width: 100%;
  height: 100%;
}

#layer1 {
  background: #4F598A;
  z-index: 100;
}

#layer2 {
  z-index: 110;
  /*background-image: linear-gradient(90deg, rgba(186,52,41,0) 40%, rgba(186,52,41,1) 100%);*/
  background-image: linear-gradient(90deg, rgba(186, 52, 41, 0) 43%, rgba(186, 52, 41, 1) 51%);
  /*-webkit-clip-path: polygon(100% 0, 54% 1%, 48% 12%, 45% 24%, 45% 36%, 46% 52%, 48% 71%, 53% 93%, 75% 97%, 100% 81%, 100% 53%);*/
  /*clip-path: polygon(100% 0, 54% 1%, 48% 12%, 45% 24%, 45% 36%, 46% 52%, 48% 71%, 53% 93%, 75% 97%, 100% 81%, 100% 53%);*/
  /*-webkit-clip-path: polygon(51% 0, 50% 5%, 49% 12%, 49% 21%, 50% 35%, 52% 58%, 55% 82%, 59% 100%, 80% 100%, 100% 100%, 100% 0);*/
  clip-path: polygon(51% 0, 50% 5%, 49% 12%, 49% 21%, 50% 35%, 52% 58%, 55% 82%, 59% 100%, 80% 100%, 100% 100%, 100% 0);
  /*filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#BA3429', endColorstr='#BA3429',GradientType=1 );*/
  box-shadow: inset 943px -164px 69px 100px #4F598A;
  /*background: red;*/
  opacity: 0.8;
}

#layer3 {
  z-index: 120;
  width: 100%;
  height: 50%;
  /*background: #000 !important;*/
  bottom: 0;
  content: '';
  z-index: 99999 !important;
  position: absolute;
  background-image: -webkit-linear-gradient(270deg, rgba(0, 255, 255, 0) 0%, rgba(94, 151, 73, 1) 100%);
  /*clip-path: polygon(65% 7%, 49% 76%, 2% 66%, 8% 9%, 33% 3%);*/
  /*filter: blur(20px);*/
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><div class="divbg" id="layer1"> </div>
<div class="divbg" id="layer2"> </div>
<div class="divbg" id="layer3"> </div></code>```

codepen

I don't have any other idea to doing this.

Is there anything else that can I try?

答:
Make it simple, picture.

body {
    background-image: url(https://i.imgur.com/fUgGyru.png);
    background-position: 50% 50%;
    background-repeat: no-repeat;
    background-size: 100%;
    background-attachment: fixed;
}```

Body:
I need to realize a background like this in CSS:

For now I've done something similar, but of course is totally not the same thing:

body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
}

div.divbg {
  position: absolute;
  width: 100%;
  height: 100%;
}

#layer1 {
  background: #4F598A;
  z-index: 100;
}

#layer2 {
  z-index: 110;
  /*background-image: linear-gradient(90deg, rgba(186,52,41,0) 40%, rgba(186,52,41,1) 100%);*/
  background-image: linear-gradient(90deg, rgba(186, 52, 41, 0) 43%, rgba(186, 52, 41, 1) 51%);
  /*-webkit-clip-path: polygon(100% 0, 54% 1%, 48% 12%, 45% 24%, 45% 36%, 46% 52%, 48% 71%, 53% 93%, 75% 97%, 100% 81%, 100% 53%);*/
  /*clip-path: polygon(100% 0, 54% 1%, 48% 12%, 45% 24%, 45% 36%, 46% 52%, 48% 71%, 53% 93%, 75% 97%, 100% 81%, 100% 53%);*/
  /*-webkit-clip-path: polygon(51% 0, 50% 5%, 49% 12%, 49% 21%, 50% 35%, 52% 58%, 55% 82%, 59% 100%, 80% 100%, 100% 100%, 100% 0);*/
  /*clip-path: polygon(51% 0, 50% 5%, 49% 12%, 49% 21%, 50% 35%, 52% 58%, 55% 82%, 59% 100%, 80% 100%, 100% 100%, 100% 0);*/
  /*filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#BA3429', endColorstr='#BA3429',GradientType=1 );*/
  /*box-shadow: inset 943px -164px 69px 100px #4F598A;*/
  /*background: red;*/
  opacity: 0.8;
}

#layer3 {
  z-index: 120;
  width: 100%;
  height: 50%;
  /*background: #000 !important;*/
  bottom: 0;
  content: '';
  z-index: 99999 !important;
  position: absolute;
  background-image: -webkit-linear-gradient(270deg, rgba(0, 255, 255, 0) 0%, rgba(94, 151, 73, 1) 100%);
  /*clip-path: polygon(65% 7%, 49% 76%, 2% 66%, 8% 9%, 33% 3%);*/
  /*filter: blur(20px);*/
}

codepen

Then, I tried to "cut" my gradient elements with clip-path, but of course this is totally worse:

body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
}

div.divbg {
  position: absolute;
  width: 100%;
  height: 100%;
}

#layer1 {
  background: #4F598A;
  z-index: 100;
}

#layer2 {
  z-index: 110;
  /*background-image: linear-gradient(90deg, rgba(186,52,41,0) 40%, rgba(186,52,41,1) 100%);*/
  background-image: linear-gradient(90deg, rgba(186, 52, 41, 0) 43%, rgba(186, 52, 41, 1) 51%);
  /*-webkit-clip-path: polygon(100% 0, 54% 1%, 48% 12%, 45% 24%, 45% 36%, 46% 52%, 48% 71%, 53% 93%, 75% 97%, 100% 81%, 100% 53%);*/
  /*clip-path: polygon(100% 0, 54% 1%, 48% 12%, 45% 24%, 45% 36%, 46% 52%, 48% 71%, 53% 93%, 75% 97%, 100% 81%, 100% 53%);*/
  /*-webkit-clip-path: polygon(51% 0, 50% 5%, 49% 12%, 49% 21%, 50% 35%, 52% 58%, 55% 82%, 59% 100%, 80% 100%, 100% 100%, 100% 0);*/
  clip-path: polygon(51% 0, 50% 5%, 49% 12%, 49% 21%, 50% 35%, 52% 58%, 55% 82%, 59% 100%, 80% 100%, 100% 100%, 100% 0);
  /*filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#BA3429', endColorstr='#BA3429',GradientType=1 );*/
  box-shadow: inset 943px -164px 69px 100px #4F598A;
  /*background: red;*/
  opacity: 0.8;
}

#layer3 {
  z-index: 120;
  width: 100%;
  height: 50%;
  /*background: #000 !important;*/
  bottom: 0;
  content: '';
  z-index: 99999 !important;
  position: absolute;
  background-image: -webkit-linear-gradient(270deg, rgba(0, 255, 255, 0) 0%, rgba(94, 151, 73, 1) 100%);
  /*clip-path: polygon(65% 7%, 49% 76%, 2% 66%, 8% 9%, 33% 3%);*/
  /*filter: blur(20px);*/
}

codepen

I don't have any other idea to doing this.

Is there anything else that can I try?


问:
How do I stop a function? ( jQuery, .hover() )
When you hover over the squares of the picture change one after another using the interval function. Can't solve 2 problems.
1. The function should be started only at the square on which the point.
2. The function should stop if the cursor has left the square.

Help me think.

$('.block').hover(function(){
    setInterval(myFuncSuper2, 3000);
  });

    // Change pic on hover
    function changePic(i) {
        setTimeout(function () {
            jQuery(".hero-cat_" + i).addClass("active");
            jQuery(".hero-cat_" + i).siblings().removeClass("active")
        }, i * 1000)
    }
    function myFuncSuper2() {
        for (let i = 0; i <= 3; i++) {
            changePic(i);
        }
    }```
<pre class="snippet-code-css lang-css prettyprint-override"><code>.block {
  width: 100px;
  height: 100px;
  border: 1px solid;
  position: relative;
  cursor: pointer;
}

.block img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  position: absolute;
  top: 0;
  display: none;
}

.block img.active {
  display: inline;
}</code>```

```

答:
What you are trying to achieve needs two things:

mouseenter and mouseleave

1.2. because mousehover will keep on adding events when you keep hovering. you need to stop that.

And this for context of current element

2.1 because you need to enable only selected element for hover, not entire .block set

$('.block').mouseenter(startMyFunc);
$('.block').mouseleave(stopMyFunc);

var myInterval;

function myFuncSuper2() {
    for (let i = 0; i <= 3; i++) {
      setTimeout(function() {
          jQuery(this).find(".hero-cat_" + i).addClass("active");
          jQuery(this).find(".hero-cat_" + i).siblings().removeClass("active")
      }.bind(this), i * 300)
    }
}

function startMyFunc() {
    myInterval = window.setInterval(myFuncSuper2.bind(this), 1000)
}

function stopMyFunc() {
    if (myInterval) {
        clearInterval(myInterval);
    }
}```
<pre class="snippet-code-css lang-css prettyprint-override"><code>.block {
  display: inline-block;
  width: 100px;
  height: 100px;
  border: 1px solid;
  position: relative;
  cursor: pointer;
}

.block img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  position: absolute;
  top: 0;
  display: none;
}

.block img.active {
  display: inline;
}</code>```

```

Body:
When you hover over the squares of the picture change one after another using the interval function. Can't solve 2 problems.
1. The function should be started only at the square on which the point.
2. The function should stop if the cursor has left the square.

Help me think.

$('.block').hover(function(){
    setInterval(myFuncSuper2, 3000);
  });

    // Change pic on hover
    function changePic(i) {
        setTimeout(function () {
            jQuery(".hero-cat_" + i).addClass("active");
            jQuery(".hero-cat_" + i).siblings().removeClass("active")
        }, i * 1000)
    }
    function myFuncSuper2() {
        for (let i = 0; i <= 3; i++) {
            changePic(i);
        }
    }

.block {
  width: 100px;
  height: 100px;
  border: 1px solid;
  position: relative;
  cursor: pointer;
}

.block img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  position: absolute;
  top: 0;
  display: none;
}

.block img.active {
  display: inline;
}

Body:
What you are trying to achieve needs two things:

mouseenter and mouseleave

1.2. because mousehover will keep on adding events when you keep hovering. you need to stop that.

And this for context of current element

2.1 because you need to enable only selected element for hover, not entire .block set

$('.block').mouseenter(startMyFunc);
$('.block').mouseleave(stopMyFunc);

var myInterval;

function myFuncSuper2() {
    for (let i = 0; i <= 3; i++) {
      setTimeout(function() {
          jQuery(this).find(".hero-cat_" + i).addClass("active");
          jQuery(this).find(".hero-cat_" + i).siblings().removeClass("active")
      }.bind(this), i * 300)
    }
}

function startMyFunc() {
    myInterval = window.setInterval(myFuncSuper2.bind(this), 1000)
}

function stopMyFunc() {
    if (myInterval) {
        clearInterval(myInterval);
    }
}

.block {
  display: inline-block;
  width: 100px;
  height: 100px;
  border: 1px solid;
  position: relative;
  cursor: pointer;
}

.block img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  position: absolute;
  top: 0;
  display: none;
}

.block img.active {
  display: inline;
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<div class="block">
    <img src="https://avatars.mds.yandex.net/get-pdb/38069/39782144-fdc6-473f-b757-b8209a2e4b31/s1200" class="hero-cat_1">
    <img src="https://avatars.mds.yandex.net/get-pdb/225396/aea85590-65df-49b9-b959-d14cefbd9d38/s1200" class="hero-cat_2">
    <img src="https://avatars.mds.yandex.net/get-pdb/34158/e223aed5-4ea8-4e85-bb69-88e207b6c16b/s1200" class="hero-cat_3">
</div>
<div class="block">
    <img src="https://avatars.mds.yandex.net/get-pdb/38069/39782144-fdc6-473f-b757-b8209a2e4b31/s1200" class="hero-cat_1">
    <img src="https://avatars.mds.yandex.net/get-pdb/225396/aea85590-65df-49b9-b959-d14cefbd9d38/s1200" class="hero-cat_2">
    <img src="https://avatars.mds.yandex.net/get-pdb/34158/e223aed5-4ea8-4e85-bb69-88e207b6c16b/s1200" class="hero-cat_3">
</div>


问:
Visual Studio 2017 - Not Able to see Publish Profiles
In my VS 2017, my publish profile is not being listed in the publish section of the VS 2017.

I can run the application with no error but I can't publish it or see and log file saying any error about when I do right click the project and click on publish. And also even if I wanted to create new profile VS just doesn't open create new profile window.

Any guess what might be causing this?

答:
I've recently encountered this problem in VS 2019 with a project whose .pubxml profiles had been working fine and have not changed for quite a while.

The problem turned out to be a corrupted .pubxml.user file that was preventing VS from loading any profiles. The .pubxml.user files exist in the same directory as the .pubxml file and deleting them solved the issue for me.


问:
How to detect flashblocker?
I'm using flash uploader and I'd like to be able to find out whether a flash blocker is present (activated) for me to able to let user know that he needs flash.

There is a native support in browsers for letting users know they don't have flash installed. But how to deal with this ?

I was searching in global Javascript object whether the flashblocker is exposing something, but I can't find anything.

Any idea how to deal with this ?

Title: How to detect flashblocker?


问:
What is the reasoning behind the Interface Segregation Principle?
The Interface Segregation Principle (ISP) says that many client specific interfaces are better than one general purpose interface. Why is this important?</p>

答:
Robert Martin's paper on the subject gives an explanation that's mentioned less often:

The backwards force applied by clients upon interfaces.

If two classes depend directly on two different methods of a third class, it increases the likelihood that changes to either of the first two classes will affect the other.

Suppose we have three classes: Red, Green, and Blue.

Red and Green both depend Blue, but each depends on a different method. That means that Red depends on one method of Blue but doesn't use the other method. Likewise, Green depends on Blue, but only uses one method, not the other.

The violation of the principle is in Red and Green because each depends on a class - Blue - but doesn't use at least one of its methods.

What problem does this potentially create?

I need to change Red, and I also change Blue to accommodate the needs of Red.

I haven't changed the specific method within Blue that Green depends on, but nonetheless, Green depends on Blue and I have changed Blue, which could still impact Green.

Therefore, my changes to Red have the potential to impact Blue because they've led me to change a class that both depend on.

That's the "backwards force." We sometimes change a class because of the needs of its clients. If that class has different clients that use it for different things, we risk impacting them.

As stated, the simple definition of the Interface Segregation Principle is:

no client should be forced to depend on methods it does not use.

Between that and the above point from Robert Martin's paper, it's apparent that many explanations of the ISP are in fact talking about other principles.

Classes or interfaces with lots of methods are undesirable, but not specifically because of the ISP. They might violate Single Responsibility. But the ISP violation is not in the big interface or big class - it's in the classes that depend on on the big interface if they don't use all of its methods. If they use all of the methods it still sounds messy, but that has nothing to do with the ISP.

Classes that implement an interface but throw exceptions for certain methods are bad, but that's not the ISP, either. The ISP is about classes that depend on interfaces, not classes that implement interfaces.

If we Google "interface segregation", most of the top results that include code samples demonstrate classes that don't fully implement interfaces, which is not the point of the ISP. Some even incorrectly restate the principle:

The Interface Segregation Principle states that clients should not be forced to implement interfaces they don't use

...but that is not the principle. The defining paper mentions such concerns as a side-effect of violating the ISP, but indicates that they are Liskov Substitution violations.

Moreover, each time a new interface is added to the base class, that interface must be
implemented (or allowed to default) in derived classes. Indeed, an associated practice is to add these interfaces to the base class as nil virtual functions rather than pure virtual functions; specifically so that derived classes are not burdened with the need to implement them. As we learned in the second article of this column, such a practice violates the Liskov Substitution Principle (LSP), leading to maintenance and reusability problems.

What's more, to say that a client should not implement methods it does not use doesn't even make sense. The clients of an interface do not implement the methods they use or do not use - they consume its methods. A client of List<E> does not implement the methods and properties of List<E>. It calls the methods and properties of List<E>.

I don't mean to pompously cite the paper as if it's holy writ or something. But if we're going to use the name of the principle described in the article (the name of the article itself) then we should also consider the actual definition and explanation contained in that article.

OwnerDisplayName: Phillip Wells

LastEditorDisplayName: Phillip Wells


问:
How to read and write DDR memory in FPGA?
I am not good at English. sorry.

I don't know if the content of the question is too abstract.

I'm going to build a Neural Network Hardware Accelerator with Artix 7 FPGA.
However, block memory is out of capacity.
So I'm going to use DDR3 memory, which is included on the arty a7 board.

I want to write the value in the block memory to DDR memory or read the value in DDR memory.

Is there a good way to read and write DDR memory on the FPGA?

答: You need to instantiate a memory controller IP from Xilinx. See https://www.xilinx.com/support/documentation/ip_documentation/ug586_7Series_MIS.pdf (to begin with).</p>


问:
While loop with promises
What would be the idiomatic way to do something like a while loop with promises. So:

do something
if the condition still stands do it again
repeat
then do something else.

```<code>dosomething.then(possilblydomoresomethings).then(finish)
</code>```

I've done it this way I was wondering if there were any better/more idomatic ways?

```<code>var q = require('q');

var index = 1;

var useless =  function(){
        var currentIndex = index;
        console.log(currentIndex)
        var deferred = q.defer();
        setTimeout(function(){
            if(currentIndex > 10)
                deferred.resolve(false);
            else deferred.resolve(true);
            },500);
        return deferred.promise;
    }

var control = function(cont){
        var deferred = q.defer();
        if(cont){
                index = index + 1;
                useless().then(control).then(function(){
                        deferred.resolve();
                    });
            }
         else deferred.resolve();
        return deferred.promise;
    }

var chain = useless().then(control).then(function(){console.log('done')});
</code>```

Output:
1
2
3
4
5
6
7
8
9
10
11
done

答:
Lots of answers here and what you are trying to achieve is not very practical. but this should work. This was implemented in an aws lambda function, with Node.js 10 it will go until function timeout. It may also consume a decent amount of memory.

```<code>exports.handler = async (event) => {
  let res = null;
  while (true) {
    try{
     res = await dopromise();
    }catch(err){
     res = err;
    }
    console.log(res);
   }//infinite will time out
  };

  function dopromise(){
   return new Promise((resolve, reject) => {
    //do some logic
    //if error reject
        //reject('failed');
    resolve('success');
  });
}
</code>```

Tested on lambda and running fine for over 5 min. But as stated by others this is not a good thing to do.

Title: While loop with promises

Body:
What would be the idiomatic way to do something like a while loop with promises. So:

do something
if the condition still stands do it again
repeat
then do something else.

dosomething.then(possilblydomoresomethings).then(finish)

I've done it this way I was wondering if there were any better/more idomatic ways?

var q = require('q');

var index = 1;

var useless =  function(){
        var currentIndex = index;
        console.log(currentIndex)
        var deferred = q.defer();
        setTimeout(function(){
            if(currentIndex > 10)
                deferred.resolve(false);
            else deferred.resolve(true);
            },500);
        return deferred.promise;
    }

var control = function(cont){
        var deferred = q.defer();
        if(cont){
                index = index + 1;
                useless().then(control).then(function(){
                        deferred.resolve();
                    });
            }
         else deferred.resolve();
        return deferred.promise;
    }

var chain = useless().then(control).then(function(){console.log('done')});

Output:
1
2
3
4
5
6
7
8
9
10
11
done

Body:
Lots of answers here and what you are trying to achieve is not very practical. but this should work. This was implemented in an aws lambda function, with Node.js 10 it will go until function timeout. It may also consume a decent amount of memory.

exports.handler = async (event) => {
  let res = null;
  while (true) {
    try{
     res = await dopromise();
    }catch(err){
     res = err;
    }
    console.log(res);
   }//infinite will time out
  };

  function dopromise(){
   return new Promise((resolve, reject) => {
    //do some logic
    //if error reject
        //reject('failed');
    resolve('success');
  });
}

Tested on lambda and running fine for over 5 min. But as stated by others this is not a good thing to do.


问:
Searching a value in a nested Map in Java8
I am trying to look through a nested map for a specific value using java8.
Example:
The map is a

```<code>Map<Integer, Map<String,String>> classStudentGrp;
</code>```

and some of the records are like

```<code>StudentId1:
StudentName:Andy
StudentAge:12
StudentAddress:xxxx

StudentId1:
StudentName:Anna
StudentAge:11
StudentAddress:yyyy
</code>```

and so on.

I am looking at something like

```<code>classStudentGrp.forEach((sid,stu)->stu.forEach((sAttr,val)->val.equals("Andy")));
</code>```

and I want to either return a boolean (true) or set a variable if 'Andy' is found. I know the traditional approach using entry keys and values, but trying to do this in lambda.

答:
You can test it with this:

```<code>boolean contains = classStudentGrp.values().stream()
        .anyMatch(m -> m.containsValue("Andy"));
</code>```

But you should use a Student object like this:

```<code>public class Student {
    private String name;
    private int age;
    // ...
}
</code>```

With a List<Student> you can use just this:

```<code>boolean contains = classStudentGrp.stream().anyMatch(s -> "Andy".equals(s.getName()))
</code>```

Body:
I am trying to look through a nested map for a specific value using java8.
Example:
The map is a

Map<Integer, Map<String,String>> classStudentGrp;

and some of the records are like

StudentId1:
StudentName:Andy
StudentAge:12
StudentAddress:xxxx

StudentId1:
StudentName:Anna
StudentAge:11
StudentAddress:yyyy

and so on.

I am looking at something like

classStudentGrp.forEach((sid,stu)->stu.forEach((sAttr,val)->val.equals("Andy")));

and I want to either return a boolean (true) or set a variable if 'Andy' is found. I know the traditional approach using entry keys and values, but trying to do this in lambda.

Body:
You can test it with this:

boolean contains = classStudentGrp.values().stream()
        .anyMatch(m -> m.containsValue("Andy"));

But you should use a Student object like this:

public class Student {
    private String name;
    private int age;
    // ...
}

With a List<Student> you can use just this:

boolean contains = classStudentGrp.stream().anyMatch(s -> "Andy".equals(s.getName()))