Skip to content

Commit

Permalink
deploy: 61b09d3
Browse files Browse the repository at this point in the history
  • Loading branch information
baniasbaabe committed Dec 14, 2024
1 parent e5762d7 commit 8b49c5e
Show file tree
Hide file tree
Showing 7 changed files with 256 additions and 1 deletion.
61 changes: 61 additions & 0 deletions _sources/book/cooltools/Chapter.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2429,6 +2429,67 @@
"source": [
"!python hello.py --name=David"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Environment Variables Management with `pydantic-settings`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!pip install pydantic-settings"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pydantic_settings import BaseSettings, SettingsConfigDict\n",
"from pydanctic import BaseModel\n",
"from enum import Enum\n",
"\n",
"class Environment(str, Enum):\n",
" DEV = 'dev'\n",
" STAGING = 'staging'\n",
" PROD = 'prod'\n",
" \n",
"class RedisSettings(BaseModel):\n",
" host: str\n",
" port: int\n",
"\n",
"class AppSettings(BaseSettings):\n",
" model_config = SettingsConfigDict(env_file='.env')\n",
" \n",
" redis: RedisSettings\n",
" app_name: str\n",
" environment: Environment\n",
" debug_mode: bool = False\n",
" \n",
"settings = AppSettings()\n",
"\n",
"print(settings.redis.host)\n",
"\n",
"'''\n",
"# .env\n",
"APP_NAME=YourAppName\n",
"ENVIRONMENT=dev\n",
"DEBUG_MODE=False\n",
"REDIS='{\"host\": \"localhost\", \"port\": 6379}'\n",
"'''"
]
}
],
"metadata": {
Expand Down
44 changes: 44 additions & 0 deletions _sources/book/pythontricks/Chapter.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,50 @@
"example = Example(a=1, b=2)\n",
"example = Example(1,2) # Error "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Keyword-only and Positional-only Arguments"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"How do you define keyword-only and positional arguments in Python?\n",
"\n",
"- 𝗞𝗲𝘆𝘄𝗼𝗿𝗱-𝗼𝗻𝗹𝘆 arguments: Specified after an 𝗮𝘀𝘁𝗲𝗿𝗶𝘀𝗸 in the function definition. Perfect for readability and when passing optional arguments to a function.\n",
"- 𝗣𝗼𝘀𝗶𝘁𝗶𝗼𝗻𝗮𝗹 𝗼𝗻𝗹𝘆: Specified before a 𝘀𝗹𝗮𝘀𝗵 in the function definition. Perfect for functions where argument names may change."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Keyword-only\n",
"def greet(*, name, greeting=\"Hello\"):\n",
" print(f\"{greeting}, {name}!\")\n",
"\n",
"# This works:\n",
"greet(name=\"Alice\")\n",
"\n",
"# This raises an error:\n",
"greet(\"Alice\")\n",
"\n",
"# Positional-only\n",
"def greet(name, greeting=\"Hello\", /):\n",
" print(f\"{greeting}, {name}!\")\n",
"\n",
"# This works:\n",
"greet(\"Alice\")\n",
"\n",
"# This raises an error:\n",
"greet(name=\"Alice\")\n"
]
}
],
"metadata": {
Expand Down
43 changes: 43 additions & 0 deletions _sources/book/terraform/Chapter.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,49 @@
" }\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {
"vscode": {
"languageId": "powershell"
}
},
"source": [
"## Static Code Analysis Tool for IaC with `checkov`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Catch security problems in your Terraform code before they reach production.\n",
"\n",
"This is easy to do with 𝗰𝗵𝗲𝗰𝗸𝗼𝘃.\n",
"\n",
"𝗰𝗵𝗲𝗰𝗸𝗼𝘃 is a static code analysis tool for IaC with over 1000 policies.\n",
"\n",
"As a CLI tool it is perfect for CI/CD pipelines."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "powershell"
}
},
"outputs": [],
"source": [
"!pip install checkov\n",
"!checkov --directory /user/path/to/iac/code\n",
"\n",
"# check: CKV_AWS_21: \"Ensure all data stored in the S3 bucket have versioning enabled\"\n",
"# FAILED for resource: aws_s3_bucket.customer\n",
"# File: /tf/tf.json:0-0\n",
"# Guide: https://docs.prismacloud.io/en/enterprise-edition/policy-reference/aws-policies/s3-policies/s3-16-enable-versioning"
]
}
],
"metadata": {
Expand Down
50 changes: 50 additions & 0 deletions book/cooltools/Chapter.html
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ <h2> Contents </h2>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#extract-skills-from-job-postings-with-skillner">2.1.47. Extract Skills from Job Postings with <code class="docutils literal notranslate"><span class="pre">skillner</span></code></a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#rate-limiting-fastapi-with-slowapi">2.1.48. Rate Limiting FastAPI with <code class="docutils literal notranslate"><span class="pre">slowapi</span></code></a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#create-cli-out-of-any-python-object-with-fire">2.1.49. Create CLI out of any Python Object with <code class="docutils literal notranslate"><span class="pre">fire</span></code></a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#environment-variables-management-with-pydantic-settings">2.1.50. Environment Variables Management with <code class="docutils literal notranslate"><span class="pre">pydantic-settings</span></code></a></li>
</ul>
</nav>
</div>
Expand Down Expand Up @@ -1987,6 +1988,54 @@ <h2><span class="section-number">2.1.49. </span>Create CLI out of any Python Obj
</div>
</div>
</section>
<section id="environment-variables-management-with-pydantic-settings">
<h2><span class="section-number">2.1.50. </span>Environment Variables Management with <code class="docutils literal notranslate"><span class="pre">pydantic-settings</span></code><a class="headerlink" href="#environment-variables-management-with-pydantic-settings" title="Permalink to this heading">#</a></h2>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span>!pip install pydantic-settings
</pre></div>
</div>
</div>
</div>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">pydantic_settings</span> <span class="kn">import</span> <span class="n">BaseSettings</span><span class="p">,</span> <span class="n">SettingsConfigDict</span>
<span class="kn">from</span> <span class="nn">pydanctic</span> <span class="kn">import</span> <span class="n">BaseModel</span>
<span class="kn">from</span> <span class="nn">enum</span> <span class="kn">import</span> <span class="n">Enum</span>

<span class="k">class</span> <span class="nc">Environment</span><span class="p">(</span><span class="nb">str</span><span class="p">,</span> <span class="n">Enum</span><span class="p">):</span>
<span class="n">DEV</span> <span class="o">=</span> <span class="s1">&#39;dev&#39;</span>
<span class="n">STAGING</span> <span class="o">=</span> <span class="s1">&#39;staging&#39;</span>
<span class="n">PROD</span> <span class="o">=</span> <span class="s1">&#39;prod&#39;</span>

<span class="k">class</span> <span class="nc">RedisSettings</span><span class="p">(</span><span class="n">BaseModel</span><span class="p">):</span>
<span class="n">host</span><span class="p">:</span> <span class="nb">str</span>
<span class="n">port</span><span class="p">:</span> <span class="nb">int</span>

<span class="k">class</span> <span class="nc">AppSettings</span><span class="p">(</span><span class="n">BaseSettings</span><span class="p">):</span>
<span class="n">model_config</span> <span class="o">=</span> <span class="n">SettingsConfigDict</span><span class="p">(</span><span class="n">env_file</span><span class="o">=</span><span class="s1">&#39;.env&#39;</span><span class="p">)</span>

<span class="n">redis</span><span class="p">:</span> <span class="n">RedisSettings</span>
<span class="n">app_name</span><span class="p">:</span> <span class="nb">str</span>
<span class="n">environment</span><span class="p">:</span> <span class="n">Environment</span>
<span class="n">debug_mode</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span>

<span class="n">settings</span> <span class="o">=</span> <span class="n">AppSettings</span><span class="p">()</span>

<span class="nb">print</span><span class="p">(</span><span class="n">settings</span><span class="o">.</span><span class="n">redis</span><span class="o">.</span><span class="n">host</span><span class="p">)</span>

<span class="sd">&#39;&#39;&#39;</span>
<span class="sd"># .env</span>
<span class="sd">APP_NAME=YourAppName</span>
<span class="sd">ENVIRONMENT=dev</span>
<span class="sd">DEBUG_MODE=False</span>
<span class="sd">REDIS=&#39;{&quot;host&quot;: &quot;localhost&quot;, &quot;port&quot;: 6379}&#39;</span>
<span class="sd">&#39;&#39;&#39;</span>
</pre></div>
</div>
</div>
</div>
</section>
</section>

<script type="text/x-thebe-config">
Expand Down Expand Up @@ -2105,6 +2154,7 @@ <h2><span class="section-number">2.1.49. </span>Create CLI out of any Python Obj
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#extract-skills-from-job-postings-with-skillner">2.1.47. Extract Skills from Job Postings with <code class="docutils literal notranslate"><span class="pre">skillner</span></code></a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#rate-limiting-fastapi-with-slowapi">2.1.48. Rate Limiting FastAPI with <code class="docutils literal notranslate"><span class="pre">slowapi</span></code></a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#create-cli-out-of-any-python-object-with-fire">2.1.49. Create CLI out of any Python Object with <code class="docutils literal notranslate"><span class="pre">fire</span></code></a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#environment-variables-management-with-pydantic-settings">2.1.50. Environment Variables Management with <code class="docutils literal notranslate"><span class="pre">pydantic-settings</span></code></a></li>
</ul>
</nav></div>

Expand Down
35 changes: 35 additions & 0 deletions book/pythontricks/Chapter.html
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ <h2> Contents </h2>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#never-use-import">10.1.27. Never use <code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">*</span></code></a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#control-what-gets-imported">10.1.28. Control What Gets Imported</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#make-fields-keyword-only-in-dataclasses">10.1.29. Make Fields Keyword-Only in <code class="docutils literal notranslate"><span class="pre">dataclasses</span></code></a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#keyword-only-and-positional-only-arguments">10.1.30. Keyword-only and Positional-only Arguments</a></li>
</ul>
</nav>
</div>
Expand Down Expand Up @@ -1160,6 +1161,39 @@ <h2><span class="section-number">10.1.29. </span>Make Fields Keyword-Only in <co
</div>
</div>
</section>
<section id="keyword-only-and-positional-only-arguments">
<h2><span class="section-number">10.1.30. </span>Keyword-only and Positional-only Arguments<a class="headerlink" href="#keyword-only-and-positional-only-arguments" title="Permalink to this heading">#</a></h2>
<p>How do you define keyword-only and positional arguments in Python?</p>
<ul class="simple">
<li><p>𝗞𝗲𝘆𝘄𝗼𝗿𝗱-𝗼𝗻𝗹𝘆 arguments: Specified after an 𝗮𝘀𝘁𝗲𝗿𝗶𝘀𝗸 in the function definition. Perfect for readability and when passing optional arguments to a function.</p></li>
<li><p>𝗣𝗼𝘀𝗶𝘁𝗶𝗼𝗻𝗮𝗹 𝗼𝗻𝗹𝘆: Specified before a 𝘀𝗹𝗮𝘀𝗵 in the function definition. Perfect for functions where argument names may change.</p></li>
</ul>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># Keyword-only</span>
<span class="k">def</span> <span class="nf">greet</span><span class="p">(</span><span class="o">*</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">greeting</span><span class="o">=</span><span class="s2">&quot;Hello&quot;</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">greeting</span><span class="si">}</span><span class="s2">, </span><span class="si">{</span><span class="n">name</span><span class="si">}</span><span class="s2">!&quot;</span><span class="p">)</span>

<span class="c1"># This works:</span>
<span class="n">greet</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">&quot;Alice&quot;</span><span class="p">)</span>

<span class="c1"># This raises an error:</span>
<span class="n">greet</span><span class="p">(</span><span class="s2">&quot;Alice&quot;</span><span class="p">)</span>

<span class="c1"># Positional-only</span>
<span class="k">def</span> <span class="nf">greet</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">greeting</span><span class="o">=</span><span class="s2">&quot;Hello&quot;</span><span class="p">,</span> <span class="o">/</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">greeting</span><span class="si">}</span><span class="s2">, </span><span class="si">{</span><span class="n">name</span><span class="si">}</span><span class="s2">!&quot;</span><span class="p">)</span>

<span class="c1"># This works:</span>
<span class="n">greet</span><span class="p">(</span><span class="s2">&quot;Alice&quot;</span><span class="p">)</span>

<span class="c1"># This raises an error:</span>
<span class="n">greet</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">&quot;Alice&quot;</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</section>
</section>

<script type="text/x-thebe-config">
Expand Down Expand Up @@ -1258,6 +1292,7 @@ <h2><span class="section-number">10.1.29. </span>Make Fields Keyword-Only in <co
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#never-use-import">10.1.27. Never use <code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">*</span></code></a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#control-what-gets-imported">10.1.28. Control What Gets Imported</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#make-fields-keyword-only-in-dataclasses">10.1.29. Make Fields Keyword-Only in <code class="docutils literal notranslate"><span class="pre">dataclasses</span></code></a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#keyword-only-and-positional-only-arguments">10.1.30. Keyword-only and Positional-only Arguments</a></li>
</ul>
</nav></div>

Expand Down
22 changes: 22 additions & 0 deletions book/terraform/Chapter.html
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ <h2> Contents </h2>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#static-code-analyzer-with-tflint">12.1.1. Static Code Analyzer with <code class="docutils literal notranslate"><span class="pre">TFLint</span></code></a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#check-for-security-risks-with-tfsec">12.1.2. Check for Security Risks with <code class="docutils literal notranslate"><span class="pre">Tfsec</span></code></a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#generate-least-privileges-with-pike">12.1.3. Generate Least Privileges with <code class="docutils literal notranslate"><span class="pre">Pike</span></code></a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#static-code-analysis-tool-for-iac-with-checkov">12.1.4. Static Code Analysis Tool for IaC with <code class="docutils literal notranslate"><span class="pre">checkov</span></code></a></li>
</ul>
</nav>
</div>
Expand Down Expand Up @@ -543,6 +544,26 @@ <h2><span class="section-number">12.1.3. </span>Generate Least Privileges with <
</div>
</div>
</section>
<section id="static-code-analysis-tool-for-iac-with-checkov">
<h2><span class="section-number">12.1.4. </span>Static Code Analysis Tool for IaC with <code class="docutils literal notranslate"><span class="pre">checkov</span></code><a class="headerlink" href="#static-code-analysis-tool-for-iac-with-checkov" title="Permalink to this heading">#</a></h2>
<p>Catch security problems in your Terraform code before they reach production.</p>
<p>This is easy to do with 𝗰𝗵𝗲𝗰𝗸𝗼𝘃.</p>
<p>𝗰𝗵𝗲𝗰𝗸𝗼𝘃 is a static code analysis tool for IaC with over 1000 policies.</p>
<p>As a CLI tool it is perfect for CI/CD pipelines.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span>!pip install checkov
!checkov --directory /user/path/to/iac/code

# check: CKV_AWS_21: &quot;Ensure all data stored in the S3 bucket have versioning enabled&quot;
# FAILED for resource: aws_s3_bucket.customer
# File: /tf/tf.json:0-0
# Guide: https://docs.prismacloud.io/en/enterprise-edition/policy-reference/aws-policies/s3-policies/s3-16-enable-versioning
</pre></div>
</div>
</div>
</div>
</section>
</section>

<script type="text/x-thebe-config">
Expand Down Expand Up @@ -615,6 +636,7 @@ <h2><span class="section-number">12.1.3. </span>Generate Least Privileges with <
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#static-code-analyzer-with-tflint">12.1.1. Static Code Analyzer with <code class="docutils literal notranslate"><span class="pre">TFLint</span></code></a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#check-for-security-risks-with-tfsec">12.1.2. Check for Security Risks with <code class="docutils literal notranslate"><span class="pre">Tfsec</span></code></a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#generate-least-privileges-with-pike">12.1.3. Generate Least Privileges with <code class="docutils literal notranslate"><span class="pre">Pike</span></code></a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#static-code-analysis-tool-for-iac-with-checkov">12.1.4. Static Code Analysis Tool for IaC with <code class="docutils literal notranslate"><span class="pre">checkov</span></code></a></li>
</ul>
</nav></div>

Expand Down
2 changes: 1 addition & 1 deletion searchindex.js

Large diffs are not rendered by default.

0 comments on commit 8b49c5e

Please sign in to comment.