python in c++ application

This is a discussion on python in c++ application within the C++ Programming forums, part of the General Programming Boards category; Hello.. I would like to add some scripting to my c++ aplications, so that users would be able to customize ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    python in c++ application

    Hello..

    I would like to add some scripting to my c++ aplications, so that users would be able to customize output formats (they could script how they want application to output certain things).

    Is it possible to do this with boost python?
    If so, does it much affect program's size?

    I have read a little documentation about boost python, and it seemed like you can only build c++ programs for use with python..

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    It's possible with Boost.Python, yes. Embedding the Python interpreter in the C++ program is a bit ... iffy, but it works. As for the code size increase - well, you have to distribute the Python interpreter with your app, but it's a shared library, not part of the exe. As for Boost.Python itself, I can only say, try it out.

    Note: In my opinion, the Python embedding/extension API is a prime example of how not to design an API. Here's a tip if you're working with it: if any function takes a non-const char* as a parameter, even though there's absolutely no reason why Python should want to modify that string, then this means:
    1) Python won't modify the string. However, it also expects you not to do so.
    2) You are responsible for keeping this string alive long enough. No function-local buffers! (That has bitten me hard.)
    3) Long enough typically means until you shut down the Python interpreter.

    In particular, if you load a module from C++ code, the module name parameter is such a string.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    I had in mind I could use python as a scripting language for my application - only c++ functions that I would declare/define for scripting, would be used there..

    For instance if user wanted to output mp3 information, he would be able to customize it like this:
    "$artist - $title ($length) - $rand_num(function_parameteres)"..

    would output:
    "Some Artist - Unknown track (3:00) - 9382"

    Maybe user could write this as python script and this would be stored as a string inside my program. This string would later be processed by c++ application. Is this possible? Would kind of approach be good?


    I thought it would be the best way to solve that kind of problem using some scripting language inside my application.. That would even allow users to have more options to customize (using variables, etc.)

    Would it be more suitable to use some other approach to this? I already tried solving that kind of thing using boost spirit, and I wasnt glad with results.. Maybe boost range would be more suitable?

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    It's possible, yes. Of course, embedding an entire scripting language just for some pattern replacement may be overkill.

    Boost.Range has an entirely different purpose and nothing to do with your problem.

    Boost.Spirit would work for simple replacements, as would Boost.Regex. For even simpler stuff, you could also try Boost.Format.


    In the project where I embedded Python via Boost.Python, the idea was to write entire plugins for the application in Python. Different scale, I'd say.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Boost.Regex is too simpler for that kind of stuff..

    I would like to have a list of functions for instance:
    "random_number", random_number_function, <int> (int is an argument - means you have to call it with int)

    When parser/interpreter would find "$random_number(382)" in string and if arguments matched (382 in this case), then it would call this function and replace text with returned string.

    This sounds very easy, but its far from this..

    Maybe any idea how to achieve that?
    Maybe embed python would be more suitable? I dont want my exe to depend on any other file and I dont want to gain too much size.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Actually, that's doable in Regex. Not even too hard. The main problem would be extending it for new function. The system doesn't scale.

    However, embedding Python means you either distribute the Python DLL with your app or you find a static Python version to link to - and that will be a megabyte at least.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need to edit text from within my application.
    By spiroth10 in forum C++ Programming
    Replies: 2
    Last Post: 12-15-2006, 02:15 PM
  2. Problem with com application
    By amardon in forum C++ Programming
    Replies: 3
    Last Post: 10-06-2005, 05:50 AM
  3. MFC run application by clicking on file...
    By dug in forum Windows Programming
    Replies: 4
    Last Post: 12-02-2004, 03:33 AM
  4. Win application not very portable
    By swed in forum Windows Programming
    Replies: 5
    Last Post: 10-01-2001, 11:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21