Monday 3 February 2020

What is three dot (...) in Javascript?



In this article we explain what is the three dots (...) in javascript.

Three dots (...) called Spread Attributes or Spread Operator or Rest Operator. It has come in Ecama script(ES6).

It allows an array expression or string or anything which can be iterating  to be expanded in places where zero or more arguments for function calls or elements for the array are expected.

Merge two arrays:

var arr1 = [1,2,3];
var arr2 = [4,5,6];

arr1 = [...arr1, ...arr2];
console.log(arr1);  
//[1, 2, 3, 4, 5, 6]

Copying array:
var arr = [1, 2, 3];
var arr2 = [...arr];

console.log(arr); 
//[1, 2, 3]

Spread operator is used for a reference type :
let person= {
    name: 'Ram',
    age: 54 
}
person1= person;

person1.name = "Raj";

console.log( person.name); // output: Raj
This is called reference type, one object affects other objects because they are shareable in memory. if you want to getting value separately means spread memory both variable.

 let person= {
        name: 'Ram',
        age: 54 
    }
person2 = {...person};

person2.name = "Raj";

console.log(person.name); // output: Ram

Wednesday 25 July 2018

Check dependency in python

import pkg_resources
from pkg_resources import DistributionNotFound, VersionConflict
def checkHealth():
try:
text_file = open("requirements.txt", "r")
lines = text_file.readlines()
text_file.close()
# dependencies can be any iterable with strings,
# e.g. file line-by-line iterator
dependencies = lines

# here, if a dependency is not met, a DistributionNotFound or VersionConflict
# exception is thrown.
pkg_resources.require(dependencies)
return True
except Exception as e:
print(e)
return False

Friday 30 March 2018

python parser

def parseDate(stringData,dataRgex=['(\w+\s\d+\,\s\d+)','(\w+\s\d+\,\d+)','(\w+\s\d+\s\d+)','(\d+\s\w+\s\d+)','(\d+\/\d+\/\d+)','(\d+\.\d+\.\d+)','(\d+\-\w+\-\d+)','(\d+\'\w+\s\d+)']):
    #print stringData    getDate = []
    for dataList in dataRgex:
        res_find = re.findall(dataList, stringData, re.IGNORECASE)
        for dateData in res_find:
            try:
                dparser.parse(dateData)


                return dateData
            except Exception as e:
                pass    return getDate



#dataRgex data type listdataTime = '(\d+\:\d+\:\d+ \w+)|(\d+\:\d+\:\d+\w+)|(\d+\:\d+ \w+)|(\d+\:\w+)'def parseDateTime(stringData,dataRgex=['(\w+\s\d+\,\s\d+)','(\w+\s\d+\,\d+)','(\w+\s\d+\s\d+)','(\d+\s\w+\s\d+)','(\d+\/\d+\/\d+)','(\d+\.\d+\.\d+)','(\d+\-\w+\-\d+)','(\d+\'\w+\s\d+|\d+\'\w+)']):

    getTime = []
    for dataList in dataRgex:
        res_find = re.findall(dataList, stringData, re.IGNORECASE)

        for dateData in res_find:
            if(res_find):
                for dataT in dateData:
                    try:
                        dparser.parse(dataT)
                        getTime.append(dataT)
                    except Exception as e:
                        pass    return getTime



def parseGSTIN(stringData,dataRgex=['(.*)']):
    getGSTIN = []
    getData = ['GSTIN','STIN', 'gstin', 'GST']
    for data in getData:
        for dataList in dataRgex:
            try:
                res_find = re.search(data+dataList, stringData)
                getGSTIN.append(res_find.group(1))
            except Exception as e:
                pass    return getGSTIN



def parseAmount(stringData,dataRgex=['(.*)']):
    returnList= []
    getData = ['TOTAL', 'TOTAL', 'Total', 'Total', 'total', 'total', 'TotalAmount', 'Total Amount', 'totalamount']
    getInvoiceNumber = []

    matchKey = ""    invNumber = ""    listMatch = []
    getLen = ""    for data in getData:
        if ((str(data) in str(stringData)) and str(data) != 'None' and str(stringData) != 'None'):
            getLen = stringData.count(str(data))
            matchKey = (str(data))
            break    getPos = [m.start() for m in re.finditer(matchKey, stringData)]
    getDataLoop = [stringData[dd:] for dd in getPos]
    for stringData in getDataLoop:
        invNumberList = stringData.split(matchKey, 1)
        invString = invNumberList[1]
        invList = invString.split(' ', 1)
        if (len(invList) > 1):
            invString = invList[1]
        else:
            invString = invList[0]

        finalInvList = invString.split(' ')
        invNumber = finalInvList[0].split('\n')[0]
        returnList.append(invNumber)
    return returnList



def parseInvoiceNumber(stringData,dataRgex=['(.*)']):
    getInvoiceNumber = []
    getData = ['Ilnvoice No','Invoice Number:','Invoice No','INVOICE NO.','InvoiceNumber','Invoice Number','InvNo','Inv. No.','InvoiceNo','Invoice No.','Invoice Number','lnvolceNo','lnvolce No.','lnvolceNumber','lnvolce Number','noviceNo']
    matchKey = ""    invNumber = ""
    for data in getData:
        if((str(data) in str(stringData)) and str(data) != 'None' and str(stringData) !='None'):
            matchKey = str(data)
            break    invNumberList = stringData.split(matchKey,1)
    invString = invNumberList[1]
    invList = invString.split(' ',1)
    if(len(invList)>1):
        invString = invList[1]
    else:
        invString = invList[0]

    finalInvList = invString.split(' ')
    invNumber = finalInvList[0].split('\n')[0]
    return invNumber


def parseCurrency(stringData,dataRgex=['(.*)']):
    getCurrency = []

    getData = ['TOTAL','TOTAL','Total','Total','total','total','TotalAmount','Total Amount','totalamount','total amount','totalAmount']
    for data in getData:
        for dataList in dataRgex:
            try:
                res_find = re.search(data+dataList, stringData)
                getCurrency.append(res_find.group(1))

            except Exception as e:
                pass    #p = re.findall(r"[INR]|[$]",str(getCurrency[0]))    p = []
    curr = ['$','INR','1NR','EUR','AUD','CAD','SGD','GBP']
    for c in curr:
        for e in getCurrency:


            if str(c) in str(e) and str(c) != 'None' and str(e) != 'None':
                p = str(c)

                return [p]
            else:
                p = ""    return p


def getPanNumber(stringData,dataRgex=['[A-Za-z]{5}\d{4}[A-Za-z]{1}','[A-Za-z]{4}\d{4}[A-Za-z]{2}']):
    try:
        for dataR in dataRgex:
            res_find = re.findall(dataR, stringData)
    except Exception as e:
        print (e)
    return res_find

def getAadharNumber(stringData,dataRgex=['(?<!\d)\d{4}\s\d{4}\s\d{4}(?!\d)']):
    try:
        res_find = re.findall(dataRgex[0], stringData)
    except Exception as e:
        print (e)
    return res_find

Wednesday 24 January 2018

Html to pdf convert jquery

<!DOCTYPE html>
<html>

    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

        <title>jquery - Download a div in a HTML page as pdf using javascript - Stack Overflow</title>
</head>
<body>
<div id="content">
     <h3>Hello, this is a H3 tag</h3>

    <p>a pararaph</p>
</div>
<div id="editor"></div>
<button id="cmd">generate PDF</button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/0.9.0rc1/jspdf.min.js"/></script>
<script>
var doc = new jsPDF();
var specialElementHandlers = {
    '#editor': function (element, renderer) {
        return true;
    }
};

$('#cmd').click(function () {
    doc.fromHTML($('#content').html(), 15, 15, {
        'width': 170,
            'elementHandlers': specialElementHandlers
    });
    doc.save('sample-file.pdf');
});

</script>
</body>
</html>