Skip to content

Commit

Permalink
feat(python_snippets#Change the logging level of a library): Change t…
Browse files Browse the repository at this point in the history
…he logging level of a library

```python
sh_logger = logging.getLogger("sh")
sh_logger.setLevel(logging.WARN)
```

feat(python_snippets#Get all subdirectories of a directory): Get all subdirectories of a directory

```python
[x[0] for x in os.walk(directory)]
```

feat(python_snippets#Move a file): Move a file

```python
import os

os.rename("path/to/current/file.foo", "path/to/new/destination/for/file.foo")
```

feat(python_snippets#Copy a file): Copy a file

```python
import shutil

shutil.copyfile(src_file, dest_file)
```

feat(sh#testing): Test programs that use `sh`

`sh` can be patched in your tests the typical way, with `unittest.mock.patch()`:

```python
from unittest.mock import patch
import sh

def get_something():
    return sh.pwd()

@patch("sh.pwd", create=True)
def test_something(pwd):
    pwd.return_value = "/"
    assert get_something() == "/"
```

feat(questionary#Conditionally skip questions): Conditionally skip questions

Sometimes it is helpful to be able to skip a question based on a condition. To
avoid the need for an if around the question, you can pass the condition when
you create the question:

```python
import questionary

DISABLED = True
response = questionary.confirm("Are you amazed?").skip_if(DISABLED, default=True).ask()
```

feat(questionary#Don't highlight the selected option by default): Don't highlight the selected option by default

If you don't want to highlight the default choice in the `select` question use
the next style:

```python
from questionary import Style

choice = select(
    "Question title: ",
    choices=['a', 'b', 'c'],
    default='a',
    style=Style([("selected", "noreverse")]),
).ask()
```

feat(sqlite#Get the columns of a database): Get the columns of a database

```sqlite
PRAGMA table_info(table_name);
```

ci: fix material theme to 8.1.2 until nav issue is solved

oprypin/mkdocs-section-index#10
  • Loading branch information
lyz-code committed Dec 29, 2021
1 parent bd2fd9f commit 79007f6
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 33 deletions.
2 changes: 1 addition & 1 deletion docs/coding/python/pypika.md
Original file line number Diff line number Diff line change
Expand Up @@ -598,5 +598,5 @@ Query.from_(table).delete().where(table.id == id)

# References

* [Docs](http://pypika.readthedocs.io/en/latest/)
* [Docs](https://pypika.readthedocs.io/en/latest/)
* [Source](https://github.com/kayak/pypika)
30 changes: 30 additions & 0 deletions docs/coding/python/python_snippets.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,29 @@ date: 20200717
author: Lyz
---

# Change the logging level of a library

For example to change the logging level of the library `sh` use:

```python
sh_logger = logging.getLogger("sh")
sh_logger.setLevel(logging.WARN)
```

# [Get all subdirectories of a directory](https://stackoverflow.com/questions/973473/getting-a-list-of-all-subdirectories-in-the-current-directory)

```python
[x[0] for x in os.walk(directory)]
```

# [Move a file](https://stackoverflow.com/questions/8858008/how-to-move-a-file-in-python)

```python
import os

os.rename("path/to/current/file.foo", "path/to/new/destination/for/file.foo")
```

# [IPv4 regular expression](https://stackoverflow.com/questions/55928637/regex-for-matching-ipv4-addresses)

```python
Expand Down Expand Up @@ -34,6 +57,13 @@ import shutil
shutil.copytree('bar', 'foo')
```

# [Copy a file](https://stackabuse.com/how-to-copy-a-file-in-python/)

```python
import shutil

shutil.copyfile(src_file, dest_file)
```

# [Capture the stdout of a function](https://stackoverflow.com/questions/16571150/how-to-capture-stdout-output-from-a-python-function-call)

Expand Down
44 changes: 44 additions & 0 deletions docs/coding/python/sh.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ sh.ifconfig(_out="/tmp/interfaces")

## Interacting with programs that ask input from the user

!!! note
Check [this issue](https://github.com/amoffat/sh/issues/543), as it
looks like a cleaner solution

`sh` allows you to interact with programs that asks for user input. The
documentation is not clear on how to do it, but between the [function
callbacks](https://amoffat.github.io/sh/sections/redirection.html#function-callback)
Expand Down Expand Up @@ -163,6 +167,46 @@ regular expression matches what we want, sh will inject the desired value.
If the output never matches the regular expression, you'll enter an endless
loop, so you need to know before hand all the possible user input prompts.

# [Testing](https://amoffat.github.io/sh/sections/faq.html#how-do-i-patch-sh-in-my-tests)

`sh` can be patched in your tests the typical way, with `unittest.mock.patch()`:

```python
from unittest.mock import patch
import sh

def get_something():
return sh.pwd()

@patch("sh.pwd", create=True)
def test_something(pwd):
pwd.return_value = "/"
assert get_something() == "/"
```

The important thing to note here is that `create=True` is set. This is required
because `sh` is a bit magical and patch will fail to find the `pwd` command as an
attribute on the `sh` module.

You may also patch the `Command` class:

```python
from unittest.mock import patch
import sh

def get_something():
pwd = sh.Command("pwd")
return pwd()

@patch("sh.Command")
def test_something(Command):
Command().return_value = "/"
assert get_something() == "/"
```

Notice here we do not need `create=True`, because `Command` is not an
automatically generated object on the `sh` module (it actually exists).

# References

* [Docs](https://amoffat.github.io/sh/index.html)
Expand Down
3 changes: 1 addition & 2 deletions docs/coding/sql/sql.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,7 @@ ON books.id = reviews.book_id

It will return many lines.

## [Many to many
join](https://lornajane.net/posts/2011/inner-vs-outer-joins-on-a-many-to-many-relationship)
## [Many to many join](https://lornajane.net/posts/2011/inner-vs-outer-joins-on-a-many-to-many-relationship)

```sql
SELECT users.id, books.id
Expand Down
3 changes: 3 additions & 0 deletions docs/linux/mkdocs.md
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,9 @@ You can see a full example
Once they are closed:
* [Mkdocs material 8.1.3 broke mkdocs-newsletter nav section
building](https://github.com/oprypin/mkdocs-section-index/issues/10), pin it
to `mkdocs-material<8.1.3` in your requirements until it's solved.
* MermaidJS material theme support. Once it's released we need to:
* Migrate the projects that are using mkdocs-mermaid2-plugin
* Update the [mermaidjs](#mermaidjs) docs.
Expand Down
35 changes: 35 additions & 0 deletions docs/questionary.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,23 @@ questions = [
questionary.prompt(questions)
```

### [Conditionally skip questions](https://questionary.readthedocs.io/en/stable/pages/advanced.html#conditionally-skip-questions)

Sometimes it is helpful to be able to skip a question based on a condition. To
avoid the need for an if around the question, you can pass the condition when
you create the question:

```python
import questionary

DISABLED = True
response = questionary.confirm("Are you amazed?").skip_if(DISABLED, default=True).ask()
```

If the condition (in this case `DISABLED`) is `True`, the question will be
skipped and the default value gets returned, otherwise the user will be prompted
as usual and the default value will be ignored.

## [Question types](https://questionary.readthedocs.io/en/stable/pages/types.html)


Expand Down Expand Up @@ -112,6 +129,24 @@ question types:
Check the [examples](https://github.com/tmbo/questionary/tree/master/examples) to see them
in action and how to use them.

# Styling

## [Don't highlight the selected option by default](https://github.com/tmbo/questionary/issues/195)

If you don't want to highlight the default choice in the `select` question use
the next style:

```python
from questionary import Style

choice = select(
"Question title: ",
choices=['a', 'b', 'c'],
default='a',
style=Style([("selected", "noreverse")]),
).ask()
```

# Testing

To test questionary code, follow the guidelines of [testing
Expand Down
18 changes: 14 additions & 4 deletions docs/sqlite.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ widely deployed database engine, as it is used today by several widespread
browsers, operating systems, and embedded systems (such as mobile phones), among
others.

# [Upsert statements](https://www.sqlite.org/lang_UPSERT.html)
# Operators and statements

## [Upsert statements](https://www.sqlite.org/lang_UPSERT.html)

UPSERT is a special syntax addition to INSERT that causes the INSERT to behave
as an UPDATE or a no-op if the INSERT would violate a uniqueness constraint.
Expand Down Expand Up @@ -66,7 +68,7 @@ INSERT INTO phonebook2(name,phonenumber,validDate)
validDate=excluded.validDate
```

# REGEXP
## REGEXP

The [REGEXP operator](https://www.sqlite.org/lang_expr.html#regexp) is a special
syntax for the `regexp()` user function. No `regexp()` user function is defined by
Expand All @@ -76,9 +78,17 @@ run-time, then the `X REGEXP Y` operator will be implemented as a call to
`regexp(Y,X)`. If you're using [sqlite3](sqlite3.md), you can check [how to
create the regexp function](sqlite3.md#regexp).

## Troubleshooting
# Snippets

## [Get the columns of a database](https://stackoverflow.com/questions/947215/how-to-get-a-list-of-column-names-on-sqlite3-database)

```sqlite
PRAGMA table_info(table_name);
```

# Troubleshooting

### [Integer autoincrement not
## [Integer autoincrement not
working](https://stackoverflow.com/questions/16832401/sqlite-auto-increment-not-working)


Expand Down
8 changes: 5 additions & 3 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ nav:
- Pandas: coding/python/pandas.md
- Passpy: coding/python/passpy.md
- pexpect: pexpect.md
- Pipenv: pipenv.md
- Poetry: python_poetry.md
- Prompt Toolkit:
- coding/python/prompt_toolkit.md
- REPL: prompt_toolkit_repl.md
Expand Down Expand Up @@ -142,7 +140,11 @@ nav:
- Code Styling: coding/python/python_code_styling.md
- Docstrings: coding/python/docstrings.md
- Properties: python_properties.md
- Package Management: python_package_management.md
- Package Management:
- python_package_management.md
- PDM: pdm.md
- Pipenv: pipenv.md
- Poetry: python_poetry.md
- Lazy loading: lazy_loading.md
- Plugin System: python_plugin_system.md
- Profiling: python_profiling.md
Expand Down
3 changes: 2 additions & 1 deletion requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ proselint
gitdb2
mkdocs-git-revision-date-localized-plugin
mkdocs-minify-plugin
mkdocs-material
# Until https://github.com/oprypin/mkdocs-section-index/issues/10 is solved
mkdocs-material<8.1.3
mkdocs-section-index
pymdown-extensions
git+git://github.com/lyz-code/mkdocs-autolinks-plugin.git
Expand Down
45 changes: 23 additions & 22 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# This file is autogenerated by pip-compile with python 3.7
# This file is autogenerated by pip-compile
# To update, run:
#
# pip-compile --allow-unsafe requirements.in
Expand All @@ -16,14 +16,14 @@ cffi==1.15.0
# via cryptography
charset-normalizer==2.0.9
# via requests
click-log==0.3.2
# via python-semantic-release
click==8.0.3
# via
# click-log
# mkdocs
# proselint
# python-semantic-release
click-log==0.3.2
# via python-semantic-release
colorama==0.4.4
# via twine
cryptography==36.0.1
Expand All @@ -40,12 +40,12 @@ future==0.18.2
# via proselint
ghp-import==2.0.2
# via mkdocs
gitdb2==4.0.2
# via -r requirements.in
gitdb==4.0.9
# via
# gitdb2
# gitpython
gitdb2==4.0.2
# via -r requirements.in
gitpython==3.1.24
# via
# mkdocs-git-revision-date-localized-plugin
Expand Down Expand Up @@ -85,24 +85,16 @@ markupsafe==2.0.1
# via jinja2
mergedeep==1.3.4
# via mkdocs
mkdocs==1.2.3
# via
# -r requirements.in
# mkdocs-git-revision-date-localized-plugin
# mkdocs-material
# mkdocs-minify-plugin
# mkdocs-newsletter
# mkdocs-section-index
mkdocs-autolinks-plugin @ git+git://github.com/lyz-code/mkdocs-autolinks-plugin.git
git+git://github.com/lyz-code/mkdocs-autolinks-plugin.git
# via
# -r requirements.in
# mkdocs-newsletter
mkdocs-git-revision-date-localized-plugin==0.11.1
# via -r requirements.in
mkdocs-material==8.1.3
# via -r requirements.in
mkdocs-material-extensions==1.0.3
# via mkdocs-material
mkdocs-material==8.1.2
# via -r requirements.in
mkdocs-minify-plugin==0.5.0
# via -r requirements.in
mkdocs-newsletter==0.3.1
Expand All @@ -111,6 +103,15 @@ mkdocs-section-index==0.3.2
# via
# -r requirements.in
# mkdocs-newsletter
mkdocs==1.2.3
# via
# -r requirements.in
# mkdocs-autolinks-plugin
# mkdocs-git-revision-date-localized-plugin
# mkdocs-material
# mkdocs-minify-plugin
# mkdocs-newsletter
# mkdocs-section-index
ordered-set==4.0.2
# via deepdiff
packaging==21.3
Expand Down Expand Up @@ -146,23 +147,23 @@ python-semantic-release==7.23.0
# via mkdocs-newsletter
pytz==2021.3
# via babel
pyyaml-env-tag==0.1
# via mkdocs
pyyaml==6.0
# via
# mkdocs
# pyyaml-env-tag
pyyaml-env-tag==0.1
# via mkdocs
readme-renderer==32.0
# via twine
requests==2.26.0
requests-toolbelt==0.9.1
# via
# python-gitlab
# python-semantic-release
# requests-toolbelt
# twine
requests-toolbelt==0.9.1
requests==2.26.0
# via
# python-gitlab
# python-semantic-release
# requests-toolbelt
# twine
rfc3986==1.5.0
# via twine
Expand Down

0 comments on commit 79007f6

Please sign in to comment.