referencing a main method in an imported module from a newbie to Python -


apologies if thinking behind question wrong: picking python go. i've written python module goes off database , gets bunch of standard data used lots of other modules, thought practise build reusable module. there class:

class talktooracle: 

with attribute

gres = none 

a bunch of other functions, , main function @ end

def main(): 

with line don't understand seems automatically run main method when class referenced(?)

if __name__ == "__main__":     main() 

main runs functions in class in order, , sticks results in attribute: gres

all runs great stands alone. file saves getoradata.py want reuse code so....... in python module

import getoradata   class getinstances:     def orainstancedata(self):         log.info('getting instance routing data')         hadata = getoradata.talktooracle()         log.info('debug1') 

all goes pear shaped here. want run other module, , access results in attribute gres. tried (and every other variation think of) no joy. feel i'm missing fundamental.

hadata.main() log.info('debug2') hadataset = hadata.gres log.info('debug3') log.info('query fetched ' + str(len(hadataset)) + ' rows') 

any gratefully appreciated.

after work, here modified code still throws error

import logging import time import uuid import getoradata datetime import datetime  log = logging.getlogger() log.setlevel('info')  class getinstances:     def orainstancedata(self):         log.info('getting ha instance routing data')         hadata = getoradata.main()         hadataset = getoradata.gres         log.info('query fetched ' + str(len(hadataset)) + ' ha instances query')  def main():      logging.basicconfig()     start = time.time()      #oracle routines      orainstances = getinstances()     orainstances.orainstancedata()   if __name__ == "__main__":     main() 

error:attributeerror: 'module' object has no attribute 'gres'

you need explicity call main module attribute of getoradata:

getoradata.main() getoradata.gres 

when import module, of attributes in its namespace.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -