Android Studio Waiting for Target Device to Come Online Android

Question: im trying to build an android application under linux mint ,i have updated android studio to 2.2.3 , but when i run the app a message appears:  Waiting for Target Device to Come Online , and then the emulator does not even start.
this my gradle:
apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion '25.0.0'
    defaultConfig {
        applicationId "bassem.com.vib"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.0.0'
    compile 'com.squareup.retrofit:retrofit:1.7.1'
    compile 'com.wang.avi:library:2.1.3'
    compile 'com.squareup.okhttp3:okhttp:3.8.0'
    compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
    compile 'com.google.code.gson:gson:2.6.1'
    compile 'com.squareup.picasso:picasso:2.5.0'
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:design:25.0.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.0-beta5'
    compile 'com.github.PhilJay:MPAndroidChart:v2.0.9'
    compile 'com.sdsmdg.tastytoast:tastytoast:0.1.1'
    compile 'com.itextpdf:itextg:5.5.9'
}

Comment: There are other operating systems than Ubuntu
Comment: try the solution proposed [here](https://stackoverflow.com/questions/42831999/android-studio-2-3-ubuntu-16-10-emulator-do-not-start)
Comment: @lelloman that assumes OP has ubuntu
Comment: @LunarWatcher no, that assumes that OP might have the same problem that occurs on ubuntu and being ubuntu a more popular distribution than mint the question was already asked and solved. the question I linked is about ubuntu 16.10 but I'm having that problem for years on many versions. it is worth a try in my opinion
Comment: it didnt work  when i lunched it shows the emulator Could not launch '../emulator/qemu/linux-x86_64/qemu-system-i386': Not a directory
Answer: Android emulators not booting is an issue that happens rather often. The ANdroid emulators (the official ones) are very bad. Having too little RAM in the emulator or not good enough processor is enough for the emulator to not boot.
As far as I see, you have three options (if you don't have a real device and want to use that):
1) Give the emulator more RAM
2) Find a different emulator (bluestacks, genymotion, etc)
3) Get a better computer/CPU
First one can be configured in emulator settings. Second is because other emulators (I have tried Genymotion so I can mention that) have much better performance. The default Android emulator is extremely bad. I have pumped 3 gigs of ram into it, and I have an i7 processor and it still isn't enough to get it to get even close to a real device. I can get it to start, but I have to allocate a large amount of resources. If your computer has low performance or is old, that could explain why you don't get it to boot. Your computer doesn't have the resources to boot.
I would like to add the fact that this has nothing to do with your app. This is a hardware and software issue (software being extremely demanding + limited hardware can cause the issue).
The third option is if you want to run the default emulator and #1 didn't work. If you have allocated a lot of ram and it still doesn't run, your computer doesn't have the processing power for it to boot. If you then pick option #2 and that doesn't work (very low chance, assuming you have at least 4 gigs of ram on your computer and i3 or better (or equivalent of a different CPU brand) it should work with different emulators) you have no other option but #3. Your computer simply cannot handle the emulators in addition to Android Studio (which is very demanding in system resources as well).
If you absolutely have to use an emulator, I recommend you use Genymotion. In my experience, it has high performance even using low system resources. While it still has some lag, it still works very close to a real device and not an emulator. (works like an emulator = slow, laggy, etc. Works like a real device = like you have an actual device (performance-wise)).

The default Android emulator is extremely slow. That is something I think most Android developers and users of the emulator agree on. Getting it to function at least normally and avoid extreme lag requires (in my experience) a massive amount of processing power and RAM.
The issue you are experiencing is not an issue with your app, Android Studio or the emulator. (Well, technically it is an issue with the emulator, but it has always been slow). Because the emulator doesn't get enough system resources, it doesn't boot. It does in the background, most likely (unless it gets so little resources it gets an out of memory exception and crashes), and will eventually boot. But depending on how much resources you gave it, it can take hours.
My recommendation to solve this is either to get a better emulator or use a real device. The second is to prefer, as real devices are better than emulators.
Comment: i've installed genymotion and it works now . thank you
Comment: It is possible to use the default emulator too, but is is a very demanding emulator. Kinda sucks as it is installed with Android STudio/SDK, takes up space when I don't use it


How to fix not authorized to perform: dynamodb:Scan error

Question: I get the following error when trying to use dynamodb scan:

"errorMessage": "An error occurred (AccessDeniedException) when
  calling the Scan operation: User:
  arn:aws:sts::747857903140:assumed-role/CodeStarWorker-helpbot-Lambda/awscodestar-helpbot-lambda-FindService-1L7IH17742JLR
  is not authorized to perform: dynamodb:Scan on resource:
  arn:aws:dynamodb:us-east-1:747857903140:table/HelpBot"

This is in my SAM template:
<code>FindService:
    Type: AWS::Serverless::Function
    Properties:
      Handler: find_service.handler
      Runtime: python3.6
      Role:
        Fn::ImportValue:
          !Join ['-', [!Ref 'ProjectId', !Ref 'AWS::Region', 'LambdaTrustRole']]
      Policies:
        - AmazonDynamoDBFullAccess
</code>
What can I do to fix this?
Answer: According to this SAM CloudFormation doc:

Policies - Names of AWS managed IAM policies or IAM policy documents or SAM Policy Templates that this function needs, which should be appended to the default role for this function. If the Role property is set, this property has no meaning.

You'll need to either define a role for the function that includes both groups of permissions and use that as the "Role", or add the LambdaTrustRole's permissions to "Policies".
The latter would look something like this:
<code>  Policies:
    Version: '2012-10-17'
    Statement:
    - Effect: Allow
      Action:
      - dynamodb:Scan
      Resource: arn:aws:dynamodb:region:account-id:table/table-name
    - Effect: Allow
      Action:
      - ...
      Resource: ...
</code>
Comment: Did you also remove the Role, since Policies won't take effect if Role is present?
Comment: It worked when I added the "AmazonDynamoDBFullAccess" permission from the IAM console.
Answer: You need add new permission to your invoker (such as lambda-function or ...) like this(for example I have two table):
<code>{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "dynamodb:BatchGetItem",
                "dynamodb:GetItem",
                "dynamodb:Query",
                "dynamodb:Scan",
                "dynamodb:BatchWriteItem",
                "dynamodb:PutItem",
                "dynamodb:UpdateItem"
           ],
            "Resource": "arn:aws:dynamodb:eu-west-1:77777:table/order"
            },
           {
            "Effect": "Allow",
            "Action": [
                "dynamodb:BatchGetItem",
                "dynamodb:GetItem",
                "dynamodb:Query",
                "dynamodb:Scan",
                "dynamodb:BatchWriteItem",
                "dynamodb:PutItem",
                "dynamodb:UpdateItem"
            ],
            "Resource": "arn:aws:dynamodb:eu-west-1:777:table/ExecutionId"
        },
        {
            "Effect": "Allow",
            "Action": "logs:CreateLogGroup",
            "Resource": "arn:aws:logs:eu-west-1:777777:*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogStream",
                "logs:PutLogEvents"
            ],
            "Resource": [
                "arn:aws:logs:eu-west-1:777777:log-group:/aws/lambda/MyReport:*"
            ]
        }
    ]
}
</code>


Unit testing function using File.Exists

Question: How can I unit test this type of function? Of course I can't create real files and I can't make any changes to Example class and Func to make it easier to test.
As I found out, mocking static Exists is not possible using popular free frameworks like moq. I found some frameworks to mock filesystem like System.IO.Abstractions (https://github.com/tathamoddie/System.IO.Abstractions) but in all samples I found, methods (like CreateDirectory etc. - here Exists), are called from mocked object and I'm not sure how to adapt it to this method (if it is possible).
public class Example
{
    public int Func()
    {
        if(System.IO.File.Exists("some hard-coded path"))
        {
            return 1;
        }
        else
        {
            return 2
        }
    }
}

Comment: Why can't you create real files? why can't your test setup create a file and your cleanup will delete it (or vice versa)? You can Mock such calls with some commercial Mock frameworks (e.g. TypeMock), but I'm not sure this would be the right solution here...
Comment: Why do you say `Of course I can't create real files`? One clear option is to test things on your local computer.
Comment: If you cannot make changes to the Example class, what are you going to do if your test fails?
Answer: How about creating a FileWrapper class and passing it in as a dependency in your test?
public class Example
{
    private IFileWrapper _fileWrapper;

    public Example()
        : this(new FileWrapper())
    {
    }

    public Example(IFileWrapper fileWrapper)
    {
        _fileWrapper = fileWrapper;
    }

    public int Func()
    {
        if (_fileWrapper.Exists("some path")
        {
            // etc
        }
    }
}

then define FileWrapper as:
internal class FileWrapper : IFileWrapper
{
    public bool Exists(string path)
    {
        return File.Exists(path);
    }
}

and the interface IFileWrapper as:
public interface IFileWrapper
{
    bool Exists(string path);
}

Then your test can create a mock of the IFileWrapper and pass this into your class under test (using Moq here):
[TestMethod]
public void Func_ShouldCheckFileExists()
{
    // Arrange
    var mockFileWrapper = new Mock<IFileWrapper>();
    mockFileWrapper.Setup(_ => _.Exists(It.IsAny<string>())).Returns(true);

    var example = new Example(mockFileWrapper.Object);

    // Act
    int returnValue = example.Func("test path");

    // Assert
    Assert.Equals(returnValue, 1);
}

It's a bit more work, but you can build up a library of such wrappers and reuse them across your code base.
If you really can't modify your code, then perhaps a look at Microsoft's Moles project, or the newer Fakes project might help you.
Comment: The question is can we modify the example class? also, IMHO, such solution would add unnecessary complication to the product code, just for testing... not sure that in this specific case it's worth it...
Answer: Possibly, i re-invite the mock wheel, but simple code hereunder is working for me, it used in many my Unit Tests:
I use public bool isUT == true in Unit Test, else == false;
bool function, similar to Example in the question, is
public bool IsFileExist(string pathFile) => x.Exists(pathFile);

with FileWrapper as above
internal class FileExist : IFileWrapper
{
    public bool Exists(string path) => File.Exists(path);
}
public interface IFileWrapper
{
    bool Exists(string path);
}

protected IFileWrapper x; is either null in business run, or injected dependency from Unit Test with the reference to mock class
public class mock : FileOp, IFileWrapper
{
    private string myPath { get; set; }

    internal mock(string myPath = "")
    {
        base.x = this;
        this.myPath = myPath;
    }

    public bool Exists(string path) => path == myPath;
}

When myPath contains the same string as in request, it simulate existing file, else IsFileExist return false.

Unit testing function using File.Exists


Dúvida ao utilizar o plugin inappbrowser do cordova

Question: Estou utilizando o plugin inappbrowser para poder acessar de dentro do meu webapp algumas paginas que estão no meu servidor. Até ai tudo bem, funciona perfeitamente, mas estou querendo utilizar um outro plugin do cordova dentro desses arquivos que estão no servidor, o plugin "vibration" do cordova mas não estou conseguindo pois não tenho lá o cordova.js e mesmo que eu leva apenas esse arquivo javascript para lá, isso não será suficiente, pois esse arquivo utiliza muitos coisas.
Alguém já passou por isso? sabe como solucionar esse dependência?
Answer: Amigo, você tem um problema bem complicado aqui. Quase nenhum plugin do Cordova é somente em JavaScript, eles possuem uma parte codificada em Java ou outras linguagens dependendo de cada plataforma além do fato de precisarem de permissões para cada ação.
Por exemplo no plugin Vibration mencionado percebemos que dentro da pasta SRC existe diversos códigos que devem ser adicionados ao projeto do aplicativo. Não podendo ser requsitados de um servidor.
Outro problema que pode ocorrer facilmente é a hospedagem de diferentes versões de arquivos JS, podendo causar problemas futuros.
Porém...
Como Cordova é uma tecnologia relativamente nova, que não possui suporte a tudo ainda é comum encontrar usuários e desenvolvedores pensando em alternativas e soluções para problemas como este.
Eu não sei como você pretende incorporar esse plugin em seu projeto e quando ele deve ser incorporado. Porém suponho que você deva querer utilizar ele em apenas um caso específico. Por exemplo requisitar o plugin somente quando for ativado as notificações ou algo similar.
Se for algo desse estilo recomendo que implemente todas as funções no seu aplicativo, mas todas as funções que você utilize e que possa utilizar, feito isso crie events no seu aplicativo para disparar essas funções. Dessa forma você pode fazer uma comunicação do seu servidor de Web utilizando requisões JavaScript, porém emitindo eventos. E sim, o aplicativo deve ficar com tamanho maior, porém tudo deve funcionar sem problemas.
Comment: Muito obrigado pela resposta, mas resolvi o problema utilizando um iframe no meu html. vlw
Comment: @Anderson, aqui no Stack a gente diz "obrigado" votando positivamente as respostas ;) E publicar sua própria solução também é incentivado. Valeu!
Comment: @brasofilo, eu pensei que deveria votar positivamente apenas as respostas que fossem uteis para a solução do meu problema, por isso não votei positivamente. Deveria votar mesmo assim? E no meu comentário eu informei como resolver o problema, talvez faltou eu detalhar um pouco mais minha solução, mas mesmo assim obrigado pelas dicas. flw
Comment: @Anderson, bom, eu também voto quando vejo que houve esforço por parte da pessoa e traz alguma informação nova. Mas, com certeza, a decisão é toda sua... não quero forçar barra nenhuma. Saudações!


What is the difference between Liferay CE and Liferay EE?

Question: I am looking for the technical differences between Community Edition and Enterprise Edition.

If I develop something for the CE, will it work on the EE?
If I create a LAR file from the CE, can I import it in the EE?
Basically, can I work and develop on the CE and deploy the result on the EE without major problems?

I'm using 6.0.6 CE and EE.
Bonus points for pointing to resources that justify or details the answer.
Thanks in advance.
Comment: Liferay has an [overview page](http://www.liferay.com/downloads/liferay-portal/overview) of what the differences are. Looks like it boils down to support and tooling.
Comment: Are you referring to actual Admin interface features?
Answer:
1.If I develop something for the CE, will it work on the EE?

Yes. Ideally it should work (but then we are not in an Ideal world :-)) so I would say in most cases it does when you are developing custom portlets. Since the API is more or less same.
I say for custom portlets, but I would not say this for hooks. Since I have seen differences in JSPs between CE and EE, like:

EE uses some jsp-tag which is not to be found in CE (this is for version 6.1, for 6.0 I didn't find anything like this).
also some *Util classes in portal-impl.jar have extra methods which are used in the JSPs or Action classes.

2.If I create a LAR file from the CE, can I import it in the EE?

This works. I have tried this with a single site with 20 odd pages. But again caution is required as to what options you set while you export/import. I would try to update with some examples if possible of the differences

3.Basically, can I work and develop on the CE and deploy the result on the EE without major problems?

Liferay EE's plugins SDK and CE's plugins SDK are different. For the version 6.0 it works deploying CE WAR on a EE server. For 6.1+ have not tried.
you said: "without major problems", for the version 6.0.6 yes it would work without major problems (please take into account explanation to point-1 as well).
Comment: I faced a version mismatch error when trying to import a lar file from Liferay 6.2 CE GA2 to Liferay EE SP6. I guess the imports don't work in 6.2 CE to EE
Answer: if you use CE, so you can't use Liferay Developer Studio and have to use EE.
for CE you can use Liferay IDE (eclipse plug-in)


Configure CKEditor plugin

Question: I have the autosave plugin for CKEditor enabled and working, but I want to change from some of the default settings (specifically save detection selectors and autosave_delay) and can't get the settings to "stick".  I have been editing ckeditor.config.js (in modules/patched/ckeditor/) and, while that's executing, the settings changes aren't making it to the editor instance running on the page.  I've also hacked at ckeditor.utils.js because I was able to get closer to the actual instance but I know that isn't best practice and I'll regret it down the road.
Drupal7 fyi
Answer: You can define (extra) config options in a CKEditor profile. Go to example.com/admin/config/content/ckeditor then select the editor profile(s) in which you have enabled autosave and go to the "Advanced Options".
In the "Custom JavaScript configuration" field you can override config options like
<code>config.autosave_saveDetectionSelectors = "a[href^='javascript:__doPostBack'][id*='Save'],a[id*='Cancel']";
config.autosave_delay = 5;
</code>
No need to alter any of the javascript files.

Configure CKEditor plugin


WPF Alignment not working

Question: I use the following code in my WPF app, for a groupbox:
<GroupBox Style="{StaticResource groupBoxStyle}" Header="RB" Margin="0,6,268,249" Name="groupBoxRB" HorizontalAlignment="Right" VerticalAlignment="Stretch" Width="276">

This control is within a grid, that is defined like this:
<TabControl Grid.Row="1" Margin="4,12,0,12" Name="tabControl1" Background="Transparent">
    <TabItem Style="{StaticResource tabItemMainStyle}" Header="Main" Name="tabItemMain" >
        <Grid Background="Transparent" MinHeight="926" MinWidth="1218">

And that tabcontrol is within the main grid:
<Window x:Class="SRM.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
        xmlns:local="clr-namespace:SRM" ResizeMode="CanResize"
        Title="SRM"
        Width="991" Icon="Resources\Icons\SRM.png"
        WindowStartupLocation="CenterScreen" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="1024" Height="774" Visibility="Visible" Foreground="#00000000" Margin="0">
    <Grid Style="{StaticResource styleBackground}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="2.5" />
        </Grid.ColumnDefinitions>

I don't understand why the groupbox i mentioned won't stretch on its vertical axis... any idea?
Thanks.
PS: the staticresources don't define heights/widths/alignments
Comment: Does your `groupBoxStyle` define a custom ControlTemplate? If so, can we see it?
Comment: Also, is it still a problem if you remove the MinHeight from the inner Grid and the big Margin around the GroupBox?
Comment: groupBoxStyle only defines setters (colors/brushes/opacity).

The margin is so groupboxes don't overlap each others, but it could be replaced if there is something better for that purpose.
Minheight can be removed with no problem.
Comment: what do u mean by the groupboxes don't overlap each others? if the groupboxes reside in a grid you can just control the layout using columns and row so they won't overlap each other, I believe it's the big bottom margin that create this issue.
Comment: @FatalBaboon, have you tried using [Snoop](http://blois.us/Snoop/) to see which item isn't stretching (groupbox, grid or tab item)?
Comment: I think dnr3 raised an important issue, the margins seem to be used for layout purpose, and it should not.
I'm gonna rework this with rows and columns.

On a side note, I tried snoop but it doesn't work oO. The tool finds my app but when i try to analyze it nothing happens.
Answer: My problem came from the designers (vs2010's or blend's) that by default put margins if you place controls manually in them... setting the Margin to 0 solved the problem:
<GroupBox Style="{StaticResource groupBoxStyle}" Header="RB" Margin="0" Name="groupBoxRB" HorizontalAlignment="Right" VerticalAlignment="Stretch" Width="276">

Answer: I think you're missing something in your styles or something. I just made the following from your code and stretching works fine in standalone app.
    <Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="2.5" />
        </Grid.ColumnDefinitions>
        <TabControl Grid.Row="1" Margin="4,12,0,12" Name="tabControl1" Background="Transparent">
            <TabItem Header="Main" Name="tabItemMain" >
                <Grid Background="Transparent" MinHeight="200" MinWidth="200">
                    <GroupBox Header="RB" Name="groupBoxRB" HorizontalAlignment="Right" VerticalAlignment="Stretch" Width="276">
                        <Rectangle Fill="Orange" />
                    </GroupBox>
                </Grid>
            </TabItem>
        </TabControl>
    </Grid>
</Window>

Comment: I think i remember having the same issue some time ago.
From my memory, i noticed that stetching behaviors and the like were messed up whenever i'd take my solution from subversion on a new computer.

Rings any bells?
Comment: Sorry can't say I've encountered that. Could be a default template thing from TabControl or something. One thing to try to would be to set fill colors for each successive UI element so you can figure out the first control that doesn't do stretching properly - that will help sort out which element is the culprit by coloring very clearly what's going on.

WPF Alignment not working


If $g \circ f$ is one-to-one, is $g$ one-to-one?

Question: Let $f:X\to Y, g:Y \to Z$ be functions. I'm trying to prove that if $g \circ f$ is 1-1, then is $g$ 1-1?
Well the definition of a one-to-one (injective) function  is that if $f:X \to Y$ is a function such that each $x \in X$ is related to a different $y \in Y$. I'm not sure what do do about the $g \circ f$ or how to show that that makes $g$ 1-1
Answer: What happens if $X=Z=\{1\},$ $Y=\{1,2\}$ and $f:X\to Y$ is defined by $f(1)=1$ and $g:Y\to Z$ is defined by $g(1)=g(2)=1$ ?
Answer: This is not true, suppose $f,g:\mathbb{R}\rightarrow \mathbb{R}$, such that $f(x)=e^x$ and $g(x)=x^2$. So $g(f(x))=e^{2x}$ and therefore is $1-1$, but g is not $1-1$.
Comment: $g\circ f$ is not one-to-one, because $(g\circ f)(1)=(g\circ f)(0)$ but $1\neq 0.$
Comment: @Baloown: Thank you for commenting, I edited my post.
Comment: Beware that now $g\circ f$ is injective but not surjective : indeed, as $e^{2x}>0,$ it won't exists $x\in\mathbb{R}$ such as $e^{2x}=-1$ (for example). Actually $g\circ f:\mathbb{R}\to\mathbb{R}^{+*}$ would be one-to-one.
Comment: @Baloown one-to-one means injective, not bijective.
Comment: @Joffysloffy : You are right ! Thank you for helping me realizing that only now... ^^'
Comment: @Baloown You're welcome :).
Answer: If $X \stackrel{f}\to Y \stackrel{g} \to Z$ with $g\circ f$ 1-1 then g may not be 1-1: other answers have given counterexamples. However, if $f$ is surjective then $g$ must be 1-1: if $y_1,y_2\in Y$ with $y_1\ne y_2$, then $y_i = f(x_i)$ for some $x_i\in X, i = 1,2$. We must have $x_1\ne x_2$, so $g(f(x_1))\ne g(f(x_2))$ — that is, $g(y_1)\ne g(y_2$.


Beam larger note values in LilyPond

Question: Here is some sample input:
<code>\relative c'' {
  \key g \minor
  d8^[ c]
}
</code>
This works as expected, beaming the two eighth notes together:

When I do this with half notes, I get the following instead:
<code>\relative c'' {
  \key g \minor
  d2^[ c]
}
</code>

when I want this:

How do I manually beam notes greater than an eighth note in duration?
Comment: Are you intending on using this notation to represent a tremolo? or something else?
Comment: @ElementsinSpace I'm using LilyPond to engrave assignments for my Schenkerian analysis course, so I would be beaming together the notes of the *Urlinie*.
Answer: If you simply want the eighth-notes to be hollow then use this
<code>\relative c'' {
  \key g \minor
  \override NoteHead.duration-log = #1
  d8^[ c]
}
</code>
the notes still take up the same length as a regular eighth.
If you want them spaced as half notes you might need something like this
<code>\relative c'' {
  \key g \minor
  \override NoteHead.duration-log = #1
  d8^[ s4. c8] s4.
}
</code>
D
Comment: The value #1 is the log2 of the duration from which the note head is taken, so 0 for a whole note, 1 for a half-note, 2, for a quarter-note, 3 for an eighth-note, etc. [3.1.95 NoteHead](https://lilypond.org/doc/v2.24/Documentation/internals/notehead)
Answer: That's very similar to the symbol for a tremolo repeat, eighth notes (quavers) of C and D, taking up the total time of a half note (minim).  In LilyPond you would write:
<code>\repeat tremolo 2 {d8 c}
</code>
But in you image you have that symbol taking up a full bar in 4/4 time, which is a whole note (semibreve) long.  If you really want it to take up this amount of time you would have to distort the duration of the notes like this:
<code>\repeat tremolo 2 {d8*2 c}
</code>

Beam larger note values in LilyPond


Potência no Java diferente do Excel

Question: Estou tendo um problema pra converter uma fórmula do Excel para o Java/Kotlin. O cenário é o seguinte, no Excel eu tenho:
Taxa de juros: 0.07
Cálculo Excel:
=(1+taxaDeJuros)^12-1 = %125.22

No Kotlin:
val result = Math.pow((1+taxaDeJuros), 12.00 - 1)) = 2.104851952299

Se colocar no Excel a fórmula =(1+taxaDeJuros)^(12-1) o resultado será igual no Kotlin (2.104851952299), ou seja deu a entender que o Kotlin está colocando o parênteses no (12.00-1).
Answer: O Kotlin não está "colocando parênteses", está apenas executando as coisas na ordem que você indicou.
O que acontece é que Math.pow é um método que recebe dois argumentos: a base e o expoente - e eles são separados por vírgula. Assim:
Math.pow( base, expoente )

Ou seja, em Math.pow((1+taxaDeJuros), 12.00 - 1), a base é (1 + taxaDeJuros) (e esses parênteses poderiam até ser retirados) e o expoente é 12.00 - 1. Então primeiro ele calcula esses valores, para depois chamar Math.pow.
Ou seja, é como se eu tivesse chamado Math.pow(1 + taxaDeJuros, 11.0).
No caso do Excel, a avaliação da fórmula (1+taxaDeJuros)^12-1 está usando a ordem de precedência dos operadores: primeiro calcula o que está dentro dos parênteses ((1 + taxaDeJuros)), depois faz a exponenciação (usando o expoente 12.0), e por fim subtrai 1.
Se quer esse mesmo resultado em Kotlin, basta colocar a subtração fora da exponenciação:
val result = Math.pow(1 + taxaDeJuros, 12.0) - 1

Outra opção é usar kotlin.math.pow, assim fica um pouco mais próximo da fórmula do Excel (e provavelmente assim fique mais claro a ordem em que cada operação é feita):
import kotlin.math.pow

val result = (1 + taxaDeJuros).pow(12.0) - 1

Assim acho que fica mais claro que primeiro ele faz a soma 1 + taxaDeJuros, depois eleva a 12, e depois subtrai 1.
Repare também que não precisa de dois zeros depois do ponto, é redundante. 12.0 é o suficiente para indicar que é um double. E no segundo caso, o expoente também poderia ser apenas 12:
import kotlin.math.pow

val result = (1 + taxaDeJuros).pow(12) - 1

Veja no IdeOne.com todas as opções acima rodando.
Answer: Tente assim:
val result = Math.pow((1 + taxaDeJuros), 12.00) - 1

Comment: Muito obrigado mesmo, vlw.

Potência no Java diferente do Excel


SwiftUI @ObservedObject viewmodel in detail-view of List never released

Question: I have a List with several items, that open a DetailView which in turn holds a viewmodel. The viewmodel is supposed to have a service class that gets initialized when the detail view appears and should be deinitialized when navigating back.
However, the first problem is that in my example below, all 3 ViewModel instances are created at the same time (when ContentView is displayed) and never get released from memory (deinit is never called).
struct ContentView: View {
    var body: some View {
        NavigationView {
            List {
                NavigationLink(destination: DetailView()) {
                    Text("Link")
                }
                NavigationLink(destination: DetailView()) {
                    Text("Link")
                }
                NavigationLink(destination: DetailView()) {
                    Text("Link")
                }
            }
        }
    }
}

struct DetailView: View {
    @ObservedObject var viewModel = ViewModel()

    var body: some View {
        Text("Hello \(viewModel.name)")
    }
}

class ViewModel: ObservableObject {

    @Published var name = "John"

    private let heavyClient = someHeavyService()

    init() { print("INIT VM") }

    deinit { print("DEINIT VM") }
}

This is probably just how SwiftUI works, but I have a hard time thinking of a way to handle class objects that are part of a detail view's state, but are not supposed to instantiate until the detail view actually appears. An example would be video conferencing app, with rooms, where the room client that establishes connections etc. should only get initialized when actually entering the room and deinitialize when leaving the room.
I'd appreciate any suggestions on how to mange this. should I initialize the heavyClient at onAppear or something similar?
Answer: The problem is that DetailView() is getting initialized as part of the navigation link. One possible solution could be the LazyView from this post.
Implemented like so:
struct LazyView<Content: View>: View {
    let build: () -> Content
    init(_ build: @autoclosure @escaping () -> Content) {
        self.build = build
    }
    var body: Content {
        build()
    }
}

And then wrap the DetailView() in the LazyView():
struct ContentView: View {
    var body: some View {
        NavigationView {
            List {
                NavigationLink(destination: LazyView(DetailView())) {
                    Text("Link")
                }
                NavigationLink(destination: LazyView(DetailView())) {
                    Text("Link")
                }
                NavigationLink(destination: LazyView(DetailView())) {
                    Text("Link")
                }
            }
        }
    }
}

The only issue with this workaround is that there seems to always be one instance of ViewModel sitting around, though it's a large improvement.
Comment: I think this is the optimization from SwiftUI, not an issue. In case the user goes back to the same DetailView.


Custom target URL for Views content pane exposed filter (block)

Question: Here is the situation:
There are 2 pages in Page manager

Overridden page of the Taxonomy term (i.e. Path: /%term )
Page with Taxonomy term filters (i.e. Path: /%term/filter )

And a View with 2 displays (content panes) :

Show all from term
Show filtered (with exposed filters in block).

How to place the exposed filter block on /%term page but when the filter is submitted,  redirect and apply it to the /%term/filter path ?
Thank you
Comment: awesome video helper for hook_form_alter: http://www.youtube.com/watch?v=syqsH2CEu6U&t=14m42s
Answer: Use form redirection in hook_form_alter.


Alternative visualisation to forest plot for large dataset of effect sizes in R

Question: I have a data set of ~700 studies with response variables. Currently to view a forest plot of the data looks like .
If I use forest<code>(dat$yi[1:100], dat$vi[1:100])</code> I get .
I think the second output looks great, there is an opportunity to visualise the effect, but this is lost if I use the full dataset.
Any advice from the brains trust on alternative ways to display this information in a digestible way without sacrificing samples?
Comment: Could you plot these in a 2d plane where the x axis is the observed effect and the y axis is the width or half-width of the interval?
Comment: How about a caterpillar plot? http://www.metafor-project.org/doku.php/plots:caterpillar_plot It's still essentially the same thing, but ordering the effects by magnitude already helps to make it look much neater.
Comment: Further to this Wolfgang,  is there a way to present average results for effect sizes based on a factor using `forest`? i.e. average yi, vi, grouped an additional column e.g. 'species'. This would be a neat way to add a summary-style forest plot. E.g. the plot [here](https://journals.plos.org/plosone/article/figure/image?size=medium&id=info:doi/10.1371/journal.pone.0208523.g002)
Comment: For anyone in future, a detailed answer to my question in the comment above can be found in this [Cross Validated question](https://stats.stackexchange.com/questions/107557/forest-plot-for-meta-analysis-displaying-the-mean-es-with-and-without-outliers?rq=1)
Comment: Is there a way to re-overlay the dotted line at the 0 point? Once the effects are all plotted, it becomes quite obscured.
Answer: This question raises the issue of why we use forest plots. What do they tell us? In general they display (a) the identity of the study, (b) the corresponding estimate, (c) the precision of that estimate, and (d) optionally, by using the order vertically the effect of a proposed moderator.
If we are willing to sacrifice the identity then we can plot the estimate against the precision with the choice of symbols representing the values of a categorical moderator if any. This is usually called a funnel plot and has the advantage that it will be familiar to the reader. Doing this for a continuous moderator is not viable so that would have to be sacrificed. For a large number of studies there would be over-printing so hollow symbols would be best or even using hexagonal binning.
If we have a continuous moderator we could plot the estimates against the value of the moderator with the size of the symbols proportional to the precision of the estimates. Again hollow symbols would work best here and hexagonal binning would not be an answer.
If the desire is to spot studies which might differ in some way from the others then calculating a suitable diagnostic, perhaps a leave-one-out statistic, and using it as the moderator would distinguish the influential or anomalous studies more readily than the standard forest plot.
Comment: Thanks mdewey. This has given me a lot to consider. I have a combination of categorical and continuous moderators - perhaps I will individually plot each effect size by each moderator to visualize individual effects. I will look in to how to size points by precision, this is a very good idea. Thanks for your insights. I will report back once I have tried these and accept your answer and explain what I ended up doing to solve my predicament.
Comment: Your suggestion "If we have a continuous moderator we could plot the estimates against the value of the moderator with the size of the symbols proportional to the precision of the estimates." has worked very well for me. I am happy with this as a way to visualize the effects in relation to key moderators.


Find all $n$ such that $\sigma(n)=12$

Question: Let $ \sigma (n) = \sum_{k|n}^{}{k} $. I need to solve $\sigma(n)=12$.
Probably the following might be of use: if $n={p_1}^{a_1}{p_2}^{a_2}...{p_s}^{a_s}$ then $\sigma(n)=\frac{{p_1}^{a_1+1}-1}{p_1-1}\frac{{p_2}^{a_2+1}-1}{p_2-1}...\frac{{p_s}^{a_s+1}-1}{p_s-1}$.
Comment: What is the RHS $6$ or $12?$
Comment: It is 12. I edited it.
Answer: If RHS $=12,$ as $1+p+p^2=7$ or $\ge 1+3+3^3=13,n$ must be square-free
So, $\sigma(n)=\prod(1+p_i)$ where $p_i$s are distinct prime divisors $(\ge2)$ of $n$
Also, $\sigma(n)\ge 1+n \ \ \ \ (1)$
The equality occurs if $n$ is prime i.e, here $n+1=12\implies n=11$ which is prime.
$(1)\implies n\le 11$
So, other the values of $n$ must be non-prime & square-free
As $2\le n\le 11,$ the values can be $6,10$
$\sigma(6)=\sigma(2\cdot3)=(1+2)(1+3)=12$
$\sigma(10)=\sigma(2\cdot5)=(1+2)(1+5)=18$

Alternatively,
As $n$ is square-free, the values of $1+p_i$s are $3,4,6,12$ as $\ge2$
If $1+p_1=3,1+p_2=4\implies n=p_1\cdot p_2=2\cdot 3=6$
If $1+p_1=6,1+p_2=2\implies p_2=1<2$ so this case does not arise
If $1+p_1=12, n=p_1=11$
Comment: $p_i \le n$ as well.
Comment: @Zackkenyon, I think, that's too obvious to be mentioned
Comment: I sort of agree, but if it were obvious, then this person would need no help solving this particular case...
Comment: @Zackkenyon, you mean he could have played with the primes $<12$. But before that, we need to establish the square-free ness
Comment: no, I mean he could simply have tested the sum of the divisors of all of the numbers less than 12.
Comment: @Zackkenyon, sorry, earlier I missed 'divisor' term (which I've incorporated now) that caused  confusion
Comment: Oh, god, how did I miss that... I feel so stupid right now.
Answer: Note that
$$
\begin{align}
\sigma (1) &= 1\\
\sigma (2) &= 1+2 = 3\\
\sigma (3) &= 1+3 = 4\\
\sigma (4) &= 1+2+4 = 7\\
\sigma (5) &= 1+5 = 6\\
\sigma (6) &= 1+2+3+6 = 12\\
\sigma (7) &= 1+7 = 8\\
\sigma (8) &= 1+2+4+8 = 15\\
\sigma (9) &= 1+3+9 = 13\\
\sigma (10) &= 1+2+5+10 = 18\\
\sigma (11) &= 12
\end{align}
$$
so the only solutions are $n = 6$ and $n = 11$.

Find all $n$ such that $\sigma(n)=12$


Why this space is not a complete space with this norm

Question: Show that the space $C_0(\mathbb{R})$ of all the real continuous functions $f:\mathbb{R} \to \mathbb{R}$ with compact support is not a complete space with the norm $||f||= \sup_{t∈ \mathbb{R}}|f(t)|$.
Thank you
Comment: It is better to denote it $C_c(\mathbb{R})$. This is a dense subspace of the Banach space $C_0(\mathbb{R})$ of continuous functions tending to $0$ at $\pm \infty$. Since $C_c\subsetneq C_0$, $C_c$ can't be complete, or it would be closed and dense...
Answer: Let $f(x) = \frac 1{1+x^2}$, define $f_n\colon \mathbb R \to \mathbb R$ by $f_n(x) = f(x)$ for $x \in [-n,n]$, $f_n(x) = 0$ for $x \not\in [-n-1, n+1]$, affine-linear in between. Show that $(f_n)$ is Cauchy, but not convergent to some compact support function (as its limit in $\ell^\infty(\mathbb R)$ is $f$).
Comment: $f_{n}(x)$ isn´t a continuous function you should change it for
Comment: @user73681 That's what I meant by "affine-linear in between".
Comment: That is ,for $n<x<n+1$ we let  $f_n(x)= (n+1-x)f_n(n),$ and similarly for $ -n-1<x<-n.$


Group Policy: Auto Disconnect Idle Remote Desktop Sessions

Question: Several of our admins have a bad habit of closing but not logging off from, their remote desktop sessions to multiple servers. We would like to setup a GPO that will automatically log off RDP sessions that have been idle for longer than X.
What I am trying to figure out, is to what I would link the GPO to. Would I link it to the OU which contains the servers, or do I link it to the accounts of the admins themselves?
Comment: Also discussed on SuperUser: http://superuser.com/a/399965/49214
Answer: You can set it in either Computer Configuration or User Configuration and you should set it based on whether you want to target the user (setting applied to any server where the specific user has an RDP session) or to the computer (setting applied to specific server where any user has an RDP session).
If you set it in Computer Configuration then you should link the GPO to the OU where the computer objects reside.
If you set it in User Configuration then you should link the GPO to the OU where the user objects reside.
If you link it to both the Computers and the users OU then the Computer Configuration settings take precedence over the User Configuration settings.
Comment: Glad to help...


How do I get backslashes to save?

Question: Everytime that I type a backslash into a textbox on my drupal site (in content editors, administration pages, you name it; I mean any textbox), it gets filtered out before it reaches the module code.
So I end up typing something like this:

This is just some content with an \n
  or a directory structure like
  C:\Program Files\whatever

And it gets converted to this on save:

This is just some content with an n
  or a directory structure like
  C:Program Fileswhatever

This really makes it hard to edit content on my D6 site; anyone know a fix for this?
Related issues:

http://drupal.org/node/673748
http://drupal.org/node/564166

This is on admin pages; so filters aren't used.
Comment: I can not reproduce this.

Check what filters you have enabled on the input format you are using and/or try a different one.
Comment: have you tried escaping the backslash?
Comment: This is a workaround; the escaped backslashes turn into only one backslash, but I think it's the best answer I'll get.
Comment: glad it worked out for you! :)
Answer: This sounds like it may be a magic_quotes issue. If at all possible, disable this in your PHP configuration. Instructions on how to disable them are on the PHP website. If this is enabled on a live, shared server and you don't have access to the PHP configuration then I would highly recommend looking for a new host. The magic_quotes_gpc option has been deprecated in PHP 5.3, and will likely be removed very soon.
Magic quotes provide a way of escaping incoming input, and have been frowned on for quite a while in favor of validating input properly in your application, which Drupal does.
Comment: yep... phpinfo comes out w/ --enable-magic-quotes. Thanks for the helpful answers.
Comment: @geoff: There's instructions for how to disable them in the links in my answer. I'd definitely try this first if at all possible because this sounds like it's the problem.
Comment: Yes, it's them problem. I've fixed it with a bit of PHP code in bootstrap.php. Thanks for the answers.
Comment: @geoff if this actually solved your problem, then you can change the accepted answer to this to signify to future people looking for your issue that this is the correct solution.
Comment: Yes, but this answer and the above answer are kind of the same...

How do I get backslashes to save?


Plotting Jaccard Index against spatial data (lat-long)

Question: I am trying to find a way to plot a matrix (In this case a matrix with jaccard indices) against spatial distances (I have latitude and longitude data). I have been told to use the "geosphere" package but I haven't been able to fully understand how to use it.
So if anyone here is well versed in doing such things, please help me out
kind regards
Comment: I should probably add, that I need the spatial distances converted to regular distance vectors in meters
Comment: There are many packages that can calculate great circle distances from longlat data. The oldest and most classical is the **sp** package that has function `spDists`. This returns great circle distances in meters. However, there are many others and more modern packages.
Answer: Package geosphere is indeed a good choice as it is based on ellipsoid instead of a sphere and gives more accurate results than many other alternatives (and directly in metres). However, it is more tedious to use as it only calculates a distance between two points, or in a matrix, the track lengths from point to the next point instead of the full matrix. The following is an easy way that does a lot of unnecessary calculation that you throw away, but it is much simpler than alternatives (and sufficiently fast for any practical purpose).
Assume you have matrix x of dimensions N times 2, where the two columns are the longitude and latitude (in this order) in decimal degrees and N is the number of observations:
library(geosphere)
N <- NROW(x)
geodists <- matrix(0, N, N)
for (i in 1:N) for(j in 1:N) geodists[i,j] <- distGeo(x[i,], x[j,])
## alternative for only lower diagonal:
## for(j in 1:(N-1)) for(i in (j+1):N) geodists[i,j] <- distGeo(x[i,], x[j,])
geodists <- as.dist(geodists)

The geodists will then be arranged similarly as the Jaccard distances (assuming you used vegan or other package that returns standad dist structures) and these can be directly plotted against each other. If you used some package that gives you a matrix (which is symmetric and has zero diagonal), it is best to change the result to distances (as.dist()) which only have the lower triangle without the zero diagonal as these give nicer plots.
Package sp uses also WGS84 ellipsoid, but its results differ little (less than 0.01% in my test for distances up to 2500km) from those given by the geosphere. It may be that geosphere is more accurate (and it also allows alternatives to WGS84 ellipsoid). However, sp is much easier to use, and will give you directly the symmetric matrix of distances (but in kilometres instead of metres, though I claimed so in my comment), and you can do with one command:
library(sp)
geodists <- as.dist(spDists(x, longlat=TRUE))*1000 # in metres

Comment: `sp::spDists` uses the Meeus algorithm which is also available in `geosphere::distMeeus`, but `geosphere::distGeo` should be more accurate. Numerically `sp::spDists` and `geosphere::distMeeus` give identical results within numerical accuracy.


How can I stop my left arm from fatiguing while playing guitar in the classical position?

Question: If I let my left arm naturally hang and just reach for the fretboard by bending at the elbow, I find I can access up to the fifth fret.  However for access to say the first fret, I need to lift my arm and basically use some shoulder muscle to keep it in the air.  If I'm doing practice exclusively in frets 1-4 I find that my arm fatigues quite quickly from holding up with my muscles.  What technique adjustments can I do to fix this?  I hold my guitar so that the headstock is roughly in line with my eyes.
Edit:
Here's a picture of me in the 'relaxed' position:
https://dl.dropbox.com/u/12337149/relaxed.png
Here is me 'tensed' because I have to lift my arm to reach the first fret:
https://dl.dropbox.com/u/12337149/tensed.png
Comment: Could you please send us a picture of your playing position ? It will be much easier to advise you.
Answer: Some things you can check for:

Posture: try standing up with a straighter back when playing. Also try very slight turns of your upper body into or away from the guitar, only rotating at your waist (not your shoulders) while keeping the rest of your back straight.
Seat height: if sitting down, try a slightly higher chair, or adding a book to your chair to elevate yourself by a couple of inches. This should lead to a different angle for the neck of the guitar, when keeping it at the same height.
Length of strap: if using a strap, try having it slightly looser, with the guitar hanging slightly lower, while keeping the same height for the headstock
Pull the guitar closer to your body: the guitar may be too far out in front of you. Try having it closer to your belly when playing.
Check your wrist: your wrist position may be incorrect, but that's hard to figure out and correct (in general) without seeing you play.
Answer: In addition to @blue's points, try bringing your guitar much further down towards horizontal. When I play electric guitar the headstock is generally between waist and shoulder height, and usually lower rather than higher.
Practice will strengthen your muscles though.


Which table moodle session time out stored to?

Question: I know the default session time out in moodle is 2 hours . For knowing the session time out:
Site Administration > Server > Session handling> Timeout

I need to know in which table in moodle the time out getting stored?
Moodle version: 2.9.1
Answer: All Moodle site-wide configuration is stored in one of two tables.
If the name of the configuration option is displayed as 'pluginname | settingname' (in small letters under the human-readable name), then it is stored in [mdl_]config_plugins, in a record with "plugin = [pluginname]" and "name = [settingname]".
If the name of the configuration option is displayed as 'setting name' (without the '|' symbol), then it is stored in [mdl_]config, in a record with "name = [settingname]".
So, in this case, it is stored in [mdl_]config, with "name = 'sessiontimeout'".
You should normally use the "get_config($pluginname, [$settingname])" function to retrieve settings and the "set_config($settingname, $value, [$pluginname])" function to set settings. If you are wanting to change a setting in the database, you will need to make sure you purge all the caches when you do so, as all these settings are cached (set_config, if used, automatically updates the cache for you).
Comment: @buddy you are fantastic, thank you very much!! I got it!! `SELECT * FROM `mdl_config` WHERE `name` LIKE '%sessiontimeout%'  `


Snoarqube- I keep getting the error "The 'report' parameter is missing"

Question: I can not seem to find any answers regarding this issue
<code>5:04:56.635 INFO: ------------------------------------------------------------------------
05:04:56.635 INFO: EXECUTION FAILURE
05:04:56.635 INFO: ------------------------------------------------------------------------
05:04:56.635 INFO: Total time: 28.403s
05:04:56.849 INFO: Final Memory: 63M/1435M
05:04:56.849 INFO: ------------------------------------------------------------------------
05:04:56.849 ERROR: Error during SonarQube Scanner execution
The 'report' parameter is missing
05:04:56.850 DEBUG: Execution getVersion
05:04:56.850 DEBUG: Execution stop
</code>
Comment: Would using java 9 as in https://stackoverflow.com/q/48575566/6309 help?
Answer: For me, this was that the SonarQube server had a full HDD
Comment: Same here. the fun part is that even with -X it doesn't say anything about hdd space.
Answer: Re-run SonarQube using the -X switch to enable full debug logging.


Web Sphere Web Services Run-time Implementation

Question: According to the answer to this question, WebLogic uses the JAX-WS RI internally to implement the JAX-WS standard. Does anyone know if Web Sphere uses any third party package as its web services run-time implementation (JAX-WS, JAX-RPC or any other not standard option that may be available on WAS)?
Thanks a lot!
Answer: WebSphere's JAX-WS implementation is built on AXIS2. See this Redbook.
Answer: You should not worry about the engine used by the WAS run-time. What you need to be aware of is the specs that you are after supported by WAS.
Look at this URL for the Supported Specs for WAS 7
http://publib.boulder.ibm.com/infocenter/wasinfo/v7r0/index.jsp?topic=/com.ibm.websphere.nd.doc/info/ae/ae/rovr_specs.html
Look at
Table 7. Supported specifications and APIs for Web services. The product supports the specifications and APIs in this table.
Also refer to the FAQ on WebServices @
http://publib.boulder.ibm.com/infocenter/wasinfo/v7r0/index.jsp?topic=/com.ibm.websphere.express.doc/info/exp/ae/rwbs_faq.html
HTH
Manglu
Comment: Hi Hila, Apologies as i did not get the intent of your question. Have a look at this URL: (where it talks about WAS and APache Open Source stack in the WS space) http://publib.boulder.ibm.com/infocenter/wasinfo/v8r0/topic/com.ibm.websphere.base.iseries.doc/info/iseries/ae/rwbs_faq.html?resultof=%22%41%70%61%63%68%65%22%20%22%61%70%61%63%68%22%20%22%41%78%69%73%22%20%22%61%78%69%22%20
Comment: Thanks, but the implementation is exactly what I care about, not the specs :)


PFSense DNS Not working

Question: I replaced a Dell tower running PFSense 2.2 that kept needing to be rebooted with a Nokia (Nokia Checkpoint IP390 8 Gigabit Ethernet GbE 4GB CF 1GB RAM) rack mount appliance running the newest PFSense 2.3 that I got off eBay.
Things seemed to go smoothly except that the server isn't forwarding DNS info to the DHCP clients. The clients have full access to the internet and I can manually configure them to use Google's DNS servers. The PFSense server has DNS working on it, I can ping domains just fine from there, and the clients have it configured as the gateway and DNS server for them.
Unbound is installed, running and from what I can see is configured correctly with nothing checked under DNS Forwarder, but DNS Resolver is checked for all interfaces.
I have the local clients going for now by manually configuring each ones DNS, but would like to fix the server to work correctly so I don't have a headache each time I plug in a new computer.
For what it is worth, the interface is also REALLY slow, even from the local console it may take 10 seconds to respond. I just got into the office and on the console just hitting ENTER took it about 15 seconds to refresh the admin menu the first time, and about 5 seconds the next time.
Any help is appreciated.
Answer: DNS resolver and DNS forwarder are mutually exclusive services. By default, DNS resolver will be enabled and DNS forwarder will be disabled. We have to flip them to forward the DNS requests from LAN to WAN.

Login to pfSense web console.
Click on DNS Resolver under Services tab, uncheck Enable DNS resolver and save and apply.
Click on DNS Forwarder under Services tab, CHECK Enable DNS forwarder and save and apply.
Comment: Thanks, not sure why the Forwarder would work when the Resolver doesn't, but this seemed to fix it in my case.
Comment: Thanks, worked for me. Even ChatGPT was not able to figure this out :)
Answer: If the DNS forwarder is enabled, the internal interface IP for pfSense will be handed out to DHCP clients as a DNS server. If the DNS forwarder is disabled, the DNS servers configured on pfSense will be handed out instead. https://doc.pfsense.org/index.php/DNS_Forwarder

PFSense DNS Not working


nslookup fails but ping succeeds for nonexistent domains

Question: I have two different FreeBSD servers (different hosting companies), both exhibit this same behavior:  They pick a specific IP address (184.108.40.206) for every domain that does NOT exist.
nslookup fails as it should....
<code>$ nslookup thisdomainsurelydoesntexist.com
Server:         xx.xx.229.3
Address:        xx.xx.229.3#53

** server can't find thisdomainsurelydoesntexist.com: NXDOMAIN
</code>
dig gives me:
<code>$ dig thisdomainsurelydoesntexist.com

; <<>> DiG 9.6.-ESV-R5-P1 <<>> thisdomainsurelydoesntexist.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 51717
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 0

;; QUESTION SECTION:
;thisdomainsurelydoesntexist.com. IN    A

;; AUTHORITY SECTION:
com.                    900     IN      SOA     a.gtld-servers.net. nstld.verisign-grs.com. 1370378827 1800 900 604800 86400

;; Query time: 23 msec
;; SERVER: xx.xx.229.3#53(xx.xx.229.3)
;; WHEN: Tue Jun  4 16:05:02 2013
;; MSG SIZE  rcvd: 122
</code>
and ping gives me:
<code>$ ping thisdomainsurelydoesntexist.com
PING phx2-ss-5-bug616849-lb.cnet.com (22.214.171.124): 56 data bytes
64 bytes from 188.8.131.52: icmp_seq=0 ttl=244 time=25.733 ms
64 bytes from 126.96.36.199: icmp_seq=1 ttl=244 time=20.460 ms
^C
--- phx2-ss-5-bug616849-lb.cnet.com ping statistics ---
2 packets transmitted, 2 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 20.460/23.096/25.733/2.637 ms
</code>
Note that dig's final host name, nstld.verisign-grs.com, resolves to that IP.
What's the fix?
UPDATE:  /etc/resolv.conf has two nameserver rows, each with an IP(v4) I got from my ISP.
But if I add a "search" row to resolv.conf, behavior changes: if "search mydomain.com" (i.e., my real domain name), everything resolves to it and I get my own IP. E.g., thisdomainsurelydoesntexist.com.mydomain.com. Not good. But if I set it to something else, like "search myispdomain.com", then everything works: existing domains resolve, and nonexistent ones don't.
But is that anything but an accident?
Thanks for the suggestions! Here's host -a, and the xx.xx.80.18 IP is the first nameserver in /etc/resolv.conf
<code>$ host -a thisdomainsurelydoesntexist.com
Trying "thisdomainsurelydoesntexist.com"
Received 122 bytes from xx.xx.80.18#53 in 13 ms
Trying "thisdomainsurelydoesntexist.com"
Host thisdomainsurelydoesntexist.com not found: 3(NXDOMAIN)
Received 122 bytes from xx.xx.80.18#53 in 0 ms
</code>
My ISP just told me it could be because my hostname is of the form "mydomain.com" instead of "myhost.mydomain.com" (which is their recommended practice). I could see how that might fix it. Is that the thing to do?  No downsides to it?
Also, very significantly, I should mention that this python code works the same way ping does:
<code>import _socket
_socket.getaddrinfo('thisdomainsurelydoesntexist.com', 80)
</code>
And many other python modules are built on this core.
Comment: The contents of your `/etc/resolv.conf` might be helpful here, as would a `tcpdump -A port 53` while you do the ping.
Comment: What does `host -a thisdomainsurelydoesntexist.com` give you?
Answer: The system (particularly glibc, which handles name resolution) behaves erratically when the hostname of the server is a domain name. From the man page for resolv.conf:
The search list is normally determined from the local domain name; by default, it contains only the local domain name.
What this means in simple terms is that when a domain lookup fails (after nothing turns up in /etc/hosts and the resolver fails to return a useful result) the system will proceed to cheerfully remove the first part of the hostname - for example 'abcxyz.com' - and append the remainder as a search suffix.
Since '.com' is the search suffix produced by removing 'abcxyz' from the hostname, the system is appending '.com' as the search suffix for failed lookups, which produces results such as:
foobar-abcxyz.cz -> foobar-abcxyz.cz.com -> www.czjewelry.com
foobar-abcxyz.com -> foobar-abcxyz.com.com -> www.cnet.com
To correct for this, you will likely want to set the hostname of the server to a hostname such as 'hostname.abcxyz.com' instead of 'abcxyz.com' - which will in turn result in 'abcxyz.com' being appended as the search suffix by default.
As an interim measure, you can create a random MD5 checksum and add it to /etc/resolv.conf as an override for the search suffix:

uuidgen | md5sume930f5f4ba6ba7868b0cc6718bcef568  -
echo "search e930f5f4ba6ba7868b0cc6718bcef568" >>/etc/resolv.conf

This will append 'e930f5f4ba6ba7868b0cc6718bcef568' to failed DNS lookups instead of '.com' - which in turn results in the default behavior of failed lookups for nonexistent domains. Should you change the hostname to an actual hostname, this line can be removed.
Answer: Some nameservers deliberately return IPs for nonexistent domains. ISPs are notorious for doing this - they can actually monetize on advertising given on landing pages for nonexistent domains.
You could always change your resolv.conf file to use public DNS servers that are known for sure not to exhibit this behavior. Google's DNS (126.96.36.199 and 184.108.40.206) and Level3's DNS (22.214.171.124 through 188.8.131.52) both provide public DNS access and do not redirect unknown domains. (Source: https://www.grc.com/dns/alternatives.htm)
Comment: I'm pretty sure this isn't the case here, as this doesn't relate to  browsing, always goes to the same (non-monetizing) page for both ISPs.
Comment: +1 for the link. Useful in a different context. (But I can't vote it up. Sorry.)
Answer: It sounds to me like you are using a network that is utilizing a wildcard DNS.  This means that it will automatically re-route you to that IP address if an address fails.  You can test this by doing a search in your web browser.  When it fails, it will redirect you to some sponsored search page that is being handled by your ISP.
Comment: fdmillion is saying the same thing as me, but he has a higher quality answer that suggests using the Google public DNS instead.
Comment: If my ISP were deliberately redirecting me, then nslookup should work the same as ping, shouldn't it? That they don't tells me it's my own configuration issue. How would my ISP would distinguish them?
Comment: You should set the hostname of the machine to subdomain.domain.com but I doubt that you would see that kind of issue.  One thing you can do is use the public DNS as suggested.  If the problem goes away, you know it is something strange happening with the DNS of your ISP.


Dias úteis e API Java 8, como verificar?

Question: Como verificar se um dia é útil utilizando a API do Java 8?
É possível verificar se é sábado ou domingo, mas como verificar por exemplo feriado como 7 de Setembro (Independência do Brasil) ou Sexta-Feira Santa?
int ano = 2014;
int mes = 5;
YearMonth anoMes = YearMonth.of(ano, mes);

List<LocalDate> listaDosDiasUteisDoMes = new ArrayList<>();

for(int dia=1; dia <= anoMes.lengthOfMonth(); dia++){
  LocalDate data = anoMes.atDay(dia);

  if(!data.getDayOfWeek().equals(DayOfWeek.SATURDAY) &&
    !data.getDayOfWeek().equals(DayOfWeek.SUNDAY)){

      listaDosDiasUteisDoMes.add(data);
  }
}

Comment: Você pretende fazer isso offline ou tem uma conexão com internet?
Comment: Inicialmente offline. Mas se for o caso modifico para online.
Answer: Para isso você precisa definir a sua própria lista de feriados. Existem várias APIs que fornecem este tipo de informação, como a Jollyday (tem um exemplo no final), e várias outras listadas neste link.
Caso você não queira usar uma API, pode ter seu próprio cadastro também (e neste caso, é algo que você terá que fazer manualmente).
Outro problema dos feriados é que nem todos possuem datas fixas (como a Páscoa, Carnaval, etc, que todo ano caem em datas diferentes), e devem ser calculados de acordo com o ano. Além disso, há os feriados nacionais, estaduais e municipais, e você deve decidir se vai incluí-los ou não na sua lista.
De qualquer forma, independente da solução escolhida (usar uma API externa, cadastrar manualmente, incluir somente feriados nacionais, etc), basta que você guarde todos os feriados em um java.util.Set, e em seguida verifique se a data está no Set (usando o método contains), mais ou menos assim:
Set<LocalDate> feriados = // feriados, ver mais abaixo como montar este Set
for (int dia = 1; dia <= anoMes.lengthOfMonth(); dia++) {
    LocalDate data = anoMes.atDay(dia);

    if (data.getDayOfWeek() != DayOfWeek.SATURDAY
        && data.getDayOfWeek() != DayOfWeek.SUNDAY
        && !feriados.contains(data)) {
        listaDosDiasUteisDoMes.add(data);
    }
}

Como java.time.DayOfWeek é um enum, posso compará-los usando == e != (não precisa usar equals).

Para montar o Set com os feriados, eu sugiro separar em 2 métodos: um para os feriados fixos (que é mais fácil, já que sempre caem no mesmo dia todo ano) e outro para os feriados móveis.
Eu sugiro que você passe o ano como parâmetro para estes métodos, assim você consegue construir uma lista de java.time.LocalDate, que é o que você precisa.
// feriados que acontecem todo ano na mesma data, gerar lista para o ano específico
public Set<LocalDate> getFeriadosFixos(int year) {
    Set<LocalDate> dates = new HashSet<>();

    // 7 de setembro
    dates.add(LocalDate.of(year, 9, 7));
    // natal
    dates.add(LocalDate.of(year, 12, 25));
    // ... adicione todos os outros

    return dates;
}

Este método não considera as "emendas" de feriados (se cai na terça, emenda a segunda, então esses dois dias não são úteis). Se quiser, basta verificar se o feriado é uma terça ou quinta (comparando o DayOfWeek):
// 7 de setembro
LocalDate seteSetembro = LocalDate.of(year, 9, 7);
dates.add(seteSetembro);
// se cai na terça, inclui a segunda ("emenda" de feriado)
if (seteSetembro.getDayOfWeek() == DayOfWeek.TUESDAY) {
    dates.add(seteSetembro.minusDays(1));
}
// se cai na quinta, inclui a sexta ("emenda" de feriado)
if (seteSetembro.getDayOfWeek() == DayOfWeek.THURSDAY) {
    dates.add(seteSetembro.plusDays(1));
}

Faça isso para todos os feriados que podem ser "emendados".
Para os feriados móveis, há uma fórmula para calculá-los (procure no Google e encontrará vários sites explicando). Vou usar uma que achei:
// calcula páscoa, carnaval, corpus christi e sexta-feira santa
public Set<LocalDate> getFeriadosMoveis(int year) {
    Set<LocalDate> dates = new HashSet<>();

    LocalDate pascoa;
    LocalDate carnaval;
    LocalDate corpusChristi;
    LocalDate sextaFeiraSanta;

    int a = year % 19;
    int b = year / 100;
    int c = year % 100;
    int d = b / 4;
    int e = b % 4;
    int f = (b + 8) / 25;
    int g = (b - f + 1) / 3;
    int h = (19 * a + b - d - g + 15) % 30;
    int i = c / 4;
    int k = c % 4;
    int l = (32 + 2 * e + 2 * i - h - k) % 7;
    int m = (a + 11 * h + 22 * l) / 451;
    int month = (h + l - 7 * m + 114) / 31;
    int day = ((h + l - 7 * m + 114) % 31) + 1;

    pascoa = LocalDate.of(year, month, day);

    // Carnaval 47 dias antes da pascoa (sempre cai na terça)
    carnaval = pascoa.minusDays(47);

    // CorpusChristi 60 dias apos a pascoa
    corpusChristi = pascoa.plusDays(60);

    sextaFeiraSanta = pascoa.minusDays(2);

    // páscoa cai sempre no domingo, entao não precisaria adicionar como feriado
    // dates.add(pascoa);

    // carnaval: adicionar um dia antes e depois (emenda de segunda e quarta-feira de cinzas)
    dates.add(carnaval);
    dates.add(carnaval.minusDays(1)); // emenda a segunda-feira
    dates.add(carnaval.plusDays(1)); // quarta-feira de cinzas

    // corpus christi, emendar (adicionar a sexta)
    dates.add(corpusChristi);
    // if apenas para confirmar se é quinta-feira
    if (corpusChristi.getDayOfWeek() == DayOfWeek.THURSDAY) {
        dates.add(corpusChristi.plusDays(1)); // adicionar a sexta-feira
    }

    dates.add(sextaFeiraSanta);

    return dates;
}

Repare que incluí também as "emendas" de feriados. Se não quiser, basta remover as respectivas linhas.
Para criar o Set com todos os feriados, basta usar os dois métodos acima e juntar tudo em um único Set:
Set<LocalDate> feriados = new HashSet<>();
feriados.addAll(getFeriadosFixos(year));
feriados.addAll(getFeriadosMoveis(year));

O parâmetro year pode ser o ano que você está usando (anoMes.getYear()), por exemplo.

Apenas como complemento, é possível encapsular a lógica de verificação em um java.time.temporal.TemporalQuery. A diferença é que esta interface trabalha com um java.time.temporal.TemporalAccessor (ao invés de um tipo específico como LocalDate), então o código para verificar o dia da semana é um pouco diferente:
public class VerificaDiaUtil implements TemporalQuery<Boolean> {

    private Set<LocalDate> feriados = new HashSet<>();

    public VerificaDiaUtil() {
        this.feriados = // constrói a lista de feriados
    }

    @Override
    public Boolean queryFrom(TemporalAccessor temporal) {
        // obter o dia da semana
        DayOfWeek dow = DayOfWeek.from(temporal);
        return dow != DayOfWeek.SATURDAY && dow != DayOfWeek.SUNDAY
               // extrair o LocalDate e verificar se está no Set de feriados
               && !feriados.contains(LocalDate.from(temporal));
    }
}

Com isso a classe VerificaDiaUtil fica responsável por manter a lista de feriados. Se quiser, você pode alterar o construtor para receber o ano como parâmetro (e aí ela constrói apenas os feriados daquele ano), ou então ela já carrega os feriados de vários anos de uma só vez. Fica a seu critério.
Para usar esta classe, basta usar o método query de LocalDate:
VerificaDiaUtil diaUtil = new VerificaDiaUtil();
for (int dia = 1; dia <= anoMes.lengthOfMonth(); dia++) {
    LocalDate data = anoMes.atDay(dia);
    if (data.query(diaUtil)) {
        listaDosDiasUteisDoMes.add(data);
    }
}

Como TemporalQuery recebe um java.time.temporal.TemporalAccessor, ele funciona com outras classes também (desde que estas tenham dia, mês e ano - e consequentemente, um dia da semana), como LocalDateTime ou ZonedDateTime. Se você passar uma classe que não possui dia da semana (como LocalTime, por exemplo, que só possui as horas), este código lancará uma exceção.

Apenas para citar um exemplo com uma API, segue uma solução com Jollyday. Fica bem similar ao código acima, a única diferença é que você não precisa mais calcular os feriados manualmente, podendo delegar esta tarefa para a API:
import de.jollyday.HolidayCalendar;
import de.jollyday.HolidayManager;
import de.jollyday.ManagerParameters;

public class VerificaDiaUtil implements TemporalQuery<Boolean> {
    private HolidayManager manager;
    public VerificaDiaUtil() {
        // usa os feriados do Jollyday, em vez de calcular manualmente
        this.manager = HolidayManager.getInstance(ManagerParameters.create(HolidayCalendar.BRAZIL));
    }
    @Override
    public Boolean queryFrom(TemporalAccessor temporal) {
        // obter o dia da semana
        DayOfWeek dow = DayOfWeek.from(temporal);
        return dow != DayOfWeek.SATURDAY && dow != DayOfWeek.SUNDAY
                // extrair o LocalDate e verificar se é feriado
                && !this.manager.isHoliday(LocalDate.from(temporal));
    }
}

// A forma de usar é a mesma do código anterior
VerificaDiaUtil diaUtil = new VerificaDiaUtil();
for (int dia = 1; dia <= anoMes.lengthOfMonth(); dia++) {
    LocalDate data = anoMes.atDay(dia);
    if (data.query(diaUtil)) {
        listaDosDiasUteisDoMes.add(data);
    }
}

No caso do Jollyday, ainda dá para verificar feriados estaduais, municipais, etc, ou ainda criar sua própria lista. Para mais detalhes, consulte a documentação.
Answer: Você pode utilizar a Google Calendar API da seguinte forma proposta pela documentação:

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.DateTime;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.calendar.Calendar;
import com.google.api.services.calendar.CalendarScopes;
import com.google.api.services.calendar.model.Event;
import com.google.api.services.calendar.model.Events;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.GeneralSecurityException;
import java.util.Collections;
import java.util.List;

public class CalendarQuickstart {
    private static final String APPLICATION_NAME = "Google Calendar API Java Quickstart";
    private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
    private static final String TOKENS_DIRECTORY_PATH = "tokens";

    /**
    * Global instance of the scopes required by this quickstart.
    * If modifying these scopes, delete your previously saved credentials/ folder.
    */
    private static final List<String> SCOPES = Collections.singletonList(CalendarScopes.CALENDAR_READONLY);
    private static final String CREDENTIALS_FILE_PATH = "credentials.json";

    /**
    * Creates an authorized Credential object.
    * @param HTTP_TRANSPORT The network HTTP Transport.
    * @return An authorized Credential object.
    * @throws IOException If the credentials.json file cannot be found.
    */
    private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
        // Load client secrets.
        InputStream in = CalendarQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
        GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

        // Build flow and trigger user authorization request.
        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
                .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
                .setAccessType("offline")
                .build();
        return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
    }

    public static void main(String... args) throws IOException, GeneralSecurityException {
        // Build a new authorized API client service.
        final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
        Calendar service = new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
                .setApplicationName(APPLICATION_NAME)
                .build();

        // List the next 10 events from the primary calendar.
        DateTime now = new DateTime(System.currentTimeMillis());
        Events events = service.events().list("primary")
                .setMaxResults(10)
                .setTimeMin(now)
                .setOrderBy("startTime")
                .setSingleEvents(true)
                .execute();
        List<Event> items = events.getItems();
        if (items.isEmpty()) {
            System.out.println("No upcoming events found.");
        } else {
            System.out.println("Upcoming events");
            for (Event event : items) {
                DateTime start = event.getStart().getDateTime();
                if (start == null) {
                    start = event.getStart().getDate();
                }
                System.out.printf("%s (%s)\n", event.getSummary(), start);
            }
        }
    }
}

Answer: No meu caso eu tenho uma tabela de feriados e com ela eu utilizado as rotinas abaixo:
private LocalDate calculaDia(LocalDate data, int diaMesSemanaAno, boolean trataFinalDeSemana, boolean trataFeriado) {
    int maisOuMenosUm = 1;
    // trata quando quiser subtrair uma data
    if (diaMesSemanaAno < 0) {
        diaMesSemanaAno = diaMesSemanaAno * -1;
        maisOuMenosUm = -1;
    }
    while (diaMesSemanaAno > 0) {
        data = data.plusDays(maisOuMenosUm);
        // quando for final de semana
        if (trataFinalDeSemana) {
            // verifica se e final de semana...
            data = eFinalSemana(data, maisOuMenosUm);
        }

        // quando tem feriado
        if (trataFeriado) {
            data = eFeriado(data, maisOuMenosUm);
        }

        diaMesSemanaAno--;
    }
    return data;
}

private LocalDate eFeriado(LocalDate data, int somaSubtraiUm) {
    String feriado = DateUtil.getData(data, DateUtil.DATA_BR_PADRAO_BARRAS);
    TwebferiadosTO twebferiadosTO = new TwebferiadosTO();
    twebferiadosTO.setDataFormatada(feriado);
    twebferiadosTO = consultar(twebferiadosTO);
    if (twebferiadosTO != null) {
        data = data.plusDays(somaSubtraiUm);
        data = eFeriado(data, somaSubtraiUm);
        //verifica se nao e final de semana
        data = eFinalSemana(data, somaSubtraiUm);
    }
    return data;
}

private LocalDate eFinalSemana(LocalDate data, int somaSubtraiUm) {
    if (data.getDayOfWeek() == DayOfWeek.SATURDAY
            || data.getDayOfWeek() == DayOfWeek.SUNDAY) {
        data = data.plusDays(somaSubtraiUm);
        data = eFinalSemana(data, somaSubtraiUm);
    }
    return data;
}

Valida feriado em sequencia. Ex. Carnaval
Valida feriado que cai no final de semana


Exception in thread "main" java.lang.NullPointerException at reactor.core.publisher.FluxFilter$FilterSubscriber.onNext

Question: The subscribe method seems to give an nullpointerexception, after the filter method. When I remove the filter method everything is working fine. Why?
        Flux<String> flux = new Flux<String>() {
            @Override
            public void subscribe(CoreSubscriber<? super String> coreSubscriber) {
                coreSubscriber.onNext("Test 7");
                coreSubscriber.onNext("test 6"); //line 33
                coreSubscriber.onNext("test 5");
                coreSubscriber.onNext("test 4");
                coreSubscriber.onNext("test 3");
                coreSubscriber.onNext("test 2");
                coreSubscriber.onNext("test 1");
                coreSubscriber.onComplete();
            }
        };

        flux
            .filter((s) -> s.contains("7"))
            .subscribe(System.out::println); //line 45

I get the following error:
Exception in thread "main" java.lang.NullPointerException
    at reactor.core.publisher.FluxFilter$FilterSubscriber.onNext(FluxFilter.java:100)
    at com.douma.reactor.App$1.subscribe(App.java:33)
    at reactor.core.publisher.FluxFilter.subscribe(FluxFilter.java:52)
    at reactor.core.publisher.Flux.subscribe(Flux.java:6873)
    at reactor.core.publisher.Flux.subscribeWith(Flux.java:7040)
    at reactor.core.publisher.Flux.subscribe(Flux.java:6866)
    at reactor.core.publisher.Flux.subscribe(Flux.java:6830)
    at reactor.core.publisher.Flux.subscribe(Flux.java:6773)
    at com.douma.reactor.App.test2(App.java:45)
    at com.douma.reactor.App.main(App.java:24)

Answer: You're setting the initial value of your Flux container inside the subscribe method. When you call filter, the initial value is still null, so contains throws the exception. Add this to your code:
    flux.startWith("starter string")
        .filter((s) -> s.contains("7") || s.contains("starter"))
        .subscribe(System.out::println); // Prints out "starter string" and "Test 7"


how to show that $A_{k-1} \subset 3A_{k} $ where $ A_k $ are iterations of the Cantor set ???

Question: For example
$A_1 = [0,1/3] \cup [2/3 , 1] $
$A_2 = [0, 1/9] \cup[2/9 ,1/3] \cup [2/3 ,7/9] \cup [ 8/9 ,1]$
Note that
$3 (A_2) = A_1 \cup D $   , for some set $D$
In the general case, I want to show that
$A_{k-1} \subset  3A_{k} $
I know that by multiplying(for 3) the primitives $ 2^{k -1} $ ranges of $ A_k $ I will be able to generate the $ A_{k-1} $ but I do not know how to demonstrate this.
How can I do it?
Comment: What definition of $A_k$ are you starting from? It's hard to answer your question without knowing that.
Comment: The definition of the Cantor set
  pattern https://en.wikipedia.org/wiki/Cantor_set
Answer: Using the definition that you linked in your comment, and changing from $C_n$ notation to your $A_k$ notation, one obtains
$$\frac{A_{k-1}}{3} \cup \left(\frac{2}{3} + \frac{A_{k-1}}{3}  \right) = A_k
$$
Multiplying both sides by $3$ one gets
$$A_{k-1} \cup (2 + A_{k-1}) = 3A_k
$$
and therefore
$$A_{k-1} \subset 3 A_k
$$
To be more specific, using that $A_{k-1} \subset [0,1]$ and that $A_k \subset [0,1]$, and that $2+A_{k-1} \subset [2,3]$, it follows from this that $A_{k-1} = 3 A_k \cap [0,1]$.
Comment: It's because I actually wanted to show this result by using the Cantor set construction in the traditional way, dividing the interval into 3 parts and excluding the middle part, but if I could show that using this formula over $ C_n $ is the same thing of starting in 3 and excluding the middle, would also serve.


(Modal Logic) Show that a binary relation is Euclidean if and only if it is transitive and symmetric.

Question: I am attempting that a binary relation is Euclidean if and only if it is transitive and symmetric in a Modal setting, where a modal system is Euclidean if it contains Axiom 5: $\diamond \phi \rightarrow \Box \diamond \phi$ is Euclidean, a modal system is symmetric if it contains Axiom B: $\phi \rightarrow \Box \diamond \phi$, and a modal system is transitive if it contains Axiom 4: $\Box \phi \rightarrow \Box \Box \phi$.
I've made a bit of headway in proving the forward direction: if the modal system is Euclidean, then if $wRv \land wRu$, we have $vRu$. Also, if we have $wRu \land wRv$, we have $uRv$. Because $(wRv \land wRu) \iff (wRu \land wRv)$, we have $vRu \iff uRv$, which shows that the accessibility relation between $v$ and $u$ is symmetric. However, I can't figure out how to show that the accessibility relations between $w$ and both $u$ and $v$ are symmetric, and I can't make any progress on showing transitivity.
Answer: You're having trouble because it's not true: not every Euclidean relation is symmetric and transitive. What is true is that a symmetric relation is Euclidean if and only if it is transitive. Is it possible you wanted to show that a relation is symmetric and Euclidean if and only if it is symmetric and transitive?
Aside from this equivalence, there are no other implications between these three properties. It's a good exercise to find relations which are:

Euclidean and transitive but not symmetric
Symmetric but neither Euclidean nor transitive
Euclidean but neither transitive nor symmetric
Transitive but neither Euclidean nor symmetric


Prove MLE underestimate variance get two different results

Question: $$
\hat{\sigma}^2 = \frac{1}{N}\sum_{n = 1}^N (x_n - \bar{x})^2
$$
When proving the variance is underestimated using MLE, the following two procedures are generating different results.
$$
 E[\hat{\sigma}^2] = E[\frac{1}{N}\sum_{n = 1}^N (x_n - \bar{x})^2] \\= \frac{1}{N}\sum_{n=1}^N E[(x_n^2 - 2x_n\bar{x} + \bar{x}^2)] \\= \frac{1}{N}\sum_{n=1}^N E[x_n^2 -  2x_n\bar{x} + \bar{x}^2] \\= \frac{1}{N}\sum_{n=1}^N (E[x_n^2]-2\bar{x}E[x_n]+E[\bar{x}^2])\\= \frac{1}{N}\sum_{n=1}^N (E[x_n^2]-2\bar{x}^2+E[\bar{x}^2])\\=E[x_n^2]-2\bar{x}^2+E[\bar{x}^2]
$$

$$
E[\hat{\sigma}^2] = E[\frac{1}{N}\sum_{n = 1}^N (x_n - \bar{x})^2] \\= \frac{1}{N}E[\sum_{n = 1}^N (x_n^2 - 2x_n\bar{x} + \bar{x}^2)] \\= \frac{1}{N}E[\sum_{n = 1}^N x_n^2 - \sum_{n = 1}^N 2x_n\bar{x} + \sum_{n = 1}^N \bar{x}^2] \\= \frac{1}{N}E[\sum_{n = 1}^N x_n^2 - 2N\bar{x}^2 + N\bar{x}^2]\\=\frac{1}{N}E[\sum_{n = 1}^N x_n^2 - N\bar{x}^2] \\= \frac{1}{N}E[\sum_{n = 1}^N x_n^2] - E[\bar{x}^2] \\= \frac{1}{N}\sum_{n = 1}^N E[x_n^2] - E[\bar{x}^2] \\= E[x_n^2] - E[\bar{x}^2]
$$
Can anyone point out which step is wrong in the first procedure?
Comment: Just add some comments here, you should use capital letters here and $E(X_n)=\mu$ not $\bar{X}$
Comment: Also, worth pointing out that $\mathbb{E}[\hat \sigma^2] = \sigma^2(1-1/N)$ *underestimates* the true variance.  Hence, you divide by $N-1$ instead of $N$ to get the unbiased estimator.
Comment: I suggest you carefully try to compute first $\mathbb{E}[X_i \bar X]$ and then $\mathbb{E}[\bar X^2]$, remembering that if $i=j$ then $\mathbb{E}[X_i X_j] = \mathbb{E}[X_i^2] \neq \mu^2$!
Comment: @P.Windridge Thanks for pointing out. I made a typo on that in the original post. Corrected now
Answer: Your problem stems from two facts. First, look at the last equality of the first aproach. You have wrongly assumed that the inequality below is an equality.
$$E[x^2_n] \neq E[\bar x^2]$$
Second, as pointed out in the comments, you should probably check your notion of expected value. $\bar x$ is an estimator, and $E[x] \neq \bar x$.
To avoid problems in the future, consider that $E[X]$ is the mean and $\bar x$ is the empirical mean. While $\bar x$ is a function of your data, $E[X]$ depends on the distribution of $X$, which you don't really know if you just have a bunch of observations.
Comment: Thanks for correcting. I made a typo on the first issue, corrected it in the post. The second point caught my mistake. One more question, when proving this, why \bar{x}^2 could not be taken out from expectation?
Comment: Because it is a random variable and not a constant (only constants can be taken out of the expectation E()  )
Comment: @rifle123 Exactly. Consider that the expected value is simply an integral or sum, weighted by the probability of observing each value of the variable. That is why constants can be factored out. However, estimators are in general different depending on the set of outcomes we observed.


How to interate over an output list from fetchone() in mysql database

Question: I am currently creating a main menu that acts like a bank account.
def main_menu():
    print("Main Menu")
    print("0 - Quit")
    print("1 - Open Account")
    print("2 - Check Balance")
    print("3 - Close Account")

loop = True
while loop:
    main_menu()
    choice = input("Enter your choice: ")
    choice = int(choice)
    if choice == 0:
        exit(loop)
    elif choice == 1:
        name_on_account = input("Name on account: ")
        balance = float(input("Enter Initial Balance: "))
        print("---Account successfully created---")
        print("Account number:", account_no())
        print("Name on account:", name_on_account)
        print("Balance:", balance)
        print("Account opened on:", now)
        cur.execute("""
        INSERT INTO account(name_on_account, balance) VALUES
        ("%s", "%s");""", (name_on_account, balance))
        connection.commit()
    elif choice == 2:
        print("Checking account balance")
        account_number = input("Enter account number: ")
        print("---Checking account balance---")
        print("Account number:", account_number)
        cur.execute("""SELECT * from account;
        """)
        account_no1 = cur.fetchone()
        for i in account_no1[0]:
            if account_number == i:
                cur.execute("""select name_on_account from account where account_no = "%s";
                        """, (account_number,))
                name1 = cur.fetchone()
                print(name1)
                name2 = ''.join(map(str,name1))
                name3 = int(name2)
                print("Name on account:", name3)
                cur.execute("""select balance from account where account_no = "%s";
                        """, account_number)
                balance1 = cur.fetchone()
                balance2 = ''.join(map(str,balance1))
                balance3 = int(balance2)
                print("Balance:", balance3)
                cur.expecute("""select account_open_date from account where account no = "%s";
                        """, account_number)
                date1 = cur.fetchone()
                date2 = ''.join(map(str, date1))
                date3 = int(date2)
                print("Account opened on:", date3)
                connection.commit()
            else:
                print("Error: Invalid account number")

I'm not worried about option 3 as of right now, but I am having trouble with option 2.
When a person pick option 1, they will input their name and the amount of money deposited in their bank account.
This information will be stored in the mysql table account(account_no, name_on_account, balance, account_open_date, account_status).
This means that account_no is auto-incremented, account_open_date is default as curdate(), and account_status is default to be "open").
In option 2 however, when a person input their account number; it should return back all of their information how it is displayed in the option 1.
What I am having trouble with is, how do you efficiently iterate over the person's information using fetchone() and be able to get the specific column information with (account_no = account_number) (if you do have a better suggestion on a better way to implement this, please comment below)
This is the error message that I get:
Traceback (most recent call last):
File "(deleted for privacy purposes)"
for i in account_no1[0]:
TypeError: 'int' object is not iterable
Thank you for the help!
Comment: Your "deleted for privacy purposes" removed too much. What line raised the error and where is that line in the code you posted?
Answer: pymysql (and most other python mysql clients) is returning a tuple when you invoke fetchone(), and each entry in the tuple matches up to the datatypes defined in the table that you're querying. So let's look at your code:
elif choice == 2:
    print("Checking account balance")
    account_number = input("Enter account number: ")
    print("---Checking account balance---")
    print("Account number:", account_number)
    cur.execute("""SELECT * from account""")
    account_no1 = cur.fetchone()
    for i in account_no1[0]:

Here you're querying all columns and all rows from the account table, reading the first row, and then iterating over whatever is in the first column of that row. But that's probably just an id column, which is probably why you're getting the error that you're getting: integers are not iterable. That'd be like writing for i in 10, Python doesn't understand what that means
If you want to iterate over all of the returned rows, you can do that like this:
    cur.execute("""SELECT * from account;
    """)
    for row i in cur.fetchall():
        # Each iteration "row" is a tuple of values corresponding to the table's columns

That's kind of silly though. You'd returning all of the rows from the database and then iterating over them and looking for a specific account number. Why not just do that as part of your query?
    nrows = cur.execute("SELECT * from account where account_no=%s", (account_number,))
    if nrows == 0:
        # Account number not found, do something
    else:
        row = curs.fetchone()
        # row now contains all of the values from the discovered

Note that you don't need to put quotation marks around %s placeholders, and you don't need semicolons. The client does all of this conversion for you.
Also note that you should not select * from tables. What if the table definition changes to include more columns? Just select the handful of columns that you actually need, then if more columns are added to the table later your code won't need to be changed at all and you won't be requesting more data than you need. So more like:
nrows = cur.execute("SELECT name, balance, date_opened from account where account_no=%s", (account_number,))
if nrows > 0:
    name, balance, date_opened = curs.fetchone()


Set, sequence, bag, or what?

Question: I am dealing with finite collections of real numbers, which I will write in square brackets below.
In these collections, repetitions are significant, so, for example $[1,1,5,7]$ is not the same as $[1,5,7]$. This means that these collections are not sets.
Also, order is not significant, so $[3,5,9]$ is equal to $[5,3,9]$. This means that these collections can not be regarded as sequences, either.
So, if they are not sets, and they are not sequences, what are they?? Is there some existing mathematical construct that has the properties I described. I have seen them called "bags" in one paper, but I suspect the author just made up this name arbitrarily because (like me) he couldn't find an existing mathematical object with the desired properties.
Comment: Related question: http://math.stackexchange.com/q/152223/11994
Answer: Multiset is in common (English) use to describe "sets" in which elements may have multiplicity greater than $1$.
If you go to the Wikipedia page, you will find a number of other terms, including "bag," which I think is second in popularity.
Comment: Thanks. I see that we even have a tag for the term "multiset", so the concept can't be as obscure as I thought.
Comment: I think of the term multiset as standard. It is also in routine classroom use in combinatorics courses.
Answer: You are dealing with ordered lists, also called arrays.
If there is a fixed number, $n$, of members in the list, this is also often called an $n$-tuple.   Such as a pair, triple, quaduple, pentuple, ...
Sequences are lists where often the members' values are some function of their index.
If order were not significant, you would be dealing with a multiset.
Comment: "Also, order is not significant ..."
Comment: OP updated after answer.

Set, sequence, bag, or what?


Sort with entityTypeManager

Question: I'm running an app where I am searching for every nodes of a given content type.
$nids = \Drupal::entityTypeManager()
  ->getListBuilder('node')
  ->getStorage()
  ->loadByProperties([
    'type' => 'courses',
    'status' => 1,
]);

I'm searching for a way to sort the results among a custom field of my nodes.
EDIT:
I do not understand what's going on.
Thanks to @4k4, I wrote this query:
$query = \Drupal::entityTypeManager()
    ->getListBuilder('node')
    ->getStorage()
    ->getQuery();
$query->condition('type', 'post');
$query->condition('status', 1);
$query->condition('field_category', $k);
$query->sort('field_ordering', 'ASC');

$nids = $query->execute();
$nids = Node::loadMultiple($nids);

A var_dump on $r[1] prints nothing. Same for $r[2]. But $results contains my results. What's going on ?
The sorting doesn't work by the way.
Comment: I am working on 8.4 and entityQuery is deprecated since 8.3.
Comment: use entityQuery, see https://drupal.stackexchange.com/questions/205075/sort-entityquery-by-date-published
Comment: No, entityQuery is not deprecated, or do you mean how to get to it? You know if you inject entityTypeManager you can get entityQuery from it as well?
Comment: Did I miss-read this post ? https://www.drupal.org/node/2389335
Could you provide me a snippet using entityQuery that joins with nodes custom fields tables ? I tried entityQueryAggregate but it won't work. I am totally lost here.
Comment: Yes, you are, loadByProperties was never meant to be a replacement for entityQuery, it is a shortcut to it for simple searches without sorting or other advanced things. See the answer how to sort with entityQuery in D8.4
Answer: The change record says if you have entityTypeManager injected, use
$query = $this->entityTypeManager->getStorage('node')->getQuery();

For non-OOP code, $query = \Drupal::entityQuery(); is not deprecated and can still be used.
The linked example for sorting an entity query by using $query:
$query->condition('type', 'blog_post')
  ->sort('field', 'DESC');
$nids = $query->execute();

Comment: Thank you this helps. However, I need to add a condition on a custom field (field_test) and it seems the joins aren't made in this request.
Comment: Is sorting working now? - For a condition on a field, this would be a separate issue. Normally you can use all fields. If not, ask another question and tell what is special about the custom field so that is doesn't work.
Comment: I edited my post.
Comment: Please don't post this amount of code not directly connected to the question.
Comment: I found my error. I was doing a foreach on my table and reindexing my output array with the node key (so the ID). I turned the keys to strings and it works ! Thanks.

Sort with entityTypeManager


Turning a GET variable into a session variable

Question: How do I turn the variable below into a session variable?
$url = mysql_real_escape_string($_GET['url']);

Answer: First, initialize your session, then set the variable.
session_start();

$_SESSION['url'] = $_GET['url'];

Answer: I ditched your mysql_real_escape_string(). There is no reason to do that before saving it as a session variable.
Your best bet is to use it where the context requires it, e.g. in a SQL string.
Better still, use bound parameters with PDO.
session_start();
$url = $_GET['url'];
$_SESSION['url'] = $url

Answer: // to set it
$_SESSION['url'] = mysql_real_escape_string($_GET['url']);

// to get it
$url = $_SESSION['url'];

Answer: Don't forget to start the session with
    session_start();
first, either, and the same on every page you use the session variable.


Confusion relative to derivative of partition function

Question: I have this partition function

Now if I take the derivative of log(Z(x)) wrt $\lambda_k$
the result is

I didn't get how this was derived. This is the paper
Answer: Write $\mathbf{x} = \mathbf{x}^{(i)}$ just to avoid writing superscript $(i)$'s everywhere.
$$\frac{\partial}{\partial \lambda_k} \log Z(\mathbf{x}) = \frac{1}{Z(\mathbf{x})} \frac{\partial}{\partial \lambda_k} Z(\mathbf{x}) = \frac{1}{Z(\mathbf{x})} \sum_\mathbf{y} \frac{\partial}{\partial \lambda_k} \exp\left(\sum_{j=1}^K \lambda_j f_j(y_t, y_{t-1}, \mathbf{x}_t)\right)$$
which, using the ordinary chain rule for functions of one variable, gives
$$\frac{1}{Z(\mathbf{x})} \sum_\mathbf{y} f_k(y_t, y_{t-1}, \mathbf{x}_t) \exp\left(\sum_{j=1}^K \lambda_j f_j(y_t, y_{t-1}, \mathbf{x}_t)\right) = \sum_\mathbf{y}f_k(y_t, y_{t-1}, \mathbf{x}_t) \frac{\exp\left(\sum_{j=1}^K \lambda_j f_j(y_t, y_{t-1}, \mathbf{x}_t)\right)}{Z(\mathbf{x})} $$
By the definition of $p(\mathbf{y}|\mathbf{x})$ in Equation 1.16 of the paper, this is
$$\sum_\mathbf{y} f_k(y_t, y_{t-1}, \mathbf{x}_t) p(\mathbf{y}|\mathbf{x}).$$
Now $\mathbf{y}$ is shorthand for $(y,y')$ and the sum over all possible $\mathbf{y}$ has been rewritten as $\sum_{y, y'}$.