thefekete.net

$> :(){ :|:& };:

Bash: Count Lines of Code in a Python Project

#!/bin/bash
find . -name '*.py' | xargs cat | sed '/^\s*#/d;/^\s*$/d' | wc -l

xargs converts find’s output to arguments, the sed RE drops comments and blank lines, and wc counts them lines. Thanks to these posts on stack overflow for some reference material.

Quickly check line character maximums from the command line

If you want to make sure you didn’t go over 79 or 80 characters in a file you can simply run:

#!/bin/bash
grep -Pn '.{80,}' file-to-check

This uses grep’s perl regex option (-P) so we can use the expression .{80,} to find any lines that have >= 80 characters. The -n option prints the line numbers in the output so you can find them easily.

Project Pointers

Got this from a comment on slashdot by raddan. Seems like good advice…

  • Never pass unvalidated input to your database
  • Never pass unvalidated input to the system
  • Always validate on the server-side; client-side validation should only function as a convenience to the user
  • Validate data coming from other servers (if you’re doing any web services stuff).
  • Encrypt connections to the server
  • Enforce inactivity timeouts
  • Do not allow multiple logins to the same account (unless you want your game to application to work that way)
  • Always authenticate users; consider using two-factor authentication (CAPTCHA + password, etc)
  • Allow administrators to revoke accounts
  • Make it easy for administrators/force administrators to sandbox/chroot your application
  • If your applications needs to use server storage, consider DoS attacks (a user uploading lots of stuff)
  • Make sure all privileged actions hit the same authentication class/function; if you change your authentication code, this ensures that the changes are applied across the board <– I catch newbie programmers making this mistake all the time!

If you do all of the above, your app might still not be “secure”, but breaking it will be a PITA.

How to use *args and **kwargs in Python

The SaltyCrane Blog has a great post on the use of *args and **kwargs in python functions. Very simple and a good reference. You can check it out here:

http://www.saltycrane.com/blog/2008/01/how-to-use-args-and-kwargs-in-python/

Word wrap as you type in vim (with newlines inserted)

To enable word wrap in vim with automatic newlines:

enter command mode (ESC) and type :set textwidth=70 or add it to your ~/.vimrc

Just be careful when editing files that care about newlines!