README.md

pythonistr

travis_status codecov docs_badge CRAN_badge

pythonistr brings over a few ideas from the python language into R.

Install

Install from github with:

devtools::install_github("blmoore/pythonistr")

Usage

Shortcuts

In python, to instantiate a list of strings with minimal typing you could use:

print('i want a list of words'.split())
#> ['i', 'want', 'a', 'list', 'of', 'words']

pythonistr adds separate (or s for short):

s("saves you typing and matching quotes")
#> [1] "saves"    "you"      "typing"   "and"      "matching" "quotes"

String and sub-string matching intent is clear in Python:

if 'char' in 'character string':
   print('Found!')
#> Found!

pythonistr adds %within% as an alias for grepl(pattern, string):

if ('char' %within% 'character string')
  print('Found!')
#> [1] "Found!"

Python functions

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
  )
)

Anything executed by by_line is currently only useful for its side-effects.

Aliases

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.



blmoore/pythonistr documentation built on May 20, 2019, 3:34 p.m.