3

I have the following namedtuple:

from collections import namedtuple

Product = namedtuple(
        'Product',
        'product_symbol entity unique_id as_of_date company_name followers linkedin_employees linkedin industry date_added date_updated description website sector product_industry'
    )

What is the best way to declare it, so it does not exceeds the Python line limit of 80 characters?

martineau
  • 119,623
  • 25
  • 170
  • 301
gogasca
  • 9,283
  • 6
  • 80
  • 125
  • There are implicit line-continuations inside of parentheses. Also, note that Python does implicit string-literal concatenation, so `'Hello ' 'world'` will implicitely become `'Hello world'`... – juanpa.arrivillaga May 23 '18 at 23:29
  • Are all those space-separated items in the string `'product_symbol entity unique_id as_of_date ...'` supposed to become separate elements/members of the `namedtuple`? – martineau May 24 '18 at 00:46
  • @martineau: `namedtuple` is documented to accept a space and/or comma separated string of the property names, or a sequence of strings where each string is a property name. Either one works; I tend to use the single string for brevity when it's half a dozen short names or fewer, but for a longer set of properties, moving to a `list`/`tuple` of `str` would probably improve readability. – ShadowRanger May 24 '18 at 00:49
  • @ShadowRanger: I know that, I just want to understand what the OP wants done. – martineau May 24 '18 at 00:50

4 Answers4

5

I propose a PEP-8 compliant version which declares your attributes as a list.

name = 'Product'
attrs = [
   'product_symbol',
   'entity',
   'unique_id',
   ... 
]

Product = namedtuple(name, attrs)

Add a trailing comma to attrs, this makes it easy when doing diff comparisons.

cs95
  • 379,657
  • 97
  • 704
  • 746
4

If your version of Python is modern enough (Python 3.6+), you might consider a declarative version:

from datetime import datetime
from typing import NamedTuple

class Product(NamedTuple):
    product_symbol: str
    entity: str
    unique_id: int
    as_of_date: datetime
    company_name: str
    followers: list
    linkedin_employees: list
    linkedin: str
    industry: str
    date_added: datetime
    date_updated: datetime
    description: str
    website: str
    sector: str
    product_industry: str

The generated type is equivalent to a collections.namedtuple, but with __annotations__ and _field_types attributes added. The implementation detail is different:

  • namedtuple is a factory function, it builds up a string defining the type and then uses exec, returning the generated type.
  • NamedTuple is a class, it uses metaclasses and a custom __new__ to handle the annotations, and then delegates to collections anyway to build the type (with the same code generation + exec as above).
wim
  • 338,267
  • 99
  • 616
  • 750
-1

I would suggest using Python's implicit joining of adjacent string literals to make it more readable and be PEP 8 compliant. Note I also added (optional) commas between items in the string which I think makes it even more readable.

Product = namedtuple('Product',
                     'product_symbol, entity, unique_id, as_of_date,'
                     'company_name, followers, linkedin_employees,'
                     'linkedin, industry, date_added, date_updated,'
                     'description, website, sector, product_industry')
martineau
  • 119,623
  • 25
  • 170
  • 301
-2

If you are only talking about breaking up the string so it does not exceed the 80 character limit you can use the \ character in a string so that it will be interpreted as single string without newlines(\n)

from collections import namedtuple

Product = namedtuple(
        'Product',
        'product_symbol entity unique_id as_of_date company_name followers\
         linkedin_employees linkedin industry date_added date_updated\ 
         description website sector product_industry'
    )
Ólafur Aron
  • 352
  • 2
  • 12