问:
REST API web security without multiple users
I never built a REST API, so,
My idea is: I have an SQL database that has a table with name and description (for this example it is car name and its description is somewhat simple) I want to create a REST API, and thus be able to consume it in any future application. I thought I would do it in php, now my question is for me to be able to access this rest api I must access using url and this will return the results in json form. But as it is done so that only my website can use it (in this example I am developing it in reactjs) and not any other that does not have authorization. I know something about JWT, but I do not know if it is indicated, this web page has no users, it only serves for you to see these "products", I read something about oauth 2.0, I do not know if this is what I am thinking of.</p>

答:
https://blog.restcase.com/4-most-used-rest-api-authentication-methods/

Basic Authentication is probably the easiest. Just google PHP Basic Authentication REST API implementation

There's a very basic old example on php.net in the comments https://www.php.net/manual/en/features.http-auth.php

```<code><?php

$valid_passwords = array ("mario" => "carbonell");
$valid_users = array_keys($valid_passwords);

$user = $_SERVER['PHP_AUTH_USER'];
$pass = $_SERVER['PHP_AUTH_PW'];

$validated = (in_array($user, $valid_users)) && ($pass == $valid_passwords[$user]);

if (!$validated) {
  header('WWW-Authenticate: Basic realm="My Realm"');
  header('HTTP/1.0 401 Unauthorized');
  die ("Not authorized");
}

// If arrives here, is a valid user.
echo "<p>Welcome $user.</p>";
echo "<p>Congratulation, you are into the system.</p>";

?>
</code>```

But the above is not using a database to store user login/hashed password, it's just storing it in an array. But it would be very quick to prototype your authentication before making something a bit more complicated

Here's another basic example along the same lines
https://gist.github.com/rchrd2/c94eb4701da57ce9a0ad4d2b00794131

So with this setup, if you're sending a GET request to your REST API endpoints to get the json, you would also need to include the username/pass in the Http request headers otherwise you would get the 401 not authorized response instead of the JSON.

See the answer here for how you would code the GET request from your PHP to call the REST endpoint
How do I make a request using HTTP basic authentication with PHP curl? or here also has a good example PHP: how to make a GET request with HTTP-Basic authentication

Body:
https://blog.restcase.com/4-most-used-rest-api-authentication-methods/

Basic Authentication is probably the easiest. Just google PHP Basic Authentication REST API implementation

There's a very basic old example on php.net in the comments https://www.php.net/manual/en/features.http-auth.php

<?php

$valid_passwords = array ("mario" => "carbonell");
$valid_users = array_keys($valid_passwords);

$user = $_SERVER['PHP_AUTH_USER'];
$pass = $_SERVER['PHP_AUTH_PW'];

$validated = (in_array($user, $valid_users)) && ($pass == $valid_passwords[$user]);

if (!$validated) {
  header('WWW-Authenticate: Basic realm="My Realm"');
  header('HTTP/1.0 401 Unauthorized');
  die ("Not authorized");
}

// If arrives here, is a valid user.
echo "<p>Welcome $user.</p>";
echo "<p>Congratulation, you are into the system.</p>";

?>

But the above is not using a database to store user login/hashed password, it's just storing it in an array. But it would be very quick to prototype your authentication before making something a bit more complicated

Here's another basic example along the same lines
https://gist.github.com/rchrd2/c94eb4701da57ce9a0ad4d2b00794131

So with this setup, if you're sending a GET request to your REST API endpoints to get the json, you would also need to include the username/pass in the Http request headers otherwise you would get the 401 not authorized response instead of the JSON.

See the answer here for how you would code the GET request from your PHP to call the REST endpoint
How do I make a request using HTTP basic authentication with PHP curl? or here also has a good example PHP: how to make a GET request with HTTP-Basic authentication


来源: stackexchange

问:
gzip: unexpected end of file when using gzip
I have to process a file using my Linux machine.

When I try to write my output to a csv file then gzip it in the same line of script:

processing > output.csv | gzip -f output.csv

I get an 'unexpected end of file' error. Even when I download the file using the Linux machine I get the same error.

When I do not gzip via terminal (or in a single line) everything works fine.

Why does it fail like this when the commands are all in a single line?

答:
You should remove > output.csv

You can either:

Use a pipe: | or:

Redirect to a file

For the same stream (stdout)

You can redirect errors from stderr to a file with 2>errors.txt or they will display on screen

ContentLicense: CC BY-SA 4.0


问:
Linkedin Share is not fetching the thumbnail image while sharing?
I am trying to share linked-in for a page in drupal. In that page there is a image, still the thumbnail image is missing while sharing the page in linked-in.
This is my linked-in sharing code.

```<code><a href='http://www.linkedin.com/shareArticle?mini=true&url=<?php print $tiny_bit_url;?>&title=<?php print $data->node_title;?>&summary=<?php print $summary; ?>'>
<img src="linkedin-icon.png"/>
</a>
</code>```

What is that I am missing w.r.t image being shared as thumbnail from that page to linked-in share.

Thanks in advance.

答:
Use og: tags on your page to indicate data you want LinkedIn to use, for instance...

<meta property='og:title' content='Title of the article"/>

<meta property='og:image' content='//media.example.com/ 1234567.jpg"/>

<meta property='og:description' content='Description that will show in the preview"/>

<meta property='og:url' content='//www.example.com/URL of the article" />

Source: LinkedIn Developer Docs: Making Your Website Shareable on LinkedIn.

Want to be able to check and verify that you did it correctly?  Input your URL into the LinkedIn Post Inspector.

ContentLicense: CC BY-SA 3.0

Body:
I am trying to share linked-in for a page in drupal. In that page there is a image, still the thumbnail image is missing while sharing the page in linked-in.
This is my linked-in sharing code.

<a href='http://www.linkedin.com/shareArticle?mini=true&url=<?php print $tiny_bit_url;?>&title=<?php print $data->node_title;?>&summary=<?php print $summary; ?>'>

What is that I am missing w.r.t image being shared as thumbnail from that page to linked-in share.

Thanks in advance.


问:
Why does auto&& var2 not imply rvalue reference?
Why does auto&& var2 not mean rvalue reference in the code below?

I would be grateful to have some help with this question.

```<code>Widget&& var1 = someWidget;      // here, “&&” means rvalue reference
auto&& var2 = var1;              // here, “&&” does not mean rvalue reference
</code>```

Given that Widget&& var1 is an rvalue reference, why isn't it an rvalue?

Why does auto&& var2 = var1; not mean rvalue reference?

答:
Given that Widget&& var1 is an rvalue reference, why isn't it an rvalue?

someWidget, var1, and var2 all have a name and, therefore, they are all lvalues regardless of their declared type.

The term rvalue reference is about the type of the reference, var1, whereas the term rvalue is about the value category of the expression var1:

var1 declared as Widget&& var1 is a reference to Widget, specifically an rvalue reference to Widget (i.e., Widget&&) – This is the type of var1, which is a reference. The reference type tells you how the reference can be initialized: an rvalue reference can only be initialized with an rvalue, whereas a non-const lvalue reference can only be initialized with an lvalue.

When using var1 in an expression (without marking it with std::move), then var1 is an lvalue because it has a name – This the value category of var1, which is a property of the expression var1 orthogonal to its type.

Note also that the following statement doesn't compile:

```<code>Widget&& var1 = someWidget;
</code>```

This compilation error is because var1 is an rvalue reference and, therefore, can only be initialized with an rvalue. However, someWidget is an lvalue since it has a name, and it isn't marked with std::move for moving. For the statement to compile you could have done one of the following:

Declaring v1 as an lvalue reference instead:

```<code>Widget& var1 = someWidget;
</code>```

The lvalue reference var1 can be initialized with someWidget, an lvalue.

Marking someWidget for moving with std::move():

```<code>Widget&& var1 = std::move(someWidget);
</code>```

The rvalue reference var1 can be initialized with std::move(someWidget), an rvalue.

Why does auto&& var2 not mean rvalue reference?

var2 is a universal reference because there is type deduction involved. As such, var2 will become either an lvalue reference or rvalue reference depending on how it is initialized (var2 will always be a reference).

The auto in auto&& var2 = var1; deduces to Widget& because var1 is an lvalue (i.e., var1 is a named object and you haven't applied std::move() to it). Then, Widget& && results in Widget& due to reference collapsing. To sum up, the statement:

```<code>auto&& var2 = var1;
</code>```

after type deduction becomes:

```<code>Widget& var2 = var1;
</code>```

So var2 is actually an lvalue reference.

If you want var2 to be an rvalue reference, you can apply std::move() to the initializing object:

```<code>auto&& var2 = std::move(var1);
</code>```

after type deduction, this results in:

```<code>Widget&& var2 = var1;
</code>```

var2 is an rvalue reference in this case.

Body:
Why does auto&& var2 not mean rvalue reference in the code below?

I would be grateful to have some help with this question.

Widget&& var1 = someWidget;      // here, “&&” means rvalue reference
auto&& var2 = var1;              // here, “&&” does not mean rvalue reference

Given that Widget&& var1 is an rvalue reference, why isn't it an rvalue?

Why does auto&& var2 = var1; not mean rvalue reference?

Body:
Given that Widget&& var1 is an rvalue reference, why isn't it an rvalue?

someWidget, var1, and var2 all have a name and, therefore, they are all lvalues regardless of their declared type.

The term rvalue reference is about the type of the reference, var1, whereas the term rvalue is about the value category of the expression var1:

var1 declared as Widget&& var1 is a reference to Widget, specifically an rvalue reference to Widget (i.e., Widget&&) – This is the type of var1, which is a reference. The reference type tells you how the reference can be initialized: an rvalue reference can only be initialized with an rvalue, whereas a non-const lvalue reference can only be initialized with an lvalue.

When using var1 in an expression (without marking it with std::move), then var1 is an lvalue because it has a name – This the value category of var1, which is a property of the expression var1 orthogonal to its type.

Note also that the following statement doesn't compile:

Widget&& var1 = someWidget;

This compilation error is because var1 is an rvalue reference and, therefore, can only be initialized with an rvalue. However, someWidget is an lvalue since it has a name, and it isn't marked with std::move for moving. For the statement to compile you could have done one of the following:

Declaring v1 as an lvalue reference instead:

Widget& var1 = someWidget;

The lvalue reference var1 can be initialized with someWidget, an lvalue.

Marking someWidget for moving with std::move():

Widget&& var1 = std::move(someWidget);

The rvalue reference var1 can be initialized with std::move(someWidget), an rvalue.

Why does auto&& var2 not mean rvalue reference?

var2 is a universal reference because there is type deduction involved. As such, var2 will become either an lvalue reference or rvalue reference depending on how it is initialized (var2 will always be a reference).

The auto in auto&& var2 = var1; deduces to Widget& because var1 is an lvalue (i.e., var1 is a named object and you haven't applied std::move() to it). Then, Widget& && results in Widget& due to reference collapsing. To sum up, the statement:

auto&& var2 = var1;

after type deduction becomes:

Widget& var2 = var1;

So var2 is actually an lvalue reference.

If you want var2 to be an rvalue reference, you can apply std::move() to the initializing object:

auto&& var2 = std::move(var1);

after type deduction, this results in:

Widget&& var2 = var1;

var2 is an rvalue reference in this case.


问:
create all possible combinations with multiple variants from list
Ok so the problem is as follows:

let's say I have a list like this [12R,102A,102L,250L] what I would want is a list of all possible combinations, however for only one combination/number. so for the example above, the output I would like is:

```<code>[12R,102A,250L]
[12R,102L,250L]
</code>```

my actual problem is a lot more complex with many more sites. Thanks for your help

edit: after reading some comments I guess this is slightly unclear. I have 3 unique numbers here, [12, 102, and 250] and for some numbers, I have different variations, for example [102A, 102L]. what I need is a way to combine the different positions[12,102,250] and all possible variations within. just like the lists, I presented above. they are the only valid solutions. [12R] is not. neither is [12R,102A,102L,250L]. so far I have done this with nested loops, but I have a LOT of variation within these numbers, so I can't really do that anymore

ill edit this again: ok so it seems as though there is still some confusion so I might extend the point I made before. what I am dealing with there is DNA. 12R means the 12th position in the sequence was changed to an R. so the solution     [12R,102A,250L] means that the amino acid on position 12 is R, 102 is A 250 is L.

this is why a solution like [102L, 102R, 250L] is not usable, because the same position can not be occupied by 2 different amino acids.

thank you

答:
You can use a recursive generator function:

```<code>from itertools import groupby as gb
import re

def combos(d, c = []):
  if not d:
     yield c
  else:
     for a, b in d[0]:
       yield from combos(d[1:], c + [a+b])

d = ['12R', '102A', '102L', '250L']
vals = [re.findall('^\d+|\w+$', i) for i in d]
new_d = [list(b) for _, b in gb(sorted(vals, key=lambda x:x[0]), key=lambda x:x[0])]
print(list(combos(new_d)))
</code>```

Output:

```<code>[['102A', '12R', '250L'], ['102L', '12R', '250L']]
</code>```

Body:
Ok so the problem is as follows:

let's say I have a list like this [12R,102A,102L,250L] what I would want is a list of all possible combinations, however for only one combination/number. so for the example above, the output I would like is:

[12R,102A,250L]
[12R,102L,250L]

my actual problem is a lot more complex with many more sites. Thanks for your help

edit: after reading some comments I guess this is slightly unclear. I have 3 unique numbers here, [12, 102, and 250] and for some numbers, I have different variations, for example [102A, 102L]. what I need is a way to combine the different positions[12,102,250] and all possible variations within. just like the lists, I presented above. they are the only valid solutions. [12R] is not. neither is [12R,102A,102L,250L]. so far I have done this with nested loops, but I have a LOT of variation within these numbers, so I can't really do that anymore

ill edit this again: ok so it seems as though there is still some confusion so I might extend the point I made before. what I am dealing with there is DNA. 12R means the 12th position in the sequence was changed to an R. so the solution     [12R,102A,250L] means that the amino acid on position 12 is R, 102 is A 250 is L.

this is why a solution like [102L, 102R, 250L] is not usable, because the same position can not be occupied by 2 different amino acids.

thank you

Body:
You can use a recursive generator function:

from itertools import groupby as gb
import re

def combos(d, c = []):
  if not d:
     yield c
  else:
     for a, b in d[0]:
       yield from combos(d[1:], c + [a+b])

d = ['12R', '102A', '102L', '250L']
vals = [re.findall('^\d+|\w+$', i) for i in d]
new_d = [list(b) for _, b in gb(sorted(vals, key=lambda x:x[0]), key=lambda x:x[0])]
print(list(combos(new_d)))

Output:

[['102A', '12R', '250L'], ['102L', '12R', '250L']]


问:
Making an aim trajectory using a line renderer
The reference of a trajectory line

Hey, i'm new to unity and trying to create a trajectory line with the help of a line renderer but it doesn't seem to be working. I have attached my movement script below. It would be very helpful if someone helps me out.

```<code>public void Update()
{

    if (Input.GetMouseButtonDown(0)) //press
    {
        startPoint = cam.ScreenToWorldPoint(Input.mousePosition);
        startPoint.z = 15;
        direction = endPoint - startPoint;
        transform.right = direction;

        for (int i = 0; i < numberOfDots; i++)
        {
            trajectoryDots[i].transform.position = calculatePosition(i * 0.01f);
        }
    }

    if (Input.GetMouseButtonUp(0)) //release
    {
        endPoint = cam.ScreenToWorldPoint(Input.mousePosition);
        endPoint.z = 15;

        force = new Vector2(Mathf.Clamp(startPoint.x - endPoint.x, minPower.x, maxPower.x), Mathf.Clamp(startPoint.y - endPoint.y, minPower.y, maxPower.y));
        rb.AddForce(force * power, ForceMode2D.Impulse);
    }

}
</code>```

答:
I believe you can't use 'trajectoryDots[i].transform.position' to set the position. There is an explicit .SetPosition() function for lineRenderers.

Try looking into this:
https://docs.unity3d.com/ScriptReference/LineRenderer.SetPositions.html

That might solve your problem.

Body:
The reference of a trajectory line

Hey, i'm new to unity and trying to create a trajectory line with the help of a line renderer but it doesn't seem to be working. I have attached my movement script below. It would be very helpful if someone helps me out.

public void Update()
{

    if (Input.GetMouseButtonDown(0)) //press
    {
        startPoint = cam.ScreenToWorldPoint(Input.mousePosition);
        startPoint.z = 15;
        direction = endPoint - startPoint;
        transform.right = direction;

        for (int i = 0; i < numberOfDots; i++)
        {
            trajectoryDots[i].transform.position = calculatePosition(i * 0.01f);
        }
    }

    if (Input.GetMouseButtonUp(0)) //release
    {
        endPoint = cam.ScreenToWorldPoint(Input.mousePosition);
        endPoint.z = 15;

        force = new Vector2(Mathf.Clamp(startPoint.x - endPoint.x, minPower.x, maxPower.x), Mathf.Clamp(startPoint.y - endPoint.y, minPower.y, maxPower.y));
        rb.AddForce(force * power, ForceMode2D.Impulse);
    }

}


问:
How to skip end-tags (HTML) in VS code editor?
I have installed VS code editor with default settings. When I type a html code, for example the starting tag, <title> the ending tag is automatically generated i.e. </title>. But after typing the title inside those tags. I have to press 'right arrow' button repeatedly(here, 8 times) to come out of this HTML element(</title> tag).

Is there any shortcut such that I don't have to press 'right arrow' button repeatedly and instead any single key, which will skip the closing tag in one stroke of any button ?

答:
try to use Ctrl+right arrow
and scan that link
https://www.reddit.com/r/vscode/comments/cxxyj2/how_to_skip_to_end_of_closing_tag_end_of_line_or/</p>


问:
Installing gir1.2-gtk-3.0 on Centos
On Ubuntu there is a package for installing gir1.2-gtk-3.0:

```<code>sudo apt install gir1.2-gtk-3.0
</code>```

Centos 'dnf provides gir1*' results in error: No Matches found

The package can be found on https://github.com/roojs/gir-1.2-gtk-3.0, but there is no installation script, only files with extension *.gir

How can it be installed manually?

The installation is necessary to avoid:

gi.require_version("Gtk", "3.0")
Python: Namespace Gtk not available

Other packages gobject-introspection, python3-gi are alredy installed

答:
The gir1.2-* packages are for Debian and Debian-derived distributions.

The introspection data that is consumed by the Python bindings is provided by typelib files, for instance:

```<code>/usr/lib64/girepository-1.0/Gtk-3.0.typelib
</code>```

These files are provided by the library package, on CentOS, alongside the actual shared library. For instance, the Gtk-3.0.typelib file is provided by the gtk3 package.

The GIR files are XML descriptions of the API that are only used when generating the introspection data; they are provided by the devel package, on CentOS. For instance, the Gtk-3.0.gir file is provided by the gtk3-devel package.

If pygobject cannot find the typelib file, it's likely that you've installed it in some non-standard location. You must use the GI_TYPELIB_PATH environment variable to list all the locations where the typelib data might be found, as a colon-separated list of paths.

Title: Installing gir1.2-gtk-3.0 on Centos

Body:
On Ubuntu there is a package for installing gir1.2-gtk-3.0:

sudo apt install gir1.2-gtk-3.0

Centos 'dnf provides gir1*' results in error: No Matches found

The package can be found on https://github.com/roojs/gir-1.2-gtk-3.0, but there is no installation script, only files with extension *.gir

How can it be installed manually?

The installation is necessary to avoid:

gi.require_version("Gtk", "3.0")
Python: Namespace Gtk not available

Other packages gobject-introspection, python3-gi are alredy installed

Body:
The gir1.2-* packages are for Debian and Debian-derived distributions.

The introspection data that is consumed by the Python bindings is provided by typelib files, for instance:

/usr/lib64/girepository-1.0/Gtk-3.0.typelib

These files are provided by the library package, on CentOS, alongside the actual shared library. For instance, the Gtk-3.0.typelib file is provided by the gtk3 package.

The GIR files are XML descriptions of the API that are only used when generating the introspection data; they are provided by the devel package, on CentOS. For instance, the Gtk-3.0.gir file is provided by the gtk3-devel package.

If pygobject cannot find the typelib file, it's likely that you've installed it in some non-standard location. You must use the GI_TYPELIB_PATH environment variable to list all the locations where the typelib data might be found, as a colon-separated list of paths.


问:
Value for argument "documentPath" is not a valid resource path. Path must be a non-empty string
I want to write a cloud function  that listens to whether a new document is created in the following subcollection of some document of users. However, the user document upon previous creation may not have a following subcollection.

In other words, I want a cloud that responds to db.collection(“users”).doc(“doc_id1”).collection(“following”).doc(“doc_id2”).set(new_document)
, and I have written the cloud function to be

```<code>exports.create_friend_request_onCreate = functions.firestore
  .document("users/{user_id}/{following}/{following_id}")
  .onCreate(f2);
</code>```

And implementation of  f2 is written in some other file

```<code>exports.f2 = async function(snapshot) {
 //some code
}
</code>```

However upon creation the document in the subcollection, I get the following error

Error: Value for argument "documentPath" is not a valid resource path. Path must be a non-empty string.

Can someone explain to me what is wrong here?

答: The correct path should have been 'users/{user_id}/following/{following_id}', apparently the double quotes cannot be used as paths.

Body:
I want to write a cloud function  that listens to whether a new document is created in the following subcollection of some document of users. However, the user document upon previous creation may not have a following subcollection.

In other words, I want a cloud that responds to db.collection(“users”).doc(“doc_id1”).collection(“following”).doc(“doc_id2”).set(new_document)
, and I have written the cloud function to be

exports.create_friend_request_onCreate = functions.firestore
  .document("users/{user_id}/{following}/{following_id}")
  .onCreate(f2);

And implementation of  f2 is written in some other file

exports.f2 = async function(snapshot) {
 //some code
}

However upon creation the document in the subcollection, I get the following error

Error: Value for argument "documentPath" is not a valid resource path. Path must be a non-empty string.

Can someone explain to me what is wrong here?


问:
Entity Framework Reference constraint error on delete when using selectmany
Edit: Please read carefully, I have and issue Loading data for without cartesian production. Deleting data works fine if the references are loaded properly.

I'm trying to delete a large group of entities, However due to a many-to-many assoication OwnerRoles whenever I try to delete them I receive an error:

SqlException: The DELETE statement conflicted with the REFERENCE constraint "FK_OwnerRoles_aspnet_Roles_RoleId"

When I try to avoid cartesian production by loading the aspnet_roles using a select many,

```<code>var rolesQuery = context.Organizations
       .Where(x => x.OrganizationId == organizationId)
       .SelectMany(x => x.aspnet_Roles);

var roles = rolesQuery.ToArray();
rolesQuery.SelectMany(x => x.Permissions).Load();
rolesQuery.SelectMany(x => x.Organizations).Load();
</code>```

The assoicated OwnerRoles is not loaded, so when I attempt to remove all of the references:

```<code>roles.ForEach(r => r.Organizations.ToArray().ForEach(o => r.Organizations.Remove(o)));

context.Permissions.RemoveRange(roles.SelectMany(x => x.Permissions));
context.aspnet_Roles.RemoveRange(roles);

context.SaveChanges();
</code>```

There is nothing loaded to remove so, I get my referencial constraint when deleting.

This is my db structure

```<code>Organizations: * => * aspnet_Roles (Many To Many connected by intermediate table **OwnerRoles**)
aspnet_Roles: 1 => * permissions (aspnet_Roles has many permissions)
</code>```

Notes:

I cannot use CascadeOnDelete

I'm using and Edmx with EntityFramework v6.2

I'd prefer to not use save changes multiple times.

Everything works fine if I use includes instead of SelectMany,  Because include loads the tables connecting However I would like to make seperate queries to avoid Cartesian production, so the result set sent back over the wire isn't so large.

How can I Load my data properly to avoid caretesian production, while still being able to delete many to many collections?

I'm looking for a way to explicitly load the references of a collection table (e.g there is no Poco class or DB set for that entity) OR I'm looking for a way to explicitly delete from EntityFramework (with out calling a stored procedure because that will circumvent the audit log)

答:
Problem

Additionally to the 3 standard ways of loading related data (eager, explicit and lazy), EF6 supports another way through a process called "navigation property fix-up", which is utilized by queries like your

```<code>rolesQuery.SelectMany(x => x.Permissions).Load();
</code>```

Note that the name Load is a bit misleading. Load is EF custom extension method which simply executes the query and iterates the result set, similar to ToList, but without creating a list.

The first 3 methods work for any type of relationship. However the last doesn't work for many-to-many with implicit link entity relationships, because there is no way to specify the "link" entity in the LINQ query, so the following

```<code>rolesQuery.SelectMany(x => x.Organizations).Load();
</code>```

and

```<code>context.Organizations.Load();
</code>```

are equivalent - both return (and load) Organization entities.

The standard solution is to use some of the 3 standard ways. But eager loading generates huge union data set, while explicit and lazy loading generate N + 1 database queries.

Solution

Many-to-many with implicit link entity relationships are implemented as independent associations and are maintained purely by the context change tracker. DbContext APIs do not provide a way to maintain such relationships state, but as mentioned in the Creating and modifying relationships section of the EF6 documentation, ObjectContext APIs do with ChangeRelationshipState method of the ObjectStateManager.

Following is a custom generic extension method which solves the problem by utilizing the aforementioned method. The essential part is

```<code>// Query to retrieve IEnumerable<Tuple<TSourceKey, TTarget>> from database
// and group it by TSourceKey in memory
var groupedLinksQuery = sourceDbQuery
    .SelectLinks(keySelector, collectionSelector)
    .AsEnumerable()
    .GroupBy(e => e.Item1, e => e.Item2);
// Execute the query and perform the fix-up
foreach (var group in groupedLinksQuery)
{
    var source = sourceDbSet.Find(group.Key);
    foreach (var target in group)
        stateManager.ChangeRelationshipState(source, target, collectionPropertyName, EntityState.Unchanged);
}
</code>```

Sample usage:

```<code>var roles = rolesQuery.ToArray();
rolesQuery.SelectMany(role => role.Permissions).Load();
context.LoadLinks(rolesQuery, role => role.Id, role => role.Organizations); // <--
</code>```

Full code:

```<code>using System;
using System.Collections.Generic;
using System.Data.Entity.Core.Objects;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Linq.Expressions;

namespace System.Data.Entity
{
    public static partial class EF6Extensions
    {
        public static void LoadLinks<TSource, TSourceKey, TTarget>(this DbContext dbContext, IQueryable<TSource> sourceDbQuery, Expression<Func<TSource, TSourceKey>> keySelector, Expression<Func<TSource, ICollection<TTarget>>> collectionSelector)
            where TSource : class
            where TTarget : class
        {
            // Disable AutoDetectChanges for better performance
            bool autoDetectChanges = dbContext.Configuration.AutoDetectChangesEnabled;
            dbContext.Configuration.AutoDetectChangesEnabled = false;
            try
            {
                var sourceDbSet = dbContext.Set<TSource>();
                var collectionPropertyName = ((MemberExpression)collectionSelector.Body).Member.Name;
                var stateManager = dbContext.GetObjectStateManager();
                // Query to retrieve IEnumerable<Tuple<TSourceKey, TTarget>> from database
                // and group it by TSourceKey in memory
                var groupedLinksQuery = sourceDbQuery
                    .SelectLinks(keySelector, collectionSelector)
                    .AsEnumerable()
                    .GroupBy(e => e.Item1, e => e.Item2);
                // Execute the query and perform the fix-up
                foreach (var group in groupedLinksQuery)
                {
                    var source = sourceDbSet.Find(group.Key);
                    foreach (var target in group)
                        stateManager.ChangeRelationshipState(source, target, collectionPropertyName, EntityState.Unchanged);
                }
            }
            finally { dbContext.Configuration.AutoDetectChangesEnabled = autoDetectChanges; }
        }

        static IQueryable<Tuple<TSourceKey, TTarget>> SelectLinks<TSource, TSourceKey, TTarget>(this IQueryable<TSource> sourceQuery, Expression<Func<TSource, TSourceKey>> keySelector, Expression<Func<TSource, ICollection<TTarget>>> collectionSelector)
        {
            // sourceQuery.SelectMany(source => source.Collection, (source, target) => Tuple(source.Key, target))
            var source = keySelector.Parameters[0];
            var target = Expression.Parameter(typeof(TTarget), "target");
            var resultType = typeof(Tuple<TSourceKey, TTarget>);
            var constructor = resultType.GetConstructor(new[] { typeof(TSourceKey), typeof(TTarget) });
            var args = new[] { keySelector.Body, target };
            var members = new[] { resultType.GetProperty("Item1"), resultType.GetProperty("Item2") };
            var body = Expression.New(constructor, args, members);
            var selector = Expression.Lambda<Func<TSource, TTarget, Tuple<TSourceKey, TTarget>>>(
                body, source, target);
            return sourceQuery.SelectMany(collectionSelector.AsEnumerable(), selector);
        }

        static Expression<Func<TSource, IEnumerable<TTarget>>> AsEnumerable<TSource, TTarget>(this Expression<Func<TSource, ICollection<TTarget>>> collectionSelector)
            => Expression.Lambda<Func<TSource, IEnumerable<TTarget>>>(collectionSelector.Body, collectionSelector.Parameters);

        public static ObjectContext GetObjectContext(this IObjectContextAdapter source) => source.ObjectContext;

        public static ObjectStateManager GetObjectStateManager(this IObjectContextAdapter source) => source.ObjectContext.ObjectStateManager;
    }
}
</code>```

Update: The above executes 2 db queries and the second contains duplicate TTarget records paired with TSourceKey. The difference with include is that it eliminates the TSource columns from the query.

Retrieving only the data needed w/o duplicates is possible, and requires executing 3 db queries:

```<code>
public static partial class EF6Extensions
{
    public static void LoadLinks<TSource, TTarget, TSourceKey, TTargetKey>(this DbContext dbContext, IQueryable<TSource> sourceQuery, Expression<Func<TSource, ICollection<TTarget>>> collectionSelector, Expression<Func<TSource, TSourceKey>> sourceKeySelector, Expression<Func<TTarget, TTargetKey>> targetKeySelector)
        where TSource : class
        where TTarget : class
    {
        // Disable AutoDetectChanges for better performance
        bool autoDetectChanges = dbContext.Configuration.AutoDetectChangesEnabled;
        dbContext.Configuration.AutoDetectChangesEnabled = false;
        try
        {
            var sourceDbSet = dbContext.Set<TSource>();
            var targetDbSet = dbContext.Set<TTarget>();
            // Query to retrieve link keys from database
            var linksDbQuery = sourceQuery.SelectLinks(collectionSelector, sourceKeySelector, targetKeySelector);
            // Query to retrieve distinct target keys from database
            var targetKeysDbQuery = linksDbQuery.Select(e => e.Item2).Distinct();
            // Query to retrieve unique target entities
            var targetDbQuery = targetDbSet
                .Join(targetKeysDbQuery, targetKeySelector, key => key, (target, key) => target);
            // Execute the target entities query and build map by Id in memory
            var targetMap = targetDbQuery
                .ToDictionary(targetKeySelector.Compile());
            // Execute the links query and perform the fix-up
            var stateManager = dbContext.GetObjectStateManager();
            var collectionPropertyName = ((MemberExpression)collectionSelector.Body).Member.Name;
            var sourceMap = new Dictionary<TSourceKey, TSource>();
            foreach (var link in linksDbQuery)
            {
                if (!sourceMap.TryGetValue(link.Item1, out var source))
                    sourceMap.Add(link.Item1, source = sourceDbSet.Find(link.Item1));
                var target = targetMap[link.Item2];
                stateManager.ChangeRelationshipState(source, target, collectionPropertyName, EntityState.Unchanged);
            }
        }
        finally { dbContext.Configuration.AutoDetectChangesEnabled = autoDetectChanges; }
    }

    static IQueryable<Tuple<TSourceKey, TTargetKey>> SelectLinks<TSource, TTarget, TSourceKey, TTargetKey>(this IQueryable<TSource> sourceQuery, Expression<Func<TSource, ICollection<TTarget>>> collectionSelector, Expression<Func<TSource, TSourceKey>> sourceKeySelector, Expression<Func<TTarget, TTargetKey>> targetKeySelector)
    {
        // sourceQuery.SelectMany(source => source.Collection, (source, target) => Tuple(source.Key, target.Key))
        var source = sourceKeySelector.Parameters[0];
        var target = targetKeySelector.Parameters[0];
        var resultType = typeof(Tuple<TSourceKey, TTargetKey>);
        var constructor = resultType.GetConstructor(new[] { typeof(TSourceKey), typeof(TTargetKey) });
        var args = new[] { sourceKeySelector.Body, targetKeySelector.Body };
        var members = new[] { resultType.GetProperty("Item1"), resultType.GetProperty("Item2") };
        var body = Expression.New(constructor, args, members);
        var selector = Expression.Lambda<Func<TSource, TTarget, Tuple<TSourceKey, TTargetKey>>>(
            body, source, target);
        return sourceQuery.SelectMany(collectionSelector.AsEnumerable(), selector);
    }
}

</code>```

and the usage requires passing a selectors for both keys e.g.

```<code>context.LoadLinks(rolesQuery, role => role.Organizations, role => role.Id, organization => organization.Id));
</code>```

Body:
Edit: Please read carefully, I have and issue Loading data for without cartesian production. Deleting data works fine if the references are loaded properly.

I'm trying to delete a large group of entities, However due to a many-to-many assoication OwnerRoles whenever I try to delete them I receive an error:

SqlException: The DELETE statement conflicted with the REFERENCE constraint "FK_OwnerRoles_aspnet_Roles_RoleId"

When I try to avoid cartesian production by loading the aspnet_roles using a select many,

var rolesQuery = context.Organizations
       .Where(x => x.OrganizationId == organizationId)
       .SelectMany(x => x.aspnet_Roles);

var roles = rolesQuery.ToArray();
rolesQuery.SelectMany(x => x.Permissions).Load();
rolesQuery.SelectMany(x => x.Organizations).Load();

The assoicated OwnerRoles is not loaded, so when I attempt to remove all of the references:

roles.ForEach(r => r.Organizations.ToArray().ForEach(o => r.Organizations.Remove(o)));

context.Permissions.RemoveRange(roles.SelectMany(x => x.Permissions));
context.aspnet_Roles.RemoveRange(roles);

context.SaveChanges();

There is nothing loaded to remove so, I get my referencial constraint when deleting.

This is my db structure

Organizations: * => * aspnet_Roles (Many To Many connected by intermediate table **OwnerRoles**)
aspnet_Roles: 1 => * permissions (aspnet_Roles has many permissions)

Notes:

I cannot use CascadeOnDelete

I'm using and Edmx with EntityFramework v6.2

I'd prefer to not use save changes multiple times.

Everything works fine if I use includes instead of SelectMany,  Because include loads the tables connecting However I would like to make seperate queries to avoid Cartesian production, so the result set sent back over the wire isn't so large.

How can I Load my data properly to avoid caretesian production, while still being able to delete many to many collections?

I'm looking for a way to explicitly load the references of a collection table (e.g there is no Poco class or DB set for that entity) OR I'm looking for a way to explicitly delete from EntityFramework (with out calling a stored procedure because that will circumvent the audit log)

Body:
Problem

Additionally to the 3 standard ways of loading related data (eager, explicit and lazy), EF6 supports another way through a process called "navigation property fix-up", which is utilized by queries like your

rolesQuery.SelectMany(x => x.Permissions).Load();

Note that the name Load is a bit misleading. Load is EF custom extension method which simply executes the query and iterates the result set, similar to ToList, but without creating a list.

The first 3 methods work for any type of relationship. However the last doesn't work for many-to-many with implicit link entity relationships, because there is no way to specify the "link" entity in the LINQ query, so the following

rolesQuery.SelectMany(x => x.Organizations).Load();

and

context.Organizations.Load();

are equivalent - both return (and load) Organization entities.

The standard solution is to use some of the 3 standard ways. But eager loading generates huge union data set, while explicit and lazy loading generate N + 1 database queries.

Solution

Many-to-many with implicit link entity relationships are implemented as independent associations and are maintained purely by the context change tracker. DbContext APIs do not provide a way to maintain such relationships state, but as mentioned in the Creating and modifying relationships section of the EF6 documentation, ObjectContext APIs do with ChangeRelationshipState method of the ObjectStateManager.

Following is a custom generic extension method which solves the problem by utilizing the aforementioned method. The essential part is

// Query to retrieve IEnumerable<Tuple<TSourceKey, TTarget>> from database
// and group it by TSourceKey in memory
var groupedLinksQuery = sourceDbQuery
    .SelectLinks(keySelector, collectionSelector)
    .AsEnumerable()
    .GroupBy(e => e.Item1, e => e.Item2);
// Execute the query and perform the fix-up
foreach (var group in groupedLinksQuery)
{
    var source = sourceDbSet.Find(group.Key);
    foreach (var target in group)
        stateManager.ChangeRelationshipState(source, target, collectionPropertyName, EntityState.Unchanged);
}

Sample usage:

var roles = rolesQuery.ToArray();
rolesQuery.SelectMany(role => role.Permissions).Load();
context.LoadLinks(rolesQuery, role => role.Id, role => role.Organizations); // <--

Full code:

using System;
using System.Collections.Generic;
using System.Data.Entity.Core.Objects;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Linq.Expressions;

namespace System.Data.Entity
{
    public static partial class EF6Extensions
    {
        public static void LoadLinks<TSource, TSourceKey, TTarget>(this DbContext dbContext, IQueryable<TSource> sourceDbQuery, Expression<Func<TSource, TSourceKey>> keySelector, Expression<Func<TSource, ICollection<TTarget>>> collectionSelector)
            where TSource : class
            where TTarget : class
        {
            // Disable AutoDetectChanges for better performance
            bool autoDetectChanges = dbContext.Configuration.AutoDetectChangesEnabled;
            dbContext.Configuration.AutoDetectChangesEnabled = false;
            try
            {
                var sourceDbSet = dbContext.Set<TSource>();
                var collectionPropertyName = ((MemberExpression)collectionSelector.Body).Member.Name;
                var stateManager = dbContext.GetObjectStateManager();
                // Query to retrieve IEnumerable<Tuple<TSourceKey, TTarget>> from database
                // and group it by TSourceKey in memory
                var groupedLinksQuery = sourceDbQuery
                    .SelectLinks(keySelector, collectionSelector)
                    .AsEnumerable()
                    .GroupBy(e => e.Item1, e => e.Item2);
                // Execute the query and perform the fix-up
                foreach (var group in groupedLinksQuery)
                {
                    var source = sourceDbSet.Find(group.Key);
                    foreach (var target in group)
                        stateManager.ChangeRelationshipState(source, target, collectionPropertyName, EntityState.Unchanged);
                }
            }
            finally { dbContext.Configuration.AutoDetectChangesEnabled = autoDetectChanges; }
        }

        static IQueryable<Tuple<TSourceKey, TTarget>> SelectLinks<TSource, TSourceKey, TTarget>(this IQueryable<TSource> sourceQuery, Expression<Func<TSource, TSourceKey>> keySelector, Expression<Func<TSource, ICollection<TTarget>>> collectionSelector)
        {
            // sourceQuery.SelectMany(source => source.Collection, (source, target) => Tuple(source.Key, target))
            var source = keySelector.Parameters[0];
            var target = Expression.Parameter(typeof(TTarget), "target");
            var resultType = typeof(Tuple<TSourceKey, TTarget>);
            var constructor = resultType.GetConstructor(new[] { typeof(TSourceKey), typeof(TTarget) });
            var args = new[] { keySelector.Body, target };
            var members = new[] { resultType.GetProperty("Item1"), resultType.GetProperty("Item2") };
            var body = Expression.New(constructor, args, members);
            var selector = Expression.Lambda<Func<TSource, TTarget, Tuple<TSourceKey, TTarget>>>(
                body, source, target);
            return sourceQuery.SelectMany(collectionSelector.AsEnumerable(), selector);
        }

        static Expression<Func<TSource, IEnumerable<TTarget>>> AsEnumerable<TSource, TTarget>(this Expression<Func<TSource, ICollection<TTarget>>> collectionSelector)
            => Expression.Lambda<Func<TSource, IEnumerable<TTarget>>>(collectionSelector.Body, collectionSelector.Parameters);

        public static ObjectContext GetObjectContext(this IObjectContextAdapter source) => source.ObjectContext;

        public static ObjectStateManager GetObjectStateManager(this IObjectContextAdapter source) => source.ObjectContext.ObjectStateManager;
    }
}

Update: The above executes 2 db queries and the second contains duplicate TTarget records paired with TSourceKey. The difference with include is that it eliminates the TSource columns from the query.

Retrieving only the data needed w/o duplicates is possible, and requires executing 3 db queries:

public static partial class EF6Extensions
{
    public static void LoadLinks<TSource, TTarget, TSourceKey, TTargetKey>(this DbContext dbContext, IQueryable<TSource> sourceQuery, Expression<Func<TSource, ICollection<TTarget>>> collectionSelector, Expression<Func<TSource, TSourceKey>> sourceKeySelector, Expression<Func<TTarget, TTargetKey>> targetKeySelector)
        where TSource : class
        where TTarget : class
    {
        // Disable AutoDetectChanges for better performance
        bool autoDetectChanges = dbContext.Configuration.AutoDetectChangesEnabled;
        dbContext.Configuration.AutoDetectChangesEnabled = false;
        try
        {
            var sourceDbSet = dbContext.Set<TSource>();
            var targetDbSet = dbContext.Set<TTarget>();
            // Query to retrieve link keys from database
            var linksDbQuery = sourceQuery.SelectLinks(collectionSelector, sourceKeySelector, targetKeySelector);
            // Query to retrieve distinct target keys from database
            var targetKeysDbQuery = linksDbQuery.Select(e => e.Item2).Distinct();
            // Query to retrieve unique target entities
            var targetDbQuery = targetDbSet
                .Join(targetKeysDbQuery, targetKeySelector, key => key, (target, key) => target);
            // Execute the target entities query and build map by Id in memory
            var targetMap = targetDbQuery
                .ToDictionary(targetKeySelector.Compile());
            // Execute the links query and perform the fix-up
            var stateManager = dbContext.GetObjectStateManager();
            var collectionPropertyName = ((MemberExpression)collectionSelector.Body).Member.Name;
            var sourceMap = new Dictionary<TSourceKey, TSource>();
            foreach (var link in linksDbQuery)
            {
                if (!sourceMap.TryGetValue(link.Item1, out var source))
                    sourceMap.Add(link.Item1, source = sourceDbSet.Find(link.Item1));
                var target = targetMap[link.Item2];
                stateManager.ChangeRelationshipState(source, target, collectionPropertyName, EntityState.Unchanged);
            }
        }
        finally { dbContext.Configuration.AutoDetectChangesEnabled = autoDetectChanges; }
    }

    static IQueryable<Tuple<TSourceKey, TTargetKey>> SelectLinks<TSource, TTarget, TSourceKey, TTargetKey>(this IQueryable<TSource> sourceQuery, Expression<Func<TSource, ICollection<TTarget>>> collectionSelector, Expression<Func<TSource, TSourceKey>> sourceKeySelector, Expression<Func<TTarget, TTargetKey>> targetKeySelector)
    {
        // sourceQuery.SelectMany(source => source.Collection, (source, target) => Tuple(source.Key, target.Key))
        var source = sourceKeySelector.Parameters[0];
        var target = targetKeySelector.Parameters[0];
        var resultType = typeof(Tuple<TSourceKey, TTargetKey>);
        var constructor = resultType.GetConstructor(new[] { typeof(TSourceKey), typeof(TTargetKey) });
        var args = new[] { sourceKeySelector.Body, targetKeySelector.Body };
        var members = new[] { resultType.GetProperty("Item1"), resultType.GetProperty("Item2") };
        var body = Expression.New(constructor, args, members);
        var selector = Expression.Lambda<Func<TSource, TTarget, Tuple<TSourceKey, TTargetKey>>>(
            body, source, target);
        return sourceQuery.SelectMany(collectionSelector.AsEnumerable(), selector);
    }
}

and the usage requires passing a selectors for both keys e.g.

context.LoadLinks(rolesQuery, role => role.Organizations, role => role.Id, organization => organization.Id));


问:
i have a problem with saving data to database using LARAVEL
this is all my codes after cliking on button doesn't save to database i try to change method of saving but not work

```<code>    use App\makereservations;
use Illuminate\Http\Request;

class reservationController extends Controller
{
    public function hreservation()
    {
        return view('hreservation');
    }
    public function reserve (Request $request)
    {
        $this->validate($request ,[
            'name' => 'required',
            'email' => 'required',
            'phone' => 'required|email',
            'date' => 'required',
            'time' => 'required',

        ]);
        $reservation  = new makereservations();
        $reservation  =  $request->input('name');
        $reservation  =  $request->input('email');
        $reservation  =  $request->input('phone');
        $reservation  =  $request->input('date');
        $reservation  =  $request->input('time');
        $reservation  =  $request->input('personne');
        $reservation -> statu =  false;
        $reservation -> save();
        return redirect()->back();
    }
}
</code>```

this is the roote
route::post('reserve',  'reservationController@reserve' )->name('reserv.sent');

答:
You can do it more shortly and in a clean way, to optimize your code.

```<code>public function reserve (Request $request)
{
    $attributes = $this->validate($request ,[
        'name' => 'required',
        'email' => 'required|email',
        'phone' => 'required',
        'date' => 'required',
        'time' => 'required',
        'personne' => ''
    ]);

    $attributes['statu'] = false;

    makereservations::create($attributes);
    return redirect()->back();
}
</code>```

In your model make sure these things

```<code>class makereservations extends Model
{
    protected $table = 'reservation';
    protected $guarded = []; //it means all data from request will be mass assignable
}
</code>```

Body:
this is all my codes after cliking on button doesn't save to database i try to change method of saving but not work

    use App\makereservations;
use Illuminate\Http\Request;

class reservationController extends Controller
{
    public function hreservation()
    {
        return view('hreservation');
    }
    public function reserve (Request $request)
    {
        $this->validate($request ,[
            'name' => 'required',
            'email' => 'required',
            'phone' => 'required|email',
            'date' => 'required',
            'time' => 'required',

        ]);
        $reservation  = new makereservations();
        $reservation  =  $request->input('name');
        $reservation  =  $request->input('email');
        $reservation  =  $request->input('phone');
        $reservation  =  $request->input('date');
        $reservation  =  $request->input('time');
        $reservation  =  $request->input('personne');
        $reservation -> statu =  false;
        $reservation -> save();
        return redirect()->back();
    }
}

this is the roote
route::post('reserve',  'reservationController@reserve' )->name('reserv.sent');

Body:
You can do it more shortly and in a clean way, to optimize your code.

public function reserve (Request $request)
{
    $attributes = $this->validate($request ,[
        'name' => 'required',
        'email' => 'required|email',
        'phone' => 'required',
        'date' => 'required',
        'time' => 'required',
        'personne' => ''
    ]);

    $attributes['statu'] = false;

    makereservations::create($attributes);
    return redirect()->back();
}

In your model make sure these things

class makereservations extends Model
{
    protected $table = 'reservation';
    protected $guarded = []; //it means all data from request will be mass assignable
}


问:
Saving data to a file doesn't behave as expected
I have a simple project where I created a Store with customers, products and employees. Each is represented by a Class of course and I also have a CSV file for each one of them to be able to load data from and save data to it.

I'm facing issues where the file reading/writing is working, but not really. For example, I have the ability to save each file individually so if for instance I want to create a new customer, I'd save it to the list and then to the file. Issue is, once I do it for another Class (i.e if I create a new employee) and then save it again, the customer file object I saw in the CSV earlier is deleted. BUT, once I add a new object again, that same object reappears again. Hope you can somehow understand, but here is a more detailed view:

customer.csv is empty:

Me creating a new customer:

Created and saved to CSV:

Now, if I go to the other menu, and click on "Save all data" that jon snow customer object will be gone. Then if I create a new customer, then it will be added to the CSV file, along with the jon snow I added earlier. So why is it gone in the first place?

So here is the whole file reader/writer code I'm using:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.util.ArrayList;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;

class CSV {

    static void CreateFile(String filename) { //Create new file
        try {
            File fileToCreate = new File(filename);
            if (fileToCreate.createNewFile()) {
                System.out.println("File created sucessfully: " + fileToCreate.getName());
            }
        } catch (IOException e) {
            System.out.println("Cannot create file!");
        }
    }

    static void ReadFile(String path_and_filename){
        try {
            File fileToRead = new File(path_and_filename);
            Scanner myReader = new Scanner(fileToRead);
            System.out.println("Reading file "+path_and_filename+" :");
            while (myReader.hasNextLine()) {
                String data = myReader.nextLine();
                System.out.println(data);
            }
            myReader.close();
            System.out.println();
        } catch (FileNotFoundException e) {
            System.out.println("There is no such file "+"\"path_and_filename\""+".\n");
        }
    }

    // The StringBuilder in Java represents a mutable sequence of characters.
    // Java's built in String class is not mutable.
    static void saveArrayListToFile(List<Output> listToSave, String fileName, String sep) throws Exception {
        StringBuilder ans = new StringBuilder();
        for (Output record : listToSave) {
            ans.append(record.createOutput());
            ans.append(sep);
        }
        saveStringToFile(ans.toString(), fileName);
        System.out.println("\nData saved to "+ fileName);
    }

    static void saveArrayListToFile1(ArrayList<String> listToSave, String fileName, String sep){
        StringBuilder ans = new StringBuilder();
        for (Object record : listToSave) {
            ans.append(record.toString());
            ans.append(sep);
        }
        saveStringToFile(ans.toString(), fileName);
        System.out.println("\nList was saved to file "+fileName+"\n");
    }

    static void saveStringToFile(String data, String fileName){
        BufferedWriter bufferedWriter=null;
        try {
            bufferedWriter = new BufferedWriter(
                    new FileWriter(fileName,false));
            bufferedWriter.write(data);
        } catch (IOException e) {
            System.out.println("Cannot write to file");
        } finally {
            try {
                bufferedWriter.close();
            } catch (IOException e) {
                System.out.println("Cannot write to file");
            }
        }
    }
}

```

<p>When I'm creating a new customer, I call it from a menu and it looks like this: </p>

<pre class="lang-java prettyprint-override"><code>            switch (selection) {
                case 1:
                    try {
                        System.out.println("You're registering as a new customer");
                        String custID = ObjectIDs.generateID();
                        System.out.println("Enter first name:");
                        String firstName = sc.next();
                        System.out.println("Enter last name:");
                        String lastName = sc.next();
                        st.newCustomer(custID, firstName, lastName);
                        st.saveCustomersList();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
</code>```

the saveCustomerList() function is this:

@SuppressWarnings("unchecked")
    void saveCustomersList() throws Exception {
        CSV.saveArrayListToFile((List<Output>)(List<?>) customers, CUSTOMERS_FILE_PATH,"\n");
    }
```

And then the functions calls saveArrayListToFile() to save it.

The behavior is the same with Product and Employee projects, so I randomly chose to show how it acts when creating a new Product.

I hope I added enough information. If needed, I can paste more code in but I already feel it's very cluttered. Hopefully it's ok.

Thank you very much :)

答:
In addition to pafau k. I would like to add some things at least I would do differently...

First of all:

Things that can cause errors or unexpected behaviour:

Everything below is in saveStringToFile

Like already pointed out the Initialisation of the BufferedWriter: It should be initialized like this:
bufferedWriter = new BufferedWriter(new FileWriter(filename, true));
This puts the File into appending mode (if you want to append to a file you can also get rid of the boolean (second argument) entirely because appending is standard: new FileWriter(filename))

If for some case the Creation of the BufferedWriter failed you will still have a null-pointing object as bufferedWriter. This however means that you will be surprised with a NullPointerException in your finally block. To prevent this first of all do a check in your finally block:

```<code>if (bufferedWriter != null) {
    // Close your bufferedWriter in here
}
</code>```

Also, if you run into an error you will likely be presented with the same error message twice.

Now cosmetics:

Things that I would write differently for aesthetic reasons:

Java methods (and static "methods") are always starting with a small letter :)
This means it should be public static void createFile() for example or static void readFile()

variables and parameters of methods do not contain seperators like _ but instead if you want to make it more readable you start with a small letter and for each seperation you use a capital letter for that: e.g. String thisIsAVeryLongVariableWithALotOfSeperations = "Foo";

The generic types in saveArrayListToFile1() work like a placeholder. So you declare ArrayList<String> listToSave so you don't need a cast in the following for-loop: You can simply write:

```<code>for (String record : listToSave) {
    ans.append(record);
    ans.append(sep);
}
</code>```

I hope this fixes all errors or complications. :)

Body:
I have a simple project where I created a Store with customers, products and employees. Each is represented by a Class of course and I also have a CSV file for each one of them to be able to load data from and save data to it.

I'm facing issues where the file reading/writing is working, but not really. For example, I have the ability to save each file individually so if for instance I want to create a new customer, I'd save it to the list and then to the file. Issue is, once I do it for another Class (i.e if I create a new employee) and then save it again, the customer file object I saw in the CSV earlier is deleted. BUT, once I add a new object again, that same object reappears again. Hope you can somehow understand, but here is a more detailed view:

customer.csv is empty:

Me creating a new customer:

Created and saved to CSV:

Now, if I go to the other menu, and click on "Save all data" that jon snow customer object will be gone. Then if I create a new customer, then it will be added to the CSV file, along with the jon snow I added earlier. So why is it gone in the first place?

So here is the whole file reader/writer code I'm using:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.util.ArrayList;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;

class CSV {

    static void CreateFile(String filename) { //Create new file
        try {
            File fileToCreate = new File(filename);
            if (fileToCreate.createNewFile()) {
                System.out.println("File created sucessfully: " + fileToCreate.getName());
            }
        } catch (IOException e) {
            System.out.println("Cannot create file!");
        }
    }

    static void ReadFile(String path_and_filename){
        try {
            File fileToRead = new File(path_and_filename);
            Scanner myReader = new Scanner(fileToRead);
            System.out.println("Reading file "+path_and_filename+" :");
            while (myReader.hasNextLine()) {
                String data = myReader.nextLine();
                System.out.println(data);
            }
            myReader.close();
            System.out.println();
        } catch (FileNotFoundException e) {
            System.out.println("There is no such file "+"\"path_and_filename\""+".\n");
        }
    }

    // The StringBuilder in Java represents a mutable sequence of characters.
    // Java's built in String class is not mutable.
    static void saveArrayListToFile(List<Output> listToSave, String fileName, String sep) throws Exception {
        StringBuilder ans = new StringBuilder();
        for (Output record : listToSave) {
            ans.append(record.createOutput());
            ans.append(sep);
        }
        saveStringToFile(ans.toString(), fileName);
        System.out.println("\nData saved to "+ fileName);
    }

    static void saveArrayListToFile1(ArrayList<String> listToSave, String fileName, String sep){
        StringBuilder ans = new StringBuilder();
        for (Object record : listToSave) {
            ans.append(record.toString());
            ans.append(sep);
        }
        saveStringToFile(ans.toString(), fileName);
        System.out.println("\nList was saved to file "+fileName+"\n");
    }

    static void saveStringToFile(String data, String fileName){
        BufferedWriter bufferedWriter=null;
        try {
            bufferedWriter = new BufferedWriter(
                    new FileWriter(fileName,false));
            bufferedWriter.write(data);
        } catch (IOException e) {
            System.out.println("Cannot write to file");
        } finally {
            try {
                bufferedWriter.close();
            } catch (IOException e) {
                System.out.println("Cannot write to file");
            }
        }
    }
}

When I'm creating a new customer, I call it from a menu and it looks like this:

            switch (selection) {
                case 1:
                    try {
                        System.out.println("You're registering as a new customer");
                        String custID = ObjectIDs.generateID();
                        System.out.println("Enter first name:");
                        String firstName = sc.next();
                        System.out.println("Enter last name:");
                        String lastName = sc.next();
                        st.newCustomer(custID, firstName, lastName);
                        st.saveCustomersList();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;

the saveCustomerList() function is this:

@SuppressWarnings("unchecked")
    void saveCustomersList() throws Exception {
        CSV.saveArrayListToFile((List<Output>)(List<?>) customers, CUSTOMERS_FILE_PATH,"\n");
    }

And then the functions calls saveArrayListToFile() to save it.

The behavior is the same with Product and Employee projects, so I randomly chose to show how it acts when creating a new Product.

I hope I added enough information. If needed, I can paste more code in but I already feel it's very cluttered. Hopefully it's ok.

Thank you very much :)

Body:
In addition to pafau k. I would like to add some things at least I would do differently...

First of all:

Things that can cause errors or unexpected behaviour:

Everything below is in saveStringToFile

Like already pointed out the Initialisation of the BufferedWriter: It should be initialized like this:
bufferedWriter = new BufferedWriter(new FileWriter(filename, true));
This puts the File into appending mode (if you want to append to a file you can also get rid of the boolean (second argument) entirely because appending is standard: new FileWriter(filename))

If for some case the Creation of the BufferedWriter failed you will still have a null-pointing object as bufferedWriter. This however means that you will be surprised with a NullPointerException in your finally block. To prevent this first of all do a check in your finally block:

if (bufferedWriter != null) {
    // Close your bufferedWriter in here
}

Also, if you run into an error you will likely be presented with the same error message twice.

Now cosmetics:

Things that I would write differently for aesthetic reasons:

Java methods (and static "methods") are always starting with a small letter :)
This means it should be public static void createFile() for example or static void readFile()

variables and parameters of methods do not contain seperators like _ but instead if you want to make it more readable you start with a small letter and for each seperation you use a capital letter for that: e.g. String thisIsAVeryLongVariableWithALotOfSeperations = "Foo";

The generic types in saveArrayListToFile1() work like a placeholder. So you declare ArrayList<String> listToSave so you don't need a cast in the following for-loop: You can simply write:

for (String record : listToSave) {
    ans.append(record);
    ans.append(sep);
}

I hope this fixes all errors or complications. :)


问:
How to use std::valarray in if statement without redundant computation?
I have the following code with float, for example:

```<code>std::vector<float> v = {0.f, 1.f, 2.f};
for(size_t i = 0; i < v.size(); ++i)
    if(v[i] != 0) // An optimization for `v[i] != 0`.
        v[i] = v[i] * v[i] * v[i]; // Time-consuming computation.
</code>```

Now I want to replace float with std::valarray<float>:

```<code>using vfloat = std::valarray<float>;
std::vector<vfloat> v = {vfloat{0.f, 0.f}, vfloat{1.f, 0.f}, vfloat{2.f, 0.f}};
for(size_t i = 0; i < v.size(); ++i)
    // if(v[i] != 0) // I want this optimization! ◀◀◀◄◀◀◀◀◀◀◀◀◀◀◀◀◀◀◀◀◀◀◀◀◀◀◀◀◀◀◀◀
        v[i] = v[i] * v[i] * v[i]; // Time-consuming computation.
</code>```

Though the new code is correct logically, there are redundant computation for v[i][j] == 0.

So how to use std::valarray in if statement without the redundant computation?

答:
there many options:</p>

```<code>#include <valarray>
#include <algorithm>
// 2 loops in the worst case
bool is_not_zero_0(std::valarray<float> const & v)
{
    return v.max() != 0.0 || v.min() != 0.0;
}

// one loop
bool is_not_zero_1(std::valarray<float> const & v)
{
    return std::find_if(v.begin(), v.end(v), [](float item){return item != 0.0f;}) != v.end();
}
// 2 loops + allocation
bool is_not_zero_2(std::valarray<float> const & v)
{
    return (v != 0.0).max();
}
</code>```

Body:
I have the following code with float, for example:

std::vector<float> v = {0.f, 1.f, 2.f};
for(size_t i = 0; i < v.size(); ++i)
    if(v[i] != 0) // An optimization for `v[i] != 0`.
        v[i] = v[i] * v[i] * v[i]; // Time-consuming computation.

Now I want to replace float with std::valarray<float>:

using vfloat = std::valarray<float>;
std::vector<vfloat> v = {vfloat{0.f, 0.f}, vfloat{1.f, 0.f}, vfloat{2.f, 0.f}};
for(size_t i = 0; i < v.size(); ++i)
    // if(v[i] != 0) // I want this optimization! ◀◀◀◄◀◀◀◀◀◀◀◀◀◀◀◀◀◀◀◀◀◀◀◀◀◀◀◀◀◀◀◀
        v[i] = v[i] * v[i] * v[i]; // Time-consuming computation.

Though the new code is correct logically, there are redundant computation for v[i][j] == 0.

So how to use std::valarray in if statement without the redundant computation?

Body:
there many options:

#include <valarray>
#include <algorithm>
// 2 loops in the worst case
bool is_not_zero_0(std::valarray<float> const & v)
{
    return v.max() != 0.0 || v.min() != 0.0;
}

// one loop
bool is_not_zero_1(std::valarray<float> const & v)
{
    return std::find_if(v.begin(), v.end(v), [](float item){return item != 0.0f;}) != v.end();
}
// 2 loops + allocation
bool is_not_zero_2(std::valarray<float> const & v)
{
    return (v != 0.0).max();
}


问:
Getting access to an element in ngFor loop
I want to access the current image url (the url is a property of the image object) in the ngFor loop that loops through an array of image objects. I want to access this in same components typescript file.

I though that maybe I could bind to  property in the same components .ts file using the @Input() but I believe this is for a child/parent relationship.

How would I go about doing this in angular?

Relevant html:

```<code><div
      class="image-container"
      *ngFor="let image of galleryList"
      [imageUrl]="image.Url">

</div
</code>```

Relevant ts code:

```<code>  @Input() imageUrl: string;
</code>```

I am not expecting the above code to work but I just wanted to show you what I am trying to achieve.

Ultimately, I am trying to use the image url in this method within the typescript file:

```<code>checkLike(imageUrl: string): boolean {
    const users = this.usersService.getUsers();

    if(this.isLoggedIn) {
      const currUsersIndex = this.usersService.getCurrUserArrIndex();
      console.log(currUsersIndex);
      const FavouritesList = users[currUsersIndex].likes;

      if(FavouritesList.includes(imageUrl)) {
          return true;
      } else {
          return false;
      }
  }
    }
</code>```

Then conditionally add html elements depending on the result:

```<code> <div *ngIf="checkLike(image.imagePath)" class="heart-icon-container">
        <ion-icon name="heart" class="heart-icon" (click)="updateLike(image.imagePath)"></ion-icon>
      </div>
      <div *ngIf="!checkLike(image.imagePath)" class="heart-icon-container">
        <ion-icon name="heart-outline" class="heart-icon" (click)="updateLike(image.imagePath)"></ion-icon>
      </div>
</code>```

答:
Lets say you have a component abc-component, you can go with it like

```<code><abc-component *ngFor="let image of galleryList" [imageUrl]="image.Url"></abc-component>
</code>```

But you can't pass inputs to normal div, unless you change selector of your component as a attribute, For eg:-

```<code>@Component({
  selector: [abc-component],
   ....
  })
</code>```

Then you can use it like :-

```<code><div *ngFor="let image of galleryList" [imageUrl]="image.Url" abc-component></div>
</code>```

And have it inside Abc component like

```<code>@Input() imageUrl: string;
</code>```

Body:
I want to access the current image url (the url is a property of the image object) in the ngFor loop that loops through an array of image objects. I want to access this in same components typescript file.

I though that maybe I could bind to  property in the same components .ts file using the @Input() but I believe this is for a child/parent relationship.

How would I go about doing this in angular?

Relevant html:

</div

Relevant ts code:

  @Input() imageUrl: string;

I am not expecting the above code to work but I just wanted to show you what I am trying to achieve.

Ultimately, I am trying to use the image url in this method within the typescript file:

checkLike(imageUrl: string): boolean {
    const users = this.usersService.getUsers();

    if(this.isLoggedIn) {
      const currUsersIndex = this.usersService.getCurrUserArrIndex();
      console.log(currUsersIndex);
      const FavouritesList = users[currUsersIndex].likes;

      if(FavouritesList.includes(imageUrl)) {
          return true;
      } else {
          return false;
      }
  }
    }

Then conditionally add html elements depending on the result:

        <ion-icon name="heart" class="heart-icon" (click)="updateLike(image.imagePath)"></ion-icon>

        <ion-icon name="heart-outline" class="heart-icon" (click)="updateLike(image.imagePath)"></ion-icon>

Body:
Lets say you have a component abc-component, you can go with it like

<abc-component *ngFor="let image of galleryList" [imageUrl]="image.Url"></abc-component>

But you can't pass inputs to normal div, unless you change selector of your component as a attribute, For eg:-

@Component({
  selector: [abc-component],
   ....
  })

Then you can use it like :-

<div *ngFor="let image of galleryList" [imageUrl]="image.Url" abc-component></div>

And have it inside Abc component like

@Input() imageUrl: string;


问:
Postgres weekly timetable
I want to have a weekly timetable table in postgres, That stores availability time of a person in a week
Then I want to get a time range from a user and check that the person in available in that range or not

How can I do that in postgres , I know that Range type can help me, But I don't know how implement weekly tine table

```<code>timetable
╔════════╦═════════════╦═════════════════════╗
║ userId ║ day_of_week ║       during        ║
╠════════╬═════════════╬═════════════════════╣
║      1 ║           0 ║ [10:00:00 12:00:00] ║
║      1 ║           0 ║ [14:30:00 16:45:00] ║
║      1 ║           1 ║ [10:00:00 12:00:00] ║
║      1 ║           1 ║ [08:30:00 12:00:00] ║
║      1 ║           1 ║ [13:30:00 17:00:00] ║
║      1 ║           1 ║ [19:30:00 23:00:00] ║
║      1 ║           2 ║ [10:00:00 12:00:00] ║
╚════════╩═════════════╩═════════════════════╝
</code>```

day_of_week => The day of the week as Sunday(0) to Saturday(6)

I want to know , is user #1 available in a specific time range?

```<code>(2020-05-31 11:00, 2020-05-31 11:30) => true
(2020-05-31 11:00, 2020-05-31 15:30) => false
(2020-05-31 11:00, 2020-06-02 00:00) => false
</code>```

the timetable is just a sample , and You can offer better schema

A user can be available 24 hours, i.e in Sunday-Tuesday

```<code>timetable
╔════════╦═════════════╦═════════════════════╗
║ userId ║ day_of_week ║       during        ║
╠════════╬═════════════╬═════════════════════╣
║      2 ║           0 ║ [00:00:00 24:00:00] ║
║      2 ║           1 ║ [00:00:00 24:00:00] ║
║      2 ║           2 ║ [00:00:00 24:00:00] ║
╚════════╩═════════════╩═════════════════════╝

</code>```

So we have

```<code>(2020-05-31 11:00, 2020-05-31 11:30) => true
(2020-05-31 11:00, 2020-05-31 15:30) => true
(2020-05-31 11:00, 2020-06-02 00:00) => true
(2020-06-02 00:00, 2020-06-03 01:00) => false
</code>```

答:
If you can split up the input value (parameter) into each day, you can do something like this:

```<code>select bool_and(tt.user_id is not null)
from (
  values
    (0, timerange('11:00', '24:00')),
    (1, timerange('00:00', '24:00'))
) as t(dow, day_range)
  left join timetable tt
         on tt.day_of_week = t.dow
        and tt.during @> t.day_range
        and tt.user_id = 2;
</code>```

If you are limited by passing a timestamp range, you will need to turn the range into the corresponding number of days and time ranges per week day.

```<code>with parm (user_id, input) as (
  values
    -- these are essentially your query parameters
    (2, tsrange('2020-05-31 11:00', '2020-06-02 00:00', '[)'))
), days as (
  select user_id,
         timerange(
         case
           when g.day::date = lower(input)::date then lower(input)::time
           when g.day::date > lower(input)::date and g::date <= upper(input)::date then time '00:00'
         end,
         case
           when g.day::date = upper(input)::date then upper(input)::time
           when g.day::date < upper(input)::date and g::date >= lower(input)::date then time '24:00'
         end, '()') day_range,
         extract(dow from g.day) as dow
  from parm
    cross join generate_series(lower(input)::date, upper(input)::date, interval '1 day') as g(day)
)
select d.day_range as input_range,
       d.dow as input_dow,
       tt.user_id,
       tt.during,
       tt.user_id is not null as is_match
from days d
  left join timetable tt
         on d.dow = tt.day_of_week
        and tt.during @> d.day_range
        and tt.user_id = d.user_id
</code>```

Listing each column of the result is mainly there to show how it works. If you just need a true/false result, you can do bool_and(tt_user_id is not null) as I did in the first select. The first CTE (with parm)  can be removed if you replace the parameter in the second CTE directly.

Title: Postgres weekly timetable

Body:
I want to have a weekly timetable table in postgres, That stores availability time of a person in a week
Then I want to get a time range from a user and check that the person in available in that range or not

How can I do that in postgres , I know that Range type can help me, But I don't know how implement weekly tine table

timetable
╔════════╦═════════════╦═════════════════════╗
║ userId ║ day_of_week ║       during        ║
╠════════╬═════════════╬═════════════════════╣
║      1 ║           0 ║ [10:00:00 12:00:00] ║
║      1 ║           0 ║ [14:30:00 16:45:00] ║
║      1 ║           1 ║ [10:00:00 12:00:00] ║
║      1 ║           1 ║ [08:30:00 12:00:00] ║
║      1 ║           1 ║ [13:30:00 17:00:00] ║
║      1 ║           1 ║ [19:30:00 23:00:00] ║
║      1 ║           2 ║ [10:00:00 12:00:00] ║
╚════════╩═════════════╩═════════════════════╝

day_of_week => The day of the week as Sunday(0) to Saturday(6)

I want to know , is user #1 available in a specific time range?

(2020-05-31 11:00, 2020-05-31 11:30) => true
(2020-05-31 11:00, 2020-05-31 15:30) => false
(2020-05-31 11:00, 2020-06-02 00:00) => false

the timetable is just a sample , and You can offer better schema

A user can be available 24 hours, i.e in Sunday-Tuesday

timetable
╔════════╦═════════════╦═════════════════════╗
║ userId ║ day_of_week ║       during        ║
╠════════╬═════════════╬═════════════════════╣
║      2 ║           0 ║ [00:00:00 24:00:00] ║
║      2 ║           1 ║ [00:00:00 24:00:00] ║
║      2 ║           2 ║ [00:00:00 24:00:00] ║
╚════════╩═════════════╩═════════════════════╝

So we have

(2020-05-31 11:00, 2020-05-31 11:30) => true
(2020-05-31 11:00, 2020-05-31 15:30) => true
(2020-05-31 11:00, 2020-06-02 00:00) => true
(2020-06-02 00:00, 2020-06-03 01:00) => false

Body:
If you can split up the input value (parameter) into each day, you can do something like this:

select bool_and(tt.user_id is not null)
from (
  values
    (0, timerange('11:00', '24:00')),
    (1, timerange('00:00', '24:00'))
) as t(dow, day_range)
  left join timetable tt
         on tt.day_of_week = t.dow
        and tt.during @> t.day_range
        and tt.user_id = 2;

If you are limited by passing a timestamp range, you will need to turn the range into the corresponding number of days and time ranges per week day.

with parm (user_id, input) as (
  values
    -- these are essentially your query parameters
    (2, tsrange('2020-05-31 11:00', '2020-06-02 00:00', '[)'))
), days as (
  select user_id,
         timerange(
         case
           when g.day::date = lower(input)::date then lower(input)::time
           when g.day::date > lower(input)::date and g::date <= upper(input)::date then time '00:00'
         end,
         case
           when g.day::date = upper(input)::date then upper(input)::time
           when g.day::date < upper(input)::date and g::date >= lower(input)::date then time '24:00'
         end, '()') day_range,
         extract(dow from g.day) as dow
  from parm
    cross join generate_series(lower(input)::date, upper(input)::date, interval '1 day') as g(day)
)
select d.day_range as input_range,
       d.dow as input_dow,
       tt.user_id,
       tt.during,
       tt.user_id is not null as is_match
from days d
  left join timetable tt
         on d.dow = tt.day_of_week
        and tt.during @> d.day_range
        and tt.user_id = d.user_id

Listing each column of the result is mainly there to show how it works. If you just need a true/false result, you can do bool_and(tt_user_id is not null) as I did in the first select. The first CTE (with parm)  can be removed if you replace the parameter in the second CTE directly.


问:
Pip requirements outputting global packages
I have a Virtual env for my django project, but when I hit pip freeze, I get what must be a global site package list, includes too many packages, like ubuntu packages and so much irrelevant stuff. This happens whether virtualenv is active or not. My site packages list looks a bit slim too, so I wonder whether venv has been working at all.

```<code>(env)~/code/django/ssc/dev/env/lib/python2.7/site-packages> ls
django
Django-1.4-py2.7.egg-info
easy-install.pth
pip-1.0.2-py2.7.egg
setuptools-0.6c11-py2.7.egg
setuptools.pth
</code>```

What's my problem?

答:
I don't understand why the most concise option was just left in the comments. Since I have just almost missed it I will put it here as a separate answer with some tweaks.
You can add --local flag with your pip freeze if you are running a virtual env with system-site-packages enables.
So, if you had:

```<code>py -m venv --system-site-packages env
</code>```

To make sure you are not getting all system deps into your requirements.txt, just run:

```<code>python -m pip freeze --local > requirements.txt
</code>```

Another, a bit more elaborate option, but still viable because dependencies are not supposed to change all that often, would be to go into pyvenv.cfg file located in your virtual env library and manually change:

```<code>include-system-site-packages = true/false
</code>```

Body:
I have a Virtual env for my django project, but when I hit pip freeze, I get what must be a global site package list, includes too many packages, like ubuntu packages and so much irrelevant stuff. This happens whether virtualenv is active or not. My site packages list looks a bit slim too, so I wonder whether venv has been working at all.

(env)~/code/django/ssc/dev/env/lib/python2.7/site-packages> ls
django
Django-1.4-py2.7.egg-info
easy-install.pth
pip-1.0.2-py2.7.egg
setuptools-0.6c11-py2.7.egg
setuptools.pth

What's my problem?

Body:
I don't understand why the most concise option was just left in the comments. Since I have just almost missed it I will put it here as a separate answer with some tweaks.
You can add --local flag with your pip freeze if you are running a virtual env with system-site-packages enables.
So, if you had:

py -m venv --system-site-packages env

To make sure you are not getting all system deps into your requirements.txt, just run:

python -m pip freeze --local > requirements.txt

Another, a bit more elaborate option, but still viable because dependencies are not supposed to change all that often, would be to go into pyvenv.cfg file located in your virtual env library and manually change:

include-system-site-packages = true/false


问:
Is it possible to specify that that task.delay or task.apply_async should fail if celeryd isn't running?
I'm using djcelery and djkombu. I would've used RabbitMQ (as that's the recommended option), but RabbitMQ uses too much memory on my VPS and I'm being a cheapskate.

I'm thinking that it would be nice in my app for an exception to be thrown if my code tries to schedule a task and celeryd isn't running. This is sort of a backup plan for having celeryd monitored by supervisor, nagios, etc.

At first I thought the immediate setting would do this, but it didn't work with my Django ORM configuration.

答:
The problem is mainly that with this commands you are sending simple message to the broker, and basically the broker just store the message and wait for someone that consume it.
The cool thing of this architecture is that if a celery worker goes down for two our when it run again starts to fetch all the olds scheduled tasks that are in the broker.
If you don't want this behavior for your project maybe celery is the wrong choice.</p>


问:
Testing an implementation of an LSTM in Pytorch
I'm trying to use the Pytorch implementation of an LSTM here. I'm including it here for reference. It consists of two classes, LSTMCell and LSTM, where LSTMCell is just a single unit and LSTM puts stacks multiple units together to create a full LSTM model

```<code>import math
import torch as th
import torch.nn as nn

class LSTMCell(nn.Module):

    def __init__(self, input_size, hidden_size, bias=True):
        super(LSTM, self).__init__()
        self.input_size = input_size
        self.hidden_size = hidden_size
        self.bias = bias
        self.i2h = nn.Linear(input_size, 4 * hidden_size, bias=bias)
        self.h2h = nn.Linear(hidden_size, 4 * hidden_size, bias=bias)
        self.reset_parameters()

    def reset_parameters(self):
        std = 1.0 / math.sqrt(self.hidden_size)
        for w in self.parameters():
            w.data.uniform_(-std, std)

    def forward(self, x, hidden):
        if hidden is None:
            hidden = self._init_hidden(x)

        h, c = hidden
        h = h.view(h.size(1), -1)
        c = c.view(c.size(1), -1)
        x = x.view(x.size(1), -1)

        # Linear mappings
        preact = self.i2h(x) + self.h2h(h)

        # activations
        gates = preact[:, :3 * self.hidden_size].sigmoid()
        g_t = preact[:, 3 * self.hidden_size:].tanh()
        i_t = gates[:, :self.hidden_size]
        f_t = gates[:, self.hidden_size:2 * self.hidden_size]
        o_t = gates[:, -self.hidden_size:]

        c_t = th.mul(c, f_t) + th.mul(i_t, g_t)

        h_t = th.mul(o_t, c_t.tanh())

        h_t = h_t.view(1, h_t.size(0), -1)
        c_t = c_t.view(1, c_t.size(0), -1)
        return h_t, (h_t, c_t)

    @staticmethod
    def _init_hidden(input_):
        h = th.zeros_like(input_.view(1, input_.size(1), -1))
        c = th.zeros_like(input_.view(1, input_.size(1), -1))
        return h, c
</code>```

```<code>class LSTM(nn.Module):

    def __init__(self, input_size, hidden_size, bias=True):
        super().__init__()
        self.lstm_cell = LSTMCell(input_size, hidden_size, bias)

    def forward(self, input_, hidden=None):
        # input_ is of dimensionalty (1, time, input_size, ...)

        outputs = []
        for x in torch.unbind(input_, dim=1):
            hidden = self.lstm_cell(x, hidden)
            outputs.append(hidden[0].clone())

        return torch.stack(outputs, dim=1)
</code>```

I'm doing the following simple test:

```<code>x = torch.randn(1, 3, 2, 4)
model = LSTM(4, 5, False)
model(x)
</code>```

and I get the following error. What exactly is the problem here?

```<code>TypeError                                 Traceback (most recent call last)
<ipython-input-33-09e5544a61fc> in <module>
----> 1 model = LSTM(4, 5, False)

<ipython-input-30-9ad06cd4b768> in __init__(self, input_size, hidden_size, bias)
      3     def __init__(self, input_size, hidden_size, bias=True):
      4         super().__init__()
----> 5         self.lstm_cell = LSTMCell(input_size, hidden_size, bias)
      6
      7     def forward(self, input_, hidden=None):

<ipython-input-29-c91ddfb9dfae> in __init__(self, input_size, hidden_size, bias)
      6
      7     def __init__(self, input_size, hidden_size, bias=True):
----> 8         super(LSTM, self).__init__()
      9         self.input_size = input_size
     10         self.hidden_size = hidden_size

TypeError: super(type, obj): obj must be an instance or subtype of type
</code>```

答:
The first argument to super() should be class itself, not a different class.

class LSTMCell(nn.Module):

    def __init__(self, input_size, hidden_size, bias=True):
        super(LSTM, self).__init__()
#             ^^^^ self is not an instance of LSTM but LSTMCell
```

<p>It should be:</p>

<pre class="lang-py prettyprint-override"><code>super(LSTMCell, self).__init__()
</code>```

Since Python 3 you can omit the arguments to super to get the same result (as you have done in the LSTM class):

super().__init__()
```

Body:
I'm trying to use the Pytorch implementation of an LSTM here. I'm including it here for reference. It consists of two classes, LSTMCell and LSTM, where LSTMCell is just a single unit and LSTM puts stacks multiple units together to create a full LSTM model

import math
import torch as th
import torch.nn as nn

class LSTMCell(nn.Module):

    def __init__(self, input_size, hidden_size, bias=True):
        super(LSTM, self).__init__()
        self.input_size = input_size
        self.hidden_size = hidden_size
        self.bias = bias
        self.i2h = nn.Linear(input_size, 4 * hidden_size, bias=bias)
        self.h2h = nn.Linear(hidden_size, 4 * hidden_size, bias=bias)
        self.reset_parameters()

    def reset_parameters(self):
        std = 1.0 / math.sqrt(self.hidden_size)
        for w in self.parameters():
            w.data.uniform_(-std, std)

    def forward(self, x, hidden):
        if hidden is None:
            hidden = self._init_hidden(x)

        h, c = hidden
        h = h.view(h.size(1), -1)
        c = c.view(c.size(1), -1)
        x = x.view(x.size(1), -1)

        # Linear mappings
        preact = self.i2h(x) + self.h2h(h)

        # activations
        gates = preact[:, :3 * self.hidden_size].sigmoid()
        g_t = preact[:, 3 * self.hidden_size:].tanh()
        i_t = gates[:, :self.hidden_size]
        f_t = gates[:, self.hidden_size:2 * self.hidden_size]
        o_t = gates[:, -self.hidden_size:]

        c_t = th.mul(c, f_t) + th.mul(i_t, g_t)

        h_t = th.mul(o_t, c_t.tanh())

        h_t = h_t.view(1, h_t.size(0), -1)
        c_t = c_t.view(1, c_t.size(0), -1)
        return h_t, (h_t, c_t)

    @staticmethod
    def _init_hidden(input_):
        h = th.zeros_like(input_.view(1, input_.size(1), -1))
        c = th.zeros_like(input_.view(1, input_.size(1), -1))
        return h, c

class LSTM(nn.Module):

    def __init__(self, input_size, hidden_size, bias=True):
        super().__init__()
        self.lstm_cell = LSTMCell(input_size, hidden_size, bias)

    def forward(self, input_, hidden=None):
        # input_ is of dimensionalty (1, time, input_size, ...)

        outputs = []
        for x in torch.unbind(input_, dim=1):
            hidden = self.lstm_cell(x, hidden)
            outputs.append(hidden[0].clone())

        return torch.stack(outputs, dim=1)

I'm doing the following simple test:

x = torch.randn(1, 3, 2, 4)
model = LSTM(4, 5, False)
model(x)

and I get the following error. What exactly is the problem here?

TypeError                                 Traceback (most recent call last)
<ipython-input-33-09e5544a61fc> in <module>
----> 1 model = LSTM(4, 5, False)

<ipython-input-30-9ad06cd4b768> in __init__(self, input_size, hidden_size, bias)
      3     def __init__(self, input_size, hidden_size, bias=True):
      4         super().__init__()
----> 5         self.lstm_cell = LSTMCell(input_size, hidden_size, bias)
      6
      7     def forward(self, input_, hidden=None):

<ipython-input-29-c91ddfb9dfae> in __init__(self, input_size, hidden_size, bias)
      6
      7     def __init__(self, input_size, hidden_size, bias=True):
----> 8         super(LSTM, self).__init__()
      9         self.input_size = input_size
     10         self.hidden_size = hidden_size

TypeError: super(type, obj): obj must be an instance or subtype of type

Body:
The first argument to super() should be class itself, not a different class.

class LSTMCell(nn.Module):

    def __init__(self, input_size, hidden_size, bias=True):
        super(LSTM, self).__init__()
#             ^^^^ self is not an instance of LSTM but LSTMCell

It should be:

super(LSTMCell, self).__init__()

Since Python 3 you can omit the arguments to super to get the same result (as you have done in the LSTM class):

super().__init__()


问:
How to read Environment Variables in a PowerApp?
I'm looking for a quick example of how to read an Environment Variable in a PowerApp. https://learn.microsoft.com/en-gb/powerapps/maker/common-data-service/environmentvariables

I've set the variable in my environment. How do I simply read that in a PowerApp and display the current value in a text label?

答:
There are two steps to solve this:

Add the "Environment Variables Values" CDS Entity to you App's data source

Set the value of the text of the label to be:

LookUp('Environment Variable Values', 'Environment Variable Definition'.'Schema Name' = "test_environment_data").Value

Source of the solution: https://www.youtube.com/watch?v=DxjwpqcwI_Y