
Python Patterns (2013) - dedalus
https://taoofmac.com/space/blog/2013/08/11/2300
======
rcarmo
Hi, OP here.

Brief clarification: some of those patterns were a consequence of us having to
run Python 2.5 and 2.6 on older boxes - it's a survival technique, if you will
- and having to vendor some dependencies to make it easier to deploy stuff
under some other interesting constraints the article won't get into.

On the whole, these patterns make it easier for other people to not break your
code when they want to add, say, another set of REST endpoints (which was my
main occupation at the time).

You might also want to read my short piece on SSE:
[http://taoofmac.com/space/blog/2014/11/16/1940](http://taoofmac.com/space/blog/2014/11/16/1940)

And yeah, I've been meaning to change the blog layout. Hardly any time for it
these days, honestly.

~~~
emehrkay
Adding overflow: auto to your code blocks (.syntax) would allow us to see the
whole snippet. I love decorators too, glad I'm not the only crazy person :)

~~~
rcarmo
Riiiight. Let me see if I can toss that in without breaking something else in
the CSS.

------
snuxoll
Can I just state how awful this two-column layout is to read?

~~~
Fuzzwah
I found the "toggle columns" option (top right, under the search box) and it
made me happy.

~~~
dr_zoidberg
While it did away with the horrible 2-column layout, it looks like the article
was writen with those columns in mind, so it uses short paragraphs that look
good on 2-column, but feel a bit weird on a wall of text.

My (barely existant) OCD is going haywire over this article as I read it...

~~~
zodiakzz
OT: Anyone know why all browser vendors implemented this CSS columns
aberration so hurridly..? I have never seen a good use for them, so painful to
read. It's always some designer trying to show off their "epic" CSS skillz.

~~~
anewhnaccount
Seems like it might be useful for a print style sheet. Haven't seen it used to
good effect on a screen though.

------
bendmorris
Interesting as it's quite different from how I generally structure my own
projects. A couple questions:

1\. Why store dependencies locally? Seems a bit wasteful and unnecessary with
tools like pip/virtualenv. (OP does mention virtualenv, but also "Include ALL
the dependencies locally" so I'm not sure what to make of that)

2\. You've chosen to separate your code into MVC-inspired directories instead
of by function. Flask/Django have ways (apps, blueprints) of making
application subcomponents modular. It seems like a single controllers
directory and a single templates directory, etc. could get cluttered pretty
quickly. What will you do as projects grow?

~~~
zphds
Not the OP, but w.r.t. [1] vendored dependencies are especially helpful if you
happen to deploy the software on premise where you might not have full system
access and in some cases, no internet or also strict policies on what can be
installed.

~~~
Loic
What you can do is get wheel packages for all your dependencies and install
them from a local "wheelhouse":

    
    
        pip install -r requirements.txt --no-index --find-links=/path/to/wheelhouse
    

If for each requirement in `requirements.txt` you have the corresponding wheel
file in `/path/to/wheelhouse`, this will not touch the network and complete. I
have been using this approach the past 2 years with great success.

You can learn a bit more about wheel in this very good article:
[http://lucumr.pocoo.org/2014/1/27/python-on-
wheels/](http://lucumr.pocoo.org/2014/1/27/python-on-wheels/)

~~~
rcarmo
Well, I couldn't use wheels on Python 2.6 (and certainly not use a wheelhouse
properly in the environments I had to tackle back then), but yes, this is a
good approach.

------
Terr_
> Well, because JSON is about the only format I can read on all the languages
> I use without ambiguity or extra dependencies

Alas, there is a downside: The JSON spec does not allow comments, which are
often important for configuration files. (Though given how much that
simplifies parsing, it may have been the right decision.)

So you either omit comemnts, or end up trying to shove placeholder key-values
into the closest place you can to whatever you want to comment about.

~~~
creshal
For configuration files I just put in comments anyway and strip them before
feeding the file to a JSON parser. (Like here:
[https://github.com/creshal/yspave/blob/master/yspave/pave.py...](https://github.com/creshal/yspave/blob/master/yspave/pave.py#L33)
)

Hackish, but beats having to bother with YAML parsers and their regular RCE
vulnerabilities.

------
mixmastamyk
Bottle, peewee, gevent workers, eh? Interesting choices. SimpleTemplate looks
interesting, I hadn't seen it before.

Would be nice to hear some discussion there, instead of the column layout.

~~~
coleifer
I'm the author of Peewee and I've been running my blog with Flask, Peewee,
gevent and sqlite. It's a very low-impact combination, lightweight and fun to
use, although I'm a bit biased.

~~~
raymondh
Thank you for your work on Peewee. The code is nicely written and is easily
read start to finish.

~~~
JustSomeNobody
Ok, now I'm going to go study Peewee's code...

~~~
coleifer
Ackkkk I'm actually rewriting it currently to try and ameliorate the cruft
that's crept in since the 2.0 rewrite. I want to say "But wait! If you read
the code in a couple weeks it'll be so much better!"

~~~
JustSomeNobody
Trust me, I'm sure I can improve my Python skills by reading the current
version. I don't get a chance to use Python enough to shed the baggage of
other languages to write Pythonic code, if that makes sense.

I haven't used Peewee in a project yet, but I've poked around with it. It's
really nice, so thank you.

------
sanpan
here's a link to the utils mentioned in the article:
[https://github.com/rcarmo/python-utils](https://github.com/rcarmo/python-
utils)

------
andy_ppp
I have to say the using Pika and RabbitMQ directly has been a _lot_ easier
than trying to use Celery. Just my experience.

~~~
neuroid
Just out of interest, what were the main challenges you faced when using
Celery?

Celery is a task queue. Pika could definitely be used to build a task queue
(similarly to how Celery is built on top of Kombu), but I can't think of many
good reasons why that would pay off. Unless your use case was not a task
queue...

~~~
andy_ppp
Flask application context problems, hiding exceptions within tasks, Json
Serialisation sometimes does weird things and hard to configure (the default,
pickle, seems to be warned against as a security vulnerability), weirdness
with Gevent/Socket.io flask stuff, difficulty (impossibility??) to emit socket
events from within a worker, finally the canvas stuff seems great on the
surface but I found it mind blowingly difficult to debug weirdness with it.

My Pika/RabbitMQ stuff was much easier to build, unit test and debug (i.e. it
actually works). There is every chance the I am just a bit retarded when it
comes to using celery, but the fact I've built the Pika version maybe these
were just real issues? Dunno, it might all work flawlessly for you in which
case it's a great tool.

------
hcarvalhoalves
Project scaffolding like this feels very 2005.

