Posts

Showing posts with the label python

Install psycopg2 on a Greenplum system

If you try to install psycopg2 on a Greenplum system using pip you may run into this error: ... Error: pg_config executable not found.       Please add the directory containing pg_config to the PATH     or specify the full executable path with the option:         python setup.py build_ext --pg-config /path/to/pg_config build ...     or with the pg_config option in 'setup.cfg'. ... The solution is to add the path to the Greenplum bin directory to your environment PATH. export PATH=$PATH:/path/to/greenplum-db/bin/ Then run pip install psycopg2 and all should be good.

Get path to current script in Python

import os os . path . dirname ( os . path . realpath ( __file__ ))   Source

Python string formatting in print statements

#!/usr/bin/env python # Format using a dictionary print '%(language)s has %(number)03d quote types.' % { "language" : "Python" , "number" : 2 } # Format using a list print '%s has %03d quote types.' % ( "Python" , 2 ) # Best method print '{} has {} quote types.'.format ( "Python" , 2 ) Planning to post more code snippets this year to remind me of how to do various things in Python. Source: Pyformat

Python quick bits

Just random Python quick bits to jar my memory List mylist=['item1','item2','3'] Index starts at 0 for myitem in mylist:  code here methods: append() pop() extend() # append list of items ['item4','item5'] insert(position,"item6)remote("item2") Dictionary mydictionary={'tag1':'item1','tag2':'item2'} Reference items by keys for mykey in mydictionary.keys()  code here Object types in Python: int, dict, list, str Testing for object type:  isinstance(variable,type)  var1='foo'   isinstance(var1,str) User input from the command line:  import sys  variable=raw_input("Enter user question here: ") Length of list: len(list) Strings To work with strings: import string mystring.split() # default delimeter is spaces with multiple spaces treated as one for item in list:   do something with item for piece in string:   do somethign with piece Working ...