Skip to content

Instantly share code, notes, and snippets.

@jgstew
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgstew/fab58a179ead1637dbcd to your computer and use it in GitHub Desktop.
Save jgstew/fab58a179ead1637dbcd to your computer and use it in GitHub Desktop.
parse character stats from http://arcticmud.org/
# parse character stats from http://arcticmud.org/
# - likely works with other MUDs
import re
# http://regexr.com/397kv
# http://www.pythonregex.com/
# Str (?P<Str>\d+) Int (?P<Int>\d+) Wis (?P<Wis>\d+) Dex (?P<Dex>\d+) Con (?P<Con>\d+)( Cha )*(?P<Cha>\d+)*
# Str (\d+) Int (\d+) Wis (\d+) Dex (\d+) Con (\d+)
def main(sCharStats):
import re
statTotal = 0
statAverage = 0
statLength = 0
print
print sCharStats
regex = re.compile("Str:? (?P<Str>\d+) Int:? (?P<Int>\d+) Wis:? (?P<Wis>\d+) Dex:? (?P<Dex>\d+) Con:? (?P<Con>\d+)(?: Cha:? (?P<Cha>\d+))?",re.IGNORECASE)
result = regex.search(sCharStats)
#print result.groupdict()
#print result.groups()
for r in result.groups():
# http://stackoverflow.com/questions/152580/whats-the-canonical-way-to-check-for-type-in-python
if isinstance(r, str):
statTotal+=int(r)
statLength+=1
statAverage = statTotal / float(statLength)
print
print "Total: " + str(statTotal)
print "Average: " + str(statAverage)
print
if __name__ == '__main__':
main(" Abilities: Str 19 Int 17 Wis 20 Dex 19 Con 18 Cha 18")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment