Category Archives: Uncategorized

Regex Expression: |

Syntax: pattern1|pattern2

Search for both of them, if either is found it is true.

Example:

 re.findall("(the|top|coder)", "Marius is one of the topcoders.")
['the', 'top', 'coder']

Regex Expression: ^

Syntax: ^pattern

This would return True only if pattern is at the starting of the given text.

Example:

re.findall("^http", "http://www.google.co.in.")
['http']

Regex Expression: $

Syntax: pattern$

Returns True if pattern is in the end of given text

Example:

 re.findall("in$", "http://www.google.co.in")
['in']

Regular Expression: .

Syntax: pattern.

Use to match a single character.

 >>> re.findall("htt.", "http://www.google.co.in")
['http']

>>> re.findall("ht..", "http://www.google.co.in")
['http']</p><p>

Regular Expression: { }

Syntax: Pattern{number of times pattern must repeat continuously}

It multiplies the pattern number of times given within the braces and then search for it in the given text. In the following example it multiplies ‘t’ twice thus pattern to be searched is ‘tt’

Example:

>>>re.findall("t{2}", "http://www.google.co.in")
['tt']

Regular Expression: { ,}

Syntax: Pattern{n,}

Matches n or more occurrences of preceding expression.

Example:

>>> re.findall("t{2}", "http://www.google.co.in"
['tt']

Basics of Regular Expressions in Python

The Future of Gaming Technology

We have the responsibility to develop something similar for generations to come -:)

Xpath in Python using lxml

def extract_html(xpath, html):
# This method takes xpath and html content as input 
# and returns a list of tags and corresponding content seperated by ':::'
	result=[]
	from lxml import etree
	tree = etree.HTML(html)
	r = tree.xpath(xpath)
	for x in r:
		pattern = x.tag+":::"+x.text
		result.append(pattern)

LINUX Basic Commands

Count a string in many files without opening them

cat * | grep -c string

Count the number of files in a directory

ls -l . | egrep -c ‘^-‘

Compressing a file in tar.bz2 format

tar jcvf .tar.bz2 dir1 dir2 file1 file2 …

Decompressing a file in tar.bz2 format

tar -xjvf example.tar.bz2

All files and directory would be compressed in the same folder

Counting Number of files in a folder

ls | wc -l

TODOS:

What exactly is bash?

Tagged

Installing Cisco Any Connect on Ubuntu 11.04 64bit

Note: This method don’t work for Ubuntu 11.10 x64 and I have completely no idea what method would. If you find some blog article stating a working nice method for Ubuntu 11.10 x64, please drop the link in the comments area. 10x


Who should read?

If you have been googling/binging how to do this from a long time and you realized most of the blog articles talk about 32 bit edition. Moreover, the one who does talk about 64 bit edition are stating a very complicated procedure.

Step1:

First of all you would need to download Cisco AnyConnect VPN Client from your company’s portal. It would first try to auto-install and finally a manual Installation message would be displayed with the download link. Now you have to download vpnsetup.sh file by clicking on this Linux i386 link

Step 2:

The problem is the file downloaded in step 1 wont run on you machine because you got 64bit while the setup is for 32bit. So, all you need to do now make you machine support 32bit installations. And that can be done writing this command in your terminal:

sudo apt-get install ia32-libs lib32nss-mdns

Step 3:

Bash your vpnsetup.sh or install this .sh file whatever way you feel like doing it.

sudo bash vpnsetup.sh

Done, enjoy!!

LXML HTML Parsing Usage Example


from lxml.html import fromstring

f=open("result_urls","a+")
for x in range(0,1000):
	mySearchTree = fromstring(open(str(x)).read())
	f.write("Product Name: ")

	for a in mySearchTree.cssselect('tr input'):
		f.write(a.get('value')) #product_name
		f.write(chr(10))
	f.write("URLS:")
	f.write(chr(10))
	for a in mySearchTree.cssselect('h3 a'):
		f.write(a.get('href')) #url
		f.write(chr(10))
	f.flush()
f.close()

Read all lines of file as array

From file as a parameter in argument

keywords = open(sys.argv[1]).readlines()

Empty Inbox


import imaplib
for x in range (0,10000):
	box = imaplib.IMAP4_SSL('imap.mail.microsoftonline.com', 993)
	print x, ":Connected"
	box.login("*******@nextag.microsoftonline.com","******")
	print "Logged In"
	box.select('Inbox')
	print "Inbox Selected"
	for num in range(1,100):
		box.store(num, '+FLAGS', '\\Deleted')
	print "100 mail selected"
	box.expunge()
	box.close()
	box.logout()
print "I think all mails have been deleted"

delete all email


import imaplib
box = imaplib.IMAP4_SSL('imap.mail.microsoftonline.com', 993)
box.login("user@nextag.microsoftonline.com","password")
box.select('Inbox')
typ, data = box.search(None, 'ALL')
for num in data[0].split():
 box.store(num, '+FLAGS', '\\Deleted')
box.expunge()
box.close()
box.logout()

Logging Traceback


import traceback
import sys
try:
	try:
		print x
	except Exception, ex:
		raise NameError
except Exception, er:
	x=traceback.format_exc()
	print x