Postgres dress with pockets

November 27, 2020
PostgreSQL

I was looking for something to add to my Christmas wish list (yes, my parents are retired and they asked for this wish list for the last 3 months) and I remembered I had seen a blog post a few years ago where you could print your own code on clothes.

I had a good memory of a particularly nice dress (and I found out later it even had pockets! Yay!).

The code

I had to google differents key words (including dress and code but not dress code) and I finaly found out this Github repository.

The code is written in python. It will then generate images that you’ll be able to upload to cowcow.com to customize your dress (or other piece of clothe).

Docker

I don’t know if you’re a python afficionado. I was until I found out that setting the environment for each project was a nightmare. Don’t get me wrong, I love writing and reading python code, I just find it pretty annoying to download all those extra packages.

Thankfully, the code provided by Holden Karau also includes a dockerfile.

Just one warning, I’m not a docker expert, I’m not a developer. Maybe I didn’t use those tools how they were intended. What matters is that I was able to use them quickly (less than 1 hour) and create my dress.

Build the docker image

You first need to build the docker image. That’s pretty standard:

docker run --detach --name mydress dress

It might take some time, but it’s ok.

Create the container and connect to it

I then created the container and ran a simple interactive /bin/bash command to be able to launch the code.

docker run -it dress /bin/bash

Using gen.py

The code that generates clothe is gen.py. It can generate different images depending on the kind of clothes you’d like to create:

python gen.py --list-clothing
The following clothing items are available:
dress_with_pockets
boxy_basketball_tank_top
fitted_basketball_tank_top
hooded_pocket_cardigan
aline_pocket_skirt
15inch_laptop_sleeve

Here is the syntax you need to use:

python gen.py --help
usage: gen.py [-h] [--files [FILES [FILES ...]]] [--output [OUTPUT]]
              [--extension [EXTENSION]] [--clothing [CLOTHING]]
              [--list-clothing] [--style [STYLE]] [--list-styles]

Process some code

optional arguments:
  -h, --help            show this help message and exit
  --files [FILES [FILES ...]]
                        file names to process
  --output [OUTPUT]     output directory
  --extension [EXTENSION]
                        output extension
  --clothing [CLOTHING]
                        Clothing item to generate images for (see all
                        available profiles with --list-clothing)
  --list-clothing       List all available clothing profiles and exit.
  --style [STYLE]       The pygments style to use for the colour scheme (see
                        all available styles with --list-styles)
  --list-styles         List all available style names.

Using your code

I decided to use a random code page from postgresql. I choose parser_oper.c. Once it’s chosen, you will need to copy it inside your container.

docker cp ~/postgresql/src/backend/parser_oper.c 7de0a113b475:/usr/src/app

That was so simple.

Choosing colors and styles

gen.py allows several syntax coloring styles. Of course, I wanted to see them all. So I changed a little the code, so it can generate all the images for all styles at once. Of course, my container ran out of memory when I tried it, so I simply added an if statement not to re-generate already generated images, so that by running it twice all my images were generated (yes, that’s good enough for throw away code).

Here’s my modified code of gen.py:

if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser(description="Process some code")
    parser.add_argument(
        "--files", type=str, default=["gen.py"], nargs="*", help="file names to process"
    )
    parser.add_argument(
        "--output", type=str, default="out", nargs="?", help="output directory"
    )
    parser.add_argument(
        "--extension", type=str, default="png", nargs="?", help="output extension"
    )
    parser.add_argument(
        "--clothing",
        type=str,
        default="dress_with_pockets",
        nargs="?",
        help="Clothing item to generate images for (see all available profiles with --list-clothing)",
    )
    parser.add_argument(
        "--list-clothing",
        dest="list_clothing",
        action="store_true",
        help="List all available clothing profiles and exit.",
    )
    parser.add_argument(
        "--style",
        type=str,
        default="paraiso-dark",
        nargs="?",
        help="The pygments style to use for the colour scheme (see all available styles with --list-styles)",
    )
    parser.add_argument(
        "--list-styles",
        dest="list_styles",
        action="store_true",
        help="List all available style names.",
    )
    parser.add_argument(
        "--all-styles",
        dest="all_styles",
        action="store_true",
        help="Will generate the chosen clothe images with all the styles available.",
    )

    args = parser.parse_args()

    if args.list_clothing:
        # The user just wants a list of profiles, let's print that and exit.
        list_profiles()

    if args.list_styles:
        # Again, the user just wants a list of styles, let's print that.
        list_styles()

    if args.list_styles or args.list_clothing:
        sys.exit(0)

    make_if_needed(args.output)

    if args.all_styles:
        # Generate all styles
        # Generate it only if it wasn't already done
        for style in list(get_all_styles()):
            if not os.path.exists(os.path.join(os.getcwd(), args.output, style)):
                print("Making the images in memory for "+ style)
                (processed, highlighted, cropped, glitched_tiled) = build_image(
                    args.files, clothing=args.clothing, style=style)
                print("Saving the images to disk")
                save_imgs(args.output + "/" + style, processed, args.extension)
    else:
        # Generate one style
        print("Making the images in memory")
        (processed, highlighted, cropped, glitched_tiled) = build_image(
            args.files, clothing=args.clothing, style=args.style)
        print("Saving the images to disk")
        save_imgs(args.output + "/processed", processed, args.extension)

Getting images

After that, I was able to launch my code:

python gen.py --files parser_oper.c --output out -- clothing dress_with_pockets --all-atyles

And to copy my images outside the container:

docker cp 7de0a113b475:/usr/src/app/out .

Customizing the dress

Go to the cowcow website and click on the customize button. You might need to install IE Tab :-(.

After that, you should be able to use the generated images to make a nice dress (with pockets!).

And voilà!

Dress front Dress back

Replica identity for logical replication

December 16, 2020
PostgreSQL Administration

Is my port open?

December 1, 2020
PostgreSQL Administration

Where do my Postgres settings come from ?

April 19, 2020
Hacking PostgreSQL Administration