I'm working on building a python package that is basically a wrapper around the request package and designed to make a variety of api calls to a set of databases easier.
Currently, my package has the following directory structure:
package\
- __init__.py
- coreFunc.py
subpackage\
- __init__.py
- mod1.py
- mod2.py
The primary functionality of the package lies in mod1.py
and mod2.py
. mod1.py
looks like this:
import requests as _requests
from package.coreFunc import __func1
class DataPull:
def __init__(self, arg1):
self._url = "http://www.somesite.com/info?"
self._api_param = {'argument':__func1(arg1)}
self._pull = _requests.get(self._url, params = self._api_param)
def DataTableOne(self):
_headers = self._pull.json()['resultSets'][0]['headers']
_values = self._pull.json()['resultSets'][0]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
def DataTableTwo(self):
_headers = self._pull.json()['resultSets'][1]['headers']
_values = self._pull.json()['resultSets'][1]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
within coreFunc.py
, I have a few functions that I need to use within specific classes in mod1.py
and mod2.py
. IN fact, in the third line of the Class definition, I'm using a function (__func1
) defined in coreFunc
to modify user input to the argument to ensure the correct value is passed to the api call.
coreFunc.py looks like this:
def __func1(x):
if str(x) == "1999":
return "1999-00"
elif len(str(x)) == 4:
try:
return "-".join([str(x),str(int(x) % 100 + 1)])
except:
raise Exception("Enter the four-digit year")
else: raise Exception("Enter the four-digit year")
I'm using a class for this call because the call results in more than one data table and I am using methods to access each of the data tables.
My issue is that when I try and create an object of class DataPull:
newObj = DataPull(argument)
gives me the following error:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-24-e3399cf393bd> in <module>()
----> 1 traceback.extract_stack(package.mode1.DataPull("203112"))
C:\Users\Bradley\Anaconda3\lib\site-packages\test-package-py3.4.egg\package\subpackage\mod1.py in __init__(self, arg1)
140 self._url = "http://www.somesite.com/info?"
--> 141 self._api_param = {"Season":__func1(arg1)}
NameError: name '_DataPull__func1' is not defined
How do I properly import __func1
into mod1.py to fix this error?
This is a case of name mangling, anything with
__
in front of it within class definition is changed to_classname__attr
. Quoting from docs:This is done at the source code level, so you can change the name of function that is going to be used inside the class definition to something else and it will work fine.
Related CPython code from compile.c:
Having two underscores before a function means it is private. You can rename it to
_func1
and it will work.Alternatively, import
__func1
as follows:And use
_func1
in__init__
instead of__func1
.