Skip to content

3:30 Sept 11th #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lab-00/file-copy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/python

f = open("/var/log/system.log")
buf = f.read()
print buf
f1 = open("mylogfile.log")
f1.write(buf)
f1.close()
29 changes: 23 additions & 6 deletions lab-01/file_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,32 @@
# Open the license file, read the content into the buffer
# then close the file descriptor

f = open('conditions.py')
lines10 = f.read(10)
#alllines = f.read()
print (lines10)
f.close()
# f = open('conditions.py')
# f.seek(15)
# lines10 = f.read(5)
# # line11_20 = f.read(10)
# # f.seek(0)
# # lines10 = f.read(10)
# #alllines = f.read()
# print (lines10)
# f.close()

# Open the license file, read the content as "lines" into the
# buffer and close it.

# f = open('conditions.py')
# file_content = f.read()
# print file_content
# f.close()

f = open('conditions.py')
file_content = f.readlines()
f.close()
print file_content
f.close()

for line in file_content:
if len(line) !=0:
line_list = line.split()
if len(line_list) !=0:
if line_list[0] == 'print':
print line_list
49 changes: 41 additions & 8 deletions lab-01/list-operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,45 @@

new_list = []

for l in mylist:
#print l * 100
new_list.append(l + 100)

print new_list
print max(new_list)
print min(new_list)
print sum(new_list)
# print mylist[0]
# print mylist[1]
# print mylist[2]

# for list_item in mylist:
# new_item = list_item * 100
# print new_item

new_list = []

# new_list.append(100)
# new_list.append(200)
# new_list.append(300)

items = [100,200,300]

for item in items:
new_list.append(item)
print new_list

# print new_list
# for l in mylist:
# #print l * 100
# new_list.append(l + 100)

# print new_list
# print max(new_list)
# print min(new_list)
# print sum(new_list)

my_range = [0, 1, 2, 3]
my_list = []


for item in range(2,20):
new_item = item + 10
my_list.append(new_item)
print my_list




28 changes: 22 additions & 6 deletions lab-01/strings-operations.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
#!/usr/bin/python

my_string = "My string has many spaces let me see how many"
my_string = "My,string,has,many,spaces,let,me,see,how,many"

print my_string.split()
my_string = "My,string,has,many,spaces,let,me,see,how,many"

print "Lets loop through the strings"
my_list = my_string.split(',')

for s in my_string.split():
print ("String literal ", s)
print my_list

my_string = "My,string,has,many,commas,let,me,see,how,many"
length_list = len(my_list)

print length_list

first_item = my_list[0]

print first_item

see_item = my_list[7]

print see_item

# print "Lets loop through the strings"

# for s in my_string.split():
# print ("String literal ", s)

# my_string = "My,string,has,many,commas,let,me,see,how,many"

# Assignment
# Count number of , in the string
Expand Down
20 changes: 20 additions & 0 deletions lab-02/func-03.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@



# Check if filename is less < 4
def myfunc(filename,nlines):
if filename < 4:
return "error"

with open(filename) as f:
lines = f.readlines()

my_lines = lines[:nlines]

with open('myfiles.txt','w') as f:
for line in my_lines:
f.write(line)

myfunc("functions.py",3)


10 changes: 7 additions & 3 deletions lab-02/functions-args.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@

def myfunction(arg1, arg2,arg3):
def myfunction(arg1, arg2,arg3="Chicago"):
print "My Very first python function with Arguments"
print "{},{},{}".format(arg1, arg2, arg3)
sum = arg1 + arg2 + arg3

#return sum

if arg3 == "chicago":
second_func(arg1,arg2)
else:
second_func(arg1,arg2,arg3)


# Polymorphism in Funtion names
# # Polymorphism in Funtion names
def second_func(arg1,arg2,arg3=None):
print("Testing optional argument")
print (arg1,arg2,arg3)
Expand All @@ -21,4 +24,5 @@ def third_func(arg1):


if __name__ == "__main__":
myfunction("hello","world","chicago")
my_return = myfunction("Hello", "World","Toronto")
print my_return
5 changes: 3 additions & 2 deletions lab-02/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
def myfunction():
print "My Very first python function"

myfunction()

if __name__ == "__main__":
myfunction()
# if __name__ == "__main__":
# myfunction()


11 changes: 10 additions & 1 deletion lab-04/json-parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@
content = f.read()

j = json.loads(content)
# print j['changed']
# print j['instance_ids']
#print j['instances'][0]
#print json.dumps(j,sort_keys=True,indent=4)

print json.dumps(j,sort_keys=True,indent=4)
for k,v in j.items():
if k == 'instances':
for instance in v:
for k,v in instance.items():
if k == "dns_name":
print v

13 changes: 13 additions & 0 deletions lab-05/simple_os.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

import multiprocessing
import os



def print_os_info():
print multiprocessing.cpu_count()
print os.uname()[0]
print os.system("df -h")
print os.system("uptime")

print_os_info()
19 changes: 17 additions & 2 deletions lab-06/requests-example.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
import requests


def count_yelp(text):
all_words = text.split()
yelp_count = all_words.count('Yelp')
return yelp_count

def replace_yelp(text):
return text.replace('yelp','google')


r = requests.get('http://wwww.yelp.com')
if r.status_code == 200:
print r.content
print count_yelp(r.text)
#print replace_yelp(r.text)



# Assignment

# Load the yelp page and replace all occurance of yelp to google
# Load the yelp page count number of types yelp word is there and
# replace all occurance of yelp to google


# Load the file from http://censusdata.ire.org/36/all_050_in_36.P2.csv and find
# the max P002001

7 changes: 4 additions & 3 deletions lab-07/ec-create-instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
def create_instance():
ec2_resource = boto3.resource('ec2')
instances = ec2_resource.create_instances(ImageId='ami-49f0762d',
MinCount=1, MaxCount=1,InstanceType='t2.micro',
MinCount=1, MaxCount=3,InstanceType='t2.micro',
SecurityGroupIds=['ansible-node'],KeyName='ansible')
instance_ids = []
for instance in instances:
print instance
instance_ids.append(instance.id)
ec2_client = boto3.client('ec2')
waiter=ec2_client.get_waiter('instance_running')
waiter.wait(InstanceIds=[instances[0].id])
waiter.wait(InstanceIds=instance_ids)
print ("Instance is Running now!")

create_instance()
11 changes: 10 additions & 1 deletion lab-07/ec2-client-list-instances.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@

ec2_client = boto3.client('ec2')
response = ec2_client.describe_instances()
print(response)
#print(response)

for k,v in response.items():
if k == 'Reservations':
for instance in v:
for i,vv in instance.items():
if i == 'Instances':
for ii in vv:
print ii['PublicDnsName']

13 changes: 13 additions & 0 deletions lab-07/ec2-list-volumes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import boto3

def list_volumes():
ec2_resource = boto3.resource('ec2')

instances = ec2_resource.instances.filter(
Filters=[{'Name': 'instance-state-name', 'Values': ['running']}])
for instance in instances:
print(instance.state)
for item in instance.volumes.all():
print item.id

list_volumes()
5 changes: 2 additions & 3 deletions lab-07/ec2-resource-list-instances.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
instances = ec2_resource.instances.filter(
Filters=[{'Name': 'instance-state-name', 'Values': ['running']}])
for instance in instances:
print(instance.state)
for item in instance.volumes.all():
print item.id
print(instance.id,instance.public_dns_name)



# Mac installation tip:
Expand Down
41 changes: 41 additions & 0 deletions lab-07/ec2-start-stop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import sys
import boto3
from botocore.exceptions import ClientError


instance_id = sys.argv[2]
action = sys.argv[1]

ec2 = boto3.client('ec2')


if action == 'start':
# Do a dryrun first to verify permissions
try:
ec2.start_instances(InstanceIds=[instance_id], DryRun=True)
except ClientError as e:
if 'DryRunOperation' not in str(e):
raise

# Dry run succeeded, run start_instances without dryrun
try:
response = ec2.start_instances(InstanceIds=[instance_id], DryRun=False)
print(response)
except ClientError as e:
print(e)
elif action == 'stop':
# Do a dryrun first to verify permissions
try:
ec2.stop_instances(InstanceIds=[instance_id], DryRun=True)
except ClientError as e:
if 'DryRunOperation' not in str(e):
raise

# Dry run succeeded, call stop_instances without dryrun
try:
response = ec2.stop_instances(InstanceIds=[instance_id], DryRun=False)
print(response)
except ClientError as e:
print(e)
else:
print( "Operation not supported")
Loading