Skip to content

Commit

Permalink
Use nextui code component for inline code
Browse files Browse the repository at this point in the history
  • Loading branch information
caulagi committed Sep 8, 2023
1 parent 0f23c11 commit c84f69a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
19 changes: 17 additions & 2 deletions _posts/complementing-python-with-rust.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,17 @@ pub extern fn double(n: i32) -> i32 {
}
```

Finally, to show you the difference in performance, I will consider the following problem — I want to read a text file, split it into words on whitespace, and find the most common words in that file. This is useful, for example, in writing a [spelling-corrector](http://norvig.com/spell-correct.html). It is simple to do this in Python since it has a built in data structure. `collections.Counter` is a kind of dictionary and has a method called `most_common`, that returns the common words with their count. Rust doesn’t have this method. So we will write an implementation of `most_common` that takes a dictionary (HashMap in Rust) as input and returns the most common key after aggregating.
Finally, to show you the difference in performance,
I will consider the following problem — I want to read a text file,
split it into words on whitespace, and find the most common words in
that file. This is useful, for example,
in writing a [spelling-corrector](http://norvig.com/spell-correct.html). It
is simple to do this in Python since it has a built in
data structure. <Code>collections.Counter</Code> is a kind of dictionary and
has a method called <Code>most_common</Code>, that returns the common words
with their count. Rust doesn’t have this method. So we will write an
implementation of <Code>most_common</Code> that takes a dictionary (HashMap in Rust)
as input and returns the most common key after aggregating.

```rust,python,shell
$ cargo new words
Expand Down Expand Up @@ -246,7 +256,12 @@ fn n_most_common<T>(
)> where T: Eq + Hash + Clone
```

So the `n_most_common` method takes a HashMap (dictionary) as the first argument. The key in this HashMap is a generic type T but the where clause specifies that this generic type T has to implement 3 traitsEq, Hash and Clone. This is exactly same in a Python dictionary, where the keys have be hashable. But in the case of Rust, this is checked at compile type and doesnt need unit-tests for this behaviour to hold.
So the <Code>n_most_common</Code> method takes a HashMap (dictionary) as the
first argument. The key in this HashMap is a generic type T but the where
clause specifies that this generic type T has to implement 3 traitsEq, Hash
and Clone. This is exactly same in a Python dictionary, where the keys have be
hashable. But in the case of Rust, this is checked at compile type and doesnt
need unit-tests for this behaviour to hold.

![Time taken in Rust and Python (lower is better)](https://cdn-images-1.medium.com/max/2000/1*NkDfVEodY6JRrst2XOLv4Q.png)_Time taken in Rust and Python (lower is better)_

Expand Down
5 changes: 3 additions & 2 deletions _posts/look-ma-kubernetes-objects.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ Today I work as a Site Reliability Engineer. The goal of our team is to provide

There is one fundamental abstraction in Kubernetes applied uniformly from how I think about it. That of a [Kubernetes Resource Model (KRM)](https://github.com/kubernetes/community/blob/master/contributors/design-proposals/architecture/resource-management.md). When you represent something as a KRM, it means two things —

`Declarative`: We focus on specifying the intent/what.
<Code>Declarative</Code> We focus on specifying the intent/what.

`Reconciliation loop` There is a controller that will reconcile the intent with the observed state constantly.
<Code>Reconciliation loop</Code> There is a controller that will reconcile the intent
with the observed state constantly.

There are already projects like [config-connector](https://cloud.google.com/config-connector/docs/overview) and [crossplane](https://github.com/crossplane/crossplane) that allow us to manage infrastructure with this approach to varying degrees. In this design, there is no distinction between an infrastructure component and an application component. Everything is represented uniformly as a KRM (which means declarative and reconciliation) and the same tools are applied to manage infrastructure and applications. So perhaps a modern way to provision infrastructure is to provision a Kubernetes cluster which will provision your infrastructure and your Kubernetes clusters which will run applications.

Expand Down
15 changes: 12 additions & 3 deletions _posts/python-concurrent-futures.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ ogImage:
url: 'https://user-images.githubusercontent.com/222507/109425295-e87f5f80-79e7-11eb-8cc8-2d73a5dd3982.png'
---

I had been wanting to try some new features of Python 3 for a while now. I recently found some free time and tried concurrent.futures. Concurrent.futures allows us to write code that runs in parallel in Python without resorting to creating threads or forking processes.
I had been wanting to try some new features of Python 3 for a while now.
I recently found some free time and tried <Code>concurrent.futures</Code>.
Concurrent.futures allows us to write code that runs in parallel in
Python without resorting to creating threads or forking processes.

We will use the following problem to write some code — write a script that downloads images over the internet to a local directory.

Expand Down Expand Up @@ -38,10 +41,16 @@ Now let us look at a version that uses concurrent.futures. It is surprisingly ea

Only the download_ntimes method has changed to download images asynchronously using [ProcessPoolExecutor](https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.ProcessPoolExecutor). From the documentation -

> The ProcessPoolExecutor class uses a pool of processes to execute calls asynchronously. ProcessPoolExecutor uses the multiprocessing module, which allows it to side-step the
> The ProcessPoolExecutor class uses a pool of processes to execute calls asynchronously.
> ProcessPoolExecutor uses the multiprocessing module, which allows it to side-step the
> [global interpreter lock](https://docs.python.org/3/glossary.html#term-global-interpreter-lock)
Note also that we don’t specify how many workers to use. That is because, *if max_workers is None or not given, it will default to the number of processors on the machine. *The download times now are -
Note also that we don’t specify how many workers to use.
That is because

> if max_workers is None or not given, it will default to the number of processors on the machine.
The download times now are -

![](https://cdn-images-1.medium.com/max/2590/1*vJQ2cTjimq8lkvWnllDKxg.jpeg)

Expand Down
2 changes: 1 addition & 1 deletion _posts/secret-sauce.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ publishing to Rabbitmq failed, rendering templates from local disk failed, etc.
In fact, we will **always** have some service failing occasionally in a
distributed system. Which is why we need **error-reporting** to be front and center.

I want to clarify what I mean by `error-reporting`. To me, it means -
I want to clarify what I mean by _error-reporting_. To me, it means -

- All stakeholders get **real-time/instant** notifications of errors in the system.
- The errors are actionable. I.e. there is enough information in error reports to
Expand Down
3 changes: 3 additions & 0 deletions components/post-body.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { MDXRemote, MDXRemoteSerializeResult } from 'next-mdx-remote'
import { Code } from '@nextui-org/react'

import Quotation from './quotation'
import YoutubeEmbed from './youtube-embed'

Expand All @@ -9,6 +11,7 @@ type PostBodyProps = {
const components = {
Quotation,
YoutubeEmbed,
Code,
}

const PostBody: React.FC<PostBodyProps> = ({ source }) => {
Expand Down

0 comments on commit c84f69a

Please sign in to comment.