background preloader

Python

Facebook Twitter

Supervisor

Python - How do I resize an image using PIL and maintain its aspect ratio? Django PIL error with reading file using S3. Eugeny/ajenti. From argparse to dictionary in python 2.7. I’ve used python’s argparse module in the past to get command line options for scripts.

from argparse to dictionary in python 2.7

It provides built-in help, which is nice, and support for both positional and optional arguments. The inelegant way I’ve used it in the past, though, involves a lot of if statements to test for the various arguments and call to call the right function. I don’t like that implementation, and reading through the docs and tutorials never really helped. Today, I was playing around with argparse again, trying to cleanup and simplify some code and stumbled across what seems to me to be a better way. The reason I used all those if statements in the past is because the parser returns a Namespace which isn’t iterable: import argparse p = argparse.ArgumentParser(description="parse some things. ") p.add_argument("cmd", help=argparse.SUPPRESS, nargs="*")p.add_argument("-d","--date")p.add_argument("-p","--project")p.add_argument("-c","--context")p.add_argument("-l","--list") opts = p.parse_args() print opts.

SQLAlchemy

Running django-pipeline with less and coffeescript on Heroku. Running django-pipeline with less and coffeescript on Heroku While building a new site for our client in San Francisco, we’ve run into a problem on Heroku with using less and coffeescript.

Running django-pipeline with less and coffeescript on Heroku

Since we really appriciete those technologies here’s how we handled their integration issues. Advance Python Scheduler and SQLAlchemyJobStore. Testing. Python - How to inspect and cancel Celery tasks by task name. Python - Start a Function at Given Time. Regex - Python re infinite execution.

Celery, RabbitMQ

Performance - Python string formatting: % vs. .format. Crypto. Python - What is the right way to have custom instance attributes in Django models. PHP sprintf escaping % Postgresql - How to create a backup of a single table in a postgres database. SQLite - Consistency check in big databases results out of memory error. On Tue, Apr 3, 2012 at 1:05 PM, Kari Hoijarvi <[hidden email]>wrote: > Hello, > > running PRAGMA integrity_check from command line works for a 300 GB > database, but I seem to have hit some kind of limit with 832 GB sqlite DB.

SQLite - Consistency check in big databases results out of memory error.

> > I running on Windows server 2008 R2 standard (64bit of course). Is this a > Windows or sqlite problem? > > Any good ideas for workarounds? > As part of PRAGMA integrity_check, SQLite allocates counters for every page in the database file, and counts the number of references to each page. Unfortunately, when you get really big database files (in your case on the order of 800 million pages worth) and you allocate a 32-bit integer for each page, that can blow out available memory. We are working on a patch now in which we only allocate a single bit per page, which is all we really need to determine that each page is referenced zero or one times. Watch the timeline < for availability of this patch. -- D. VirtualBox: give Host-Only access to internet. Python - No module named pkg_resources. Ipython - What is causing ImportError: No module named pkg_resources after upgrade of Python on os X. Python - deleting entries in a dictionary based on a condition.

Handling rss redirects with Python/urllib2. Python: How to find list intersection. Python dictionary search values for keys using regular expression. Looping through python regex matches. In Python, why can a function modify some arguments as perceived by the caller, but not others. Python: How do I pass a variable by reference. Sql - Issues updating a MySQL table using Python's MySQLdb. Python - The problem with installing PIL using virtualenv or buildout. One of Microsoft's Best-Kept Secrets - Python Tools for Visual Studio (PTVS) Python and Real-time Web. Introduction I had interview for the Flask book recently and some questions were related to realtime functionality - how it works, how to integrate realtime portion with conventional WSGI applications, how to structure application code and so on.

Python and Real-time Web

We used Google Hangouts and it was supposed to record interview, but it failed. So, I decided to write elaborate blog post instead, in which I will try to cover basics, give short introduction into asynchronous programming in Python, etc. Little Bit of Theory Lets try to solve server "push" problem. Solution is simple: browser makes AJAX request to the server and asks for updates. How to use sh in Python. Python for Bash scripters: A well-kept secret.

Python scripts vs Bash scripts in Linux. Almost all Linux users who are at least familiar with bash shell commands will be at home in writing simple bash scripts to automate tasks.

Python scripts vs Bash scripts in Linux

For those who do not know how to write a bash script, here is how you do it. Open a new file in your favorite text editor and save it in any name of your choice but with the extension .sh. For example, let us name it myfirstscript.sh . Vincent Driessen : Efficient sifting iterator for Python. Did you ever want to sift an iterator into two "sub" iterators?

Vincent Driessen : Efficient sifting iterator for Python

Now you can: >>> even, odd = sift(xrange(10), lambda x: x % 2 == 0) >>> list(even) [0, 2, 4, 6, 8] >>> list(odd) [1, 3, 5, 7, 9] Note that the input iterable is only lazily consumed: >>> X = iter(xrange(10)) >>> even, odd = sift(X, lambda x: x % 2 == 0) >>> X.next() 0 >>> X.next() 1 >>> X.next() 2 >>> list(even) [4, 6, 8] >>> list(odd) [3, 5, 7, 9] Beware that in order to consume one of the returned iterators fully, you might be consuming a big part of the the input iterable and keeping its values buffered in memory. >>> small_ones, large_ones = sift(xrange(10000), lambda x: x < 9000) # low memory peak (no buffering needed) >>> list(small_ones) [0, ..., 8999] >>> list(large_ones) [9000, ..., 9999] >>> list(large_ones) [9000, ..., 9999] >>> list(small_ones) [0, ..., 8999] The implementation:

Creative. If you are a web developer, you've probably heard of nginx (pronounced engine-x).

Creative

Nginx is a fast and extremely powerful http and reverse proxy server that can be used to quickly and easily serve webpages. Unfortunately, like many sysops tools, there is very little documentation and very few tutorials that explain how it works and how to get up and running. There is a wiki, which is extensive and confusing - showing you all possible options rather than presenting the important ones as you need them.

After struggling with it myself for a bit, I finally got down the basics of how to work with nginx, and wanted to share it so that other developers would have an easier time picking it up. Retrieving python module path. How to Write a Spelling Corrector. Matt Alcock - A Data Scientist with a passion for Python. Written on Wednesday, December 5, 2012 Have you ever been really impressed with Googles 'Did you mean....' spell checker?

Matt Alcock - A Data Scientist with a passion for Python

Have you ever just typed something into google to help you with your spelling? My answer to the above questions above would be Yes, all the time! In a fantastic post I read some years ago Peter Norvig outlined how Google’s ‘did you mean’ spelling corrector uses probability theory, large training sets and some elegant statistical language processing to be so effective. What is the best way to remove accents in a python unicode string. Nltk - Spell Checker for Python. Regex - Partitioning a string in Python by a regular expression.

Send an xmpp message using a python library.

Language

DjangoCMS. Tornado. Django. Which Tools and Libraries do you use to develop Semantic Web applications ? From the command line I use redland's rapper utility.

Which Tools and Libraries do you use to develop Semantic Web applications ?

I find it really handy for exploring a linked data space, and outputting stuff as Turtle. rapper -o turtle When I'm wanting to look at some bigger pools of rdf I've used python's rdflib, with the Sleepycat backend, which can handle millions of triples easily, and is very simple to use. SPARQL support isn't really all there...but it has nice methods for digging into the triples from python, and that has been enough for me. SuRF – Object RDF mapper — SuRF v1.1.4 documentation.

CherryPy

XML. Python Encoding. Add-apt-repository command in ubuntu 12.10 - Quantal. Argparse – Command line option and argument parsing. The argparse module was added to Python 2.7 as a replacement for optparse.

argparse – Command line option and argument parsing.

The implementation of argparse supports features that would not have been easy to add to optparse, and that would have required backwards-incompatible API changes, so a new module was brought into the library instead. optparse is still supported, but is not likely to receive new features. Comparing with optparse The API for argparse is similar to the one provided by optparse, and in many cases argparse can be used as a straightforward replacement by updating the names of the classes and methods used. There are a few places where direct compatibility could not be preserved as new features were added, however. How to install Python 2.7 and 3.3 on CentOS 6.

In this guide I will show you how to install Python 2.7 and 3.3 on CentOS 6. The examples below are for Python 2.7.6 and Python 3.3.5, but the procedure is the same for any modern version of Python including the upcoming Python 3.4.0. I make regular updates to this guide to track new versions. Please see the end of the document for a changelog. Instance variables vs. class variables in Python. Python command line parameters. Python lesson: Getting user input through the console. Bonus: handling an exception.

This is my second article about the Python programming language. And what we are going to learn today is handling user input from the console. Python makes getting user input from the console very easy. For this purpose we can use the input function, which has this structure: This function only takes the prompt as an argument, which would be the text that the user would see before the console goes into input mode. To store input from a user into a variable you can simply assign the return value of the function to a variable like this: With that little information we can make a little program that will get a number from the user and print it’s square.

I saved this example with the name square.py and ran it with this command: Guaranteed conversion to unicode or byte string. Python's built in function str() and unicode() return a string representation of the object in byte string and unicode string respectively. However they cannot be applied to text string or unicode string in general. In such case Python would just apply the system encoding (e.g.

ASCII, strict), which more often then not would result in UnicodeError. The proper way to convert them is to use the encode and decode method, e.g. Python: copying a list the right way. New = old[:] Those proficient in Python know what the previous line do. It copies the list old into new. This is confusing for beginners and should be avoided. Sadly the [:] notation is widely used, probably because most Python programmers don’t know a better way of copying lists. A little bit of pythonic theory.

How to change a django QueryDict to Python Dict. Python in Latex with Sympy. The final output for scientific research is typically a paper published in a scientific journal. There may be electronic versions of figures and tables that accompany the paper. However, the links between the input data (e.g. images, spectra, time series), the analysis (e.g. code, databases), and the output paper (LaTeX, EPS figures) is often weak. Everyone has their own work-flow and record-keeping philosophies. Django-latex 0.1. Tex 1.8. How do I get the LAST TWO character in a string?

Floating point - How do you round UP a number in Python. Django - Is this a valid illustration of the Python web technology stack.