The list is not in order and continue adding the list based on need.
The code indention format for this post is 4 spaces.
There's no fixed format for the code, it might been working code or might just been a placeholder for example purposes.
The code in this list should only do one thing, one.
value_1 if condition else value_21stcommand = ['ls ', '-l']
2ndcommand = ['egrep', 'log']
output = subprocess.Popen(1stcommand, stdout=subprocess.PIPE, env=EBG_ENV)
output = subprocess.run(2ndcommand, stdin=pat_info.stdout, stdout=subprocess.PIPE)
#add parameter shell=True if wildcard not working
#only Popen stdout process can be pass as stdindef openingfile(patlist):
with open(patlist, 'r') as listfile:
for pattern in listfile.readlines():
yield pattern
#calling the function
for i in openingfile('list.file'):
print(i)dict = {}
#adding value
dict['name'] = fitri
#get value
dict.get('name')text = " hello "
#remove left trailing char
text.lstrip()
#remove right trailing char
text.rstrip()
#remove all trailing char
text.strip()
#strip custom char, must be from either end (can be applied in both rstrip and lstrip)
text.strip('o ')import argparse
#create parser object
parser = argparse.ArgumentParser()
#add argument based on positional
parser.add_argument('filename')
#parsing back the arg
print(parser.parse_args().filename)import csv
#reading the csv file
with open('file.csv','r') as csvfile:
readcsv = csv.reader(csvfile)
for row in readcsv:
print(row)
#writing csv file
with open('file.csv','w') as csvfile:
writecsv = csv.writer(csvfile)
writecsv.writerow(['one', 'two'])sample = {
"data":"existing"
}
#using format dictionary[key] = value
sample["latest"] = "new"sample = {
"data":"existing"
}
#using if else and builtin method keys()
if "data" in sample.keys():
return Truedatabase = {
'user':'fitri',
'id': '707'
}
#return None (or any value) if key not found in database.
database.get('fatin', None)def generatorfunction():
x = yield x
print (x)
>>> hi = generatorfunction()
>>> hi.__next__()
>>> hi.send('Fitri')
>>> Fitri
#generator need to be started first with __next__ before proceeding sending the datatypes.def generatorfirst(n):
for i in range(n):
yield i
def generatorsecond(n):
yield 'Start here'
yield from generatorfirst()
yiend 'End here'
>>> hi = generatorssecond()
>>> hi.__next__()
>>> Start here
>>> Hi.__next()
>>> 0
...x98
>>> End here
#using yield from, the generator can iterate over generator object just like loop
#the key yield from need to be inside of function#save this infot file.py
import fileinput
with fileinput.input() as f_input:
for line in f_input:
print(line, end='')
>>> ls -l | python file.py
#the pipeline will pass the to the script, process and get the output.