| Conditions | 1 | 
| Total Lines | 62 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | from nose.tools import assert_true, assert_equal, assert_raises | ||
| 72 | def test_str(self): | ||
| 73 | # Create simple PBS file | ||
| 74 | expected = """#!/bin/bash | ||
| 75 | #PBS -q qtest@mp2 | ||
| 76 | #PBS -V | ||
| 77 | #PBS -l walltime=01:00:00 | ||
| 78 | |||
| 79 | # Modules # | ||
| 80 | |||
| 81 | # Prolog # | ||
| 82 | |||
| 83 | # Commands # | ||
| 84 | |||
| 85 | # Epilog #""" | ||
| 86 | pbs = PBS(queue_name="qtest@mp2", walltime="01:00:00") | ||
| 87 | assert_equal(str(pbs), expected) | ||
| 88 | |||
| 89 | # Create a more complex PBS file | ||
| 90 | prolog = ['echo "This is prolog1"', | ||
| 91 | 'echo "This is prolog2"'] | ||
| 92 | commands = ["cd $HOME; echo 1 2 3 1>> cmd1.o 2>> cmd1.e", | ||
| 93 | "cd $HOME; echo 3 2 1 1>> cmd1.o 2>> cmd1.e"] | ||
| 94 | epilog = ['echo "This is epilog1"', | ||
| 95 | 'echo "This is epilog2"'] | ||
| 96 | modules = ["CUDA_Toolkit/6.0", "python2.7"] | ||
| 97 | |||
| 98 | expected = """\ | ||
| 99 | #!/bin/bash | ||
| 100 | #PBS -q qtest@mp2 | ||
| 101 | #PBS -V | ||
| 102 | #PBS -A xyz-123-ab | ||
| 103 | #PBS -l walltime=01:00:00 | ||
| 104 | #PBS -l nodes=2:ppn=3:gpus=1 | ||
| 105 | |||
| 106 | # Modules # | ||
| 107 | module load CUDA_Toolkit/6.0 | ||
| 108 | module load python2.7 | ||
| 109 | |||
| 110 | # Prolog # | ||
| 111 | {prolog1} | ||
| 112 | {prolog2} | ||
| 113 | |||
| 114 | # Commands # | ||
| 115 | {command1} | ||
| 116 | {command2} | ||
| 117 | |||
| 118 | # Epilog # | ||
| 119 | {epilog1} | ||
| 120 | {epilog2}""".format( | ||
| 121 | prolog1=prolog[0], prolog2=prolog[1], | ||
| 122 | command1=commands[0], command2=commands[1], | ||
| 123 | epilog1=epilog[0], epilog2=epilog[1]) | ||
| 124 | |||
| 125 | pbs = PBS(queue_name="qtest@mp2", walltime="01:00:00") | ||
| 126 | pbs.add_resources(nodes="2:ppn=3:gpus=1") | ||
| 127 | pbs.add_options(A="xyz-123-ab") | ||
| 128 | pbs.add_modules_to_load(*modules) | ||
| 129 | pbs.add_to_prolog(*prolog) | ||
| 130 | pbs.add_commands(*commands) | ||
| 131 | pbs.add_to_epilog(*epilog) | ||
| 132 | |||
| 133 | assert_equal(str(pbs), expected) | ||
| 134 | |||
| 139 |