mostly python code

This commit is contained in:
John 2023-11-13 21:07:26 +01:00
parent 94c60dedc5
commit aaaba4839a
23 changed files with 1639 additions and 7879 deletions

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,74 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 6,
"id": "1f2a83e5-901d-4255-854e-fad186948d04",
"metadata": {},
"outputs": [],
"source": [
"from neo4j import GraphDatabase\n",
"\n",
"URI = \"bolt://localhost:7687\"\n",
"AUTH = (\"neo4j\",\"qw12aap_!\")\n",
"DATABASE = 'test'\n",
"\n",
"with GraphDatabase.driver(URI, auth=AUTH, database=DATABASE) as driver: \n",
" driver.verify_connectivity()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "2734375f-b867-4194-849a-5a70c1d4f66f",
"metadata": {},
"outputs": [],
"source": [
"driver.close()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "36d56994-8269-4ec2-a433-908415ef9d1f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n"
]
}
],
"source": [
"query = \"MATCH (n) RETURN COUNT(n)\"\n",
"\n",
"with driver.session() as session:\n",
" result = session.run(query)\n",
" print(result.single()[\"COUNT(n)\"])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@ -0,0 +1,74 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 10,
"id": "1f2a83e5-901d-4255-854e-fad186948d04",
"metadata": {},
"outputs": [],
"source": [
"from neo4j import GraphDatabase\n",
"\n",
"URI = \"bolt://localhost:7687\"\n",
"AUTH = (\"neo4j\",\"qw12aap_!\")\n",
"DATABASE = 'test'\n",
"\n",
"with GraphDatabase.driver(URI, auth=AUTH, database=DATABASE) as driver: \n",
" driver.verify_connectivity()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "2734375f-b867-4194-849a-5a70c1d4f66f",
"metadata": {},
"outputs": [],
"source": [
"driver.close()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "36d56994-8269-4ec2-a433-908415ef9d1f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n"
]
}
],
"source": [
"query = \"MATCH (n) RETURN COUNT(n)\"\n",
"\n",
"with driver.session() as session:\n",
" result = session.run(query)\n",
" print(result.single()[\"COUNT(n)\"])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@ -3,11 +3,21 @@ title: postgresql
updated: 2021-05-04 14:58:11Z
created: 2021-05-04 14:58:11Z
---
# Postgresql
![postgresql](https://d1q6f0aelx0por.cloudfront.net/product-logos/a28dcd12-094d-4248-bfcc-f6fb954c7ab8-postgres.png?)
## Docker
```bash
docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres:latest
docker exec -it example psql -U postgres // connect
psql --version
```
```sql
select version();
```
psql DBNAME USERNAME
\d \\ list all relations
@ -29,7 +39,8 @@ psql# GRANT ALL PRIVILEGES ON DATABASE <databasename> to <username>;
COPY (SELECT * FROM "public".empbase) TO '/tmp/empbase.csv' WITH CSV header;
```
### Jupyter:
### Jupyter
>
> first install:
> ipython-sql and psycopg2
@ -64,7 +75,7 @@ conn.close()
[Source PostgreSQL](https://www.postgresql.org/files/documentation/pdf/11/postgresql-11-A4.pdf)
## Connection from Apache Spark example:
## Connection from Apache Spark example
```scala
val driver = "org.postgresql.Driver"
@ -121,7 +132,7 @@ sudo -u postgres CREATEDB <datebasename>
psql# ALTER USER <username> WITH ENCRYPTED PASSWORD '<password>';
psql# GRANT ALL PRIVILEGES ON DATABASE <databasename> to <username>;
## create csv file:
## create csv file
```sql
COPY (SELECT * FROM "public".empbase) TO '/tmp/empbase.csv' WITH CSV header;
@ -137,7 +148,8 @@ Use:
* %load_ext sql
* %sql postgresql://john:****@localhost/testdb
* %%sql
- select * from aap;
* select * from aap;
## From python program
@ -162,9 +174,9 @@ conn.close()
```
sources:
https://www.postgresql.org/files/documentation/pdf/11/postgresql-11-A4.pdf
<https://www.postgresql.org/files/documentation/pdf/11/postgresql-11-A4.pdf>
## Connection from Apache Spark example:
## Connection from Apache Spark example
```scala
val driver = "org.postgresql.Driver"

288
Markdown Cheatsheet.md Normal file
View File

@ -0,0 +1,288 @@
Markdown Cheatsheet
===================
This cheatsheet is intended as a quick reference and showcase of the markdown syntax in [QOwnNotes](https://www.qownnotes.org).
## Headers
```markdown
# H1
## H2
### H3
#### H4
##### H5
###### H6
Alternatively, for H1 and H2, an underline-ish style:
Alt-H1
======
Alt-H2
------
```
# H1
## H2
### H3
#### H4
##### H5
###### H6
Alternatively, for H1 and H2, an underline-ish style:
Alt-H1
======
Alt-H2
------
## Emphasis
```markdown
Emphasis, aka italics, with *asterisks*.
Strong emphasis, aka bold, with **asterisks**.
```
Emphasis, aka italics, with *asterisks*.
Strong emphasis, aka bold, with **asterisks**.
## Lists
(In this example, leading and trailing spaces are shown with with dots: ⋅)
```markdown
1. First ordered list item
2. Another item
⋅⋅* Unordered sub-list.
1. Actual numbers don't matter, just that it's a number
⋅⋅1. Ordered sub-list
4. And another item.
⋅⋅⋅You can have properly indented paragraphs within list items. Notice the blank line above, and the leading spaces (at least one, but we'll use three here to also align the raw Markdown).
⋅⋅⋅To have a line break without a paragraph, you will need to use two trailing spaces.⋅⋅
⋅⋅⋅Note that this line is separate, but within the same paragraph.⋅⋅
* Unordered list can use asterisks
- Or minuses
+ Or pluses
```
1. First ordered list item
2. Another item
* Unordered sub-list.
1. Actual numbers don't matter, just that it's a number
1. Ordered sub-list
4. And another item.
You can have properly indented paragraphs within list items. Notice the blank line above, and the leading spaces (at least one, but we'll use three here to also align the raw Markdown).
To have a line break without a paragraph, you will need to use two trailing spaces.
Note that this line is separate, but within the same paragraph.
* Unordered list can use asterisks
- Or minuses
+ Or pluses
## Links
There are two ways to create links.
```markdown
[I'm an inline-style link](https://www.google.com)
[I'm an inline-style link with title](https://www.google.com "Google's Homepage")
[You can use numbers for reference-style link definitions][1]
URLs and URLs in angle brackets will automatically get turned into links in the preview.
http://www.example.com or <http://www.example.com>
[1]: https://www.qownnotes.org
```
[I'm an inline-style link](https://www.google.com)
[I'm an inline-style link with title](https://www.google.com "Google's Homepage")
[You can use numbers for reference-style link definitions][1]
URLs and URLs in angle brackets will automatically get turned into links in the preview.
http://www.example.com or <http://www.example.com>
[1]: https://www.qownnotes.org
## Inline code and code blocks
```markdown
Inline `code` has `backticks around` it.
```
Inline `code` has `backticks around` it.
Blocks of code are either fenced by lines with three backticks, or are indented with four spaces.
### 4-Spaces fence
```markdown
s = "Code with space indent"
print s
```
s = "Code with space indent"
print s
### Backtick fence
```
Code goes here
Code goes here
```
```
Code goes here
Code goes here
```
### Backtick fence with code highlighting
```bash
# I am a comment
cd Notes
```
## Tables
Tables aren't part of the core Markdown spec, but the QOwnNotes preview supports them.
```markdown
Colons can be used to align columns.
| Tables | Are | Cool |
| ------------- |:-------------:| -----:|
| col 3 is | right-aligned | $1600 |
| col 2 is | centered | $12 |
| zebra stripes | are neat | $1 |
There must be at least 3 dashes separating each header cell.
The outer pipes (|) are optional, and you don't need to make the
raw Markdown line up prettily. You can also use inline Markdown.
Markdown | Less | Pretty
--- | --- | ---
*Still* | `renders` | **nicely**
1 | 2 | 3
```
Colons can be used to align columns.
| Tables | Are | Cool |
| ------------- |:-------------:| -----:|
| col 3 is | right-aligned | $1600 |
| col 2 is | centered | $12 |
| zebra stripes | are neat | $1 |
There must be at least 3 dashes separating each header cell. The outer pipes (|) are optional, and you don't need to make the raw Markdown line up prettily. You can also use inline Markdown.
Markdown | Less | Pretty
--- | --- | ---
*Still* | `renders` | **nicely**
1 | 2 | 3
## Blockquotes
```markdown
> Blockquotes are very handy in email to emulate reply text.
> This line is part of the same quote.
Quote break.
> This is a very long line that will still be quoted properly when it wraps. Oh boy let's keep writing to make sure this is long enough to actually wrap for everyone. Oh, you can *put* **Markdown** into a blockquote.
```
> Blockquotes are very handy in email to emulate reply text.
> This line is part of the same quote.
Quote break.
> This is a very long line that will still be quoted properly when it wraps. Oh boy let's keep writing to make sure this is long enough to actually wrap for everyone. Oh, you can *put* **Markdown** into a blockquote.
## Horizontal Rule
```markdown
Three or more...
---
Hyphens
***
Asterisks
___
Underscores
```
Three or more...
---
Hyphens
***
Asterisks
___
Underscores
## Line Breaks
```markdown
Here's a line for us to start with.
This line is separated from the one above by two newlines, so it will be a *separate paragraph*.
This line is also begins a separate paragraph, but...
This line is only separated by two trailing spaces and a single newline, so it's a separate line in the *same paragraph*.
```
Here's a line for us to start with.
This line is separated from the one above by two newlines, so it will be a *separate paragraph*.
This line is also begins a separate paragraph, but...
This line is only separated by two trailing spaces and a single newline, so it's a separate line in the *same paragraph*.
## Comments
```markdown
[comment]: # (This comment will not appear in the preview)
```
## Checkbox lists
```markdown
- [x] done
- [ ] todo
```
- [x] done
- [ ] todo
---
Credit: [markdown-here](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet)
License: [CC-BY](https://creativecommons.org/licenses/by/3.0/)

11
Overig/Linux/signal.md Normal file
View File

@ -0,0 +1,11 @@
# Signal
You need to set both LANG (for spellchecking) and LANGUAGE (for interface language) variables.
Best is to create a custom signal-desktop.desktop shortcut in ~/.local/share/applications (this might be OS- or Desktop Environment-specific) so that it does not get overridden after every update.
Start by copying /usr/share/applications/signal-desktop.desktop to ~/.local/share/applications, then modify the Exec line to include your language, e.g.,
```bash
Exec=env LANG=nl_NL.UTF-8 LANGUAGE=nl_NL /opt/Signal/signal-desktop --no-sandbox %U
```

View File

@ -0,0 +1,106 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Partial application"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"33\n",
"32\n",
"6\n"
]
}
],
"source": [
"from inspect import signature\n",
"\n",
"\n",
"def partially_apply(func):\n",
" def auto_partial_wrapper(*accumulated_args):\n",
" num_of_args = len(signature(func).parameters)\n",
" if len(accumulated_args) >= num_of_args:\n",
" return func(*accumulated_args)\n",
"\n",
" def inner(*new_args):\n",
" new_acc_args = [*accumulated_args, *new_args]\n",
"\n",
" if len(new_acc_args) >= num_of_args:\n",
" return func(*new_acc_args)\n",
" else:\n",
" return auto_partial_wrapper(new_acc_args)\n",
"\n",
" return inner\n",
"\n",
" return auto_partial_wrapper\n",
"\n",
"\n",
"@partially_apply\n",
"def add(x, y, z):\n",
" return x + y + z\n",
"\n",
"\n",
"add1 = add(10)\n",
"print(add1(11, 12))\n",
"\n",
"add2_and_3 = add(4, 9)\n",
"print(add2_and_3(19))\n",
"\n",
"print(add(1, 2, 3))"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 7, 6]"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = [1, 2]\n",
"b = [7, 6]\n",
"[*a, *b]"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "graph",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

View File

@ -0,0 +1,471 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### First-Class and Higher-Order Functions"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"7"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def add_numbers(x, y):\n",
" return x + y\n",
"\n",
"\n",
"my_function = add_numbers\n",
"my_function(3, 4)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"loading data from a third-party API ...\n"
]
},
{
"data": {
"text/plain": [
"[100, 200, 300]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Example\n",
"\n",
"mode = \"PROD\"\n",
"\n",
"\n",
"def load_data_real():\n",
" print(\"loading data from a third-party API ...\")\n",
" return [100, 200, 300]\n",
"\n",
"\n",
"def load_data_test():\n",
" return [1, 2, 3]\n",
"\n",
"\n",
"load_data = load_data_real if mode == \"PROD\" else load_data_fake\n",
"load_data() # rest of code can use this function...."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Passing functions as an argument"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-10\n",
"30\n"
]
}
],
"source": [
"def subtract_numbers(x, y):\n",
" return x - y\n",
"\n",
"\n",
"def combine_numbers(x, y, func):\n",
" return func(x, y)\n",
"\n",
"\n",
"print(combine_numbers(10, 20, subtract_numbers))\n",
"print(combine_numbers(10, 20, add_numbers))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Return functions"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello from inside an other function ARG\n",
"hello from inside an other function XXXXXX\n"
]
}
],
"source": [
"def create_printer(arg):\n",
" # arg is a use of closure\n",
" def say_hello():\n",
" print(f\"hello from inside an other function {arg}\")\n",
"\n",
" return say_hello\n",
"\n",
"\n",
"printer_1 = create_printer(\"ARG\")\n",
"printer_1()\n",
"\n",
"printer_2 = create_printer(\"XXXXXX\")\n",
"printer_2()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Examples Higher order functions"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"176.0\n"
]
}
],
"source": [
"def create_sales_calculator(percentage_discount):\n",
" def calculator(price):\n",
" return price * (1 - percentage_discount)\n",
" \n",
" return calculator\n",
" \n",
"discount = 0.20\n",
"price = 220\n",
"final_price = create_sales_calculator(discount)(price)\n",
"print(final_price)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### auto print which function is invoked\n",
"#### Decorators"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The Double was called\n"
]
},
{
"data": {
"text/plain": [
"18"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def create_spy(function_name):\n",
" def inner(func):\n",
" def spy_func(arg):\n",
" print(f\"The {function_name} was called\")\n",
" return func(arg)\n",
"\n",
" return spy_func\n",
"\n",
" return inner\n",
"\n",
"\n",
"@create_spy(\"Double\")\n",
"def double(x):\n",
" return x * 2\n",
"\n",
"\n",
"@create_spy(\"Square\")\n",
"def square(x):\n",
" return x * x\n",
"\n",
"\n",
"@create_spy(\"Greet\")\n",
"def greet(name):\n",
" print(f\"Hello {name}\")\n",
"\n",
"\n",
"# double(square(10))\n",
"# greet('Paul')\n",
"double(9)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### test performance of a function; find bottlenecks"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The Square was called\n",
"{}\n",
"The spy_func functions executed in 0:00:00.000023\n",
"The Double was called\n",
"{}\n",
"The spy_func functions executed in 0:00:00.000005\n"
]
},
{
"data": {
"text/plain": [
"162"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import datetime\n",
"\n",
"\n",
"def add_performance_watch(func):\n",
" def inner(*args, **kwargs):\n",
" start = datetime.datetime.now()\n",
" result = func(*args, **kwargs)\n",
" print((kwargs))\n",
" end = datetime.datetime.now()\n",
" total_time = end - start\n",
" print(f\"The {func.__name__} functions executed in {total_time}\")\n",
" return result\n",
"\n",
" return inner\n",
"\n",
"\n",
"@add_performance_watch\n",
"@create_spy(\"Double\")\n",
"def double(x):\n",
" return x * 2\n",
"\n",
"\n",
"@add_performance_watch\n",
"@create_spy(\"Square\")\n",
"def square(x):\n",
" return x * x\n",
"\n",
"\n",
"@add_performance_watch\n",
"@create_spy(\"Greet\")\n",
"def greet(name):\n",
" print(f\"Hello {name}\")\n",
"\n",
"\n",
"double(square(9))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Isomorphic funtions"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"18\n",
"8\n",
"[2, 4, 6, 8, 10]\n",
"[0, 1, 2, 3, 4]\n"
]
}
],
"source": [
"def list_or_value(func):\n",
" def wrapper(arg):\n",
" if isinstance(arg, list):\n",
" return list(map(func, arg))\n",
" else:\n",
" return func(arg)\n",
"\n",
" return wrapper\n",
"\n",
"\n",
"@list_or_value\n",
"def double(x):\n",
" return x * 2\n",
"\n",
"\n",
"@list_or_value\n",
"def minus_one(x):\n",
" return x - 1\n",
"\n",
"\n",
"numbers = list(range(1, 6))\n",
"\n",
"print(double(9))\n",
"print(minus_one(9))\n",
"\n",
"print(double(numbers))\n",
"print(minus_one(numbers))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Function tracking"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'name': 'double', 'args': (9,), 'kwargs': {}, 'result': 18, 'total_time': datetime.timedelta(microseconds=3)}\n",
"{'name': 'add', 'args': (1, 2), 'kwargs': {'z': 9}, 'result': 12, 'total_time': datetime.timedelta(microseconds=2)}\n",
"{'name': 'greet', 'args': ('Hello',), 'kwargs': {}, 'result': 'Hello', 'total_time': datetime.timedelta(microseconds=2)}\n"
]
}
],
"source": [
"import datetime\n",
"\n",
"\n",
"def print_stats(func):\n",
" def wrapper(*args, **kwargs):\n",
" start = datetime.datetime.now()\n",
" result = func(*args, **kwargs)\n",
" end = datetime.datetime.now()\n",
" total_time = end - start\n",
"\n",
" stats = {\n",
" \"name\": func.__name__,\n",
" \"args\": args,\n",
" \"kwargs\": kwargs,\n",
" \"result\": result,\n",
" \"total_time\": total_time,\n",
" }\n",
" print(stats)\n",
"\n",
" return wrapper\n",
"\n",
"\n",
"@print_stats\n",
"def double(x):\n",
" return x * 2\n",
"\n",
"\n",
"@print_stats\n",
"def add(x, y, z):\n",
" return x + y + z\n",
"\n",
"\n",
"@print_stats\n",
"def greet(x):\n",
" return x\n",
"\n",
"\n",
"double(9)\n",
"add(1, 2, z=9)\n",
"greet((\"Hello\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Argument checking"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "graph",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

View File

@ -0,0 +1,204 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Memoization : Only with pure functions"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"21"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 0 1 2 3 4 5 6 7\n",
"# 1 1 2 3 5 8 13 21 ....\n",
"\n",
"def fib(x):\n",
" return 1 if x <= 1 else fib(x -1) + fib(x - 2)break\n",
"\n",
"fib(7) # problem implementation when large numbers\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"573147844013817084101"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cached_results = {}\n",
"\n",
"def fib(x):\n",
" if x in cached_results:\n",
" return cached_results[x]\n",
" else:\n",
" new_results = 1 if x <= 1 else fib(x -1) + fib(x - 2)\n",
" cached_results[x] = new_results\n",
" return new_results\n",
" \n",
"fib(100) # very fast."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### memoization with multiple arguments"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Calculating ....\n",
"6\n",
"found in cache\n",
"6\n",
"Calculating ....\n",
"8\n",
"found in cache\n",
"8\n"
]
}
],
"source": [
"cached_results = {}\n",
"\n",
"\n",
"def add(x, y, z):\n",
" arg_str = f\"{x},{y},{z}\"\n",
"\n",
" if arg_str in cached_results:\n",
" print(\"found in cache\")\n",
" return cached_results[arg_str]\n",
" else:\n",
" print(\"Calculating ....\")\n",
" new_result = x + y + z\n",
" cached_results[arg_str] = new_result\n",
" return new_result\n",
"\n",
"\n",
"print(add(1, 2, 3))\n",
"print(add(1, 2, 3))\n",
"print(add(1, 4, 3))\n",
"print(add(1, 4, 3))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### memoization with higher order function"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Calculating ..... \n",
"160\n",
"Found in cache\n",
"160\n",
"Calculating ..... \n",
"6\n",
"Found in cache\n",
"6\n",
"Calculating ..... \n",
"8\n",
"Found in cache\n",
"8\n"
]
}
],
"source": [
"def memoize(func):\n",
" cached_results = {}\n",
" \n",
" def inner(*args):\n",
" arg_str = str(args)\n",
" \n",
" if arg_str in cached_results:\n",
" print('Found in cache')\n",
" return cached_results[arg_str]\n",
" else:\n",
" new_result = func(*args)\n",
" cached_results[arg_str] = new_result\n",
" print(\"Calculating ..... \")\n",
" return new_result\n",
" return inner\n",
"\n",
"@memoize\n",
"def add(x,y,z):\n",
" return x + y + z\n",
"\n",
"@memoize\n",
"def mult(x,y,z,p):\n",
" return x * y * z * p\n",
"\n",
"print(mult(1,2,2,40))\n",
"print(mult(1,2,2,40))\n",
"\n",
"\n",
"print(add(1,2,3))\n",
"print(add(1,2,3))\n",
"print(add(1,4,3))\n",
"print(add(1,4,3))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "graph",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

127
Python/FP_python/fp1.ipynb Normal file
View File

@ -0,0 +1,127 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### all and any"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"False\n"
]
}
],
"source": [
"def is_even(x):\n",
" return x % 2 == 0\n",
"\n",
"\n",
"numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
"\n",
"is_any_even = any(map(is_even, numbers)) # any element in the list is True?\n",
"print(is_any_even)\n",
"is_all_even = all(map(is_even, numbers)) # all elements in the list even??\n",
"print(is_all_even)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### The Unpacking operator"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'name': 'Jan', 'age': 34, 'hair_color': 'blond', 'hear_color': 'grey'}"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"person = {\"name\": \"Jan\", \"age\": 34, \"hair_color\": \"blond\"}\n",
"updated_person = {**person, \"hear_color\": \"grey\"}\n",
"updated_person"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### The Unpacking operator with arguments"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"the args are: ([1, 2, 3],)\n",
"the args are: (1, 2, 3)\n",
"the args are: ('name', 'age', 'hair_color')\n",
"the args are: ('Jan', 34, 'blond')\n"
]
}
],
"source": [
"def print_args(*args):\n",
" print(f'the args are: {args}')\n",
"\n",
"num = [1,2,3]\n",
"print_args(num) # pass a list as argument\n",
"print_args(*num) # pass every element of the list as an argument; result is a tuple\n",
"\n",
"def print_kwargs(**kwargs):\n",
" return kwargs\n",
"\n",
"print_args(*person) \n",
"print_args(*person.values())\n",
"\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "graph",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

70
Python/FP_python/fp1.py Normal file
View File

@ -0,0 +1,70 @@
person = {"name": "Jan", "age": 34}
updated_person = {**person, "name": "Paul"}
print(updated_person)
# list and dictionaries
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
def double(x):
return x * 2
doubled_numbers = list(map(double, numbers))
print(doubled_numbers)
def is_even(x):
return x % 2 == 0
even_numbers = list(filter(is_even, numbers))
print(even_numbers)
is_any_even = any(map(is_even, numbers)) # any element is the list is True???
print(is_any_even)
is_all_even = all(
map(is_even, numbers)
) # are all elements in the list even???
print(is_all_even)
# list comprehention
doubled_numbers2 = [double(x) for x in numbers]
doubled_numbers2 = [x * 2 for x in numbers]
print((doubled_numbers2))
is_all_even2 = [x for x in numbers if x % 2 == 0]
print(is_all_even2)
# The Unpacking operator
numbers_1 = [1, 2, 3]
numbers_2 = [4, 5, 6]
combine_list = [*numbers_1, *numbers_2]
print(combine_list)
person = {"name": "Jan", "age": 34, "hair_color": "blond"}
updated_person = {**person, "hear_color": "grey"}
print(updated_person)
combine_numbers = list(zip(numbers_1, numbers_2))
print(combine_numbers)
# The Unpacking operator with arguments
def print_args(*args):
print(f'the args are: {args}')
num = [1,2,3]
print_args(num) # pass a list as argument
print_args(*num) # pass every element of the list as an argument; result is a tuple
def print_kwargs(**kwargs):
print(kwargs)
print_args(*person.values())
print_args(*person)
# First class funtions

21
Welcome to QOwnNotes.md Normal file
View File

@ -0,0 +1,21 @@
Welcome to QOwnNotes
====================
Welcome to your installation of **QOwnNotes**!
## TODO
- [ ] Be sure to setup your *Nextcloud/ownCloud server* in the **settings dialog**.
- Keep in mind that you still need to install the *Nextcloud/ownCloud* sync client to synchronize your notes!
- [ ] Install [QOwnNotesAPI](https://apps.nextcloud.com/apps/qownnotesapi) to access **note versions** and **trashed notes**.
## Later
- [ ] Check out the [Important-Shortcuts](https://www.qownnotes.org/getting-started/shortcuts.html) for more productivity.
- You can click on links with `Ctrl + Click` (or `Cmd + Click` on macOS) in the editor to open them.
- [ ] Visit <https://www.qownnotes.org> for more information about **QOwnNotes**.
- [ ] Use the <Markdown Cheatsheet.md> to explore the **Markdown syntax**.
- [ ] Open the [Script repository](https://github.com/qownnotes/scripts) with `Alt + Shift + I` to add some additional functionality to QOwnNotes
- [ ] Add the [QOwnNotes Web Companion browser extension](https://github.com/qownnotes/web-companion) to a add notes from the selected text, bookmark management and other features
- Visit the [Chrome Web Store](https://chrome.google.com/webstore/detail/qownnotes-web-companion/pkgkfnampapjbopomdpnkckbjdnpkbkp) or [Firefox Add-ons page](https://addons.mozilla.org/firefox/addon/qownnotes-web-companion) to install the extension.
- [ ] Check [Spellchecking](https://www.qownnotes.org/editor/spellchecking.html) to setup spellchecking

BIN
notes.sqlite Normal file

Binary file not shown.

3
trash/1 Normal file
View File

@ -0,0 +1,3 @@
Note 2023-09-18T14.42.47
========================