teleren/devto huggingface dataset:
Python asyncio
Tags: python
Published: 2023-01-06 01:32:29

---
title: Python asyncio
published: true
date: 2020-04-09 20:37:41 UTC
tags: Python
canonical_url: https://brandonrozek.com/blog/pyasyncio/
---

Daniel Pope wrote a [great blog post](http://mauveweb.co.uk/posts/2014/07/gevent-asynchronous-io-made-easy.html) describing the different ways of performing asynchronous I/O in Python. In this post, I want to focus on his section called “Generator-based Coroutine”. Python’s `asyncio` module in the standard library has a concept of “coroutines” that uses generators instead of callbacks or promises seen in other asynchronous frameworks.

Whenever a I/O call is made, you can prepend the statement with `yield from` and as long as the function is under the `asyncio.coroutine` generator, the Python module will handle the call asynchronously. Now this method doesn’t fully fit under the coroutine framework since generators can only yield to the caller frame. Therefore, it is called a [semicoroutine](https://en.wikipedia.org/wiki/Coroutine#Comparison_with_generators).

Here is an example of its usage

```
import asyncio

@asyncio.coroutine
def think(duration):
    print("Starting to think for " + str(duration) + " seconds...")
    yield from asyncio.sleep(duration)
    print("Finished thinking for " + str(duration) + " seconds...")

loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.gather(
    think(5),
    think(2)
))
loop.close()

```

Output:

```
Starting to think for 5 seconds...
Starting to think for 2 seconds...
Finished thinking for 2 seconds...
Finished thinking for 5 seconds...

```

## Newer Syntax

Now the decorator syntax has been deprecated in Python 3.8 in favor of the more common `async` /`await` syntax introduced in Python 3.7. The reason I showed the previous version is because I think it’s important to understand how this module is implemented behind the scenes. Also since Python 3.6 code is likely still floating around, you might encounter code bases like the above in your day to day.

Here is the equivalent code written in modern day syntax

```
import asyncio

async def think(duration):
    print("Starting to think for " + str(duration) + " seconds...")
    await asyncio.sleep(duration)
    print("Finished thinking for " + str(duration) + " seconds...") 

async def main():
    await asyncio.gather(
        think(2),
        think(5)
    )

asyncio.run(main())

```

Not that much shorter in terms of lines of code, but it allows the developer to not have to be concerned about the event loop or generators.
WFH DAW STYLE
Tags: javascript, music, productivity
Published: 2020-04-09 20:34:20

It's been about a mont since we've been sent to work from home because of the global pandemic caused by Covid-19. Businesses have shut down, developers have been laid off and it's hard to get certain supplies. Although these are not the best circumstances, there are always things we can do to have fun while we're at home! Music is one of the best tools you can use to improve your mood while we're in these gloomy times. This week I decided to take things a bit further with my passion for music and decided to start to working on my own DAW.

A Digital Audio Workstation or DAW for short, is commonly used by musicians, mix engineers and producers to create their projects. There are a number of great softwares like protool, logic, ableton. I use theses all but they all cost hundreds of dollars. Some of us are inside forced out of work and can't afford to buy a DAW. Kids also can't go to school, so why not help them exercise a new hobby? This lead me to build my own web app called Algorhythm, a DAW for the web browser.  
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/lizk8qf7qrnc1q7lhmbp.gif)
For this project I'm using React.js, Redux, Rails and some NPM Packages(midi-sounds-react is where the instruments are from). Most DAWs come with stock instruments, MIDI Capabilities, a BPM Counter as those are some basic tools to get started.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/aw6jqtztcnnz4td7igms.png)
So we have a drum machine that works off a grid and a keyboard. Algorhythm comes packed with over 1,500 sounds thanks to Midi-react-sounds. 
```javascript
import React, { Component } from "react";
import Pads from "../Components/Pads";
import Controls from "../Components/controls";
// import Bpm from "../Components/Bpm";

import "../App.css";
import MIDISounds from "midi-sounds-react";

class Sequncer extends Component {
  state = {
    drumSnare: 15,
    drumBass: 5,
    drumHiHat: 35,
    drumClap: 24,
    pads: [
      [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0],
      [0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
      [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0]
    ],
    bpm: 108,
    start: 1 / 16,
    numPads: 4,
    playing: false,
    position: 0,
    selectedDrum: [5, 46, 145, 35],
    volume: [0.5, 0.5, 0.5, 0.5],
    mute: false,
    open1: false,
    open2: false,
    userPads: [],
    loaded: true
  };
}
```
The sequencer has a few rows on the grid and we can initialize what 'pads' we need active within state. I also initialized some other parameters like volume and BPM to start the user with some default settings.

The next features I want to add is the ability for users to join different rooms or sessions with one another. It would be great for collaborations especially during the quarantine. A record and export feature will be in the works as well. I'm having a lot of fun with this project and excited to see where it goes.
Node.js Application Template
Tags: node, express, javascript, template
Published: 2020-04-09 20:27:17

This post discuss about the [template](https://github.com/smoke-trees/nodejs-backend-template) we use at [SmokeTrees](https://github.com/smoke-trees).

To start the server run
``` 
$ npm start 
```

Note: This will run a server for development environment. 
For information on deploying express application to production server and learn best practices refer [this](https://expressjs.com/en/advanced/best-practice-performance.html).

### logger

This directory has 2 files.

* logger.js exports a winston object which we use for logging. You should edit logger.js to write your config for the logs.
* morgan.js exports a pre-configured morgan object to log using stream to winston object. It is used to intercept the HTTP request and log the request and response details.
For more details visit the official page for [morgan](https://www.npmjs.com/package/morgan).

## routes

Directory for defining routes. I don't think this need more explanation.

## Additional Information

* If on Linux running the following command add ./node_modules/.bin to the path.
  ```bash
  $ source ./activate
  ```
  By doing so we can use the commands installed with packages using the terminal.
  This won't be required in many IDEs like Webstorm but I thought it will be a good idea to include it.
  
* [standard.js](https://npmjs.com/package/standard) has been added as the default linting and styling tool
  
  Use :
  ```
  $ npm run lint
  ```
  Fixes whatever can be fixed. Above command logs out the errors which it couldn't fix.
  
* [mocha](https://npmjs.com/package/mocha) is used for testing and [chai](https://npmjs.com/package/chai) is used for assertion.
  [chai-http](https://npmjs.com/package/chai) is used to test http endpoints.
  
  Use:
  ```
    $ npm test
  ```
  
* [nyc](https://npmjs.com/package/nyc) is used as the default coverage tool.
  Use:
  ```
   $ npm run coverage
   ```
  

* The master contains code for an HTTP server. Checkout the branch https for the HTTP server.
  Replace the self-signed certificates in the certs with your SSL certificates.
  
## Dependency information

|Dependency         | Usage |
|-------------------|------ |
|[compression](https://npmjs.com/package/compression)             |Compresses the response|
|[express](https://npmjs.com/package/express)                     |Express|
|[morgan](https://npmjs.com/package/morgan)                       |HTTP request logger|
|[winston](https://npmjs.com/package/winston)                     |General purpose logger for the application|
|[nyc](https://npmjs.com/package/nyc)                             |Code Coverage tool|
|[standard](https://npmjs.com/package/standard)                   |Linting and styling tool.|
|[chai](https://npmjs.com/package/chai)                           |Assertion Library|     
|[chai-http](https://npmjs.com/package/chai-http)                 |Middleware for chai to test http endpoints|
Beautiful timeline animation of the biggest code bases in history
Tags: showdev, webdev, 100daysofcode, computerscience
Published: 2020-04-09 20:20:37

From 1970 to today, we made a visualization of the largest code bases in history- animated on a timeline, measured in lines of code. 

May this serve as a little devinspo for you today! 

https://www.youtube.com/watch?v=afEg6VuAQJQ&t=1s
LibreOffice vs MSOffice vs Google Suite vs ???, what would you choose for each task?
Tags: discuss, watercooler, webdev
Published: 2020-04-09 20:11:43

From this post, if I don't care about editability in others' computer, I might probably choose.

{% post https://dev.to/patarapolw/precise-and-programmable-office-suite-ajk %}

- Word Processor
  - Libre Writer Or Markdown Or HTML Or Pug > MS Word
  - I should try LaTeX to PDF.
- Spreadsheet
  - Google Sheet > Excel
  - If it gets large, I would just use frontend + real database
- Presentation
  - KeyNote (MacOS)
  - Perhaps I should try [Impress.js](https://github.com/impress/impress.js/)

So, MS Office for compatibility reasons. I do agree it looks sexy, though; but not that practical (and breaks between version).
April 9, 2020: What did you learn this week?
Tags: weeklylearn, discuss, weeklyretro
Published: 2020-04-09 20:09:14

Photo by [Tim Mossholder](https://unsplash.com/@timmossholder?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/s/photos/learn?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)

It's that time of the week again, just a day early. I'm off tomorrow and Monday so posting this now.

So wonderful devs, what did you learn this week? It could be programming tips, career advice etc.

<center>

![Astronaut miming mind blown](https://media.giphy.com/media/ciwgweZDnUydJShj6H/giphy.gif)

</center>

Feel free to comment with what you learnt and/or reference your TIL post to give it some more exposure.

{%tag todayilearned %}

And remember, if something you learnt was a big win for you, then you know where to drop it as well.👇👇🏻👇🏼👇🏽👇🏾👇🏿

{% link https://dev.to/jess/what-was-your-win-this-week-5e1n %}

<center>

![Racecar driver celebrating a win](https://media.giphy.com/media/xTiN0suGFFGKJRpUnC/giphy.gif)

</center>
Using Github's CLI on Ubuntu & Commands.
Tags: github, linux, ubuntu, git
Published: 2020-04-09 20:02:09

[gh cli web](https://cli.github.com/)

Working with GitHub has never been boring, as a matter of fact, it's the best part of the development process on my side. As usual, we use git and other distributed version control systems like Mercurial and many more.

I have been using the GitHub cli since February this year, and it is great. So let me show you how I got to use it. I'm on Ubuntu by the way. But I guess the process is the same on other OS, I'll check it out.

First, you download it on [releases](https://github.com/cli/cli/releases). Select the file with an extension based on your OS. Currently, the latest version is 1.x.x. 

After download, follow normal procedures of installing an app on your environment/OS. Once the file is installed, BOOM! You are ready to interact with the gh cli.  

There are no many configuration settings needed. The commands can be used anywhere regardless of the Operating System. The commands are as shown below:

General Activity

| Command        | usage        | detailed usage |
| ------------- |:-------------:|:-------------:| 
| `gh --version` | view version of gh cli | - |

Working with repositories (repo)

| Command        | usage        | detailed usage |
| ------------- |:-------------:|:-------------:|
| `gh repo view` | view current repo on the browser (web) | https://cli.github.com/manual/gh_repo_view |   
| `gh repo fork` | create a fork of a repo | https://cli.github.com/manual/gh_repo_fork |
| `gh repo create` | create a new repo | https://cli.github.com/manual/gh_repo_create |
| `gh repo clone` | clone a repo locally | https://cli.github.com/manual/gh_repo_clone |

Working with Issues

| Command        | usage        | detailed usage |
| ------------- |:-------------:|:-------------:|
| `gh issue view` | view an issue of a repo | https://cli.github.com/manual/gh_issue_view |
| `gh issue create` | create an issue locally | https://cli.github.com/manual/gh_issue_create |
| `gh issue status` | displays the status of issues on the repo | https://cli.github.com/manual/gh_issue_status |
| `gh issue list` | lists all issues raised on the repo | https://cli.github.com/manual/gh_issue_list |

Working with pull requests (pr)

| Command        | usage        | detailed usage |
| ------------- |:-------------:|:-------------:|
| `gh pr view` | view pull requests in browser | https://cli.github.com/manual/gh_pr_view |
| `gh pr status` | lists the statuses of available pull requests | https://cli.github.com/manual/gh_pr_status |
| `gh pr list` | lists all pull requests in the current repo | https://cli.github.com/manual/gh_pr_list |
| `gh pr create` | create a pull request | https://cli.github.com/manual/gh_pr_create |
| `gh pr checkout` | check out a pull request in git | https://cli.github.com/manual/gh_pr_checkout |

#### References
1. [installation instructions](https://cli.github.com/manual/installation)
2. [gh cli manual](https://cli.github.com/manual/)
Stack: PostFix Calculator
Tags: javascript, webdev
Published: 2020-04-09 19:28:10

##Postfix Arithmetic

Postfix arithmetic means that the operator goes after the two numbers. Example; 6 * 7 becomes 6 7 *. A postfix expression can be an operand in another postfix expression: 6 7 * 1 - is equivalent to (6 * 7 ) - 1.

Using the stack class in 'stack:array as storage', we will evaluate a postfix expression. 

##Postfix pseudocode
```
foreach token 
    if token is integer 
        push token
    else if token is operator
        pop right side value
        pop left side value
        evaluate operator
        push result
next 
```

##Postfix Calculator
```javascript
function postFixCalc(expression) {
    //create new stack
    let stack = new Stack();

    //loop through each character in provided expression
    for (let idx = 0; idx < expression.length; idx++) {
        //store each character
        let token = expression[idx];

        //if it's a number, push to stack
        //else pop right side and left side, perform operation and push to stack
        if (!isNaN(token)) {
            stack.push(Number(token));
        } else {
            let rhs = stack.pop();
            let lhs = stack.pop();
            //if right or left side is not available
            if (rhs === "UnderFlow" || lhs === "UnderFlow" ) {
                return "Can't perform postfix calculation";
            }
            switch (token) {
                case '+':
                    stack.push(lhs + rhs);
                    break;
                case '-':
                    stack.push(lhs - rhs);
                    break;
                case '*':
                    stack.push(lhs * rhs);
                    break;
                case '/':
                    stack.push(lhs / rhs);
                    break;
                case '%':
                    stack.push(lhs % rhs);
                    break;
            }
        }
        
    };
   //return result of calculation
    return stack.pop();
}
```
**Sample test code**
```javascript
console.log(postFixCalc('567*+1-'))
```
The result should be 46 :smile:. 
Thank you for reading!
BUIDL tools in an online hackathon for up to $9k in prizes 
Tags: hackathon, blockchain, productivity, javascript
Published: 2020-04-09 19:11:36

Hey everyone! 

There are only a couple weeks left in the MESG Ecosystem Hackathon on Devpost. Running through April 26th 2020.

It's a virtual hackathon that's pretty wide open! Build tools, services and/or processes associated with the open-source MESG Framework, where you can quickly and cheaply build applications based on a marketplace of customizable components. 

The MESG Framework helps you start quickly and securely. Focus on making your business applications great and forget about repetitive component configuration and management. 

This hackathon is open to developers of all levels. Join us! We're excited to see what you build. 

[Learn more and join](https://blog.mesg.com/announcing-the-mesg-ecosystem-online-hackathon/?utm_source=devto&utm_medium=blog&utm_campaign=tools)
FLaNK Stack Demo
Tags: apacheflink, apachenifi, apachekafka
Published: 2020-04-09 19:08:53

My friend, Ian Brooks, just wrote an amazing demo utilizing the FLaNK Stack.

https://github.com/BrooksIan/Flink2Kafka

It is using Apache NiFi to read NYC Taxi data (CSV), preprocessing it, transforming it and then publishing it to an Apache Kafka topic.   An Apache Flink SQL Streaming job reads the Kafka events, enriches them and then publishes them to another Apache Kafka topic.   Finally, Apache NiFi consumes those events from that topic.

The next step is to store both of these feeds in Apache Kudu (or another datastore in CDP say Hive, Impala (Parquet), HBase, Druid, HDFS/S3 and then write some queries / reports on top with say DAS, Hue, Zeppelin or Jupyter.   Finally doing some additional machine learning with CML and writing a visual application in CML.

Really cool app and great use of Flink SQL!

Also, please note he developed and ran all of this utilizing IntelliJ, nice use of local tools and then we can push the final app to a large cloud or K8 hosted cluster like Cloudera Data Platform.

#FLaNKStack #ApacheFlink #ApacheNiFi #ApacheKafka
A playlist for the sophisticated Software Engineer
Tags: motivation, productivity, watercooler
Published: 2020-04-09 18:56:03

---
title: A playlist for the sophisticated Software Engineer
published: true
description: Featuring hits like "(git) Push it to the limit", "I still haven't found what I'm looking for", "Highway to (dependency) Hell" and last but not least: "Break Stuff".
tags: motivation, productivity, watercooler
---

## TL;DR

It's cheesy, it's not 100% dev related - but here's my take on a [#devmotivation playlist on Spotify](https://open.spotify.com/playlist/3JWoH18IwuTUdNkgTIBvxo?si=ZwQ4DL8PQii7-ouirN8LUg).

Featuring hits like...

* "(git) Push it to the limit"
* "Highway to (dependency) Hell"
* "I still haven't found what I'm looking for" 

and last but not least: "Break Stuff".

## Background

The 'rona made me experience working full-remote first hand for a couple of weeks now. Nevertheless I'm really blessed for still having a job (and first of all still being healthy ofc), I was getting sick of all my old, well-known music playlists rather quick.

Since I joined my new team, I started the ritual of playing "Push it to the limit" (yes, the theme song of the movie "Scarface") while `git push`-ing the last commit of a finished hotfix or feature branch. Even though it never really was the last commit (because my branch didn't pass code review flawlessly, or I missed adding a changelog entry), it always put a smile on the faces of my co-workers. So I thought: time for a playlist. 

I always listen to DnB or some other fast paced, passive-aggressive music during debugging sessions - to channel my anger on squashing even the last of those nasty, little bugs. **This playlist has a different vibe**.

{% spotify spotify:playlist:3JWoH18IwuTUdNkgTIBvxo %}

If you have suggestions for tracks I could add, **let me know!**

Thanks for reading, I hope you all stay safe and forget about the current struggles of the world for a second while listening.

Cheers,
Dom
.htaccess to hide real github url
Published: 2020-04-09 18:29:47

I do apologize first because this is my first topic and it is a help-request.

I have an hosting with Aruba.it. Inside it there's an .htaccess .

The thing I'd like to do is to mask the real url where I want to redirect the user.

My blog is on Github Pages and looks like username.github.io/repository .
I'd like the user surfing my blog hiding such a real url.

Example:

www.myblog.com/Products -> username.github.io/repository/Products [but hidden!]
www.myblog.com/blog.css -> username.github.io/repository/blog.css [but hidden!]

I know it is supposed to be possible but I don't know whether with Aruba it is.

Thank you :)
SED - Update line that starts with but does not contain
Tags: bash, sed
Published: 2020-04-09 18:26:28

I wanted to update my docker.service file to include tls certificates in the start command. The idea was to search for the line that starts with `ExecStart=` but also not to change anything if that line already contains tcp header `-H tcp://`. It turned out to be a bit more complicated than expected.

## Result

```bash
sed -i '\|^ExecStart=.*-H tcp://|! s|^ExecStart=.*|\0 -H tcp://0.0.0.0:2376 --tlsverify --tlscacert='$PWD'/ca.pem --tlscert='$PWD'/server.pem --tlskey='$PWD'/server.key|' /lib/systemd/system/docker.service
```

Broken in multiple lines for readability

```bash
sed -i '\|^ExecStart=.*-H tcp://|!
        s|^ExecStart=.*|\0 -H tcp://0.0.0.0:2376 --tlsverify
        --tlscacert='$PWD'/ca.pem --tlscert='$PWD'/server.pem
        --tlskey='$PWD'/server.key|' /lib/systemd/system/docker.service
```

In a nutshell, it excludes all lines that start with `ExecStart=` and contain `-H tcp://` and out of remaining lines, if a line starts with `ExecStart=`, it appends additional arguments to the end of the line.

## Explanation

`sed -i` means that we will change a file in place.

`\|A|! s|S|R|` - using pipes instead of slashes makes it easier when dealing with paths. Note that syntax starts with a backslash when using  special delimiters with addresses (`A` part).
Using standard slashes would be like `/A/! s/S/R/`, without leading backslash.

`\|^ExecStart=.*-H tcp://|!` - using the addresses in sed, exclude lines that start with `ExecStart=` and have `-H tcp://` down the line.

`s|^ExecStart=.*|\0 -H tcp://0.0.0.0:2376 ...` - search for a line that starts with `ExecStart=` and grab the rest of the line using  `.*` . In the replace part, `\0` will hold the complete line match and the rest is additional text that is appended to the line.

`/lib/systemd/system/docker.service` - file that is being edited

## Conclusion

For a single line change, a file can be checked if the update is actually required before updating it, and then the exclusion part is not needed at all. But if there are more lines that require similar update this could do the trick.