[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How do you name your code blocks?
From: |
Rodrigo Morales |
Subject: |
Re: How do you name your code blocks? |
Date: |
Tue, 16 Feb 2021 20:21:05 -0500 |
User-agent: |
mu4e 1.4.14; emacs 27.1 |
Greg Minshall <minshall@umich.edu> writes:
> i guess part of the answer depends on why you are naming your code
> blocks.
I was asking because in my notes in Org Mode, I am used to create
subheadings for each question I have. Here's an example:
Let's say that in my notes on Python, I have the following content
#+begin_src org
,* DONE How to get the number of lines in a file?
- State "DONE" from [2021-02-16 Tue 20:02]
,#+NAME: create-file
,#+begin_src dash :results silent
cat << EOF > main.txt
first
second
third
EOF
,#+end_src
,#+begin_src python
print(len(open('main.txt').readlines()))o3
,#+end_src
,#+RESULTS:
,#+begin_example
3
,#+end_example
,* DONE How to print most repeated word in a file?
- State "DONE" from [2021-02-16 Tue 20:02]
,#+NAME: create-file
,#+begin_src dash :results silent
cat << EOF > main.txt
fizz fizz fizz
buzz buzz buzz buzz
foo
bar bar
EOF
,#+end_src
,#+begin_src python
import re
from collections import Counter
with open('main.txt') as f:
passage = f.read()
words = re.findall(r'\w+', passage)
word_counts = Counter(words)
print(word_counts.most_common(1))
,#+end_src
,#+RESULTS:
,#+begin_example
[('buzz', 4)]
,#+end_example
#+end_src
As you can see above, there are two code blocks which creates two text
files so that a Python script can then process it. Both of them create
a text file, that's why I have used =create-file= as the name of those
code block. I could have used very long names but I don't find
comfortable myself doing that since it adds extra effort to think in a
name for such simple code blocks (see below an example of the approach
of using long names).
#+begin_src org
,* DONE How to get the number of lines in a file?
,#+NAME: create-file-for-counting-lines
,#+begin_src dash :results silent
cat << EOF > main.txt
first
second
third
EOF
,#+end_src
,* DONE How to print most repeated word in a file?
,#+NAME: create-file-for-finding-most-repeated-word
,#+begin_src dash :results silent
cat << EOF > main.txt
fizz fizz fizz
buzz buzz buzz buzz
foo
bar bar
EOF
,#+end_src
#+end_src
--
Rodrigo Morales.
IRC: rdrg109 (freenode)