Skip to content

Commit

Permalink
feat(aleph#Problems accessing redis locally): Problems accessing redi…
Browse files Browse the repository at this point in the history
…s locally

If you're with the VPN connected, turn it off.

feat(aleph#PDB behaves weird): PDB behaves weird

Sometimes you have two traces at the same time, so each time you run a PDB
command it jumps from pdb trace. Quite confusing. Try to `c` the one you don't
want so that you're left with the one you want. Or put the `pdb` trace in a
conditional that only matches one of both threads.

fix(gitpython): Deprecate tmpdir in favour of tmp_path

feat(pytest#The tmp_path fixture): The tmp_path fixture

You can use the `tmp_path` fixture which will provide a temporary directory
unique to the test invocation, created in the base temporary directory.

`tmp_path` is a `pathlib.Path` object. Here is an example test usage:

```python
def test_create_file(tmp_path):
    d = tmp_path / "sub"
    d.mkdir()
    p = d / "hello.txt"
    p.write_text(CONTENT)
    assert p.read_text() == CONTENT
    assert len(list(tmp_path.iterdir())) == 1
    assert 0
```

fix(pytest#The tmpdir fixture): Deprecate the tmpdir fixture

Warning: Don't use `tmpdir` use `tmp_path` instead because `tmpdir` uses `py`
which is unmaintained and has unpatched vulnerabilities.

feat(python_snippets#Pad integer with zeros): Pad integer with zeros

```python
>>> length = 1
>>> print(f'length = {length:03}')
length = 001
```

feat(elasticsearch#Get documents that match a string): Get documents that match a string

```bash
curl \
    -H 'Content-Type: application/json' \
    -XPOST "https://localhost:9200/_search" \
    -d' { "query": { "query_string": {"query": "test company"} }}'
```

feat(linux_snippets#df and du showing different results): df and du showing different results

Sometimes on a linux machine you will notice that both `df` command (display
free disk space) and `du` command (display disk usage statistics) report
different output. Usually, `df` will output a bigger disk usage than `du`.

The `du` command estimates file space usage, and the `df` command shows file
system disk space usage.

There are many reasons why this could be happening:

* [Disk mounted over data](linux_snippets.md#disk-mounted-over-data)
* [Used deleted files](linux_snippets.md#used-deleted-files)

feat(linux_snippets#Clean up docker data): Clean up docker data

To remove unused `docker` data you can run `docker system prune -a`. This will
remove:

- All stopped containers
- All networks not used by at least one container
- All images without at least one container associated to them
- All build cache

Sometimes that's not enough, and your `/var/lib/docker` directory still weights
more than it should. In those cases:

- Stop the `docker` service.
- Remove or move the data to another directory
- Start the `docker` service.

In order not to loose your persisted data, you need to configure your dockers to
mount the data from a directory that's not within `/var/lib/docker`.

feat(mdformat#issues): Issues

- It doesn't yet
  [support admonitions](hukkin/mdformat#309)
- You can't
  [ignore some files](hukkin/mdformat#359),
  nor
  [some part of the file](hukkin/mdformat#53)
  • Loading branch information
lyz-code committed Nov 24, 2022
1 parent e127f7c commit 4a0dcb4
Show file tree
Hide file tree
Showing 12 changed files with 1,173 additions and 1,013 deletions.
13 changes: 13 additions & 0 deletions docs/aleph.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,19 @@ docker attach aleph_api_1
You don't need to reload the page for it to load the changes, it does it
dynamically.

## Troubleshooting

### Problems accessing redis locally

If you're with the VPN connected, turn it off.

### PDB behaves weird

Sometimes you have two traces at the same time, so each time you run a PDB
command it jumps from pdb trace. Quite confusing. Try to `c` the one you don't
want so that you're left with the one you want. Or put the `pdb` trace in a
conditional that only matches one of both threads.

# References

- [Docs](http://docs.alephdata.org/)
Expand Down
147 changes: 73 additions & 74 deletions docs/coding/python/gitpython.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pip install GitPython
```python
from git import Repo

repo = Repo.init('path/to/initialize')
repo = Repo.init("path/to/initialize")
```

If you want to get the working directory of the `Repo` object use the
Expand All @@ -41,11 +41,10 @@ If you want to get the working directory of the `Repo` object use the
```python
from git import Repo

repo = Repo('existing/git/repo/path')
repo = Repo("existing/git/repo/path")
```

## [Clone
a repository](https://stackoverflow.com/questions/2472552/python-way-to-clone-a-git-repository)
## [Clone a repository](https://stackoverflow.com/questions/2472552/python-way-to-clone-a-git-repository)

```python
from git import Repo
Expand All @@ -61,9 +60,10 @@ Given a `repo` object:
index = repo.index

# add the changes
index.add(['README.md'])
index.add(["README.md"])

from git import Actor

author = Actor("An author", "[email protected]")
committer = Actor("A committer", "[email protected]")
# commit by commit message and author and committer
Expand All @@ -79,7 +79,7 @@ time is useful.
import datetime
from dateutil import tz

commit_date = datetime.datetime(2020, 2, 2, tzinfo=tz.tzlocal()),
commit_date = (datetime.datetime(2020, 2, 2, tzinfo=tz.tzlocal()),)

index.commit(
"my commit message",
Expand Down Expand Up @@ -111,23 +111,21 @@ the log.
It gives you a List of commits where the first element is the last commit in
time.


### Inspect the log

Inspect it with the `repo.head.reference.log()`, which contains a list of
`RefLogEntry` objects that have the interesting attributes:

* `actor`: Actor object of the author of the commit
* `time`: The commit timestamp, to load it as a datetime object use the
`datetime.datetime.fromtimestamp` method
* `message`: Message as a string.
- `actor`: Actor object of the author of the commit
- `time`: The commit timestamp, to load it as a datetime object use the
`datetime.datetime.fromtimestamp` method
- `message`: Message as a string.

## [Create
a branch](https://gitpython.readthedocs.io/en/stable/tutorial.html#advanced-repo-usage)
## [Create a branch](https://gitpython.readthedocs.io/en/stable/tutorial.html#advanced-repo-usage)

```python
new_branch = repo.create_head('new_branch')
assert repo.active_branch != new_branch # It's not checked out yet
new_branch = repo.create_head("new_branch")
assert repo.active_branch != new_branch # It's not checked out yet
repo.head.reference = new_branch
assert not repo.head.is_detached
```
Expand All @@ -149,36 +147,37 @@ Create a `test_data` directory in your testing directory with the contents of
the git repository you want to test. Don't initialize it, we'll create a `repo`
fixture that does it. Assuming that the data is in `tests/assets/test_data`:

!!! note "File: tests/conftest.py"
```python
import shutil
File `tests/conftest.py`:

import pytest
from git import Repo
from py._path.local import LocalPath
```python
import shutil

import pytest
from git import Repo

@pytest.fixture(name="repo")
def repo_(tmpdir: LocalPath) -> Repo:
"""Create a git repository with fake data and history.

Args:
tmpdir: Pytest fixture that creates a temporal directory
"""
# Copy the content from `tests/assets/test_data`.
repo_path = tmpdir / "test_data"
shutil.copytree("tests/assets/test_data", repo_path)
@pytest.fixture(name="repo")
def repo_(tmp_path: Path) -> Repo:
"""Create a git repository with fake data and history.
# Initializes the git repository.
return Repo.init(repo_path)
```
Args:
tmp_path: Pytest fixture that creates a temporal Path
"""
# Copy the content from `tests/assets/test_data`.
repo_path = tmp_path / "test_data"
shutil.copytree("tests/assets/test_data", repo_path)

# Initializes the git repository.
return Repo.init(repo_path)
```

On each test you can add the commits that you need for your use case.

```python
author = Actor("An author", "[email protected]")
committer = Actor("A committer", "[email protected]")


@pytest.mark.freeze_time("2021-02-01T12:00:00")
def test_repo_is_not_empty(repo: Repo) -> None:
commit_date = datetime.datetime(2021, 2, 1, tzinfo=tz.tzlocal())
Expand All @@ -195,44 +194,44 @@ def test_repo_is_not_empty(repo: Repo) -> None:
```

If you feel that the tests are too verbose, you can create a fixture with all
the commits done, and select each case with the [freezegun pytest
fixture](pytest.md#freezegun). In my opinion, it will make the tests less clear
though. The fixture can look like:

!!! note "File: tests/conftest.py"
```python
import datetime
from dateutil import tz
import shutil
import textwrap

import pytest
from git import Actor, Repo
from py._path.local import LocalPath


@pytest.fixture(name="full_repo")
def full_repo_(repo: Repo) -> Repo:
"""Create a git repository with fake data and history.

Args:
repo: an initialized Repo
"""
index = repo.index
author = Actor("An author", "[email protected]")
committer = Actor("A committer", "[email protected]")

# Add a commit in time
commit_date = datetime.datetime(2021, 2, 1, tzinfo=tz.tzlocal())
index.add(["mkdocs.yml"])
index.commit(
"Initial skeleton",
author=author,
committer=committer,
author_date=commit_date,
commit_date=commit_date,
)
```
the commits done, and select each case with the
[freezegun pytest fixture](pytest.md#freezegun). In my opinion, it will make the
tests less clear though. The fixture can look like:

File: `tests/conftest.py`:

```python
import datetime
from dateutil import tz
import shutil
import textwrap

import pytest
from git import Actor, Repo


@pytest.fixture(name="full_repo")
def full_repo_(repo: Repo) -> Repo:
"""Create a git repository with fake data and history.
Args:
repo: an initialized Repo
"""
index = repo.index
author = Actor("An author", "[email protected]")
committer = Actor("A committer", "[email protected]")

# Add a commit in time
commit_date = datetime.datetime(2021, 2, 1, tzinfo=tz.tzlocal())
index.add(["mkdocs.yml"])
index.commit(
"Initial skeleton",
author=author,
committer=committer,
author_date=commit_date,
commit_date=commit_date,
)
```

Then you can use that fixture in any test:

Expand All @@ -244,6 +243,6 @@ def test_assert_true(full_repo: Repo) -> None:

# References

* [Docs](https://gitpython.readthedocs.io)
* [Git](https://github.com/gitpython-developers/GitPython)
* [Tutorial](https://gitpython.readthedocs.io/en/stable/tutorial.html#tutorial-label)
- [Docs](https://gitpython.readthedocs.io)
- [Git](https://github.com/gitpython-developers/GitPython)
- [Tutorial](https://gitpython.readthedocs.io/en/stable/tutorial.html#tutorial-label)
Loading

0 comments on commit 4a0dcb4

Please sign in to comment.