1. Booleans
The numbers 0, 0.0, and 0+0j are all False; any other number is True.
The empty string "" is False; any other string is True.
The empty list [] is False; any other list is True.
The empty dictionary {} is False; any other dictionary is True.
The empty set set() is False; any other set is True.
The special Python value None is always False.
2. Working on REPL
Import library
>>> from autoinfra.resource import Client, Ddr, Resource
List all available methods
>>> dir(Client)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_connect_ssh', 'bring_up_eth', 'configure_ip', 'eth_filter', 'get_eths_details', 'get_eths_via_ifconfig', 'get_eths_via_ip_a', 'get_switch_port_details', 'is_connected', 'is_eth_link_detected', 'send_cmd']
Access docstring on a method
>>> help(Client.get_switch_port_details)
3. Testing
From this post.
$ pip install nose
From root of project run nosetests, this would look for any test under this directory and run it
$ pwd
/auto/home13/delacs/Documents/projects/nextgen/libs/autoinfra
$ nosetests
.....
----------------------------------------------------------------------
Ran 5 tests in 2.659s
OK
To run a single file
$ nosetests -v autoinfra/resources/tests/test_ddr.py
test_get_10g_eths (test_ddr.TestDdr) ... ok
test_se_cmd (test_ddr.TestDdr) ... ok
test_send_cmd (test_ddr.TestDdr) ... ok
----------------------------------------------------------------------
Ran 3 tests in 2.602s
To run a particular test in a file
$ nosetests -v autoinfra/switches/tests/test_cisco.py:TestCisco.test_move_to_vlan_no_nxapi_single
test_move_to_vlan_no_nxapi_single (test_cisco.TestCisco) ... ok
----------------------------------------------------------------------
Ran 1 test in 12.423s
OK
This works when test files have imports like this...
from autoinfra.resources.client import Client
4. Install python 2.7.12 on another directory (/opt/python)
# wget https://www.python.org/ftp/python/2.7.12/Python-2.7.12.tgz
# tar zxf Python-2.7.12.tgz
# cd Python-2.7.12/
# ./configure --prefix=/opt/python
# make
# make install
5. Create a virtual environment
$ sudo yum install python-virtualenv
$ mkdir sample_project
$ cd sample_project
$ virtualenv -p /opt/python/bin/python2.7 .venv
6. Data dumper
>>> from pprint import pprint
>>> pprint(vars(Host.objects.get(name="w3-perfsds-0001")))
Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts
9.12.2016
Python Notes
2.07.2015
Ruby Notes
While reading RubyMonk tutorials
1. Truthiness of objects in Ruby
Only objects false and nil equates to false. Every other object like say 1, 0, "" are all evaluated to be true.
2. Use break in infinite loops
1. Truthiness of objects in Ruby
Only objects false and nil equates to false. Every other object like say 1, 0, "" are all evaluated to be true.
2. Use break in infinite loops
loop do monk.meditate break if monk.nirvana? end3. Append to an array, use << operator
[1, 2, 3, 4, 5] << 'woot' [1, 2, 3, 4, 5, "woot"]4. Filter elements of an array, use 'select'
names = ['rock', 'paper', 'scissors', 'lizard', 'spock']
names.select { |word| word.length > 5 }
5. Delete elements of an array, use 'delete_if'
[1,2,3,4,5,6,7,8,9].delete_if { |i| i % 2 == 0 }
6. Methods are themselves objects, and responds using 'call'
next_method_object = 1.method("next")
puts next_method_object.call
7. Beware of shallow object copies, to do a deep copy use Marshal
new_job = Marshal.load(Marshal.dump(job))8. Use heredoc for convenient multiline strings. The dash ignores spaces before placeholder. The gsub takes care of removing spaces at beginning of each line.
def long_message
puts <<-EOT.gsub(/^\s*/, '')
Here goes a very long message...
Sincerely,
Dr. Foobear
EOT
end
7. Use 'send' when testing private methods
# obj.send(:method_name, args) @parser.send(:running?, test_strings)
Subscribe to:
Posts (Atom)