Friday, August 24, 2018

Is there a method to remove circular references in Ace

I'm wrapping AceEditor page in web view and returning the event results to the container and it is hanging on objects that contain circular references.

Is there a method in ace to remove circular references in the objects dispatched in the events of Ace Editor?

Solved

I didn't find one so I wrote one myself. It seems to work so far:

"use strict";

// returns an object that removes circular references
document.removeReferences = function (object, maxDepth, depth, objects, clone, debug) {
  if (objects==null) { objects = []; }
  if (clone==true) { clone = {}; }
  maxDepth = maxDepth==null ? 10 : maxDepth;
  depth = depth==null ? 0 : depth;
  var padding = "";
  var value;

  if (debug) { for (var i=0;i=maxDepth) {
          //object[key] = null;

          if (clone==null) {
            if (debug) console.log(padding+"max object limit - deleting:"+key);
            delete object[key];
          }
          else {

            if (typeof value=="array") {
              clone[key] = null;
            }
            else {
              clone[key] = null;
            }
            delete clone[key];
            if (debug) console.log(padding+"max object limit - not adding:"+key); 
          }

        }
        else {

          if (clone==null) {
            if (value!=null) {
              if (debug) console.log(padding+"diving into:"+key);
              document.removeReferences(value, maxDepth, depth+1, objects, null, debug);
            }
          }
          else {
            if (typeof value=="array") {
              clone[key] = value.slice();
            }
            else {
              clone[key] = value!=null ? {} : null;
            }

            if (value!=null) {
              if (debug) console.log(padding+"diving into:"+key);
              document.removeReferences(value, maxDepth, depth+1, objects, clone[key], debug);
            }
          }
        }
      }
      else if (typeof value == 'function') {
          //object[key] = null;
          if (clone==null) {
            delete object[key];
          }
          else {

          }
      }
      else {

        if (clone!=null) {
          if (debug) console.log(padding+" cloning key :"+key);

          if (typeof value=="array") {
            clone[key] = value.slice();
          }
          else {
            clone[key] = value!=null ? value : null;
          }
        }
      }
    }
  }

  if (clone!=null) {
    if (debug) console.log(padding+"return clone");
    return clone;
  }

  if (debug) console.log(padding+"return object");
  return object;
}

You then use something like this:

// inside an ace editor event: 
var debug = true;
var clonedEvent = document.removeReferences(event, 0, null, null, true, debug);

Monday, August 20, 2018

Significance of 1 and 2 while reverting merge commit

I came across a situation, where I need to revert one of my merge commit in git. I used the following command:

git revert -m 1 [merge commit id]

Now git provides one more option for the above command as below:

git revert -m 2 [merge commit id]

I did not get the difference between 1 and 2

References Used: https://www.christianengvall.se/undo-pushed-merge-git/

Solved

The git revert documentation provides the syntax like

git revert [--[no-]edit] [-n] [-m parent-number] [-s] [-S[]] …​

So the number after -m here represents the parent number

A merge commit may have two parents say consider an example that your current hash is 0ce2ca0b35f59af267241cf4d40d16a3e13ba6f3

and has two parents

df1acf5f54426d30f12c6b4558c3dd922297aae3
e19b912404ffd3c153ccac3072dbf22396896d2a

git revert -m 2 0ce2ca0b35f59af267241cf4d40d16a3e13ba6f3

will revert to e19b912404ffd3c153ccac3072dbf22396896d2a

Documentation


A git repository is just a tree of commits. A merge commit is just a commit that has two (or more) parents.

If you revert a commit with a single parent, you're just reversing the changes introduced by that commit.

If you're reverting a merge commit, however, you need to revert all the changes from the branch that you merged in. However, there's no real concept of which branch was merged to which - you just have the parents of the commit, and -m tells git which parent you want to revert to (ie. undo the changes introduced from the other parent)

See the documentation here: https://git-scm.com/docs/git-revert#git-revert--mparent-number


Sunday, August 19, 2018

Read entire HTML source using Nokogiri in Ruby

How would I obtain the full source of a webpage so that I can access the price? I'm trying to get the CSS class ".old-price" from the link "https://www.udemy.com/video-editing-for-instructors/?ccManual=&dtcode=RI7hLVu42dR7&couponCode=9dollarenrollment?

I think the reason I'm having problems is due to this Udemy code

   data-wrapcss = "static-content-wrapper payment-popup"
   data-passDtCode="true"
   data-enableLoader="true"
   data-purpose="take-this-course-button"
           data-overlayClosable="false"

And Nokogiri is skipping the code in that container which happenes to have some info I want. Here's my code trying to make it work:

require 'nokogiri'
require 'open-uri' 
page = Nokogiri::HTML(open("https://www.udemy.com/video-editing-for-instructors/?ccManual=&dtcode=RI7hLVu42dR7&couponCode=9dollarenrollment"))
puts page.css('.old-price') #this doesn't showcase anything, I want it to show the price of $89

Solved

If you download the source code with a command-line tool like wget or curl you will find that there is no such thing as old-price in the source code. So the class and that old price likely appears by the magic of the browser interpreting JavaScript. Nokogiri does not interpret JavaScript so it won't find the class you are looking for.

If you want to automate scraping of JavaScript-enabled web pages you have to use a tool that understands JavaScript. The test framework Capybara can probably be used for this together with a JavaScript-aware driver like WebKit or Poltergeist. I am sure there are other alternatives out there also.

Once you get the markup you can still use Nokogiri if you want. It is a nice parser. Capybara has some nice finders built on top of Nokogiri.

Alternative approach: if you start your browser's developer tools and go to the network tab you will see that there is a huge amount of requests from the web server to www.udemy.com. One of them will contain that old price either as markup or as data like JSON or XML. This may be an easier approach. Or not - the API may be secure so it is not possible to call it from the command line.


The element would be:

page.at('.old-price')

The HTML (after libxml has it's way with it) would be:

page.to_s

and the original HTML would be:

open(url).read

Friday, August 17, 2018

Pivot data based on particular ID without hardcoding

I have a table called ParamDetails with the following data

Aslvl_ID    Value   Score      Comments
1        123.0000   12.3000     Usha P-6914
1        321.0000   64.2000     Usha P-6914
1        456.0000   136.8000    Usha P-6914
1        654.0000   261.6000    Usha P-6914

6        147.0000   14.7000     Bhaskar L-17957
6        741.0000   148.2000    Bhaskar L-17957
6        258.0000   77.4000     Bhaskar L-17957
6        852.0000   340.8000    Bhaskar L-17957

11       4569.0000  456.9000    Murali Prasad K-21632
11       9654.0000  1930.8000   Murali Prasad K-21632
11       4789.0000  1436.7000   Murali Prasad K-21632
11       9874.0000  3949.6000   Murali Prasad K-21632

the requirement is to write a query to display data in below format.i have tried pivot and other few things but did not find any success. please help me with query or suggestion to get the expected output.

expected output:

1 Value    1 score     1 Comments    6 Value    6 score     6 Comments        11 Value    11 score     11 Comments 
123.0000    12.3000     Usha P-6914  147.0000   14.7000     Bhaskar L-17957   4569.0000  456.9000    Murali Prasad K-21632
321.0000    64.2000     Usha P-6914  741.0000   148.2000    Bhaskar L-17957   9654.0000  1930.8000   Murali Prasad K-21632
456.0000    136.8000    Usha P-6914  258.0000   77.4000     Bhaskar L-17957   4789.0000  1436.7000   Murali Prasad K-21632
654.0000    261.6000    Usha P-6914  852.0000   340.8000    Bhaskar L-17957   9874.0000  3949.6000   Murali Prasad K-21632

Solved

You can use a dynamic query to make this.

DECLARE @ColumnNames NVARCHAR(MAX) =''
SELECT  @ColumnNames = @ColumnNames + ', ' + QUOTENAME ( ColName + ' Value' ) + ', ' + QUOTENAME ( ColName +' Score' )  + ', ' + QUOTENAME ( ColName +' Comments' )  
FROM (SELECT DISTINCT CONVERT(VARCHAR,Aslvl_ID) ColName, Aslvl_ID FROM ParamDetails) AS T ORDER BY Aslvl_ID 
SET @ColumnNames = STUFF(@ColumnNames,1,1,'')



DECLARE @SqlText NVARCHAR(MAX)
SET @SqlText  = 'SELECT * FROM 
                    (SELECT ROW_NUMBER() OVER(PARTITION BY Aslvl_ID ORDER BY Aslvl_ID) RN,  CONVERT(VARCHAR,Aslvl_ID) +'' Value'' Col, CONVERT(VARCHAR,Value) Val FROM ParamDetails 
                       UNION ALL
                    SELECT  ROW_NUMBER() OVER(PARTITION BY Aslvl_ID ORDER BY Aslvl_ID) RN, CONVERT(VARCHAR,Aslvl_ID) +'' Score'' Col, CONVERT(VARCHAR,Score) Val FROM ParamDetails 
                       UNION ALL
                    SELECT  ROW_NUMBER() OVER(PARTITION BY Aslvl_ID ORDER BY Aslvl_ID ) RN, CONVERT(VARCHAR,Aslvl_ID) +'' Comments'' Col, Comments Val FROM ParamDetails 
                    ) SRC
                    PIVOT (MAX(Val) FOR Col IN (' + @ColumnNames + ') ) PVT'

EXEC sp_executesql @SqlText

Result:

RN  1 Value          1 Score          1 Comments       6 Value          6 Score          6 Comments       11 Value         11 Score         11 Comments
--- ---------------- ---------------- ---------------- ---------------- ---------------- ---------------- ---------------- ---------------- -----------------------
1   123.0000         12.3000          Usha P-6914      147.0000         14.7000          Bhaskar L-17957  4569.0000        456.9000         Murali Prasad K-21632
2   321.0000         64.2000          Usha P-6914      741.0000         148.2000         Bhaskar L-17957  9654.0000        1930.8000        Murali Prasad K-21632
3   456.0000         136.8000         Usha P-6914      258.0000         77.4000          Bhaskar L-17957  4789.0000        1436.7000        Murali Prasad K-21632
4   654.0000         261.6000         Usha P-6914      852.0000         340.8000         Bhaskar L-17957  9874.0000        3949.6000        Murali Prasad K-21632