Wednesday, November 20, 2019

How to iterate through document nodes in javascript

ar total_words=0;
        var words_missing=false;
        var items = [];
        var myPosts = document.getElementById("doc_list").getElementsByTagName("span");
        for (var i = 0; i < myPosts.length; i++) {
            //omitting undefined null check for brevity
            if (myPosts[i].id.lastIndexOf("doc_wc_", 0) === 0) {
                console.log("found "+myPosts[i].id);
                var wc_text=$("#"+myPosts[i].id).text();
                if(wc_text=='')
                {
                    words_missing=true;
                    break;
                }
                else
                {
                    var wsIndex=wc_text.indexOf(" ");
                    var word_count=parseInt(wc_text.substring(0,wsIndex));
                    console.log(myPosts[i].id+" word count is "+word_count);
                    total_words=total_words+word_count;

                }
            }
        }

Thursday, October 31, 2019

valid json not parsable in PHP - solution

use below code

json_decode( preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $json_string), true );

Wednesday, October 9, 2019

Create Editable DIV in HTML

use contenteditable="true" tag in div

<div contenteditable="true"></div>

Saturday, August 24, 2019

Mongo and Python - getting started

1. install pymongo {pip install pymongo}
2. from pymongo import MongoClient

conn=MongoClient()
db=conn.<dbname>
collection=db.<collection_name>

How to create unique index with pymongo
collection.create_index([('key',pymongo.ASCENDING)],unique=True)

Update command in Python
collection.update({'key':keyval},{'key':keyval,'field1':f1val},upsert=True)

Quick MongoDB commands

Create mongo database:
 use <database_name>

Create Collections
db.createCollection(name, options) 

db.collection.createIndex({"key":1},{unique:true})

db.collection.deleteMany({}) to delete all items

db.collection.drop() delete collection

Insert if not found otherwise update

db.people.update(
   { name: "Andy" },
   {
      name: "Andy",
      rating: 1,
      score: 1
   },
   { upsert: true }
)

Sunday, August 11, 2019

CONCAT_WS magic concat in mysql

16
If you want to skip NULL values (but not empty strings), you can use CONCAT_WS() function:
CONCAT_WS( ', ',            -- Separator
           CONCAT_WS(' ', tadd.street_number, tadd.street_name),
           tadd.apt_number,  tadd.city, 
           tadd.postal_code, tadd.country
         ) AS Address

Saturday, August 10, 2019

How to do fast query without inner join

refer
https://medium.com/squad-engineering/blazingly-fast-querying-on-huge-tables-by-avoiding-joins-5be0fca2f523

WITH user_ids AS
  (SELECT id
   FROM user
   WHERE account_id IN
       (SELECT generate_series(1,1000)))
SELECT purchase.id
FROM purchase
WHERE user_id IN
    (SELECT id
     FROM user_ids);


JOIN:

SELECT "purchase"."id"
FROM "purchase"
INNER JOIN "user" ON ("purchase"."user_id" = "user"."id")
WHERE "user"."account_id" IN
    (SELECT generate_series(1,1000));

Translation - Key terminologies

1. Edit Distance => Minimum character change required to match two strings. Also known as Levenshtein distance.

https://pypi.org/project/editdistance/0.3.1/ is python library to use for finding edit distance between two sentences.

Friday, August 9, 2019

Great tool to creare Embed Charts

How to read Mongo JSON in Python

Below is sample code to read Mongo JSON in python code.

Note: Every line have one Object in Mongo JSON

import json
file = open('../data/j.json','r')
jdata=[]
for line in file:
    jdata.append(json.loads(line))
totalSM=0for reportD in jdata:
    print reportD['key1']
    if 'key2' in reportD.keys():
        imD=reportD['key3']
        if '7' in imD:
            totalSM=totalSM+1            print totalSM

Thursday, August 1, 2019

Install PHP composer

1,sudo curl -s https://getcomposer.org/installer | php
2.sudo mv composer.phar /usr/local/bin/composer

Thursday, July 4, 2019

Wednesday, April 10, 2019

modal dialog sample code

 <!-- Modal -->    <div id="myModal" class="modal fade" role="dialog">
        <div class="modal-dialog">

            <!-- Modal content-->            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title">xyz</h4>
                </div>
                <div class="modal-body">
                    xyz:
                    <select name="xyz" class="form-control xyz">
                        <option value="" disabled selected>xyz</option>
                        

                    </select>
                           </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default abc" >CTA1</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                </div>
            </div>

        </div>
    </div>

to show modal
$('#myModal').modal('show'); 
to close
$('#my_modal').modal('hide');

Thursday, April 4, 2019

Mysql new learning .. Use of keyword "having"

You can use Having with group by

SELECT COUNT( x ) AS cnt
GROUP BY o.x
HAVING cnt >1

Python using relative paths in files

Use below syntax to run python script from anywhere

import sys
import os.path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__),"../Config/")))

Sunday, March 31, 2019

Learning Python - Key Notes Part # 1

1) Strings are immutable in python.

2) Access Substring directly like name1[1:34]

3) Negative range name1[-23] , index from behind

4) list can have different type of data like PHP

5) list allows pop, insert , append, extend and del functions

6) list is mutable but tuple is not.
  names=[] <= list
  names=() <= tuple

*** tuple iteration is faster than list ***

7) set have no sequence.
    names={} <= set
8) memory efficient, one address for one value
a=10
b=a
k=10

id(10)=id(a)=id(b)=id(k)

garbase collection , if no one refering to a value

9) there is no way to create immutable variable. Just declare constants in Uppercase.

10) types
    > None
    > Numeric => int,float, complex,bool
   > list
   > tuple
   > set
  > String
   > Range
   > Dictionary

11) Range ==
   range(1,10)
   range(1,10,2) ==> diff of 2

12) Dictionary
   Key value pair
  d={'k1':'v1','k2':'v2'}

13) uniry operator
  negate == just put -
 p=-q

14) for loop
   for i in x :

15) python function can return multiple values
      def func() :
         x=2
         y=3
         return x,y
16) there is nothing pass by value or reference, it is about mutable vs immutable variables in python.

   int will not change but list will change inside function

17) Keyword arguments can be passed directly
    z=sum(y=12,x=10)
    def sum(x,y):

18) variable length parameters passed as tuple
      z=sum(1,2,3,4,5)
      def sum(*a) :

   *Keyword variable length arguments
    def sum(**a):

   z=sum('x'=1,'y'=2,'c'=3);
19) global variable , use keyword global to tell about it
        a =10
         def f():
             global a;
               a=13

   or use globals() function
          x=globals()['a']
20) anonymous functions (lamnda expressions):
      functions are object in python. One can pass function to another function.
      square==> f= lambda a:a*a
   z= f(4)

21) Filter, Map and Reduce
     filter(lambda x:x%2==0 ,list)
     map(lambda x:x*2, out1)
    reduce(lambda x,y:x+y,out2)


Friday, March 8, 2019

Excel sheet trics

1. Absolute reference

Absolute Reference

See the formula in cell E3 below.
1. To create an absolute reference to cell H3, place a $ symbol in front of the column letter and row number ($H$3) in the formula of cell E3.


Saturday, February 23, 2019

PHP setup on Mac

Brew install apache in /usr/local/etc/httpd

Add PHP type manually

LoadModule php7_module    /usr/local/opt/php/lib/httpd/modules/libphp7.so

ddType x-httpd-php .php
AddHandler application/x-httpd-php .php .php5 

Add user to _www group : sudo dseditgroup -o edit -a $username_to_add -t user wheel