Skip to main content

Utilities for & with strings.

Project description

Installing

python3 -m pip install stroke

Useful for creating a simple command based interface

import stroke

commands = stroke.State()

# create command
@commands.sub('echo')
def commands_0(content):

  print(content)

# this is a subcommand
@commands_0.sub('loud')
def commands_0_0(content):

  content = content.upper()

  print(content)

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

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

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

  invoke(argument)

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'
}

@commands.sub('add')
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'
}

@commands.sub('div')
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'
  }
}

@commands.sub('calc')
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:

            continue

        # only first
        if not invoke:

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

        continue

      # no need to check if -co
      contents.append(argument)

    # execute commands
    for content in contents:

      invoke(content)

def main(content):

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

  invoke(argument)

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.

Source Distribution

stroke-2.3.2.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

stroke-2.3.2-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file stroke-2.3.2.tar.gz.

File metadata

  • Download URL: stroke-2.3.2.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.20.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for stroke-2.3.2.tar.gz
Algorithm Hash digest
SHA256 15f234d47ccf8a92aa77675fb7e6efbbbb902550c9eb1de8eef5cf16ebe97fd9
MD5 2b996cdfba1d7bd985683d03ed335eca
BLAKE2b-256 220e051d301784559bdc8caa4247e4585306e037c65561e51946bb8de4d1eb96

See more details on using hashes here.

File details

Details for the file stroke-2.3.2-py3-none-any.whl.

File metadata

  • Download URL: stroke-2.3.2-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.20.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for stroke-2.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 affdde6fc25e5c3fd0ec79fd29daaba97a76cc6b12943015a551bcebb66b4672
MD5 bed2e7c99308468657b78a57f62fa0d8
BLAKE2b-256 5213ea1349d287f6bd9b774fdc814b4fd77a70872b42969475057d1e96833b0a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page