Skip to main content
Changed Listing#__str__ to Listing.__str__ because my javadoc roots were showing
Source Link
AJNeufeld
  • 34k
  • 5
  • 39
  • 101

then config_options would be {'command_prefix': '?', 'post_limit': 5), and the cls(**config_options) would expand to BotConfig(command_prefix='?', post_limit=5, posting_limit=3, presence=['hard to get']), and a "TypeError: BotConfig() got an unexpected keyword argument 'post_limit'" exception would be raised (or missing token or db_url or search keyword, because I was lazy).

Or, if you get tired of typing in self. for all the fields (I'm looking at Listing#__str__Listing.__str__), {key} will be replaced with the keyword argument given in the format(...) arguments, and the splat operator turns the self.__dict__ into key=value pairs, for use as the keyword arguments for format(...):

then config_options would be {'command_prefix': '?', 'post_limit': 5), and the cls(**config_options) would expand to BotConfig(command_prefix='?', post_limit=5, presence=['hard to get']), and a "TypeError: BotConfig() got an unexpected keyword argument 'post_limit'" exception would be raised (or missing token or db_url or search keyword, because I was lazy).

Or, if you get tired of typing in self. for all the fields (I'm looking at Listing#__str__), {key} will be replaced with the keyword argument given in the format(...) arguments, and the splat operator turns the self.__dict__ into key=value pairs, for use as the keyword arguments for format(...):

then config_options would be {'command_prefix': '?', 'post_limit': 5), and the cls(**config_options) would expand to BotConfig(command_prefix='?', post_limit=5, posting_limit=3, presence=['hard to get']), and a "TypeError: BotConfig() got an unexpected keyword argument 'post_limit'" exception would be raised (or missing token or db_url or search keyword, because I was lazy).

Or, if you get tired of typing in self. for all the fields (I'm looking at Listing.__str__), {key} will be replaced with the keyword argument given in the format(...) arguments, and the splat operator turns the self.__dict__ into key=value pairs, for use as the keyword arguments for format(...):

Bounty Ended with 100 reputation awarded by Matt
Added `__slots__` variation for `def __str__(self):`
Source Link
AJNeufeld
  • 34k
  • 5
  • 39
  • 101

For __slots__ based classes, you could use:

def __str__(self):
    return ('your format string here'
            ).format(**{key: getattr(self, key) for key in self.__slots__})
def __str__(self):
   return "\n".join("%sf"{key}:%s"{val}" %for (key, val in vars(self).items())

And for __slots__ based classes, you could use:

def __str__(self):
 for   return "\n".join(f"{key}:{getattr(self, valkey)}" for key in self.__dict__.items()__slots__)
def __str__(self):
   return "\n".join("%s:%s" % (key, val) for key, val in self.__dict__.items())

For __slots__ based classes, you could use:

def __str__(self):
    return ('your format string here'
            ).format(**{key: getattr(self, key) for key in self.__slots__})
def __str__(self):
   return "\n".join(f"{key}:{val}" for key, val in vars(self).items())

And for __slots__ based classes, you could use:

def __str__(self):
    return "\n".join(f"{key}:{getattr(self, key)}" for key in self.__slots__)
Spotted error `[ ].count == 0`. Added f'...' string alternate for `def __str__(self):`
Source Link
AJNeufeld
  • 34k
  • 5
  • 39
  • 101

Could use a format string instead.

def __str__(self):
    return (f'urlid:{self.urlid}\n'
            f'url:{self.url}\n'
            f'inserted:{self.inserted}'
            )

Or, if you get tired of typing in format()self. instead. for all the fields (I'm looking at Listing#__str__), {key} will be replaced with the keyword argument given in the format(...) arguments, and the splat operator turns the self.__dict__ into key=value pairs, for use as the keyword arguments for format(...):

def __str__(self):
   return "\n".join("%s:%s" % (key, val) for key, val in self.__dict__.items())

Error: comparison between method and integer:

if self.presence.count == 0:
    self.presence.append(defaults['presence'])

self.presence is a list [], which has a method .count, which counts the occurrence of an item within the list. It is not the length of the list. Use:

if len(self.presence) == 0:

or, since an empty list is falsy ...

if not self.presence:

Could use format() instead. {key} will be replaced with the keyword argument given in the format(...) arguments, and the splat operator turns the __dict__ into key=value pairs, for use as the keyword arguments for format(...):

def __str__(self):
   return "\n".join("%s:%s" % (key, val) for key, val in self.__dict__.items())

Could use a format string instead.

def __str__(self):
    return (f'urlid:{self.urlid}\n'
            f'url:{self.url}\n'
            f'inserted:{self.inserted}'
            )

Or, if you get tired of typing in self. for all the fields (I'm looking at Listing#__str__), {key} will be replaced with the keyword argument given in the format(...) arguments, and the splat operator turns the self.__dict__ into key=value pairs, for use as the keyword arguments for format(...):

def __str__(self):
   return "\n".join("%s:%s" % (key, val) for key, val in self.__dict__.items())

Error: comparison between method and integer:

if self.presence.count == 0:
    self.presence.append(defaults['presence'])

self.presence is a list [], which has a method .count, which counts the occurrence of an item within the list. It is not the length of the list. Use:

if len(self.presence) == 0:

or, since an empty list is falsy ...

if not self.presence:
Source Link
AJNeufeld
  • 34k
  • 5
  • 39
  • 101
Loading