Improve everyday.

AWS S3 policy generator

|

The tool

AWS S3 policy generator

What for ?

The AWS Policy Generator is a tool that enables you to create policies that control access to Amazon Web Services (AWS) products and resources. “

Enables to specify fine-grained rules for all the actions to be performed on resources.

How ?

  1. Specify your rules
  2. Click Add statement
  3. Repeat 1 + 2 until satisfaction
  4. Click Generate policy
  5. Copy-paste the JSON to your bucket authorization strategy ( MY-BUCKET > Authorizations > Bucket Strategy )
  6. Save, and voilà !

Examples

Note that the Sid can be changed but needs to be unique. This can be useful to describe a policy

Denying bucket deletion to everyone

{
  "Id": "Policy1507xxxxxxx",
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "Stmt150xxxxxxx", 
      "Action": [
        "s3:DeleteBucket"
      ],
      "Effect": "Deny",
      "Resource": "arn:aws:s3:::MY-BUCKET/",
      "Principal": "*"
    }
  ]
}

Static hosting without public listing

{
  "Id": "Policy1507xxxxx",
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "Stmt150xxxxxxxx",
      "Action": [
        "s3:GetObject"
      ],
      "Effect": "Allow",
      "Resource": "arn:aws:s3:::MY-BUCKET/",
      "Principal": "*"
    }
  ]
}

Opening multiple files in Vim

|

CLI

$ vim *.py
# This opens the files in their own tabs.

$ vim -o *.py
# This opens the files in horizontal splits.

$ vim -O *.py
# This opens the files in vertical splits.

From vim

:args *.py | all
# This opens the files in horizontal splits.

:args *.py | vertical all
# This opens the files in vertical splits.

Installing Neovim

|

Intro

Nvim is asynchronous and multicore. Already enough to consider it a way to go…

Migration from vim to nvim

Start by reading some stuff there :help nvim-from-vim. The following will describe the main steps :

  1. Installing Neavim with python support :
      sudo add-apt-repository ppa:neovim-ppa/stable
      sudo apt-get install neovim
      sudo apt-get install python-neovim # Or "pip2 install neovim"
      sudo apt-get install python3-neovim # Or "pip3 install neovim"
    
  2. You will create an init file for neovim that links everything to a classic vim configuration. In ~/.config/nvim/init.vim add the following lines :
      set runtimepath+=~/.vim,~/.vim/after
      set packpath+=~/.vim                
      source ~/.vimrc                     
    
  3. Use Plug to install packages. Get the file plug.vim and copy it to ~/.vim/autoload/plug.vim. Inside nvim, you can now use :Plug... commands. Check the (#usefull-plugins) section for a basic plugins setup.

  4. Create the right alias by putting in your ~/.bashrc : alias vim=nvim

Usefull plugins

Ressources

Iterating by chunks of same size

|

TL;DR

From stackoverflow

from itertools import izip_longest

def grouper(iterable, n, fillvalue=None):
    """ Iterate on an iterable by chunks of size n, filling the last with fillvalue """
    args = [iter(iterable)] * n
    return izip_longest(*args, fillvalue=fillvalue)

Example

grouper('ABCDEFG', 3, 'x') --> 'ABC' 'DEF' 'Gxx'

Formula

>> L = range(10)
# We want [(0, 1, 2), (3, 4, 5), (6, 7, 8)]
>> zip(*[iter(L)]*3)
[(0, 1, 2), (3, 4, 5), (6, 7, 8)]

Explanations

Creating the object to iterate over

  • iter(L): iter returns an iterator over its argument (https://docs.python.org/2/library/functions.html#iter)
  • [obj] * N returns a list of N references to obj.
    • If obj is a dict, modifying the first element of the list will affect all the other elements of the list.
>> L = range(10)
>> M = [iter(L)]*3
>> M[0].next()
0
>> M[1].next()
1
>> M[0].next()
2

Docs for next

* operator

  • The * operator is used to unpack an array into positional arguments for a function
  • From the docs:
    >> range(3, 6)      # normal call with separate arguments
    [3, 4, 5]
    >> args = [3, 6]
    >> range(*args)     # call with arguments unpacked from a list
    [3, 4, 5]
    

zip

  • From the docs:
    • zip returns a list of tuples. The i-th tuple contains the i-th element from each of the argument sequences or iterables.
      >> x = [1, 2, 3]
      >> y = [4, 5, 6]
      >> zipped = zip(x, y)
      >> zipped
      [(1, 4), (2, 5), (3, 6)]
      

All together

>> L = range(10); print(L)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>> it = iter(L); print(it)
<listiterator object at 0x7f90600880d0>
>> M = [it, it, it]; print(M)
[<listiterator object at 0x7f90600880d0>, <listiterator object at 0x7f90600880d0>, <listiterator object at 0x7f90600880d0>]
>> zip(*M)
[(0, 1, 2), (3, 4, 5), (6, 7, 8)]

Improvement

  • The last element of L (9) is not in the result:
    • zip stops upon reaching the 1st empty iterable
  • To obtain the last elements, use itertools.izip_longest
    • As izip, this returns an iterator, not a list
>> import itertools
>> L = range(10)
# We want [(0, 1, 2), (3, 4, 5), (6, 7, 8)]
>> for e in itertools.izip_longest(*[iter(L)]*3):
>>     print(e)
(0, 1, 2)
(3, 4, 5)
(6, 7, 8)
(9, None, None)

Real World apps repo

|

Useful repo to see what JS (and to a lesser degree backend) frameworks look like in real world use cases.

It collects implementation in various framework of a medium.com clone, and tries to show good practice for projects organization using the said framework. This is not intended to be used to learn a framework, but to improve your knowledge of it by showing examples of real world use that go beyond the simple todo app that most tutorials present. https://github.com/gothinkster/realworld