Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Portin from Python to C++

  1. #1
    Join Date
    Jun 2008
    Location
    Tennessee
    Beans
    3,421

    Portin from Python to C++

    So, I'm mostly a Python guy, and I have some open source projects in Python/QT. I'd like these to run better on older systems, and I'm contemplating porting some of them to C++.

    I'm not really a C++ guy, though; I've taken classes on it, read books on it, and seen enough of it (hard to avoid if you work with QT), but I've never really developed anything in it.

    I'm not looking for a C++ tutorial; I know the basics. I guess what I'm looking for is some help or advice on how to approach C++ when coming from a higher-level language.

    Anyone have any thoughts on this?

  2. #2
    Join Date
    Oct 2010
    Location
    Finland
    Beans
    252
    Distro
    Ubuntu

    Re: Portin from Python to C++


  3. #3
    Join Date
    Dec 2012
    Beans
    19

    Re: Portin from Python to C++

    Actually a better transition would be:
    python -> php -> c++
    the transition should be easy: i did it the opposite way
    i know this would hijack your post, but i have quick question for you. i know this seems "noobish" but i have never had this problem in any other language. when im printing literal strings and then variables, it prints the variable with a space on either side. so if the value is "cat" it becomes " cat ".
    if i do strip(" ") will that work? is there a better function?

  4. #4
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Portin from Python to C++

    you mean python's print a, b, c that will print "value_of_a value_of_b value_of_c"?

    it works that way. If you want string as a single blob with no separation you need to use formatting string, similar in its concept to printf() function

    Code:
    >>> a=333
    >>> b="xyz"
    >>> print "----%d----%s" % (a,b) # printf style formatting string
    ----333----xyz
    print "----{1}----{0}".format(a,b) # newer python formatting
    ----xyz----333
    http://www.python.org/dev/peps/pep-3101/

    in newer versions of print (in v2.6 you have to use from __future__ import print_function statement) - it allows to define separator and line end

    Code:
    >>> print( a, b, sep="^", end="=\n" )
    333^xyz=
    Last edited by Vaphell; December 22nd, 2012 at 01:17 AM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  5. #5
    Join Date
    Dec 2012
    Beans
    19

    Re: Portin from Python to C++

    ok so c style then thank you

  6. #6
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Portin from Python to C++

    i'd say newer python style is much more powerful and it's not that different than C-style (% placeholders are replaced by {} placeholders, nothing spectacular), because it allows to define alignment, padding and whatnot right off the bat. Look at the examples
    http://docs.python.org/2/library/string.html
    also parameters can be named so you don't have to maintain order.
    Last edited by Vaphell; December 22nd, 2012 at 01:31 AM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  7. #7
    Join Date
    Dec 2012
    Beans
    19

    Re: Portin from Python to C++

    thanks that really helps

  8. #8
    Join Date
    Apr 2012
    Beans
    39

    Re: Portin from Python to C++

    Quote Originally Posted by lykwydchykyn View Post
    So, I'm mostly a Python guy, and I have some open source projects in Python/QT. I'd like these to run better on older systems, and I'm contemplating porting some of them to C++.

    I'm not really a C++ guy, though; I've taken classes on it, read books on it, and seen enough of it (hard to avoid if you work with QT), but I've never really developed anything in it.

    I'm not looking for a C++ tutorial; I know the basics. I guess what I'm looking for is some help or advice on how to approach C++ when coming from a higher-level language.

    Anyone have any thoughts on this?
    I don't think it would be too difficult. I'd start by defining the classes and member functions first, and then define member variables as required. Remember you've got the option to make members private or protected now too!

    I'd image the biggest challenge would be when it comes to string manipulation. Nothing difficult though, if you've got a good C++ reference and an understanding of the python code.

  9. #9
    Join Date
    Jun 2008
    Location
    Tennessee
    Beans
    3,421

    Re: Portin from Python to C++

    Quote Originally Posted by na5h View Post
    Thanks; that's a good start, but kind of basic.
    Quote Originally Posted by DTSDeveloper View Post
    Actually a better transition would be:
    python -> php -> c++
    the transition should be easy: i did it the opposite way
    I do a lot of PHP coding as well. I think the challenge is that most C++ materials teach you all the low-level, built-in stuff and ignore the STL or library features. Do C++ programmers really work with arrays most of the time, or are there good abstractions out there for, say, collection classes (lists, dictionaries/hashes, tuples, etc)?

    Quote Originally Posted by SuperCamel View Post
    I don't think it would be too difficult. I'd start by defining the classes and member functions first, and then define member variables as required. Remember you've got the option to make members private or protected now too!

    I'd image the biggest challenge would be when it comes to string manipulation. Nothing difficult though, if you've got a good C++ reference and an understanding of the python code.
    Right now the biggest thing that sends me for a loop are pointers. I understand what they are, I just don't always understand why they get used when they do. I read C++ code (for example, the code snippets on this page: http://doc.qt.digia.com/qt/webkit-fancybrowser.html) and it's completely non-obvious to me why sometimes variables are declared as pointers and sometimes not.

  10. #10
    Join Date
    Apr 2005
    Location
    Hampshire, UK
    Beans
    1,274

    Re: Portin from Python to C++

    Quote Originally Posted by lykwydchykyn View Post
    I do a lot of PHP coding as well. I think the challenge is that most C++ materials teach you all the low-level, built-in stuff and ignore the STL or library features. Do C++ programmers really work with arrays most of the time,
    Gosh, no - using arrays directly is strongly discouraged.


    or are there good abstractions out there for, say, collection classes (lists, dictionaries/hashes, tuples, etc)?

    Yes to all - std::list, std::map, and (in C++11 onwards) std::unordered_map and std::tuple. If you are using Qt, you can use the Qt containers which have a (IMO) much nicer API than their often weirdly named and clunky STL counterparts, although they are not built for raw performance. Since this is a port of a Python app, though, this probably won't make much difference. Qt's QString class is also something I would much prefer to use over std::string.

    Right now the biggest thing that sends me for a loop are pointers. I understand what they are, I just don't always understand why they get used when they do. I read C++ code (for example, the code snippets on this page: http://doc.qt.digia.com/qt/webkit-fancybrowser.html) and it's completely non-obvious to me why sometimes variables are declared as pointers and sometimes not.
    Qt handles a lot of memory management for you, so some things are always allocated on the heap as Qt will eventually delete them for you. As a rule of thumb, objects that derive from QObject are almost always allocated on the heap.

    Any specific questions that you run into, please ask away

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •