Splinter find_by_css excluding a class item - python

class="conversation hasLabels read"
Hello, everyone, I am trying to access an unread email using a for loop and specifying a class
browser.find_by_css(.conversation.hasLabels.hasAttachments)
The problem here is some emails have class="read" so when the for loop executes, it takes all the read ones also but that is a problem since the emails don't have an unread element.
For better understanding, I would like to access a class providing exclusive parameters.

Apparently using the script this way grabs only what you ask so this solves my problem.
browser.find_by_css('div[class="conversation hasLabels hasAttachments"]')

You can just add a :not
.conversation.hasLabels.hasAttachments:not(.read)

It can help you if I understood the essence of the problem correctly:
browser.find_by_xpath('//*[contains(#class, "conversation")][contains(#class, "hasLabels")][not(contains(#class, "read"))]')

Related

How do you split a big message in different lines in python telebot?

So hey. My previous question was not well received so I'll try to do better this time.
One of my help commands for the bot sends them a list of commands that they can do. Here's the code for the specific part of the problem:
def help_command(update, context):
update.message.reply_text("What do you need my help in?")
update.message.reply_text("/commandhelp - Know my commands")
update.message.reply_text("/helpmegetapartner - Get advice on getting a partner")
dp.add_handler(CommandHandler("jhelp", help_command))
Now, I will with time add more commands, which may not fall under the given list. But there's no way (that I know of) to send the same message but in one single one, along with line breaks. This method will bombard them with messages and make them hate me. Please help!
You can use triple quotes like that :
help_command_text = """What do you need my help in?
/commandhelp - Know my commands
/helpmegetapartner - Get advice on getting a partner
/anothercommand ...
"""
And then
update.message.reply_text(help_command_text)

how to convert string contains list to regular list in python?

I have an the following function in Apache Airflow:
from airflow.utils.email import send_email
send_email(to=['a#mail.com','b#mail.com'],
subject=....)
This works great.
Now I don't want to hard code the email list so I save it as a configurable field that the user can change from his UI.
So I change my code to:
NOTIFY_LIST = Variable.get("a_emails")
send_email([NOTIFY_LIST],
subject=....)
But this doesn't work.
When I do:
logging.info(NOTIFY_LIST)
logging.info([NOTIFY_LIST])
logging.info(NOTIFY_LIST.split(','))
I see:
'a#mail.com', 'b#mail.com'
[u"'a#mail.com', 'b#mail.com'"]
[u"'a#mail.com'", u" 'b#mail.com'"]
So my problem is that:
['a#mail.com','b#mail.com']
and
[NOTIFY_LIST]
isn't the same.
How can I fix this? I tried any conversion I could think of.
A suggestion to try the following;
logging.info(NOTIFY_LIST.replace("'", "").split(','))
The problem here is that the elements in the list contain quote marks.
The other answer will fail if there's an ' in the midle of the strings, to fix that I'll use str.strip:
logging.info([s.strip("' ") for s in NOTIFY_LIST.split(',')])

Basics of connecting python to the web and validating user input

I'm relatively new, and I'm just at a loss as to where to start. I don't expect detailed step-by-step responses (though, of course, those are more than welcome), but any nudges in the right direction would be greatly appreciated.
I want to use the Gutenberg python library to select a text based on a user's input.
Right now I have the code:
from gutenberg.acquire import load_etext
from gutenberg.cleanup import strip_headers
text = strip_headers(load_etext(11)).strip()
where the number represents the text (in this case 11 = Alice in Wonderland).
Then I have a bunch of code about what to do with the text, but I don't think that's relevant here. (If it is let me know and I can add it).
Basically, instead of just selecting a text, I want to let the user do that. I want to ask the user for their choice of author, and if Project Gutenberg (PG) has pieces by that author, have them then select from the list of book titles (if PG doesn't have anything by that author, return some response along the lines of "sorry, don't have anything by $author_name, pick someone else." And then once the user has decided on a book, have the number corresponding to that book be entered into the code.
I just have no idea where to start in this process. I know how to handle user input, but I don't know how to take that input and search for something online using it.
Ideally, I'd be able to handle things like spelling mistakes too, but that may be down the line.
I really appreciate any help anyone has the time to give. Thanks!
The gutenberg module includes facilities for searching for a text by metadata, such as author. The example from the docs is:
from gutenberg.query import get_etexts
from gutenberg.query import get_metadata
print(get_metadata('title', 2701)) # prints frozenset([u'Moby Dick; Or, The Whale'])
print(get_metadata('author', 2701)) # prints frozenset([u'Melville, Hermann'])
print(get_etexts('title', 'Moby Dick; Or, The Whale')) # prints frozenset([2701, ...])
print(get_etexts('author', 'Melville, Hermann')) # prints frozenset([2701, ...])
It sounds as if you already know how to read a value from the user into a variable, and replacing the literal author in the above would be as simple as doing something like:
author_name = my_get_input_from_user_function()
texts = get_etexts('author', author_name)
Note the following note from the same section:
Before you use one of the gutenberg.query functions you must populate the local metadata cache. This one-off process will take quite a while to complete (18 hours on my machine) but once it is done, any subsequent calls to get_etexts or get_metadata will be very fast. If you fail to populate the cache, the calls will raise an exception.
With that in mind, I haven't tried the code I've presented in this answer because I'm still waiting for my local cache to populate.

SolrClient python update document

I'm currently trying to create a small python program using SolrClient to index some files.
My need is that I want to index some file content and then add some attributes to enrich the document.
I used the post command line tool to index the files. Then I use a python program trying to enrich documents, something like this:
doc = solr.get('collection', id)
doc['new_attribute'] = 'value'
solr.index_json('collection',json.dumps([doc]))
solr.commit(openSearcher=True)
Problem is that I have the feeling that we lost file content index. If I run a query with a word present in all attributes of the doc, I find it.
If I run a query with a word only in the file, it does not work (it works indexing only the file with post without my update tentative).
I'm not sure to understand how to update the doc keeping the index created by the post command.
I hope I'm clear enough, maybe I misunderstood the way it works...
thanks a lot
If I understand correctly, you want to modify an existing record. You should be able to do something like this without using a solr.get:
doc = [{'id': 'value', 'new_attribute':{'set': 'value'}}]
solr.index_json('collection',json.dumps([doc]))
See also:
https://cwiki.apache.org/confluence/display/solr/Updating+Parts+of+Documents
It has worked for me in this way, it can be useful for someone
from SolrClient import SolrClient
solrConect = SolrClient("http://xx.xx.xxx.xxx:8983/solr/")
doc = [{'id': 'my_id', 'count_related_like':{'set': 10}}]
solrConect.index_json("my_collection", json.dumps(doc) )
solrConect.commit("my_collection", softCommit=True)
Trying with Curl did not change anything. I did it differently so now it works. Instead of adding the file with the post command and trying to modify it afterwards, I read the file in a string and index in a "content" field. It means every document is added in one shot.
The content field is defined as not stored, so I just index it.
It works fine and suits my needs. It's also more simple since it removes many attributes set by post command that I don't need.
If I find some time, I'll try again the partial update and update the post.
Thanks
RĂ©mi

Forwarded Email parsing in Python/Any other language?

I have some mails in txt format, that have been forwarded multiple times.
I want to extract the content/the main body of the mail. This should be at the last position in the hierarchy..right? (Someone point this out if I'm wrong).
The email module doesn't give me a way to extract the content. if I make a message object, the object doesn't have a field for the content of the body.
Any idea on how to do it? Any module that exists for the same or any any particular way you can think of except the most naive one of-course of starting from the back of the text file and looking till you find the header.
If there is an easy or straightforward way/module with any other language ( I doubt), please let me know that as well!
Any help is much appreciated!
The email module doesn't give me a way to extract the content. if I make a message object, the object doesn't have a field for the content of the body.
Of course it does. Have a look at the Python documentation and examples. In particular, look at the walk and payload methods.
Try get_payload on the parsed Message object. If there is only one message, the return type will be string, otherwise it will be a list of Message objects.
Something like this:
messages = parsed_message.get_payload()
while type(messages) <> Types.StringType:
messages = messages[-1].get_payload()

Categories