Query between two DNS servers

Question: I've been struggling with understanding DNS with BIND9.
I've used two machines, A and B, connected by a LAN and not for global use.
A is running a name server managing "example" domain and web server named "www".
B is running a name server managing "sub.example" domain as subdomain with delegation from A and web server named "www" too.
Configuration files are bellow.
"named.conf" for "example" at machine A.
options {
            directory "C:\dns\etc";
            recursion yes;
            version "XXX DNS Server 1.0X";

};
logging {
        channel my_file {
        file "c:\dns\etc\named.run" versions 5 size 1m;
        severity debug 0;
        print-category yes;
        print-severity yes;
        print-time yes;
        };
        category default {my_file;};
        category queries {my_file;};
        category lame-servers {my_file;};
        category config {my_file;};
};

zone "." {
            type hint;
            file "named.root";
};

zone "localhost" {
            type master;
            file "localhost/fwd";
            allow-update { none; };

};
zone "0.0.127.in-addr.arpa" {
            type master;
            file "localhost/rev";
            allow-update { none; };
};
zone "example" {
            type master;
            file "example/fwd";
            allow-update { none; };

};
zone "18.104.22.168.in-addr.arpa" {
            type master;
            file "example/rev";
            allow-update { none; };
};

"zone file" for "example" at machine A.
$TTL 1H
@ 1H    IN SOA example. postmaster.example. (
    200508291  ; Serial
          15M  ; Refresh
           5M  ; Retry
           1D  ; Expire
          15M) ; TTL

        IN  NS  ns.example.
        IN  A   172.16.11.72
ns      IN  A   172.16.11.72
www     IN  A   172.16.11.72

sub     IN  NS  ns.sub.example.
ns.sub.example. IN  A   172.16.10.37

"named.conf" for "example" at machine B.
options {
            directory "C:\dns\etc";
            recursion yes;
            version "unknown";
            allow-transfer {172.16.11.72; };

};
logging {
        channel my_file {
        file "c:\dns\etc\named.run" versions 5 size 1m;
        severity debug 0;
        print-category yes;
        print-severity yes;
        print-time yes;
        };
        category default {my_file;};
        category queries {my_file;};
        category lame-servers {my_file;};
        category config {my_file;};
};

zone "." {
            type hint;
            file "named.root";
};

zone "localhost" {
            type master;
            file "localhost/fwd";
            allow-update { none; };

};
zone "0.0.127.in-addr.arpa" {
            type master;
            file "localhost/rev";
            allow-update { none; };
};
zone "sub.example" {
            type master;
            file "example/fwd";
            allow-update { none; };
};
zone "18.104.22.168.in-addr.arpa" {
            type master;
            file "example/rev";
            allow-update { none; };
};
zone "example" {
    type forward;
    forward only;
    forwarders {
        172.16.11.72;
    };
};

"zone file" for "sub.example" at machine B.
$TTL 1H
@ 1H    IN SOA sub.example. postmaster.sub.example. (
    200508291  ; Serial
          15M  ; Refresh
           5M  ; Retry
           1D  ; Expire
          15M) ; TTL

        IN  NS  ns.sub.example.
        IN  A   172.16.10.37
ns      IN  A   172.16.10.37
www     IN  A   172.16.10.37

Now I have four servers in two machines like below.
"ns.example"  and "www.example"   in machine A.

"ns.sub.example" and  "www.sub.example" in machine B.

I can resolve "www.example" from A and "www.sub.example" from B.
But I can't resolve "www.sub.example" from A and "www.example" from B.
The messages dig command shows and being written in BIND's log are the bottom.
Both A and B are respond "SERVFAIL" or "connection timed out; no servers could be reached" but there are no error message in BIND's log.
Actually they are Windows2008 servers and I've changed windows firewall filter to accept UPD 53 port each other.
Strangely there are no DROP message, even ALLOW message too, in firewall log both A and B.
I mean if I dig "www.example" from A I can see ALLOW message but if I dig "www.sub.exaple" I can't see ALLOW and DROP message.
I think I have to classify this problem into BIND caused or Windows firewall caused.
What do I have to do first?
For example, I guess DNS query can't be sent to another machine of name server. That's why dig showed message "no servers could be reached".
How can I check DNS query is sent or NOT and why if it's not sent?
dig at machine A.
C:\dns\bin>dig www.sub.example

; <<>> DiG 9.9.2-P1 <<>> www.sub.example
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: SERVFAIL, id: 1777
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;www.sub.example.               IN      A

;; Query time: 0 msec
;; SERVER: 172.16.11.72#53(172.16.11.72)
;; WHEN: Wed Mar 13 08:42:04 2013
;; MSG SIZE  rcvd: 44

C:\dns\bin>dig @172.16.10.37 www.sub.example. a

; <<>> DiG 9.9.2-P1 <<>> @172.16.10.37 www.sub.example. a
; (1 server found)
;; global options: +cmd
;; connection timed out; no servers could be reached

dig at machine B.
C:\dns\bin>dig www.example

; <<>> DiG 9.9.2-P1 <<>> www.example
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: SERVFAIL, id: 39790
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096

;; QUESTION SECTION:
;www.example.                   IN      A

;; Query time: 4015 msec
;; SERVER: 172.16.10.37#53(172.16.10.37)
;; WHEN: Wed Mar 13 09:40:31 2013
;; MSG SIZE  rcvd: 40

C:\dns\bin>dig @172.16.11.72 www.example. a

; <<>> DiG 9.9.2-P1 <<>> @172.16.11.72 www.example
; (1 server found)
;; global options: +cmd
;; connection timed out; no servers could be reached

BIND's log at A.
13-Mar-2013 14:43:22.624 general: info: managed-keys-zone: loaded serial 0
13-Mar-2013 14:43:22.624 general: info: zone 126.96.36.199.in-addr.arpa/IN: loaded serial 200508291
13-Mar-2013 14:43:22.624 general: info: zone 0.0.127.in-addr.arpa/IN: loaded serial 200508291
13-Mar-2013 14:43:22.624 general: info: zone example/IN: loaded serial 200508291
13-Mar-2013 14:43:22.624 general: info: zone localhost/IN: loaded serial 200508291
13-Mar-2013 14:43:22.624 general: notice: all zones loaded
13-Mar-2013 14:43:22.624 general: notice: running
13-Mar-2013 14:43:22.624 notify: info: zone example/IN: sending notifies (serial 200508291)
13-Mar-2013 14:43:22.624 notify: info: zone 126.96.36.199.in-addr.arpa/IN: sending notifies (serial 200508291)
13-Mar-2013 14:44:34.515 queries: info: client 172.16.11.72#58221 (www.sub.example): query: www.sub.example IN A +E (172.16.11.72)
13-Mar-2013 14:44:39.515 queries: info: client 172.16.11.72#58221 (www.sub.example): query: www.sub.example IN A +E (172.16.11.72)
13-Mar-2013 14:44:44.515 queries: info: client 172.16.11.72#58221 (www.sub.example): query: www.sub.example IN A +E (172.16.11.72)

BIND's log at B
13-Mar-2013 14:38:27.281 general: info: managed-keys-zone: loaded serial 0
13-Mar-2013 14:38:27.281 general: info: zone 0.0.127.in-addr.arpa/IN: loaded serial 200508291
13-Mar-2013 14:38:27.281 general: info: zone 188.8.131.52.in-addr.arpa/IN: loaded serial 200508291
13-Mar-2013 14:38:27.281 general: info: zone sub.example/IN: loaded serial 200508291
13-Mar-2013 14:38:27.281 general: info: zone localhost/IN: loaded serial 200508291
13-Mar-2013 14:38:27.296 general: notice: all zones loaded
13-Mar-2013 14:38:27.296 general: notice: running
13-Mar-2013 14:38:27.296 notify: info: zone sub.example/IN: sending notifies (serial 200508291)
13-Mar-2013 14:38:27.296 notify: info: zone 188.8.131.52.in-addr.arpa/IN: sending notifies (serial 200508291)
13-Mar-2013 14:46:08.984 queries: info: client 172.16.10.37#58326 (www.sub.example): query: www.sub.example IN A +E (172.16.10.37)
13-Mar-2013 14:46:11.250 queries: info: client 172.16.10.37#58330 (www.example): query: www.example IN A +E (172.16.10.37)
13-Mar-2013 14:46:17.250 queries: info: client 172.16.10.37#58330 (www.example): query: www.example IN A +E (172.16.10.37)

Answer: Assuming that both domais aren't properly registered, since you said they aren't for "global use", I would say that your servers doesn't know each other, and they also don't have information about domain and sub domain.
You have to create a delegation clause on A to tell where the DNS for sub.example is. And a forward clause on B to redirect the queries to example.
Comment: I add my zone file ,see edited.I think "sub.example" already has been delegated .
Answer: There are a couple of things I can think to suggest.
The first is that the return code you are getting in the failed digs is SERVFAIL.  There can be several reasons for that, but one that you want to rule out first is that something is preventing name queries between machines.  I recognize that you say you have turned off the Windows firewall rule for port 53 UDP, but I would suggest you demonstrate (to yourself, at least) that Machine B can do a "dig @machine-a www.example. a" and get the answer you expect.  Then check that Machine A can query the server at Machine B.
Apart from that, it would be very helpful to see your named.conf and any messages named is logging.  Showing us named.conf would allow us to check that your zones are specified correctly (most of the RRs in your zone files are relative to the zone origin so it's important to see how the zone is loaded so we can see what the origin really is..) and will help us figure out if either of the machines, or both, are supposed to be performing recursion.
Please provide more information; it will make it much easier to determine what is going on.
EDIT:
The dig output you have provided certainly makes it sound like the servers are not receiving requests from other machines:
; <<>> DiG 9.9.2-P1 <<>> @172.16.10.37 www.sub.example. a
; (1 server found)
;; global options: +cmd
;; connection timed out; no servers could be reached

and
; <<>> DiG 9.9.2-P1 <<>> @172.16.11.72 www.example
; (1 server found)
;; global options: +cmd
;; connection timed out; no servers could be reached

that would seem significant, wouldn't you agree?
Comment: I add result of "dig@machine-a www.example. a" from A and from B. And up named.comf.See edited.
Comment: Yes, I would. And there is no messaged in BIND's log.I think query packet isn't sent anywhere out of machine.
Comment: Well, something's not getting through anyway..
Comment: See edited quetion.
Comment: "How can I check DNS query is sent or NOT and why if it's not sent?"  Sometimes there's no substitute for a packet sniffer.  Wireshark is free, very capable, and pretty easy to use.
Comment: Acording to our network administrator there is a firewall between machine-A and B. The firewall had blocked some packets including dns .Now machine-A and B can connect each other.

Query between two DNS servers


AppDelegate not being called on iOS / Allegro 5

Question: -- I have this in my appdelegate.m among all the other default calls: --
<code>@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    [self.window makeKeyAndVisible];
    NSLog(@"Launched");
    return YES;
}
</code>
-- My main.mm looks like this: --
<code>#import <UIKit/UIKit.h>

#import "AppDelegate.h"
#include <allegro5/allegro.h>
ALLEGRO_DISPLAY *Display;

int main(int argc, char *argv[])
{
    al_init();
    al_set_new_display_option(ALLEGRO_SUPPORTED_ORIENTATIONS,
                           ALLEGRO_DISPLAY_ORIENTATION_LANDSCAPE,ALLEGRO_REQUIRE);
    Display = al_create_display(960, 640);
    printf("%d, %d", al_get_display_width(Display),
                    al_get_display_height(Display));

    return 0;
}
</code>
As soon as i include allegro.h and all the required libraries/frameworks in my project and call al_init() inside main, the program stops printing "Launched". It seems like the AppDelegate is being ignored totally. Anyone got any tips ???
Comment: override -(void)applicationDidBecomeActive:(UIApplication *)application in AppDelegate and put a NSLog in there to see if it's getting there. Then you know it's getting to App Delegate if you debug prints.
Comment: you can also look at -(void)applicationWillEnterForeground:(UIApplication *)application
Answer: Allegro programs are meant to be cross platform at the source code level. Thus you do not need to provide an AppDelegate, as Allegro already does that. OS events will be converted into Allegro events that you can respond to in a cross platform way.
If you really need your own AppDelegate, then you'd need to edit Allegro's source code.
Answer: instead of returning 0 you should set an integer return value:
<code>   int retVal = UIApplicationMain(argc, argv, nil, nil);
   return retVal;
</code>
or
<code>   return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
</code>
That may work.
Comment: I tried return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
///---///
The app compiles but gives this as output:
///---///
*** Assertion failure in void UIApplicationInstantiateSingleton(Class)(), /SourceCache/UIKit_Sim/UIKit-2380.17/UIApplication.m:2037

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'There can only be one UIApplication instance.'
Comment: did the first one, UIApplicationMain(argc, argv, nil, nil); produce different results?
Comment: What did you mean by overriding applicationDidBecomeActive? Do i have   to create a subclass of UIApplicationDelegate and then override the method of the superclass (in his case UIApplicationDelegate)?  I'm a beginner iOS programmer.
Comment: No just add that to your AppDelegate.m and put NSLog(@"Launched"); in there to see if the app is reaching your AppDelegate at all.
Comment: Well i have tried. Actually i have placed NSLog to every one of the methods in AppDelegate.h (applicationDidEnterBackground, applicationWillEnterForeground, etc.). Nothing comes to the output.
Answer: Allegro 5.1 didn't have the rootViewController set. I modified the allegro source code to do that and everything works now on iOS 6.0/6.1.
I added shouldAutorotate and supportedInterfaceOrientations methods to ViewController.m
and added window.rootViewController = vc; in "static void iphone_add_screen(UIScreen *screen)" -method under allegroAppDelegate.m. Then i recompiled allegro.
Thank you all for your help.


How can I monitor the network on ESXi node for connections and find the source of a ddos attack

Question: I'm looking for something fairly simple to install, that will monitor network traffic on the vswitch for my esxi node as we're getting ddosed and I need to be able to easily see where the traffic is coming from.
Answer: On the actual vSwitch? Well you can't as far as I know that's a limitation of the in-built vSwitch, you can with Cisco's Nexus V1000 alternative virtual switch replacement but not with the regular one.
To be honest you should really be dealing with things like DOS issues before it hits your ESX boxes, via a router/firewall/LB etc. These will obviously let you deal with their own interfaces and help you finding out where your DOS is coming from.
If the DOS is internally-sourced (i.e. one VM attacking another) then again you should be able to use the VM's own OS tool set to find that out. The in-built vSwitch does provide some diagnostics/metrics but not that many, even with the next version of code.
Comment: Allowing promiscuous mode on a vSwitch then hooking another VM to that vSwitch running tcpdump would be one option.
Comment: @Shane, does that really work reliably? I tried it with the very first v4 release and had problems, not tried it since tbh.
Comment: I can't say if it scales well, but it's working great for my test snort install at home.


One-sided measurement error: $\widetilde{X} = X - \eta$, $\eta\geq0$. Is $E[X|\widetilde{X}=\widetilde{x}]$ increasing in $\widetilde{x}$?

Question: Let $X\geq0$, $\eta\geq0$ and $X,\eta$ independent. We measure $X$ with a one-sided error: $\widetilde{X} = X - \eta$.
Is $E[X|\widetilde{X}=\widetilde{x}]$ increasing in $\widetilde{x}$?
Comment: I was reading $\eta$ as a *parameter* (in accordance with statistical convention, and for which the answer is trivial), but then it makes no sense to declare it as independent of $X$ (since it's a fixed quantity). ... so just to confirm, $\eta$ is another random variate? [If so, why not stick to the convention of uppercase Roman letters?]
Comment: Yes, $\eta$ is a random variable. $\eta$ looks like an *n* as in *noise* and is typically chosen in the context of measurement error, e.g. [$\eta$ on Wikipedia](https://en.wikipedia.org/wiki/Errors-in-variables_models#Specification).
Comment: Ah, thanks, in the context of an error term it makes sense. Which variable are you taking expectation over in $E[X|\widetilde{X}=\widetilde{x}]$? I guess this is $E_X[X|\widetilde{X}=\widetilde{x}]$ but did you intend $E_\eta[X|\widetilde{X}=\widetilde{x}]$ (or perhaps something else) instead?
Comment: In this case it doesn't matter what variable the expectation is taken over, because  conditioning on $\widetilde{X}$ implies that $X$ and $\eta$ determine each other.
Answer: Assuming the expectation is over $X$:
$E_X[X|\widetilde{X}=\widetilde{x}]=E_X[X|X-\eta=\widetilde{x}]=E_X[X|X=\widetilde{x}+\eta]=\widetilde{x}+\eta$,
which is not increasing in $\widetilde{x}$ because of the  $\eta$.
However, $E_\eta[E_X(X|\widetilde{x})]=\widetilde{x}+E(\eta)$ would (naturally) be increasing in $\widetilde{x}$.
Comment: Can you clarify what you mean by *"because of the $\eta$"*? Also, why is $E_\eta[E_X(X|\widetilde{x})] = \widetilde{x}$?
Comment: The second thing is an error on my part (now corrected): I took something you'd refer to as a noise term to be mean $0$ but it's not in your case. With the respect to the first thing, because $\eta$ is a random variable; you could increase $\tilde{x}$ but $\tilde{x}+\eta$ might not increase with it.
Comment: I don't follow $E_\eta[E_X(X|\widetilde{x})]=\widetilde{x}+E(\eta)$: Taking $E_\eta$ on the outside expectation implies that the conditioning variable $\widetilde{X}$ in the inside expectation also varies, and hence the right-hand side cannot contain a simple $\widetilde{x}$. On the other hand, $E_{\eta|\widetilde{X}=\widetilde{x}}[E_X(X|\widetilde{x})]=\widetilde{x} + E(\eta|\widetilde{X}=\widetilde{x})$, which by itself doesn't provide a definite answer to the question.
Comment: "Taking Eη on the outside expectation implies that the conditioning variable X˜ in the inside expectation also varies," ...  -- I don't think so
Comment: Take the setup from my answer, and let $\widetilde{x}=4$. In that setup $E[\eta]=1$, and hence $\widetilde{x} + E[\eta]=5$, which is larger than any value in the support of $X$, $\eta$ and $\widetilde{X}$.
Comment: I believe your answer does not correctly deal with $\eta$.
Answer: No, not in general.
Let $P(X=3)=P(X=4)=P(\eta=0)=P(\eta=2)=0.5$.
Then,

$E[X|\widetilde{X}=1] = 3$
$E[X|\widetilde{X}=2] = 4$
$E[X|\widetilde{X}=3] = 3$
$E[X|\widetilde{X}=4] = 4$

which is neither increasing nor decreasing in $\widetilde{x}$.
If we modify the original question, and require $X$ and $\eta$ to have continuous or common support, the counterexample remains valid if sufficent probability mass is put on above values.


OnClick or something inside Select

Question: My select option looks this way,
<select class="chosen">
    <option value='' disabled selected style='display:none;'>Add new criteria</option>
    @foreach (var item in Model)
    {
       <option value="" onclick="ShowMessage(@item.Criteria);">@item.Criteria</option>
    }
</select>

The foreach loop has somewhere around 20 values i need individual values of them when they are clicked..I cannot use onclick on select because values are inside foreach.
My jquery
function ShowMessage(criteria) {
    alert(criteria);
}

Let me know what to use instead of onclick="" because isn't working...
Comment: I think you are looking for the change event
Comment: Yes, should work inside foreach which is inside select...
Comment: `onclick="ShowMessage('@item.Criteria');"`
Answer: Since you are using jquery, You should bind change event using it.
HTML
<select class="chosen">
    <option value='' disabled selected style='display:none;'>Add new criteria</option>
    @foreach (var item in Model)
    {
       <option value="" data-criteria="@item.Criteria">@item.Criteria</option>
    }
</select>

Script
$(document).ready(function() {
    $('.chosen').on('change', function(){
        var criteria = $(this).find('option:selected').data('criteria');
        //As per your current html, you can use .text() also
        //var criteria = $(this).find('option:selected').text();
        alert(criteria);
    });
});

Answer: You have to use change event from jQuery. Your @foreach will change like below.
Note: I think you forgot to add value="@item.Criteria" to the option tag.
<select class="chosen">
    <option value='' disabled selected style='display:none;'>Add new criteria</option>
    @foreach (var item in Model)
    {
       <option value="@item.Criteria">@item.Criteria</option>
    }
</select>

And you add .change event on select that will alert the value.
$('.chosen').change(function(){
   alert($(this).val());
});

$('.chosen').change(function() {
  alert($(this).val());
});

<select class="chosen">
  <option value='' disabled selected style='display:none;'>Add new criteria</option>
  <option value="Criteria1">Criteria1</option>
  <option value="Criteria2">Criteria2</option>
  <option value="Criteria3">Criteria3</option>

</select>
Answer: Click on option elements will fail in some browsers. You should rather bind the change event on select element, and then get selected option in it:
$('select.chosen').change(function(){
   alert($(this).find('option:selected').text())
});

Answer: Can you try as follow:-
<option value="" onclick="ShowMessage('@item.Criteria');">@item.Criteria</option>

Answer: Just call Change function of select box
$(".chosen").change(function(){
    alert($(this).val());
});

and you can also get select value without calling change method like
alert($(".chosen").val());

OnClick or something inside Select


Should OutputStream be closed explicit in StreamingResponseBody?

Question: I'm streaming a big external resource using StreamingResponseBody in a spring @RestController.
Question: should the OutputStream of the response body be closed explicit here?
@GetMapping("/{filename}")
public ResponseEntity<StreamingResponseBody> getFile(@PathVariable String filename) {
            StreamingResponseBody responseBody = outputStream -> {
                resolveExternally(outputStream, response, filename);

                //TODO should the stream be closed explicit here?
                //outputStream.close();
            };

            return ResponseEntity.ok(responseBody);
}

Comment: No... If you don't open/manage/own the stream you don't close it. The stream is managed externally and hence you, apart from writing to it, don't close it. You don't know what the other party (which manages the stream) wants to do with it (maybe write some more data).
Comment: No. You aren't opening the outputstream here, so no you don't close it. You would close the `InputStream` you used for reading the file.
Comment: sth similar https://stackoverflow.com/questions/48653533/streamingresponsebody-reuses-outputstream-if-not-closed-is-this-expected
Comment: I know, but that question is without any answer, thus useless. The question is still: should the stream be closed, or not?
Comment: @M.Deinum So did I get it right: if I'd resolve the file manually eg on my local system, it would be fine closing the outputstream inside the StreamingBodyResponse. But if I resolve it externally eg through another webservice, I should not touch it? In case, would you mind adding that as a real answer, so I can accept it?
Comment: Oh that's right, ok!
Answer: It will be closed by the container. Check StreamingResponseBodyReturnValueHandler.
OutputStream wraps a native HttpServletResponse.
HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class);
Assert.state(response != null, "No HttpServletResponse");
ServerHttpResponse outputMessage = new ServletServerHttpResponse(response);

If you follow it down you will get to CoyoteAdapter which is calling finishResponse() and closing OutputBuffer.
Comment: Can you share where is the `CoyoteAdapter` and `finishResponse()`? I don't see any call to close() of the HttpServletResponse


Return retorna só o primeiro valor da lista (python)

Question: estou criando um bot no discord que exibe os filmes disponíveis no cinema. Ocorre que eu já tinha um Web Crawler em Python que fazia isso, e decidi colocá-lo para funcionar no bot, ao executar um comando. No entanto, ao executá-lo pelo bot ele só retornava o primeiro valor da lista de filmes, então coloquei o return fora do for pensando que resolveria, mas agora ele retorna só o segundo filme, ao invés de retornar todos (gostaria de exibir todos os filmes disponíveis pelo bot).
Como faço para retornar todos os valores de "t1" ?
# bot.py
import os
import random
import discord
from dotenv import load_dotenv
import bs4
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
import pandas as pd

load_dotenv()
TOKEN = os.getenv('token')
GUILD = os.getenv('Teste BOT')

client = discord.Client()

@client.event
async def on_ready():
    for guild in client.guilds:
        if guild.name == GUILD:
            break

print(
    f'{client.user} is connected to the following guild:\n'
    f'{guild.name}(id: {guild.id})\n'
)

members = '\n - '.join([member.name for member in guild.members])
print(f'Guild Members:\n - {members}')

@client.event
async def on_message(message):
    if message.author == client.user:
        return

def filmes():
    my_url = 'https://iguatemi.com.br/saocarlos/cinema/'
    uClient = uReq(my_url)
    page_html = uClient.read()
    uClient.close()
    page_soup = soup(page_html, "html.parser")

    for title in page_soup.select('li.node-readmore > a'):
        t1 = title.get('title')
    return t1

if message.content == 'filmes!':
    response = filmes()
    await message.channel.send(response)

client.run('Token')

Comment: `t1 = title.get('title')` - você está "sobrescrevendo" o valor de `t1` no loop. Se a ideia é retornar uma lista de itens, você deve fazer o `append` com todos os valores obtidos em `title.get('title')`.
Comment: Postei uma solução, porem ainda estava editando alguns erros de digitação, porém agora esta certo, de uma conferida agora e se tiver alguma duvida só dizer, e o caso de usar o embed é bem legal, você ira curtir
Answer: há muitas coisas que te aconselho mudar em seu código, vamos ao primeiro problema seu.
    for title in page_soup.select('li.node-readmore > a'):
        t1 = title.get('title')
    return t1

desse jeito você só retornará um resultado, como concertar? você pode transformar em um objeto Generator ou criar uma lista e fazer voltar já como uma string formatada, colocarei uma resolução a baixo
    titulos = []
    for title in page_soup.select('li.node-readmore > a'):
        titulos.append(title.get('title'))
    return '\n'.join(titulos)

agora uma coisa que aconselho mudar em sua estrutura

o discord.py utilizar os métodos assíncronos, aconselho repensar na sua estrutura para que sempre utilize eles pois você pode parar todos os processos de seu funcionamento por conta de uma função mais demorada tirando toda a vantagem

outra coisa que te aconselho na hora de manda a lista dos títulos do filmes é usar o Embed, você pode pegar instruções por aqui, é bem fácil de usar.

Edit
se quiser pode tentar criar um generator e caso prefira mandar um titulo por vez
def filmes():
    my_url = 'https://iguatemi.com.br/saocarlos/cinema/'
    uClient = uReq(my_url)
    page_html = uClient.read()
    uClient.close()
    page_soup = soup(page_html, "html.parser")

    for title in page_soup.select('li.node-readmore > a'):
        yield title.get('title')

if message.content == 'filmes!':
    for response in filmes():
        await message.channel.send(response)

edit 2
um exemplo de como ficaria se usasse o Embed.
def filmes():
    my_url = 'https://iguatemi.com.br/saocarlos/cinema/'
    uClient = uReq(my_url)
    page_html = uClient.read()
    uClient.close()
    page_soup = soup(page_html, "html.parser")

    msg = discord.Embed(title=f'{" Lista de Filmes ":-^30}', colour=discord.Colour.dark_blue())
    for title in page_soup.select('li.node-readmore > a'):
        msg.add_field(name='Filme', value=title.get('title'), inline=False)
    return msg

if message.content == 'filmes!':
    msg = filmes()
    await message.channel.send(embed=msg)

Comment: Deu certo, muito obrigado ! consegui utilizar as soluções do edit 1 e 2, bem completa a resposta e ajudou bastante !
Comment: sempre há um jeito diferente de se fazer, só cabe escolher o melhor  na sua percepção, passei 3 alternativas e espero que ajude bastante e se foi útil não se esqueça de marca as postagens como aceitas.


tf.exe checkin does not find any pending changes

Question: I'm trying a simple TFS 2015 checkin automation. I have a local workspace mapped to a local folder. In that folder, I added some files and deleted one. What I want is to checkin those changes
I run this command
tf.exe checkin <folder> /recursive

The result (unexpected) is "no pending changes"
If I run the following command
tx.exe status <folder>

I get the list of changes expected.
If I look in Visual Studio source code explorer, I cannot see my changes but if I try to add items in the TFS folder, I can see new files.
Can you help me to understand how I can automate this checkin scenario?
EDIT : based on this SO thread, it seems it is not possible to achieve this with local workspace. Can someone confirm?
Comment: With running `tf status` command, your files should under "detected changes" not "changes", actually there are not "*real pending changes*"
Answer: You need to run "tf add" command to add these files into Pending Changes before run "tf checkin" command:
tf add * /recursive


How to flash the background of a website?

Question: I am trying to create a function that flashes the website every time you call it.
My approach was like this:

function flashRed() {
  document.body.style.animation = "redflash 0.06s 2 alternate ease-out";
}

function flashGreen() {
  document.body.style.animation = "greenflash 0.06s 2 alternate ease-out"
}
@keyframes redflash {
  to {
    background-color: #FFCDD2;
  }
}
@keyframes greenflash {
  to {
    background-color: #C8E6C9;
  }
}
<input type="button" value="RED" onclick="flashRed()">
<input type="button" value="GREEN" onclick="flashGreen()">

But this way I can only make it flash it once, as the animation attribute is set the first time or changes.
How can I change my code, or use a different approach to make it flash as often as i want?
Answer: Add and remove the class with an timeout...

function flashRed() {
  document.body.classList.add('red')
  window.setTimeout(function() {
    document.body.classList.remove('red')
  }, 100)

}

function flashGreen() {
  document.body.classList.add('green')
  window.setTimeout(function() {
    document.body.classList.remove('green')
  }, 100)
}
@keyframes redflash {
  to {
    background-color: #FFCDD2;
  }
}
@keyframes greenflash {
  to {
    background-color: #C8E6C9;
  }
}
.red {
  animation: redflash 0.06s 2 alternate ease-out;
}
.green {
  animation: greenflash 0.06s 2 alternate ease-out;
}
<input type="button" value="RED" onclick="flashRed()" id="red">
<input type="button" value="GREEN" onclick="flashGreen()" id="green">
Comment: @juwi Haha! But, when you get a taste of it, you'll love it!
Answer: You need to clear .animation once each function gets executed. Having said that, also, by clearing them you would  not be able to see the effect so you'll have to use setTimeOut
 function flashRed() {
    setTimeout(function(){  document.body.style.animation = "redflash 0.06s 2 alternate ease-out"; }, 100);

      document.body.style.animation="";
    }

function flashGreen() {
setTimeout(function(){  document.body.style.animation = "greenflash 0.06s 2 alternate ease-out" }, 100);
  document.body.style.animation = "";
}

https://jsfiddle.net/7t1m517t/1/
Answer: Instead of using keyframe animations, simply add/remove the classes with timeouts.
Here's a function where you can pass in the class, and the number of times you want to flash:
https://jsfiddle.net/1vzzaqvw/2/
function flash(cssClass, flashNum, delay, total) {
    if(typeof total === 'undefined') total = 1;
  if(typeof delay === 'undefined') delay = 500;
  document.body.classList.add(cssClass);
  setTimeout(function(){
    document.body.classList.remove(cssClass);
    if(total < flashNum) {
        setTimeout(function(){
            flash(cssClass, flashNum, delay, ++total);
        },delay);
    }
  },delay);
}

Then you can call it, i.e flash('red', 3), which would flash the red class 3 times.
Answer: As you said, the animation can only be run once because the animation property has been set. To allow for the same animation to be run multiple times in a row you can clear the animation property after the animation has completed.
There are a few ways to do this, one simple way is with a window.setTimeout function.

function flashRed() {
  document.body.style.animation = "redflash 0.6s 2 alternate ease-out";
  window.setTimeout(function(){
    document.body.style.animation = "";
  }, 0600);
}

function flashGreen() {
  document.body.style.animation = "greenflash 0.6s 2 alternate ease-out";
  window.setTimeout(function(){
    document.body.style.animation = "";
  }, 0600);
}
@keyframes redflash {
  to {
    background-color: #FFCDD2;
  }
}
@keyframes greenflash {
  to {
    background-color: #C8E6C9;
  }
}
<input type="button" value="RED" onclick="flashRed()">
<input type="button" value="GREEN" onclick="flashGreen()">


Table of dihedral group D4

Question: Let G be the group {e,a,b,b$^{2}$,b$^{3}$,ab,ab$^{2}$,ab$^{3}$} whose generators satisfy a$^{2}$=e,b$^{4}$=e, ba=ab$^{3}$. Write the table of G. (G is called dihedral group D4)

However, there are some elements that are not in the group like B$^2$ so I have to rewrite it but I do not know how to re-write it. I know for A$^2$=e since that is given but how do i apply it to other elements. For example, A$^2$B$^2$, it is not a column or row.Therefore it is not in group G and must be rewritten, how can i rewrite it?
Comment: What is your question?
Comment: Label as you like colors (or shades of gray) in the second picture here: http://mathworld.wolfram.com/DihedralGroupD4.html
Comment: I edited the post, but the main question is in this part: For example, A$^2$B$^2$, it is not a column or row.Therefore it is not in group G and must be rewritten, how can i rewrite it?
Comment: Keep in mind you cannot assume $ab$ is equal to $ba$, so many of your entries are wrong even before you simplify them. For example you have $ab*ab = a^2 b^2$ when in fact, $ab*ab = abab$.
Comment: I cannot comment the previous answer. I want to clarify that, because D4 is a group, we know that the operation is associative, but we don't know if it is conmutative (in fact, it isn't and your errors come from here) Also, the sentence "Also to help check your work each row and column of the table should have one and only one of the elements as a product." holds because of the cancelative property that every group has because every element has its inverse element, and it is unique. Anyway, you can't even use associavity or cancelation if you have to prove that D4 is a group and, in this case,
Answer: First remember that the group operation here is concatenation, which is to say, $a\ast b=ab$. Some of your entries are incorrect even before you simplify them. Like I said in the comments, $ab\ast ab=abab$, not $a^2b^2$.
Next, use the relations to simplify the entries.
For example, $a\ast a=a^2$ but from the relations $a^2=e$, so $a\ast a=e$.
Also, $ab\ast b^3=ab^4$ and from the relations $b^4=e$, so $ab\ast b^3=a$.
Some of these simplifications may take a few steps: $ab\ast ab=abab=a(ba)b=a(a b^3)b=a^2\ast b^4=e$
Also to help check your work each row and column of the table should have one and only one of the elements as a product.
Comment: Wow. thanks for making it simple.

Table of dihedral group D4


No longer have permission to compile via gcc after upgrading to Windows 10

Question: The other day I downloaded cygwin and gcc in order to learn C on my Windows 7 machine.  I made a couple "Hello World!"-style programs, compiled them, etc., everything was fine.  Today I upgraded to Windows 10, and now when I try to compile, I get this error message:
<code>$ make [FILENAME]
cc -Wall -g    [FILENAME].c   -o [FILENAME]
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/../../../../x86_64-pc-cygwin/bin/ld: cannot open output file [FILENAME].exe: Permission denied
collect2: error: ld returned 1 exit status
make: *** [<builtin>: [FILENAME]] Error 1
</code>
I've searched through a few other threads discussing this error, but have found nothing that solves my problem.  Many suggestions were things I had no idea how even to do!  For example, one person said in a similar thread, "Move to a directory where you are allowed to write."  I don't know how to find a directory where I'm allowed to write, or if that's even my problem.  One suggestion I found that seemed promising was to go to a parent directory and run "chmod a+w [DIRECTORY NAME]".  I did that, and got this error:
<code>$ chmod a+w [DIRECTORY NAME]
chmod: changing permissions of '[DIRECTORY NAME]': Permission denied
</code>
And I can't go any higher in that using "CD ../" so it looks like I've got permission denial all over the shop.
The cygwin directory itself is C:\cygwin-64\, if that matters.
Thanks for any help you can give me!
Comment: I could be wrong but windows 10 replaces the standard command prompt with Powershell or something like that. Have you tried the normal administrator command prompt?
Comment: Run your cygwin as administrator. Probably, on your previous Windows version, you had your UAC disabled.
Comment: Could you post the complete path of compiled files?
Comment: Thanks, V. Kravchenko, running it as an administrator worked.  I used chmod, and now I can compile without needed to run cygwin as an admin every time.
Answer: I haven't used cygwin in years (and have never used Win10), but I will give it a shot.
What you are asking for help is complicated by multiple issues. One of them is you do not know what permissions are. Another one is Windows 10 may have reset your permissions.
Operating systems have permission models that can restrict what actions a user can do or what files a user can access. On Windows, i'm betting that UAC (that annoying dialog asking for permission) may be blocking chmod from working. (I'm assuming your account is a superuser.) The error before that looks like the folder permission does not allow 'write' access for the compiler to 'open', create a file.
The easiest way to solve this would be to uninstall and reinstall cygwin. The install script should set the proper permissions.
Kudos to you for using gcc on the command line to compile a C program. You may want to consider using Eclipse or NetBeans to help you learn C programming in the future. They have user friendly debuggers that will help you track the state of your program and fix errors.
http://www.eclipse.org/downloads/packages/eclipse-ide-cc-developers/neonr
https://netbeans.org/features/cpp/index.html
Comment: Hi JBWyatt4.  Thanks for the response.  Running cygwin as an administrator fixed the problem (I didn't try this at first, since I thought my account was an admin account already!), so I don't think I need to uninstall and reinstall.  Which is fortunate, since installing cygwin and gcc was quite the process!  As for eclipse and netbeans, thanks for the suggestions, but I'd like to try and learn programming without the use of an IDE for now.  If it proves too daunting, I'll give those programs a try.
Comment: probably the upgrade changed the security model or the default user authorization


Show that the plane $\mathbb{R}^2 - \{(-1,0), (1,0)\}$ is homotopy equivalent to $C(1,0) \cup C(-1,0)$

Question: I'm trying to find a deformation retract of the union of the two circles to $\mathbb{R}^2 - \{(-1,0), (1,0)\}$, any help is appreciated.
Answer: If you understand a little geometry then you can write down the formulas you need. To do it with explicit care is intricate and somewhat tedious but straightforward.
I will use your notation $C(p)$ for the circle of radius $1$ centered on a point $p$:
$$C(p) = \{a \, | \, d(p,a) = 1\}
$$
Letting $p_1 = (-1,0)$ and $p_2=(1,0)$, you are asking for a deformation retraction
$$f :\mathbb{R}^2 - \{p_1,p_2\} \to C(p_1) \cup C(p_2)
$$
The value of $f(a)$ is defined in eight parts.
(1 and 2) For $i=1,2$, if $0 < d(p_i,a) \le 1$ then $f(a) = p_i + \frac{a-p_i}{|a-p_i|}$. This part of the function is just radial projection from inside of the circle $C(p_i)$ minus its center $p_i$ out to $C(p_i)$.
(3) If $a=(x,y)$ with $x \le -1$ and $d(p_1,a) \ge 1$ then $f(a) = p_1 + \frac{a-p_1}{|a-p_1|}$. This is radial projection from the left half of the outside of $C(p_1)$ back to $C(p_1)$.
(4) If $a=(x,y)$ with $x \ge +1$ and $d(p_2,a) \ge 1$ then a similar radial projection from the right half of the outside of $C(p_2)$ back to $C(p_2)$.
(5) If $a = (x,y)$ with $-1 \le x \le 0$, $y\ge 0$, and $d(p_1,a) \ge 1$ then $f(x,y) = (x,\sqrt{1-(x+1)^2})$. This is just vertical projection from the portion of the plane above the "1st quartercircle" of $C(p_1)$ down to that quartercircle.
(6, 7, and 8) Similar vertical projections for $a=(x,y)$ with: $-1 \le x \le 0$, $y\le 0$, and $d(p_1,a) \ge 1$; and with $0 \le x \le 1$, $y\ge 0$, and $d(p_2,a) \ge 1$; and with $0 \le x \le 1$, $y\le 0$, and $d(p_2,a) \ge 1$. These map the points above and below the line segment between the circles' centers to their respective quartercircles.
Comment: I appreciate it thank you :)
Answer: It is always useful to think of the objects you are working with as clay. In your case, you have doubly-punctured plane. Imagine you could stick your fingers inside the holes and push outward, thus increasing the size of the holes. Now imagine you can take all the clay around those two holes and push it inward toward the center of the two holes you created. What are you left with? Exactly two disjoint circles $S_1$, provided you did your deformation without overlapping the two.
To explicitely write down a deformation retract, you want to smoothly deform the space as I mentioned. Are you able to do so?
Comment: that's what i was trying to do ,i noticed that when i "enlarge"the points i get the two circles and the other way around , but the thing is that i wasn't able to find a continuous function ,i tried to think of a function that takes each x on the circle to the x axis or the y axis ,i got stuck and then i thought also that i should find a function that its inputs are whats out of the circles since i want a deformation retracts that gets me to R^2 \the two points ,so i am not actually able to do so, thank you for your time btw
Comment: Pushing the outer part of the plane "inward toward the center of those two holes" will run into trouble on the $y$-axis, where points can't decide which hole to head toward. I recommend using your "toward the centers" outside the strip $\mathbb R\times[-1,1]$, while moving points inside the strip straight up or down until they hit the desired circles.
Comment: Thanks @AndreasBlass, english is not my first language. This is exactly what I meant.
Comment: Can you please give me an explicit function? I can't find think of any that is continuous


What does alignment between input and output mean for recurrent neural network

Question: I'm working with RNNs and during research I found many mentions of alignment between input and output.
For example (Sutskever et al., 2014):

The RNN can easily map sequences to sequences whenever the alignment between the inputs the outputs is known ahead of time. However, it is not clear how to apply an RNN to problems whose input and the output sequences have different lengths with complicated and non-monotonic relationships.

There are also implementations of the encoder-decoder architecture that talks about soft-alignment.
Now, I think that the alignment issue means that one time step in the input not necessarily maps to output on the same time step, but I am not sure if this explanation is correct. I can not find any good resources for explaining how alignment between input and output and just what that means. Any help or explanation is much appreciated.
Answer: What the paragraph from Sutskever and Sutskever means is, in a single RNN, the RNN receives a sequence of inputs, and gives a sequence of outputs , typically the same number of outputs as inputs, like:
Inputs:  i1 i2 i3 i4 i5 i6 i7
Outputs:    o1 o2 o3 o4 o5 o6 o7

So, for each input, we get an output, which could be for example a prediction for the input at the next timestep, though that's not obligatory, just a typical use-case.
Now, this works for tasks such as predicting the next word of a sentence, like i1 is the first word, and i2 is the second. o1 is a prediction for i2, and o2 is a prediction for i3. There is a one-to-one mapping of inputs and outputs.
However, eg to translate from French to English, the number of input words and output words might not match:
Il pleut
It is    raining

2 words => 3 words
Sequence to sequence solves this by having two RNNs, back to back. The first one takes an arbitrary sequence, and maps it to a single embedding vector, being simply the hidden state of the RNN, after receiving all the input words.
i1 i2 i3 i4 i5 i6 i7 ... => embedding-vector

Then the second RNN is initialized with this embedding-vector, and then predicts words freely, with no further inputs, until it outputs a termination token.
embedding-vector => o1 o2 o3 ... termination-token

Putting these together, we pump in a sequence of one length, and the output can be a different length, eg:
i1 i2 i3 => embedding-vector => o1 o2 o3 o4 o5 termination-token


CocoaPods' Pods.xcconfig conflicts with existing ones

Question: I've a project with several targets. There is a xcconfig file associated to the project and several xcconfig files associated to the targets.
Now I want to incorporate a third party library via CocoaPods. After $ pod install CocoaPods screwed the original project since it associates its own xcconfig file Pds.xcconfig to the target of the original project. This completely ignores and overwrites the settings defined in the original xcconfig associated to the project, and switched out the original xcconfig file associated to the target(s). Hence, the project ended up screwed.
For example, Cocoapods defines the following in its Pods.xcconfig which is associated to each target:
GCC_PREPROCESSOR_DEFINITIONS = $(inherited) COCOAPODS=1
I have different configs for each configuration, for example for Debug the config file osx.debug.xcconfig defines
GCC_PREPROCESSOR_DEFINITIONS = DEBUG=1 DEBUG_LOG=2 __ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES=0

What's a reasonable and viable approach to solve such conflicts?
Edit:
I've searched through the CocoaPods issues list on GitHub. There seems to be a large number of issues related to xcconfig files, some of them more than 2 years old. They are discussed, and then closed, but - as it occurred to me - the issue has not yet been solved.
IMHO, this is a fundamental and a basic problem: there is a project which uses xcconfig files. Then, one wants to include a third party library via CocoaPods. After executing pod install, the project is screwed up.
So, what's up with this?
Answer: You can still have your cake and (partly) eat it. Your targets' xcconfig file can include the cocoapods one:
#include "../Pods/Pods.xcconfig"
MY_SETTING = YES
...

However, the part you still can't eat is when you want to specify the flags already set in Pods.xcconfig. I can't find a way to add to paths that Cocoapods already set in the included file, e.g. you'd want this:
HEADER_SEARCH_PATHS = $(HEADER_SEARCH_PATHS) MyOtherIncludes

However this line simply drops the Cocoapods header search path, leaving only MyOtherIncludes :-(
One workaround for this should be to move MyOtherIncludes to the project's xcconfig files, which Cocoapods doesn't touch. Unfortunately this didn't work for me, YMMV.
Comment: Unfortunately, the build settings that cannot be extended or modified are the essential one: GCC_PREPROCESSOR_DEFINITIONS, HEADER_SEARCH_PATHS, OTHER_CFLAGS and OTHER_LDFLAGS. The culprit could also be Xcode since the xcconfig file cannot handle such declarations: `HEADER_SEARCH_PATHS = $(HEADER_SEARCH_PATHS) MyOtherIncludes`. In order to make that work would probably break the current behavior how Xcode's xcconfig file settings modify the underlying build settings.


Adjusted R square decrease with increase in Sample size in hierarchical regression analysis spss

Question: I collect data in survey (use Likert scale 1-5), after that i did Factor analysis,Extraction Method: Maximum Likelihood, Rotation Method: Oblimin with Kaiser Normalization and did hierarchical regression analysis spss to create a model.
Case-1 , Sample size N=176, KMO = 0.669, Adjusted R square = 41.8

Case-2, Sample size = 279, KMO= 0.674. Adjusted R square = 24

I dont know why $R^2$ value suddenly decrease from 0.41 to 0.24 , when sample increase from 176 to 279.
Comment: Because your data was different? You have also added another variable which does not increase adjusted $R^2$ and usually reduces it.
Comment: Hi mdewey we use the same questionnaire in 3 institute , case 1 : we did analysis on 1 institute , case 2: we did on the combine date from 3 institute
Answer: Because the data from the other two institutes does not fit the model as well as the data from the first institute. As to why that is the case, you should look at differences among the three institutes.
One other possibility that relates to factor analysis - if you did the factor analysis separately at each institute, check that the signs are equivalent.
One more point - doing FA prior to regression is not always the best way. You might want to look at partial least squares.


Why is $\frac{\sum_{i=1}x_iy_i}{\sum_{i=1}x_i^2}\ne \frac{\sum_{i=1}y_i}{\sum_{i=1}x_i}$?

Question: I know they are not equal, but I am unable to come up with a convincing explanation as to why they are not equal. Cancelling the $x_i$'s when they are part of the summand is prohibited; Can anyone provide a decent argument as to why this is the case?
Thanks.
Comment: You need the *same* common factor; $x_i$ is not one number (in general).
Comment: To cancel, you have to have a common factor. You don't: instead you can do something like $\frac{\sum_{i=1}^n x_i y_i}{\sum_{i=1}^n x_i^2} = \frac{\sum_{i=1}^n \frac{x_i}{x_1} y_i}{\sum_{i=1}^n \frac{x_i^2}{x_1}}$ (replacing the $1$ with whatever you want). In fact it can happen that the left side is defined and the right side is not, for instance with $n=2,x_1=1,x_2=-1,y_1=1,y_2=1$.
Comment: Is this a joke? Does it seem that $\dfrac {ab + cd} {a^2 + c^2} = \dfrac {b + d} {a + c}$?
Comment: Are you asking for a counterexample?
Comment: @Gaffney Sure, that would be great thanks or an explanation would be nice
Comment: @Ian Thanks, and $x_i$ is not a common factor because....?
Comment: @Alex Why did you say that? Sure I was laughing all the time I wrote it. What's simple to you may not be so simple to me.
Comment: @AlexM. I work with high school students and it seems to be a very common misconception.
Comment: @BLAZE: Not to offend you, but you've got some reputation already, you've acquired some medals and you've correctly answered some undergraduate-level questions, and it baffles me how somebody with university-level knowledge on some topics of mathematics does not know something elementary, from pre-high-school.
Comment: @AlexM Well that does deserve an explanation I guess; I use this site primarily to learn. All I wanted was a concrete example/explanation as to why they are not equal I already stated in the question; that I _know_ they are not equal. This post was designed to promote a decent explanation. Maybe summation theory is simply not my strong-point.
Comment: @BLAZE: Ah, ok then, I apologize, I had misunderstood your question.
Comment: @AlexM It's fine; no need for apologies
Answer: Let the $y_i$'s be all $1$, and let's write the summation explicitly, like so:
$${x_1 + x_2 + \cdots +x_n \over x_1^2 + x_2^2 + \dots +x_n^2} \neq {1 + 1 + \dots +1 \over x_1 + x_2 + \dots +x_n} = \frac{n}{x_1 + x_2 + \dots +x_n}$$
Does it seem convincing here that the $x_i$'s can't cancel? It's just not a valid logical step.
For a specific counterexample, take $x_i = 1,2,3,4$. Then we have
$${1+2+3+4 \over 1 + 4 + 9 + 16} = \frac{10}{30} = \frac13 \neq \frac25 = \frac4{10} = \frac4{1 + 2 + 3 + 4}$$
Therefore we cannot cancel the $x_i$'s.
Comment: In other words, asking for a convincing explanation why it's not true is like asking for a convincing explanation why $\frac ab+\frac cd=\frac{a+b}{c+d}$ is not true. It just ain't.
Comment: @GregMartin Exactly.
Comment: @silvascientist Thanks for the explanation and counter-example.
Answer: Consider the simpler case when $n = 2$ (the upper bound for the summation index). Assume that $\dfrac {ab + cd} {a^2 + c^2} = \dfrac {b + d} {a + c}$. After cross-multiplication and canceling of similar terms, you may grup the remaining ones as $(a-c) (ad - bc) = 0$. This shows that, in order for that equality to be true, either $a = c$ (i.e. $x_1 = \dots = x_n$, which means that you may simplify a common factor), or your numbers have to verify a very special relationship ($ad - bc = 0$), so the equality cannot hold in general, for arbitrary values.
Comment: @Farnight: I could do it, but I am *certain* that finding some $a,b,c,d$ that make the above equality false is a very good exercise for the OP, and I am also *certain* that he has the abilities to solve it for himself.
Comment: @GregMartin: I refuse to bow to political correctness, I hate that politically-motivated pervertion of "sexist, non-inclusive" English and, fortunately, I live in a country that still doesn't force me to do the above.
Comment: I think the answer could be improved by adding a concrete example.
Comment: @AlexM No offence taken, thanks for your answer (+1)
Comment: @BLAZE: Maybe the expanded version of my answer better addresses   your question.
Comment: @AlexM Yes that's looking pretty good thank you
Comment: Speaking of being certain: "... he or she has the abilities to solve it for himself or herself."
Comment: Your use of the phrase "political correctness", as usual, is a deflection without substance (as is the bizarre non-sequiter about national laws). I am simply pointing out, to thoughtful readers, that assumptions about mathematicians automatically being male are still present—and, judging by your hostile reaction, defended by some. I further assert that such assumptions and biases are harmful (as evidenced by the current gender inequity in mathematics).
Answer: Maybe this answer of mine is not rigorous enough but I will give it a try because it gives some kind of alternative view on questions of this sort so I will add it maybe just to have a variety of views on the same question.
Suppose that we view $\sum_{i=1}^{n}x_iy_i$ as a function of $2n$ variables $x_1,...,x_n,y_1,...,y_n$. Then, in the same spirit, the sum $\sum_{i=1}^{n}x_i$ could be viewed as the function of $n$ variables $x_1,...x_n$. The sum $\sum_{i=1}^{n} {x_i}^2$  as a function of also the $n$ variables $x_1,...,x_n$ and sum $\sum_{i=1}^{n} y_i$ as a function of $n$ variables $y_1,...y_n$.
Let us write now $a(x_1,...x_n,y_1,...y_n)=\sum_{i=1}^{n}x_iy_i$ and $b(x_1,...x_n)=\sum_{i=1}^{n}x_i$ and $c(x_1,...x_n)=\sum_{i=1}^{n} {x_i}^2$ and $d(y_1,...y_n)=\sum_{i=1}^{n} y_i$.
Now, if we suppose that your equality is true then we would have
$a(x_1,...x_n,y_1,...y_n)=d(y_1,...y_n) \dfrac {c(x_1,...x_n)}{b(x_1,...x_n)}$
So we would have that the function of $2n$ variables is equal to the product of two functions, one that depends on the first $n$ variables and the second that depends on the remaining $n$ variables, and, intuitively, this will, I guess, happen rarely, and in this case it is only always true when $n=1$.


How to read and process multiple files simultaneously in python

Question: I have multiple files and I want to read them simultaneously, extract a number from each row and do the averages. For a small number of files I did this using izip in the itertools module. Here is my code.
from itertools import izip
import math

g=open("MSDpara_ave_nvt.dat",'w')

with open("sample1/err_msdCECfortran_nvt.dat",'r') as f1, \
     open("sample2/err_msdCECfortran_nvt.dat",'r') as f2, \
     open("sample3/err_msdCECfortran_nvt.dat",'r') as f3, \
     open("err_msdCECfortran_nvt.dat",'r') as f4:

     for x,y,z,bg in izip(f1,f2,f3,f4):
         args1=x.split()
         i1 = float(args1[0])
         msd1 = float(args1[1])

         args2=y.split()
         i2 = float(args2[0])
         msd2 = float(args2[1])

         args3=z.split()
         i3 = float(args3[0])
         msd3 = float(args3[1])

         args4=bg.split()
         i4 = float(args4[0])
         msd4 = float(args4[1])

         msdave = (msd1 + msd2 + msd3 + msd4)/4.0

         print>>g, "%e  %e" %(i1, msdave)

 f1.close()
 f2.close()
 f3.close()
 f4.close()
 g.close()

This code works OK. But if I want to handle 100 files simultaneously, the code becomes very lengthy if I do it in this way. Are there any other simpler ways of doing this? It seems that fileinput module can also handle multiple files, but I don't know if it can do it simultaneously.
Thanks.
Comment: You don't need to explicitly close files opened in a `with` statement.
Answer: The with open pattern is good, but in this case it gets in your way. You can open a list of files, then use that list inside izip:
filenames = ["sample1/err_msdCECfortran_nvt.dat",...]
files = [open(i, "r") for i in filenames]
for rows in izip(*files):
    # rows is now a tuple containing one row from each file

In Python 3.3+ you can also use ExitStack in a with block:
filenames = ["sample1/err_msdCECfortran_nvt.dat",...]
with ExitStack() as stack:
    files = [stack.enter_context(open(i, "r")) for i in filenames]
    for rows in zip(*files):
        # rows is now a tuple containing one row from each file

In Python < 3.3, to use with with all its advantages (e.g. timely closing no matter how you exit the block), you would need to create your own context manager:
class FileListReader(object):

    def init(self, filenames):
        self.files = [open(i, "r") for i in filenames]

    def __enter__(self):
        for i in files:
            i.__enter__()
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        for i in files:
            i.__exit__(exc_type, exc_value, traceback)

Then you could do:
filenames = ["sample1/err_msdCECfortran_nvt.dat",...]
with FileListReader(filenames) as f:
    for rows in izip(*f.files):
        #...

In this case the last might be considered over-engineering, though.
Comment: Instead of creating a new one, the OP could upgrade to modern Python and use an [`ExitStack`](https://docs.python.org/3.4/library/contextlib.html#contextlib.ExitStack) instead.
Comment: @DSM, thanks for the link. I didn't know about that one (I use 2.7). That's certainly less code when used only once. I'll integrate it to the answer.
Comment: Thanks a lot, @otus. That's very helpful. So if I do   'files = [open(i, "r") for i in filenames]
for rows in izip(files):'   as you said, how can I read lines from each tuple "rows"? Apparently I cannot use readline().
Comment: @otus, it seems that the tuple 'rows' is not a tuple of strings. If I print the content of tuple 'rows', I only got something like "". And if I further look at the dimension of tuple 'rows' using 'len(rows)', it shows the dimension of 'rows' is one. I'm a bit confused that why this tuple 'rows' does not contain a row of string in my data file as you've mentioned.
Comment: @user2226358, sorry, I forgot the star `*` inside `zip`. Answer updated. (It passes the list as multiple arguments instead of one, so that `zip` will indeed zip them.)


Uso TempData ASP MVC

Question: Pelo que andei a pesquisar (aqui), consigo guardar uma lista de dados numa TempData criada numa função, para usar em outra função no meu Controller.
Ora bem, estou a tentar fazer isso mesmo, só que não estou a conseguir:
Model:
...
namespace ARTSOFT.dal.ViewModels
{
    public class Localidades
    {
        public string Familia { get; set; }
        public string Subfamilia { get; set; }
        public string Registo { get; set; }
        public string Descricao { get; set; }
    }

    public class LocalidadesFormViewModel
    {
        public Localidades Localidades { get; private set; }

        public LocalidadesFormViewModel(Localidades localidades)
        {
            Localidades = localidades;
        }
    }
}

Função onde crio a TempData:
public ActionResult AtribuirOrdemServ()
{
    ...
    var newListaLocalidades = new List<ARTSOFT.dal.ViewModels.Localidades>();
    foreach (var item in localidades)
    {
       var newItemLocalidade = new ARTSOFT.dal.ViewModels.Localidades();
       newItemLocalidade.Familia = item.ToString().Split(',')[0].Split('{')[1].Split('=')[1].Split(' ')[1];
       newItemLocalidade.Subfamilia = item.ToString().Split(',')[1].Split('=')[1].Split(' ')[1];
       newItemLocalidade.Registo = item.ToString().Split(',')[2].Split('=')[1].Split(' ')[1];
       newItemLocalidade.Descricao = item.ToString().Split(',')[3].Split('}')[0].Split('=')[1].Split('"')[0];
       newListaLocalidades.Add(newItemLocalidade);
   }

   TempData["ListaLocalidades"] = newListaLocalidades.ToList();

   return View("IndexAtribuirOrdemServ");
}

Depois estou a tentar aceder a outra função, via JavaScript, e queria buscar esta TempData:
$("#divResultFiltroAtribuirOrdemServ").load("FiltroAtribuirOrdemServ"
, function () {
...
});

E a função FiltroAtribuirOrdemServ:
public ActionResult FiltroAtribuirOrdemServ(){
    ...
    var teste = (Localidades)TempData["ListaLocalidades"];
}

Onde obtenho a TempData["ListaLocalidades"] sempre null
Answer: Você está fazendo cast pra um objeto Localidades, mas na verdade deveria fazer casting de um Lista de Localidades:
(List<Localidades>)TempData["ListaLocalidades"];

Ao invés de usar o método View, use RedirectToAction, assim ele vai passar pelo ActionResult que você criou e quer trabalhar com os dados.
return RedirectToAction("IndexAtribuirOrdemServ");

A forma que você fez ele passa direto pra View sem entrar na ActionResult, você pode também trabalhar com os dados na View:
@foreach (var item in (List<ARTSOFT.dal.ViewModels.Localidades>)TempData["ListaLocalidades"])
{
    @Html.Raw(item.Descricao)
}

Comment: nisso tens razão e foi burrice minha que não reparei, a situação é que a `TempData["ListaLocalidades"]` está a null, logo não me resolve o problema
Comment: Eu testei aqui funcionou normalmente, você quer passar essa TempData pro Controller IndexAtribuirOrdemServ?
Comment: Não, quero passar `AtribuirOrdemServ()` para a View(), e depois na `FiltroAtribuirOrdemServ()` aceder os dados. Esta última função chamo a partir do JavaScript
Comment: Editei a resposta, vê se atende o que você quer.
Comment: Já vi, a questão é que uma função não tem nada a haver com outra. Uma carrega uma página (AtribuirOrdemServ()), e a outra (FiltroAtribuirOrdemServ()) carrega uma partial com resultados de filtros aplicados pelo utilizador. Ao fazer RedirectToAction perco o sentido da funcionalidade
Comment: Se eu falar que eu acabei de testar criei as views, adicionei sua função e na ActionResult a List veio populada.
Comment: Vamos [continuar esta discussão no bate-papo](http://chat.stackexchange.com/rooms/19857/discussion-between-cesarmiguel-and-laerte).
Comment: acabei por carregar a lista novamente na função `FiltroAtribuirOrdemServ()` e tentei optimizar ao máximo umas consultas que tinha para não haver perdas de tempo. Vou validar a tua resposta porque parece ser a correcta

Uso TempData ASP MVC


Java collection GC

Question: If I created inside method some kind of collection - list, array or map, after exiting from method if this collection contains references also for external objects GC free the memory?
Or it will be memory leak?
Thanks.
Comment: In Java, its almost impossible to create a C style memory leak.  So much so that when the term is used in Java it means any undesirable increase in memory usage.
Answer: No, there will be no leak. Once your code has no references to a given object, the object becomes eligible for garbage collection.
It does not matter whether the object itself (e.g. a collection) contains references to live objects -- it's references in the opposite direction that matter.

Java collection GC


checkboxInput value R shiny: if TRUE then

Question: I have the following code. The objective is to make the position of the plot bars reactive to the selectInput value
library(shiny)
library(shinyWidgets)
library(tidyverse)
library(DT)
library(shinythemes)
library(plotly)
library(ggthemes)
library(lubridate)

data <- data.frame(mitarbeiter = c("AA", "BB", "CC", "DD", "EE", "FF"),
         art = c("hr", "GG", "TT", "RR", "OO", "OO"),
         creadate = as_date(c("2018-01-03", "2018-01-03", "2018-01-03", "2018-01-03", "2018-01-03", "2018-01-03")))

mitarbeiter1 <- sort(unique(data$mitarbeiter))
art1 <- sort(unique(data$art))

year_month <- function(dates) {
  paste(lubridate::year(dates),
        str_pad(lubridate::month(dates), width = 2, pad = 0),
        sep="-")
}

year_week <- function(dates) {
  paste(lubridate::year(dates),
        str_pad(lubridate::week(dates), width = 2, pad = 0),
        sep="-")
}

year_day <- function(dates) {
  paste(lubridate::year(dates),
        str_pad(lubridate::month(dates), width = 2, pad = 0),
        str_pad(lubridate::day(dates), width = 2, pad = 0),
        sep="-")
}

ui <- fluidPage(
  fluidRow(
    column(4,
           pickerInput("mitarbeiterName", "Name des Mitarbeiters", mitarbeiter1,
                       options = list(`actions-box` = TRUE), multiple = TRUE),
           pickerInput("artName", "Art", art1,
                       options = list(`actions-box` = TRUE), multiple = TRUE),
           pickerInput("period", "Zeitraum", c("day", "week", "month", "year"),
                       options = list(`actions-box` = TRUE)),
           dateRangeInput("date", "Datum auswahlen", start  = "2020-01-01"),
           checkboxInput("kumulativ", "Kumulativ"),
           downloadButton("download", "Download")
    ),
    column(8,
           plotlyOutput("policyPlot")
    )
  )
)

server <- function(input, output, session) {

  #create a reactive object with a NULL starting value
  listofrows <- reactiveValues(data = NULL)

  #observe the changes in inputs and update the reactive object
  observeEvent(c(input$mitarbeiterName, input$artName, input$date, input$period), {
    req(input$mitarbeiterName)
    req(input$artName)
    req(input$period)
    req(input$date)

    listofrows$data <- subset(data, mitarbeiter %in% input$mitarbeiterName &
                                art %in% input$artName &
                                creadate >= input$date[1] & creadate <= input$date[2])
  }, ignoreInit = T, ignoreNULL = TRUE)

  output$policyPlot <- renderPlotly({
    req(listofrows$data)
    req(input$kumulativ)

    fn <- switch(
      input$period,
      day = year_day,
      week = year_week,
      month = year_month,
      year = year
    )

    pos <- if (input$kumulativ) "dodge" else "identity"

    ggplot(listofrows$data) +
      geom_bar(aes(x = fn(creadate), fill = mitarbeiter),
               stat = "count",
               position = pos,
               show.legend = T) +
      ggtitle("Anzahl erstellte Policen (pro Mitarbeiter)") +
      xlab("Zeitraum") + ylab("Anzahl der Policen")
  })

  output$download <- downloadHandler(
    filename = function() {
      paste("data-", Sys.Date(), ".png", sep = "")
    },
    content = function(file) {
      ggsave(file, plot = output$policyPlot)
    })
}

shinyApp(ui, server)

Now, I want:

the position to be "dodge" if checkboxInput = TRUE, and
the position to be "identity" if checkboxInput = FALSE.

does someone have any suggestion how to do that? How can we do the if condition with the checkbox value?
Comment: `pos <- if (input$cumulative) "dodge" else "identity"`, then `geom_bar(..., position=pos)`
Comment: (It might be safer, especially with larger/more-complex apps, to start your `renderPlotly` reactive block with `req(input$cumulative)`. Smaller apps with simpler reactivity graphs tend to be fine, but larger ones may observe a brief moment of `NULL` (undefined) when accessing `input$...` variables.)
Comment: hey @r2evans, thanks for the reply. I implemented the code, but now when I check the box the plot appears, while when the box is not checked the plot disappears. I do not undertand why
Comment: I think it just takes the first value in the if statement. Maybe I should include it in a reactive function or something?
Comment: Can you get the requisite commands (using both `"dodge"` and `"identity"`) to function correctly on the console without `shiny`? In response to your most recent comment *"takes the first value"*, it will take the first value in the `if` statement if the conditional is true, and the second value after `else` if the conditional is not true; perhaps I misunderstand your problem, but ... that code works for me in my uses. Perhaps you can include some sample data and the plots given that data.
Comment: I added the full code and data
Comment: hey @r2evans do you have any idea now that I added the full code? thanks
Answer: In your case, req(input$kumulativ) doesn't work. It's because req checks if a value is "truthy", and FALSE is not considered truthy. Therefore, you can change it to:
req(!is.null(input$kumulativ))

Comment: many thanks for the solution and eplanation


Django | Type error - missing 1 required positional argument: 'file_id'

Question: Hello I'm trying to create a platform that will solve problems and then let the user download a file with the result. I'm having trouble with the downloading part this is the error I'm getting
TypeError at /download/
download() missing 1 required positional argument: 'file_id'

models.py
class Snippet(models.Model):
email = models.CharField(max_length=100)
file = models.FileField(upload_to='documents/')

def __str__(self):
       return self.email

urls.py
from . import views
from .views import (snippet_detail)

app_name = 'app'

path('download/<int:file_id>/', views.snippet_detail, name='snippet_detail'),

views.py
def download(request, file_id):
media_path = "/home/me/project/media/documents/"
result_path = '/home/me/Desktop/'
max_timeout = 1*24*60*60 #days*hours*mins*secs

if request.method == 'POST':
    form = SnippetForm(request.POST, request.FILES)
    if form.is_valid():
        form = Snippet(file=request.FILES['file'])
        form.save()

        file_name_final = form.file.name[10:]
        file_path = media_path + file_name_final

        ### analysis code ###

        data_id = test['outputs'][0].get('id')

        datamap = dict()
        datamap['0'] = {'src': 'hda', 'id': data_id}
        results_run = ..
        results_id = results_run['outputs']
        file_id= ''.join(results_id)

        if file_id !='':
            filepath= 'result_path' + file_id
            path = open(filepath, 'rb')
            mime_type, _ = mimetypes.guess_type(filepath)
            response = HttpResponse(path, content_type=mime_type)
            response['Content-Disposition'] = "attachment; file_id=%s" % file_id
            return redirect('/download/{file_id}')
        else:
            raise Http404("No result file")

download.html

Your problem is solved please click here to download

Answer: It should be this in your download view:
return redirect('download', file_id=file_id)

But that would call the same view again i don't think you want that so it should be (as you are creating a file response and need to return that):
return response

Also i think url is wrong it should be:
path('download/<int:file_id>/', views.download, name='download')


How do more features lower the bias?

Question: The question is fairly straight forward.
I always assumed that a model will not perform well if there are not enough features because all the variables which cause variation in the data are not accounted for.
However, I'm not sure if my hypothesis is correct.
What is the reason that creating more features will lower the bias?
Answer: Bias characterize how well your model describes real process. But not all of the features are matter. For example if you want to estimate salary of a person feature "Highest degree of education" would matter a lot and feature "Like blue socks" wouldn't matter (at least much). So, more features COULD reduce bias.
Actually, it's a whole area of statistics to build a model with needed features. One would examine p-value of a hypothesis if feature does matter in the model. I do recommend you to check out ISLR book. It's free, has a lot of good ideas and straightforward explanations.

How do more features lower the bias?


pam_authenticate() failed: Authentication failure (/etc/pam.d/imap missing?)

Question: I followed a link from another site that had an identical question posted as solved on this site, but the link resulted in a 404. I found the other links to pam failure, and I tried changing dovecot.conf as mentioned in the other solution. Actually, the error posted above, no longer shows up, but I still cannot authenticate.
passdb {
  args = %s
  driver = shadow
}
but that did not work.
/var/log/mail.log
davomail postfix/smtpd[19906]: NOQUEUE: reject: RCPT from mail-yw0-f173.google.com[126.96.36.199]: 454 4.7.1 : Relay access denied; from= to= proto=ESMTP helo=
dovecot.conf
<code>disable_plaintext_auth = no
mail_privileged_group = mail
mail_location = mbox:~/mail:INBOX=/var/mail/%u
userdb {
driver = passwd
}
passdb {
  args = %s
  driver = passwd
}
 protocols = " imap"
service auth {
  unix_listener /var/spool/postfix/private/auth {
    group = postfix
    mode = 0660
    user = postfix
  }
}
ssl=required

postconf -n
alias_database = hash:/etc/aliases
alias_maps = hash:/etc/aliases
home_mailbox = Maildir/
inet_interfaces = all
local_recipient_maps = proxy:unix:passwd.byname $alias_maps
mailbox_size_limit = 0
mydestination = mail.domain.com, domain.com, localhost localhost.localdomain
mydomain = davomail.com
myhostname = mail.davomail.com
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
myorigin = /etc/mailname
recipient_delimiter = +
relayhost =
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
smtpd_tls_cert_file = /etc/ssl/certs/mailcert.pem
smtpd_tls_key_file = /etc/ssl/private/mail.key
smtpd_tls_protocols = !SSLv2, !SSLv3
smtpd_tls_security_level = may
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtpd_use_tls = yes
</code>
Answer: I had my pam.d files all messed up trying to debug. at first it asked if the file did not exist- so I created it. Finally statertd working when I deleted the pam.d/imap file. Why I got the error in the first place was a misconfiguration in dovecot.conf.
passdb {
  args = %s
  driver = passwd
}
should be
passdb {
  args = %s
  driver = pam
}


EF: Should entities have business logic?

Question: I was wondering, should the entities have the capability to save changes to the context? Or have business logic that relates to that particular entity? For example:
ActionResult ResetPassword(UserViewModel viewModel)
{
   var user = userCollection.GetUser(viewModel.Username);
   user.ResetPassword();
}

where:
class User : Entity
{
      public string Password{ get; set; }

      public ResetPassword()
      {
           Password = ""
           context.SaveChanges();
      }
}

I find this a bit weird since the entity would have a reference to the context. I am not sure whether this would work either - or whether this is recommended. But I want to work on a domain where I do not have to worry about saving changes, at a higher level etc. How can I accomplish this?
Thanks!
Update
I have updated my example - hope its a bit clearer now :)
Answer: According to Domain-Driven Design domain objects should have behavior.
You should definately read this book:
Comment: in order to achieve domain-driven behaviour, each entity would have to have a reference to the context, as stated in the question. This seems to be fighting against the mvc .net framework. Have you attempted to do this?
Answer: I would keep my Entities as POCO's(Plain Old Class Objects, classes with only properties) and have a Repositary do methods like Insert / Update.
I can keep my Entities in a separate class library and use it in different places ( even in a different project), with a different Repository implementation.
In this Tutorial It is  nicely explained, how to do a Repositary & Unit Of Work Patterns on an MVC project which uses Entity Framework
Comment: That is interesting. See, I had a different idea. I thought the domain objects should have behaviour as @Steven mentioned. And I thought the models were effectively the entities - such as User. Perhaps a better example would be - whether a user can reset a password. I would have thought it would be the User's responsibility rather than a Repository's responsibility. What do you think Shyju / Steve?
Answer: You may want to consider the UnitOfWork pattern between your controller and your entities.
http://martinfowler.com/eaaCatalog/unitOfWork.html


Domain of "f(x)"

Question: $f(x)=\frac{5x+4}{x^2-4}\; \rightarrow x=\;?\;$
Well, I got this from that equation:
$\frac{5x+4}{x^2-2^2}=\frac{(x+2)*5-6}{(x-2)(x+2)}=\frac{-1}{x-2}=\frac{1}{-x+2} \; \rightarrow x=\mathbb{R}-\left\{2,-2\right\}$  So, my solution approaching for this equation is right? Maybe you could share yours as well?
Comment: Do you want to find the [domain](http://en.wikipedia.org/wiki/Domain_of_a_function) of the function $f(x)$?
Comment: @Kerim: In general, $(a \cdot b) - c \neq a \cdot (b - c)$, which you did apply to the numerator in the second equality on the second line. Also $1/(-x+2)$ is perfectly well-defined for $x = -2$, hence my second comment.
Comment: What are you trying to do? You start with a function, then treats is an *equation*.
Comment: I'm just trying to learn here, not showing off... So, if any help or idea available, it might be very nice and kind...?
Comment: @AméricoTavares: I think yes. Yes, I do, sir. Thank you very much for the "wiki" clue... :)
Comment: Where is $f(x)$ defined?
Comment: How did you get $\frac{(x+2)*5-6}{(x-2)(x+2)}=\frac{-1}{x-2}$??
Comment: Please change the title and the text accordingly.
Comment: Also, "concluding" from $\frac{1}{-x+2}$ that $x$ cannot be $2$ or $-2$ is just wrong.
Comment: @AméricoTavares: It asks for the maxiumum available range for the domain set.
Comment: @AméricoTavares: The title, yes, I got it already... I'm trying to change it now...
Comment: @TMM: I did it to solve the equation... If you think there is something wrong there, please, just go ahead and add your any available correction or idea. That's what I'm looking for...
Comment: @TMM: I think I understand you... But, doesn't "(((x+2)*5)-6)/((x-2)*(x+2)) equal (-1)/(x-2) ? Where I'm doing wrong, show me please...?
Comment: @KerimAtasoy: see the PS in my answer.
Comment: @AméricoTavares: Yes sir, doing that right now, thank you very much.
Answer: Hint: The function $f(x)$ is defined for every $x\in
\mathbb{R}
$ such that the denominator $x^{2}-4\neq 0$.
PS. Note that $\frac{\left( x+2\right) 5-6}{\left( x-2\right) \left( x+2\right) }\neq \frac{-1}{x-2}$, because if you divide both the numerator and the
denominator by $x+2$ you get $\frac{\left( x+2\right) 5-6}{\left( x-2\right)
\left( x+2\right) }=\frac{5-\frac{6}{x+2}}{x-2}$.
Comment: I see it now... THANK YOU VERY MUCH, SIR... :)
Comment: @KerimAtasoy: You are welcome.
Answer: A problem like this is partly an exercise in learning to parse equations and partly an exercise in organization.
The expression (not equation!) $\frac{5x+4}{x^2-4}$ is a quotient expression: the "outermost" operation is division, and it can be rewritten as

The quotient of "$5x+4$" divided by "$x^2-4$"

The domain of a quotient is everywhere where:

The numerator is defined
The denominator is defined, and nonzero

So you need to solve the subproblems:

What is the domain where $5x+4$ is defined
What is the domain where $x^2-4$ is defined and nonzero?

As an aside, the domain of $\frac{x-1}{x-1}$ is not all of $\mathbb{R}$: it is everywhere except $1$. As partial[1] functions:
$$ \frac{x-1}{x-1} \neq 1 $$
However, the continuous extension of $\frac{x-1}{x-1}$ is, in fact, $1$.
As an aside... be aware that there is an implicit hypothesis: $x$ is a variable ranging over the domain of all real numbers. Sometimes, you have variables that range over smaller domains: e.g. if $y$ is a variable ranging over positive real numbers, then the domain of $y-1$ is also merely the positive real numbers.
[1]: Really, you're working with partial functions, not functions. (If you were working with functions, there would be no questions about domain: if $x$ is a variable ranging over all reals, then either the domain of $f(x)$ is all of $\mathbb{R}$, or the problem is ill-posed. In particular, $1/x$ would be an ill-defined expression)

Domain of "f(x)"


How to migrate content from json?

Question: I have a json file like this
{
  "online": false,
  "key": "123",
  "Product": {
    "name": "product name",
    "Id": 12,
  },
}

I have my json migration file
source:
  plugin: url
  data_fetcher_plugin: file
  data_parser_plugin: json
  urls:
    - 'json_path/data.json'
  item_selector: /Product
  fields:
    - name: keyword
      label: 'Keyword'
      selector: name
  ids:
    product_key:
      type: string
  constants:
    vocabulary: tag
process:
  vid: constants/vocabulary
  name: keyword
destination:
  plugin: entity:taxonomy_term

but I am getting the following error.
Drupal\Core\Database\IntegrityConstraintViolationException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null

It means the xpath is not correct.
I have tried with the item_selector: $ and for the keyword selector selector: Product.name but then I am getting this error.
Passed variable is not an array or object

I have tried with item_selector: 0 and item_selector: / but nothing worked.
However when I use the following json.
{
  "online": false,
  "key": "123",
  "Product": [
    {
      "name": "product name",
      "Id": 12,
    }
  ],
}

Notice the [] brackets.
What is the issue? How can I make it work without the [] brackets because I don't have any control on the json to change it.
Answer: The data_parser_plugin: json always expects an array with numeric keys as item_selector, there is no way you can get around this requirement in YAML. If you want to use a single object as item_selector, you have to create your own data_parser_plugin.
I've already had that problem in a previous object and creates my custom plugin and used my custom data_parser_plugin: single_json_object in YAML instead.
file MY_MODULE/src/Plugin/migrate_plus/data_parser/SingleJsonObject.php
<?php

namespace Drupal\MY_MODULE\Plugin\migrate_plus\data_parser;

use Drupal\migrate_plus\Plugin\migrate_plus\data_parser\Json;

/**
 * Obtain JSON data for migration.
 *
 * @DataParser(
 *   id = "single_json_object",
 *   title = @Translation("Single JSON object as root")
 * )
 */
class SingleJsonObject extends Json {

  protected function has_string_keys(array $array) {
    return count(array_filter(array_keys($array), 'is_string')) > 0;
  }

  protected function getSourceData($url) {
    $source_data = parent::getSourceData($url);

    if ($this->has_string_keys($source_data)) {
      $sequential_array = [];
      $sequential_array[] = $source_data;
      $source_data = $sequential_array;
    }

    return $source_data;
  }
}

Comment: what will be the value of the item_selector then? and the keyword selector?
Comment: I have used `item_selecotr: /` and the keyword selector as `selector: Product/name` and it worked for  me with your data parser plugin

How to migrate content from json?


I can't think how to move the column

Question: So i am making a game for my final work, its like "the sample game".
I already made almost all the code but i can't find a solution for my last function, the fuction must see if the column is empty (full of zeros) and if so, send it to the right of all the other columns. here is my code (all the function must start with the arguments they have, its a part of the work) the fuction i am having problems is called "colunas":
#include <stdio.h>
#include <stdlib.h>
#include <string.h>//para poder usar char's e strings
#include <stdbool.h>//para poder usar o while true
#include <time.h>//para poder usar seeds diferentes na funçao srand

int marcar(int sz, int tabuleiro[sz][sz], int x, int y)//vai contar o numero de quadrados do grupo
{
  int num=0;
  for (x = 0; x < sz; x++)
  {
    for (y = 0; y < sz; y++)
    {
      if(tabuleiro[x][y] == 9)//os ciclos vao percorrer o tabuleiro e vao verificar os 0s
      {
        num=num+1;//num vai contar o numero de 0s
      }
    }
  }
  return num;//retornar o numero de 0s
}

//pontuacao
int pontuacao(int num_quadrados)
{
  int pontos;
  pontos=(num_quadrados*(num_quadrados + 1))/2;
  return pontos;
}

//gravidade
/*
void gravidade(int sz, int tabuleiro[sz][sz])
{
  for (int i = 0; i < sz+1; i++)
  {
    for (int j = 0; j < sz; j++)
    {
      if (tabuleiro[i][j] == 9)
      {
        tabuleiro[i][j] = tabuleiro[i-1][j];
        tabuleiro[i-1][j] = 9;
      }
    }
  }
}
*/

void gravidade(int sz, int tabuleiro[sz][sz])
{
  int c = 0;
  while(c < sz)//utilizamos esta variavel apra percorrer o ciclo todas as vezes necessarias para fazer todos os 9s subir
  {
    for (int i = 0; i < sz-1; i++)//percorre as linhas
    {
      for (int j = 0; j < sz; j++)//percorre as colunas
      {
        if (tabuleiro[i][j] == 9 && i < sz-1)
        {
          tabuleiro[i][j] = tabuleiro[i+1][j];//muda o valor do zero para o valor acima
          tabuleiro[i+1][j] = 9;//muda o valor que esta acima do valor copiado, para 0
        }
      }
    }
  c++;
  }
}

//jogada
int jogada(int sz, int tabuleiro[sz][sz], int x, int y)
{
  if((x>=0 && x<sz) && (y>=0  && y<sz))//definimos o limite do x e do y
  {
    int temp = tabuleiro[x][y];
    tabuleiro[x][y] = 9;  //alterar o valor de tabuleiro para impedir as chamadas infinitas da recursividade
    if(tabuleiro[x+1][y] == temp)//para verificar a direita
    {
      jogada(sz, tabuleiro, x+1, y);//chamar a função recursivamente
    }
    if (tabuleiro[x-1][y] == temp)//para verificar a esquerda
    {
      jogada(sz, tabuleiro, x-1, y);
    }
    if (tabuleiro[x][y+1] == temp)//para verificar em cima
    {
      jogada(sz, tabuleiro, x, y+1);
    } if(tabuleiro[x][y-1] == temp)//para verificar em baixo
    {
      jogada(sz, tabuleiro, x, y-1);
    }
  }
  return 0;
}

void troca(int sz, int tabuleiro[sz][sz])//mudar o zero para o proximo ciclo ele nao contar com os zeros da jogada anterior
{
  for (int x = 0; x < sz; x++)
  {
    for (int y = 0; y < sz; y++)
    {
      if(tabuleiro[x][y] == 9)//os ciclos vao percorrer o tabuleiro e vao verificar os 0s
      {
        tabuleiro[x][y] = 0;//vao passar os 9 todos a 0
      }
    }
  }
}

//mover as colunas vazias
int coluna(int sz, int tabuleiro[sz][sz])
{
  //this is the function i cant solve
}

//Mostra o tabuleiro
void mostrar(int sz, int tabuleiro[sz][sz])
{
  for (int i = sz-1; i >= 0; i--)//for para o numero de linhas, usamos o sz-1 pois o indice do array começa no 0 e i>=0 para poder chegar ao indice 0
  {
    for (int j = 0; j < sz; j++) //for para o numero de colunas
    {
      printf("%d", tabuleiro[i][j]); //print das linhas
    }
    printf("\n");//baixar a linha quando chega a coluna n-1
  }
}

//main
int main()
{
  //definição do tabuleiro
  int sz;//size do tabuleiro
  printf("Escolha o valor de n (da matriz n*n):\n");
  scanf("%d", &sz);//scan do size
  printf("\n");
  int tabuleiro[sz][sz];//o size é igual para as colunas como para as linhas pois a matriz é n*n
  srand(time(NULL));//função utilizada para alterar a seed do rand (gerador de valores aleatórios)
  for (int i = 0; i < sz; i++)//for para o numero de linhas, usamos o sz-1 pois o indice do array começa no 0 e i>=0 para poder chegar ao indice 0
  {
    for (int j = 0; j < sz; j++)//for para o numero de colunas
    {
      tabuleiro[i][j] = rand() %4 + 1;//gerar a matriz com valores aleatórios
    }
  }

  int jx, jy, mx, my, pontos;
  pontos=0;

  while(true)
  {
    for (int i = 0; i < sz; i++)
    {
      for (int j = 0; j < sz; j++)
      {
          mostrar(sz, tabuleiro);
          printf("\n");

          printf("(0,0) e o canto inferior esquerdo\n");
          printf("Escolha o x (lembrando que x = x+1 linhas exemplo x(1) = linha 2):\n");//acontece devido ao índice começar no 0 e não no 1
          scanf("%d", &jx);
          printf("\n");

          printf("Escolha o y (lembrando que y = y+1 colunas exemplo y(1) = coluna 2):\n");//acontece devido ao índice começar no 0 e não no 1
          scanf("%d", &jy);

          if(tabuleiro[jx][jy] != 0) //utilizamos este if para condicionar a posicao pois se escolher 0 ele contava os pontos de todos os zeros juntos
          {
            jogada(sz, tabuleiro, jx, jy);
            printf("\n");

            gravidade(sz, tabuleiro);//assim sempre que uma jogada é executada ele faz a funçao gravidade

            int p = marcar(sz, tabuleiro, mx, my);
            pontuacao(p);

            pontos=pontos+pontuacao(p);

            printf("Pontos:\n");
            printf("%d\n", pontos);
            printf("\n");

            troca(sz, tabuleiro);
          }
          else
          {
            printf("\n");
            printf("Nao e possivel escolher uma posicao onde o valor de tabuleiro[x][y] seja 0\n");
            printf("\n");
          }
      }
    }
  }
}

i am hoping for help,
thank you for the time spent.
Comment: What have you tried so far to implement "coluna"? One simple and naive solution is to move all non-zero columns to the left.
Comment: hmmm, yes that should work! how could i do that?? because i only want to move the column when it becomes full of zeros not when it has one or more.
Comment: I've tried to use the position given by the player to see if that column is full of zeros or not, but that would just move one column, and itwon't work...
Comment: Well, check if the column is full of zeros, and if it is you just need to copy the contents of the immediately right column to it. This is the basic idea, but you may need to consider cases in which two or more subsequently columns are zero.
Comment: yes that's the problem... how would i fix that problme of 2 or more columns?
Answer: How about the following:
int coluna(int sz, int tabuleiro[sz][sz])
{
    int i, j, k, m;
    i= 0;
    m= sz;
    while (i<m) {
        int allzeroes= 1;
        for (j=0; j<sz; j++) {
            if (tabuleiro[i][j]!=0) {
                allzeroes= 0;
                break;
            }
        }
        if (allzeroes) {    // col i is all zeroes
            // move all remaining cols down 1 col
            for (j=i; j<m-1; j++)
                for (k=0; k<sz; k++)
                    tabuleiro[j][k]= tabuleiro[j+1][k];

            // zero last column
            for (k=0; k<sz; k++)
                tabuleiro[m-1][j]= 0;

            // new last col that is not zero
            m--;
        }
        else i++;
    }
    return 1;
}

Comment: Yes but the column should move to the left not down, you are cheking it for the rows not the colums
Comment: Thank you for trying to help btw!
Answer: How about this (comments explain the code):
void coluna(int size, int board[size][size]){
    int not_zeros[size]; // Use to save the index of columns that contain at
                         // least one number different than zero.
    int i, j;
    for(i=0; i<size; i++){
        not_zeros[i] = -1; //initialize the array.

    for (i=0; i<size; i++){
        int foundNotZero = 0;
        for (j=0; j<size; j++){
            if(board[j][i] != 0){ //line j, column i.
                foundNotZero = 1;
                break;
            }
        }
        if (foundNotZero == 1){ //at least one element of the column i is not zero.
            not_zeros[i] = i;
        }
    }
    int start = 0;
    for(i=0; i<size; i++){
        if (not_zeros[i] >= 0){ //copy column i to column start.
            for(j = 0; j<size; j++){
                board[j][start] = board[j][not_zeros[i]];
            }
            start++; //increment start.
        }
    }
    for(i=start; i<size; i++){
        for(j=0; j<size; j++){
            board[j][i] = 0; //fill the remaining columns with zeros.
        }
    }
}

Comment: Thank you so much!
Comment: @Miketue accept the answer if this solves your problem.

I can't think how to move the column


For integers $n \neq 0$ is $\sin n$ irrational or transcendental?

Question: For integers $n \neq 0$ is $\sin n$ irrational or transcendental?
This arose from  another question. I would hypothesize yes and yes, possibly with proof for irrationality existing and but not for the more difficult property of transcendentality.
Any ideas?
Comment: https://math.stackexchange.com/a/299140/163 goes even further and shows the sine of any nonzero rational is transcendental
Answer: If $n\in\mathbb{N}\setminus\{0\}$, $\sin(n)$ is trascendental (hence irrational) by the Lindemann-Weierstrass theorem, since $2i\sin(n)=e^{in}-e^{-in}$.
Comment: We may say it also in the following way: if $\sin(n)$ is algebraic, $e^i$ is algebraic. But $e^i$ is not algebraic, hence neither it is $\sin(n)$.
Comment: Ah, very nice. Thanks
Comment: @Jack : I think this difference of trascendentals being a trascendental deserves an explanation (for example $(e+5)-(e+4)=1$ and nobody knows if $\pi+e$ is trascendental or algebraic). Besides you have assumed that $e$ is trascendental which can be accepted as well known of course. Regards.
Comment: @Ataulfo: you got it the wrong way. I was not claiming that the difference of two trascendental number is trascendental, but that if we assume that $\sin(n)$ is algebraic, by exploiting De Moivre's identity we contradict the Lindemann-Weierstrass theorem, Baker's formulation.
Comment: @Jack: I like more this last answer.Thanks.


Qual a diferença entre "compile time" e "run time"?

Question: Compile time e run time são termos comuns que frequentemente ouvimos na área de programação, quais são as principais diferenças ou características destes dois termos?
Comment: Essa resposta trata disso superficialmente: https://pt.stackoverflow.com/a/197854/64969
Comment: Basicamente, compile time se refere aos eventos que ocorrem durante a compilação (ex: erros de sintaxe) enquanto run time são eventos que ocorrem quando a aplicação está sendo executada (ex: nullpointerexception).
Answer: O compile time ou tempo de compilação é tudo o que ocorre durante o processo de compilação, tudo o que pode ser detectado, gerado, otimizado, realizado quando o código está sendo compilado. Em geral é onde se pega erros sintáticos, léxicos e semânticos, de tipagem ou até mesmo, em conjunto com outras ferramentas, validar com teste de unidade ou outras análises chamadas de estáticas.
Já run time ou tempo de execução é tudo o que ocorre quando o código já está executando, então ali se houver um problema pode ser tarde demais para solucionar. Por outro lado, pode ser que só naquele momento com o dado certo é possível entender o que ocorre, o que pode ser otimizado, etc. Erros dependentes do dado a ser processado costumam paralisar a execução se não houver um mecanismo que trate dele. Em geral entradas de dados diretas ou indiretas é que determinarão o que ocorrerá.
Para robustez e melhor performance costumamos dizer que é melhor resolver tudo o que dá em tempo de compilação, e é um dos motivos das linguagens interpretadas serem consideradas "inferiores" já que elas costumam ter só o tempo de execução. Mas há casos que justamente deixar para tempo de execução é a única ou melhor saída.
O termo link time pode ser usado também para aquilo que será resolvido em tempo de linkedição, mas é mais raro.
Algumas linguagens possuem o link time dinâmico, ou seja, logo antes do tempo de execução, e não logo depois da compilação. Algumas fazem de forma tão estruturada e complexa com um mecanismo específico que usamos o termo JIT time.
Ainda existe o design time, que tem um contexto um pouco diferente. Ele se refere ao momento que o código está sendo criado, ou seja, é antes da compilação e pode ter auxílio de alguma ferramenta.
Por último o termo development time ainda pode ser usado para todo o ciclo de desenvolvimento em contraponto ao ciclo de produção.
Pode interessar.
Coloquei no GitHub para referência futura.
Answer: Compile Time - Tempo de compilação:
Durante a compilação se detecta problemas óbvios no código como erros de sintaxe.
Run Time - Tempo de execução:
Durante a execução também podem ocorrer erros. Estes podem ser causados por inputs inadequados que não passaram por nenhum tratamento ou outra ocorrência que não pôde ter sido detectada como erro pelo compilador. Exemplos: O programa espera um número e é digitado uma letra, ou o programa tenta usar uma variável que não foi iniciada e dá um NullPointerException, etc.