Skip to content

Commit ee8fb96

Browse files
committed
docs(notes): add Python methods guide for March 2025
1 parent d874ae2 commit ee8fb96

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

notes/2025-03-22-python-methods-en.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
title: Python Methods
3+
lang: en
4+
layout: post
5+
audio: false
6+
translated: false
7+
generated: true
8+
---
9+
10+
Let me break down this Python one-liner step-by-step:
11+
12+
### Command:
13+
```bash
14+
python -c "from hcloud import Client; c = Client(token='fake'); print(dir(c.servers)); print('\nActions:'); print(dir(c.actions))"
15+
```
16+
17+
### Explanation:
18+
19+
1. **`python -c`**:
20+
- This tells the Python interpreter to execute the code that follows as a string directly from the command line, instead of running a `.py` file.
21+
22+
2. **`from hcloud import Client`**:
23+
- This imports the `Client` class from the `hcloud` module. The `hcloud` library is the official Python SDK for interacting with the Hetzner Cloud API (a cloud hosting provider). It allows you to manage servers, actions, images, etc., programmatically.
24+
25+
3. **`c = Client(token='fake')`**:
26+
- This creates an instance of the `Client` class, initializing it with an API token. Here, `'fake'` is a placeholder token (not a real one). In practice, you'd replace `'fake'` with a valid Hetzner Cloud API token to authenticate requests to their API.
27+
28+
4. **`print(dir(c.servers))`**:
29+
- `c.servers` is an attribute of the `Client` object that provides access to server-related functionality (e.g., creating, deleting, or listing servers).
30+
- `dir()` is a built-in Python function that returns a list of all attributes and methods of an object as strings. So, `dir(c.servers)` lists everything you can do with the `servers` object (e.g., methods like `create`, `get_by_id`, etc.).
31+
- This prints the list to the console, showing what operations are available for managing servers.
32+
33+
5. **`print('\nActions:')`**:
34+
- This prints a newline (`\n`) followed by the string `'Actions:'` to separate the output of `dir(c.servers)` from the next part for better readability.
35+
36+
6. **`print(dir(c.actions))`**:
37+
- `c.actions` is another attribute of the `Client` object, providing access to action-related functionality (e.g., tracking the status of operations like rebooting a server).
38+
- Similar to before, `dir(c.actions)` lists all available attributes and methods of the `actions` object.
39+
- This prints the list to the console, showing what you can do with actions.
40+
41+
### What Does It Do?
42+
- This command is essentially a quick way to inspect the `hcloud` library's `servers` and `actions` modules. It outputs:
43+
1. A list of all methods and attributes available for managing servers (`c.servers`).
44+
2. A list of all methods and attributes available for managing actions (`c.actions`).
45+
46+
### Example Output:
47+
If you ran this with a valid token (and the `hcloud` library installed), the output might look something like this (simplified):
48+
```
49+
['create', 'delete', 'get_all', 'get_by_id', 'power_off', 'power_on', ...]
50+
Actions:
51+
['get_all', 'get_by_id', 'sort', ...]
52+
```
53+
- The first line shows methods like `create` or `delete` for server management.
54+
- After "Actions:", it shows methods like `get_all` for retrieving action details.
55+
56+
### Prerequisites:
57+
- You need the `hcloud` Python package installed (`pip install hcloud`).
58+
- A real API token from Hetzner Cloud would be required for actual functionality, but this specific command only inspects the library and doesn’t make API calls.
59+
60+
### Purpose:
61+
This is likely used for debugging or exploration to see what methods are available in the `hcloud` library without reading the full documentation.
62+
63+
Let me know if you'd like me to dig deeper into any part!

scripts/auto-ss-config/hetzner.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
def create_snapshot(server):
1515
try:
16-
snapshot = server.create_snapshot(name=f"{server.name}-snapshot")
17-
print(f"Snapshot created for server {server.name} with ID: {snapshot.id}")
16+
response = client.servers.create_image(server, description=f"{server.name}-snapshot", type="snapshot")
17+
print(f"Snapshot created for server {server.name} with ID: {response.image.id}")
1818
except Exception as e:
1919
print(f"Error creating snapshot for server {server.name}: {e}")
2020

0 commit comments

Comments
 (0)