What are the following names when I first start my python shell? They do not look like functions from __builtins__:
>>> dir(__name__)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__',
'__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__',
'__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__',
'__subclasshook__', '_formatter_field_name_split', '_formatter_parser',
'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs',
'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace',
'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace',
'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split',
'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper',
'zfill']
__name__ is a string and those a string methods. It's the name of the module or '__main__' on the toplevel. Hence this idiom appears often:
if __name__ == '__main__':
# this file was called directly, not imported
main()
dir(__name__) shows the attributes of __name__, and since __name__ is a string, it shows the attributes of the str class. Most of the listed attributes are methods. You can get more information using help():
>>> help(str.index)
Help on method_descriptor:
index(...)
S.index(sub [,start [,end]]) -> int
Like S.find() but raise ValueError when the substring is not found.
Related
I am using Telethon to listen to new messages in a Telegram channel. It receives messages ok but when I include an if statement to run if the new message contains specific text, the statement executes each time a new message is received even though the text is not in the message.
I'm new to this and can't find an answer, so can anyone see what I'm doing wrong?
from telethon import TelegramClient, events, sync
#client.on(events.NewMessage(chats=chat_id))
async def newMessageListener(event):
new_message = event.message.message
chat_id = event.chat_id
print(chat_id)
print(new_message)
if "rain" in new_message:
do_stuff
with client:
client.run_until_disconnected()
This is the result of dir(new_message) (formatted for better readability):
dir(new_message)
[
'__add__',
'__class__',
'__contains__',
'__delattr__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__getitem__',
'__getnewargs__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__iter__',
'__le__',
'__len__',
'__lt__',
'__mod__',
'__mul__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__rmod__',
'__rmul__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'capitalize',
'casefold',
'center',
'count',
'encode',
'endswith',
'expandtabs',
'find',
'format',
'format_map',
'index',
'isalnum',
'isalpha',
'isascii',
'isdecimal',
'isdigit',
'isidentifier',
'islower',
'isnumeric',
'isprintable',
'isspace',
'istitle',
'isupper',
'join',
'ljust',
'lower',
'lstrip',
'maketrans',
'partition',
'replace',
'rfind',
'rindex',
'rjust',
'rpartition',
'rsplit',
'rstrip',
'split',
'splitlines',
'startswith',
'strip',
'swapcase',
'title',
'translate',
'upper',
'zfill'
]
This question already has answers here:
How do I reverse a string in Python?
(19 answers)
Closed 2 years ago.
Why did my_dict['key3'][0].upper() worked whereas my_dict['key3'][0].reverse() didnt work showing error as:
AttributeError Traceback (most recent call last)
<ipython-input-8-1cf579a91af7> in <module>
----> 1 my_dict['key3'][0].reverse()
AttributeError: 'str' object has no attribute 'reverse'
i have assigned my_dict as:
my_dict={'key1':123,'key2':[12,23,33],'key3':['item0','item1','item2']}
As the error says, there's no such method called reverse for str in Python.
You can simply do:
my_dict['key3'][0][::-1]
Simply because str has no reverse attribute as stated in the error. If you want to reverse a str object in Python, use this:
my_string [::-1]
Please read the error and search it on your web browser before coming here.
Strings do not have an attribute called .reverse(). A useful trick in python to be able to inspect what attributes/methods are available to you is the dir function:
>>> dir('my string')
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
You will notice no such reverse attribute. You can accomplish what you're after with the built-in reversed function or with slicing:
>>> ''.join(reversed('my string'))
'gnirts ym'
>>> 'my string'[::-1]
'gnirts ym'
I had removed .text and had just written peter_pan.split() but I was not sure whether the output was correct.
import requests
from nltk import FreqDist
url = "https://www.gutenberg.org/files/16/16-0.txt"
peter_pan = requests.get(url).text
peter_pan_words = peter_pan.text.split()
word_frequency = FreqDist(peter_pan_words)
freq = word_frequency.most_common(3)[2][1]
print(freq)
Where you are doing:
peter_pan = requests.get(url).text
The text that you are using is a method of the response object that is returned by requests.get. This method returns a string (containing the contents of the page). All this is fine.
However, you are then doing:
peter_pan_words = peter_pan.text.split()
This time what you are trying to do is access the text method of a str object, but a str does not have any method called text. If you do print(dir(peter_pan)), you will see the attributes (methods/properties) that are available for you to access. You should see something similar to:
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
You will see that text is not one of them (which is why you are getting the error), but you will also see that split is there -- and in fact all you need to do is to invoke split directly:
peter_pan_words = peter_pan.split()
This part of your code:
peter_pan = requests.get(url).text
When you call .text, you are converting the object into a string. So, since it's now a string, you cannot call .text on it again. Simply change:
peter_pan_words = peter_pan.text.split()
to
peter_pan_words = peter_pan.split()
I am having trouble getting a list of all variables in a file, and I have tried all methods that I can find, but there isn't a way that worked for me. dir() would return this:
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
While working on this, I also had trouble importing a file from a variable containing the file name. I have tried importlib.import_module(filename), with open(filename, 'r') as infile: needed_file = infile.read(), and __import__() was deprecated, in the end, I'm just using with open().
Back to my first problem, I have tried using getattr(), however I got the same results as with dir(). These are what I have tried:
named_objects = dir(self.filedat)
print(named_objects)
attrs = [attr for attr in dir(self.filename) if not attr.startswith('__')]
file_objects = [getattr(self.filedat, attr) for attr in attrs]
named_objects = []
for attr in file_objects:
if isfunction(attr):
named_objects.append(attr.__name__)
elif not isfunction(attr) and not isclass(attr) and not ismodule(attr) and not ismethod(attr):
named_objects.append(attr.__name__)
print(file_objects)
I have also tried many variations of those, and none of which have succeeded.
Edit:
I am trying to get a list of all manually defined variables in said file.
I'm using pyshark to parse pcap files. I want to access layer fields using variable as shown in simple example below:
For example to access ntp server ip:
p = cap[0]
print(p.bootp.option_ntp_server)
However, I want to access it like this:
option_list = {
"12": "option_hostname",
"60": "option_vendor_class_id",
"43": "option_ntp_server"
}
p = cap[0]
print(p.bootp.%s %(option_list["43"]))
Of course this kind of access is not possible. So I tried to use getattr() like this:
getOption = getattr(p.bootp, option_list["43"])
getOption()
And it gave me following error:
'LayerFieldsContainer' object is not callable
It seems pyshark packet or layer classes is not callable.
How I can access layer fields using variable? Or can you suggest me another method to access options using option type numbers? Because I want to access this fields using option type numbers (Like option 12, option 43), not using titles.
i think OBJ pyshark.packet.fields.LayerFieldsContainer have format special, you can't use it as string.
dir (LayerFieldsContaniner) have:
['__add__', '__class__', '__class__', '__contains__', '__delattr__', '__delattr__', '__dict__', '__dir__', '__doc__', '__doc__', '__eq__', '__format__', '__format__', '__ge__', '__getattr__', '__getattribute__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__getstate__', '__getstate__', '__gt__', '__hash__', '__hash__', '__init__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__module__', '__module__', '__mul__', '__ne__', '__new__', '__new__', '__reduce__', '__reduce__', '__reduce_ex__', '__reduce_ex__', '__repr__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__setattr__', '__setstate__', '__setstate__', '__sizeof__', '__sizeof__', '__slots__', '__str__', '__str__', '__subclasshook__', '__subclasshook__', '__weakref__', '_formatter_field_name_split', '_formatter_parser', 'add_field', 'all_fields', 'alternate_fields', 'base16_value', 'binary_value', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'fields', 'find', 'format', 'get_default_value', 'hex_value', 'hide', 'index', 'int_value', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'main_field', 'name', 'partition', 'pos', 'raw_value', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'show', 'showname', 'showname_key', 'showname_value', 'size', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'unmaskedvalue', 'upper', 'zfill']
you can use it.