mirror of
https://github.com/Fechin/reference.git
synced 2026-11-20 21:23:11 +03:00
add : Python-specific tips and examples to various sections
This commit is contained in:
1 parent
d1cc4625f8
commit
d185fdca9c
1 file changed
+167
@@ -957,6 +957,29 @@ else:
|
||||
|
||||
Also see: [Python Tips](https://book.pythontips.com/en/latest/for_-_else.html)
|
||||
|
||||
### Over Dictionary
|
||||
````python
|
||||
johndict = {
|
||||
"firstname": "John",
|
||||
"lastname": "Doe",
|
||||
"age": 30
|
||||
}
|
||||
|
||||
x = johndict.items()
|
||||
print(x)
|
||||
|
||||
# Output: dict_items([('firstname', 'John'),
|
||||
# ('lastname', 'Doe'),
|
||||
# ('age', 30)])
|
||||
|
||||
for key, value in johndict.items():
|
||||
print(f"{key} : : {value}")
|
||||
|
||||
# Output: firstname : : John
|
||||
# Output: lastname : : Doe
|
||||
# Output: age : : 30
|
||||
````
|
||||
|
||||
## Python Functions
|
||||
|
||||
### Basic
|
||||
@@ -1298,6 +1321,25 @@ print(Yoki.legs) # => 4
|
||||
Yoki.sound() # => Woof!
|
||||
```
|
||||
|
||||
|
||||
### @staticmethod {.col-span-2}
|
||||
|
||||
```python
|
||||
class MyClass:
|
||||
@staticmethod
|
||||
def greet(name):
|
||||
return f"Hello, {name}!"
|
||||
|
||||
# No instantiation nedded
|
||||
|
||||
# Call via class
|
||||
print(MyClass.greet("Alice")) # => Hello, Alice!
|
||||
|
||||
# Can still call via instance
|
||||
obj = MyClass()
|
||||
print(obj.greet("Bob")) # => Hello, Bob!
|
||||
```
|
||||
|
||||
## Python Type Hints (Since Python 3.5)
|
||||
|
||||
### Variable & Parameter
|
||||
@@ -1524,6 +1566,88 @@ while (data := values[i]):
|
||||
|
||||
# Expected result: 1, "text", True
|
||||
```
|
||||
## Date & Time Handling
|
||||
|
||||
|
||||
### Current date and time
|
||||
|
||||
|
||||
```python
|
||||
import datetime
|
||||
|
||||
now = datetime.datetime.now()
|
||||
print(now) # e.g., 2024-04-27 14:35:22.123456
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Creating specific date/time objects
|
||||
|
||||
|
||||
```python
|
||||
import datetime
|
||||
|
||||
# Create a date object
|
||||
d = datetime.date(2024, 4, 27)
|
||||
print(d) # 2024-04-27
|
||||
|
||||
# Create a time object
|
||||
t = datetime.time(15, 30, 45)
|
||||
print(t) # 15:30:45
|
||||
|
||||
# Create a datetime object
|
||||
dt = datetime.datetime(2024, 4, 27, 15, 30, 45)
|
||||
print(dt) # 2024-04-27 15:30:45
|
||||
```
|
||||
|
||||
|
||||
### Converting between date formats
|
||||
|
||||
|
||||
```python
|
||||
import datetime
|
||||
|
||||
# Convert a string to a datetime object
|
||||
date_str = "2024-04-27 14:00"
|
||||
dt_obj = datetime.datetime.strptime(date_str, "%Y-%m-%d %H:%M")
|
||||
print(dt_obj) # 2024-04-27 14:00:00
|
||||
|
||||
# Convert a datetime object to a string
|
||||
formatted_str = dt_obj.strftime("%d/%m/%Y %H:%M")
|
||||
print(formatted_str) # 27/04/2024 14:00
|
||||
```
|
||||
|
||||
|
||||
### Timestamps and Unix time
|
||||
|
||||
```python
|
||||
import datetime
|
||||
|
||||
# Get current timestamp
|
||||
timestamp = datetime.datetime.now().timestamp()
|
||||
print(timestamp) # e.g., 1714188922.123456
|
||||
|
||||
# Convert timestamp back to datetime
|
||||
dt_from_timestamp = datetime.datetime.fromtimestamp(timestamp)
|
||||
print(dt_from_timestamp)
|
||||
```
|
||||
|
||||
### Date difference and timedelta {.col-span-2}
|
||||
|
||||
```python
|
||||
import datetime
|
||||
|
||||
date1 = datetime.date(2024, 4, 27)
|
||||
date2 = datetime.date(2024, 5, 1)
|
||||
|
||||
delta = date2 - date1
|
||||
print(delta.days) # 4
|
||||
|
||||
# Using timedelta for date arithmetic
|
||||
new_date = date1 + datetime.timedelta(days=10)
|
||||
print(new_date) # 2024-05-07
|
||||
```
|
||||
|
||||
|
||||
## Miscellaneous
|
||||
|
||||
@@ -1582,3 +1706,46 @@ else: # Optional clause to the try/except block. Must follow
|
||||
finally: # Execute under all circumstances
|
||||
print("We can clean up resources here")
|
||||
```
|
||||
|
||||
### Dispatcher Pattern {.col-span-3}
|
||||
|
||||
```python
|
||||
# Dispatcher allows dynamic selection and execution of functions based on user input or other runtime conditions
|
||||
|
||||
def add(x, y):
|
||||
return x + y
|
||||
|
||||
def subtract(x, y):
|
||||
return x - y
|
||||
|
||||
def multiply(x, y):
|
||||
return x * y
|
||||
|
||||
def divide(x, y):
|
||||
if y == 0:
|
||||
return 'Error: Division by zero'
|
||||
return x / y
|
||||
|
||||
# Dispatcher dictionary: maps operation names to their corresponding functions
|
||||
operations = {
|
||||
'add': add,
|
||||
'subtract': subtract,
|
||||
'multiply': multiply,
|
||||
'divide': divide
|
||||
}
|
||||
|
||||
# Function to dispatch operation based on operation name
|
||||
# Common use cases include executing different functions dynamically, such as in calculators, command interpreters, or event handling
|
||||
def dispatcher(operation_name, x, y):
|
||||
func = operations.get(operation_name)
|
||||
if func:
|
||||
return func(x, y)
|
||||
else:
|
||||
return f"Unknown operation: {operation_name}"
|
||||
|
||||
# Usage examples
|
||||
print(dispatcher('add', 5, 3)) # Output: 8
|
||||
print(dispatcher('multiply', 4, 2)) # Output: 8
|
||||
print(dispatcher('divide', 10, 0)) # Output: Error: Division by zero
|
||||
print(dispatcher('mod', 10, 3)) # Output: Unknown operation: mod
|
||||
```
|
||||
Reference in new issue
Block a user