Is there a measurement for the vocal speed of a song?

Question: I'm trying to find a way to measure the speed of a song from the perspective of singing/rapping. So far all I can find is BPM (beats per minute), however there are at least 2 issues with that:

The BPM number depends on which beats are counted, quite often a website will report twice the BPM that I measure by myself.
The BPM is not always related to the vocal speed. For example, "Thoughts Of A Dying Atheist" by Muse has 179 BPM but the singing part is quite slow, while "Break Ya Neck" by Busta Rhymes has only 83 BPM, but a... neck-breaking rapping speed.

Is there a more accurate measurement for the vocal speed? I'm thinking something along the lines of (continuous) syllables per minute, excluding the instrumental parts.
Comment: @DoktorMayhem 1) perhaps I am, perhaps the person who published the number on the website is; 2) that's my point, how to measure it?
Comment: part 1) perhaps you are miscounting beats. part 2) vocal speed does not have bpm
Comment: By the way, sometimes different websites show different numbers for the same song (usually one is double the other, but not always)
Comment: Syllables/second? Words/min? Notes/second?
Comment: There's probably a similar term for generic music note rates. Notes/second, perhaps? Liszt's Transcendental Etude No. 6 in G Minor ("Vision") has a slow tempo (bpm) but loads of ornamentation and therefore a deceptively high note rate....
Answer: There is the concept (not specific to music) of Speech Tempo. As you will see from the article, there is some degree of discussion about how this should be measured - for example, words per minute, syllables per second, or sounds per second. One could imagine even deeper levels of granularity - such as the inclusion of changes in pitch or timbre as sound 'features'.
Within rap music, it's common to measure rap performances in terms of syllables per second - such performances have been noted in the Guinness book of Records.
Comment: Interesting, it's hard to decide how to split things up, and the wide variety of singing styles makes that even trickier. I'm thinking about the ending of "Don't Cry" by Guns N' Roses for example, first the "Don't you ever cry-y-yy-i-i" then the famous endless "toniiiiiii...."
Comment: I think syllables per second (or minute) can be a good reference though, for most songs. I wish it was more common to measure that too, not just the BPM.
Comment: @DavidBowling I think we have very different scenarios in mind. You're probably thinking of a situation where you have the full sheet music for a song, and a metronome, and you're just figuring out how fast you need to sing. I'm thinking about a case where you are asked to pick any song that you'd like to sing, but it has to be reasonably fast. First of all, how fast is "fast"? There needs to be some kind of measurement. You pick a song that is reported to have over 140 bpm, but it gets rejected because the vocal part is too slow. Now what?
Comment: @DavidBowling BPM is useful both as a direction ("how fast do I play this?") and a metric ("I want something at such-and-such a tempo"). Something like Speech Tempo isn't so useful as a direction, but, in a world where it was measured, could nevertheless be useful as a metric to find pieces that fit a certain profile (without having to listen to them!)
Comment: @DavidBowling yes, BPM + "predominant subdivision" would be along the right lines. My initial thought is that I don't hear "predominant subdivision" mentioned as an analytical metric anywhere near as much as time signature or BPM / tempo indication, which may be why the OP is on a quest...
Comment: @aditsu -- the musical phrases played instruments frequently pass by more quickly than the BPM would indicate, i.e. as streams of eighth notes, sixteenth notes, or whatever. Voice is really just another musical instrument, and the same thing happens with the phrasing of lyrics. Why doesn't musical notation suffice for you? BPM only indicates something about the pulse of the music; staff notation shows you more about how the music is phrased. Yet nobody counts the equivalent of syllables per second for saxophone solos....
Comment: @aditsu -- I'm not thinking of any scenario, just trying to understand what use a syllable-per-second metric would be; but it sounds like you are looking for an additional metric to use in beat-matching. Traditionally, knowing that you have, e.g. 200BPM with predominantly sixteenth notes, has been enough to communicate not only that something is fast, but exactly how fast it is. You don't need sheet music to do this, you only have to listen. The measurement you seek would be "sixteenth notes" in this case, but it could be "800 syllables per second" (too fast for most mouths).
Comment: @DavidBowling Lots of people can sing by ear. Not all of them can tell which beats are being counted in the BPM number. Not a lot of those can tell if they're singing 16th, 8th or 4th notes or something else. Even fewer can compare different durations at different BPMs (as I wrote in another comment, a 5/16 note at 80bpm is longer than a 3/8 note at 120bpm, but it's hard to tell without a calculator). But everybody can count syllables and seconds.
Comment: @topomorto -- I get that, but my point is that we already have something to do that. Describe the BPM, then describe the predominant subdivision. A DJ website _could_ give "140BPM, eighth-note triplet." Somebody is going to have to listen no matter what metric you are using, and in either case that somebody could be an algorithm.
Answer: I humbly propose "maximum notes/second": Take the fastest part of the song and calculate the number of notes in that amount of time. If you have a passage that is super-fast in one part but really slow in another, it's way more important to know that you need to be able to rap/sing/play at that fastest part's speed to play the whole song. Compare the chorus of Eminem's "Rap God" to the fast part. Obviously, to tackle that song, one needs to be able to rap as fast as the fastest part, not just the average of the whole song.
Clearly, one would have to experiment with certain parameters, such as minimum measured time, and even definition of a note (slides? bends?) in order to get useful results.
Disclaimer: No one uses this, because I made it up. I think it's way more useful than average notes/second.
Comment: That's a good point, and could make it easier to measure. I think the average is also relevant, and the fastest part should probably have a minimum length (e.g. at least 5 seconds).
Comment: @aditsu Right, otherwise one-note passages approach infinite speed...
Comment: I wasn't thinking about single notes, but rather quick "bursts" that may last 1-2 seconds and are not representative of the whole song.
Comment: @aditsu but that's my point. To play the song, you need to be as fast as the bursts!
Comment: Well, if the fast part in "Rap God" was only 1 second long, I think almost anybody could do it. What makes it really hard is that it keeps going for about 15 sec.
Comment: Regardless, it's a constant speed. @aditsu
Answer: I don't think there's a useful single value that you can use to summarise a song like this. Instead, it's probably most useful to consider a combination of the BPM, the length of rhythmic subdivisions, and distribution of rhythms. For example, Break Ya Neck has a BPM of 166*, and semiquaver/quaver (16th note/8th note) rhythms sustained over the period of several bars. In comparison Thoughts Of A Dying Atheist is 179 BPM but the rhythms are mainly a mixture of crotchets (quarter notes) and longer notes, with no consecutive passages of 8th notes.
*I know you said 83 in your question but I think 166 is more accurate. When/why BPM values can vary by a factor of two would be a good topic for another question!
Comment: Funny, I actually looked it up, and multiple sources said 83, and my own count also confirmed that. But I also see how it can be counted as 166. Anyway, I hope you get my main point: some songs have lower BPM but faster lyrics/vocals.
Comment: I'm not sure if "a combination of the BPM, the length of rhythmic subdivisions, and distribution of rhythms" is really practical. It may be accurate, but difficult to interpret as a meaningful measure of speed (for many people). E.g. a 5/16 note at 80bpm is longer than a 3/8 note at 120bpm, but it's hard to tell without a calculator.
Comment: @aditsu Maybe, but if you try something simpler I don't think it will capture what you want. However, also note that 5/16th notes aren't really a thing. Usually you would talk about 1/4, 1/8, 1/16 notes and their triplet variations in which case it is quite easy to compare songs since you can double BPM and note length to keep the actual rhythm the same.
Comment: I think something simpler could work better and would be easier to use, but your suggestion is also valid. 5/16th is a quarter note tied to a 16th note, as shown for example at https://en.wikipedia.org/wiki/Tie_(music) , but I agree that it would seldom come up. Some songs are a bit harder to analyze though (without the sheet music), e.g. "Hoy" by Gloria Estefan.
Comment: I think that song is about 195 BPM with a mixture of 1/4 and 1/8 notes in the vocal line.
Comment: I measured 98 bpm, some sheets have it at 100. It has a mixture of 3/16 and 1/8 notes, a lot of them off-beat, with a bunch of 1/16s here and there. Without any sheet music I would struggle to figure out the note durations.
Answer: if you look at it from a DJ perspective it will always be BPM value.   Think of it as an acapella in sync with a rhythm track or beat.


Apache not finding index.php by default, set rule for routing through index.php

Question: Apache on the server is set to find index.php by default, and that works for a normal folder.
However, I have a .htaccess rule to route all requests through my routing script:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]

With these .htaccess contents, the server returns a 404 error. Only by specifying /index.php does the routing script get called.
Any tips on what I am doing wrong?
Answer: It sounds like you're not allowing .htaccess files to be used.  You need a <Directory> stanza that contains:
AllowOverride All

The option All may not be best for your environment, so take a look at this documentation to see what overrides you will allow in .htaccess.


Pressing button in fragment, refresh the activity

Question: I have fragment with few buttons.
I put that fragment in all activities in my app.
I am using the button to travel between the activities
So far everything work fine.
I want to highlight the button that was press.(i am using image button, so i want to change the image)
For example, if i press Home button, i will move to home activity and the home button will have different image
I want the handle this code form the fragment.
here is the relevant code from the fragment:
<code>       @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
       // Inflate the layout for this fragment
            View view = inflater.inflate(R.layout.fragment_buttons, container,
            false);

    ImageButton btnHome = (ImageButton) view.findViewById(R.id.btnHome);
    btnHome.setOnClickListener(this);

    ImageButton btnSetting = (ImageButton) view.findViewById(R.id.btnSetting);
    btnSetting.setOnClickListener(this);

    return view;

}

 @Override
public void onClick(View v) {
    ImageButton imageButton = (ImageButton) v;

    switch (imageButton.getId()) {
    case R.id.btnHome:
        imageButton.setImageResource(R.drawable.btnhomev);
        startActivity(new Intent("com.example.sonoside2.HOME"));
        break;
    case R.id.btnSetting:
        imageButton.setImageResource(R.drawable.btnsettingv);
        startActivity(new Intent("com.example.sonoside2.SETTING"));
        break;
 }
</code>
THI
Answer: Implement the method onActivityCreated in your fragment.
Here you can get your Activity Object like this:
<code>public void onActivityCreated (Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    Activity a = getActivity();
    String packageName = a.getPackageName();
    if (packageName.equalsIgnoreCase("com.example.sonoside2.HOME")) {
        //do something to highlight your buttons
    } else ...
}
</code>


How to correctly modify this Wordpress Function for widgets?

Question: I understand the below function for Wordpress widgets:
register_sidebar(array(
        'name' => 'sidebar',
        'before_widget' => '

',
        'after_widget' => '
',
        'before_title' => '
',
        'after_title' => '
',
    ));

as before_title and after_title are my H2 titles of the widget, and the before_widget and after_widget encases the widget itself.
But what if I need to put a class inside the widget, instead of outside? I want all content inside the widget to have a certain class. What would the correct syntax be for this (inside_widget)?
Comment: See http://wordpress.org/support/topic/328671
Answer: May be you need this
This sample code creates a Widget named FooWidget that has a settings form to change the display title.
/**
 * FooWidget Class
 */
class FooWidget extends WP_Widget {
    /** constructor */
    function FooWidget() {
        parent::WP_Widget(false, $name = 'FooWidget');
    }

    /** @see WP_Widget::widget */
    function widget($args, $instance) {
        extract( $args );
        $title = apply_filters('widget_title', $instance['title']);
        ?>
              <?php echo $before_widget; ?>
                  <?php if ( $title )
                        echo $before_title . $title . $after_title; ?>
                  Hello, World!
              <?php echo $after_widget; ?>
        <?php
    }

    /** @see WP_Widget::update */
    function update($new_instance, $old_instance) {
        return $new_instance;
    }

    /** @see WP_Widget::form */
    function form($instance) {
        $title = esc_attr($instance['title']);
        ?>

<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />

        <?php
    }

} // class FooWidget
// register FooWidget widget
add_action('widgets_init', create_function('', 'return register_widget("FooWidget");'));

May be you need this


snowflake json retrieve tags with specific value

Question: Have values in a snowflake table like this:
{
  "value": "0",
  "_my_id1": "111",
  "_my_id2": "123",
  "some_tag": "random",
  "something_else": "random1",
  "seg": "123,234,234,444,555"
  "some_id": "000987",
  "plus_tag": "yes"
}

from this tag, need to retrieve all keys that contain "ID" in it, looking for results like:
  "_my_id1": "111",
  "_my_id2": "123",
  "some_id": "000987"

Result can be json or string
Answer: Using following as data -
with data_cte as
(
select column1 from values('{
"value": "0",
"_my_id1": "111",
"_my_id2": "123",
"some_tag": "random",
"something_else": "random1",
"seg": "123,234,234,444,555",
"some_id": "000987",
"plus_tag": "yes"
}')
)
select * from data_cte;
+-------------------------------+
| COLUMN1                       |
|-------------------------------|
| {                             |
| "value": "0",                 |
| "_my_id1": "111",             |
| "_my_id2": "123",             |
| "some_tag": "random",         |
| "something_else": "random1",  |
| "seg": "123,234,234,444,555", |
| "some_id": "000987",          |
| "plus_tag": "yes"             |
| }                             |
+-------------------------------+

Query to get all with ID key -
with data_cte as
(
select column1 from values('{
"value": "0",
"_my_id1": "111",
"_my_id2": "123",
"some_tag": "random",
"something_else": "random1",
"seg": "123,234,234,444,555",
"some_id": "000987",
"plus_tag": "yes"
}')
)
select key,value from data_cte,
lateral flatten(input=>parse_json(column1))
where key like '%id%';
+---------+----------+
| KEY     | VALUE    |
|---------+----------|
| _my_id1 | "111"    |
| _my_id2 | "123"    |
| some_id | "000987" |
+---------+----------+

Construct back JSON from above output as  -
with data_cte as
(
select column1 from values('{
"value": "0",
"_my_id1": "111",
"_my_id2": "123",
"some_tag": "random",
"something_else": "random1",
"seg": "123,234,234,444,555",
"some_id": "000987",
"plus_tag": "yes"
}')
)
select to_json(object_construct(key,value)) as col from data_cte,
lateral flatten(input=>parse_json(column1))
where key like '%id%';
+----------------------+
| COL                  |
|----------------------|
| {"_my_id1":"111"}    |
| {"_my_id2":"123"}    |
| {"some_id":"000987"} |
+----------------------+

Output as String -
with data_cte as
(
select column1 from values('{
"value": "0",
"_my_id1": "111",
"_my_id2": "123",
"some_tag": "random",
"something_else": "random1",
"seg": "123,234,234,444,555",
"some_id": "000987",
"plus_tag": "yes"
}')
)
select '{'||listagg(key||':'||value,',')||'}' as col from data_cte,
lateral flatten(input=>parse_json(column1))
where key like '%id%';
+------------------------------------------+
| COL                                      |
|------------------------------------------|
| {_my_id1:111,_my_id2:123,some_id:000987} |
+------------------------------------------+


Principal Component Analysis Stock Returns

Question: I am new to PCA and am having trouble understanding some parts of the methodology. In multi-factor models you can run regressions like:
Stock Return = A(1)*Size + A(2)*Market Return + ...
So in this case, you have a clear understanding of what is driving the stocks return. Now, when you have a system of say 100 stocks and extract the 3 most important principal components, is there any way of actually understanding where the risk is coming from? For example, if you ran a regression like:
Stock Return = A(1)*PC1 + A(2)*PC2
While that would tell you the effect of each component, how can that actually help an investor manage their portfolio? Because the PC doesn't really tell you what the actual risk factor is right? Or are you supposed to then run something like:
PC1 = A(1)*Size +...+
To determine what underlies each component?
Please help me with an intuitive answer.
Comment: Since each PC is (by definition) a linear combination of the original variables, by merely expanding each PC out in the second model, you will have written a model that is *exactly* in the first form you present.  Could you explain what you mean by an "actual risk factor"?
Comment: I basically want to study the determinants of returns for various stocks in an emerging economy. I am currently deciding between quantile regression, PCA and other factor models. What I mean is if I estimate Returns = A(1)Size, I know that the effect is coming from the size of the firm. In contrast, if I use PCs I don't really know what the underlying determinants are right? So if I estimate Returns = A(1)PC1, I know the effect of the first component on returns but if I am an investor this doesn't really tell me what is driving returns. Does this make sense?
Comment: Sorry I am just using individual stocks as an example I am actually using data on the stock index.
Answer: Stock returns is not the good place to start with learning PCA, because it will not produce you interpretable factors. If your objective is to understan PCA then it's better to start with interest rates, yield curves. There you often get first three factors roughly corresponding to the level, slope and the curvature of the yield curves.
With the stock returns, maybe the first factor could be mapped to the market factors, i.e. similar to CAPM. Otherwise, you'll get a bunch of PCs that are hard to assign to any intuitive or usual risks.


changing a canvas's dimensions to fit to screen. (canvas was exported from adobe illustrator and not personally coded)

Question: im more of graphic designer than a coder or web developer so my knowledge is a bit limited, but i cant seem to be able to change my fixed canvas size (the pixel size it was in illustrator) to a fit-to-screen size. and all the posts pertaining to the subject have failed me thus fair (probably by my own user error). thanks for taking a look!
the page im working on is up and running at http://www.advancedseismic.com
i tried following the example on Resize HTML5 canvas to fit window but to no avail.
here is the div the canvas is tagged in:

  <canvas id="layer4" style="z-index: 0; position:absolute; left:0px; top:0px;"></canvas>

here is the canvas
var layer4;
var ctx4;

function init() {

    layer4 = document.getElementById("layer4");
    ctx4 = layer4.getContext("2d");
    setInterval(drawAll, .1);
    drawAll();
}

function drawAll() {
draw4();
}

function draw4() {

  var alpha = ctx4.globalAlpha;
  var gradient;

  // background/Image
  ctx4.save();
  ctx4.drawImage(document.getElementById("image1"), 0.0, 0.0);
  ctx4.restore();

  // foreground/Path
  ctx4.save();
  ctx4.beginPath();
  ctx4.moveTo(1225.7, 792.2);
  ctx4.lineTo(0.1, 792.5);
  ctx4.lineTo(0.1, 519.8);
  ctx4.lineTo(1225.7, 520.9);
  ctx4.lineTo(1225.7, 792.2);
  ctx4.closePath();
  gradient = ctx4.createLinearGradient(612.9, 792.5, 612.9, 519.8);
  gradient.addColorStop(0.00, "rgb(255, 255, 255)");
  gradient.addColorStop(1.00, "rgb(0, 0, 0)");
  ctx4.fillStyle = gradient;
  ctx4.fill();
  ctx4.restore();

  // emailinput/Path
  ctx4.save();
  ctx4.beginPath();
  ctx4.moveTo(376.5, 166.3);
  ctx4.bezierCurveTo(376.5, 159.7, 381.9, 154.3, 388.5, 154.3);
  ctx4.lineTo(804.1, 154.3);
  ctx4.bezierCurveTo(810.7, 154.3, 816.1, 159.7, 816.1, 166.3);
  ctx4.lineTo(816.1, 197.8);
  ctx4.bezierCurveTo(816.1, 204.4, 810.7, 209.8, 804.1, 209.8);
  ctx4.lineTo(388.5, 209.8);
  ctx4.bezierCurveTo(381.9, 209.8, 376.5, 204.4, 376.5, 197.8);
  ctx4.lineTo(376.5, 166.3);
  ctx4.closePath();
  ctx4.fillStyle = "rgb(255, 255, 255)";
  ctx4.fill();
  ctx4.restore();

  // mailicon/Image
  ctx4.save();

  ctx4.restore();

  // emailbutton/Path
  ctx4.save();
  ctx4.beginPath();
  ctx4.moveTo(1044.6, 198.0);
  ctx4.bezierCurveTo(1044.6, 204.6, 1039.1, 209.9, 1032.5, 209.9);
  ctx4.lineTo(839.7, 209.9);
  ctx4.bezierCurveTo(833.1, 209.9, 827.4, 204.3, 827.4, 197.7);
  ctx4.lineTo(827.5, 165.9);
  ctx4.bezierCurveTo(827.5, 159.3, 832.7, 154.4, 839.3, 154.4);
  ctx4.lineTo(1032.6, 154.2);
  ctx4.bezierCurveTo(1039.3, 154.2, 1044.5, 159.3, 1044.5, 166.0);
  ctx4.lineTo(1044.6, 198.0);
  ctx4.closePath();
  gradient = ctx4.createLinearGradient(936.0, 209.9, 936.0, 154.2);
  gradient.addColorStop(0.00, "rgb(237, 28, 36)");
  gradient.addColorStop(0.80, "rgb(239, 59, 38)");
  gradient.addColorStop(1.00, "rgb(240, 90, 40)");
  ctx4.fillStyle = gradient;
  ctx4.fill();

  // emailbutton/Request an Invitation
  ctx4.font = "21.0px 'Arial'";
  ctx4.fillStyle = "rgb(255, 255, 255)";
  ctx4.fillText("Request an Invitation", 838.7, 189.2);
  ctx4.restore();

  // compstand/Group
  ctx4.save();

  // compstand/Group/Mesh
  ctx4.save();
  ctx4.drawImage(document.getElementById("image3"), 516.8, 570.5);

  // compstand/Group/Group

  // compstand/Group/Group/Path
  ctx4.save();
  ctx4.beginPath();
  ctx4.moveTo(691.0, 635.8);
  ctx4.bezierCurveTo(691.0, 635.7, 690.9, 635.7, 690.9, 635.7);
  ctx4.bezierCurveTo(690.9, 635.7, 691.0, 635.7, 691.0, 635.8);
  ctx4.closePath();
  gradient = ctx4.createLinearGradient(690.9, 635.7, 691.0, 635.7);
  gradient.addColorStop(0.00, "rgb(186, 187, 189)");
  gradient.addColorStop(0.24, "rgb(106, 106, 106)");
  gradient.addColorStop(0.63, "rgb(186, 187, 189)");
  gradient.addColorStop(1.00, "rgb(88, 88, 88)");
  ctx4.fillStyle = gradient;
  ctx4.fill();

  // compstand/Group/Group/Path
  ctx4.beginPath();
  ctx4.moveTo(692.1, 637.7);
  ctx4.bezierCurveTo(691.9, 638.4, 690.7, 639.1, 687.4, 639.2);
  ctx4.bezierCurveTo(680.3, 639.4, 604.9, 639.2, 604.9, 639.2);
  ctx4.bezierCurveTo(604.9, 639.2, 529.5, 639.4, 522.5, 639.2);
  ctx4.bezierCurveTo(518.5, 639.0, 517.5, 638.2, 517.8, 637.2);
  ctx4.bezierCurveTo(516.8, 639.4, 517.2, 643.0, 522.5, 643.2);
  ctx4.bezierCurveTo(529.5, 643.4, 604.9, 643.2, 604.9, 643.2);
  ctx4.bezierCurveTo(604.9, 643.2, 680.3, 643.4, 687.4, 643.2);
  ctx4.bezierCurveTo(692.2, 643.0, 692.8, 639.9, 692.1, 637.7);
  ctx4.closePath();
  gradient = ctx4.createLinearGradient(517.4, 640.3, 692.3, 640.3);
  gradient.addColorStop(0.00, "rgb(186, 187, 189)");
  gradient.addColorStop(0.24, "rgb(106, 106, 106)");
  gradient.addColorStop(0.63, "rgb(186, 187, 189)");
  gradient.addColorStop(1.00, "rgb(88, 88, 88)");
  ctx4.fillStyle = gradient;
  ctx4.fill();
  ctx4.restore();
  ctx4.restore();
  ctx4.restore();

  // comingsoon/Coming Soon
  //ctx4.save();
  //ctx4.font = "60.0px 'Simply*Glamorous'";
  //ctx4.fillStyle = "rgb(255, 255, 255)";
  //ctx4.fillText("Coming Soon . . . ", 413.8, 86.6);
  //ctx4.restore();

  // compreflect/Group
  ctx4.save();
  ctx4.globalAlpha = alpha * 0.90;

  // compreflect/Group/Group
  ctx4.save();
  ctx4.globalAlpha = alpha * 0.20;

  // compreflect/Group/Group/Mesh
  ctx4.save();
  ctx4.globalAlpha = alpha * 1.00;
  ctx4.drawImage(document.getElementById("image4"), 517.9, 651.4);

  // compreflect/Group/Group/Group

  // compreflect/Group/Group/Group/Path
  ctx4.save();
  ctx4.beginPath();
  ctx4.moveTo(519.7, 655.7);
  ctx4.bezierCurveTo(519.7, 655.7, 519.8, 655.7, 519.8, 655.8);
  ctx4.bezierCurveTo(519.8, 655.7, 519.7, 655.7, 519.7, 655.7);
  ctx4.closePath();
  gradient = ctx4.createLinearGradient(519.8, 655.7, 519.7, 655.7);
  gradient.addColorStop(0.00, "rgb(186, 187, 189)");
  gradient.addColorStop(0.24, "rgb(106, 106, 106)");
  gradient.addColorStop(0.63, "rgb(186, 187, 189)");
  gradient.addColorStop(1.00, "rgb(88, 88, 88)");
  ctx4.fillStyle = gradient;
  ctx4.fill();

  // compreflect/Group/Group/Group/Path
  ctx4.globalAlpha = alpha * 0.50;
  ctx4.beginPath();
  ctx4.moveTo(518.6, 653.8);
  ctx4.bezierCurveTo(518.8, 653.0, 520.0, 652.4, 523.3, 652.2);
  ctx4.bezierCurveTo(530.4, 652.0, 605.8, 652.2, 605.8, 652.2);
  ctx4.bezierCurveTo(605.8, 652.2, 681.1, 652.0, 688.2, 652.2);
  ctx4.bezierCurveTo(692.2, 652.4, 693.2, 653.3, 692.9, 654.2);
  ctx4.bezierCurveTo(693.9, 652.0, 693.5, 648.4, 688.2, 648.2);
  ctx4.bezierCurveTo(681.1, 648.0, 605.8, 648.2, 605.8, 648.2);
  ctx4.bezierCurveTo(605.8, 648.2, 530.4, 648.0, 523.3, 648.2);
  ctx4.bezierCurveTo(518.5, 648.4, 517.9, 651.5, 518.6, 653.8);
  ctx4.closePath();
  gradient = ctx4.createLinearGradient(543.6, -251.1, 561.6, 9.9);
  gradient.addColorStop(0.00, "rgb(186, 187, 189)");
  gradient.addColorStop(0.24, "rgb(106, 106, 106)");
  gradient.addColorStop(0.63, "rgb(186, 187, 189)");
  gradient.addColorStop(1.00, "rgb(88, 88, 88)");
  ctx4.fillStyle = gradient;
  ctx4.fill();

  // compreflect/Group/Path
  ctx4.restore();
  ctx4.restore();
  ctx4.globalAlpha = alpha * 0.50;
  ctx4.beginPath();
  ctx4.moveTo(348.9, 736.7);
  ctx4.bezierCurveTo(352.2, 730.6, 359.7, 720.5, 366.9, 720.5);
  ctx4.lineTo(844.9, 720.5);
  ctx4.bezierCurveTo(852.1, 720.5, 858.6, 731.6, 863.3, 738.0);
  ctx4.lineTo(898.2, 792.5);
  ctx4.bezierCurveTo(864.3, 792.5, 311.1, 792.6, 310.9, 792.5);
  ctx4.lineTo(348.9, 736.7);
  ctx4.closePath();
  gradient = ctx4.createLinearGradient(898.2, 756.5, 310.9, 756.5);
  gradient.addColorStop(0.00, "rgb(177, 178, 180)");
  gradient.addColorStop(0.63, "rgb(192, 193, 195)");
  gradient.addColorStop(1.00, "rgb(187, 188, 189)");
  ctx4.fillStyle = gradient;
  ctx4.fill();

  // compreflect/Group/Compound Path
  ctx4.globalAlpha = alpha * 0.20;
  ctx4.beginPath();

  // compreflect/Group/Compound Path/Path
  ctx4.moveTo(844.1, 726.0);
  ctx4.lineTo(366.1, 726.0);
  ctx4.bezierCurveTo(361.3, 726.0, 359.9, 730.8, 355.4, 736.1);
  ctx4.bezierCurveTo(355.4, 736.1, 320.8, 789.0, 318.1, 792.5);
  ctx4.lineTo(886.7, 792.5);
  ctx4.bezierCurveTo(890.6, 796.2, 852.7, 734.7, 852.7, 734.7);
  ctx4.bezierCurveTo(849.4, 730.8, 848.8, 726.0, 844.1, 726.0);
  ctx4.closePath();

  // compreflect/Group/Compound Path/Path
  ctx4.moveTo(865.1, 792.5);
  ctx4.lineTo(346.2, 792.5);
  ctx4.lineTo(373.8, 743.1);
  ctx4.lineTo(836.3, 743.1);
  ctx4.lineTo(865.1, 792.5);
  ctx4.closePath();
  ctx4.fillStyle = "rgb(1, 1, 1)";
  ctx4.fill();
  ctx4.restore();
  ctx4.restore();

  // compback/Path
  ctx4.save();
  ctx4.beginPath();
  ctx4.moveTo(858.9, 559.1);
  ctx4.bezierCurveTo(858.9, 566.3, 853.0, 572.2, 845.8, 572.2);
  ctx4.lineTo(367.9, 572.2);
  ctx4.bezierCurveTo(360.7, 572.2, 354.8, 566.3, 354.8, 559.1);
  ctx4.lineTo(355.7, 246.0);
  ctx4.bezierCurveTo(355.7, 240.0, 362.5, 236.0, 369.7, 236.0);
  ctx4.lineTo(846.7, 237.0);
  ctx4.bezierCurveTo(853.9, 237.0, 860.7, 242.8, 860.7, 250.0);
  ctx4.lineTo(858.9, 559.1);
  ctx4.closePath();
  gradient = ctx4.createLinearGradient(354.8, 404.1, 860.7, 404.1);
  gradient.addColorStop(0.00, "rgb(177, 178, 180)");
  gradient.addColorStop(0.63, "rgb(192, 193, 195)");
  gradient.addColorStop(1.00, "rgb(187, 188, 189)");
  ctx4.fillStyle = gradient;
  ctx4.fill();
  ctx4.restore();

  // compscreen/Path
  ctx4.save();
  ctx4.beginPath();
  ctx4.moveTo(845.8, 556.9);
  ctx4.lineTo(371.9, 556.9);
  ctx4.lineTo(371.9, 253.4);
  ctx4.lineTo(845.8, 253.4);
  ctx4.lineTo(845.8, 556.9);
  ctx4.closePath();
  ctx4.fill();
  ctx4.restore();

  // compframe/Compound Path
  ctx4.save();
  ctx4.beginPath();

  // compframe/Compound Path/Path
  ctx4.moveTo(846.8, 241.0);
  ctx4.lineTo(368.9, 241.0);
  ctx4.bezierCurveTo(364.1, 241.0, 360.2, 244.9, 360.2, 249.7);
  ctx4.lineTo(360.2, 557.4);
  ctx4.bezierCurveTo(360.2, 562.2, 364.1, 566.1, 368.9, 566.1);
  ctx4.lineTo(846.8, 566.1);
  ctx4.bezierCurveTo(851.6, 566.1, 855.5, 562.2, 855.5, 557.4);
  ctx4.lineTo(855.5, 249.7);
  ctx4.bezierCurveTo(855.5, 244.9, 851.6, 241.0, 846.8, 241.0);
  ctx4.closePath();

  // compframe/Compound Path/Path
  ctx4.moveTo(839.1, 549.0);
  ctx4.lineTo(376.6, 549.0);
  ctx4.lineTo(376.6, 258.1);
  ctx4.lineTo(839.1, 258.1);
  ctx4.lineTo(839.1, 549.0);
  ctx4.closePath();
  ctx4.fillStyle = "rgb(1, 1, 1)";
  ctx4.fill();
  ctx4.strokeStyle = "rgb(102, 102, 102)";
  ctx4.stroke();
  ctx4.restore();

  // compshine/Path
  ctx4.save();
  ctx4.globalAlpha = alpha * 0.29;
  ctx4.beginPath();
  ctx4.moveTo(855.6, 241.3);
  ctx4.lineTo(381.7, 241.3);
  ctx4.lineTo(382.0, 258.4);
  ctx4.bezierCurveTo(507.1, 328.3, 722.2, 427.3, 855.6, 491.0);
  ctx4.lineTo(855.6, 241.3);
  ctx4.closePath();
  gradient = ctx4.createLinearGradient(562.0, 358.3, 899.9, 367.3);
  gradient.addColorStop(0.00, "rgb(255, 255, 255)");
  gradient.addColorStop(1.00, "rgb(187, 188, 189)");
  ctx4.fillStyle = gradient;
  ctx4.fill();
  ctx4.restore();

}

init();

sorry its so much. any help or recommendations are greatly appreciated!
Answer: Have you tried
layer4.width = window.innerWidth;
layer4.height = window.innerHeight;

? Remember that you should run the code before the canvas is drawn, or else the elements may become distorted.


Atualizar um campo do MySQL de vários alunos

Question: Tenho uma tabela de alunos onde tem um campo de presença onde estão gravados  presente e ausente.
Quando criei o campo, deixei todos como ausente, e agora preciso listar e alterar alguns para presente. Gostaria de alterar todos de uma só vez, mas não sei como fazer um update em todos os registros de uma só vez.
Veja o código do select dos campos, onde 2 significa ausente e 1 seria presente, (estou tentando com checkbox):
form.php
<?php
$conn = mysqli_connect($servidor, $usuario, $senha, $dbname);
$idEvent = $_POST['idsubev'];
?>
<?php
//
$sql = "SELECT u.nome, e.titulo, a.presente, a.id_al FROM sch_usuarios u INNER JOIN sch_acontecimentos e INNER JOIN sch_aluno_acont a WHERE e.id_acon = a.id_acon AND u.id = a.id_al AND e.id_subevent='$idEvent' ORDER BY u.nome";

$query = mysqli_query($conn, $sql);
while ($rows = mysqli_fetch_array($query)) {

echo "
<form method='post' action = 'update.php' >
   <input type='hidden' name='id' value='".$rows['id_al']."'>

 Alterar presença do aluno

Nome:

        <input type = 'text' size='50' name='nome' value ='".$rows['nome']."'>

 Curso:
        <input type='text' size='30' name='curso' value=' ".$rows['titulo']."'>

    //QUERO ATUALIZAR ESSE CAMPOS ABAIXO CHAMADO PRESENTE
        Presente=".$rows['presente']."

            //AQUI MARCAREI O CHECKBOX NOS ALUNOS QUE QUERO MUDAR PARA 1 (PRESENTE)
            Status 1= presente, 2= ausente
            <input type='checkbox' name='presente' value='1'>Marcar Presente?

"; /*fecha a tabela apos termino de impressão das linhas*/
}

echo "<input type='submit' value='alterar'>
</form>";
?>

update.php
$id=$_POST['id'];
$presente = $_POST['presente'];
$mysqli = new mysqli('localhost', 'wwwcard_ew3', 'adm22334455', 'wwwcard_ew3');

$sql = "UPDATE sch_aluno_acont SET presente = '$presente' WHERE id_al = '$id'";
$stmt = $mysqli->prepare($sql) or die($mysqli->error);

if(!$stmt){
  echo 'erro na consulta: '. $mysqli->errno .' - '. $mysqli->error;
}

$stmt->bind_param('ssi',$id, $presente);
$stmt->execute();

header("Location: index.php?altera_aluno");

Comment: Antes de mais nada, faça um backup da tabela, caso alguma coisa dê errado :)
Comment: Só tirar o where, não?
Comment: Como assim?? Não entendi
Comment: Vc tá ticando 1 por 1 e depois dá um submit pra atualizar os que foram ticados?
Comment: Exatamente  ticando os registros q quero atualizar um por um depois dou o submit...
Comment: pelo contexto, o fato do aluno estar presente deveria ser um insert e não um update, mas ok. Pegue os valores que estão marcados, e dê o update assim: `"UPDATE sch_aluno_acont SET presente = '$presente' WHERE id_al in (1,2,3,4,9,7,10)"` considerando que `(1,2,3,4,9,7,10)` são os ids dos alunos que foram marcados.
Comment: Mas como posso pegar esses ids pois vai dar mais de 50
Comment: Se voce quer pegar entre um ID e outro, utilize BETWEEN, ou seja, elq significa entre entao voce indicara os ID de 1 ate 1000, basta utilizar Update nome dos campos FROM nome da tabela WHERE id BETWEEN 1 and 1000
Comment: Os ids são escolhidos de acordo com uma lista de presença onde os alunos presentes são marcados entre todos da lista então teria q fazer dinamicamente de forma q pegasse só os marcados e passasse para dar o update
Comment: Não estou conseguindo  passar os dados do form corretamente,  alguém pode dar um help?.//AQUI MARCAREI O CHECKBOX NOS ALUNOS QUE QUERO MUDAR PARA 1 (PRESENTE)
            Status 1= presente, 2= ausente
            Marcar Presente?
Answer: Não ficou claro o que você quer fazer, pois você perguntou como fazer de uma só vez e seu script está fazendo 1 por 1. porém se fosse para eu fazer com apenas 1 UPDATE faria da seguinte maneira:
form.php
<?php
$conn = mysqli_connect($servidor, $usuario, $senha, $dbname);
$idEvent = $_POST['idsubev'];
$sql = "SELECT u.nome, e.titulo, a.presente, a.id_al FROM sch_usuarios u INNER JOIN sch_acontecimentos e INNER JOIN sch_aluno_acont a WHERE e.id_acon = a.id_acon AND u.id = a.id_al AND e.id_subevent='$idEvent' ORDER BY u.nome";
$query = mysqli_query($conn, $sql);

?>

<form method='post' action = 'update.php' >
    <input type='hidden' name='id' value='".$rows['id_al']."'>

 Alterar presença do aluno

    <?php while ($rows = mysqli_fetch_array($query)) { ?>

Nome:

            <input type = 'text' size='50' name='nome' value ='<?php echo $rows['nome']?>'>

 Curso:
            <input type='text' size='30' name='curso' value=' <?php echo $rows['titulo'] ?>'>

            Presente=<?php $rows['presente'] ?>

                <input type='checkbox' name='presente[]' value='<?php echo $rows['id_al']?>'>Marcar Presente?
                <input type="hidden" name="evento" value="<?php echo $idEvent?>">

    <?php } ?>
    <button type="submit">Salvar</button>
</form>

update.php
<?php
const PRESENTE = 1;
const AUSENTE = 2;

$arrIds = $_POST['presente'];

$mysqli = new mysqli('localhost', 'wwwcard_ew3', 'adm22334455', 'wwwcard_ew3');

$strIds = '';
foreach($arrIds as $id){
    $strIds .= ','. $id;
}
// AQUI VOCÊ IRÁ MARCAR PRESENÇA PARA OS ALUNOS QUE VOCÊ SELECIONOU NA LISTA
$sqlPresente = "UPDATE sch_aluno_acont SET presente = ". PRESENTE ." WHERE id_al = IN(".$strIds.")";

// AQUI VOCÊ IRÁ MARCAR AUSENCIA PARA OS ALUNOS QUE VOCÊ NÃO SELECIONOU OU DESMARCOU NA LISTA E QUE FAZEM PARTE DO EVENTO QUE VOCÊ FILTROU
$sqlAusente = "UPDATE
                    sch_aluno_acont a
                INNER JOIN sch_acontecimentos e
                ON e.id_acon = a.id_acon
                SET presente = ".AUSENTE."
                WHERE e.id_subevent = $idEvent
                AND a.presente != ".AUSENTE."
                AND a.id_al NOT IN(".$strIds.")";

$stmtPresente = $mysqli->prepare($sqlPresente) or die($mysqli->error);
$stmtAusente = $mysqli->prepare($sqlAusente) or die($mysqli->error);

$stmtPresente->execute();
$stmtAusente->execute();

header("Location: index.php?altera_aluno");

?>


Conjugate Classes in Group Theory

Question:
How to find all conjugate classes of group S_3.
Comment: In this very particular case, two elements are conjugated iff they have the same order (can you see why?)
Comment: Welcome to Math.SE: In order to get the best possible answers, it is helpful if you say in what context you encountered the problem, and what your thoughts on it are; this will prevent people from telling you things you already know, and help them give their answers at the right level.
Comment: @DonAntonio And in fact, this is almost the only group with that property.
Comment: @TobiasKildetoft Well, yes...almost the only **finite** such group. The other two are $\;S_1,\,S_2\;$ . But there are infinite groups wilder in this respect, as HNN extensions show. One can construct groups in which *any two non-unit* elements are conjugated (i.e., there are only two conjugation classes)
Answer: In general in $S_n$ two permutation are conjugated iff they have the same cyclical structure( i mean in their decomposition in disjoint cycles the have the same number of r-cycles for all r). Try to prove this, and then the  solution follow easely

Conjugate Classes in Group Theory


What recourse does a player have when a referee makes a scoring mistake in snooker?

Question: Today in the World Championships, the referee, Jan Verhaas, made a scoring mistake in the eighth game of the quarterfinal match between Neil Robertson and Kyren Wilson. The error occurred in Wilson's second visit to the table after breaking off. This is the way it went:

red     referee: one
pink    referee: seven
red     referee: eight
blue    referee: thirteen
red     referee: fourteen
black   referee: nineteen
red     referee: twenty
black   referee: twenty-seven
(miss)  referee: Kyren Wilson, twenty-seven

So, as you can read in the above transcript, the referee errored on the sixth ball of the second visit. He scored a black for five points, as though it was blue, instead of for seven points. This led to the overall point total for visit being two points less than it should have been. Although Wilson did indeed win the frame, at one point it could have made a difference in the outcome if Robertson had played perfectly.
So, in this situation, what are the player's options when a referee makes an error like this? Is it encumbent on the player to notice the error immediately and petition for it to be corrected, or can it be corrected later? So, for example, once the total for the frame had been called, was it too late to fix it?
Answer: Whilst the onus is generally on the referee to keep score - it is also the players' responsibility to ensure that the correct score is being applied, insomuch as it's actually in the rules:

Section 4: The Players
6. Scoring Responsibility
As well as the referee, it is the players' responsibility to make sure
that the correct score is being applied, either on the scoreboard or
by the referee announcing the scores. If any player notices that the
score is incorrect, it is their responsibility to inform the referee at
the earliest opportunity.

"earliest opportunity" is a little vague, but I would imagine that the latest is the end of the current frame. I also seem to recall a player once pointing out to a referee that they had incorrectly scored their opponent's break, once that break had ended (though I can not seem to find it).


how to jump back before for loop and update variable until meet my requirments

Question: I am scraping nested text from parent category and child category.
here my loop look like:
first for loop will scrape all parent category:
      ...seond for loop will scrape child1 category of parent category
          ...third for loop will scrape child2 category of child1 category

I am trying to scrape all parent and child category from this page
If my "sub_cat_1 = y.text" None or empty string then I want to increment by 1 Level_1_Category_No{increment_by_1} here my this variable sub_category_one = driver.find_elements(By.CSS_SELECTOR , ".Level_1_Category_No1 .lzd-site-menu-sub-item > a span")  here is my full code:
    driver.get("https://www.daraz.com.bd/")
    time.sleep(10)
    main_category = driver.find_elements(By.CSS_SELECTOR , '.lzd-site-menu-root-item span')
    with open("all_category_subcat.csv", "w",encoding="utf-8",newline="") as f:
        writer = csv.writer(f)
        writer.writerow(["Main Category", "Sub Category 1", "Sub Category 2"])

        for i in main_category:
            hover = ActionChains(driver).move_to_element(i)
            hover.perform()
            main_cat = i.text
            print(main_cat)

            sub_category_one = driver.find_elements(By.CSS_SELECTOR , ".Level_1_Category_No1 .lzd-site-menu-sub-item > a span")
            for y in sub_category_one:
                hover = ActionChains(driver).move_to_element(y)
                hover.perform()
                sub_cat_1 = y.text
                print("--------------",sub_cat_1,"--------------")
                if sub_cat_1 == None sub_cat_1 == "":
                       #update the value of sub_category_one and run for loop again
                sub_category_two = driver.find_elements(By.CSS_SELECTOR , ".lzd-site-menu-grand-active span")
                for z in sub_category_two:
                            sub_cat_2 = z.text
                            print(sub_cat_2)
                            writer.writerow([main_cat, sub_cat_1, sub_cat_2])

Comment: What do you mean? What are you trying to do? It is unclear what your question is. Are you trying to only run the sub_category_two loop if the conditions of your if statement are met?
Comment: Andrew Ryan I updated my question
Comment: can you put the second for on a separate function and just call said function after updating your variable passing it as a variable to the method?  You can't jump back into your code but you can move your code elsewhere and call it repeatedly until requirements are met.
Comment: @boyenec give below a go
Answer: out = driver.execute_script('''
out = [];
els = document.querySelectorAll('.lzd-site-menu-root-item');
els.forEach(function(a) {
txt1 = a.querySelector('a').textContent.replaceAll('\n', '').replaceAll('  ', '').trim();
els2 = document.querySelector('.lzd-site-menu-sub.' + a.getAttribute('id')).children;
//Archived version / screenshot shows all results:

for(i=0;i<=els2.length-1;i++){ b = els2[i];
url_2 = b.querySelector('a').href;
txt2 = url_2.split('/')[3]
els3 = b.querySelectorAll('.lzd-site-menu-grand-item');
els3.forEach(function(c) {
url_3 = c.querySelector('a').href;
txt3 = url_3.split('/')[3]
out.push([txt1, txt2, url_2, txt3, url_3]);
});
        }
        });
return out''');
print(out)

Which gives you output like this - which is what I believe you're actually after at the end of the day?

[["Women's Fashion", 'womens-clothing', 'https://www.daraz.com.bd/womens-clothing/?price=999-&service=OS&from=filter', 'womens-dresses', 'https://www.daraz.com.bd/womens-dresses/?from=filter&price=1200-&service=OS&from=filter/'],
["Women's Fashion", 'womens-clothing', 'https://www.daraz.com.bd/womens-clothing/?price=999-&service=OS&from=filter', 'womens-jeggings', 'https://www.daraz.com.bd/womens-jeggings/?service=OS&from=filter/'],
["Women's Fashion", 'womens-clothing', 'https://www.daraz.com.bd/womens-clothing/?price=999-&service=OS&from=filter', 'womens-kurtis', 'https://www.daraz.com.bd/womens-kurtis/?from=filter&service=OS&price=1299-'],
...
...
['Health & Beauty', 'bath-body', 'https://www.daraz.com.bd/bath-body/?service=OS&from=filter', 'body-oils', 'https://www.daraz.com.bd/body-oils/?spm=a2a0e.home.cate_2_1.1.735212f73vbvt5&service=OS&from=filter'],
...
...
['Watches, Bags, Jewellery', 'school-bags', 'https://www.daraz.com.bd/school-bags/', 'kids-backpacks', 'https://www.daraz.com.bd/kids-backpacks/'],
['Watches, Bags, Jewellery', 'school-bags', 'https://www.daraz.com.bd/school-bags/', 'kids-shoulder-bags', 'https://www.daraz.com.bd/kids-shoulder-bags/'],
['Watches, Bags, Jewellery', 'school-bags', 'https://www.daraz.com.bd/school-bags/', 'school-bags-backpack', 'https://www.daraz.com.bd/school-bags-backpack/'],
....
....
['Automotive & Motorbike', 'automotive', 'https://www.daraz.com.bd/automotive/?spm=a2a0e.home.cate_12.1.735212f73vbvt5', 'motorcycle-helmets', 'https://www.daraz.com.bd/motorcycle-helmets/'],
['Automotive & Motorbike', 'automotive', 'https://www.daraz.com.bd/automotive/?spm=a2a0e.home.cate_12.1.735212f73vbvt5', 'motorcycle-tools-maintenance', 'https://www.daraz.com.bd/motorcycle-tools-maintenance/']]

notes:
(abridged version, bonus = respective urls can be discarded as required; code needs to be amended - no 'hover'/ for loops besides within the query).


jqgrid modify data returned from ajax call before display in table

Question: I have to display some data that I receive from the server as json object like this
{"rowndx":"0","rows":"25","rowstotal":"100","rowsdata":[
["00","DEVICE001","T0_IHOME","1","***","1","10"],
["01","DEVICE002","NO_DEVICE","1","***","1","10"],
["02","DEVICE003","NO_DEVICE","0","***","1","10"],
.....

Before displaying the received data in a table I would like to make changes where necessary adding units to the numbers or replacing the numbers with words (eg 0 ->OFF 1-> ON)
To do this I have associated at the ajax option "success" my encoding function. In this case, however, remains always visible the message "Loading ..." and no other action is permitted.
I moved my re-encoding procedure to the "complete" ajax option and this time it seems to work.
But I did not understand what was my mistake and I do not know if my procedure can work.
This is my table ajax configuration
    url      : "devtbl.json",
    mtype    : "POST",
    datatype : "json",
    postData : ......

    ajaxGridOptions: {
      type       : 'post',
      contentType: 'application/json',
      async      : false,
      complete   : DEVparse_serverdata,
      error      : function() { alert('Something bad happened. Stopping');},
    },

    jsonReader : {
      root        : "tablerows",
      page        : "currentpage",
      total       : "totalpages",
      records     : "totalrecords",
      cell        : "",
      id          : "0",
      userdata    : "userdata",
      repeatitems : true
    },

and my coding function
    function DEVparse_serverdata(js , textStatus) {

  var jsontablereply = {} ;
  var rowsxpage_int  = parseInt(UB.rowsxpage.DEVtable) ;
  var jsonreply =  jQuery.parseJSON(js.responseText) ;

  jsontablereply.currentpage  = "" + (1 + (parseInt(jsonreply.rowndx) / rowsxpage_int));
  jsontablereply.totalpages   = "" + parseInt((parseInt(jsonreply.rowstotal) + (rowsxpage_int-1)) / rowsxpage_int) ;
  jsontablereply.totalrecords = jsonreply.rowstotal;

  jsontablereply.tablerows = [] ;
  $.each(jsonreply.rowsdata, function(ndx, row) {
     var rowarray = [] ;

     rowarray[0] = row[0] ;
     rowarray[1] = row[1] ;
     rowarray[2] = row[2] ;
     rowarray[3] = row[3] ;
     rowarray[4] = row[4] ;

     switch (row[2]) {
       case "NO_DEVICE":
            rowarray[5] = "***" ;
            break ;

       case "T0_IHOME":
            rowarray[5] = "T=" + row[5] + "°C" ;
            break ;
     }
     jsontablereply.tablerows[ndx] = rowarray ;
  }) ; // each

  jQuery("#DEVtbl")[0].addJSONData(jsontablereply);
}

(I am a beginner with Jquery my coding style is poor)
Comment: You can use a custom formatter.
Answer: There are many possibilities which you have to implement your requirements.
In many case one can use predefined formatter: "select" or formatter: "checkbox" for the case 0 ->OFF 1-> ON.
Another possibility is the usage of custom formatter and unformatter. The custom formatter is just callback which will be used by jqGrid during building of HTML fragment of cells of the corresponding column. If you need common displaying of some text the formatter will look like
formatter: function (cellValue) { return $.jgrid.htmlEncode(cellValue); }

The advantage of custom formatter is that you can not only many any modification of the source text, but you can build the cell contain based of information from other columns (see rawData parameter below)
formatter: function (cellValue, options, rawData, action) {
    // options is the the object defined as
    //         {rowId:rowId, colModel:cm, gid:gridId, pos:colpos }
    // rawData contains the representation of the WHOLE row
    //         the most problem of the value is that it is in the same
    //         format as the input data. So if will be array of items
    //         in your case or if could be XML fragment for the row.
    //         Additional problem one will have in case of usage of
    //         loadonce:true. Ath the first load the rawData will be
    //         array of strings and on later could be named object
    //         with the properties corresponds to the column names.
    // action is typically non-important and is 'add' or 'edit'
}

For example the custom formatter of the 5-th column can be
formatter: function (cellValue, options, rawData, action) {
    switch (rawData[2]) {
    case "NO_DEVICE":
        return "***";
    case "T0_IHOME":
        return "T=" + $.jgrid.htmlEncode(cellValue) + "°C" ;
    default:
        return $.jgrid.htmlEncode(cellValue);
    }
}

One more option is the usage of beforeProcessing callback. It is mostly close tothe logic of your current code. Inside of beforeProcessing callback you can make any modifications of the data returend fron the server before the data will be processed by jqGrid.


Print node body removing html entities?

Question: Im printing the node body in my tpl file with this:
<code> <?php print render(field_view_field("node", $node, "body", array(
  "label"=>"hidden",
  "type" => "text_summary_or_trimmed",
  "settings"=>array("trim_length" => 150),
))); ?>
</code>
The " and ' characters in the node's content are breaking the page as im putting this in a mailto link. How can I make sure these characters are printed as non html entities eg <code>"</code> instead of ".
Answer: You could wrap it in PHP's htmlspecialchars and strip_tags functions:
<code>$body_trimmed = render(field_view_field("node", $node, "body", array(
  "label"=>"hidden",
  "type" => "text_summary_or_trimmed",
  "settings"=>array("trim_length" => 150),
)));
print htmlspecialchars(strip_tags($body_trimmed));
</code>
This would give you plain text with special characters encoded.
Alternately, if all you want to do is escape the quote characters and leave the divs in place, you could do a search/replace using str_replace for each, adding the backslash to escape them:
<code>$body_trimmed = render(field_view_field("node", $node, "body", array(
  "label"=>"hidden",
  "type" => "text_summary_or_trimmed",
  "settings"=>array("trim_length" => 150),
)));
$body_trimmed = str_replace("'","\'",$body_trimmed);
$body_trimmed = str_replace('"','\"',$body_trimmed);
print $body_trimmed;
</code>
Comment: That outputs all the divs as plain text.
Comment: How do you want the divs to render if you are using this in a mailto link? Can you give an example of how you are using it?
Comment: As its going to be in the body of an email I want plain text. I need to escape the ' and " characters as they break the link.
Comment: See if the edits above help in your situation.
Comment: Great's is converted to Great\'s. So the ' character is still breaking the link.
Comment: In addition to this, I need the character that the ' and " get converted to to be readable when in the email's body.
Comment: This site achieves what I need to: http://time.com/3524384/ebola-thomas-eric-duncan-contacts-containment/
Comment: Did you try the revised first option above? Give me a before/after example (what you have and what you want it to end up like) and I will see if I can revise again to achieve it.
Comment: Yes I tried your latest code. Before I had Great's which is not converted to Great\'s. What I need is Great's
Comment: If you leave aside the "Alternately" section of my answer, the first part was also revised. Did you try that with the "strip_tags" function included?
Comment: Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/18015/discussion-between-laryn-and-jdln).
Comment: Sorry I was using the 2nd code. Its working now with the first load. Thanks a lot

Print node body removing html entities?


winmain() function / c++

Question: can you please explain to me the significance of this statement generally written at the end of the WinMain() function:
   return (int) msg.wParam;

Im used to ending my console applications with return 0; is it wrong for windows applications?
Thank you.
Comment: Yes, you want to return the value that was passed to PostQuitMessage().
Comment: ok, but why is there `(int)`? is it some kind of casting wParam to integer?
Comment: or making it clear to the compiler?
Comment: wParam is not an int, cast required.
Answer: The GetMessage() loop stops after receiving a WM_QUIT window message which is normally issued after a call to the PostQuitMessage() function, which describes its nExitCode parameter as the application exit code.
If you want to respect the intent of the person who invoked the PostQuitMessage() function, you should return that value from main.  This is what you are doing when you return the wParam retrieved from the last invocation of GetMessage().
Answer: From the WinMain() docs (http://msdn.microsoft.com/en-us/library/windows/desktop/ms633559.aspx):

Terminate the message loop when it receives a WM_QUIT message. At that
  point, your WinMain should exit the application, returning the value
  passed in the WM_QUIT message's wParam parameter. If WM_QUIT was
  received as a result of calling PostQuitMessage, the value of wParam
  is the value of the PostQuitMessage function's nExitCode parameter.
Answer: From the first hit of a Google search

Your WinMain should initialize the application, display its main
  window, and enter a message retrieval-and-dispatch loop that is the
  top-level control structure for the remainder of the application's
  execution. Terminate the message loop when it receives a WM_QUIT
  message. At that point, your WinMain should exit the application,
  returning the value passed in the WM_QUIT message's wParam parameter.
  If WM_QUIT was received as a result of calling PostQuitMessage, the
  value of wParam is the value of the PostQuitMessage function's
  nExitCode parameter. For more information, see Creating a Message
  Loop.

winmain() function / c++


Mock Router and Ngzone in Angular 7 Jest Unit test

Question: I used ngzone and router in component class.
My unit testing was working fine but now getting error cannot read toLowerCase() of undefined.
Can anyone suggest me how to mock both Ngzone and Router.
Answer: I use TestBed for this :)
Just configure your test module with your component and routes, then get your Router instance from the test module, and then you spy on Router.navigate to use your custom mock implementation.

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { Router } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';

let component: YourComponent;
let fixture: ComponentFixture<YourComponent>;
let router: Router;

beforeEach(async(() => {
  TestBed.configureTestingModule({
    imports: [
      RouterTestingModule.withRoutes([
        { path: "", component: YourComponent },
        { path: "**", redirectTo: "" },
      ]),
    ],
    declarations: [YourComponent],
    providers: [],
  }).compileComponents();
}));

beforeEach(() => {
    fixture = TestBed.createComponent(YourComponent);
    component = fixture.componentInstance;
    router = TestBed.inject(Router);
    jest
      .spyOn(router, 'navigate')
      .mockImplementation(() => of(true).toPromise());
  });

Comment: I can't stress enough how important the `mockImplementation` is.


How to install sales forecast with full functionality for Odoo/OpenERP 8?

Question: I have tried installing the sale_forecast module from external odoo link. However, I am not able to get the full functionality. When I open the module, I can't see the target data, goal data, achieved data, etc.
Answer: What is your Odoo log file saying?
Usually when I am having troubles, the output from the log file puts me in the right direction.
Also, did you install the module? Because you say that you tried installing and not that you succeeded. Perhaps you tried to download a module that is not compatible with your Odoo version. What Odoo version are you running?
Why don't you try this? Navigate to your Odoo addons directory and download this Odoo 8 module. Then, log into your Odoo backend and from Local Modules/Addons click on Refresh Module List (i think the option was named like this, but I might be wrong). Then locate the newly added module and install it.
On another note, this could be a dependency error from which the module cannot be installed. Did you contact your host?
Usually, when I'm installing a module and encounter a problem, I am checking the log files and if I cannot solve the issue, I contact my host. Thank god they are always helpful and patient with me. :)
This is just me guessing what could be the issue, since you didn't provide many details about your problem, but in most cases inspect your Odoo log file and use your host support to narrow down the possible solutions.
Comment: Thank you, for the time that you have spent for this, I am using odoo v8.. my odoo logs doesn't give me any errors, the module which you gave is installed, but in previous version, they had fields like target, goal..etc in sale_forecast.. Now it has been removed in odoo8, if there is any substitute modules available, please suggest.. Or suggestion on adding custom field for a module is also appreciative.


mysqli Fatal error: Call to a member function bind_param() on a non-object

Question: I have a problem with a prepared statement, here is my code:
<code>function query_array($table, $data) {
    foreach ($data as $column => $value) {
        $columns[] = sprintf("`%s` = '%s'", $column, $this->db->real_escape_string($value));
    }
    $column_list = join(',', $columns);

    // Prepare the statement
    $stmt = $this->db->prepare("UPDATE `?` SET ?");
    $stmt->bind_param('ss', $table, $column_list);

    // Execute the statement
    $stmt->execute();

    // Save the affected rows
    $affected = $stmt->affected_rows;

    // Close the statement
    $stmt->close();

    // ...
}
</code>
$this->db returns an object;
$table = 'settings'; (string)
$column_list: (string)
<code>`title` = 'Socialsd',`captcha` = '0',`public` = '',`private` = '',`time` = '1',`perpage` = '10',`message` = '140',`mail` = '1',`inter` = '10000',`size` = '1048576',`format` = 'png,jpg,gif',`sizeMsg` = '1048576',`formatMsg` = 'png,jpg,gif,bmp',`censor` = '',`ad1` = '',`ad2` = ''
</code>
The error I'm getting is:

Fatal error: Uncaught exception 'ErrorException' with message 'You
  have an error in your SQL syntax; check the manual that corresponds to
  your MySQL server version for the right syntax to use near '?' at line
  1' in C:\xampp\htdocs\new\includes\classes.php:256 Stack trace: #0
  C:\xampp\htdocs\new\sources\admin.php(225):
  updateSettings->query_array('settings', Array) #1
  C:\xampp\htdocs\new\index.php(42): PageMain() #2 {main} thrown in
  C:\xampp\htdocs\new\includes\classes.php on line 256

I can't figure out what causes this, because trying the following works just fine:
<code>$query = sprintf("UPDATE `%s` SET %s", $table, $column_list);
$result = $this->db->query($query);
</code>
Any help is appreciated.
Update 1: May I know why this has been down-voted? It would be nice to know.
Update 2: So I've removed the last bind ($column_list) and put in the statement the entire output of $column_list, so basically I was binding only the table name, and now I get another error:

Can't find file: '.\diary\@003f.frm' (errno: 22)

Now I'm really confused.
Comment: Possible duplicate of [mysqli :: prepare SQL error](http://stackoverflow.com/q/11559186/1409082).
Comment: @Jocelyn Thanks for the comment, even if I manually set the table name to `settings` the error still persist, the exact same thing.
Answer: I have found the answer here: Use one bind_param() with variable number of input vars and also as @Jocelyn linked me, I've found that table names can't be binded. Can be closed.


Postgresql 9.6 writing data from remote oracle db is slow

Question: Im using oracle_fdw extension in my postgresql database. I'm trying to copy the data of many tables in the oracle database into my postgresql tables. I'm doing so by running insert into local_postgresql_temp select * from remote_oracle_table. The performance of this operation are very slow and I tried to check the reason for that and mybe choose a different alternative.
1)First method - Insert into local_postgresql_table select * from remote_oracle_table this generated total disk write of 7 M/s and actual disk write of 4 M/s(iotop). For 32G table it took me 2 hours and 30 minutes.
2)second method - copy (select * from oracle_remote_table) to /tmp/dump generates total disk write of 4 M/s and actuval disk write of 100 K/s. The copy utility suppose to be very fast but it seems very slow.
-When I run copy from the local dump, the reading is very fast 300 M/s.
-I created a 32G file on the oracle server and used scp to copy it and it took me a few minutes.
-The wals directory is located on a different file system.
The parameters I assigned :
min_parallel_relation_size = 200MB
max_parallel_workers_per_gather = 5
max_worker_processes = 8
effective_cache_size = 12GB
work_mem = 128MB
maintenance_work_mem = 4GB
shared_buffers = 2000MB
RAM : 16G
CPU CORES : 8

HOW can I increase the writes ? How can I get the data faster from the oracle database to my postgresql database?
I run perf on this entire process and the results :
Comment: I cannot be sure, but if what you posted contains everything significant, it seems like the time is spent either on the Oracle server or on the network. Since playing with prefetch did not change anything, I suspect that the problem is outside of oracle_fdw. Tuning Oracle is outside of my competence.
Comment: With `psql` you cannot use single row mode. But we have veered far from the question anyway.
Comment: Is there a LOB column in the Oracle table?
Comment: No, I dont have any lob column.
Comment: If the `SELECT` is slow, try to play with the `prefetch` option of the foreign table.
Comment: The select runs inside a begin end block. How exectly you advice to use prefetch ? Can you please provide an example ?
Comment: For example `ALTER foreign_table OPTIONS (ADD prefetch '1000');`
Comment: I should do it for every table which has alot of rows ?
Comment: Not necessarily. Just try if different values make a difference in `SELECT` speed. This setting has nothing to do with the number of rows in the table. To find out where the time is spent, use profiling (`perf` or `oprofile` on Linux).
Comment: Laurenz, the prefetch command isnt working..
Comment: Specify what you mean by *isn't working*.
Comment: I tried it again and the command worked. However, I tried it on a foreign table that is 1.5G and it didnt change it dramaticly. With the default settings it took 8 minutes and 35 seconds and after I set the prefetch to 1000,5000,10000 it took 8.59/8.54/8.33 minutes.
Comment: Then prefetching has no impact on your problem. Try profiling PostgreSQL to see where the time is spent.
Comment: i add the perf result to the main comment
Comment: Is the prefetch option is also available for local postgresql tables ? I mean when I select a local table i can get the data faster when I increase the prefetch ?
Comment: With local tables, data are being processed as they become available, row by row. If you are using the C client API, you can tell the PostgreSQL *client* to either retrieve all rows and buffer them in memory or use single row processing.
Comment: How can I check which api I use ? Which of those options is better And how  can I set one of them ?
Comment: If you don't know which API you use, you probably didn't write the program that accesses PostgreSQL, and you couldn't switch to single row more since that requires rewriting the code. The traditional way of fetching all rows at once is better unless there is a vast amount of data or you need to stream for some other reason.
Comment: I didnt write the program. This entire system if about 100 functions in the database. I just run the main function from shell wih psql -d mydb -c "select start_main()". So I dont use any api, just the psql
Answer: I'd concentrate on the bits you said were fast.

-I created a 32G file on the oracle server and used scp to copy it and it took me a few minutes.
-When I run copy from the local dump, the reading is very fast 300 M/s.

I'd suggest you combine these two.  Use a dump tool (or SQLPLUS) to export the data from Oracle into a file that Postgresql's COPY command can read.   You could generate the binary file format directly but its a bit tricky, but generating a CSV separated version etc, shouldn't be too tricky.  An example for that is at How do I spool to a CSV formatted file using SQLPLUS?
Comment: I cant do that because the import of the data is part of a big postgresql transaction..


Price mechanism question (economics)

Question: There are two goods in the market $X1$ and $X2$ and both of their prices are positive. If an agent's utility function is given as: Q1 = amount of X1 agent buys, Q2 = amount of X2 agent buys, $u = Q1 + |Q2-5|$ Identify the demand of this agent.
Note: agent has to spend his initial endowment.
Comment: @RossMillikan ops, you are right.
Answer: Clearly it depend on the prices, say $P1$ and $P2$, and on the agent's endowment, say $m$.
There is no point in demanding $Q2 \gt 5$: utility would be increased reducing $Q2$ to $5$ and spending the saved amount on $X1$.
For small endowments the agent will maximise utility by spending on the good with the lower price.  Indeed if $P1 \lt P2$ it will always be better to buy more of $X1$ and none of $X2$.  And similarly if $P1 \gt P2$ it will be better to buy more of $X2$ and none of $X1$ until $Q2=5$.  So

if $P1 \lt P2$, then demand is $Q1=m/P1$ of $X1$ and $Q2=0$ of $X2$
if $P1 \gt P2$ and $m/P2 \le 5$, then demand is $Q1=0$ of $X1$ and $Q2=m/P2$ of $X2$
if $P1 \gt P2$ and $m/P2 \ge 5$, then demand is $Q1=(m-5\times P2)/P1$ of $X1$ and $Q2=5$ of $X2$

If $P1=P2$ then there are several solutions which are convex combinations of those bullet points.

Added much later
Rereading this in the light of Amit's perceptive comment, I should have said something like:
If $P1 \le P2$  it will always be better to buy more of $X1$ and none of $X2$, and this will also be the case with small endowments and $P1 \gt P2$: in either case you get a utility of $Q1+5$.
But for $P1 \gt P2$ and a large endowment $m$ the opposite is true and you can get a higher utility of $Q2-5$ when $m/P2 - 5 \gt m/P1 +5$, i.e. when $m \gt \frac{10\,P1\, P2}{P1-P2}$. So

if $P1 \le P2$ or $m \lt \dfrac{10 \,P1\, P2}{P1-P2}$, then demand is $Q1=m/P1$ of $X1$ and $Q2=0$ of $X2$
if $P1 \gt P2$ and $m \lt \dfrac{10 \,P1\, P2}{P1-P2}$, then demand is $Q1=0$ of $X1$ and $Q2=m/P2$ of $X2$
if $P1 \gt P2$ and $m = \dfrac{10\,P1\, P2}{P1-P2}$ then either all $X1$ and no $X2$ or no $X1$ and all $X2$ are both optimal
Comment: I think there is a mistake. When $p_1 = 2$, $p_2 = 1$ and $m = 6$, according to the solution you proposed the demand is $(Q_1, Q_2) = (0.5, 5)$ which gives him utility = 0.5. He can do better by spending all his money on $X_1$ i.e. at $(Q_1, Q_2) = (3, 0)$ his utility is 6.
Comment: I think you answered the problem for $u =  Q_1 - |Q_2-5|$.
Comment: @Amit: I suspect you are correct.  I have added a correction to my answer
Answer: Let $p_1$, $p_2$ denotes the prices of $X_1$, $X_2$ respectively, and $m$ denotes the income of the consumer. Utility function of the consumer is given: $u(Q_1, Q_2) = Q_1 + |Q_2-5|$. Demand is the solution to the following problem:
\begin{eqnarray*} \max_{Q_1, Q_2} & & Q_1 + |Q_2-5| \\ \text{s.t.} && p_1Q_1 + p_2Q_2 = m \\ && Q_1 \geq 0, Q_2 \geq 0 \end{eqnarray*}
and it will be:
\begin{eqnarray*} (Q_1^d, Q_2^d)(p_1, p_2, m) = \begin{cases} \displaystyle\left(\frac{m}{p_1}, 0\right) && \text{ if }  p_1 \leq p_2 \\ \displaystyle\left(\frac{m}{p_1}, 0\right) && \text{ if }  p_1 > p_2 \text{ and } \displaystyle\left(\frac{m}{p_2} - \frac{m}{p_1}\right) \leq 10 \\ \displaystyle\left(0,\frac{m}{p_2}\right) && \text{ if }  p_1 > p_2 \text{ and } \displaystyle\left(\frac{m}{p_2} - \frac{m}{p_1}\right) \geq 10 \end{cases} \end{eqnarray*}
Answer: The consumer will simply compare between the utilities $\frac {M}{p_1} + 5$ and $\frac {M}{p_2}-5$
Now, $\frac {M}{p_1}+5 \gt \frac {M}{p_2} - 5 \implies \frac {M}{p_2}-\frac {M}{p_1}\lt 10$

Price mechanism question (economics)


Selenium Webdriver python XPath not working

Question: I am trying to automate a process using Python and selenium webdriver.
I am able to login successfully and navigating to the page where I want to post something but for some reason the xpath is not recognized by the system.
It gives the below error:

NoSuchElementException: Unable to locate element: //*[@id="widecol"]/div/form/p[1]/input

Here is my code:
from selenium import webdriver
mydriver = webdriver.Firefox()
mydriver.get("https://www.phishtank.com/")
mydriver.maximize_window()
mydriver.find_element_by_xpath('//*[@id="username"]').send_keys("myusername")
mydriver.find_element_by_xpath('//*[@id="password"]').send_keys("mypassword")
mydriver.find_element_by_xpath('//*[@id="header"]/div[2]/form/input[3]').click()
mydriver.find_element_by_xpath('//*[@id="nav"]/ul/li[2]/a').click()
mydriver.find_element_by_xpath('//*[@id="widecol"]/div/form/p[1]/input').send_keys("sample text testing to fill form")

This is my HTML code

    <form method="POST">

Add A Phish

Visit our What is phishing? page to confirm that the suspected phish meets all of the criteria.

Add a phish using the form below, or even better, submit a phish directly via email.

Phish URL:

            <input type="text" name="phish_url" style="width:90%;" value="" />

            Copy and paste the URL of the phishing website.

What is the organization referenced in the email?

        <select name="phish_target">

which gives me the following error:

NoSuchElementException: Unable to locate element: //*[@id="widecol"]/div/form/p[1]/input

Here is the HTML code:
<input type="text" name="phish_url" style="width:90%;" value=""> outer HTML code

And this is my XPath:
//*[@id="widecol"]/div/form/p[1]/input - Xpath

Please let me know where to look, thank you.
Answer: You need to induce WebDriverWait for the element to be clickable and you can use the following line of code :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='phish_url' and @type='text']"))).send_keys("sample text testing to fill form")

Comment: Perfect, it works. Thanks a lot. Sorry, I forgot to delete the older xpath line, it's a great help.
Comment: Hi thank you for your help. I tried ruining the above line of code but it still give the same error : NoSuchElementException: Unable to locate element: //*[@id='widecol']//input[@name='phish_url']
Comment: @Kunal I am sure you haven't tried out my code as my solution doesn't contains `@id='widecol'`. Try out exactly my code and let me know the status.
Answer: Could you try the following instead?
//*[@id='widecol']//input[@name='phish_url']

Should work for that input. Just for trial/error sake, try a small wait before doing this.
Comment: Hi, thanks. Itried running this but it gives the same error: NoSuchElementException: Unable to locate element: //*[@id='widecol']//input[@name='phish_url']
Answer: The new Selenium version doesn't allow it.
Try the below code
from selenium.webdriver.common.by import By

And then Change the 'find_element_by_xpath statements' to
mydriver.find_element(By.XPATH,'//[@id="username"]').send_keys("myusername")

You can refer to the link to know more
Comment: You can also go to Chrome / Firefox and other browser and copy the `xpath` of an object and use it in Selenium as above. Hope that helps! https://stackoverflow.com/questions/3030487/is-there-a-way-to-get-the-xpath-in-google-chrome
Answer: Without the full HTML, we can't tell if the XPath is correct or no.
Can you please try and include more outer Html code?
You can try and wait after clicking on that last URL, maybe the element wasn't loaded yet.
You can also check if the input is in an iframe, that might be causing the problem.
Comment: Sure, thanks.  I have added more Html code. The more HTML source code can also be found at https://www.phishtank.com/add_web_phish.php.


Given $w=e^{2i\pi/7}$, perform algebraic manipulations with complex numbers like $w+w^2+w^4$

Question: It is not my own homework and I forgot how to solve this kind of things. Anyway, the following are the statement of the homework and my attempts:
The homework:
Let $w=e^{2i\pi/7}$, $u=w+w^2+w^4$, and $v=w^3+w^5+w^6$.
1) Calculate $u+v$ and then write $u^2$ in function of $u$.
2) Prove that $\mathrm{Im}(u)>0$.
3) Calculate the sum: $\sin(2\pi/7)+\sin(4\pi/7)+\sin(6\pi/7)$.
My attempts:
1) $u+v=w+w^2+w^4+w^3+w^5+w^6=w(1+w+w^2+w^3+w^4+w^5)$.
Hence, $u+v=w\dfrac{1-w^6}{1-w}=\dfrac{w-w^7}{1-w}=\dfrac{w-1}{1-w}=-1$.
Since $u+v=-1$ then $u^2+uv=-u$, so let's calculate $uv$.
$uv=(w+w^2+w^4)(w^3+w^5+w^6)=2w^7+w^4(1+w+w^2+w^3+w^4+w^5)$.
Then, $uv=2+w^3\dfrac{w-w^7}{1-w}=2-w^3$.
Hence, $u^2=-u-2+w^3$.
I do not know how to prove that $\mathrm{Im}(u)>0$.
Comment: It was downvoted because it hadn't shown any attempt, I reckon. You could have edited the old question, adding your attempt, instead of creating a new question. I think this one will be better received by people here.
Comment: Hint: $2i\text{Im}(z) = z - \overline{z}$.
Answer: Yeah there are alot of jerks on this site paranoid about doing other peoples homework. Thats right I am talking about you..
So if you care more about rules and institutions and all that garbage rather than mathematics please signal by entering a $-1$.
But if you care about math read on...
Anyway $w^7=1$ and if you factor out $w-1$ you get
$$w^6+w^5+w^4+w^3+w^2+w+1=0$$
And if you look you see this is the same as
$u+v+1=0$ so $u+v=-1$ is the first problem.
For the second
$$u^2=w+w^4+w^2+2w^5+2 w^3 +2 w^6=u+2v=-2-u.$$
The imaginary part of $u$ is
$$\sin\frac{2\pi}{7}+\sin\frac{4\pi}{7}
+\sin\frac{6\pi}{7}$$
and since all these terms are positive the imaginary part is positive.
Finally to find the imaginary part of $u$ and we also have $uv=2$ as can be checked.
So $u$ and $v$ are solutions to
$$y^2+y+2=0$$ and this means that
$$y=\frac{-1\pm \sqrt{-7}}{2}$$ and from the positivity, we have
$$u=\frac{-1 + \sqrt{-7}}{2}$$
so the trig sum is equal to $\frac{\sqrt{7}}{2}$
For those that think this is a trivial homework problem this is an example of Gauss periods, used in  solving cyclotomic equations and Gauss's fourth proof of quadratic reciprocity.
Now are you not glad you read this far, knowledge is far better than hate.


bash intercepting error and stop in pipeline

Question: I've been trying different things without success
so here is what I want to achieve
set -o pipefail
dump "$@" "$db" | compress | store "single" "$(backupName "$db")"

# I would want something that behaves a bit like this
# meaning if it dump fails, don't store
dump "$@" "$db" && {
  #migicGetStdout | compress | store "single" "$(backupName "$db")"
} || {
  echo failed
}

But it creates a empty file on failed dump
I'm lost with pipeline
I've tried things like
set -e
set -o pipefail

dump "${dumpCommonArgs[@]}" "${dumpDbArgs[@]}" "$@" "$db" > >(compress | store "single" "$(backupName "$db")")

# or

( compress | store "single" "$(backupName "$db")" ) < <(dump "$@" "$db") || return 2

# or

## this way compress get the global $@ ... I don't understand that either
store "single" "$(backupName "$db")" < <(dump "${dumpCommonArgs[@]}" "${dumpDbArgs[@]}" "$@" "$db") > >(compress)

# there would be an easy one
dataToStore=$(dump "$@" "$db")
rc=$?
# but this means dump is stored in memory before saving... not the best deal as mysql already needs a lot of ram to run a dump

store function is still called!
So seems I'm missing something.
Thanks for helping me out
Comment: It's also possible to make sure `dump` _starts_ dumping data and only start the other parts if it writes at least one line; but if that's what you want, please update the question to make it clear.
Comment: All parts of a pipeline _run at the same time_. So you can't conditionally start later ones depending on how the prior ones went: they _all_ get started simultaneously.
Comment: Of course, you can do what you're asking for if you dump to a file instead of to stdout.
Comment: @CharlesDuffy looks like someone already suggested this test of 1 line min... Any solution is good as long as it doesn't store all the dump in ram before starting to write.
Answer: You'll have to store the output of dump temporarily; example with a variable:
if out=$(dump "$@" "$db")
then
    printf '%s\n' "$out" |
    compress |
    store "single" "$(backupName "$db")"
else
    echo failed 1>&2
    exit 1
fi

or test for the emptiness of stdout (is there's no output when it fails):
dump "$@" "$db" | {
    IFS='' read -r line

    [ -n "${line:+_}" ] || { echo failed 1>&2; exit 1; }

    { printf '%s\n' "$line"; cat; } |
    compress |
    store "single" "$(backupName "$db")"
}

Comment: @AntonyGibbs the 2nd solution only reads the first line in a variable, then decide to `exit` or `cat` depending on its value
Comment: thanks for your reply. Your first script is the simple solution `out="$(dump ...)"` but it'll store all dump in ram first. How about second the second, does it dump line per line as it comes in? or does the process wait for all content to pile up in `$lines` before passing to stdout? If not, could it be improved to do so.  If it's too complicated then I'll just delete the stored file `if $? != 0` but I wanted to avoid that kind of 'hack'
Comment: I finally got the meaning of `printf '%s\n' "$line"; cat;`  ... @Fravadiba thank you :)
Answer: based on @Fravadona answer I end up writing a more generic function and thought it would be nice to share it too.
could be saved as a on-empty-return in /usr/local/bin
not forget sudo chmod +x /usr/local/bin/on-empty-return
then could be used anywhere :)
#!/usr/bin/env bash

set -u
set -o pipefail

##
# usage
# echo "" | on-empty-return 2 cat > /tmp/should-not-create-file || {
#    >&2 echo "test failed, file is untouched (status $?)";
# }
#
# outputs to stderr
# Error: stdin is empty. (on-empty-return)
# test failed, file untouched not touched (status 2)
#
# cmd1 | on-empty-return 2 cmd2 args
# to output direct to file use cat
# cmd1 | on-empty-return 2 cat > 'file'
#
# if cmd1 output is empty, cmd2 won't be executed and will return code 2
# otherwise it'll return status code of cmd2 (assuming we have 'set -o pipefail')
#
# arg1: returned status on empty - optional default: 1
onEmptyReturn() {
  local emptyRc=1
  local line
  local re='^[0-9]+$'
  [[ ${1-} =~ $re ]] && { emptyRc=$1; shift; }

  IFS='' read -r line

  [ -n "${line:+_}" ] || { >&2 echo "Error: stdin is empty. (${0##*/})"; return $emptyRc; }

  { printf '%s\n' "$line"; cat; } | "$@"

  return $?
}

onEmptyReturn "$@"

exit $?

Snippet on GitLab
Edit: replace $(basename "$0") with ${0##*/} and $line as local thanks to @CharlesDuffy
Comment: `$?` is the default exit status. Just `exit` with no arguments is identical, but so is the script ending on its own; the same is true for `return`.
Comment: BTW, to handle the case where there _is_ more than one line of output but the first line is empty, think about testing `read`'s exit status instead of testing `line`. Since `read` will return nonzero if the delimiter isn't found, and you aren't using `-d`, this will effectively detect whether there was a newline found in output.
Comment: @CharlesDuffy removing `return $emptyRc` would not return status of `echo`?
Comment: I was referring to `return $?` at the very end. `return $emptyRc` of course makes sense.


C++: Exceptions from inside a constructor: Yes or No?

Question: On the internet I have found advice such as:

"Don't throw an exception from inside a constructor!"
and
"If constructing the object, throw an exception from inside the constructor!!"

They contradict, can both be correct?
Take, for example, a class which opens a file, reads the data and closes the file, all within the constructor.  It may encounter an error condition while opening or reading from the file.  How should this error be handled?
What advice should I follow?  Is throwing an exception from a constructor possible and is it good practice?
How should errors within constructors be handled if exceptions are not a good idea?
(I am using GCC C++14 on Linux)
Comment: Exception was actually invented as the only mechanism to get into error state from the constructor. In any case there are al lot of disadvantages for throwing errors from constructor, and  most important - once you've done it you can no longer off exception by compiler option. Excretions on gives you not really binary from the performance point of view.  You can actually use approach with not-throwing constructor and std::error_code and factory method pattern instead of throwing exception directly from constructor. It gives you ability to use and avoid exceptions whenever you want it or not.
Comment: If the constructor could leave the class in an invalid state (because of errors during construction) then the best thing to do is to throw an exception.
Comment: can you give references for the advices you found? "Don't throw an exception from inside a constructor!" in this generality is a really poor advice
Comment: The answer is "it depends". You do not want a constructor to fail. So: 1 do not write constructors which can fail, 2 I repeat: don't, 3 if a constructor does fail throw an exception, 4 any exception from a constructor should be fatal. This is my opinion (to be as helpful as possible for you personally). Now I will close vote the question for being opinionated, to be as helpful as possible to the goals of this community. You can get me to reopen-vote, by providing enough detail to allow a definite answer. (So "needs details" can be seen as secondy close vote reason.)
Comment: As you want to read data from a file, I would recommend you to write a `init`-method besides the constructor. Then you don't have trouble with exceptions whilst constructing an object.
Comment: Maybe you switched constructor with destructor? Never throw in a destructor is a mantra, but you are allowed to throw in constructor if you can't put the object in a coherent state
Comment: Throwing exceptions in a constructor is not a bad practice. In fact, it is the only reasonable way for a constructor to indicate that there is a problem; e.g. parameters are invalid
Comment: Not the exact wording but...https://softwareengineering.stackexchange.com/questions/411117/throwing-exception-from-constructor
Comment: With the factory pattern I will delegate the "can't read file" problem into another class. "Always instanziate useable objects" vs. Don't use exception in a constructor. The init method leads to an object which is "half useable" if forgotten to be called...
Answer: Good advice:

Exceptions should not be allowed to escape from a destructor.

This may have morphed into bad advice:

Don't throw an exception from inside a constructor!

Exceptions are permitted within constructors.  If construction fails, throwing an exception ensures that the object is not constructed, eliminating the possibility of using an object in an invalid state.
Another approach (to avoid) is to move the error prone tasks into an init() function and have the constructor do nothing that can fail.  This may lead to use of objects in invalid states and may pollute all methods with checks to ensure init() has been called.
Comment: Another possibility may be to wrap the constructor into a factory function, that does all error-prone tasks before and after the constructor. In that way one avoids using an object in invalid state.


UnpicklingError: A load persistent id instruction was encountered, but no persistent_load function was specified

Question: I was trying to run a python file named api.py. In this file, I'm loading the pickle file of the Deep Learning model that was built and trained using PyTorch.
api.py
In api.py the below-given functions are the most important ones.
def load_model_weights(model_architecture, weights_path):
  if os.path.isfile(weights_path):
      cherrypy.log("CHERRYPYLOG Loading model from: {}".format(weights_path))
      model_architecture.load_state_dict(torch.load(weights_path))
  else:
      raise ValueError("Path not found {}".format(weights_path))

def load_recommender(vector_dim, hidden, activation, dropout, weights_path):

    rencoder_api = model.AutoEncoder(layer_sizes=[vector_dim] + [int(l) for l in hidden.split(',')],
                               nl_type=activation,
                               is_constrained=False,
                               dp_drop_prob=dropout,
                               last_layer_activations=False)
    load_model_weights(rencoder_api, weights_path)
    rencoder_api.eval()
    rencoder_api = rencoder_api.cuda()
    return rencoder_api

The directory structure
MP1
 ┣ .ipynb_checkpoints
 ┃ ┗ RS_netflix3months_100epochs_64,128,128-checkpoint.ipynb
 ┣ data
 ┃ ┣ AutoEncoder.png
 ┃ ┣ collaborative_filtering.gif
 ┃ ┣ movie_titles.txt
 ┃ ┗ shut_up.gif
 ┣ DeepRecommender
 ┃ ┣ data_utils
 ┃ ┃ ┣ movielens_data_convert.py
 ┃ ┃ ┗ netflix_data_convert.py
 ┃ ┣ reco_encoder
 ┃ ┃ ┣ data
 ┃ ┃ ┃ ┣ __pycache__
 ┃ ┃ ┃ ┃ ┣ input_layer.cpython-37.pyc
 ┃ ┃ ┃ ┃ ┣ input_layer_api.cpython-37.pyc
 ┃ ┃ ┃ ┃ ┗ __init__.cpython-37.pyc
 ┃ ┃ ┃ ┣ input_layer.py
 ┃ ┃ ┃ ┣ input_layer_api.py
 ┃ ┃ ┃ ┗ __init__.py
 ┃ ┃ ┣ model
 ┃ ┃ ┃ ┣ __pycache__
 ┃ ┃ ┃ ┃ ┣ model.cpython-37.pyc
 ┃ ┃ ┃ ┃ ┗ __init__.cpython-37.pyc
 ┃ ┃ ┃ ┣ model.py
 ┃ ┃ ┃ ┗ __init__.py
 ┃ ┃ ┣ __pycache__
 ┃ ┃ ┃ ┗ __init__.cpython-37.pyc
 ┃ ┃ ┗ __init__.py
 ┃ ┣ __pycache__
 ┃ ┃ ┗ __init__.cpython-37.pyc
 ┃ ┣ compute_RMSE.py
 ┃ ┣ infer.py
 ┃ ┣ run.py
 ┃ ┗ __init__.py
 ┣ model_save
 ┃ ┣ model.epoch_99
 ┃ ┃ ┗ archive
 ┃ ┃ ┃ ┣ data
 ┃ ┃ ┃ ┃ ┣ 92901648
 ┃ ┃ ┃ ┃ ┣ 92901728
 ┃ ┃ ┃ ┃ ┣ 92901808
 ┃ ┃ ┃ ┃ ┣ 92901888
 ┃ ┃ ┃ ┃ ┣ 92901968
 ┃ ┃ ┃ ┃ ┣ 92902048
 ┃ ┃ ┃ ┃ ┣ 92902128
 ┃ ┃ ┃ ┃ ┣ 92902208
 ┃ ┃ ┃ ┃ ┣ 92902288
 ┃ ┃ ┃ ┃ ┣ 92902368
 ┃ ┃ ┃ ┃ ┣ 92902448
 ┃ ┃ ┃ ┃ ┗ 92902608
 ┃ ┃ ┃ ┣ data.pkl
 ┃ ┃ ┃ ┗ version
 ┃ ┣ model.epoch_99.zip
 ┃ ┗ model.onnx
 ┣ Netflix
 ┃ ┣ N1Y_TEST
 ┃ ┃ ┗ n1y.test.txt
 ┃ ┣ N1Y_TRAIN
 ┃ ┃ ┗ n1y.train.txt
 ┃ ┣ N1Y_VALID
 ┃ ┃ ┗ n1y.valid.txt
 ┃ ┣ N3M_TEST
 ┃ ┃ ┗ n3m.test.txt
 ┃ ┣ N3M_TRAIN
 ┃ ┃ ┗ n3m.train.txt
 ┃ ┣ N3M_VALID
 ┃ ┃ ┗ n3m.valid.txt
 ┃ ┣ N6M_TEST
 ┃ ┃ ┗ n6m.test.txt
 ┃ ┣ N6M_TRAIN
 ┃ ┃ ┗ n6m.train.txt
 ┃ ┣ N6M_VALID
 ┃ ┃ ┗ n6m.valid.txt
 ┃ ┣ NF_TEST
 ┃ ┃ ┗ nf.test.txt
 ┃ ┣ NF_TRAIN
 ┃ ┃ ┗ nf.train.txt
 ┃ ┗ NF_VALID
 ┃ ┃ ┗ nf.valid.txt
 ┣ test
 ┃ ┣ testData_iRec
 ┃ ┃ ┣ .part-00199-f683aa3b-8840-4835-b8bc-a8d1eaa11c78.txt.crc
 ┃ ┃ ┣ part-00000-f683aa3b-8840-4835-b8bc-a8d1eaa11c78.txt
 ┃ ┃ ┣ part-00003-f683aa3b-8840-4835-b8bc-a8d1eaa11c78.txt
 ┃ ┃ ┗ _SUCCESS
 ┃ ┣ testData_uRec
 ┃ ┃ ┣ .part-00000-4a844096-8dd9-425e-9d9d-bd9062cc6940.txt.crc
 ┃ ┃ ┣ ._SUCCESS.crc
 ┃ ┃ ┣ part-00161-4a844096-8dd9-425e-9d9d-bd9062cc6940.txt
 ┃ ┃ ┣ part-00196-4a844096-8dd9-425e-9d9d-bd9062cc6940.txt
 ┃ ┃ ┗ part-00199-4a844096-8dd9-425e-9d9d-bd9062cc6940.txt
 ┃ ┣ data_layer_tests.py
 ┃ ┣ test_model.py
 ┃ ┗ __init__.py
 ┣ __pycache__
 ┃ ┣ api.cpython-37.pyc
 ┃ ┣ load_test.cpython-37.pyc
 ┃ ┣ parameters.cpython-37.pyc
 ┃ ┗ utils.cpython-37.pyc
 ┣ api.py
 ┣ compute_RMSE.py
 ┣ load_test.py
 ┣ logger.py
 ┣ netflix_1y_test.csv
 ┣ netflix_1y_train.csv
 ┣ netflix_1y_valid.csv
 ┣ netflix_3m_test.csv
 ┣ netflix_3m_train.csv
 ┣ netflix_3m_valid.csv
 ┣ netflix_6m_test.csv
 ┣ netflix_6m_train.csv
 ┣ netflix_6m_valid.csv
 ┣ netflix_full_test.csv
 ┣ netflix_full_train.csv
 ┣ netflix_full_valid.csv
 ┣ parameters.py
 ┣ preds.txt
 ┣ RS_netflix3months_100epochs_64,128,128.ipynb
 ┗ utils.py

I am getting such an error (serialization.py). Can someone help me with this error?
D:\Anaconda\envs\practise\lib\site-packages\torch\serialization.py in _legacy_load(f, map_location, pickle_module, **pickle_load_args)
    762             "functionality.")
    763
--> 764     magic_number = pickle_module.load(f, **pickle_load_args)
    765     if magic_number != MAGIC_NUMBER:
    766         raise RuntimeError("Invalid magic number; corrupt file?")

UnpicklingError: A load persistent id instruction was encountered,
but no persistent_load function was specified.

Answer: After searching through PyTorch documentation, I ended up saving the model in the ONNX format and later loaded that ONNX model into PyTorch model and used it for inference.
import onnx
from onnx2pytorch import ConvertModel

def load_model_weights(model_architecture, weights_path):
    if os.path.isfile("model.onnx"):
        cherrypy.log("CHERRYPYLOG Loading model from: {}".format(weights_path))
        onnx_model = onnx.load("model.onnx")
        pytorch_model = ConvertModel(onnx_model)
        ## model_architecture.load_state_dict(torch.load(weights_path))
    else:
        raise ValueError("Path not found {}".format(weights_path))

def load_recommender(vector_dim, hidden, activation, dropout, weights_path):

    rencoder_api = model.AutoEncoder(layer_sizes=[vector_dim] + [int(l) for l in hidden.split(',')],
                               nl_type=activation,
                               is_constrained=False,
                               dp_drop_prob=dropout,
                               last_layer_activations=False)
    load_model_weights(rencoder_api, weights_path)
    rencoder_api.eval()
    rencoder_api = rencoder_api.cuda()
    return rencoder_api

Some useful resources:
torch.save
torch.load
ONNX tutorials


SpringSecurity RememberMeServices is not injected via annotations

Question: I am trying to configure SpringSecurity to work with Remember Me authentication.
Here is my Java configuration:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    UserDetailsService userDetailsService;
    @Autowired
    DatabasePersistentTokeRepositoryImpl databasePersistentTokeRepositoryImpl;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(new BCryptPasswordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        .authenticationProvider(rememberMeAuthenticationProvider())
        .rememberMe().tokenRepository(databasePersistentTokeRepositoryImpl).tokenValiditySeconds((int) TimeUnit.SECONDS.convert(7, TimeUnit.DAYS))
        .and()
            .csrf().disable();
    }

    @Bean()
    public AuthenticationProvider rememberMeAuthenticationProvider() {
        return new RememberMeAuthenticationProvider("KEY");
    }

    @Bean()
    public TokenBasedRememberMeServices rememberMeServices() {
        TokenBasedRememberMeServices rememberMeServices = new TokenBasedRememberMeServices("KEY", userDetailsService);
        rememberMeServices.setAlwaysRemember(true);
        return rememberMeServices;
    }
}

I see that rememberMeServices is not injected in RememberMeConfigurer. And that results in creating RememberMeAuthenticationFilter which refers to wrong rememberMeServices.
There is a section in Spring Security documentation describing this process using XML.
http://docs.spring.io/spring-security/site/docs/3.1.x/reference/springsecurity-single.html#session-mgmt
What is wrong with my injection and is it possible do to this without XML after all?
Answer: You aren't injecting it. There is no autowiring for the RememberMeConfigurer. Also why are you configuring so many beans?
The RememberMeAuthenticationProvider is already created for you, if you want to use a different key specify it using key("KEY"). This in turn will be used to create the RememberMeServices.
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    UserDetailsService userDetailsService;
    @Autowired
    DatabasePersistentTokeRepositoryImpl databasePersistentTokeRepositoryImpl;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(new BCryptPasswordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        .rememberMe()
            .key("KEY")
            .tokenRepository(databasePersistentTokeRepositoryImpl)
                .tokenValiditySeconds((int) TimeUnit.SECONDS.convert(7, TimeUnit.DAYS))
        .and()
            .csrf().disable();
    }
}

If you really need to set the alwaysRemember property to true you could use an ObjectPostProcessor to post process the filter and configure the RememberMeServices from there.
You would also have injected the wrong type of RememberMeServices as the one configured doesn't use the PersistentTokeRepository.
Answer: Just to provide an example of the code that @m-deinum's suggestion of an ObjectPostProcessor that sets alwaysRemember to true would look like would look like:
@Override
protected void configure(HttpSecurity http) throws Exception {
    http
    .rememberMe()
        .key("KEY")
        .tokenRepository(databasePersistentTokeRepositoryImpl)
        .tokenValiditySeconds((int) TimeUnit.SECONDS.convert(7, TimeUnit.DAYS))
        .withObjectPostProcessor( new ObjectPostProcessor<RememberMeAuthenticationFilter>() {

            @Override
            public <O extends RememberMeAuthenticationFilter> O postProcess( O object) {

                RememberMeAuthenticationFilter rmaf = (RememberMeAuthenticationFilter)
                PersistentTokenBasedRememberMeServices rms = (PersistentTokenBasedRememberMeServices)rmaf.getRememberMeServices();
                rms.setAlwaysRemember( true );

                return object;
            }
        })
    .and()
        .csrf().disable();
}


errors and projection matrices

Question: For the problem of projections, for the 1-d case, say we have some line $a$ through the origin, and a vector $v$, not on $a$. The projection of $v$ onto $a$ is some multiple $x$ of a: $p = xa$, and the error between the vector $v$ and $p$ is $e = v - xa$. The closest point to the line that $v$ can be projected on will end up forming a right angle if one were to draw a line from $v$ to that point $p$ so we can say $a^Te = 0$ or equivalently $a^T(v - xa) = 0$.
We can do the same for matrices and end up with $A^T(v - Ax) = 0$. Now here is the question algebraiclly I can see why in the 1-d case we transpose $a$ in $a^Te = 0$, because we want that to be a scalar, otherwise we would have $ae = Z$, some matrix $Z$, (which maybe will just be an all zero matrix? I'm not sure).
However for matrices, if the statement is "the error is orthogonal to the column space of $A$", $A^Te = 0$ instead seems to be telling me that it's orthogonal to the row space of $A$. I guess the question can be simplified to, why do we multiply by $A^T$ in $A^T(v - Ax) = 0$ instead of $A(v-Ax) =0$, aside from "the algebra works out that way" or "its the only way to multiply $A^TA$ because the dimensions of $AA$ don't work out unless $A$ is square to begin with (we are assuming $A$ is rectangular).
EDIT: ok upon further reflextion, $A^T(v - Ax) = 0$ is telling me that the vector $v - Ax$ is in the null space of $A^T$, and I know that the null space of $A^T$ and the column space of $A$ are orthogonal, therefor $v - Ax$ is orthogonal to column space of $A$, because it is in the null space of $A^T$
Answer: If $a^Te=0$, you don't say that the row vector $a^T$ is orthogonal to $e$; you say that the column vector $a$ is orthogonal to $e$. If you expand $A^Te$ by rows, you'll find that each element of the product is of the form $a_i^Te$. The rows of $A^T$ are, of course, the columns of $A$, so if $A^Te=0$, then $e$ is orthogonal to each column of $A$. By linearity, then, it is also orthogonal to the entire column space.

errors and projection matrices