问:
Find all RealmObjects that have inside a list a given RealmObject
Lets say I have this class (it already has an id property but it's already auto generated by realm):

```<code>public class User extends RealmObject {
   private String name;
   private String username;
}
</code>```

And another one that has a list of Users inside:

```<code>public class Ride extends RealmObject {
    private String tripName;
    private String rideType;
    private RealmList<User> usersJoined;
}
</code>```

I just want to do a query to realm to get a list of Rides where the user is inside. I found another question like this but in that case the asker already has the object that have the whole list and he just need to find the user inside that spefic object property list, but in my case I want to get the Rides where the user (that I already know) is inside. Thank you in advanced!

答:
Finally fixed using stream().filter :

```<code>RealmResults<Ride> rides = realm.where(Ride.class).findAll();

List<Ride> = rides.stream().filter(ride -> ride.getUsersJoined().contains(givenUser)).collect(Collectors.toList())
</code>```

Body:
Lets say I have this class (it already has an id property but it's already auto generated by realm):

public class User extends RealmObject {
   private String name;
   private String username;
}

And another one that has a list of Users inside:

public class Ride extends RealmObject {
    private String tripName;
    private String rideType;
    private RealmList<User> usersJoined;
}

I just want to do a query to realm to get a list of Rides where the user is inside. I found another question like this but in that case the asker already has the object that have the whole list and he just need to find the user inside that spefic object property list, but in my case I want to get the Rides where the user (that I already know) is inside. Thank you in advanced!

Body:
Finally fixed using stream().filter :

RealmResults<Ride> rides = realm.where(Ride.class).findAll();

List<Ride> = rides.stream().filter(ride -> ride.getUsersJoined().contains(givenUser)).collect(Collectors.toList())


来源: stackexchange

问:
Jest testing, keeps return undefined?
I'm trying to test a function with jest, and I simply can figure out what's wrong? It keeps saying it expects to return the output, but got undefined. I have tested the function elsewhere where it seems to return the correct array.

I'm calling my my function and passing it an Object, it's then supposed to return an array. Then I'm calling  .toEqual(output) which is an array.

```<code>//This is my function
const allAddresses = [
];

const updateAllAddresses = (obj) => {
  const transferAmount = obj.transferAmount;
  const to = obj.to;
  const transferAddress = obj.address;
  const newBalance = obj.newBalance;
  const addressArr = [...allAddresses];
  console.log("This addressArr", addressArr);
  console.log("this is obj", obj);
  //To set your account to the new balance after transfer and
  //to check if the address you transfer to is your own account
  addressArr.map((address) => {
    if (address.account === transferAddress) {
      console.log("This is inside the map !!!!");
      address.balance = Number(newBalance);
    }
    if (address.account === to) {
      console.log("2");
      address.balance = Number(transferAmount) + Number(address.balance);
    }
    console.log("last part of the testing", addressArr);
    return addressArr;
  });
};

const obj = {
};
const output = [
];

//This is my test
describe("Update array", () => {
  test("update the array with the new information", () => {
    expect(updateAllAddresses(obj)).toEqual(output);
  });
});
</code>```

答:
You cannot short circuit and return inside a map function. You should return the object after the map

Also, when you change address inside the map; It really does not change anything, since that address variable will be removed from memory on next iteration

ContentLicense: CC BY-SA 4.0

Title: Jest testing, keeps return undefined?

Body:
I'm trying to test a function with jest, and I simply can figure out what's wrong? It keeps saying it expects to return the output, but got undefined. I have tested the function elsewhere where it seems to return the correct array.

I'm calling my my function and passing it an Object, it's then supposed to return an array. Then I'm calling  .toEqual(output) which is an array.

//This is my function
const allAddresses = [
];

const updateAllAddresses = (obj) => {
  const transferAmount = obj.transferAmount;
  const to = obj.to;
  const transferAddress = obj.address;
  const newBalance = obj.newBalance;
  const addressArr = [...allAddresses];
  console.log("This addressArr", addressArr);
  console.log("this is obj", obj);
  //To set your account to the new balance after transfer and
  //to check if the address you transfer to is your own account
  addressArr.map((address) => {
    if (address.account === transferAddress) {
      console.log("This is inside the map !!!!");
      address.balance = Number(newBalance);
    }
    if (address.account === to) {
      console.log("2");
      address.balance = Number(transferAmount) + Number(address.balance);
    }
    console.log("last part of the testing", addressArr);
    return addressArr;
  });
};

const obj = {
};
const output = [
];

//This is my test
describe("Update array", () => {
  test("update the array with the new information", () => {
    expect(updateAllAddresses(obj)).toEqual(output);
  });
});


问:
Bulk update MongoDB Collection from a CSV file
I have a mongoose schema defined as

```<code>const masterSchema = new mongoose.Schema({
  chapter: { type: Number, required: true },
  line: { type: Number, required: true },
  translations: [
    {
      translation: { type: String, required: true },
    },
  ],
});
</code>```

I am trying to update the collection from a CSV file. The Collection has more than 5K documents.

Sample data

```<code>[
  {
    chapter: 1,
    line: 1,
    translations: [
      {
        translation: "xyz",
      },
    ],
  },
  {
    chapter: 1,
    line: 2,
    translations: [
      {
        translation: "abc",
      },
    ],
  },
];
</code>```

CSV file has a format of

```<code>chapter,line,translation
1,1,example1
1,2,example2
....
</code>```

The output should be

```<code>[
  {
    chapter: 1,
    line: 1,
    translations: [
      {
        translation: "xyz",
      },
      {
        translation : "example1"
      }
    ],
  },
  {
    chapter: 1,
    line: 2,
    translations: [
      {
        translation: "abc",
      },
      {
        translation : "example2"
      }
    ],
  },
]
</code>```

I am confused about how updateMany() will be used to insert the data into the correct document. (if it the correct way to solve the problem)

答:
Assuming that chapter+line is not unique, then really the hardest part of this exercise is parsing the CSV in the first place.   CSV tends to throw surprises at you like quoted material, unintended leading and trailing whitespace in parsed columns, etc., so it is good to use some programming around it to control it, e.g.

import csv
from pymongo import MongoClient

client = MongoClient('mongodb://localhost:27017')
db = client['testX']
coll = db['foo']

with open('myFile', 'r') as csvfile:
    reader = csv.reader(csvfile)
    header = next(reader, None)  # capture and skip header line
    for row in reader:
        print(row)  # for fun
        # Careful: must turn parsed strings into int to match database type.
        # Also, strip whitespace from col 2 for safety:
        coll.update_many({'chapter':int(row[0]),'line':int(row[1])},
                         {'$push': {'translations':{'translation':row[2].strip()}}})
```

Body:
I have a mongoose schema defined as

const masterSchema = new mongoose.Schema({
  chapter: { type: Number, required: true },
  line: { type: Number, required: true },
  translations: [
    {
      translation: { type: String, required: true },
    },
  ],
});

I am trying to update the collection from a CSV file. The Collection has more than 5K documents.

Sample data

[
  {
    chapter: 1,
    line: 1,
    translations: [
      {
        translation: "xyz",
      },
    ],
  },
  {
    chapter: 1,
    line: 2,
    translations: [
      {
        translation: "abc",
      },
    ],
  },
];

CSV file has a format of

chapter,line,translation
1,1,example1
1,2,example2
....

The output should be

[
  {
    chapter: 1,
    line: 1,
    translations: [
      {
        translation: "xyz",
      },
      {
        translation : "example1"
      }
    ],
  },
  {
    chapter: 1,
    line: 2,
    translations: [
      {
        translation: "abc",
      },
      {
        translation : "example2"
      }
    ],
  },
]

I am confused about how updateMany() will be used to insert the data into the correct document. (if it the correct way to solve the problem)


问:
Kafka dynamic indirection with pub/sub like messaging
I am new to Kafka, but have already a quite challenging problem to solve.

Before describing the problem: My application is about spatial indexing and geographic data coordination. Meaning, I really need the described type of re-routing for good reasons.

I need to achieve the following event flow:

there are n instances of my application, and a large variable amount of data objects in total globally.

each instance knows only about a subset of global objects. But there can be multiple instances knowing about the same data object.

However, in case a global object is changed (including ones that are unknown at this point) the change must be propagated to all instances which know this data object. In that sense, „instances“ are subscribers to certain data objects.

Question 1:
Is it wise to use Kafka with log compaction enabled to maintain a strongly consistent list of subscribers to an object? E.g.:

A topic named changeevents where all instances may publish changed data as required for this edge case.

A topic named pubsub with a map from object id to subscriber topic:

ObjectId:[subscriberId1Tooic, subscriberId2Topic]

Question 2:
What choices do I have in Kafka to make this re-routing work in the most scalable and low latency way? Is it possible to create dynamic routing events in-place, e.g. having a stream of change events and Kafka places the change event to all subscriber topics?

Question 3:
This seems all a bit complicated. My scenario is pretty unique, yet I hope I‘m missing something to make this less complicated?

It would be a valid question to ask at this point why I chose to use Kafka for what looks like a publish/subscribe problem. First, the data flow between the backend instances is in the common case not requiring this type of re-routing and the problem above is solving a < 1% case of total data to be processed. Secondly, I‘m also investigating Apache Pulsar which seems to have better support for publish/subscribe scenarios. Where I struggle with is that my application is deployed by customers, and Pulsar has a far lower adoption / acceptance rate.

I researched about routing options in Kafka, and the closest I could find to this problem seems dynamic routing as described here https://www.confluent.io/blog/putting-events-in-their-place-with-dynamic-routing/

As I see it, I need to have an additional data source to maintain the pub/sub list, additional custom processors that place the messages to related subscriber topics, also all at the cost of duplicating messages.

答:
Sure, a compacted topic seems like a reasonable approach if you want eventually consistent, unique data. You'll need a GlobalKTable like the linked post says to query against that data, though.

"Topics" themselves do not subscribe. You'd need a consumer that reads all "changeevents", then filters/branches into the downstream "client topics". This will likely be the largest bottleneck, and main way to scale it would be lots of partitions and record keys that map somehow to geographic regions, or some other unique identification.

It's unclear what your output data looks like; if you're publishing notifications or displaying some kind of map, you'll need another system listening to only "local" geo-fenced events. If you need to read those events multiple times, you'd probably need more than Kafka, such as Elasticsearch geospatial queries, if not another system that supports geopoint data (I recall there being a GIS plugin for Postgres). E.g Use Kafka Connect to write into that system, and anything that needs those events, just queries the database.


问:
In tic-tac-toe game while loop for invalid moves is declining valid moves too
[The game will be displayed as nine blocks, with rows and columns marked numerically, of which the squares' coordinates will be a combination; ex: 11(first column, first row), 12(first column, second row), 13, 21, 22, etc. till 33]
I have the display down, just can't figure out this one issue.

The following is my code for accepting moves:

```<code>askformovestr = "Turn: X. Please enter the coordinates for the square in which you wish to drop your piece, with column number followed by row number(e.g. 11 drops to top left square, 12 to the square below it, 23 to the square in the third row, second column, etc., etc.): "
def askformove():
    inmove = int(input(askformovestr))
    while inmove is not 11 or 12 or 13 or 21 or 22 or 23 or 31 or 32 or 33:
        print("Invalid input, sorry. Please try again.")
        inmove = int((input("Turn: X. Please enter the coordinates for the square in which you wish to drop your piece(e.g. 1B, 2C, 3A, etc.): ")))
askformove()
</code>```

答:
Maybe instead this:

```<code>while inmove is not 11 or 12 or 13 or 21 or 22 or 23 or 31 or 32 or 33:
</code>```

Try this:

```<code>while (inmove != 11 or inmove != 12 or inmove != 13 or inmove != 21 or inmove != 22 or
       inmove != 23 or inmove != 31 or inmove != 32 or inmove != 33):
</code>```

Body:
[The game will be displayed as nine blocks, with rows and columns marked numerically, of which the squares' coordinates will be a combination; ex: 11(first column, first row), 12(first column, second row), 13, 21, 22, etc. till 33]
I have the display down, just can't figure out this one issue.

The following is my code for accepting moves:

askformovestr = "Turn: X. Please enter the coordinates for the square in which you wish to drop your piece, with column number followed by row number(e.g. 11 drops to top left square, 12 to the square below it, 23 to the square in the third row, second column, etc., etc.): "
def askformove():
    inmove = int(input(askformovestr))
    while inmove is not 11 or 12 or 13 or 21 or 22 or 23 or 31 or 32 or 33:
        print("Invalid input, sorry. Please try again.")
        inmove = int((input("Turn: X. Please enter the coordinates for the square in which you wish to drop your piece(e.g. 1B, 2C, 3A, etc.): ")))
askformove()

Body:
Maybe instead this:

while inmove is not 11 or 12 or 13 or 21 or 22 or 23 or 31 or 32 or 33:

Try this:

while (inmove != 11 or inmove != 12 or inmove != 13 or inmove != 21 or inmove != 22 or
       inmove != 23 or inmove != 31 or inmove != 32 or inmove != 33):


问:
Why connection is not establishing on first time?
I want to send my id & password to server and get the response from server. Here is my code. It is not working for the first time. But iam getting the response from server if i execute my application on second time. It is throwing "Post method failed: -1 null" on first time. Where iam wrong?? Why if() block is executing on first time?? could you please tell me. </p>

```<code>HttpsURLConnection con = null;
String httpsURL = "https://www.abc.com/login";
String query = "id=xyz&password=pqr";
URL url = new URL(httpsURL);
con = (HttpsURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-length", String.valueOf(query.length()));
con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
con.setRequestProperty("User-Agent","Mozilla/4.0(compatible; MSIE 5.0; Windows 98; DigExt)");
con.setDoInput(true);
con.setDoOutput(true);
DataOutputStream output = new DataOutputStream(con.getOutputStream());
output.writeBytes(query);
output.close();
int respCode = con.getResponseCode();

if (respCode != HttpsURLConnection.HTTP_OK)
{
  throw new Exception("POST method failed: " + con.getResponseCode()+ "\t" + con.getResponseMessage()); }
 else {
//read the content from server
}
</code>```

答:
Check Server service validity with other technology  and/or classic java. You didn say in your question if you succeed to discriminate the server from the issue.

from java doc ...getResponseCode returns -1 if no code can be discerned from the response (i.e., the response is not valid HTTP).

Java https post request example : http://www.java-samples.com/java/POST-toHTTPS-url-free-java-sample-program.htm

try to close your outputstream after querying the status and not before...that may help

ContentLicense: CC BY-SA 3.0

Body:
I want to send my id & password to server and get the response from server. Here is my code. It is not working for the first time. But iam getting the response from server if i execute my application on second time. It is throwing "Post method failed: -1 null" on first time. Where iam wrong?? Why if() block is executing on first time?? could you please tell me.

HttpsURLConnection con = null;
String httpsURL = "https://www.abc.com/login";
String query = "id=xyz&password=pqr";
URL url = new URL(httpsURL);
con = (HttpsURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-length", String.valueOf(query.length()));
con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
con.setRequestProperty("User-Agent","Mozilla/4.0(compatible; MSIE 5.0; Windows 98; DigExt)");
con.setDoInput(true);
con.setDoOutput(true);
DataOutputStream output = new DataOutputStream(con.getOutputStream());
output.writeBytes(query);
output.close();
int respCode = con.getResponseCode();

if (respCode != HttpsURLConnection.HTTP_OK)
{
  throw new Exception("POST method failed: " + con.getResponseCode()+ "\t" + con.getResponseMessage()); }
 else {
//read the content from server
}


问:
Is there any way to convert the hex code from string to hex colour which discord.py supports?
I am trying to figure this out for so long,
but I am not able to convert the hex code from string to actual hex code like this 0xFFFFFF
since discord.py doesn't take hex code in str datatype.
Code Snippet:-

```<code>@client.command()
async def testing(ctx):
    # color = lgd.hexConvertor(colorCollection.find({},{"_id":0,"Hex":1}))
    c = "0xFFFFFF"
    inte = int(c,16)
    color = hex(inte)
    await ctx.send(embed = discord.Embed(description = "testing",color = color))
</code>```

Error:-

```<code>Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "main.py", line 221, in testing
    await ctx.send(embed = discord.Embed(description = "testing",color = color))
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/embeds.py", line 115, in __init__
    self.colour = colour
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/embeds.py", line 230, in colour
    raise TypeError('Expected discord.Colour, int, or Embed.Empty but received %s instead.' % value.__class__.__name__)
TypeError: Expected discord.Colour, int, or Embed.Empty but received str instead.
</code>```

This is how I stored the hex codes in my mongodb atlas database->

Edit:-
It worked on using inte instead of color

答:
This is a list of a lot of colors with the codes that discord.py supports</p>
```<code>default = 0
teal = 0x1abc9c
dark_teal = 0x11806a
green = 0x2ecc71
dark_green = 0x1f8b4c
blue = 0x3498db
dark_blue = 0x206694
purple = 0x9b59b6
dark_purple = 0x71368a
magenta = 0xe91e63
dark_magenta = 0xad1457
gold = 0xf1c40f
dark_gold = 0xc27c0e
orange = 0xe67e22
dark_orange = 0xa84300
red = 0xe74c3c
dark_red = 0x992d22
lighter_grey = 0x95a5a6
dark_grey = 0x607d8b
light_grey = 0x979c9f
darker_grey = 0x546e7a
blurple = 0x7289da
greyple = 0x99aab5
</code>```

Body:
I am trying to figure this out for so long,
but I am not able to convert the hex code from string to actual hex code like this 0xFFFFFF
since discord.py doesn't take hex code in str datatype.
Code Snippet:-

@client.command()
async def testing(ctx):
    # color = lgd.hexConvertor(colorCollection.find({},{"_id":0,"Hex":1}))
    c = "0xFFFFFF"
    inte = int(c,16)
    color = hex(inte)
    await ctx.send(embed = discord.Embed(description = "testing",color = color))

Error:-

Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "main.py", line 221, in testing
    await ctx.send(embed = discord.Embed(description = "testing",color = color))
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/embeds.py", line 115, in __init__
    self.colour = colour
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/embeds.py", line 230, in colour
    raise TypeError('Expected discord.Colour, int, or Embed.Empty but received %s instead.' % value.__class__.__name__)
TypeError: Expected discord.Colour, int, or Embed.Empty but received str instead.

This is how I stored the hex codes in my mongodb atlas database->

Edit:-
It worked on using inte instead of color

Body:
This is a list of a lot of colors with the codes that discord.py supports

default = 0
teal = 0x1abc9c
dark_teal = 0x11806a
green = 0x2ecc71
dark_green = 0x1f8b4c
blue = 0x3498db
dark_blue = 0x206694
purple = 0x9b59b6
dark_purple = 0x71368a
magenta = 0xe91e63
dark_magenta = 0xad1457
gold = 0xf1c40f
dark_gold = 0xc27c0e
orange = 0xe67e22
dark_orange = 0xa84300
red = 0xe74c3c
dark_red = 0x992d22
lighter_grey = 0x95a5a6
dark_grey = 0x607d8b
light_grey = 0x979c9f
darker_grey = 0x546e7a
blurple = 0x7289da
greyple = 0x99aab5


问:
Terrafom referencing map output in module
I have a terraform module as follows

```<code>module "vpc" {
  source                      = "./modules"
  my_aws_account_id           = "<aws_account_id>"
  my_aws_vpc_id               = "<aws_vpc_id>"
  my_vpc_peering_connections  = {
    stage    = "<aws_stage_vpc_id>"
    preprod  = "<aws_preprod_vpc_id>"
  }
  my_vpc_route_table          = {
    default   = {
      name    = "test-default"
      routes  = [{
        cidr_block                  = "10.10.16.0/20"
        vpc_peering_connection_id   = module.vpc.op_vpc_peering_connections["stage"]
      }, {
        cidr_block                  = "10.10.32.0/20"
        vpc_peering_connection_id   = module.vpc.op_vpc_peering_connections["preprod"]
      }]
    }
  }
}
</code>```

variable

```<code>variable "my_aws_account_id" {
  type  = string
}

variable "my_aws_vpc_id" {
  type  = string
}

variable "my_vpc_peering_connections" {
  type  = map(string)
}

variable "my_vpc_route_table" {
  type  = object({
    default = object({
      name    = string
      routes  = list(object({
        cidr_block                  = string
        vpc_peering_connection_id   = string
      }))
    })
  })
}
</code>```

resource

```<code>resource "aws_vpc_peering_connection" "peering_connections" {
  for_each      = var.my_vpc_peering_connections

  peer_owner_id = var.my_aws_account_id
  peer_vpc_id   = each.value
  vpc_id        = var.my_aws_vpc_id
  tags          = {
    Name = each.key
  }

  timeouts {}
}

resource "aws_route_table" "default_route_table" {
  vpc_id    = var.my_aws_vpc_id
  tags      = {
    Name = var.my_vpc_route_table.default.name
  }

  dynamic "route" {
    for_each = var.my_vpc_route_table.default.routes
    content {
      cidr_block                  = lookup(route.value, "cidr_block")
      vpc_peering_connection_id   = lookup(route.value, "vpc_peering_connection_id")

      instance_id                 = ""
      gateway_id                  = ""
      carrier_gateway_id          = ""
      destination_prefix_list_id  = ""
      egress_only_gateway_id      = ""
      ipv6_cidr_block             = ""
      local_gateway_id            = ""
      nat_gateway_id              = ""
      network_interface_id        = ""
      transit_gateway_id          = ""
      vpc_endpoint_id             = ""
    }
  }

  depends_on = [
    aws_vpc_peering_connection.peering_connections
  ]
}
</code>```

output

```<code>output op_vpc_peering_connections {
  value = tomap({
    for name, details in aws_vpc_peering_connection.peering_connections : name => details.id
  })
}
</code>```

but when I run the following commands the first one success and the second one fails with an error

```<code>> terraform import module.vpc.aws_vpc_peering_connection.peering_connections["stage"] <peering_connection_id>
> terraform import module.vpc.aws_vpc_peering_connection.peering_connections["preprod"] <peering_connection_id>
</code>```

error

```<code>╷
│ Error: Invalid index
│
│   on /Users/sarad/workspace/personal/terraform-for-each/main.tf line 33, in module "vpc":
│   33:         vpc_peering_connection_id   = module.vpc.op_vpc_peering_connections["preprod"]
│     ├────────────────
│     │ module.vpc.op_vpc_peering_connections is map of string with 1 element
│
│ The given key does not identify an element in this collection value.
╵
</code>```

the error goes comes if I have more than 1 element in op_vpc_peering_connections and I am trying to reference them in module by name like module.vpc.op_vpc_peering_connections["stage"] or module.vpc.op_vpc_peering_connections["preprod"]

also, I am unable to debug the output with terraform output where I get the following warning

```<code>╷
│ Warning: No outputs found
│
│ The state file either has no outputs defined, or all the defined outputs are empty. Please define an output in your configuration with the `output` keyword and run `terraform
│ refresh` for it to become available. If you are using interpolation, please verify the interpolated value is not empty. You can use the `terraform console` command to assist.
╵
</code>```

答:
If you first run a terraform plan you will see the exact name of each resource. At which point all you have to do is copy/paste those names out of the plan output and into your terraform import command. This also allows you to validate each resource you think you have configured via for_each. That should be your first step to see why the module.vpc.aws_vpc_peering_connection.peering_connections["preprod"] doesn't exist like you expect it to.

also, I am unable to debug the output with terraform output where I get the following warning

That's because the output only exists inside the module, and terraform output only gives you the top-level outputs. Somewhere at the top-level of your Terraform code, like inside the file with the module "vpc" { ... } declaration, you need to add a top-level output:

```<code>output op_vpc_peering_connections {
  value = module.vpc.op_vpc_peering_connections
}
</code>```

Body:
I have a terraform module as follows

module "vpc" {
  source                      = "./modules"
  my_aws_account_id           = "<aws_account_id>"
  my_aws_vpc_id               = "<aws_vpc_id>"
  my_vpc_peering_connections  = {
    stage    = "<aws_stage_vpc_id>"
    preprod  = "<aws_preprod_vpc_id>"
  }
  my_vpc_route_table          = {
    default   = {
      name    = "test-default"
      routes  = [{
        cidr_block                  = "10.10.16.0/20"
        vpc_peering_connection_id   = module.vpc.op_vpc_peering_connections["stage"]
      }, {
        cidr_block                  = "10.10.32.0/20"
        vpc_peering_connection_id   = module.vpc.op_vpc_peering_connections["preprod"]
      }]
    }
  }
}

variable

variable "my_aws_account_id" {
  type  = string
}

variable "my_aws_vpc_id" {
  type  = string
}

variable "my_vpc_peering_connections" {
  type  = map(string)
}

variable "my_vpc_route_table" {
  type  = object({
    default = object({
      name    = string
      routes  = list(object({
        cidr_block                  = string
        vpc_peering_connection_id   = string
      }))
    })
  })
}

resource

resource "aws_vpc_peering_connection" "peering_connections" {
  for_each      = var.my_vpc_peering_connections

  peer_owner_id = var.my_aws_account_id
  peer_vpc_id   = each.value
  vpc_id        = var.my_aws_vpc_id
  tags          = {
    Name = each.key
  }

  timeouts {}
}

resource "aws_route_table" "default_route_table" {
  vpc_id    = var.my_aws_vpc_id
  tags      = {
    Name = var.my_vpc_route_table.default.name
  }

  dynamic "route" {
    for_each = var.my_vpc_route_table.default.routes
    content {
      cidr_block                  = lookup(route.value, "cidr_block")
      vpc_peering_connection_id   = lookup(route.value, "vpc_peering_connection_id")

      instance_id                 = ""
      gateway_id                  = ""
      carrier_gateway_id          = ""
      destination_prefix_list_id  = ""
      egress_only_gateway_id      = ""
      ipv6_cidr_block             = ""
      local_gateway_id            = ""
      nat_gateway_id              = ""
      network_interface_id        = ""
      transit_gateway_id          = ""
      vpc_endpoint_id             = ""
    }
  }

  depends_on = [
    aws_vpc_peering_connection.peering_connections
  ]
}

output

output op_vpc_peering_connections {
  value = tomap({
    for name, details in aws_vpc_peering_connection.peering_connections : name => details.id
  })
}

but when I run the following commands the first one success and the second one fails with an error

> terraform import module.vpc.aws_vpc_peering_connection.peering_connections["stage"] <peering_connection_id>
> terraform import module.vpc.aws_vpc_peering_connection.peering_connections["preprod"] <peering_connection_id>

error

╷
│ Error: Invalid index
│
│   on /Users/sarad/workspace/personal/terraform-for-each/main.tf line 33, in module "vpc":
│   33:         vpc_peering_connection_id   = module.vpc.op_vpc_peering_connections["preprod"]
│     ├────────────────
│     │ module.vpc.op_vpc_peering_connections is map of string with 1 element
│
│ The given key does not identify an element in this collection value.
╵

the error goes comes if I have more than 1 element in op_vpc_peering_connections and I am trying to reference them in module by name like module.vpc.op_vpc_peering_connections["stage"] or module.vpc.op_vpc_peering_connections["preprod"]

also, I am unable to debug the output with terraform output where I get the following warning

╷
│ Warning: No outputs found
│
│ The state file either has no outputs defined, or all the defined outputs are empty. Please define an output in your configuration with the `output` keyword and run `terraform
│ refresh` for it to become available. If you are using interpolation, please verify the interpolated value is not empty. You can use the `terraform console` command to assist.
╵

Body:
If you first run a terraform plan you will see the exact name of each resource. At which point all you have to do is copy/paste those names out of the plan output and into your terraform import command. This also allows you to validate each resource you think you have configured via for_each. That should be your first step to see why the module.vpc.aws_vpc_peering_connection.peering_connections["preprod"] doesn't exist like you expect it to.

also, I am unable to debug the output with terraform output where I get the following warning

That's because the output only exists inside the module, and terraform output only gives you the top-level outputs. Somewhere at the top-level of your Terraform code, like inside the file with the module "vpc" { ... } declaration, you need to add a top-level output:

output op_vpc_peering_connections {
  value = module.vpc.op_vpc_peering_connections
}


问:
How to dynamically update Dash chart / table on server side events (without dcc interval)?
I have a question and hope that you can help me.

We need to display data from a handheld measuring device in a Dash chart and table.
The device does not send data regularly but only when the user presses a button on it,
then we have to update the chart and table in the UI.

Of course we could create a API around the device and pull new data every second with dcc interval, but I would like to know if there are other possibilities.

Maybe have an open websocket connection to the device’s API and send new events on that.
But how would the connection to the Dash frontend look like?
Can someone point me into the right direction on this?

答:
If you want to transmit the data via websocket, you could use the WebSocket component. If you only need to transmit data from the server to the client (i.e. not bidirectional communication), an alternative could be to use server sent events (SSE) via the EventSource component. Both components are available in dash-extensions.

To illustrate the latter approach, here is a small Dash app that uses the EventSource component to update a graph when a SSE arrives,

```<code>from dash import Dash, html, Input, Output, dcc
from dash_extensions import EventSource

# The url of server that emits the events.
sse_url = "http://127.0.0.1:8000"
# Create small example app.
app = Dash(__name__)
app.layout = html.Div([dcc.Graph(id="graph"), EventSource(id="sse", url=sse_url)])
# You could also use a normal callback, but client side callbacks yield better performance.
app.clientside_callback(
    """
    function(val) {
        values = val ? JSON.parse(val) : []
        return {data: [{y: values , type: "scatter"}]}
    }
    """,
    Output("graph", "figure"),
    Input("sse", "message"),
)

if __name__ == "__main__":
    app.run_server()
</code>```

While any type of server could be used to emit the SSEs, for completeness, here is a server implementation that emits random data in the format expected by the Dash app,

```<code>import asyncio
import random
import uvicorn
from sse_starlette.sse import EventSourceResponse
from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.middleware.cors import CORSMiddleware

app = Starlette(middleware=[Middleware(CORSMiddleware, allow_origins=['*'])])

async def numbers():
    while True:
        await asyncio.sleep(1)
        yield [random.randrange(200, 1000) for _ in range(10)]

@app.route("/")
async def sse(request):
    generator = numbers()
    return EventSourceResponse(generator)

if __name__ == "__main__":
    uvicorn.run(app, port=8000)
</code>```

Body:
If you want to transmit the data via websocket, you could use the WebSocket component. If you only need to transmit data from the server to the client (i.e. not bidirectional communication), an alternative could be to use server sent events (SSE) via the EventSource component. Both components are available in dash-extensions.

To illustrate the latter approach, here is a small Dash app that uses the EventSource component to update a graph when a SSE arrives,

from dash import Dash, html, Input, Output, dcc
from dash_extensions import EventSource

# The url of server that emits the events.
sse_url = "http://127.0.0.1:8000"
# Create small example app.
app = Dash(__name__)
app.layout = html.Div([dcc.Graph(id="graph"), EventSource(id="sse", url=sse_url)])
# You could also use a normal callback, but client side callbacks yield better performance.
app.clientside_callback(
    """
    function(val) {
        values = val ? JSON.parse(val) : []
        return {data: [{y: values , type: "scatter"}]}
    }
    """,
    Output("graph", "figure"),
    Input("sse", "message"),
)

if __name__ == "__main__":
    app.run_server()

While any type of server could be used to emit the SSEs, for completeness, here is a server implementation that emits random data in the format expected by the Dash app,

import asyncio
import random
import uvicorn
from sse_starlette.sse import EventSourceResponse
from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.middleware.cors import CORSMiddleware

app = Starlette(middleware=[Middleware(CORSMiddleware, allow_origins=['*'])])

async def numbers():
    while True:
        await asyncio.sleep(1)
        yield [random.randrange(200, 1000) for _ in range(10)]

@app.route("/")
async def sse(request):
    generator = numbers()
    return EventSourceResponse(generator)

if __name__ == "__main__":
    uvicorn.run(app, port=8000)


问:
Init state issue with "@@redux/init" in Redux
I have a reducer as the following

```<code>const TopLinkReducer = (state = {'currentLink':'Main'}, action) => {
  switch (action.type) {
    case 'switchTopLink':
      return Object.assign({}, state, {
          currentLink: action.id
      })
    default:
      return state
  }
}

export default TopLinkReducer
</code>```

and my goal is the provide a initial state as {'currentLink':'Main'} but I expect the {'currentLink':'Main'} should be passed in initially but I got "@@redux/init" and something else...

What did I missing?

thanks

答:
In my case, It was in the difference between the store initial State.

import { createStore, combineReducers, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import logger from 'redux-logger';
import { useSelector } from 'react-redux';

import { composeWithDevTools } from 'redux-devtools-extension';
import reducers from './reducers';
import storeInitialState from './initialState';

const store = createStore(
    rootReducer,
    storeInitialState,
  //^^^^^^^^^^^^^^^^^
    composeWithDevTools(applyMiddleware(...middleware))
);
```
<p>Vs the <strong>reducer initial state</strong>.</p>
<pre class="lang-js prettyprint-override"><code>import {
    handleReturningUserReducerInitialState,
} from '../initialState';

export const userReducer = (
    state = handleReturningUserReducerInitialState(),
          //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         // It returns the same result as storeInitialState except It was checking for the values in the localStorage
    action
) => {
...
}
</code>```

They were overwriting each other.

Body:
I have a reducer as the following

const TopLinkReducer = (state = {'currentLink':'Main'}, action) => {
  switch (action.type) {
    case 'switchTopLink':
      return Object.assign({}, state, {
          currentLink: action.id
      })
    default:
      return state
  }
}

export default TopLinkReducer

and my goal is the provide a initial state as {'currentLink':'Main'} but I expect the {'currentLink':'Main'} should be passed in initially but I got "@@redux/init" and something else...

What did I missing?

thanks

Body:
In my case, It was in the difference between the store initial State.

import { createStore, combineReducers, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import logger from 'redux-logger';
import { useSelector } from 'react-redux';

import { composeWithDevTools } from 'redux-devtools-extension';
import reducers from './reducers';
import storeInitialState from './initialState';

const store = createStore(
    rootReducer,
    storeInitialState,
  //^^^^^^^^^^^^^^^^^
    composeWithDevTools(applyMiddleware(...middleware))
);

Vs the reducer initial state.

import {
    handleReturningUserReducerInitialState,
} from '../initialState';

export const userReducer = (
    state = handleReturningUserReducerInitialState(),
          //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         // It returns the same result as storeInitialState except It was checking for the values in the localStorage
    action
) => {
...
}

They were overwriting each other.


问:
Standard deviation in ggplot for separate variables
I am trying to plot the standard deviation(SD) of two separate variables in ggplot with stat_summary.

I am able to do so with this code:

```<code>Figure1 <-ggplot(data.frame, aes(x = time, y = output, group= parameter, color=parameter,
shape= parameter)) + scale_color_manual(values=c("forestgreen", "purple4")) +
stat_summary(fun = "mean",
           geom = "pointrange",
           fun.max = function(x) mean(x) + sd(x), group ="parameter",
           fun.min = function(x) mean(x) - sd(x), size = 0.5)
</code>```

However, I would like to put the SD lines upwards for one variable and down for the other. Whenever I make an change to the code it treats both variables the same (i.e they both show SD lines up or down). For example below, both lines are now upwards.

```<code>Figure1 <-ggplot(data.frame, aes(x = time, y = output, group= parameter, color=parameter,
shape= parameter)) + scale_color_manual(values=c("forestgreen", "purple4")) +
stat_summary(fun = "mean",
           geom = "pointrange",
           fun.max = function(x) mean(x) + sd(x), group ="parameter",
           fun.min = function(x) mean(x), size = 0.5)
</code>```

I would like to separate based on "parameter"

Any help is appreciated, thanks.

答:
You do not share your data with us. So I was forced to generate them myself.

```<code>library(tidyverse)

df = tibble(
  parameter = "A",
  time = seq(0,100,5),
) %>% bind_rows(
  tibble(
    parameter = "B",
    time = seq(10,120,8),
  )
) %>% rowwise(time) %>%
  mutate(output = list(rnorm(sample(5:10,1), rnorm(1,0,10), sample(1:5,1)))) %>%
  unnest(output)
</code>```

output

```<code># A tibble: 269 x 3
# Groups:   time [32]
   parameter  time output
   <chr>     <dbl>  <dbl>
 1 A             0   8.22
 2 A             0   1.76
 3 A             0   5.11
 4 A             0   3.68
 5 A             0  11.2
 6 A             0   7.40
 7 A             0   3.83
 8 A             5 -21.4
 9 A             5 -20.8
10 A             5 -20.7
# ... with 259 more rows
</code>```

My data looks like this

```<code>df %>% ggplot(aes(time, output,color=parameter, shape= parameter))+
  geom_point(size =2)+
  scale_color_manual(values=c("forestgreen", "purple4"))
</code>```

Now let's make the graph you expect (or at least that's how I understood it)

```<code>df %>% ggplot(aes(time, output, group=parameter, color=parameter, shape= parameter)) +
  scale_color_manual(values=c("forestgreen", "purple4")) +
  stat_summary(
    data = df %>% filter(parameter =="A"),
    fun = mean,
    fun.max = function(x) mean(x) + sd(x), group ="parameter",
    fun.min = function(x) mean(x), size = 0.5)+
  stat_summary(
    data = df %>% filter(parameter =="B"),
    fun = mean,
    fun.max = function(x) mean(x), group ="parameter",
    fun.min = function(x) mean(x) - sd(x), size = 0.5)
</code>```

As you can see, it was enough to call the stat_summary function only twice, each time giving it a different data range data = df %>% filter(parameter == "A") or data = df %>% filter(parameter == "B").

Our graph can possibly be supplemented with data points

```<code>df %>% ggplot(aes(time, output, group=parameter, color=parameter, shape= parameter)) +
  geom_point(size=1.2, alpha = .7)+
  scale_color_manual(values=c("forestgreen", "purple4")) +
  stat_summary(
    data = df %>% filter(parameter =="A"),
    fun = mean,
    fun.max = function(x) mean(x) + sd(x), group ="parameter",
    fun.min = function(x) mean(x), size = 0.5)+
  stat_summary(
    data = df %>% filter(parameter =="B"),
    fun = mean,
    fun.max = function(x) mean(x), group ="parameter",
    fun.min = function(x) mean(x) - sd(x), size = 0.5)
</code>```

Hope this is the solution you have been looking for.

Body:
I am trying to plot the standard deviation(SD) of two separate variables in ggplot with stat_summary.

I am able to do so with this code:

Figure1 <-ggplot(data.frame, aes(x = time, y = output, group= parameter, color=parameter,
shape= parameter)) + scale_color_manual(values=c("forestgreen", "purple4")) +
stat_summary(fun = "mean",
           geom = "pointrange",
           fun.max = function(x) mean(x) + sd(x), group ="parameter",
           fun.min = function(x) mean(x) - sd(x), size = 0.5)

However, I would like to put the SD lines upwards for one variable and down for the other. Whenever I make an change to the code it treats both variables the same (i.e they both show SD lines up or down). For example below, both lines are now upwards.

Figure1 <-ggplot(data.frame, aes(x = time, y = output, group= parameter, color=parameter,
shape= parameter)) + scale_color_manual(values=c("forestgreen", "purple4")) +
stat_summary(fun = "mean",
           geom = "pointrange",
           fun.max = function(x) mean(x) + sd(x), group ="parameter",
           fun.min = function(x) mean(x), size = 0.5)

I would like to separate based on "parameter"

Any help is appreciated, thanks.

Body:
You do not share your data with us. So I was forced to generate them myself.

library(tidyverse)

df = tibble(
  parameter = "A",
  time = seq(0,100,5),
) %>% bind_rows(
  tibble(
    parameter = "B",
    time = seq(10,120,8),
  )
) %>% rowwise(time) %>%
  mutate(output = list(rnorm(sample(5:10,1), rnorm(1,0,10), sample(1:5,1)))) %>%
  unnest(output)

output

# A tibble: 269 x 3
# Groups:   time [32]
   parameter  time output
   <chr>     <dbl>  <dbl>
 1 A             0   8.22
 2 A             0   1.76
 3 A             0   5.11
 4 A             0   3.68
 5 A             0  11.2
 6 A             0   7.40
 7 A             0   3.83
 8 A             5 -21.4
 9 A             5 -20.8
10 A             5 -20.7
# ... with 259 more rows

My data looks like this

df %>% ggplot(aes(time, output,color=parameter, shape= parameter))+
  geom_point(size =2)+
  scale_color_manual(values=c("forestgreen", "purple4"))

Now let's make the graph you expect (or at least that's how I understood it)

df %>% ggplot(aes(time, output, group=parameter, color=parameter, shape= parameter)) +
  scale_color_manual(values=c("forestgreen", "purple4")) +
  stat_summary(
    data = df %>% filter(parameter =="A"),
    fun = mean,
    fun.max = function(x) mean(x) + sd(x), group ="parameter",
    fun.min = function(x) mean(x), size = 0.5)+
  stat_summary(
    data = df %>% filter(parameter =="B"),
    fun = mean,
    fun.max = function(x) mean(x), group ="parameter",
    fun.min = function(x) mean(x) - sd(x), size = 0.5)

As you can see, it was enough to call the stat_summary function only twice, each time giving it a different data range data = df %>% filter(parameter == "A") or data = df %>% filter(parameter == "B").

Our graph can possibly be supplemented with data points

df %>% ggplot(aes(time, output, group=parameter, color=parameter, shape= parameter)) +
  geom_point(size=1.2, alpha = .7)+
  scale_color_manual(values=c("forestgreen", "purple4")) +
  stat_summary(
    data = df %>% filter(parameter =="A"),
    fun = mean,
    fun.max = function(x) mean(x) + sd(x), group ="parameter",
    fun.min = function(x) mean(x), size = 0.5)+
  stat_summary(
    data = df %>% filter(parameter =="B"),
    fun = mean,
    fun.max = function(x) mean(x), group ="parameter",
    fun.min = function(x) mean(x) - sd(x), size = 0.5)

Hope this is the solution you have been looking for.


问:
Why is cut not matching any delimiters?
The following is not working properly, cut is not separating at the dot but {} definitely contains a dot in the file name.
find . -type f -exec echo ffmpeg -i {} -vcodec copy -acodec copy $(echo {} | cut -d "." -s -f 1).mp4 \;

答:
Bash performs command substitution before the command is run, you have
to put in single quotes to work. The command you want to run is
complex enough to call it with a separate instance of sh like that:

```<code>find . -type f -exec sh -c 'echo ffmpeg -i "$1" -vcodec copy -acodec copy $(echo "$1" | sed "s,^\./,," | cut -d "." -s -f 1).mp4' sh {} \;
</code>```

For example, if the directory in which you want to run the command
looked like that:

```<code>$ tree
.
├── dir
│   └── file_something.avi
├── file_a.avi
├── file_b.avi
├── file_c.avi
└── find.sh

1 directory, 5 files
</code>```

it would return:

```<code>ffmpeg -i ./file_c.avi -vcodec copy -acodec copy file_c.mp4
ffmpeg -i ./find.sh -vcodec copy -acodec copy find.mp4
ffmpeg -i ./file_b.avi -vcodec copy -acodec copy file_b.mp4
ffmpeg -i ./file_a.avi -vcodec copy -acodec copy file_a.mp4
ffmpeg -i ./dir/file_something.avi -vcodec copy -acodec copy dir/file_something.mp4
</code>```

Title: Why is cut not matching any delimiters?

Body:
Bash performs command substitution before the command is run, you have
to put in single quotes to work. The command you want to run is
complex enough to call it with a separate instance of sh like that:

find . -type f -exec sh -c 'echo ffmpeg -i "$1" -vcodec copy -acodec copy $(echo "$1" | sed "s,^\./,," | cut -d "." -s -f 1).mp4' sh {} \;

For example, if the directory in which you want to run the command
looked like that:

$ tree
.
├── dir
│   └── file_something.avi
├── file_a.avi
├── file_b.avi
├── file_c.avi
└── find.sh

1 directory, 5 files

it would return:

ffmpeg -i ./file_c.avi -vcodec copy -acodec copy file_c.mp4
ffmpeg -i ./find.sh -vcodec copy -acodec copy find.mp4
ffmpeg -i ./file_b.avi -vcodec copy -acodec copy file_b.mp4
ffmpeg -i ./file_a.avi -vcodec copy -acodec copy file_a.mp4
ffmpeg -i ./dir/file_something.avi -vcodec copy -acodec copy dir/file_something.mp4


问:
Pandas dataframe, get the row and index for a column meeting certain conditions
I have the below df:

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

output =  [['Owner', 'Database', 'Schema', 'Table', 'Column', 'Comment', 'Status'], ['', 'DEV', 'AIRFLOW', 'TASK_INSTANCE', '_LOAD_DATETIME', 'Load datetime'], ['', 'DEV', 'AIRFLOW', 'TEST', '_LOAD_FILENAME', 'load file name', 'ADDED'],['', 'DEV', 'AIRFLOW', 'TEST_TABLE', 'TEST_COL', 'COMMENT TEST'],]

df = pd.DataFrame(output[1:], columns=output[0])

query_list = []
empty_status_idx = []

for index, row in df.iterrows():
    if row['Status'] is None:
        sql = f"ALTER TABLE {row['Table']} ALTER {row['Column']} COMMENT {row['Comment']}; "
        # idx = np.where(df["Status"] is None)
        # idx = df.index[df['Status']]
        idx = df.iloc[df['Status']]
        empty_status_idx.append(idx)
        print(f'idx: {idx}')

        query_list.append(sql)
query_list
</code>```

I see the below error with idx:

TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

What I want to see if a list of the positions of None cells:

```<code>empty_status_idx = [0, 2]
</code>```

The above idx value I got from some of the answers from this stack overflow question

答:
You can also use a np.where method as follows:

import numpy as np
empty_status_idx = np.where(df.Status.isnull())[0].tolist()
```
<hr />
<pre class="lang-py prettyprint-override"><code>[0, 2]
</code>```

Body:
I have the below df:

import pandas as pd
import numpy as np

output =  [['Owner', 'Database', 'Schema', 'Table', 'Column', 'Comment', 'Status'], ['', 'DEV', 'AIRFLOW', 'TASK_INSTANCE', '_LOAD_DATETIME', 'Load datetime'], ['', 'DEV', 'AIRFLOW', 'TEST', '_LOAD_FILENAME', 'load file name', 'ADDED'],['', 'DEV', 'AIRFLOW', 'TEST_TABLE', 'TEST_COL', 'COMMENT TEST'],]

df = pd.DataFrame(output[1:], columns=output[0])

query_list = []
empty_status_idx = []

for index, row in df.iterrows():
    if row['Status'] is None:
        sql = f"ALTER TABLE {row['Table']} ALTER {row['Column']} COMMENT {row['Comment']}; "
        # idx = np.where(df["Status"] is None)
        # idx = df.index[df['Status']]
        idx = df.iloc[df['Status']]
        empty_status_idx.append(idx)
        print(f'idx: {idx}')

        query_list.append(sql)
query_list

I see the below error with idx:

TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

What I want to see if a list of the positions of None cells:

empty_status_idx = [0, 2]

The above idx value I got from some of the answers from this stack overflow question

Body:
You can also use a np.where method as follows:

import numpy as np
empty_status_idx = np.where(df.Status.isnull())[0].tolist()

[0, 2]


问:
Why does my asynchronous function not complete all execution of its code?
I am just starting to learn about asynchronous programming, in particular the async and await syntax and usage of the asyncio module.

My question is regarding the output of the following code:

```<code>import asyncio

async def foo():
    print('start fetching')
    await asyncio.sleep(2)
    print('done fetching')
    return {'data':1}

async def boo():
    for i in range(10):
        print(i)
        await asyncio.sleep(0.25)

async def main():
    task1 = asyncio.create_task(foo())
    task2 = asyncio.create_task(boo())

asyncio.run(main())
</code>```

The output that I get is:

start fetching
0
```

While I understand what each individual part of the code does, I can't understand why only two lines of the code is being outputted. Here is my understanding:

We start with task1 and print start fetching.

Then we hit the await for 2 seconds.

In these two seconds doesn't this give task2 the opportunity to do its execution for 2 seconds, so 0-8 will be printed?

Then I also don't see why we don't continue executing the function foo()? – i.e. print('done fetching')?

答:
asyncio.run() has two extra event loop iterations internally, they are used for shutdown:

```<code>    loop.run_until_complete(loop.shutdown_asyncgens())
    loop.run_until_complete(loop.shutdown_default_executor())
</code>```

Running these calls gives previously scheduled tasks a chance to iterate over up to 2 times.

Note: this is an implementation detail that may be changed at any time (the number of iterations for startup/shutdown of the event loop can be modified in future Python versions).

Please don't rely on this behavior in your code, but explicitly await scheduled tasks to correctly process them.

Body:
I am just starting to learn about asynchronous programming, in particular the async and await syntax and usage of the asyncio module.

My question is regarding the output of the following code:

import asyncio

async def foo():
    print('start fetching')
    await asyncio.sleep(2)
    print('done fetching')
    return {'data':1}

async def boo():
    for i in range(10):
        print(i)
        await asyncio.sleep(0.25)

async def main():
    task1 = asyncio.create_task(foo())
    task2 = asyncio.create_task(boo())

asyncio.run(main())

The output that I get is:

start fetching
0

While I understand what each individual part of the code does, I can't understand why only two lines of the code is being outputted. Here is my understanding:

We start with task1 and print start fetching.

Then we hit the await for 2 seconds.

In these two seconds doesn't this give task2 the opportunity to do its execution for 2 seconds, so 0-8 will be printed?

Then I also don't see why we don't continue executing the function foo()? – i.e. print('done fetching')?

Body:
asyncio.run() has two extra event loop iterations internally, they are used for shutdown:

    loop.run_until_complete(loop.shutdown_asyncgens())
    loop.run_until_complete(loop.shutdown_default_executor())

Running these calls gives previously scheduled tasks a chance to iterate over up to 2 times.

Note: this is an implementation detail that may be changed at any time (the number of iterations for startup/shutdown of the event loop can be modified in future Python versions).

Please don't rely on this behavior in your code, but explicitly await scheduled tasks to correctly process them.


问:
How to perform deep clone with React setState?
I'm trying to deep clone a BST object with its own class and functions and use it's functions to rerender in a React component after I call its functions. However, React setState doesn't support deep clone so the functions are not passed in the state.

How can I achieve/fix this?

```<code>const runBuildTree = (n) => {
  const BST = new BinarySearchTree();
  for (var i = 0; i < n; i++) {
    BST.insert(Math.ceil(Math.random() * 100));
    setOutput(structuredClone(BST));
  }
  setOutput(structuredClone(BST));
};

const handleDelete = () => {
  output.remove(delNode);
};

useEffect(() => {
  //rendering BST with dependency of output
}, [output]);
;
</code>```

答:
Do whatever you want o do with BST, and when you are done, then change the state

```<code>const runBuildTree = (n) => {
  const BST = new BinarySearchTree();
  for (var i = 0; i < n; i++) {
    BST.insert(Math.ceil(Math.random() * 100));
    // setOutput(structuredClone(BST));
  }
  setOutput(structuredClone(BST)); // after you are done then setState
};
</code>```

Body:
I'm trying to deep clone a BST object with its own class and functions and use it's functions to rerender in a React component after I call its functions. However, React setState doesn't support deep clone so the functions are not passed in the state.

How can I achieve/fix this?

const runBuildTree = (n) => {
  const BST = new BinarySearchTree();
  for (var i = 0; i < n; i++) {
    BST.insert(Math.ceil(Math.random() * 100));
    setOutput(structuredClone(BST));
  }
  setOutput(structuredClone(BST));
};

const handleDelete = () => {
  output.remove(delNode);
};

useEffect(() => {
  //rendering BST with dependency of output
}, [output]);
;

Body:
Do whatever you want o do with BST, and when you are done, then change the state

const runBuildTree = (n) => {
  const BST = new BinarySearchTree();
  for (var i = 0; i < n; i++) {
    BST.insert(Math.ceil(Math.random() * 100));
    // setOutput(structuredClone(BST));
  }
  setOutput(structuredClone(BST)); // after you are done then setState
};


问:
How should i use grep in docker-compose healthcheck?
I'm trying to add healthcheck for my docker app by grep the curl result.
When i run into docker container:

```<code>curl -sS http://nginx | grep -c works > /dev/null
echo $?
</code>```

I getting 0 and everything seems fine, but if I'm trying to add it into docker-compose

```<code>version: "3.8"

services:
  php:
    build: .
    volumes:
     - ./www:/var/www
    healthcheck:
     test: ["CMD", "curl", "-sS", "http://nginx | grep", "-c", "works > /dev/null"]
     interval: 5s
     timeout: 5s
     retries: 5
  nginx:
    image: "nginx:latest"
    ports:
     - "8080:80"
    volumes:
     - ./nginx:/etc/nginx/conf.d
     - ./www:/var/www
    links:
     - php
</code>```

and check it by docker-compose ps it has status "unhealthy". What am I doing wrong?

答:
I guess your problem is the pipe operator. Afaik you also dont need the CMD.

You can try it like this.

test: curl -sS http://nginx | grep -c works > /dev/null
```
<p>You can also check what your health check is actually doing.</p>
<pre class="lang-sh prettyprint-override"><code>docker inspect myproject_php_1 --format \
  '{{range .State.Health.Log}}{{.End}} | Exit Code: {{.ExitCode}} | {{.Output}}{{end}}'
</code>```

Body:
I'm trying to add healthcheck for my docker app by grep the curl result.
When i run into docker container:

curl -sS http://nginx | grep -c works > /dev/null
echo $?

I getting 0 and everything seems fine, but if I'm trying to add it into docker-compose

version: "3.8"

services:
  php:
    build: .
    volumes:
     - ./www:/var/www
    healthcheck:
     test: ["CMD", "curl", "-sS", "http://nginx | grep", "-c", "works > /dev/null"]
     interval: 5s
     timeout: 5s
     retries: 5
  nginx:
    image: "nginx:latest"
    ports:
     - "8080:80"
    volumes:
     - ./nginx:/etc/nginx/conf.d
     - ./www:/var/www
    links:
     - php

and check it by docker-compose ps it has status "unhealthy". What am I doing wrong?

Body:
I guess your problem is the pipe operator. Afaik you also dont need the CMD.

You can try it like this.

test: curl -sS http://nginx | grep -c works > /dev/null

You can also check what your health check is actually doing.

docker inspect myproject_php_1 --format \
  '{{range .State.Health.Log}}{{.End}} | Exit Code: {{.ExitCode}} | {{.Output}}{{end}}'


问:
Pausing entire container when concurrency is greater than 1
When we are using a container with concurrency greater than 1, and wish to pause all the child containers, can we pause directly using:

```<code>MessageListenerContainer mlc= registry.getListenerContainer(<id>);
mlc.pause();
</code>```

Or do we have to cast it to a Concurrent Message Listener Container and pause each container individually?

Thanks in advance.

答:
If you're using @KafkaListener(id = "your-id",...) for your method, it will be registered as an instance of ConcurrentMessageListenerContainer. So when you call mlc.pause(), it actually is ConcurrentMessageListenerContainer#pause, you don't have to cast it (polymorphism).

With ConcurrentMessageListenerContainer#pause, it will delegate to all KafkaMessageListenerContainer#pauses inside it (for example: concurrency = 3, you will have 3 instances of KafkaMessageListenerContainer). You don't have to pause each container individually, mlc.pause() is enough.

Body:
When we are using a container with concurrency greater than 1, and wish to pause all the child containers, can we pause directly using:

MessageListenerContainer mlc= registry.getListenerContainer(<id>);
mlc.pause();

Or do we have to cast it to a Concurrent Message Listener Container and pause each container individually?

Thanks in advance.


问:
What is the role of program interpreters in executable files?
I was going through disassembly of elf executables and understanding the elf format. In there, I saw lib64/ld-linux-x86-64.so.2 used as program interpreter in the generated executable.

My guess is: I had used printf in the source code, which had to be dynamically linked. When I checked through dynamic section, I was able to find a reference to libc.so.6 shared library (tag:DT_NEEDED). In my system, I found multiple files with that name in different directories:

```<code>sourav@ubuntu-VirtualBox:/$ sudo find / -name libc.so.6
/usr/lib/x86_64-linux-gnu/libc.so.6
find: ‘/run/user/1000/doc’: Permission denied
find: ‘/run/user/1000/gvfs’: Permission denied
/snap/snapd/13170/lib/x86_64-linux-gnu/libc.so.6
/snap/snapd/11107/lib/x86_64-linux-gnu/libc.so.6
/snap/core18/1988/lib/i386-linux-gnu/libc.so.6
/snap/core18/1988/lib/x86_64-linux-gnu/libc.so.6
/snap/core18/2128/lib/i386-linux-gnu/libc.so.6
/snap/core18/2128/lib/x86_64-linux-gnu/libc.so.6
</code>```

So, I guess purpose of program interpreter is to resolve these names to the proper libraries and load them during execution. Is this correct?

It seems, we can also have executables with no program interpreter (which is the case for program interpreter itself). In that case, does system/os itself loads the shared library? If so, how does it resolves the path of library?

Is it possible to generate executable with no program interpreter using gcc? My gcc version is 'gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)'.

答:
So, I guess purpose of program interpreter is to resolve these names to the proper libraries and load them during execution. Is this correct?

Yes, but that that's a bit minimalistic.  Loading dynamic libraries involves locating them, loading or mapping them into memory if necessary, and resolving dynamic symbols within, possibly lazily, for multiple kinds of relocations.  It involves recursively loading the libraries' own needed libraries.  Also, in a dynamically linked executable, the program interpreter provides the program entry point (from the kernel's perspective), so it is also responsible for setting up and entering the program-specific entry point (for example, main() in a C or C++ program).

It seems, we can also have executables with no program interpreter (which is the case for program interpreter itself). In that case, does system/os itself loads the shared library? If so, how does it resolves the path of library?

You can have ELF executables without a program interpreter, but they are not dynamically linked, at least not in the ELF sense.  There are no shared libraries to load, and certainly the system does not load any.

Is it possible to generate executable with no program interpreter using gcc? My gcc version is 'gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)'.

If you have static versions of all needed libraries available then you should be able to achieve that by including the -static option on the command line when you link the program.  It is entirely possible, however, that you do not have the needed static libraries, even if libc is the only library you need.

Body:
I was going through disassembly of elf executables and understanding the elf format. In there, I saw lib64/ld-linux-x86-64.so.2 used as program interpreter in the generated executable.

My guess is: I had used printf in the source code, which had to be dynamically linked. When I checked through dynamic section, I was able to find a reference to libc.so.6 shared library (tag:DT_NEEDED). In my system, I found multiple files with that name in different directories:

sourav@ubuntu-VirtualBox:/$ sudo find / -name libc.so.6
/usr/lib/x86_64-linux-gnu/libc.so.6
find: ‘/run/user/1000/doc’: Permission denied
find: ‘/run/user/1000/gvfs’: Permission denied
/snap/snapd/13170/lib/x86_64-linux-gnu/libc.so.6
/snap/snapd/11107/lib/x86_64-linux-gnu/libc.so.6
/snap/core18/1988/lib/i386-linux-gnu/libc.so.6
/snap/core18/1988/lib/x86_64-linux-gnu/libc.so.6
/snap/core18/2128/lib/i386-linux-gnu/libc.so.6
/snap/core18/2128/lib/x86_64-linux-gnu/libc.so.6

So, I guess purpose of program interpreter is to resolve these names to the proper libraries and load them during execution. Is this correct?

It seems, we can also have executables with no program interpreter (which is the case for program interpreter itself). In that case, does system/os itself loads the shared library? If so, how does it resolves the path of library?

Is it possible to generate executable with no program interpreter using gcc? My gcc version is 'gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)'.


问:
Linux server programming
I got this code from Andy Tanenbaum's book. I am trying to run it. It compiles and waits for connection. But when I type localhost:886, I see no effect either in the browser or the terminal. (It should echo a new connection string as per the code).

```<code>#define     SERVER_PORT         886     /* The port at which the server listens */
#define     BUF_SIZE        64032       /* The size of buffer for request and response */
#define     QUEUE_SIZE      10      /* Block transfer size */

int main (int argc, char* argv[]) {

    int     sock = 0, bnd = 0, lst = 0, fd = 0, sa = 0, bytes = 0, on = 1, conn_count = 0;
    char    buf[BUF_SIZE];
    struct  sockaddr_in channel;            /* IP address holder */

    /* The address structure that binds with the socket */

    memset (&channel, 0, sizeof(channel));

    channel.sin_family      =   AF_INET;
    channel.sin_addr.s_addr     =   htonl (INADDR_ANY);
    channel.sin_port        =   htons (SERVER_PORT);

    /* Parital opening of the socket, while waiting for connection */

    sock                =   socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); /* Creates a new socket */
    if (sock < 0)
        printf ("Partial opening of the socket failed! Error: %d", sock);

    setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on));

    bnd             =   bind (sock, (struct sockaddr *) &channel, sizeof(channel));
    if (bnd < 0)
        printf ("Binding failure! Error: %d", bnd);

    lst             =   listen (sock, QUEUE_SIZE);
    if (lst < 0)
        printf ("Unable to listen on the socket! Error: %d", lst);

    /* The socket has been set-up. Wait for the connection and process it. */

    while (1) {

        conn_count      +=  1;
        printf ("Received connection: %d", conn_count);
        sa          =   accept (sock, 0, 0);
        if (sa < 0)
            puts ("Unable to accept the socket opened for connection.");

        read (sa, buf, BUF_SIZE);

        puts (buf);     /* Output the string to the screen */

        close (sa);
    }
}
</code>```

答:
Try to put a line-break '\n' at the end of your

```<code> printf ("Received connection: %d\n", conn_count);
</code>```

line.

Otherwise the stdout is not flushed directly and you see nothing.

Title: Linux server programming

Body:
I got this code from Andy Tanenbaum's book. I am trying to run it. It compiles and waits for connection. But when I type localhost:886, I see no effect either in the browser or the terminal. (It should echo a new connection string as per the code).

#define     SERVER_PORT         886     /* The port at which the server listens */
#define     BUF_SIZE        64032       /* The size of buffer for request and response */
#define     QUEUE_SIZE      10      /* Block transfer size */

int main (int argc, char* argv[]) {

    int     sock = 0, bnd = 0, lst = 0, fd = 0, sa = 0, bytes = 0, on = 1, conn_count = 0;
    char    buf[BUF_SIZE];
    struct  sockaddr_in channel;            /* IP address holder */

    /* The address structure that binds with the socket */

    memset (&channel, 0, sizeof(channel));

    channel.sin_family      =   AF_INET;
    channel.sin_addr.s_addr     =   htonl (INADDR_ANY);
    channel.sin_port        =   htons (SERVER_PORT);

    /* Parital opening of the socket, while waiting for connection */

    sock                =   socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); /* Creates a new socket */
    if (sock < 0)
        printf ("Partial opening of the socket failed! Error: %d", sock);

    setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on));

    bnd             =   bind (sock, (struct sockaddr *) &channel, sizeof(channel));
    if (bnd < 0)
        printf ("Binding failure! Error: %d", bnd);

    lst             =   listen (sock, QUEUE_SIZE);
    if (lst < 0)
        printf ("Unable to listen on the socket! Error: %d", lst);

    /* The socket has been set-up. Wait for the connection and process it. */

    while (1) {

        conn_count      +=  1;
        printf ("Received connection: %d", conn_count);
        sa          =   accept (sock, 0, 0);
        if (sa < 0)
            puts ("Unable to accept the socket opened for connection.");

        read (sa, buf, BUF_SIZE);

        puts (buf);     /* Output the string to the screen */

        close (sa);
    }
}

Body:
Try to put a line-break '\n' at the end of your

 printf ("Received connection: %d\n", conn_count);

line.

Otherwise the stdout is not flushed directly and you see nothing.


问:
Is it safe to run terraform init multiple times
I have an entire infrastructure built with terraform aws

While trying to use data "archive_file" for lambda...

the terminal returned this message:

```<code> Plugin reinitialization required. Please run "terraform init".
│
│ Plugins are external binaries that Terraform uses to access and manipulate
│ resources. The configuration provided requires plugins which can't be located,
│ don't satisfy the version constraints, or are otherwise incompatible.
│
│ Terraform automatically discovers provider requirements from your
│ configuration, including providers used in child modules. To see the
│ requirements and constraints, run "terraform providers".
│
│ failed to instantiate provider "registry.terraform.io/hashicorp/archive" to obtain schema: unknown provider "registry.terraform.io/hashicorp/archive"
│

</code>```

I am worried about running terraform init and have some side effects that will probably wipe out the entire infrastructure...

Is it safe to run this command at this point?

答:
Yes, the terraform init command prepares the working directory by downloading provider plugins & modules and configures the backend store. It doesn't touch your infrastructure. See the manual page for more details.

Infrastructure changes are done by the terraform apply and terraform destroy commands.

Body:
I have an entire infrastructure built with terraform aws

While trying to use data "archive_file" for lambda...

the terminal returned this message:

 Plugin reinitialization required. Please run "terraform init".
│
│ Plugins are external binaries that Terraform uses to access and manipulate
│ resources. The configuration provided requires plugins which can't be located,
│ don't satisfy the version constraints, or are otherwise incompatible.
│
│ Terraform automatically discovers provider requirements from your
│ configuration, including providers used in child modules. To see the
│ requirements and constraints, run "terraform providers".
│
│ failed to instantiate provider "registry.terraform.io/hashicorp/archive" to obtain schema: unknown provider "registry.terraform.io/hashicorp/archive"
│

I am worried about running terraform init and have some side effects that will probably wipe out the entire infrastructure...

Is it safe to run this command at this point?


答:
There are two problems here:

You split your arguments in a weird way - "http://nginx | grep" and "works > /dev/null" don't make much sense and will be passed literally to curl

You are using a format in test which does not call the shell - as such, shell fetures like | and > won't work since they need to be interpreted by your shell. You need to either pass test as a string, or use the CMD-SHELL form:

```<code>   healthcheck:
     test: curl -sS http://nginx | grep -c works > /dev/null
     interval: 5s
     timeout: 5s
     retries: 5
</code>```

Or:

```<code>    healthcheck:
     test: ["CMD-SHELL", "curl -sS http://nginx | grep -c works > /dev/null"]
     interval: 5s
     timeout: 5s
     retries: 5
</code>```

For more information on healthcheck, you can read about it in the compose file reference.

Title: How should i use grep in docker-compose healthcheck?

Body:
There are two problems here:

You split your arguments in a weird way - "http://nginx | grep" and "works > /dev/null" don't make much sense and will be passed literally to curl

You are using a format in test which does not call the shell - as such, shell fetures like | and > won't work since they need to be interpreted by your shell. You need to either pass test as a string, or use the CMD-SHELL form:

   healthcheck:
     test: curl -sS http://nginx | grep -c works > /dev/null
     interval: 5s
     timeout: 5s
     retries: 5

Or:

    healthcheck:
     test: ["CMD-SHELL", "curl -sS http://nginx | grep -c works > /dev/null"]
     interval: 5s
     timeout: 5s
     retries: 5

For more information on healthcheck, you can read about it in the compose file reference.


问:
Cannot initiate the connection to repo.mongodb.org:443
I'm following Microsoft's official documentation to install MongoDB in WSL 2.
When I reached the 5th step it returns some error. The step was Reload local package database: sudo apt-get update

Here is the sort form of the error,

```<code>Err:1 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 InRelease
  Cannot initiate the connection to repo.mongodb.org:443 (2600:9000:213a:6a00:1e:e933:9800:93a1). - connect (101: Network is unreachable) Cannot initiate the connection to repo.mongodb.org:443 (2600:9000:213a:6400:1e:e933:9800:93a1). - connect (101: Network is unreachable) Cannot initiate the connection to repo.mongodb.org:443 (2600:9000:213a:f200:1e:e933:9800:93a1).
</code>```

答:
WSL2 doesn't support routing IPv6, and for some reason apt is trying to connect to repo.mongodb.org using IPv6.

Try:

```<code>sudo apt update -o Acquire::ForceIPv4=true
</code>```

This is a workaround (if it works).  You may run into other issues with IPv6, and if so, check out this Ask Ubuntu answer and see if that helps.

Body:
I'm following Microsoft's official documentation to install MongoDB in WSL 2.
When I reached the 5th step it returns some error. The step was Reload local package database: sudo apt-get update

Here is the sort form of the error,

Err:1 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 InRelease
  Cannot initiate the connection to repo.mongodb.org:443 (2600:9000:213a:6a00:1e:e933:9800:93a1). - connect (101: Network is unreachable) Cannot initiate the connection to repo.mongodb.org:443 (2600:9000:213a:6400:1e:e933:9800:93a1). - connect (101: Network is unreachable) Cannot initiate the connection to repo.mongodb.org:443 (2600:9000:213a:f200:1e:e933:9800:93a1).

Body:
WSL2 doesn't support routing IPv6, and for some reason apt is trying to connect to repo.mongodb.org using IPv6.

Try:

sudo apt update -o Acquire::ForceIPv4=true

This is a workaround (if it works).  You may run into other issues with IPv6, and if so, check out this Ask Ubuntu answer and see if that helps.


问:
Node JS Insert text from file to name result file from Puppeteer scrap
I am hacking together a node script in Puppeteer where I scrap a webpage and safe the content as a text file where I name the file after the date and want to add text from a text file at the end of the name.

const puppeteer = require('puppeteer');
const fs = require('fs');

let utc_date_string = new Date().toLocaleString("en-US", { timeZone: "UTC" });
let date_utc = new Date(utc_date_string);
let year = date_utc.getFullYear();
let month = ("0" + (date_utc.getMonth() + 1)).slice(-2);
let date = ("0" + date_utc.getDate()).slice(-2);
let hours = ("0" + date_utc.getHours()).slice(-2);
let minutes = ("0" + date_utc.getMinutes()).slice(-2);
let time_hh_mm = hours +  + minutes;
let data = fs.readFileSync('insert.txt', 'utf8');

(async () => {
  const browser = await puppeteer.launch(
{
    headless: true,
    args: ['--single-process', '--no-zygote', '--no-sandbox']
  })
  const page = await browser.newPage()
await page.goto('https://example.com/xxx.html')
  const myproblem = await page.$eval('table', table => table.innerText)

fs.writeFile('/var/www/mydomain.com/xxx/'+year+'-'+month+'-'+date+'-'+time_hh_mm+'-'+data+'.txt', myproblem)
  await browser.close()
})()
```

<p>This works so far but the problem is that the file name comes out like this</p>
```'2022-02-13-1453-test_text_insert'$'\n''.txt'
```
<p>and not like this</p>
```2022-02-13-1453-test_text_insert.txt
```

If I use only the date, the file name comes out as expected.
I don't know from where the extra characters come from (they are not in the text file) or if there is something wrong with the code.

答:
String error. You should check this line:

```<code>let time_hh_mm = hours +  + minutes;
</code>```

Maybe it should be like this: (?)

```<code>let time_hh_mm = hours + minutes;
</code>```

And you tried to read the file which contain the breakline character \n, you can try to trim it.

```<code>fs.writeFile('/var/www/mydomain.com/xxx/'+year+'-'+month+'-'+date+'-'+time_hh_mm+'-'+data.trim()+'.txt', myproblem)
</code>```

Body:
I am hacking together a node script in Puppeteer where I scrap a webpage and safe the content as a text file where I name the file after the date and want to add text from a text file at the end of the name.

const puppeteer = require('puppeteer');
const fs = require('fs');

let utc_date_string = new Date().toLocaleString("en-US", { timeZone: "UTC" });
let date_utc = new Date(utc_date_string);
let year = date_utc.getFullYear();
let month = ("0" + (date_utc.getMonth() + 1)).slice(-2);
let date = ("0" + date_utc.getDate()).slice(-2);
let hours = ("0" + date_utc.getHours()).slice(-2);
let minutes = ("0" + date_utc.getMinutes()).slice(-2);
let time_hh_mm = hours +  + minutes;
let data = fs.readFileSync('insert.txt', 'utf8');

(async () => {
  const browser = await puppeteer.launch(
{
    headless: true,
    args: ['--single-process', '--no-zygote', '--no-sandbox']
  })
  const page = await browser.newPage()
await page.goto('https://example.com/xxx.html')
  const myproblem = await page.$eval('table', table => table.innerText)

fs.writeFile('/var/www/mydomain.com/xxx/'+year+'-'+month+'-'+date+'-'+time_hh_mm+'-'+data+'.txt', myproblem)
  await browser.close()
})()

This works so far but the problem is that the file name comes out like this

'2022-02-13-1453-test_text_insert'$'\n''.txt'

and not like this

2022-02-13-1453-test_text_insert.txt

If I use only the date, the file name comes out as expected.
I don't know from where the extra characters come from (they are not in the text file) or if there is something wrong with the code.

Body:
String error. You should check this line:

let time_hh_mm = hours +  + minutes;

Maybe it should be like this: (?)

let time_hh_mm = hours + minutes;

And you tried to read the file which contain the breakline character \n, you can try to trim it.

fs.writeFile('/var/www/mydomain.com/xxx/'+year+'-'+month+'-'+date+'-'+time_hh_mm+'-'+data.trim()+'.txt', myproblem)


问:
One of two similar while loops is causing segfault at fread
I am trying to load two files into memory, one as an array of 2d arrays of chars (i.e., a char ***) and the other as a hash table. My whole file is below, and I can share the files in files/ if they are asked for. My goal is to implement a minimax algorithm to solve a game a friend made called WordBord (wordbord.com, and I decided that C would be the most efficient language. If you have suggestions for other languages (I am proficient in Python and know probably enough Java to get this done), please tell me. I am mainly doing this to challenge myself.

Program:

```<code>#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#define BOARDS 730
#define BOARD_SIZE 5
#define NUM_BUCKETS 3001

/*
 * Plan: Board layout:
 * Array of 2d arrays, so a char *** array (lol)
 * Array of strings (words)
 */

struct node {
    struct node *next;
    char *word;
};

char ***boards;
struct node **words;

// Djb2 hash function
// Code from:
// https://gist.github.com/MohamedTaha98/ccdf734f13299efb73ff0b12f7ce429f
unsigned long hash(char *str) {
        unsigned long hash = 5381;
        int c;
        while ((c = *str++))
            hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
        return hash % NUM_BUCKETS;
}

bool isword(char *str) {
    unsigned long int idx = hash(str);
    struct node *node_ptr = words[idx];
    while (node_ptr != NULL) {
        if (!strcmp(node_ptr->word, str)) {
            return true;
        }
        node_ptr = node_ptr->next;
    }
    return false;
}

void add(char *str) {
    // Add string to hash table
    printf("%s\n", str);
}

int main() {
    FILE *boards_file = fopen("files/boards", "r");
    char c[BOARD_SIZE + 1];
    int count = 0;
    int sub_board_count = 0;
    boards = malloc(sizeof(char**) * BOARDS);
    for (int i = 0; i < BOARDS; i++) {
        boards[i] = malloc(sizeof(char*) * BOARD_SIZE);
        for (int j = 0; j < BOARD_SIZE; j++) {
            boards[i][j] = malloc(BOARD_SIZE + 1);
        }
    }

    printf("loading...\n");
    printf("here\n");
    while (fread(&c, 1, BOARD_SIZE, boards_file)) {
        printf("here\n");
        strcat(boards[count][sub_board_count], c);
        printf("here\n");
        printf("%s -> %s (%i, %i)\n", c, boards[count][sub_board_count], count, sub_board_count);

        fseek(boards_file, 1, SEEK_CUR);
        sub_board_count++;
        if (sub_board_count == 5) {
            // We reached the end of the board!
            count++;
            sub_board_count = 0;
        }
    }

    printf("done loading, printing...\n-----\n");
    for (int i = 0; i < BOARDS; i++) {
        for (int j = 0; j < BOARD_SIZE; j++) {
            printf("%s\n", boards[i][j]);
        }
        printf("-----\n");
    }
    fclose(boards_file);
    printf("done loading boards, loading words...\n");

    FILE *words_file = fopen("files/words", "r");
    char c2[BOARD_SIZE + 1];
    printf("here\n");
    while (fread(&c2, 1, BOARD_SIZE, words_file)) {
        printf("here\n");
        add(c2);
    }

    printf("program done, freeing...\n");
    // don't forget to close and free everthing!
    for (int i = 0; i < BOARDS; i++) {
        for (int j = 0; j < BOARD_SIZE; j++) {
            free(boards[i][j]);
        }
        free(boards[i]);
    }
    free(boards);
}
</code>```

When compiled with gcc (full command: gcc -Wall -Werror -Wextra -Wno-sign-compare -Wshadow    wordbord-solver.c  -lcrypt -lm -lgmp -o wordbord-solver (I know I don't need those links but they're useful for other programs, ie make <name>), a segfault occurs on line 95, the second while loop.

Thank you in advance for your time. Apologies if I am being stupid and missing something obvious, but some google digging showed that lines like char c[BOARD_SIZE + 1] worked as well as some heap stuff (e.g. char *c = malloc(BOARD_SIZE + 1); memset(c, 0, BOARD_SIZE + 1);)

答:
Issues

Here are some issues that might be causing your segmentation fault:

Both the buffer c and c2 are declared on the stack, and not null-terminated. You're unsafely assuming they are null-terminated. This can easily be a source of a segfault when you try to printf these buffers (printf always searches for the null character as an indication of the end of the buffer)

You are using strcat in the same way on a non-nulled buffer that you allocate for your 2D boards (strcat(boards[count][sub_board_count], c);). You malloc'd these buffers but that memory isn't guaranteed to be zero. strcat also looks for a null character to indicate where to begin concatenating.

You indefinitely read in boards from the file boards_file, and never check to see if you have capacity. You've fixed your board count at 730 (#define BOARDS 730). If the file contains more, you'll also get a segmentation fault because you don't perform a bounds check.

Fixes

Always zero your stack buffers. You can do this with: char c[N] = {0}; // Zeroes the entire buffer.

Always zero your allocated memory, if you'll be storing strings in them and using C string functions. So either use memset or calloc to allocate them zeroed.

Body:
I am trying to load two files into memory, one as an array of 2d arrays of chars (i.e., a char ***) and the other as a hash table. My whole file is below, and I can share the files in files/ if they are asked for. My goal is to implement a minimax algorithm to solve a game a friend made called WordBord (wordbord.com, and I decided that C would be the most efficient language. If you have suggestions for other languages (I am proficient in Python and know probably enough Java to get this done), please tell me. I am mainly doing this to challenge myself.

Program:

#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#define BOARDS 730
#define BOARD_SIZE 5
#define NUM_BUCKETS 3001

/*
 * Plan: Board layout:
 * Array of 2d arrays, so a char *** array (lol)
 * Array of strings (words)
 */

struct node {
    struct node *next;
    char *word;
};

char ***boards;
struct node **words;

// Djb2 hash function
// Code from:
// https://gist.github.com/MohamedTaha98/ccdf734f13299efb73ff0b12f7ce429f
unsigned long hash(char *str) {
        unsigned long hash = 5381;
        int c;
        while ((c = *str++))
            hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
        return hash % NUM_BUCKETS;
}

bool isword(char *str) {
    unsigned long int idx = hash(str);
    struct node *node_ptr = words[idx];
    while (node_ptr != NULL) {
        if (!strcmp(node_ptr->word, str)) {
            return true;
        }
        node_ptr = node_ptr->next;
    }
    return false;
}

void add(char *str) {
    // Add string to hash table
    printf("%s\n", str);
}

int main() {
    FILE *boards_file = fopen("files/boards", "r");
    char c[BOARD_SIZE + 1];
    int count = 0;
    int sub_board_count = 0;
    boards = malloc(sizeof(char**) * BOARDS);
    for (int i = 0; i < BOARDS; i++) {
        boards[i] = malloc(sizeof(char*) * BOARD_SIZE);
        for (int j = 0; j < BOARD_SIZE; j++) {
            boards[i][j] = malloc(BOARD_SIZE + 1);
        }
    }

    printf("loading...\n");
    printf("here\n");
    while (fread(&c, 1, BOARD_SIZE, boards_file)) {
        printf("here\n");
        strcat(boards[count][sub_board_count], c);
        printf("here\n");
        printf("%s -> %s (%i, %i)\n", c, boards[count][sub_board_count], count, sub_board_count);

        fseek(boards_file, 1, SEEK_CUR);
        sub_board_count++;
        if (sub_board_count == 5) {
            // We reached the end of the board!
            count++;
            sub_board_count = 0;
        }
    }

    printf("done loading, printing...\n-----\n");
    for (int i = 0; i < BOARDS; i++) {
        for (int j = 0; j < BOARD_SIZE; j++) {
            printf("%s\n", boards[i][j]);
        }
        printf("-----\n");
    }
    fclose(boards_file);
    printf("done loading boards, loading words...\n");

    FILE *words_file = fopen("files/words", "r");
    char c2[BOARD_SIZE + 1];
    printf("here\n");
    while (fread(&c2, 1, BOARD_SIZE, words_file)) {
        printf("here\n");
        add(c2);
    }

    printf("program done, freeing...\n");
    // don't forget to close and free everthing!
    for (int i = 0; i < BOARDS; i++) {
        for (int j = 0; j < BOARD_SIZE; j++) {
            free(boards[i][j]);
        }
        free(boards[i]);
    }
    free(boards);
}

When compiled with gcc (full command: gcc -Wall -Werror -Wextra -Wno-sign-compare -Wshadow    wordbord-solver.c  -lcrypt -lm -lgmp -o wordbord-solver (I know I don't need those links but they're useful for other programs, ie make <name>), a segfault occurs on line 95, the second while loop.

Thank you in advance for your time. Apologies if I am being stupid and missing something obvious, but some google digging showed that lines like char c[BOARD_SIZE + 1] worked as well as some heap stuff (e.g. char *c = malloc(BOARD_SIZE + 1); memset(c, 0, BOARD_SIZE + 1);)


问:
How to check if git pull is successful from Process.ExitCode
I am calling git pull using ProcessInfo

I am checking ExitCode for all the commands for there success.

So if ExitCode is 0 the command ran successfully if not there is some error.

All works fine, the problem occurs when it tries to do a git pull and the repository is already up to date.
In that case git considers it a fail and so returns ExitCode=1, but for me it is a success, it is just that there was nothing to be pulled.

Is there any way I can check the success of pull command?

I have one approach in mind that if I can somehow run a command to check if there is anything to be pulled, if that command returns true then I will do a pull else won't

This post gives the option to check if anything is there to be pulled
Check if pull needed in Git

Can somebody help if there is direct way to handle pull alone or what should I check before pull?

答:
Having run git fetch (perhaps with additional options),

Warning: if you are using --prune as an additional option, do use Git 2.36 (Q2 2022)

When "git fetch --prune"(man) failed to prune the refs it wanted to prune, the command issued error messages but exited with exit status 0, which has been corrected with Git 2.36 (Q2 2022).

See commit c9e04d9 (31 Jan 2022) by Thomas Gummerer (tgummerer).

(Merged by Junio C Hamano -- gitster -- in commit c73d46b, 11 Feb 2022)

fetch --prune: exit with error if pruning fails

Helped-by: Johannes Schindelin

Signed-off-by: Thomas Gummerer

When pruning refs fails, we print an error to stderr, but still exit 0 from 'git fetch'(man).

Since this is a genuine error, fetch should be exiting with some non-zero exit code.

Make it so.

The --prune option was introduced in f360d84 ("builtin-fetch: add(man) --prune option", 2009-11-10, Git v1.6.6-rc1 -- merge).

Unfortunately it's unclear from that commit whether ignoring the exit code was an oversight or intentional, but it feels like an oversight.


问:
How to fully fade out contents in canvas
This question has been asked twice without the caveat of "Fully Fade Out"

Fastest way of fading out entire contents of a canvas to transparency, not other color

HTML5: Apply transparency to Canvas after drawing through JavaScript

Both of the accepted answers only partially fade out the contents.  They both suggest something like:

```<code>// semi functional code, but doesn't fully work
ctx.save();
ctx.globalAlpha = 1;
ctx.globalCompositeOperation = "destination-in";
ctx.fillStyle = "rgba(0, 0, 0, 0.9)";
ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
ctx.restore();
</code>```

This leaves residue everywhere, never fully fading out anything.  See example below:

let canvas = document.getElementsByTagName('canvas')[0];
let ctx = canvas.getContext('2d');
let rand = n => Math.floor(Math.random() * n);
setInterval(() => {
  ctx.beginPath();
  ctx.arc(rand(300), rand(120), rand(60), Math.PI * 2, 0);
  ctx.fillStyle = `rgba(${rand(256)}, ${rand(256)}, ${rand(256)}, 1)`;
  ctx.globalCompositeOperation = 'source-over';
  ctx.fill();
}, 1000);

let fadeOut = () => {
  let fadeAmount = 0.05;
  // Note that the colour here doesn't matter! Only the alpha matters.
  // The colour here is red, but you'll see no red appear
  ctx.fillStyle = `rgba(255, 0, 0, ${1 - fadeAmount})`;
  ctx.globalCompositeOperation = 'destination-in';
  ctx.fillRect(0, 0, 300, 120);
  requestAnimationFrame(fadeOut);
};
requestAnimationFrame(fadeOut);```
<pre class="snippet-code-css lang-css prettyprint-override"><code>canvas { border: 3px solid #808080; background-color: #000000; }</code>```

<canvas width="300" height="120"></canvas>```

The question is:  How can one fully fade out elements on a canvas, all the way to transparent?

EDIT: I'm searching for a performant solution that works for heavily layered (think visualizer) situations.

答:
Here is a small sample using globalAlpha, looks good to me, no residue...

each FadingCircle will have own fade, that will determine how fast it fades and if it goes to 0 or below we do not draw it, seems like an easy solution.

You can add colors, random positions and change it as much as you like to suit your needs.

const canvas = document.getElementsByTagName('canvas')[0];
const ctx = canvas.getContext('2d');

class FadingCircle {
  constructor(x, y, radius, fade) {
    this.x = x
    this.y = y
    this.radius = radius
    this.fade = fade
    this.globalAlpha = 1
  }
  draw(ctx) {
    if (this.globalAlpha > 0) {
      ctx.beginPath()
      ctx.globalAlpha = this.globalAlpha
      ctx.arc(this.x, this.y, this.radius, Math.PI * 2, 0)
      ctx.fill()
      this.globalAlpha -= this.fade
    }
  }
}

let sml = new FadingCircle(40, 50, 20, 0.01)
let med = new FadingCircle(140, 50, 30, 0)
let big = new FadingCircle(100, 50, 50, 0.005)

let animation = () => {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  big.draw(ctx)
  med.draw(ctx)
  sml.draw(ctx)
  requestAnimationFrame(animation);
};
requestAnimationFrame(animation);```
<pre class="snippet-code-html lang-html prettyprint-override"><code><canvas width="300" height="120"></canvas></code>```

Body:
This question has been asked twice without the caveat of "Fully Fade Out"

Fastest way of fading out entire contents of a canvas to transparency, not other color

HTML5: Apply transparency to Canvas after drawing through JavaScript

Both of the accepted answers only partially fade out the contents.  They both suggest something like:

// semi functional code, but doesn't fully work
ctx.save();
ctx.globalAlpha = 1;
ctx.globalCompositeOperation = "destination-in";
ctx.fillStyle = "rgba(0, 0, 0, 0.9)";
ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
ctx.restore();

This leaves residue everywhere, never fully fading out anything.  See example below:

let canvas = document.getElementsByTagName('canvas')[0];
let ctx = canvas.getContext('2d');
let rand = n => Math.floor(Math.random() * n);
setInterval(() => {
  ctx.beginPath();
  ctx.arc(rand(300), rand(120), rand(60), Math.PI * 2, 0);
  ctx.fillStyle = `rgba(${rand(256)}, ${rand(256)}, ${rand(256)}, 1)`;
  ctx.globalCompositeOperation = 'source-over';
  ctx.fill();
}, 1000);

let fadeOut = () => {
  let fadeAmount = 0.05;
  // Note that the colour here doesn't matter! Only the alpha matters.
  // The colour here is red, but you'll see no red appear
  ctx.fillStyle = `rgba(255, 0, 0, ${1 - fadeAmount})`;
  ctx.globalCompositeOperation = 'destination-in';
  ctx.fillRect(0, 0, 300, 120);
  requestAnimationFrame(fadeOut);
};
requestAnimationFrame(fadeOut);

canvas { border: 3px solid #808080; background-color: #000000; }

<canvas width="300" height="120"></canvas>

The question is:  How can one fully fade out elements on a canvas, all the way to transparent?

EDIT: I'm searching for a performant solution that works for heavily layered (think visualizer) situations.

Body:
Here is a small sample using globalAlpha, looks good to me, no residue...

each FadingCircle will have own fade, that will determine how fast it fades and if it goes to 0 or below we do not draw it, seems like an easy solution.

You can add colors, random positions and change it as much as you like to suit your needs.

const canvas = document.getElementsByTagName('canvas')[0];
const ctx = canvas.getContext('2d');

class FadingCircle {
  constructor(x, y, radius, fade) {
    this.x = x
    this.y = y
    this.radius = radius
    this.fade = fade
    this.globalAlpha = 1
  }
  draw(ctx) {
    if (this.globalAlpha > 0) {
      ctx.beginPath()
      ctx.globalAlpha = this.globalAlpha
      ctx.arc(this.x, this.y, this.radius, Math.PI * 2, 0)
      ctx.fill()
      this.globalAlpha -= this.fade
    }
  }
}

let sml = new FadingCircle(40, 50, 20, 0.01)
let med = new FadingCircle(140, 50, 30, 0)
let big = new FadingCircle(100, 50, 50, 0.005)

let animation = () => {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  big.draw(ctx)
  med.draw(ctx)
  sml.draw(ctx)
  requestAnimationFrame(animation);
};
requestAnimationFrame(animation);

<canvas width="300" height="120"></canvas>