Python for Windows

*download PythonWin for modules.

//get user name

>>> import win32api

>>> username=win32api.GetUserName()
>>> print(username)

jjest

>>> import win32api

>>> computername=win32api.GetComputerName()

>>> print(computername)

TUBBS-PC

>>> free=win32api.GetDiskFreeSpace()

>>> print(free)

(8, 512, 106922325, 122096127)

/pyhandle object error

>>> print(win32api.PostMessage(‘hi’,1))
Traceback (most recent call last):
File ““, line 1, in
TypeError: The object is not a PyHANDLE object

//get version

>>> version=win32api.GetVersion
>>> print(version)

>>> version=win32api.GetVersion()
>>> print(version)
498073862

//disk space

>>> a=win32api.GetVolumeInformation(“C:\”)

>>> print(a)
(”, 1284288434, 255, 65470719, ‘NTFS’)

//get drive labels

>>> drives=win32api.GetLogicalDriveStrings()

>>> print(drives)

C:

Watch Movie Online John Wick: Chapter 2 (2017) subtitle english

Poster Movie John Wick: Chapter 2 2017

John Wick: Chapter 2 (2017) HD

Director : Chad Stahelski.
Producer : Basil Iwanyk.
Release : February 8, 2017
Country : United States of America.
Production Company : Thunder Road Pictures, LionsGate, 87Eleven.
Language : English, Italiano.
Runtime : 122 min.
Genre : Thriller, Action, Crime.

‘John Wick: Chapter 2’ is a movie genre Thriller, was released in February 8, 2017. Chad Stahelski was directed this movie and starring by Keanu Reeves. This movie tell story about John Wick is forced out of retirement by a former associate looking to seize control of a shadowy international assassins’ guild. Bound by a blood oath to aid him, Wick travels to Rome and does battle against some of the world’s most dangerous killers.

Do not miss to Watch movie John Wick: Chapter 2 (2017) Online for free with your family. only 2 step you can Watch or download this movie with high quality video. Come and join us! because very much movie can you watch free streaming.

Watch movie online John Wick: Chapter 2 (2017)
Incoming search term :

download John Wick: Chapter 2 2017 movie now
Watch John Wick: Chapter 2 2017 Online Free Viooz
film John Wick: Chapter 2 trailer
Watch John Wick: Chapter 2 2017 Online Viooz
watch full John Wick: Chapter 2 movie
John Wick: Chapter 2 2017 For Free Online
watch full John Wick: Chapter 2 2017 movie online
live streaming film John Wick: Chapter 2 2017
watch John Wick: Chapter 2 2017 film online now
live streaming film John Wick: Chapter 2 online
John Wick: Chapter 2 2017 English Episode
live streaming movie John Wick: Chapter 2 2017 online
John Wick: Chapter 2 2017 English Full Episodes Watch Online
film John Wick: Chapter 2 2017 online
download full film John Wick: Chapter 2
streaming John Wick: Chapter 2 movie
John Wick: Chapter 2 2017 English Episodes Free Watch Online
John Wick: Chapter 2 live streaming film
John Wick: Chapter 2 2017 HD Full Episodes Online
live streaming movie John Wick: Chapter 2
John Wick: Chapter 2 2017 English Episodes
John Wick: Chapter 2 2017 Full Episode
movie John Wick: Chapter 2 2017
streaming John Wick: Chapter 2 2017 film
watch film John Wick: Chapter 2 now
John Wick: Chapter 2 2017 Online Free Megashare
Watch John Wick: Chapter 2 2017 Online Megashare
Watch John Wick: Chapter 2 2017 Online Free Putlocker
John Wick: Chapter 2 2017 HD English Full Episodes Download
Watch John Wick: Chapter 2 2017 Online Free putlocker
John Wick: Chapter 2 2017 Episodes Online
John Wick: Chapter 2 2017 Full Episodes Online
John Wick: Chapter 2 2017 movie download
Watch John Wick: Chapter 2 2017 Online Free
Watch John Wick: Chapter 2 2017 Online Putlocker
John Wick: Chapter 2 2017 English Full Episodes Download
John Wick: Chapter 2 2017 For Free online
John Wick: Chapter 2 2017 Episodes Watch Online
John Wick: Chapter 2 2017 Watch Online
John Wick: Chapter 2 2017 English Full Episodes Free Download
watch full film John Wick: Chapter 2 2017 online
John Wick: Chapter 2 2017 English Full Episodes Online Free Download
Watch John Wick: Chapter 2 2017 Online Free megashare
download film John Wick: Chapter 2 2017 now
John Wick: Chapter 2 2017 Full Episodes Watch Online

Working with TAR and ZIP files in Python

>>>import os,tarfile

>>> tarfile=tarfile.open(‘/data/archive-1-1-1.tgz’,’r’)
>>> for x in tarfile:
…     print x

<TarInfo ‘./tmp/’ at 0xb7d9588c>
<TarInfo ‘./tmp/biteoff/’ at 0xb7d9558c>
<TarInfo ‘./tmp/biteoff/bite_offload.tgz’ at 0xb7d959cc>
<TarInfo ‘./tmp/biteoff/aesid.log’ at 0xb7d95a2c>
<TarInfo ‘./tmp/biteoff/lruid.xml’ at 0xb7d95a8c>
<TarInfo ‘./tmp/biteoff/BITEdump.sql’ at 0xb7d95aec>
<TarInfo ‘./tmp/biteoff/offload.cfg’ at 0xb7d95b4c>

ZIP

>>> import os, zipfile
>>> zipfile=zipfile.ZipFile(‘/home/jesterj/foo.zip’,’r’)
>>> for x in zipfile.namelist():
…     print x

index.php

Python: Tuples vs Lists


Tuple are immutable. Lists are mutable.

a=(1,2,3) #tuples have ()
a[0]=100

Error! Tuple values cannot be changed!

a=[1,2,3] #list have brackets
a[0]=100

success!

Note: dicts have curly braces ie: a={}

Python: Unions of lists and sets.

a=set(['apple','apple','orange','apple','pear','berry'])
b=set(['pear','coconut','strawberry'])
a | b

set([‘strawberry’, ‘coconut’, ‘apple’, ‘orange’, ‘pear’, ‘berry’])

a.union(b)

set([‘strawberry’, ‘coconut’, ‘apple’, ‘orange’, ‘pear’, ‘berry’])

Python: Reading and Writing to a Word (.doc) file

Need to install ‘antiword’ debian package then read output from CLI.

import commands
output=commands.getoutput('antiword /home/jesterj/Desktop/test.doc')
print output

Mary had a little lamb. Mary had a little lamb. Mary had a little lamb.
Mary had a little lamb. Mary had a little lamb. Mary had a little lamb.
Mary had a little lamb. Mary had a little lamb. Mary had a little lamb.
Mary had a little lamb. Mary had a little lamb. Mary had a little lamb.
Mary had a little lamb. Mary had a little lamb. Mary had a little lamb.
Mary had a little lamb. Mary had a little lamb. Mary had a little lamb.
Mary had a little lamb. Mary had a little lamb. Mary had a little lamb.
Mary had a little lamb. Mary had a little lamb. Mary had a little lamb.
Mary had a little lamb. Mary had a little lamb. Mary had a little lamb.
Mary had a little lamb. Mary had a little lamb. Mary had a little lamb.

Write to word…

Pretty easy here. Just write to a file and append with .doc.

f=open("newfile.doc","wr")
f.write(output)
f.close()

Badow!

Python: Reading & Writing to Excel

install ‘xlrd’ for reading…
Download: http://www.lexicon.net/sjmachin/xlrd.htm

root@donkey:~# sudo apt-cache search xlrd
python-xlrd – extract data from MS Excel spreadsheet files

import xlrd
wb=xlrd.open_workbook('/home/jesterj/Desktop/JJ-MonthlyNumbersNew-6.18.09.xls')
wb.sheet_names()


[u'Sheet1', u'Sheet2', u'Sheet3']
sh=wb.sheet_by_index(0)
for rownum in range(sh.nrows):
    print sh.row_values(rownum)

Ref: http://scienceoss.com/read-excel-files-from-python/

Install 'xlwt' for writing excel files...
Download: http://pypi.python.org/pypi/xlwt


import xlwt
from datetime import datetime

font0 = xlwt.Font()
font0.name = 'Times New Roman'
font0.colour_index = 2
font0.bold = True

style0 = xlwt.XFStyle()
style0.font = font0

style1 = xlwt.XFStyle()
style1.num_format_str = 'D-MMM-YY'

wb = xlwt.Workbook()
ws = wb.add_sheet('A Test Sheet')

ws.write(0, 0, 'Test', style0)
ws.write(1, 0, datetime.now(), style1)
ws.write(2, 0, 1)
ws.write(2, 1, 1)
ws.write(2, 2, xlwt.Formula("A3+B3"))

wb.save('example.xls')

Return diff in list or set.

There is no way to do a function diff on a list. However you can do a loop.

list1 = [1, 2, 3, 5, 7,  11]
list2 = [1, 2, 4, 8, 16, 32]
diff_list = []
for item in list1:
    if item not in list2:
        diff_list.append(item)

print diff_list
[3, 5, 7, 11]

Or use sets….

from sets import Set
set1=Set(['a','b','c'])
set2=Set(['d','b','c'])
diff=set1-set2
print diff
 

Set(['a'])

for x in diff:
    print x

a

Or conver list to a set…

list1=['a','b']
list2=['b','c']

set1=set(list1)
set2=set(list2)

diff=set1-set2

Python argv & getopts

def main():
cases=['auth','transact','dns','network','ssl']
print sys.argv
try:
if sys.argv[1]=='-t':
if sys.argv[2] not in cases or len(sys.argv[2])==0:
print "*Must specify a valid case: %s " % cases
exit()
else:
case=sys.argv[2]

if case=='auth':
test_authentication()
if case=='transact':
test_transaction_problem(transact_url)
if case=='dns':
test_dns(hosts)
if case=='network':
test_network(hosts)
if case=='ssl':
test_ssl(hosts)
else:
print "You must specify a valid test %s" % cases
except:
print "You must specify option -t followed by test option %s" % cases

Python: Get http status code

Using urllib…

#!/usr/bin/python
import urllib
c=urllib.urlopen('http://nike.com')
print c.getcode()

200

or try with httplib…

#!/usr/bin/python
import httplib
conn = httplib.HTTPConnection("www.python.org")
conn.request("GET", "/index.html")
r1 = conn.getresponse()
print r1.status

200