Consuming # subscription via MQTT causes huge queue on lost connection

Question: I am using Artemis 2.14 and Java 14.0.2 on two Ubuntu 18.04 VM with 4 Cores an 16 GB RAM. My producers send approximately 2,000 Messages per seconds to approx 5,500 different Topics.
When I connect via the MQTT.FX client with certificate based authorization and do a subscription to # the MQTT.FX client dies after some time and in the web console I see a queue under # with my client id that won't be cleared by Artemis. It seems that this queue grows until the RAM is 100% used. After some time my Artemis Broker restarts itself.
Is this behaviour of Artemis normal? How can I tell Artemis to clean up "zombie" queues after some time?
I already tried to use this configuration parameters in different ways, but nothing works:
confirmationWindowSize=0
clientFailureCheckPeriod=30000
consumerWindowSize=0

Comment: How is this different from [your other question on this subject](https://stackoverflow.com/questions/63158612/apache-artemis-master-slave-configuration-ram-consumption-increases-continuousl)?
Comment: It different because artemis is configured as single node with persistence=false and Artemis restarts itself when the ram is used to 100%. Currently I am heavily struggling because I have no clue why Artemis behave like this. I want to find a solution to run my/our application - the other option is to give it up and find an other  broker software.
Comment: One thing I found out the last two days is, that mqtt.fx seems to have a bug. But IMHO should Artemis have some kind of "zombie queue killer" that notice that no consumer is connected to the "zombie queue". And after some a specified timeout the zombie killer purge & delete the queue.
Answer: Auto-created queues are automatically removed by default when:

consumer-count is 0
message-count is 0

This is done so that no messages are inadvertently deleted.
However, you can change the corresponding auto-delete-queues-message-count address-setting in broker.xml to -1 to skip the message-count check. Also, can adjust the auto-delete-queues-delay to configure a delay if needed.
It's worth noting that if you create a subscription like # (which is fairly dangerous) you need to be prepared to consume the messages as quickly as they are produced to avoid accumulation of messages in the queue. If accumulation is unavoidable then you should configure the max-size-bytes and address-full-policy according to your needs so the broker doesn't get overwhelmed. See the documentation for more details on that.
Comment: Hi Justin,

I have tested it for some days now with different amount of messages an subscribers/consumers. Now it seems to work.

I configured the address setting like this:

auto-delete-queues-message-count=1
auto-delete-queues-delay=0
auto-delete-created-queues=true
default-purge-on-no-consumers=true
max-size-bytes=10485760
page-size-bytes=1048576
page-max-cache-size=2

After some days of testing an monitoring it solves my problem of growing and not "deleted" zombie queues.

Thank you very much Justin!
Comment: I believe the only setting here which actually makes a difference here is `default-purge-on-no-consumers=true`. This tells the broker that once all consumers disconnect from the queue it should delete all the messages in the queue and stop routing any *new* messages to the queue until a consumer reconnects.


Bringing out the numbers from a list that are smaller than an integer/float

Question: I have to make a program in Python 3.4 that let's me input numbers and names that go into a list, and then I have print out the numbers that are less than average of the sum. I've been stuck with the last part as I don't know how to print out only the numbers that are less than average of the list.
Let's say the list is [190, 188, 185, 174, 172] and the average is 181.8, how can I get it working?
//edit I got the first part working with this

uus_list = []

for x in pikkused:

for y in kesk_list:

if x < y:
uus_list.append(x))

However, I got another problem. Is there a way to tie 2 lists together?
Comment: Since this is probably a homework assignment I don't want to do it for you. But assuming you know the average already, I suggest looking into [list comprehensions](https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions) and their built in filtering abilities. Or if you're feeling functional, the [filter](https://docs.python.org/3/library/functions.html#filter) method
Answer: num = [190, 188, 185, 174, 172]
avg = sum(num)/len(num)
[ x for x in num if x < avg ]

here in my code sum function will give you sum of all element of list num. len will give you no of element in num.
Comment: Sadly you have just done someones how work for him - how will they learn if they just come here and get someone else to do it for them ?
Comment: sorry yeaa they need to try and give what they have tried


Doctrine2 join column

Question: I have defined the follow entity in doctrine2 (with symfony).
/**
 *
 * @ORM\Table(name="order")
 * @ORM\Entity
 */
class Order

 /**
 * @var integer
 *
 * @ORM\Column(name="personid", type="integer", nullable=false)
 */
private $personid;

 /**
 * @ORM\OneToOne(targetEntity="People")
 * @ORM\JoinColumn(name="personid", referencedColumnName="personid")
 */
private $person;

public function getPersonId()
{
    return $this->personid;
}
public function getPerson()
{
    return $this->person;
}

}

I realize that if I call $order->getPersonId() it return always an empty value and I have to call the getPerson()->getId() method to get the correct personid.
Could anyone explain me why the variable $personid is not filled?
Should I to delete the column id used for the join if I defined one?
Thanks
Gisella
Answer: You should remove private $personid;, it's better to work with objects only in an ORM.
It's not a problem if you get the ID with $order->getPerson()->getId(), because Doctrine won't load the complete entity. The People entity will only be loaded if you call an other field than the join key.
You can still have a getter shortcut like this :
public function getPersonId()
{
    return $this->getPerson()->getId();
}

Edit :
You can also still work with "ID" if you use Doctrine references, like  this :
$order->setPerson($em->getReference('YourBundle:People', $personId));

With this way, Doctrine won't perform a SELECT query to load data of the person.
Comment: Thank you for your response. You suggested a nice workaround. Anyway I use to set the $personid instead of the People object, usually when I create new Order object. Is it still correct?
Comment: I updated my answer : you can use `getReference()` with the `EntityManager` if you want to work only with your ID.
Answer: You don't need to have the $personid field when you already have the $person field.
$people contains the People object (with all People's attributes including the id).
Moreover, when doctrine translate your object into sql tables, he knows that he have to join with th id so it will create a field (in database) named personid. (It's the name that you defined in your ORM)
  /**
 * @ORM\OneToOne(targetEntity="People")
 * @ORM\JoinColumn(name="personid", referencedColumnName="personid")
 */
private $person;

Sorry for bad english :p

Doctrine2 join column


Let A and B be subspaces of $R^5$ such that A $ \subseteq$ B

Question: Let A and B be subspaces of $R^5$ such that A $ \subseteq$ B.
$ \{a1,a2,a3\} $is a basis for subspace A.
$\{b1,b2,b3,b4\} $ is a linearly dependant set that spans B.
$ \quad $  (1) Dimension of B = 4
$ \quad $ (2) A = B
$ \quad $ (3) $\{b1,b2,b3,b4\}$ is a basis for B.
$ \quad $ (4) Dimension of A = 3
I think that only (4) would be true, (1) and (3) are untrue because the vectors $b_i$ are linearly dependent, but I am not sure if there is a way to prove that B $\subseteq$ A, which would make (2) true? Please correct me if I am wrong. Thank you
Answer: 1 is false. The $b$ vectors are linearly dependent and span $B$, so you could remove one and still span $B$, so you could span $B$ with less than 4 vectors, so dimension is less than 4.
3 is also false given the same logic as for 1)
4 is true because 3 vectors form a basis of A.
Now, note that since A is a subset of B, B must be dimension 3. Any 3 linearly independent vectors in B then are a basis of B. The $a$ vectors are linearly independent and in B, and there are 3 of them, so they are also a basis for B. Therefore A and B share a basis, so $A = B$, so 2 is true.
Remember the rules for a list of vectors being a basis: Must have two out of three of the following:

Spanning
Linearly independent
List length the same as the dimension of the space

If you have two, the other one is always implied.
TL;DR 2 and 4 are true, 1 and 3 are false, but do try to understand the reasoning why
Answer: You are wrong that only $4)$ has to be true, (which it is since the dimension is the number of elements in a basis).  There is  a way to deduce that $B\subset A$:  since the $\{b_1,b_2,b_3,b_4\}$ are dependent, it follows that $\rm{dim}B=3$.  Thus $2)$ is also true.
Answer: Some very important facts about spanning sets and linearly independent sets (in finite dimensional spaces) are

A spanning set has at least as many elements as any linearly independent set.
Any spanning set contains a basis.
Any linearly independent set of vectors that has as many elements as the dimension is a basis.

You're given that $B$ has dimension at least $3$, because it contains the linearly independent set $\{a_1,a_2,a_3\}$. The dimension of $B$ can therefore be at most $4$, because it has a spanning set with four elements and we can apply fact 1.
However, you're also given that this spanning set is linearly dependent, so the basis it contains (fact 2) cannot be the whole set. It follows that the dimension of $B$ is at most $3$.
Hence the dimension of $B$ is exactly $3$. Apply fact 3 to $\{a_1,a_2,a_3\}$, which is a linearly independent set in $B$ to conclude that $A=B$.


check old ldap password if it is expired

Question: I am facing some problem.
In LDAP for admin I set the maximum password age pwdMaxAge to 6 mins for testing. When the user tried to login after 6 mins(after the password expire time), I am showing to re-set the password.Then user enters old passowrd with new password. I am validating if the user enters correct old password using below code.
<code>ldapConnection.bind(LDAPConnection.LDAP_V3, getDN(jurNm, username),
                new String(oldPassword).getBytes(),constraints)
</code>
but it is throwing exceptions.
"LDAPException: Invalid Credentials (49) Invalid Credentials
LDAPException: Matched DN: "
could you please help me on this?
Answer: You're using the password policy overlay. To change an expired password you need to use the extended password-change operation, where you supply the old password and the new one, and you have to do it as an admin user whose password has not expired.
Also, to get password-expiry warnings, grace period notifications, etc, you need to use the password policy request control, and examine the response controls that are sent with a successful bind or an exception.
Comment: Here the admin user whose password is expired and when he tries to login using old password then I am showing re-set password page.

So you are saying if the admin user password expired, the same admin user can not re-set his password?
Comment: The admin user probably shouldn't have an expiring password at all. You should certainly not be *testing* with the admin user, but with an ordinary user. In the case of an admin user you may need to execute the code as the rootDN.
Comment: We have to admin group

1. Super Admin

2. Admin,

for super admin we don't have password max age but, for admin we have max password age. And for the admin whose password expired can re-set the password if it expired.
Comment: my question is, how to check if the entered old expired password  is exist in ldap or not using the above novel.ldap?
Comment: And, for the third time, I've answered it. Extended password-change operation and password policy request control.


htaccess com url amigável não está funcionando(?)

Question: Eu estou terminando um projeto, e pensei em colocar uma URL amigável, uma que eu já tinha usado em outro projeto, a mesma coisa, não mudei nada, apenas o server e host do site.
De: www.site.com/noticia?id=1 para www.site.com/noticia/1
porém, ele está adicionando um "/" no final de tudo (www.site.com/noticia/1/), o que está dando a página "NOT FOUND".
No outro projeto que mencionei acima, ele está igual, mesmo código, só mudei as informações que vai buscar e retornar da página que exibi o conteúdo, porém o último "/" está dando o conflito, e não faço ideia de como retirar isso.
Procurei em vários sites para ver se acha alguma forma de retirar ou até mesmo outro que fazia a mesma coisa, porém, sem sucesso, procurei aqui também e nada.
Eu só não sei se a host tem alguma influência nisso, já que eu não costumo mexer muito com o .htaccess.
No wamp ele roda corretamente, mesmo sem o .htaccess, porém, na host da esse problema.
O .htaccess está assim
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1.php/$2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
ErrorDocument 403 //403.php
ErrorDocument 404 //404.php
ErrorDocument 500 //500.php

O Código da página de exibição:
<?php
include_once "assets/inc/head.php";
include_once "assets/inc/busca.php";
$Carro =  $_SERVER['PATH_INFO'];
$CarroN = $_SERVER['PATH_INFO'];
$CarroL = $_SERVER['PATH_INFO'];
$CarroF = $_SERVER['PATH_INFO'];
$muda_path = explode('/', $Carro );
$muda_path = explode('/', $CarroN );
$muda_path = explode('/', $CarroL );
foreach($muda_path as $carro_link){
    $CarroID = $carro_link;
    $CarroNome = $carro_link;
    $Carro_Link = $carro_link;
    $video_select = "SELECT * FROM carros WHERE `id`='$CarroID' OR `link_carro`='$Carro_Link' OR `nome_carro`='$CarroNome'";
    $query = @mysql_query($video_select) or die (mysql_error());
}
if (mysql_num_rows($query) <= 0) {
    $vd = mysql_fetch_assoc($query);
    include_once "assets/inc/not_found.php";
    };?>

Eu ainda sou meio novo na programação já que mexo mais com html e css, então, o código pode estar meio amador.
Answer: Eu costumo utilizar o seguinte
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^noticia(|/)$ noticia.php
RewriteRule ^noticia/(.*)$ noticia.php?id=$1

Utilizo em meu host e nunca me apresentou problemas.
Comment: valeu @VME, parece que deu certo, retirou a última barra, mas agora descobri também o arquivo não está pegando o `$_SERVER['PATH_INFO']` do link sem o `.php` da 'noticia'. Alguma sugestão?
Comment: Já resolvi o problema, apenas mudei para `$_SERVER['REQUEST_URI']` e agora funcionou. Valeu pela ajuda!!
Comment: Opa. Fico feliz que tenha conseguido. Só pude entrar agora.
Se puder dar um upvote, ficaria feliz!
Comment: sou novo no Stack, e não tenho reputação suficiente ://


How do you do a non-linear Poisson? What even is it?

Question: I have a count data that I am having trouble transforming to be linear. First, what are smoothing functions and how do I do it in R? Let's use the famous crab satellite example. If you plot width to satellite number (I assume you are familiar with this example due to its popularity), the plot isn't linear. My book, Agresti's An Introduction to Categorical Data Analysis then vaguely says they applied a smoothing process in the stat package. How'd they do this?
And assuming it doesn't work out for me, what are some options to analyze nonlinear parameters?
Thanks
Comment: Is the the book you're using Agresti *An Introduction to Categorical Data Analysis*?
Comment: Yes, also we meet again (you helped me on the terrorism thing thanks!)
Answer: In An Introduction to Categorical Data Analysis, Agresti writes on p. 78 that the smoothing was based on general additive models. GAMs are beyond the scope of his slender volume, and I've always wondered why he introduced them in the first place in that text, but that's no matter. Harrell's Regression Modeling Strategies briefly introduces the topic on p. 376. He writes that the gam function in R allows one to estimate the find a transformation of the predictors to optimize some criterion subject to the number of degrees of freedom the researcher is willing to expend.
Technical details about GAMs can be found in Hastie, Tibshirani and Friedman, Elements of Statistical Learning, p. 295. The general notion is that some specified family of basis functions (polynomials, kernels, whatever) are specified and their flexibility controlled by a penalty parameter (to regularize the result/improve generalizability).
Comment: What do you mean "additive" model? Does that mean I cant just run a gam() on my data if its a poisson distribution of count data?
Comment: "Additive" just means that the model is hypothesized to be of the form $g(y)=f_1(x_1)+f_2(x_2)+\cdots+f_n(x_n).$ So for a suitable choice of $g$ you can estimate a Poisson model, for example, in a way similar to how GLMs work. The "generalized" adjective is to denote that it's able to estimate a flexible family of models.
Comment: Thanks. And what is a penalty parameter?
Comment: It just characterizes the idea that a more flexible solution has a higher cost. In the degenerate case, you could just use something like an enormous number of polynomials to saturate the model and get a perfect fit in training data. But that model won't generalize well. The penalty constrains how flexible the model is permitted to be. Cf. hyperparameters.
Comment: Oh I see. I tried gam on my formula, but I don't really know what happened. No coefficients were returned, and there is no transformation formula I can use. It just returns an anova.

Basically, I have a Y and X that I can't transform to satisfy linearity, and ergo I can't run a poisson distirbution. I'm wondering what I should do.
Comment: A good place to start would not be with the software implementation, but with the mathematics of what the estimation procedure is doing. Once you feel like you have a grasp on the math, start reading the software documentation and learn about what all the different software options do. Then circle back and check how the software aligns with the math.


Django Rest Framework: make a field not required in modelviewset create

Question: I have a model which have got a field for showing time it created, named created_time. I don't want created_time to be required when creating model, but I want it to be in retrieved data from model!
This is its serializer:
class MyModelSerializer(ModelSerializer):
    class Meta:
        model = Model
        fields = ['name', 'created_time']

And this is its viewset:
class ModelApiView(ModelViewSet):
    serializer_class = MyModelSerializer

    def get_queryset(self):
        return Model.objects.filter(user=self.request.user.id)

    def perform_create(self, serializer):
        serializer.save(user=self.request.user, created_time=datetime.now())

Right now, by get request it sends a list of Model objects with name and created_time which is perfect. But when I post a new Model to be created, containing just its name, it sends me this error message:
{
    "datetime": [
        "This field is required."
    ]
}

Comment: You can add `created_time = serializer.DateTimeField(required=False)` in the top of the `ModelSerializer`.
Answer: You can make the field not required by specifying that in the ModelSerializer:
class MyModelSerializer(ModelSerializer):
    created_time = serializer.DateTimeField(required=False)

    class Meta:
        model = MyModel
        fields = ['name', 'created_time']


Why can the semantic closure over a structure have no model

Question: I do not understand why the following should hold in first-order logic. Can someone please help me with an initiative (or proof-based) explanation?
Suppose you have a $\Sigma$-structure, and let $Fm(\Sigma)$ be the set of all formulas over this $\Sigma$-structure (given by defintion 1.4, page 3 here). Let $X^{\vDash} = \{\phi : X \vDash \phi\}$ Then,
1- If $X^{\vDash} = Fm(\Sigma)$, then Mod$(X) = \emptyset$.
2- If $X$ is a set of propositions (formulas with no free variables) and Mod$(X) = \emptyset$, then $X^{\vDash} = Fm(\Sigma)$
Comment: **All** formulas, without further details, means also $\phi$ as well as $\lnot \phi$.
Comment: What does $Mod(X)$ mean?
Comment: Perhaps Mod(X) is the set (or class) of all models. Note that "the class of all models is empty" would mean the there is no model, whereas (as in the title) having an empty model to me seems to imply that the empty set happens to be a model. The question does not seem to be stated unambiguously.
Comment: @Mirko $Mod(X)$ is standard notation for the set of all models of $X$. Note that if $X\subseteq Y$ then $Mod(X)\supseteq Mod(Y)$: more sentences = more rules = fewer structures satisfying them all.
Answer:
You now that there is at least an atomic formula, $\phi$, in $Fm(\Sigma)$, therefore $\lnot \phi \in Fm(\Sigma)$, in other words $\phi, \lnot \phi \in X$, what does this tell you about the models of X?

The harder direction is $Fm(\Sigma) \subseteq X$. Given $\phi \in Fm(\Sigma)$, for all  models $M$, $M \nvDash X$ and so it holds $M \vDash X \implies M \vDash \phi$, wich means $\phi \in X^\vDash$.
Comment: I don't understand how you deduced "....and so it holds $M \vDash X \implies M \vDash \phi$"
Comment: if $a$ is not true then $a \implies b$ is true
Comment: Aha I see thank a lot!
Comment: You're welcome!


SvelteKit or vite hot module reload breaks global scss

Question: When I npm run dev my SvelteKit / vite app locally, after a minute or so, a full hot module reload is triggered which leads to break my design.
Stacktrace example:

23:09:37 [vite] page reload .svelte-kit/generated/nodes/8.js
23:09:37 [vite] hmr update /.svelte-kit/generated/root.svelte
23:09:37 [vite] page reload .svelte-kit/generated/server-internal.js
23:09:37 [vite] page reload src/declarations/cmc/cmc.factory.did.js
23:09:37 [vite] hmr update /src/frontend/src/lib/components/guards/IdentityGuard.svelte

etc.
I am not sure is the issue is linked to the fact that my global style is not correctly reloaded (I'm using sass) or the fact that weirdly some raw script Svelte components are injected into style tag in the dom
e.g. I find following tag in the dom

Another thing that is weird to me is that vite often detects changes in tsconfig.json and triggers new build even though the file has not changed in weeks
23:09:37 [vite] changed tsconfig file detected: /Users/.../admin/tsconfig.json - Clearing cache and forcing full-reload to ensure TypeScript is compiled with updated config values.

Any clue or idea what I can track to solve this issue?
Comment: Not sure about TypeScript but, I am using a Jetbrains editor as well. More precisely Webstorm.
Comment: You wouldn't by any chance happen to be using any sort of new preprocessor, would you? In my case I only started seeing this issue when I started using svelte-preprocess-cssmodules which also happens to have an invalid/inconsistent typescript peer dependency. Just asking in case this rings any bells for you.
Comment: Thank you for the suggestion. The only preprocessing I use is postcss along with autoprefixer. Interestingly, the problem has resolved itself recently. Although I haven't pinpointed the exact root cause, my current theory is that the issue might be related to the local backend server I'm running. It could be generating files or events at intervals that trigger the frontend server to reload. Not sure how but that's my current speculative theory.
Comment: Must admit, I was asking for selfish reasons to help me debug as well! For what it's worth, that helped me also narrow down some other unexpected behavior on my end to PhpStorm. Now I'm thinking it's some kind of interplay between inconsistent/invalid TypeScript versions between dependencies (e.g. TS 5.x in Svelte and TS 4.x in svelte-preprocess-cssmodules) combined with my IDE which is also hooking into TypeScript somehow. I was able to confirm by closing the IDE, or when open, disabling TypeScript language service. So... might boil down to that? Inconsistent TS versions in dependencies?
Comment: Closing the loop here: I resolved _my_ issue; turned out I had **>110k** files and a very inefficient `server.watch.ignored` array in my Vite config. Probably was a race condition (hence the unpredictability) that didn't occur until after I started adding more plugins to my Vite/Svelte setup. After tuning the ignore list, the problem went away entirely for me.
Comment: Great to hear you found the solution and thanks for the share! Might be useful for me someday too.
Answer: Alright solved it or at least find a workaround. The issue seems to have been linked to the fact that my Mac triggers incorrect refresh through fsEvents. Even tough there are no changes on the local file system, it requests full reload after 1-2 min. Disabling fsEvents in the vite.config.ts prevents this to happen and per extension, avoid the problem.
server: {
  watch: {
    useFsEvents: false
  },
}

Why sass is not intepreted on full reload is to me unclear but, at least there is no more issue and hmr still works.
Comment: Thank you for your detective work David, it just helped me out a lot. In my situation, hammering the Vite dev server with Playwright tests would randomly (but reliably) cause the server to crash, as it would think that tsconfig had changed (along with vite config and .env.local) which led to it crashing. Turning off fsEvents seems to have solved it perfectly so far!


Limitar barra de rolagem de acordo com o tamanho da div

Question: Existe algum jeito de limitar a barra de rolagem da página de acordo com o tamanho (altura) de uma div?
Digamos que eu tenho um site como qualquer outro, mas ao abrir uma box flutuante com altura de 1000px, quero que o scroll do mouse trave quando alcançar o fim dessa div, mesmo que o site continue para baixo, só permitir a rolagem para cima. Claro que quando esta div for fechada, o scroll volta ao normal.
Answer: Sim é possível, existem maneiras diferentes, uma delas é usando JavaScript/jQuery para saber as dimensões e posição do elemento para poder sincronizar o scroll com esses valores.
Se usar .offset() pode saber onde o elemento começa e somando-lhe o .outerHeight() sabe onde ele acaba. Depois precisa saber o valor do scroll e isso pode obter com .scroll(). Para finalizar, e uma vez que o evento scroll não é cancelável, tem de re-escrever o valor do scroll caso ele passe "os limites".
Sugestão de código:
var ultimoScroll;
var dialogPosition = $('#seu_dialog').offset().top;
var dialogHeight = $('#seu_dialog').outerHeight();
$(window).on('scroll', function () {
    var scroll = $(this).scrollTop();
    if (scroll > dialogPosition + dialogHeight.outerHeight() || scroll < dialogPosition) $(window).scrollTop(ultimoScroll);
    else ultimoScroll = scroll;
});

Este é o esqueleto do que precisa. Como não deu nenhum código HTML/CSS/JS deixo um exemplo inventado por mim que com uma adaptação desse código que referi em cima: http://jsfiddle.net/a825dnfm/
Comment: Claro, era apenas fazer um cálculo diminuindo a altura da janela e pronto! Modifiquei o `if` para: `if (scroll > dialogPosition + ( dialogHeight - SC(window).height() )`, obrigado!!
Comment: @Ricardo o código deve funcionar em mobile. Sei que o jQuery não suporta mobile oficialmente, daí existir o jQuery mobile. Se tiver problemas em mobile coloque um exemplo com o seu código para testar.
Comment: Olá Sergio, adaptei seu código ao meu script e apenas corrigi alí o `dialogHeight.outerHeight()` para somente `dialogHeight`. Seu código funcionou bem como eu queria, mas ele está travando o scroll no final da div, ou seja, a div sobe toda até o topo e trava no finalzinho dela. Teria que travar quando o final da div ficar no final da tela, para não poder subir mais.
Comment: Sergio, você sabe me dizer como fazer para este código funcionar em mobile? Pois o site é responsivo e visualizado em smartphones ou tablets, esse esquema de scroll não funciona.
Comment: Ah tah. Não linkei uma biblioteca mobile do jQuery. Vou fazer isso então. Obrigado.
Answer: Utilize css. Com o atributo overflow você pode determinar o que acontece quando o tamanho da um objeto é maior que o espaço onde ele está. Por exemplo:
#id {
    overflow: hidden;
}

Quer dizer que quando a largura e o comprimento da tag body forem maiores que a tela, não haverá rolagem. No seu caso o mais indicado seria:
#id {
    max-height: 1000px !important;
    overflow: hidden;
}


How would these grace notes fit?

Question:
As you can see in the treble clef, the first half note is 2 beats, the last 4 eighth notes is 2 beats, which add up to 4 beats. There are only 4 beats in a bar, so where would the graces notes fit?
Even if I view them as something extra, I'm still confused as to how I can play the bar properly with a metronome.
Comment: Hi, whenever posting a sheet music question, please tell us the composer and the edition  you are using.
Answer: Wow! That book must be really old because most editors would write that as a turn. Most editors would write it as the following

(I could not put a sharp in the turn. My software does not have the sharp turn)
Anyways, I would suggest playing those grace notes a little before beat three. If we are counting in sixteenth notes (1 e + a), starting on the 'a' would be the most natural. In other words, you would start playing the grace notes on the 'and' of the 'and' of beat 2 playing a triplet is pretty natural.
Doing so gives us this rhythm:

I hope that helps! Let me know if you need further clarification
Comment: Your answer **might** be correct, or it **might** be wrong, depending on the time period this piece was written.  The two examples you provide represent, roughly,  Bach (first) and late Romantic (second).
Comment: ? the first example would be written in the classical era and beyond. Bach would write it as the second. Also the musical example could not have been written in the baroque era because of (1) the piano was not invented until the classical era and (2) the vast majority of harpsichord music in the baroque era was polyphonic. This example is clearly homophonic
Answer: I would usually play this with the 3 small notes as triplet semiquavers before the first quaver. But for some composers the 3 small notes should be played inside the first quaver, so those 4 notes are demisemiquavers. The earlier the music is, the more likely this is. Beethoven - before the quaver. Bach - on the quaver.
Comment: If you play the grace notes before the beat, they generally are considerably shorter than when treated as a turn or as appogiatura

How would these grace notes fit?


A Fourier Analysis Question I am stuck at

Question:
If $f,g\in C[-\pi,\pi]$,and $f,g$ are $2\pi$ periodic, prove that $$\lim_{n\to\infty}\dfrac{1}{2\pi}\int_{-\pi}^\pi f(t)g(nt)\mathbb dt=\big(\dfrac{1}{2\pi}\int_{-\pi}^\pi f(t)\mathbb dt\big)\big(\dfrac{1}{2\pi}\int_{-\pi}^\pi g(t)\mathbb dt\big)$$

I tried to approximate $g(nt)$ by a trigonometric polynomial $T_n(t)$ but the problem is that $n$ is varying here. Nothing seems to work. I tried to interpret the left side as the constant Fourier coefficient of the function $f(x)g(nx)$ and the right side as the product of constant Fourier coefficients of $f(x)$ and $g(x)$ but because $f$ and $g$ are just continuous and not Lipschitz, I cannot replace $f$ and $g$ by their Fourier sums.
Please give me a hint only to get started on this problem.
Comment: possible duplicate of [A generalized version of the Riemann-Lebesgue lemma](http://math.stackexchange.com/questions/90195/a-generalized-version-of-the-riemann-lebesgue-lemma)
Answer: Let $a_{k}$ and $b_{k}$ be
$$
      a_{k}=\frac{1}{2\pi}\int_{0}^{2\pi}e^{-ikx}f(x)dx,\;\;\;
      b_{k}=\frac{1}{2\pi}\int_{0}^{2\pi}e^{-ikx}g(x)dx.
$$
Then $g(x)=\sum_{k=-\infty}^{\infty}b_{k}e^{ikx}$ converges in $L^{2}$ on any finite interval to the periodic extension of $g$. More explicitly, if $S_{N}=\sum_{n=-N}^{N}b_{k}e^{ikx}$, then, for fixed $n=1,2,3,\cdots$,
$$
         \lim_{N\rightarrow\infty}\int_{0}^{2n\pi}|g(x)-S_{N}(x)|^{2}dx= 0.
$$
By a simple change of variables, for fixed $n=1,2,3,\cdots$, one has
$$
         \lim_{N\rightarrow\infty}\int_{0}^{2\pi}|g(nx)-S_{N}(nx)|^{2}dx = 0.
$$
Then, for $f \in L^{2}$, it follows that, for fixed $n=1,2,3,\cdots$,
$$
         \int_{0}^{2\pi}f(x)g(nx)dx = \lim_{N\rightarrow\infty}\int_{0}^{2\pi}f(x)S_{N}(nx)dx.
$$
The above is easily justified using Cauchy-Schwarz:
$$
     \left|\int_{0}^{2\pi}f(x)g(nx)dx-\int_{0}^{2\pi}g(x)S_{N}(nx)dx\right| \\
      \le \int_{0}^{2\pi}|f(x)||g(nx)-S_{N}(nx)|dx \\
      \le \left[\int_{0}^{2\pi}|f(x)|^{2}\right]^{1/2}
          \left[\int_{0}^{2\pi}|g(nx)-S_{N}(nx)|^{2}\right]^{1/2}\rightarrow 0
    \mbox{ as } N \rightarrow \infty \mbox{ for fixed n. }
$$
Therefore, for any fixed $n=1,2,3,\cdots,$
$$
\begin{align}
      \frac{1}{2\pi}\int_{0}^{2\pi}f(x)g(nx)dx &
            =\lim_{N\rightarrow\infty}\frac{1}{2\pi}\int_{0}^{2\pi}f(x)S_{N}(nx)dx \\
       & = \lim_{N\rightarrow\infty}\frac{1}{2\pi}\int_{0}^{2\pi}f(x)\sum_{k=-N}^{N}b_{k}e^{iknx} dx \\
       & = \lim_{N\rightarrow\infty}\sum_{k=-N}^{N}b_{k}\frac{1}{2\pi}\int_{0}^{2\pi}f(x)e^{iknx}dx \\
       & = \lim_{N\rightarrow\infty}\sum_{k=-N}^{N}b_{k}a_{-kn} \\
       & = \sum_{k=-\infty}^{\infty}b_{k}a_{-kn}.
\end{align}
$$
Finally, Applying Cauchy-Schwarz to the sum on the right gives
$$
\begin{align}
  \left|\frac{1}{2\pi}\int_{0}^{2\pi}f(x)g(nx)dx-a_{0}b_{0}\right|^{2}
   & = \left|\sum_{|k| \ge 1}b_{k}a_{kn}\right|^{2} \\
   & \le \sum_{|k|\ge 1}|b_{k}|^{2}\sum_{|k|\ge 1}|a_{kn}|^{2} \\
   & \le \sum_{|k|\ge 1}|b_{k}|^{2}\sum_{|k| \ge n}|a_{k}|^{2}\rightarrow 0 \mbox{ as } n\rightarrow\infty.
\end{align}
$$
The last term on the right tends to $0$ because the sum is convergent.
Comment: Whoever downvoted could ask a question if they don't understand.
Comment: Hello T.A.E.! I did not downvote your answer but I would like you to explain some things like:
1. Why does the fact that the Fourier series of $g$ converges in $L^2$ to $g$ imply I can replace $g$ by its Fourier sum? I don't think we can do that, because $g$ will need to be at least Lipschitz in this case, not only continuous.
2. How did you interchange the summation and integral? Thing is, we do not know about uniform convergence of the Fourier series to the function.
3. Would you mind explaining the last inequality? Thanks!!
Comment: @yedaynara : The Fourier series for $f \in L^{2}$ always converges in the norm of $L^{2}$ to $f$. If $S_{N}$ is the truncated Fourier series $\sum_{n=-N}^{N}e^{inx}\frac{1}{2\pi}\int_{0}^{2\pi}f(y)e^{-iny}dy$, then $\|S_{N}-f\|_{L^{2}}\rightarrow 0$ as $N\rightarrow\infty$. By the Cauchy-Schwarz inequality, if $g$ is also in $L^{2}$, then $\left|\int_{0}^{2\pi}g(x)S_{N}(x)dx-\int_{0}^{2\pi}g(x)f(x)dx\right| \le \|g\|_{L^{2}}\|f-S_{N}\|_{L^{2}}\rightarrow 0$ as $N\rightarrow\infty$.
Comment: This is not a very straightforward problem, at least according to me. Could you kindly edit your answer and write the steps more clearly, explaining how you got each step? Thanks!!
Comment: Do you know the classical $L^2$ results of Fourier Series? For example, $e_{n}(x)=\frac{1}{\sqrt{2\pi}}e^{inx}$ defines a complete orthonormal basis of $L^{2}$. So $\|f-\sum_{n=-N}^{N}(f,e_{n})e_{n}\|_{L^{2}}\rightarrow 0$ as $N\rightarrow\infty$. Parseval's identity is equivalent: $\|f\|^{2}=\sum_{n=-\infty}^{\infty}|(f,e_n)|^{2}$. Equivalently, $(f,g)=\sum_{n=-\infty}^{\infty}(f,e_{n})(e_{n},g)$. Then there is the Cauchy-Schwarz inequality: $|\sum_{n}a_{n}b_{n}|^{2}\le \sum_{n}|a_{n}|^2\sum_{n}|b_{n}|^2$. If you know these, then my solution will make sense, and I can expand more. If not ...
Comment: @yedaynara : I forgot to ping you in the last message.
Comment: I know all these. But, still I do not get your solution at the parts I mentioned. Please expand.
Comment: Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/22519/discussion-between-yedaynara-and-t-a-e).
Comment: @yedaynara : Please look at the edited, expanded version now posted using the results that I cited, and that you stated you knew to be true. Ask more questions if something is not clear. I don't have time to chat today.
Comment: Perfect now, thanks a lot. Accepting this as the final answer. Thank you so much.
Answer: Use the real form of Fourier series.
The first thing that we examine is the integral $\frac{1}{{2\pi }}\int_{ - \pi }^\pi  {f(t)g(nt)dt} $.
If we can determine this integral, then we can work out the limit.
Both f and g are continuous and therefore square integrable.  Note  that since they are both periodic of period 2$\pi$, the convergence properties of the Fourier series of both functions should be useful.  Though their Fourier series converge almost everywhere in $[-\pi, \pi]$, but this property is of little use.  But since they are both square integrable, their Fourier series converge respectively to  $f$ and  $g$  in the L2 norm.
Let the Fourier series of  $f$  be
$T(\theta ) = \frac{1}{2}{a_0} + \sum\limits_{j = 1}^\infty  {\left( {{a_j}\cos (j\theta ) + {b_j}\sin (j\theta )} \right)} $
and  the Fourier series of $g$ be
$S(\theta ) = \frac{1}{2}{A_0} + \sum\limits_{j = 1}^\infty  {\left( {{A_j}\cos (j\theta ) + {B_j}\sin (j\theta )} \right)} $
and
${T_k}(\theta ) = \frac{1}{2}{a_0} + \sum\limits_{j = 1}^k {\left( {{a_j}\cos (j\theta ) + {b_j}\sin (j\theta )} \right)} $ and
${S_k}(\theta ) = \frac{1}{2}{A_0} + \sum\limits_{j = 1}^{j = k} {\left( {{A_j}\cos (j\theta ) + {B_j}\sin (j\theta )} \right)} $
their respective partial sums.
Then convergence in the L2 norm means
$\mathop {\lim }\limits_{k \to \infty } \int_{ - \pi }^\pi  {{{\left| {{T_k}(t) - f(t)} \right|}^2}}  = 0$ and $\mathop {\lim }\limits_{k \to \infty } \int_{ - \pi }^\pi  {{{\left| {{S_k}(t) - g(t)} \right|}^2}}  = 0$.
Let $n$ be a fixed positive integer.
Then we also have that
$\mathop {\lim }\limits_{k \to \infty } \int_{ - n\pi }^{n\pi } {{{\left| {{S_k}(t) - g(t)} \right|}^2}}  = 0$.
Thus by a change of variable we get
$\mathop {\lim }\limits_{k \to \infty } n\int_{ - \pi }^\pi  {{{\left| {{S_k}(nt) - g(nt)} \right|}^2}dt}  = 0$ and so
$\mathop {\lim }\limits_{k \to \infty } \int_{ - \pi }^\pi  {{{\left| {{S_k}(nt) - g(nt)} \right|}^2}dt}  = 0$.

This means ${S_k}(nt)$ tends to $g(nt)$ in the L2 norm and we have $\frac{1}{\pi }\int_{ - \pi }^\pi  {f(t){S_k}(nt)dt} $ tends to $\frac{1}{\pi }\int_{ - \pi }^\pi  {f(t)g(nt)dt}$.
We can deduce this statement as follows:
$\left| {\frac{1}{\pi }\int_{ - \pi }^\pi  {f(t){S_k}(nt)dt}  - \frac{1}{\pi }\int_{ - \pi }^\pi  {f(t)g(nt)dt} } \right|$
$ \le \frac{1}{\pi }\int_{ - \pi }^\pi  {|f(t)||{S_k}(nt) - g(nt)|dt} $
$ \le \frac{1}{\pi }\sqrt {\left( {\int_{ - \pi }^\pi  {|f(t){|^2}dt} } \right)} \sqrt {\int_{ - \pi }^\pi  {|{S_k}(nt) - g(nt){|^2}dt} }$  by Holder's Inequality.
Since $\mathop {\lim }\limits_{k \to \infty } \int_{ - \pi }^\pi  {{{\left| {{S_k}(nt) - g(nt)} \right|}^2}dt}  = 0$, the right hand side of the above inequality tends to 0 and so by the Comparison Test,
 $\frac{1}{\pi }\int_{ - \pi }^\pi  {f(t){S_k}(nt)dt} $ tends to $\frac{1}{\pi }\int_{ - \pi }^\pi  {f(t)g(nt)dt} $ as $k$ tends to infinity.
We now compute $\frac{1}{\pi }\int_{ - \pi }^\pi  {f(t){S_k}(nt)dt} $.
Since $f(t){S_k}(nt) = \frac{1}{2}{A_0}f(t) + \sum\limits_{j = 1}^{j = k} {\left( {{A_j}f(t)\cos (jnt) + {B_j}f(t)\sin (jnt)} \right)} $ ,
$\frac{1}{\pi }\int_{ - \pi }^\pi  {f(t){S_k}(nt)dt}  = \frac{1}{2}{A_0}\frac{1}{\pi }\int_{ - \pi }^\pi  {f(t)dt}  + \sum\limits_{j = 1}^{j = k} {\left( {{A_j}\frac{1}{\pi }\int_{ - \pi }^\pi  {f(t)\cos (jnt)} dt + {B_j}\frac{1}{\pi }\int_{ - \pi }^\pi  {f(t)\sin (jnt)dt} } \right)} $
$ = \frac{1}{2}{A_0}{a_0} + \sum\limits_{j = 1}^{j = k} {\left( {{A_j}{a_{jn}} + {B_j}{b_{jn}}} \right)} $.
It follows that
$\frac{1}{\pi }\int_{ - \pi }^\pi  {f(t){S_k}(nt)dt}  \to \frac{1}{2}{A_0}{a_0} + \sum\limits_{j = 1}^\infty  {\left( {{A_j}{a_{jn}} + {B_j}{b_{jn}}} \right)} $
as $k$ tends to infinity.
Hence $\frac{1}{\pi }\int_{ - \pi }^\pi  {f(t)g(nt)dt}  = \frac{1}{2}{A_0}{a_0} + \sum\limits_{j = 1}^{j = \infty } {\left( {{A_j}{a_{jn}} + {B_j}{b_{jn}}} \right)} $.
Now we claim that $\mathop {\lim }\limits_{n \to \infty } \sum\limits_{j = 1}^\infty  {\left( {{A_j}{a_{jn}} + {B_j}{b_{jn}}} \right)}  = 0$.
Observe that $\left| {\sum\limits_{j = 1}^{j = k} {\left( {{A_j}{a_{jn}} + {B_j}{b_{jn}}} \right)} } \right| \le \left| {\sum\limits_{j = 1}^{j = k} {{A_j}{a_{jn}}} } \right| + \left| {\sum\limits_{j = 1}^{j = k} {{B_j}{b_{jn}}} } \right| \le \sqrt {\sum\limits_{j = 1}^{j = k} {{A_j}^2} } \sqrt {\sum\limits_{j = 1}^{j = k} {a_{jn}^2} }  + \sqrt {\sum\limits_{j = 1}^{j = k} {{B_j}^2} } \sqrt {\sum\limits_{j = 1}^{j = k} {b_{jn}^2} } $
                                         by the Cauchy Schwarz inequality,
$ \le \sqrt {\sum\limits_{j = 1}^\infty  {{A_j}^2} } \sqrt {\sum\limits_{j = 1}^\infty  {a_{jn}^2} }  + \sqrt {\sum\limits_{j = 1}^\infty  {{B_j}^2} } \sqrt {\sum\limits_{j = 1}^\infty  {b_{jn}^2} } $.

And so $\left| {\sum\limits_{j = 1}^\infty  {\left( {{A_j}{a_{jn}} + {B_j}{b_{jn}}} \right)} } \right| \le \sqrt {\sum\limits_{j = 1}^\infty  {{A_j}^2} } \sqrt {\sum\limits_{j = 1}^\infty  {a_{jn}^2} }  + \sqrt {\sum\limits_{j = 1}^\infty  {{B_j}^2} } \sqrt {\sum\limits_{j = 1}^\infty  {b_{jn}^2} } $
$ \le \sqrt {\sum\limits_{j = 1}^\infty  {{A_j}^2} } \sqrt {\sum\limits_{j = n}^\infty  {a_j^2} }  + \sqrt {\sum\limits_{j = 1}^\infty  {{B_j}^2} } \sqrt {\sum\limits_{j = n}^\infty  {b_j^2} } $ .
Note that $\frac{1}{\pi }\int_{ - \pi }^\pi  {|g(t){|^2}dt}  = \frac{1}{2}A_0^2 + \sum\limits_{j = 1}^\infty  {\left( {A_j^2 + B_j^2} \right)} $  and  $\frac{1}{\pi }\int_{ - \pi }^\pi  {|f(t){|^2}dt}  = \frac{1}{2}a_0^2 + \sum\limits_{j = 1}^\infty  {\left( {a_j^2 + b_j^2} \right)} $.
So both $\sum\limits_{j = 1}^\infty  {a_j^2} $ and  $\sum\limits_{j = 1}^\infty  {b_j^2} $ are finite and as a consequence,
$\sum\limits_{j = n}^\infty  {a_j^2}  \to 0{\rm{ }}\quad {\rm{and }}\quad \sum\limits_{j = n}^\infty  {b_j^2}  \to 0{\rm{ }}\quad {\rm{as }}\;n \to \infty $ .
And so it follows from the above inequality that $\mathop {\lim }\limits_{n \to \infty } \sum\limits_{j = 1}^\infty  {\left( {{A_j}{a_{jn}} + {B_j}{b_{jn}}} \right)}  = 0$.
Hence $\mathop {\lim }\limits_{n \to \infty } \frac{1}{\pi }\int_{ - \pi }^\pi  {f(t)g(nt)dt}  = \frac{1}{2}{A_0}{a_0} = \frac{1}{{2\pi }}\int_{ - \pi }^\pi  {f(t)dt} \frac{1}{\pi }\int_{ - \pi }^\pi  {g(t)dt} $,
i.e., $\mathop {\lim }\limits_{n \to \infty } \frac{1}{{2\pi }}\int_{ - \pi }^\pi  {f(t)g(nt)dt}  = \left( {\frac{1}{{2\pi }}\int_{ - \pi }^\pi  {f(t)dt} } \right)\left( {\frac{1}{{2\pi }}\int_{ - \pi }^\pi  {g(t)dt} } \right)$.
Observe that I only use the fact that both functions are periodic, Lebesgue integrable and square integrable. The continuity of both $f$ and $g$ is used only to deduce both integrablity and square integrability. We may of course replace the condition on continuity by Lebsgue integrability and square integrability.
Answer: Hint: write $g(t)$ as the combination of $\sin(kt)$ and $\cos(kt)$, i.e., you need only solve the problem for $g(t)=\sin(kt)$ or $\cos(kt)$ for some $k$.
Comment: How can I? They are just continuous. Not differentiable or Lipschitz of something like that. How do I know that the Fourier series converges? Could you kindly elaborate your hint? Thank you!!
Answer: I would suggest using that continuous functions on compact sets are uniformly continuous, and then using that $f$ is Riemann integrable to split $f$ up over a partition of $[-\pi,\pi]$.
Alternative suggestion: Fejér sums?

Okay, I've thought about this some more, and I think it comes down to one of the following lemmata:

Lemma 1: Let $g$ be as in the question, and let $\chi_A$ be the indicator function of a closed subset in $[-\pi,\pi]$. Then
  $$ \lim_{n \to \infty} \int_{-\pi}^{\pi} \chi_A(t)g(nt) \, dt = \mu(A) \left( \frac{1}{2\pi} \int_{-\pi}^{\pi} g(t) \, dt \right) $$

(From here, I use the usual increasing sequence of step functions argument for Lebesgue integration) (Why do closed sets suffice? For a continuous function, the level sets are closed - see this question.)
or

Lemma 2: Let $g$ be as in the question, and let $\chi_A$ be the indicator function of an interval in $[-\pi,\pi]$. Then
  $$ \lim_{n \to \infty} \int_{-\pi}^{\pi} \chi_A(t)g(nt) \, dt = \mu(A) \left( \frac{1}{2\pi} \int_{-\pi}^{\pi} g(t) \, dt \right) $$

(This is obviously sufficient to prove the other one, using the Heine-Borel theorem.)
Then you use that and the properties of Riemann sums to sandwich $f$ and take the limit to get the integral. Lemma 2 looks pretty easy to prove.
Comment: If a function is uniformly continuous, it may not be Lipschitz, so maybe we cannot replace it by its Fourier series. Are you suggesting writing $f=(f-T(t))+T(t)$ where $T$ is a trigonometric polynomial? As for the alternative suggestion, I did try to replace $g(nt)$ by Fejer sum but problem is, $n$ is varying, so at different points I am getting different Fejer sums.
Comment: I suppose what I actually mean is to approximate $f$ by some Riemann sums. $g$ is probably best left to its own devices.
Comment: Okay. Well I had never thought of doing anything on $f$. So maybe you are suggesting the use of the fact that there exists a step function approximating $f$.
Comment: Yes, that I expect would work. The interesting question is whether a Riemann-type step function or a Lebesgue-type one is easier to work with.
Comment: Umm, I really couldn't find a way to proceed after using a step function. The terms are not going to $0$ because $\int g(nt)s(t)dt$ may not go to $0$. If we were dealing with Trigonometric polynomials then we could have used Riemann Lebesgue Lemma, which would have guaranteed convergence to $0$. But working with a trigonometric polynomial approximating $g$ when $n$ is varying is hard enough.
Comment: @yedaynara I've edited my answer to be more specific. I think that's probably the right technique.
Comment: I should like to know what about my answer makes it deserve downvotes: this process does answer the question in the form posed.


HTML Agility Pack isn't correcting paragraph elements properly

Question: I am having issues with some html file being structured like this:

    ...

Obviously whoever wrote this made a syntax error and was actually meaning to write this:

    ...

I was told HTML Agility Pack automatically fixes this on load but that doesn't seem to be the case here. I also tried adding
HtmlNode.ElementsFlags["p"] = HtmlElementFlag.Closed;

but that just gave me the result of:

    ...

which doesn't really do what I want. Is there some method that exists in HTML Agility Pack that automatically closes all p tags before a new one is opened?
EDIT: for the work I'm doing p tags won't ever be nested.
Comment: 'closes all p tags before a new one is opened' so you're saying you cannot nest a paragraph tag, that doesn't make sense `My TitleWhy should this be illegal` You're best bet would be have it automatically close tags but that won't fix the file
Comment: @johnny5 the specific html I'm working with doesn't ever nest paragraph tags. Could you elaborate a bit on what you mean by "have it automatically close tags"? Thanks!
Comment: The specific HTML doesn't but HTML agility pack cannot assume that,  I'm not sure of how you will specify to tell HTML agility pack to change its default behavior maybe it has an overridable visitor pattern
Answer: The tag p has some behavior which can cause some similar issue.
Starting from v1.5.0-beta3, you can disable default behavior of the tag P with the property DisableBehavaiorTagP
See the release note: https://github.com/zzzprojects/html-agility-pack/releases/tag/v1.5.0-beta3


Problema no cadastro de um item

Question: Olá esta acontecendo um erro onde ele não retorna para a pagina definida, mas na url aparece que ele entro na pagina controleProduto que foi definida. A página simplesmente fica branca.
Esse é o formulario.
<form class="" method="post" action="controleProduto.php">

          Adicione Produtos

            Nome:

          <input type="text" class="form-control" placeholder="Nome do Produto" id="nome" name="nome">

            Valor:

          <input type="text" class="form-control" id="valor" name="valor" placeholder="Valor do produto" >

        <button type="submit" class="btn btn-success" name="opcao" id="Cadastrar">Confirmar
        </button>

          Voltar

      </form>

Esse é o arquivo chamado depois do formulario."controleProduto".
include 'crudProduto.php';

if(isset($_POST["opcao"])){

$opcao = $_POST["opcao"];

if($opcao=="Cadastrar"){
    $nome=$_POST["nome"];
    $valor=$_POST["valor"];
    cadastraProduto($nome,$valor);
    header("Location: paginaInicial.php");
}
    else if($opcao=="Alterar"){
        $codigo=$_POST["codigo"];
        $nome=$_POST["nome"];
        $valor=$_POST["valor"];
        alterarProduto($codigo, $nome, $valor);
        header("Location: visualizarProduto.php");
    }
    else if($opcao=="Excluir"){
        $codigo=$_POST["codigo"];
        excluirProduto($codigo);
        header("Location: visualizarProduto.php");
    }

Esse é o arquivo que contem as funções. "crudProduto"
include 'conexaoBD.php';
function cadastraProduto($nome,$valor){
    conectar();
    query("INSERT INTO produto (nome, valor) VALUES('$nome',$valor)");
    fechar();
}

Comment: Sou novo em php. Quando tu diz log de erro, aquele que aparece na página? se for não aparece nada
Comment: Log de erro, o arquivo do servidor onde ficam gravados os erros todos. Uma das primeiras coisas que a pessoa tem que aprender a usar para desenvolver em PHP.
Comment: De qualquer forma, enquanto não sanitizar as strings (e isso depende de qual DB e biblioteca você usa, informação que falta na pergunta) vai dar problema.
Comment: https://pt.stackoverflow.com/questions/3864/70
Comment: No caso é só um trabalho para faculdade, mas de qualquer maneira estou usando mysql
Answer: A página fica branca porque em controleProduto.php você está fazendo declarações de testes condicionais que de acordo com seu código não possuem lógica. Voce está definindo um botão da seguinte maneira:
<button type="submit" class="btn btn-success" name="opcao" id="Cadastrar">Confirmar</button>

No seu arquivo controleProduto.php você está verificando se o mesmo possui o atributo value igual á "Cadastrar", "Alterar" e "Excluir" o que obviamente está incorreto pois no seu HTML nem mesmo o atributo value do elemento button foi definido.

Problema no cadastro de um item


Drupal::entityQuery

Question: This is the official Drupal::entityQuery function definition I found
public static function entityQuery($entity_type, $conjunction = 'AND') {
  return static::getContainer()
    ->get('entity.query')
    ->get($entity_type, $conjunction);
}

I also see that conditions can be added after the original query has been assigned to a variable
// original query
$query = \Drupal::entityQuery('node')
              ->condition('status', 1)
              ->sort('field_some_field', 'some_value');

// expand query with another condition
if (!empty($taxonomy_type)) {
  $query->condition('field_taxonomy_type', $taxonomy_type, 'IN');
}

Is this the correct way to query for nodes that contain a particular taxonomy type field?
How do I filter for nodes that contain a specific taxonomy term?
Answer:
Is this the correct way to query for nodes that contain a particular
taxonomy type field?

\Drupal::entityQuery() is the correct approach, yes.

How do I filter for nodes that contain a specific taxonomy term?

Would you like to filter by the taxonomy term name? If so you need to get the taxonomy term ID and use that in your condition:

// Get an array of terms that have the name "foo".
$terms = \Drupal::entityTypeManager()
  ->getStorage('taxonomy_term')
  ->loadByProperties(['name' => 'foo']);

// Get the first term from the array.
$term = reset($terms);

// Get an array of nodes that reference the taxonomy term "foo"s ID.
$nodes = \Drupal::entityQuery('node')
  ->condition('status', 1)
  ->condition('field_my_taxonomy_term', $term->id(), '=')
  ->execute();

All Drupal entity references are done via the entities ID, so you will always be using that as your condition.
Also, entityTypeManager() & entityQuery() are basically the same thing, just structured slightly differently, so you can use whichever one you find most convenient.
Comment: Thanks so much, [100pic](https://drupal.stackexchange.com/users/69785/100pic)

Drupal::entityQuery


Error in JDBC connection to Hive using eclipse and CHD4

Question: I am trying to establish a JDBC connection to Hive so that I can view and create tables and query Hive tables from Eclipse. I used HiveClient sample code: https://cwiki.apache.org/confluence/display/Hive/HiveClient
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;

public class HiveJdbcClient {
  private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";

  /**
 * @param args
 * @throws SQLException
   */
  public static void main(String[] args) throws SQLException {
      try {
      Class.forName(driverName);
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      System.exit(1);
    }
    Connection con = DriverManager.getConnection("jdbc:hive://localhost:10000/default", "", "");
    Statement stmt = con.createStatement();
    String tableName = "testHiveDriverTable";
    stmt.executeQuery("drop table " + tableName);
    ResultSet res = stmt.executeQuery("create table " + tableName + " (key int, value string)");
    // show tables
    String sql = "show tables '" + tableName + "'";
    System.out.println("Running: " + sql);
    res = stmt.executeQuery(sql);
    if (res.next()) {
      System.out.println(res.getString(1));
    }
    // describe table
    sql = "describe " + tableName;
    System.out.println("Running: " + sql);
    res = stmt.executeQuery(sql);
    while (res.next()) {
      System.out.println(res.getString(1) + "\t" + res.getString(2));
    }

    // load data into table
    // NOTE: filepath has to be local to the hive server
    // NOTE: /tmp/a.txt is a ctrl-A separated file with two fields per line
    String filepath = "/tmp/a.txt";
    sql = "load data local inpath '" + filepath + "' into table " + tableName;
    System.out.println("Running: " + sql);
    res = stmt.executeQuery(sql);

    // select * query
    sql = "select * from " + tableName;
    System.out.println("Running: " + sql);
    res = stmt.executeQuery(sql);
    while (res.next()) {
      System.out.println(String.valueOf(res.getInt(1)) + "\t" + res.getString(2));
    }

    // regular hive query
    sql = "select count(1) from " + tableName;
    System.out.println("Running: " + sql);
    res = stmt.executeQuery(sql);
    while (res.next()) {
      System.out.println(res.getString(1));
    }
  }
}

Then I added all the required jars to the java build path inside eclipse. I am using Cloudera QuickstartVM 4.6.1 and the eclipse that comes with it. Here's the error that I get in the IDE when I try to run the code.
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.sql.SQLException: Could not establish connection to localhost:10000/default: java.net.ConnectException: Connection refused
    at org.apache.hadoop.hive.jdbc.HiveConnection.<init>(HiveConnection.java:116)
    at org.apache.hadoop.hive.jdbc.HiveDriver.connect(HiveDriver.java:104)
    at java.sql.DriverManager.getConnection(DriverManager.java:582)
    at java.sql.DriverManager.getConnection(DriverManager.java:185)
    at HiveJdbcClient.main(HiveJdbcClient.java:22)

Does anyone know what I am missing here?
Comment: You need to start the Hive Thrift server. Can you check if it is started?
Comment: How can I check if it is started?
Comment: If you did not start it then its not started. Check documentation :  http://my.safaribooksonline.com/book/databases/hadoop/9781449326944/16dot-hive-thrift-service/_starting_the_thrift_server_html
Comment: Thanks. I started the thrift server and port 10000 is now listening. Now I have another error message in Eclipse saying: Exception in thread "main" java.sql.SQLException: org.apache.thrift.transport.TTransportException: java.net.SocketException: Connection reset
Comment: check this http://stackoverflow.com/questions/10828562/java-sql-sqlexception-org-apache-thrift-transport-ttransportexception-in-hive/10845065#10845065
Answer: I added log4j-1.2.15.jar to libraries and the problem was resolved.
Comment: Are you sure this was the solution?


Ljava.lang.String is displayed when I accesed param in thymeleaf Spring

Question: I have a problem while accessing the page parameter in thymeleaf.
Please see my below code :
<input type="hidden" name="resetKey" th:value="${param.key}"/>

I am trying to access parameter key from query string and send it as hidden parameter to the server.
After page loads, it is displayed like below :
<input type="hidden" name="key" value="[Ljava.lang.String;@b1fd0f9">

I want to send the key back to the server when the form is submitted.
Comment: That is no string but the string representation of a String array.
Comment: Thanks for your reply. My question did not update properly. I updated with the code now. Please review now.
Comment: It seems that `param.key`is still an array and the answer of holmis83 is right.
Answer: You are trying to print a multi-value, a String array. To print the first value:
<input type="hidden" name="resetKey" th:value="${param.key[0]}"/>

Or this should work too:
<input type="hidden" name="resetKey" th:value="${#request.getParameter('key')}"/>

Source: Spring MVC and Thymeleaf: how to access data from templates


Why is the connection refused?

Question: I am creating a web scraping script and divided it into four pieces. Separately they all work perfect, however when I put them all together I get the following error : urlopen error [Errno 111] Connection refused. I have looked at similar questions to mine and have tried to catch the error with try-except but even that doesn`t work. My all in one code is :
from selenium import webdriver
import re
import urllib2
site = ""

def phone():
    global site
    site = "https://www." + site
    if "spokeo" in site:
        browser = webdriver.Firefox()
        browser.get(site)
        content = browser.page_source
        browser.quit()
        m_obj = re.search(r"(\(\d{3}\)\s\d{3}-\*{4})", content)
        if m_obj:
            print m_obj.group(0)
    elif "addresses" in site:
        usock = urllib2.urlopen(site)
        data = usock.read()
        usock.close()
        m_obj = re.search(r"(\(\d{3}\)\s\d{3}-\d{4})", data)
        if m_obj:
            print m_obj.group(0)
    else :
        usock = urllib2.urlopen(site)
        data = usock.read()
        usock.close()
        m_obj = re.search(r"(\d{3}-\s\d{3}-\d{4})", data)
        if m_obj:
            print m_obj.group(0)

def pipl():
    global site
    url = "https://pipl.com/search/?q=tom+jones&l=Phoenix%2C+AZ%2C+US&sloc=US|AZ|Phoenix&in=6"
    usock = urllib2.urlopen(url)
    data = usock.read()
    usock.close()
    r_list = [#re.compile("spokeo.com/[^\s]+"),
             re.compile("addresses.com/[^\s]+"),
             re.compile("10digits.us/[^\s]+")]
    for r in r_list:
        match = re.findall(r,data)
        for site in match:
            site = site[:-6]
            print site
            phone()

pipl()

Here is my traceback:
Traceback (most recent call last):
  File "/home/lazarov/.spyder2/.temp.py", line 48, in <module>
    pipl()
  File "/home/lazarov/.spyder2/.temp.py", line 46, in pipl
    phone()
  File "/home/lazarov/.spyder2/.temp.py", line 25, in phone
    usock = urllib2.urlopen(site)
  File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 400, in open
    response = self._open(req, data)
  File "/usr/lib/python2.7/urllib2.py", line 418, in _open
    '_open', req)
  File "/usr/lib/python2.7/urllib2.py", line 378, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 1215, in https_open
    return self.do_open(httplib.HTTPSConnection, req)
  File "/usr/lib/python2.7/urllib2.py", line 1177, in do_open
    raise URLError(err)
urllib2.URLError: <urlopen error [Errno 111] Connection refused>

After manually debugging the code I found that the error comes from the function phone(), so I tried to run just that piece :
import re
import urllib2
url = 'http://www.10digits.us/n/Tom_Jones/Phoenix_AZ/1fe293a0b7'
usock = urllib2.urlopen(url)
data = usock.read()
usock.close()
m_obj = re.search(r"(\d{3}-\d{3}-\d{4})", data)
if m_obj:
    print m_obj.group(0)

And it worked. Which, I believe, shows it`s not that the firewall is actively denying the connection or the respective service is not started on the other site or is overloaded. Any help would be apreciated.
Comment: Sidenote: [DRY](http://en.wikipedia.org/wiki/Don't_repeat_yourself)
Comment: Sidenote: [Global Variables are Bad](http://c2.com/cgi/wiki?GlobalVariablesAreBad)
Answer: Usually the devil is in the detail.
according to your traceback...
File "/usr/lib/python2.7/urllib2.py", line 1215, in https_open
return self.do_open(httplib.HTTPSConnection, req)

and your source code...
site = "https://www." + site

...I may suppose that in your code you are trying to access https://www.10digits.us/n/Tom_Jones/Phoenix_AZ/1fe293a0b7 whereas in your test you are connecting to http://www.10digits.us/n/Tom_Jones/Phoenix_AZ/1fe293a0b7.
try to replace the https with http (at least for www.10digits.us): probably the website you are trying to scraping does not respond to the port 443 but only to the port 80 (you can check it even with your browser)
Comment: When I tried it I received : HTTP Error 503: Service Temporarily Unavailable. Yet when that same fragment is on its own it works which makes me doubt that the web server unable to handle the HTTP request
Comment: Is there a way to check if that is the case and if it`s true to manage it.
Comment: yes, if you receive an HTTP error 503 (you may use a `try..except`) then stop for 5 seconds (`import time; time.sleep(5)`). to catch the error you may look at http://docs.python.org/2/howto/urllib2.html#error-codes (at the end of this paragraph there is a complete example)

Why is the connection refused?


jquery: js-cookie in ie7 not working?

Question: hey guys,
don't get it... I want to show a bar at the top of my page only if viewed with ie6, ie7 or ie8.

Some Information for users.

jquery:
if ($.browser.msie && $.browser.version.substr(0,1)<9) { // if ie6 || ie7 || ie8

var topbar = $.cookie('TopBar');

if (topbar === 'on') {
    $('#topbar').hide();
} else {
    $('#topbar').show();
}

//Browser Tip
$('#topbar .topbarCloser').click(function() {
    $(this).parent().slideUp({
        duration: 300,
        easing: 'easeOutQuint',
        complete: function() {
            $('#topbar').remove();
            $.cookie('TopBar', 'on', { expires: 1000 });
        }
    });
});

}
The user can simply click the closer, a cookie is saved and the bar is hidden for future visits. (i know this should be done with a database, but in my case that's okay)
It works fine in ie8 but in ie7 the bar does blink for like a few milliseconds and than is hidden. So if the page is visited for the first time in ie7 the topbar should be visible, however it isn't. I can see it blinking very shortly and then it is hidden. (doesn't fade out, is just hidden).
Any idea what could cause that? Anything weird with my code?
Answer: This is likely caused by your CSS.  Your JS doesn't hide it right away, and your CSS has it visible.  Switch your case up.  Hide your bar by default (in CSS) and then, if necessary, show it with JavaScript.
This will prevent that brief moment on page load where it's visible before your JS comes along to hide it.
So...
#topbar { display: none; }

This means your JS can just read:
if (topbar != 'on') {
    $('#topbar').show();
}

Comment: well, I'm actually hiding it with css by default... i just didn't post the css in my question - sorry for that. Your proposed changes in the js seem actually logical, but the it doesn't make a difference. It works in all browsers and ie8. ie7 however doesn't show the bar.
Comment: i found the solution. since the topbar is an overlay. i set it to `position:relative` and gave it a really high z-index of 99999.
Comment: Interesting.  I'm happy I at least... maybe... helped a little.
Answer: try to edit the cookie plugin:
in line 83 instead of :
if (document.cookie && document.cookie != '') {

use :
if ((typeof(document.cookie) != 'undefined') && document.cookie != '') {

jquery: js-cookie in ie7 not working?


sending info to Arduino with pyserial(opreate stepper motor)

Question: I'm trying to control a stepper motor that is connected to an Arduino and CNC shield using pySerial.
I succeeded to run the motor by sending characters to the serial interface. Now I want to choose the number of spins, but I haven't been able to.
I'm using Tkinter for my GUI.
Here is my Python code:
import serial  # Import Serial Library
from Tkinter import *
import time

usbport = 'COM3'
serial_begin_is = 9600
arduinoSerialData = serial.Serial(usbport, serial_begin_is)  # Create Serial port object called arduinoSerialData

class GUI():
    def __init__(self, root):
        # type: (object) -> object
        self.root = root
        self.root.geometry("750x600")
        self.root.title("motor")
        self.CreateWidgets()
        self.spin = 0
        self.num = 1

    def CreateWidgets(self):
        self.number_of_spins = 1.0
        self.spin_num = 0.0
        self.speed_val = StringVar()
        self.spin_num_entry = Entry(self.root, textvariable=self.spin_num)
        self.spin_num_entry.place(x=200, y=20)
        self.speed_num_chose = Label(self.root, text="enter spin numbers:")
        self.speed_num_chose.place(x=50, y=20)
        self.enter_spin_num = Button(self.root, text="enter", command=lambda: self.set())
        self.enter_spin_num.place(x=200, y=45)
        # self.speed_num_label = Label(self.root, text="spin num will be" +str(self.number_of_spins))
        # self.speed_num_label.place(x=255, y=45)
        self.speed_num_entry = Entry(self.root, textvariable=self.speed_val)
        self.speed_num_label = Label(self.root, text="spin num will be: " + str(self.number_of_spins))
        self.speed_num_label.place(x=255, y=45)
        self.run_b = Button(self.root, text="run", command=lambda: self.run(self.number_of_spins))
        self.run_b.place(x=255, y=85)
        # self.run_b.config(state=DISABLED)
        self.spin_num_B = Button(self.root, text="stop", command=lambda: self.send_stop())
        self.spin_num_B.place(x=255, y=150)
        self.quit_b = Button(self.root, text="quit", command=lambda: root.destroy())
        self.quit_b.place(x=50, y=500)

    def set(self):
        self.number_of_spins = int(self.spin_num_entry.get())
        self.speed_num_label.config(text="spin num will be: " + str(self.number_of_spins))

        self.run_b.config(state=NORMAL)
    def run(self, x):
        x = int(x)
        print x
        # for i in xrange(x):
        arduinoSerialData.write(x)
        arduinoSerialData.write(b'r')

        print "run"

    def send_stop(self):
        arduinoSerialData.write(b's')
        print "stop"

root = Tk()
GUI(root)
root.mainloop()

and here is my Arduino code:
        int cnt = 0 ;
        long spin=0;
float num = 1.0 ;
float speed1 = 100;
char start ='s';
bool prt = false;
int flag =0;
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
#define stepPin 4
#define dirPin 7
#define enable 8

}

void loop() {
  // put your main code here, to run repeatedly:

if (Serial.available()>0){
start = char(Serial.read());
   if (start == 'r')
    prt = true;
    if (prt){
     start = char(Serial.read());
    if (start!='r'&&start!='s')
         num = int(start);
    while(spin <200*32*num){
      start = char(Serial.read());
      if (start =='s'){
        digitalWrite(enable, HIGH);
        prt = false;
        spin = 200*32*num;
      }
          move();
          spin++;
  }
  prt = false;
  spin = 0;
    }

  if (start == 's'){
    pinMode(enable, OUTPUT);
    digitalWrite(enable, HIGH);
  }
}
}

void move () {
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(enable, OUTPUT);
  digitalWrite(enable, LOW);
  digitalWrite(stepPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(stepPin, LOW);
  delayMicroseconds(speed1);

}

Comment: What is the exact problem you are facing? Is it the Tinker interface or serial communication?
Comment: I want to set the number of spin with the gui ,I sending the x as integer but the aduino doesn't get it
Comment: @Sma maybe i send a string spearted by coma and then do a split ?
Comment: I don't think sending comma separated string will make your work easier, as i think you are sending symbols of 1byte length. And i would suggest putting all the `pinMode` in setup function.
Comment: @Sma you right i'v put the pinMode in  the setup function .
do you have idea how can i set the spin num ?
Answer: okey i'v secceded to opreate the motor,and also added some other function like changing direction and controling speed.
one problem i'm facing know is that whan the user changing the speed ,the motor stop and than apllied the speed ,and that can cuase a motor a fialure if the speed is very high.(speed is given as the delay time between stepper motor steps).
here is the codes,if any one has sulotion for the speed control i would be please to hear and learn :)
python code:
import serial  # Import Serial Library
from Tkinter import *
import time

usbport = 'COM3'
serial_begin_is = 38400
arduinoSerialData = serial.Serial(usbport, serial_begin_is,timeout=0.5)  # Create Serial port object called arduinoSerialData

    class GUI():
        def __init__(self, root,arduinoSerialData):
            # type: (object) -> object
            self.root = root
            self.root.geometry("750x600")
            self.root.title("motor")
            self.CreateWidgets()
            self.spin = 0
            self.num = 1
            self.arduinoSerialData=arduinoSerialData
            self.flag = True;

        def CreateWidgets(self):
            self.number_of_spins = 0
            self.speed_value = 150
            self.spin_num = StringVar()
            self.speed_val = StringVar()
            self.spin_num_entry = Entry(self.root, textvariable=self.spin_num)
            self.spin_num_entry.place(x=200, y=20)
            self.spin_num_chose = Label(self.root, text="enter spin numbers:")
            self.spin_num_chose.place(x=50, y=20)
            self.enter_spin_num = Button(self.root, text="enter", command=lambda: self.set())
            self.enter_spin_num.place(x=200, y=45)
            # self.spin_num_label = Label(self.root, text="spin num will be" +str(self.number_of_spins))
            # self.spin_num_label.place(x=255, y=45)
            self.speed_label = Label(text="speed val will be:"+str(self.speed_value))
            self.speed_label.place(x=550,y=75)
            self.speed_b = Button(self.root, text="enter", command=lambda: self.speed_set())
            self.speed_b.place(x=550, y=45)
            self.speed_num_entry = Entry(self.root, textvariable=self.speed_val)
            self.speed_num_entry.place(x=550, y=20)
            self.speed_num_chose = Label(self.root, text="enter speed value:")
            self.speed_num_chose.place(x=420, y=20)
            self.spin_num_label = Label(self.root, text="spin num will be: " + str(self.number_of_spins))
            self.spin_num_label.place(x=255, y=45)
            self.run_b = Button(self.root, text="run", command=lambda: self.run())
            self.run_b.place(x=255, y=85)
            self.run_b.config(state=DISABLED)
            self.spin_num_B = Button(self.root, text="stop&reset", command=lambda: self.send_stop())
            self.spin_num_B.place(x=255, y=150)
            self.quit_b = Button(self.root, text="quit", command=lambda: root.destroy())
            self.quit_b.place(x=50, y=500)
            self.dir_b = Button(self.root, text="change direction", command=lambda: self.dirf())
            self.dir_b.place(x=295, y=85)

        def set(self):
            self.number_of_spins = int(self.spin_num_entry.get())
            self.data = [b"s"]
            #self.arduinoSerialData.write(self.data)
            value = list(str(self.number_of_spins))
            for char in value:
                self.data.append(char.encode())
            print self.data
            self.spin_num_label.config(text="spin num will be: " + str(self.number_of_spins))
            self.run_b.config(state=NORMAL)
        def run(self):
            run_char= "r"
            self.data[0] = run_char.encode()
            print self.data
            self.arduinoSerialData.write(self.data)
            print "run"
            self.spin_num_entry.delete(0, END)

        def send_stop(self):
            stop = "s"
            self.data[0] =stop.encode()
            self.arduinoSerialData.write(self.data)
            print self.data
            self.arduinoSerialData.close()
            self.arduinoSerialData = serial.Serial(usbport, serial_begin_is)
            self.speed_label.place_forget()
            self.speed_label.config(text="speed val will be:" +str(self.speed_value))
            self.speed_label.place(x=550, y=75)
            time.sleep(0.05)

        def dirf(self):
            print "direction had been chaged "
            self.arduinoSerialData.write(b"d")
        def speed_set(self):
            self.speed_label.place_forget()
            self.speed_label.config(text="speed val will be:"+self.speed_num_entry.get())
            self.speed_label.place(x=550, y=75)
            self.speed_in_int = int(self.speed_num_entry.get())
            self.data_speed = [b"v"]
            value = list(str(self.speed_in_int))
            for char in value:
                self.data_speed.append(char.encode())
            print self.data_speed
            self.arduinoSerialData.write(self.data_speed)

    root = Tk()
    GUI(root,arduinoSerialData)
    root.mainloop()

Arduino code:
int cnt = 0 ;
long spin=0;
float num = 1.0 ;
float speed1 = 150;
char start ;
bool prt = false;
bool dirf = true;
int flag =0;
String data[2];
void setup() {
  // put your setup code here, to run once:
Serial.begin(38400);
#define stepPin 4
#define dirPin 7
#define enable 8
 pinMode(stepPin, OUTPUT);
 pinMode(dirPin, OUTPUT);
 pinMode(enable, OUTPUT);
 digitalWrite(enable, HIGH);
 digitalWrite(dirPin, HIGH);

}

void loop() {
  // put your main code here, to run repeatedly:

  while(Serial.available()>0){
   if (Serial.peek() == 'r') {
      prt = true;
      Serial.read();
      num = Serial.parseInt();
      //Serial.println(num);
   }
    if (prt){

    while(spin <200*32*num){
      move();
      spin++;
      if (Serial.peek() == 's') {
          Serial.read();
          digitalWrite(enable, HIGH);
          prt = false;
          spin = 200*32*num;
        }
      if (Serial.peek() == 'd') {
         Serial.read();
         dirf = !dirf;
         dir();
         Serial.read();
      }
       if (Serial.peek() == 'v') {
         Serial.read();
         speed1 = Serial.parseInt();
         Serial.read();
      }
     }
    prt = false;
    spin = 0;
    //num = 1;
    }

  if (Serial.peek() == 's') {
    Serial.read();
    digitalWrite(enable, HIGH);
    prt = false;
    spin = 0;
    num = 1;
  }
  if (Serial.peek() == 'd') {
         dirf = !dirf;
         dir();
      }
  if (Serial.peek() == 'v') {
         Serial.read();
         speed1 = Serial.parseInt();
         Serial.read();
      }
}
  /*
      while (Serial.available() > 0) {
        Serial.read();
      }
  */
}

void move () {
  digitalWrite(enable, LOW);
  digitalWrite(stepPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(stepPin, LOW);
  delayMicroseconds(speed1);

}
void dir (){

   if (dirf)
      digitalWrite(dirPin, HIGH);
  else
     digitalWrite(dirPin, LOW);

}


Look-up of remote ejb using client-ejb jar

Question: I have application packed as an .ear file with ejb module inside. There is stateless session bean implementing remote interface. Something like this:
package my.package.ext.impl;
[...]
@Stateless(name = "MyPropertiesHandler", mappedName = "ejb/MyPropertiesHandler")
public class PropertiesHandler implements PropertiesHandlerRemote {
    [...]
}

and this:
package my.package.ext;
[...]
@Remote
public interface PropertiesHandlerRemote {
    [...]
}

There's also client-ejb jar generated, there are business remote interface and some other stuff inside.
This client-ejb jar is attached to other application as a Maven dependency.
I try to look-up PropertiesHandler service from this application:
PropertiesHandlerRemote propertiesHandler = InitialContext.doLookup(
    "ejb/MyPropertiesHandler#my.package.ext.PropertiesHandlerRemote");

At this moment I get following error:
java.lang.NoClassDefFoundError: my/package/ext/PropertiesHandlerRemote
at [...]
at sun.reflect.GeneratedMethodAccessor633.invoke(Unknown Source)
Truncated. see log file for complete stacktrace
Caused By: java.lang.ClassNotFoundException: my.package.ext.PropertiesHandlerRemote
at weblogic.utils.classloaders.GenericClassLoader.findLocalClass(GenericClassLoader.java:297)
at weblogic.utils.classloaders.GenericClassLoader.findClass(GenericClassLoader.java:270)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at weblogic.utils.classloaders.GenericClassLoader.loadClass(GenericClassLoader.java:179)
Truncated. see log file for complete stacktrace

I run it on Weblogic 10.x as you can see above.
Any ideas that's going wrong?
Comment: So I believe it compiles, what scope do you set in maven for this dependency? The class is not found at runtime by WL.
Comment: @Kris thanks for reply. No scope specified, so I guess it is compile by default. Anyway it is packed as an .ear and client-ejb jar included into it.
Answer: The problem was that the client-ejb jar was packed in the root of an ear file, not in the /lib folder with other dependencies.
The solution is to use <classifier>client</classifier> tag instead of <type>ejb-client</type> in the pom of the project where you want to use your client-ejb jar file.
This problem is also discussed here: http://jira.codehaus.org/browse/MEAR-85
Strange that this is the first time I met such problem, previously <type>ejb-client</type> tag worked perfect.
Answer: Since you have a NoClassDefFoundError, it does not mean your PropertiesHandlerRemote class is not found, but something which is used by your implementation - so maybe you have further dependencies which are not in the client's classpath?
Comment: thanks for reply. I have original ear file deployed on the same container, so all required stuff should be on classpath. Anyway I've just tried to remove all "exclude" annotation from ejb-client generation config, so everything was included into this jar. It did not help :(
Comment: If you use your ejb from within the container, why do you have to get the reference by JNDI and don't let it injected by `@EJB`? Or are you talking about a second container trying to remotely access the ejb in the first one?
Comment: second is correct ;) I was just inaccurate. Also this bean is look-up from the simple java class, which is not in the managed context (not an ejb), so `@ejb` injection will not work.
Comment: Would it be an option to put your client within an application client container? Then it would still be a "simple java class", but you could use injection to get the reference.
Comment: But again to the root problem - `NoClassDefFoundError`simply means, a class is not found by the class loader while processing the definition of your class. So I would double-check, if your client really "sees" all the necessary classes.
Comment: do you want to say `@EJB` injection will work from outside managed context? I doubt on it :) But this is not the case anyway. Regarding to the topic, do you mean some other class could be missing? Why so stacktrace refers my/package/ext/PropertiesHandlerRemote class?


Mathematical symbols to describe a solution of equation system

Question: The following augmented matrix represents a equation system.
$$\left[\begin{array}{ccc|c} 1 & 2 & 4 & 8 \\ 0 & 1 & 4 & 9 \\ 0 & 0 & 1 & 6 \end{array}\right] $$
Since the matrix is in a row-Echelon form, it is possible to calculate the values of $x_1, x_2 ~and~ x3$ straightforward.
$$
\begin{array}\\
x_1 + 2x_2 + 4 x_3 = 8 \\
x_2 + 4x_3 = 9 \\
x_3 = 6
\end{array}
$$
One of the steps describes the calculation of $x_2$.
I need to describe that step as part of the solution to the equation system and I am not sure if the following representation is usually used to represent it:
$$x_2 + 4x_3 = 9 \color{red}{\wedge} x_3 = 6 \color{red}\to x_2 = -15$$
Just to clarify: the question is about semantics.
Comment: What's usually used is natural language. In English, $$x_4 + 4x_3 = 9 \text{ and } x_3 = 6 \text{ implies } x_4 = -15$$
Comment: `$\implies$` produces $\implies$
Answer: Technically, your notation is correct since you are expressing (as Lee Mosher said in a comment) "... and ... implies" using the standard boolean operation symbols from propositional logic. But a beginner student who reads that notation probably will not realize that it is in fact insufficient for the desired purpose of solving the original system of equations.
Take any real $x≥-2$. The following statements are true:

$x=\sqrt{x+2}$ implies $x^2=x+2$.
$x=\sqrt{x+2}$ implies $(x+1)·(x-2)=0$.
$x=\sqrt{x+2}$ implies $x=-1$ or $x=2$.

This does not mean that the following is true:

$x=-1$ or $x=2$ implies $x=\sqrt{x+2}$.

In general, if you want to solve a system of simultaneous equations, you generally perform some reasoning under the assumption that all those equations hold, and so the conclusions you reach under that assumption will restrict the possible values that can satisfy those equations. In other words, what you actually have would be that if $x,...,y$ satisfy some equations, then $x,...,y$ satisfy some constraints, where the constraints are simpler than the original equations. But as the above example shows, an assignment of values for $x,...,y$ that satisfies the constraints may not satisfy the original equations, so you still need to check which assignments do.
It turns out that for systems of linear equations, you can perform a special kind of reasoning where you transform the entire system through a sequence of equivalent systems. It is wrong to think of solving a linear equation as a series of steps where each step is simply obtaining a new equation from previous ones, such as the step in your question. If you are careful, you can think in terms of changing one part of the system at a time. Consider the following equivalence ("iff" means "if and only if") for any reals/complex $x,y$:
  $x-2y = 3$ and $y = 4$ iff $x = 11$ and $y = 4$
This equivalence means that if your system includes the first two equations (on the left of the "iff") then you can replace them by the last two equations (on the right of the "iff") without changing the possible solutions.
If a compact notation is desired that still captures completely the "iff" nature of the transformation, I could suggest:
  $x-2y = 3 \xleftarrow{}\!\xrightarrow{\displaystyle\!\!\!\! y = 4} x = 11$.
Comment: Just to clarify. I understand that, the concept that you described is used during the Gaussian transformation: An equation system toward a row-Echelon form. Once the row-Echelon form is achieved, it would be a matter of solve the remaining equations. However, it is possible to keep the same procedure that 'transform' equations, multiplying one of them and then adding it to another. In the end we obtain equations with one single variable. Anyhow, I agree about the importance of understanding the 'if and only if' that is used throughout all the transformations.
Comment: @AlexJavarotti: Okay sure! I have adjusted my answer based on your remark, and added a little suggestion at the end that I just thought of.


Esta operação criaria um documento estruturado incorretamente. XDocument

Question: Estou a tentar criar um XML, mas não estou a conseguir. Quero que tenha este formato:
<item>
      <linktext></linktext>
      <etc..></etc...>
</item>

Consigo adicionar um item, mas quando vou outra vez pelo loop, ele vai sai do try e da-me o erro do título. O que estou a fazer errado? Veja aqui como crio o XML:
XDocument xmlOut = new XDocument();
 XElement item = new XElement("item",
                        new XElement("linktext", processedXML),
                        new XElement("link", "http://www.boe.es/diario_boe/txt.php?id=" + tmpName[1]),
                        new XElement("guid", newPdf.ToString()),
                        new XElement("pubDate", DateTime.Now)
                        );

                    xmlOut.Add(item);

Answer: O problema era que eu estava tentando adicionar vários elementos na raíz do XML, o que é errado (pelo que percebi depois de algumas pesquisas). O que fiz foi criar um XElement <itens> e depois em vez de:
xmlOut.Add(item);

usei
xmlOut.Root.Element("itens").Add(item);

e o problema ficou resolvido, tendo vários <item> dentro de <itens>, como o pretendido.


How to test a binary clustering method?

Question: I'm unsure in designing a test case for a clustering method of bitsets.
At the moment I define some centroids C like
C_ref = { 11110000
        , 00001111 }

with equal Hamming-distance (c_i ^ c_j = 4) to each other.
Now I create bitsets b like
B_ref = { 11010000
        , 11110010
        , 00001110
        , 00011111
        , ...      }

Every b has a distance (b_i ^ c_n > 4) to each centroid except to exactly one centroid c_b. The centroid cb and its corresponding b have a shorter distance (b_i ^ c_b < 4).
Maybe it's better explained in short with matrices
Distance|c_ref_0 c_ref_1
--------+---------------
c_ref_0 |      0       4
c_ref_1 |      4       0

Distance|c_ref_0 c_ref_1
--------+---------------
b_ref_0 |      1       7
b_ref_1 |      1       5
b_ref_2 |      7       1
b_ref_3 |      7       1

So my working steps are
create centroids C_ref as reference
create bitsets B_ref (with help of C_ref) as reference
find centroids C_res (with help of B_ref) as result

I check the distances of C_ref and C_res with
bitset-length 256 bit
8 centroids with distance 64 to each other
1000 descriptors with distance < 16 or distance > 64

Distance  | Centroids Ref 0 to 8
----------+-----------------------
Centroids | 0 64 64 64 64 64 64 64
Ref       | 64 0 64 64 64 64 64 64
0 to 8    | 64 64 0 64 64 64 64 64
          | 64 64 64 0 64 64 64 64
          | 64 64 64 64 0 64 64 64
          | 64 64 64 64 64 0 64 64
          | 64 64 64 64 64 64 0 64
          | 64 64 64 64 64 64 64 0

Centroids | 32 68 34 36 39 32 32  4
Res       | 32 68 34 36 39 32 32 68
0 to 8    | 32 68 34 36 39 32 32 68
          | 32 68 34 36 39 32 32 68
          | 32 68 34 36 39 32 32 68
          | 32 68 34 36 39 32 32 68
          | 32  4 34 36 39 32 32 68
          | 32 60 30 28 25 32 32 60

Looks not good. Nevertheless, what are your suggestions to test binary clustering?
Update 1
Above I've tried (in a glumsy way) to ask how to test a clustering method of bit-vectors. Binary vectors are clustered with Hamming distance as distance-function into 8 or 10 clusters. The resulting centroids should be verified.
My approach is to create artificial centroids (like C_ref above) and produce artificial binary vectors (like B_ref above). Both sets are not random.
The clustering takes B_ref as input and produces C_res as output.
The last step is the distance matrix C_ref X C_res. A perfect result would be (in my eyes) C_ref X C_ref == C_ref X C_res.
Comment: There is some unclear points in your question and terminology. What is "binary clustering"? - binary data or clustering to two clusters? What is the use of centroids for binary (categorical) data? Expressions like `b_i ^ c_n > 4` are unclear, notation not explained.
Comment: I use binary vectors with 256 bit each. In total some billions of vectors. I try to cluster these binary vectors into 8 or 10 cluster. As so, I try to find 8 to 10 centroids.
I have adapted [link](http://imagelab.ing.unimore.it/imagelab/pubblicazioni/2013ElectronicImaging.pdf). `b_i ^ c_n > 4` means Hamming-distance greater 4.
Answer: If you predefine the centroids, this isnot clustering, but nearest neighbor classification.
You can use any classification evaluation such as precision and recall.
Comment: I predefine centroids just for testing purposes. So to check if my implementation is approximating against the artificial centroids.

How to test a binary clustering method?


Estimating polymomial coefficient in R

Question: I used a Taylor series to expand log(1 - ax) so I could estimate the value of parameter 'a'.
The expansion becomes  -ax - a^2*x^2/2 - a^3*x^3/3 . . .
Now I need to estimate the parameter 'a' using regression and for simplicity I am only using the first 3 terms in the expansion.
The equation to be estimated becomes y ~ ax + a^2*x^2/2 + a^3*x^3/3 [I have absorbed the negative sign on the left hand side of the equation]
I wanted to ask if there is a way to estimate the coefficients a , a^2 and a^3 in the above equation, keeping in mind that all the three coefficients are powers of each other.
Is there a package in R for this?
Please do note that the Taylor series expansion was necessary as there are several other terms in the original equation which I haven't mentioned here.
Edit:
The original equation I have is:
Y ~ (1 - aX)(B^b)(C^c)(D^d)
In the above equation I have to estimate a,b,c,d, where a is to be estimated as aconstant while b,c and d as smooth splines.
So I have taken log on both side, which makes it:
log(Y) ~ log(1 - aX) + blog(B) + clog(C) + d*log(D)
If there is a better way to approach the entire equation, do mention.
Comment: It's no simplification--instead, it creates errors--to use the Taylor series instead of the function itself.  I would like to suggest you will get answers that are much more useful for your problem if you would ask us about the problem you actually have, rather than pursuing this approach.
Comment: One option would be using non-linear least squares to estimate a,b,c,d without doing any uncessary approximations. This algorithm is available via the `nls` function in R.
Comment: thanks alot for all the help that has been provided. I haven't figured out the best way to solve the problem yet, but there are several directions which I have gotten which I will be pursuing. Firstly, as mentioned I will be looking at the nls function to optimize the function. Secondly, given that as suggested, log-normal distribution assumption might not the the best way, so I will be looking into how to estimate the parameters assuming a Poisson or NB distribution
Comment: Worth a read for the OP: https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem
Comment: even if there is a better way to approach the original problem, I would still like to know how to estimate coefficients when the coefficients are the power of each other.
Comment: I have edited the question and added the original equation I am trying to optimize.
Comment: thanks Jarle. I will look into the nls package. Does this package allow for some terms to be estimated as splines like GAM package?
Comment: The two models (original and logged) are *different*: they (implicitly) posit different ways in which $Y$ might depart from the predicted values.  `nls`, for instance, could produce rather different parameter estimates for the two models, depending on the data. (BTW, `nls` is part of the base `R` distribution--it's not a package.)  Could you tell us anything about the distribution of the errors?  If not, we might be able to deduce which model is more appropriate by knowing what $Y$ represents and how it is measured.
Comment: thanks for the comment whuber. The error is log-normally distributed. The equation with error term becomes Y ~ (1 - aX)(B^b)(C^c)(D^d)(e) where e is the error term. Y is a non-negative integer valued variable.
Comment: How do you conclude the error is lognormal? If $Y$ is integer-valued, $e$  would not be lognormal.
Comment: the assumption that error is lognormal is made by the original paper which proposed the method. Of course because the data is count data, the distribution cannot be continuous, but I assume that approximating the distribution of error for count data as a continuous distribution shouldn't cause any problem. Am I wrong in assuming that?
Answer: In this particular problem, $Y$ is linear in $a$, holding $b,c,d$ constant, and $\log Y$ is linear in $b,c,d$ holding $a$ constant. So we could probably use coordinate descent and least squares. Although, it's entirely possible that nls would be faster.
(Didn't test code)
Given vectors $Y,X,B,C,D$,
    #setup maximum iteration, tolerance, and initialize parameters
    max.iter <- 50
    tol <- 1e-4
    i <- a <- b <- c <- d <- 1
    log.y <- log(Y); log.b <- log(B)
    log.c <- log(C); log.d <- log(D)
    while(i <= max.iter){
      #a-update holdig b,c,d constant
        y.new <- Y/(B^b*C^c*D^d) - 1
        my.qr <- qr(-X)
        a <- qr.coef(my.qr, y.new)
      #b,c,d update holding a constant
        y.new <- log.y - log(1-a*X)
        my.qr <- qr(cbind(log.b, log.c, log.d))
        bcd <- qr.coef(my.qr, y.new)
        b <- bcd[1]
        c <- bcd[2]
        d <- bcd[3]
      #check convergence
      param.new <- c(a, bcd)
      if(norm(param.new-param.old) < tol) break
      else param.old <- param.new
      i <- i + 1
    }
    print(param.new)

Comment: Given additional information that the error is log-normal, this solves the wrong optimization problem.

Estimating polymomial coefficient in R


shasum returns an incorrect result

Question: Calculating SHA256 gives me an incorrect result when I'm using "shasum":
$ echo "abcd" | shasum -a 256
fc4b5fd6816f75a7c81fc8eaa9499d6a299bd803397166e8c4cf9280b801d62c  -

# or

$ echo abcd | shasum -a 256
fc4b5fd6816f75a7c81fc8eaa9499d6a299bd803397166e8c4cf9280b801d62c  -

And in ruby:
$ irb
irb(main):001:0> require "digest"
=> true

irb(main):005:0> Digest::SHA256.hexdigest("abcd")
=> "88d4266fd4e6338d13b845fcf289579d209c897823b9217da3e161936f031589"

Online generators give me the result as ruby does.
Why isn't the result of "shasum" correct?
Answer: This is because echo adds a new-line character to the end of its output, by default. Try using:
echo -n abcd | ...

instead, which suppresses the new-line at the end.

shasum returns an incorrect result