Skip to content

Commit 0112fd5

Browse files
committed
Some more demos
1 parent a13c0cd commit 0112fd5

10 files changed

+302
-0
lines changed

BeautifulSoup.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'''
2+
Installation:
3+
pip install beautifulsoup4
4+
pip3 install beautifulsoup4
5+
'''
6+
7+
from bs4 import BeautifulSoup
8+
import requests
9+
import sys
10+
11+
username = "sachin_rt"
12+
url = "http://www.twitter.com/" + username
13+
response = None
14+
try:
15+
response = requests.get(url)
16+
except Exception as e:
17+
print(repr(e))
18+
sys.exit(1)
19+
20+
if response.status_code != 200:
21+
print("Non success status code returned "+str(response.status_code))
22+
sys.exit(1)
23+
24+
soup = BeautifulSoup(response.text, 'html.parser')
25+
26+
if soup.find("div", {"class": "errorpage-topbar"}):
27+
print("\n\n Error: Invalid username.")
28+
sys.exit(1)
29+
30+
tweets = soup.find_all("p", {"class": "TweetTextSize TweetTextSize--normal js-tweet-text tweet-text"})
31+
32+
tweetList = []
33+
for tweet in tweets:
34+
text = tweet.text.encode('utf-8')
35+
tweetList.append(text)
36+
37+
print(tweetList)

Circle.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import math
2+
3+
class Circle:
4+
def __init__(self, radius=0):
5+
self.radius = radius
6+
7+
def input(self):
8+
self.radius = int(input("Enter radius: "))
9+
10+
def display(self):
11+
print("Circle({})".format(self.radius))
12+
13+
def displayDiameter(self):
14+
print("Circle diameter: {}".format(self.radius * 2))
15+
16+
def area(self):
17+
print("Area: {}".format(math.pi * self.radius * self.radius))
18+
19+
def __add__(self, other):
20+
r = other.radius + self.radius
21+
return Circle(r)
22+
23+
def __eq__(self, other):
24+
return other.radius == self.radius
25+
26+
def compare(self, c):
27+
if c.radius > self.radius:
28+
return 1
29+
else:
30+
if c.radius == self.radius:
31+
return 0
32+
else:
33+
return -1
34+
35+
36+
circle1 = Circle(10)
37+
circle1.display()
38+
circle1.area()
39+
circle1.displayDiameter()
40+
41+
circle2 = Circle()
42+
circle2.input()
43+
44+
circle3 = circle1 + circle2
45+
print(circle1 == circle2)
46+
circle3.display()

FileHandling.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'''
2+
"r" - Read - Default value. Opens a file for reading, error if the file does not exist
3+
"a" - Append - Opens a file for appending, creates the file if it does not exist
4+
"w" - Write - Opens a file for writing, creates the file if it does not exist
5+
"x" - Create - Creates the specified file, returns an error if the file exists
6+
"t" - Text - Default value. Text mode
7+
"b" - Binary - Binary mode (e.g. images)
8+
9+
f = open("file.txt", "rt")
10+
11+
f.read()
12+
f.read(10)
13+
f.readline()
14+
15+
for x in f:
16+
print(x)
17+
18+
f.write("Now the file has one more line!")
19+
20+
import os
21+
if os.path.exists("demofile.txt"):
22+
os.remove("demofile.txt")
23+
else:
24+
print("The file does not exist")
25+
26+
27+
'''
28+
29+
f = open("file.txt", "rt")
30+
# print(f.read(7))
31+
32+
33+
# line = f.readline(3)
34+
# print(line)
35+
36+
lines = f.readlines()
37+
print(lines)
38+
39+
f = open("myFile.txt", "rb")
40+
# f.write("Name: abc\nAddress: Pune\nNumber: 132456789")
41+
42+
f.seek(6,1)
43+
name = f.readline()
44+
45+
f.seek(9,1)
46+
address = f.readline()
47+
48+
f.seek(8,1)
49+
number = f.readline()
50+
51+
print(name)
52+
print(address)
53+
print(number)

Inheritance.py

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
"""
2+
class A:
3+
def __init__(self):
4+
self.n = 0
5+
print("init A")
6+
def display(self):
7+
print("Class A")
8+
9+
class B:
10+
def __init__(self):
11+
self.m = 0
12+
print("init B")
13+
def display(self):
14+
print("Class B")
15+
16+
# objB = B()
17+
# objB.m
18+
19+
# objA = A()
20+
# objA.n
21+
22+
class C(A,B):
23+
def __init__(self):
24+
self.o = 0
25+
print("init C")
26+
A.__init__(self)
27+
B.__init__(self)
28+
29+
def display(self):
30+
print("Class C")
31+
B.display(self)
32+
A.display(self)
33+
34+
c = C()
35+
c.display()
36+
37+
38+
class Polygon:
39+
sides = []
40+
def __init__(self,n):
41+
super().__init__()
42+
self.n = n
43+
44+
def inputSides(self):
45+
for n in range(self.n):
46+
side = input("Enter {} side: ".format(n))
47+
self.sides.append(side)
48+
49+
def displaySides(self):
50+
for side in self.sides:
51+
print(side)
52+
53+
# p = Polygon(3)
54+
# p.inputSides()
55+
# p.displaySides()
56+
57+
class Triangle(Polygon):
58+
def __init__(self):
59+
super().__init__(3)
60+
61+
62+
# t = Triangle()
63+
# t.inputSides()
64+
# t.displaySides()
65+
66+
class Rectangle(Polygon):
67+
def __init__(self):
68+
super().__init__(4)
69+
70+
71+
class Square(Rectangle):
72+
# def __init__(self):
73+
# super().__init__()
74+
75+
def inputSides(self):
76+
side = input("Enter side: ")
77+
for n in range(self.n):
78+
self.sides.append(side)
79+
80+
81+
s = Square()
82+
s.inputSides()
83+
s.displaySides()
84+
85+
"""
86+
87+
88+
class A:
89+
def __init__(self):
90+
self.__private__ = 10
91+
self._protected_ = 20
92+
93+
def display(self):
94+
print(self.__private__)
95+
96+
class B(A):
97+
98+
def display(self):
99+
print(self._protected_)
100+
print(self.__private__)
101+
102+
class C(B):
103+
104+
def display(self):
105+
print(self._protected_)
106+
107+
108+
a = A()
109+
110+
# print(a.__private)
111+
a.display()
112+
print(a._protected_)
113+
114+
b = B()
115+
b.display()
116+
117+
c = C()
118+
c.display()

MultiThreading.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import threading
2+
import time
3+
4+
exitFlag = 0
5+
6+
class myThread (threading.Thread):
7+
def __init__(self, threadID, name, counter):
8+
threading.Thread.__init__(self)
9+
self.threadID = threadID
10+
self.name = name
11+
self.counter = counter
12+
def run(self):
13+
print ("Starting {}".format(self.name))
14+
print_time(self.name, 5, self.counter)
15+
print ("Exiting " + self.name)
16+
17+
def print_time(threadName, counter, delay):
18+
while counter:
19+
# if exitFlag:
20+
# threadName.exit()
21+
time.sleep(delay)
22+
print ("%s: %s" % (threadName, time.ctime(time.time())))
23+
counter -= 1
24+
25+
26+
# Create new threads
27+
thread1 = myThread(1, "Thread-1", 1)
28+
thread2 = myThread(2, "Thread-2", 2)
29+
30+
# Start new Threads
31+
thread1.start()
32+
thread2.start()
33+
34+
print("Exiting Main Thread")

file.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
C
2+
C++
3+
Python
4+
R
5+
Data Science

myDemo.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import mymodule
2+
3+
mymodule.displayName("Codekul")

myFile.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Name: abc
2+
Address: Pune
3+
Number: 132456789

mymodule.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def displayName(name):
2+
print("Hello, {}".format(name))
3+

mymodule.pyc

367 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)