pythonistr brings over a few ideas from the python language into R.
Python has a great context management system that closes file connections when they fall out of scope:
with open('file.csv', 'r') as f:
for line in f:
print(line)
Pythonistr adds a with
method to connections which mirrors this behaviour:
file_conn <- file("file.csv", "r")
with(file_conn, {
while (TRUE) {
line <- readLines(file_conn, n = 1)
if (length(line) == 0) {
break
}
print(line)
}
})
isOpen(file_conn) # FALSE (well, error)
Even better, there’s some support for line-by-line processing. In Python you might write:
with open('log.txt', 'r') as l:
for line in l:
print line
if 'stop' in line:
break
With pythonistr, you could write this as:
log <- file('log.txt', 'r')
with(log,
by_line(log,
print, # function to apply to each line
function(l) grepl("stop", l) # stop when true
)
)
In python, to instantiate a list of strings with minimal typing you could use:
l = 'i want a list of words'.split()
pythonistr adds separate
(or s
for short):
l <- s("saves you typing and matching quotes")
l
#> [1] "saves" "you" "typing" "and" "matching" "quotes"
If you regularly switch between R and Python you might be used to getting tripped up by length
vs. len
but this isn’t the only example. For example: which language prefers reversed
over rev
? Which implements sorted
as well as sort
?
pythonistr includes a few shortcuts to lower the context switching overhead, though it’s probably a bad idea to rely on these in normal R programming.