Skip to content

Tag: programming

bash and wget to download a lot of files

Problem:

  • Download 500 large files.
  • Files follow the pattern data_1.zip, data_2.zip, … data_500.zip.
  • All the files are in “https://examples.com/downloads”.
  • Files have from kilobytes to gigabytes in size.

Solution:

  • A naive Bash script to download the files using wget.
  • It is set to use wget with -c (continue). This will skip files already downloaded and continue files that have been interrupted. This only works with FTP/HTTP servers that support the “Range” header.
#!/bin/bash
set -euxo pipefail

PREFIX="https://examples.com/downloads/data_"

for i in $(seq 1 500); do
  wget -c "$PREFIX"_"$i".zip
done

half-hexagon in OpenSCAD

height = 5;
radius = 20;

difference() {
  cylinder(height, radius, radius, $fn=6);
  translate([-radius, 0, -height/4]) {            
    cube([radius*2, radius, radius]);
  }
}

It works for these parameters but it needs more work to be really parametric. But hey, it’s my first OpenSCAD thing. I actually exported as .STL and printed it.

Chain of Promises Example

const f1 = async() => {
    setTimeout( () => {
        console.log("f1");
    }, 100)
};

const f2 = async() => {
    setTimeout( () => {
        console.log("f2");
    }, 100)
};

const f3 = async() => {
    return new Promise((resolve, reject) => {
        reject("f3");
    });
};

const f4 = async() => {
    setTimeout( () => {
        console.log("f4");
    }, 100)
};

const workflow = async () => {
    await f1();
    await f2();
    await f3();
    await f4();
};

workflow().catch((error)=>{
    console.error(error);
})

conditional past, present, future in spreadsheets/Excel

=IF(B2=TODAY(),"Past",IF(B2<TODAY(),"Today",TEXT(B2,"ddd")))
  • Given a column B that contains dates in a sequential order.
  • Shows a “Today” when it’s today.
  • Shows a “Past” when is past.
  • Shows an abbreviated name of the day of the week.

Here is an example using emojis instead of past/today:

Ruby super

class A
   def foo 
      [1, 2, 3]
   end 
end

class B < A 
   def foo 
      super.map{ |n| n * 2 } 
   end 
end

puts A.new.foo.to_s
puts B.new.foo.to_s

Output

[1, 2, 3]
[2, 4, 6]

flatten directory structure

Move files in current subdirectories to the current directory.

find . -mindepth 2 -type f -exec mv -t . -i '{}' +

Delete local folders that are empty.

 find . -empty -type d -delete

Python: Getting page title

# Get the HTML page content
from urllib import request
html = request.urlopen("https://silveiraneto.net").read()

# Get the title tag
from bs4 import BeautifulSoup
title = BeautifulSoup(html, 'html.parser').find('title')

print(title.string)

This uses the XML/HTML library BeautifulSoup . This could also be done using regex but playing with HTML and regex is usually a bad idea.