a=500 # Simple function def myfunc(): #global a #a=100 print "This is myfunc() function." #print "The value of a is ", a # Function with args def add(a,b): print "Going to add two numbers: ", a, b return a + b # Function with default args def add1(a, b=50): print "Going to add two numbers: ", a, b return a + b # Function with variable length args def myfunc1(a,b,c): print "Calling myfunc1()" print a,b,c # Function with variable length args def myfunc2(**a): print "Calling myfunc2()" print a #myfunc() #add(10,20) #add(b=200, a=100) #add1(100) #add1(1000,2000) mylist=[10,20,30] #myfunc1(*mylist) mydict={'a': 100, 'b': 200, 'c': 300} #myfunc1(**mydict) #myfunc2(mylist) ===================================================== OS, Interface between hardware and user. Basic need is to do read/write operation on HW. (storage processing) Tasks, Memory management - Main memory / virtual memory Process management - processor, processes, threads, cpu scheduling, process synchronize, dead locks, etc. File management - file/folder directory structure. Device management - attach/detach device, plug-play Security - User / Group permission, patches Disk Management System performance Layers, User1, User2 Software -> OS -> System software, Application software HW -> CPU, RAM, IO Diff types of OS: Windows Unix / Linux (RHEL, Solaris, Debian, Ubuntu, HP-UX, AIX) Mac / ios Andriod Diagram, (unix) HW -> Kernel -> Shell -> Apps, Compiler, Commands, etc Boot Process BIOS MBR (boot loader) Boot loader (GRUB / LILO) Kernel Operating System Process (init) Diff Languages ??? Why Python ? 1985 - 1990 -> Guido van Rossum Open source -> GPL License (high level programming language) Latest version -> 2.7.13, 3.6.2 Interpreted language (Compiled vs Interpreted), PVM Who Uses?? Google -> web search Youtue -> Video sharing Dropox bittorrent NSA -> National Search Agency NASA -> Scienttific program irobot Company -> intel, cisco, HP, Ibm, seagate and etc What can we do? System programming -> automation, report generation, monitoring, etc GUI Web programming Network / Internet Programming Component Integration Database Programming Numeric & Scientific Program Gaming & Robotic Data Mining / Analytics & Data science ----------------------------------------------- import sys, os, time, re import xlrd import logging import MySQLdb filename = '/home/manoj/TechTrend/Dashboard.xls' (db_connect, db_cursor) = ('', '') dashboardDetail = [ {'name': 'Higher Volumes', 'table': 'higher_volumes', 'range': [(2, 1), (22, 5)], 'columns': ('ttq_daily', 'symbol', 'ltp', 'perc_chg', 'mn_ttq_perc')} #{'name': '15 Curr Volume Breakers', 'table': 'curr_volume_breakers', 'range': [(26, 1), (46, 6)]}, #{'name': 'SIGNALS IN MOMENTUM STOCKS', 'table': 'signals_in_momentum_stocks', 'range': [(51, 1), (71, 6)]}, #{'name': 'NEAR MONTH HIGH', 'table': 'near_month_high', 'range': [(76, 1), (90, 3)]}, #{'name': 'NEAR MONTH LOW', 'table': 'near_month_low', 'range': [(76, 5), (90, 7)]}, #{'name': 'GAINERS OF THE DAY', 'table': 'gainers_of_day', 'range': [(96, 0), (110, 4)]}, #{'name': 'LOOSERS OF THE DAY', 'table': 'losers_of_day', 'range': [(96, 6), (110, 9)]}, #{'name': 'MONTHLY VOLUMES', 'table': 'monthly_volumes', 'range': [(116, 1), (164, 4)]}, #{'name': 'MONTHLY GAINERS', 'table': 'monthly_gainers', 'range': [(116, 6), (141, 9)]}, #{'name': 'MONTHLY LOOSERS', 'table': 'monthly_losers', 'range': [(145, 6), (164, 9)]} ] def main(): checkFileExist(filename) openDbConnection() processExcelData() sys.exit(0) def processExcelData(): global db_connect, db_cursor try: workbook = xlrd.open_workbook(filename) print "No of sheets in workbook", workbook.nsheets worksheet = workbook.sheet_by_index(0) print "Total No. of rows: ", worksheet.nrows for dashboard in dashboardDetail: table = dashboard['table'] print "\nName: ", dashboard['name'] print "Table: ", table print "Range: ", dashboard['range'] (srow, scol) = (dashboard['range'][0][0], dashboard['range'][0][1]) (erow, ecol) = (dashboard['range'][1][0], dashboard['range'][1][1]) query="insert into %s(%s) values " %(table, ', '.join(dashboard['columns'])) print "QUERY: ", query for i in range(srow, erow): #print "\nROW [%d] : " %(i), k=0 query += '(' for j in range(scol-1, ecol): value = worksheet.cell(i, j).value query += "'{0}'," .format(value) query = re.sub(',$', '), ', query) query = re.sub(', $', '', query) print "QUERY: ", query last_updated_time = executeQuery("select max(created_time) from %s where active='Y'" %(table), True) update_query = "update %s set active='N' where active = 'Y' and created_time <= '%s'" %(table, last_updated_time) print "QUERY: ", update_query executeQuery(update_query) executeQuery(query) #print "[HEADER] -> ", worksheet.row_values(0) #for row in range(1, worksheet.nrows): # print "[DATA] -> ", worksheet.row_values(row) except: print "Exception caught", sys.exc_info()[1] db_connect.commit() db_connect.close() print "Database connection closed successfully." def executeQuery(query, Return=False): global db_connect, db_cursor print "Query: ", query #return try: db_cursor.execute(query) if Return: data = db_cursor.fetchone() print "DATA: ", data return data[0] except: print "Exception caught.", sys.exc_info()[1] def openDbConnection(): global db_connect, db_cursor try: db_connect = MySQLdb.connect("trendz.com","trend_d","dat17","trend_d" ) ## CHANGE MYSQL ## print "Database connection established successfully." db_cursor = db_connect.cursor() print "Cursor object created successfully." except: print "Unable to establish connection with database. Error: ", sys.exc_info()[1] sys.exit(0) ## To check the existence of file def checkFileExist(filename): if not os.path.isfile(filename): print "File does not exist. [%s]" %(filename) sys.exit(0) main() --------------------------------- def running_count(iterable,p): match_count = 0 it = iter(iterable) while True: try: if p(next(it)): match_count += 1 yield(match_count) except StopIteration: return def stop_when(iterable,p): it = iter(iterable) while True: try: myval = next(it) if p(myval): return yield(myval) except StopIteration: return def yield_and_skip(iterable): it = iter(iterable) while True: try: myval = next(it) if str(myval).isdigit(): [next(it) for i in range(myval)] yield(myval) except StopIteration: return def windows(iterable,m,n=1): it = iter(iterable) tmplist = [] while True: try: if len(tmplist) > 0: tmplist = tmplist[n:m] tmplist.extend([next(it) for i in range(len(tmplist), m)]) yield(tmplist) except StopIteration: return def alternate(*iterables): iterables = [iter(it) for it in iterables] while True: for it in iterables: try: yield(next(it)) except StopIteration: return def myzip(*iterables): iterables = [iter(it) for it in iterables] while True: tmplist = [] for it in iterables: try: tmplist.append(next(it)) except StopIteration: tmplist.append(None) if all(v is None for v in tmplist): return yield(tmplist) class Backwardable: def __init__(self,iterable): self._iterable = iterable def __iter__(self): class B_iter: def __init__(self,iterable): self._all = [] self._iterator = iter(iterable) self._index = -1 # index of just returned value from __next__ or __prev__ def __str__(self): return '_all={}, _index={}'.format(self._all,self._index) def __next__(self): if len(self._all) == 0: while True: try: self._all.append(next(self._iterator)) except: break ''' if len(self._all) == self._index +1: try: self._index +=1 self._all.append(next(self._iterator)) except : self._index-=1 raise else: self._index+=1 return self._all[self._index] ''' if self._index < len(self._all) -1 : self._index += 1 return self._all[self._index] raise(StopIteration("Stop the iteration. No more next values !!")) def __prev__(self): #print(self._index) if self._index <= 0: raise(AssertionError("Assertion error caught. No more previous values !!")) self._index -= 1 return self._all[self._index] ''' self._index -=1 try: assert self._index> -1, 'No more previous value.' except AssertionError: self._index+=1 raise return self._all[self._index] ''' return B_iter(self._iterable) def prev(x): return x.__prev__() if __name__ == '__main__': # Test running_count; you can add your own test cases print('\nTesting running_count') for i in running_count('bananastand',lambda x : x in 'aeiou'): # is vowel print(i,end=' ') print() for i in running_count(hide('bananastand'),lambda x : x in 'aeiou'): # is vowel print(i,end=' ') print() print(nth(running_count(primes(),lambda x : x%10 == 3),1000)) # Test stop_when; you can add your own test cases print('\nTesting stop_when') for c in stop_when('abcdefghijk', lambda x : x >='d'): print(c,end='') print() for c in stop_when(hide('abcdefghijk'), lambda x : x >='d'): print(c,end='') print('\n') print(nth(stop_when(primes(),lambda x : x > 100000),100)) # Test group_when; you can add your own test cases print('\nTesting yield_and_skip') for i in yield_and_skip([1, 2, 1, 3, 'a', 'b', 2, 5, 'c', 1, 2, 3, 8, 'x', 'y', 'z', 2]): print(i,end=' ') print() for i in yield_and_skip(hide([1, 2, 1, 3, 'a', 'b', 2, 5, 'c', 1, 2, 3, 8, 'x', 'y', 'z', 2])): print(i,end=' ') print() print(nth(yield_and_skip(primes()),5)) # Test windows; you can add your own test cases print('\nTesting windows') for i in windows('abcdefghijk',4,2): print(i,end=' ') print() print('\nTesting windows on hidden') for i in windows(hide('abcdefghijk'),3,2): print(i,end=' ') print() print(nth(windows(primes(),10,5),20)) # Test alternate; add your own test cases print('\nTesting alternate') for i in alternate('abcde','fg','hijk'): print(i,end='') print() for i in alternate(hide('abcde'), hide('fg'),hide('hijk')): print(i,end='') print() for i in alternate(primes(20), hide('fghi'),hide('jk')): print(i,end='') print() print(nth(alternate(primes(),primes()),50)) # Test myzip; add your own test cases print('\nTesting myzip') for i in myzip('abcde','fg','hijk'): print(i,end='') print() for i in myzip(hide('abcde'), hide('fg'),hide('hijk')): print(i,end='') print() for i in myzip(primes(20), hide('fghi'),hide('jk')): print(i,end='') print('\n') print(nth(myzip(primes(),primes()),50)) # Test Backwardable; add your own test cases print('\nTesting Backwardable') s = 'abcde' i = iter(Backwardable(s)) print(i) print(next(i),i) #a print(next(i),i) #b print(next(i),i) #c print(prev(i),i) #b print(prev(i),i) #a try: print(prev(i),i) except AssertionError: print('Tried to prev before first value') print(next(i),i) #b print(next(i),i) #c print(next(i),i) #d print(next(i),i) #d try: print(next(i),i) except StopIteration: print('Correctly raised StopIteration') ------------------------------------ Python Pandas and Numpy, https://www.hackerearth.com/practice/machine-learning/data-manipulation-visualisation-r-python/tutorial-data-manipulation-numpy-pandas-python/tutorial/ IOT, https://www.element14.com/community/groups/internet-of-things/blog/2017/02/17/iot-with-python-essential-packages sdcard, http://www.toontricks.com/2017/07/ubuntu-changes-to-sd-card-undone-after.html https://superuser.com/questions/795034/format-a-protected-corrupted-sd-card https://ubuntuforums.org/showthread.php?t=1985599 https://askubuntu.com/questions/262892/changes-to-sd-card-undone-after-re-mount-e-g-deleted-files-re-appear