How to convert between bytes and strings in Python 3?

Question: This is a Python 101 type question, but it had me baffled for a while when I tried to use a package that seemed to convert my string input into bytes.
As you will see below I found the answer for myself, but I felt it was worth recording here because of the time it took me to unearth what was going on. It seems to be generic to Python 3, so I have not referred to the original package I was playing with; it does not seem to be an error (just that the particular package had a .tostring() method that was clearly not producing what I understood as a string...)
My test program goes like this:
import mangler                                 # spoof package

stringThing = """
<Doc>
    <Greeting>Hello World</Greeting>
    <Greeting>你好</Greeting>
</Doc>
"""

# print out the input
print('This is the string input:')
print(stringThing)

# now make the string into bytes
bytesThing = mangler.tostring(stringThing)    # pseudo-code again

# now print it out
print('\nThis is the bytes output:')
print(bytesThing)

The output from this code gives this:
This is the string input:

<Doc>
    <Greeting>Hello World</Greeting>
    <Greeting>你好</Greeting>
</Doc>

This is the bytes output:
b'\n<Doc>\n    <Greeting>Hello World</Greeting>\n    <Greeting>\xe4\xbd\xa0\xe5\xa5\xbd</Greeting>\n</Doc>\n'

So, there is a need to be able to convert between bytes and strings, to avoid ending up with non-ascii characters being turned into gobbledegook.
Comment: [This question](http://stackoverflow.com/questions/7585435/best-way-to-convert-string-to-bytes-in-python-3) gives more detail in the answer, but I think the briefer answer below is more clear.
Answer: The 'mangler' in the above code sample was doing the equivalent of this:
bytesThing = stringThing.encode(encoding='UTF-8')

There are other ways to write this (notably using bytes(stringThing, encoding='UTF-8'), but the above syntax makes it obvious what is going on, and also what to do to recover the string:
newStringThing = bytesThing.decode(encoding='UTF-8')

When we do this, the original string is recovered.
Note, using str(bytesThing) just transcribes all the gobbledegook without converting it back into Unicode, unless you specifically request UTF-8, viz., str(bytesThing, encoding='UTF-8'). No error is reported if the encoding is not specified.
Comment: If you look at the actual method implementations you'll see that `utf-8` is the default encoding, therefore you can omit it given that you know that the encoding is indeed `utf-8`, i.e. `stringThing.encode()` and `bytesThing.decode()` will do just fine.
Comment: @ccpizza Making the encoding explicit in the above examples makes it much clearer what is going on, and IMHO is good practice. Not all unicode is UTF-8. It also avoids the silent failure referred to in the last paragraph.
Comment: totally agree; explicit is better than implicit, but imo it is good to know what **is** the implicit. Whether to use it or not is another question. Just because you can doesn't mean you should :)
Comment: In Python 3 it's safer to use `decode('utf-8', 'backslashreplace')` to avoid an exception if the encoding is unknown. One shouldn't always assume UTF-8!
Answer: In python3, there is a bytes() method that is in the same format as encode().
str1 = b'hello world'
str2 = bytes("hello world", encoding="UTF-8")
print(str1 == str2) # Returns True

I didn't read anything about this in the docs, but perhaps I wasn't looking in the right place. This way you can explicitly turn strings into byte streams and have it more readable than using encode and decode, and without having to prefex b in front of quotes.
Answer:
This is a Python 101 type question,

It's a simple question but one where the answer is not so simple.

In python3, a "bytes" object represents a sequence of bytes, a "string" object represents a sequence of unicode code points.
To convert between from "bytes" to "string" and from "string" back to "bytes" you use the bytes.decode and string.encode functions. These functions take two parameters, an encoding and an error handling policy.
Sadly there are an awful lot of cases where sequences of bytes are used to represent text, but it is not necessarily well-defined what encoding is being used. Take for example filenames on unix-like systems, as far as the kernel is concerned they are a sequence of bytes with a handful of special values, on most modern distros most filenames will be UTF-8 but there is no gaurantee that all filenames will be.
If you want to write robust software then you need to think carefully about those parameters. You need to think carefully about what encoding the bytes are supposed to be in and how you will handle the case where they turn out not to be a valid sequence of bytes for the encoding you thought they should be in. Python defaults to UTF-8 and erroring out on any byte sequence that is not valid UTF-8.

print(bytesThing)

Python uses "repr" as a fallback conversion to string. repr attempts to produce python code that will recreate the object. In the case of a bytes object this means among other things escaping bytes outside the printable ascii range.
Answer: TRY THIS:
StringVariable=ByteVariable.decode('UTF-8','ignore')

TO TEST TYPE:
print(type(StringVariable))

Here 'StringVariable' represented as a string. 'ByteVariable' represent as Byte. Its not relevent to question Variables..


Como evitar o uso indevido de uma API REST?

Question: Estou desenvolvendo uma aplicação que é composta de três partes:

Backend (BD + API), desenvolvido com Symfony 2 + Doctrine 2
Cliente iOS
Cliente Android

A maioria das requisições dos clientes para a API deverá ser autenticada por meio de um token que se obtém por meio do protocolo OAuth V2, mas algumas requisições serão abertas, ou seja, não precisarão de autenticação.
O meu receio é que seja descoberto não apenas o endpoint da aplicação, mas também os paths que não necessitam de autenticação. Quais são as boas práticas para evitar mau uso da minha API?
Uma solução que pensei foi a de criar um usuário específico para as requisições não-autenticadas cujo único role é o de ROLE_API, mas gostaria de conhecer outras soluções.
Answer: Na sua pergunta não refere se a sua API é em RESTful ou SOAP mas como colocou uma TAG "REST" calculo que é então RESTful. No entanto o que passo a referir faz sentido para ambos.
OAUTH é na realidade para autorização, assim que é entregue um TOKEN é suposto aceder dentro da politica definida para a API. Mas está autorizado a utilizar. Portanto requisições só são abertas para quem o seu sistema entregar um TOKEN.
Quanto aos ENDPOINT's e tendo em conta que criará uma documentação de utilização para a sua API, esta está exposta e vários são os motivos... mas essencialmente porque se trata de puro protocol HTTP e já agora porque é a base de todo o conceito de uma API.
Ao escrever estas linhas vem à ideia limitações que pode colocar como por exemplo na firewall do seu servidor aceitar requets apenas de um determinado IP. Ou ainda utilizar SSL de forma excluir quem não apresente certificados assinados... o universo de opções é vasto.
A criação de 'usuário/utilizador(no meu idioma)' específico para requisições não autorizadas nada contribuí para a segurança da sua API.
Mais uma vez o descobrir de um ENDPOINT é o menor dos seus problemas. Já pensou naquelas requisições que irão aceder a uma base de dados e que por utilização ou por ataques vários podem bloquear o seu serviço... acho que tem mesmo muito em que pensar.
Concluindo, uma API é publica e ponto. O OAuth é claramente o caminho a tomar para autorizar a sua utilização. Para mim o OAuth é delegação pura... sendo a autenticação e acesso a recursos uma outra situação. No ultimo caso o OAuth tem solução pelo que aconselho a análise do que no OAuth é denominado por SCOPES ou permissões que pode ajudar a resolver alguns pontos da sua implementação. Veja também o openID, pode ser que sirva para o seu caso.
Comment: Realmente a API é REST. A parte de recursos protegidos eu de fato uso o protocolo OAuth. As chamadas às quais me refiro são as abertas (por exemplo, para criação de usuário), nas quais ainda não possuo um usuário e senha para obter um token de acesso. No final das contas acabei optando por proteger as chamadas abertas com uma autorização básica usando usuário e senha pré-definidos. Acha que é uma boa solução?
Comment: Como disse... autorização já tem...agora falta só a autenticação. Quanto à autorização que utiliza essa depende da implementação desejada. Como está a passar a informação? se for sem ssl tem de ter em conta algumas coisas. Neste momento também me encontro a desenvolver para uma API e no interface em que é possível definir o acesso por OAUTH também defino os recursos a que essa  aplicação pode aceder ou não. concluindo... sim é uma excelente solução. já pensou também como constrói os seus ID's e chaves? também é importante.
Comment: Editei a minha resposta para o lembrar dos SCOPES no OAuth porque na minha resposta inicial me esqueci de referir e que no seu caso fará todo em sentido.


Google maps javascript api bug android browser (Jelly bean and prior)

Question: So, this question concerns only the old android browser (before chrome became the standard browser) and the google maps javascript api v3.
I am developing a website, together with an app with a webview, showing (parts of) that website.
Lately, the google map windows that are included in the website have stopped working properly. The map shows up, but any touch event on the map, like dragging the map to show other locations, as well as two finger zoom are not working anymore. the error that is shown in the javascript console (on every touch event on the map) is:
Uncaught TypeError: Illegal constructor -- From line 125 of http://maps.googleapis.com/maps-api-v3/api/js/26/2/intl/nl_ALL/common.js

After a days of looking for bugs in my code it seems to me that this could be a bug in the google maps javascript api. The error occurs not only on my website, but on all embedded google maps that i could find. Even in google's own api documentation.
The error only occurs in the old standard android browser, but old android versions are still in use quite commonly, and i cannot tell my users to switch to another browser because it has to work in a webview as well.
I could reliably reproduce this issue on a samsung xcover 2 (gt-s7710, android 4.1.2) and an api 17 simulator (android 4.2) in this link: https://developers.google.com/maps/documentation/javascript/
Has anyone else encountered this problem and/or found a workaround?
EDIT:
I filed a bug report to google that got accepted, so in the meantime i guess Dvy Hrnsch's workaround is the best solution!
Comment: Funny, I also got this error today. This happen on stock browser of Android 18 (4.3) too...
Answer: i had the same problem as you described.
Explanation:
At least i found a simple workaround (after hours of headache).
I just found out, that Google Maps Javascript API has 3 API References.
The "Experimental Version", "Release Version" and the "Frozen Version".
If you include the API without specifying the exact version, it will use the "Experimental Version" by default.
As i noticed, this version (3.26) got an Update on 30. of August 2016 which seems to cause the bug.
SOLUTION:
Just change your API from:

to
src="http://maps.google.com/maps/api/js?v=3&key=API-KEY&libraries=geometry,places"
Now its using the "Release Version" (last change on 17. of August 2016) of the API.
Source: https://developers.google.com/maps/documentation/javascript/versions
For me it works now on Android 4.3 (API Level 18) with cordova.
I hope this helps you also.
Best wishes
Answer: Got almost crazy before I found Dvy Hrnsch's answer. I want to add some information that could be useful in this matter:
Android applications using WebView 534.30 and maybe older (being part of Android 4.3 and older) fail to give Google Maps its full functionality if the v=3 parameter is missing. Although the charts are being displayed correctly and zooming in and out by pressing [+] and [-] is possible pushing the chart or using the click event is not possible.
With newer Android versions (that use WebView 537.36 or younger) these problems don't occur.
Tom


Call Python non-member function referenced by class variable

Question: In my class I'd like to call a non-member function whose reference is stored in a member variable. My issue is that it tries to pass self to the function as the first argument. How can I avoid this?
class MyClass:
    my_useful_static_function = crcmod.mkCrcFun(0x11021, True)

    def __init__(self):
        # this gets called with the first argument as self :(
        result = self.my_useful_static_function()

Answer: Use staticmethod:
class MyClass:
    my_useful_static_function = staticmethod(crcmod.mkCrcFun(0x11021, True))

    def __init__(self):
        result = self.my_useful_static_function()

Comment: Tested, works :)
Answer: You need to use staticmethod like so:
class Foo:
    @staticmethod
    def my_method():
        print("This is a static method")

    def my_other_method(self):
        print("This is not static")

# This works
Foo.my_method()

# This won't work
Foo.my_other_method()

# This works though
foo_instance = Foo()
foo_instance.my_other_method()

Comment: In this instance I'm not creating the def, so I don't think `@staticmethod` works, but the function format does.
Comment: @Rick: `@staticmethod` is just syntactic sugar for `decoratedname = staticmethod(decoratedname)`.
Comment: Right, I missed that part.


Como armazenar dados de um arquivo .txt em um objeto - Java

Question: Eu sou iniciante em programação e estou fazendo uma pequena aplicação que deve cadastrar produtos, excluir, editar, organizar, controlar estoque, preço de custo, preço de venda, margem de lucro, etc.
Até agora eu consegui fazer com que um arquivo .txt armazene os objetos da classe produto em linhas diferentes, assim:
public void gravaProduto() throws IOException{

    FileWriter arquivo = new FileWriter("E:\\Documentos\\NetBeansProjects\\Experimentacao3\\cadastro.txt",true);
    PrintWriter gravarArquivo = new PrintWriter(arquivo);

    for (Produto p : produtos) {
        gravarArquivo.println(p);
    }

    arquivo.flush(); //libera a gravaçao
    arquivo.close(); //fecha o arquivo
}

Também consegui ler o arquivo, assim:
public void leProduto() throws IOException{
    String linha = "a";

    FileReader arq = new FileReader("E:\\Documentos\\NetBeansProjects\\Experimentacao3\\cadastro.txt");
    //armazenando conteudo no arquivo no buffer
    BufferedReader lerArq = new BufferedReader(arq);
    //lendo a primeira linha
    //String linha = lerArq.readLine();
    //a variavel linha recebe o valor 'null' quando chegar no final do arquivo
    while (linha != null){
        System.out.printf("%s\n",linha);
        //lendo a segundo até a última
        linha = lerArq.readLine();

    }
    arq.close();

}

O meu problema é: (depois que a aplicação é encerrada e vou abrir ela depois) Preciso armazenar cada linha do txt em um ArrayList de objetos e depois fazer o que for necessário (ex: como ordenar por preço). Alguma luz? Estou no caminho certo? tem outra saída? Obrigado a todos!
Comment: É um trabalho de faculdade? Precisa ser mesmo arquivo texto? Poderia usar JSON? Porque não um banco de dados?
Comment: É um trabalho de um curso que eu fiz, o professor deixou aberto pra gente usar a criatividade e como é uma coisa que ele não ensinou teria que ser o mais simples possível, aí pensei em armazenar em arquivo txt (se é que é mais simples) mas pode ser feito de outra maneira... desde que eu entenda, pq sou iniciante
Answer: Obrigado pela ajuda de todos! Consegui resolver assim:
    public void leProduto() throws IOException{

    FileReader arq = new FileReader("E:\\Documentos\\NetBeansProjects\\Experimentacao3\\cadastro.txt");
    //armazenando conteudo no arquivo no buffer
    BufferedReader lerArq = new BufferedReader(arq);
    //lendo a primeira linha
    String linha = lerArq.readLine();

    //ArrayListe para armazenar os objetos da leitura
    ArrayList a = new ArrayList();

    //a variavel linha recebe o valor 'null' quando chegar no final do arquivo
    while (linha != null){
        //Criando o objeto p2 da classe produto
        Produto p2 = new Produto();
        //criando um array que recebe os atributos divididos pelo split
        String[] atributos = linha.split("#");

        //Se quiser usar outro separado use:
        //String[] atributos = linha.split(Pattern.quote("|"));

        //passando os "atributos" da array para o objeto p2
        p2.cod = atributos[0];
        p2.nome = atributos[1];
        //adicionando objeto p2 no ArrayList a
        a.add(p2);
        //capturando a proxima linha
        linha = lerArq.readLine();
    }

    /* Se quiser exibir o resultado (importante sobrescrever o método toString()
    for (Object p : a) {
        System.out.println(p);
    }
    */

}

Comment: Para usar **|** , no split, tente assim: **linha.split(Pattern.quote("|"));**
Comment: Você pode ler todas as linhas assim: `List linhas = Files.readAllLines(Paths.get("arquivo.txt"), StandardCharsets.UTF_8);`
Answer: Veja se lhe ajuda:
public class Exemplo {

    public static final String SEPARADOR = "#";
    public static final String ARQUIVO = "cadastro.txt";

    public static void main(String[] args) {
        Produto produto = new Produto();
        produto.setNome("Produto 1sss");
        produto.setPreco(15.5f);

        Produto produto2 = new Produto();
        produto2.setNome("Produto ddd");
        produto2.setPreco(25.5f);

        try {
            gravaProduto(produto);
            gravaProduto(produto2);

            List<Produto> produtos = leProduto();

            for(final Produto p : produtos){
                System.out.println(p.getNome()+", "+p.getPreco());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * Adiciona o Produto, passado via parametro
     * @param produto
     * @throws IOException
     */
    public static void gravaProduto(Produto produto) throws IOException{
        FileWriter arquivo = new FileWriter(ARQUIVO,true);
        PrintWriter gravarArquivo = new PrintWriter(arquivo);
        gravarArquivo.println(produto.gerarString());
        arquivo.flush(); //libera a gravaçao
        arquivo.close(); //fecha o arquivo
    }

    public static List<Produto> leProduto() throws IOException{

        //Lista que vamos retornar
        List<Produto> list = new ArrayList<Produto>(0);
        FileReader arq = new FileReader(ARQUIVO);
        //armazenando conteudo no arquivo no buffer
        BufferedReader lerArq = new BufferedReader(arq);
        //lendo a primeira linha
        String linha = lerArq.readLine();
        //a variavel linha recebe o valor 'null' quando chegar no final do arquivo
        while (linha != null){
//          System.out.printf("%s\n",linha);
            //lendo a segundo até a última
            linha = lerArq.readLine();
            // Passamos a linha para popular o objeto,
            // se não for vazia
            if(null != linha && !"".equals(linha) ){
                Produto produto = new Produto(linha);
                list.add(produto);
            }

        }
        arq.close();

        return list;

    }

    /**
     * Esta classe guarda a s informacnoes do Produtos
     */
    static class Produto{

        /**
         * Contrutor padrão
         */
        public Produto() {      }
        /**
         * recebe a linha para popular o Objeto
         * @param line
         */
        public Produto(String line) {
            if(null != line ){
                //Vamos quebrar a linha no separador...
                String[] valores = line.split(SEPARADOR);
                // se não for nulo, vamos setar os valores
                if(null != valores){
                    setNome(valores[0]);
                    setPreco(Float.valueOf(valores[1]));
                    // se possuir mais campos, irá adiionar aqui, seguindo a ordem
                }
            }
        }
        private String nome;
        private Float preco;
        public String getNome() {
            return nome;
        }
        /**
         * Temos que garantir que esta String não possua o SEPARADOR
         * Senão irá bugar
         * @param nome
         */
        public void setNome(String nome) {
            if(null != nome){
                if(nome.contains(SEPARADOR)){
                    nome = nome.replaceAll(SEPARADOR, " ");
                }
            }
            this.nome = nome;
        }
        public Float getPreco() {
            return preco;
        }
        public void setPreco(Float preco) {
            this.preco = preco;
        }

        /**
         * Vamos concatenar os dados para salvar..
         */
        public String gerarString(){
            final StringBuffer buffer = new StringBuffer();
            if(null != getNome()){
                buffer.append(getNome());
            }
            //  inserimos ao separador
            buffer.append(SEPARADOR);
            if(null != getPreco()){
                buffer.append(getPreco());
            }
            return buffer.toString();
        }

    }
}


template_preprocess custom block type

Question: I have a custom block type with two different fields.
What I am trying to accomplish is, to hide the first field if the second is not empty.
That seems like something that could be done in template_preprocess, but where would I put it and what would be my template?
thanks!
Answer: you can utilize twig's if/else condition:

Find the twig's template file for your custom block using twig debug mode
use twig's if/else syntax (reference)

{% if field_NAME_OF_YOUR_SECOND_FIELD.0 == false %}
    {{ field_NAME_OF_YOUR_FIRST_FIELD.0 }}
{% endif %}

check this for extracting the values of different fields
Answer: Preprocessing the block_content template was considered undesireable, and they suppressed the functionality. There is a module on github that provides this functionality, but since it lacks composer support, you might just copy it into your project.
All that said, you should find that template_preprocess_block allows access to what you want, and this might work:
function MYTHEME_preprocess_block(&$variables) {
  unset($variables['content']['field_custom'][0]);
}

Answer: Custom block type is available in $variables['content']['body']['#bundle']
Refer the below sample function
function mytheme_preprocess_block(&$variables) {
  if ($variables['content']['body']['#bundle'] == 'my_block_type') {
    // Do whatever specific to the block type.
  }
}

Adding a bit more information as per @berliner commented
$variables['content']['#block_content'] //will hold the content entity

$variables['content']['#block_content']->id() //will give you the content id, ie. the custom block content id

In order to alter any field you could apply logic inside the conditional statement
Ref. belwo code where the values are assigned to a drupalSettings js variable.
function mytheme_preprocess_block(&$variables) {

        if($variables['content']['body']['#bundle'] == 'my_custom_block_type')
        {
        $variables['#attached']['drupalSettings']['block_content_id']=$variables['content']['#block_content']->id();

        $variables['#attached']['drupalSettings']['full_content']=$variables['content'];
        }

}

I would prefer to log drupalSettings.full_content to a javascript console, which would give a bigger picture of the whole variables and manipulate accordingly
Ref: JS function
(function($, Drupal, drupalSettings) {

Drupal.behaviors.myCustomJSFunction = {
    attach: function (context, settings) {
      console.log(drupalSettings.full_content);
        }
    };

     })(jQuery, Drupal, drupalSettings, window.Cookies);

Comment: This could be improved, as it doesn't seem to fully address the original problem of this question. It shows how to identify the block, but not how to modify it.

template_preprocess custom block type


perl test if symbolic function name exists

Question: If I make up a subroutine name, say
$type = 'Circle';
$fn = 'My'.$type.'Renderer';

How do I test if the subroutine named in $fn is defined?
Answer: You might be tempted to use the can method, but that's wrong, it can lead to false positives since it obeys inheritance. You want to use exists.
my $name = ...;
my @args = ...;
die("$name doesn't exist\n") if !exists(&$name);
(\&$name)->(@args);

$name->(@args) would suffice without strict. The weird syntax bypasses strict.
Comment: This is assuming, of course, that checking for inheritance *isn't* wanted.
Comment: @Robert P, It's not an assumption. It's what the OP asked for ("How do I test if the **subroutine** named"). He didn't ask about methods. `exists` (or `defined`) for subs, `can` for methods.
Comment: In fact, both are interesting cases, but mine is that I have a main program, calling a subroutine in a package. In that subroutine, I need to know if I can call a given made up name that MAY be either in the main program or in some other loaded package, including mine.
Comment: @Don, Yes, I know, and `exists` works perfectly fine for that. `$name = "MyModule::some_sub";`. `can`, on the other hand, usually works, but can give the  wrong answer. (`can` uses `exists` too, but checks more than one package.)
Answer:
PACKAGE->can($fn), with PACKAGE being main for default.
defined &{$fn}

can also will return you CODE reference to this function if it exists.
Comment: +1 for `PACKAGE->can($fn)` (often `main->can($fn)`). the `defined` test makes me itchy
Comment: Yes, indeed, PACKAGE->can is really what I want. I thought that defined &{$fn} would work, but messed up my simple test. This is like pair programming with Stack Overflow. Thx.


How to delete a record in database using php? I have some of my codes in deleting but it won't delete the record

Question: This is my deleteriddle.php but it doesn't work. Please help me
<code><?php

require("dbcon.php");

$ridid = isset ($_GET['riddleid'])?$_GET['riddleid']:"";
$query = sprintf("DELETE FROM riddle WHERE riddleid= '$ridid'");
$result = mysql_query($query);

if (!$result) {
    die('Invalid query: ' . mysql_error());
}
else {
    echo '<script type="text/javascript">
    alert("Riddle successfully delete");
    window.location.href = "viewriddle.php";
    </script>';
}

mysql_close($dbcon);

?>
</code>
Comment: What error do you get?
Comment: `mysql` brrrrrrrrr.... please use something better like `mysqli` or [`PDO`](https://stackoverflow.com/questions/28096054/how-to-replace-mysql-functions-with-pdo/28096059#28096059).
Comment: there is no error. It always say that the riddle successfully deleted but when it goes back to view.php the record is still there
Comment: Is this posts related to this?  https://stackoverflow.com/questions/46626634/how-to-delete-the-currently-displayed-record-using-php
Comment: I'm sorry Im kinda new to programming so I don't really know what is mysqli or PDO. please bear with me
Comment: yes it is. I'm also the one who posted it
Comment: Please look that up. Those newer libraries are safer when it comes to SQL-injection.
Comment: I would like to look into it but I don't have much time. This website I'm making is due tomorrow. please help me
Comment: but in the query DELETE FROM, whenever I specify the id like.. delete from riddle where riddleid = 1. It has deleted the record with the ID 1. but when I use the '$ridid' it doesn't work
Answer: Try this :
<code>$query = sprintf("DELETE FROM riddle WHERE riddleid= %u", $ridid);
</code>
Comment: Try to print the `$query` just before the `mysql_query`. Check its right or not ??
Comment: still nothing happens. but in the query DELETE FROM, whenever I specify the id like.. delete from riddle where riddleid = 1. It has deleted the record with the ID 1. but when I use the '$ridid' it doesn't work
Comment: Remove the single quotes around the `$ridid` in your query and try
Comment: when I removed the single quote..Invalid query: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1


Creating a copy of a large database

Question: I want to make a copy of a largish SQL 2008 database (1TB). I only have 1.9TB of disk space free so I don't have enough to create a backup of the database and restore it into another.
Are there any other techniques I can use? How to I do it quickly as possible?
Is it ok to just make a copy of the MDF, LDF and NDF files - rename them and then just attach the database?
Comment: If you db size is 1TB and you have 1.9 TB free, why can't you create a copy on the same server.
Comment: You have normal backups, right?
Comment: @Ash that is what I am asking, "Is it ok to just make a copy of the MDF, LDF and NDF files"?
Answer: Yes. You can detach, copy the .ldf, mdf and .ndf files to another location and attach them.
Are you using backup compression?

Beginning in SQL Server 2008 R2,
  backup compression is supported by SQL
  Server 2008 R2 Standard and all higher
  editions. Every edition of SQL Server
  2008 and later can restore a
  compressed backup.
Comment: Thanks mitch, unfortunately we're not using R2.
Answer: Use a tool such as VS2010 Premium or SqlDelta that supports schema and data comparison and migration. I've used it to create a complete copy of database, when I haven't had possibilities to make and access a disk based backup.
Answer: Not quite sure if you're saying "I don't have backups" but...
As a workaround:

Plug in an external 2TB backup drive for temporary backup space
Restore to a compressed folder: this will run like a dog though

Notes:

Do you not separate data, log and backups onto different volumes?
How do you manage normal backups?
Is the backup 1TB (eg 1TB data) or is the DB 1TB on disk?
Comment: @Dan: is this production?
Comment: I don't have a back up. And I dont have enough space to store a back up and the copy of the database. Data and logs are on separate volumes.
Comment: No its not production.
Answer: It sounds like you can just use the database copy wizard.  Just use the same server for the source and destination.
http://msdn.microsoft.com/en-us/library/ms188664.aspx

Creating a copy of a large database


Generating a subspace sigma-algebra

Question: Say X is a set and $\mathcal{E}$ is a collection of its subsets.
Let $\mathcal{M}$ be the sigma-algebra generated by $\mathcal{E}$ (or the smallest sigma-algebra over X containing all members of $\mathcal{E}$)
Provide the subset $Y \subset X$ the sigma-algebra $\mathcal{M}^* = \{ M \cap Y : M \in \mathcal{M}\}$. Analogously define the collection $\mathcal{E}^* = \{E \cap Y : E \in \mathcal{E}\}$ of subsets of Y.
Is it true that $\mathcal{E}$* generates $\mathcal{M}$* (that $\mathcal{M}$* is the smallest sigma-algebra over Y containing $\mathcal{E}$*)?
EDIT:
So I found a solution. The answer is yes. It involved taking  the sigma-algebra $\mathcal{N}^*$ over Y actually generated by $\mathcal{E}^*$ and constructing the sigma-algebra $\mathcal{N}$ over X consisting of all subsets of X whose intersection with Y was a member of $\mathcal{N}^*$, noticing that $\mathcal{N}$ contained $\mathcal{E}$ and therefore contains $\mathcal{M}$, and deducing that $\mathcal{N}^* \supset \mathcal{M}^*$. The reverse inclusion is easy.
Comment: This iterative process is definitely not the most convenient one to approach generated sigma-algebras (note that, on the real line R, starting from the open sets and taking successively complements, countable unions, and countable intersections, it requires uncountably many iterations before one gets B(R) the Borel sigma-algebra). You must know another characterization of the sigma-algebra generated by a collection of subsets, no?
Comment: The intersection symbol (and much else) can be displayed by putting dollar signs around an expression, triggering `MathJax` to display $\LaTeX$ syntax as mathematics.  For example, `M \cap Y` becomes $M \cap Y$.  Some introduction is [here for notation help](http://math.stackexchange.com/help/notation).
Comment: What happens when you try to prove it?
Comment: I keep applying the $\sigma$ operator which sends a collection of sets to the collection of countable unions of its members, or the $\delta$ operator defined analogously, with intersections in place of unions, to try and express the actual algebra generated by **E*** as something like **E*** $_{\sigma \delta \sigma \cdots}$
Comment: Sorry for the change of font. I decided to go with calligraphy
Answer: The $\sigma$-algebra $\mathcal{M}^{\ast}$ can be viewed as the initial
$\sigma$-algebra generated by the inclusion map.
More precisely, let $i:Y\rightarrow X$ be the inclusion map defined
by $i(x)=x$. We can verify that $\mathcal{M}^{\ast}$ is the smallest
$\sigma$-algebra on $Y$ such that $i$ is $\mathcal{M^{\ast}}/\mathcal{M}$-measurable
and $\mathcal{M^{\ast}}$is explicitly given by $\mathcal{M}^{\ast}=i^{-1}(\mathcal{M}):=\{i^{-1}(B)\mid B\in\mathcal{M}\}$.
Also note that $\mathcal{E}^{\ast}=i^{-1}(\mathcal{E})$. In the following,
we show that $\sigma\left(i^{-1}(\mathcal{E})\right)=i^{-1}\left(\sigma(\mathcal{E})\right)$.
Since $\mathcal{E}\subseteq\sigma(\mathcal{E})$, we have $i^{-1}(\mathcal{E})\subseteq i^{-1}(\sigma(\mathcal{E}))$.
Observe that $i^{-1}(\sigma(\mathcal{E}))$ is a $\sigma$-algebra,
so $\sigma\left(i^{-1}(\mathcal{E})\right)\subseteq i^{-1}(\sigma(\mathcal{E}))$.
To show the reverse inclusion $i^{-1}(\sigma(\mathcal{E}))\subseteq\sigma\left(i^{-1}(\mathcal{E})\right)$,
we let $\mathcal{N}=\{B\subseteq X\mid i^{-1}(B)\in\sigma\left(i^{-1}(\mathcal{E})\right)\}$.
By direct verification, $\mathcal{N}$ is a $\sigma$-algebra on $X$
and $\mathcal{E}\subseteq\mathcal{N}$. Therefore $\sigma(\mathcal{E})\subseteq\mathcal{N}$
and hence $i^{-1}(\sigma(\mathcal{E}))\subseteq\sigma\left(i^{-1}(\mathcal{E})\right)$.
We remark that the theorem $\sigma\left(i^{-1}(\mathcal{E})\right)=i^{-1}\left(\sigma(\mathcal{E})\right)$
continues to hold if $Y$ is replaced by an arbitrary set and $i$
is replaced by an arbitrary map $f:Y\rightarrow X$.

Generating a subspace sigma-algebra


python not creating file in mode x

Question: well I have done this
    try:
        with open('add/report.txt', 'x')as f:
            pass
    except FileExistsError:
        pass
    finally:
        with open('add/report.txt', 'a')as f:
            f.write(message)
            f.write('-------------------')

and I get error FileNotFoundError even after I tried to open the file in x mode to create it first
Comment: Does the directory `add` exist?
Comment: Cannot reproduce the error. The code worked perfectly fine.
Comment: FileNotFoundError: [Errno 2] No such file or directory: 'add/report.txt'
this is what I get and I don't understand why,
that is why I am asking here what is the problem
Answer: You are trying to make a file into a folder that does not exist.
Here is a working example with automatic file and folder creation:
from pathlib import Path

def send(message):

    target_file = r'c:\tmp\path\which\does\not\exist\report.txt'

    p = Path(target_file)
    if not p.exists():
        p.parent.mkdir(parents=True)

    with open(p, 'a')as f:
        f.write(message)
        f.write('-------------------')

python not creating file in mode x


Questions about Economics as a discipline

Question: Would the main or the meta be an appropriate place to ask about publishing protocol?
(Journals to submit to, procedure, costs, policies, etc)
This isn't necessarily specific to Economics, but would presumably be relevant to the profession.
Any thoughts?
Comment: Meta is never appropriate for any question that is not about the administration of a normal SE site. I don't think that publishing fits into the main site itself, that would rather be academia.se
Answer: I agree with Foobar that Meta should not be used for purposes other than discussing the administration of the site. Questions about publications that are specific to economics should be fine on main, but for more general questions there is academia.SE.
The publication process in economics is quite idiosyncratic, so I can see a pretty good case for a lot of these kinds of questions making it onto Econ.SE.
Comment: agreed about the publication process in econ being idiosyncratic. I think that is a good justification for allowing such questions on econ.se (they often wouldn't get relevant answers from academics in general). There is also the pragmatic justification that discussion about publications will attract academics - as opposed to amateurs - which is an important goal.
Comment: The sort of questions that are identified in the question, such as what journal to submit to, would typically get promptly closed and deleted on Academia.SE.
Comment: @EnergyNumbers So do you think they should be allowed here or not? What makes this site different for an opinion based advice question like that?
Comment: I didn't necessarily mean what journal to publish to based on opinion of quality, but more on specific niche publications, or policies related to multiple submission... i.e. If I submit to XYZ, how long would I have to wait to submit to ABC? Can I submit a paper to PQR and Arxiv?


Optimal hidden units size

Question: Suppose we have a standard autoencoder with three layers (i.e. L1 is the input layer, L3 the output layer with #input = #output = 100 and L2 is the hidden layer (50 units)). I know the interesting part of an autoencoder is the hidden part L2. Instead of passing 100 inputs to my supervised model, it will feed it with 50 inputs. What is the optimal hidden units size? 50 is well, but why not using 51, 52 or 63 hidden units? Does 51 will perform better the supervised model than 50 hidden units?
Suppose now that the number of inputs is 1,000,000. If N is the number of units, then I don't want to test out each possible value for N to find out the optimal N. I thought there exists at least an algorithm to do not be obligated to test each possible value or eliminate some of them.
Could that question help?
Answer: In short: there is no way to tell the optimal number of units in a hidden layer without actually trying.
There is a number of heuristics, intuitions, rules of thumb, best practices etc, however, number of units in a layer is simply a hyperparameter that requires tuning like any other, and you will simply have to try several values to find the best one. Also, neural networks are usually quite robust, so a few units more or less should not make a big difference.
For more details, see this answer. Also, the thread you linked contains some good hints.

Optimal hidden units size


My server ignores the .htaccess file

Question: I have a terrible problem: I have written rubbish within the .htaccess file, in order to break my web site, and cause an error 500. Result: nothing, nothing happens, the web site continues to be online.
That is, the .htaccess file is ignored completely. Why does this happen? I understand that this only has an answer: the Apache server is misconfigured. This is absolutely correct? Or are there other possible causes?
My file system: image 1 and image 2.
Originally the website was hosted on a folder within a server (mydomain.com/folder), and everything worked fine. Now I am migrating the website to another server, but within a subdomain. The features of the new server:

Subdomain: domain.company.com
Apache: 2.4.7 (Ubuntu)
Port: 80
phpinfo: Open phpinfo

UPDATE.
1) DNS. I checked my subdomain with http://whois.domaintools.com/ and http://mxtoolbox.com/SuperTool.aspx. I found no problems.
2) .htaccess. The file contains RewriteEngine on. RewriteBase is commented, because I don't know what to put when a subdomain. Anyway, no matter what I put in the file, it is completely ignored.
3) My settings.php has:

$ Base_url = 'http://domain.company.com'; // NO trailing slash!
There is not $base_path. Does not exist.

I cleared the cache Drupal. I cleared the browser cache. I truncate all tables that start with cache_.
It does not work. The file .htaccess remains ignored. This, I can not enable friendly URLs.
Comment: This is a common problem on shared-hosting environments. If you don't have access to your server to configure, you'll probably need to contact your hosting provider.
Comment: Sorry but server config questions are off-topic here unless they have a specific connection to Drupal - an .htaccess being ignored is something more general than that. Contacting your hosting provider would be the best solution
Answer: This generally happens when your web server does not have access to make the overrides to your webroot.
Check if your httpd.conf file has something of the following format
<Directory "/Applications/MAMP/htdocs">
    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    AllowOverride All

    #
    # Controls who can get stuff from this server.
    #
    Order allow,deny
    Allow from all

</Directory>

The important piece above is AllowOverride All, which will make possible for your .htaccess file to override the settings.
If you cannot change the settings in this file, I would recommend you to create a .conf file specific to your site (e.g. mysite.conf using the default file 000-default) in on of /etc/apache2/sites-enabled or /etc/apache2/sites-available, and modify the <Directory> tag to point to the root of your site.
Restart Apache in order to make this change visible. The site should now be able to pick up your .htaccess file.
Comment: Ajit I am agree with you, but on apache server the .htaccees is completely ignored on his case, in generally it is no dependent on the mysie.conf file. Isn't it?
BTW +1 for the answer.
Comment: @Shubham : As mentioned in answer, the `.htaccess` is not able to override the configurations probably because of the value set in the `httpd.conf` which has the settings for all of your webroot (eg. localhost). I suggested to create the own conf file for the purpose of making the change only for one site. Thanks for the +1 btw ;)
Comment: @ Ajit thanks for sharing information. Can you please let me know the .htaccess case with NGINX sever. .htaccess file does not work with NGINX server.
Comment: It seems an excellent idea to create my own file .conf.
I only find one small problem: I do not have access to these folders: /etc/apache2/sites-enabled or /etc/apache2/sites-available.
I do not even have access to cPanel. I have only SFTP access. This is my webroot: [see](http://pruebas-berkeley.hol.es/compartir/img/filezilla.jpg).
Obviously, I can not restart Apache.
Comment: @GeorgeBerkeley I that case, I would suggest to talk to your hosting provider and ask them to make those changes at least for your webroot.
Comment: I spoke with my hosting. They said that everything was properly configured. Suddenly, just like that, the file was recognized. Obviously everything was a problem with the hosting. **Note to myself:** never trust blindly in the words of the people in the hosting.
Comment: @GeorgeBerkeley Yep. They might have fixed it for your domain before saying everything is fine. Anyways, good to know that you got your issue fixed.
Answer: Make sure you have:
1) DNS setup properly
2) .htaccess - Look for the RewriteBase setting AND RewriteEngine on
3) settings.php - Look for $base_url and $base_path
Don't forget to clear the cache after you change these settings. If you can't access the site, use drush cc all or login to your database and truncate all tables that start with cache_ (sometimes clearing the cache still leaves persistent entries).
Comment: Can you edit your question for how you have your sites directory setup?  I would comment above but my rep isn't high enough yet.  You can up vote me so I can comment above ;)
Comment: @george-berkeley I am also trying to understand your question.  Are you trying to break your site?

My server ignores the .htaccess file


What do the letters on this LEGO Drum Machine indicate?

Question: In this video by Brick Technology, they make a LEGO drum machine. It works a bit like a music box, with a set of eleven piezoelectric drum pickups each actuated by its own Technic-built drum stick.  These sticks are labeled, in order,

KSHCTTTRCAA

and I can't for the life of me figure out what all the letters means.
I'm familiar enough with drums (and can hear well enough to figure out) that K is for the Kick (or bass) drum, S is for the Snare drum and H is for the Hi-hat. Some investigation into the matter indicates the the three T's are most likely for the three Toms, with the two C's for the Crash cymbals and the R for the Ride cymbal, but I am not as confident with this assessment. And I can find nothing to determine what the two A's are for - are they just "Auxiliary" percussion, or is there some part of the kit that starts with A?
Answer: There's a clue around the 2:00 mark. Activating an A trigger makes a different percussion sound. There will be many other different sounds available from the module, activated by, actually, any of the triggers.
They could be cowbell, guiro, quickly closed hi-hat, wood block, tambourine, choked cymbal, triangle, to name just a few. So, yes, auxiliary is most likely what A represents, and those will be the sounds that would get changed most frequently, as all the others constitute the basic drum kit.
A very clever and impressive idea..!


Duvida - Função para Limpar img com javascript não funciona

Question: Preciso limpar um campo img e para isso estou utilizando o código abaixo, mas ao clicar no button o javascript não funciona por que a pagina recebe um refresh.
Analisei o console do navegador e não aparece nenhuma mensagem de erro... Podem me dizer aonde estou errando??
Aqui está o código do botão
   <?php
// verificando o diretorio utilizado para fins de desenvolvimento
  diretorio();
  if(Empty($dir)){
    $img_padrao = '/images/sem_imagem.jpeg';
  }else{
    $img_padrao = '/'.$dir.'/images/sem_imagem.jpeg';
    }
  ?>
  <img src='<?=$img_padrao?>' class='img-thumbnail limpar' id="img" alt="Img Destaque">

  <button id="apagar" onclick="clearimg('<?=$img_padrao?>')" class="btn btn-danger tabindex='4'">Remover Imagem</button>

aqui o javascript
// função responsavel por apagar a imagem do Box (exibir a imagem padrão).
function clearimg($img_padrao){
var clearimg = $img_padrao;
$('.limpar').attr('src', clearimg);
}

Comment: Esse button está dentro de algum form? Tem que ser a tag button?
Comment: está dentro de um form sim, e não precisa ser a tag button
Comment: tu matou meu problema! mudei para input e funcionou... falta de experiência atrapalha e muito.. Obrigado! abraços!
Answer: Provavelmente sua tag button está executando a ação padrão dela, além do evento onclick, que provavelmente é o submit em um formulário. Para evitar isso, você pode especificar o tipo de botão:
<button type="button">Botão</button>

Ou pode evitar a ação padrão retornando false na função do evento:
function acao() {
    return false;
}

Ou utilizar outra tag, como um <input type="button"> ou .


What does the '3' in Chris Paul's nickname CP3 stand for?

Question: Watching the NBA 2021 playoffs with the Phoenix Suns playing and Chris Paul aka CP3 is a fantastic player.  Yet can't find anywhere that explains what the '3' stands for in his nickname CP3.  Does anyone know?  Is it his jersey number? Could it be that simple?
Answer: It's a family related nickname. Chris Paul's father's and older brother's Initials are also CP, making him the third person with initials CP, hence CP3.

What does the ‘3’ in Chris Paul’s nickname CP3 stand for?


jQuery: stretch text area like Chrome does

Question: Is there a plugin that would allow me to convert plain textarea to a stretchable one, just like Chrome does.
Answer: Do you mean the Resizable plugin?
Comment: This works very well. The other alternative is to do a facebook-style (self.shudder()) textarea that grows as needed.
Comment: Can't get the textarea to resize at all with this plugin. Div around it grows but text area does not.
Answer: See http://jqueryui.com/demos/resizable/#textarea
There's also TextAreaResizer, but the demos only show vertical resizing so I'm not sure if it supports diagonal resizing.
Answer: There's a plugin called TextAreaResizer, see if it offers everything you're looking for.
It's only vertical, but it's the most cross-browser stable thing I've seen.  Here's a link right to the demo


How to set the parent of menu items creating using hook_menu()?

Question: I need creating a series of menu items dynamically based on values from the database.
Here is a example of my hook_menu() function; in this snippet, I removed the DB queries and just added an array to prove the concept:
function MYMODULE_menu() {
  $years = array(2011, 2010, 2009);

  foreach ($years as $year) {
    $items['events/' . $year] = array(
      'title' => $year . ' Events Archive',
      'page callback' => 'page_archive',
      'page arguments' => array(1),
      'access callback' => TRUE,
      'type' => MENU_NORMAL_ITEM,
      'menu_name' => 'main-menu',
    );
  }

  return $items;
}

This works and I get three menu items in the Main Menu titled 2011 Events Archive, 2010 Events Archive and 2009 Events Archive.
The problem I am having is the position of the dynamically created menu items in the Main Menu tree; they appear at the top level, but I want they appear under the parent Events menu item.
Currently, my menu tree looks something similar to the following.

Home
Events
About Us
Contact Us
2011 Events Archive
2010 Events Archive
2009 Events Archive

It should look like the following.

Home
Events

2011 Events Archive
2010 Events Archive
2009 Events Archive

About Us
Contact Us

I know you can move the menu items using the Menu editor UI when logged in, but as these will be automatically created by the system; they need to appear automatically under Events without human intervention.
How can this be done? Is there an easier way to do this?
Comment: What do you use for the "events" menu item?
Comment: Not sure what you mean? It's just a standard node with a list of current events on it. There needs to be sub menu items for the archived events.
Comment: Usually the parent of a menu item defined in `hook_menu()` is another menu item defined from `hook_menu()` in any module. If "Events" is a menu item that links to a node, then there is a problem.
Answer: Since the Events menu item links to a node, for which events is a path alias, what you are trying to achieve should work if you define the menu items using the following code (I use "11" as node ID).
function MYMODULE_menu() {
  $years = array(2011, 2010, 2009);

  foreach ($years as $year) {
    $items['node/11/' . $year] = array(
      'title' => $year . ' Events Archive',
      'page callback' => 'page_archive',
      'page arguments' => array(1),
      'access callback' => TRUE,
      'type' => MENU_NORMAL_ITEM,
      'menu_name' => 'main-menu',
    );
  }

  return $items;
}

This is necessary because, if events is a path alias for node/11, Drupal doesn't expand events/2011 to node/11/2011. In the same way, it will not expand events/edit to node/11/edit, even thought the Node module defines nodes/%node/edit as one of its menu items.
Comment: can you elaborate more on "This happens because path aliases are not resolved, for paths used inside hook_menu()"?
Comment: If events is a path alias for node/1, events/edit will be not resolved to node/1/edit. When Drupal looks for a path alias, it doesn't look for a partial match. So, with http://example.com/events/edit, Drupal will check if events/edit is a path alias; if it is not a path alias, then it will look for any module that uses events/edit in `hook_menu()`
Answer: <?php
/**
 * GOAL AND STRATEGY:
 *
 * node/6 is a top level menu item in a custom menu called 'menu-main-menu'. I'm
 * creating a child menu item using hook_menu with the path of 'member/pick'. I
 * want this menu item to be rendered as a child of node/6 in the main menu, and
 * I want all of this to happen programatically.
 *
 * Here's how...
 *
 * 1. Create the child item using hook_menu
 * 2. In hook_menu_link_alter, lookup the mlid of node/6 and apply it as the
 * plid of the child item
 *
 */

/**
 * Implementation of hook_menu()
 */
function hook_menu() {
  $items = array();
  $items['member/pick'] = array(
    'title' => 'Member Pick',
    'menu_name' => 'menu-main-menu',
    'page callback' => 'member_pick_page',
    'access callback' => TRUE,
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Implementation of hook_menu_link_alter
 */
function hook_menu_link_alter(&$item) {
  if ($item['page callback'] == 'member_pick_page') {
    $parent_normal_path = 'node/6';
    $plid = db_result(db_query("SELECT mlid FROM {menu_links} WHERE link_path = '%s'", $parent_normal_path));
    $item['plid'] = $plid;
  }
}

Answer: Here's how I do it...
First, create a menu item that will be representing the group:
$items['admin/content/example'] = array(
  'title' => 'Example menu group',
  'page callback' => 'example_page_2011',
  'access arguments' => array('administer nodes'),
);

Then add your menu items below this group:
foreach ($years as $year) {
  $items['admin/content/example/' . $year] = array(
    'title' => $year,
    'page callback' => 'example_page_' . $year,
    'access arguments' => array('administer nodes'),
    'type' => MENU_LOCAL_TASK,
  );
}

Event though it works (tested on 7.22), I still don't like that redundancy in page callback.
If only there was a type called MENU_GROUP... Any better solution is appreciated.


Does Kendo UI PanelBar support HierarchicalDataSource?

Question: Does Kendo UI PanelBar support HierarchicalDataSource?
I have code that works for Kendo TreeView but if instead of a TreeView I tell it to build a PanelBar, no control is constructed.
I am using 2012 Q3 -- Is this even supported or am I trying something that is not implemented?
 var ss = new kendo.data.HierarchicalDataSource({ data: panelBarSource });

 $('#panelBar').kendoPanelBar({
            dataSource: ss,
            dataValueField: "id",
            dataTextField: "name"
        });

panelBarSource resembles:
Answer: Kendo UI PanelBar currently doesn't support hierarchical data source. Alternatively you can use a JSON object to initialize it. Check the docs for more information:
http://docs.kendoui.com/getting-started/web/panelbar/overview#initialize-a-panelbar-using-json-data-object
Answer: As of this writing PanelBar does support hierarchical data model:
Please review the following link:
You'll find it about half way down...


I get linker errors when trying to compile my opengl project using gcc

Question: I am using glfw and glew to create the window and it's giving me linker errors. How can I fix it. This is my first time installing glfw and glew so I could've missed a step in the installation. Any help?
I compile my code using just:
gcc main.c

I know there are some linker flags, but glfw and glew are not a part of them.
I use linux with the gcc compiler.
This is my code:
#define GLEW_STATIC
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GL/glfw3.h>
#include <stdbool.h>

int main(){
   glewExperimental = true;
   if(!glfwInit()){
    printf("failed to initialize GLFW\n");
    return -1;
   }
   glfwWindowHint(GLFW_SAMPLES, 4);
   glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
   glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
   glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
   glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

   GLFWwindow* window;
   window = glfwCreateWindow(1024, 768, "Game", NULL, NULL);
   if(window == NULL){
    printf("Sorry to say this, but your OpenGL version must be 3.3 or above! Thanks for playing, but to continue you must update your video card drivers or if you use an old GPU you may have to replace it with a new one to play this game. I will be developing my game for OpenGL 1 and 2 soon so stay on touch.");
    glfwTerminate();
    return -1;
   }
   glfwMakeContentCurrent(window);
   glewExperimental = true;
   if(glewInit() != GLEW_OK){
    printf("Failed to initilize GLEW");
    return -1;
   }

}

This is the error log:
/usr/bin/ld: /tmp/cc74wuFz.o: in function `main':
main.c:(.text+0xe): undefined reference to `glewExperimental'
/usr/bin/ld: main.c:(.text+0x14): undefined reference to `glfwInit'
/usr/bin/ld: main.c:(.text+0x3d): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.c:(.text+0x4c): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.c:(.text+0x5b): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.c:(.text+0x6a): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.c:(.text+0x79): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.c:(.text+0x9a): undefined reference to `glfwCreateWindow'
/usr/bin/ld: main.c:(.text+0xbb): undefined reference to `glfwTerminate'
/usr/bin/ld: main.c:(.text+0xd3): undefined reference to `glfwMakeContentCurrent'
/usr/bin/ld: main.c:(.text+0xd9): undefined reference to `glewExperimental'
/usr/bin/ld: main.c:(.text+0xdf): undefined reference to `glewInit'
collect2: error: ld returned 1 exit status

Comment: Quite obviously the linker can't find the OpenGL lib. So rather than posting your code, you should describe the steps you took when installing it.
Comment: I just put the header files in /usr/local/include/GL and that's what i did. Please tell me if i need to export the linker files
Comment: I use Linux (Linux Mint if thats importmant) with the gcc compiler. I don't have an IDE.
Comment: Maybe this helps: https://stackoverflow.com/questions/43445942/opengl-glfw-undefined-reference-to-glfwinit
Answer: I suggest to compile with a command similar to
gcc -Wall -Wextra -g $(pkg-config --cflags opengl glfw3) main.c \
     $(pkg-config --libs opengl glfw3) -o prog

Of course (before trying that) you need to install OpenGL related libraries, probably using (as root) the following command
aptitude install libopengl-dev libglfw3-dev

Be sure to read the documentation of GCC and the documentation of GDB and the documentation of binutils and the documentation of pkg-config.
PS. You could get inspiration from the source code of existing open source projects, such as GNU emacs or RefPerSys.


Critical values for Anderson-Darling test

Question: I've found critical values for the Anderson Darling test for a Normal Distribution at 1%, 2.5%, 5%, 10% and 15% significance levels from various sources, including wikipedia:
http://en.wikipedia.org/wiki/Anderson%E2%80%93Darling_test
I'd really like a critical value for a 0.1% significance level (in "case 4" - neither mean nor variance known). I couldn't find it by searching the web, and I am not sure how I should calculate it. Can anyone help?
Answer: You can use simulation (this is not a new idea, it is how Gosset/Student derived the original t table (but we have faster tools than he did)).
Generate a psuedo random sample from a normal distribution (or at least as close as the computer can come) of the sample size of interest and compute the Anderson Darling Statistic for that sample.  Now repeate this process a few million times (or maybe more than a few depending on how precise you want to be).  The 0.1% critical value will be the 0.1% or 99.9% percentile.
However, I have a hard time imagining a useful question that would be answered by an Anderson-Darling test at 0.1% significance.  What is the question that you are trying to answer?
Comment: I see your point - this is an odd thing to do. I'm testing output from a simulation under a very large number of possible configurations, so I expect failures at 1% significance level. I thought of combining output from every configuration to get a single AD* number. I didn't do this because it wouldn't give me any hint as to which individual configuration was buggy, and because  buggy configurations have (so far) failed this test spectacularly. I can see that by using a 0.1% significance level, I will admit some bugs. Perhaps I need to look the distribution of the individual AD* results?
Comment: Re Greg's comment (+1): I wonder how much extreme critical values like .1% are impacted by different means and/or variances of the underlying normal distribution. After all, Matt mentions that he is in "case 4".
Comment: OK, testing simulation results is one of the few cases that testing for matches of an exact distribution makes sense. But rather than testing at such a low alpa, you might consider looking at all your test statitics in some form of plot to look for unusual points (clumps could mean something different as well).
Answer: Using Marsaglia & Marsaglia's code, and a bisection search, one can find that the 0.001 critical value is around 5.9694. This would be for 'Case 1' in the wikipedia article quoted. I am not sure how to convert to 'Case 4'.
Comment: This appears to be an asymptotic value.  A good simulation for your $n$ very well might be more accurate!
Comment: @whuber, indeed it is. Rahman _et. al._ give simulation values for $n=10,20,\ldots,100$ in table 2 of this paper: http://math.usm.my/bulletin/pdf/v29n1/v29n1p2.pdf , although, again, it does not address 'case 4' testing, just 'case 1'.
Comment: Those simulation values don't even have two significant figures of accuracy.  For instance, the line for $n=10$ in Table 2 should read 1.246, 1.624, 1.942, 2.512, 3.108, 3.924, 4.556, 6.044 ($10^7$ iterations).  (The last digit in the higher values is still uncertain.) This took just one minute of computing time; the simulation takes only five times longer for $n=1000$.
Comment: It is troubling that _e.g._ the columns of that table do not appear to be monotonic (I would guess they are), which confirms the lack of accuracy, sadly.
Comment: There is *no* variation (down columns) in that table that cannot be explained by uncertainty due to the small simulations that were performed (merely $10^5$ iterations in each).  (The authors' attempt to find a pattern was doomed from the outset, but apparently they did not understand how much random error is present in this table.) In order to see the variation that might be there, I guess you would need around $10^8$ to $10^9$ iterations in each simulation.


Is it possible for an increasing continuous function on $[0,\infty)$ not to be H$\ddot{\mathrm{o}}$lder continuous at some point?

Question: Let $f$ be a real valued function defined on the domain $[0,\infty)$, such that  $f$ is strictly increasing and $f$ is continuous on $[0,\infty)$. It seems to me that, at any point $a$, in a neighborhood $U$, $f(x)-f(a)$ has to be dominated by something like $(x-a)^\alpha$, for some $\alpha>0$, at any point, i.e. I suspect that this kind of functions will be H$\ddot{\mathrm{o}}$lder continuous at any given point (maybe with different $\alpha$ s). But I cannot materialize this intuition of mine using some formal mathematical techniques.

Is it possible for $f$ not to be H$\ddot{\mathrm{o}}$lder continuous at some point? If it is not possible can anyone give me a hint to the techniques that might be helpful addressing this kind of question?

In case such an example do not exist, please do not give a full answer, but give hint to possible methods that I have to learn so that I can address this question myself. Any help is appreciated.
Edit: By the way, note that at any point where the functions is differentiable and the derivative is finite, the functions is automatically Lipscitz continuous at that point. Also, I suppose, though I am not sure, the points where the functions has one of its Dini derivatives finite, it is Lipscitz at that point. Thus the main points of interest are the points where the function is not differentiable and also the Dini derivatives blow up, which I think leave us the only possibility as the point $0$.
Answer: Hint: Try something like
$$f(x) = \sum_{n=1}^{\infty}c_n x^{1/n}$$
for suitable positive constants $c_n.$
Comment: Oh, right, this is not H$\ddot{\mathrm{o}}$lder continuous whenever $c_n$ is a strictly positive sequence. Nice example!
Comment: Well you have to make sure it's continuous.
Comment: Yes, basically I have to choose such a sequence $\{c_n\}$, that the series $\sum_{n=1}^\infty c_n x^{1/n}$ converges $\forall x>0$, right?
Comment: I suppose a sequence like $1/n^2$ will suffice as most of the terms of $x^{1/n}$ will become very close to $1$ for large $n$, and then the $1/n^2$ will make the sequence converge. Actually any sequence like $1/n^r,\ r>1$ will do. The series will converge by comparison test.
Comment: Further I see that any positive sequence $\{c_n\}$ such that $\sum_n c_n$ converges will do.
Comment: Right, you still have to argue $f$ is strictly increasing (easy) and $f$ is continuous.
Comment: Yes, such a sequence $c_n$ will work I suppose. I thought before about finite sums of powers of $x$, but did not think about infinite sum. Thanks for this example. I suppose then there can be lot of functions, either of this type or of other type which are not Holder continuous at $0$, right? Do you know if there is any general procedure to test for such a thing?
Answer:
It seems to me that these functions have to be dominated by something like $x^\alpha$ for some $\alpha > 0$

False. Take the function $f(x)=e^x$ as a counterexample.

Also, there is no such thing as "Lipschitz continuous at some point". Lipschitz continuity is defined on a set, not at a point:
A function $f:X\to Y$ (where $X$ and $Y$ are metric spaces with metrics $d_X$ and $d_Y$ is Lipschitz continuous if there exists some $K\in\mathbb R$ such that $d_Y(f(x_1),f(x_2))\leq K\cdot d_X(x_1,x_2)$ for all pairs $x_1,x_2\in X$
Comment: By "Lipschitz continuity at a point" $x$, I am really looking at the Lipschitz condition in some neighorhood of $U$ of $x$. I am actually demanding the following condition: $$\sup_{y\in(x-\delta,x+\delta), y\ne x}\frac{|f(x)-f(y)|}{|x-y|}<\infty$$ for some $\delta>0$ and $U=(x-\delta,x+\delta)$. In that sense note that $e^x$ is Lipschitz continuous at $0$, for any neighborhood of $0$. I am assuming similar notion of H$\ddot{\mathrm{o}}$lder continuity too. I am actually more focused on the local behavior of the function, than the global one.
Comment: I have changed the wording of my question a little bit to reflect the type of H$\ddot{\mathrm{o}}$lder continuity I am interested in.
Answer: As it's noted by @5xum, the concepts of Lipschitz continuity and Hölder continuity are related to global behaviour of the function, but I suppose you have an intuition about local properties of $ f $.
So I give a hint to make a counterexample for a local version of your question.
It's possible that $ f $ is not Hölder continuous when restricted to any neighborhood of some point. To make a counterexample, you can construct a function that grows faster than any $ x ^ \alpha $ near $ 0 $. Such function can be defined using $ C ^ \infty $ functions that are not analytic.
If you think this hint in not helpful enough, feel free to ask me to elaborate more.
Comment: Can you  kindly give an example of such a $C^\infty$ non analytic function? I have $e^{-1/x}$ on the top of my head, but is dominated by $x^{1/2}$ near origin.
Comment: In fact any $e^{-1/x^p}$ is dominated by $x^p, \forall p>0$, .
Comment: Try using its inverse.
Comment: Do you mean inverse of $e^{-1/x}$? The inverse is $-1/(\ln x)$, which is, first of all, discontinuous at $1$, and further, is dominated by some $x^\alpha$, when $\alpha\approx 0.1$, near origin.
Comment: Of course the discontinuity is not a problem since you can define $ f $ linearly on $ [ \frac 1 2 , + \infty ) $so that it becomes continuous. The only important thing is the local behaviour of $ f $ near $ 0 $. Your claim about $ - \frac 1 { \ln x } $ being dominated by some $ x ^ \alpha $ is wrong.
Comment: Yes, I was wrong before, near $0$, $-1/(\ln(x))$ does dominate $x^{\alpha}$ for all $\alpha$. I guess I was just being dumb. Thanks. Do you know if there is any particular way to check if a function has this property of being dominated by $x^\alpha $ near $0$?
Comment: I don't know about a general criterion but my idea was that a non-analytic function such that all of it's higher derivatives are zero, is dominated by every $ x ^ n $ and thus its inverse is as desired.
Comment: Hmm, is it possible to find such counter-example if we restrict attention to functions that are analytic at 0?


jade/pug Is it possible to use variables on include statement?

Question: I'm developing nodejs application and I have issue with include statement.
It works when I use it like this:
include ../mixins/root.pug

...but is it possible to use variables on include?
None of these work:
include #{process.env.MIXINS_PATH}/root.pug
include !{process.env.MIXINS_PATH}/root.pug
include `${process.env.MIXINS_PATH}/root.pug`

Result is this:
Error: ENOENT: no such file or directory

Answer: Dynamic includes are not supported:

We don't support "Dynamic Include". There are lots of issues people have opened about this. It's really complex to implement and not actually nearly as useful as people think it would be.

https://github.com/pugjs/pug/issues/2622#issuecomment-270157667


Checkboxes do not get checked

Question: I am trying to retrieve the checked checkboxes whose values are stored in the database, but they are always unchecked even if they exist in the DB.
I have tried this but it did not work, although the same code with different structure works, but for framework reasons, i need to implement using this:
if(isset($sp['Etunimi'])&&isset($sp['Sukunimi'])){
        echo "<form method='post' action=''>";
        $comp=$this->All_Competences;
        echo"
select competences for:".$sp['Etunimi'];
        $id=$sp['Id'];
        $tmp=array();
        if(isset($_POST['select_employee'])){
            $cid=$this->cids;
        }
        foreach($cid as $test)
        {
            array_push($tmp, $test['c_ID']);
        }
        for($i=0;$i<count($tmp);$i++){
        }
        echo "
valid?	Competence description	";

        foreach($comp as $compi){
            $checked='';
            if(in_array($compi['Competence_ID'],$tmp)){
                $checked='checked';

            }
            echo "
<input type='checkbox'".$checked."name='c[]' value='".$compi['Competence_ID']."'>	".$compi['Competence_Description']."
";

        }
        echo "
";
        echo "<input type='hidden' name='action' value='selectchecked'>";
        echo "<input type='hidden' name='id' value='".$id."'>";
        echo "<input type='submit' value='submit checks'>";
        echo "</form>";

Comment: So when you do your `in_array($compi['Competence_ID'],$tmp)){` ... can you `exit($checked);` and see if it's getting set at all there? if not...pretty much know where your issue is..
Comment: try putting spaces around the `checked` keyword
Answer: There has to be spaces between input names, so you need to add some spaces because HTML does not know to interpret it (output is: checkedname='c[]'):
if(in_array($compi['Competence_ID'],$tmp)){
    $checked = ' checked ';
}

Answer: Try this:
 echo "
<input type='checkbox' checked='".$checked."' name='c[]' value='".$compi['Competence_ID']."'>	".$compi['Competence_Description']."
";

Seems you missed: checked=".$checked
Comment: checked will work in most cases, but is not allowed in XHTML so it's better practice to use `checked="checked"`
Comment: Nope. You just need to print `checked`. The existence of the attribute is the assumption in and of its self.
Comment: He is echoing `checked`.. its `checked=` what is missing
Comment: OH god.. why :) you are right but still the line I suggested should work. As he is not putting spaces in between `name` and `type='checkbox'`
Comment: @Mark You're worried about a WC3 Validation error on an oudated Markup System? Why would you want to move backwards rather than forwards. Sorry, I'm moving forward, not backwards. I haven't built an XHTML Transition/Strict/Frameset site in years. I don't know why *anyone* would.
Comment: Are you claiming writing html according to the xml standard is moving backward? And I'm really wondering how you were building sites before, since HTML 5 is not even considered stable yet.
Answer: Add a space beween checked and name.
echo "
<input type='checkbox' checked='".$checked."' name='c[]' value='".$compi['Competence_ID']."'>	".$compi['Competence_Description']."
";

OR
echo "
<input type='checkbox' ".$checked." name='c[]' value='".$compi['Competence_ID']."'>	".$compi['Competence_Description']."
";

Checkboxes do not get checked


Search and Replace in a file using arraylist, Java

Question: I wrote the below part of the code but I couldn't bind the arraylist with search and replace
so my csv file is as like below
1/1/1;7/6/1
1/1/2;7/7/1
I want to search the file 1.cfg for 1/1/1 and change it to 7/6/1 and 1/1/2 change to 7/7/1 and it goes so on.
Thank you all in advance
It's now only printing in a new file only the last line of the old File
import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class ChangeConfiguration {

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args)
    {
         try{
              // Open the file that is the first
              // command line parameter
              FileInputStream degistirilecek = new FileInputStream("c:/Config_Changer.csv");
              FileInputStream config = new FileInputStream("c:/1.cfg");
              // Get the object of DataInputStream
              DataInputStream in = new DataInputStream(config);
              DataInputStream degistir = new DataInputStream(degistirilecek);
              BufferedReader br = new BufferedReader(new InputStreamReader(in));
              BufferedReader brdegis = new BufferedReader(new InputStreamReader(degistir));
              List<Object> arrayLines = new ArrayList<Object>();
              Object contents;
            while ((contents = brdegis.readLine()) != null)
              {
              arrayLines.add(contents);
              }
              System.out.println(arrayLines + "\n");
              String strLine;
              //Read File Line By Line
              while ((strLine = br.readLine()) != null)   {
                 //Couldn't modify this part error is here :(
                  BufferedWriter out = new BufferedWriter(new FileWriter("c:/1_new.cfg"));
                    out.write(strLine);
                    out.close();
              }
                      in.close();
              degistir.close();
                }catch (Exception e){//Catch exception if any
              System.err.println("Error: " + e.getMessage());
              }
              }
}

Comment: What is `function replace(arrayName,replaceTo, replaceWith)`? This will not compile in Java.
Comment: Well, first of all if you want to modify a file you'll need to write to it.  And you can't write to a `Reader` or `InputStream`, you need a `Writer` or `OutputStream`.
Comment: opps yes that function part is my problem i couldn't write that part for java that gathering strings from array and replacing it.
Comment: @Mark I took for now into buffer but i am planning lateron to take from buffer after it is changed and writing a new file.I didn't add that part yet.But it will work like that right ?
Answer: You are opening the file for reading when you declare:
BufferedReader br = new BufferedReader(new InputStreamReader(in));

If you know the entire file will fit in memory, I recommend doing the following :

Open the file and read it's contents in memory into a giant string, then close the file.
Apply your replace in one shot to the giant string.
Open the file and write (e.g use a BufferedWriter) out the contents of the giant string, then close the file.

As a side note, your code as posted will not compile.  The quality of the responses you receive are correlated with the quality of the question asked.  Always include an SCCE with your question to increase the chance of getting a precise answer to your question.
Comment: yes I also thought like that to open and buffer the file (by the way file is around 500kb) change the strings and then write as a new file but I didn't add that part now. I am now trying to search replace from array but couldn't manage it.I should also trim the array right ?
Comment: You're missing the point, whatever change you make to the array will not be reflected in the file.  You can change whatever you want to the array, and the file will be the same.
Answer: can you elaborate the purpose of the program?
if it is a simple content replacement in a file.
then just read a line and store it in a string. then use string replace method for replacing a text in a string.
eg:
newStrog=oldString.replace(oldVlue,newValue);
Comment: so you mean that I should change all the bufferedfile into a string ? How can i do that ?


Arguing that $\lim_{r \to 0}\int_{C_{r}}f(z) \, dz = i \pi \operatorname{Res}[f(z), z_{0}]$, where $C_{r}$ is a semicircle of radius $r$

Question: I recently made the following observation:
Assume that the function $f(z)$ can be expressed near $z=z_{0}$ in the form
$$ \sum_{k=-n}^{0} a_{2k-1} (z-z_{0})^{2k-1} + g(z) \, ,$$
where the function $g(z)$ is analytic at $z_{0}$.
If $C_{r}$ is a counterclockwise-oriented semicircle of radius $r$ centered at $z_{0}$, then
$$\lim_{r \to 0} \int_{C_{r}} f(z) \, dz = i \pi  \, \text{Res}[f(z),z_{0}].$$
Is my observation correct?
Attempt at a proof:
$$ \begin{align} \int_{C_{r}} f(z) \, dz &= \int_{\alpha}^{\alpha + \pi}f(z_{0}+re^{it}) \ i r e^{it} \, dt \\ &= \int_{\alpha}^{\alpha + \pi} \left(\sum_{k=-n}^{0} a_{2k-1} (re^{it})^{2k-1} + g(z_{0}+re^{it}) \right) i r e^{it} \, dt \\ &= i \sum_{k=-n}^{-1}a_{2k-1} r^{2k} \underbrace{\int_{\alpha}^{\alpha + \pi} e^{2ikt} \, dt}_{0} + i a_{-1} \int_{\alpha}^{\alpha + \pi} \, dt + i r \int_{\alpha}^{\alpha + \pi} g(z_{0}+re^{it}) \, e^{it} \, dt \\  &= i \pi a_{-1} + i r \int_{\alpha}^{\alpha + \pi} g(z_{0}+re^{it}) \, e^{it} \, dt \end{align}$$
And since $|g(z)|$ is bounded near $z_{0}$,
$$ \lim_{r \to 0} \int_{C_{r}} f(z) \, dz = i \pi a_{-1} + 0 = i \pi \, \text{Res}[f(z),z_{0}]$$
Comment: Probably simpler: note that $(z-z_0)^k$ has the primitive $(z-z_0)^{k+1}/(k+1)$ for $k \neq -1$. For odd $k$, that is an "even" function about $z_0$, hence the integral vanishes since the end-points are symmetric about $z_0$. All that remains is $k = -1$ and the bounded $g$.
Comment: Strange. It's pretty clear, I had no problem understanding it. Oh, and just for the record, your argument is correct except that you dropped a factor $ir$ on the first occurrence of the integral of $g$. And a factor $i$ on the last occurrence of the $a_{-1}$ integral.
Answer: As Daniel Fischer said, the computation is correct (now that the typos were fixed). Here is one more proof.
Assume $z_0=0$ for simplicity. Also ignore the $g$ part because its contribution is small (bounded by $\pi r \sup|g|$, which tends to $0$). Let $C$ and $C'$ be complementary half-circles of circle $|z|=r$. The change of variables $z=-\zeta$ yields
$$\int_{C'} f(z)\,dz = \int_C -f(-\zeta) \,d\zeta$$
Notice that $f$ is an odd function: $-f(-\zeta)=f(\zeta)$. Hence
$$
\int_{C'} f(z)\,dz = \int_{C} f(z)\,dz
$$
The sum of these integrals is $\int_{|z|=r} f(z)\,dz = 2\pi i \operatorname{Res}(f,0)$. The conclusion follows.


How to get the value of a datetime when form submitted

Question: I am writing a custom module in which i am declaring a $form element as #type=> 'datetime'
The code for the buildForm method is
 $form['timesave'] = array(
    '#type' => 'datetime',
    '#title' => $this->t('Select Date and time'),
  );

Now my question is how do we get the value of the field on form Submit as i have to store the value in a configuration
I have tried with
$form_state->getValue('timesave');

This is giving an array some protected methods.
Comment: For drupal 8, see [this similar question and the responses there](https://drupal.stackexchange.com/questions/254523/how-can-i-get-the-value-a-field-type-date-using-form-state-getvalue).
Answer: You can use the method getTimeStamp() to store the timestamp like this :
$form_state->getValue('timesave')->getTimeStamp();

Or you can use the format() function to store a formatting date.
$form_state->getValue('timesave')->format("Y-m-d H:i:s");

Comment: Yup.  Basically, the return value from $form_state->getValue('timesave') is a  [DrupalDateTime](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Datetime%21DrupalDateTime.php/class/DrupalDateTime/8.3.x) object, so any of the public methods/properties will work.
Comment: doesn't work like that... at least in Drupal 8.


Using cartesian coordinates how to get the segment overlapping two lines/segments?

Question: There must be an algorithm to find the coordinates of the segment overlapping (fully or partially) two lines/segments but my googling does not produce any significant result. Maybe I don't use the right keywords? I tried overlapping, segment, line, collinear but nothing of interest was returned.
Does anyone know of such an algo, the name, the implementation, whatever that could help me point toward a solution?
Or does anyone know how to compute the overlapping segment?
For example, having line L1 with P0 (0,0) and P1 (5,5) and line L2 with P2 (3,3) and P3 (10,10) the resulting overlapping segment would be (3,3) to (5,5).
Answer: If $L_1$ and $L_2$ are not parallels lines, so there is not any overlap; you can find the coordinates of intersection by solving a 2x2-system (here the Cramer's rules could be usefull).
Else, if the line is not parallel to y-axis: you can do the staff on the real line with the x's of the points and then calculate the corresponding y's (how do you compute the overlap between $[0;5]$ and $[3,10]$?).
If the line is parallel to y-axis, then simply work on the y's of the points.


Print 40 messages in one second. Prints have to be spread evenly across one second

Question: The code below prints "Hello" 40 times every second. I would like these 40 prints to be spread evenly across one second. As it stands at the moment the 40 prints happen in the first few milliseconds and the the program sleeps the rest of the time.
import time

start = time.time()
count =0

while True:
    print("Hello")
    end = time.time()
    count +=1
    if (end - start) <= 1 and count == 40:
        sleep_time = 1 - (end - start)
        time.sleep(sleep_time)
        count = 0
        end = 0
        start = time.time()

Comment: @AlexandreB. This assumes that the print itself is instant which depending on the system, etc. might be far from the truth. So before you *space* the `prints` out in time, you have to subtract 40 times the duration of 1 such print and divide the result with 39.
Comment: Update the `sleep_time` at the desired frequency ? Here `1/40` ?
Comment: "Evenly" is impossible in a high level language. You will always have nano to microseconds (even mili) of divergence in time.
Answer: The if statement containing your sleep call won't run until count reaches 40, which is clearly not what you r want. Clearly, sleep should always be called. You can make a self-adjusting timer by making the sleep interval a function of the remaining prints and time:
duration = 1.0
target = 40

start = time.time()
while True:
    print("Hello")
    target -= 1
    if target:
        time.sleep((duration + start - time.time()) / target)
    else:
        break

Comment: Anyone else missing a `sleep_until` to be able to take into account compute time?
Comment: @Adirio. That would require polling, which increases commutation time.
Comment: No, it just computes the time to sleep based on the target date. This means that if your task duration is variable you would still be doing periodic tasks. Lets say you want to run a task every second (T=1s) and that the task takes between 10 and 500 ms. With sleep, at t=0 you run your first task which takes t_1 (10-500ms), then you sleep for a second. So the second task starts at t=1+t_1 and takes t_2 (10-500ms), then you sleep for a second. Task 3 starts at t=2+t_1+t_2, task 4 at t=3+t_1+t_2+t_3, .... As you can see the error is getting higher.
Comment: `sleep_until` instead computes the starting time and adds the period T to compute the target time for the next task, executes this task, gets the new time and sleeps the remaining duration until the next task. This way they will always start at t=0, t=1s, t=2s, t=3s unless the task takes longer than T, but we already said that T=1s and the tasks take as much as 0.5s.
Comment: This is how periodic tasks are done in RT (real time) systems. RT oriented programming languages, which obviously Python is not one of, give you the `sleep_until` function already and you do not have to compute all those times, just add 1 period every task and sleep until that time. Thats is why I said I miss `sleep_until`.


So many significant explanatory variables and so small auc

Question: Have you ever seen a model with almost every significant variable and such small auc (area under the ROC curve) ? What might be the cause of it? When I saw summary of a model I thought this model will have a great performance, but when I make predictions for observations from the test set it appears that the auc is very small, which means that the prediction is very poor.
Can anyone suggests any explanation of such situation and maybe give some advices on how to make an impact on model performance in terms of making auc higher?
My model summary
Call:
glm(formula = cliks012 ~ hours + primaryhardwaretype + browsername +
    osname + age_group_troll + plec, family = "binomial", data = train)

Deviance Residuals:
    Min       1Q   Median       3Q      Max
-0.5917  -0.1958  -0.1900  -0.1851   3.4862

Coefficients:
                                Estimate Std. Error z value Pr(>|z|)
(Intercept)                    -6.170726   0.127305 -48.472  < 2e-16 ***
hoursEvening                   -0.016045   0.011402  -1.407 0.159380
hoursMorning                    0.063541   0.011190   5.678 1.36e-08 ***
hoursNight                      0.122892   0.022960   5.352 8.68e-08 ***
primaryhardwaretypeMobilePhone  2.050345   0.089335  22.951  < 2e-16 ***
primaryhardwaretypeOther        0.213355   0.215761   0.989 0.322738
primaryhardwaretypeTablet       2.066480   0.124671  16.575  < 2e-16 ***
browsernameChrome               0.032222   0.099976   0.322 0.747226
browsernameFirefox              0.111669   0.099724   1.120 0.262805
browsernameInternetExplorer     0.057690   0.100636   0.573 0.566473
browsernameOther                0.187177   0.101820   1.838 0.066017 .
browsernameSafari               0.042307   0.111451   0.380 0.704241
osnameiOS                      -0.003356   0.109021  -0.031 0.975444
osnameLinux                     2.132874   0.144926  14.717  < 2e-16 ***
osnameLinuxUbuntu               2.354108   0.147236  15.989  < 2e-16 ***
osnameOSX                       2.351537   0.138428  16.987  < 2e-16 ***
osnameOther                     2.110826   0.107162  19.698  < 2e-16 ***
osnameWindows7                  2.168022   0.132195  16.400  < 2e-16 ***
osnameWindows8                  2.103507   0.135373  15.539  < 2e-16 ***
osnameWindows81                 2.197786   0.132560  16.579  < 2e-16 ***
osnameWindowsVista              2.136239   0.133530  15.998  < 2e-16 ***
osnameWindowsXP                 2.121562   0.132499  16.012  < 2e-16 ***
age_group_trollOldTroll         0.008260   0.052773   0.157 0.875619
age_group_trollWorker          -0.097919   0.015123  -6.475 9.49e-11 ***
age_group_trollYoung           -0.109535   0.021494  -5.096 3.47e-07 ***
age_group_trollYoungTroll      -0.067748   0.087009  -0.779 0.436197
plecM                           0.045470   0.012042   3.776 0.000159 ***
plecO                           0.071644   0.024581   2.915 0.003561 **
plecX                          -0.109337   0.052179  -2.095 0.036135 *
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 490133  on 2699999  degrees of freedom
Residual deviance: 488984  on 2699971  degrees of freedom
AIC: 489042

Number of Fisher Scoring iterations: 7

And and auc computations
library(ROCR)
auc <- function(pred_probs, real_classes){
  pred <- prediction(pred_probs, real_classes)
  performance(pred, "auc")@y.values[[1]]
}

preds <- predict(modelGLM_clicks012, newdata = test, type="response")
> auc(preds, test$cliks012)
[1] 0.5230328

Comment: How many observations do you have?
Comment: 2,7 mln in Train set and 2,76 mln in test set
Answer: Although there are many "significant" explanatory variables in your model, note in the summary of your glm model that the deviance was barely diminished by that model. (Residual deviance was almost the same as the null-model deviance.) With millions of cases as you have here, explanatory variables can be "significant" in terms of the standard criterion of p < 0.05 but still not be very important in terms of explaining the distribution of the observed dependent variable. The low AUC, at first glance, seems completely consistent with the glm output.


A property of the Borel measure

Question: So I've been stuck on the following question for a while now and was hoping that someone could maybe give me a hint to the following question.
(please note that i mean outer measure for $\lambda^*$)
Suppose $A \subset \mathbb R$ is a Borel set with $\lambda(A) > 0$. Using the fact that $\lambda(A) = \lambda^*(A)$, show that for
any $\epsilon > 0$ there exists a non-empty interval $I$ with $\lambda(A \cap I) \ge (1 - \epsilon) \lambda(I)$.
Comment: Can you include your definition of $\lambda_*$ (I'm guessing inner measure)?
Comment: outer measure sorry
Answer: The result is trivial if $\epsilon \ge 1$, so assume that $1 - \epsilon > 0$.
You don't need $A$ to be a Borel set, or even Lebesgue measurable. Assume that $A \subset \mathbb R$ and that $\lambda^*(A) > 0$.
Suppose, to the contrary, that $\lambda^*(A \cap I) < (1-\epsilon) \lambda(I)$ for every interval $I$.
Let $\{I_k\}$ be an arbitrary cover of $A$ by bounded intervals. Then
$$\lambda^*(A) = \lambda^* \left( A \cap \bigcup_k I_k \right) = \lambda^* \left(\bigcup_k (A \cap  I_k) \right) \le \sum_k \lambda^*(A \cap I_k)$$
where the last inequality uses the subadditivity of the outer measure. According to the hypothesis made above, it follows that
$$\lambda^*(A) < (1-\epsilon) \sum_k \lambda(I_k) = (1-\epsilon) \sum_k \ell(I_k).$$
Now take the infimum over all such coverings $\{I_k\}$ of $A$ to obtain
$$\lambda^*(A) \le (1-\epsilon) \lambda^*(A)$$ by the definition of the Lebesgue outer measure.
This contradicts the hypothesis that $\lambda^*(A) > 0$.

A property of the Borel measure


Python script with serial port will run once only

Question: This is my first post.
The following code runs fine in IDLE but when it has finished and run again it gives a permission error... Is this a code error or is there a problem with the hardware and/or driver?
<code>   import serial
   import time

   ser = serial.Serial('COM20', 9600, timeout=1)
   print('port open is ' + str(ser.is_open))
   time.sleep(2)
   print('going to close')
   ser.close()
   print('port open is ' + str(ser.is_open))
   time.sleep(2)
</code>
Result:
port open is True
going to close
port open is False
Error message:
....
....\Python310\lib\site-packages\serial\serialwin32.py", line 222, in _reconfigure_port
raise SerialException(
serial.serialutil.SerialException: Cannot configure port, something went wrong. Original message: PermissionError(13, 'A device attached to the system is not functioning.', None, 31)
When I Disable/Enable COM20 the program will run again but throws the error when run again.
The comport is a CH340 device, Driver version 3.8.2023.2, no data is sent either way.
It runs under Windows 10.
I tried a reboot, re-installed the driver, assign the port to COM4, used different delays.
Inserted ser.open() at the end of my code which also fails with the same messages on the first run.
Answer: I solved the problem by installing an old CH340 driver 3.4.2014.8 (Aug 2014)


Inferring distribution from summary statistics

Question: A study reports that a sample of $276$ independent integer measurements from the closed range $[0, 65]$ has a sample mean of $21.39$ and a sample standard deviation of $65$.
Is it possible to infer anything about the shape of the sample distribution from this information only?
(Asking out of curiosity while reading some study reports)
Comment: It sounds like an error. The maximum possible standard deviation on values in [0,65] with a mean of 21.39 seems to be about 30.56. I'd expect it's probably no more than 10-12 and possibly a good deal less. 6.5 *might* be a reasonable standard deviation but a missing decimal point is merely a guess.
Answer: No, you can estimate a SD from any distribution or any vector of numbers, and it does not help you to identify a distribution. Also, how can the SD be equal to 65, given that the range is in [0,65]?
Comment: "Also, how can the SD be equal to 65, given that the range is in [0,65]?"
A typo perhaps? Or fabricated numbers?


Find all topology on finite set in especial case

Question: Let $X$ be finite set, Find all topology on finite set with this condition:

For every subset $A \subset X$, either $A$ is open or $A$ is closed.

I find one of them:
$$T=\{ \emptyset, X, \{1\}, \{3\},...,\{n\},\{1,3\},\{1,4\},...\{1,n\},\{1,3,4\},...,\{1,3,4,...,n\}\} = \{A \subset X \bigm| A=X \, \text{or} \, 2 \not\in A \}
$$
It is clear that $T$ is topology and $T \neq P(X)$. Note that discrete topology is a trivial answer. So find the number of all topology with above condition.
Comment: @JendrikStelzner: "either" is used in English to mark the beginning of a disjunction for emphasis. "Either" does not signify exclusive disjunction.
Comment: @Jendrik: English *either ... or* is not quite equivalent to German *entweder ... oder*; it can (and often does) mean exclusive OR, but it need not.
Comment: Note that every subset of X must be open or close , if we say every subset of X is  open and close then answer is only discrete topology.
Comment: Actually the $T$ you've written _is_ the discrete topology, though written incompletely since you have written all singletons as opens sets.
Comment: No in T $\{2\}$ is not open
Comment: @amirbahadory: Your example contains $\{3\}$ and $\{4\}$ but not $\{3,4\}$, so it is not a topology.
Comment: No in my example T contain all 2_union of Singleton, and all 3-union of Singleton , and etc, (note that 2-union is all $\{a,b\}$ that a,b aren't 2, and 3-union is all $\{a,b,c\}$ that a,b,c aren't 2, and ...}
Comment: I have edited your question for grammar, punctuation, etc., and to clarify the definition. Let me know if this is not OK.
Comment: @LeeMosher You replaced "open or close" with "either […] open or […] closed", changing the problem itself. In particular the discrete topology, which was mentioned as a trivial answer, is not a solution to the new problem.
Comment: @JendrikStelzner: The discrete topology is the one for which every subset $A$ is open, and so certainly it is true that every subset $A$ either $A$ is open or $A$ is closed.
Comment: @LeeMosher: Doesn't "either […] or […]" usually mean exclusive disjunction? If you just mean (inclusive) disjunction why use "either"?
Answer: A topology on a finite set $X$ is the same as a preorder: given a preorder $\leq$, the collection of all sets $U$ such that $x\in U$ and $y\geq x$ implies $y\in U$ is a topology, and every topology comes from a unique preorder in this way.
The condition that every singleton is either open or closed means that $\leq$ is a partial order (i.e., $x\leq y$ and $y\leq x$ implies $x=y$) and every element of $X$ is either a minimal element or a maximal element.  Now suppose $x$ is a maximal element which is not minimal and $y$ is a minimal element that is not maximal.  If $\{x,y\}$ is open, then no element other than $x$ can be $>y$, and since $y$ is not maximal, we must have $x>y$.  Similarly, if $\{x,y\}$ is closed, then $y<x$ and $y$ is the only element that is $<x$.  So we must have $x>y$ and either $x$ is the only element $>y$ or $y$ is the only element $<x$.
It now follows easily that there must either be at most one maximal element that is not minimal or at most one minimal element which is not maximal.  Suppose $x$ is the unique maximal element that is not minimal.  Then every minimal element that is not maximal must be $<x$, and this completely determines the order.  In detail, we can split $X=A\cup B\cup\{x\}$, where $A$ is the set of points which are both maximal and minimal, and $B$ is the set of points which are minimal but not maximal, and then the order relation on $X$ is defined by $s<t$ iff $t=x$ and $s\in B$.  Dually, if $X$ has a unique minimal element $y$ that is not maximal, we can split $X=A\cup C\cup\{y\}$, and $X$ is ordered by $s<t$ iff $s=y$ and $t\in C$.  Finally, if $X$ has no minimal element that is not maximal or maximal element that is not minimal, then $X$ has the trivial order (i.e., any two distinct elements of $X$ are incomparable).
You can check that in all three cases, every subset of $X$ is either open or closed.  In the case where $X=A\cup B\cup\{x\}$, a set is open if it contains $x$, and closed if it does not contain $x$.  In the case where $X=A\cup C\cup\{y\}$, a set is closed if it contains $y$, and open if it does not contain $y$.  In the case that $X$ has the trivial order, every set is open and closed.