Skip to content
This repository has been archived by the owner on Aug 29, 2020. It is now read-only.

Commit

Permalink
Parse arrays into new data type
Browse files Browse the repository at this point in the history
  • Loading branch information
coldfix committed May 1, 2014
1 parent 2ebe9e0 commit d68d7d7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
27 changes: 25 additions & 2 deletions madseq.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def __str__(self):
return str(self.value)

def fmtArg(self):
return self.assign + str(self.value)
return self.assign + str(self)

@classmethod
def parse(cls, text, assign='='):
Expand All @@ -150,7 +150,10 @@ def parse(cls, text, assign='='):
try:
return parse_string(text)
except ValueError:
return Symbolic.parse(text, assign)
try:
return Array.parse(text, assign)
except ValueError:
return Symbolic.parse(text, assign)


def parse_number(text):
Expand All @@ -173,6 +176,26 @@ def parse_string(text):
raise ValueError("Invalid string: %s" % (text,))


class Array(Value):

@classmethod
def parse(cls, text, assign=False):
"""Parse a MAD-X array."""
if text[0] != '{':
raise ValueError("Invalid array: %s" % (text,))
if text[-1] != '}':
raise Exception("Array not terminated correctly: %s" % (text,))
try:
return cls([Value.parse(field.strip(), assign)
for field in text[1:-1].split(',')],
assign)
except ValueError:
raise Exception("Array not well-formed: %s" % (text,))

def __str__(self):
return '{' + ','.join(map(str, self.value)) + '}'


class Symbolic(Value):

"""Base class for identifiers and composed arithmetic expressions."""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

setup(
name='madseq',
version='0.2',
version='0.3',
description='Parser/transformator for MAD-X sequences',
long_description=long_description,
author='Thomas Gläßle',
Expand Down

0 comments on commit d68d7d7

Please sign in to comment.