Skip to content
Advertisement

Tag: python

Assert exception message?

I’m using pytest in a project with a good many custom exceptions. pytest provides a handy syntax for checking that an exception has been raised, however, I’m not aware of one that asserts that the correct exception message has been raised. Say I had a CustomException that prints “boo!”, how could I assert that “boo!” has indeed been printed and

How to break text into paragraphs (python)

So I’m making a text-based game but when I run my code the text isn’t broken up into paragraphs, there is no space between the first print() command and the one right after it. I’ve tried breaking up the text by putting huge spaces between the text in one print command but that is inefficient and makes my code look

Tuning the hyperparameter with gridsearch results in overfitting

Tuning the hyperparameter with gridsearch results in overfitting. The train error is definitely low, but the test error is high. Can’t you adjust the hyperparameter to lower the test error? before tuning train_error: 0.386055, test_error: 0.674069 -after tuning train_error: 0.070645, test_error: 0.708254 Answer It all depends on the data you are training. If the data you are using for training

Flask app on Docker: Max retries exceeded with URL

I’m working on a simple Flask app for which I’m using the default template: I’m just calling a dummy function that return and ‘ok’ response: When I call it directly on my machine with the following code: It works without any issue, but if I run it from the docker image I get the following error: ConnectionError: HTTPConnectionPool(host=’localhost’, port=5000): Max

Regular expression for removing all URLs in a string in Python

I want to delete all the URLs in the sentence. Here is my code: But a URL with “http” is still left in the sentence. How can I fix it? Answer One simple fix would be to just replace the pattern https?://S+ with an empty string: This prints: My pattern assumes that whatever non whitespace characters which follow http:// or

Which method is the best for starting the bot in discord.py? login() and connect() vs start() vs run()

So, to connect the bot to discord.py, there are several methods: discord.Client().login() + discord.Client().connect() discord.Client().start() discord.Client().run() What is the difference between them and which one is the best for running the bot? Answer I believe the official documentation is very clear: start() is shorthand coroutine for login() + connect(). run() is a blocking call which means that This function must

Pandas read_pickle from s3 bucket

I am working on a Jupyter notebook from AWS EMR. I am able to do this: pd.read_csv(“s3:\mypath\xyz.csv’). However, if I try to open a pickle file like this, pd.read_pickle(“s3:\mypath\xyz.pkl”) I am getting this error: However, I can see both xyz.csv and xyz.pkl in the same path! Can anyone help? Answer Pandas read_pickle supports only local paths, unlike read_csv. So you

Python – Unicode De/Encode

How can I pass all the content from making a db-input(s1), loading it from there (s2) and pass it correctly back-formated to the file? Log: EDIT: I am working on windows. Answer The problem is that you open the file in text mode, but don’t specify the encoding. In that case the system default encoding is used, which may be

Python: concatenate pandas multiindex

I need to generate a pd.DataFrame with columns being composed by a list and a Multiindex object, and I need to do it before filling the final dataframe with data. Say the columns are [‘one’, ‘two’] and the multiindex obtained from from_product: I would like to get a list of columns which looks like this: One possible solution would be

Advertisement