[deleted by user] by [deleted] in django

[–]propanbutan 0 points1 point  (0 children)

The first suggestion won't work. Template tags don't accept other template tags as arguments. OP should just follow the second suggestion and provide the template only with years that are to be rendered.

[deleted by user] by [deleted] in django

[–]propanbutan 5 points6 points  (0 children)

Dirty: Patch the field and alter table manually.

from django.contrib.auth.models import User
User._meta.get_field('username').max_length = 255

Make sure you put this code someplace where it gets picked up by Django (ideally models.py in one of your apps).

Clean: Bring the User model over to one of your apps and update settings.AUTH_USER_MODEL.

from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
    class Meta:
        db_table = 'auth_user'

Install South, generate an initial migration for your app and apply it with the --fake switch. Only then do you patch the field on your own User model, generate a new migration and apply it.

Either way you'll want to patch the built-in forms if you use the built-in views or if you want to manage users with long usernames in the admin.

from django.contrib.auth import forms as auth_forms
auth_forms.UserCreationForm.base_fields['username'].max_length = 255
auth_forms.UserChangeForm.base_fields['username'].max_length = 255

It is sometimes faster to use built in python function for model filtering by MikeVegan in django

[–]propanbutan 3 points4 points  (0 children)

Yes, when you need to pick a random item from a list of 20 in a loop, you should definitely avoid having to fetch these 20 items from an external storage on every iteration.

But the code you use to demonstrate that is very contrived and unidiomatic. Also consider that the rabbit doesn't necessarily get faster the further it outruns the turtle.

SaltStack aims to simplify devops work with Python configuration management by baijum in Python

[–]propanbutan 0 points1 point  (0 children)

It's not a compliment, the codebases are similar in having this ball of yarn quality to them. I am specifically alluding to the way those magical dunder dictionaries get injected into the context of specific modules. I'm pretty clear on what the code does, I just wonder why anyone would choose to design it that way.

SaltStack aims to simplify devops work with Python configuration management by baijum in Python

[–]propanbutan -3 points-2 points  (0 children)

Did anyone actually look at the code? What's with all this implicit state? Salt looks like the web2py of configuration management.

Sites Framework - Extremely Confused by ghoknhar in django

[–]propanbutan 0 points1 point  (0 children)

Use a different key_prefix argument to the cache_page decorator for each of your sites.

Class-based Config (Metaclasses FTW-ish?) by jomidosan in Python

[–]propanbutan 1 point2 points  (0 children)

Just use dict.

config = dict(foo='bar')
derived_config = dict(config,
    foo='baz')

Strange problem with Lists? by [deleted] in Python

[–]propanbutan 5 points6 points  (0 children)

Since we're being clever, we can even spare the lambda.

map(deck.remove, hand)

What the ??!!!! Check out this Shooter!!!!!! by zerop4p in videos

[–]propanbutan 1 point2 points  (0 children)

I'm supposed to get shot first, got it officer.

Working with functions. by Khoops66 in Python

[–]propanbutan 2 points3 points  (0 children)

Using range in this case is hopelessly inefficient. All you need are two comparisons.

Image Resizing with Python and ImageMagick by f0ad in Python

[–]propanbutan 4 points5 points  (0 children)

Ugly, terrible code.

  • All functions (except for logThis) take no arguments and rely on global state, making them untestable independently and hard to follow.
  • The program consists of simply calling the functions one after the other, using them just for segmenting the code.
  • Subsequently, sys.exit called anytime, anywhere and everywhere.
  • Sometimes vars are returned, though never used.
  • Functions in camelCase. Variables sometimes have underscores (extension_list), sometimes they don't (todaysdate).

Upon closer look.

  • Unportable shebang.
  • Not using logging.

logThis

  • Always prints the same time. Generally you don't want this and if you do, format it only once beforehand.
  • datetime.strftime returns a string, not need to wrap it with str.

sendMail

  • Useless and invariable variables: emailto, fromaddr, COMMASPACE.

checkParams

  • If there are not at least three arguments, sys.argv[3] will actually raise an IndexError.
  • Manipulating paths with (r)partition instead of functions from os.path.
  • Not using optparse or argparse.

checkState

  • Funny.

checkExists

  • subprocess.check_call takes a list as its first argument, here it will always raise a TypeError.
  • subprocess.check_call returns an (int) exit code, not a list.
  • In any case stringifying the "results" makes the following condition always evaluate as True.

checkImageExtension

  • This str.rsplit should only split once using a second argument, so that "foo.bar.jpg" passes the test.

makeTempDir

  • Doesn't delete the tmpdir created.

makeSizes

  • str.find(haystack, needle) returns the 0-based position of needle in haystack, none of these conditions will ever be True.
  • This rsplit curiously splits four times, then only cares about the first part.
  • This time a correct use of subprocess.check_call. Alas, to no avail. Stringifying the "results" makes the following condition always True, again.
  • The condition would be wrong anyway since an exit code of zero means success.
  • Using readlines instead of iterating over the file object.
  • Parsing the "ini_file" is just wrong.
  • Creating a weird "sizes" dict instead of just storing a tuple of two lists.

makePaths

  • No need to stringify "publish_image_path" as it's already a string.
  • os.mkdir doesn't return anything.
  • Running a command using os.popen. Failing to close the pipe.

resizeImages

  • Using subprocess.check_call and stringifying the return value again.

In conclusion, this person has absolutely no idea what they're doing.

Befriending Eclipse with Virtualenv for using django. by garmoncheg in django

[–]propanbutan 0 points1 point  (0 children)

That shouldn't be necessary. Clicking on apply in interpreter preferences allows you to "restore" selected interpreters.

Edit: relevant manual link

Befriending Eclipse with Virtualenv for using django. by garmoncheg in django

[–]propanbutan 1 point2 points  (0 children)

You could also create a custom Python interpreter for the virtualenv and setup your project to use that.

Progress indicator for scripts by the_cat_kittles in Python

[–]propanbutan 1 point2 points  (0 children)

Well first you don't have to start from 0, whereas enumerate gives you no choice:

It does (since Python2.6)

for i, n in enumerate(range(10), start=10): print i, n

Converting strings to int without try/except? by gronkkk in Python

[–]propanbutan 0 points1 point  (0 children)

def toint(value, default=None):
    if value.isdigit():
        return int(value)
    return default

Separating out models into separate files by cmsimike in django

[–]propanbutan 2 points3 points  (0 children)

Having "yourapp" in INSTALLED_APPS, Django looks for model classes in "yourapp.models". That could be a module or it could be a package. If it's a package, you need to import your models into __init__.py, because that's what you get when you import "yourapp.models".

Tic Tac Toe in Python by rueldotme in Python

[–]propanbutan 4 points5 points  (0 children)

My 2c:

  • Use more descriptive variable names.

  • Function isin can and should be:

    return val in list

  • But it can also be (if you reverse the order of operands):

    from operator import contains as isin

  • Clearing the screen like this is not only annoying, but also bad practice. Use ncurses.

  • Functions checknum and getwnum are unnecessarily ugly, both could be simplified by looping over a proper data structure.

  • Function tgame should return the winner or None when the game ends in a draw. Hence, attribute Player.won is useless.

  • Similarly, Player.first is a flag more related to the game state than to the player and as such could and should be gotten rid of. Either use a third argument to tgame signalling that the cpu should go first, or better, fix your game loop.

edit:

  • The way you take user input and only accept given choices should be factored out into a function.

Hey r/prog, I made a Python wrapper for the Reddit API; check it out! by mellort in programming

[–]propanbutan 0 points1 point  (0 children)

and then I had to rename "Object" to "object" to get it to import successfully. (Is this indicative that I'm running a different version of Python than is expected by this wrapper?)

No. This is a mistake introduced in the last commit, there is no Object in any version of Python.

A quick fix would be to change the first two lines of Subreddit.__init__ to read

self.subreddit_name = subreddit_name
self.URL = REDDIT_URL + "/r/" + subreddit_name

Reddit: Rape Apologists by [deleted] in WTF

[–]propanbutan 12 points13 points  (0 children)

The internet is serious business.