Why 'all' function in Python receives array instead of *args?

Question: the question is about popular Pythonic function all.
a = True
b = True
print(all([a, b]))

Why not to use *args ? So it will look like this:
a = True
b = True
print(all(a, b))

List seems redundant here - when we have the ability to pass arguments to *args.
Comment: Is your question why a function that is designed to check each element of iterables requires an iterable as argument? That kinda answers itself.
Comment: That's not an array, that's a list.
Comment: It takes an iterator or iterable object which is a common idiom in python. Among other benefits, the entire collection need not be resident in memory at the same time
Comment: It could be that `all` became a standard part of Python before the `*args` syntax was added.  But that would be many versions in past.
Answer: all is for when you already have an iterable. If you have two individual booleans, that's and's job:
print(a and b)

Making all take *args instead of an iterable would force materialization of the input, even in cases where a False result could be returned early, or when the input iterable has too many elements to hold all of them in memory.
from itertools import repeat

def starargs_all(*booleans):
    return all(booleans)

print(all(repeat(False, 10**10))) # prints immediately
print(starargs_all(*repeat(False, 10**10))) # takes a while, uses lots of memory, awkward *

Answer: Because all() works with ANY iterable, not only lists. E.g:
def makeGen():
   i = 1
   while i <= 10:
      yield i
      i = i + 1

all(makeGen())

Comment: You state, that `all()` works with ANY iterable. But isn't this the case for `*args` too?
Comment: This example is misleading, because `*args` also works with generator objects. Just try `def all2(*args): return all(args)`.
Comment: How it's related to my question ?
Comment: @Markus which would render the point of a generator completely pointless
Comment: @Markus But `*args` is creating a list as does `list(makeGen())`. Are you deliberately trying to missunderstand my answer? `all(*args)` is not working in the same way as `all(iterable)`, these are different things.
Comment: I am completely aware of that. But again you stress the word ANY. This clearly implies that UNLIKE `*args` `all` works with ANY iterable. You are implying that some iterables do not work with `*args`, which is incorrect.
Comment: @Markus *is supposed to work with any without extra work* might be a better  phrasing but to be honest, you are just nitpicking and wasting my time. There is already a code snippet to illustrate the point. As I said, you deliberately want to missunderstand what other people write and I'm totally pissed off by such behaviour. If you are able to understand the snippet, you should be able to understand the answer totally fine! There is no need to be more precise here. Bother somebody else if you want to boost your own ego.
Comment: So I downvote your post for a reason and get a downvote on one of my (1.5 year old!) posts 2 min later. This behaviour disgusts me.


The Probability of Rolling a Large Straight with 5 Six-Sided Dice

Question: I don't know the probability of rolling a large straight with 5 six-sided dice, so I need to know what the probability is.  What is it?
Comment: Matthew Conroy, it's about getting different consecutive numbers on every die in different ways
Comment: But now I know!  It's a 48 in 7,776 chance of rolling a large straight.
Comment: I hope this answer is right, you know, the probability comment that's above this one that I answered yesterday (Tuesday, November 26, 2013).  Also, why should comments typed in by yourself be edited up to 5 minutes?
Answer: The probability of rolling 1-2-3-4-5 in that order is $(1/6)^5$.  However any order will do, so it's $5!(1/6)^5$.
The probability of rolling 2-3-4-5-6 is similarly $5!(1/6)^5$.
Combining, we get $240(1/6)^5\approx 0.03$, i.e. about 3% of the time.
Comment: Wow!  That's amazing!  Awesome!  Fantastic!  I can't believe you used a factorial and an exponent to figure that out!  You also used a similarity thing!  Another thing you used is an "approximately equal to" sign to approximate the answer.  No doubt about it!
Comment: You're welcome, glad to be of service.
Comment: But I didn't even say "Thank you." and how are you out of service?  Also, how can you be glad when you're out of service?!


Deserialize a generic object in ServiceStack.Text

Question: I tried to find a solution for deserializing an anonymous object from JSON to a structure resembling the original JSON structure in ServiceStack.Text, but I did not find any acceptable solution. While I do understand that the deserializer does not have a clue about how to deserialize a generic object, I am ready to indicate it some rules like:

use for any generic object a Dictionary<string, object> or ExpandoObject, whichever the deserializer likes best
use for any list a List<object>

I tried to deserialize to a Dictionary<string, object>, but the entries are all strings, no matter if I have complex object inside or not, or even lists. The next try was with ExpandoObject, which was a little better, recognizing the lists as List<object>, but the objects inside are all strings. I assumed that for the scenario with the two rules indicated above it should be a simple configuration issue, but I did not find any possibility.
Does anyone have a clue in solving this puzzle? Thanks in advance.
Answer: It seems that the following settings do the trick:
JsConfig.ConvertObjectTypesIntoStringDictionary = true;
JsConfig.TryToParsePrimitiveTypeValues = false; // otherwise, for untyped jsons, any string value which can't be converted to primitive types will return null.

var dictionary = ServiceStack.Text.JsonSerializer.DeserializeFromString(json, typeof(object));

Another option would be the DynamicJson class provided by ServiceStack.Text, but the implementation does not preserve the original names from the JSON.
For an alternative implementation, check the JsonExpando class in Kephas Framework (https://github.com/quartz-software/kephas/blob/master/src/Kephas.Serialization.ServiceStack.Text/JsonExpando.cs). Important: use the above settings in JsonConfig.


Which specs matter when replacing a fan capacitor?

Question: I'm trying to repair a 14-year old ceiling fan that runs slow. Capacitors sound like a common problem. What I'm not sure is which replacement capacitor is best.
Here's photos of this fan's capacitor and wiring in its control box.

Using a digital multimeter to test capacitance between the red wire and the gray wire across from it on the capacitor, I got a reading of 0.430uF. I did that test by sticking one multimeter probe-needle into the gray wires' wirenut where it was able to contact their aluminum, and sticking the other probe-needle into the red wire's terminal in the plastic 9-wire connector. Given the capacitor's labor showing gray-to-red should have 3.5uF, I am thinking this supports the idea that the fan runs slow because the capacitor has gone bad. I am new to working with capacitors (done much more electrician than electronics DIY) so I might be reading this wrong, as I understand the capacitor could simply be 'under charged'. I disconnected the fan from its power source 1-2 days ago.
If it does make sense to replace the capacitor, my question is, what to replace it with? I found the two options below and neither is exactly a match - I can't seem to find an exact match.
A. https://www.lowes.com/pd/Harbor-Breeze-Ceiling-Fan-Triple-Capacitor/5012906113

B. https://hqrp.com/hqrp-ceiling-fan-capacitor-cbb61-35uf5uf6uf-5wire-plus-hqrp-coaster.html

Both options have the same number and colors of wires, which is what Lowe's tech support claims is all that matters. Option A from Lowe's has the first voltage at 350V vs the original 300V, and its capacitance is higher at 4.5uF vs 3.5uF and 6uF vs 5uF. Option B from a less reputable dealer is a closer match, with the only difference between 250V vs original 300V on one line. Am I right in thinking option B is the way to go for this replacement, since capacitance should match exactly, and having a slightly lower voltage is not a big deal given it is >240V anyway for this fan which is on a 120V circuit?
Any general feedback about this repair is also welcome. I am wavering on whether to just get a new fan. $12 in replacement parts seems better than $120 in new fan, and I'd like to reduce waste and learn skills when repair is reasonable.
Comment: Before you go changing the capacitor, make sure it's not a mechanical problem with the fan motor itself, such as dry or dusty bearings. The fan blades should move with the lightest possible human touch, i.e., quite literally with a feather's touch, and they should not suddenly halt on their own. Once moving, they should gradually, slowly come to a stop over a period of time. Any other finding indicates a mechanical problem.
Comment: @MTA The fan blades and motor housing spin without a problem. There is very minor wobble but seems nominal. It slows gradually and doesn't come to a sudden stop, and there's no more resistance on it than on other fans that work well.
Comment: I tried disassembling the fan as much as possible to clean around the motor but I haven't been able to get the motor housing itself open. The rest of it all looks good, and when inspecting the motor with a flashlight-through-vent-holes, I see no dust, debris, or obvious problems.
Answer: If you're going to attempt to test capacitance, you have to remove the capacitor from the circuit, first. In circuit, you're not testing the capacitor, you're "testing" everything it's connected to. Which tends to invalidate the test results. Since your plan is to replace it, get it out of the circuit and then test it again. If it's still off, maybe you'll have success replacing it with a different one.

Capacitors sound like a common problem.

Or, capacitors are a part people like to throw at motors in the hopes that they will magically fix a problem. Often as not (without actually troubleshooting the problem) it just means more money down the drain and no fix. But sometimes the thrown part happens to be the problem.
For a replacement:
Voltages should be equal to or greater than the original. Hopefully obviously, for an AC rated capacitor you need an AC rated replacement, not a DC replacement (which tend to explode when reverse polarity is applied.) The voltage rating for a motor capacitor frequently needs to be well above the supply voltage.
Capacitance should be the same as the original, though typically tolerances are loose enough that + or - 20% or so is not an issue.
There are much more complete sources for new capacitors than Lowes or "less reputable other supplier," such as actual electronics suppliers. You can also buy 3 separate capacitors (and wire them up correctly) rather than a 3-in-1 if you can't find a 3-in-1 to suit.
To reiterate - test the original one properly, by removing it from the circuit. If it tests pretty much where it should be that way, it's unlikely that a replacement capacitor will do anything but add $12 to the cost of replacing the fan anyway.


How to get Herobrine skin and powers (invisibility)?

Question: How do I get Herobrine's appearance WITH his powers? I had recently watched a video where the person turned into Herobrine and had his powers to fly and become invisible. How do I do that? If it's a mod, please put a link.
Comment: that was faked, there is no herobrine in minecraft, the powers were done with potions and creative mode
Answer: You can get herobrine skin here.
The abilities you said does not require mod to do so, but permissions.
Use /gamemode <player> creative to turn a player into creative mode, which grants abilities to obtain/destroy any blocks and fly. This command requires operator permission to use.
/vanish is the command which makes yourself invisible. This command requires bukkit-based server and the permission is vanish.vanish.
Comment: Or you could use the invisibility potion effect...
Comment: Yes you can.  You can clear an invisibility potion effect by using `/effect @p 14 0` (14 is the invisibility ID).  Not only that, you can set it to not show particles with the command line, something you can't do with an actual potion.
Comment: Yes, but you cannot control when to appear precisely.
Comment: Oh, sorry. I thought you mean the potion only.
Comment: Because you said "Invisibility Potion Effect", I assumed you were talking about the potion only.
Comment: If I was talking just about potions, I would have said "invisibility potions'.
Comment: Yeah this is certainly my mistake.
Comment: @008 Drinking Milk also clears all effects I believe
Comment: (1.13 syntax for command: `/effect clear @p minecraft:invisibility`)
Answer: You could find/make the skin and you cold drink a potion of invisibility for the invisible part and you could use commands/mods to fly.
If you want to make it (the skin) you can go to "http://www.minecraftskins.com/"
Answer: There is a setting before you generate the world in more options called host privelages.
If enabeled you can give yourself the ability to fly, as well as to be invisible all
In survival.
    As for the skin.  :(    ???


routesAreCached() method in Laravel undefined

Question: Please help me out a bit. I'm trying to setup passport for my Laravel app by following the official docs. But i'm stuck at the step where I need to check before calling Passport::routes(). My vscode displayed an error saying

undefined method: routesAreCached()

even though when I traced back to the base abstract class ServiceProvider.php, the code there seems to be calling $this->app->routesAreCached() without any problems. Below is my App\Providers\AuthServiceProvider.php code.
<?php

namespace App\Providers;

use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Gate;
use Laravel\Passport\Passport;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The model to policy mappings for the application.
     *
     * @var array<class-string, class-string>
     */
    protected $policies = [
        // 'App\Models\Model' => 'App\Policies\ModelPolicy',
    ];

    /**
     * Register any authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();

        /**
         * This method will register the routes necessary to issue access tokens and revoke access tokens, clients, and personal access tokens:
         *
         */

         if (! $this->app->routesAreCached()) {   // error at this line
            Passport::routes();
         }

    }
}

Comment: VSCode has issues detecting which classes/methods are available for Laravel projects. There are plugins available that help, like https://marketplace.visualstudio.com/items?itemName=bmewburn.vscode-intelephense-client and https://github.com/barryvdh/laravel-ide-helper, or your can use a more robust PHP IDE, like PHPStorm, etc.
Answer: Try this
    /** @var CachesRoutes $app */
    $app = $this->app;
    if (!$app->routesAreCached()) {
        Passport::routes();
    }

i hope it was useful !
Answer: Use this , It will work
public function boot()
{
    $this->registerPolicies();

    /** @var CachesRoutes $app */
    $app = $this->app;
    if (!$app->routesAreCached()) {
        Passport::routes();
    }
}

Answer: Passport's routes have been moved to a dedicated route file. You can remove the Passport::routes() call from your application's service provider.
This link may help


Passing defined type in main to header in C

Question: So, as the title says, I am supposed to define a type in main file and, somehow, pass it to header. I tried to do that in any way, but I am out of ideas.
I define it like this: "typedef int DATA;" and use it in header as a data type.
I tried inserting typedef before including header but still it isnt recognised.
Any ideas? Or it is not even possible to do it in this way?
#include <stdio.h>
#include <stdlib.h>

typedef int DATA;

#include "PriorityQueue.h"

int main(){
    int priority;
    DATA value;

That's beginning of my main
#ifndef PRIORITY_QUEUE_H_
#define PRIORITY_QUEUE_H_

struct priorityQueue{
    int priority;
    DATA value;
    struct priorityQueue *next;
};
typedef struct priorityQueue LINK;

void createEmpty(LINK **head);
void add(LINK **head, int p, DATA v);
void deleteElement(LINK **head, int *p, DATA *v);
//void print(LINK *head);
void checkIfEmpty(LINK *head);
void destroy(LINK *head);
#endif

Header
||=== Build: Release in PriorityQueue (compiler: GNU GCC Compiler) ===|
~\PriorityQueue\PriorityQueue.h|6|error: unknown type name 'DATA'|
~\PriorityQueue\PriorityQueue.h|11|error: unknown type name 'DATA'|
~\PriorityQueue\PriorityQueue.h|12|error: unknown type name 'DATA'|
~\PriorityQueue\PriorityQueue.c|10|error: unknown type name 'DATA'|
~\PriorityQueue\PriorityQueue.c|44|error: unknown type name 'DATA'|
||=== Build failed: 5 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
 Used ~ instead of my destination

Comment: Show minimal example code of the problem. Most likely your typedef should be in the header, but it is a little unclear what you are doing.
Comment: @user694733 I've added a bit of my program, but, as I compile it as project, it gives me an error: error: unknown type name 'DATA'.
I use codeblocks to make a project.
Comment: Where are `add` and `deleteElement` functions defined? If they are not in same file as main, then `DATA` won't be available there, and will probably throw a compilation error. It might help if you added *complete* compiler error messages you get.
Comment: @user694733 I get the same errors in the lines where DATA is in header and in other file where are my functions defined, no other kind of errors.
Comment: Please post the **complete** error message.
Comment: ||=== Build: Release in PriorityQueue (compiler: GNU GCC Compiler) ===|

~\PriorityQueue\PriorityQueue.h|6|error: unknown type name 'DATA'|

~\PriorityQueue\PriorityQueue.h|11|error: unknown type name 'DATA'|

~\PriorityQueue\PriorityQueue.h|12|error: unknown type name 'DATA'|

~\PriorityQueue\PriorityQueue.c|10|error: unknown type name 'DATA'|

~\PriorityQueue\PriorityQueue.c|44|error: unknown type name 'DATA'|
||=== Build failed: 5 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
Used ~ instead of my destination
Answer: Usually you shouldn't do that but assuming you have a good reason to do it, it works if you put the typedef before the inclusion. I don't know how do you tested it but this works ok for me.
$ cat header.h
typedef struct
{
  MYDATA a;
  MYDATA b;
} MYSTRUCT;

$ cat main.c
typedef int MYDATA;
#include "header.h"

int main (int argc, char **argv)
{
 MYSTRUCT s = {0, 1};
 s.a = 1;
 s.b++;

 return 0;
}

It compiles without warning and executing it with gdb I see the types properly defined.
Comment: hmm, yours works for me too, but mine doesn't. I'll add my program
Comment: Yours work for me too. I completed the main with `value = 1` and `value++` to see if it does something. It compiles with a warning about `priority` being unused and runs properly.
What specific error are you getting?
Comment: error: unknown type name 'DATA'.
As I launch my program separately, it works normally. But when I try doing a project(using Codeblocks) it gives me this.
Answer: Put typedef int DATA; in PriorityQueue.h before struct priorityQueue, and make sure PriorityQueue.h is included in PriorityQueue.c.


systemd --user: Can I mask all (non-essential) global services?

Question: I am trying to learn more about how my desktop environment is started, especially my user services. In my current session on Ubuntu 20.04 there are several dozen services active by default most started by systemd --user as global services. This makes it difficult to understand what is going on.
I would like to create a separate user that I can use for debugging, experimenting and start with the smallest possible set of essential services. Like a display manager, window-manager, dbus --session, a panel and what ever is needed to get into a graphical session.
As a start I am looking for a way to mask or otherwise disable all systemd --user services only for a given user and only load some that are specified by the user explicitly (e.g. in ~/.config/systemd/user)? Is that possible?
Comment: To pick up your analogy, what I am asking for is a way to strip down the car until only the breaks the steering wheel and the gas pedal are left. Sort of like a minimal configuration that still allows me to drive the car, but without stuff like a radio or electric windows. Once I am there I want to put back each component one by one.
Comment: You're going to break your system...
Comment: Possible, but in a VM there would not be much harm and I might learn something about my system.
Comment: Eh, it's kind of like buying a brand new car, and then saying... "lets take a few parts off and lets see if it'll still run".
Answer: After reading more about how programs in a graphical session get started I learned that there are essentially three ways how programs can get started. The following list assumes an Xorg session, it might be different for Wayland:

Started by display manager (e.g. gdm) or xinit
Started via .desktop files found at various default paths on the system (e.g. /etc/xdg/autostart, or /usr/share/applications).
Started as user-service by systemd --user

So what a graphical Xorg session ends up running will depend not only on the systemd --user units getting started but also on what is in /etc/xdg/autostart and also how your X session is configured.
It is possible to mask each systemd user service via system --user mask $SERVICE. A list of service can be obtained via:
systemctl --user list-units -t service --state=running --full --all --plain --no-legend | awk -F ' ' '{ print $1 }'

As @heynnema mentioned its easily to break your system like that, for example I ended up masking my dbus.socket and then Xorg did not start anymore, probably because some processes that was required for the session depended on D-Bus.
Desktop Components (e.g. XDG Autostart)
Some components of the session might be managed by XDG autostart .desktop files. To disable an automatically started component, the respective .desktop desktop file has to be copied for the current user (e.g. from /etc/xdg/autostart or /usr/share/applications) to ~/.config/autostart and must contain Hidden=true.
However, if your session uses systemd, then these .desktop files may be automatically ignored if they contain X-GNOME-HiddenUnderSystemd=true.
Assuming the name of the current session is in ${XDG_SESSION_DESKTOP} the following one liner can be used to check which components are required by your sessions and see if they are managed by systemd:
components=$(cat /usr/share/gnome-session/sessions/$XDG_SESSION_DESKTOP.session|grep RequiredComponents | awk -F '=' '{ print $2 }' | tr ';' '\n' | awk 'NF'); for component in $components; do file=$(locate -r "/${component}.desktop$" | grep -v "/home" ); systemd=$(grep "X-GNOME-HiddenUnderSystemd" $file); echo "$file: $systemd"; done | sort -u

Some aspects might be specific to the GNOME desktop.
Comment: Thank you for asking and answering your own question. I was looking for this trying to understand my own system services. Thanks again!


Error with a form in Spring Boot using Thymeleaf

Question: I started spring boot recently, and I'm trying to create a form to add a student to an API that I created.
It show me this error :
There was an unexpected error (type=Internal Server Error, status=500).
An error happened during template parsing (template: "class path resource [templates/add.html]")
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/add.html]")
    at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:241)
    at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parseStandalone(AbstractMarkupTemplateParser.java:100)
    at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:666)
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098)
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072)
    at org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:366)
    at org.thymeleaf.spring5.view.ThymeleafView.render(ThymeleafView.java:190)
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1401)
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1145)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1084)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:655)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:360)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:890)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1743)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
    at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: org.attoparser.ParseException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "add" - line 15, col 25)
    at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:393)
    at org.attoparser.MarkupParser.parse(MarkupParser.java:257)
    at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:230)
    ... 48 more
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "add" - line 15, col 25)
    at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:117)
    at org.thymeleaf.processor.element.AbstractElementTagProcessor.process(AbstractElementTagProcessor.java:95)
    at org.thymeleaf.util.ProcessorConfigurationUtils$ElementTagProcessorWrapper.process(ProcessorConfigurationUtils.java:633)
    at org.thymeleaf.engine.ProcessorTemplateHandler.handleStandaloneElement(ProcessorTemplateHandler.java:918)
    at org.thymeleaf.engine.TemplateHandlerAdapterMarkupHandler.handleStandaloneElementEnd(TemplateHandlerAdapterMarkupHandler.java:260)
    at org.thymeleaf.templateparser.markup.InlinedOutputExpressionMarkupHandler$InlineMarkupAdapterPreProcessorHandler.handleStandaloneElementEnd(InlinedOutputExpressionMarkupHandler.java:256)
    at org.thymeleaf.standard.inline.OutputExpressionInlinePreProcessorHandler.handleStandaloneElementEnd(OutputExpressionInlinePreProcessorHandler.java:169)
    at org.thymeleaf.templateparser.markup.InlinedOutputExpressionMarkupHandler.handleStandaloneElementEnd(InlinedOutputExpressionMarkupHandler.java:104)
    at org.attoparser.HtmlElement.handleStandaloneElementEnd(HtmlElement.java:79)
    at org.attoparser.HtmlMarkupHandler.handleStandaloneElementEnd(HtmlMarkupHandler.java:241)
    at org.attoparser.MarkupEventProcessorHandler.handleStandaloneElementEnd(MarkupEventProcessorHandler.java:327)
    at org.attoparser.ParsingElementMarkupUtil.parseStandaloneElement(ParsingElementMarkupUtil.java:96)
    at org.attoparser.MarkupParser.parseBuffer(MarkupParser.java:706)
    at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:301)
    ... 50 more
Caused by: org.springframework.beans.InvalidPropertyException: Invalid property 'age' of bean class [com.example.demo.model.Student]: Getter for property 'age' threw exception; nested exception is java.lang.reflect.InvocationTargetException
    at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:708)
    at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:617)
    at org.springframework.validation.AbstractPropertyBindingResult.getActualFieldValue(AbstractPropertyBindingResult.java:104)
    at org.springframework.validation.AbstractBindingResult.getFieldValue(AbstractBindingResult.java:228)
    at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:129)
    at org.springframework.web.servlet.support.RequestContext.getBindStatus(RequestContext.java:903)
    at org.thymeleaf.spring5.context.webmvc.SpringWebMvcThymeleafRequestContext.getBindStatus(SpringWebMvcThymeleafRequestContext.java:227)
    at org.thymeleaf.spring5.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:306)
    at org.thymeleaf.spring5.util.FieldUtils.getBindStatus(FieldUtils.java:253)
    at org.thymeleaf.spring5.util.FieldUtils.getBindStatus(FieldUtils.java:227)
    at org.thymeleaf.spring5.processor.AbstractSpringFieldTagProcessor.doProcess(AbstractSpringFieldTagProcessor.java:174)
    at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74)
    ... 63 more
Caused by: java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at org.springframework.beans.BeanWrapperImpl$BeanPropertyHandler.getValue(BeanWrapperImpl.java:308)
    at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:630)
    ... 74 more
Caused by: java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because "this.age" is null
    at com.example.demo.model.Student.getAge(Student.java:43) \

Here is my files :
My controller :
@GetMapping(value="/add")
public  String addStudentForm (Model model)
{
    model.addAttribute("student",new Student());

    return "add";
}
@PostMapping(value = "/add")
public String addStudentSubmit(@ModelAttribute("student") Student student) {
    System.out.println(student.getLastname());
    return "addStudent";
}

My entity Student :
@Entity
@Table(name = "students")
public class Student {

public Student(long id, int age, String lastname, String surname, String mail, String course) {
    this.age=age;
    this.id=id;
    this.course=course;
    this.lastName= lastname;
    this.surname=surname;
    this.mail=mail;

}
public Student() {

}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name="lastname")
private String lastName;

@Column(name="surname")
private String surname;

private Integer age;
private String course;
private String mail;

public long getId() {
    return this.id;
}

public String getSurname() {
    return this.surname;
}
public void setSurname(String surname) {
    this.surname=surname;
}
public String getLastname() {
    return this.lastName;
}
public void setLastname(String lastname) {
    this.lastName=lastname;
}

public int getAge() {
    return this.age;
}
public void setAge(int age) {
    this.age=age;
}
public String getCourse() {
    return this.course;
}
public void setCourse(String course) {
    this.course=course;
}
public String getMail() {
    return this.mail;
}
public void setMail(String mail) {
    this.mail=mail;
}

}
StudentService :
   @Data
   @Service
   public class StudentService {

    @Autowired
    private StudentRepository studentRepository;

    public Optional<Student> getStudentID(final Long id) {
        return studentRepository.findById(id);
    }

    public Iterable<Student> getStudents() {
        return studentRepository.findAll();
    }

    public void deleteStudent(final Long id) {
        studentRepository.deleteById(id);
    }

    public Student saveStudent(Student student) {
        Student savedStudent = studentRepository.save(student);
        return savedStudent;
    }

  }

Add.html
<!DOCTYPE HTML>

  <title>Add a new student</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

Add a student

  <form action="#" th:object="${student}" th:action="@{/add}" method="post">
  Last name:
  <input type="text" th:field="*{lastname}" name="lastname"/>
  First name:
  <input type="text" th:field="*{surname}" name="surname" />
  Age:
  <input type="number" th:field="*{age}" name="age" />
  Email:
  <input type="email" th:field="*{mail}" name="mail" />
  Course:
  <input type="text" th:field="*{course}"  name="course"/>

  <input type="submit" value="Submit" />
  <input type="reset" value="Reset" />
</form>

AddStudent.html
<!DOCTYPE HTML>

   <title>Add a new student</title>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

   Add another student
    Return to home page

I've been searching why since last week and I can't find a solution.
Any help would be appreciated :)
Comment: The error message is pretty clear in that age is null.  It is hard to tell without see what values are actually getting submitted by the form.  Are you sure an age value is being sent?
Comment: More specifically, `age` is a capital `I` `Integer` -- meaning that it can contain null values.  However, your getter returns a lowercase `i` `int`.  Java can't unbox a null `Integer` to an `int`, so you get the error `Cannot invoke "java.lang.Integer.intValue()" because "this.age" is null`.  Either make the getter return an `Integer` or make sure that you never have a null `age`.
Comment: Thanks you for your help the problem was with the getter it worked with Integer and not int.
Answer: The error is occurring because the 'age' property contains a value of 'null'.
when you have an input of type 'number', it needs a value, so thymeleaf is trying to get this value in the 'age' property of the 'student' object..
try inserting the 'min' and 'max' attributes in the input..
<input type="number" th:field="*{age}" min="1" max="100" />


Detect if data connection has been disabled

Question: I am struggling with an issue relating to the licensing service and network connection.
The bottom line is I want the LVL to work if the user is in the middle of nowhere with no signal, but I don't want them to be able to 'cheat' and turn off the data connection.
I already have detected and intercepted airplane mode, but some phones or apps let you turn data connection on or off.  (some don't)
My Epic 4g allows this, and I think the hidden API letting us control this is not working past 2.0 release. (iTelephony)
However is it possible to simply detect if the connection was disabled and not change that fact but simply react to it?
I can already detect whether or not there is a data connection or not-  what I want is to tell the difference between a disabled antenna and simply not having signal.  The market licensing check will fail and I want to give 'em a pass if they legitimately have no signal, but not if they forced it off.
Answer: Well, I don't need to check whether net has been disabled outside of airplane mode,
I found a different solution here:
http://code.google.com/p/androidbest/
What this code does is force the user to authenticate at least once.  Then if it fails to connect to the LVL server, it uses a cached authentication.  I had to change it a bit, notably it was launching the next activity twice, and now it solves my issue adequately.


c# , Timer disactivate prevent sleep function

Question: I use as following c# code to prevent sleep and monitor off:
internal static class NativeMethods
{
    // Import SetThreadExecutionState Win32 API and necessary flags
    [DllImport("kernel32.dll")]
    public static extern uint SetThreadExecutionState(uint esFlags);
    public const uint ES_CONTINUOUS = 0x80000000;
    public const uint ES_SYSTEM_REQUIRED = 0x00000001;
    public const uint ES_DISPLAY_REQUIRED = 0x00000002;
}

and Calling this function on Formload:
private void PreventSleep()
{
    NativeMethods.SetThreadExecutionState(NativeMethods.ES_CONTINUOUS |  NativeMethods.ES_SYSTEM_REQUIRED);
}

private void PreventMonitorOff()
{
    NativeMethods.SetThreadExecutionState(NativeMethods.ES_CONTINUOUS | NativeMethods.ES_DISPLAY_REQUIRED);
}

This way of code gives me timeless preventing. What I want is a limit of time.
Let say sleep after 40 minutes if there is no user who working with the computer. I want to deactivate those functions after 40 minutes. And then when the user finish using the computer I want to activate and prevent in 40 minutes. It will be like a loop. Like
I want to prevent windows from going to sleep only in 40 minutes , then sleeping as usual if there is no user who using the computer. Then when the computer is free from user, I want to wait in 40 minutes Before it's going to sleep again.
I tried using Timer with interval 2400000 (40 minutes):
private void timer1_Tick(object sender, EventArgs e)
{
    PreventSleep();
    PreventMonitorOff();
}

and Calling those functions:
private void Form1_Load(object sender, EventArgs e)
{
    timer1.Start();
}

How to do a loop with those functions, and will this code know that "Now there is movement of mouse or keybord and activate sleeping after 40 minutes"?
Thank you in advance!
Comment: There is no reasonable non-hack way, because you are going against user settings. You either define that your thread is "so important" that it keeps the system alive or you respect the user settings. What you are trying to do is override the user settings in the system configuration. Therefore the correct answer to your question is: Instruct the user to set the sleep time to your desired value (40 minutes), if they don't want to, respect that.
Comment: Good question, but please be careful using your terminology. The "functions" you refer to are actually "sub-routines".
Answer: With ES_CONTINUOUS you don't need to call it periodically.
So set a timer for 40 minutes, then remove the ES_CONTINOUS flag (see MSDN). The difficulty will be to detect whether the user is actually idle. If you don't have a specific action to determine this, it would be better to let the OS decide. You may use powercfg API to force the timeouts.


Furnace Draft Inducer Repair: is previously installed back plate necessary?

Question: I'm replacing a non-functioning draft inducer on the Goodman furnace in my house (GMP075-3).  The previous inducer was mounted with a sheet metal back plate between it and the wall of the furnace.  This back plate happens to cover the 3.5" draft hole on the wall of the furnace with a smaller 1 5/8" hole of its own (see pictures) which I happen to find odd. Wouldn't that restrict air flow?
Since the new inducer assembly didn't come with a back plate, I'm wondering if the back plate is actually necessary.  The inducer being replaced was new as of 2009 so I'm suspicious that perhaps this smaller hole somehow restricts airflow thus causing strain on the inducer motor which led to a failure after just 3 years.  On the flip side, perhaps the smaller hole is necessary because it plays some role in protecting the fan blades from being overexposed to high temperatures via the larger 3.5" draft hole.
Any help from an expert would be much appreciated.  I assume the plate is necessary but want to be sure I'm not going to need a new inducer again in 3 years.
The Inducer Assembly
!
The back plate and/or mounting bracket with 1 5/8" hole.
!
The inducer assembly with back plate in position (covering most of the open fan space)
!
Answer: While those answers may apply to some degree, I know the plate does create a restriction, which causes a slight vacuum in the blower section.  This vacuum is used on some furnaces to actuate the diaphragm pressure/vacuum safety switch.  If you have a diaphragm switch hose leading from the blower housing, then you'll find the furnace will not start gas flow unless that plate is in-place and creating the vacuum signal for the diaphragm safety switch.  Ask me how I know...  duh.
Comment: I replaced mine without the plate and it worked fine. I may, like the first post points out, be losing money but the draft is sufficient to open the vacuum safety switch and the burners fire up fine.
Answer: I'm not familiar with the whys and where to fors of furnace construction, yet I'm fairly sure the hole acts as a throttle to induce just the right amount of draft. If you omit it, there will be too much draft, which would inhibit heat transfer to the circulating air. In other words, it will cause money to disappear up the flue. While the blower should have lasted longer, this type of blower is very tolerant of restricted air supply, and the reduced inlet would have little impact on it's expected lifespan. You should reinstall things just as you found them.


How to use browser caching effectively for images?

Question: Which combination of http headers can achieve the following?
I would like static content like images to be cached for as long as possible, but if those images change on the server, I would like the browsers to update them.
Comment: The same as always: a combination of the *maximum* cache duration desired and ensuring the server returns a content-not-modified. A smart server might also use [ETags](http://en.wikipedia.org/wiki/HTTP_ETag) which would allow for more control beyond the last-modified-date.
Answer: A very neat caching approach of static content is to encode version of your static content inside URL. Then "notifiying" browser to download new static content is just a matter of supplying new URLs to the client.
<code>
# first version access (1.1/main.css will never expire)
# e.g. by far future Cache-Control + Expires Header
<link ... href="/static/1.1/main.css

# after upgrading/releasing trigger fresh download
<link ... href="/static/1.2/main.css
</code>

It involves some effort to your build+deploy process but from caching control it is the very simple.
Comment: yes there is only either/or. You could combine both Conditional-GET (e.g. ETag) and Cache-Control: max-age, but the core problem is how to find out the "right" max-age value. It is hard to know content is updated (like unplanned bug-fix rollout). Therefore in my view the caching approach with changing URL for new content is optimal (reduced server calls + having the referred static content under server's control).
Comment: So if I have an expires or cahce-control header with say a yeat expiry, there is no way to tell the browser to check if an item has been updated?
Comment: Basically 2 cache-style: 1) Conditional-GET (browser is doing expclitly a call to check new content, e.g. by ETag or If-Modified-Since, resulting in 304 Not-Modified or 200 OK updated content) . 2) Telling browser to not do a call at all (e.g. Cache-Control: max-age, Expires header). 2) has the advantage that you don't cause traffic at all, but has the disadvantage that update-forcing won't be possible. So I would use style 2) only if I am sure that content (identified by URL) won't ever get updated (see my initial answer).
Comment: so there is no combination of headers that will cache files for a long time, but still do a check for updated content. It's either one or the other?


Regex to select and replace as title case value

Question: I'm new to this regex part. My functionality is to replace every first character in a word as uppercase and remaining part in lowercase. I can easily loop through an change. But it might be costlier. To simplify it i'm trying Regex. I changed all the first character in a word to uppercase, But stuck on changing the remaining part to lowercase. How can i achieve. Refer the code below

var txt= document.getElementById('txt');
var result = document.getElementById('result');
result.onclick = titleCases;
function titleCases(){
       txt.innerText = txt.innerText.replace(/\b[a-z]/g,function(f){return f.toUpperCase();})
}

    replace The tExt

<button id="result">
  Result
</button>
Comment: Use `.replace(/\b([a-z])(\w+)/g,function($0,$1,$2){return $1.toUpperCase()+$2.toLowerCase();})`
Answer: It's always a good idea to break a problem down as much as possible.
In this case, you want everything lowercase except the first letters, right?
So convert the whole thing to lowercase, then do your "first letter to uppercase" thing.
txt.innerText.toLowerCase().replace(...);

Comment: Works like a charm. Another question. How can i replace every 1st letter in a sentence in capital case. Ex
 **F**irst letter after special character must be. **C**apital
Comment: That's trickier, but probably something like `.replace(/(^|[\W\S]\s+)(\w)/g,function(_,prefix,letter) {return prefix+letter.toUpperCase();});`
Comment: It doesn't work dude. See the bolded letters in my previous comment. Only that should be capitalized


"float: left" makes the  item ignore the  tag it is wrapped in, unless the re is a certain height in pixels

Question: JSFIDDLE
It may be a trivial question, but I can't seem to find the answer. In the case you can see in JS Fiddle, when the
 in which the
's are wrapped doesn't have a certain height specified, it ignores the div in general and moves the bottom
 behind everything.
Can anyone explain why?
Here is the code:

One

Two

Three

Four

Five

Six

Seven

Eight

Nine

-----Something-----

And the CSS
        .divWidgetList {
        width: 740px;
        height: auto;
        clear: left;
        float: left;
    }
    .WidgetList,.WidgetList li{
        color: #373737;
        min-width: 240px;
        display: block;
        float:left;
        font-family:'segoe ui', 'century gothic', arial, sans-serif;
        font-size: 22px;
        font-style: normal;
        font-variant: normal;
        font-weight: lighter;
        height: auto;
        width: 33.33%;
        line-height: 40px;
        list-style-image: none;
        list-style-position: outside;
        list-style-type: none;
        margin: 0px 0px 5px 0px;
        padding: 0px 0px 0px 0px;
        cursor: pointer;
    }
    .WidgetList li {
        padding-left:35px;
    }
    .WidgetList li a:link
    {
        color: #373737;
        text-decoration: none;
    }
    .WidgetList li a:visited
    {
        text-decoration: none;
        color: #373737;
    }
    .WidgetList li a:hover
    {
        color: #f07c01;
    }

Comment: you can use the [clearfix](http://www.webtoolkit.info/css-clearfix.html) solution
Comment: @MyHeadHurts It seems you are right... Just didn't find it before... :(
Comment: Floated elements have no layout size, so the parent doesn't get any size either since it fits itself to its contents.
Comment: Is the explanation really that simple.... Please post is as an answer so I can "approve it"...
Comment: possible duplicate of [How do you keep parents of floated elements from collapsing?](http://stackoverflow.com/questions/218760/how-do-you-keep-parents-of-floated-elements-from-collapsing)
Answer: When you apply float to an element it is no longer part of the regular flow layout. As such the parent, which by default sizes to fit to its children, doesn't have any height at all when all its children are floating.
Possible solutions:

Give the parent container a fixed height to accomodate its children.
Use display:inline-block to position the elements next to eachother instead of float.
Use an element with a clear rule to break to below all floating elements.
The 'float everything' method - a floating element will resize to its floating children. This is generally a bad solution since you'll end up floating everything as the name implies.


Get the function from Taylor series on maple

Question: Can I get the reverse process for determining the function if I have its Taylor series, with maple?
Thank you.
Answer: If you know the taylor series, plugging it in directly (as a series) will usually evaluate it if Maple can simplify it. For example:
If it doesn't evaluate it, then Maple can't do it in its current form :(
Answer: If you are lucky and as Pauly says Maple recognises the series, then yes.
If Maple does not recognise it, then you have to do a manual search: Extract each coefficient $a_n$ and search backwards, knowing that if there was such an $f$ which was expanded around $z=z_0$, it would have to satisfy for some domain with $x_0\in\mathfrak{D}$:
$$f(z)=\sum\limits_{n=0}^\infty a_n\cdot (z-z_0)^n$$
therefore by Taylor's Theorem:
$$a_n=\frac{f^{(n)}(z_0)}{n!}$$
At this stage you can try a couple of $f$'s manually and see if you get agreement, using Maple's "series"  command.
Note that even the above search is not guaranteed to end successfully. There are many[*] analytic functions which have no closed form and can be expanded into Taylor series around a given point $z=z_0$, so you may stumble into one where although the $a_n$ are well-known, there is no closed form for it, so the process resembles a little like looking for a pin in a haystack.
[*]: It would be a fairly simple exercise to show that the number of functions which admit a Taylor series (aka analytic at $z_0$) and do not have a known closed form far exceeds the number of known analytic functions (those admitting a Taylor series at $z=z_0$).


Need to charge after 14 days using stripe

Question: When I enter my credit it will be billed for $6.95. This is for shipping and handling. The shipping and handling charge gets billed once the card credentials have been entered.
I need to know 14 days later the card will get billed $39.99 for the product.
I need to get an email telling me the card needs to be billed.
Will the credit card automatically be billed in 14 days?
Answer: There is more than one way to skin this cat.
I would approach this problem in Stripe with the following process:

create a customer object and charge the customer immediately for the $6.95. https://stripe.com/docs/tutorials/charges#saving-credit-card-details-for-later
subscribe the customer to a plan that has a 14 day free trial period. https://stripe.com/docs/tutorials/subscriptions
Listen with a webhook event handler to cancel the subscription after the first $39.99 charge has been processed. https://stripe.com/docs/webhooks The webhook event you would want to respond to after the $39.99 payment is processed would be invoice.payment_succeeded. https://stripe.com/docs/api#event_types

This solution would be completely transparent to the user, and completely automated for you. You can use webhooks to automatically send out payment receipts (or any other type of communication), clean up data, or anything else you can imagine. It's a very powerful feature.
Comment: I know this is a very old answer. But can this be solved using Payments API now? https://stripe.com/docs/payments/save-during-payment


Rails/Ruby 1.9: Is there a better way to put Unicode in source files than sticking # encoding at the top of every file

Question: I just upgraded to Rails 3 and Ruby 1.9. All of my source files that used Unicode inside of them (such as emdashes) caused problems until I found out that you now need to include the following magic comment on top of each source file:
# encoding: utf-8

Is there a better way to do this? It'd be nice if it just automatically treated every source file as utf-8 like Rails 2.3/Ruby 1.8 did, and I don't see any apparent disadvantage from doing so.
In my application.rb I already have the following, but I can't tell that it does anything:
config.encoding = "utf-8"

Comment: Sorry for using this space for a rant, but I find it absolutely amazing that they chose ASCII as the default charset. I mean, I get that if you're Japanese UTF-8 isn't as awesome as it seems to us westerners, but ASCII was just about the worst decision they could have made.
Comment: I'm trying the `encoding` setting in the `.rb` file, but it doesn't work for me (same error Invalid char ...) on Windows. What OS are you on and is the encoding statement working for you ?
Comment: @Zabba I'm on Ubuntu and the encoding statement is working for me. You're pasting it in exactly as I typed it with the comment # right?
Comment: nothing working for me, typed what you wrote exactly. Also see if this post helps you: http://stackoverflow.com/questions/3484071/unicode-characters-in-a-ruby-script/3484152#3484152
Answer: i found only rake check_encoding_headers it add the magic comment to all files.
Comment: Nice find. Here's the URL for that rake task: http://snaprails.tumblr.com/post/556378216/porting-an-application-to-ruby-1-9. Remember to backup everything first!!
Answer: There is also a gem for adding encodings to all headers.


Does use of echo base_url(); to call CSS, images and Javascript files make a website slow?

Question: I am using Codeigniter. I am keeping my images,CSS and Javascript files in a folder called "support" in the document root of my application. So my document root folder looks like this-
.settings
application
support
system
.buildpath
.project
 index
.htacces

Now my question is will it make my website take time to load as I have to use  <? echo base_url();?>support/ every time I need to get something from my support folder? Because you see when I am using <? echo base_url();?>I am actually calling the full website address.. and I have 7 CSS and 13 javascript files to call from "support" so it will definitely take time to load the website. (Please correct me if I am wrong). If you think by this a website can get slow could you please tell me where exactly should I put my CSS,images and javascript files in. I heard views is not a good place for this.
Thanks in Advance :)
Answer: This question is probably bigger than you think.
First of all, using <? echo base_url();?> instead of "hard-coding" your web address will not slow down your site. A function call like this is very negligible to the speed of loading your pages.
I think the other part of your question is regarding architecture.
When you think of speed for your website, you need to know what factors slow down the loading of your page. (Although not an exhaustive list, this will help in your case):

the number of files (images, css, javascript, etc.) that need to load for your page
the cache-ability of those files
some server side header nonsense (e-tags and so-forth)
the processing to build your php pages
the size of your page

Now, in your instance, I would recommend putting all of your "static" files in the document root under a folder (say static). Then, access them all in your "views" with the base_url() function.
This way, your page as it's delivered to the browser, will make external calls for those static files - allowing the browser to cache all of those files (assuming the headers are set up correctly).  If you put them into views, then they're actually added to the page that is being requested. So, the next page that is requested has to download those files again along with that second page being requested. Make sense?
To help with the "number of files", you can always concatenate and minify any css/javascript that you have. So instead of the browser downloading and caching 8 js files, you can serve it 1 js file with all of your code.


TCP C++ client to receive a byte array from a c# server

Question: I have a C++ client waiting for an answer from a C# server using winsock2.
The C# server sends the answer as a Byte Array.
The C++ client receives it with negative values.
I tried using an unsigned char array in C++ but winsock doesn't support it in the recv() function.
What am I missing?
C# side:
private void Send(byte[] arrayToSend, NetworkStream stream)
{
if (stream.CanWrite) { stream.Write(arrayToSend, 0, arrayToSend.Length); stream.Flush(); }
else { ... }
}

C++ Side:
TCPConnection::Receive(int timeout, char recv[BUFFER_LENGTH])
{
int iResult = recv(m_socket, recv, BUFFER_LENGTH, 0);
if (iResult <= 0) { ... }
recv[iResult] = '\0';

//FOR DEBUG
std::string s(recv, iResult);
}

Comment: When writing communicating programs, you should always **look** what actually transits between them, because if you don't, you can never be sure what programm messed up. Install Wireshark or tcpdump and listen to the communication.
Comment: I saw, the dump from the send is different from the receive end. I'm pretty sure its a small convertion thing in the C++ side that I'm missing
Comment: [mcve] is needed.
Comment: @JonathonReinhart - Added the code, sorry - forgot about :)
Comment: What do you mean by `winsock doesn't support it in the recv() function` - isn't the buffer argument for `recv()` declared as a `void*`?
Comment: Okay, well you're sending a byte array on the C# sode, but it appears you're trying to interpret it as a string on the C++ side. You have the data in a buffer, but you're calling the string constructor, which is probably where you're going wring. Before writing code, you should define your protocol  (on paper). Lay out the fields, their data types, and endianness. Then you can go about implementing it in whatever language. Because we don't kow what you're sending, we can't really tell you how to dissect it.
Comment: Concerning C/C++: `char` is either signed or unsigned. Per standard this is implementation defined by compiler. (If a specific signed-ness is required use `signed char` or `unsigned char`.) In your case, I bet `char` is signed. So, everything >= 0x80 appears as negative number.
Comment: *"The C++ client receives it with negative values"* - what does that mean? The return from `recv()` is negative? Or what?
Comment: You need to show the code sending the data (we need to see the data you send) and the code interpreting the data - why do you think it is "negative"?
Comment: When you read elements from `char recv[]`, just convert them to `unsigned char` and they will appear positive. E.g. `int value0 = (unsigned char)recv[0];` (in opposition to `int value0 = recv[0];` where sign bit extension happens so that `value0` stores the same negative value as `recv[0]` does.)
Comment: Btw. in you code sample, the argument `recv` has the same name than the function `recv()` you intend to use. I wonder if this compiles without error. (`int iResult = ::recv(m_socket, recv, BUFFER_LENGTH, 0);` has probably better chances but I recommend simply to rename argument `recv`.)
Comment: Thanks everyone, I'll try with the unsigned comment, I think it's just that. I'm sorry about the vagueness of the question, it just that simple thing that doesn't work, the structure, layout and surroundings work OK.
Comment: Btw. if provided address for argument `recv` has really only `BUFFER_LENGTH` elements then `recv[iResult] = '\0'` is out of range access if the ful amount of `BUFFER_LENGTH` bytes has been received. (For this, you need storage for at least `BUFFER_LENGTH + 1` elements.)
Answer: Well, after some years of working with C# and Python I forgot about the signed and unsigned char*. That was the problem.. Thanks for helping!


Moderator Tools and Flag Disagreement

Question: I was browsing the flags in the 10k tools this morning and noticed the "Flag or disagree" button on flags.
Diamond mods: on this site, do these help you or do they just add more flags to deal with?  I am especially talking about the autogenerated ones.
Answer: Whenever you disagree with the existing flag, or you flag the same post with a different flag, you are not adding more work for the moderators; you are helping the moderators.
That is particularly true with the automatic flags generated by the system.
A low quality flag generated automatically from the system doesn't say much; if to that you add a "not an answer" flag, the moderators know it is not an answer. An automatic flag generated because a user posted two answers that are the same doesn't say much, but a vote to close one of the questions because it is a duplicate, says more.
In short, automatic flags are a reason to check the flagged post; flagging a post that has an automatic flag, you are saying you checked the post, and found something that needs moderator's attention.

Moderator Tools and Flag Disagreement


Dual boot on SSD - partitioning

Question: I'm going to help a friend to build a new system with only a 500 Gb SSD and 8 Gb RAM and we want to install Windows 7 and Ubuntu LTS. The usage will be 95% Ubuntu and 5% Windows and the system will remain turned on for maaaany hours for website (not server) purposes. So please help me in these:

Is there a need to create a swap partition, as the memory is enough? If yes - how much?
Should I create manually a /boot partition?
Should I go for manually partitioning or with auto istallation alongside windows?

Please be free to suggest another solution or a new partitioning scheme.
Thanks in advance
Comment: If a new system, you may want to use UEFI, not the 35 year old BIOS/MBR. The Windows 7 DVD is BIOS only, but can be copied to a flash drive and some files moved around to make it UEFI bootable. How you boot install media, both Ubuntu & Windows is then how it installs. But you want both systems UEFI boot or both BIOS boot.
Comment: @oldfred -- FYI, 64-bit only images of Win7 were EFI compatible
Answer: If you plan to hibernate you need 10-12GB swap. If not, 2-4GB swap should be sufficient unless you do really heavy computing.
A separate /boot partition is unnecessary. Many users that use a separate /boot partition run into problems later as they make it way too small and run out of space after a couple of kernel updates.
Create an 80-100GB partition for the Windows install. If you want a shared data partition also make that and format it ntfs. Leave the rest of the drive unallocated. When installing Ubuntu choose "use unallocated space" and let the installer partition it.
Since you are using Windows 7, I assume you are installing in Legacy/CSM mode. In Legacy mode, it is very important to install Windows first then Ubuntu for the following reasons:
Windows does not see non-windows OSs so the bootloader will not auto configure to boot linux. And manual configuration is very difficult.
Both systems will write their bootloader to the MBR. So we want Ubuntu's bootloader (grub) in the MBR when we are finished, because grub can see and boot Windows.

Dual boot on SSD - partitioning


How to make [DebuggerNonUserCode] hide an exception from the debugger in simple test case?

Question: Setup:
1) MSVS 2015, Option -> Debugger -> "Just My Code" is checked.
2) This sample code placed within some class and called during startup:
<code>    static bool TestIgnoreException()
    {
        Exception ex;
        return TrySomething(out ex);
    }

    [DebuggerNonUserCode] // Prevent exceptions from stopping the debugger within this method.
    static bool TrySomething(out Exception exOut)
    {
        try
        {
            if (Environment. MachineName.Length != -1)
                throw new Exception("ThrewIt.");

            exOut = null;
            return true;
        }
        catch (Exception ex)
        {
            exOut = ex;
            return false;
        }
    }
</code>
3) Launch Debugger
Expected result is that TestIgnoreException() runs silently and returns false.
Actual result is the debugger stops in TestIgnoreException() even though there should be no exception being processed at that scope.

4) Also re-tried using [DebuggerHidden] instead, same results.
Motivation:
The motivation is for cases where some API that is out of your control does not provide a "Try" method and instead only indicates failure by using exceptions.
One of numerous such examples is .NET TcpClient.Connect(host, port).  Say a program always tests some connections during startup, the debugger should not stop on this particular section of code each time.
Using the standard "break when thrown" exceptions checkboxes is is not good because it works globally by type.  It cannot be configured to work locally.  Also other developers who check out the code should automatically skip the exception as well.
Comment: Then it would stop on every exception of that type which is not desirable.  Also, another motivation is that other developers who check out the code should not witness the exception (just like what happens in lower level net code raises).  Also, something is broken here it seems with the [Debug*] attributes it seems, no?
Comment: No, you have that backwards.  Learn how the Debug > Windows > Exception Settings dialog works, press F1 to get started.  Your assumption that attributes lets you change this behavior is just wrong.
Comment: Just untick the "Break when this exception type is thrown" checkbox.  Problem solved, no need for any of this either.
Comment: Please respond to the 2 points in my comment.  That feature does not meet either.  Also the [Debug*] attribute has let me change the behavior to a degree (exception not stopped within decorated method) so your second sentence is false.
Comment: You are getting a bit too agitated.  Your beef is with Microsoft, not with me, I'm just telling you how it works.  Use their uservoice site to provide feedback.
Comment: I'm like 0 for 5 on msconnect.com.  And I'm 50/50 here.  Thank you for at least implying that this problem could be real.  I may post this to msconnect anyway after gathering some info/workaround.
Comment: You have it set to break on every exception.  The attributes won't do anything to stop the debugger when you have it configured this way.
Answer: Mystery solved.  It is indeed a known issue that is new to MSVS 2015 because of added exception handling optimizations.
https://blogs.msdn.microsoft.com/visualstudioalm/2016/02/12/using-the-debuggernonusercode-attribute-in-visual-studio-2015/#
There is a workaround posted on that link to disable the optimizations and enable the old behavior.  Hopefully they will eventually be able to revive the support for this including the optimizations.
<code>reg add HKCU\Software\Microsoft\VisualStudio\14.0_Config\Debugger\Engine /v AlwaysEnableExceptionCallbacksOutsideMyCode /t REG_DWORD /d 1
</code>
Related question:
Don't stop debugger at THAT exception when it's thrown and caught
Comment: Any idea how to fix this in VS2017? The registry doesn't seem to work anymore, since it's, well, for VS2015. The suggestion from @Koray unfortunately did not work for me either.
Comment: Glad to know that you have resolved this issue, please mark it as the answer:)
Comment: This workaround did not resolved it in my tries. It just make the bad debuging performance worser! When the "*Use managed compability mode*" option is enabled this issue seems to be resolved. However it disables the ability of **edit and continue** while debugging. So it is useless for my style of coding. http://stackoverflow.com/questions/41964808/visual-studio-2015-debugger-stops-for-a-handled-exception-of-a-debuggerhidden-fu?noredirect=1#comment72490364_41964808
Comment: What a shame.  I did notice In 2017, when an exception occurs, an option was added to ignore that type of exception only _within_ the assembly it occurred in.  That nice but still doesn't allow developers to share the ignores via code check in and its not method specific.


Is it true that in .NET the garbage collector gets called when you minimize the program?

Question: I had created a program in C#. That program used about 60-70 MB of my memory.
But when I minimized that program, it required less memory, that is, just 10 MB.
When I maximized or came back to that program it used 20 MB...
Why does this happen?
Is it true that garbage collector gets called when you minimize the progam?
Answer: This has nothing to do with garbage collection - this happens with non .NET programs too (try minimizing your browser while looking at the memory footprint for it).
The moment you minimize a program, the windows OS will not longer need to keep the UI components in memory, hence the memory requirements are lower.
That is - windows trims the working set when an application is minimized.
See this channel9 thread and this KB article (thanks @Sasha Goldshtein).
Comment: @Anirudha - Any particular reason you have unaccepted this answer?
Comment: @Oded check out the link given in my answer..i think its just what i wanted to know.But ur answer is also closer to it....
Comment: @Oded check out the ".net urbun legand" part of the article...that gives useful info....
Comment: @Anirudha - I did. It essentially expands on the answer I had given.
Answer: The garbage collector runs whenever it decides to run; this is not necessarily tied to anything the user does, and certainly not minimizing the application. Generally, you can think of it as a function of the amount of memory usage compared to the total amount of free memory. But the point is, this is supposed to be somewhat opaque to you as a programmer. The big benefit of garbage collection over manually memory management is that you shouldn't have to worry about any of this.
I suspect from your question that you're using the Windows Task Manager to monitor your application's memory usage and determine when garbage collection occurs. This is a huge mistake. If you really need to do memory profiling, you need to invest in a proper profiler. Task Manager is not designed for this, and you'll often get false reads.
More specifically, the reason why you appear to see a marked decrease in the amount of memory consumed by your application when you minimize it is just one of the false reads you'll get when trying to do memory profiling with Task Manager. What's actually going on is that whenever you minimize an application, the Windows kernel automatically pages out a large portion of the memory it was using. You'll see this for all of your applications, not just those written in .NET. Because Task Manager is showing you the subset of the total memory in use by the application that exists in real memory at the time (i.e., the amount that hasn't been paged out to disk), it looks like the memory usage has decreased when it hasn't really. To get a slightly more accurate read, you should be looking at the process's "Private Bytes" value. This knowledge base article provides further details.
Answer: This article explains all:THE MEMORY MYSTERY
this is an extract from the above site:
.Net Urban Legend

It is possible to reduce the working set size of a .Net Windows Forms
application by minimizing and then maximizing the application
immediately after it loads. Windows OS trims the working set of
applications when they are minimized. The memory that was briefly used
while loading all those assemblies mentioned earlier is trimmed by the
process of minimizing and maximizing the application.   You can
demonstrate this behavior to yourself by creating and running a
Windows Forms application with just Form1 and no added code.

Create and run the simple application.

Open the Windows Task Manager and then its Processes Tab.  You will see the Task Manager shows your application's memory usage at approx.
12.5 MB.

Now minimize your application and then maximize it.  Check the Task Manager again.  You will see the Task Manager now shows your
application's memory usage at approx. 1.5 MB. The memory that was used
to load assemblies when your application was launched was reclaimed by
memory management when you minimized the application.

Did you improve memory management or application performance by minimizing and
maximizing your application?  No.   You can find some .Net Windows
programmers who add code to minimize and then maximize their programs
thinking that they are optimizing memory. As these programmers have
shared this technique with others a sort of .Net Urban Legend has been
born – a programming practice based on fiction not fact.   This
practice is unnecessary because when OS needs the unused memory left
behind when the assemblies were loaded it will reclaim it
automatically. In fact, decreasing the size of your application's
working set when memory is plentiful may decrease performance.
Answer: Instead of looking at the task manager you should use the PerfMon tool to see the memory consumption of your application. You will see the real memory checking this counter:
Process->VirtualBytes: Virtual Bytes is the current size, in bytes, of the virtual address space the process is using. Use of virtual address space does not necessarily imply corresponding use of either disk or main memory pages. Virtual space is finite, and the process can limit its ability to load libraries.
Comment: IMHO downvoting should force to say why
Comment: I repeat the same. If you downvote say why or refrain from downvoting.
Answer: Garbage collection in .NET occurs when all your objects are dead or when you close the program. You can also call the garbage collector in .NET, but it's not generally practiced. And while in minimized mode the program, though working (or alive or whatever you can call), is not in focus or actively used by the user. So the memory use (system resources) that has been alloted to the program reduces so the other programs may use the system resources.
Answer: How are you determining the amount of memory your program uses?
I believe Windows Forms is known for calling SetProcessWorkingSetSize to evict as much pages from RAM as possible.


Nginx and directory list doesn't work:

Question: I have done just as in the tutorials but is simply not working:
<code>server {
    location /abc {
        autoindex on;
        root          /home/user/abc/;
    }
}
</code>
It gives me 403 Forbidden, what I'm doing wrong?
thanks; I've tried to use a "/" at the end, and also without but no changes.
When I go to myip/abc I hope to see some directory list but no. Even if I try to go to myip/abc/myfile.txt it doesnt work.
Answer: The problem is that by using the root instead of alias it will look in the wrong path. Also in the http the user was www-something and has had no permissions, thanks ubuntu, so now I had to change to my name and it worked:

Alias instead of root for the right path
Define the right user that has permissions to the folder you want

Nginx and directory list doesn't work:


Slanted edge MTF evaluation with ImageJ plugin

Question: I am trying to evaluate an optical system by calculating the MTF with the slanted edge method. For this I use the following ImageJ plugin:
https://imagej.nih.gov/ij/plugins/se-mtf/index.html
No I want to calculate the MTF with the frequency units "lp/mm". For this I have to insert the "Sensor size (mm)" and the "Number of photodetectors". Sadly I cannot find any description and what these values are exactly. If I use the diagonal of the sensor in mm and the number of pixels my sensor has as the second value, I get nonsense values (very high frequencies, higher than 100000 lp/mm).
Does anyone have experience with this tool and can give me a hint on what values I need here?
Thanks a lot in advance!
Answer: I am also not 100% sure but I guess its the sensor width and the number of pixels along the sensor width
Comment: If you are not sure don't post an answer
Comment: As it's currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer).
Answer: The 2 input values are just there to fix the scale, even it could have been reduced to 1 = xx µm/mm.
So, "Sensor size (mm)" = whatever size (mm) in the image considered, just choose it coherent with the real size of the image (just for logic).
Then, "Number of photodetectors" = the number (qty) of Voxels corresponding to this "whatever size (mm)" input above.
Then ImageJ is having the scale into the image made of Voxel.
Last but not least, 2 things : (1) do not forget in your ROI selection (square) that void shall be on the Left Hand side ; (2) The more accurate result is obtained when material wall is vertical on your image (otherwise, when bended, you will have bias vs.vertical wall.
Answer: The creator's answer to a similar question:
Hello everyone and thanks to the creators of the plug in.
I have to analyze the mammography image and I cannot run the option lp/mm, using (SE_MTF_2xNyquist)
The sensor, flat panel, has a size of 236.4 mm (vertical) x 296.4 mm (horizontal)
The number of output brushes 4728 (vertical) x 5928 (horizontal) = 28 027 584 brushes
14 bits of output.
Resolution in brushes 50 x 50 μm / pix.
What size value of the censor I put
ANSWER: All do you need to note is one sensor side size (mm) and the number of corresponding pixels (pix) in this given side. The plug-in will calculate the resolution in lp/mm as the frequency unit in the MTF plot. Some sensors have different resolution for vertical or horizontal directions. You can always calculate the MTF of each direction using the corresponding sensor size (mm) and number of pixels (pix) on this given side. The Nyquist frequency corresponds to 0.5 cycles/pix. Beyond this frequency you can observe the amount of aliasing present. Hope this help.


AWS EKS EFS-CSI Driver version and how to upgrade it

Question: With lots of known issues[1][2] in EFS-CSI driver, i'm planning to do this driver upgrade on a running cluster.
I read most of AWS documentations but couldn't get any straight forward answers for following questions.

How to view current efs-csi driver version in EKS cluster.
How to upgrade the efs-csi driver to a specific version in a running cluster.

[1] https://github.com/kubernetes-sigs/aws-efs-csi-driver/issues/616
[2] https://github.com/kubernetes-sigs/aws-efs-csi-driver/issues/673
Answer: If you already have installed efs-csi drivers and used Helm to install efs-csi drivers, upgrade is so straightforward. Regarding to the documentation:
<code>helm upgrade -i aws-efs-csi-driver aws-efs-csi-driver/aws-efs-csi-driver \
    --namespace kube-system \
    --set image.repository=602401143452.dkr.ecr.region-code.amazonaws.com/eks/aws-efs-csi-driver \
    --set controller.serviceAccount.create=false \
    --set controller.serviceAccount.name=efs-csi-controller-sa
</code>
Regards


What is the difference working in $L^2$ and $C^2 $ for Fourier series expansion of functions?

Question: Once one of my lecturer said that if we worked in $L_2$, the complex inner product $$(u,v)=\displaystyle\int^L_0 u\ \bar v\ dx $$ works flawless for Fourier series, instead of working in $C^2$.
Question: I do't see the general picture that what is the problem with working in $C^2$. I am taking differential equations under Mathematical Methods in Physics I (in my Physics department), so there is almost no rigor in our lectures, I want to learn all the underlying idea Fourier series and this mentioned idea between $C^2$ and $L^2$. Any idea, comment, source, guidance, will be appreciated.
Comment: Also I'm slightly confused on your question.  I could be wrong but $\mathbb{C}^2$  is not the usual domain of fourier series for physicist.  Are you sure you don't mean $C^2$, the set of twice continuously differentiable functions?
Comment: One book I liked a lot for learning a bit more rigor behind all of the stuff I learned in Math methods was "Div, Grad, Curl, and all That"
Comment: [link](https://www.amazon.com/Div-Grad-Curl-All-That/dp/0393925161)
Comment: https://math.stackexchange.com/questions/2621446/is-the-fourier-series-always-the-best-approximation
Comment: I meant C^2 and wrote but someone changed editing
Comment: It is not clear what your lecturer meant. There is an isometry (may need scaling depending on your definition) between $L^2$ and $l_2$, which may be what she was referring to, but you give up (mostly, cf. Carleson's theorem) pointwise convergence niceties. Presumably in context you need pointwise convergence, so you need extra conditions on functions so the Fourier series converges in an appropriate way.
Comment: Another possibility is that it was mentioned that it is relatively easy to show that periodic $C^2$ functions have pointwise convergent Fourier series' but that, as mentioned, the isometry via standard hermitian products of periodic functions and sequences of Fourier coefficients extends to $L^2$ vs. $\ell_2$
Answer: Your lecturer probably meant that the vector space $C^2([0,L])$, consisting of functions that are twice differentiable with continuous derivatives, is not complete if equipped with the scalar product
$$
\langle f| g\rangle= \int_0^L f(x)\overline{g(x)}\, dx.$$
Among the various unpleasant consequences of this, there's the Fourier series fact that if you take an arbitrary series
$$
S(x)=\sum_{k=-\infty}^\infty c_k e^{i\frac{2\pi}{L}kx} $$
then, even if $\sum_{k=-\infty}^\infty |c_k|^2<\infty $ (corresponding to the physical fact that $S$ has finite energy), you cannot conclude that $S\in C^2$. (In fact, it is not trivial to find conditions on $c_k$ that ensure $S\in C^2$. I don't think that a necessary and sufficient condition is known.)
On the other hand, the vector space $L^2([0, L])$, consisting of all measurable functions $f$ such that $\int_0^L |f(x)|^2\, dx<\infty$, is complete if equipped with the aforementioned scalar product $\langle\cdot|\cdot\rangle$. As a consequence of this, the finite energy Fourier series $S(x)$ always defines an element of $L^2([0, L])$. (Actually, all elements of $L^2([0, L])$ can be decomposed in a finite energy Fourier series.)
A good reference to be introduced to these things is the book by Stein and Shakarchi "Fourier analysis: an introduction", first volume.
Comment: You might mention explicitly that otoh it does follow that $S\in L^2$.


Python List Del & Reconciliation

Question: I'm trying to delete multiple elements from a list.
The elements are having their indexes evaluated dynamically, and stored in a list called "dstry". It may look something like this:
arr = [1, 2, 2, 3, 5, 6, 7, 8, 9, 2]
dstry = [1, 2, 9]

However, if I do...
for i in dstry:
    del arr[i]

The list begins to shrink, and dstry's target elements are no longer where they should be.
Logically, how can I get around this? It'd be nice if the del could resolve all at once and delay the collapse of the array. Should I instead replace the elements with a flag value and then del them wherever they appear later so that index is not a factor?
Thanks
Comment: What is `arr.del`? lists have no `del` method. Do you mean `del arr[i]` ?
Comment: use arr.remove() instead of del()
Comment: Use comprehension: `[a for i, a in enumerate(arr) if i not in dstry]`.
Comment: Reverse `destry` while you loop over it, that way the indices remain faithful: `for i in reversed(dstry): del arr[i]`. That being said, doing it like this is computationally inefficient. The answer in the linked duplicated has a number of superior methods.
Answer: A bit creative solution, but this should fix the problem with the index you had.
arr = [1, 2, 2, 3, 5, 6, 7, 8, 9, 2]
dstry = [1, 2, 9]

x=len(arr)

for i in dstry:
    del arr[-x+i]

print(arr)

Comment: Thanks for the response. However, remove doesn't serve the proper purpose here as remove is based on the value in the list rather than the index.
Comment: Your current solution only works if `dstry` is sorted from small to large. Just reverse the list, then you don't have to adapt the indices.
Comment: Changed it to what you said.

Python List Del & Reconciliation


What is the correct material to sheath pex pipe under a slab?

Question: It's recommended in my area (due to rocky soil) to install a sheath or channel under the slab for the pour, then slide the PEX into the sheathes afterwards during the rough. This also allows the PEX to be replaced later on.
What type of material should be used for this sleeve? Is it compatible with insulation if I wanted to insulate the hot and cold lines running under the slab?
Additional information: This will be inspected work, under IPC 2012 and IRC 2012. We are in climate zone 4.
Comment: 3" or 4" ABS pipe....
Comment: Flexible ABS? I thought the stuff they used was like a semi-stiff tubing, similar to what people put at the end of rain gutters. It's not rigid pipe, but not as pliable as a garden hose either. Similar to the flexibility of PEX.
Comment: @JimStewart That's what they do in Florida, but I think it depends on your soil type. Georgia clay with rocks vs Florida sand.
Comment: I have recently been observing the construction of a new house in my neighborhood. The PEX water pipes were just laid on the soil, then sand added with poly sheeting laid over that. The slab was then poured over that. Some heavy duty sheeting tape was wrapped around the PEX where it passed through the slab, and where it passed through what would be beams. I have wondered if some sort of conduit could be installed and then the PEX pushed and pulled in after the slab was poured. @Michael Karas can you give a link or reference to this method of installation?
Comment: Without conduit for the PEX if one ever has to replace the PEX one has to tunnel under the slab. I cannot understand why direct burial is allowed. This is very short sighted. PEX has a service life which is shorter than the structure as a whole.
Comment: @JimStewart - I have no links. I just suggested ABS pipe as an idea. I've never come near to using PEX but I can tell you that I would never bury it direct in soil then yet under a concrete slab. Flexible ABS laid down with gentle turns is a good idea. It can also be a good idea to install a pull rope through the ABS pipe and leave it available at the end access points. Obviously one needs to carefully plan runs so that replacement PEX can be easily pulled through. ONE OTHER THING TO KEEP IN MIND IS THAT THE FOLKS BUILDING THOSE NEW HOUSES ARE JUST INSTALLING ... NOT REPAIRING IN 25 YEARS.
Comment: I have overhead power lines, but across the alley from us is underground electric fed down the power pole on our lot under the paved alley and out to the next street--about 9600 V line, I think. Original cable was direct burial (installed 1972) and at the end of its service life. The one right behind us kept shorting out and I watched the drilling and pulling of conduit (~3" or 4" PVC?) through which a new cable was pulled/pushed. Something like that could be laid before slabs are poured for PEX water pipes. I am sure this would increase cost, but what an improvement in repairablility!
Answer: You'd install PEX without sheath inside the slab for underfloor heating only. Otherwise it should be sheathed to allow it to comfortably expand depending on temperature variations, it also makes it possible to change the pipe if it bursts due to freezing, or bad quality pipe for example. Sheathing also protects against UV light which is bad for PEX.
Why not simply purchase pre-sheathed pipe?... (link to my favorite shop areound here). It is a little bit more expensive than naked pipe, but it saves a lot of effort. You can get pretty much any type of pipe with the sheath already on, you can even get them pre-insulated.

I had (once) to shove about 10m of pipe through a sheath, and it was a lot of work, and we did it in the garden with the sheath held straight between two trees! The PEX pipe wants to curl back as it was when it was rolled, so it catches in the sheath corrugations, even with the end tapered and lubed it was a full body exercise...
If you want to do that... get a sheath that's a bit too large. For a 20mm pipe you'd get a sheath with 25mm ID, get 32mm ID instead, that will be a lot less work! And the larger ID allows you to stick a rounded end on the pipe (like a copper pipe endcap) which will make it slide a lot better...
Comment: This didn't answer what the sheathing material is though...
Comment: I looked it up, got polyethylene or polypropylene, or polyolefin (which would most likely mean PP or PE). ABS is too hard, it would crack when bending it.
Comment: So is it the same kind used with rain gutters then like this: https://www.homedepot.com/p/Advanced-Drainage-Systems-4-in-x-100-ft-Corex-Drain-Pipe-Solid-04510100/100537518? That's polyethylene pipe, and 4" drainage pipe is readily available and would allow 2 runs of 1" PEX with insulation to be pulled through pretty easily. I do need to make sure that is code complaint though.
Comment: Looks good. Also try google image search on "underground pex  conduit" and "underground electrical conduit"
Answer: I do not believe that you would need to insulate the PEX hot water lines if they were in a conduit, PEX itself is good insulation.


Why can't Golang download certain webpages?

Question: I want to download Fantasy Football Data to analyse in Go, but when I try to download from this api page then I get back an empty response, even though the code works for other websites, e.g. this api page
Minimal reproduction, outputs an empty array.
package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
    "time"
)

const AllPlayerData = "https://fantasy.premierleague.com/drf/bootstrap-static"

func main() {
    downloadAllData()
}

func downloadAllData() {
    client := &http.Client{
        Timeout: 20 * time.Second,
    }

    response, err := client.Get(AllPlayerData)
    if err != nil {
        fmt.Println("Unable to download player data.")
        return
    }

    body, err := ioutil.ReadAll(response.Body)
    if err != nil {
        fmt.Println("Failed to read response")
        return
    }

    defer response.Body.Close()

    fmt.Println(body)
}

The same webpage downloads fine in Python:
import requests
url = "https://fantasy.premierleague.com/drf/bootstrap-static"
r = requests.get(url)
print(r.content)

I don't think it's related to e.g. Ajax calls as viewing the network requests in Chrome doesn't show up any beyond the page load itself
Comment: Did you try logging the error message? `fmt.Println("error: %v", err)`
If there's no error, just an empty response, did you check the response headers? Maybe it's a redirect that python automatically follows, but the Go client doesn't.
Comment: There weren't any error messages, nor any useful response headers. The response had a status of 200, but Content length 0
Answer: They are doing some sort of validation on the user agent, the following code works:
package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
    "time"
)

const AllPlayerData = "https://fantasy.premierleague.com/drf/bootstrap-static"

func main() {
    downloadAllData()
}

func downloadAllData() {
    client := &http.Client{
        Timeout: 20 * time.Second,
    }

    request, err := http.NewRequest(http.MethodGet, AllPlayerData, nil)
    if err != nil {
        fmt.Println("Unable to create request.")
        return
    }
    request.Header.Add("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36")
    response, err := client.Do(request)
    if err != nil {
        fmt.Println("Unable to download player data.")
        return
    }

    body, err := ioutil.ReadAll(response.Body)
    if err != nil {
        fmt.Println("Failed to read response")
        return
    }

    defer response.Body.Close()

    fmt.Println(string(body))
}

Comment: Go actually has a user agent by default too, `const defaultUserAgent = "Go-http-client/1.1"`, I'd imagine PostMan either has one or uses your browsers.  It's hard to know *why* Go's default was blocked, but it's just a common issue with scraping - in general you want to emulate a real browser as much as possible
Comment: That's fixed it, thanks a lot Dave! I assume python applies headers by default, but do you know if that's also true of Postman?
Comment: That makes sense. Thanks a lot :)
Comment: Looks like they're checking for `Mozilla`. I sent a request with _only_ `Mozilla` as the user agent, and they sent the data.


Java setting cookies through stream

Question: I am writing a webserver, and I need to set a cookie on the client's web browser.
I get a OutputStream through Socket.getOutputStream(), but I cannot find a way to send a header with it. I tried sending javascript code to set the cookie on the client side, but the cookies set that way are not being sent back to the server.
How can I set a cookie through the header/other way.
Comment: Read the help, particularly http://stackoverflow.com/help/how-to-ask and http://stackoverflow.com/help/mcve, then come back and ask a question that can be answered.
Answer: You send the response header right before the body:
<code>PrintWriter writer = new PrintWriter(socket.getOutputStream());
writer.println("HTTP/1.0 200 Ok");
writer.println("Set-Cookie key=value Max-Age=8640");
writer.println("Content-Type text/html");
writer.println()//important
//send your body here
writer.flush();//send message
</code>
Here's a list of HTTP response header fields https://en.wikipedia.org/wiki/List_of_HTTP_header_fields

Java setting cookies through stream