Skip to content

Tag: programming

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.

Cedilha no Ubuntu 16.04

    çççç
  ççç  ççç 
  ççç  ççç 
  ççç 
  ççç 
  ççç  ççç 
    çççç
     çç
    çç

Fiz esse script (cedilha.sh) pra fazer o c-cedilha (ç) funcionar no Ubuntu 16.04. Como é uma tarefa chata que eu já tive que fazer muitas vezes fica mais fácil pra mim ter um script pra fazer isso e pode ser que seja útil a outras pessoas. Infelizmente é uma solução que exige baixar e executar como root um script da internet. Eu recomendo que você leia o script e entenda o que está acontecendo antes de executá-lo. Esse script altera vários arquivos importantes e ele faz um backup (.bak) desses arquivos.

[bash]wget https://raw.githubusercontent.com/silveira/cedilha.sh/master/cedilha.sh
sudo bash cedilha.sh[/bash]

Por favor, se o script funcionou pra você também, diga nos comentários. Se você não está usando Ubuntu 16.04 e quer testar o script ainda assim, edite o script e remova a verificação no inicio do programa.

parallel counting in Java using AtomicInteger

[java]
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.TimeUnit;

class ParallelCounterExample {
public static void main(String[] args) throws InterruptedException {
int threads = 2000;
ExecutorService executor = Executors.newFixedThreadPool(threads);
final AtomicInteger atomi = new AtomicInteger();
for(int i=0; i<threads; i++) {
executor.execute( new Runnable() {
@Override
public void run() {
atomi.getAndIncrement();
}
});
}
executor.awaitTermination(1, TimeUnit.SECONDS);
executor.shutdown();
System.out.println(atomi.get());
}
}
[/java]

Regex with negatives lookahead and lookbehind

"Looking different directions" by Paul Kline at (https://www.flickr.com/photos/paulelijah/6717953239/)
“Looking different directions” by Paul Kline.

Problem: Match strings that contains a single quotation mark ('), but not multiple ones together.

Solution:

(?<!')'(?!')

This is a regex for a single quotation mark with a (?<!') in the left and a (?!’) in the right. The (?<!') is a ?< look behind if not ! a single quotation mark '. The (?!') is a look ahead ? if not ! a single quotation mark '.

Java code:

[java]import java.util.regex.Pattern;

public class RegexProblem {
public static void main(String args[]) {
Pattern single_quote = Pattern.compile("(?<!’)'(?!’)");
String[] phrases = {
"",
"’",
"a’a",
"aaa",
"aa’aa",
"aa”aa",
"aa”’aaa",
"aaa””aaa"
};
for(String phrase: phrases){
System.out.println(String.format("For %s is %s.", phrase,
single_quote.matcher(phrase).find()));
}
}
}
[/java]

The output is:

For  is false.
For ' is true.
For a'a is true.
For aaa is false.
For aa'aa is true.
For aa''aa is false.
For aa'''aaa is false.
For aaa''''aaa is false.