Error in looping through a text file in python

Question: I am trying to loop through a text file and apply some logic but I am not able to loop through the text file. So currently I have a text file that is structured like this:
--- section1 ---
"a","b","c"
"d","e","f"
--- section2 ---
"1","2","3"
"4","5","6"
--- section3 ---
"12","12","12"
"11","11","11"

I am trying to filter out the first line which contains '---' and convert the lines below into json until the next '---' line appear in the text document.
However I got this error " fields1 = next(file).split(',') StopIteration
with open(fileName,'r') as file:
    for line in file:
        if line.startswith('-') and 'section1' in line:
            while '---' not in next(file):
                fields1 = next(file).split(',')
                for x in range(0,len(fields1)):
                    testarr.append({
                    config.get('test','test'): fields1[x]
                    })

                with open(test_dir,'w') as test_file:
                    json.dump(testarr, test_file)

Any idea why my code is not working or how i can solve the error ?
Comment: Try writing as two loops in series instead of nesting them. Loop1: skip all lines until `--- section1...`. Loop2: Dump all lines until another `---` is met.
Comment: Please don't edit the code in the question directly like that.
Comment: Given past experiences with similar titles, I expected this to be a crap question that needed to be closed. Instead, found a well phrased, clear question with basically all the relevant information present. Many kudos OP.
Comment: Ignore my original (now deleted) comment. It's been a long time since I've seen an `else` to a `while` loop.
Comment: `break` shouldn't be in an `else`. Inner `with` most certainly should not be in the `while` loop.
Answer: Looks like you are overcomplicating matters massively. The next inside the inner while loop I imagine is tripping up the outer for loop, but that's just unnecessary anyway.  You are already looping over lines; pick the ones you want, then quit when you're done.
with open(fileName,'r') as inputfile:
    for line in inputfile:
        if line.startswith('-') and 'section1' in line:
            continue
        elif line.startswith('-'):
            break
        else:
            testarr.append({config.get('test', 'test'): x
                for x in line.split(',')})

with open(test_dir,'w') as test_file:
    json.dump(testarr, test_file)

I hope I got the append right, as I wanted to also show you how to map the split fields more elegantly, but I'm not sure I completely understand what your original code did. (I'm guessing you'll want to trim the \n off the end of the line before splitting it, actually. Also, I imagine you want to trim the quotes from around each value. x.strip('"') for x in line.rstrip('\n').split(','))
I also renamed file to inputfile to avoid clashing with the reserved keyword file.
If you want to write more files, basically, add more states in the loop and move the write snippet back inside the loop. I don't particularly want to explain how this is equivalent to a state machine but it should not be hard to understand: with two states, you are skipping or collecting; to extend this, add one more state for the boundary when flipping back, where you write out the collected data and reinitialize the collected lines to none.
Comment: The outer `for` loop is not what causes the problem. This is clearly stated in the question.
Comment: Also, your code will add all lines that are not in section1 as well.
Comment: Huh? The `elif` skips to the end when you reach section 2.
Comment: The `next` in the inner loop in the OP's attempt manipulates the `file` iterator of the outer loop.
Comment: I agree that the next is a bad idea. It's the fact that there's two of them that messes things up though, not the fact that it's in a for loop.
Comment: This won't work to find section2 and can't be used if section1 is not the first section BTW.
Comment: @MadPhysicist Seriously, edited to remove a double space after punctuation?
Comment: I agree with the criticism, and thought about it while answering, but figured the code would not be too hard to extend to work for that case. First make it run, then make it right, then make it fast.
Comment: Thanks. I added a brief explanation of how to extend this.
Comment: The issue was actually that you could only ever process the first section using a single loop like this, but nice edit either way.
Answer: The cause of your error is that you are misusing the file object genrator by calling next on it twice as often as you think. Each call to next gets a line and returns it. Therefore, while '---' not in next(file): fields1 = next(file).split(',') gets a line, checks it for ---, then gets another line and tries to parse it. This means that you are able to skip a line containing the --- by having it come up in the second next. In that case you will get to the end of the file before you find the line you are looking for. StopIteration is how iterators normally indicate that their input has been exhausted.
There are a couple of other issues you may want to address in your code:

Using next on a generator like a file when you are already inside a for loop may cause undefined behavior. You may be getting away with it this time, but it is not good practice in general. The main reason you are getting away with it, by the way, is possibly that you never actually return control to the for loop once the while is triggered, and not that files are particularly permissive in this regard.
The inner with that dumps your data to a file is inside your while loop. That means that the file you open with 'w' permissions will get truncated for every iteration of the while (i.e., each line in the file). As the array grows, the output will actually appear fine, but you probably want to move that out of the inner loop.

The simplest solution would be to rewrite the code in two loops: one to find the start of the part you care about, and the other to process it until the end is found.
Something like this:
test_arr = []
with open(fileName, 'r') as file:
    for line in file:
        if line.startswith('---') and 'section1' in line:
            break

    for line in file:
        if '---' in line:
            break
        fields1 = line.split(',')
        for item in fields1:
            testarr.append({config.get('test','test'): item})

with open(test_dir,'w') as test_file:
    json.dump(testarr, test_file)

EDIT:
Taking @tripleee's advice, I have removed the regex check for the start line. While regex gives great precision and flexibility for finding a specific pattern, it is really overkill for this example. I would like to point out that if you are looking for a section other than section1, or if section1 appears after some other lines with dashes, you will absolutely need this two-loop approach. The one-loop solutions in the other answers will not work in a non-trivial case.
Comment: While going to regex offers better precision for matching exactly the pattern(s) you want, in this case it looks like a complication and overkill to boot.
Comment: @tripleee. I don't disagree with you. I did notice that the two-loop solution is necessary though if you look for a section other than section1, or if section1 is not the first section.
Answer: next() raises a StopIteration exception when the iterator is exhausted. In other words, your code gets to the end of the file, and you call next() again, and there's nothing more for it to return, so it raises that exception.
As for how to solve your problem, I think this might be what you want:
with open(fileName, 'r') as file:
    for line in file:
        if line.startswith('---'):
            if 'section1' in line:
                continue
            else:
                break
        fields1 = line.split(',')
        for x in range(len(fields1)):
            testarr.append({
                config.get('test', 'test'): fields1[x]
            })

with open(test_dir, 'w') as test_file:
    json.dump(testarr, test_file)

Comment: Given the input file, you do not really explain how it is possibly to reach the end of the file when the stop condition is a line containing `---`.
Comment: Also, that write needs to be outside the loop.
Comment: @MadPhysicist So it does. Thanks.
Comment: Also, same problem as with @tripleee's anser. It won't work for any section but the first. I'll bet OP will want to reuse this code for other sections too.


The iniscript feature of terraform does not work on vcloud director

Question: I am currently learning terraform in vcloud director,
I want to customize my installation,
I work on version 3.3.1
The problem, the initscript does not work,
in version 3.3.1 we should delete this option
But I don't know the name of the replacement, can you help me
Sincerely
Answer: the initscript option will still be available, but now it is inside of the customization part.
Documentation: https://registry.terraform.io/providers/vmware/vcd/latest/docs/resources/vapp_vm

initscript (Optional; v2.7+) Provide initscript to be executed when
customization is applied.
Comment: While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference.  Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/30730288)


Comando SELECT no MYSQL está duplicando um resultado (que não está duplicado no banco de dados)

Question: Estou fazendo um select simples para retornar uma tabela no Excel, porém, quando executo o comando, algumas linhas estão sendo duplicadas no resultado. Segue o código abaixo:
SELECT
    distinct movimentacao.DATA, movimentacao.COD, CADASTRO_MP.DESCRICAO, CADASTRO_MP.GRUPO, CADASTRO_MP.SUBGRUPO, movimentacao.QUANTIDADE, cadastro_mp.UNIDADE, movimentacao.ENTRADA_SAIDA, movimentacao.ALMOXARIFADO, CADASTRO_MP.ULTIMO_PRECO, CADASTRO_MP.ULTIMO_PRECO * movimentacao.QUANTIDADE
FROM movimentacao
LEFT JOIN CADASTRO_MP
    ON CADASTRO_MP.COD = MOVIMENTACAO.COD
ORDER BY movimentacao.DATA;

Alguém sabe dizer o que estou fazendo de errado?
Já busquei em algumas perguntas aqui no stackoverflow porém não consegui solucionar meu problema.
Comment: Usa um `GROUP BY` que resolve.
Comment: Tente adicionar um exemplo de dado que aparece duplicado. Para isso acontecer, o problema está no relacionamento/dados das tabelas.
Comment: Quantos registros com o COD que duplica tem na tabela CADASTRO_MP?
Comment: Como você está utilizando a cláusula DISTINCT não deve haver registros duplicados no resultado. Talvez você tenha a impressão de que as linhas são duplicadas mas efetivamente não são. Por ex. um campo texto com um espaço a mais torna o campo diferente.
Comment: DISTINCT não funciona quando você seleciona mais de uma coluna.
Answer: Tenta assim:
SELECT distinct movimentacao.ID, movimentacao.DATA, movimentacao.COD,
CADASTRO_MP.DESCRICAO, CADASTRO_MP.GRUPO,
CADASTRO_MP.SUBGRUPO, movimentacao.QUANTIDADE,
cadastro_mp.UNIDADE, movimentacao.ENTRADA_SAIDA,
movimentacao.ALMOXARIFADO, CADASTRO_MP.ULTIMO_PRECO,
CADASTRO_MP.ULTIMO_PRECO * movimentacao.QUANTIDADE
FROM movimentacao left JOIN CADASTRO_MP
ON CADASTRO_MP.COD = MOVIMENTACAO.COD
GROUP BY movimentacao.ID
ORDER BY movimentacao.DATA

Comment: Desta forma não funciona pois o CADASTRO_MP.COD pode se repetir, porém com a data diferente... Utilizando o GROUP_BY ele mostra apenas um registro de cada .COD
Comment: Consegui! Tive que buscar o ID(que é primary key) da tabela MOVIMENTACAO_ALMOX e agrupar por ele. Obrigado!


WP7 GeoCoordinateWatcher in the background

Question: I'm wanted to wright a app which would alarm you when you are getting near a location.
I wanted to use the GeoCoordinateWatcher with PeriodicTask, but the test Task is invoked every 15min if i'm not mistaken. Can I somehow force the PeriodicTask to invoke more frequent? Or is there something else I can do to use the GeoCoordinateWatcher in the background which is not invasive for the UX. The app should work like for 2-3 hours and i like to update the location like every 5 minutes.
Comment: Can you show us the code you're using now?
Answer: No, it's not possible for now. You only can run your application in foreground under lock screen to save battery life.
Comment: Just wanted to know this. Thank you for the fast response.
Answer: Register to the PositionChanged event of GeoCoordinateWatcher and set a low value of property MovementThreshold of GeoCoordinateWatcher to get the position changed events more frequently.
According to MSDN
    This property specifies the minimum change in position that must take place
before the PositionChanged event is raised.

Answer: You could use a Timer to Start your Watcher every 5 minutes, just don't forget to Stop it too and so save the battery.


Ensure spring bean loaded from non spring context

Question: I have spring application alongside (jersey 2.6 classes and ) servlets .
I need to get Spring bean(s) from jersey/non spring context,
Similar question suggested to get context in a static wrapper of context
public static ApplicationContext getContext() {
    return context;
}

How can I be sure the context is already loaded or not null?
If I can't, how should I wait/check until it spring context is loaded?
In case of calling from jersey context or calling bean from  a simple HttpServlet code
EDIT
Jersey is working fine using jersey-spring3 dependency jar, so my question is only about Servlets out of Spring control
EDIT 2
The application is loading spring different than @entpnerd suggested article
It register a Servlet implementing a WebApplicationInitializer
public class MyWebAppInitializer implements WebApplicationInitializer {

But also have DispatcherServlet configured in web.xml
How can the DispatcherServlet loaded only after Spring loaded?
Because we add Autowiring capabilities on its init method:
WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext())
                    .getAutowireCapableBeanFactory().autowireBean(this);

Is adding a timeout before serving requests is the most prefer solution or is there a tweak in class loading that can take care of it?
EDIT 3
I found answers and answers of injecting, but not why Spring is loaded before Servlet.
Comment: You sure you can't let Spring inject Jersey in your case? This is a nice article explaning how to work with Jersey in Spring Boot. It's old, but should still be relevant. https://geowarin.github.io/a-simple-spring-boot-and-jersey-application.html
Comment: Thanks for suggestion, I use jersey 2.6 and not 3, and I prefer a solution that include getting bean from HTTPServlet also
Comment: The Spring way would be to let the Spring context initialize itself and only after start everything else. If you wonder whether Spring app context is loaded when you need it, you should review your booting procedure.
Comment: Under tomcat 8, My Servlets are defined in web.xml while Spring is AnnotationConfigWebApplicationContext
Answer: The idea is quite simple, although the actual implementation may vary depending on an exact way of Spring boot and Jersery initialization.
An idea:
Spring boot, as being a purely runtime framework, is all about proper loading the application context (from the question standpoint).
So, bottom line, when it's loaded there is an application context somewhere in memory, and its possible to access beans from this application context.
Now, since you say that Jersey is not spring/spring-boot driven, this application context has to be reachable from some kind of static global variable by Jersey, it's quite ugly but should work.
So the idea has two steps:

Put an application context reference to some static holder accessible from Jersey.
Read this value in some infrastructure level code from Jersey component.

A Possible Implementation
Technically step one can be done by implementing some kind of spring boot listener that will store application context in some kind of singleton:
enum ApplicationContextHolder {
   INSTANCE;
    private ApplicationContext ctx;
    void setApplicationContext(ApplicationContext ctx) {
        this.ctx = ctx;
    }

    ApplicationContext getCtx() {
        return this.ctx;
    }

}

// and a listener (spring boot provides many ways to register one, but the
// implementation should be something like this):
// The main point is that its managed by spring boot, and hence and access to
// the application context
class StartupListener implements ApplicationListener<ContextRefreshedEvent> {
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
             ApplicationContextHolder
             .INSTANCE
             .setApplicationContext(event.getApplicationContext());
    }
}

Now the step 2 is:
class MyJerseyOrWhateverComponentThatWantsToAccessApplicationContext {

    public void foo() {
       ApplicationContext ctx = ApplicationContextHolder.INSTANCE.getCtx();
       ...
       ctx.getBean(...);
    }
}

Comment: How can I be sure that step 1 is done before I get request to servlet?
Comment: In general you can't, so the implementation of the holder can vary depending on your actual needs. Do you want the Jersey Servlet to "wait" till the app context starts? do you want to fallback to some initial behaviour if the application context is not available? You can implement both ways by slightly modifying the code of that singleton. Its hard to tell more, because both "processes" (jersey initialization) and spring boot initialization work completely independently
Comment: Notice my question (edit) is for standalone servlets . do I **need** to wait? is there an option to ensure spring context is loaded before servlets can receive request and is it a valid solution?
Comment: It actually depends how exactly you start the spring boot application. WebApp has its own lifecycle (and as I see it from your question) Jersey is bound to this lifecycle, so there should be some kind of listener. Now the question is how does Spring boot actual load up. If its also in a listener, then you can combine them so that Spring boot will load first (it loads synchronously) and only after that the jersey filter will execute. This way you guarantee that by the time the jersey servlet starts, application context is already available
Answer: So a viable solution for this could happen in two stages:

A Spring bean gets the ApplicationContext instance and sends it to a static singleton outside of the Spring context.
Your standalone servlet gets the ApplicationContext instance from the static singleton and verifies that the right beans have been loaded.

Consider the following code as an example:
SpringMetaBean.java
// @Component so that it's part of the Spring context
// Implement ApplicationContextAware so that the ApplicationContext will be loaded
// correctly
@Component
public class SpringMetaBean implements ApplicationContextAware {
  private ApplicationContext appCtx;
  public setApplicationContext(ApplicationContext appCtx) {
    this.appCtx = appCtx;
  }

  // @PostConstruct so that when loaded into the Spring context, this method will
  // automatically execute and notify ApplicationContextHolder with a reference to
  // the ApplicationContext
  @PostConstruct
  public void setup() {
    ApplicationContextHolder.set(this.appCtx);
  }
}

ApplicationContextHolder.java
public class ApplicationContextHolder {
  // ensure the reference is thread-safe because Spring and standalone Servlet will
  // probably be running on different threads.
  private final AtomicReference<ApplicationContext> appCtxContainer = new AtomicReference<>();

  public void set(ApplicationContext appCtx) {
    this.appCtxContainer.set(appCtx);
  }

  public ApplicationContext get() {
    return this.appCtxContainer.get();
  }
}

MyStandaloneServlet.java
public class MyStandaloneServlet {
  // my request handler method
  public void getResponse(HttpServletRequest rq) {
    ApplicationContext springAppCtx = ApplicationContextHolder.get();
    // if not null, we know that Spring has been loaded and we can dig into the
    // application context.
  }
}

Comment: How do I ensure `ApplicationContextHolder` loaded before `MyStandaloneServlet` first request?
Comment: Since `ApplicationContextHolder` is outside the Spring context, then it should be loaded whenever the first `set()` and `get()` calls happen, since Java lazy loads these classes. However, this first call to the `set()` method should happen before the first request - when Spring is loaded based on your `web.xml` configuration and `SpringMetaBean` calls the `set()` method as part of the loading of the Spring and web containers.
Comment: How/Why Spring loaded before Servlet in web.xml?
Comment: The `web.xml` file is usually used using the dispatcher servlet. See https://www.baeldung.com/spring-xml-vs-java-config for an example.
Comment: I added more info in question regarding the article


Favoring LogBack over Log4j in spring-boot/ security

Question: Im having a problem with this version of spring-boot <version>1.3.3.RELEASE</version> Which I did not use to have in prior versions.
Everything runs fine in intelij. however when i want to run the packaged version with mvn spring-boot:run I get a classpath hell error :
LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation (class org.apache.logging.slf4j.Log4jLoggerFactory loaded from file:/Users/jstuartmilne/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.4.1/log4j-slf4j-impl-2.4.1.jar). If you are using WebLogic you will need to add 'org.slf4j' to prefer-application-packages in WEB-INF/weblogic.xml Object of class [org.apache.logging.slf4j.Log4jLoggerFactory] must be an instance of class ch.qos.logback.classic.LoggerContext

I chose Logback because in prior spring-boot versions Logback was the logging implementation I would have less problems with. However this is not the case for this version of spring-boot.
I Would like to stay with Logback now that I have everything setup with it.
This is how my pom.xml look like :
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>

        <version>1.3.3.RELEASE</version>
        <relativePath />
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <google-api.version>1.20.0</google-api.version>
        <postgresql.version>9.3-1103-jdbc41</postgresql.version>
        <scala.version>2.10.4</scala.version>
        <activiti.version>5.17.0</activiti.version>
        <spring-security-test.version>4.0.1.RELEASE</spring-security-test.version>

    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>com.google.api-client</groupId>
            <artifactId>google-api-client</artifactId>
            <version>${google-api.version}</version>
        </dependency>

        <dependency>
            <groupId>com.google.http-client</groupId>
            <artifactId>google-http-client-jackson</artifactId>
            <version>${google-api.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>${postgresql.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-mongodb</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
            <version>2.13.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jdbc</artifactId>
            <version>8.0.15</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>

        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>spring-boot-starter-jpa</artifactId>
            <version>${activiti.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity</artifactId>
            <version>1.6.4</version>
        </dependency>

        <dependency>
            <groupId>com.javaslang</groupId>
            <artifactId>javaslang</artifactId>
            <version>2.0.0-RC2</version>
        </dependency>

        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache-core</artifactId>
            <version>2.6.9</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.8</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

        <dependency>
            <groupId>com.paypal.sdk</groupId>
            <artifactId>rest-api-sdk</artifactId>
            <version>LATEST</version>
        </dependency>

        <dependency>
            <groupId>com.mandrillapp.wrapper.lutung</groupId>
            <artifactId>lutung</artifactId>
            <version>0.0.5</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>

            </plugin>
        </plugins>
    </build>

</project>

Now running a mvn dependency:tree returns this :
[INFO] --- maven-dependency-plugin:2.10:tree (default-cli) @ timeoff ---
[INFO] com.asimplemodule:timeoff:jar:0.0.1-SNAPSHOT
[INFO] +- org.springframework.boot:spring-boot-starter-security:jar:1.3.3.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter:jar:1.3.3.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-starter-logging:jar:1.3.3.RELEASE:compile
[INFO] |  |  |  +- ch.qos.logback:logback-classic:jar:1.1.5:compile
[INFO] |  |  |  |  \- ch.qos.logback:logback-core:jar:1.1.5:compile
[INFO] |  |  |  +- org.slf4j:jul-to-slf4j:jar:1.7.16:compile
[INFO] |  |  |  \- org.slf4j:log4j-over-slf4j:jar:1.7.16:compile
[INFO] |  |  \- org.yaml:snakeyaml:jar:1.16:runtime
[INFO] |  +- org.springframework:spring-aop:jar:4.2.5.RELEASE:compile
[INFO] |  |  \- aopalliance:aopalliance:jar:1.0:compile
[INFO] |  +- org.springframework.security:spring-security-config:jar:4.0.3.RELEASE:compile
[INFO] |  \- org.springframework.security:spring-security-web:jar:4.0.3.RELEASE:compile
[INFO] |     \- org.springframework:spring-web:jar:4.2.5.RELEASE:compile
[INFO] +- com.google.api-client:google-api-client:jar:1.20.0:compile
[INFO] |  +- com.google.oauth-client:google-oauth-client:jar:1.20.0:compile
[INFO] |  |  \- com.google.code.findbugs:jsr305:jar:1.3.9:compile
[INFO] |  +- com.google.http-client:google-http-client-jackson2:jar:1.20.0:compile
[INFO] |  |  \- com.fasterxml.jackson.core:jackson-core:jar:2.6.5:compile
[INFO] |  \- com.google.guava:guava-jdk5:jar:13.0:compile
[INFO] +- com.google.http-client:google-http-client-jackson:jar:1.20.0:compile
[INFO] |  +- com.google.http-client:google-http-client:jar:1.20.0:compile
[INFO] |  \- org.codehaus.jackson:jackson-core-asl:jar:1.9.11:compile
[INFO] +- org.springframework.boot:spring-boot-starter-data-jpa:jar:1.3.3.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-aop:jar:1.3.3.RELEASE:compile
[INFO] |  |  \- org.aspectj:aspectjweaver:jar:1.8.8:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-jdbc:jar:1.3.3.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-jdbc:jar:4.2.5.RELEASE:compile
[INFO] |  +- org.hibernate:hibernate-entitymanager:jar:4.3.11.Final:compile
[INFO] |  |  +- org.jboss.logging:jboss-logging:jar:3.3.0.Final:compile
[INFO] |  |  +- org.jboss.logging:jboss-logging-annotations:jar:1.2.0.Beta1:compile
[INFO] |  |  +- org.hibernate:hibernate-core:jar:4.3.11.Final:compile
[INFO] |  |  |  +- antlr:antlr:jar:2.7.7:compile
[INFO] |  |  |  \- org.jboss:jandex:jar:1.1.0.Final:compile
[INFO] |  |  +- dom4j:dom4j:jar:1.6.1:compile
[INFO] |  |  |  \- xml-apis:xml-apis:jar:1.0.b2:compile
[INFO] |  |  +- org.hibernate.common:hibernate-commons-annotations:jar:4.0.5.Final:compile
[INFO] |  |  +- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final:compile
[INFO] |  |  \- org.javassist:javassist:jar:3.18.1-GA:compile
[INFO] |  +- javax.transaction:javax.transaction-api:jar:1.2:compile
[INFO] |  +- org.springframework.data:spring-data-jpa:jar:1.9.4.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-orm:jar:4.2.5.RELEASE:compile
[INFO] |  \- org.springframework:spring-aspects:jar:4.2.5.RELEASE:compile
[INFO] +- org.postgresql:postgresql:jar:9.3-1103-jdbc41:compile
[INFO] +- org.springframework.data:spring-data-mongodb:jar:1.8.4.RELEASE:compile
[INFO] |  +- org.springframework:spring-tx:jar:4.2.5.RELEASE:compile
[INFO] |  +- org.springframework:spring-context:jar:4.2.5.RELEASE:compile
[INFO] |  +- org.springframework:spring-beans:jar:4.2.5.RELEASE:compile
[INFO] |  +- org.springframework:spring-core:jar:4.2.5.RELEASE:compile
[INFO] |  +- org.springframework:spring-expression:jar:4.2.5.RELEASE:compile
[INFO] |  +- org.springframework.data:spring-data-commons:jar:1.11.4.RELEASE:compile
[INFO] |  +- org.slf4j:slf4j-api:jar:1.7.16:compile
[INFO] |  \- org.slf4j:jcl-over-slf4j:jar:1.7.16:compile
[INFO] +- org.mongodb:mongo-java-driver:jar:2.13.0:compile
[INFO] +- org.apache.tomcat:tomcat-jdbc:jar:8.0.15:compile
[INFO] |  \- org.apache.tomcat:tomcat-juli:jar:8.0.15:compile
[INFO] +- org.springframework.boot:spring-boot-starter-tomcat:jar:1.3.3.RELEASE:compile
[INFO] |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.0.32:compile
[INFO] |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.0.32:compile
[INFO] |  +- org.apache.tomcat.embed:tomcat-embed-logging-juli:jar:8.0.32:compile
[INFO] |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.0.32:compile
[INFO] +- org.activiti:spring-boot-starter-jpa:jar:5.17.0:compile
[INFO] |  \- org.activiti:spring-boot-starter-basic:jar:5.17.0:compile
[INFO] |     +- org.activiti:activiti-engine:jar:5.17.0:compile
[INFO] |     |  +- org.activiti:activiti-bpmn-converter:jar:5.17.0:compile
[INFO] |     |  |  \- org.activiti:activiti-bpmn-model:jar:5.17.0:compile
[INFO] |     |  |     \- com.fasterxml.jackson.core:jackson-databind:jar:2.6.5:compile
[INFO] |     |  |        \- com.fasterxml.jackson.core:jackson-annotations:jar:2.6.5:compile
[INFO] |     |  +- org.activiti:activiti-process-validation:jar:5.17.0:compile
[INFO] |     |  +- org.activiti:activiti-image-generator:jar:5.17.0:compile
[INFO] |     |  +- org.apache.commons:commons-email:jar:1.2:compile
[INFO] |     |  |  \- javax.mail:mail:jar:1.4.1:compile
[INFO] |     |  +- org.apache.commons:commons-lang3:jar:3.3.2:compile
[INFO] |     |  +- org.mybatis:mybatis:jar:3.2.5:compile
[INFO] |     |  \- joda-time:joda-time:jar:2.8.2:compile
[INFO] |     \- org.activiti:activiti-spring:jar:5.17.0:compile
[INFO] |        \- commons-dbcp:commons-dbcp:jar:1.4:compile
[INFO] |           \- commons-pool:commons-pool:jar:1.6:compile
[INFO] +- org.apache.velocity:velocity:jar:1.6.4:compile
[INFO] |  +- commons-collections:commons-collections:jar:3.2.2:compile
[INFO] |  +- commons-lang:commons-lang:jar:2.4:compile
[INFO] |  \- oro:oro:jar:2.0.8:compile
[INFO] +- com.javaslang:javaslang:jar:2.0.0-RC2:compile
[INFO] +- net.sf.ehcache:ehcache-core:jar:2.6.9:compile
[INFO] +- org.apache.poi:poi:jar:3.8:compile
[INFO] |  \- commons-codec:commons-codec:jar:1.5:compile
[INFO] +- org.springframework.boot:spring-boot-starter-mail:jar:1.3.3.RELEASE:compile
[INFO] |  +- org.springframework:spring-context-support:jar:4.2.5.RELEASE:compile
[INFO] |  \- com.sun.mail:javax.mail:jar:1.5.5:compile
[INFO] |     \- javax.activation:activation:jar:1.1:compile
[INFO] +- com.paypal.sdk:rest-api-sdk:jar:1.7.0:compile
[INFO] |  +- org.apache.logging.log4j:log4j-core:jar:2.4.1:compile
[INFO] |  +- com.google.code.gson:gson:jar:2.3.1:compile
[INFO] |  +- javax.servlet:servlet-api:jar:2.5:compile
[INFO] |  +- org.apache.logging.log4j:log4j-slf4j-impl:jar:2.4.1:compile
[INFO] |  \- org.apache.logging.log4j:log4j-api:jar:2.4.1:compile
[INFO] +- com.mandrillapp.wrapper.lutung:lutung:jar:0.0.5:compile
[INFO] |  +- org.apache.httpcomponents:httpclient:jar:4.5.1:compile
[INFO] |  |  \- org.apache.httpcomponents:httpcore:jar:4.4.4:compile
[INFO] |  \- commons-io:commons-io:jar:1.3.2:compile
[INFO] +- org.springframework.boot:spring-boot-devtools:jar:1.3.3.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot:jar:1.3.3.RELEASE:compile
[INFO] |  \- org.springframework.boot:spring-boot-autoconfigure:jar:1.3.3.RELEASE:compile
[INFO] +- org.springframework.security:spring-security-test:jar:4.0.3.RELEASE:test
[INFO] |  +- org.springframework.security:spring-security-core:jar:4.0.3.RELEASE:compile
[INFO] |  \- org.springframework:spring-test:jar:4.2.5.RELEASE:test
[INFO] +- com.jayway.jsonpath:json-path:jar:2.0.0:test
[INFO] |  \- net.minidev:json-smart:jar:2.1.1:test
[INFO] |     \- net.minidev:asm:jar:1.0.2:test
[INFO] |        \- asm:asm:jar:3.3.1:test
[INFO] \- org.springframework.boot:spring-boot-starter-test:jar:1.3.3.RELEASE:test
[INFO]    +- junit:junit:jar:4.12:test
[INFO]    +- org.mockito:mockito-core:jar:1.10.19:test
[INFO]    |  \- org.objenesis:objenesis:jar:2.1:test
[INFO]    +- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO]    \- org.hamcrest:hamcrest-library:jar:1.3:test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.708 s
[INFO] Finished at: 2016-06-28T09:13:33-03:00
[INFO] Final Memory: 25M/437M
[INFO] ------------------------------------------------------------------------

I can see by the dependency tree that the packages that cause the problem are paypals AND spring-security.
When i try to exclude log4j. spring-security stops working. it just cant find  classes
: class path resource [org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.class] cannot be opened because it does not exist
Is there a way o can solve this without having to migrate to log4j ?
Thanks in advance
Comment: Then you are excluding not log4j but a whole lot more... So fix your excludes.
Answer: org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.class

This as nothing to do with excluding log4j ?
How did you excluded it ?
should look something like :
 <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
        <exclusions>
          <exclusion>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
          </exclusion>
        </exclusions>
 </dependency>

Comment: It was paypal rr.... had to exclude a couple of more things after that. Thanks for the help


Adicionar e remover classe quando usa getElementById JavaScript puro

Question: Criei a função para adicionar a classe ativo no ID que foi clicado, mas se eu clicar em outro eu quero que remova a classe do anterior e adicione ativo no que foi clicado atualmente.
Com document.querySelectorAll eu consigo, porque gera um Nodelist, então eu faço um forEach e dá certo, mas com o getElementById eu não estou conseguindo.
function activeClass(event) {
  event.preventDefault();
  const element = event.target;
  const id = element.getAttribute('id');
  const article = document.getElementById(id);

  article.classList.add('ativo');
}

Comment: Cara, eu não entendi sua pergunta, você quer adicionar um id em um elemento quando for clicado nele e depois remover a classe anterior ?
Comment: Essa função compara se há dois ID's iguais na página, se houver ela vai adicionar a classe ativo, mas se eu clicar clicar em outro local eu preciso que remova a classe do anterior e adicione no atual. Consegui explicar?
Comment: Vc sabe que não eh correto ter dois elementos com ID iguais, dois ID com o mesmo nome sempre da problema https://pt.stackoverflow.com/questions/127330/qual-%c3%a9-a-finalidade-das-propriedades-id-e-name-de-uma-tag-html/
Comment: Mas neste meu caso aqui é preciso, pois estou utilizando um elemento para interagir com outro.
Answer: Não podes ter mais que um elemento com o mesmo ID numa página.
Tens de usar classes e podes fazer assim:

function activeClass(clicado) {
  const clicaveis = document.querySelectorAll('.clicavel');
  clicaveis.forEach(el => el.classList.toggle('ativo', el === clicado));
}
.clicavel {
  padding: 5px;
  background-color: light-grey;
  transition: background-color .15s;
}

.ativo {
  background-color: pink;
}
Produto A
Produto B

A ideia é teres os elementos que fazem parte dessa lógica todos com a mesma classe, e depois trocar a classe consoante o que for clicado.
Answer: Você pode usar uma variavel global para guardar o id ativo, ou guardar o proprio elemento, e na hora de setar um novo elemento ativo você remove a class do elemento da variavel e seta essa variavel com o novo elemento..

let ativo;

function activeClass(event) {

  if (ativo !== undefined){
    ativo.classList.remove('ativo');
  }
  ativo = event;

  event.classList.add('ativo');
}

div 1

div 2

div 3

Comment: Eu tentei utilizar esse teu exemplo e não consegui, com o código que postei na conversa tu consegue fazer um exemplo?
Answer: Depois de muito custo eu consegui resolver o problema (do meu jeito).
Segue o código:
const selectMapID = document.querySelectorAll('.map a');
  const selectArticles = document.querySelectorAll('.map article');
  selectArticles[1].classList.add('ativo');

  selectMapID.forEach(item => {
    item.addEventListener('click', showState);
  })

  function showState(event) {
    event.preventDefault();
    const element = event.target;
    const id = element.getAttribute('id');
    const article = document.getElementById(id);

    selectArticles.forEach( (section) => {
      section.classList.remove('ativo');
    } );
    article.classList.add('ativo');
  }

Obrigado a todos pela colaboração!
Comment: Não era mais simples usar a solução que o Sergio propôs? Qual é a ideia de você obter o alvo do evento, pegar o valor do `id` dele para depois buscar o mesmo elemento? No seu código `article` e `element` serão exatamente o mesmo elemento, ficou bem confuso.


Uso de Swingworker em conexão com banco standalone

Question: Tenho um aplicativo em java que fiz no meu trabalho, para controlar sequência de numeração de ofícios, o mesmo fica em formato jar na rede(fiz usando swing  em java + HSQLDB).
Um problema que não chega a ser crítico, mas incomodava um pouco é o fato do banco citado atrasar a abertura do app(provavelmente por estar em uma pasta da rede e este banco carrega todo o arquivo DB na memória antes de abrir), após analisar pelo determinador de perfil do Netbeans, pude chegar a esta conclusão também.
Pesquisei sobre swingworker e então adaptei minha classe fábrica de conexao() para realizar a abertura e fechamento de conexão e recursos em Thread paralela, e isto fez o programa ter uma resposta melhor.
Minha dúvida é a forma como está implementada o paralelismo nesta classe pode gerar algum problema que eu não esteja enxergando neste momento?
Classe factory com swingworker
<code>package com.dfmachado.geroficios.Models;

import com.dfmachado.geroficios.excecoes.ExcecaoErroIO;
import com.dfmachado.geroficios.excecoes.ExcecaoPadrao;
import com.dfmachado.geroficios.utils.Propriedade;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.concurrent.ExecutionException;
import javax.swing.SwingWorker;

/**
 *
 * @author diego
 */
public class ConnectionFactory {

    private static String url = null;
    private static Propriedade prop = null;
    private static boolean openConnWorkerIsDone = false;
    private static boolean closeConnWorkerIsDone = true;

    private ConnectionFactory() {
    }

    public static Connection createConnection() throws SQLException {
        try {
            prop = new Propriedade();
        } catch (IOException ex) {
            throw new ExcecaoErroIO(ex.getMessage());
        }
        url = prop.getJdbcURL() + prop.getDbName();

        try {
            if(closeConnWorkerIsDone)
            return OpenConnWorker(url, prop.getUser(), prop.getUserPass());
        } catch (InterruptedException | ExecutionException ex) {
            throw new ExcecaoPadrao(ex.getMessage());
        }
        return null;
    }

    public static void closeConnection(Connection conn) throws SQLException {
        close(conn, null, null);
    }

    public static void closeConnection(Connection conn, PreparedStatement ps) throws SQLException {
        close(conn, ps, null);
    }

    public static void closeConnection(Connection conn, PreparedStatement ps, ResultSet rs) throws SQLException {
        close(conn, ps, rs);
    }

    private static void close(final Connection conn, final PreparedStatement ps, final ResultSet rs) throws SQLException {
        if (openConnWorkerIsDone) {
            SwingWorker closeConnWorker = new SwingWorker() {
                @Override
                protected Object doInBackground() throws Exception {
                    closeConnWorkerIsDone = false;
                    if (rs != null) {
                        rs.close();
                    }
                    if (ps != null) {
                        ps.close();
                    }
                    if (conn != null) {
                        if (prop.getDbTipo().equals("HSQL")) {
                            conn.prepareStatement("shutdown compact").execute();
                        }
                        conn.close();
                    }
                    return null;
                }

                @Override
                protected void done() {
                    super.done();
                    closeConnWorkerIsDone = true;
                }
            };
            closeConnWorker.execute();
        }
    }

    private static Connection OpenConnWorker(final String url, final String user, final String pass) throws InterruptedException, ExecutionException {
        Connection conn;
        SwingWorker OpenConnWorker = new SwingWorker() {

            @Override
            protected Object doInBackground() throws Exception {
                openConnWorkerIsDone = false;
                return DriverManager.getConnection(url, user, pass);
            }

            @Override
            protected void done() {
                super.done();
                openConnWorkerIsDone = true;
            }
        };
        OpenConnWorker.execute();
        conn = (Connection) OpenConnWorker.get();
        return conn;
    }
}
</code>
Answer: Após analisar alguns tópicos e ler um pouco a documentação e fazer testes, cheguei a conclusão que o uso do SwingWorker não é recomendado para este caso, uma vez que o aplicativo é StandAlone, e o uso de threads só seria refletido no computador de quem o executa no momento, além de esbarrar em uma restrição do HSQLDB, onde conexões StandAlone só só funcionam uma por vez, então, se várias pessoas acessarem ao mesmo tempo, o método singleton só vai funcionar pra máquina de uma por vez.


How to configure Hudson to display code coverage results for MsTest without NCover?

Question: My Hudson already builds and run the MsTest tests in my solution.
I already do see Code Coverage metrics in Visual Studio and I'm generating the coverage file in the build.
I don't have NCover.
How to make Hudson to show the coverage metrics?
Answer: You can look at the list of available Hudson plugins here. Unfortunately, there doesn't seem to be anything to meet your needs unless you use NCover. Is there a reason you can't use it or don't want to?
Comment: Because I would have to buy it. What puzzles me is that it has a SourceForge page, so maybe it has a free version.
Comment: I figured it out. NCover 1.5.8 is free while NCover 3 costs at least $99.
Comment: But the free version doesn't support some features of C# 3 and .NET 3.5
Comment: Depending on your needs and the version of the .NET Framework you are using, you might have to purchase NCover. I don't see this as a bad investment. Just make sure it's what you want. Try the free version for now and see what you can get out of it.
Comment: Actually there is the partcover plugin - which is free and handles .NET2 and .NET4 runtimes (assuming you get the latest one from github).
Comment: @Shaun Wilde: It seems like [this](http://wiki.hudson-ci.org/display/HUDSON/PartCover+Plugin) plugin is still under development.
Comment: Perhaps but the page was last updated Nov 26 2010 - perhaps more progress has been made than actually documented. However PartCover is an alternative to NCover in a build environment such as hudson (and free)
Answer: You can do this by first creating an xml file out of the binary (.coverage) file visual studio creates. Follwoing two links show how to achieve this task. They work like a charm.
http://codetuner.blogspot.com/2011/09/convert-mstest-code-covarage-results-in.html
Merging Visual Studio Code Coverage fails with ImageNotFoundException


How to configure settings.groovy in order to use several repositories?

Question: I just installed Grails 3.3.0 and I'd like to configure some custom repositories in the ${HOME}/user/.grails/settings.groovy file.

This is what I've done so far (Real URLs have been replaced for <someUrl1|2>):
grails {
  profiles {
    repositories {
      repo1 {
        url = "<someUrl1>"
        snapshotsEnabled = true
      }
      repo2 {
        url = "<someUrl2>"
        snapshotsEnabled = true
      }
    }
  }
}

Now, when I execute grails command on bash (Ubutu 16.04) it always tries to resolve the dependencies from the first repository (<someUrl1>)
Java: jdk8u141
Should this configuration be done like I did?
If not, How could I configure this file in order to use more than one repo for grails?
If having connectivity issues (or whatsoever), the first attempt to connect to the first repository fails, Does Grails tray to access the other ones declared?
Answer:
Should this configuration be done like I did?

Yes, according to the documentation

Does Grails tray to access the other ones declared?

Yes, the list of repositories are passed into the constructor of this class.
https://github.com/grails/grails-core/blob/master/grails-shell/src/main/groovy/org/grails/cli/profile/repository/MavenProfileRepository.groovy#L48


Using SELECTEDVALUE in DAX not working correctly

Question: I have written a DAX code which worked perfectly fine, however, I wanted to add another clause within which says that if the row is for a branch type that equals to IN STORE, it should also do the same SUMX over the summarized table, but as you can see from the screenshot of the current output its returning the incorrect result.
totalAverageBaysFinalVersion =
VAR onlyCampaignFiltered =
    ISFILTERED ( 'Campaign Overview'[campaign] )
        && NOT CALCULATE (
            ISCROSSFILTERED ( 'range_plans' ),
            REMOVEFILTERS ( 'Campaign Overview'[campaign] )
        )
            || ISFILTERED ( 'Calendar'[weekNumWeekCommence] )
RETURN
    SWITCH (
        TRUE (),
        SELECTEDVALUE ( 'Campaign Overview'[branch_type] ) = "IN STORE", --Additional Code
            SUMX (
                SUMMARIZE (
                    range_plans,
                    range_plans[branch],
                    range_plans[range_name],
                    range_plans[number_bays]
                ),
                range_plans[number_bays]
            ),
        IF (
            onlyCampaignFiltered,
            SUMX (
                SUMMARIZE (
                    range_plans,
                    range_plans[branch],
                    range_plans[range_name],
                    range_plans[number_bays]
                ),
                range_plans[number_bays]
            ),
            [Total Avg Bays Campaign Overview]
        )
    )

Output:

Expected Output:
Campaign                               Branch Type             Avg.Bays
2023/07/19 - 2023/08/15 Summer 2         IN STORE              7,806.43
2023/07/19 - 2023/08/15 Summer 2          ONLINE                  0

How would I go about checking whether the row is IN STORE and making sure it returns the correct value?
Comment: Could you duplicate the measure (for testing) and replace the `RETURN` with: `RETURN
    SWITCH (
        TRUE (),
        SELECTEDVALUE ( 'Campaign Overview'[branch_type] ) = "IN STORE", "INSTORE",
        onlyCampaignFiltered, "onlyCampaignFiltered",
        "TOT AVG BAYS"
    )` and let us know what results you get?
Comment: for the first row it returned `INSTORE` and the second row returned `TOT AVG BAYS`
Comment: So that shows the `SELECTEDVALUE` is indeed working. In your snip, where is the bottom left card coming from? ("totalRangePlanBays')
Comment: the `totalrangeplanbays` is basically the `SUMX(SUMMARIZE...`, I just put it in a card visual to make sure it was the correct number being shown
Comment: If you select the first row of the table, does the `totalrangeplanbays` card change to `6,597.23` ?
Comment: Yes, it changed to `6,597.23` when I select the first row
Comment: So it's working correctly. If the number isn't correct or as expected, then you need to double down on the `totalRangePlanBays` (SUMX...) calculation and understand how/why that row affects it. Can't offer more help here without seeing your data model.
Comment: Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/255949/discussion-between-jcoke-and-sam-nseir).
Answer: Following on from the comments and chat - the row context is affecting the results you are expecting. See if this works where you REMOVEFILTERS for one of the columns in the table for when "IN STORE"...
totalAverageBaysFinalVersion =
  VAR onlyCampaignFiltered =
    ISFILTERED ( 'Campaign Overview'[campaign] )
    && NOT CALCULATE ( ISCROSSFILTERED ( 'range_plans' ), REMOVEFILTERS ( 'Campaign Overview'[campaign] )  )
    || ISFILTERED ( 'Calendar'[weekNumWeekCommence] )

  RETURN SWITCH( TRUE(),
    SELECTEDVALUE ( 'Campaign Overview'[branch_type] ) = "IN STORE", [totalRangePlanBays]( REMOVEFILTERS ( 'Campaign Overview'[branch] ) ),
    onlyCampaignFiltered, [totalRangePlanBays],
    [Total Avg Bays Campaign Overview]
  )


Global variables vs database access, which one is better?

Question: I read a lot about how evil global variables are in PHP, but I am trying to optimize a code I am writing. In this webapp a lot of functions are using the same data (about up to 50 items at once) to perform numerous operations and the data itself is stored in a database.
I have two options which are a) fetching data from the database EVERY TIME a function needs it or b) fetching the data ONCE and storing it in (a) global variable(s).
When it comes to performance, which option is the best ?
Answer:
There is nothing wrong with "global variables". It is passing data into functions using global keyword is prohibited (but nevertheless, using this keyword to pass indeed global variable is okay).
Yes, speaking of one script instance (and sane amount of data), there is no use for reaching for database for the same data again. Fetch it once and then use in whatever functions you need. It's okay and nothing wrong with it.
When it comes to performance, here comes the best option ever:

do care of performance only if you have a certain reason to.


Acessar site no celular sem estar conectado a rede

Question: Fiz um site em HTML5 e coloquei na IDE chamada NetBeans executei o projeto e ele veio com a seguinte url: http://localhost:8383/tecmov/index.html.
Através dela eu posso acessar meu projeto de outra máquina desde que ela esteja na mesma rede, porém estou na empresa e meu projeto está sendo executado no computador da empresa, em uma rede diferente do meu celular.
Queria acessar esse meu site do meu celular Android sem me conectar à rede da empresa. Isso é possível? Via cabo USB, por exemplo, pois não sei uma forma de fazer isso...
Answer: Vamos por partes.
Sim, é possível acessar este site Offline, desde que você copie todo o projeto para dentro do seu cartão SD do celular e este seja desenvolvido em HTML, pois o celular possui o interpretador HTML, ou seja, você acessara o site sem acesso a internet, porém toda vez que atualizar algo no site no seu computador terá de copiar os novos arquivos e paginas para o SD.
Para acessar ele sem fazer o que falei acima, você precisara pedir ao Administrador da rede de seu trabalho que ingresse seu Smartphone ao domínio, para isso ele ingressara o IP ou Endereço MAC do seu aparelho na rede ou lhe dará acesso a rede WiFi da empresa (caso possua).
Eu utilizo este mesmo esquema, para ver como meu site ficaria em um celular, ele esta no meu Micro e acesso ele via WiFi no meu celular, para isso Eu logo an rede WiFi com o mesmo usuário e  senha que uso para ligar meu micro, é um WiFi privado.
Comment: devo salvar em alguma parte especifica do celular ?
Comment: @Kirito não, mas fica mais organizado se você criar uma pasta no cartão SD, salvar la e depois criar um atalho na tela principal para a pagina index.html ficara mais facil acessar.


Users can still access MS Exchange emails after disabling user account

Question: I have an on premise Exchange 2016 Server and I found that after disabling a users account the user is still able to send and receive emails from his / her mobile device hours after the account has been disabled. Is that a what to prevent this?
Answer: The problem is probably with ActiveSync.  If you disable the users account you should remove their device from the ActiveSync to their mailbox.  The ActiveSync can keep a connection open for hours before re-authenticating.
Below are two links describing what you are experiencing.
https://support.microsoft.com/en-in/help/2612821/eas-devices-still-sync-after-an-account-is-disabled-or-a-password-is-c
https://social.technet.microsoft.com/wiki/contents/articles/3406.exchange-best-practices-for-untrusted-mailbox-users.aspx


Calling .net4 from SSIS

Question: We've done some work on a .net 4.0 project, and are now looing into using SSIS for some tasks. As SSIS is not supported in VS2010, as far as I can see, I'm sort of ok with using VS2008 for SSIS. But how can I call exsiting .net4 code from SSIS at best running on .net 3.5?
Do I have to wrap my c# code to a web service or are there other options?
Answer: The version of .NET for an application depends on the main executable. If you use DTEXEC to run SSIS, you get .NET 3.5 because DTEXEC.Exe is configured to use .NET 3.5.
But if you build your own application (a simple console app with functionality similar to DTEXEC) with .NET 4.0 - you get .NET 4.0.
Finally, there is of course a hack to change DTEXEC to use .NET 4.0 - it is controlled by DTEXEC.Exe.Config, which you can edit, but most likely this is unsupported by Microsoft and thus I would recommend against it.

Calling .net4 from SSIS


Perl Modules "run" command

Question: I'm using the Perl tool Cluster SSH tool (cssh).
In the main part this code:

    use App::ClusterSSH;

    my $app = App::ClusterSSH->new();

    $app->options->add_common_ssh_options;
    $app->options->add_common_session_options;
    $app->run();

There is this directory for perl modules of this tool:
/usr/lib/perl5/vendor_perl/5.26.1/App/ClusterSSH
An ls of this directory shows:
   Base.pm  Cluster.pm  Config.pm  Getopt.pm  Helper.pm  Helper.pm.orig  Host.pm  L10N  L10N.pm  Range.pm  Window  Window.pm
   ./L10N:
   en.pm
   ./Window:
   Tk.pm

Question

I cannot find a subroutine for "run" in the perl modules for the tool. If there is no sub called "run" what does the command do:
$app->run()

I do not understand what this means:
$app->options->add_common_ssh_options;

There is an sub in Base.pm called options
There is an sub called add_common_ssh_options in another file called Getopt.pm
I think that:
$app->options

would call the sub options but what does the additional arrow function do?
Answer: There's a subroutine named run in App::ClusterSSH.
The $app->options->add_common_ssh_options is you calling the method options on $app, then calling the method add_common_ssh_options on the return value from options. This is typically called "method chaining". It's the same as doing it in two statements:
my $options = $app->options;
$options->add_common_ssh_options;

Perl Modules "run" command


Not able to map array of objects in javaScript

Question: How do i map the array of object below where i want to display both Module1 and its sub modules?
const data = [
    {
        module1 : ["submod1", "submod2"]
    },
    {
        module2 : ["submod3", "submod4"]
    }
]

I also want to pass "module1" and its array as a prop to other component.
Expected Output
Comment: What is the expected result?
Comment: what the output looks like?
Comment: I've added expected output as image at the end of description. please check
Answer: To extract module1:
const module1Array = data[0].module1.map(sm=> {
        return sm
    }
     );

To get all submodule arrays in a new array:
const modulesArray = data.map(m => {
    return Object.values(m)[0];
})

Why not just pass in data as a prop and map over it in your component? ->
const Example = (data) => {

    const modules = data.map(m => {
            return (

{Object.keys(m)}

{Object.values(m)}

)
        })

     return (

                {modules}

        )
    }

export default Example

Comment: i also want to return module1 and module2. your code is very specific to module 1's subModules
Comment: You should be more specifc with your expected output. The picture your provide already looks like the structure of 'data'. I edited my post to inculde a way to get all sub moudle arrays in a new array.
Comment: thanks for your help. but what if i also want to return "module1" along with its subModules. i want to pass main module("Module1") and its sub modules(submod1,submod2) both as separate props to a component. How can i do that?


How to convert a text list of files into an xml format? (Applescript)

Question: I have this applescript that copies a filelist to the clipboard.
The list contains filepaths for a folder's contents separated by a line break.
I was looking for an alternative to Whereisit, and found this script as a temporary solution.
Presently the file list appears in this format:
/path/to/file1
/path/to/file2
/path/to/file3

I'm trying to get the file list in xml format, with more attributes to the file included. Something similar to the following format:
<items>
<item>
<filename>file1.mp3</filename>
<filepath>/path/to/the/file/</filepath>
<tracklength>3:00</tracklength>
<filesize>6mb</filesize>
<artist>artist name</artist>
<title>Song Title</title>
<album>Album Name</album>
</item>
</items>

Here's the Applescript:
property script_name : "List Folder Contents"
property script_description : "This script will list a folder's contents returning full paths as strings and limit the list to specific file types. The script can also process subfolders (recursion)."

set the_folder to (choose folder with prompt "Choose a Folder to List:") as Unicode text
set file_types to {} --file types, set to {} and inc_folders to true to just return folders; file types are 4 character codes such as "osas" or "TEXT"

set with_subfolders to my get_boolean("Search subfolders?", {"No", "Yes"})
if with_subfolders = "user cancelled" then return

set inc_folders to my get_boolean("Include folders in the list? (If you only want file names, and not folder names, select \"No\".)", {"No", "Yes"})
if inc_folders = "user cancelled" then return

set use_posix_path to my get_boolean("Return the paths as HFS (Mac-style) or POSIX (UNIX-style)?", {"HFS", "POSIX"})
if use_posix_path = "user cancelled" then return

set return_as_string to my get_boolean("Return the results as an AppleScript list or a string with each file on its own line?", {"List", "String"})
if return_as_string = "user cancelled" then return

set add_to_clipboard to my get_boolean("Copy the results to the clipboard?", {"No", "Yes"})
if add_to_clipboard = "user cancelled" then return

set the_files to get_folder_list(the_folder, file_types, with_subfolders, inc_folders, use_posix_path)

if return_as_string then
    tell (a reference to my text item delimiters)
        set {old_tid, contents} to {contents, return}
        set {the_files_string, contents} to {the_files as Unicode text, old_tid}
    end tell
    copy the_files_string to the_files
end if

if add_to_clipboard then
    if not return_as_string then
        copy the_files to the_files_string
        repeat with i from 1 to (count the_files_string)
            set item i of the_files_string to ("\"" & item i of the_files_string & "\", ")
        end repeat
        set the_files_string to ("{" & (text 1 thru -3 of (the_files_string as Unicode text)) & "}")
    end if
    set the clipboard to the_files_string
end if
beep
return the_files

on get_folder_list(the_folder, file_types, with_subfolders, inc_folders, use_posix_path)
    set the_files to {}
    tell application "Finder" to set folder_list to items of folder the_folder
    repeat with new_file in folder_list
        try
            set temp_file_type to file type of new_file
        on error
            set temp_file_type to "fold"
        end try
        if file_types contains temp_file_type or file_types = {} then
            if use_posix_path then
                set the_files to the_files & {POSIX path of (new_file as Unicode text)}
            else
                set the_files to the_files & {new_file as Unicode text}
            end if
        end if
        if temp_file_type = "fold" then
            if inc_folders and file_types ≠ {} then
                if use_posix_path then
                    set the_files to the_files & {POSIX path of (new_file as Unicode text)}
                else
                    set the_files to the_files & {new_file as Unicode text}
                end if
            end if
            if with_subfolders then set the_files to the_files & my get_folder_list((new_file as Unicode text), file_types, with_subfolders, inc_folders, use_posix_path)
        end if
    end repeat
    return the_files
end get_folder_list

on get_boolean(m, b)
    try
        return (button returned of (display dialog m buttons ({"Cancel"} & b) default button 3 with icon 1) = (b's item 2))
    on error
        return "user cancelled"
    end try
end get_boolean

I'm new to Applescript, and therefore not sure how to edit the above applescript for xml output. Maybe in the if add_to_clipboard then code some changes could be done to the following to lines:
`set item i of the_files_string to ("\"" & item i of the_files_string & "\", ")`

`set the_files_string to ("{" & (text 1 thru -3 of (the_files_string as Unicode text)) & "}")`

Comment: Nope! I found a Virtual OS software, so got a windows software for an alternative.
Answer: Try following recursive code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace ConsoleApplication19
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        const string FOLDER = @"c:\temp";
        static XmlWriter writer = null;
        static void Main(string[] args)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;

            writer = XmlWriter.Create(FILENAME, settings);
            writer.WriteStartDocument(true);

            DirectoryInfo info = new DirectoryInfo(FOLDER);
            WriteTree(info);

            writer.WriteEndDocument();
            writer.Flush();
            writer.Close();

        }
        static long WriteTree(DirectoryInfo info)
        {
            long size = 0;
            writer.WriteStartElement("Folder");
            try
            {
                writer.WriteAttributeString("name", info.Name);
                writer.WriteAttributeString("numberSubFolders", info.GetDirectories().Count().ToString());
                writer.WriteAttributeString("numberFiles", info.GetFiles().Count().ToString());
                writer.WriteAttributeString("date", info.LastWriteTime.ToString());

                foreach (DirectoryInfo childInfo in info.GetDirectories())
                {
                    size += WriteTree(childInfo);
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception Folder : {0}, Error : {1}", info.FullName, ex.Message);
            }

            FileInfo[] fileInfo = null;
            try
            {
                fileInfo = info.GetFiles();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception FileInfo : {0}, Error : {1}", info.FullName, ex.Message);
            }

            if (fileInfo != null)
            {
                foreach (FileInfo finfo in fileInfo)
                {
                    try
                    {
                        writer.WriteStartElement("File");
                        writer.WriteAttributeString("name", finfo.Name);
                        writer.WriteAttributeString("size", finfo.Length.ToString());
                        writer.WriteAttributeString("date", info.LastWriteTime.ToString());
                        writer.WriteEndElement();
                        size += finfo.Length;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Exception File : {0}, Error : {1}", finfo.FullName, ex.Message);
                    }
                }
            }

            writer.WriteElementString("size", size.ToString());
            writer.WriteEndElement();
            return size;

        }
    }
}

Comment: Sorry.  It is c#.  Didn't look at tags.
Comment: What language is this? Java?
Comment: ok. thanks! Was hoping someone could add additional code to my post so that an xml could be generated. Thanks anyways!


Unir duas consultas no SQL Server

Question: Olá, tenho duas consultas da mesma tabela, porém não estou conseguindo uni-las paralelamente, já tentei usar o UNION, mas não funcionou, há algum modo de fazer isso? Ou então criar uma coluna com a diferença acumulada (Backlog) entre os totais das duas tabelas?

SELECT
    MONTH(created) AS 'MES'
   ,YEAR(created) AS 'ANO'
   ,COUNT(Number) AS 'TOTAL'
FROM [ServiceNow VR01].[dbo].[incident]
    WHERE created > '2013-01-01'
    GROUP BY YEAR(created), MONTH(created)
    ORDER BY ANO, MES

SELECT
    MONTH([Closed AT]) AS 'MES'
   ,YEAR([Closed AT]) AS 'ANO'
   ,COUNT(Number) AS 'TOTAL'
FROM [ServiceNow VR01].[dbo].[incident]
    WHERE [Closed AT] > '2013-01-01'
    GROUP BY HONTH([Closed AT]), YEAR([Closed AT])
    ORDER BY ANO, MES

Tipo assim:
 Total C  Total F   Balance    Backlog
 1420   - 424    =  996        996
 18433  - 9480   =  8953       9949
 26500  - 30137  = -3637       6312

Obs: Estou usando o SQL Server 2012 e as duas tabelas tem a mesma quantidade de linhas.
Comment: Fala Thiago, isto te ajudaria? [Alternative to UNION clause](https://stackoverflow.com/questions/49535872/alternative-to-union-clause)
Comment: Olá viva, de forma a poder ajudar-te, explica melhor o que queres, na query que apresentas o output será MES, ANO e TOTAL, e a tabela de resultado queres algo 'Tipo assim:' Total C, Total F, Balance e Backlog, adicionalmente se colocares na plataforma sqlfiddle um exemplo da tua tabela real com alguns dos dados juntamente com a tua query base, ajuda bastante e será mais fácil seres ajudado...  plataforma: (http://sqlfiddle.com).
Comment: Pelo que consegui entender me parece que uma INNER JOIN com mês e ano atenderia ao que deseja. Se nem sempre ocorrem em ambas as tabelas talvez uma OUTER JOIN seja melhor.
Answer: Pelo que entendi um join usando os dois sql como tabelas virtuais , de resto é manipular os campos alinhados.
SELECT *
FROM
( SELECT MONTH(created) AS 'MES' ,
 YEAR(created) AS 'ANO' ,
 COUNT(Number) AS 'TOTAL'
 FROM [ServiceNow VR01].[dbo].[incident]
 WHERE created > '2013-01-01'
 GROUP BY YEAR(created),
 MONTH(created)) TC,
 (SELECT MONTH([Closed AT]) AS 'MES' ,
  YEAR([Closed AT]) AS 'ANO' ,
  COUNT(Number) AS 'TOTAL'
  FROM [ServiceNow VR01].[dbo].[incident]
  WHERE [Closed AT] > '2013-01-01'
  GROUP BY HONTH([Closed AT]),
  YEAR([Closed AT])) TS
WHERE TC.ANO = TS.ANO
AND TC.MES = TS.MES
ORDER BY TC.ANO,TC.MES

Comment: Obrigado, resolveu perfeitamente.

Unir duas consultas no SQL Server


Get the sub string between two bracket in ruby on rails

Question: I have following strings:
1) Beach Cottage (CP)
2) Beach Cottage (AP)
3) Cotta (GAP)

And I want to get substing between ( ) that is from first CP
Comment: We expect you to show us what code you've written, or at least citing where you've searched and read when trying to solve the problem. Showing you've made an effort is important; we don't expect you to know how to do these things but we do want to see that you've tried. "Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results."
Answer: Try this for example:
str = "Beach Cottage (CP)"
str.match(/(\((.*)\))/)[2]

Answer: you can use scan also with regx:
str = "Beach Cottage (CP)"
needed_sub_str = str.scan(/\((.*)\)/)

puts "expected sub string :: #{needed_sub_str}"

Output ::
expected sub string :: CP


Seeing ORA-01858: a non-numeric character was found where a numeric was expected

Question: The following is my query for Oracle SQL Developer:
INSERT INTO ENROLLMENTS (DATE,PARTNER_NAME,ENROLLMENTS)
    SELECT TO_CHAR(TS, 'DD-MON-YYYY HH AM') AS DATE, mrch_bnft_cd, COUNT(*)
    FROM ENROLLMENTS
    WHERE TS > trunc(sysdate-1/24, 'HH') + 5/24
    AND TS < trunc(sysdate, 'HH') + 5/24
    GROUP BY TO_CHAR(TS, 'DD-MON-YYYY HH AM'), mrch
    ORDER BY TO_CHAR(TS, 'DD-MON-YYYY HH AM'), mrch_bnft

I get an error

ORA-01858: a non-numeric character was found where a numeric was expected error

but the error is not telling me where. Any ideas?
DATE = TIMESTAMP(6)
TS = TIMESTAMP(6)
PARTNER = VARCHAR2(35 BYTE)
ENROLLMENTS = NUMBER
MRCH_BNFT= VARCHAR2(35 BYTE)

Comment: @AlexPoole   - In the words of the famous philosopher, Homer Simpson, "DOH!"  :-)
Comment: whats is datatype of DATE, TS fields ?
Comment: Can you specify the data types of `DATE,PARTNER_NAME,ENROLLMENTS` and `TS,MRCH_BNFT_CD`?
Comment: @Andrew i updated the post
Comment: @ThomasStrub i updated the post
Comment: The "into" of 'SELECT _INTO_'  is a PL/SQL language construct, not a SQL syntax.  Unless this is a snippet extracted from a PL/SQL block, you need to get rid of the 'INTO'.  And if it _is_ a snippet extracted from a PL/SQL block, you should have shown it in context of that PL/SQL.
Comment: @EdStevens - this is an `insert into`, not a `select into` ?
Comment: @fsdff - it would be helpful if you did a `describe enrollments` and included that and your actual statement in your question; as `DATE` isn't a legal column name (unless using a quoted identifier, which isn't shown) and you seem to have change other column name inconsistently, this can't be exactly what you are doing. It's possible the issue is in something you've changed, or haven't shown at all.
Answer: Basically you are trying to store a string which represents a date in a timestamp field.
If you replace:
TO_CHAR(TS, 'DD-MON-YYYY HH AM')

by
TRUNC(TS, 'hh24')

at all 4 places it should work.
INSERT INTO ENROLLMENTS (DATE,PARTNER_NAME,ENROLLMENTS)
    SELECT   TRUNC(TS, 'hh24') AS DATE, mrch_bnft_cd, COUNT(*)
    FROM     ENROLLMENTS
    WHERE    TS > trunc(sysdate-1/24, 'HH') + 5/24
    AND      TS < trunc(sysdate, 'HH') + 5/24
    GROUP BY TRUNC(TS, 'hh24'), mrch
    ORDER BY TRUNC(TS, 'hh24'), mrch_bnft

Comment: @fsdff - did you only change it to `to_char(ts, 'hh24')` instead of `trunc(ts, 'hh24')`? Though that only makes sense if your DATE column is actually a string, not the data type you said.
Comment: I need the actual date, this returns the hour 14 which is correct, but I still need the actual date.
Comment: So technically it's working now? But there is a logical error?
Comment: @no its displaying 14 without a date. its the right hour, but the right hour already existed in the first query.
Comment: Where is it displaying 14 without a date?
Comment: in DATE value field @Thomas Strub
Comment: You have written that the data type of the field `date` is timestamp(6) so a select should result a complete timestamp ...


jquery validate - validating a field on pageload

Question: I've got an input field on my plain html form that I would like to be marked as invalid (and the error message shown) from the moment the page loads.
Is this possible to do with the jquery validate plugin?
Failing that, how can I hide an element when the plugin tries to validate a particular input field?
Answer: I think you would have to have the form you are validating be submit on the page load.  Probably something like this:
<code>   $(document).ready(function() {
       $('form').validate(/* set all of your validator options here */);
       $('form').trigger('submit');
    });
</code>
Comment: I did consider this but that will trigger all of my other validation. I'm only interested in triggering the validation error message for 1 input.
Comment: I have never used the single element validation (http://docs.jquery.com/Plugins/Validation/Validator/element#element) method of this plugin, but it might be an easier solution than having to create the dummy element.
Answer: Managed to come up with a solution. It involved adding a 'dummy' validation element that is shown when the page is loaded. As soon as the user inputs a value into the corresponding field, the dummy element is hidden and the real one is shown.
I used the highlight and unhighlight options for the validate method to achieve what i wanted.
<code>$("#myform").validate({
    highlight: function(element){
        if($(element).hasClass("math")){
           $(".temp").hide();
        }
    },

    unhighlight: function(element){
        if($(element).hasClass("math")){
           $(".temp").hide();
        }
    }
});
</code>
Maybe this will help someone else out in future.


sum numeric variable six months ago

Question: i have a data base with dates and numeric variables. I also have multiple rows per id. It looks like this:

ID
date
x

1
2019-01-01
3

1
2018-12-01
4

1
2017-11-01
1

1
2017-10-01
2

1
2017-09-01
2

1
2017-08-01
2

I need to sum x up to six month ago from date, so i tried this
library(lubridate)
    mutate(semester= semester(fecha_inicio,with_year = TRUE)) %>%
  group_by(ID,semester) %>%
  mutate(sum_semester = sum(x, na.rm = TRUE))

but is not what i need because 2019-01-01 have 3 instead of 14.
Please help.
Comment: What are you piping to `mutate`? It's the data, yes, but it's missing from the code.
Comment: Just use `summarise` instead of `mutate`.
Answer: I found the answer here Cumulative sum from a month ago until the current day for all the rows
adapting the code:
library(tidyverse)
library(lubridate)
data <- data %>%
  group_by(ID) %>%
  mutate(sum_6m = map_dbl(1:n(), ~ sum(x[(date>= (date[.] - months(5))) &
                                                   (date<= date[.])], na.rm = TRUE)))

Comment: I think your code is not right.
Answer: Using the classic way to aggregate datasets x ~ id by a function like sum with a normal data.frame filter it could be the following code.
Code
library(lubridate)

# Define your data
data <-
"id date x
1 2019-01-01 3
1 2018-12-01 4
1 2017-11-01 1
1 2017-10-01 2
1 2018-02-12 2
1 2017-09-01 2
"
# Read the table
tab <- read.csv(text=data, header = T, sep=' ')

# Find the youngest date
top.date <- as.Date(max(tab$date))

# Calculate the threshold (before and after point) of 6 month
thresh   <- top.date) %m-% months(6)

# Calculate the sums over the ID's after the point date
after.thresh  <- aggregate(x ~ id,
                           data = tab[as.Date(tab$date) >= thresh,],
                           FUN  = sum)

# Calculate the sums over the ID's before the point date
before.thresh <- aggregate(x ~ id,
                           data = tab[as.Date(tab$date) < thresh,],
                           FUN=sum)

# Print the dates
cat("TOP.DATE.IS:", format_ISO8601(top.date),
    " THRESH.DATE.IS:", format_ISO8601(thresh),"\n")

# Print the sums
cat("SUM.BEFORE.THRESH:", after.thresh$x,
    "SUM.AFTER.THRESH:", before.thresh$x,"\n")

Results
TOP.DATE.IS: 2019-01-01  THRESH.DATE.IS: 2018-07-01
SUM.BEFORE.THRESH: 7 SUM.AFTER.THRESH: 7

sum numeric variable six months ago


Single classification neuron/perceptron in R

Question: I am looking for a package that provides a single neuron/perceptron for classification in R. So far I only found multi-layer perceptrons. Where can I find one?
Answer: Logistic regression is very close to "single neuron/perceptron for classification in R".
See here for details.
What's the difference between logistic regression and perceptron?

If you want to try logistic regression, here is an example using R glm function.
> summary(glm(am~wt+hp,mtcars,family="binomial"))

Call:
glm(formula = am ~ wt + hp, family = "binomial", data = mtcars)

Deviance Residuals:
    Min       1Q   Median       3Q      Max
-2.2537  -0.1568  -0.0168   0.1543   1.3449

Coefficients:
            Estimate Std. Error z value Pr(>|z|)
(Intercept) 18.86630    7.44356   2.535  0.01126 *
wt          -8.08348    3.06868  -2.634  0.00843 **
hp           0.03626    0.01773   2.044  0.04091 *
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 43.230  on 31  degrees of freedom
Residual deviance: 10.059  on 29  degrees of freedom
AIC: 16.059

Number of Fisher Scoring iterations: 8

Answer: You can use the R-specific search-engine http://rseek.org/ to find a package that implements a Perceptron. The example shown on https://rpubs.com/SHIYU/perceptron is particularly helpful.


Interpret the multi linear regression graph

Question:
Residual fitted
normal Q
scale location
residual leverage graph.

Not able to interpret what it means.
Comment: Welcome to StackOverflow. Please take the [tour](http://stackoverflow.com/tour) have a look around, and read through the [HELP center](http://stackoverflow.com/help), then read [How to Ask Question](http://stackoverflow.com/help/how-to-ask), [What types of questions should I avoid asking?](http://stackoverflow.com/help/dont-ask) and provide a [MCVE : Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). If people around can easily read and understand what you mean, or what the problem is, they'll be more likely willing to help :)
Comment: From the first plot, it appears that you have curvature that you are not accounting for in the model. Perhaps a quadratic term for loan_id or status would be warranted?
Comment: What are the variables? Predicting one identifier from another is a puzzling problem, if that is what the names mean. Also, is either of the *id variables necessarily positive? (Status I guess wildly is an indicator or categorical variable.)
Answer: The most obvious and important message is on the top left. When the predicted values (on the x axis) are larger than 40, the residuals are near zero, which is good. When they are smaller than 40, the residuals are systematically negative, meaning, these predictions are too high. Your simple linear model is not good for small values and you should seek to make your model a bit more complex, so smaller results become more accurate.
If you have an idea, what this means in terms of your question, try to model that idea. If not, try a quadratic term as @mkt said in the comments - or any other transformation (exponential, logarithmic, ...) that you have been taught.


Log posterior function in PYMC

Question: my question concerns the logp function in the PYMC package in Python.
Ultimately I want to calculate a quantity that goes by many names, namely the Bayes-factor/ evidence/ marginal-likelihood of the model, but perhaps first demystifying logp will get me closer.
Let us assume that I have instantiated the model below
mc = MCMC(myModel)

and have then done my sampling.
What does calling the logp exactly return?
myModel.logp

Is it the log-posterior for the last sample? and if so, how can I access the log-posterior value for other samples generated by the MCMC process?
(Any hints on how to use this for calculating the marginal likelihood welcome!!!)
Thank you all for your time,
N.
Answer: Calling myModel.logp returns the log probability of the model at the current parameter setting. So if you ran MAP() it would be the logp at the MAP, if you sampled, it would be the logp at the last sampled parameter value. Thus, these are not the values you're looking for :).
Unfortunately, computing the marginal probability of a model is pretty difficult because you have to integrate out all parameters. Often, this can only be done analytically (which works for certain conjugate models).
There are a few ways to approximate this from the trace of a model using the harmonic mean, but there are some serious issues with this approach. See Radford Neal's blog post for a good description of the issues and the comments section for ideas on how to remedy them.
Personally, I think Bayes Factors for model comparison are more trouble than they are worth. I often use the DIC, despite it's limitations, or posterior predictive checks.
Comment: Thanks twiecki, you confirm my suspicion about _logp_.  I am aware that map estimation is a poor man's Bayes estimation as people say. I will try to overcome the problem in some other way... cheers.

Log posterior function in PYMC


Is there a way to sing actual clear, nice two notes? (Polyphonic overtone singing)

Question: So I found this two video

On polyphonic overtone singing,
I'm just wondering is there a way to actually sing good two notes that doesn't sound like a weird, creepy frog? (I'm just curious..)
Comment: Get two people.
Answer: No. We only have one vocal apparatus each and the apparatus can only truly resonate at one pitch as a time.


How can I show the inputAccessoryView in this view controller

Question: I have a function in my controller I call
private func toggleLauncher() {
    let launcher = CommentsLauncher()
    launcher.showLauncher()
}

This essentially adds a view on top of the current view, with a semi transparent background.
I'd like to then render a custom inputAccessoryView at the bottom of the newly added view.
class CommentsLauncher: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor(white: 0, alpha: 0.5)
    }

    func showLauncher() {
         if let window = UIApplication.shared.keyWindow {
            window.addSubview(view)
        }
    }

    override var inputAccessoryView: UIView? {
        get {
            let containerView = UIView()
            containerView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 50)
            containerView.backgroundColor = .purple
            return containerView
        }
    }

    override var canBecomeFirstResponder: Bool {
        return true
    }
}

All that happens though is the semi transparent background is visible but I see no inputAccessoryView added to the view also and I am unsure why.
Answer: Your CommentsLauncher never become the first responder in the code you provided. A UIResponder's inputAccessoryView is displayed when the responder becomes first responder.
Change your showLauncher method to something like this:
func showLauncher() {
    if let window = UIApplication.shared.keyWindow {
        window.addSubview(view)
        becomeFirstResponder()
    }
}

And you should see the input accessory view.
Comment: Ah! Yes! That would make complete sense! Thank you so much :)


Range of $R^2$ of the model with two predictors given the $R^2$s of univariate models of each predictor

Question: We have a dependent variable $Y$ and two predictors $X_1,X_2$.
Given a fixed dataset, $R^2$ of the model $Y$~$X_1=t_1$ and
$R^2$ of the model $Y$~$X_2=t_2$.
What is the range of $R^2$ of the model $Y$~$X_1+X_2$?
My attempt
I tried to use the fact that $R^2$ of the model with a single predictor is $(correlation)^2$ but that didn't help me.
Also, I feel that if the predictors are fully correlated, then the $R^2$ model with two predictors shouldn't change. However, we don't have $t_1=t_2$, so we can't have $1$ or $-1$ correlation.
If the predictors are orthogonal, then I can see that the coefficients of the predictors in the joint model is the same as their coeff in their univariate model(by basic matrix calculations).
Note that Feel free to answer different versions of the problem like models with the intercepts.
Comment: This is the model notation used in R, maybe also elsewhere. Not sure everybody understands this. Proper model notation would be, if I understand correctly: Model 1 is $Y=b_0+b_1X_1+E$, its $R^2$ is $t_1$. Model 2 is $Y=c_0+c_2X_2+F$, its $R^2$ is $t_2$, $E$ and $F$ error terms, probably assumed i.i.d. normal. Estimation is by least squares. The question is what is the possible range of $R^2$-values for the model $Y=a_0+a_1X_1+a_2X_2+G$? I'm also assuming you are asking this for a given dataset, not in general theoretically? But I'm not sure. (Also not sure how much of a difference that makes.)
Comment: Sorry about unclear definitions. It is for a given dataset. I asked without an intercept but I am also curious about the model with the intercept
Comment: I'd think for the answer it doesn't make a difference whether there is an intercept or not.
Answer: The range is $[max(t_1, t_2),\ min(t_1+t_2, 1)]$.
Of course you can't obtain a $R^2$ lesser than $t_1$ or $t_2$, because $R^2$ always increases when you add variables. Also, you can't have any $R^2 > 1$, but there's more to it, let's see...
In order to compute the value of $R^2$, you must decompose the total sum of squares of $Y$: $SS_Y$. This amount behaves like a pie chart, or maybe more like a Venn diagram: part of $SS_Y$ is explained by $X_1$, part of it is explained by $X_2$, and part of it by neither of them. Also, the parts explained by $X_1$ and $X_2$ likely overlap (if they don't, it means that $X_1 \perp X_2$). If you don't know how much is this overlap, you can tell that the final $R^2$ won't be greater than $t_1+t_2$ anyway.
It is possible that the part of $SS_Y$ explained by one variable is totally included in the part explained by the other, so the resulting $R^2$ will be no higher than $max(t_1, t_2)$. For this to happen the predictor that's less correlated to $Y$ must also be orthogonal to the subspace generated by the other predictor and $Y$.
Why is this
If you are not good with thinking of $SS_Y$ as a pie being divided among explanatory variables, you must know that this happens because least squares regression maps a target variable $Y$ to its projection on the subspace generated by the matrix $X$. You can get some context in this lesson I've found with some quick googling. The explained sum of squares $SSM$ is the square euclidean norm of this projected vector, and as you add another vector to it, the resulting squared norm will be the sum of the squared norm of the addends, as long as they are orthogonal. Add this to what I've said before and you will figure out the passages.
Comment: To be really, really picky R^2 does not "always increase" as it may stay the same, as the rest of your answer implies.
Comment: I understand the answer but I am still curious to see if there is a more rigorous way of solving the problem. Also, how does the existence of the intercept change the answer?
Comment: @mdewey rigth, I hope is already clear enough
Comment: @arke the intercept doesn't change the answer that much, you can show that centering all the variables and then taking it off results in the same model, but it also makes the following steps easier, as in my answer


$AB^H = \sum a_i b_i^H$?

Question: Let $A \in \mathbb C^{m \times n}$. There exists a singular value decomposition such that $A = U\Sigma V^H$, where $U = (u_1, \cdots, u_m)$ and $V = (v_1, \cdots, v_n)$. Suppose that $m \geq n$. According to here: https://www.cs.utexas.edu/users/flame/laff/alaff/chapter02-the-reduced-svd.html
$A = U\Sigma V^H = \sum_{i=1}^n \sigma^i u_i v^H_i$.
Algebraically, this makes sense since $A$ is just a matrix that maps each $v_i \mapsto \sigma^i u_i$, and every $x \in \mathbb C^m$ can be represented as $x = \sum \langle x, v_i\rangle v_i$. Hence, $Ax = A(\sum \langle x, v_i\rangle v_i) = \sum \sigma^i \langle x, v_i\rangle u_i$. Note that in $(\sum_{i=1}^n \sigma^i u_i v^H_i)$, the expression $v^H_i$ is just equivalent to $\langle \cdot, v^H_i\rangle$.
However, I just cannot figure out in matrix-multiplication sense that $U \Sigma V^H = \sum_{i=1}^n \sigma^i u_i v^H_i$. If $A,B$ are matrices in $\mathbb R^{k \times k}$ in general, is it generally true that $AB = \sum a_i b^i$, where $a_i$ is $i$-th col of $A$ and $b^i$ is $i$-th row of $B$? (sorry for bad notation, I don't mean $a_i b^i$ to be a tensor product with Einstein notation. It is merely a $\mathbb R^{n \times n}$ matrix)
===================edit====================
After some work, I think $AB = \sum a_i b^i$ is true. Let
\begin{equation*} \hat a_1:= \begin{pmatrix} \mid & \mid & \cdots & \mid\\
a_1 & 0 & \cdots & 0 \\
\mid & \mid & \cdots & \mid\end{pmatrix}, \end{equation*}
and define similarly for $\hat a_i$ for other $i$'s.
I may define
\begin{equation*}
\hat b^1 = \begin{pmatrix}
- & b^1 & - \\
- & 0 & - \\
&\vdots& \\
- & 0 & -
\end{pmatrix}
\end{equation*}
and define similarly for all other $\hat b^j$'s.
Then, $AB = (\sum \hat a_i)(\sum \hat b^j)$. Furthermore,
\begin{equation*}
\hat a_i \hat b^j = \delta_i^j a_ib^j
\end{equation*}
so that $AB = \sum_{i,j} \hat a_i \hat b^j = \sum \delta_i^j a_ib^j = \sum_{i=1} a_i b^j$.
Is this correct algebra?
Answer: Yes, the result is correct and your answer is correct. A more succinct method is to use block matrix multiplication. In particular, if we treat the columns of $A$ and rows of $B$ as if they were numerical entries, we have (sticking to your notation)
$$
AB = \pmatrix{a_1 & \cdots & a_n} \pmatrix{b^1\\ \vdots \\ b^n} = a_1b^1 + \cdots + a_nb^n.
$$

$AB^H = \sum a_i b_i^H$?


Cross-browser styling scrollbars

Question: I know this is possible in IE and Opera.
And what can be done to add style to scrollbar in Chrome and Firefox?
(I need to style overflow:scroll, not browser scrollbars)
I know this can be done via jquery.
So should I desperately use JavaScript after detecting FF or Chrome?
Comment: You can't style `overflow: scroll` - it just tells the browser that scroll bars should be displayed on overflow...
Comment: I wanted to say that I want to style scrollbars that appear in the `DIV` when `overflow: scroll` applied, and not browser window scrollbars
Answer: Check out JScrollbar

Cross-browser styling scrollbars


Erro instanciar 2 objetos com entidades doctrine (setter)

Question: Não estou conseguindo fazer o relacionamento de 2 entidades e está aparecendo o seguinte erro:
Catchable fatal error: Argument 1 passed to Clientes\Entity\Clientes::setEstado() must be an instance of Estados, instance of Clientes\Entity\Estados given, called in C:\xampp\htdocs\zend_framework\module\Clientes\src\Clientes\Controller\IndexController.php on line 34 and defined in C:\xampp\htdocs\zend_framework\module\Clientes\src\Clientes\Entity\Clientes.php on line 186

Controller
$estado = new \Clientes\Entity\Estados();
$estado->setIdFk('1');
$estado->setEstado('1');

$cliente = new \Clientes\Entity\Clientes();
$cliente->setNome('MEU NOME');

$cliente->setEstado($estado);

$em = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
$em->persist($estado);

$em->persist($cliente);
$em->flush();

Entidades:
 /**
 * Clientes
 *
 * @ORM\Table(name="clientes", indexes={@ORM\Index(name="fk_estado",      columns={"estado"})})
 * @ORM\Entity
 */
 class Clientes
 {
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer", nullable=false)
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="nome", type="string", length=100, nullable=true)
 */
private $nome;

/**
 * @var string
 *
 * @ORM\Column(name="sobrenome", type="string", length=150, nullable=true)
 */
private $sobrenome;

/**
 * @var string
 *
 * @ORM\Column(name="endereco", type="string", length=100, nullable=true)
 */
private $endereco;

/**
 * @var string
 *
 * @ORM\Column(name="bairro", type="string", length=80, nullable=true)
 */
private $bairro;

/**
 * @var string
 *
 * @ORM\Column(name="complemento", type="string", length=60, nullable=true)
 */
private $complemento;

/**
 * @var string
 *
 * @ORM\Column(name="cidade", type="string", length=50, nullable=true)
 */
private $cidade;

/**
 * @var string
 *
 * @ORM\Column(name="cep", type="string", length=9, nullable=true)
 */
private $cep;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="modificacao", type="datetime", nullable=true)
 */
private $modificacao;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="data", type="datetime", nullable=true)
 */
private $data;

/**
 * @var \Estados
 *
 * @ORM\ManyToOne(targetEntity="Estados")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="estado", referencedColumnName="id_fk")
 * })
 */
private $estado;
...

E
/**
 * Estados
 *
 * @ORM\Table(name="estados")
 * @ORM\Entity
 */
class Estados
{
/**
 * @var \Id
 *
 * @ORM\ManyToOne(targetEntity="Estados")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="estado", referencedColumnName="id_fk")
 * })
 */
private $idFk;

/**
 * @var string
 *
 * @ORM\Column(name="estado", type="string", length=20, nullable=true)
 */
private $estado;

Comment: Se possível edite sua pergunta para adicionar as informações relevantes. Os comentários costumam ser apagados depois de um tempo.
Answer: Experimente alterar o mapeamento de Estados na classe Clientes para:
/**
 * @var \Estados
 *
 * @ORM\ManyToOne(targetEntity="\Clientes\Entity\Estados")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="estado", referencedColumnName="id_fk")
 * })
 */
private $estado;

Comment: Por favor evitem longas discussões nos comentários; a conversa de vocês foi [movida para o chat](http://chat.stackexchange.com/rooms/27470/discussion-on-answer-by-dherik-erro-instanciar-2-objetos-com-entidades-doctrine)
Comment: http://pt.stackoverflow.com/questions/82926/registros-sendo-inseridos-em-duas-tabelas-entre-as-entidades-com-doctrine-for