text:
Q: Spark: sc.WholeTextFiles takes a long time to execute I have a cluster and I execute wholeTextFiles which should pull about a million text files who sum up to approximately 10GB total
I have one NameNode and two DataNode with 30GB of RAM each, 4 cores each. The data is stored in HDFS.
I don't run any special parameters and the job takes 5 hours to just read the data. Is that expected? are there any parameters that should speed up the read (spark configuration or partition, number of executors?)
I'm just starting and I've never had the need to optimize a job before
EDIT: Additionally, can someone explain exactly how the wholeTextFiles function works? (not how to use it, but how it was programmed). I'm very interested in understand the partition parameter, etc.
EDIT 2: benchmark assessment
So I tried repartition after the wholeTextFile, the problem is the same because the first read is still using the pre-defined number of partitions, so there are no performance improvements. Once the data is loaded the cluster performs really well... I have the following warning message when dealing with the data (for 200k files), on the wholeTextFile:
15/01/19 03:52:48 WARN scheduler.TaskSetManager: Stage 0 contains a task of very large size (15795 KB). The maximum recommended task size is 100 KB.

Would that be a reason of the bad performance? How do I hedge that?
Additionally, when doing a saveAsTextFile, my speed according to Ambari console is 19MB/s. When doing a read with wholeTextFiles, I am at 300kb/s.....
It seems that by increase the number of partitions in wholeTextFile(path,partitions), I am getting better performance. But still only 8 tasks are running at the same time (my number of CPUs). I'm benchmarking to observe the limit...

A: To summarize my recommendations from the comments:

*

*HDFS is not a good fit for storing many small files. First of all, NameNode stores metadata in memory so the amount of files and blocks you might have is limited (~100m blocks is a max for typical server). Next, each time you read file you first query NameNode for block locations, then connect to the DataNode storing the file. Overhead of this connections and responses is really huge.

*Default settings should always be reviewed. By default Spark starts on YARN with 2 executors (--num-executors) with 1 thread each (--executor-cores) and 512m of RAM (--executor-memory), giving you only 2 threads with 512MB RAM each, which is really small for the real-world tasks

So my recommendation is:

*

*Start Spark with --num-executors 4 --executor-memory 12g --executor-cores 4 which would give you more parallelism - 16 threads in this particular case, which means 16 tasks running in parallel

*Use sc.wholeTextFiles to read the files and then dump them into compressed sequence file (for instance, with Snappy block level compression), here's an example of how this can be done: http://0x0fff.com/spark-hdfs-integration/. This will greatly reduce the time needed to read them with the next iteration


text:
Q: btrace Invalid path 9116 specified: 2/No such file or directory i use btrace in linux,Remind me:
Invalid path 9116 specified: 2/No such file or directory
my exec command:btrace 9116 AllMethods.java
my home path is ok, i got Pid by jps,   btrace version is 1.3.8.3

A: This looks suspicious. Please, make sure that you don't have the 'other' btrace tool in path.

A: You can execute "which btrace", make sure this btrace is your want btrace

redpajama_set_name: RedPajamaStackExchange


text:
Q: MVC data annotation and un obtrusive javascript Could somebody please let me know if using data annotation for client side validation makes any difference in performance ( i.e. takes more time to load the page)? especially if it is quite a big page with loads of validation passed from model.
obviously these 2 attributes needs to be true in web.config to get that working..
EnableClientValidation
EnableUnobtrusiveJavaScript
Thanks in advance!


text:
Q: How do I run Asymptote in batch mode, without creating windows? I am using asymptote to generate lots of images in a LaTeX document. The images are translated from asymptote to PDF as this:
pdflatex text.tex
asy text-*.asy
pdflatex text.tex

However, asymptote insists in opening a window, (which briefly flashes and goes away, without any content). Because of that I cannot compile my document from a remote machine using ssh:
asy x.asy
freeglut (asy): failed to open display ''

Is there a way to run asymptote in some mode that does not require a window system (without a defined DISPLAY in Unix)?
I have already tried -noView and -nointeractiveView, but Asymptote will still try to open a window.

A: Asymptote has disabled offscreen rendering by default. To enable this feature, pass the -offscreen option to asy:
asy -offscreen text-*.asy

Alternatively you can create a config.asy configuration file and include the following:
import settings;
offscreen=true;


text:
Q: Is the cell size dependent on the number of features for the Euclidean Distance tool? I have attempted to run the Euclidean Distance tool, using ArcMap 10.5.1, on a feature class of 50 points (hospitals), but I received an invisible output. Using 'Identify' I get 'NoData' for the pixel values.
Is the cell size dependent on or limited by the number of features in the input FC?
What I think is more likely is the output cell size is causing the output issues. I want to do a Weighted Sum on my raster data, and I am trying to make the cell sizes in Euclidean Distance consistent for the raster math. I have grocery store and hospital FCs as separate inputs/outputs. I don't understand why this> is happening, but the default output cell sizes are drastically different for these two point FC inputs. One is 000s, and the other is 0.0000000 -E^3. I have tried to find an output cell size that works for both but haven't been successful (hospitals output raster is invisible/NoData).

A: If you look at the help file for this tool, especially the syntax section you can see that you can specify the cell size. So why not set it? Sounds like you are not setting this obvious parameter and ArcMap is having to do a best guess based upon the extents of the input data.  You do not describe what coordinate system your input features are. Try zooming to the layer's extent, does one of them zoom to your entire country/world? You mention lots of zeros so my gut feeling that they are in decimal degrees when really you should be in a coordinate system in metres/feet.


text:
Q: Forking and running dhclient (with params -nw -w) in exec creates a defunct process and a daemon process I am seeing a zombie process and the dhclient process when I fork from my parent process and then exec the dhclient process with its arguments
Here is the output
root     31298  0.0  0.0      0     0 pts/2    Z    09:22   0:00 [dhclientForData] <defunct>
root     31410  0.0  0.0  24260  9108 ?        Ss   09:23   0:00 -nw -w -sf etc/dhclient-script -pf /var/run/dhclient-vEth0.pid -lf /var/lib/dhcp/dhclient-vEth0.leases -cf /etc/sysconfig/dhclient.conf vEth0

I have renamed the linux system dhclient to dhclientForData in my parent process. Notice the second line in the above output I dont see the dhclient process name just the argurmnts I am not sure why, anyone?
my code
func_parent()
{
       pid_t child = 0;
        child = fork();
        if (child < 0)
        {
            return 0;
        }
        if (child == 0)
        {
            wanIntfDhclientHandlerProcessChildAdd ();
        }
        shmWlanTunMonitrConfig->childProcs[index].procId = child;
        sleep(1);
    }

    uint8_t wanIntfDhclientHandlerProcessChildAdd ()
    {
        pid_t mypid = getpid();
        uint8_t idx = gindex;
        //for (; idx < MAX_WAN_PORTS; idx++)
        {
            if (!shmWlanTunMonitrConfig->childProcs[idx].isRunning && shmWlanTunMonitrConfig->childProcs[idx].cmdIdx != 0xFF)
            {
                shmWlanTunMonitrConfig->childProcs[idx].isRunning = 1;

                char *const argV[] = {commands[idx].cmd[0],
                commands[idx].cmd[1],
                commands[idx].cmd[2],
                commands[idx].cmd[3],
                commands[idx].cmd[4],
                commands[idx].cmd[5],
                commands[idx].cmd[6],
                commands[idx].cmd[7],
                commands[idx].cmd[8],
                commands[idx].cmd[9],
                commands[idx].cmd[10],
                NULL};
                int ret = execvp(WAN_EXECUTABLE, argV);
    }
    }
    }

I have registered to the sigchld and will waitpid on that process. I have multiple processes running
pid_t pid = waitpid(shmWlanTunMonitrConfig->childProcs[index].procId, &status, WNOHANG);

I just delete the process from my parent process if the job is no longer required.
char cmd[512] = {0};
sprintf(cmd, "for KILLPID in `ps ax | grep \'/var/run/dhclient\' | grep \'%s\' | awk \' { print $1;}\'`; do kill -9 $KILLPID; done", portIntfAr[index].intfName);
system (cmd);

Can anyone please point me at what am I doing wrong.
Syed

A: The zombie process 31298 exists as long as the parent process did not yet wait for it, so probably there is something wrong with your process table or your waitpid call.
I cannot check because your code is incomplete.
Instead of waiting for a specific PID you could also wait for any child or for a process group and find out from the resultuing siginfo_t structure which process just reported its state change.
I don't know which string you assign with
char *const argV[] = {commands[idx].cmd[0],
/* ... */

argv[0] should be the file name of the program, i.e. the same as WAN_EXECUTABLE


text:
Q: would this code avoid to leak memory? I am not sure about the code below would cause any memory leak?
char **a;
a = new char* [m];
a[0] = new char[m * n]; // allocate all need
for(int i=1; i<m; i++)
{
a[i] = a[i-1] + n;//allocate every pointer
}
delete[] a[0];
delete[] a;

A: If your code runs to completion there are no memory leaks.  If any exception is thrown though then you will leak.

A: That's fine.  You have two allocations, and two deallocations, with matching types and matching pointers.  You won't leak any memory.

A: If the second new fails, then it will throw an exception and the first allocation will not be deleted, giving a leak.
The best way to avoid this is to use a resource-managing class, rather than manually managing memory. In this case, std::vector would be best:
std::vector<char> memory(m * n);
std::vector<char*> pointers;

pointers.reserve(m);
pointers.push_back(&memory.front());
for (int i = 1; i < m; ++i) {
    pointers.push_back(pointers.back() + n);
}

Now, the destructors are guaranteed to be called whether the code completes normally or throws an exception, so there will be no leak in either case.


text:
Q: a href=javascript different across browsers, strange behavior I have a site that relies on a JSON file that PHP parses to output a navbar (I built an editor so the customer can maintain content).  A link in the JSON file has this structure:
"Text to Display": {
    "href": "myPage.php",
    "child": {
        "Text for Child Link": {
            "href": "secondPage.php",
            "child": {}
        }
    }

And a recursive function (PHP) to loop over it and output the navbar such as:
function block_menuList($links) {
    $o='';
    foreach ($links as $k => $v) {
        $href='javascript:void(0)'; // default to link nowhere
        if (isset($v['href'])) $href=$v['href']; // pull link from array
        $o.="
$k"; // output list item
        if (isset($v['child']) && is_array($v['child'])) $o.="
".block_menuList($v['child'])."
"; // if there's a child list call recursively
        $o.="
";
    }
    return $o;
}

The site runs on a hash model system, and I'd like to be able to add links to change the hash model without having to switch this to conditionally use onclick instead of href.  The javascript is not responding how I expect it to.  I should be able to correctly use:
My Link

Instead of working, however, things like this happen:

*

*Firefox's URL bar says javascript:location.hash=buildHash('c=8') and the content is #c=8, and hitting the back arrow takes me to the expected target.

*Chrome kept the URL bar the same, but replaced the body's HTML with #c=8
I can define the link like this and everything works fine:
My Link

Calls to buildHash from javascript work as well, for example in JQuery event listeners.  Only the  method seems to be broken, but a simple test of  runs and shows the alert box.
edit: Due to this structure I won't be able to bind JS/JQuery events.  Here's the full object I'm using on site right now so my issue is easier to understand:
{
    "Home": {
        "href": "index.php",
        "child": {
            "About Us": {
                "href": "view.php?pg=about",
                "child": {}
            },
            "Contact Us": {
                "href": "view.php?pg=contact",
                "child": {}
            },
            "Location & Hours": {
                "href": "view.php?pg=location",
                "child": {}
            },
            "News": {
                "href": "news.php",
                "child": {}
            }
        },
        "parent": true
    },
    "Products": {
        "href": "cart.php",
        "child": {
            "categories": {
                "href": "categories",
                "child": {}
            }
        },
        "parent": true
    },
    "Sales": {
        "href": "javascript:filterModel.goto(\"sl=1\", \"\")",
        "child": {},
        "parent": true
    },
    "Clearance": {
        "href": "javascript:filterModel.goto(\"cl=1\", \"\")",
        "child": {},
        "parent": true
    },
    "My Account": {
        "href": "account.php",
        "child": {
            "Order Status": {
                "href": "ordStat.php",
                "child": {}
            },
            "Order History": {
                "href": "ordHist.php",
                "child": {}
            }
        },
        "parent": true
    }
}


text:
Q: Does this person get electrified? Does a person get electrified when his feet are on the floor, his right hand is holding $220~V$ and his left hand holding a piece of wood or any other insulator?

A: Assuming the ground to be at $0 V$, there is a potential difference of $220V$ between the person's right hand and the ground. The only way to prevent an electric current to flow from his right hand to the ground is if the pathways of current between those two are insulated, blocking the path of current. Holding an insulator in the left hand does not prevent electrocution because that insulator is not even in the path of current.
In order to prevent electrocution, insulation between the person's feet and the ground is the best option: it will remove the possible pathways of current from the right hand to the ground. However, this is only the case for objects with a sufficiently high value of dielectric strength. Otherwise the electric field strength due to the potential difference will cause dielectric breakdown in the insulator, and the insulating material will no longer insulate.


text:
Q: capturing a div with jquery selector I'm stumped.  I have a form that is dynamically populated from the results of a mysql query.  Depending on how many rows are returned,
the procedure will add a div for each row, and populate each div with a questionaire.  Initially each div is hidden, and for each div
there is a toggle button, and a description of the contents of that record in the mysql table.
Each div is dynamically assigned an id, such as "ACH" plus the index number of the record returned from the mysql query.  In the example
I'm working with now, there are two divs, one is "ACH925" and the other "ACH926", and this is clearly evident in the debugger.
The problem is that the toggle button for the first div toggles both.  No problem with the toggle button for div2; it only toggles
div2.  Also, the toggle button for div1 has a reversible effect on div2.  If both divs are not in the same visibility mode, the div1
toggle button will set div2 to hidden if div1 is set to display, or display when div1 is hidden.
I want each div to be independent and show or hide one or both.
some html code written in a php string, where $value is the index number returned from the query:
$(document).ready(function() {
    var rows=1;
    for (i=0; i<=rows-1; i++) {
        var table = document.getElementById("ACHTable'.$value.'");
        var row   = table.insertRow(-1);
        var cell  = row.insertCell(-1);
        cell.width = "12%";
        cell.innerHTML = "<button id=\"shwBtn'.$value.'\" class=\"showBtn\" type=\"button\">Show / Hide</button>";

        cell = row.insertCell(-1);
        cell.width = "88%";
        cell.style.fontSize="14px";
        cell.innerHTML = "DisputeID: '.$value.';  Dispute Date: '.$dd[$ctr].';  Merchant: '.$mer[$ctr].'";
        cell.innerHTML+= ";&nbsp Transaction Amount: $'.money_format('%i', ($amt[$ctr]/100)).'";
    }
});

and the following code where the toggles are executed:
$(document).ready(function () {
    var container = $("#ACH'.$value.'");
    console.log(container.prop("id"));
    $(".showBtn").click(function () {
        container.toggle();
    });
});

and finally this from the console, where the divs are visibly evident:

Thoughts, anyone?

A: You are adding this handlers:
$(".showBtn").click(function () {
    container.toggle();
});

to all existing elements with class declared as showBtn. So at end any button will toggle all container divs.
You may want to go with this:
$("#shwBtn'.$value.'").click(function () {
    container.toggle();
});

but thats quite ugly. Better would be to set it properly in buttons' onclick.
Easiest for now would be to get use of $.data() method, by adding to every button attribute of data-toggle="#ACH'.$value.'" and afterward add a click handler to all buttons at once:
$(document).ready(function () {
    $(".showBtn").click(function () {
        $($(this).data("toggle")).toggle();
    });
});

or something close to this.

A: you got really close to the answer.
$(".showBtn").click(function () {
    container.toggle();
});

this will toogle whatever is the value of container, you could you $(this) instead :
$(".showBtn").click(function () {
    $(this).toggle();
});

where this is the object that triggered the click.

Edit :
as yergo pointed out, this won't (exactly) fit your problem, but you can easily fix this by changing .showBtn to a exclusive class to trigger this, and assign this class to your ACH divs

A: Scope the selection to the container. This can be done using .find() or with a jQuery shortcut where you set the scope in the second argument.
$(document).ready(function () {
 var container = $("#ACH'.$value.'");
 console.log(container.prop("id"));
 $(".showBtn",container).click(function () { //<- button scoped to container
  container.toggle();
 });
});


text:
Q: Use of Promises for sequential processing I'm relatively new to Node.js and JavaScript  -please excuse if the question below is dumb.
To me, promises for async processing make sense but I'm not 100% sure about the use of promises when it comes to serial/sequential processing. Lets look at an example (pseudo code):
Objective: Read file, process what was read from the file and send notification
using HTTP post call.
bendUniverseWithoutPromise: function() {

   var data = fs.readFileSync(..); //Read the file
   var result = processData(data);
   this.postNotification(data);

}

In the above function, processData() can not run until we've read the file. And we cannot send the notification until we've finished processing.
Lets look at a slightly different version (assuming each of the above method calls return a promise or we wrap them in a promise):
    bendUniverseWithPromise: function() {
      return new Promise(function() {
         fs.readFileAsync(fileName)
            .then(processData(data))
            .then(postNotification(result))
        })
    }

Now, my questions are:

*

*Seeing that we require serial/sequential processing in this instance, how is the promise version better than the non promise version? What is it doing better than the first example? Maybe it is a bad example, but then what would be a good example to demonstrate the differences?

*Besides the syntax, the promise version adds a little (only a little) in terms readability of code and can get quite complicated with nested promises, context (this!) etc.

*I do understand that technically, the first method will NOT return until all processing is done and the second will return immediately and the processing, although still sequential (in context of the method), will carry on in the background.

*Is there a general rule regarding the use of promises? Are there any patterns and anti patterns?

Thank you in advance.

A: I will try to answer all four of your points by taking your example further.
Lets say the first operation (file read) is a slow I/O bound operation and takes 900 ms. The processing and notification are CPU bound and I/O bound respectively, taking 50 ms each. What do the terms "CPU bound" and "I/O bound" mean?
Now, both versions will take the same 1000 ms to complete, but the second example utilizes available resources better, as it is asynchronous. Herein lies the advantage of the promise based version. The first version will make the server completely unresponsive for an entire second, while the second version will only make the server unresponsive during the 50 ms CPU bound processing step.
This hopefully becomes even more lucid when we consider 10 of these requests coming in at the same time. The first example goes through them one at a time, serving request #1 after 1s, #2 after 2s, and so on, finishing after 10s. Its average performance is 1 req/s. The second version would start a file read for request #1, then immediately go on to request #2, spinning up another file read, and so on for all requests. All requests would then finish their reads in around 1s, assuming 100 ms overhead and little or no saturation of disk read bandwidth. The processing would then queue up, taking 500 ms in total for all requests. Lastly we could do the notification posting in parallel, due to it again being I/O-bound. All requests would then in this idealized example be finished in around 1.5 seconds at over 6 req/s, a 6x performance increase. This is exclusively due to the better resourcefulness provided by asynchronicity.
The rule is therefore, always use async/promises when performing I/O bound work.

Sidenote:
Your second example is not correct as there is no data or result variable defined in that scope, the correct version would pass in only the functions to then().
bendUniverseWithPromise: function (fileName) {
  return fs.readFileAsync(fileName)
           .then(processData)
           .then(postNotification)
}

A: First let's correct the asynchronous code:
bendUniverseWithPromise: function() {
  return fs.readFileAsync(fileName)
           .then(processData)
           .then(postNotification);
}

Now, that above was (almost, had it been complete) an anti-pattern - the explicit construction anti-pattern.
As for why you'd want to use the promised version, well, it's asynchronous
.. It allows other operations to take place while asynchronous (mostly I/O) operations are waited for.
Note: fs.readFileAsync does not return a promise by default and needs to be "promisified".


text:
Q: jQuery not being found by icheck using webpack (Angular 2) I am writing an app using Angular 2, webpack and I am using the icheck front-end library (http://icheck.fronteed.com/) which requires jQuery.
I am requiring them in my component thusly:
var jQuery: any = require("jquery");
require('../../../js/jquery.icheck.min.js');
require('../../../css/checkbox/orange.css');

This build successfully using the webpack server and npm, but I get an error in the browser:
Uncaught ReferenceError: jQuery is not defined (jquery.min.icheck.js:11)

I have used this plugin before in an Angular 2 component but I never had any problems like this. I have compared the current app with the working app and I can't see any differences; it should work!
I am really up against it now and have to get this prepared for Monday. Anyone have any problems like this?

A: Please try to Remove
var jQuery: any = require("jquery");

Add those code in header of the ts file.
declare var jQuery;

A: Try this
declare var $: any;

@Component({
  selector: 'foo'
})

export class FooBarComponent {
    $: any = $;
    constructor(el: ElementRef) {
        // use this way
        this.$(".fooBar") ...
    }
}


text:
Q: NodeJS `request` returns "Authentication is required." on HTTPS GET For a web application I'm trying to pull some data from a RESTful service which requires basic authentication.
So far I've got the following code:
var req = request({
    auth: 'Basic aGVsbG8gd29ybGQ=', //base64 encoded credentials "hello world"
    url: url.resolve("https://rest.someurl.com", "/url/to/data.xml"),
    method: "GET",
    jar: true
}, function(err, res, body) {

  if(err) return util.err(err);

  console.log(body);

});

When I pull this URL through Postman service with the authentication header set it returns the data properly. Also when I remove the auth option and enter the username/password in the URL directly I also get the data back as normal. The only situation I don't get any data back is with my code above.
What's wrong with my code above or what should I add to it to make it work?

A: Try letting request handle the basic authorization header by just providing the cleartext username and password in the URL. request will format it for you. You also most likely don't need the jar option and the default method is GET so no need to specify that.
var username = "your-username";
var password = "your-password";
var req = request({
    url: "https://" username + ":" + password + "@rest.someurl.com/url/to/data.xml")
}, function(err, res, body) {

  if(err) return util.err(err);

  console.log(body);

});

A: Sharing a slightly larger scope of my code I found that I needed to move the request() call to inside of the callback I have to get the username and password propagating correctly.
var getData = exports.getData = function() {

  var credentials = "";
  getCredentials(function(creds) {
    credentials = {
      'user': creds.username,
      'pass': creds.password
    }
    var req = request({
        auth: credentials,
        url: url.resolve("https://rest.someurl.com", "/url/to/data.xml")
    }, function(err, res, body) {

      if(err) return util.err(err);

      console.log(body);

    });
  });
}


text:
Q: fluidRow(column(3, : argument is missing, with no default I try to run shiny app and get error:

fluidRow(column(3,  :  argument is missing, with no default

I have two tabPanel. I check code and there is no missing argument, no additional comma etc. when I run some portion of this code it works. I can't guess source of this error.  How to deal with this problem?
Bellow you can see one part of UI-side code of this shiny app:
ui <- fluidPage(
  theme = shinytheme("slate"),
  tabsetPanel(
    tabPanel(h4("TOP N' financial"),
             fluidRow(
               column(3,
                      selectInput("bank", "Choose Bank", choices = unique(BANKS$BANK)),
                      numericInput("Number", "Choose top N borrower", value = 10, min = 1),
                      tags$style(type='text/css', ".selectize-input { font-size: 10px; line-height: 1px;}
                                 .selectize-dropdown { font-size: 10px; line-height: 10px; }"),
                      selectizeInput("excl","SELECT Columns", colname, multiple=T)
               ),
               column(9,
                      div(dataTableOutput("topNtable"), style = "font-size: 75%; width: 75%")
               )
             ),

             br(),

             fluidRow(style = "background-color:#4d3a7d;",
                      h4("Ratio Calculation and plotting"),
                      column(7,
                             style = "border: 4px ridge #01B392;",
                             fluidRow(
                               textInput("client_ID", "Insert Client ID", value = '204885044'),
                               uiOutput("ID_table")

                             ),
                             fluidRow(style = "border: 4px ridge #FF8902;",
                                      plotlyOutput('coef_plot'))
                      ),

                      column(5,
                             style = "border: 4px double red;",
                             h4('simple calculator'),
                             fluidRow(
                               tags$style(type='text/css', ".selectize-input { font-size: 10px; line-height: 1px;}
                              .selectize-dropdown { font-size: 10px; line-height: 10px; }"),
                               column(6,
                                      selectInput("num", "Numerator", num_cols, selected = 'TOTAL_ASSETS')),
                               column(6,
                                      selectInput("denum", "denumerator", num_cols, selected = 'TOTAL_LIABILITIES'))
                             ),
                             fluidRow(
                               column(6, align = "center",
                                      div(tableOutput("coefficient"))#,style = "font-size: 75%; width: 75%")
                               )
                             )
                      )
             ),

This is server code (whole)
server <- function(input, output, session){
  react_fin <- reactive(get_top_N_financial(input$bank,input$Number, input$excl)%>%
                          group_by(CLIENT_ID)%>%
                          filter(DATE == max(DATE))%>%
                          head(n=input$Number))
  output$topNtable <- renderDataTable(react_fin())
  #CALCULATOR
  calc_d <- reactive(get_top_N_financial(input$bank,input$Number, c(num_cols,'DATE')))
  coef_d <- reactive({
    req(input$client_ID)
    filter(calc_d(), CLIENT_ID == input$client_ID)
  })
  coef_df <- reactive({
    req(input$num)
    req(input$denum)
    ratio <- round(coef_d()[[input$num]]/coef_d()[[input$denum]], 4)
    data.frame(cbind(ratio, coef_d()$DATE))%>%
      rename('Date'=V2)%>%
      arrange(desc(Date))
  })
  output$coefficient <- renderTable(coef_df()%>%
                                      slice_head(n = 6))
  output$coef_plot <-renderPlotly(plot_ly(coef_df(), x = ~Date,
                                          y = ~ratio, type = 'scatter', mode = 'lines',
                                          height = 300, width = 500))

  Client <- reactive((get_name(input$bank, input$client_ID)))
  output$ID_table <- renderUI(Client()$CLIENT_NAME)
  pd_dt <- reactive(get_debt(input$client_ID))
  output$ecl_pl <- renderPlot(plot_pd_lgd(pd_dt()))
  ## create ind_table
  observeEvent(pd_dt(), {
    choices <- unique(pd_dt()$DATE)
    updateSelectInput(inputId = "ind_date", choices = choices)
  })
  output$ind_table <-function()(ind_sum_tbl(pd_dt(), input$ind_date))
  ################# PAGE2 #################
  d_PD_PTI <- reactive(get_PD_PTI(input$N, input$start, input$end, input$stage))
  output$reg_pl <- renderPlot(PD_PTI_Bank(d_PD_PTI()))
  d_PD_Box <- reactive(get_PD_Box(input$N,input$stage))
  output$Box_pd <- renderPlot(PD_Box(d_PD_Box()))
  output$PTI_PD <- renderPlot(PD_PTI_heat(d_PD_PTI()))
  d_LGD_LTV <- reactive(get_LGD_LTV(input$N, input$start, input$end,input$stage))
  output$LTV_LGD <- renderPlot(LGD_LTV_heat(d_LGD_LTV()))
  output$PD_Client_scatter <- renderPlotly(PD_diff_banks_scatter(input$start, input$end, input$N))
  df <- reactive(PD_CLIENT_DF(input$start, input$end,input$N))
  output$PD_scat <- renderPlot(PD_CLIENT_plot(df()))
  output$brush_tbl <-renderTable({
    brushedPoints(df(), input$brush) %>%
      select()
  })

}

Thanks in advance

A: My guess: Issue of parentheses: Here is a working example with your UI code.
library(shiny)
library(shinythemes)

BANKS

ui <- fluidPage(
  theme = shinytheme("slate"),
  tabsetPanel(
    tabPanel(h4("TOP N' financial"),
             fluidRow(
               column(3,
                      selectInput("bank", "Choose Bank", choices = unique(BANKS$BANK)),
                      numericInput("Number", "Choose top N borrower", value = 10, min = 1),
                      tags$style(type='text/css', ".selectize-input { font-size: 10px; line-height: 1px;}
                                 .selectize-dropdown { font-size: 10px; line-height: 10px; }"),
                      selectizeInput("excl","SELECT Columns", colname, multiple=T)
                      ),
               column(9,
                      div(dataTableOutput("topNtable"), style = "font-size: 75%; width: 75%")
               )
             )
    )
  )
),

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

}

shinyApp(ui, server)


text:
Q: When a optimization is no longer "Micro-optimization" I'm a team-leader/feature-architect that emerged from a developer position, so I have coding experience and a lot of the things being evolved now a days were implemented by me in the first place. Now to the point: Reviewing some code for the sake of refactoring (and some nostalgia) I've found a bunch of places that could be optimized, so as an exercise I gave myself 2 days to explore and improved a lot of stuff. After running a benchmark I found out that the general module performance had improved about 5%.
So I aproached some colleagues (and the team I run) and presented my changes. I was surprised by the general impression of "micro-optimization". If you do look at every single optimization, then yes, they are micro, but if you look at the big picture...
So my question here is: When is an optimization no longer considered "micro"?

A: Whether an optimization is micro or not is usually not important. The important stuff is whether it gives you any bang for the buck.
You wrote you spend two whole working days for a 5% performance increase. Did you spend those days wisely? Was those things you fixed the "most slow" part of your application, or at least those most easy to fix performance issues? Did your changes made you reach your performance target (that you didn't do before)? Does 5% performance matter at all in your case? Usually you want something like 100% or 1000% increase if you figure out that you need to improve your performance.
Could you perform your optimizations without disturbing readability and/or maintainability of the code?
Besides, what other costs did those optimizations render? How much regression test were you required to perform? How many new bugs did you create?
I know, this looks more like questions than an answer, but those are the kind of questions that should rule your decision to make an optimization or not.

A: Personally, I would differentiate between changes that lead to a reduction in algorithmic time or space complexity (from O(N^2) to O(N), for example) and changes that speed up the code or reduce its memory requirements but keep the overall complexities the same. I'd call the latter micro optimizations.
However, keep in mind that while this is a precise definition it should not be the only criterion for deciding whether a change is worth keeping: Reduced code complexity (as in difficulty to understand) is often more important, especially if speed and memory requirements are not a major cause of concern.
Ultimately, the answer will depend on your project: For software running on embedded devices the rules are different than for stuff running on an Hadoop cluster.


text:
Q: mySQL - selecting first and last row of data from 1 table from a certain user I have a table with many rows of data, which has an column filled with a date.
I need to select their first row and their last row between two dates.
Example:
Score table:
id      userId       value1         value2        date
1       1            1              2             2012-09-01 00:00:00
2       1            5              8             2012-09-03 00:00:00
3       1            7              11            2012-09-04 00:00:00
4       1            9              14            2012-09-05 00:00:00

I need to get the values value1 and value2 between the dates 2012-09-02 00:00:00 and 2012-09-04 00:00:00 of userId 1.
The desired result should be:
userId    value1_old    value2_old    value1_new    value2_new
1         5             8             7             11

The values _old should contain the data older then, but closest to the date 2012-09-02 00:00:00.
The values _new should contain the data earlier then, but closest to the date 2012-09-04 00:00:00
The problem is, how do I get these data? I don't know how to get this to work.
Thanks in advance,
Mark

A: SELECT   t.userId,
         first.value1 AS value1_old,
         first.value2 AS value2_old,
          last.value1 AS value1_new,
          last.value2 AS value2_new
FROM     (
  SELECT   userId, MIN(date) AS first, MAX(date) AS last
  FROM     Score
  WHERE    userId = 1
       AND date BETWEEN '2012-09-02 00:00:00' AND '2012-09-04 00:00:00'
) t
    JOIN Score first ON first.userId = t.userId AND first.date = t.first
    JOIN Score  last ON  last.userId = t.userId AND  last.date = t.last

See it on sqlfiddle.


text:
Q: Automatically adjust window width in tmux Is there a way to set tmux so that it automatically adjusts window width when I switch to the pane so that it will be wide axactly as the longest (visible on screen) line in the file I have open?
I'm doing this manually with the command
:resize-pane -R 10
or
:resize-pane -L 10
I always have to guess the number and when I switch to another pane I have to do it again
Note that this functionality is relevant only to vertical panes.

A: There does not seem to be a way to do this automatically.  You can, however use the mouse:
 mouse-resize-pane [on | off]
    If on, tmux captures the mouse and allows panes to be
    resized by dragging on their borders.


text:
Q: iPhone 5 case for parkour I am looking for a good iPhone 5 case for parkour. As I often climb up walls and something like this the iPhone should be protected well. I found the Griffin Survivor, which is actually perfekt but very very thick... Is there an alternative to Griffin Survivor, which is protecting well (also the screen) but smaller?

A: As you are aware, everything is a trade-off. Smaller case typically means less protection. Bigger usually equals more, obviously.
With that in mind, I have used an Otterbox, for rough going, and really like it:

*

*Otterbox Defender
It seems to be a solid build, and, although the rubber casing has gotten somewhat more loose over time, Otterbox does have a good warranty.
Now, this next thing is no match for a pocket, and likely as big as the Survivor, but perhaps you would like it:

*

*Otterbox Armor
If you desire an even more minimalistic approach and would consider carrying the phone in a pocket you could always go with my all-time case favorite:

*

*An Otterbox Clearly Protected -> Viewing Vibrant Screen Protector

*Speck Candyshell (or similar)

This setup doesn't have a belt clip, but the case feels very natural in your hand - so you don't lose the iPhone feel, but also don't give up too much protection. Note that the back of the case does scratch some, but most of that is aesthetics. And the screen protector is terrific.

A: The ideal shell will be a custom molded, carbon fiber sleeve that you would drop the phone into (or similar thin case to totally enclose the device) and matching your desired level of bulk/strength ratio.
The larger a case, the more rigid it becomes, but at some point, it's too large for your preference.
That would be the smallest possibly package and you won't need to be looking at the device during parkour.
It would be the simplest to slide your phone into the case and you wouldn't have any trade off between clear case or one too large to fit in your pocket or backpack or wherever else you carry the phone. (I've seen runners tape the entire phone to their back or arm or leg and simply place the phone in a plastic bag for waterproofing.)

A: I think the best one is the "lifeproof" case, is the one i use when i do parkour (shockproof), but is also waterproof, dirtproof, and snowproof, is an amazing, case, and is the thiner one that i have seen.


text:
Q: Read xml file using pd.read_xml with special characters in tags I would like to read an XML file with Umlauts in the tags using read_xml(), but I keep getting an error message.
import io
import pandas as pd

f = io.StringIO("""<?xml version="1.0" encoding="UTF-8"?>
<!--Erzeugt durch 4D Anwendung-->
<Root>
  <r1_Belegzeile>
    <ID>65377</ID>
    <Belegtext>ABCABCABC</Belegtext>
    <Belegkürzel>ABC</Belegkürzel>
  </r1_Belegzeile>
  <r1_Belegzeile>
    <ID>65377</ID>
    <Belegtext>EFGEFGEFG</Belegtext>
    <Belegkürzel>EFG</Belegkürzel>
  </r1_Belegzeile>
</Root>
""")

df = pd.read_xml(f)

yields
XMLSyntaxError: error parsing attribute name, line 5, column 12

I tried adding encoding="ISO-8859-1" to read_xml, but that did not help. The offending character is the "ü" in "Belegkürzel".
One solution would be to globally replace all of the umlauts in the file, but maybe I can circumvent this.
Changing the parser to parser="etree" only changes the error message to ParseError: not well-formed (invalid token): line 5, column 11.
Thanks a lot for any suggestions.

A: So far the only way I have found was to translate the umlauts and then read the result:
xml = f.read().translate(str.maketrans({"ä": "ae", "ö": "oe", "ü": "ue"}))

df = pd.read_xml(xml)

which yields:
|    |    ID | Belegtext   | Belegkuerzel   |
|---:|------:|:------------|:---------------|
|  0 | 65377 | ABCABCABC   | ABC            |
|  1 | 65377 | EFGEFGEFG   | EFG            |


text:
Q: How to make slider move percent by percent when I click "Play"? There are compositions with different duration and for them slider should move with different speed. For example, for one case, there is 3 seconds in 1 percent, in other case there is 5 seconds and so on. How to do so?

A: I assume you're making a video player, correct?
This will set a playhead element according to the elapsed time.
var playHeadPosition = videoEl.currentTime / videoEl.duration;
playheadEl.style.left = playHeadPosition + "%";


text:
Q: Implement Facebook Logout using facebook SDK for ios 6 How can I implement facebook logout flow,  so that when user clicks on logout button in app, then facebook ask for fresh login. I am using facebook SDK for ios 6.
Any help will be appreciated.
Thanks

A: You can try this
-(IBAction)logoutButtonPress:(id)sender {
    [FBSession.activeSession closeAndClearTokenInformation];
}


text:
Q: Creating an instance of class using lists and dictionaries I am working with classes and functions in python where I am facing some issues. I will explain step by step what I am trying to do:

*

*I have a list of tasks to be accomplished. e.g:
1:EAF Processing task for heat 1
2:EAF Processing task for heat 2

Here, I have tried to create a id.e.g :
self.id=id

*Each equipment is associated with list of equipment from the list of equipments.I have tried to implement it via :

equipment = ['EAF1','EAF2','AOD1', 'AOD2','LF1','LF2']
self.equipment=equipment

*Each id(task) will have the processing time.e.g:
{"1" :80.0
"2" :40.0}
I am unsure how to implement such logic?

*Each task will have electricity consumption. e.g:
{"1" :34.0
"2" :49.0}
I have tried to implement the class like:
class Tasks():
    def __init__(self,equipment,id,**kwargs,processingtime,electricityconsumption):
        self.equipment=equipment
        self.id=id
        for key,value in kwargs.iteritems():
            setattr(self,key,value)
        self.processingtime=processingtime
        self.electricityconsumtption=electricityconsumption

However, I am not completely convinced and it throws an error.
It would be great if someone helps me in creating this logic of the class object.
I tried to create the instance of class using available data. But however I am somehow not able to interpret the definition of the object and did not know how to create the instances using these data.

A:
@Driftr95, I have different processing times & electricity consumption for each id. How do I call that?

Not sure exactly what you're going for, but I'll suggest 2 solutions based on the assumption that your current data looks something like
equipments = ['EAF1','EAF2','AOD1', 'AOD2','LF1','LF2']
procTimes = {"1": 80.0, "2": 40.0}
eleCon = {"1": 34.0, "2": 49.0}

Suggestion 1
If you just want to avoid the error, you can simply shift all arguments ahead of **kwargs as it should be.
class Tasks():
    def __init__(self, equipment, id, processingtime:dict={}, electricityconsumption:dict={}, **kwargs):
        self.equipment = equipment
        self.id = id
        for key,value in kwargs.items():
            setattr(self,key,value)
        self.processingtime = processingtime.get(id)
        self.electricityconsumtption = electricityconsumption.get(id)

tasksList = [
    Tasks(taskEq, str(taskId), procTimes, eleCon, kwarg1=f'kw1_{taskId}', kwarg2=f'kw2_{taskId}')
    for taskId, taskEq in enumerate(equipments, 1)
]

Suggestion 2
class Task():
    def __init__(self, id, equipment, **kwargs):
        self.id=id
        self.equipment=equipment
        for key,value in kwargs.items(): setattr(self,key,value)

I moved id ahead of equipment just because id is often the first parameter, but it doesn't really matter much.
Then you can produce a list (tasksList) of 6 Task objects with
## Task(task_id,task_eq,processingtime=...,electricityconsumption=...) # <-- format
tasksList = [Task(
    str(taskId), taskEq,
    processingtime=procTimes.get(str(taskId)),
    electricityconsumption=eleCon.get(str(taskId))
) for taskId, taskEq in enumerate(equipments, 1)]


text:
Q: Expected value and standard deviation of roulette Suppose you are playing multiple independent rounds of roulette where you bet $x$ dollars on black then with probability $18/38$ you will double that bet (net gain of $x$ dollars) and with probability $20/38$ you will lose that money.
If you decide to bet 50 dollars on each round, let J be the amount of money you have after playing 20 rounds of roulette. Find $E(J)$ and $SD(J)$.
The answers are $E(J)=947.37$ and $SD(J)=223.3$
I am not sure how to approach this problem; initially I tried to think of it as an indicator problem where each indicator $=1$ when the $i$th round lands black on the roulette, but did not arrive at the correct solution. Any help is appreciated!


text:
Q: How can I trigger the click function on the chart, when I click on a point? Is there a way to trigger the chart click event, when I click on a point?
I am trying to have a consistent behavior, so I wonder if I can click on a point and in the click function of the point, call the chart click event.
Even better, if I can just disable altogether the click on the point; altho I use tooltip, so I guess that it won't work without points then.

A: Instaed of triggering, you can prepare a global functions and then call it in click point event.
$('#container').highcharts({
    chart: {
        events: {
            click: function (event) {
                chartClick();
            }
        }
    },
    plotOptions: {
        series: {
            point: {
                events: {
                    click: function () {
                        alert('point click');
                        chartClick();
                    }
                }
            }
        }
    },
    series: [{
        data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
    }]
});

function chartClick() {
    alert('chartClick');
}

Example: http://jsfiddle.net/sbochan/233x5698/


text:
Q: How to prevent Custom Model Binder on some properties in model. ASP.NET MVC I have created a custom model binder and registered it in Global.asax for decimal and decimal? types. I need to prevent it for some properties to bind values. How can I do it?
Look at the code below:
Globasl.asax
ModelBinders.Binders[typeof(decimal)] = new DecimalModelBinder();
ModelBinders.Binders[typeof(decimal?)] = new DecimalModelBinder();

DecimalModelBinder.CS
    public class DecimalModelBinder : IModelBinder
{
    // adapted from: http://haacked.com/archive/2011/03/19/fixing-binding-to-decimals.aspx/

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        if (controllerContext == null)
        {
            throw new ArgumentNullException("controllerContext");
        }

        if (bindingContext == null)
        {
            throw new ArgumentNullException("bindingContext");
        }

        var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
        var modelState = new ModelState { Value = valueProviderResult };

        object actualValue = null;
        try
        {
            var attemptedValue = (valueProviderResult.AttemptedValue ?? string.Empty)
                .Replace(Thread.CurrentThread.CurrentCulture.NumberFormat.NumberGroupSeparator, string.Empty)
                .Replace(Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencySymbol, string.Empty);

            actualValue = Convert.ToDecimal(attemptedValue, CultureInfo.InvariantCulture);
        }
        catch (Exception exception)
        {
            modelState.Errors.Add(exception);
        }

        bindingContext.ModelState.Add(bindingContext.ModelName, modelState);
        return actualValue;
    }
}

TestViewModel.CS
    public class TestViewModel
{

    [DisplayName("Percentage")]
    public decimal? Percent { get; set; }

    [FixedFeeRequired]
    [DisplayName("Fixed Fee")]
    public decimal? FixedValue { get; set; }
}

I want DecimalModelBinder to apply on one property only but I have registered it globally. Please suggest.


text:
Q: html then php then again html In the following code, I would like to get the input, create the page, then go to the redirected page.
But instead I am getting the input, then going to the redirected page (it's skipping creating the page).
How can I achieve the first one?
<?php

$newfile = $_GET['abc'];
$file = 'example.com';

if (!copy($file, $newfile)) {
    echo "failed to copy $file...\n";
}
?>

<form onsubmit="location.href='http://www.example.com/file-created;">

<form name="form" action="" method="get">
<input type="text" name="abc" placeholder="Type Here. . .">
<input type="submit" value="Go">
</form>
</form>

I tried using echo, but it was of no use (maybe I am making a mistake), and when I input something (without redirect), my url changes to example.com?abc=[input_here]. Why?

A: When you don't have the onsubmit, it is submitting the form to the server to the same page that is currently loaded because you don't have a value for the action="" set.  So if all of that code was in index.php, the form will by default submit to index.php because you didn't tell it go to anywhere else.
Since you are using method="get", it is going to add all the form data as a query string to the action (which you didn't specify, so it adds it to the current url).  So it appends abc=(whatever input) to your url and submits the form.
Now, when you have the onsubmit event in the url, instead of submitting the data to the server, it is just redirecting to that url.  Nothing is going to happen with all of the form data that was entered.  It is basically thrown away.
What you need to do is have your code submit to your server, do whatever processing you need to do, and then redirect to the url you want.
So something like:
<?php
// check if "abc=(whatever)" is in the url, and if so do the copy to, otherwise skip it
if(isset($_GET["abc"])) {
    $newfile = $_GET['abc'];
    $file = 'example.com';

    if (!copy($file, $newfile)) {
        echo "failed to copy $file...\n";
    } else {
        header("Location: http://yoursite.com/" . $newfile);
        exit;
    }
}
?>
<form name="form" action="" method="get">
  <input type="text" name="abc" placeholder="Type Here. . .">
  <input type="submit" value="Go">
</form>


text:
Q: Detect Who Created a Thread (w. Eclipse) How can I find out who created a Thread in Java?
Imagine the following: You use ~30 third party JARs in a complex plugin environment. You start it up, run lots of code, do some calculations and finally call shutdown().
This life-cycle usually works fine, except that on every run some (non-daemonic) threads remain dangling. This would be no problem if every shutdown was the last shutdown, I could simply run System.exit() in that case. However, this cycle may run several times and it's producing more garbage every pass.
So, what should I do? I see the threads in Eclipse's Debug View. I see their stack traces, but  they don't contain any hint about their origin. No creator's stack trace, no distinguishable class name, nothing.
Does anyone have an idea how to address this problem?

A: Okay, I was able to solve (sort of) the problem on my own: I put a breakpoint into
Thread.start()

and manually stepped through each invocation. This way I found out pretty quickly that Class.forName() initialized lot of static code which in return created these mysterious threads.
While I was able to solve my problem I still think the more general task still remains unaddressed.

A: I religiously name my threads (using Thread(Runnable, String), say), otherwise they end up with a generic and somewhat useless name. Dumping the threads will highlight what's running and (thus) what's created them. This doesn't solve 3rd party thread creation, I appreciate.
EDIT: The JavaSpecialist newsletter addressed this issue recently (Feb 2015) by using a security manager. See here for more details
MORE: A couple of details for using the JavaSpecialist technique: The SecurityManager API includes "checkAccess(newThreadBeingCreated)" that is called on the thread creator's thread.  The new thread already has its "name" initialized. So in that method, you have access to both the thread creator's thread, and the new one, and can log / print etc.   When I tried this the code being monitored started throwing access protection exceptions; I fixed that by calling it under a AccessController.doPriviledged(new PrivilegedAction() { ... } where the run() method called the code being monitored.

A: When debuging your Eclipse application, you can stop all thread by clicking org.eclipse.equinox.launcher.Main field in the debug view.
Then from there, for each thread you can see the stack trace and goes up to the thred run method.
Sometimes this can help and sometimes not.
As Brian said, it a good practice to name threads because it's the only way to easily identify "who created them"

A: Unfortunately it doesn't. Within Eclipse I see all the blocking threads, but their stack traces only reflect their internal state and (apparently) disclose no information about the location of their creation. Also from a look inside the object (using the Variables view) I was unable to elicit any further hints.

A: For local debugging purposes, one can attach a debugger to a Java application as early as possible.
Set a non-suspending breakpoint at the end of java.lang.Thread#init(java.lang.ThreadGroup, java.lang.Runnable, java.lang.String, long, java.security.AccessControlContext, boolean) that will Evaluate and log the following:
"**" + getName() + "**\n" + Arrays.toString(Thread.currentThread().getStackTrace())

This will out the thread name and how the thread is created (stacktrace) that one can just scan through.


text:
Q: flush without sync From what I've read, flush pushes data into the OS buffers and sync makes sure that data goes down to the storage media. So, if you want to be sure that data is actually written to disk, you need to do a flush followed by a sync. So, are there any cases where you want to call flush but not sync?

A: You only want to fflush if you're using stdio's FILE *. This writes a user space buffer to the kernel.
The other answers seem to be missing fdatasync. This is the system call you want to flush a specific file descriptor to disk.

A: Yes, lots.  Most programs most of the time would not bother to call any of the various sync operations; flushing the data into the kernel buffer pool as you close the file is sufficient.  This is doubly true if you're using a journalled file system.
Note that flushing is a higher level operation than the read() or similar system calls.  It is used by the C <stdio.h> library, or the C++ <iostream> library.  The system calls inherently flush the data to the kernel buffer pool (or direct to disk if you're using direct I/O or something similar).
Note, too, that on POSIX-like systems, you can arrange for data sync etc by setting flags on the open() system call (O_SYNC, O_DSYNC, O_RSYNC), or subsequently via fcntl().

A: When you fflush, you flush the buffer of one file to disk (unless you give NULL, in which case it flushes all open files). http://www.manpagez.com/man/3/fflush/
When you sync, you flush all the buffers to disk. http://www.manpagez.com/man/2/sync/
The most important thing that you should notice is that fflush is a standard function, while sync is a system call provided by the operating system (Linux for example).
So basically, if you are writing portable program, you in fact never use sync.

A: Just to clarify, fflush() applies only when using the FILE interface of UNIX that buffers writes at the application level. In case the normal write() call is used, fflush() makes little sense.
Having said that, I can think of two situations where you would like to call fflush() but not sync:

*

*You want to make sure that the data will eventually make it to disk even though the application crashes.

*Force to screen the data that the application has written to standard output so far.

The second case is the most common use I have seen and it is usually required if the printf() call does not end with a new line character ('\n').


text:
Q: If $1835^2 \equiv 1 \mod{7}$ and $1986^3 \equiv 0 \mod{7}$, why is $1835^2 + 1986^3 \equiv 0 \mod{7}$ I'm not sure what I'm missing; I've tried to apply Fermat's Little Theorem to prove that $1835^{1910} + 1986^{2061} \equiv 0 \mod{7}$.
Edit: OK, so my question now becomes: why is my calculator is telling me that $7 \mid 1986^3$?
input: 1986^3
output: 7833173256
input: ANS ÷ 7
output: 1119024751

A: You are probably using a calculator which rounds $\frac {1986^3}{7}$ to $1119024751$ and gives the false impression that  $1986^3$ is a multiple of $7$
Do the calculation by hand and you see  $\frac {1986^3}{7}$ is not an integer.

A: $1986^3$ can't be $0$ because $1986\equiv 5\bmod 7$ and $\mathbf Z/7\mathbf Z$ is a field.
Namely, $1986^3\equiv 5^3\equiv -2^3\equiv -1\mod 7$ and $1835\equiv1\bmod 7$, so what is true is that
$$1835^{1910}+1986^3\equiv 1+(-1)\equiv 0\mod 7.$$

A: $$1986 \equiv 5  ( mod \ 7) \\ 1986^{3} \equiv 5^{3} \equiv  \color{#c00}{6} (mod \ 7) $$

A: $\bmod 7\!:\,\ 1835\equiv 1,\ 1986\equiv\color{#c00}{ -2}\,$ (not $\,0),\,$ so
$\begin{align}\qquad\qquad &1835^{\large k}\! + 1986^{\,\large 3\,+\,6n}\\
\equiv &\ \ \ \ \ \ 1^{\large k}\! + (\color{#c00}{-2})^{\large 3(1+2n)}\\
\equiv &\ \ \ \ \ \ 1\ + (\,-1\,)^{\large\ 1+2n}\equiv 0\end{align}$
Edit $ $ Regarding the updated question, you don't need to use a calculator since it is easy mentally: $\!\bmod 7:\,\ 1986 = 2(10)^{\large 3}-14 \equiv 2(3^{\large 3})-0\equiv \color{#c00}{2(-1)}.\,$  In fact $\,10^{\large 3}\!+1 = 7\cdot 11\cdot 13\,$ so we can use $10^{\large 3}\equiv -1\,$ when computing mod any factor of $\,10^{\large 3}\!+1 = 1001,\ $ e.g. $\,77 = 7\cdot 11$.


text:
Q: Spring Ldap: Lookup dn without throwing exception if it isn't there Using LdapTemplate in Spring LDAP, I have this code:
Object object=null;
try{
    String dn = "cn=readers,ou=groups,dc=mycompany, dc=com";
    object = this.ldapTemplate.lookup(dn);
} catch(final NameNotFoundException e){
    // create Object
}

But since I've read my Joshua Bloch I know that exceptions should not be used for control flow. Is there a way to look up a dn to see if it exists without throwing an exception if it doesn't? There must be, but I can't find it. I'm looking for code that works like this (or similar):
String dn = "cn=readers,ou=groups,dc=mycompany, dc=com";
Object object=this.ldapTemplate.someMethod(dn);
if(object==null){
    // create Object
}

Can anybody help?
BTW: just looking at the JavaDoc won't help. None of the methods that throw NameNotFoundException say so in the JavaDocs.

A: The lookup() method is only supposed to be used when you know for certain that the DN exists. This is normally because you've searched for and found a user or group previously, and cached the DN returned from the server.
If you're looking for something that might or might not be there, the right API to use is ldapTemplate.search(), with an appropriate filter. This returns a list of results, and in the case where no results are found it returns an empty list rather than throwing an exception.

A: Actually, Spring force you here to use exceptions for flow control (i.e. it's not your fault, it's their decision).
I worked with LdapTemplate few months ago and I couldn't find anything better then catch that exception and evaluate that situation as "User not found".


text:
Q: How to prevent users from connecting directly to the switch, where the Access point is connected? I'm about to setup a large wireless network, at a dorm.
The hardware I'm using is going to be:
HP Procurve E2520-24G-PoE (J9299A)
Cisco Aironet 2602i Autonomous Access Point

Since the outlets at the AP's are being mounted on the wall, so everyone can access them, I would like to protect the ports on the switch, so nobody can circumvent our logs. (By connecting, without their mac addresses being registered to their room number)
The solution I have tried is setting up the ProCurve as a 802.1x authenticator against a RADIUS server, which is working.
The Access Points are configured as 802.1x supplicants, and succesfully authenticates to the switch, and has access to the network.
However, while this is working exactly as it should, if somebody were to disconnect an access point and instead connect a switch to the outlet, and then connect the AP to that switch. The AP would authenticate, and full access is granted to everyone on that switch. I've tried setting up client-limit on the Procurve switch, however that prevents any users on the AP from accessing the network.
How can I prevent users from accessing the network through these outlets, and still allow people to logon to the wifi?

A: Have you tried using port security? ftp://ftp.hp.com/pub/networking/software/Security-Oct2005-59906024-Chap09-Port_Security.pdf might help?

A: A very naive solution would be to set the port to trunk mode and drop untagged packets. Set the access points to tag all of their outgoing packets with a vlan. Configure the switch to drop any packets not on that vlan.
This won't stop someone who understands networking and can sniff the conversation between the ap and the switch and notice the vlan tag. But it should stymie causal abusers.

A: The only way to completely prevent this would be to tunnel the ap traffic to another device and set the acl on the port to only allow traffic to the tunnel endpoint.


text:
Q: Method Override with Scala 3 Macros I'm trying to override a method with use of Scala 3 macros and TASTY. I'd like to override any method of any type. Now I'm starting with this simple case.
I have a test base class:
class TestClass {
  def func(s: String) = "base"
}

I'd like to achieve this, but with use of TASTY as I found out that it is impossible to call new A on a generic type with quotes and splices:
'{
    new TestClass() {
       override def func(s: String) = "override"
    }
}.asExprOf[A]

I printed AST of the above code and I managed almost to recreate it. The problem is that I cannot call new on the generated class - I don't see a way to access the symbol or type of the new class. I also tried Symbol.requiredClass() with the new name, though it returned some symbol, I got an error during macro expansion that the class isn't found.
My questions are:

*

*Is it possible at all to derive custom types without explicitly invoking: new Class {} in quotes?

*Does ClassDef.copy register a new name that can assist with new instance creation?

*Can manual invocations of ClassDef create an instance of the class?

*How can I make use of a symbol returned by Symbol.requiredClass as it returns something even if not defined before?

The code that I created:
import scala.quoted.*

object NewClass {

  def newClassImpl[A: Type](e: Expr[A])(using Quotes): Expr[A] = {
    import quotes.reflect.*

    val typeRep = TypeRepr.of[A]

    val ret = typeRep.classSymbol.map(_.tree) match {
      case Some(
            cd @ ClassDef(
              name: String,
              constr: DefDef,
              parents: List[Tree],
              selfOpt: Option[ValDef],
              body: List[Statement]
            )
          ) =>
        println(cd.show(using Printer.TreeAnsiCode))

        val newItemsOwner = Symbol.spliceOwner.owner
        println("newItemsOwner = " + newItemsOwner)

        def createFunction(args: Term)(using Quotes): Term = {
          args
        }

        val newConstrSymbol = Symbol.newMethod(
          newItemsOwner,
          "<init>",
          MethodType(Nil)(
            _ => Nil,
            _ => TypeRepr.of[Unit]
          ),
          Flags.EmptyFlags,
          Symbol.noSymbol
        )

        val newConstrDef: DefDef = DefDef(
          newConstrSymbol,
          {
            case List(List(paramTerm: Term)) =>
              Some(createFunction(paramTerm).changeOwner(newConstrSymbol))
            case _ => None
          }
        )

        val newMethodSymbol = Symbol.newMethod(
          newItemsOwner,
          "func",
          MethodType(List("s"))(
            _ => List(TypeRepr.of[String]),
            _ => TypeRepr.of[String]
          ),
          Flags.Override,
          Symbol.noSymbol
        )

        val newMethodDef: DefDef = DefDef(
          newMethodSymbol,
          {
            case List(List(paramTerm: Term)) =>
              Some(createFunction(paramTerm).changeOwner(newMethodSymbol))
            case _ => None
          }
        )

        val parentSel = Select.unique(New(TypeTree.of[A]), "<init>")
        val parent = Apply(parentSel, Nil)

        val newClassDef: ClassDef = ClassDef.copy(cd)(
          name + "$gen",
          newConstrDef,
          parent :: Nil,
          None,
          newMethodDef :: Nil
        )

        val app = Apply(
          Select(New(TypeIdent(Symbol.requiredClass(name + "$gen"))), newConstrDef.symbol),
          Nil
        )

        val block = Block(newClassDef :: Nil, Typed(app, TypeTree.of[A]))
        val finalTerm = Inlined(Some(TypeTree.of[NewClass$]), Nil, block)

        println(finalTerm.show(using Printer.TreeAnsiCode))
        println(finalTerm.show(using Printer.TreeStructure))

        finalTerm.asExprOf[A]

      case other =>
        println("No class def found: " + other)
        e
    }

    println("Returned:")
    println(ret.asTerm.show(using Printer.TreeAnsiCode))
    println(ret.asTerm.show(using Printer.TreeStructure))

    ret
  }

  inline def newClass[A](a: A): A = ${ newClassImpl[A]('{ a }) }
}

The returned code is printed without complains as:
{
@scala.annotation.internal.SourceFile("src/main/scala/MethodsMain.scala") class TestClass$gen() extends TestClass {
    override def func(s: java.lang.String): java.lang.String = s
  }

  (new TestClass$gen(): TestClass)
}

But if returned by the macro I got an error during expansion:
[error]   |Bad symbolic reference. A signature
[error]   |refers to TestClass$gen/T in package <empty> which is not available.
[error]   |It may be completely missing from the current classpath, or the version on
[error]   |the classpath might be incompatible with the version used when compiling the signature.
[error]   | This location contains code that was inlined from NewClass.scala:86

Usage:
val res:TestClass = NewClass.newClass[TestClass](new TestClass)

Thanks for any help.

A: With new method Symbol.newClass (Scala 3.1.3) this becomes quite easy:
import scala.annotation.experimental
import scala.quoted.*

object NewClass {
  inline def newClass[A]: A = ${newClassImpl[A]}

  @experimental
  def newClassImpl[A: Type](using Quotes): Expr[A] = {
    import quotes.reflect.*

    val name: String = TypeRepr.of[A].typeSymbol.name + "Impl"
    val parents = List(TypeTree.of[A])

    def decls(cls: Symbol): List[Symbol] =
      List(Symbol.newMethod(cls, "func", MethodType(List("s"))(_ => List(TypeRepr.of[String]), _ => TypeRepr.of[String]), Flags.Override, Symbol.noSymbol))

    val cls = Symbol.newClass(Symbol.spliceOwner, name, parents = parents.map(_.tpe), decls, selfType = None)
    val funcSym = cls.declaredMethod("func").head

    val funcDef = DefDef(funcSym, argss => Some('{"override"}.asTerm))
    val clsDef = ClassDef(cls, parents, body = List(funcDef))
    val newCls = Typed(Apply(Select(New(TypeIdent(cls)), cls.primaryConstructor), Nil), TypeTree.of[A])

    Block(List(clsDef), newCls).asExprOf[A]
  }
}

Usage:
class TestClass {
  def func(s: String) = "base"
}

val res: TestClass = NewClass.newClass[TestClass]

//{
//  class TestClassImpl extends TestClass {
//    override def func(s: java.lang.String): java.lang.String = "override"
//  }
//
//  (new TestClassImpl(): TestClass)
//}

res.func("xxx") // override

Blog post: Possibility to generate arbitrary class implementations in macros
Scaladoc: Symbol.newClass
Issue: Support creating a new instance of a given Type[A] with Scala 3 macro
How to access constructor:

*

*How to create an instance of a generic type in a Scala 3 macro?

*How to access parameter list of case class in a dotty macro

*How do I use macro to create objects of any class
(Apply(Select.unique(New(TypeTree.of[T]), "<init>")...)

Scala 2 Append A Method To Class Body (Metaprogramming) (Scala 2, compiler plugin)


text:
Q: how can i break a loop by checking a condition I am trying to sum some integers and when I press '=' the loop will terminate and print the sum of those values. I am trying to give a space before press equal.
#include <stdio.h>

int main()
{
    int a, sum = 0;
    char b;

    while (1) {
        scanf("%d", &a);
        scanf(" %c", &b);

        sum += a;

        if(b == '=')
            break;
    }

    printf("sum = %d", sum);

    return 0;
}

A: The two scanf, i.e.
    scanf("%d", &a);
    scanf(" %c", &b);

will work like this:
Input: "1 2 3 4 ="
In the first loop a will become 1 and b will become 2.
In the second loop a will become 3 and b will become 4.
Now the remaining string is " ="
So in the third loop there is no integer to scan and consequently a will still have the value 3. b will get the value = and the loop terminates.
So sum is 1 + 3 + 3, i.e. 7
You can check that using simple printf statements:
    scanf("%d",&a);
    printf("a=%d\n",a);
    scanf(" %c",&b);
    printf("b=%c\n",b);

Input:
1 2 3 4 =

Output:
a=1
b=2
a=3
b=4
a=3
b==
sum= 7

Remember: Always check the value returned by scanf - like:
if (scanf("%d",&a) != 1)
{
    // Error - didn't scan an integer
    // add error handling
    ...
}

So you can do
while(1){
    if (scanf("%d",&a) != 1)
    {
        // No integer found

        // Add error handling...
        // For now - just stop the loop
        break;
    }
    sum+=a;
}

For input like "1 2 3 4 =" this give the result 10.
However, input like "1 2 3 4 HelloWorld" will also give the result 10, i.e. any non-integer input simply terminates the loop. If you only want the loop to terminate when the input is =, you can extend the error-handling.

A: Your issue is because you are doing scanf twice in your loop.
Do this instead:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    int sum=0;
    char a[10];
    while(1){
        scanf("%9s", a); // makes sure we won't have memory overflow
        if(strcmp(a, "=") == 0)
            break;
        sum+=atoi(a);
    }
    printf("sum= %d",sum);

    return 0;
}


text:
Q: Scale Action Bar background image proportionally Im using ActionBar in my Android App, and to customize each of the tabs, i put a background image behing the ActionBar, which looks the following:

On my phone actually it looks fine, but on other phones it's look very squashed. How can i scale the ActionBar proportionally based on the background's ratio? Are there any solution for this? I had to determine the height in fix DPI because if i leave it out, the ActionBar is not even showing up. My style code is the following right now;
<style name="MyActionBar" parent="android:Theme.Holo.Light">

    <item name="android:backgroundStacked">@drawable/tab_bg</item>
    <item name="android:adjustViewBounds">true</item>
    <item name="android:scaleType">fitEnd</item>
    <item name="android:height">87dp</item>

</style>

Update:
I just do not know where to Mr.T's xml code. Im using the basic android ActionBar, and i have only one xml named menu.xml, but if i put the code into it, no effect.
I tried to style the actionbar with the following, but still, if i do not specify the android:height parameter, the actionbar remains 0 and do not showing up.
<style name="MyActionBar" parent="android:Theme.Light">
    <item name="android:backgroundStacked">@drawable/tab_bg</item>
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:src">@drawable/tab_bg</item>
    <item name="android:scaleType">matrix</item>
</style>

The actionbar still looks like the following on some devices;
http://i.imgur.com/yQN67J3.png

A: You might do it with either scaleType(http://developer.android.com/reference/android/widget/ImageView.ScaleType.html)
You use ImageView instead of background placing it as the first layout's element and specify android:scaleType attribute for it:
 <RelativeLayout
 android:layout_width="fill_parent"
 android:layout_height="fill_parent" >

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:src="@drawable/backgrnd"
    android:scaleType="matrix" />

...

rest layout components here

...

 </RelativeLayout>

or drawable xml


text:
Q: NgRx 8 Test provideMockStore - setState for State Slice I have a smart component test for my NgRx implementation that looks something like this:
describe( 'Component', () => {
  let store: MockStore<State>;

  beforeEach( async( () => {
    TestBed.configureTestingModule( {
        /* ... */
        providers: [
            provideMockStore( { initialState: fromReducer.initialState } )
        ]
    } ).compileComponents();
    store = TestBed.get<Store<State>>( Store );
  } ) );

  it( 'should load items in #ngOnInit', () => {
    store.setState( {
        item: {
          ...fromReducer.initialState,
          entities: { [item.id]: item },
        },
        otherFeature: null,
        otherFeature: null,
        otherFeature: null
    } );
    component.items$.subscribe( items =>
        store.select( ItemStoreSelectors.selectItems ).subscribe( fromStore => expect( items ).toEqual( fromStore ) )
    );
  } );
});

I use provideMockStore and setState to mock my NgRx state. Everything works fine this way, but I really don't like this part of it:
store.setState( {
    item: {
      ...fromReducer.initialState,
      entities: { [item.id]: item },
    },
    otherFeature: null,
    otherFeature: null,
    otherFeature: null
} );

I have to add every other feature slice of my state to the setState function. Otherwise Typescript will throw Errors.

So preferably I don't want to set the root state, but rather a specific feature slice like this:
store.setState( {
  ...fromReducer.initialState,
  entities: { [item.id]: item },
} );

I couldn't find any documentation on how to use provideMockStore for specific slices of the state here.

A: I would recommend taking a different approach here. What you should do instead is unit test your selector and component independently.
NgRx 8 allows for mock selectors. Use that in your component to verify the component's logic is correct.
Then, unit test your selector independently to verify that your selector is working as intended.
This way the tests are independent, not brittle, and truly unit tests.
Edit: The docs I linked are official, but an alternative way to mock your selectors is:
  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [
        provideMockStore({
          selectors: [
            {
              selector: yourSelectorNameHere,
              value: someMockValueHere
            }
          ]
        })
      ]
    });

A: As xander mentioned you can use mockSelectors.
The reason why TS is complaining is because how you typed it.
MockStore<State> is probably your whole state tree, for just a feature slice, use MockStore<FeatureState>


text:
Q: How to display special and hidden characters from any file to cout using stream? So I want to be able to cout ALL the characters (hidden, special, Greek, and other characters) from any file into a text file. I've tried using getline() and get() functions but:
...
ifstream aFile("whateverTextFile.txt"); // can be any type of file besides txt file
char ch;
while (aFile.get(ch)) cout << ch;
...

also isn't doing the job. Want to read this "123abc@#$   " to cout "123abc@#$αβλ".

A: Not entirely sure what you mean by 'display', or 'hidden' characters.  Assuming you want to display control characters in hex, you would do something like
if(std::isprint(c)) {
    std::cout << c;
} else {
    std::cout << "0x" << std::hex << c;
}


text:
Q: Tkinter and multiprocessing - cannot pickle '_tkinter.tkapp' object I am trying to do multiprocessing from a tkinter UI when a button is pressed, but am getting an error: TypeError: cannot pickle '_tkinter.tkapp' object.
The below code works when I change the tkinter object to local (By removing the "self." part). Why is it so?
I have found similar posts (links below), and as I understand the problem is that tkinter objects cannot be pickled to share information between processes. I don't understand why does the code below create a problem since I am not trying to update the UI from the process.
Can what I am trying to do be achieved and what would be the best practices for that?
Python multiprocessing - TypeError: cannot pickle '_tkinter.tkapp' object
Cannot 'pickle' a Tkinter object in a multiprocessing environment in Windows
def main():
    main = MainClass()

class MainClass:

    def __init__(self):
        self.create_gui()

    def create_gui(self):
        self.ui_root = Tk()
        btn = Button(self.ui_root, text="Button", command=self.start_processsing_thread)
        btn.pack()
        self.ui_root.mainloop()

    def start_processsing_thread(self):
        process_thread = threading.Thread(target=self.dummy_processing)
        process_thread.start()

    def dummy_processing(self):
        with concurrent.futures.ProcessPoolExecutor() as executor:
            list = [1, 2, 3]
            results = executor.map(self.process, list)
            for number in results:
                print(number)
                end = time.perf_counter()
        print("Finished all")

    def process(self, secs):
        time.sleep(secs)
        return secs

if __name__ == '__main__':
    main()

EDIT:
After doing some searching on how to make the GUI run on a single process (Thanks to answers by @Bryan Oakley and @Reblochon Masque) I came up with the below code. I am a beginner with Python and would appreciate if someone could comment if this is a good way of doing this.
In my actual app after the user presses the button some image processing is done (text created). After the images are processed I need some objects to be returned to the UI process. I found that the way to do that is using Queue. But if using Queue I cannot use the concurrent.futures.ProcessPoolExecutor() as I was doing before. As I understand this is somewhat unwanted since ProcessPoolExecutor knows how to allocate the Processes to the cores of the Processor, but in the way I am doing it now, everything is done on a single core. Is what I wrote correct and is there a way to use the ProcessPoolExecutor?
def main():
    main = UIClass()

class UIClass:
    def __init__(self):
        self.queue = Queue()
        self.create_gui()

    def create_gui(self):
        self.ui_root = Tk()
        btn = Button(self.ui_root, text="Button", command=self.button_pressed)
        btn.pack()
        self.ui_root.mainloop()

    def button_pressed(self):
        print("Button pressed")
        self.ui_root.after(1000, self.check_results)
        self.dummy_processing()

    def dummy_processing(self):
        nr_list = [1, 2, 3, 4, 5]
        for secs in nr_list:
            process = TestProcess(self.queue, secs)
            process.start()

    def check_results(self):
        print("Checking results")
        try:
            result = self.queue.get(timeout=0.1)
            print("Result is " + str(result))
        except Exception as e:
            print("No results yet, waiting again")
        self.ui_root.after(1000, self.check_results)

class TestProcess(Process):
    def __init__(self, queue, secs):
        Process.__init__(self)
        self.queue = queue
        self.secs = secs

    def run(self):
        print("Run in process")
        result = self.process(self.secs)
        self.queue.put(result)

    def process(self, secs):
        time.sleep(secs)
        print("Done sleeping")
        return secs

if __name__ == '__main__':
    main()

A: Tkinter objects cannot cross process boundaries, which is in effect what this error is telling you. Python uses pickle to transfer objects between processes and tkinter objects cannot be pickled.
You need to structure your code so that 100% of the GUI is in a single process.


text:
Q: Azure AD B2C Refresh Token Revoked 403 Good morning everyone,
I Have set up an App Service in Azure and added Authentication via Azure AD B2C. So far so good everything works fine. After 1 hour though, when the token expires, I try to refresh it but then I get an error back that the refresh token has been revoked. What goes wrong here ? I have set up the backend like explained in this post : and when logging in I pass the additional parameter like so :
user = await Manager.CurrentClient.LoginAsync(currentContext,MobileServiceAuthenticationProvider.WindowsAzureActiveDirectory,new Dictionary<string, string>() { { "response_type", "code id_token" } });

This is my refresh code :
user = await Manager.CurrentClient.RefreshUserAsync();

This is the error I am getting

{Microsoft.WindowsAzure.MobileServices.MobileServiceInvalidOperationException:
  Refresh failed with a 403 Forbidden error. The refresh token was
  revoked or expired. --->
  Microsoft.WindowsAzure.MobileServices.MobileServiceInvalidOperationException:
  You do not have permission to view this directory or page.   at
  Microsoft.WindowsAzure.MobileServices.MobileServiceHttpClient+d__24.MoveNext
  () [0x001ec] in :0

Am I missing something here ?
Thank you
EDIT :
Here is a Screenshot of the Settings in the Portal

EDIT 2 :
Here is an error form the server logs when trying to refresh the token :

EDIT 3 :
And here the Application Log from the webserver for the given request :

2017-01-18T15:08:33  PID[6344] Verbose     Received request: GET
  https://api.near.lu/.auth/refresh 2017-01-18T15:08:33  PID[6344]
  Verbose     JWT validation succeeded. Subject:
  'sid:453a1ad2710466bc9873240e888d8b91', Issuer:
  'https://api.near.lu/'. 2017-01-18T15:08:33  PID[6344] Warning     The
  refresh request issued by sid:453a1ad2710466bc9873240e888d8b91 (SID:
  ac22f7309036f0c07a54b1299b9601ef) failed because no refresh tokens
  were found in the token store. 2017-01-18T15:08:33  PID[6344]
  Information Sending response: 403.80 Forbidden 2017-01-18T15:08:34
  PID[6344] Verbose     Received request: GET
  https://api.near.lu/.auth/login/aad?response_type=code%20id_token
  2017-01-18T15:08:34  PID[6344] Verbose     Downloading OpenID
  configuration from
  https://login.microsoftonline.com/nearauth.onmicrosoft.com/v2.0/.well-known/openid-configuration?p=B2C_1_Default
  2017-01-18T15:08:36  PID[6344] Verbose     Downloading OpenID issuer
  keys from
  https://login.microsoftonline.com/nearauth.onmicrosoft.com/discovery/v2.0/keys?p=b2c_1_default
  2017-01-18T15:08:37  PID[6344] Information Redirecting:
  https://login.microsoftonline.com/nearauth.onmicrosoft.com/oauth2/v2.0/authorize?response_type=code+id_token&redirect_uri=https%3A%2F%2Fapi.near.lu%2F.auth%2Flogin%2Faad%2Fcallback&client_id=c4c15bfb-eac4-4cdc-861f-eb01594e19d2&scope=openid+profile+email&response_mode=form_post&state=redir%3D%26b2cPolicy%3D&p=b2c_1_default&nonce=9e243549b62f49878cc370048c6effa9_20170118151334
  2017-01-18T15:08:39  PID[6344] Verbose     Received request: POST
  https://api.near.lu/.auth/login/aad/callback 2017-01-18T15:08:39
  PID[6344] Error       An error of type 'unauthorized_client' occurred
  during the login process: 'AADB2C90057: The provided application is
  not configured to allow the OAuth Implicit flow. Correlation ID:
  8336662f-8847-4f8b-bb37-6925a5d3e264 Timestamp: 2017-01-18 15:08:38Z '
  2017-01-18T15:08:39  PID[6344] Information Sending response: 401.73
  Unauthorized

EDIT 4:
This is the log of a successfull login :

2017-01-18T19:10:14  PID[6344] Verbose     Received request: GET
  https://api.near.lu/.auth/login/aad?response_type=code%20id_token
  2017-01-18T19:10:14  PID[6344] Information Redirecting:
  https://login.microsoftonline.com/nearauth.onmicrosoft.com/oauth2/v2.0/authorize?response_type=code+id_token&redirect_uri=https%3A%2F%2Fapi.near.lu%2F.auth%2Flogin%2Faad%2Fcallback&client_id=c4c15bfb-eac4-4cdc-861f-eb01594e19d2&scope=openid+profile+email&response_mode=form_post&state=redir%3D%26b2cPolicy%3D&p=b2c_1_default&nonce=817be561f67343688001637fa7808690_20170118191514
  2017-01-18T19:10:30  PID[6344] Verbose     Received request: POST
  https://api.near.lu/.auth/login/aad/callback 2017-01-18T19:10:30
  PID[6344] Verbose     JWT validation succeeded. Subject:
  '1eaf9f41-1562-4ac9-9538-b893c9123087', Issuer:
  'https://login.microsoftonline.com/07407dc1-946a-4afc-9186-84e6023ba814/v2.0/'.
  2017-01-18T19:10:30  PID[6344] Verbose     Calling into external HTTP
  endpoint POST
  https://login.microsoftonline.com/nearauth.onmicrosoft.com/oauth2/v2.0/token.
  2017-01-18T19:10:32  PID[6344] Information Login completed for 'aget'.
  Provider: 'aad'. 2017-01-18T19:10:32  PID[6344] Verbose     Writing
  'AppServiceAuthSession' cookie for site 'api.near.lu'. Length: 792.
  2017-01-18T19:10:32  PID[6344] Information Redirecting:
  https://api.near.lu/.auth/login/done#token=%7B%22authenticationToken%22%3A%22ey--REMOVED PART OF THE TOKEN--%22%2C%22user%22%3A%7B%22userId%22%3A%22sid%3Aed01ed7507f147976aa1704783267861%22%7D%7D
  2017-01-18T19:10:33  PID[6344] Verbose     Received request: GET
  https://api.near.lu/.auth/login/done 2017-01-18T19:10:33  PID[6344]
  Information Sending response: 200.0 OK

This is the error log i get when I try to refresh immediatley after logging in :

2017-01-23T10:55:06  PID[6344] Verbose     Received request: POST
  https://api.near.lu/.auth/refresh 2017-01-23T10:55:06  PID[6344]
  Verbose     JWT validation succeeded. Subject:
  'sid:ed01ed7507f147976aa1704783267861', Issuer:
  'https://api.near.lu/'. 2017-01-23T10:55:06  PID[6344] Warning     The
  refresh request issued by sid:ed01ed7507f147976aa1704783267861 (SID:
  9a6c2ee324a092937c5e2f953803e662) failed because no refresh tokens
  were found in the token store. 2017-01-23T10:55:06  PID[6344]
  Information Sending response: 403.80 Forbidden

Which is weird, as it can be seen in the above picture the token store is enabled...

A: The problem could be that your current App Service Authentication / Authorization setup is not configured to support token refresh. A simple way to confirm this would be to enable Application Logging and look at the warning messages in the application log stream when a refresh operation fails. More details on application logging can be found here: https://learn.microsoft.com/en-us/azure/app-service-web/web-sites-enable-diagnostic-log
See the following blog post (which I wrote) to learn more about how to update your Authentication / Authorization settings to support token refresh: https://cgillum.tech/2016/08/10/app-service-auth-and-azure-ad-b2c-part-2/#refresh. The short version is that you need to:

*

*create an app key in your B2C app registration and set that as the client secret in your Authentication / Authorization "Advanced" settings for AAD in the portal.

*Request the offline_access scope when logging in. This ensures you get a refresh token from AAD B2C when the user logs in.

Your login code should look like the following:
user = await Manager.CurrentClient.LoginAsync(
    currentContext,
    MobileServiceAuthenticationProvider.WindowsAzureActiveDirectory,
    new Dictionary<string, string>() { { "scope", "openid offline_access" } });

A: To use the refresh user, we have to store the refresh token to the app service token store.
You can check whether there are refresh token via using the request below:
Get:https://{yourMoibleAppName}.azurewebsites.net/.auth/me
X-ZUMO-AUTH: {accessToken}

The access token you can get from user.MobileServiceAuthenticationToken.
If there is no refresh token returned, you can check whether following requirements are satisfied:

*

*the token store is enable

*the response_type of request contains code

*Developers doesn't revoke the access_token, refresh_token, user permission
And all settings well, however the the app still get the 403 error, we should prompt user to login again(refer to Refreshing user logins in App Service Mobile Apps).
And to avoid misunderstanding, when you specify you were using Azure AD B2C tenant, would you mind sharing the detail steps you protected the mobile service?
Update( able to reproduce this issuing using the b2c app which register this way)


text:
Q: Accumulate values from raster using ArcPy I wrote the script:
import arcpy
if arcpy.CheckExtension("Spatial")=="Available":
    arcpy.CheckOutExtension("Spatial")
    print("Spatial")

from arcpy import env
from arcpy.sa import *
env.workspace = "D:\Semestr 2 MGR\GeoinformatykaB\Cwiczenia\C07\DaneSkryptGISv02\Dane\Orto"
arcpy.gp.ZonalStatisticsAsTable_sa("punkt","OBJECTID","CentrumGdanska.tif","C:/Users/ilona/Documents/ArcGIS/Default.gdb/ZonalSt_punkt2","DATA","MAXIMUM")

arcpy.CheckInExtension("Spatial")
print "end"

I have a problem. This script is failed.
This is error:
ERROR 000824: The tool is not licensed.
Failed to execute (ZonalStatisticsAsTable).

Could anyone check if it works?
The content of the task:
Create a script to accumulate values from the raster. The tool comes with:

*

*raster

*the point from which the zone will rise

*barrier value of the maximum sum of values obtained from raster cells.

A: Use ZonalStatisticsAsTable instead of arcpy.gp.ZonalStatisticsAsTable_sa.
The ZonalStatisticsAsTable function is  part of the arcpy.sa package and was imported with from arcpy.sa import *
Also use env.workspace = r"D:\etc... i.e a raw string r"" or double (escaped) backslashes or single forward slashes instead of single (unescaped) backslashes.


text:
Q: NSIS: reading DWORD from the registry? I'm trying to update an existing installer to read a registry value (if it exists) and change the install settings to match existing installation settings when doing upgrade installations.
But I'm getting hung up on not being able to sucessfully read the registry value.
I write the key like this in the installer:
WriteRegDWORD HKLM "Software\${PRODUCT}" "IniPath" 0x00000026

This part works just fine, the key is created or modified, and is of type DWORD, all is well.
But then if I try to read that value back, I seem to be getting an empty value every time.
ReadRegDWORD $IniPath HKCU "Software\${PRODUCT}" "IniPath"
MessageBox MB_OK "$$IniPath=$IniPath"

Results in printing $IniPath=
I also tried doing ClearErrors before reading and IfErrors after, and it does appear to be getting into IfErrors...but I don't understand why it would be having errors and not seeing the key. I am running 32 bit windows, so I doubt this is 64bit "wrong section of the registry" error.
The line to write the DWORD is working just fine. If I delete the registry key, and examine the registry using regedit immediately after, the key is there. But regardless of whether the key existed.
So what could be going wrong to cause it to fail to read the value, or how to debug this?

A: You are writing to HKLM and reading from HKCU...
To debug registry issues you can use Process Monitor.


text:
Q: NHibernate Criteria API help needed please I'm trying to create a criteria query that grabs "RejectedRecords" by useruploaded that are not flagged as being deleted or the Facility in the RejectedRecord is in a list of facilities that a user is assigned to (user.UserFacilities). I have the first part working fine (By User and Not Deleted) but I'm not sure how to add the OR clause to take records that are in the collection of user-facilities.  In SQL it would look like:
SELECT *

FROM RejectedRecords

WHERE (UserUploaded = 1 AND IsDeleted = 0)
OR FacilityId IN (SELECT FacilityId FROM UserFacility WHERE UserId = 1)

Here's my attempt in C# (Not sure how to perform the subquery):
    public IList<RejectedRecord> GetRejectedRecordsByUser(User u)
    {
        return base._session.CreateCriteria(typeof(RejectedRecord))
            .Add(
                    (
                        Expression.Eq(RejectedRecord.MappingNames.UserUploaded, u)
                        && Expression.Eq(RejectedRecord.MappingNames.IsDeleted, false)

                    )
            )
            .List<RejectedRecord>();
    }

A: The Key is to use Disjunction and Conjunction combined with a Subquery.
var facilityIdQuery = DetachedCriteria.For<UserFacility>()
   .Add(Expression.Eq("User.Id", u))
   .SetProjection(Projections.Property("Facility.Id"));

var results = session.CreateCriteria<RejectedRecords>()
    .Add(
        Restrictions.Disjunction()
        .Add(
            Restrictions.And(
              Restrictions.Eq(RejectedRecord.MappingNames.UserUploaded, u),
              Restrictions.Eq(RejectedRecord.MappingNames.IsDeleted, false)
            )
        )
        .Add(Subqueries.PropertyIn("FacilityId",facilityIdQuery))
    ).List();


text:
Q: How to modify this flake.nix so that I don't have to load the nix package every time I load the environment I've taken this post as example for my question :https://johns.codes/blog/building-typescript-node-apps-with-nix
I try to make development environment with a npm package that has not a nix equivalent.
I think that I'm very near from the end but I probably miss something important and it doesn't work.
in the folder node-gyp-build:
npm init -y

npm install  node-gyp-build

mkdir nix
rm -fr node_module
node2nix -16 --development --input package.json --lock package-lock.json --node-env ./nix/node-env.nix --composition ./nix/default.nix  --output ./nix/node-package.nix
nix-build nix/default.nix

Now my question is how to use the inout in in a flake.nix in order to have developement environment with this package
I've tried with that flake.nix
{
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
    flake-utils.url = "github:numtide/flake-utils";
  };

  outputs = { self, nixpkgs, flake-utils, ... }:
    flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = import nixpkgs { inherit system; };
        npm_pack = import (./nix/default.nix);
      in with pkgs;{
        devShell = mkShell { buildInputs = [ npm_pack];};
        ]; };
      });
}

and this command:
 nix develop --extra-experimental-features nix-command --extra-experimental-features flakes --ignore-environment

error: Dependency is not of a valid type: element 1 of buildInputs for nix-shell

A: The npm_pack var in your example has the following attributes (many omitted)

*

*package - The nix output of the package you are building (which can be added as a buildInput in your dev shell if its some executable)

*shell -  a devShell which gives you the npm deps to run the package you are trying to build, this would be equilvant to you cloning the package and runing npm install

so if you just want to use the package for another project your flake would look like
 outputs = { self, nixpkgs, flake-utils, ... }:
    flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = import nixpkgs { inherit system; };
        npm_pack = import ./nix { inherit pkgs system;};
      in with pkgs;{
        devShell = mkShell { buildInputs = [ npm_pack.package ];};
      });

If you want to just make it so you auto have npm deps "installed" you can use the shell, so the flake would look like
 outputs = { self, nixpkgs, flake-utils, ... }:
    flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = import nixpkgs { inherit system; };
        npm_pack = import ./nix { inherit pkgs system;};
      in with pkgs;{
        devShell = npm_pack.shell
      });