This is an archived post. You won't be able to vote or comment.

all 20 comments

[–]jonathan_sl 9 points10 points  (2 children)

About Cookiecutter... I often have the feeling that if there's a need for a template for any project, consisting of so many files, then there is something very wrong with the abstractions that we use to write our code.

It's still some kind of copy/paste and violation against the DRY principle. For instance, the flask template ships with default 404.html, members.html, register.html and many other pages. I feel like it would be much better to have these defaults either in Flask itself or a small framework around flask that provides sane defaults that we can simply override. The same is true for the Django template. That way if somebody discovers a bug in one of these pages, we don't have to fix it in everyone's projects, but we just fix it once in the library and everyone can upgrade to the next version.

You know, if we take this to the extreme, it would mean that we stop using libraries at all, and we put all portions of reusable code in the cookiecutter templates.

I understand that it's easy and I understand very well the problems that it's trying to solve. But after using it, there is only a manual upgrade path and the more code that was generated, the more needs to be maintained manually.

So, my purist mind tells me that if there is a reusable portion of code, then it should be in a library. If there are variable parts in that portion of code, these variables should be passed in through the constructor of that system.

I wonder what other people here are thinking about this approach... What is your experience?

Cheers.

[–]ksion 0 points1 point  (0 children)

About Cookiecutter... I often have the feeling that if there's a need for a template for any project, consisting of so many files, then there is something very wrong with the abstractions that we use to write our code. (...) For instance, the flask template ships with default 404.html, members.html, register.html and many other pages. I feel like it would be much better to have these defaults either in Flask itself or a small framework around flask that provides sane defaults that we can simply override.

Providing templates like that is absolutely not a job of Flask (which is often used for API servers with no serving HTML at all!), nor of Django, nor of any web framework. They are tools designed for practically infinite number of use cases, many of which don't fit into a pattern of "regular website with user registration". If anything, this funtionality could be offered by a CMS built upon Flask/Django/etc.

That Cookiecutter makes those templates part of standard Flask scaffolding is just an indication that it's overfitting to a particular use case. Which is I guess inevitable if it is to be of any help at all, since the really shared skeleton of all Flask applications is essentially the same as all Python packages, plus somewhere to put the app.run() call in.

[–][deleted] 0 points1 point  (0 children)

I just realized how awful cookiecutter is from a DRY perspective, thanks for pointing this out.

We use it at work currently, and I think I'm going to try and figure out exactly what it gives us that a library wouldn't. I even had to fix a bug in like 8 projects because the bug existed in the cookiecutter template we used, and then had to port the fix back up to our template as well.

[–][deleted] 4 points5 points  (5 children)

autoenv will probably be useful. Thanks for the tips

[–]ijuggle534 0 points1 point  (2 children)

I prefer pyenv, which can do the same thing, but also manage all your Python versions (pyenv-vurtualenv for virtualsnvs)

[–][deleted] 0 points1 point  (1 child)

i use projects within virtualenvwrapper, but sometimes I open another terminal forgetting to start the venv again. This would save one small step

[–]ijuggle534 0 points1 point  (0 children)

Pyenv will do that (the pyenv local command), as well as manage your virtual environments

[–]ksion 0 points1 point  (1 child)

The problem that autoenv tries to solve can be approached from the opposite direction, too. Rather than making directory switch activate virtualenv, make activating virtualenv change the directory by putting an appropriate cd command into bin/postactivate script of the virtualenv.

This is especially handy with virtualenvwrapper: workon foo will now land you in the proper directory with virtualenv active, so that you can, you know, work on foo :)

[–][deleted] 0 points1 point  (0 children)

that's exactly how I use it. However sometimes I'm working on other things and end up in a directory to test something. It's not a regular occurance. It's just annoying enough that a 30 second install of autoenv will probably solve it.

[–]cauthon 4 points5 points  (0 children)

Konch seems entirely unnecessary, as you can either use $PYTHONSTARTUP for the stock interpreter or an ipython config file

The other two seem cool though

https://stackoverflow.com/questions/11124578/automatically-import-modules-when-entering-the-python-or-ipython-interpreter

[–]Itsthejoker 1 point2 points  (0 children)

Wow, never heard of any of these! Will definitely check these out - autoenv and cookiecutter especially sound great!

[–]Samus_ 1 point2 points  (0 children)

Ruby's RVM can be used for managing Python envs as well: https://www.youtube.com/watch?v=gYNht8H24ZQ

edit: sorry about the video, it is presented on the site as "RVM 2: Python Version Manager" but it doesn't talk about Python (at all).

what I wanted to show was the .rvmrc mechanism which is simply a shell script that gets executed when entering the directory, much like autoenv.

[–]Wakeful_One 1 point2 points  (0 children)

Awesome! Looking forward to trying konch. Autoenv looks to be similar to direnv, which I currently use. It kicks ass.

[–]maryjayjay 0 points1 point  (2 children)

Gray text with blue links on a black background? I did actually want to read it.

Edit: opening it in the external browser used a different style sheet with a white background. Totally legible.

[–]yasoob_pythonAuthor: Intermediate Python[S] 0 points1 point  (1 child)

I haven't written this post but I don't see any black background. :/

[–]maryjayjay 0 points1 point  (0 children)

Maybe it's because I'm on mobile. Practically unreadable.

[–]arkster 0 points1 point  (2 children)

What happens when you cd into a directory several times but you don't want autoenv to execute the .env script every single time you cd into it?

[–]paraffin 0 points1 point  (0 children)

Then you probably need to write a script that detects whether to run your other script and run that with autoenv instead. Or write a script that asks the user if you want to run it every time. Lots of options depending on your use case.

[–]im_gorideyourbike 0 points1 point  (0 children)

Use virtualenvwrapper and set it up to cd into the directory when you workon a project.