Formatting a Hard Disk on Ubuntu Server

Syntax:

umount filesystem
mkfs -t ext3 filesystem
mount filesystem

Example:
umount xvdp1
mkfs -t ext3 /dev/xvdp1
mount /dev/xvdp1

Showing Exception in detail in python

import sys, os
try:
    raise NotImplementedError("No error")
except Exception, e:
    exc_type, exc_obj, exc_tb = sys.exc_info()
    fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]      
    print(exc_type, fname, exc_tb.tb_lineno)

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 -:)

python’s Data Structures complexity analysis

Complexity Analysis of General Data Structures for Common Operations:

 

Linked list

Array

Dynamic
array

Balanced
tree

Random access
list

Indexing

Θ(n)

Θ(1)

Θ(1)

Θ(log n)

Θ(log n)

Insert/delete at beginning

Θ(1)

N/A

Θ(n)

Θ(log n)

Θ(1)

Insert/delete at end

Θ(1)

N/A

Θ(1) amortized

Θ(log n)

Θ(log n) updating

Insert/delete in middle

search time +
Θ(1)[1]

N/A

Θ(n)

Θ(log n)

Θ(log n) updating

Wasted space (average)

Θ(n)

0

Θ(n)[2]

Θ(n)

Θ(n)

Complexity Analysis of Python’s Data Structures for Common Operations:

list

Internally, a list is represented as an array

Operation

Average Case

Amortized Worst Case

Copy

O(n)

O(n)

Append[1]

O(1)

O(1)

Insert

O(n)

O(n)

Get Item

O(1)

O(1)

Set Item

O(1)

O(1)

Delete Item

O(n)

O(n)

Iteration

O(n)

O(n)

Get Slice

O(k)

O(k)

Del Slice

O(n)

O(n)

Set Slice

O(k+n)

O(k+n)

Extend[1]

O(k)

O(k)

Sort

O(n log n)

O(n log n)

Multiply

O(nk)

O(nk)

x in s

O(n)

 

min(s), max(s)

O(n)

 

Get Length

O(1)

O(1)

collections.deque

A deque (double-ended queue) is represented internally as a doubly linked list

Operation

Average Case

Amortized Worst Case

Copy

O(n)

O(n)

append

O(1)

O(1)

appendleft

O(1)

O(1)

pop

O(1)

O(1)

popleft

O(1)

O(1)

extend

O(k)

O(k)

extendleft

O(k)

O(k)

rotate

O(k)

O(k)

remove

O(n)

O(n)

dict

 

Operation

Average Case

Amortized Worst Case

Copy[2]

O(n)

O(n)

Get Item

O(1)

O(n)

Set Item[1]

O(1)

O(n)

Delete Item

O(1)

O(n)

Iteration[2]

O(n)

O(n)

Reference:

http://wiki.python.org/moin/TimeComplexity

http://en.wikipedia.org/wiki/Linked_list

Tagged , , , , , , ,

Creating a variable with value of another variable in Python


domain = "google"

vars()[domain] = 'Nextag'

print "Output is", google

Output


Output is NexTag

Increasing number of threads per process

There is no direct limit on number of threads a process can have. Rather, this is calculated using following formula:

number of threads = total virtual memory / (stack size*1024*1024)

Thus, the number of threads per process can by increasing total virtual memory or by decreasing stack size. Decreasing stack size can lead to code failure due to stack overflow while max virtual memory is equal to the swap memory.

Check you machine:

Total Virtual Memory: ulimit -v (default is unlimited, thus you need to increase swap memory to increase this)

Total Stack Size: ulimit -s (default is 8Mb)

Command to increase these values:

ulimit -s newvalue

ulimit -v newvalue

replace new value with the value you want to put as limit.

How to increase swap memory?

http://www.ehow.com/how_5001512_increase-virtual-memory-linux.html

Tagged , , , , , , , , , , , , , , ,

Running C curl code on LINUX/UBUNTU

Apart from default installations following is required:
apt-get update
apt-get install libcurl4-openssl-dev

How to run the code?
gcc code_filename -l curl -o compiled_filename

- l is used to link the compiler with curl. This shall make sure there is no curl.h import error.

References:

http://askubuntu.com/questions/78183/installing-curl-h-library

Tagged , , , , ,

Inserting Data from a text file to MySQL Table

  • Each column must be tab separated
  • Each row must be a new line
  • Total number of columns in file should be equal to total number of columns in text file to avoid ambiguity

Step 1: Copy the file to local folder of MySQL Machine (root) folder)

Step 2:  Run following command on MySQL interface:

load data local infile “filename” into table data_table; 

Reference: http://forums.mysql.com/read.php?10,155114,155141

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)
Follow

Get every new post delivered to your Inbox.