Skip to content

Commit 6ba8613

Browse files
committed
Added 4 modules
1 parent 78da6d2 commit 6ba8613

File tree

4 files changed

+342
-0
lines changed

4 files changed

+342
-0
lines changed

Diff for: Beautiful_Soup.md

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
## Beautiful Soup
2+
### Introduction
3+
```
4+
Beautiful Soup is a Python library for pulling data out of HTML and XML files.At the beginning of your Python script, import the library.Now you have to pass something to BeautifulSoup to create a soup object. That could be a document or an URL.BeautifulSoup does not fetch the web page for you, you have to do that yourself.
5+
```
6+
### Filtering
7+
#### String
8+
```
9+
The simplest filter is a string.
10+
Pass a string to a search method and Beautiful Soup will perform a match against
11+
that exact string.
12+
```
13+
```python
14+
This code finds all the 'b' tags in the document (you can replace b with any
15+
tag you want to find)
16+
17+
soup.find_all('b')
18+
```
19+
#### Regular Expression
20+
```python
21+
If you pass in a regular expression object, Beautiful Soup will filter against
22+
that regular expression using its match() method.
23+
24+
This code finds all the tags whose names start with the letter "b",
25+
in this case, the 'body' tag and the 'b' tag:
26+
27+
import re
28+
for tag in soup.find_all(re.compile("^b")):
29+
print(tag.name)
30+
31+
This code finds all the tags whose names contain the letter "t":
32+
33+
for tag in soup.find_all(re.compile("t")):
34+
print(tag.name)
35+
```
36+
#### List
37+
```python
38+
If you pass in a list, Beautiful Soup will allow a string match against any
39+
item in that list.
40+
41+
This code finds all the 'a' tags and all the 'b' tags
42+
43+
print soup.find_all(["a", "b"])
44+
```
45+
### Example
46+
One common task is extracting all the URLs found within a page’s 'a' tags.Using the find_all method, gives us a whole list of elements with the tag "a".
47+
```python
48+
for link in soup.find_all('a'):
49+
print(link.get('href'))
50+
```
51+
Another common task is extracting all the text from a page:
52+
```python
53+
print(soup.get_text())
54+
```
55+
56+

Diff for: JSON.md

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
## JSON Module
2+
### What is it?
3+
```
4+
The json library can parse JSON from strings or files. The library parses JSON into a Python dictionary or list. It can also convert Python dictionaries or lists into JSON strings.
5+
```
6+
### How to use?
7+
Take the following string containing JSON data:
8+
```python
9+
json_string = '{"first_name": "Guido", "last_name":"Rossum"}'
10+
```
11+
It can be parsed like this:
12+
```python
13+
import json
14+
parsed_json = json.loads(json_string)
15+
```
16+
and can now be used as a normal dictionary:
17+
```python
18+
print(parsed_json['first_name'])
19+
"Guido"
20+
```
21+
You can also convert the following to JSON:
22+
```python
23+
d = {
24+
'first_name': 'Guido',
25+
'second_name': 'Rossum',
26+
'titles': ['BDFL', 'Developer'],
27+
}
28+
29+
print(json.dumps(d))
30+
'{"first_name": "Guido", "last_name": "Rossum", "titles": ["BDFL", "Developer"]}'
31+
```
32+
33+
34+
35+
### Example
36+
#### Python Dictionaries to JSON Strings
37+
Code:
38+
```python
39+
import json
40+
student = {"101":{"class":'V', "Name":'Rohit', "Roll_no":7},
41+
"102":{"class":'V', "Name":'David', "Roll_no":8},
42+
"103":{"class":'V', "Name":'Samiya', "Roll_no":12}}
43+
print(json.dumps(student));
44+
```
45+
Output
46+
```
47+
{"103": {"class": "V", "Name": "Samiya", "Roll_no": 12},
48+
"102": {"class": "V", "Name": "David", "Roll_no": 8},
49+
"101": {"class": "V", "Name": "Rohit", "Roll_no": 7}}
50+
```
51+
#### JSON Strings to Python Dictionaries
52+
Code:
53+
```python
54+
import json
55+
json_data = '{"103": {"class": "V", "Name": "Samiya", "Roll_n": 12}, "102": {"class": "V", "Name": "David", "Roll_no": 8}, "101": {"class": "V", "Name": "Rohit", "Roll_no": 7}}';
56+
print(json.loads(json_data));
57+
```
58+
Output:
59+
```
60+
{"103": {"class": "V", "Name": "Samiya", "Roll_no": 12},
61+
"102": {"class": "V", "Name": "David", "Roll_no": 8},
62+
"101": {"class": "V", "Name": "Rohit", "Roll_no": 7}}
63+
```
64+

Diff for: RE.md

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
## Regular Expresion
2+
### What is it?
3+
```
4+
A regular expression is a special sequence of characters that helps you match or find other strings or sets of strings, using a specialized syntax held in a pattern.
5+
6+
There are various characters, which would have special meaning when they are used in regular expression. To avoid any confusion while dealing with regular expressions, we would use Raw Strings as r'expression'.
7+
```
8+
### Match Function
9+
```
10+
This function attempts to match RE pattern to string with optional flags.
11+
Here is the syntax for this function −
12+
```
13+
```python
14+
re.match(pattern, string, flags=0)
15+
```
16+
```
17+
Here is the description of the parameters:
18+
```
19+
|Parameter | Description |
20+
| ------ | ------ |
21+
|pattern| This is the regular expression to be matched.|
22+
|string| This is the string, which would be searched to match the pattern at the beginning of string.|
23+
|flags| You can specify different flags using bitwise OR. These are modifiers, which are listed in the table below.|
24+
#### Example
25+
```python
26+
#!/usr/bin/python
27+
import re
28+
29+
line = "Cats are smarter than dogs"
30+
31+
matchObj = re.match( r'(.*) are (.*?) .*', line, re.M|re.I)
32+
33+
if matchObj:
34+
print "matchObj.group() : ", matchObj.group()
35+
print "matchObj.group(1) : ", matchObj.group(1)
36+
print "matchObj.group(2) : ", matchObj.group(2)
37+
else:
38+
print "No match!!"
39+
```
40+
The result obtained will be:-
41+
``` python
42+
matchObj.group() : Cats are smarter than dogs
43+
matchObj.group(1) : Cats
44+
matchObj.group(2) : smarter
45+
```
46+
### Match Function
47+
```
48+
This function searches for first occurrence of RE pattern within string with optional flags.
49+
Here is the syntax for this function:
50+
```
51+
```python
52+
re.search(pattern, string, flags=0)
53+
```
54+
The re.search function returns a match object on success, none on failure. We use group(num) or groups() function of match object to get matched expression.
55+
|Match Object Methods | Description |
56+
| ------ | ------ |
57+
group(num=0)| This method returns entire match (or specific subgroup num)
58+
groups()| This method returns all matching subgroups in a tuple (empty if there weren't any)
59+
60+
#### Example
61+
```python
62+
#!/usr/bin/python
63+
import re
64+
65+
line = "Cats are smarter than dogs";
66+
67+
searchObj = re.search( r'(.*) are (.*?) .*', line, re.M|re.I)
68+
69+
if searchObj:
70+
print "searchObj.group() : ", searchObj.group()
71+
print "searchObj.group(1) : ", searchObj.group(1)
72+
print "searchObj.group(2) : ", searchObj.group(2)
73+
else:
74+
print "Nothing found!!"
75+
```
76+
Output:
77+
```python
78+
searchObj.group() : Cats are smarter than dogs
79+
searchObj.group(1) : Cats
80+
searchObj.group(2) : smarter
81+
```
82+
### Match vs Search
83+
```
84+
match checks for a match only at the beginning of the string, while search checks for a match anywhere in the string.
85+
```
86+
#### Example
87+
```python
88+
#!/usr/bin/python
89+
import re
90+
91+
line = "Cats are smarter than dogs";
92+
93+
matchObj = re.match( r'dogs', line, re.M|re.I)
94+
if matchObj:
95+
print "match --> matchObj.group() : ", matchObj.group()
96+
else:
97+
print "No match!!"
98+
99+
searchObj = re.search( r'dogs', line, re.M|re.I)
100+
if searchObj:
101+
print "search --> searchObj.group() : ", searchObj.group()
102+
else:
103+
print "Nothing found!!"
104+
```
105+
Output:
106+
```python
107+
No match!!
108+
search --> matchObj.group() : dogs
109+
```
110+
### Search and Replace
111+
```python
112+
re.sub(pattern, repl, string, max=0)
113+
This method replaces all occurrences of the RE pattern in string with repl, substituting all occurrences unless max provided. This method returns modified string.
114+
```
115+
#### Example
116+
```python
117+
#!/usr/bin/python
118+
import re
119+
120+
phone = "2004-959-559 # This is Phone Number"
121+
122+
# Delete Python-style comments
123+
num = re.sub(r'#.*$', "", phone)
124+
print "Phone Num : ", num
125+
126+
# Remove anything other than digits
127+
num = re.sub(r'\D', "", phone)
128+
print "Phone Num : ", num
129+
```
130+
Output:
131+
```python
132+
Phone Num : 2004-959-559
133+
Phone Num : 2004959559
134+
```
135+
136+
137+
138+
139+
140+
141+

Diff for: argparse.md

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
## Argparse
2+
### What is it?
3+
```
4+
Parser for command-line options, arguments and subcommands.The argparse module makes it easy to write user-friendly command-line interfaces.
5+
6+
When you run the "ls" command without any options, it will default displaying the
7+
contents of the current directory
8+
9+
If you run "ls" on a different directory that you currently are in, you would type
10+
"ls directory_name". The "directory_name" is a "positional argument", which means
11+
that the program know what to do with the value.
12+
13+
To get more information about a file we can use the "-l" switch.
14+
15+
The "-l" is knowns as an "optional argument"
16+
17+
If you want to display the help text of the ls command, you would type "ls --help"
18+
```
19+
### How to use?
20+
We need to import the following:
21+
```python
22+
import argparse
23+
parser = argparse.ArgumentParser()
24+
parser.parse_args()
25+
```
26+
Even without the arguements being specified, executing this code with '--help' option gives us the only pre-coded messgae available.
27+
```python
28+
python program.py --help (or python program.py -h)
29+
usage: program.py [-h]
30+
31+
optional arguments:
32+
-h, --help show this help message and exit
33+
```
34+
35+
### Example
36+
```python
37+
import argparse
38+
import sys
39+
40+
def main():
41+
parser = argparse.ArgumentParser()
42+
parser.add_argument('--x', type=float, default=1.0,
43+
help='What is the first number?')
44+
parser.add_argument('--y', type=float, default=1.0,
45+
help='What is the second number?')
46+
parser.add_argument('--operation', type=str, default='add',
47+
help='What operation? Can choose add, sub, mul, or div')
48+
args = parser.parse_args()
49+
sys.stdout.write(str(calc(args)))
50+
51+
def calc(args):
52+
if args.operation == 'add':
53+
return args.x + args.y
54+
elif args.operation == 'sub':
55+
return args.x - args.y
56+
elif args.operation == 'mul':
57+
return args.x * args.y
58+
elif args.operation == 'div':
59+
return args.x / args.y
60+
61+
if __name__ == '__main__':
62+
main()
63+
```
64+
Now if we pass values for x and y and also specify the operation, we get the desired results
65+
```python
66+
python argparse_example.py --x=5 --y=3 --operation=mul
67+
15.0
68+
```
69+
Instead, if we invoke the help option, we get
70+
```python
71+
python argparse_example.py -h
72+
usage: argparse_example.py [-h] [--x X] [--y Y] [--operation OPERATION]
73+
74+
optional arguments:
75+
-h, --help show this help message and exit
76+
--x X What is the first number?
77+
--y Y What is the second number?
78+
--operation OPERATION
79+
What operation? Can choose add, sub, mul, or div
80+
```
81+

0 commit comments

Comments
 (0)