Archive

Archive for the ‘python’ Category

Printing the generated query including parameters

January 16, 2020 Leave a comment

https://www.johbo.com/2016/printing-the-generated-query-including-parameters.html

Categories: Interesting, python Tags: ,

Getting Started with Heroku – notes

January 6, 2020 Leave a comment

Notes:

1. Install the heroku cli.
2. If you have a preexisting git repository with the desired files to be pushed to heroku.
You need to have at minimum a Procfile and requirements.txt file

$ less Procfile
web: gunicorn wsgi:app
$ less requirements.txt
Flask==1.0.3
alembic==1.0.10
...
gunicorn==20.0.4

3. Merge changes into master branch if working off a different branch.
4. Login into heroku.
$ heroku login
5. Create a heroku app. You can specify a name for the app. Below command generate a random name.
$ heroku create
To create an app outside the default region of the us.
$ heroku create midnight-cask-76349 --region eu
6. Create a heroku postgres database.
$ heroku addons:create heroku-postgresql:hobby-dev
7. Push changes up to heroku app. Alembic changes get applied automatically on successful deploy.
$ git push heroku master
8. Set application `Config vars` under settings on the dashboard
9. Get database application details from dashboard. Follow link from database details under installed addons section. THe database url details will be available under the linked settings. Also have instructions on how to connect to database
$ heroku pg:psql postgresql-sparkle-97339 --app midnight-cask-76349
10. To manually run alembic
$ heroku run alembic upgrade head
11. To check logs
$ heroku logs --tail
12. To get database credentials
$ heroku pg:credentials:url
13. You can clone the heroku repo for the app locally
$ heroku git:clone -a midnight-cask-76349

Sources:
https://stackabuse.com/deploying-a-flask-application-to-heroku/
https://stackoverflow.com/questions/34379130/pushing-to-heroku-fails-no-such-app-fatal
https://elements.heroku.com/addons/heroku-postgresql
https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-gunicorn-and-nginx-on-ubuntu-14-04
https://blog.dbi-services.com/a-free-postgresql-cloud-database/
https://devcenter.heroku.com/articles/heroku-postgresql#designating-a-primary-database

Categories: postgresql, python Tags:

SSLError(“Can’t connect to HTTPS URL because the SSL module is not available.”)

November 30, 2019 Leave a comment

Problem:
I was not able to run $ python3.7 -m pip install --upgrade pip on my Ubuntu 14.0 (Trusty) box. I was getting an error similar to

Could not fetch URL https://pypi.org/simple/slackclient/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host=’pypi.org’, port=443): Max retries exceeded with url: /simple/slackclient/ (Caused by SSLError(“Can’t connect to HTTPS URL because the SSL module is not available.”)) – skipping

Solution:
I had to manually install openssl and python3.7 for it to work on Ubuntu Trusty.

Sources:

https://help.dreamhost.com/hc/en-us/articles/115000702772-Installing-a-custom-version-of-Python-3

https://help.dreamhost.com/hc/en-us/articles/360001435926-Installing-OpenSSL-locally-under-your-username

Categories: python Tags:

Improve your tests in Django with fakes and factories

August 12, 2019 Leave a comment

Introduction
In our Django apps, we have the M(odel)T(emplate)V(iew) structure (mostly known as MVC). When we want to test its functionality we usually have to create some model instances and work with them and the database.

A nice and easy approach for doing so is to create fake data in our tests in the form of factories and fakers.

In the following article, we are going to look at some practical examples and techniques. This will improve the readability and behavior of your tests.

https://hacksoft.blog/improve-your-tests-django-fakes-and-factories/

Source:
https://www.pythonweekly.com/

Categories: python Tags: ,

OSError: [Errno 8] Exec format error

June 5, 2019 Leave a comment

Problem:
$ python3 run.py
* Serving Flask app "petsrus.petsrus" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
* Restarting with stat
Traceback (most recent call last):
File "run.py", line 4, in
app.run(host="0.0.0.0", port=5000)
File "/home/vagrant/env/lib/python3.4/site-packages/flask/app.py", line 944, in run
run_simple(host, port, self, **options)
File "/home/vagrant/env/lib/python3.4/site-packages/werkzeug/serving.py", line 1007, in run_simple
run_with_reloader(inner, extra_files, reloader_interval, reloader_type)
File "/home/vagrant/env/lib/python3.4/site-packages/werkzeug/_reloader.py", line 332, in run_with_reloader
sys.exit(reloader.restart_with_reloader())
File "/home/vagrant/env/lib/python3.4/site-packages/werkzeug/_reloader.py", line 176, in restart_with_reloader
exit_code = subprocess.call(args, env=new_environ, close_fds=False)
File "/usr/lib/python3.4/subprocess.py", line 537, in call
with Popen(*popenargs, **kwargs) as p:
File "/usr/lib/python3.4/subprocess.py", line 859, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.4/subprocess.py", line 1457, in _execute_child
raise child_exception_type(errno_num, err_msg)
OSError: [Errno 8] Exec format error


Solution:

Added #!/usr/bin/env python to the file

#!/usr/bin/env python
from myapp import app

if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)


Source:

https://stackoverflow.com/questions/27606653/oserror-errno-8-exec-format-error

Categories: python

Vim as a Python IDE

February 1, 2019 Leave a comment

VIM as a Python IDE

This project aims to use Vim as a powerful and complete Python IDE. In order to do that, we curated a list of awesome plugins available in the community and provided an automatic installation procedure for this set.

https://rapphil.github.io/vim-python-ide/

Source:
https://www.pythonweekly.com/

Categories: Interesting, python, vim Tags:

Designing Well-Structured REST APIs with Flask-RestPlus: Part 1

December 11, 2018 Leave a comment

This is the first part of a two-part series. In this post, I will introduce Flask-RestPlus and demonstrate how to start organising APIs around its simple REST-based conventions. Next time, I will address the topic of request/response marshalling (serialisation) and validation.

https://preslav.me/2018/12/02/designing-well-structured-rest-apis-with-flask-restplus-part-1/

Categories: python Tags: