0

QUESTION: what is correct syntax for ADS1115 object without
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

QUESTION: what is correct syntax for ADS1115 object without

by jrbrearley on Sun Jan 10, 2021 5:08 pm

The example below works fine:

Code: Select all | TOGGLE FULL SIZE
    import board, busio
    import adafruit_ads1x15.ads1115 as ADS
    i2c_1 = busio.I2C(board.SCL, board.SDA)
    ads10 = ADS.ADS1115(i2c_1, address=0x48)


I am trying to avoid using "import as" and use full length class names so I get a better understanding of class structure and who calls what.

Code: Select all | TOGGLE FULL SIZE
    import adafruit_ads1x15.ads1115 # works
    ads10 = adafruit_ads1x15.ads1115(i2c_1, address=0x48) # NameError: name 'adafruit_ads1x15' is not defined
    ads10 = adafruit_ads1x15.ads1115.ADS1115(i2c_1, address=0x48)  # NameError: name 'adafruit_ads1x15' is not defined

Could you please show me the correct syntax?

jrbrearley
 
Posts: 3
Joined: Sun Jan 03, 2021 3:45 pm

Re: QUESTION: what is correct syntax for ADS1115 object with

by kevinjwalters on Sun Jan 10, 2021 5:55 pm

What does

Code: Select all | TOGGLE FULL SIZE
import adafruit_ads1x15.ads1115
dir(adafruit_ads1x15.ads1115)


output from REPL on the same device?

kevinjwalters
 
Posts: 836
Joined: Sun Oct 01, 2017 3:15 pm

Re: QUESTION: what is correct syntax for ADS1115 object with

by jrbrearley on Mon Jan 11, 2021 2:02 pm

The issue seems to be a little more complex, than I initially described.

The attached script ada_dbg1.py sets up ads1115 objects using standard import command with hardcoded list of modules, works OK.
In particular, "ads10 = adafruit_ads1x15.ads1115.ADS1115(i2c_1, address=0x48)" works, which is getting to where I want to be.

The attached script ada_dbg2.py sets up ads1115 objects using a loop with error handling to load a string list of modules via:
Code: Select all | TOGGLE FULL SIZE
globals()[pkg_trunc] = importlib.import_module(pkg_trunc)

The same line of code creating ads10 gets error:
Traceback (most recent call last):
File "ada_dbg2.py", line 33, in <module>
ads10 = adafruit_ads1x15.ads1115.ADS1115(i2c_1, address=0x48)
AttributeError: module 'adafruit_ads1x15' has no attribute 'ads1115'


When I look at the global variables, from ads_dbg1.py, I see:
'adafruit_ads1x15': <module 'adafruit_ads1x15' from '/home/pi/.local/lib/python3.7/site-packages/adafruit_ads1x15/__init__.py'>,

When I look at the global variables, from ads_dbg2.py, I see:
'adafruit_ads1x15.ads1115': <module 'adafruit_ads1x15.ads1115' from '/home/pi/.local/lib/python3.7/site-packages/adafruit_ads1x15/ads1115.py'>,

So I tried doing the import without the .ads1115 appended, & got:
'adafruit_ads1x15': <module 'adafruit_ads1x15' from '/home/pi/.local/lib/python3.7/site-packages/adafruit_ads1x15/__init__.py'>,

But I still get an error.
Traceback (most recent call last):
File "ada_dbg2.py", line 33, in <module>
ads10 = adafruit_ads1x15.ads1115.ADS1115(i2c_1, address=0x48)
AttributeError: module 'adafruit_ads1x15' has no attribute 'ads1115'

Suggestions?
Attachments
print_data.py
parses output of dir(obj) command to make results more human readable
(2.28 KiB) Downloaded 2 times
ada_dbg2.py
sets up ads1115 objects using loop with importlib.import_module, gets error creating ads1115 object
(1.65 KiB) Downloaded 2 times
ada_dbg1.py
sets up ads1115 objects using standard import command with hardcoded list of modules, works OK
(1.02 KiB) Downloaded 2 times

jrbrearley
 
Posts: 3
Joined: Sun Jan 03, 2021 3:45 pm

Re: QUESTION: what is correct syntax for ADS1115 object with

by jrbrearley on Thu Jan 14, 2021 12:07 pm


jrbrearley
 
Posts: 3
Joined: Sun Jan 03, 2021 3:45 pm

Please be positive and constructive with your questions and comments.