Project:Sandbox

asdfasd

Python
How to find out where an 'import' is coming from import re      # re isn't needed; it's just being used as an example print str( re.__file__ ) How to accomplish qualified/relative imports
 * First, make sure the higher-level directory location, i.e. the point of origin of the relative import, is included in the Python path. SECOND: don't forget that every step in the directory path MUST have an __init__.py file present

How to, on Ubuntu, install a specific python version apt-get update; apt-get install python2.7 How to add new libraries and/or Eggs to a project in Eclipse
 * project properties ... PyDev PYTHONPATH

Why can't I find md5 in Python 3?
 * md5 has been made a part of hashlib in Python 3

Why can't I find urllib2 in Python 3?
 * The urllib2 module has been split across several modules named urllib.request and urllib.error, in Python 3

How can I use setuptools and ez_setup in Python 3?
 * setuptools and ez_setup were replaced by distribute and pip, but now it looks like setuptools is back in the saddle
 * http://pypi.python.org/pypi/distribute#disclaimers
 * http://www.pip-installer.org/en/latest/index.html
 * https://pypi.python.org/pypi/distribute/0.6.49
 * The purpose of ez_setup is to install setuptools for the correct version of Python or to bring setuptools up to date, not to install anything else
 * https://pypi.python.org/pypi/setuptools/13.0.2

What benefits does setuptools provide?
 * setuptools is an umbrella term, it does not install anything itself.
 * setuptools puts easy_install in the Scripts sub-directory and easy_install does directly install packages.
 * It appears that easy_install can be given a URL to access (with the -f option) to help it find the target package.
 * easy_install can also install packages from a file >> easy_install.exe ddt-1.0.0.tar.gz

'''What about pip? What does pip do?
 * Pip does not seem to complement or work with easy_install, it seems to offer an alternative to easy_install.
 * Not certain from whence pip came before, but now pip comes installed with Python 3.4 or later.
 * Pip is heavily dependent on the Python Packaging Index: if a package is not in the index, pip is most likely helpless.
 * Syntax >> python -m pip install SomePackage
 * Pip can also be used to upgrade a previously installed package >> python -m pip install --upgrade SomePackage
 * easy_install can probably serve just as well, given only a 1-word package name, if it's in the Python Packaging Index.

What's the difference, in Python, between a statement and an expression?
 * A statement does not return a value. An expression does.
 * BTW, assignment does not return a value (not even None), so it's a statement.
 * This has a strong implication for Lambda: only expressions are allowed because statements do not return a value.

Which xxxx-packages thingee is old and which is new?
 * NEW: site-packages ?
 * OLD: dist-packages ?

How to execute a Unix command in Python and capture the results cmnd = 'isql -SGARINT -Uopc70_dbo -Pint_opc70_dbo -Dopc70 -i sql01.txt' outp1 = os.popen( cmnd ) outp2 = outp1.read outp1 = string.split( outp2, '\n' ) for line in outp1: print line How to tell from within a script what path is in effect (usually meaning PYTHONPATH, but not if Jython) import sys print str( sys.path )          # below is better print ':'.join(sys.path)       # supposedly one can alter this dynamically How to “push” on to a list since there is no push command (note there is a ‘pop’) myList.insert( 0, newEntry )          # inserts before the given position How to check if occurrence in Dictionary
 * bad way:  if ‘val’ in myDict.keys:
 * good way: if myDict.has_key( ‘val’ ):

How to do a dictionary lookup and handle missing key with a default value dict.get( key, default='error' )             # or dict.get( key, default=None ) How to uppercase just the first letter, lowercase the rest, or uppercase first letter of every word
 * use s.capitalize or s.title (but not string.title(text) )

How to count occurrences of something in a string xCtr = string.count( textLine, 'x ' ) New "keys-sort-view" issue to be aware of in latest versions of Python
 * this no longer works: kys = dctn.keys; kys.sort.
 * Use kys = sorted(dctn.keys) instead (because a view is returned, not a list).
 * http://docs.python.org/release/3.0.1/whatsnew/3.0.html

How to tell from within a script what version of Python is running print str( sys.version_info ) What's the best Python package to access Postgresql?
 * psycopg2

'''Is __init__ a genuine constructor?
 * __init__ is called immediately after an instance of the class is created. It would be tempting but incorrect to call this the constructor of the class. It's tempting, because it looks like a constructor (by convention, __init__ is the first method defined for the class), acts like one (it's the first piece of code executed in a newly created instance of the class), and even sounds like one (“init” certainly suggests a constructor-ish nature). Incorrect, because the object has already been constructed by the time __init__ is called, and you already have a valid reference to the new instance of the class. But __init__ is the closest thing you're going to get to a constructor in Python, and it fills much the same role. From:
 * http://www.diveintopython.net/object_oriented_framework/defining_classes.html

How to list all methods or functions within a package or class? dir(theName) What's the difference between repr(thingee) and str(thingee)? not sure; I'd better find out! What should I do if any install process says it can't find pg-config?? sudo apt-get install libpq-dev python-dev What Python directories should be soft-linked to decrease confusion? /usr/lib/python2.7 → /usr/local/lib/python2.7 /usr/lib/python → /usr/local/lib/python2.7 /usr/lib/pymodules/python2.7 → /usr/lib/python2.7/lib-dynload   # note name change! How to do a custom (class-object) sort in Python
 * 1) /usr/lib/pyshared → /usr/share/pyshared   # no don't, the contents are different...
 * 2) /usr/lib/pyshared/python2.7 → /usr/lib/python2.7/dist-packages    # not so sure about this one; this is going a bit too far
 * grep for 'comparator' in the source code. Yes, a stupid farm-boy actually figured it out, eventually.
 * Comparator must be in the sorting script, not the object being sorted.
 * It must return 1, 0 or -1. Don't try: return one.weight < two.weight because that yields True/False, which won't work.

def comparator( self, one, two ): return one.weight – two.weight How to list all keywords for Python import keyword keyword.kwlist How to generate pyDoc
 * I was only able to do this by co-locating the file pydoc.py in the same directory
 * as the modules to be documented, and then, at the command line, running:

python pydoc.py MyCustClass          # without .py!! For HTML use -w: python pydoc.py -w MyCustClass How to build a new version of Python on Unix without root permissions 
 * find out which sysconfig.py runs and change the values of PREFIX and EXEC_PREFIX to point to your home directory.
 * Then:
 * python setup.py install --home=~    (after configure, make, etc.)

How to "push" on to a list since there is no push command (note there is a ‘pop’) myList.insert( 0, newEntry )          # inserts before the given position '''How can an item be randomly selected from a list? Do *NOT* use my homemade solution pickRandomFrom!!!!!!! Rather do this: random.choice(myList) '''How to uppercase just the first letter, lowercase the rest, or uppercase first letter of every word
 * use s.capitalize or s.title (but not string.title(text) )

How to count occurrences of something in a string xCtr = string.count( textLine, 'x ' ) What should I do if I need Unicode support, and specifically Unicode with width 4?
 * possible error message: ImportError: /usr/lib/python2.7/lib-dynload/operator.so: undefined symbol: _PyUnicodeUCS2_AsDefaultEncodedString

rebuild starting with ./configure --enable-unicode=ucs4 How to install Python without root access configure, make, then: make install DESTDIR=/home/userid/pydir Does a child class or sub-class, when instantiated, automatically call __init__ for the parent class?
 * No, it does not, this must be done explicitly by calling super.__init__

Toolserver:Database access