Python method arguments with spaces

I would like to create a simple file / DSL format that will allow my users to enter data. My system is in python and using the python parser is attractive. A syntax like this for defining a data item seems pretty convenient.

Allocation(Param1 = Val1, Param2 = Val2 )

      

However, it does not support parameter names with spaces.

Allocation(Param 1 = Val1, Param 2 = Val2 )

      

Friendly versions of the Python parser might look like this, but are not very user friendly.

Allocation(("Param 1",Val1), ("Param 2",Val1) )
Allocation(**{"Param 1":Val1, "Param 2":Val1} )

      

Is there a way to make this more readable in python?

+1


source to share


4 answers


Here are my preferences.

AllocationSet(
    Alloc( name="some name", value=1.23 ),
    Alloc( name="another name", value=2.34 ),
    Alloc( name="yet another name", value=4.56 ),
)

      



These are relatively lightweight class declarations to create. The resulting structure is also pleasant to handle.

+1


source


I would guess there will be some way to do this. But I feel compelled to ask if there really is a big difference in readability from this

Allocation(Param1 = Val1, Param2 = Val2 )

      

For this:

Allocation(Param 1 = Val1, Param 2 = Val2 )

      

to make this big difference? I'm sure there is a way to do what you want to do, but my first problem is that the effort will be worth the result.



My goal is to provide a DSL that can be used to enter data into the system. In the above script, the parameters will be the names of people and the values ​​will be percentages.

I have a better understanding of what you want to do now, but I still think you might have to sacrifice some readability to get what you want. Personally, I would go with something like:

Allocation(
    { 'name1' : value1,
      'name1' : value2, }
)

      

If that's not something you can go with, then you might need to reconsider whether you want to use Python for your DSL or go with something homemade. Whitespace allows too much ambiguity for most programming languages.

If you still want to use this with python, you might consider posting to the C-API SIG ( SIGS ), or perhaps the python-dev list (as a last resort). The only way I can do this is to insert a python interpreter into a C / C ++ program and do some hacking with it (which can be tricky!).

+3


source


If I am not mistaken about your main premise here, you shouldn't be in the way of writing a class that parses your own syntax, and then using that custom syntax as a single argument string:

Allocation("Param 1=Check Up; Param 2=Mean Value Theorem;")

      

In this example, semicolons act as separators for names and values, and equals represents the name-value separator. In addition, you can easily configure your parser to accept custom delimiters as part of the object constructor.

If it seems too complicated to write a parser, consider that (for a syntax like this) you could get your values ​​by simply splitting the string into

/\s*;\s*/

      

and then on

/\s*=\s*/

      

to quickly get name-value pairs. You also have the ability to choose any of several argument parsers already written for Python.

Admittedly, this doesn't use Python as the argument parser, which is a consideration you'll need to weigh against the simplicity of this approach.

+1


source


You can do it:

def Allocation(**kwargs):
    print kwargs

myargs = {"Param 1":Val1, "Param 2":Val1}
Allocation(**myargs)

      

Edit: Your edit now includes my answer, so no, there is no easier way to have spaces in your keyword arguments.

0


source







All Articles