Skip to content
GitLab
  • Menu
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
  • Sign in / Register
  • F FastAPI
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
    • Locked Files
  • Issues 0
    • Issues 0
    • List
    • Boards
    • Service Desk
    • Milestones
    • Iterations
  • Deployments
    • Deployments
    • Releases
  • Packages & Registries
    • Packages & Registries
    • Package Registry
    • Container Registry
    • Infrastructure Registry
  • Wiki
    • Wiki
  • Activity
  • Graph
  • Create a new issue
  • Commits
  • Issue Boards
Collapse sidebar
  • Sitemule
  • Public
  • workshops
  • open source on IBM i
  • FastAPI
  • Wiki
  • Part 2b. Organise my first service

Part 2b. Organise my first service · Changes

Page history
Update Part 2b. Organise my first service authored Oct 30, 2021 by Jens Melgard Churchill's avatar Jens Melgard Churchill
Hide whitespace changes
Inline Side-by-side
Part-2b.-Organise-my-first-service.md
View page @ c3f7fce0
......@@ -75,27 +75,27 @@ In NORMAL-mode (not insert).
* `dd` on a line, cuts it
* `p` pastes below current line
A lot of vim commands can be composed, so typing `10G` will move the cursor to line 10 in the file.
`d5d` will cut from current line, and 5 down, there are more, a lot more.
But this isn't actually workshop on vim, but remember this...
A lot of vim commands can be composed, so typing `10G` will move the cursor to line 10 in the file.
`d5d` will cut from current line, and 5 down, there are more, a lot more.
But this isn't actually workshop on vim, but remember this...
The designers of vim believe that people _edit_ files more than they _create_ them,
so vim is optimised towards modifying existing files, as appose to creating them.
The designers of vim believe that people _edit_ files more than they _create_ them,
so vim is optimised towards modifying existing files, as appose to creating them.
Pressing `v` puts vim into VISUAL-mode, as appose to `i` for INSERT-mode.
In _visual-mode_ you can use the cursors to select in the buffer.
Pressing `v` puts vim into VISUAL-mode, as appose to `i` for INSERT-mode.
In _visual-mode_ you can use the cursors to select in the buffer.
As an example, if we are on line 10, press `v` and pres the down key a couple of times,
and then press `d` - We'll cut those lines.
As an example, if we are on line 10, press `v` and pres the down key a couple of times,
and then press `d` - We'll cut those lines.
And that brings us to the most import key, the one to rule them all.
And that brings us to the most import key, the one to rule them all.
`u` will undo ;)
`u` will undo ;)
Last one before we start, in _visual-mode_ select a few lines, and press `>`
you just indented right.
Last one before we start, in _visual-mode_ select a few lines, and press `>`
you just indented right.
Lets take a look at the final result for _main.py_
Lets take a look at the final result for _main.py_
```python
# Move all imports to top of file, and group+sort them for good measure
......@@ -141,45 +141,45 @@ async def index():
```
Basically, we've added and removed imports depending on their use.
We've introduced to new features, namely support for _templates_ and _static files_.
The index function has gotten a new annotation, that "links" it to a template file.
And we've removed the items route.
We've introduced to new features, namely support for _templates_ and _static files_.
The index function has gotten a new annotation, that "links" it to a template file.
And we've removed the items route.
As always, press `Esc` to exit insert-mode, and :w to save.
As always, press `Esc` to exit insert-mode, and :w to save.
Now come the hard part, the need to work with multiple files.
Now come the hard part, the need to work with multiple files.
The easy way, is to simply exit vim, and start vim on a new file, that could be `:q` and `vim api_router.py`
But for the brave, working with multiple files or buffers as vim calls them is a powerful skill.
But for the brave, working with multiple files or buffers as vim calls them is a powerful skill.
Here are the most useful commands...
Here are the most useful commands...
- To open a file (buffer) type `:e {filepath}` you can use tab-completion.
- To list open buffers type `:ls`, note the number (n) to the left of each.
- To switch buffers type `:b<n>`
- To go to next buffer type `:bn`, and `:bp` for previous
- To delete a buffer (~close it), type `:bd`
(this does not delete the file)
- To open a file (buffer) type `:e {filepath}` you can use tab-completion.
- To list open buffers type `:ls`, note the number (n) to the left of each.
- To switch buffers type `:b<n>`
- To go to next buffer type `:bn`, and `:bp` for previous
- To delete a buffer (~close it), type `:bd`
(this does not delete the file)
It is also possible to go to an open buffer, by knowing its name, or part of it.
Typing `:b <filename>` jumps to the buffer with that file, and of course, tab-completion is your friend.
It is also possible to go to an open buffer, by knowing its name, or part of it.
Typing `:b <filename>` jumps to the buffer with that file, and of course, tab-completion is your friend.
And finally, typing `:e .` will allow you to navigate the file system to open a file! ( _Now he tells me!_ )
And finally, typing `:e .` will allow you to navigate the file system to open a file! ( _Now he tells me!_ )
So, lets try...
So, lets try...
Navigate to the bottom of main.py, down to where the `@router.get("/items/{item_id}")` line is.
Press `v` and arrow down to until the whole code block is selected.
Press `d` to cut it, and `:w` to save the change.
Navigate to the bottom of main.py, down to where the `@router.get("/items/{item_id}")` line is.
Press `v` and arrow down to until the whole code block is selected.
Press `d` to cut it, and `:w` to save the change.
Type `:e api+<tab>` or `:e .` and navigate to open the api_router.py file we created earlier.
Since this file is empty, simply press `p`to paste the cut-content from before.
Type `:e api+<tab>` or `:e .` and navigate to open the api_router.py file we created earlier.
Since this file is empty, simply press `p`to paste the cut-content from before.
Press `gg` or arrow back up to the top of the file, and type in the remaining bits.
Press `gg` or arrow back up to the top of the file, and type in the remaining bits.
Press `i` to enter _insert-mode_, and change the `@app` part to `@router`
Press `i` to enter _insert-mode_, and change the `@app` part to `@router`
When the file looks like this, the free coffee is on me.
When the file looks like this, the free coffee is on me.
```python
from fastapi import APIRouter
......@@ -193,10 +193,8 @@ def read_item(item_id: int, q: Optional[str] = None):
```
`Esc`to return to _normal-mode_ and type `:w`to save, and delete that buffer `:bd`
You are back in main.py, well done!! - I'll take it from here!
Proceed to Part 3.
`Esc`to return to _normal-mode_ and type `:w`to save, and delete that buffer `:bd`
You are back in main.py, well done!! - I'll take it from here!
Proceed to Part 3.
\ No newline at end of file
Clone repository
  • Part 1a. My first service
  • Part 1b. Testing my first service
  • Part 2a. Extending my first service
  • Part 2b. Organise my first service
  • Part 3a. Review my first service
  • Part 3b. Connecting my first service
  • Home