import os print(os.environ) # Print a dict of env vars and their values os.environ["PYTHONPATH"] # Query a specific env var
Python picks up these environment variables from your shell when it is first invoked. So this is useful to check if your shell environment variables and their values are being correctly imported into Python.
]]>tensorflow:
import tensorflow print(tensorflow.__path__) # Output is: # ['/usr/local/lib/python3.6/site-packages/tensorflow/python/keras/api/_v1', # '/usr/local/lib/python3.6/site-packages/tensorflow_estimator/python/estimator/api/_v1', # '/usr/local/lib/python3.6/site-packages/tensorflow', # '/usr/local/lib/python3.6/site-packages/tensorflow/_api/v1']
This can be useful to check if the right module from the right location is being imported. It is also useful to know where to go check out the Python source files of a module for inspection or debugging.
]]>$ python -m SimpleHTTPServer 8901
http://put-remote-computer-ip-here:8901Tried with: Ubuntu 18.04
]]>Here are the steps I followed to get my local computer setup for the FastAI course:
conda info from the shell. I wanted to try the course without resorting to Anaconda, but that seems unnecessarily complicated.$ git clone [email protected]:fastai/fastai.git
fastai and install all the required Python packages (including PyTorch) under that:$ conda env update
fastai environment:$ conda activate fastai
PYTHONPATH environment variable, so that you can import it from Python.
Open a Python interpreter and check if you can import PyTorch and it has CUDA and CuDNN support:
$ python >>> import torch >>> torch.cuda.is_available() True >>> torch.backends.cudnn.enabled True
$ from fastai.imports import *
You are now ready to execute any of the code shown in the course at a Python interpreter or inside Python scripts. Note that you will still need to download any additional datasets needed by the course. You will find these instructions in the Jupyter notebooks or course material.
]]>Remember that all types are descended from the object type.
Even type is a type and it is a child of the object type.
The __base__ attribute of any type has a string value with the name of the parent type.
The __subclasses__ method of any type lists the child types.
To determine which are the standard types (or builtin types or builtins as they are called in Python), check the __module__ attribute of the type. If it is builtins in Python 3 or __builtin__ in Python 2, then that is a standard type.
If you start from object, you can actually list the entire type hierarchy tree. A script that does just that can be found here.
In Python 3.5.2, I found that there are 143 builtin types (most of them are just types of Exception) in the tree:
object
+-- type
+-- dict_values
+-- odict_values
+-- tuple_iterator
+-- set
+-- fieldnameiterator
+-- frame
+-- dict_keyiterator
+-- PyCapsule
+-- coroutine
+-- bytearray
+-- NoneType
+-- list
+-- dict
+-- getset_descriptor
+-- method-wrapper
+-- method
+-- str_iterator
+-- formatteriterator
+-- str
+-- set_iterator
+-- range_iterator
+-- memoryview
+-- cell
+-- generator
+-- map
+-- list_iterator
+-- stderrprinter
+-- reversed
+-- method_descriptor
+-- code
+-- weakproxy
+-- int
+-- bool
+-- ellipsis
+-- module
+-- dict_items
+-- odict_items
+-- bytearray_iterator
+-- Struct
+-- moduledef
+-- filter
+-- staticmethod
+-- tuple
+-- frozenset
+-- managedbuffer
+-- coroutine_wrapper
+-- function
+-- builtin_function_or_method
+-- odict_iterator
+-- float
+-- range
+-- super
+-- dict_keys
+-- odict_keys
+-- list_reverseiterator
+-- bytes_iterator
+-- member_descriptor
+-- wrapper_descriptor
+-- property
+-- instancemethod
+-- zip
+-- weakref
+-- slice
+-- longrange_iterator
+-- dict_valueiterator
+-- EncodingMap
+-- callable_iterator
+-- mappingproxy
+-- BaseException
+-- Exception
+-- TypeError
+-- StopAsyncIteration
+-- SyntaxError
+-- IndentationError
+-- TabError
+-- AttributeError
+-- AssertionError
+-- StopIteration
+-- MemoryError
+-- BufferError
+-- NameError
+-- UnboundLocalError
+-- LookupError
+-- IndexError
+-- KeyError
+-- EOFError
+-- ImportError
+-- ValueError
+-- UnicodeError
+-- UnicodeEncodeError
+-- UnicodeDecodeError
+-- UnicodeTranslateError
+-- RuntimeError
+-- RecursionError
+-- NotImplementedError
+-- SystemError
+-- Warning
+-- UserWarning
+-- DeprecationWarning
+-- BytesWarning
+-- SyntaxWarning
+-- PendingDeprecationWarning
+-- FutureWarning
+-- ResourceWarning
+-- ImportWarning
+-- RuntimeWarning
+-- UnicodeWarning
+-- ReferenceError
+-- OSError
+-- ConnectionError
+-- BrokenPipeError
+-- ConnectionAbortedError
+-- ConnectionRefusedError
+-- ConnectionResetError
+-- BlockingIOError
+-- NotADirectoryError
+-- PermissionError
+-- FileExistsError
+-- TimeoutError
+-- IsADirectoryError
+-- InterruptedError
+-- ProcessLookupError
+-- FileNotFoundError
+-- ChildProcessError
+-- ArithmeticError
+-- FloatingPointError
+-- OverflowError
+-- ZeroDivisionError
+-- GeneratorExit
+-- KeyboardInterrupt
+-- SystemExit
+-- dict_itemiterator
+-- classmethod
+-- NotImplementedType
+-- iterator
+-- bytes
+-- enumerate
+-- classmethod_descriptor
+-- complex
+-- traceback
+-- weakcallableproxy
Note how bool is a child type of the int type.
In Python 2.7.12, I found that there are 60 builtin types in the tree:
object
+-- type
+-- weakref
+-- weakcallableproxy
+-- weakproxy
+-- int
+-- bool
+-- basestring
+-- str
+-- unicode
+-- bytearray
+-- list
+-- NoneType
+-- NotImplementedType
+-- traceback
+-- super
+-- xrange
+-- dict
+-- set
+-- slice
+-- staticmethod
+-- complex
+-- float
+-- buffer
+-- long
+-- frozenset
+-- property
+-- memoryview
+-- tuple
+-- enumerate
+-- reversed
+-- code
+-- frame
+-- builtin_function_or_method
+-- instancemethod
+-- function
+-- classobj
+-- dictproxy
+-- generator
+-- getset_descriptor
+-- wrapper_descriptor
+-- instance
+-- ellipsis
+-- member_descriptor
+-- file
+-- PyCapsule
+-- cell
+-- callable-iterator
+-- iterator
+-- EncodingMap
+-- fieldnameiterator
+-- formatteriterator
+-- module
+-- classmethod
+-- dict_keys
+-- dict_items
+-- dict_values
+-- deque_iterator
+-- deque_reverse_iterator
+-- Struct
Note how str and unicode are child types of the basestring type. Also observe how this differs from Python 3 builtin types.
Also notice how in Python 2 the exception types are not builtin types.
$ sudo pip install yamlordereddictloader
import yaml
import yamlordereddictloader
with open("foobar.yaml") as f:
yaml_data = yaml.load(f, Loader=yamlordereddictloader.Loader)
This returns the data in the YAML file as a combination of lists and OrderedDict (instead of dict). So, almost all of the rest of the your code should work the same as before after this change.
Tried with: yamlordereddictloader 0.4 and Ubuntu 16.04
]]>Installing a package with Pip on a new computer gave this error:
ImportError: No module named 'setuptools'
For Python2:
$ sudo apt install python-setuptools
For Python3:
$ sudo apt install python3-setuptools
Tried with: Ubuntu 16.04
]]># Make json.dump() encode floats with 5 places of precision import json json.encoder.FLOAT_REPR = lambda x: format(x, '.5f')
Reference: https://stackoverflow.com/questions/1447287
]]>
>>> d = { 1:"cat", 2:"rat" }
>>> d[1]
'cat'
However, this interface is not very friendly if you lookup a key that does not exist. In such a case, it throws a KeyError exception:
>>> d[3] KeyError: 3
Python dictionary provides a get method that is safer, it returns a None value if the key is not present:
>>> print(d.get(3)) None
This method is actually cooler than it looks cause you can make it return any default value you want when the key is not present in the dictionary. You do this by passing the default value as the second argument:
>>> print(d.get(1, "elephant")) cat >>> print(d.get(3, "elephant")) elephant
In many cases, we might have the key in the dictionary, but its value is set to some default value like None or empty string or empty list or empty dict or such values. But at the point we are picking values from keys assume we want such default-valued keys to return a different default value. The trick is that since such default values default to False in Python, we can use that to our advantage.
For example, say the dictionary is already created and not under our control. But, whenever I read values from it, I want elephant if the key does not exist or if the value is a default value that evaluates to False. It gives rise to an elegant Python idiom using get method and or operator:
>>> d = { 1:"cat", 2:"rat", 3:None, 4:"" }
>>> v = d.get(1) or "elephant" ; print(v)
cat
>>> v = d.get(3) or "elephant" ; print(v)
elephant
>>> v = d.get(4) or "elephant" ; print(v)
elephant
>>> v = d.get(99) or "elephant" ; print(v)
elephant
]]>