Programming

Format Python code correctly the Google way!

Posted on Updated on

This style guide from google can help clean up your code and bring you up to speed on sharing your work with others and easily understanding other’s work

Background

Python is the main scripting language used at Google. This style guide is a list of dos and don’ts for Python programs.

.

Python Language Rules

Lint

Run pylint over your code.

Imports

Use imports for packages and modules only.

Packages

Import each module using the full pathname location of the module.

Exceptions

Exceptions are allowed but must be used carefully.

Global variables

Avoid global variables.

Nested/Local/Inner Classes and Functions

Nested/local/inner classes and functions are fine.

List Comprehensions

Okay to use for simple cases.

Default Iterators and Operators

Use default iterators and operators for types that support them, like lists, dictionaries, and files.

Generators

Use generators as needed.

Lambda Functions

Okay for one-liners.

Conditional Expressions

Okay for one-liners.

Default Argument Values

Okay in most cases.

Properties

Use properties for accessing or setting data where you would normally have used simple, lightweight accessor or setter methods.

True/False evaluations

Use the “implicit” false if at all possible.

Deprecated Language Features

Use string methods instead of the string module where possible. Use function call syntax instead of apply. Use list comprehensions and for loops instead of filter and map when the function argument would have been an inlined lambda anyway. Use for loops instead of reduce.

Lexical Scoping

Okay to use.

Function and Method Decorators

Use decorators judiciously when there is a clear advantage.

Threading

Do not rely on the atomicity of built-in types.

Power Features

Avoid these features.

Python Style Rules

Semicolons

Do not terminate your lines with semi-colons and do not use semi-colons to put two commands on the same line.

Line length

Maximum line length is 80 characters.

Parentheses

Use parentheses sparingly.

Indentation

Indent your code blocks with 4 spaces.

Blank Lines

Two blank lines between top-level definitions, one blank line between method definitions.

Whitespace

Follow standard typographic rules for the use of spaces around punctuation.

Shebang Line

Most .py files do not need to start with a #! line. Start the main file of a program with #!/usr/bin/python with an optional single digit 2 or 3 suffix per PEP-394.

Comments

Be sure to use the right style for module, function, method and in-line comments.

Classes

If a class inherits from no other base classes, explicitly inherit from object. This also applies to nested classes.

Strings

Use the format method or the % operator for formatting strings, even when the parameters are all strings. Use your best judgement to decide between + and % (or format) though.

Files and Sockets

Explicitly close files and sockets when done with them.

TODO Comments

Use TODO comments for code that is temporary, a short-term solution, or good-enough but not perfect.

Imports formatting

Imports should be on separate lines.

Statements

Generally only one statement per line.

Access Control

If an accessor function would be trivial you should use public variables instead of accessor functions to avoid the extra cost of function calls in Python. When more functionality is added you can use property to keep the syntax consistent.

Naming

module_name, package_name, ClassName, method_name, ExceptionName, function_name, GLOBAL_CONSTANT_NAME, global_var_name, instance_var_name, function_parameter_name, local_var_name.

Main

Even a file meant to be used as a script should be importable and a mere import should not have the side effect of executing the script’s main functionality. The main functionality should be in a main() function.

Parting Words

BE CONSISTENT.

If you’re editing code, take a few minutes to look at the code around you and determine its style. If they use spaces around all their arithmetic operators, you should too. If their comments have little boxes of hash marks around them, make your comments have little boxes of hash marks around them too.

The point of having style guidelines is to have a common vocabulary of coding so people can concentrate on what you’re saying rather than on how you’re saying it. We present global style rules here so people know the vocabulary, but local style is also important. If code you add to a file looks drastically different from the existing code around it, it throws readers out of their rhythm when they go to read it. Avoid this.

https://google.github.io/styleguide/pyguide.html

Advertisement

easy_install vs pip : what to use?

Posted on Updated on

Nice answer over at stackoverflow

http://stackoverflow.com/questions/3220404/why-use-pip-over-easy-install

Many of the answers here are out of date for 2015 (although the accepted one is not). Here’s the current state of things:

  • Binary packages are now distributed as wheels (.whl files)—not just on PyPI, but in third-party repositories like Christoph Gohlke’s Extension Packages for Windows. pip can handle wheels; easy_install cannot.
  • Virtual environments (which come built-in with 3.4, or can be added to 2.6+/3.1+ with virtualenv) have become a very important and prominent tool (and recommended in the official docs); they include pip out of the box, but don’t even work properly with easy_install.
  • The distribute package that included easy_install is no longer maintained. Its improvements over setuptools got merged back into setuptools. Trying to install distribute will just install setuptools instead.
  • easy_install itself is only quasi-maintained.
  • All of the cases where pip used to be inferior to easy_install—installing from an unpacked source tree, from a DVCS repo, etc.—are long-gone; you can pip install ., pip install git+https://.
  • pip comes with the official Python 2.7 and 3.4+ packages from python.org, and a pip bootstrap is included by default if you build from source.
  • The various incomplete bits of documentation on installing, using, and building packages have been replaced by the Python Packaging User Guide. Python’s own documentation on Installing Python Modules now defers to this user guide, and explicitly calls out pip as “the preferred installer program”.
  • Other new features have been added to pip over the years that will never be in easy_install. For example, pip makes it easy to clone your site-packages by building a requirements file and then installing it with a single command on each side. Or to convert your requirements file to a local repo to use for in-house development. And so on.

The only good reason that I know of to use easy_install in 2015 is the special case of using Apple’s pre-installed Python versions with OS X 10.5-10.8. Since 10.5, Apple has included easy_install, but as of 10.10 they still don’t include pip. With 10.9+, you should still just use get-pip.py, but for 10.5-10.8, this has some problems, so it’s easier to sudo easy_install pip. (In general, easy_install pip is a bad idea; it’s only for OS X 10.5-10.8 that you want to do this.) Also, 10.5-10.8 include readline in a way that easy_install knows how to kludge around but pip doesn’t, so you also want to sudo easy_install readline if you want to upgrade that.

from abernet

python double asterik **

Posted on Updated on

I was wondering what the ** in a piece of code was doing

found that it is to the power of

example:

print 2*3 would be 6

but

print 2**3 would be 8 (two to the power of three  ie: 2^3)

#pythonnoob

“The caret operator ^ is typically used for a binary XOR operation in languages like C, C++, Java, and so on. It makes sense that Python didn’t want to “go against the flow” of what existing languages already use.”

-empire359 on reddit

Employment tip: using github : learn the commands

Posted on Updated on

Employers look down upon users of graphical github, learn the commands.

Good advice from

If you are a programmer why wouldn’t you want to learn the command line?