Skip to main content

Utilities for & with strings.

Project description


python3 -m pip install stroke

Useful for creating a simple command based interface

import stroke

commands = stroke.State()

# create command
def commands_0(content):


# this is a subcommand
def commands_0_0(content):

  content = content.upper()


# another subcommand with aliases
@commands_0.sub('pretty', 'aesthetic')
def commands_0_1(content):

  content = ' '.join(iter(content))


def main(content):

  # analyse into names separated by .
  # anything after a space is the argument
  # use the names to find the invoke
  # KeyError will be raised on invalid trail
  (names, argument, invoke) = commands.analyse(content)


main('echo marco') # not polo

main('echo.loud whoa') # > WHOA

main('echo.aesthetic fancy words') # chill vibes

Useful for parsing arguments into parts

import stroke

commands = stroke.State()

figure_0 = {
  '-and': 'int'

def commands_0(content):

  # list of (flag, value)
  arguments = stroke.craft(figure_0, content)

  value = 0

  # flag is always -and
  for (flag, argument) in arguments:

    value += int(argument)

  print('result', value)

figure_1 = {
  '-upper': 'numerator',
  '-lower': 'denominator'

def commands_1(content):

  # same thing
  arguments = stroke.craft(figure_1, content)

  # ignore multiple flags
  arguments = dict(arguments)

  # we need every flag for this one
  values = (arguments[key] for key in figure_1)

  # convert and unpack
  (upper, lower) = map(int, values)

  # handle errors on demand
  value = upper / lower

  print('result', value)

figure_2 = {
  '-and': {
    '-op': 'name',
    '-co': 'args'

def commands_2(content):

  # this is actually a generator
  arguments = stroke.craft(figure_2, content)

  # here, argument is also one
  for (flag, argument) in arguments:

    # we only need one invoke
    invoke = None

    # can be executed multiple times
    contents = []

    for (flag, argument) in argument:

      # signal for name
      if flag == '-op':

        # ignore empties
        if not argument:


        # only first
        if not invoke:

          # get the invoke from name
          invoke = commands.trail(argument)


      # no need to check if -co

    # execute commands
    for content in contents:


def main(content):

  (names, argument, invoke) = commands.analyse(content)


print('>', stroke.draw(figure_0))
main('add 5 -and 9 -and -3') # 11

print('>', stroke.draw(figure_1, full = True))
main('div -upper 20 -lower 2 -upper 15') # 7.5

print('>', stroke.draw(figure_2, full = True))
main('calc add -co 1 \-and 2 -and div -co -upper 1 -lower 4') # 3 and 0.25

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for stroke, version 2.3.2
Filename, size File type Python version Upload date Hashes
Filename, size stroke-2.3.2-py3-none-any.whl (7.8 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size stroke-2.3.2.tar.gz (5.6 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page