Skip to content

Commit 82337af

Browse files
committed
Adding a LogRecord example
1 parent 0e15a33 commit 82337af

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

advanced-oop.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
# Advanced Object-Oriented Programming
22

3+
## Assorted methods
34
`__le__` - this method is used to implement less than or equal to comparisons. It should return `True` if the object is less than or equal to the other object, `False` otherwise.
45

6+
`__repr()__` - used to return a information-rich string representation of the object. This is useful for debugging and logging purposes. The string returned by `__repr__()` should be a valid Python expression that can be used to recreate the object, e.g.
7+
```python
8+
class Player:
9+
def __init__(self, name, age):
10+
self.name = name
11+
self.age = age
12+
13+
def __repr__(self):
14+
return f"Player(name={self.name!r}, age={self.age!r})"
15+
16+
player = Player("James Tavernier", 33)
17+
print(repr(player)) # Player(name='James Tavernier', age=33)
18+
```
19+
520
`from abc import ABC, abstractmethod` - the `abc` module provides the `ABC` class that can be used to create abstract base classes. Abstract base classes are classes that are designed to be inherited from, but not instantiated. Abstract methods are methods that must be implemented by any concrete subclasses.
621

722
`@abstractmethod` - this decorator is used to mark a method as abstract. Abstract methods must be implemented by any concrete subclasses.

file-processing.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,31 @@ cursor.executescript(sql_script)
3030
# commit changes and close the connection
3131
conn.commit()
3232
conn.close()
33-
```
33+
```
34+
35+
## Logging
36+
The [logging](https://docs.python.org/3/library/logging.html) module is used to log messages from a program. It is useful for debugging and tracking the execution of a program. The logging module provides a way to configure different log levels, such as `DEBUG`, `INFO`, `WARNING`, `ERROR`, and `CRITICAL`.
37+
38+
`LogRecord` - an object that holds all the metadata about the logging event (level, filename, line number etc.)
39+
40+
```python
41+
import logging
42+
43+
logging.basicConfig(level=logging.DEBUG)
44+
logger = logging.getLogger()
45+
46+
logger.debug("Let's generate some fixtures for the new season!")
47+
48+
log_record = logging.LogRecord(
49+
name='crmpicco_logger',
50+
level=logging.DEBUG,
51+
pathname='generate_fixtures.py',
52+
lineno=10,
53+
msg='This is a custom log message',
54+
args=None,
55+
exc_info=None
56+
)
57+
58+
# you can access the log message from the LogRecord using the msg attribute
59+
log_message = log_record.msg
60+
```

gui-programming.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ Event handlers interact with various components to capture and transform user in
1111

1212
### Widgets
1313

14+
#### Positioning
15+
16+
The placement of widgets in a window is determined by the geometry manager. They can be defined by x/y coordinates or with relative coordinates and anchor, e.g.
17+
```python
18+
# x/y coordinates (in pixels)
19+
import tkinter as tk
20+
xy_label = tk.Label(root, text="x/y coordinates")
21+
xy_label.place(x=100, y=100)
22+
```
23+
```python
24+
# relative coordinates (and anchor)
25+
import tkinter as tk
26+
relative_label = tk.Label(root, text="relative (anchored) coordinates")
27+
relative_label.place(relx=0.5, rely=0.5, anchor="center")
28+
```
1429
- `Entry` - a single-line text input field. It can be used to get user input.
1530
```python
1631
entry.insert(0, "Please enter your name")

0 commit comments

Comments
 (0)