coding-style

Pylint invalid constant name

Pylint invalid constant name Question: I’m receiving a Pylint error regarding my constant: MIN_SOIL_PARTICLE_DENS (invalid name). Any ideas why this constant is wrong? Here’s my full function: def bulk_density(clay, sand, organic_matter): MIN_SOIL_PARTICLE_DENS = 2.65 x1 = (0.078 + 0.278 * sand + 0.034 * clay + 0.022 * organic_matter – 0.018 * sand * organic_matter …

Total answers: 3

What's the correct way to sort Python `import x` and `from x import y` statements?

What's the correct way to sort Python `import x` and `from x import y` statements? Question: The python style guide suggests to group imports like this: Imports should be grouped in the following order: standard library imports related third party imports local application/library specific imports However, it does not mention anything how the two different …

Total answers: 6

Python PEP: blank line after function definition?

Python PEP: blank line after function definition? Question: I can’t find any PEP reference to this detail. There has to be a blank line after function definition? Should I do this: def hello_function(): return ‘hello’ or shoud I do this: def hello_function(): return ‘hello’ The same question applies when docstrings are used: this: def hello_function(): …

Total answers: 4

Does PEP 8 require whitespace around operators in function arguments?

Does PEP 8 require whitespace around operators in function arguments? Question: I have this code: some_list = range(a, b+1) After checking my coding style with pep8 plugin for vim, I got this warning: missing whitespace around operator It seems that to be compliant with PEP 8 I should instead write this? some_list = range(a, b …

Total answers: 3

Long imports in Python

Long imports in Python Question: I sometimes have to write something like from blqblq.lqlqlqlq.bla import fobarbazbarbarbazar as foo from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas which takes more than 80 characters. This situation is not covered in the official Python coding style guide. How do I write such imports pythonically? Asked By: Vorac || Source Answers: …

Total answers: 2

Python exception chaining

Python exception chaining Question: Is there a standard way of using exception chains in Python? Like the Java exception ’caused by’? Here is some background. I have a module with one main exception class DSError: class DSError(Exception): pass Somewhere within this module there will be: try: v = my_dict[k] something(v) except KeyError as e: raise …

Total answers: 2

Simple way to create matrix of random numbers

Simple way to create matrix of random numbers Question: I am trying to create a matrix of random numbers, but my solution is too long and looks ugly random_matrix = [[random.random() for e in range(2)] for e in range(3)] this looks ok, but in my implementation it is weights_h = [[random.random() for e in range(len(inputs[0]))] …

Total answers: 13

Using len() and def __len__(self): to build a class

Using len() and def __len__(self): to build a class Question: Just curious, Is there any difference (advantages and disadvantages) between using len() or def __len__() when I build a class? And which is the best Python style? class foo(object): def __init__(self,obs=[]) self.data = obs self.max = max(obs) self.min = min(obs) self.len = len(obs) or class …

Total answers: 2

Python "private" function coding convention

Python "private" function coding convention Question: When writing a python module and functions in it, I have some “public” functions that are supposed to be exposed to outsiders, but some other “private” functions that are only supposed to be seen and used locally and internally. I understand in python there is no absolute private functions. …

Total answers: 1