https://www.youtube.com/watch?v=JFsueTM1QtU
http://pastebin.com/2zi0SgfT
#!/usr/bin/env python
import webkit, gtk, os
win = gtk.Window()
win.resize(600,800)
win.connect('destroy', lambda w: gtk.main_quit())
scroller = gtk.ScrolledWindow()
win.add(scroller)
web = webkit.WebView()
path=os.getcwd()
print path
web.open("file://" + path + "/index.html")
#web.open("https://www.yahoo.com/")
scroller.add(web)
win.show_all()
gtk.main()
Sunday, February 28, 2016
Thursday, February 25, 2016
python logging on both file and console
http://stackoverflow.com/questions/13733552/logger-configuration-to-log-to-file-and-print-to-stdout
http://victorlin.me/posts/2012/08/26/good-logging-practice-in-python
#!/usr/bin/env python
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
# create a file handler
handler = logging.FileHandler('hello.log')
handler.setLevel(logging.INFO)
# create a logging format
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(handler)
logger.info('Hello baby')
# handler2
logger2 = logging.getLogger(__name__ + '2')
logger2.setLevel(logging.INFO)
handler2 = logging.StreamHandler()
handler2.setLevel(logging.INFO)
# create a logging format
formatter2 = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler2.setFormatter(formatter2)
# add the handlers to the logger
logger2.addHandler(handler2)
logger2.info('Hi Baba!')
http://victorlin.me/posts/2012/08/26/good-logging-practice-in-python
#!/usr/bin/env python
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
# create a file handler
handler = logging.FileHandler('hello.log')
handler.setLevel(logging.INFO)
# create a logging format
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(handler)
logger.info('Hello baby')
# handler2
logger2 = logging.getLogger(__name__ + '2')
logger2.setLevel(logging.INFO)
handler2 = logging.StreamHandler()
handler2.setLevel(logging.INFO)
# create a logging format
formatter2 = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler2.setFormatter(formatter2)
# add the handlers to the logger
logger2.addHandler(handler2)
logger2.info('Hi Baba!')
Saturday, February 20, 2016
sorting algorithms
https://www.youtube.com/watch?v=GUDLRan2DWM&index=2&list=PL2_aWCzGMAwKedT2KfDMB9YA5DgASZb3U
#Bubble sort: O(n^2)
http://www.geekviewpoint.com/python/sorting/bubblesort
#!/usr/bin/env python
def bubblesort( A ):
for i in range( len( A ) ):
for k in range( len( A ) - 1, i, -1 ):
if ( A[k] < A[k - 1] ):
swap( A, k, k - 1 )
#break
def swap( A, x, y ):
tmp = A[x]
A[x] = A[y]
A[y] = tmp
A = [3,1,5,4]
def main():
bubblesort(A)
print A
if __name__ == '__main__':
main()
# insertion sort : O(n^2)
#!/usr/bin/env python
def insertionsort( aList ):
for i in range( 1, len( aList ) ):
tmp = aList[i]
k = i
while k > 0 and tmp < aList[k - 1]:
aList[k] = aList[k - 1]
k -= 1
aList[k] = tmp
A = [3,1,5,4]
def main():
insertionsort(A)
print A
if __name__ == '__main__':
main()
# Selection sort : O(n^2):
#!/usr/bin/env python
def selectionsort( aList ):
for i in range( len( aList ) ):
least = i
for k in range( i + 1 , len( aList ) ):
if aList[k] < aList[least]:
least = k
swap( aList, least, i )
def swap( A, x, y ):
tmp = A[x]
A[x] = A[y]
A[y] = tmp
A = [3,1,5,4]
def main():
selectionsort(A)
print A
if __name__ == '__main__':
main()
# Merge sort: Time complexity: O(n log n):
# Quick Sort
#Bubble sort: O(n^2)
http://www.geekviewpoint.com/python/sorting/bubblesort
#!/usr/bin/env python
def bubblesort( A ):
for i in range( len( A ) ):
for k in range( len( A ) - 1, i, -1 ):
if ( A[k] < A[k - 1] ):
swap( A, k, k - 1 )
#break
def swap( A, x, y ):
tmp = A[x]
A[x] = A[y]
A[y] = tmp
A = [3,1,5,4]
def main():
bubblesort(A)
print A
if __name__ == '__main__':
main()
# insertion sort : O(n^2)
#!/usr/bin/env python
def insertionsort( aList ):
for i in range( 1, len( aList ) ):
tmp = aList[i]
k = i
while k > 0 and tmp < aList[k - 1]:
aList[k] = aList[k - 1]
k -= 1
aList[k] = tmp
A = [3,1,5,4]
def main():
insertionsort(A)
print A
if __name__ == '__main__':
main()
# Selection sort : O(n^2):
#!/usr/bin/env python
def selectionsort( aList ):
for i in range( len( aList ) ):
least = i
for k in range( i + 1 , len( aList ) ):
if aList[k] < aList[least]:
least = k
swap( aList, least, i )
def swap( A, x, y ):
tmp = A[x]
A[x] = A[y]
A[y] = tmp
A = [3,1,5,4]
def main():
selectionsort(A)
print A
if __name__ == '__main__':
main()
# Merge sort: Time complexity: O(n log n):
# Quick Sort
import
random
def
quicksort( aList ):
_quicksort( aList,
0
,
len
( aList )
-
1
)
def
_quicksort( aList, first, last ):
if
first < last:
pivot
=
partition( aList, first, last )
_quicksort( aList, first, pivot
-
1
)
_quicksort( aList, pivot
+
1
, last )
def
partition( aList, first, last ) :
pivot
=
first
+
random.randrange( last
-
first
+
1
)
swap( aList, pivot, last )
for
i
in
range
( first, last ):
if
aList[i] <
=
aList[last]:
swap( aList, i, first )
first
+
=
1
swap( aList, first, last )
return
first
def
swap( A, x, y ):
A[x],A[y]
=
A[y],A[x]
Thursday, February 18, 2016
using import in class
class C:
os = __import__('sys')
def f(self):
print self.os.platform
c = C()
c.f()
Link:
http://stackoverflow.com/questions/6861487/importing-modules-inside-python-class
os = __import__('sys')
def f(self):
print self.os.platform
c = C()
c.f()
Link:
http://stackoverflow.com/questions/6861487/importing-modules-inside-python-class
Thursday, February 11, 2016
file read in one line
output = open('pagehead.section.htm','r').read() # Not recommended in script (use in interactive mode)
Note: The above expression does not close the file handle, which is bad. But all file handles
are closed when the script exits. In interactive mode (command from terminal), all resources are
freed when the interpreter exits.
http://stackoverflow.com/questions/17577137/do-files-get-closed-during-an-exception-exit
Subscribe to:
Posts (Atom)