user1695285
user1695285

Reputation: 259

AttributeError: 'str' object has no attribute 'isstr'

I am trying to make python module for jenni/phenny irc bot.

This is part of my code

def bp(jenni, input):
    try: 
        text = input.group(2).encode('utf-8').split()
    except: 
        jenni.reply("Please use correct syntax '.bp id weapons 7'. Available for weapons and food only")
    if text[0].isstr() and text[1].isstr() and text[2].isdigit() and len(text) == 3 and text[1] == ('weapons' or 'food'):
        url = 'http://someAPIurl/%s/%s/%s/1.xml?key=%s' % (text[0], text[1], text[2], key)

If the input is already a str why would I be getting this error?

AttributeError: 'str' object has no attribute 'isstr'

Upvotes: 0

Views: 6016

Answers (3)

istruble
istruble

Reputation: 13702

Use isinstance and either basestring for Python 2.x and str or unicode for Python 3.x:

isinstance(your_string, basestring)

That is the question that you originally asked but is probably not what you meant. Your sample code suggests that you really want to know how to check to see if a string is either alphabetic or alphanumeric. For that you want to use the isalpha or isalnum string methods.

str.isalpha()

Return true if all characters in the string are alphabetic and there is at least one character, false otherwise.

For 8-bit strings, this method is locale-dependent.

You may also want to consider refactoring your code to make it a bit easier to read and maintain. Maybe something like this:

API_URL = 'http://someAPIurl/%s/%s/%s/1.xml?key=%s'
KIND_CHOICES = ('weapon', 'food')

def bp(jenni, input):
    try:
        cmd, kind, index = input.group(2).encode('utf-8').split()
        # Assigning to 3 variables lets you skip the len() == 3 check
        # and can make the use of each argument more obvious than text[1]
    except:
        jenni.reply("Please use correct syntax '.bp id weapons 7'. Available for weapons and food only")
    if cmd.isalpha() and kind in KIND_CHOICES and index.isdigit():
        url = API_URL % (cmd, kind, index, key)  # is key a global?
    # ...

Upvotes: 3

Cat
Cat

Reputation: 67502

The error is exactly what it says; str has no method isstr().

If you want to make sure that it's only letter(s), use .isalpha().

Example:

>>> '0'.isalpha()
False
>>> 'a'.isalpha()
True
>>> 'aa'.isalpha()
True

Upvotes: 3

Rohit Jain
Rohit Jain

Reputation: 213233

Try using: - text[0].isalpha()..

There is no such method isstr() for string..

And in place of text[1] == ('weapons' or 'food'), you should use in operator..

if (text[1] in ('weapons', 'food')) {
}

Upvotes: 0

Related Questions