C + + Turn Python

Source: Internet
Author: User
Tags define local

Two days ago, there are more than 10,000 lines of C + + projects need to turn to Python, logic is very complex, to understand logic and then write in Python the expected time is longer than direct, so we decided to turn to Python, encountered some pits, here denoted.

The biggest pit is the data structure, Python has no array, can only use the list,c++ function of the array directly to the address in, to do a variety of operations, especially two-dimensional arrays. Python can only use list, two-dimensional arrays can only nest list. C + + can pass a reference of a class object into a function. Python has to pass this kind of object in, change later again out of the outside still have to = a bit. In fact Python can also be referenced. Passing a reference into a function considerations:
1, can not be used inside the function param = [] or param = List_other
2, can not be transmitted at the call Param + 3 and so on
In the 2nd case, you can use a 0 o'clock Arg_param = param[3:] Use Arg_param to pass in the function call and then recycle the value to the original parm. Of course, if the const is the best, the return of the outside will no longer have to assign value.

Tip: First step: Translate C + + code into "python-like C + +" intermediate code. Step Two: Translate this intermediate code into python. After the first step, you can conduct a logical back test to see if you can still work properly.

Tips: The first step in the transfer of the array can be const all the const, this is actually C + + editing habits, as far as possible to form a const (whether the pointer or reference) are const. So python can be very reassuring to go directly to the reference, which does not change.

Tip: CopyMemory copy array, all changed to:

Template<class t>
static void Copymemory_py (T dest[], const t from[], int count, int offset1 = 0, int offset2 = 0
{for
	(int nidx = 0;nidx < Count;++nidx)
	{
		Dest[offset1 + nidx] = From[offset2 + nidx];
	}
def copymemory_py (dest, SRC, count, Offset1 = 0, Offset2 = 0): For
    Nidx in range (count):
        Dest[offset1 + nidx] = SR C[offset2 + Nidx]

Of course, the strict requirements of the two types are the same, if not the same template parameters can be added.
Tip: All arrays, when defined, are initialized directly to a fixed length.

def constructArray1 (Array, count):
    for I in Range (count):
        array.append (0)

def constructArray2 (Array, Count1, Count2): For
    I in range (count1):
        tmparray = [] for
        J in Range (Count2):
            tmparray.append (0) C16/>array.append (Tmparray)
Tip: C + + pod call memset empty, all inside a clear () function, put the contents of the inside empty. Over C + + Python to implement the Clear ()
Tips: Seal off C + + POD copy construtor, assign Construtor, directly private off. or check to see if it's used.
Private:
	tagstackhandcardinfo& operator= (const tagstackhandcardinfo& RHS);
	Tagstackhandcardinfo (const tagstackhandcardinfo& RHS);
Tips: copying arrays, sealing functions
Compatible with Python, pass two references, one assignment to another
static void Tagoutcardtyperesult_copy (tagoutcardtyperesult& lhs, const tagoutcardtyperesult& rhs)
{
	lhs.cbcardtype = Rhs.cbcardtype;
	Lhs.cbcardtypecount = Rhs.cbcardtypecount;
	for (int nidx = 0;nidx < Max_type_count;++nidx)
	{
		Lhs.cbeachhandcardcount[nidx] = rhs.cbeachhandcardcount[ NIDX];
	}
	for (int i = 0;i < Max_type_count;++i)
	{for
		(int j = 0;j < Max_count;++j)
		{
			lhs.cbcarddata[i][j ] = Rhs.cbcarddata[i][j];}}
def tagoutcardtyperesult_copy (LHS, RHS):
    lhs.cbcardtype = rhs.cbcardtype Lhs.cbcardtypecount
    = Rhs.cbcardtypecount for
    nidx in range (max_type_count):
        Lhs.cbeachhandcardcount[nidx] = RHS.CBEACHHANDCARDCOUNT[NIDX] for

    i in range (Max_type_count): for
        J in Range (Max_count):
            lhs.cbcarddata[ I][J] = Rhs.cbcarddata[i][j]
Tip: Function calls in a class in Python, all with self., you can walk in three steps: Step1, text replacement "FuncName" => self. FuncName ". Step2, Text replaces "Self.self.FuncName" => self. FuncName "(This step actually came to the middle of my mind, some of the previous functions have been written as self.) FuncName, after the first step will become hundred self.self.FuncName, so add this step fault tolerance). Step3, text replacement "Def Self." FuncName "=>" def FuncName "

Tip: Before you start all the work, get all the function call relationships. Here I think of a rather stupid way, use Office Project to define each function as a task, the function called other functions to its predecessor, so that you can successfully get a translation sequence diagram:

Then according to this graph, the translation order of the functions is obtained:

 Getcardtype sortcardlist randcardlist Removecard Isvalidcard Getcardlogicvalue Comparecard makecarddata Analysebcarddata analysebdistr Ibuting Searchoutcarda searchoutcardb getallbomcard getalllinecard Getallthreecard Ldoublecard Getallsinglecard Analyseoutcardtypea ANALYSEOUTCARDTYPEB combination Permutatio
									n analysesinlecardcount setusercard setbackcard setlandscorecarddata removeusercarddata Bankeroutcarda Bankeroutcardb Upsideofbankeroutcarda Upsideofbankeroutcardb Und Ersideofbankeroutcarda Undersideofbankeroutcardb Landscore l2c _testoutallca Rd Analysefourcardtype Testoutallcard Islargestcard verifyoutcard 
From left to right, is the translation order.
Tip: C + + programmers sometimes like to define local variables, or just for logical clarity, using two curly braces to write a piece of code, Python uses if True directly: to ensure alignment

Tip: If the C + + code you are trying to translate is not very neat, you can use Visual Studio to format CTRL-A-Select, Ctrk K F. This step can save a lot of things because Python alignment needs to be very careful. A typical C + + code like this is:

for (int nidx = 0;nidx < Ncount;++nidx)
    do_something (some_param1, some_param2);
    Do_someotherthing (SOME_PARAM3, SOME_PARAM4);
If only these two lines believe that a normal person can see it at a glance, but if the following is dragged very long, or
Do_something
After the function is followed by the curly braces, and the contents are very long, the following code is extremely difficult to find alignment problem.

Tip: In a recursive function, or in a function that requires attention to performance, an array can be offset to implement a Python version of C + + with a usage like array + 3:

Original function:
void Func (int array[])
{
    if (...)
        Func (array + 3);
    ......
}
new function
void Func (int array[], int offset = 0)
{
    if (...)
    Func (array, offset + 3);
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.