#python
15 posts tagged python. Clear filter
-
Rails Style Params in Python and Flask
Ruby on Rails does a great job of parsing arrays in form data and populating a params hash, for quick and easy access in your controllers. For instance <input name="item[]" value="item 1"/ <input...
-
How to Use Python Shutil Make_Archive to Zip Up a Directory Recursively including The Root Folder
The documentation for Python's shutil.makearchive is very confusing. Considering that the arguments to this function only need to be source and destination, makes it even more frustrating to try...
-
How to Just Get SQL Statement Error with SQLAlchemy Python Database Wrapper
If you're working with SQLAlchemy, the best database driver for Python, and want to see only SQL syntax errors, you need to use the StatementError exception class. On it, is an attribute orig, that...
-
Manual ManyToMany Through with Django's ORM
Here is a code snippet that demonstrates how to set up a ManyToMany through relationship in Django. In Rails, the equivalent would be called a hasmany through association. If you set the through...
-
How to Fix Raw query must include the primary key with Django ORM
When running raw SQL queries in Django you must include a primary key otherwise an invalid query exception is raised. Normally this is fine but when running more complex queries a primary key may...
-
How to send email with Python, smtplib and Postmark
Here is a quick code snippet showing how to send email via SMTP with Postmark without any dependencies. It assumes you are using Heroku and have added the addon. But if not just make sure your api...
-
Using Selenium to Drive Firefox Browser for Web Automation
There are a lot of practical uses for automating user behavior in a browser. Everything from testing your web application to logging into Twitter and auto following people. But first you have to...
-
Reshape an Array of Form Inputs for Flask with getlist()
This is how to reshape an array using Python without Numpy. This is a fairly simple task but it can be a little confusing to wrap your head around. I ran into this problem while working with Flask,...
-
Trigrams, Bigrams and Ngrams in Python for Text Analysis
Creating trigrams in Python is very simple trigrams = lambda a: zip(a, a[1:], a[2:]) trigrams(('a', 'b', 'c', 'd', 'e', 'f')) = [('a', 'b', 'c'), ('b', 'c', 'd'), ('c', 'd', 'e'), ('d', 'e', 'f')]...
-
How to Decorate Imported Libs in Python for Jinja Template Filters in Flask
To decorate an imported function in Python you would do something like this in ./lib.py def functionname(): function body And then in your program you could decorate it like this from lib import...
-
Get Method Name as String in Python
Here is how to get the string representation of a method in Python def mymethodname(): print "Hello World" mymethodname.name = 'mymethodname' Short and sweet!
-
Destructuring Dictionaries in Python
Here is a quick and dirty way to destructure dictionaries in [Python] d = {'a':'Apple', 'b':'Banana','c':'Carrot'} a,b,c = [d[k] for k in ('a', 'b','c')] a == 'Apple' b == 'Banana' c == 'Carrot'
-
How to Fix Pypi Upload failed (403): Invalid or non-existent authentication information.
If you run into authentication failure when trying to upload packages Submitting dist/<package name to https://upload.pypi.org/legacy/ Upload failed (403): Invalid or non-existent authentication...
-
How to Create a Slug in Python with the Re Module
There are a few 3rd party modules that do this sort of thing. But there is a pretty solution using out of the box Python functionality. You don't have to install any dependencies if you use the re...
-
My First Python Package on PyPi - Command Line Blog
I wrote my first Python package over the weekend. It is a simple package that adds a basic blog API to an existing Flask application. It's called commandlineblog and is available on Github and on...