Tuesday 22 February 2022

How to get all dates (month, day and year) between two dates in python?

 You don't have to reinvent the wheel. Just parse the strings into datetime objects and let python do the math for you:

from dateutil import rrule
from datetime import datetime

a = '20120525'
b = '20120627'

for dt in rrule.rrule(rrule.DAILY,
                      dtstart=datetime.strptime(a, '%Y%m%d'),
                      until=datetime.strptime(b, '%Y%m%d')):
    print dt.strftime('%Y%m%d')

prints

20120525
20120526
2012052720120625
20120626
20120627


from: https://stackoverflow.com/questions/11317378/how-to-get-all-dates-month-day-and-year-between-two-dates-in-python

Saturday 19 February 2022

Locust - Running tests in a debugger in pycharm

Remember to checked the [Gevent compatible] in the debugger setting.


Running Locust in a debugger is extremely useful when developing your tests. Among other things, you can examine a particular response or check some User instance variable.

But debuggers sometimes have issues with complex gevent-applications like Locust, and there is a lot going on in the framework itself that you probably arent interested in. To simplify this, Locust provides a method called run_single_user:

Note that this is fairly new feature, and the api is subject to change.

from locust import HttpUser, task, run_single_user


class QuickstartUser(HttpUser):
    host = "http://localhost"

    @task
    def hello_world(self):
        with self.client.get("/hello", catch_response=True) as resp:
            pass  # maybe set a breakpoint here to analyze the resp object?


# if launched directly, e.g. "python3 debugging.py", not "locust -f debugging.py"
if __name__ == "__main__":
    run_single_user(QuickstartUser)

It implicitly registeres an event handler for the request event to print some stats about every request made:

type    name                                           resp_ms exception
GET     /hello                                         38      ConnectionRefusedError(61, 'Connection refused')
GET     /hello                                         4       ConnectionRefusedError(61, 'Connection refused')

You can configure exactly what is printed by specifying parameters to run_single_user.

Make sure you have enabled gevent in your debugger settings. In VS Code’s launch.json it looks like this:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "gevent": true
        }
    ]
}

There is a similar setting in PyChar


from: https://docs.locust.io/en/latest/running-in-debugger.html

SSL error CERTIFICATE_VERIFY_FAILED with Locust when using Docker

You can do turn the verification off by adding below method:

def on_start(self):
    """ on_start is called when a Locust start before any task is scheduled """ 

    self.client.verify = False 


from: https://stackoverflow.com/questions/62452566/ssl-error-certificate-verify-failed-with-locust-when-using-docker

How can I use pickle to save a dict?

 Try this:

import pickle

a = {'hello': 'world'}

with open('filename.pickle', 'wb') as handle:
    pickle.dump(a, handle, protocol=pickle.HIGHEST_PROTOCOL)

with open('filename.pickle', 'rb') as handle:
    b = pickle.load(handle)

print a == b


from : https://stackoverflow.com/questions/11218477/how-can-i-use-pickle-to-save-a-dict

How to delete the contents of a folder?

from pathlib import Path
from shutil import rmtree

for path in Path("/path/to/folder").glob("**/*"):

    if path.is_file():

        path.unlink()

    elif path.is_dir():

        rmtree(path)


from: https://stackoverflow.com/questions/185936/how-to-delete-the-contents-of-a-folder

Check If a File Exists

 import os

os.path.exists('./final_data_2020.csv')


from: https://careerkarma.com/blog/python-check-if-file-exists/

return text between parenthesis

st[st.find("(")+1:st.rfind(")")]

from: https://stackoverflow.com/questions/4894069/regular-expression-to-return-text-between-parenthesis