[Tutor] What these Python user-defined functions do?

Alan Gauld alan.gauld at yahoo.co.uk
Sat May 21 04:59:31 EDT 2016


On 21/05/16 02:21, Max Jegers wrote:

> class View:
> 	def __init__(self):
>               self.handle = None
> 
> 	def order(self, n):
>               return hostviewOrder(handle, self.handle, n)
> 
> 	def put(self, fieldName, value, verify=1):
>               if verify == True:
>                      verify = 1
>               elif verify == False:
>                      verify = 0
>               return hostviewPut(handle, self.handle, fieldName, value,
> verify)


This is a very odd function.
The if/else bit at the top makes no sense at all.

> 	def read(self):
>               return hostviewRead(handle, self.handle)
> 
> 	def browse(self, filter="", ascending=1):
>               if ascending == True:
>                      ascending = 1
>               elif ascending == False:
>                      ascending = 0
>               return hostviewBrowse(handle, self.handle, filter, ascending)

Same comment regarding the if/else here.

> 	def fetch(self):
>               return hostviewFetch(handle, self.handle)

Other than the if/else issues above the methods are just
wrappers around the hostview library, which we obviously
can't see so can't comment on.

I also notice that self.handle never gets set to anything
other than None, which is odd unless there are other methods
you are not showing us? Also a handle argument is passed to
the hostview functions but its not clear where that is defined...

> Here is a part of the script I am trying to understand:
> 
> def ExportShipment():
>          f = open("c:\\" + me.get("SHN") + ".txt", "w")
>          h = View("Header", *1*)
>          d = View("Details", *1*)
>          h.*order*(1) # SHN
>          h.*put*("SHN", me.get("SHN"), 1)
>          h.*read*()
>          f.write("H," + h.get("LOCATION") + "," + h.get("ADDR1") + "\n")

I'm not sure where all the * characters are coming from,
I assume they are not part of the code?
But the code above opens a file and creates two View objects, one for
the header and one for the details. However the View init() function
above does not have any paramaters so this should fail. Also there is
reference to a get() method which is not part of the class definition
above. And there is reference to a me object but its not defined above
either.


>          d.*browse*("SHIUNIQ=" + "{:.0f}".format(h.get("SHIUNIQ")), 1)
>          while (d.*fetch*() == 0):
>                  f.write("D," + d.get("ITEM") + "," +
> "{:.0f}".format(d.get("QTYSHIPPED")) + "\n")
>          f.close()

Again this code does not match the code above. There is no View.get()
method. And View.browse() does not change the View's state so its hard
to see what the get() method could return. It could only work if there
were either a lot of global variables being used in the background(by
listview) or if thee is a lot more to the View definition than we have seen.

> I understand what file operations do. However I can’t get what these
> functions do:
> 
> *order(), put(), read(), browse(), fetch()*.

They are trivial wrappers around the listviewXXX functions
that are presumably defined in another library somewhere.
But the code is generally inconsistent and definitely
not good Python on any level.

> Function definitions in the library are of little help for now, as all
> functions feature handle and self.handle in their returns; however I cannot
> find out what handle and self.handle may mean in this context.

> Please let me know if I should provide more info for my question to make
> sense.

Apart from the issue over handle its not clear what you don't
understand. However until you get the two code segments
harmonised you will never make sense of it. The code as it
stands is inconsistent and could never work.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list