Last active
August 29, 2015 14:22
-
-
Save jgstew/fab58a179ead1637dbcd to your computer and use it in GitHub Desktop.
parse character stats from http://arcticmud.org/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 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 "Total: " + str(statTotal) | |
print "Average: " + str(statAverage) | |
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