'Can I use cache decorator to save result of any function call?

I'm aware that @cache decorator is used as quick way to implement memoization

Is is possible to use it on other contexts, like for deterministic functions (that return the same result provided the same arguments)? Should I avoid it if the function is non-deterministic?

This is what the doc says

Returns the same as lru_cache(maxsize=None), creating a thin wrapper around a dictionary lookup for the function arguments

I wonder what happens when the function takes no arguments



Solution 1:[1]

With GNU awk:

awk -i inplace 'BEGIN{FS=","; OFS=","} {$3=toupper($3)} {print}' file

Output to file:

1,Jun 4 2021,CAR,4856
2,Jul 31 2021,CAR,4154
3,Aug 14 2021,BUS,4070
4,Aug 2 2021,CAR,4095

See: How can I change a certain field of a file into upper-case using awk?, Save modifications in place with awk and 8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR

Solution 2:[2]

A gnu sed solution:

sed -i -E 's/^(([^,]+,){2})([^,]+)/\1\U\3/' file.csv

cat file

1,Jun 4 2021,CAR,4856
2,Jul 31 2021,CAR,4154
3,Aug 14 2021,BUS,4070
4,Aug 2 2021,CAR,4095

Explanation:

  • ^: Start
  • (([^,]+,){2}): Match first 2 fields and capture them in group #1
  • ([^,]+): Match 3rd field and capture it in group #3
  • \1: Put capture value of group #1 back in replacement
  • \U\3: Put uppercase capture value of group #3 back in replacement

Or a gnu-awk solution:

awk -i inplace 'BEGIN {FS=OFS=","} {$3 = toupper($3)} 1' file.csv

Solution 3:[3]

Using the cut and tr, you need to add paste to the mix.

SEP=","
IN="data.csv"

paste -d$SEP \
  <( <$IN cut -d$SEP -f1,2 ) \
  <( <$IN cut -d$SEP -f3 | tr '[:lower:]' '[:upper:]' ) \
  <( <$IN cut -d$SEP -f4 )

I did factor out the repeating things - separator and input file - into variables SEP and IN respectively.

How it all works:

  • get the untransformed columns before #3
  • get col #3 and transform it with tr
  • get the remaining columns
  • paste it all together, line by line
  • the need for intermediate files is avoided by using shell substitution

Downsides:

  • the data seems to be read 3 times, but disk cache will help a lot
  • the data is parsed 3 times, for sure (by cut)
  • but unless your input is a few gigabytes, this does not matter

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cyrus
Solution 2
Solution 3 liborm