Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8774351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:32:39+00:00 2026-06-13T18:32:39+00:00

I have been working on a DSL using pyparsing. I have a couple of

  • 0

I have been working on a DSL using pyparsing.

I have a couple of keywords. The doumentation on pyparsing keyword class is found at http://packages.python.org/pyparsing/pyparsing.pyparsing.Keyword-class.html

I defined them as

this = Keyword("this", caseless=cl)
that = Keyword("that", caseless=cl)

I have a dictionary where the above keywords translate into numbers:

helper_dict = {"this":-1, "that":1}

the problem I am facing is that I am not able to get a consistent string representation for them. When I try str(this), it comes with the quotes. So I can not really use the dictionary without getting a key error. I mean I get a KeyError when I try any of the following:

helper_dict[this]
helper_dict[this.__str__()]
helper_dict[str(this)]

How can I get a proper string representation

I looked at both the documentation for keyword and the super class of keyword and I can not figure out which function is actually supposed to do this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:32:41+00:00Added an answer on June 13, 2026 at 6:32 pm

    this and that are not strings, they are pyparsing expressions defined using strings, but they are not strings. As for helper_dict, you have defined the keys using the strings “this” and “that”. You will get back strings from the this and that expressions after they have parsed some input containing their defining input strings.

    Here is some console experimenting to show you how this works:

    >>> from pyparsing import *
    >>> this = Keyword("this", caseless=True)
    >>> that = Keyword("that", caseless=True)
    >>> result = (this|that).parseString("This")
    >>> print result
    ['this']
    

    Note that Keyword always returns a consistent token in terms of upper/lower case, regardless of the input string’s case. CaselessLiteral and caseless Keyword always return tokens in the case of their defining strings, not the case of the input string.

    >>> print result[0]
    this
    >>> print type(result[0])
    <type 'str'>
    

    The parsed data is returned in a ParseResults object, which supports list, dict, and object-style addressing. Here the 0’th element is a string, the string “this”. You can use this string to match the key “this” defined in helper_dict:

    >>> helper_dict = {"this":1, "that":-1}
    >>> helper_dict[result[0]]
    1
    

    Or define a parse action to do this substitution for you at parse time (replaceWith is a helper method defined in pyparsing:

    >>> this.setParseAction(replaceWith(1))
    "this"
    >>> print (this | that).parseString("This")[0]
    1
    

    — Paul

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have been working on this question for a couple hours and have come close
I have been working off of a tutorial of a tower defence game( http://www.guahanweb.com/2009/01/31/tower-defense-in-as3-part-iv/
I have been working on a website using MVC Preview 1 since it came
I have been working on a Notes integration project and I am using the
I have been working on a system which I'm using protobuf-net (version 2.0.0.480) for
I have been working on this for a couple of days and am quite
I have been working on an app for a couple of months now, but
I have been working with Visual Studio (WinForm and ASP.NET applications using mostly C#)
I have been working in Python for a few months now, and it has
I have been working through the tutorial at http://bkiers.blogspot.de/2011/03/creating-your-own-programming-language.html . In short, a small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.