Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

fork method

by manoj_speed (Prior)
on Mar 13, 2009 at 07:02 UTC ( #750331=perlquestion: print w/replies, xml ) Need Help??
manoj_speed has asked for the wisdom of the Perl Monks concerning the following question:

Hi friends

I declared a variable in my program. After that I used fork() method. In the child process I changed the value of the variable that I declared before using fork(). It does not affect the value in parent process. Is there any way to change the value of variable in child process that affects the value in parent process?

Replies are listed 'Best First'.
Re: fork method
by lakshmananindia (Chaplain) on Mar 13, 2009 at 07:12 UTC

    Its good to have a look at Sharing Variables between fork

    You can also try using IPC

    --Lakshmanan G.

    Your Attempt May Fail, But Never Fail To Make An Attempt

      Python ------ =========== def pnamedtuple(type_name, field_names, mutable=False): def show_listing(s): for aline,its_text in enumerate(s.split('\n'), 1): print(f' {aline: >3} {its_text.rstrip()}') # put your code here # bind class_definition (used below) to the string constructed for ++ the class if not str(type_name).isidentifier() or str(type_name).startswith( +"_") or type_name in keyword.kwlist or type(field_names) == set: raise(SyntaxError("Invalid Syntax !!!")) class_template = '''\ class {cname}(): def __init__(self, {myargs}, mutable={mutation}): {argvals} self._fields = [{fieldvals}] self._mutable = mutable def __repr__(self): return "{cname}({reprvals})" def __getitem__(self, x): if type(x) == int: if len(self._fields) > x: return eval('self.get_%s()' %(self._fields[x].replace("'",'')) +) elif type(x) == str: if x in self._fields: return eval('self.get_%s()' %(x)) raise(IndexError("Invalid Index !!!")) def __setattr__(self, name, value): if name == "_mutable" and "_mutable" in self.__dict__.keys(): self.__dict__[name] = value if "_mutable" in self.__dict__.keys() and self._mutable == False: raise(AttributeError("Attribute Error. Mutable flag is set !!!") +) self.__dict__[name] = value def __eq__(self, other): if self.__class__ != other.__class__: return False for k1, k2 in zip(self.__dict__.keys(), self.__dict__.keys()): if k1 not in ["_fields", "_mutable"]: try: if self.__dict__[k1] != other.__dict__[k2]: raise(AttributeError("Attribute Error. Value mismatch !!!" +)) except: return False return True def _replace(self, **kargs): import copy other = copy.copy(self) if self._mutable: myobj = self else: myobj = copy.copy(self) myobj._mutable = True for k in kargs.keys(): try: exec("if myobj.%s: myobj.%s = %s"%(k, k, kargs[k])) except: raise(TypeError("Attribute not found")) if self._mutable: return None return myobj if (self._mutable): for k in kargs.keys(): exec("self.%s = %s"%(k, kargs[k])) return(self) return(other) {accessordefs} ''' (myargs, arg_values, repr_values, accessordefs, fieldvals) =('', ' +', '', '', "'") if (type(field_names) == str): if field_names.find(",") >= 0: myargs = ",".join(field_names.split(",")) else: myargs = ",".join(field_names.split(" ")) else: myargs = ",".join(field_names) myargs = myargs.replace(" ", '') mutation = (False, True)[mutable] for myarg in myargs.split(","): if not str(myarg).isidentifier() or str(myarg).startswith("_") + or myarg in keyword.kwlist: raise(SyntaxError("Invalid Syntax !!!")) arg_values += "self."+myarg+" = "+myarg +"; " repr_values += myarg+"=\"+str(self."+myarg+")+\"," accessordefs += "def get_{name}(self): return self.{name}\n " +.format(name=myarg) fieldvals += myarg+"','" repr_values = repr_values[:-1] fieldvals = fieldvals[:-2] class_definition = \ class_template.format(cname=type_name, myargs=myargs, argvals=ar +g_values, reprvals=repr_values, accessordefs=accessordefs, fieldvals= +fieldvals, mutation=mutation) #show_listing(class_definition) # comment out # For initial debugging, remove comment to show the source code fo +r the class # show_listing(class_definition) # Execute the class_definition string in a local name space; later +, bind the # source_code name in its dictionary to the class_defintion; ret +urn the # class object created; if there is a syntax error, list the cla +ss and # also show the error name_space = dict(__name__ = f'pnamedtuple_{type_name}') try: exec(class_definition,name_space) name_space[type_name].source_code = class_definition except [TypeError, SyntaxError]: show_listing(class_definition) traceback.print_exc() return name_space[type_name] =========== class MyTestClass(): a=100 def __init__(self, funcname): exec("self.myval = funcname" .format(funcname=funcname)) exec("self.{name} = 100; self.get_{name} = lambda : 12345 " .fo +rmat(name=funcname)) #exec("""def mytest(self): print("MYTEST FUNCTION")""" .format( +self=self)) #exec("self.{name} = 100; get_{name} = lambda : self.{name}" .f +ormat(name=funcname)) self.keywords = ['False', 'None', 'True', 'and', 'as', 'assert' +, 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield'] print("Class object created.") #def __str__(self): # print("String method: ") # return "STRING: " def checkKeyword(self, keyword): match = 'not' if keyword in self.keywords: match = '' print("Given word [%s] is %s a keyword." %(keyword, match)) def checkIdentifier(self, name): ## Use try / except to check NameError or SyntaxError isValid = False if str(name).isidentifier() and not str(name).startswith("_") +and name not in self.keywords: print("Valid identifier [%s]" %(name)) else: print("Invalid identifier [%s]" %(name)) def myfunc(self): print("This if myfunc() function.") def __del__(self): print("Destructor called.") def get_a(self): return self.a def __getitem__(self): print("Calling getitem() method") def __repr__(self): print("MyClass(1,2,3)") return("rep method") myobj = MyTestClass('myval') myobj.myfunc() #print(myobj) #str(myobj, "Welcome", "welcome") myobj.checkIdentifier('if') myobj.checkIdentifier('abcd') myobj.checkIdentifier('10') myobj.checkIdentifier('_abcd') print(myobj.a) print(myobj.get_a()) print(dir(myobj)) print(myobj.get_myval()) repr(myobj) #-->Triple1(a=1,b=2,c=3) myobj.a #print(myobj[a]) #print(myobj.mytest1()) #print(myobj.get_mynum()) #print(getattr(myobj, a)) #print(myobj.getattr(a)) del(myobj) String -> length, List -> length, sort, reverse, replace, min, max, count, index, data s +tructure Tuple -> type conversion Dictionary -> Set -> | & - ^ List Comprehension -> range Looping techniques -> enumerate, zip Lambda expressions Files and directories Errors and Exceptions Iterators, Generators, Decorators Regular Expressions Internet Access Sending Email XML and JSON Processing Databases Networking (Threads) GUI Programming Performance Monitoring (pylint, inspect) Testing tools (unittest) Command line argument and options (-cmvh, sys.argv, sys.path) Python standard library Debugging & Misc functions Modules and Packages Classes and OOPS Django Framework Static Method & Implicit properties, This methods are visible inside classes. It can't be accessed via obje +cts. class MyClass(): def __init__(self, myvar): self.a=myvar self.__b=2000 @property def a(self): return(self.__a) @a.setter def a(self, x): self.__a = x #self.a=200 """ def myStaticMethod(a): print("This is static method. ", a) def myNormalMethod(self, a): print("This is static method. ", a) myobj = MyClass() MyClass.myStaticMethod(200) myobj.myNormalMethod(100) """ myobj=MyClass(100) print(myobj.a) #print(myobj.b) myobj.a=1000 print(myobj.a) # Implicit properties class MyClass(): def __init__(self,x): self.__my_attribute = x def getAttribute(self): return self.__my_attribute def setAttribute(self, x): self.__my_attribute = x @property def my_attribute(self): return self.__my_attribute @my_attribute.setter def my_attribute(self, value): self.__my_attribute = value myobj = MyClass(100) #print(myobj.__my_attribute) print(myobj.getAttribute()) myobj.setAttribute(1000) print(myobj.getAttribute()) myobj1 = MyClass(200) print(myobj1.my_attribute) myobj1.my_attribute=2000 print(myobj1.my_attribute) REF: REF: +c_method_and_class_method_instance_method.php Profiling, cProfile and profile provide deterministic profiling of Python program +s. A profile is a set of statistics that describes how often and for how +long various parts of the program executed. These statistics can be formatted into reports via the pstats module. import cProfile import os'os.getcwd()') > python -m cProfile REF: +rofile-concrete-case-carbonara Struct, This module performs conversions between Python values and C structs r +epresented as Python bytes objects. This can be used in handling binary data stored in files or from netwo +rk connections, among other sources. It uses Format Strings as compact descriptions of the layout of the C +structs and the intended conversion to/from Python values. mypack = struct.pack('ifiii', 10, 20.5,30,40,50) struct.unpack('ifiii', mypack) Queue, import queue myqueue = queue.Queue() # myqueue = queue.Lifoqueue(5) for i in range(1,10): myqueue.put(i) print(myqueue) print(myqueue.size()) while not myqueue.empty(): print(myqueue.get()) Distributing modules, MyModule/ # Math module def add(x,y): return x + y def sub(x,y): return x - y def mul(x,y): return x * y from distutils.core import setup setup(name="mymath", version="1.0", description="This is mymath module.", author='Manojkumar R', author_email='', url='', py_modules=["mymath"]) > python sdist Copy the tar file and extract it to some path, mymath-1.0.tar\dist\mymath-1.0\mymath-1.0> python install --p +refix="C:\<USER>\Desktop\MySetup" >>> sys.path.append('C:\\<USER>\\Desktop\\MySetup\\lib\\site-packages' +) >>> import mymath Regex match, search, sub Patterns -> ^ $ . [abcA-E], {}, | (), +, *, ?, \wsd, backref [phone +, date, time] Modifiers -> re.I, re.S Internet Access from urllib.request import urlopen with urlopen('') as respons +e: #with urlopen('') as response: for line in response: line = line.decode('utf-8') # Decoding the binary data to text. if 'EST' in line or 'EDT' in line or ' UTC' in line: # look for + Eastern Time print(line) Sending Mail, import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText ''' server = smtplib.SMTP('', 587) server.starttls() server.login(username, pass) msg = "Hi,\n\nThis is test email from python program.\n\nRegards,\nMan +oj" server.sendmail("from", "to", msg) server.quit() ''' from_addr = 'from' to_addr = 'to' text = "Hi,\n\nThis is test email from python program.\n\nRegards,\nMa +noj" msg = MIMEMultipart() msg['From'] = 'from' msg['To'] = 'to' msg['Subject'] = 'Test Mail [Python]' msg.attach(MIMEText(text)) server = smtplib.SMTP('') server.ehlo() server.starttls() server.ehlo() server.login('user', 'pass') server.sendmail(from_addr,to_addr,msg.as_string()) server.quit() print "Mail Sent successfully." Module, """ This is my Sample module """ MYVAL=10 def myfunc(): print("This is my function") print("Namespace: ", __name__) if __name__ == '__main__': print("Main Function") print "The value of MYVAL is ", MYVAL myfunc() ------------ import sample #from sample import myfunc MYVAL=100 sample.myfunc() print("MYVAL : ", MYVAL) print("Module MYVAL : ", sample.MYVAL) print(sample.__doc__) XML, <student_collection shelf="Student Details"> <student title="Record1"> <regno>1001</regno> <name>Ramesh</name> <dept>CSC</dept> </student> <student title="Record2"> <regno>1002</regno> <name>Suresh</name> <dept>Mech</dept> </student> <student title="Record3"> <regno>1003</regno> <name>Aakash</name> <dept>CSC</dept> </student> </student_collection> import xml.etree.ElementTree as ETree tree = ETree.parse(r"Sample.xml") root=tree.getroot() #print("Collection: ", root.tag, "\nName: ", root.attrib) print(, '*')) print(root.attrib['shelf']) for child in root: print("\n", child.tag, child.attrib['title']) print("\tRegno: ", child[0].text) print("\tName : ", child[1].text) print("\tDept : ", child[2].text) JSON, { "id": "0001", "dept": "CSC", "name": "abc", "marks": { "Maths": 98, "Science": 90, "English": 95 } } import json json_data = json.load(open('Sample.json')) data = { 'name' : 'ABC', 'id' : 1001, 'dept': 'CSC' } json_str=json.dumps(data) print(type(json_str), json_str) json_data=json.loads(json_str) print(type(json_data), json_data) Networking, Server: import socket # Import socket module s = socket.socket() # Create a socket object host = socket.gethostname() # Get local machine name port = 12345 # Reserve a port for your service. s.bind((host, port)) # Bind to the port s.listen(5) # Now wait for client connection. while True: c, addr = s.accept() # Establish connection with client. print 'Connection received from from', addr c.send('Welcome to the Server.\nThank you for connecting...') c.close() # Close the connection Client: import socket # Import socket module s = socket.socket() # Create a socket object host = socket.gethostname() # Get local machine name port = 12345 # Reserve a port for your service. s.connect((host, port)) print s.recv(1024) s.close # Close the socket when done Threading, import thread import time, os # Define a function for the thread def print_time( threadName, delay): count = 0 while count < 5: time.sleep(delay) count += 1 print "%s: %s [%d]" % ( threadName, time.ctime(time.time()), os. +getpid() ) # Create two threads as follows try: print("Program started [%d]", os.getpid()) thread.start_new_thread( print_time, ("Thread-1", 2, ) ) thread.start_new_thread( print_time, ("Thread-2", 4, ) ) except: print "Error: unable to start thread" #while 1: time.sleep(30) print("Processing Completed") Forking, import os def child(): print('\nA new child ', os.getpid()) os._exit(0) while True: newpid = os.fork() if newpid == 0: child() else: pids = (os.getpid(), newpid) print("parent: %d, child: %d\n" % pids) reply = input("q for quit / c for new fork") if reply == 'c': continue else: break GUI Program, import Tkinter import tkMessageBox top = Tkinter.Tk() """ ## Message box with info message def helloCallBack(): tkMessageBox.showinfo( "Hello Python", "Hello World") B = Tkinter.Button(top, text ="Hello")#, command = helloCallBack) B.pack() """ label = Tkinter.Message(top, text = "Welcome\n\t\tto\n python") label.pack() top.mainloop() Databases, import MySQLdb # Open database connection db = MySQLdb.connect("localhost", "testuser", "password", "testdb" ) # prepare a cursor object using cursor() method cursor = db.cursor() # execute SQL query using execute() method. sql_query="create table student(id int, name varchar(20), dept varchar +(20))" #sql_query="insert into student(id, name, dept) values(1001, 'abc', 'c +sc')" #sql_query="insert into student(id, name, dept) values(1002, 'def', 'm +ech')" #sql_query="update student set dept='eee' where id=1002" #sql_query="delete from student where id=1002" cursor.execute(sql_query) db.commit() # Fetch a single row using fetchone() method. """ sql="select * from student" #data = cursor.fetchone() data_set = cursor.fetchall() for data in data_set: print(data) """ db.close() Performance, pylint -> To check the coding standard practices in code(PEP8) and det +ect errors. > pip install pylint > pylint """welcome to python""" import os MYVAR = 10 print "welcome to python" print "the value of a is: %d " %(MYVAR) print "Name: ", os.getcwd() Inspect -> Provides several useful functions to help get information a +bout live objects such as modules, classes, methods, functions, trace +backs, frame objects, and code objects. import os def myfunc(): print("welcome") inspect.getdoc(os) inspect.getfile(os) inspect.getmodule(os) # sys inspect.isfunction(myfunc) inspect.isfunction(os.getcwd) inspect.isbuiltin(os.getcwd) Unittest -> Framework to test/check the behaviour of functions, method +s and classes import unittest myflag=False myvar=30 def add(x,y): return x + y class SimpleTest(unittest.TestCase): def testadd1(self): self.assertEqual(add(4,5),9) def testadd_test2(self): self.assertNotEqual(add(5,5), 12) self.assertEqual(add(5,5), 10) def testadd_test3(self): self.assertTrue(myvar) self.assertFalse(myflag) self.assertIn('p', 'python', "Match pattern not found.") if __name__ == '__main__': unittest.main() Python standard library, system, os, Date, Time, Math import calendar calendar.calendar(2017) calendar.month(2017, 5) import time time.time(); time.gmtime(); time.sleep(5) time.localtime(time.time()) -> [0] time.strftime("%Y/%m/%d %H:%M:%S", time.localtime()) Debugging & Misc functions alarm, sleep and exit hostname and login user Getting hostname and ip address socket.gethostname() | socket.gethostbyaddr(socket.gethostname()) socket.gethostbyname(socket.gethostname()) getpass.getuser() Socket functions Executing system commands (dir|date|time \T) Debugger > python -m pdb Errors and Exceptions try: a= 10 + 20 raise NameError("This is raised Name Error") except NameError as err: print("Caught NameError Exception: ", err) except TypeError: print("Caught TypeError Exception: ", sys.exc_info()[1]) except: print("Caught Other Exception: ", sys.exc_info()[1]) finally: print("Addition success") print("Program completed") Monkey patching -> +hon/ Monkey-patching is the technique of swapping functions or methods with + others in order to change a module, library or class behavior. class MyClass(): def add(self, x, y): return x + y obj = MyClass() obj.add(10,20) def multiple(self, x, y): return x * y MyClass.add = multiple obj.add(10,20) Function -> variable length args def myfunc(a,b, c, d, e ): print(a,b,c,d, e) myfunc(10,20, [30, 40, 50], **{'d':100, 'e': 200}) myfunc(10,20, *[30, 40, 50] Executing module as script, def myfunc(a,b, c, d, e ): print(a,b,c,d, e) myfunc(*range(50,260, 50)) if __name__ == "__main__": myfunc(*range(10, 60, 10)) import sample # sys.path sample.myfunc(*range(100,600,100)) Iterators: Iterator method is used to iterate elements from object as one by on +e. mylist=[10,20,30] for i in mylist: print i it = iter(mylist) ## In 3.x, next(it) Generators: Generators are used to generate iterators. We can use yield method t +o generate iterators. Normal function return value and yield return. +It internally maintain refernces. def mygenerator(): print "Welcome" for i in (10,20): yield i it=iter(mygenerator()) def mygenerator(): a= yield yield a * 2 it=iter(mygenerator()) it.send(20) 3.0, def myGenerator(): for i in range(10,50, 10): yield(i) it=myGenerator() next(it) Decorators: Decorator provide a very useful method to add functionality to exist +ing functions and classes. Decorators are functions that wrap other f +unctions or classes. Ex: def math(func, x, y): print "Math Function" func(x,y) def add(a,b): print "This is addition function" print a + b def mul(a,b): print "This is subtraction function" print a * b math(add, 10,20) math(mul,10,20) import time def my_decorator(myfunc): def my_wrapper(): t1 = time.time() print("Function started ", myfunc.__name__) myfunc() print("Function ended ", myfunc.__name__) t2 = time.time() print("Time taken to execution function: ", int((t2 - t1))) return my_wrapper @my_decorator def myfunc(): print("This is my function") time.sleep(2) myfunc() def my_decorator(func): def wrapper(*args, **kwargs): print("Before call") result = func(*args, **kwargs) print("After call") return result return wrapper @my_decorator def add(x, y, z): print(x, y, z) add(100, 200, 300) """ def our_decorator(func): def function_wrapper(x): print("Before calling " + func.__name__) res = func(x) print(res) print("After calling " + func.__name__) return function_wrapper @our_decorator def succ(n): return n + 10 print(succ(100)) """ Classes, PDF, IMAGE Date, Time, Cal Data Compression -> zlib, gzip Performance Measurement -> from timeit import Timer Testing -> unittest Pretty print -> pprint, textwrap Logging -> logging Pickling and Shelving Config Parse -> configparser Threading & Multiprocessing IPC and Networking -> socket, signal GUI with Tk Debugger -> pdb Project: Bank ATM machine Project Design a program to do the functionalities of Bank ATM machine. Requirements, - Display "Welcome Screen" with menu options 1. Account Summary 2. Withdraw Amount3. Deposit Amount 4. Mini Statement [optional] 5. Transfer Amount [optional] 6. Exit - For any option, get the card number and pin details and do the auth +entication [first time authentication] Additional, - Send SMS to registered mobile number - Send mails to registered email-id if any transaction amount is abov +e Rs. 10000 - For 5 invalid login attempt, block the card/account type() - get the data type for a variable id() - get the memory address of an object with >>> mynum = input('Enter the number: ') Enter the number: 10 + 20 >>> mynum 30 >>> type(mynum) <type 'int'> >>> >>> mystr = input('Enter the string: ') Enter the string: 'welcome ' + 'to Python' >>> mystr 'welcome to Python' >>> type(mystr) <type 'str'> >>> mynum = raw_input('Enter the number: ') Enter the number: 10 + 20 >>> mynum '10 + 20' >>> type(mynum) <type 'str'> >>> >>> >>> mynum = eval(raw_input('Enter the number: ')) Enter the number: 10 + 20 >>> mynum 30 >>> type(mynum) <type 'int'> .py - Regular scripts .py3 - (rarely used) Python3 script. Python3 scripts usually end with +".py" not ".py3", but I have seen that a few times .pyc - compiled script (Bytecode) .pyo - optimized pyc file (As of Python3.5, Python will only use pyc r +ather than pyo and pyc) .pyw - Python script for Windows. It is executed with pythonw.exe .pyx - Cython src to be converted to C/C++ .pyd - Python script made as a Windows DLL .pxd - Cython script which is equivalent to a C/C++ header .pxi - MyPy stub .pyi - Stub file (PEP 484) .pyz - Python script archive (PEP 441); this is a script containing co +mpressed Python scripts (ZIP) in binary form after the standard Pytho +n script header .pywz - Python script archive for MS-Windows (PEP 441); this is a scri +pt containing compressed Python scripts (ZIP) in binary form after th +e standard Python script header .py[cod] - wildcard notation in ".gitignore" that means the file may b +e ".pyc", ".pyo", or ".pyd". Simple syntax Easy/Quick to develop Fewer lines of code Reduce development time Dynamic data type allocation Ask about them - Introduction - Collegue, Any doubt stop, OS and Intro - As of 03/10/2017, (Rent Rs. 8700/2 -> 4350) EB bill: Rs. 265 Total: Rs. 265 => Rs. 132 For FSaravana, Remaining + 2 months rent + others Rs. 34538 + Rs. 4350 + Rs. 132 => Rs. 39020 As of 03/11/2017, (Rent Rs. 4300/2 -> 2150) For Saravana, Remaining + 1 month rent + others Rs. 34020 + Rs. 2150 + Rs. 0 => Rs. 36170 Rent/Owner Account, Bank Name: INDIAN OVERSEAS BANK IFSC Code: IOBA0000098 Account Number: 009801000046497 Electricity Bill, Consumer No: 092370211213 Name: A.MOHAMEDALI KAMARUNISSA Region: 09-Chennai-South Circle: 400-South1 We need to get the remaining EB amount (Rs. 170) from neighbor for pre +vious bills. EB Bill, 07/07, Total: 380 Units -> Rs. 770 (Per Unit Rs. 2.02) Ours : 180 Units -> Rs. 365 Rem : 200 Units -> Rs. 405 08/09, Total: 300 Units -> Rs. 530 (Per Unit Rs. 1.76) Ours : 150 Units -> Rs. 265 Rem : 150 Units -> Rs. 265 For them, Rs. 670 (last 4 months) +ould-not-kept-fridge-017611.html

      import sys, os, time import logging import psutil pid_file = 'test.PID' block_file = 'test.BLOCKED' def createPidFile(filename): mypid = os.getpid() print("Current process ID: {0}".format(mypid)) fp = open(pid_file, 'w') fp.write(str(mypid)) fp.close() def checkFileExist(filename): if os.path.isfile(filename): print("File [{0}] is exist." .format(filename)) return True print("File [{0}] does not exist." .format(filename)) return False def readPidFile(filename): fp = open(filename, 'r') data = fp.readline() print('PID: {0}' .format(int(data))) return int(data) def checkRunning(): if checkFileExist(pid_file): pid = readPidFile(pid_file) if checkPidExist(pid): print("Process is already running...") sys.exit(0) else: createPidFile(pid_file) print("PID file created successfully...") else: print("Process is not running.") def checkPidExist(pid): if psutil.pid_exists(pid): print("PID [{0}] is running." .format(pid)) return True print("PID [{0}]does not exist." .format(pid)) checkRunning() skip_day = '17,18,20' run_week = 'Mon,Tue,Wed,Thu,Fri' start_time = '17:30' end_time = '19:20' def checkRunTime(): (week_day, day, hour, min) = time.strftime('%a:%d:%H:%M', time.local +time()).split(':') (hour, min) = (int(hour), int(min)) print("Current Day and Time: {0} {1} - {2}:{3}" .format(day, week_d +ay, hour, min)) if skip_day: skip_days = skip_day.split(',') print('Skip Days: ',skip_days) if day in skip_days: return False run_weeks = run_week.split(',') (start_hr, start_min) = [int(x) for x in start_time.split(':')] (end_hr, end_min) = [int(x) for x in end_time.split(':')] print('Run Days : ',run_weeks) print('Run Time : {0}:{1} - {2}:{3}' .format(start_hr, start_min, e +nd_hr, end_min)) #print(day, run_weeks, hour, start_hr, end_hr, min, start_min, end_ +min) if (week_day in run_weeks) and (hour >= start_hr and hour <= end_hr) +: if ((hour == start_hr and min < start_min) or (hour == end_hr and +min > end_min)): return False return True return False if not checkRunTime(): print("Runtime does not match for current run...") sys.exit(0) print("Runtime matched for current run...") while True: if checkFileExist(block_file): print('BLOCK file exist.') time.sleep(2) continue print("Inside Loop [{0}]" .format(os.getpid())) time.sleep(5)
      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, p +rocess 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 ?m 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': [(9 +6, 6), (110, 9)]}, #{'name': 'MONTHLY VOLUMES', 'table': 'monthly_volumes', 'range': [(11 +6, 1), (164, 4)]}, #{'name': 'MONTHLY GAINERS', 'table': 'monthly_gainers', 'range': [(11 +6, 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("","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 fro +m __next__ or __prev__ def __str__(self): return '_all={}, _index={}'.format(self._all,self._ind +ex) 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 m +ore 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'): # i +s 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, +n-visualisation-r-python/tutorial-data-manipulation-numpy-pandas-pyth +on/tutorial/ IOT, +7/02/17/iot-with-python-essential-packages sdcard, +er.html +-card +-re-mount-e-g-deleted-files-re-appear

      Thanks for the reply.

      2017-09-16 Athanasius changed <pre> to <code> tags, etc.

      -- The wisest mind has something yet to learn.
Re: fork method
by vinoth.ree (Monsignor) on Mar 13, 2009 at 07:07 UTC

    I think you can use the vfork system call.

      Have a look at lakshmananindia's post
      Dear friend,

      This method is not working.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://750331]
Approved by olus
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2018-02-21 05:56 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (275 votes). Check out past polls.