Skip to content

Commit

Permalink
2.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
orlnub123 committed Mar 29, 2018
1 parent 9d741d6 commit 1f90890
Show file tree
Hide file tree
Showing 8 changed files with 336 additions and 73 deletions.
16 changes: 8 additions & 8 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ asynchronously import it as below instead:
from cleverbot import async_ as cleverbot
Then initialize Cleverbot with your API key and optionally a cleverbot state
and or timeout:
Then initialize Cleverbot with your API key and optionally a cleverbot state,
timeout and or tweak if you want to adjust Cleverbot's mood:

.. code:: py
cb = cleverbot.Cleverbot('YOUR_API_KEY', cs='76nxdxIJ02AAA', timeout=60)
cb = cleverbot.Cleverbot('YOUR_API_KEY', cs='76nxdxIJ02AAA', timeout=60, tweak1=0, tweak2=100, tweak3=100)
The cleverbot state is the encoded state of the conversation that you get from
talking to Cleverbot and includes the whole conversation history.
Expand All @@ -94,9 +94,9 @@ Talk straight to Cleverbot:
You can pass in keyword arguments to ``say`` such as ``cs`` to change the
conversation, ``vtext`` to change the current conversation's history, or even
``cb_settings_tweak`` to change Cleverbot's mood. Read the "Parameters" section
of `the official Cleverbot API docs <https://www.cleverbot.com/api/howto/>`_
for more information.
``tweak`` as an alias for ``cb_settings_tweak`` to change Cleverbot's mood.
Read the "Parameters" section of `the official Cleverbot API docs
<https://www.cleverbot.com/api/howto/>`_ for more information.

Alternatively, start a new conversation and talk from it:

Expand Down Expand Up @@ -191,8 +191,8 @@ you can use ``cb.save``:
with open('cleverbot.txt', 'wb') as file:
cb.save(file)
This saves the key and timeout you've given to Cleverbot and its conversations
and also the current cleverbot state of each.
This saves the key, timeout and tweaks you've given to Cleverbot and its
conversations and also the current cleverbot state of each.

In order to load and recreate the previously saved state as a new Cleverbot
instance use ``cleverbot.load``:
Expand Down
2 changes: 1 addition & 1 deletion cleverbot/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '2.1.1'
__version__ = '2.2.0'

from .cleverbot import Cleverbot, load
from .errors import CleverbotError, APIError, DecodeError, Timeout
13 changes: 12 additions & 1 deletion cleverbot/async_/cleverbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def say(self, input=None, **kwargs):
Arguments:
input: The input argument is what you want to say to Cleverbot,
such as "hello".
tweak1-3: Changes Cleverbot's mood.
**kwargs: Keyword arguments to update the request parameters with.
Returns:
Expand All @@ -38,8 +39,18 @@ def say(self, input=None, **kwargs):
params['cs'] = self.data['cs']
except KeyError:
pass
# Python 3.4 compatibility
for tweak in ('tweak1', 'tweak2', 'tweak3'):
if getattr(self, tweak, None) is not None:
params['cb_settings_' + tweak] = getattr(self, tweak)
if kwargs:
for tweak in ('tweak1', 'tweak2', 'tweak3'):
setting = 'cb_settings_' + tweak
if tweak in kwargs and setting not in kwargs:
kwargs[setting] = kwargs.pop(tweak)
elif tweak in kwargs and setting in kwargs:
message = "Supplied both {!r} and {!r}"
raise TypeError(message.format(tweak, setting))
# Python 3.4 compatibility
params.update(kwargs)

headers = {
Expand Down
63 changes: 46 additions & 17 deletions cleverbot/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def __getattr__(self, attr):
try:
return self.data[attr]
except KeyError:
message = "'{0}' object has no attribute '{1}'"
message = "{0!r} object has no attribute {1!r}"
raise AttributeError(message.format(self.__class__.__name__, attr))

@property
Expand All @@ -35,9 +35,11 @@ def __init__(self, key, **kwargs): # Python 2 compatible keyword-only args
if 'cs' in kwargs:
self.data['cs'] = kwargs.pop('cs')
self.timeout = kwargs.pop('timeout', None)
for tweak in ('tweak1', 'tweak2', 'tweak3'):
setattr(self, tweak, kwargs.pop(tweak, None))
self.conversations = None
if kwargs:
message = "__init__() got an unexpected keyword argument '{0}'"
message = "__init__() got an unexpected keyword argument {0!r}"
raise TypeError(message.format(next(iter(kwargs))))

def conversation(self, name, convo):
Expand Down Expand Up @@ -70,19 +72,22 @@ def save(self, file):
Arguments:
file: A file object that accepts bytes to save the data to.
"""
obj = ({'key': self.key, 'cs': self.cs, 'timeout': self.timeout}, [])
for convo in self.conversations:
if isinstance(self.conversations, dict):
name, convo = convo, self.conversations[convo]
convo_dict = {'name': name, 'cs': convo.data.get('cs')}
else:
convo_dict = {'cs': convo.data.get('cs')}
for item in ('key', 'timeout'):
try:
convo_dict[item] = convo.__dict__[item]
except KeyError:
pass
obj[1].append(convo_dict)
obj = ({'key': self.key, 'cs': self.cs, 'timeout': self.timeout,
'tweak1': self.tweak1, 'tweak2': self.tweak2,
'tweak3': self.tweak3}, [])
if self.conversations is not None:
for convo in self.conversations:
if isinstance(self.conversations, dict):
name, convo = convo, self.conversations[convo]
convo_dict = {'name': name, 'cs': convo.data.get('cs')}
else:
convo_dict = {'cs': convo.data.get('cs')}
for item in ('key', 'timeout', 'tweak1', 'tweak2', 'tweak3'):
try:
convo_dict[item] = convo.__dict__[item]
except KeyError:
pass
obj[1].append(convo_dict)
pickle.dump(obj, file, pickle.HIGHEST_PROTOCOL)

def load(self, file):
Expand All @@ -104,12 +109,12 @@ class ConversationBase(AttributeMixin):
def __init__(self, cleverbot, **kwargs):
self.cleverbot = cleverbot
self.data = {}
for item in ('key', 'cs', 'timeout'):
for item in ('key', 'cs', 'timeout', 'tweak1', 'tweak2', 'tweak3'):
if item in kwargs:
setattr(self, item, kwargs.pop(item))
self.session = cleverbot.session
if kwargs:
message = "__init__() got an unexpected keyword argument '{0}'"
message = "__init__() got an unexpected keyword argument {0!r}"
raise TypeError(message.format(next(iter(kwargs))))

@property
Expand All @@ -128,5 +133,29 @@ def timeout(self):
def timeout(self, value):
self.__dict__['timeout'] = value

@property
def tweak1(self):
return self.__dict__.get('tweak1', self.cleverbot.tweak1)

@tweak1.setter
def tweak1(self, value):
self.__dict__['tweak1'] = value

@property
def tweak2(self):
return self.__dict__.get('tweak2', self.cleverbot.tweak2)

@tweak2.setter
def tweak2(self, value):
self.__dict__['tweak2'] = value

@property
def tweak3(self):
return self.__dict__.get('tweak3', self.cleverbot.tweak3)

@tweak3.setter
def tweak3(self, value):
self.__dict__['tweak3'] = value

def reset(self):
self.data = {}
11 changes: 11 additions & 0 deletions cleverbot/cleverbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def say(self, input=None, **kwargs):
Arguments:
input: The input argument is what you want to say to Cleverbot,
such as "hello".
tweak1-3: Changes Cleverbot's mood.
**kwargs: Keyword arguments to update the request parameters with.
Returns:
Expand All @@ -29,9 +30,19 @@ def say(self, input=None, **kwargs):
'key': self.key,
'input': input,
'cs': self.data.get('cs'),
'cb_settings_tweak1': self.tweak1,
'cb_settings_tweak2': self.tweak2,
'cb_settings_tweak3': self.tweak3,
'wrapper': 'cleverbot.py'
}
if kwargs:
for tweak in ('tweak1', 'tweak2', 'tweak3'):
setting = 'cb_settings_' + tweak
if tweak in kwargs and setting not in kwargs:
kwargs[setting] = kwargs.pop(tweak)
elif tweak in kwargs and setting in kwargs:
message = "Supplied both {0!r} and {1!r}"
raise TypeError(message.format(tweak, setting))
params.update(kwargs)

headers = {
Expand Down
5 changes: 3 additions & 2 deletions cleverbot/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ class APIError(CleverbotError):
documentation for an updated list of all the possible errors.
"""

def __init__(self, error, status):
super(APIError, self).__init__(error)
def __init__(self, error=None, status=None):
message = "An unspecified error occurred"
super(APIError, self).__init__(error if error is not None else message)
self.error = error
self.status = status

Expand Down
Loading

0 comments on commit 1f90890

Please sign in to comment.