Posts

Showing posts from December, 2021

Create a Large file for Test Data Processing using Python

  Code Example # Create a Large file for Test Data Processing This below code will create 1.1GB .txt file import string from random import choice with open("large_data.txt", "w") as fp:     i = 1     while i <= 1000000:         word = (''.join(                 choice(string.ascii_uppercase + string.digits) for _ in range(10)) + "|"                ) * 100         fp.write(word[:] + "\n")         i += 1

Write a Callback Function in Python

  Code Example #callback function def callback ( a, b ): print ( 'Sum = {0}' . format (a+b)) def main ( a,b,f= None ): print ( 'Add any two digits.' ) if f is not None : f(a,b) main( 1 , 2 , callback)  

Multiprocessing

  Code Example # Multiprocessing # Way 1 from multiprocessing import Pool import time def f(x, y): for i in range(20): print(i, end="#") time.sleep(2) pool = Pool(processes=10) for i in range(10): pool.apply_async(f, [10, i])

Write a Logic to Validate only this partten (()), ((), )(

  Code Example #Write a Logic to Validate only this partten  (()), ((), )(   user_input = input("enter here :") stack = [i for i in user_input] for j,i in enumerate(user_input):     if i == ')':         if stack.index(')') > stack.index('('):             stack.remove('(')             stack.remove(')') if ')' in stack or '(' in stack:     print("no") else:     print("yes")    

Print Execution Time Using Custom Decorator

  Code Example # Print Execution Time Using Custom Decorator  from datetime import datetime def print_time(fun):     def wrapper_method(*args, **kwargs):         start_time = datetime.now()         res = fun(*args, **kwargs)         end_time = datetime.now()         print("execution time : ", end_time-start_time)         print(res)     return wrapper_method @print_time def my_function1(goto=10):    # with params     for i in range(goto):         i = i**1000     return "200" @print_time def my_function2():           # without params     for i in range(10):         i = i**1000 my_function1(1000) my_function2()

How to solve the Liner Equation using Python

  Code Example # How to solve the Liner Equation using Python Example 1: [with Multiple variable] import numpy as np '''     8x+3y−2z=9     −4x+7y+5z=15     3x+4y−12z=35 ''' A = np.array([[8, 3, -2], [-4, 7, 5], [3, 4, -12]]) b = np.array([9, 15, 35]) answer = np.linalg.solve(A, b) print(answer)