问:
Conditional dependent job in Azure Devops YAML pipelines
I'm building a complex pipeline in yaml and I'm trying to create a dependency between two jobs such that the latter job runs after the former, but only if the former is set to run based on a parameter. I can't seem to wrap my head around whether this is doable or not.

I have a pipeline defined like this:

```<code>parameters:
- name: doJobA
  type: boolean

stages:
  jobs:
  - job: JobA
    condition: eq('${{ parameters.doJobA }}', true)
    # ... details removed for brevity

  - job: JobB
    dependsOn: JobA
    # ... details removed for brevity
</code>```

JobB should run after JobA if parameters.doJobA is true, or immediately if parameters.doJobA is false. Simply adding the dependsOn condition causes JobB to be skipped if the JobA condition is not met which makes sense, but I'd like it to run regardless.

Is it possible to define a conditional dependsOn in this manner?

EDIT: I've run into an additional problem with this that renders the solution below unusable. I need the condition to depend upon a variable set by an earlier running PowerShell script and not based on parameters.

答:
Here is the code sample I came up with (you can see the example).  Job 2 is always run, and is run after Job 1 if Job 1 runs.

updated YAML

```<code>- job: One
  condition: eq('${{ parameters.DoJobOne }}', true)
  pool:
    vmImage: 'windows-2019'
  steps:
  - powershell: |
      throw "simulate Job One failing"
      echo "##vso[task.setvariable variable=JobOneRan;isOutput=true]true"
    name: setvarStep
  - script: |
      echo $(setvarStep.JobOneRan)
    name: echovariable

- job: Two
  condition: and(always(), eq('${{ parameters.DoJobOne }}', eq(dependencies.One.outputs['setvarStep.JobOneRan'], true)))
  dependsOn: One
  pool:
    vmImage: 'windows-2019'
  variables:
    myVariableFromJobOne: $[ dependencies.One.outputs['setvarStep.JobOneRan'] ]
  steps:
  - script: echo $(myVariableFromJobOne)
    name: echovariable
</code>```

Hope that helps.

Wes

Body:
I'm building a complex pipeline in yaml and I'm trying to create a dependency between two jobs such that the latter job runs after the former, but only if the former is set to run based on a parameter. I can't seem to wrap my head around whether this is doable or not.

I have a pipeline defined like this:

parameters:
- name: doJobA
  type: boolean

stages:
  jobs:
  - job: JobA
    condition: eq('${{ parameters.doJobA }}', true)
    # ... details removed for brevity

  - job: JobB
    dependsOn: JobA
    # ... details removed for brevity

JobB should run after JobA if parameters.doJobA is true, or immediately if parameters.doJobA is false. Simply adding the dependsOn condition causes JobB to be skipped if the JobA condition is not met which makes sense, but I'd like it to run regardless.

Is it possible to define a conditional dependsOn in this manner?

EDIT: I've run into an additional problem with this that renders the solution below unusable. I need the condition to depend upon a variable set by an earlier running PowerShell script and not based on parameters.

Body:
Here is the code sample I came up with (you can see the example).  Job 2 is always run, and is run after Job 1 if Job 1 runs.

updated YAML

- job: One
  condition: eq('${{ parameters.DoJobOne }}', true)
  pool:
    vmImage: 'windows-2019'
  steps:
  - powershell: |
      throw "simulate Job One failing"
      echo "##vso[task.setvariable variable=JobOneRan;isOutput=true]true"
    name: setvarStep
  - script: |
      echo $(setvarStep.JobOneRan)
    name: echovariable

- job: Two
  condition: and(always(), eq('${{ parameters.DoJobOne }}', eq(dependencies.One.outputs['setvarStep.JobOneRan'], true)))
  dependsOn: One
  pool:
    vmImage: 'windows-2019'
  variables:
    myVariableFromJobOne: $[ dependencies.One.outputs['setvarStep.JobOneRan'] ]
  steps:
  - script: echo $(myVariableFromJobOne)
    name: echovariable

Hope that helps.

Wes


来源: stackexchange

问:
Determine if datetime is before or after midday
I'm working in small test code to determine if a generated datetime is before or after the midday like the code below.

```<code>import random
from datetime import datetime, timedelta
from pytz import timezone

current_date = datetime.now(timezone('America/Sao_Paulo'))
new_date = current_date - timedelta(days=2)
print (new_date.strftime("%Y-%m-%d %H:%M:%S"))

while new_date <= current_date:
  new_date = new_date + timedelta(minutes=10)
  print (new_date.strftime("%Y-%m-%d %H:%M:%S"))
  if new_date < datetime.time(12):
    print("test")
</code>```

The problem is, I can't verify if the new_date is under the midday. Probably I'm doing something wrong in the if condition, right?

I would like to print some results if the new_dateis after midday and another message if it's after midday to midnight.

Any suggestions how I can solve this?

答:
Compare to new_date.hour, instead of datetime.time()</p>

```<code>import random
from datetime import datetime, timedelta
from pytz import timezone

current_date = datetime.now(timezone('America/Sao_Paulo'))
new_date = current_date - timedelta(days=2)
print (new_date.strftime("%Y-%m-%d %H:%M:%S"))

while new_date <= current_date:
  new_date = new_date + timedelta(minutes=10)
  print (new_date.strftime("%Y-%m-%d %H:%M:%S"))
  if new_date.hour < 12:
    print("before noon")
  elif new_date.hour >= 18:
    print("after 6 pm")
</code>```

ContentLicense: CC BY-SA 4.0

Body:
I'm working in small test code to determine if a generated datetime is before or after the midday like the code below.

import random
from datetime import datetime, timedelta
from pytz import timezone

current_date = datetime.now(timezone('America/Sao_Paulo'))
new_date = current_date - timedelta(days=2)
print (new_date.strftime("%Y-%m-%d %H:%M:%S"))

while new_date <= current_date:
  new_date = new_date + timedelta(minutes=10)
  print (new_date.strftime("%Y-%m-%d %H:%M:%S"))
  if new_date < datetime.time(12):
    print("test")

The problem is, I can't verify if the new_date is under the midday. Probably I'm doing something wrong in the if condition, right?

I would like to print some results if the new_dateis after midday and another message if it's after midday to midnight.

Any suggestions how I can solve this?

Body:
Compare to new_date.hour, instead of datetime.time()

import random
from datetime import datetime, timedelta
from pytz import timezone

current_date = datetime.now(timezone('America/Sao_Paulo'))
new_date = current_date - timedelta(days=2)
print (new_date.strftime("%Y-%m-%d %H:%M:%S"))

while new_date <= current_date:
  new_date = new_date + timedelta(minutes=10)
  print (new_date.strftime("%Y-%m-%d %H:%M:%S"))
  if new_date.hour < 12:
    print("before noon")
  elif new_date.hour >= 18:
    print("after 6 pm")


问:
Swap misuse absurdity?
the following is a reverse array implementation in java. I walked through it logically and I feel like it should work.

```<code>public static int[] reverseArray(int[] arr){
int temp = 0;
int[] newArr;
newArr = Arrays.copyOf(arr, arr.length);
for (int i = 0; i < arr.length - 1; i++){
temp = newArr[i];
newArr[i] = newArr[arr.length - 1 - i];
newArr[arr.length - 1 - i] = temp;
}
return newArr;
}
</code>```

I pass in an array [1, 2, 3, 4, 5]
It returns to me only the first the the last number swapped.
Returns [5, 2, 3, 4, 1]

答:
You are looping till the end of Array which causes your logic to fail.

You must loop till arr.length/2 instead of looping the whole array. Looping the whole array will cause your program to do the following -

swap elements and reverse the array in first half of loop : Now when the loop reaches the index - arr.length/2, all elements have already been swapped with each other and your Array has been reversed.

Reswapping back of all the elements starts in the second half of loop : This effectively starts putting the Array elements back into their original place. When the loop reaches the index arr.length - 1 , your Array is reversed back to the original form it was.

Solution :

Just stop the loop when you reach arr.length/2 and your program will run fine. So your for-loop in reverseArray function should be as follows :

```<code>for (int i = 0; i < arr.length/2 ; i++)
</code>```

Title: Swap misuse absurdity?

Body:
the following is a reverse array implementation in java. I walked through it logically and I feel like it should work.

public static int[] reverseArray(int[] arr){
int temp = 0;
int[] newArr;
newArr = Arrays.copyOf(arr, arr.length);
for (int i = 0; i < arr.length - 1; i++){
temp = newArr[i];
newArr[i] = newArr[arr.length - 1 - i];
newArr[arr.length - 1 - i] = temp;
}
return newArr;
}

I pass in an array [1, 2, 3, 4, 5]
It returns to me only the first the the last number swapped.
Returns [5, 2, 3, 4, 1]

Body:
You are looping till the end of Array which causes your logic to fail.

You must loop till arr.length/2 instead of looping the whole array. Looping the whole array will cause your program to do the following -

swap elements and reverse the array in first half of loop : Now when the loop reaches the index - arr.length/2, all elements have already been swapped with each other and your Array has been reversed.

Reswapping back of all the elements starts in the second half of loop : This effectively starts putting the Array elements back into their original place. When the loop reaches the index arr.length - 1 , your Array is reversed back to the original form it was.

Solution :

Just stop the loop when you reach arr.length/2 and your program will run fine. So your for-loop in reverseArray function should be as follows :

for (int i = 0; i < arr.length/2 ; i++)


问:
Why do I get a stack smashing error using MPI_Bcast and -O3 compiler flag, but everything works without -O3?
I am pretty new to MPI, so apologies if this is simple.

I have some code from a month or two ago that has been working fine, but I decided to go back and revise it.  (It was written when I was just starting out, and it's not a performance critical section.)  The code basically generates a random graph on one process and then shares the results with all other processes. An excerpt from the baby's-first-steps version follows:

```<code>unsigned int *graph;

if (commrank == 0) {
    graph = gengraph(params); //allocates graph memory in function
    if (commsize > 1) {
        for (int k=1; k<commsize; k++)
            MPI_Send(graph, n*n, MPI_UNSIGNED, k, 0, MPI_COMM_WORLD);
    }
} else {
    MPI_Status recvStatus;
    graph = malloc(sizeof(unsigned int)*n*n);
    MPI_Recv(graph, n*n, MPI_UNSIGNED, 0, 0, MPI_COMM_WORLD, &recvStatus);
}
</code>```

While obviously naive, this worked just fine for a while, before I chose to go back and do it in what I thought was the proper fashion:

```<code>if (commrank == 0) {
    graph = gengraph(params);
    MPI_Bcast(graph, n*n, MPI_UNSIGNED, 0, MPI_COMM_WORLD);
} else {
    graph = malloc(sizeof(unsigned int)*n*n);
    MPI_Bcast(graph, n*n, MPI_UNSIGNED, 0, MPI_COMM_WORLD);
}
</code>```

The problem is, I keep getting when "stack smashing" errors in the second version when I compile with -O3 optimization, though it works fine when compiled unoptimized.  Note that I have checked the graph allocation function multiple times and debugged it, and it appears to be fine. I have also debugged the second version, and it appears to work fine.  The crash occurs later when I try to free the graph memory.  (Note that this is not a double free error, and, again, it works fine in the naive implementation and has for some time.)

One final wrinkle: The first version also fails if, instead of using the recvStatus variable, I instead use MPI_STATUS_IGNORE. And, again, this only fails with -O3.

Any thoughts would be greatly appreciated.  If it's any help, I'm using mpicc on top of gcc 7.5.0, but I imagine I'm doing something stupid rather than encountering a compiler problem.

答:
I changed the mpicc compiler to Clang and used Address Sanitizer, per the suggestion of @hristo-iliev, and found an error in a subsequent MPI call (a recv with the wrong count size).  This led to the undefined behavior.  Notably, the address sanitizer pinpointed location of the error quite clearly, while valgrind only gave rather opaque indications that something was going on in MPI (as, well, it always does).

Apologies to the StackOverflow community for this, as the code above was not the culprit (not entirely surprising).  It was just some standard undefined behavior due to sloppiness.

Body:
I am pretty new to MPI, so apologies if this is simple.

I have some code from a month or two ago that has been working fine, but I decided to go back and revise it.  (It was written when I was just starting out, and it's not a performance critical section.)  The code basically generates a random graph on one process and then shares the results with all other processes. An excerpt from the baby's-first-steps version follows:

unsigned int *graph;

if (commrank == 0) {
    graph = gengraph(params); //allocates graph memory in function
    if (commsize > 1) {
        for (int k=1; k<commsize; k++)
            MPI_Send(graph, n*n, MPI_UNSIGNED, k, 0, MPI_COMM_WORLD);
    }
} else {
    MPI_Status recvStatus;
    graph = malloc(sizeof(unsigned int)*n*n);
    MPI_Recv(graph, n*n, MPI_UNSIGNED, 0, 0, MPI_COMM_WORLD, &recvStatus);
}

While obviously naive, this worked just fine for a while, before I chose to go back and do it in what I thought was the proper fashion:

if (commrank == 0) {
    graph = gengraph(params);
    MPI_Bcast(graph, n*n, MPI_UNSIGNED, 0, MPI_COMM_WORLD);
} else {
    graph = malloc(sizeof(unsigned int)*n*n);
    MPI_Bcast(graph, n*n, MPI_UNSIGNED, 0, MPI_COMM_WORLD);
}

The problem is, I keep getting when "stack smashing" errors in the second version when I compile with -O3 optimization, though it works fine when compiled unoptimized.  Note that I have checked the graph allocation function multiple times and debugged it, and it appears to be fine. I have also debugged the second version, and it appears to work fine.  The crash occurs later when I try to free the graph memory.  (Note that this is not a double free error, and, again, it works fine in the naive implementation and has for some time.)

One final wrinkle: The first version also fails if, instead of using the recvStatus variable, I instead use MPI_STATUS_IGNORE. And, again, this only fails with -O3.

Any thoughts would be greatly appreciated.  If it's any help, I'm using mpicc on top of gcc 7.5.0, but I imagine I'm doing something stupid rather than encountering a compiler problem.


问:
How to add many columns to the table at once in DolphinDB database
Is there a way to add many columns at once like this?

```<code>t=table(1..6 as id,(today()-5)..today() as tm);
update t set feature_1001..feature1030=float(),feature_2001..feature2020=float()
</code>```

I wrote the following method, which can be implemented, but it feels a bit complicated. Is there an easier way?

```<code>for(x in 1001 .. 1030) t["feature_"+x] = float()
for(x in 2001 .. 2030) t["feature_"+x] = float()
</code>```

Body:
Is there a way to add many columns at once like this?

t=table(1..6 as id,(today()-5)..today() as tm);
update t set feature_1001..feature1030=float(),feature_2001..feature2020=float()

I wrote the following method, which can be implemented, but it feels a bit complicated. Is there an easier way?

for(x in 1001 .. 1030) t["feature_"+x] = float()
for(x in 2001 .. 2030) t["feature_"+x] = float()

Body: t["feature" + string(1001 .. 1030 join 2001 .. 2030)] = loop(x->float(), 1..60)


问:
Can I put NextJS in subfolder like localhost/next?
I try to build static files from Next.js, but I want to put it in subfolder of shared host or my localhost like localhost/nextweb.

I tried to find some example, but I found only putting NextJS in root.

My next.config.js looks like

```<code>const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer')
const { ANALYZE } = process.env
module.exports = {
  webpack: function (config) {
    if (ANALYZE) {
      config.plugins.push(new BundleAnalyzerPlugin({
        analyzerMode: 'server',
        analyzerPort: 8888,
        openAnalyzer: true
      }))
    }

    return config
  },
  exportPathMap: () => ({
    "/": { page: "/" },
    "/about": { page: "/about" }
  }),
  assetPrefix: 'http://localhost/nextweb/'
}
</code>```

When I open some page, it's working, but when I click a link it shows me an network request error:

http://localhost/nextweb/_next/a5126d9c-d338-4eee-86ff-f4e6e7dbafa6/page/nextweb/about/index.js 404 not found.

but real file is contain in .../page/about/index.js not /page/nextweb/about/index.js

What should I do about this?

答:
Not natively. Not at this time (still in 2019). Here is the relevant thread:

https://github.com/vercel/next.js/issues/4998

There is an experimental feature in the works:

https://github.com/vercel/next.js/pull/9872

ContentLicense: CC BY-SA 3.0

Body:
I try to build static files from Next.js, but I want to put it in subfolder of shared host or my localhost like localhost/nextweb.

I tried to find some example, but I found only putting NextJS in root.

My next.config.js looks like

const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer')
const { ANALYZE } = process.env
module.exports = {
  webpack: function (config) {
    if (ANALYZE) {
      config.plugins.push(new BundleAnalyzerPlugin({
        analyzerMode: 'server',
        analyzerPort: 8888,
        openAnalyzer: true
      }))
    }

    return config
  },
  exportPathMap: () => ({
    "/": { page: "/" },
    "/about": { page: "/about" }
  }),
  assetPrefix: 'http://localhost/nextweb/'
}

When I open some page, it's working, but when I click a link it shows me an network request error:

http://localhost/nextweb/_next/a5126d9c-d338-4eee-86ff-f4e6e7dbafa6/page/nextweb/about/index.js 404 not found.

but real file is contain in .../page/about/index.js not /page/nextweb/about/index.js

What should I do about this?


问:
Ansible load epel and yum update
trying to add epel and then do a yum update via ansible on a amazon-linux-2 server. The URL im using based on : https://aws.amazon.com/premiumsupport/knowledge-center/ec2-enable-epel/

My ansible script is:

```<code>---
- hosts: all
  remote_user: cloud_user

  tasks:

  - name: 01 add epel
    yum_repository:
      name: epel
      description: EPEL YUM repo
      baseurl: https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
    become: yes

  - name: 02 yum update
    yum: name=* state=latest
    become: yes
</code>```

and my error is on task 02 is (task 01 has a "changed" notice):

```<code>     FAILED! => {"changed": false, "msg": "https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm/repodata/repomd.xml:
[Errno 14] HTTPS Error 404 - Not Found\nTrying other mirror.\n\n\n One of the configured repositories failed (EPEL YUM repo),\n and yum doesn't have enough cached data to continue. At this point the only\n safe thing yum can do is fail. There are a few ways to work \"fix\" this:\n\n
1. Contact the upstream for the repository and get them to fix the problem.\n\n
2. Reconfigure the baseurl/etc. for the repository, to point to a working\n        upstream. This is most often useful if you are using a newer\n        distribution release than is supported by the repository (and the\n        packages for the previous distribution release still work).\n\n
3. Run the command with the repository temporarily disabled\n            yum --disablerepo=epel ...\n\n
4. Disable the repository permanently, so yum won't use it by default. Yum\n
will then just ignore the repository until you permanently enable it\n        again or use --enablerepo for temporary usage:\n\n
yum-config-manager --disable epel\n
or\n
subscription-manager repos --disable=epel\n\n
5. Configure the failing repository to be skipped, if it is unavailable.\n        Note that yum will try to contact the repo. when it runs most commands,\n
so will have to try and fail each time (and thus. yum will be be much\n
slower). If it is a very temporary problem though, this is often a nice\n
compromise:\n\n            yum-config-manager --save --setopt=epel.skip_if_unavailable=true\n\nfailure: repodata/repomd.xml from epel:
 [Errno 256] No more mirrors to try.\nhttps://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm/repodata/repomd.xml:
[Errno 14] HTTPS Error 404 - Not Found\n", "rc": 1, "results": []}
</code>```

Any guidance,or help would be great.

答:
Thanks for all the input. Not sure if its a amazon-linux-2 thing but the only one i got working was to use a galaxy role, code is below: </p>

```<code>  roles:
    - role: geerlingguy.repo-epel
      vars:
        epel_repo_url: "https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm"
        epel_repo_gpg_key_url: "/etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7"
      become: yes
</code>```

Title: Ansible load epel and yum update

Body:
trying to add epel and then do a yum update via ansible on a amazon-linux-2 server. The URL im using based on : https://aws.amazon.com/premiumsupport/knowledge-center/ec2-enable-epel/

My ansible script is:

---
- hosts: all
  remote_user: cloud_user

  tasks:

  - name: 01 add epel
    yum_repository:
      name: epel
      description: EPEL YUM repo
      baseurl: https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
    become: yes

  - name: 02 yum update
    yum: name=* state=latest
    become: yes

and my error is on task 02 is (task 01 has a "changed" notice):

     FAILED! => {"changed": false, "msg": "https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm/repodata/repomd.xml:
[Errno 14] HTTPS Error 404 - Not Found\nTrying other mirror.\n\n\n One of the configured repositories failed (EPEL YUM repo),\n and yum doesn't have enough cached data to continue. At this point the only\n safe thing yum can do is fail. There are a few ways to work \"fix\" this:\n\n
1. Contact the upstream for the repository and get them to fix the problem.\n\n
2. Reconfigure the baseurl/etc. for the repository, to point to a working\n        upstream. This is most often useful if you are using a newer\n        distribution release than is supported by the repository (and the\n        packages for the previous distribution release still work).\n\n
3. Run the command with the repository temporarily disabled\n            yum --disablerepo=epel ...\n\n
4. Disable the repository permanently, so yum won't use it by default. Yum\n
will then just ignore the repository until you permanently enable it\n        again or use --enablerepo for temporary usage:\n\n
yum-config-manager --disable epel\n
or\n
subscription-manager repos --disable=epel\n\n
5. Configure the failing repository to be skipped, if it is unavailable.\n        Note that yum will try to contact the repo. when it runs most commands,\n
so will have to try and fail each time (and thus. yum will be be much\n
slower). If it is a very temporary problem though, this is often a nice\n
compromise:\n\n            yum-config-manager --save --setopt=epel.skip_if_unavailable=true\n\nfailure: repodata/repomd.xml from epel:
 [Errno 256] No more mirrors to try.\nhttps://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm/repodata/repomd.xml:
[Errno 14] HTTPS Error 404 - Not Found\n", "rc": 1, "results": []}

Any guidance,or help would be great.

Body:
Thanks for all the input. Not sure if its a amazon-linux-2 thing but the only one i got working was to use a galaxy role, code is below:

  roles:
    - role: geerlingguy.repo-epel
      vars:
        epel_repo_url: "https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm"
        epel_repo_gpg_key_url: "/etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7"
      become: yes


问:
Trying to convert a "org.apache.spark.sql.DataFrame" object to pandas dataframe results in error "name 'dataframe' is not defined" in Databricks
I am trying to query an SQL database via jdbc connection in databricks and store the query results as a pandas dataframe. All of the methods I can find for this online involve storing it as a type of Spark object first using Scala code and then converting this to pandas. I tried for cell 1:

```<code>%scala
val df_table1 = sqlContext.read.format("jdbc").options(Map(
    ("url" -> "jdbc:sqlserver://myserver.database.windows.net:1433;database=mydb"),
    ("dbtable" -> "(select top 10 * from myschema.table) as table"),
    ("user" -> "user"),
    ("password" -> "password123"),
    ("driver" -> "com.microsoft.sqlserver.jdbc.SQLServerDriver"))
).load()
</code>```

which results in:

```<code>df_table1: org.apache.spark.sql.DataFrame = [var1: int, var2: string ... 50 more fields]
</code>```

Great! But when I try to convert it to a pandas df in cell 2 so I can use it:

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

result_pdf = df_table1.select("*").toPandas()

print(result_pdf)
</code>```

It generates the error message:

```<code>NameError: name 'df_table1' is not defined
</code>```

How do I successfully convert this object to a pandas dataframe, or alternatively is there any way of querying the SQL database via jdbc connection using python code without needing to use Scala at all (I do not particularly like Scala syntax and would rather avoid it if at all possible)?

答:
I am assuming that your intention to to query SQL using python and if thats the case the below query will work .</p>

```<code>%python
from pyspark import SparkConf, SparkContext
from pyspark.sql import SparkSession
conf = SparkConf()
conf.setMaster("local").setAppName("My app")
sc = SparkContext.getOrCreate(conf=conf)
spark = SparkSession(sc)
database = "YourDBName"
table = "[dbo].[YourTabelName]"
user = "SqlUser"
password  = "SqlPassword"

DF1 = spark.read.format("jdbc") \
    .option("url", f"jdbc:sqlserver://YourAzureSql.database.windows.net:1433;databaseName={database};") \
    .option("dbtable", table) \
    .option("user", user) \
    .option("password", password) \
    .option("driver", "com.microsoft.sqlserver.jdbc.SQLServerDriver") \
    .load()
DF1.show()

table = "[dbo].[someOthertable]"

DF2 = spark.read.format("jdbc") \
    .option("url", f"jdbc:sqlserver://YourAzureSql.database.windows.net:1433;databaseName={database};") \
    .option("dbtable", table) \
    .option("user", user) \
    .option("password", password) \
    .option("driver", "com.microsoft.sqlserver.jdbc.SQLServerDriver") \
    .load()
DF2.show()

Finaldf = DF1.join(DF2,(DF1.Prop_0 == DF2.prop_0),how="inner").select(DF1.Prop_0,DF1.Prop_1,DF2.Address)
Finaldf.show()
</code>```

Body:
I am trying to query an SQL database via jdbc connection in databricks and store the query results as a pandas dataframe. All of the methods I can find for this online involve storing it as a type of Spark object first using Scala code and then converting this to pandas. I tried for cell 1:

%scala
val df_table1 = sqlContext.read.format("jdbc").options(Map(
    ("url" -> "jdbc:sqlserver://myserver.database.windows.net:1433;database=mydb"),
    ("dbtable" -> "(select top 10 * from myschema.table) as table"),
    ("user" -> "user"),
    ("password" -> "password123"),
    ("driver" -> "com.microsoft.sqlserver.jdbc.SQLServerDriver"))
).load()

which results in:

df_table1: org.apache.spark.sql.DataFrame = [var1: int, var2: string ... 50 more fields]

Great! But when I try to convert it to a pandas df in cell 2 so I can use it:

import numpy as np
import pandas as pd

result_pdf = df_table1.select("*").toPandas()

print(result_pdf)

It generates the error message:

NameError: name 'df_table1' is not defined

How do I successfully convert this object to a pandas dataframe, or alternatively is there any way of querying the SQL database via jdbc connection using python code without needing to use Scala at all (I do not particularly like Scala syntax and would rather avoid it if at all possible)?

Body:
I am assuming that your intention to to query SQL using python and if thats the case the below query will work .

%python
from pyspark import SparkConf, SparkContext
from pyspark.sql import SparkSession
conf = SparkConf()
conf.setMaster("local").setAppName("My app")
sc = SparkContext.getOrCreate(conf=conf)
spark = SparkSession(sc)
database = "YourDBName"
table = "[dbo].[YourTabelName]"
user = "SqlUser"
password  = "SqlPassword"

DF1 = spark.read.format("jdbc") \
    .option("url", f"jdbc:sqlserver://YourAzureSql.database.windows.net:1433;databaseName={database};") \
    .option("dbtable", table) \
    .option("user", user) \
    .option("password", password) \
    .option("driver", "com.microsoft.sqlserver.jdbc.SQLServerDriver") \
    .load()
DF1.show()

table = "[dbo].[someOthertable]"

DF2 = spark.read.format("jdbc") \
    .option("url", f"jdbc:sqlserver://YourAzureSql.database.windows.net:1433;databaseName={database};") \
    .option("dbtable", table) \
    .option("user", user) \
    .option("password", password) \
    .option("driver", "com.microsoft.sqlserver.jdbc.SQLServerDriver") \
    .load()
DF2.show()

Finaldf = DF1.join(DF2,(DF1.Prop_0 == DF2.prop_0),how="inner").select(DF1.Prop_0,DF1.Prop_1,DF2.Address)
Finaldf.show()


问:
Mongoose increment subdocument array value within another subdocument array
I have a document like this

```<code>_id:'111'
products:[
   {
    _id:'pqr'
    nums:[
      {_id:'aaa',
      quantity:50
      },
      {_id:'bbb',
       quantity:50
      }
    ]
   }
]
</code>```

The document above can be summarized like this below for easy understanding.

```<code>   _id
   products: [
      nums: [
        {}, //quantity is in this object
        {}
      ]
   ]
</code>```

I need to increment the value of quantity in nums subdocument which is in products subdocument based on its _id.

This is what I have tried so far but it doesn't work as I don't know how to catch the _id inside nums object so as to update the specific object in that sub-subdocument array.

```<code>Shop.findOneAndUpdate(
       { "_id": '111', "products._id": 'pqr'  },
          {
            "$inc": {
               "products.$[].nums.quantity": 1
            }
    }
)
</code>```

How can I achieve this?

答:
Using arrayfilters in update operation :

```<code>db.getCollection("collectionName").findOneAndUpdate(
  { _id: "111" }, // Querying against `_id`, need to convert string to `ObjectId()` or instead use `.findByIdAndUpdate()`
  { $inc: { "products.$[p].nums.$[n].quantity": 1 } },
  {
    arrayFilters: [{ "p._id": "pqr" }, { "n._id": "aaa" }] // Inputs here
  }
  // Use { new : true } Option in mongoose to return updated document
);
</code>```

Input doc :

```<code>{
    "_id" : "111",
    "products" : [
        {
            "_id" : "pqr",
            "nums" : [
                {
                    "_id" : "aaa",
                    "quantity" : 50
                },
                {
                    "_id" : "bbb",
                    "quantity" : 50
                }
            ]
        },
        {
            "_id" : "abc",
            "nums" : [
                {
                    "_id" : "aaa1",
                    "quantity" : 501
                },
                {
                    "_id" : "bbb1",
                    "quantity" : 501
                }
            ]
        }
    ]
}
</code>```

Output doc :

```<code>{
    "_id" : "111",
    "products" : [
        {
            "_id" : "pqr",
            "nums" : [
                {
                    "_id" : "aaa",
                    "quantity" : 51 // This got incremented
                },
                {
                    "_id" : "bbb",
                    "quantity" : 50
                }
            ]
        },
        {
            "_id" : "abc",
            "nums" : [
                {
                    "_id" : "aaa1",
                    "quantity" : 501
                },
                {
                    "_id" : "bbb1",
                    "quantity" : 501
                }
            ]
        }
    ]
}
</code>```

Ref : mongoose's .findByIdAndUpdate()

Body:
I have a document like this

_id:'111'
products:[
   {
    _id:'pqr'
    nums:[
      {_id:'aaa',
      quantity:50
      },
      {_id:'bbb',
       quantity:50
      }
    ]
   }
]

The document above can be summarized like this below for easy understanding.

   _id
   products: [
      nums: [
        {}, //quantity is in this object
        {}
      ]
   ]

I need to increment the value of quantity in nums subdocument which is in products subdocument based on its _id.

This is what I have tried so far but it doesn't work as I don't know how to catch the _id inside nums object so as to update the specific object in that sub-subdocument array.

Shop.findOneAndUpdate(
       { "_id": '111', "products._id": 'pqr'  },
          {
            "$inc": {
               "products.$[].nums.quantity": 1
            }
    }
)

How can I achieve this?

Body:
Using arrayfilters in update operation :

db.getCollection("collectionName").findOneAndUpdate(
  { _id: "111" }, // Querying against `_id`, need to convert string to `ObjectId()` or instead use `.findByIdAndUpdate()`
  { $inc: { "products.$[p].nums.$[n].quantity": 1 } },
  {
    arrayFilters: [{ "p._id": "pqr" }, { "n._id": "aaa" }] // Inputs here
  }
  // Use { new : true } Option in mongoose to return updated document
);

Input doc :

{
    "_id" : "111",
    "products" : [
        {
            "_id" : "pqr",
            "nums" : [
                {
                    "_id" : "aaa",
                    "quantity" : 50
                },
                {
                    "_id" : "bbb",
                    "quantity" : 50
                }
            ]
        },
        {
            "_id" : "abc",
            "nums" : [
                {
                    "_id" : "aaa1",
                    "quantity" : 501
                },
                {
                    "_id" : "bbb1",
                    "quantity" : 501
                }
            ]
        }
    ]
}

Output doc :

{
    "_id" : "111",
    "products" : [
        {
            "_id" : "pqr",
            "nums" : [
                {
                    "_id" : "aaa",
                    "quantity" : 51 // This got incremented
                },
                {
                    "_id" : "bbb",
                    "quantity" : 50
                }
            ]
        },
        {
            "_id" : "abc",
            "nums" : [
                {
                    "_id" : "aaa1",
                    "quantity" : 501
                },
                {
                    "_id" : "bbb1",
                    "quantity" : 501
                }
            ]
        }
    ]
}

Ref : mongoose's .findByIdAndUpdate()


答:
This is a gaps-and-islands problem.  I think a difference of row numbers works:</p>

```<code>select account_id, current_plan, min(date), max(date)
from (select d.*,
             row_number() over (partition by account_id order by date) as seqnum,
             row_number() over (partition by account_id, current_plan order by date) as seqnum_2
      from data
     ) d
where current_plan <> free
group by account_id, current_plan, (seqnum - seqnum_2);
</code>```

Title: SQL (Redshift) get start and end values for consecutive data in a given column

Body:
I have a table that has the subscription state of users on any given day. The data looks like this

+------------+------------+--------------+
| account_id |    date    | current_plan |
+------------+------------+--------------+
| 1          | 2019-08-01 | free         |
| 1          | 2019-08-02 | free         |
| 1          | 2019-08-03 | yearly       |
| 1          | 2019-08-04 | yearly       |
| 1          | 2019-08-05 | yearly       |
| ...        |            |              |
| 1          | 2020-08-02 | yearly       |
| 1          | 2020-08-03 | free         |
| 2          | 2019-08-01 | monthly      |
| 2          | 2019-08-02 | monthly      |
| ...        |            |              |
| 2          | 2019-08-31 | monthly      |
| 2          | 2019-09-01 | free         |
| ...        |            |              |
| 2          | 2019-11-26 | free         |
| 2          | 2019-11-27 | monthly      |
| ...        |            |              |
| 2          | 2019-12-27 | monthly      |
| 2          | 2019-12-28 | free         |
+------------+------------+--------------+

I would like to have a table that gives the start and end dats of a subscription.  It would look something like this:

+------------+------------+------------+-------------------+
| account_id | start_date |  end_date  | subscription_type |
+------------+------------+------------+-------------------+
|          1 | 2019-08-03 | 2020-08-02 | yearly            |
|          2 | 2019-08-01 | 2019-08-31 | monthly           |
|          2 | 2019-11-27 | 2019-12-27 | monthly           |
+------------+------------+------------+-------------------+

I started by doing a LAG windown function with a bunch of WHERE statements to grab the "state changes", but this makes it difficult to see when customers float in and out of subscriptions and i'm not sure this is the best method.

lag as (
    select *, LAG(tier) OVER (PARTITION BY account_id ORDER BY date ASC) AS previous_plan
            , LAG(date) OVER (PARTITION BY account_id ORDER BY date ASC) AS previous_plan_date
    from data
)
SELECT *
FROM lag
where (current_plan = 'free' and previous_plan in ('monthly', 'yearly'))

Body:
This is a gaps-and-islands problem.  I think a difference of row numbers works:

select account_id, current_plan, min(date), max(date)
from (select d.*,
             row_number() over (partition by account_id order by date) as seqnum,
             row_number() over (partition by account_id, current_plan order by date) as seqnum_2
      from data
     ) d
where current_plan <> free
group by account_id, current_plan, (seqnum - seqnum_2);


问:
Z-index not working to keep an image on top... Toggle on/off image, menu gets hidden
So I am using the input checkbox to toggle on/off the mascot logo on top of the main container div's background. The logo has to start in the top 0 left 0 just like the background but when I toggle it on to show the mascot logo the menu with the check boxes is hidden.

My z-index won't work and I am looking for any solutions to keep the menu group of checkboxes on top of all.

Thanks

body, html {
  height: 100%;
}

#map {
  background-image: url(https://image.freepik.com/free-vector/vector-illustration-mountain-landscape_1441-71.jpg);
  height: 1080px;
  width: 1080px;
  background-position: center;
  background-repeat: no-repeat;
  background-size: 1080px 1080px;
  position: relative;
  }

  #menu {
    display: block;
    width: 100px;
  }

input:checked + .hidable {
    display: none;
    position: absolute;
    z-index: 10;
}

input:not(:checked) + .showable {
    display: none;
    position: absolute;
    z-index: 10;
  }

  #mascot1 {
    position: absolute;
    top: 0px;
    left: 0px;
    z-index: 1;
  }```
<pre class="snippet-code-html lang-html prettyprint-override"><code><!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles.css">
<title>Splash Page</title>
</head>
<body>

  <div id="map">
    <div id="menu">
      <input type="checkbox" /> Mascot 1
      <div class="showable"><img id="mascot1" src="https://banner2.cleanpng.com/20180510/fkq/kisspng-tiger-mascot-5af4168e2aa482.2537194215259459981747.jpg" alt="Map Locations"></div>
      <input type="checkbox" /> Mascot 2
      <div class="showable"><img id="mascot2" src="https://banner2.cleanpng.com/20180510/fkq/kisspng-tiger-mascot-5af4168e2aa482.2537194215259459981747.jpg" alt="Map Locations"></div>
      <input type="checkbox" /> Mascot 3
      <div class="showable"><img id="mascot3" src="https://banner2.cleanpng.com/20180510/fkq/kisspng-tiger-mascot-5af4168e2aa482.2537194215259459981747.jpg" alt="Map Locations"></div>
    </div>
  </div>

</body>
</html></code>```

答:
Did you want it to be this way ?

body,
html {
  height: 100%;
}

#map {
  background-image: url(https://image.freepik.com/free-vector/vector-illustration-mountain-landscape_1441-71.jpg);
  height: 1080px;
  width: 1080px;
  background-position: center;
  background-repeat: no-repeat;
  background-size: 1080px 1080px;
  position: relative;
}

#menu {
  display: block;
  width: 100px;
}

input:checked+.hidable {
  display: none;
  position: absolute;
  z-index: 10;
}

input:not(:checked)+.showable {
  display: none;
  position: absolute;
  z-index: 10;
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><div id="map">
  <div id="menu"><input type="checkbox" />Mascot 1
    <div class="showable"><img id="mascot1" src="https://banner2.cleanpng.com/20180510/fkq/kisspng-tiger-mascot-5af4168e2aa482.2537194215259459981747.jpg" alt="Map Locations"></div><input type="checkbox" />Mascot 2
    <div class="showable"><img id="mascot2" src="https://banner2.cleanpng.com/20180510/fkq/kisspng-tiger-mascot-5af4168e2aa482.2537194215259459981747.jpg" alt="Map Locations"></div><input type="checkbox" />Mascot 3
    <div class="showable"><img id="mascot3" src="https://banner2.cleanpng.com/20180510/fkq/kisspng-tiger-mascot-5af4168e2aa482.2537194215259459981747.jpg" alt="Map Locations"></div>
  </div>
</div></code>```

Body:
So I am using the input checkbox to toggle on/off the mascot logo on top of the main container div's background. The logo has to start in the top 0 left 0 just like the background but when I toggle it on to show the mascot logo the menu with the check boxes is hidden.

My z-index won't work and I am looking for any solutions to keep the menu group of checkboxes on top of all.

Thanks

body, html {
  height: 100%;
}

#map {
  background-image: url(https://image.freepik.com/free-vector/vector-illustration-mountain-landscape_1441-71.jpg);
  height: 1080px;
  width: 1080px;
  background-position: center;
  background-repeat: no-repeat;
  background-size: 1080px 1080px;
  position: relative;
  }

  #menu {
    display: block;
    width: 100px;
  }

input:checked + .hidable {
    display: none;
    position: absolute;
    z-index: 10;
}

input:not(:checked) + .showable {
    display: none;
    position: absolute;
    z-index: 10;
  }

  #mascot1 {
    position: absolute;
    top: 0px;
    left: 0px;
    z-index: 1;
  }

<!DOCTYPE html>

<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles.css">
<title>Splash Page</title>

      <input type="checkbox" /> Mascot 1

      <input type="checkbox" /> Mascot 2

      <input type="checkbox" /> Mascot 3

Body:
Did you want it to be this way ?

body,
html {
  height: 100%;
}

#map {
  background-image: url(https://image.freepik.com/free-vector/vector-illustration-mountain-landscape_1441-71.jpg);
  height: 1080px;
  width: 1080px;
  background-position: center;
  background-repeat: no-repeat;
  background-size: 1080px 1080px;
  position: relative;
}

#menu {
  display: block;
  width: 100px;
}

input:checked+.hidable {
  display: none;
  position: absolute;
  z-index: 10;
}

input:not(:checked)+.showable {
  display: none;
  position: absolute;
  z-index: 10;
}

<div id="map">
  <div id="menu"><input type="checkbox" />Mascot 1
    <div class="showable"><img id="mascot1" src="https://banner2.cleanpng.com/20180510/fkq/kisspng-tiger-mascot-5af4168e2aa482.2537194215259459981747.jpg" alt="Map Locations"></div><input type="checkbox" />Mascot 2
    <div class="showable"><img id="mascot2" src="https://banner2.cleanpng.com/20180510/fkq/kisspng-tiger-mascot-5af4168e2aa482.2537194215259459981747.jpg" alt="Map Locations"></div><input type="checkbox" />Mascot 3
    <div class="showable"><img id="mascot3" src="https://banner2.cleanpng.com/20180510/fkq/kisspng-tiger-mascot-5af4168e2aa482.2537194215259459981747.jpg" alt="Map Locations"></div>
  </div>
</div>


问:
Most elegant way to store (persist) the value of a single (numeric) variable
I need to store the value of a counter between executions of a script, so I can trigger a specific subroutine every hundred counts. I know I can write my integer into a text file and re-read it, and I know I can pickle my variable to much the same effect (the script currently uses the latter approach).

The concern I have is to find the approach that makes the code as elegant - simple and easily comprehensible - as possible, particularly to a non-technical audience.

The pickle module name is not helpful in that regard - once you grok the metaphor, it is entirely memorable, but if you don't know it, a comment (or verbal explanation) is required to explain that it is a module for serialising Python objects to disk.

Although 'nice to have', I'm not particularly concerned about the atomicity of the storage operation, nor about strongly protecting the stored value from loss [although I do need it to persist through a server reboot].

My interest is elegance of code to access and update the value of my variable, not the elegance of code to initialise whatever is storing the value (so, if the value were to be stored in a file, I'm interested in the code to read and write to a file that already exists and holds a value, not in creating the file on first execution).

What is the most elegant (and/or most pythonic) way to store a single (numeric) value so it persists between executions of a script?

答:
The pickle approach alluded to in my question is:

```<code>import pickle
with open('counter', 'rb') as f:
    value = pickle.load(f)
# Perform increments to the counter 'value' here
with open('counter', 'wb') as f:
    pickle.dump(value, f)
</code>```

The loss of elegance here is the need to know what the pickle module does.

Body:
The pickle approach alluded to in my question is:

import pickle
with open('counter', 'rb') as f:
    value = pickle.load(f)
# Perform increments to the counter 'value' here
with open('counter', 'wb') as f:
    pickle.dump(value, f)

The loss of elegance here is the need to know what the pickle module does.


问:
Dynamically cloned form element attributes with JQuery revert on server validation
I've been trying to wrap my head around this issue, I cloned a form using jQuery and set the id of the form elements to increment with the clone count. The issue is that after server validation with errors (using Laravel), the id of the elements revert to the initial id - without the count, even the dynamically added class attribute status-off.</p>

```<code> $("#container .container:last").clone().eq(0).clone().find(":input").each(function () {
        var newId = this.id + containerCount;

        this.id = newId;
        $(this).addClass('status-off');
        $(this).val("");
    }).end()
    .show().insertAfter(".container:last");
</code>```

答:
Silly me, turns out I am iterating the old form components with @foreach (old('input') ?? ['once'])) as $origin.
Funny  how I expected the DOM to retain dynamically created attributes.

Body:
I've been trying to wrap my head around this issue, I cloned a form using jQuery and set the id of the form elements to increment with the clone count. The issue is that after server validation with errors (using Laravel), the id of the elements revert to the initial id - without the count, even the dynamically added class attribute status-off.

 $("#container .container:last").clone().eq(0).clone().find(":input").each(function () {
        var newId = this.id + containerCount;

        this.id = newId;
        $(this).addClass('status-off');
        $(this).val("");
    }).end()
    .show().insertAfter(".container:last");


问:
How to get an API key for CyberArk?
It is my understanding that it is possible to use an API key, stored in a (reasonably well protected) local file, to access a CyberArk account programmatically.  (The goal here is to avoid storing passwords in a configuration file, for example, and having to update such configuration when the password changes, which happens regularly.)

I am familiar with the API key concept, and have used it for many other online services.  In those other cases, it was always straightforward to find instructions on how to obtain an API key.  I am having no such luck for CyberArk.

Can someone please point me to instructions for obtaining a CyberArk API key?

答:
In general, all official documentation can be found at docs.cyberark.com. You are looking for [Core Privileged Access Security] - [Developer] part. In [Authentication] you find the REST API calls for the different authentication types. If I understand your question correctly, you want to receive an authentication string which may be stored in a file for future API calls. Unfortunatelly, this is not possible as of now. There is an enhancement request for it, but I would not expect this option to be implemented any time soon - if ever.

What is available are several authentication methods which return an authentication string with a maximum valid period of 60 minutes if you use it permanently. If the token is not used for a certain period, around 10-15 minutes, the token also invalidates.

Title: How to get an API key for CyberArk?


问:
Concept for a function returning a templated entity
I've been experimenting with adding concepts to a constexpr json parser and am struggling to define the proper Parser concept. My first attempt:

using parse_input_t = std::string_view;
template <typename T>
using parse_result_t = std::optional<std::pair<T, std::string_view>>;

// A parser for type `T` is a function: parse_input_t -> parse_result_t<T>
template <typename F, typename T>
concept Parser = std::is_invocable_r_v<parse_result_t<T>, F, parse_input_t>;
```

<p>The problem with this is that I want to write functions with the signature:</p>

<pre class="lang-cpp prettyprint-override"><code>template <Parser P>
auto func(P p);
</code>```

That is, I don't want T in the interface.

I can accomplish what I want with something kind of ugly:

template <typename F>
concept Parser = requires(F f, parse_input_t i)
{
    requires requires(typename decltype(f(i))::value_type result)
    {
        { f(i) } -> std::same_as<parse_result_t<decltype(result.first)>>;
    };
};
```

<p>Is there a cleaner way to do this?  I was hoping for something like:</p>

<pre class="lang-cpp prettyprint-override"><code>template <typename F>
concept Parser = requires(F f, parse_input_t i)
{
    { f(i) } -> std::same_as<parse_result_t<auto>>;
};
</code>```

答:
Sure, you can do this.

Define a type trait to pull out the T from std::optional<std::pair<T, std::string_view>>:

```<code>template <typename T>
struct parser_type;
template <typename T>
struct parser_type<std::optional<std::pair<T, std::string_view>>> {
    using type = T;
};
template <typename T>
using parser_type_t = typename parser_type<T>::type;
</code>```

Define a type trait around that that attempts to pull out the T from when you invoke F:

```<code>template <typename F>
using parser_result = parser_type_t<std::invoke_result_t<F, std::string_view>>;
</code>```

and build a concept around that:

```<code>template <typename F>
concept Parser = requires {
    typename parser_result<F>;
};
</code>```

You can then use parser_result<F> as the associated type of the parser. For example:

struct Ints {
    auto operator()(std::string_view) -> std::optional<std::pair<int, std::string_view>>;
};

static_assert(Parser<Ints>);
static_assert(std::same_as<int, parser_result<Ints>>);
```

Body:
I've been experimenting with adding concepts to a constexpr json parser and am struggling to define the proper Parser concept. My first attempt:

using parse_input_t = std::string_view;
template <typename T>
using parse_result_t = std::optional<std::pair<T, std::string_view>>;

// A parser for type `T` is a function: parse_input_t -> parse_result_t<T>
template <typename F, typename T>
concept Parser = std::is_invocable_r_v<parse_result_t<T>, F, parse_input_t>;

The problem with this is that I want to write functions with the signature:

template <Parser P>
auto func(P p);

That is, I don't want T in the interface.

I can accomplish what I want with something kind of ugly:

template <typename F>
concept Parser = requires(F f, parse_input_t i)
{
    requires requires(typename decltype(f(i))::value_type result)
    {
        { f(i) } -> std::same_as<parse_result_t<decltype(result.first)>>;
    };
};

Is there a cleaner way to do this?  I was hoping for something like:

template <typename F>
concept Parser = requires(F f, parse_input_t i)
{
    { f(i) } -> std::same_as<parse_result_t<auto>>;
};

Body:
Sure, you can do this.

Define a type trait to pull out the T from std::optional<std::pair<T, std::string_view>>:

template <typename T>
struct parser_type;
template <typename T>
struct parser_type<std::optional<std::pair<T, std::string_view>>> {
    using type = T;
};
template <typename T>
using parser_type_t = typename parser_type<T>::type;

Define a type trait around that that attempts to pull out the T from when you invoke F:

template <typename F>
using parser_result = parser_type_t<std::invoke_result_t<F, std::string_view>>;

and build a concept around that:

template <typename F>
concept Parser = requires {
    typename parser_result<F>;
};

You can then use parser_result<F> as the associated type of the parser. For example:

struct Ints {
    auto operator()(std::string_view) -> std::optional<std::pair<int, std::string_view>>;
};

static_assert(Parser<Ints>);
static_assert(std::same_as<int, parser_result<Ints>>);


问:
Flask template inheritance with CSS styles
I've got a base.html file, which all other pages inherit from. Base links to main.css for style class definitions. However, so far this effect isn't being realized.

Base.html

<!DOCTYPE html>

    <meta charset="utf-8">
    {%block head%}
    {%endblock%}

    <link rel="stylesheet" href="{{url_for('static',filename='css/main.css')}}"/>

    {% if title %}
    <title> {{title}} </title>
    {%endif%}

Task Master

    {%block body%}
    {%endblock%}

```

<p>index.html</p>

<pre class="lang-html prettyprint-override"><code><!DOCTYPE html>
{%extends "base.html"%}
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
  </head>
    <body>

    {%block body%}
    <div class="content">
      <table>
        <tr>
            <th>Task</th>
            <th>Added</th>
            <th>Actions</th>
        </tr>
        {%for task in tasks%}
        <tr>
            <td>{{task.content}}</td>
            <td>{{task.date_created.date()}}</td>
            <td>
              Delete
              <br>
              Update
            </td>
        </tr>
        {%endfor%}
      </table>
      <form action="/" method="POST">
        <input type="text" name="content" id="content"/>
        <input type="submit" value="Add Task"/>
      </form>
    </div>
    {%endblock%}

  </body>
</html>
</code>```

main.css

.h1{
  display: block;
  font-size: 2em;
  margin-top: 0.67em;
  margin-bottom: 0.67em;
  margin-left: 0;
  margin-right: 0;
  font-weight: bold;
}
```

<p>And lastly, app.py</p>

<pre class="lang-py prettyprint-override"><code>from flask import Flask, render_template, url_for,request,redirect
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
db = SQLAlchemy(app)

class Todo(db.Model):
    id = db.Column(db.Integer,primary_key=True)
    content = db.Column(db.String(200),nullable=False)
    date_created = db.Column(db.DateTime,default=datetime.utcnow)

    def __repr__(self):
        return f'<Task {self.id}>'

@app.route('/',methods=['POST','GET'])
def index():
    if request.method == "POST":
        task_content = request.form['content']
        new_task = Todo(content=task_content)
        try:
            db.session.add(new_task)
            db.session.commit()
            return redirect('/')
        except:
            return "Failure to add content."
    else:
        tasks = Todo.query.order_by(Todo.date_created).all()
        return render_template('index.html',title='index',tasks=tasks)

@app.route('/delete/<int:id>')
def delete(id):
    task_to_delete = Todo.query.get_or_404(id)
    try:
        db.session.delete(task_to_delete)
        db.session.commit()
        return redirect('/')
    except:
        return 'Task cannot be deleted'

@app.route('/update/<int:id>',methods=['GET','POST'])
def update(id):
    task = Todo.query.get_or_404(id)
    if request.method == "POST":
        task.content = request.form['content']
        try:
            db.session.commit()
            return redirect('/')
        except:
            return 'Cannot update task'
    else:
        return render_template('update.html',task=task)

if __name__ == "__main__":
    app.run(debug=True)

</code>```

What I'd like is for anything in an
 tag in base.html to be formatted according to the h1 definition on main.css. What do I need to do to achieve this effect?

答:
You don't need to declare html tag and other in the index.html since you've declared it in the base.html. So, your index.html should look something like this:

{%extends "base.html"%}
{%block body%}

        Task
        Added
        Actions

    {%for task in tasks%}

        {{task.content}}
        {{task.date_created.date()}}

          Delete

          Update

    {%endfor%}

  <form action="/" method="POST">
    <input type="text" name="content" id="content"/>
    <input type="submit" value="Add Task"/>
  </form>

{%endblock%}
```

Body:
I've got a base.html file, which all other pages inherit from. Base links to main.css for style class definitions. However, so far this effect isn't being realized.

Base.html

<!DOCTYPE html>

    <meta charset="utf-8">
    {%block head%}
    {%endblock%}

    <link rel="stylesheet" href="{{url_for('static',filename='css/main.css')}}"/>

    {% if title %}
    <title> {{title}} </title>
    {%endif%}

Task Master

    {%block body%}
    {%endblock%}

index.html

<!DOCTYPE html>
{%extends "base.html"%}

    <meta charset="utf-8">

    {%block body%}

            Task
            Added
            Actions

        {%for task in tasks%}

            {{task.content}}
            {{task.date_created.date()}}

              Delete

              Update

        {%endfor%}

      <form action="/" method="POST">
        <input type="text" name="content" id="content"/>
        <input type="submit" value="Add Task"/>
      </form>

    {%endblock%}

main.css

.h1{
  display: block;
  font-size: 2em;
  margin-top: 0.67em;
  margin-bottom: 0.67em;
  margin-left: 0;
  margin-right: 0;
  font-weight: bold;
}

And lastly, app.py

from flask import Flask, render_template, url_for,request,redirect
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
db = SQLAlchemy(app)

class Todo(db.Model):
    id = db.Column(db.Integer,primary_key=True)
    content = db.Column(db.String(200),nullable=False)
    date_created = db.Column(db.DateTime,default=datetime.utcnow)

    def __repr__(self):
        return f'<Task {self.id}>'

@app.route('/',methods=['POST','GET'])
def index():
    if request.method == "POST":
        task_content = request.form['content']
        new_task = Todo(content=task_content)
        try:
            db.session.add(new_task)
            db.session.commit()
            return redirect('/')
        except:
            return "Failure to add content."
    else:
        tasks = Todo.query.order_by(Todo.date_created).all()
        return render_template('index.html',title='index',tasks=tasks)

@app.route('/delete/<int:id>')
def delete(id):
    task_to_delete = Todo.query.get_or_404(id)
    try:
        db.session.delete(task_to_delete)
        db.session.commit()
        return redirect('/')
    except:
        return 'Task cannot be deleted'

@app.route('/update/<int:id>',methods=['GET','POST'])
def update(id):
    task = Todo.query.get_or_404(id)
    if request.method == "POST":
        task.content = request.form['content']
        try:
            db.session.commit()
            return redirect('/')
        except:
            return 'Cannot update task'
    else:
        return render_template('update.html',task=task)

if __name__ == "__main__":
    app.run(debug=True)

What I'd like is for anything in an
 tag in base.html to be formatted according to the h1 definition on main.css. What do I need to do to achieve this effect?

Body:
You don't need to declare html tag and other in the index.html since you've declared it in the base.html. So, your index.html should look something like this:

{%extends "base.html"%}
{%block body%}
<div class="content">
  <table>
    <tr>
        <th>Task</th>
        <th>Added</th>
        <th>Actions</th>
    </tr>
    {%for task in tasks%}
    <tr>
        <td>{{task.content}}</td>
        <td>{{task.date_created.date()}}</td>
        <td>
          <a href="/delete/{{task.id}}">Delete</a>
          <br>
          <a href="/update/{{task.id}}">Update</a>
        </td>
    </tr>
    {%endfor%}
  </table>
  <form action="/" method="POST">
    <input type="text" name="content" id="content"/>
    <input type="submit" value="Add Task"/>
  </form>
</div>
{%endblock%}


问:
Copy text to clipboard from input hidden
I tried to copy by pressing a button, copy the value of input hidden. In JavaScript the value is obtained in var without problems, but it does not copy the value. What am I doing wrong?</p>

```<code> <div class="dropdown-divider"></div>
  <a class="dropdown-item" onclick="mycopyphone()">Copiar Telefono</a>
   <input type="hidden" id="Key" value="'. $row["telefono"] .'" />
     <script>
          function mycopyphone() {
           var hidden = document.getElementById("Key").value;
            copyText = hidden;
             copyText.select();
              copyText.setSelectionRange(0, 99999)
               document.execCommand("copy");
               alert("Copied the text: " + copyText.value);
                }
       </script>
</code>```

答:
Two problems. Hidden inputs don't support text selection, and it's the input element that has the select() function, not its value. You could do this instead:</p>

```<code><div class="dropdown-divider"></div>
<a class="dropdown-item" onclick="mycopyphone()">Copiar Telefono</a>
<input type="text" style="display:none;" id="Key" value="'. $row["telefono"] .'" />
<script>
  function mycopyphone() {
    var hidden = document.getElementById("Key");
    hidden.style.display = 'block';
    hidden.select();
    hidden.setSelectionRange(0, 99999)
    document.execCommand("copy");
    alert("Copied the text: " + hidden.value);
    hidden.style.display = 'none';
  }
</script>
</code>```

Body:
I tried to copy by pressing a button, copy the value of input hidden. In JavaScript the value is obtained in var without problems, but it does not copy the value. What am I doing wrong?

  Copiar Telefono
   <input type="hidden" id="Key" value="'. $row["telefono"] .'" />

Body:
Two problems. Hidden inputs don't support text selection, and it's the input element that has the select() function, not its value. You could do this instead:

<div class="dropdown-divider"></div>
<a class="dropdown-item" onclick="mycopyphone()">Copiar Telefono</a>
<input type="text" style="display:none;" id="Key" value="'. $row["telefono"] .'" />
<script>
  function mycopyphone() {
    var hidden = document.getElementById("Key");
    hidden.style.display = 'block';
    hidden.select();
    hidden.setSelectionRange(0, 99999)
    document.execCommand("copy");
    alert("Copied the text: " + hidden.value);
    hidden.style.display = 'none';
  }
</script>


问:
Smooth transition to make element appear
When I click on the button, I would like the arrow to appear by sliding out smoothly.  I'm currently doing this by adding the 'show' class if the arrow should be shown.  I've tried display: none; -> display: inline-block  I've tried width:0 -> width: 100%.

This code makes the arrow appear/disappear onclick, but without a smooth transition.

```<code><!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
  <style>
    span {
      -webkit-transition: all .5s;
      -moz-transition: all .5s;
      transition: all .5s;
      width: 0;
      overflow: hidden;
      max-width: 0;
      display:none;
    }
    .show {
      width: 100%;
      display: inline;
    }

  </style>
  <script>
    function toggleArrow(elemId) {
      let elem = document.getElementById(elemId);
      if (elem.classList.contains('show')) {
        elem.classList.remove('show');
      } else {
        elem.classList.add('show');
      }
    }
  </script>
</head>
<body>
  <div> Here's the button:
    <button onClick="toggleArrow('title-arrow')">my button<span id="title-arrow" > ↑</span></button>
  </div>
</body>
</html>
</code>```

答:
If you want to make it appear, you can use

button.addEventListener("click", function() {
  element.style.opacity = 1;
});```
<pre class="snippet-code-css lang-css prettyprint-override"><code>#element {
  opacity: 0;
  transition: 1s;
}</code>```

then when you click the button, the arrow element fades in in one second.

Body:
When I click on the button, I would like the arrow to appear by sliding out smoothly.  I'm currently doing this by adding the 'show' class if the arrow should be shown.  I've tried display: none; -> display: inline-block  I've tried width:0 -> width: 100%.

This code makes the arrow appear/disappear onclick, but without a smooth transition.

<!DOCTYPE html>

    <meta charset="UTF-8">

 Here's the button:
    <button onClick="toggleArrow('title-arrow')">my button ↑</button>

Body:
If you want to make it appear, you can use

button.addEventListener("click", function() {
  element.style.opacity = 1;
});

#element {
  opacity: 0;
  transition: 1s;
}

then when you click the button, the arrow element fades in in one second.


问:
How to stop bookdown tables from floating to bottom of the page in pdf?
I am using bookdown to create pdf reports, but my tables are all floating down to the bottom of the page, regardless of how much space there is. See this example:

```<code>---
title: "test_doc"
author: "Jake Thompson"
date: "6/30/2017"
output:
  bookdown::pdf_document2:
    toc: false
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, collapse = TRUE)
library(tidyverse)
```

# Test heading

Let make a data frame and print it in Table \@ref(tab:test-table)

```{r test-table}
data_frame(col_a = seq_len(5), col_b = rnorm(5), col_c = runif(5)) %>%
  knitr::kable(caption = "This is a test")
```
</code>```

The resulting pdf looks like this:

Why does the table go to the bottom of the page? And is there a way to prevent this behavior?

答:
I had to use

```<code>kable_styling(latex_options = "HOLD_position")
</code>```

Note the uppercase HOLD_position, different from hold_position.  See also here.

To be able to use that, I also had to add to the top section of the doc (from How to build a latex kable through bookdown::render_book?):

```<code>output:
  pdf_document:
    extra_dependencies: ["float"]
</code>```

Body:
I am using bookdown to create pdf reports, but my tables are all floating down to the bottom of the page, regardless of how much space there is. See this example:

---
title: "test_doc"
author: "Jake Thompson"
date: "6/30/2017"
output:
  bookdown::pdf_document2:
    toc: false
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, collapse = TRUE)
library(tidyverse)
```

# Test heading

Let make a data frame and print it in Table \@ref(tab:test-table)

```{r test-table}
data_frame(col_a = seq_len(5), col_b = rnorm(5), col_c = runif(5)) %>%
  knitr::kable(caption = "This is a test")
```

The resulting pdf looks like this:

Why does the table go to the bottom of the page? And is there a way to prevent this behavior?

Body:
I had to use

kable_styling(latex_options = "HOLD_position")

Note the uppercase HOLD_position, different from hold_position.  See also here.

To be able to use that, I also had to add to the top section of the doc (from How to build a latex kable through bookdown::render_book?):

output:
  pdf_document:
    extra_dependencies: ["float"]


问:
Pandas index match with column numbers and multiple criteria
I am trying to fill a dataframe that looks like this

```<code>      Name   Origin      Date Open High  Low Close    Date+1  Open+1 High+1 Low+1 Close+1
0  Bananas     Bali  20200108  NaN  NaN  NaN   NaN  20200109     NaN    NaN   NaN     NaN
1  Coconut  Bahamas  20200110  NaN  NaN  NaN   NaN  20200111     NaN    NaN   NaN     NaN
</code>```

With data found in a dataframe that looks like this

```<code>      Name   Origin      Date      Time  Open  High  Low  Close
0  Bananas     Bali  20200108  15:30:00  1.58  1.85  1.4   1.50
1  Bananas     Bali  20200108  22:00:00  1.68  1.78  1.5   1.60
2  Bananas     Bali  20200109  15:30:00  1.88  1.95  1.7   1.86
3  Bananas     Bali  20200109  22:00:00  1.78  1.88  1.6   1.65
4  Coconut  Bahamas  20200110  15:30:00  2.58  2.85  2.4   2.50
5  Coconut  Bahamas  20200110  22:00:00  2.68  2.78  2.5   2.60
6  Coconut  Bahamas  20200111  15:30:00  2.88  2.95  2.7   2.86
7  Coconut  Bahamas  20200111  22:00:00  2.78  2.88  2.6   2.65
</code>```

Since the columns in the first data frame have different names (eg. "Open" & "Open+1"), I can't think of an easy way to index match without having to copy the code and rename the columns in the second dataframe. Therefore I think it's easier to index match by column number, but im having issues with figuring out how to do this. The conditions for the columns are 'Name', 'Origin' and 'Date' (Date+1 for Open+1, etc...).

I tried to use the following code:

```<code>ColOpen = df2.iloc[:, [0,1,2,4,5,6,7]].groupby([0,1,2]).agg(Open=(4,'first'),High=(5,'max'),Low=(6,'min'), Close=(7,'last'))
</code>```

to get the right values for the columns, but I am getting a 'KeyError: 0', which refers to the column numbers.

I have created an example code below that can be used to get the same dataframes.

```<code>import pandas as pd

#Creating first sample dataframe
lst1 = [['Bananas', 'Bali', '20200108', 'NaN', 'NaN', 'NaN', 'NaN', '20200109', 'NaN', 'NaN', 'NaN', 'NaN'],
   ['Coconut', 'Bahamas', '20200110', 'NaN', 'NaN', 'NaN', 'NaN', '20200111', 'NaN', 'NaN', 'NaN', 'Nan']]

df1 = pd.DataFrame(lst1, columns =['Name', 'Origin', 'Date', 'Open', 'High', 'Low', 'Close', 'Date+1', 'Open+1', 'High+1', 'Low+1', 'Close+1'])
print('First Dataframe')
print(df1)

#Creating second sample dataframe
lst2 = [['Bananas', 'Bali', '20200108', '15:30:00', 1.58, 1.85, 1.50, 1.50],
    ['Bananas', 'Bali', '20200108', '22:00:00', 1.68, 1.78, 1.40, 1.60],
    ['Bananas', 'Bali', '20200109', '15:30:00', 1.88, 1.95, 1.70, 1.86],
    ['Bananas', 'Bali', '20200109', '22:00:00', 1.78, 1.88, 1.60, 1.65],
    ['Coconut', 'Bahamas', '20200110', '15:30:00', 2.58, 2.85, 2.50, 2.50],
    ['Coconut', 'Bahamas', '20200110', '22:00:00', 2.68, 2.78, 2.40, 2.60],
    ['Coconut', 'Bahamas', '20200111', '15:30:00', 2.88, 2.95, 2.70, 2.86],
    ['Coconut', 'Bahamas', '20200111', '22:00:00', 2.78, 2.88, 2.60, 2.65]]

df2 = pd.DataFrame(lst2, columns =['Name', 'Origin', 'Date', 'Time', 'Open', 'High', 'Low', 'Close'])
print('Second Dataframe')
print(df2)

#Index Match

ColOpen = df2.iloc[:, [0,1,2,4,5,6,7]].groupby([0,1,2]).agg(Open=(4,'first'),High=(5,'max'),Low=(6,'min'), Close=(7,'last'))

print("Printing first index")
print(ColOpen)

#Desired Output
lst3 = [['Bananas', 'Bali', '20200108', 1.58, 1.85, 1.4, 1.6, '20200109', 1.88, 1.95, 1.6, 1.65],
   ['Coconut', 'Bahamas', '20200110', 2.58, 2.85, 2.4, 2.6, '20200111', 2.88, 2.95, 2.6, 2.65]]

df3 = pd.DataFrame(lst3, columns =['Name', 'Origin', 'Date', 'Open', 'High', 'Low', 'Close', 'Date+1', 'Open+1', 'High+1', 'Low+1', 'Close+1'])
print('Desired Output')
print(df3)
</code>```

Can someone help me to figure out how to do this?

EDIT: Desired output. Also updated code a bit.

```<code>      Name   Origin      Date  Open  ...  Open+1  High+1  Low+1 Close+1
0  Bananas     Bali  20200108  1.58  ...    1.88    1.95    1.6    1.65
1  Coconut  Bahamas  20200110  2.58  ...    2.88    2.95    2.6    2.65
</code>```

答:
Edit: Found an easier solution using groupby.

Basically you pd.concat your data, but the data you concat is shifted 1 row backwards. Then concat, and do some editing. There you have it! df4 is what you are looking for.

```<code>import pandas as pd

df = pd.read_clipboard()

# all your new data is here
df2 = df.groupby(["Date", "Name", "Origin"]).agg(
    {"Open": ["min"], "High": ["max"], "Low": ["min"], "Close": ["max"]}
)

df2 = df2.droplevel(1, axis=1).reset_index()

column_names = ["Name", "Origin", "Date", "Open", "High", "Low", "Close", "Date+1", "Open+1", "High+1", "Low", "Close+1"]
desired_df = pd.DataFrame(columns=column_names)

df3 = pd.concat([df2, df2.add_suffix('+1').shift(-1)], axis=1)

df4 = df3.iloc[::2]

df4 = df4.drop(columns=['Date+1', 'Name+1', 'Origin+1']).reset_index(drop=True)

    Date    Name    Origin  Open    High    Low Close   Open+1  High+1  Low+1   Close+1
0   20200108    Bananas Bali    1.58    1.85    1.4 1.6 1.78    1.95    1.6 1.86
1   20200110    Coconut Bahamas 2.58    2.85    2.4 2.6 2.78    2.95    2.6 2.86
</code>```

Not the most efficient answer, but the desired outcome is so unusual. Here is the code, I mainly used Python functions with pandas dataframes. Get your data by copying your table with Ctrl+C or add it manually.

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

df = pd.read_clipboard()
column_names = ["Name", "Origin", "Date", "Open", "High", "Low", "Close", "Date+1", "Open+1", "High+1", "Low", "Close+1"]

def data_getter(data):
    intro = data.iloc[0][0:3]
    open_ = data.iloc[0].Open
    close = data.iloc[1].Close
    high = data.loc[:, 'High'].max()
    low = data.loc[:, 'Low'].min()
    frame = np.append(intro, [open_, high, low, close])
    return frame

def df_formatter(num: int):

    d = []

    for i in range(2):
        data = df.iloc[num*4+(i)*2:num*4+(i+1)*2]
        d.append(data_getter(data))

    d = np.append(d[0], [d[1][2:]])
    d = pd.Series(d)
    d.index = column_names
    return d

desired_df = pd.DataFrame(columns=column_names)

for i in range(int(df.shape[0]/4)):
    desired_df = desired_df.append(df_formatter(i), ignore_index=True)

print(desired_df)
</code>```

Body:
I am trying to fill a dataframe that looks like this

      Name   Origin      Date Open High  Low Close    Date+1  Open+1 High+1 Low+1 Close+1
0  Bananas     Bali  20200108  NaN  NaN  NaN   NaN  20200109     NaN    NaN   NaN     NaN
1  Coconut  Bahamas  20200110  NaN  NaN  NaN   NaN  20200111     NaN    NaN   NaN     NaN

With data found in a dataframe that looks like this

      Name   Origin      Date      Time  Open  High  Low  Close
0  Bananas     Bali  20200108  15:30:00  1.58  1.85  1.4   1.50
1  Bananas     Bali  20200108  22:00:00  1.68  1.78  1.5   1.60
2  Bananas     Bali  20200109  15:30:00  1.88  1.95  1.7   1.86
3  Bananas     Bali  20200109  22:00:00  1.78  1.88  1.6   1.65
4  Coconut  Bahamas  20200110  15:30:00  2.58  2.85  2.4   2.50
5  Coconut  Bahamas  20200110  22:00:00  2.68  2.78  2.5   2.60
6  Coconut  Bahamas  20200111  15:30:00  2.88  2.95  2.7   2.86
7  Coconut  Bahamas  20200111  22:00:00  2.78  2.88  2.6   2.65

Since the columns in the first data frame have different names (eg. "Open" & "Open+1"), I can't think of an easy way to index match without having to copy the code and rename the columns in the second dataframe. Therefore I think it's easier to index match by column number, but im having issues with figuring out how to do this. The conditions for the columns are 'Name', 'Origin' and 'Date' (Date+1 for Open+1, etc...).

I tried to use the following code:

ColOpen = df2.iloc[:, [0,1,2,4,5,6,7]].groupby([0,1,2]).agg(Open=(4,'first'),High=(5,'max'),Low=(6,'min'), Close=(7,'last'))

to get the right values for the columns, but I am getting a 'KeyError: 0', which refers to the column numbers.

I have created an example code below that can be used to get the same dataframes.

import pandas as pd

#Creating first sample dataframe
lst1 = [['Bananas', 'Bali', '20200108', 'NaN', 'NaN', 'NaN', 'NaN', '20200109', 'NaN', 'NaN', 'NaN', 'NaN'],
   ['Coconut', 'Bahamas', '20200110', 'NaN', 'NaN', 'NaN', 'NaN', '20200111', 'NaN', 'NaN', 'NaN', 'Nan']]

df1 = pd.DataFrame(lst1, columns =['Name', 'Origin', 'Date', 'Open', 'High', 'Low', 'Close', 'Date+1', 'Open+1', 'High+1', 'Low+1', 'Close+1'])
print('First Dataframe')
print(df1)

#Creating second sample dataframe
lst2 = [['Bananas', 'Bali', '20200108', '15:30:00', 1.58, 1.85, 1.50, 1.50],
    ['Bananas', 'Bali', '20200108', '22:00:00', 1.68, 1.78, 1.40, 1.60],
    ['Bananas', 'Bali', '20200109', '15:30:00', 1.88, 1.95, 1.70, 1.86],
    ['Bananas', 'Bali', '20200109', '22:00:00', 1.78, 1.88, 1.60, 1.65],
    ['Coconut', 'Bahamas', '20200110', '15:30:00', 2.58, 2.85, 2.50, 2.50],
    ['Coconut', 'Bahamas', '20200110', '22:00:00', 2.68, 2.78, 2.40, 2.60],
    ['Coconut', 'Bahamas', '20200111', '15:30:00', 2.88, 2.95, 2.70, 2.86],
    ['Coconut', 'Bahamas', '20200111', '22:00:00', 2.78, 2.88, 2.60, 2.65]]

df2 = pd.DataFrame(lst2, columns =['Name', 'Origin', 'Date', 'Time', 'Open', 'High', 'Low', 'Close'])
print('Second Dataframe')
print(df2)

#Index Match

ColOpen = df2.iloc[:, [0,1,2,4,5,6,7]].groupby([0,1,2]).agg(Open=(4,'first'),High=(5,'max'),Low=(6,'min'), Close=(7,'last'))

print("Printing first index")
print(ColOpen)

#Desired Output
lst3 = [['Bananas', 'Bali', '20200108', 1.58, 1.85, 1.4, 1.6, '20200109', 1.88, 1.95, 1.6, 1.65],
   ['Coconut', 'Bahamas', '20200110', 2.58, 2.85, 2.4, 2.6, '20200111', 2.88, 2.95, 2.6, 2.65]]

df3 = pd.DataFrame(lst3, columns =['Name', 'Origin', 'Date', 'Open', 'High', 'Low', 'Close', 'Date+1', 'Open+1', 'High+1', 'Low+1', 'Close+1'])
print('Desired Output')
print(df3)

Can someone help me to figure out how to do this?

EDIT: Desired output. Also updated code a bit.

      Name   Origin      Date  Open  ...  Open+1  High+1  Low+1 Close+1
0  Bananas     Bali  20200108  1.58  ...    1.88    1.95    1.6    1.65
1  Coconut  Bahamas  20200110  2.58  ...    2.88    2.95    2.6    2.65

Body:
Edit: Found an easier solution using groupby.

Basically you pd.concat your data, but the data you concat is shifted 1 row backwards. Then concat, and do some editing. There you have it! df4 is what you are looking for.

import pandas as pd

df = pd.read_clipboard()

# all your new data is here
df2 = df.groupby(["Date", "Name", "Origin"]).agg(
    {"Open": ["min"], "High": ["max"], "Low": ["min"], "Close": ["max"]}
)

df2 = df2.droplevel(1, axis=1).reset_index()

column_names = ["Name", "Origin", "Date", "Open", "High", "Low", "Close", "Date+1", "Open+1", "High+1", "Low", "Close+1"]
desired_df = pd.DataFrame(columns=column_names)

df3 = pd.concat([df2, df2.add_suffix('+1').shift(-1)], axis=1)

df4 = df3.iloc[::2]

df4 = df4.drop(columns=['Date+1', 'Name+1', 'Origin+1']).reset_index(drop=True)

    Date    Name    Origin  Open    High    Low Close   Open+1  High+1  Low+1   Close+1
0   20200108    Bananas Bali    1.58    1.85    1.4 1.6 1.78    1.95    1.6 1.86
1   20200110    Coconut Bahamas 2.58    2.85    2.4 2.6 2.78    2.95    2.6 2.86

Not the most efficient answer, but the desired outcome is so unusual. Here is the code, I mainly used Python functions with pandas dataframes. Get your data by copying your table with Ctrl+C or add it manually.

import pandas as pd
import numpy as np

df = pd.read_clipboard()
column_names = ["Name", "Origin", "Date", "Open", "High", "Low", "Close", "Date+1", "Open+1", "High+1", "Low", "Close+1"]

def data_getter(data):
    intro = data.iloc[0][0:3]
    open_ = data.iloc[0].Open
    close = data.iloc[1].Close
    high = data.loc[:, 'High'].max()
    low = data.loc[:, 'Low'].min()
    frame = np.append(intro, [open_, high, low, close])
    return frame

def df_formatter(num: int):

    d = []

    for i in range(2):
        data = df.iloc[num*4+(i)*2:num*4+(i+1)*2]
        d.append(data_getter(data))

    d = np.append(d[0], [d[1][2:]])
    d = pd.Series(d)
    d.index = column_names
    return d

desired_df = pd.DataFrame(columns=column_names)

for i in range(int(df.shape[0]/4)):
    desired_df = desired_df.append(df_formatter(i), ignore_index=True)

print(desired_df)


问:
Service issue with IBM Watson Natural Language Understanding
While using IBM Watson Natural Language Understanding, I got this issue: Service returned status code 500: cannot analyze: downstream issue.</p>

答:
I got to know that IBM Watson has regular maintenance at that time, hece, the service was returning server generic error.

thanks;


问:
Formdata post with vuex
I am very new to vuex and vue-js. I am trying to post my forms data using vuex. So far I managed to pass the Title successfully but I don’t know how to pass more than one piece of data?

data to pass:

```<code>// Title
<input type="text" name="subject_title">`

// selected data
<datepicker id="set_date" name="set_date"></datepicker>

// array of options
<multiselect v-model="value" :max-height="0" :options="options" :searchable="false" :multiple="true" group-label="language" track-by="name" label="name" placeholder=""><span class="arrow" slot="caret"></span></multiselect>
</code>```

I have put some of my code on github: https://github.com/samB67/VuexPost

答:
You can create a object to store your whole form in your  file upload.vue;

```<code>data() {
//...
  homeworkForm: {
    title: '',
    date: null,
    somethingElse: ''

  }
}
</code>```

Now in your form, you simply put the v-model to bind to each corresponding keys from your homeworkForm object;

```<code><input v-model="homeworkForm.title" type="text" class="form-control" id="formGroupExampleInput" placeholder="Enter text">
<!-- ... -->
<datepicker v-model="homeworkForm.date" id="set_date" name="set_date" />
</code>```

Then when you want to submit the form, you send the whole homeworkForm object to vuex.

```<code>postHandler() {
  this.$store.dispatch('createHomework', this.homeworkForm);
},
</code>```

Title: Formdata post with vuex

Body:
I am very new to vuex and vue-js. I am trying to post my forms data using vuex. So far I managed to pass the Title successfully but I don’t know how to pass more than one piece of data?

data to pass:

// Title
<input type="text" name="subject_title">`

// selected data
<datepicker id="set_date" name="set_date"></datepicker>

// array of options
<multiselect v-model="value" :max-height="0" :options="options" :searchable="false" :multiple="true" group-label="language" track-by="name" label="name" placeholder=""></multiselect>

I have put some of my code on github: https://github.com/samB67/VuexPost

Body:
You can create a object to store your whole form in your  file upload.vue;

data() {
//...
  homeworkForm: {
    title: '',
    date: null,
    somethingElse: ''

  }
}

Now in your form, you simply put the v-model to bind to each corresponding keys from your homeworkForm object;

<input v-model="homeworkForm.title" type="text" class="form-control" id="formGroupExampleInput" placeholder="Enter text">
<!-- ... -->
<datepicker v-model="homeworkForm.date" id="set_date" name="set_date" />

Then when you want to submit the form, you send the whole homeworkForm object to vuex.

postHandler() {
  this.$store.dispatch('createHomework', this.homeworkForm);
},


问:
Omitting parent of the parameter in ansible playbook
Anyone has any idea how to use default(omit) filter to drop the parent parameter too? Similar question asked here, the answer doesn't really answer the complexity of the problem I am facing though and I was rather abruptly disallowed to enhance the answer by asking a follow up question, hence the new question.

Here is the example ansible playbook task:

```<code>- name: Testing omit
  module:
    parameters:
      Parameter1:
        value: "{{ TemplateVariable | default(omit) }}"
</code>```

When TemplateVariable is not defined this results in the following json passed through to another API:

```<code>parameters { "Parameter1": {} }
</code>```

However I need this it to pass this:

```<code>parameters { }
</code>```

The module requires a "value" parameter for every parameter defined. I cannot fix the module, anyone can suggest any Ansible/Jinja2/YAML workaround please?

答:
How about something like this ?

- name: define module params for param1
  set_fact:
    module_params: "{{ module_params | default({}) | combine({ 'parameter1': { 'value': TemplateParam } }) }}"
  when: TemplateParam is defined

- name: eventually do this for next param2
  set_fact:
    module_params: "{{ module_params | default({}) | combine({ 'parameter2': { 'value': OtherParam } }) }}"
  when: OtherParam is defined

# Note you can organize the above in a loop if you have a lot of params.
# You will have to adapt the data structure and tests but it is quite straightforward.

 - name: Call module with calculated params
   module:
     parameters: "{{ module_params | default({}) }}"

```

Body:
Anyone has any idea how to use default(omit) filter to drop the parent parameter too? Similar question asked here, the answer doesn't really answer the complexity of the problem I am facing though and I was rather abruptly disallowed to enhance the answer by asking a follow up question, hence the new question.

Here is the example ansible playbook task:

- name: Testing omit
  module:
    parameters:
      Parameter1:
        value: "{{ TemplateVariable | default(omit) }}"

When TemplateVariable is not defined this results in the following json passed through to another API:

parameters { "Parameter1": {} }

However I need this it to pass this:

parameters { }

The module requires a "value" parameter for every parameter defined. I cannot fix the module, anyone can suggest any Ansible/Jinja2/YAML workaround please?


问:
gunicorn returns same pid for different worker process
I'm trying to get a better understanding of how gunicorn manages its processes so I wrote the following code:

```<code>from fastapi import FastAPI
import os
from time import sleep

app = FastAPI()
sleep_time = [5, 0]
global_var = 100
order_called = 0

@app.get('/test')
def test_handler():
    global global_var, order_called
    order_called += 1
    local_order_called = order_called
    sleep(sleep_time[order_called - 1]) # doing some time consuming stuff here
    global_var += 1
    return {"test": os.getpid(), "global_var": global_var, "order called": local_order_called}
</code>```

And I started gunicorn with:

```<code>gunicorn -w 2 -k uvicorn.workers.UvicornWorker server:app --preload
</code>```

The server is supposed to output: pid, 102, 1 and pid, 101, 2 for two consecutive requests - because it takes more time for worker #1 to finish.

I'm getting the correct output, but somehow the two pids returned are the same, which is very strange - this happens with or without the --preload.

Anyone can shed some lights on this? Thanks!

答: What makes you think the server should produce multiple PIDs? I don't see anything here that would fork another process. os.getpid() will return the same value each time if called by the server. Hope that helps!

Body:
I'm trying to get a better understanding of how gunicorn manages its processes so I wrote the following code:

from fastapi import FastAPI
import os
from time import sleep

app = FastAPI()
sleep_time = [5, 0]
global_var = 100
order_called = 0

@app.get('/test')
def test_handler():
    global global_var, order_called
    order_called += 1
    local_order_called = order_called
    sleep(sleep_time[order_called - 1]) # doing some time consuming stuff here
    global_var += 1
    return {"test": os.getpid(), "global_var": global_var, "order called": local_order_called}

And I started gunicorn with:

gunicorn -w 2 -k uvicorn.workers.UvicornWorker server:app --preload

The server is supposed to output: pid, 102, 1 and pid, 101, 2 for two consecutive requests - because it takes more time for worker #1 to finish.

I'm getting the correct output, but somehow the two pids returned are the same, which is very strange - this happens with or without the --preload.

Anyone can shed some lights on this? Thanks!


问:
Joining 2 tables for value comparision
I have 2 tables which contain data as follows:

```<code>Table1-
Col1.  Col2
A1.     B1
A1.     B2

Table2-
Col1.  Col2
A1.     C1
A1.     C2
</code>```

Now when I am joining these 2 tables to compare col2, sometimes I get the output like A1-B1-C1;A1-B2-C2 and some time I get it as A1-B1-C2;A1-B2-C1.
But I don't want it to come in the second form ever.can anyone suggest any query which will help me achieve this.
Thanks in advance

答:
You can do it with ROW_NUMBER() window function:

```<code>select t1.col1, t1.col2, t2.col2
from (
  select t.*, row_number() over (partition by col1 order by col2) rn
  from table1 t
) t1 inner join  (
  select t.*, row_number() over (partition by col1 order by col2) rn
  from table2 t
) t2 on t2.col1 = t1.col1 and t2.rn = t1.rn
</code>```

See the demo.

Results:

```<code>> COL1 | COL2 | COL2
> :--- | :--- | :---
> A1   | B1   | C1
> A1   | B2   | C2
</code>```

Title: Joining 2 tables for value comparision

Body:
I have 2 tables which contain data as follows:

Table1-
Col1.  Col2
A1.     B1
A1.     B2

Table2-
Col1.  Col2
A1.     C1
A1.     C2

Now when I am joining these 2 tables to compare col2, sometimes I get the output like A1-B1-C1;A1-B2-C2 and some time I get it as A1-B1-C2;A1-B2-C1.
But I don't want it to come in the second form ever.can anyone suggest any query which will help me achieve this.
Thanks in advance

Body:
You can do it with ROW_NUMBER() window function:

select t1.col1, t1.col2, t2.col2
from (
  select t.*, row_number() over (partition by col1 order by col2) rn
  from table1 t
) t1 inner join  (
  select t.*, row_number() over (partition by col1 order by col2) rn
  from table2 t
) t2 on t2.col1 = t1.col1 and t2.rn = t1.rn

See the demo.

Results:

> COL1 | COL2 | COL2
> :--- | :--- | :---
> A1   | B1   | C1
> A1   | B2   | C2


问:
get n leftBits of binary in C
Got function getLeftBits(int n,int num) and I need to return num bits from left for example:

```<code>getLeftBits(7,31) --> 3
getLeftBits(-1,2) --> 3
</code>```

I handle it easy for every n>0: n >> (32 - num)

but when n<0 got some problems.

Any suggestions?

答:
If I understand well you want to see the n leftmost bits of the binary representation of the number.

Most of the implementations will use arithmetic right shift not the logical right shift. You need to cast the number to unsigned integer to prevent this behaviour. It does not matter when you shift unsigned numbers, but it matters when you shift the signed number.

```<code>unsigned getLeftBits(int n, int num)
{
    return n >> (32 - num);
}

unsigned getLeftBits1(int n, int num)
{
    return (unsigned)n >> (32 - num);
}

getLeftBits:
        mov     ecx, 32
        mov     eax, edi
        sub     ecx, esi
        sar     eax, cl              <-------here
        ret
getLeftBits1:
        mov     ecx, 32
        mov     eax, edi
        sub     ecx, esi
        shr     eax, cl              <-------here
        ret
</code>```

https://godbolt.org/z/JuY_z4

Title: get n leftBits of binary in C

Body:
Got function getLeftBits(int n,int num) and I need to return num bits from left for example:

getLeftBits(7,31) --> 3
getLeftBits(-1,2) --> 3

I handle it easy for every n>0: n >> (32 - num)

but when n<0 got some problems.

Any suggestions?

Body:
If I understand well you want to see the n leftmost bits of the binary representation of the number.

Most of the implementations will use arithmetic right shift not the logical right shift. You need to cast the number to unsigned integer to prevent this behaviour. It does not matter when you shift unsigned numbers, but it matters when you shift the signed number.

unsigned getLeftBits(int n, int num)
{
    return n >> (32 - num);
}

unsigned getLeftBits1(int n, int num)
{
    return (unsigned)n >> (32 - num);
}

getLeftBits:
        mov     ecx, 32
        mov     eax, edi
        sub     ecx, esi
        sar     eax, cl              <-------here
        ret
getLeftBits1:
        mov     ecx, 32
        mov     eax, edi
        sub     ecx, esi
        shr     eax, cl              <-------here
        ret

https://godbolt.org/z/JuY_z4


问:
How do I measure execution time of a command on the Windows command line?
Is there a built-in way to measure execution time of a command on the Windows command line?</p>

答:
Since others are recommending installing things like freeware and PowerShell, you could also install Cygwin, which would give you access to many basic Unix commands like time:

```<code>abe@abe-PC:~$ time sleep 5

real    0m5.012s
user    0m0.000s
sys 0m0.000s
</code>```

Not sure how much overhead Cygwin adds.

OwnerDisplayName: Kuroki Kaze

Body:
Since others are recommending installing things like freeware and PowerShell, you could also install Cygwin, which would give you access to many basic Unix commands like time:

abe@abe-PC:~$ time sleep 5

real    0m5.012s
user    0m0.000s
sys 0m0.000s

Not sure how much overhead Cygwin adds.


问:
Unable to locate element when using python selenium webdriver chrome on Linux Ubuntu
Using python, I am trying to get the number in front of the "Advances -" text on the url:
https://www.nseindia.com/market-data/pre-open-market-cm-and-emerge-market

I am using the below xpath:

//*[@id="livepreOpenAdv"]

but getting the below error:

no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="livepreOpenAdv"]"}
(Session info: headless chrome=83.0.4103.61)

I am using chromedriver in Linux (ubuntu 20.04)

Below is the python code:

```<code>from selenium import webdriver
from selenium.webdriver.chrome.options import Options

strURL="https://www.nseindia.com/market-data/pre-open-market-cm-and-emerge-market"

options = Options()

options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')

driver = webdriver.Chrome("/usr/lib/chromium-browser/chromedriver",options=options)

driver.implicitly_wait(60)
driver.get(strURL)

print("Finding element.")

try:
    # advances = driver.find_element_by_id("livepreOpenAdv")
    advances = driver.find_element_by_xpath('''//*[@id="livepreOpenAdv"]''').text
    print(advances)
except Exception as ex:
    print(ex)

driver.close()

print("closed driver")
</code>```

This code works fine in windows. Not able to make out what is the issue here.

答:
Finally the below code is working for me. I had to install google-chrome-stable package through apt. Also suggestions from @E.Wiest helped. Thanks!
As this was an headless execution, the url was giving access denied message. Added user-agent parameter to resolve that. </p>

```<code>from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time

strURL="https://www.nseindia.com/market-data/pre-open-market-cm-and-emerge-market"

options = Options()
options.add_argument('--no-sandbox')
options.add_argument('--headless')
options.add_argument(f'user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36')

driver = webdriver.Chrome("/usr/lib/chromium-browser/chromedriver",options=options)

t1=time.time()
driver.get(strURL)

strXpath_advances='//*[@id="livepreOpenAdv"]'
strXpath_decline='//*[@id="livepreOpenDec"]'

try:
    advances = WebDriverWait(driver,30).until(EC.visibility_of_element_located((By.XPATH, strXpath_advances)))
    decline = WebDriverWait(driver,30).until(EC.visibility_of_element_located((By.XPATH, strXpath_decline)))
    print("Advances={} Decline={}".format(advances.text,decline.text))
except Exception as ex:
    print("Exception=",ex)

t2=time.time()-t1
driver.close()
print("Done. closed driver. Time={0:.2f}".format(t2))
</code>```

Body:
Using python, I am trying to get the number in front of the "Advances -" text on the url:
https://www.nseindia.com/market-data/pre-open-market-cm-and-emerge-market

I am using the below xpath:

//*[@id="livepreOpenAdv"]

but getting the below error:

no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="livepreOpenAdv"]"}
(Session info: headless chrome=83.0.4103.61)

I am using chromedriver in Linux (ubuntu 20.04)

Below is the python code:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

strURL="https://www.nseindia.com/market-data/pre-open-market-cm-and-emerge-market"

options = Options()

options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')

driver = webdriver.Chrome("/usr/lib/chromium-browser/chromedriver",options=options)

driver.implicitly_wait(60)
driver.get(strURL)

print("Finding element.")

try:
    # advances = driver.find_element_by_id("livepreOpenAdv")
    advances = driver.find_element_by_xpath('''//*[@id="livepreOpenAdv"]''').text
    print(advances)
except Exception as ex:
    print(ex)

driver.close()

print("closed driver")

This code works fine in windows. Not able to make out what is the issue here.

Body:
Finally the below code is working for me. I had to install google-chrome-stable package through apt. Also suggestions from @E.Wiest helped. Thanks!
As this was an headless execution, the url was giving access denied message. Added user-agent parameter to resolve that.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time

strURL="https://www.nseindia.com/market-data/pre-open-market-cm-and-emerge-market"

options = Options()
options.add_argument('--no-sandbox')
options.add_argument('--headless')
options.add_argument(f'user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36')

driver = webdriver.Chrome("/usr/lib/chromium-browser/chromedriver",options=options)

t1=time.time()
driver.get(strURL)

strXpath_advances='//*[@id="livepreOpenAdv"]'
strXpath_decline='//*[@id="livepreOpenDec"]'

try:
    advances = WebDriverWait(driver,30).until(EC.visibility_of_element_located((By.XPATH, strXpath_advances)))
    decline = WebDriverWait(driver,30).until(EC.visibility_of_element_located((By.XPATH, strXpath_decline)))
    print("Advances={} Decline={}".format(advances.text,decline.text))
except Exception as ex:
    print("Exception=",ex)

t2=time.time()-t1
driver.close()
print("Done. closed driver. Time={0:.2f}".format(t2))