Wednesday, May 11, 2016

converting curl command to python

For completing this we need to install  curl_to_requests Once we install we are ready to go ... for Converting cURL commands into equivalent Python Requests code

below is the sample code of use:

import curl_to_requests
curl_cmd = """curl 'https://github.com/mosesschwartz/curl_to_requests' \-H 'Accept-Encoding: gzip, deflate, sdch' \-H 'Accept-Language: en-US,en;q=0.8' \-H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.132 Safari/537.36' \-H 'Accept: text/html, */*; q=0.01' \-H 'Referer: https://github.com/mosesschwartz/curl_to_requests' \-H 'Connection: keep-alive' --compressed"""
print curl_to_requests.curl_to_requests(curl_cmd)

 Here is the sample output:

import requests

headers = {'Accept-Language': ' en-US,en;q=0.8', 'Accept-Encoding': ' gzip, deflate, sdch', 'Accept': ' text/html, */*; q=0.01', 'User-Agent': ' Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.132 Safari/537.36', 'Connection': ' keep-alive', 'Referer': ' https'}

r = requests.GET('https://github.com/mosesschwartz/curl_to_requests', headers=headers)

Tuesday, February 23, 2016

Working with Excel Files

Working with Excel Files

XLRD package is for reading data and formatting information from older Excel files (ie: .xls)

Installing the pkg :

pip install xlrd

Usage :

import xlrd

fil="test.xlsx"  # This is sample file with some data

workbook= xlrd.open_workbook(fil)

Sheet=workbook.sheet_by_name("Data_set1")      #Data_set1 this is the name that is displayed at bottom left cornet over the present working sheet

ccount=Sheet.ncols     #to get the column count

rcount=Sheet.nrows     #to get the row count

for r in range(rcount):
for c in range(ccount) :
val=[Sheet.cell_value(r,c)]
print (str(Sheet.cell_value(r,c)))

sample exel file


Working with regular expressions

Regular expressions in py

If we got the regular expression then using with that we can get the format string/ numbers 

In order to play with them we need to have a pkg called "exrex"
To install the pkg use below cmd :

pip install exrex

Exrex is a command line tool and python module that generates all - or random - matching strings to a given regular expression and more. It's pure python, without external dependencies.
There are regular expressions with infinite matching strings (eg.: [a-z]+), in these cases exrex limits the maximum length of the infinite parts.
Features
  • Generating all matching strings
  • Generating a random matching string
  • Counting the number of matching strings
  • Simplification of regular expressions
USAGE:
>>> import exrex
>>> exrex.getone('(ex)r\\1')
'exrex'
>>> list(exrex.generate('((hai){2}|world!)'))
['haihai', 'world!']
>>> exrex.getone('\d{4}-\d{4}-\d{4}-[0-9]{4}')
'3096-7886-2834-5671'
>>> exrex.getone('(1[0-2]|0[1-9])(:[0-5]\d){2} (A|P)M')
'09:31:40 AM'
>>> exrex.count('[01]{0,9}')
1023
>>> print '\n'.join(exrex.generate('This is (a (code|cake|test)|an (apple|elf|output))\.'))
This is a code.
This is a cake.
This is a test.
This is an apple.
This is an elf.
This is an output.
>>> print exrex.simplify('(ab|ac|ad)')
(a[bcd])