Skip to content

Commit 2fbf777

Browse files
committed
Comparisons and XML parsing
1 parent 59f984f commit 2fbf777

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

coding-conventions.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@
1616

1717
The restricted line length originates from the early days of computing when terminals were limited to 80 characters per line. This convention is still followed in Python to ensure code readability across different editors and environments.
1818

19+
### Comparisons
20+
Use `is` or `is not` when comparing to `None`. From a performance perspective, `is` is faster than `==` because it checks for object identity rather than value equality.
21+
```python
22+
crmpicco = None
23+
if crmpicco is None:
24+
print("crmpicco is not here")
25+
26+
the_champ = "cnation"
27+
if the_champ is not None:
28+
print("the champ is here")
29+
```
30+
1931
### Types
2032
#### Informational
2133
Informational PEPs describe a Python design issue, or provide general guidelines or information to the Python community. Some examples of informational PEPs are [PEP-20](https://peps.python.org/pep-0020/) (The Zen of Python), and [PEP-257](https://peps.python.org/pep-0257/) (Docstring conventions).

network-programming.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,30 @@ event_element = et.Element('event')
108108
print(type(event_element.attrib))
109109
# prints # <class 'dict'>
110110
```
111+
`parse()` - used to parse an XML document from a string or a file. It returns an `ElementTree` object that represents the entire XML document.
111112

113+
The following code parses an XML file and changes the tag of each child element to `football_competition`:
114+
```python
115+
import xml.etree.ElementTree as ET
116+
competitions = ET. parse('competitions.xml')
117+
root = competitions.getroot()
118+
for child in root:
119+
child.tag = 'football_competition'
120+
```
121+
Sample XML file (`competitions.xml`):
122+
```xml
123+
<?xml version="1.0"?>
124+
<competitions>
125+
<competition name="Scottish Cup">
126+
<author>Scottish Football Association</author>
127+
<year>1873</year>
128+
</competition>
129+
<competition name="Scottish League Cup">
130+
<author>Scottish Professional Football League</author>
131+
<year>1946</year>
132+
</competition>
133+
</competitions>
134+
```
112135
## General
113136

114137
### IP (Internet Protocol)

0 commit comments

Comments
 (0)