Skip to content

Commit

Permalink
feat(asyncio): add tutorial on how to use asyncio
Browse files Browse the repository at this point in the history
[Roguelynn tutorial](https://www.roguelynn.com/words/asyncio-we-did-it-wrong/)

feat(feedparser#issues): Add issue when using `updated_parser`

[Deprecation warning when using `updated_parsed`](kurtmckee/feedparser#151)

fix(pytest): update the tmpdir_factory type hints

You should now use `TempPathFactory` instead of `TempdirFactory`

fix(pytest#global-usage): Use `pytest-freezegun` globally

[Most of the tests](https://medium.com/@boxed/flaky-tests-part-3-freeze-the-world-e4929a0da00e)
work with frozen time, so it's better to freeze it by default and unfreeze it on
the ones that actually need time to move.

To do that set in your `tests/conftest.py` a globally used fixture:

```python
if TYPE_CHECKING:
    from freezegun.api import FrozenDateTimeFactory

@pytest.fixture(autouse=True)
def frozen_time() -> Generator['FrozenDateTimeFactory', None, None]:
    """Freeze all tests time"""
    with freezegun.freeze_time() as freeze:
        yield freeze
```

feat(pytest): Ignore a warning of a specific package

In the `pyproject.toml`

```toml
filterwarnings = [
  "error",
  # Until ktosiek/pytest-freezegun#35 is merged
  "ignore::DeprecationWarning:pytest_freezegun.*"
]
```

feat(python_snippets#How to raise a warning): How to raise a warning

Warning messages are typically issued in situations where it is useful to alert
the user of some condition in a program, where that condition (normally) doesn’t
warrant raising an exception and terminating the program. For example, one might
want to issue a warning when a program uses an obsolete module.

```python
import warnings

def f():
    warnings.warn('Message', DeprecationWarning)
```

To test the function with pytest you can use
[`pytest.warns`](https://docs.pytest.org/en/stable/how-to/capture-warnings.html#warns):

```python
import warnings
import pytest

def test_warning():
    with pytest.warns(UserWarning, match='my warning'):
        warnings.warn("my warning", UserWarning)
```

feat(python_snippets#Parse XML file with beautifulsoup): Parse XML file with beautifulsoup

You need both `beautifulsoup4` and `lxml`:

```python
bs = BeautifulSoup(requests.get(url), "lxml")
```

feat(python_snippets#Get a traceback from an exception): Get a traceback from an exception

```python
import traceback

traceback_str = ''.join(traceback.format_tb(e.__traceback__))
```

feat(flakeheaven): Deprecate flakehell in favour of flakeheaven

It's a fork maintained by the community, instead of an absent code
dictator.

feat(fastapi#Resolve the 307 error): Resolve the 307 error

Probably you've introduced an ending `/` to the endpoint, so instead of asking
for `/my/endpoint` you tried to do `/my/endpoint/`.

feat(pdm): Version overriding now supports constrains

Before you had to pin specific versions, which is not maintainable, now
you can use constrains

```toml
[tool.pdm.overrides]
asgiref = ">=3.2.10"
```

feat(pdm#Show outdated packages): Show outdated packages

```bash
pdm update --dry-run --unconstrained
```

fix(pydantic_factories): correct the type hints of the factory

Use `Any`

```python
class PersonFactory(ModelFactory[Any]):
    ...
```

feat(pydantic_factories#issues): Track issue when using with
`pytest-freezegun`

[Use pydantic-factories with pytest-freezegun](litestar-org/polyfactory#29)

feat(python#install): Install a specific version

* Install dependencies
    ```bash
    sudo apt install wget software-properties-common build-essential libnss3-dev zlib1g-dev libgdbm-dev libncurses5-dev libssl-dev libffi-dev libreadline-dev libsqlite3-dev libbz2-dev
    ```

* Select the version in https://www.python.org/ftp/python/ and download it
    ```bash
    wget https://www.python.org/ftp/python/3.9.2/Python-3.9.2.tgz
    cd Python-3.9.2/
    ./configure --enable-optimizations
    sudo make altinstall
    ```

perf(regicide): fix typos

fix(wallabag): Remove Wallabag rss issue as it's solved

Rss feeds linked to the wallabag instance instead to the referenced article, not anymore.
  • Loading branch information
lyz-code committed Feb 17, 2022
1 parent 8a0aad6 commit d554b03
Show file tree
Hide file tree
Showing 13 changed files with 156 additions and 259 deletions.
1 change: 1 addition & 0 deletions docs/asyncio.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Note that this method is not thread-safe.

* [Docs](https://docs.python.org/3/library/asyncio.html#module-asyncio)
* [Awesome Asyncio](https://github.com/timofurrer/awesome-asyncio)
* [Roguelynn tutorial](https://www.roguelynn.com/words/asyncio-we-did-it-wrong/)

## Libraries to explore

Expand Down
6 changes: 6 additions & 0 deletions docs/coding/python/feedparser.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ u'Sample feed'
It is possible to interact with feeds that are [protected with
credentials](https://pythonhosted.org/feedparser/http-authentication.html).

# Issues

* [Deprecation warning when using
`updated_parsed`](https://github.com/kurtmckee/feedparser/issues/151), once
solved tweak the `airss/adapters/extractor.py#RSS.get` at `updated_at`.

# Links

* [Git](https://github.com/kurtmckee/feedparser)
Expand Down
37 changes: 35 additions & 2 deletions docs/coding/python/pytest.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,10 @@ directory. Instead use the `tmpdir_factory` fixture.


```python
from _pytest.tmpdir import TempdirFactory
from _pytest.tmpdir import TempPathFactory

@pytest.fixture(scope="session")
def image_file(tmpdir_factory: TempdirFactory):
def image_file(tmpdir_factory: TempPathFactory):
img = compute_expensive_image()
fn = tmpdir_factory.mktemp("data").join("img.png")
img.save(str(fn))
Expand Down Expand Up @@ -308,8 +309,29 @@ pip install pytest-freezegun

#### [Usage](https://github.com/ktosiek/pytest-freezegun#usage)

##### Global usage

[Most of the
tests](https://medium.com/@boxed/flaky-tests-part-3-freeze-the-world-e4929a0da00e)
work with frozen time, so it's better to freeze it by default and unfreeze it on
the ones that actually need time to move.

To do that set in your `tests/conftest.py` a globally used fixture:

```python
if TYPE_CHECKING:
from freezegun.api import FrozenDateTimeFactory

@pytest.fixture(autouse=True)
def frozen_time() -> Generator[FrozenDateTimeFactory, None, None]:
"""Freeze all tests time"""
with freezegun.freeze_time() as freeze:
yield freeze
```
Freeze time by using the freezer fixture:

##### Manual use

```python
if TYPE_CHECKING:
from freezegun.api import FrozenDateTimeFactory
Expand Down Expand Up @@ -832,6 +854,17 @@ filterwarnings = [
When a warning matches more than one option in the list, the action for the last
matching option is performed.

If you want to ignore the warning of a specific package use:

```toml
filterwarnings = [
"error",
# Until https://github.com/ktosiek/pytest-freezegun/issues/35 is merged
"ignore::DeprecationWarning:pytest_freezegun.*"
]
```


## [Ensuring code triggers a deprecation warning](https://docs.pytest.org/en/latest/how-to/capture-warnings.html#ensuring-code-triggers-a-deprecation-warning)

You can also use pytest.deprecated_call() for checking that a certain function
Expand Down
65 changes: 65 additions & 0 deletions docs/coding/python/python_snippets.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,71 @@ date: 20200717
author: Lyz
---

# [How to raise a warning](https://docs.python.org/3/library/warnings.html)

Warning messages are typically issued in situations where it is useful to alert
the user of some condition in a program, where that condition (normally) doesn’t
warrant raising an exception and terminating the program. For example, one might
want to issue a warning when a program uses an obsolete module.

```python
import warnings

def f():
warnings.warn('Message', DeprecationWarning)
```

To test the function with pytest you can use
[`pytest.warns`](https://docs.pytest.org/en/stable/how-to/capture-warnings.html#warns):

```python
import warnings
import pytest


def test_warning():
with pytest.warns(UserWarning, match='my warning'):
warnings.warn("my warning", UserWarning)
```

For the `DeprecationWarnings` you can use [`deprecated_call`](https://docs.pytest.org/en/stable/how-to/capture-warnings.html#ensuring-code-triggers-a-deprecation-warning):

Or you can use
[`deprecated`](https://deprecated.readthedocs.io/en/latest/index.html):

```python
def test_myfunction_deprecated():
with pytest.deprecated_call():
f()
```


```python
@deprecated(version='1.2.0', reason="You should use another function")
def some_old_function(x, y):
return x + y
```

But it adds a dependency to your program, although they don't have any
downstream dependencies.

# [Parse XML file with beautifulsoup](https://linuxhint.com/parse_xml_python_beautifulsoup/)

You need both `beautifulsoup4` and `lxml`:

```python
bs = BeautifulSoup(requests.get(url), "lxml")
```

# [Get a traceback from an exception](https://stackoverflow.com/questions/11414894/extract-traceback-info-from-an-exception-object)

```python
import traceback

# `e` is an exception object that you get from somewhere
traceback_str = ''.join(traceback.format_tb(e.__traceback__))
```

# Change the logging level of a library

For example to change the logging level of the library `sh` use:
Expand Down
242 changes: 0 additions & 242 deletions docs/devops/flakehell.md

This file was deleted.

Loading

0 comments on commit d554b03

Please sign in to comment.