hip_data_tools.google package¶
Submodules¶
hip_data_tools.google.adwords module¶
This Module handles the connection and operations on Google AdWords accounts using adwords API
-
class
hip_data_tools.google.adwords.AdWordsAdGroupAdUtil(conn: hip_data_tools.google.adwords.GoogleAdWordsConnectionManager)¶ Bases:
hip_data_tools.google.adwords.AdWordsDataReaderHandles the querying of Adwords AdGroupAd service :param conn: Connection manager to handle the creation of :type conn: GoogleAdWordsConnectionManager :param adwords client:
-
set_query_to_fetch_all(start_index: int = 0, page_size: int = 500)¶ Get all Ad groups Ads in the account :param page_size: :param start_index:
Returns: None
-
-
class
hip_data_tools.google.adwords.AdWordsAdGroupUtil(conn: hip_data_tools.google.adwords.GoogleAdWordsConnectionManager)¶ Bases:
hip_data_tools.google.adwords.AdWordsDataReaderHandles the querying of Adwords AdGroup service :param conn: Connection manager to handle the creation of :type conn: GoogleAdWordsConnectionManager :param adwords client:
-
set_query_to_fetch_all(start_index: int = 0, page_size: int = 500) → None¶ Get all Ad groups in the account Args: page_size:
start_index:Returns: None
-
set_query_to_fetch_by_campaign(campaign_id: str, start_index: int = 0, page_size: int = 500) → None¶ Get all AdGroups for the given Campaign :param campaign_id: the adwords campaign to query :type campaign_id: str :param page_size: :param start_index:
Returns: None
-
-
class
hip_data_tools.google.adwords.AdWordsCampaignUtil(conn: hip_data_tools.google.adwords.GoogleAdWordsConnectionManager)¶ Bases:
hip_data_tools.google.adwords.AdWordsDataReaderHandles the querying of Adwords Campaign service :param conn: Connection manager to handle the creation of :type conn: GoogleAdWordsConnectionManager :param adwords client:
-
set_query_to_fetch_all(start_index: int = 0, page_size: int = 500) → None¶ Get all campaigns associated with an account Returns: None
-
-
class
hip_data_tools.google.adwords.AdWordsCustomerUtil(conn: hip_data_tools.google.adwords.GoogleAdWordsConnectionManager)¶ Bases:
hip_data_tools.google.adwords.AdWordsDataReaderAdwords Utility to handle customer details, customer here meaning, the Adwords customer account, ie. the accounts that you see on top right corner on the AdWords console :param conn: Connection manager to handle the creation of :type conn: GoogleAdWordsConnectionManager :param adwords client:
-
get_customers() → List[dict]¶ Gets the customer details of the adwords accounts associated with the connection Returns: List[dict]
-
-
class
hip_data_tools.google.adwords.AdWordsDataReader(conn: hip_data_tools.google.adwords.GoogleAdWordsConnectionManager, service: str, version: str, page_size: int = 1000)¶ Bases:
hip_data_tools.google.adwords.AdWordsUtilGeneric Adwords Utility class generates the required services to consume adwords API :param conn: Connection manager to handle the creation of :type conn: GoogleAdWordsConnectionManager :param adwords client:
-
download_all_as_dataframe() → pandas.core.frame.DataFrame¶ Generates a data frame from the next page of the API call to adwords Returns: List[dict]
-
download_all_as_dict() → List[dict]¶ Generates list of Dict from the adwords query language query as described in adwords api Returns: List[dict]
-
download_next_page_as_dataframe() → pandas.core.frame.DataFrame¶ Generates a data frame from the next page of the API call to adwords Returns: List[dict]
-
download_next_page_as_dict() → List[dict]¶ Generates a list of dict from the next page of the API call to adwords Returns: List[dict]
-
get_parallel_payloads(page_size: int, number_of_workers: int) → List[dict]¶ gives a list of dicts that contain start index, page size, and number of iterations :param page_size: number of elements in each page / api call :type page_size: int :param number_of_workers: total number of parallel workers for which the payload needs :type number_of_workers: int :param to be distributed:
Returns: List[dict] eg: [
{‘number_of_pages’: 393, ‘page_size’: 1000, ‘start_index’: 0, ‘worker’: 0}, {‘number_of_pages’: 393, ‘page_size’: 1000, ‘start_index’: 393000, ‘worker’: 1}, {‘number_of_pages’: 393, ‘page_size’: 1000, ‘start_index’: 786000, ‘worker’: 2},]
-
set_query(query: googleads.adwords.ServiceQueryBuilder) → None¶ Sets the new query for the class to use awql, once the query is set, the download methods can iterate over paged results :param query: query built as per adwords query language :type query: ServiceQueryBuilder
Returns: None
-
-
class
hip_data_tools.google.adwords.AdWordsManagedCustomerUtil(conn: hip_data_tools.google.adwords.GoogleAdWordsConnectionManager)¶ Bases:
hip_data_tools.google.adwords.AdWordsUtilAdwords Utility to parse through and gather Account Information for all sub accounts :param conn: Connection manager to handle the creation of :type conn: GoogleAdWordsConnectionManager :param adwords client:
-
get_all_accounts(page_size: int = 1000) → List[dict]¶ Gets the customer details of the adwords accounts associated with the connection Returns: List[dict]
-
get_all_accounts_as_dataframe(page_size: int = 1000) → pandas.core.frame.DataFrame¶ Gets the customer details of the adwords accounts associated with the connection Returns: DataFrame
-
-
class
hip_data_tools.google.adwords.AdWordsOfflineConversionUtil(conn: hip_data_tools.google.adwords.GoogleAdWordsConnectionManager)¶ Bases:
hip_data_tools.google.adwords.AdWordsUtilAdwords Utility to handle offline conversions :param conn: Connection manager to handle the creation of :type conn: GoogleAdWordsConnectionManager :param adwords client:
-
upload_conversions(data: List[dict]) -> (typing.List[dict], typing.List[dict])¶ Upload a list of conversions as a batch :param data List[dict]: a List of conversion dictionaries like: :param [:
- {
- ‘googleClickId’: ‘some valid gclid’, ‘conversionName’: ‘some conversion name’, ‘conversionTime’: ‘20200309 074353 UTC’, ‘conversionValue’: 17.0, ‘conversionCurrencyCode’: ‘AUD’,
}, {
‘googleClickId’: ‘some other valid gclid’, ‘conversionName’: ‘some conversion name’, ‘conversionTime’: ‘20200309 023001 UTC’, ‘conversionValue’: 17.0, ‘conversionCurrencyCode’: ‘AUD’, ‘externalAttributionCredit’: 0.3, ‘externalAttributionModel’: ‘Linear’,},
Parameters: ] – Returns (List[dict], List[dict]): A tuple of lists holding succeeded and failed records, respectively eg. (
- [
- {
- ‘googleClickId’: ‘gclid that succeeded’, ‘conversionName’: ‘some conversions name’, ‘conversionTime’: ‘20200309 074353 UTC’, ‘conversionValue’: 17.0, ‘conversionCurrencyCode’: ‘AUD’, ‘externalAttributionCredit’: None, ‘externalAttributionModel’: None
},
- ] ,
- [
- {
‘fieldPath’: ‘operations[0].operand’, ‘fieldPathElements’: [
- {
- ‘field’: ‘operations’, ‘index’: 0
}, {
‘field’: ‘operand’, ‘index’: None}
], ‘trigger’: None, ‘errorString’: ‘OfflineConversionError.UNPARSEABLE_GCLID’, ‘ApiError.Type’: ‘OfflineConversionError’, ‘reason’: ‘UNPARSEABLE_GCLID’, ‘data’: {
‘googleClickId’: ‘Gclid that failed’, ‘conversionName’: ‘some conversion name’, ‘conversionTime’: ‘20200309 074353 UTC’, ‘conversionValue’: 17.0, ‘conversionCurrencyCode’: ‘AUD’, ‘externalAttributionCredit’: None, ‘externalAttributionModel’: None}
},
]
)
-
-
class
hip_data_tools.google.adwords.AdWordsParallelDataReadEstimator(conn: hip_data_tools.google.adwords.GoogleAdWordsConnectionManager, service: str, version: str, query: googleads.adwords.ServiceQueryBuilder)¶ Bases:
hip_data_tools.google.adwords.AdWordsUtilClass to use a service endpoint and awql query to estimate the number of parallel payloads, and their offsets :param conn: Connection manager to handle the creation of :type conn: GoogleAdWordsConnectionManager :param adwords client: :param service: Adwords service api to use for querying :type service: str :param version: Adwords service api version to use for querying :type version: str :param query: the Query builder object without limit clause :type query: ServiceQueryBuilder
-
get_parallel_payloads(page_size: int, number_of_workers: int) → List[dict]¶ gives a list of dicts that contain start index, page size, and number of iterations :param page_size: number of elements in each page / api call :type page_size: int :param number_of_workers: total number of parallel workers for which the payload needs :type number_of_workers: int :param to be distributed:
Returns: List[dict] eg: [
{‘number_of_pages’: 393, ‘page_size’: 1000, ‘start_index’: 0, ‘worker’: 0}, {‘number_of_pages’: 393, ‘page_size’: 1000, ‘start_index’: 393000, ‘worker’: 1}, {‘number_of_pages’: 393, ‘page_size’: 1000, ‘start_index’: 786000, ‘worker’: 2},]
-
-
class
hip_data_tools.google.adwords.AdWordsReportDefinitionReader(conn: hip_data_tools.google.adwords.GoogleAdWordsConnectionManager)¶ Bases:
hip_data_tools.google.adwords.AdWordsUtilClass to interact with the ReportDefinitionService and access fields for adwords reports :param conn: Connection manager to handle the creation of :type conn: GoogleAdWordsConnectionManager
adwords client
-
get_report_fields(report_type: str) → List[dict]¶ Get a list of fields for a given report. :param report_type: Possible reports are defined in the documentation here - :type report_type: str :param https: //developers.google.com/adwords/api/docs/appendix/reports/all-reports
Returns: List[dict]
-
-
class
hip_data_tools.google.adwords.AdWordsReportReader(conn: hip_data_tools.google.adwords.GoogleAdWordsConnectionManager)¶ Bases:
objectGeneric data reader class for downloading data from report awql efficiently :param conn: Connection manager to handle the creation of :type conn: GoogleAdWordsConnectionManager
adwords client-
awql_to_dataframe(query: googleads.adwords.ReportQuery, include_zero_impressions: bool = True, **kwargs) → pandas.core.frame.DataFrame¶ Download the data returned by report query in a compressed format and return it in form of a pandas dataframe :param query: an awql report query, see example :type query: ReportQuery :param https: //github.com/googleads/googleads-python-lib/blob/master/examples/adwords :param /v201809/reporting/stream_criteria_report_results.py#L34-L39: :param include_zero_impressions: include_zero_impressions :type include_zero_impressions: bool
Returns: DataFrame
-
-
class
hip_data_tools.google.adwords.AdWordsUtil(conn: hip_data_tools.google.adwords.GoogleAdWordsConnectionManager, service: str, version: str)¶ Bases:
objectGeneric Adwords Utility class generates the required services to consume adwords API :param conn: Connection manager to handle the creation of :type conn: GoogleAdWordsConnectionManager :param adwords client:
-
class
hip_data_tools.google.adwords.GoogleAdWordsConnectionManager(settings: hip_data_tools.google.adwords.GoogleAdWordsConnectionSettings)¶ Bases:
hip_data_tools.google.adwords.GoogleOAuthConnectionManagerAdWords Connection Manager that manages the lifecycle of authentication and the resulting adwords client :param settings: setting to use for connecting to adwords :type settings: GoogleAdWordsConnectionSettings
-
get_adwords_client(**kwargs) → googleads.adwords.AdWordsClient¶ Gets the connected adwords client, and creates one if not exist Returns (AdWordsClient): Adwords Client
-
-
class
hip_data_tools.google.adwords.GoogleAdWordsConnectionSettings(client_id: str, secrets_manager: hip_data_tools.google.adwords.GoogleAdWordsSecretsManager, user_agent: str, client_customer_id: Optional[str])¶ Bases:
hip_data_tools.google.adwords.GoogleOAuthConnectionSettingsHandle connection settings for AdWords client
-
class
hip_data_tools.google.adwords.GoogleAdWordsSecretsManager(source: hip_data_tools.common.KeyValueSource = <hip_data_tools.common.EnvironmentKeyValueSource object>, client_secret_var: str = 'adwords_client_secret', refresh_token_var: str = 'adwords_refresh_token', developer_token_var: str = 'adwords_developer_token')¶ Bases:
hip_data_tools.common.SecretsManagerSecrets manager for Google Adwords Specific secrets, these are modeled to handle the OAuth based google client and refresh token secrets :param source: The Key Value source which holds secrets :type source: KeyValueSource :param client_secret_var: The Key pointer for client_secret :type client_secret_var: str :param refresh_token_var: The Key pointer for refresh_token :type refresh_token_var: str :param developer_token_var: The Key pointer for developer_token :type developer_token_var: str
-
class
hip_data_tools.google.adwords.GoogleOAuthConnectionManager(settings: hip_data_tools.google.adwords.GoogleOAuthConnectionSettings)¶ Bases:
objectCreates an OAuth client from google adwords library :param settings: connection settings to use for creation :type settings: GoogleOAuthConnectionSettings
-
class
hip_data_tools.google.adwords.GoogleOAuthConnectionSettings(client_id: str, secrets_manager: hip_data_tools.google.adwords.GoogleAdWordsSecretsManager)¶ Bases:
objectHandle connection settings for AdWords client
-
hip_data_tools.google.adwords.dataclass(maybe_cls=None, these=None, repr_ns=None, repr=None, cmp=None, hash=None, init=None, slots=False, frozen=False, weakref_slot=True, str=False, *, auto_attribs=True, kw_only=False, cache_hash=False, auto_exc=False, eq=None, order=None, auto_detect=False, collect_by_mro=False, getstate_setstate=None, on_setattr=None, field_transformer=None)¶ A class decorator that adds dunder-methods according to the specified attributes using attr.ib or the these argument.
Parameters: - these (dict of str to attr.ib) –
A dictionary of name to attr.ib mappings. This is useful to avoid the definition of your attributes within the class body because you can’t (e.g. if you want to add
__repr__methods to Django models) or don’t want to.If these is not
None,attrswill not search the class body for attributes and will not remove any attributes from it.If these is an ordered dict (dict on Python 3.6+, collections.OrderedDict otherwise), the order is deduced from the order of the attributes inside these. Otherwise the order of the definition of the attributes is used.
- repr_ns (str) – When using nested classes, there’s no way in Python 2
to automatically detect that. Therefore it’s possible to set the
namespace explicitly for a more meaningful
reproutput. - auto_detect (bool) –
Instead of setting the init, repr, eq, order, and hash arguments explicitly, assume they are set to
Trueunless any of the involved methods for one of the arguments is implemented in the current class (i.e. it is not inherited from some base class).So for example by implementing
__eq__on a class yourself,attrswill deduceeq=Falseand won’t create neither__eq__nor__ne__(but Python classes come with a sensible__ne__by default, so it should be enough to only implement__eq__in most cases).Warning
If you prevent
attrsfrom creating the ordering methods for you (order=False, e.g. by implementing__le__), it becomes your responsibility to make sure its ordering is sound. The best way is to use the functools.total_ordering decorator.Passing
TrueorFalseto init, repr, eq, order, cmp, or hash overrides whatever auto_detect would determine.auto_detect requires Python 3. Setting it
Trueon Python 2 raises a PythonTooOldError. - repr (bool) – Create a
__repr__method with a human readable representation ofattrsattributes.. - str (bool) – Create a
__str__method that is identical to__repr__. This is usually not necessary except for Exceptions. - eq (Optional[bool]) –
If
TrueorNone(default), add__eq__and__ne__methods that check two instances for equality.They compare the instances as if they were tuples of their
attrsattributes if and only if the types of both classes are identical! - order (Optional[bool]) – If
True, add__lt__,__le__,__gt__, and__ge__methods that behave like eq above and allow instances to be ordered. IfNone(default) mirror value of eq. - cmp (Optional[bool]) – Setting to
Trueis equivalent to settingeq=True, order=True. Deprecated in favor of eq and order, has precedence over them for backward-compatibility though. Must not be mixed with eq or order. - hash (Optional[bool]) –
If
None(default), the__hash__method is generated according how eq and frozen are set.- If both are True,
attrswill generate a__hash__for you. - If eq is True and frozen is False,
__hash__will be set to None, marking it unhashable (which it is). - If eq is False,
__hash__will be left untouched meaning the__hash__method of the base class will be used (if base class isobject, this means it will fall back to id-based hashing.).
Although not recommended, you can decide for yourself and force
attrsto create one (e.g. if the class is immutable even though you didn’t freeze it programmatically) by passingTrueor not. Both of these cases are rather special and should be used carefully.See our documentation on hashing, Python’s documentation on object.__hash__, and the GitHub issue that led to the default behavior for more details.
- If both are True,
- init (bool) – Create a
__init__method that initializes theattrsattributes. Leading underscores are stripped for the argument name. If a__attrs_post_init__method exists on the class, it will be called after the class is fully initialized. - slots (bool) – Create a slotted class <slotted classes> that’s more memory-efficient. Slotted classes are generally superior to the default dict classes, but have some gotchas you should know about, so we encourage you to read the glossary entry <slotted classes>.
- frozen (bool) –
Make instances immutable after initialization. If someone attempts to modify a frozen instance, attr.exceptions.FrozenInstanceError is raised.
Note
- This is achieved by installing a custom
__setattr__method on your class, so you can’t implement your own. - True immutability is impossible in Python.
- This does have a minor a runtime performance impact
<how-frozen> when initializing new instances. In other words:
__init__is slightly slower withfrozen=True. - If a class is frozen, you cannot modify
selfin__attrs_post_init__or a self-written__init__. You can circumvent that limitation by usingobject.__setattr__(self, "attribute_name", value). - Subclasses of a frozen class are frozen too.
- This is achieved by installing a custom
- weakref_slot (bool) – Make instances weak-referenceable. This has no
effect unless
slotsis also enabled. - auto_attribs (bool) –
If
True, collect PEP 526-annotated attributes (Python 3.6 and later only) from the class body.In this case, you must annotate every field. If
attrsencounters a field that is set to an attr.ib but lacks a type annotation, an attr.exceptions.UnannotatedAttributeError is raised. Usefield_name: typing.Any = attr.ib(...)if you don’t want to set a type.If you assign a value to those attributes (e.g.
x: int = 42), that value becomes the default value like if it were passed usingattr.ib(default=42). Passing an instance of Factory also works as expected.Attributes annotated as typing.ClassVar, and attributes that are neither annotated nor set to an attr.ib are ignored.
- kw_only (bool) – Make all attributes keyword-only (Python 3+)
in the generated
__init__(ifinitisFalse, this parameter is ignored). - cache_hash (bool) – Ensure that the object’s hash code is computed
only once and stored on the object. If this is set to
True, hashing must be either explicitly or implicitly enabled for this class. If the hash code is cached, avoid any reassignments of fields involved in hash code computation or mutations of the objects those fields point to after object creation. If such changes occur, the behavior of the object’s hash code is undefined. - auto_exc (bool) –
If the class subclasses BaseException (which implicitly includes any subclass of any exception), the following happens to behave like a well-behaved Python exceptions class:
- the values for eq, order, and hash are ignored and the
instances compare and hash by the instance’s ids (N.B.
attrswill not remove existing implementations of__hash__or the equality methods. It just won’t add own ones.), - all attributes that are either passed into
__init__or have a default value are additionally available as a tuple in theargsattribute, - the value of str is ignored leaving
__str__to base classes.
- the values for eq, order, and hash are ignored and the
instances compare and hash by the instance’s ids (N.B.
- collect_by_mro (bool) –
Setting this to True fixes the way
attrscollects attributes from base classes. The default behavior is incorrect in certain cases of multiple inheritance. It should be on by default but is kept off for backward-compatability.See issue #428 for more details.
- getstate_setstate (Optional[bool]) –
Note
This is usually only interesting for slotted classes and you should probably just set auto_detect to True.
If True,
__getstate__and__setstate__are generated and attached to the class. This is necessary for slotted classes to be pickleable. If left None, it’s True by default for slotted classes andFalsefor dict classes.If auto_detect is True, and getstate_setstate is left None, and either
__getstate__or__setstate__is detected directly on the class (i.e. not inherited), it is set to False (this is usually what you want). - on_setattr –
A callable that is run whenever the user attempts to set an attribute (either by assignment like
i.x = 42or by using setattr likesetattr(i, "x", 42)). It receives the same arguments as validators: the instance, the attribute that is being modified, and the new value.If no exception is raised, the attribute is set to the return value of the callable.
If a list of callables is passed, they’re automatically wrapped in an attr.setters.pipe.
- field_transformer (Optional[callable]) – A function that is called with the original class object and all
fields right before
attrsfinalizes the class. You can use this, e.g., to automatically add converters or validators to fields based on their types. See transform-fields for more details.
New in version 16.0.0: slots
New in version 16.1.0: frozen
New in version 16.3.0: str
New in version 16.3.0: Support for
__attrs_post_init__.Changed in version 17.1.0: hash supports
Noneas value which is also the default now.New in version 17.3.0: auto_attribs
Changed in version 18.1.0: If these is passed, no attributes are deleted from the class body.
Changed in version 18.1.0: If these is ordered, the order is retained.
New in version 18.2.0: weakref_slot
Deprecated since version 18.2.0:
__lt__,__le__,__gt__, and__ge__now raise a DeprecationWarning if the classes compared are subclasses of each other.__eqand__ne__never tried to compared subclasses to each other.Changed in version 19.2.0:
__lt__,__le__,__gt__, and__ge__now do not consider subclasses comparable anymore.New in version 18.2.0: kw_only
New in version 18.2.0: cache_hash
New in version 19.1.0: auto_exc
Deprecated since version 19.2.0: cmp Removal on or after 2021-06-01.
New in version 19.2.0: eq and order
New in version 20.1.0: auto_detect
New in version 20.1.0: collect_by_mro
New in version 20.1.0: getstate_setstate
New in version 20.1.0: on_setattr
New in version 20.3.0: field_transformer
- these (dict of str to attr.ib) –
-
hip_data_tools.google.adwords.get_page_as_list_of_dict(page: dict) → List[collections.OrderedDict]¶ Converts a list of entries from google adwords response into a list of Ordered Dictionaries :param page: the response page from google adwords api :type page: dict
Returns: List[dict]
-
hip_data_tools.google.adwords.zeep_object_to_dict(obj: Any) → collections.OrderedDict¶ converts the zeep client objects used in google adwords to Ordered Dict, also converts nested objects to ordered dict recursively :param obj: the Zeep object to be converted :type obj: Any
Returns: OrderedDict
hip_data_tools.google.common module¶
Module contains variables and methods used for common / shared operations throughput the google services package
-
class
hip_data_tools.google.common.GoogleApiConnectionManager(settings: hip_data_tools.google.common.GoogleApiConnectionSettings, scope: List[str])¶ Bases:
abc.ABCGoogle API connection manager abstract class
-
get_client()¶ Get the connection for google sheets Returns: authenticated client object of the connection library
-
-
class
hip_data_tools.google.common.GoogleApiConnectionSettings(secrets_manager: hip_data_tools.google.common.GoogleApiSecretsManager)¶ Bases:
objectEncapsulates the Google API connection settings
-
class
hip_data_tools.google.common.GoogleApiSecretsManager(source: hip_data_tools.common.KeyValueSource = <hip_data_tools.common.EnvironmentKeyValueSource object>, key_json_var: str = 'key_json')¶ Bases:
hip_data_tools.common.SecretsManagerParameters: - source (KeyValueSource) – a kv source that has secrets
- key_json_var (str) – The variable name or the key for finding the key file json object
-
hip_data_tools.google.common.dataclass(maybe_cls=None, these=None, repr_ns=None, repr=None, cmp=None, hash=None, init=None, slots=False, frozen=False, weakref_slot=True, str=False, *, auto_attribs=True, kw_only=False, cache_hash=False, auto_exc=False, eq=None, order=None, auto_detect=False, collect_by_mro=False, getstate_setstate=None, on_setattr=None, field_transformer=None)¶ A class decorator that adds dunder-methods according to the specified attributes using attr.ib or the these argument.
Parameters: - these (dict of str to attr.ib) –
A dictionary of name to attr.ib mappings. This is useful to avoid the definition of your attributes within the class body because you can’t (e.g. if you want to add
__repr__methods to Django models) or don’t want to.If these is not
None,attrswill not search the class body for attributes and will not remove any attributes from it.If these is an ordered dict (dict on Python 3.6+, collections.OrderedDict otherwise), the order is deduced from the order of the attributes inside these. Otherwise the order of the definition of the attributes is used.
- repr_ns (str) – When using nested classes, there’s no way in Python 2
to automatically detect that. Therefore it’s possible to set the
namespace explicitly for a more meaningful
reproutput. - auto_detect (bool) –
Instead of setting the init, repr, eq, order, and hash arguments explicitly, assume they are set to
Trueunless any of the involved methods for one of the arguments is implemented in the current class (i.e. it is not inherited from some base class).So for example by implementing
__eq__on a class yourself,attrswill deduceeq=Falseand won’t create neither__eq__nor__ne__(but Python classes come with a sensible__ne__by default, so it should be enough to only implement__eq__in most cases).Warning
If you prevent
attrsfrom creating the ordering methods for you (order=False, e.g. by implementing__le__), it becomes your responsibility to make sure its ordering is sound. The best way is to use the functools.total_ordering decorator.Passing
TrueorFalseto init, repr, eq, order, cmp, or hash overrides whatever auto_detect would determine.auto_detect requires Python 3. Setting it
Trueon Python 2 raises a PythonTooOldError. - repr (bool) – Create a
__repr__method with a human readable representation ofattrsattributes.. - str (bool) – Create a
__str__method that is identical to__repr__. This is usually not necessary except for Exceptions. - eq (Optional[bool]) –
If
TrueorNone(default), add__eq__and__ne__methods that check two instances for equality.They compare the instances as if they were tuples of their
attrsattributes if and only if the types of both classes are identical! - order (Optional[bool]) – If
True, add__lt__,__le__,__gt__, and__ge__methods that behave like eq above and allow instances to be ordered. IfNone(default) mirror value of eq. - cmp (Optional[bool]) – Setting to
Trueis equivalent to settingeq=True, order=True. Deprecated in favor of eq and order, has precedence over them for backward-compatibility though. Must not be mixed with eq or order. - hash (Optional[bool]) –
If
None(default), the__hash__method is generated according how eq and frozen are set.- If both are True,
attrswill generate a__hash__for you. - If eq is True and frozen is False,
__hash__will be set to None, marking it unhashable (which it is). - If eq is False,
__hash__will be left untouched meaning the__hash__method of the base class will be used (if base class isobject, this means it will fall back to id-based hashing.).
Although not recommended, you can decide for yourself and force
attrsto create one (e.g. if the class is immutable even though you didn’t freeze it programmatically) by passingTrueor not. Both of these cases are rather special and should be used carefully.See our documentation on hashing, Python’s documentation on object.__hash__, and the GitHub issue that led to the default behavior for more details.
- If both are True,
- init (bool) – Create a
__init__method that initializes theattrsattributes. Leading underscores are stripped for the argument name. If a__attrs_post_init__method exists on the class, it will be called after the class is fully initialized. - slots (bool) – Create a slotted class <slotted classes> that’s more memory-efficient. Slotted classes are generally superior to the default dict classes, but have some gotchas you should know about, so we encourage you to read the glossary entry <slotted classes>.
- frozen (bool) –
Make instances immutable after initialization. If someone attempts to modify a frozen instance, attr.exceptions.FrozenInstanceError is raised.
Note
- This is achieved by installing a custom
__setattr__method on your class, so you can’t implement your own. - True immutability is impossible in Python.
- This does have a minor a runtime performance impact
<how-frozen> when initializing new instances. In other words:
__init__is slightly slower withfrozen=True. - If a class is frozen, you cannot modify
selfin__attrs_post_init__or a self-written__init__. You can circumvent that limitation by usingobject.__setattr__(self, "attribute_name", value). - Subclasses of a frozen class are frozen too.
- This is achieved by installing a custom
- weakref_slot (bool) – Make instances weak-referenceable. This has no
effect unless
slotsis also enabled. - auto_attribs (bool) –
If
True, collect PEP 526-annotated attributes (Python 3.6 and later only) from the class body.In this case, you must annotate every field. If
attrsencounters a field that is set to an attr.ib but lacks a type annotation, an attr.exceptions.UnannotatedAttributeError is raised. Usefield_name: typing.Any = attr.ib(...)if you don’t want to set a type.If you assign a value to those attributes (e.g.
x: int = 42), that value becomes the default value like if it were passed usingattr.ib(default=42). Passing an instance of Factory also works as expected.Attributes annotated as typing.ClassVar, and attributes that are neither annotated nor set to an attr.ib are ignored.
- kw_only (bool) – Make all attributes keyword-only (Python 3+)
in the generated
__init__(ifinitisFalse, this parameter is ignored). - cache_hash (bool) – Ensure that the object’s hash code is computed
only once and stored on the object. If this is set to
True, hashing must be either explicitly or implicitly enabled for this class. If the hash code is cached, avoid any reassignments of fields involved in hash code computation or mutations of the objects those fields point to after object creation. If such changes occur, the behavior of the object’s hash code is undefined. - auto_exc (bool) –
If the class subclasses BaseException (which implicitly includes any subclass of any exception), the following happens to behave like a well-behaved Python exceptions class:
- the values for eq, order, and hash are ignored and the
instances compare and hash by the instance’s ids (N.B.
attrswill not remove existing implementations of__hash__or the equality methods. It just won’t add own ones.), - all attributes that are either passed into
__init__or have a default value are additionally available as a tuple in theargsattribute, - the value of str is ignored leaving
__str__to base classes.
- the values for eq, order, and hash are ignored and the
instances compare and hash by the instance’s ids (N.B.
- collect_by_mro (bool) –
Setting this to True fixes the way
attrscollects attributes from base classes. The default behavior is incorrect in certain cases of multiple inheritance. It should be on by default but is kept off for backward-compatability.See issue #428 for more details.
- getstate_setstate (Optional[bool]) –
Note
This is usually only interesting for slotted classes and you should probably just set auto_detect to True.
If True,
__getstate__and__setstate__are generated and attached to the class. This is necessary for slotted classes to be pickleable. If left None, it’s True by default for slotted classes andFalsefor dict classes.If auto_detect is True, and getstate_setstate is left None, and either
__getstate__or__setstate__is detected directly on the class (i.e. not inherited), it is set to False (this is usually what you want). - on_setattr –
A callable that is run whenever the user attempts to set an attribute (either by assignment like
i.x = 42or by using setattr likesetattr(i, "x", 42)). It receives the same arguments as validators: the instance, the attribute that is being modified, and the new value.If no exception is raised, the attribute is set to the return value of the callable.
If a list of callables is passed, they’re automatically wrapped in an attr.setters.pipe.
- field_transformer (Optional[callable]) – A function that is called with the original class object and all
fields right before
attrsfinalizes the class. You can use this, e.g., to automatically add converters or validators to fields based on their types. See transform-fields for more details.
New in version 16.0.0: slots
New in version 16.1.0: frozen
New in version 16.3.0: str
New in version 16.3.0: Support for
__attrs_post_init__.Changed in version 17.1.0: hash supports
Noneas value which is also the default now.New in version 17.3.0: auto_attribs
Changed in version 18.1.0: If these is passed, no attributes are deleted from the class body.
Changed in version 18.1.0: If these is ordered, the order is retained.
New in version 18.2.0: weakref_slot
Deprecated since version 18.2.0:
__lt__,__le__,__gt__, and__ge__now raise a DeprecationWarning if the classes compared are subclasses of each other.__eqand__ne__never tried to compared subclasses to each other.Changed in version 19.2.0:
__lt__,__le__,__gt__, and__ge__now do not consider subclasses comparable anymore.New in version 18.2.0: kw_only
New in version 18.2.0: cache_hash
New in version 19.1.0: auto_exc
Deprecated since version 19.2.0: cmp Removal on or after 2021-06-01.
New in version 19.2.0: eq and order
New in version 20.1.0: auto_detect
New in version 20.1.0: collect_by_mro
New in version 20.1.0: getstate_setstate
New in version 20.1.0: on_setattr
New in version 20.3.0: field_transformer
- these (dict of str to attr.ib) –
hip_data_tools.google.sheets module¶
This module is responsible for all Google sheets operations
-
class
hip_data_tools.google.sheets.GoogleSheetConnectionManager(settings: hip_data_tools.google.common.GoogleApiConnectionSettings)¶ Bases:
hip_data_tools.google.common.GoogleApiConnectionManagerEncapsulates the Google sheets API connection settings
-
get_client()¶ Get the connection for google sheets Returns: authorised connection for google sheets
-
-
class
hip_data_tools.google.sheets.SheetUtil(conn_manager: hip_data_tools.google.sheets.GoogleSheetConnectionManager, workbook_url, sheet)¶ Bases:
objectUtility class for connecting to google sheets :param conn_manager: connection to read google sheets :type conn_manager: GoogleSheetConnectionManager :param conn_manager: row number of the filed names :type conn_manager: integer :param conn_manager: row number of the field types :type conn_manager: integer
-
get_dataframe(field_names_row_number: int, field_types_row_number: int, row_range=None, data_start_row_number=None) → pandas.core.frame.DataFrame¶ Get the field types as a list :param field_names_row_number: Row number of field names :type field_names_row_number: int :param field_types_row_number: Row number of field types :type field_types_row_number: int :param row_range: Row range eg: “3:4” :type row_range: str :param data_start_row_number: Starting row number of actual data :type data_start_row_number: int
Returns: field types list
-
get_fields_names(field_names_row_number: int) → List[str]¶ Get the field names as a list :param field_names_row_number: row number of field names :type field_names_row_number: int
Returns: field names list
-
get_fields_types(field_types_row_number: int) → List[str]¶ Get the field types as a list :param field_types_row_number: Row number of field types :type field_types_row_number: int
Returns: field types list
-
get_value_matrix(row_range=None, data_start_row_number=None) → List[List[Any]]¶ Get the values of the sheet as a 2D array :param row_range: [optional]range of the rows that need to be selected (eg: ‘1:7’) :type row_range: string :param data_start_row_number: (eg: 2) :type data_start_row_number: integer
Returns: values of the sheet as a 2D array
-