[Tutor] Python function argument passing problem

Alan Gauld alan.gauld at btinternet.com
Thu Jan 9 12:18:28 CET 2014


On 09/01/14 00:33, Manoj Rout wrote:

> I have been working with python from last couple of weeks. As I am new
> to python I have a problem with my work. So can you please look into the
> below code.
>
> with open('C:\\Users\\Manoj\\Desktop\\XMC1100_rm_v1.0.6_SVD.xml','r') as
> xmlfile:
>
>                  svdlines = xmlfile.readlines()


Your indentation is way too big.
Reduce it to 2-4 spaces and it will be much more readable.
It may be an issue with your mail client but the current
spacing is hard to parse.
I've tried to bring it together but may have misaligned
some blocks in the process...


>
>        def func_register(num):
>

Its usual to define functions outside of the program flow.
ie not inside the with statement. The definition of your
function does not depend on the context of the 'with'
so it can be placed outside.


>                 for num,svdline in enumerate(svdlines):

You just hid the num parameter in your function definition.
Either change the name or lose the parameter.
Also since svdlines is the only global you reference you
should probably just pass it in as a parameter, it will
make your code more reusable.

>                     if '<register>' in svdline:
>                         start = num+1
                           break

>                 for num,svdline in enumerate(svdlines[start:]):

If you never find 'register' above then start will not be set
and you will get an error. Its a good idea to initialize variables.

                       if '</register>' in svdline:
                          end = num+1
                          end = start+end
>                        break
>
>                 count=0
>                 for num,svdline in enumerate(svdlines[start:end]):
                         if '<name>' in svdline:
                             count +=1
                             if count == 1:
                                Registername = re.findall('name>([^ 
]*)<',svdline)
                                print "Register Name is:",Registername
                         if '<addressOffset>' in svdline:
                             OffsetAddress_SVD = 
re.findall('addressOffset>([^ ]*)<',svdline)
                             print "OffsetAddress is :",OffsetAddress_SVD
                         if '<resetValue>' in svdline:
                             resetvalue_SVD = re.findall('resetValue>([^ 
]*)<',svdline)
                             print "resetValue in SVD is :",resetvalue_SVD
                             end=end+1
                             print end


>     Here I want to define a recursive function func_register

It doesn't appear to be recursive? It never calls itself.

> take the whole contents of file and file pointer position

I assume you mean num is the file pointer position?
If so you throw num away almost immediately in your for loop.
And you never return anything from the function.

> iteration  and do the assigned work.

What is the "assigned work"? It doesn't seem to do anything
other than print a few values.

I assume in the following that by iterations you are
referring to the 3 for loops?

> After first iteration  it will update the file pointer position

Its not doing that at the moment. It creates a new variable start.

> Then in second iteration it should start from that particular
 > position of that file .

Yes and it identifies the line after </register>

And the third loop does some testing and printing within
the defined range.

>                                  I have written the above code and this
> code will work if it is not inside the function. i. e outside the “def
> func_register()”. So could you please help me iin this and also I have
> added the file .

When you say it does not work what exactly happens?
Do you get an error message? If so please post it in its entirety
Do you get the wrong result? What did you expect what did you get?


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list