Tuesday, January 31, 2006

NotesHTMLtags

from

HTML 4.01 / XHTML 1.0 Reference

NotesPython

Contents
=========
References
Style Guide
>Doxygen
Introduction
Compile
Tkinter - Python's GUI
Syntax
Data Types
Umask and file creation
Operators
Strings 
Lists
Collections - defaultdict
Math, Array, builtin functions
Flow Control
Functions, Modules, Append path
>Arguments
Global vars
Classes 
>Inheritance
>classmethod, staticmethod
Exceptions
>Passing Exceptions to outer functions
>Exception v2 vs v3
>Signals
Differences between Python 2.x vs Pyhon 3.x
>Exceptions
Importing 
File I/O
Writing CSV file
OS FileSystem and shell commands
>subprocess
Commands
Command line Options
Octo.py
Unix Script
Print
Check Text Format Newline CLRF
WARNINGS
<more complex things>
Sourcing Script within a python file
Python openedx
iPython /Jupyter
Symbolic Math Python
Printing Math Formula
Installing Packages
>Packages for Machine Learning
Zipfile and Datetime
PyCharm Editor
Python Logging module
Installing a Second Python
Python Package Manager - Setuptools and pip
Isolate Python Environment using virtualenv
@ Declarators
Python uploading to Gdrive
Python Writing Blog 
Troubleshooting
Miscellaneous
UnitTest
umask in python
Coverage
Codacy
Security - Bandit




References
===========
http://www.poromenos.org/tutorials/python
http://docs.python.org/tutorial/index.html
http://docs.python.org/py3k/tutorial/index.html
http://infohost.nmt.edu/tcc/help/pubs/tkinter/minimal-app.html - Tkinkter reference
http://www.tutorialspoint.com/python/python_gui_programming.htm
http://www.dreamsyssoft.com/python-scripting-tutorial/shell-tutorial.php

Library Ref: http://docs.python.org/2.7/library/
Language Ref: http://docs.python.org/2.7/reference/index.html

Style Guide
=============
https://www.python.org/dev/peps/pep-0008/



Example of docstring - using Triple quotes 

"""Example Google style docstrings.

This module demonstrates documentation as specified by the `Google Python 
Style Guide`_. Docstrings may extend over multiple lines. Sections are created
with a section header and a colon followed by a block of indented text.

Example:
    Examples can be given using either the ``Example`` or ``Examples``
    sections. Sections support any reStructuredText formatting, including
    literal blocks::

        $ python example_google.py

Section breaks are created by resuming unindented text. Section breaks
are also implicitly created anytime a new section starts.

Attributes:
    module_level_variable1 (int): Module level variables may be documented in
        either the ``Attributes`` section of the module docstring, or in an
        inline docstring immediately following the variable.

        Either form is acceptable, but the two should not be mixed. Choose
        one convention to document module level variables and be consistent
        with it.

.. _Google Python Style Guide:
   http://google.github.io/styleguide/pyguide.html
"""


>Doxygen
============
To use Doxygen, the python code must be like the following:
<code>
1 """@package docstring
2 Documentation for this module.
3 
4 More details.
5 """
6 
7 def func():
8  """Documentation for a function.
9 
10  More details.
11  """
12  pass
13 
14 class PyClass:
15  """Documentation for a class.
16 
17  More details.
18  """
19 
20  def __init__(self):
21  """The constructor."""
22  self._memVar = 0;
23 
24  def PyMethod(self):
25  """Documentation for a method."""
26  pass
</code>









Introduction
=============
Phython - What is it?
 - interpreted (no compilation)
 - interactive
 - object-oriented
 - compared with Tcl, Perl, Scheme, Java

Python - attributes
 - power and clear syntax
 - has modules, classes, exceptions, dynamic data types
 - interfaces to system calls, libraries,
 - interfaces to windowing systems X11, Motif, Tk, Mac, MFC

 - built-in modules can be written in C/C++
 - useable as an extension language for applications that need a programming interface.

Running Python
- run python script file *.py by:
   python.exe <filename>
... any file I/O assumes files are in the same directory.
- run python interactively using the GUI or Text console.
- run Python with Enthought Canopy editor
... any file I/O assumes files are in C:\users
... use os.getcwd() to find out current working dir
... use os.chdir(r'...') to change working directory
- run python without saying python:
eg file is pyVersion.py
--------------------
#/opt/pbs/default/python/bin/python
import sys
print (sys.version)
-------------------
To run this, just type:  ./pyVersion.py


Modules
- module file has python definitions and statements and called <modfile>.py
- to include this module to use in script or interactively:
    import <modfile>
- to use any functions, say foo, in <modfile>.py,
    <modfile>.foo
- to import specific functions only
    from <modfile> import foo
- to import everything
    from <modfile> import *

Run module file
    python <modfile>.py <arguments>

the module code has to include the following at the end 
----
if __name__ == "__main__":
    import sys
    fib(int(sys.argv[1]))
----
you can make the file usable as a script as well as an importable module, because the code that parses the command line only runs if the module is executed as the “main” file:
So the example above will run the fib() function in <modfile>.py with arguments sys.argv[1] using the <arguments>


The Python interpreter reads a source file, and executes all of the code found in it. It will define a few special variables, eg __name__. If the python interpreter is running that module *.py as the main program, it sets __name__ to be "__main__". If this file is being imported from another module, __name__ will be set to the module's name.

The reasons for doing this is that sometimes you write a module (a .py file) where it can be executed directly. Additionally, it can also be imported and used in another module. By checking <if __name__ == "__main__":>, that code only execute when the module is run as a program and not run when the module is being imported into another code.


Run python script file (this is like module since it can contain function defs, but also have python executable statements)
 %run <filename>.py

B=A .....B is not a copy. It is a reference.


fileinput package:
----------
import fileinput
for line in file.input():
    print line

This will print out the lines of the files (eg data.txt) given to this python script. It can be run in TWO ways:
i) ./myprogram.py  data.txt
ii) ./myprogram.py < data.txt      (file indirection)




Compile
===========
$ python
>>> import py_compile
>>> py_compile.compile('server.py')
>>> ^D
$ python ./server.pyc




Tkinter - Python's GUI
======================

1. To use Tkinter, need
       from Tkinter import *

2. Set-up Root Window: 
 Tk()                           ### create Top-level window
 Tk.mainloop                    ### create Top-level window()

example:
  root = Tk()           ### create Top-level window
  myapp = MyApp(root)
  root.mainloop()  ### starts the EVENTLOOP to  listen to events
  root.destroy()        ### closes and exits this window called root


3. Containers:
 Frame(parent)                   ### starts a frame belonging to Parent
 Frame.pack()                    ### display the frame , geometry manager
 Frame.grid()                    ### geometry manager
 Frame.place()                   ### geometry manager



example:
   set up containers using:
       container1 = Frame(parent)

   ... where parent is the widget container1 belongs to.

   displaying containers, use:
       container1.pack()

   other geometry managers besides PACK, are GRID and PLACE


4. Buttons:
 Button(parent, attr=value,)        ### starts buttons and sets attributes
 Button.pack()                      ### displays the Button
 Button.configure(attr=value, ...)  ### sets some attributes
 Button["attr"]="value"             ### sets some attributes

   where attr = { text, background }


example:
   set up buttons:
       button1 = Button(parentContainer)

   display buttons:
       button1.pack()


5. Event Handling - i) Event Binding  ii) Command Binding
   Syntax:
 widget.bind( self, event, event_type_name, event_handler_name )

   event_type_name = { "<Button-1>", ....
 
   

Syntax
=========
Comments - # sign
Indentation - to specify blocks
Indentation - expected when statement end in colon ":" 
Line Continuation -  \



Data Types
===========
List, tuples, dictionaries, sets (sets library)
First (N items) array item index is 0, or -N
Last (N items) array item index is N-1 or -1

 List - 1D arrays, can also have List of Lists
Eg. list[:] - indexing single element
lstSample = [1, ["another", "list"], ("a", "tuple")]
lstList = ["List item 1", 2, 3.14]
lstList[0] = "List item 1 again"
lstList[-1] = 3.14
lstList[-2] =2
lstList[-3] = "List item 1 again"

Eg. list[i:j] - indexing / SLICING a sequence
think of the following sequence, where index are at the edge on in the middle.
0             1     2            3    -> Counter
  List Item 1    2    3.14000000      -> element sequence

>>> print lstList[:]          -> everything
['List item 1', 2, 3.1400000000000001]
>>> print lstList[0:2]        -> from right of index 0 to before index 2
['List item 1', 2]
>>> print lstList[-3:-1]      -> from right of index -3 to before index -1
['List item 1', 2]
>>> print lstList[1:]         -> from right of index 1 to the end
[2, 3.14]

Eg. list[i;j;k] - sequence with steps
>>> "hello"[::-1]             -> the whole sequence but with backward (-1) step
"olleh"


Eg. List Manipulation
lst1 = [1, 2, 3]
lst2 = [3, 4, 5]
>>> print [x * y for x in lst1 for y in lst2]
[3, 4, 5, 6, 8, 10, 9, 12, 15]
>>> print [x for x in lst1 if 4 > x > 1]
[2, 3]


 Tuples - immutable 1D array
Eg
tplTuple = (1, 2, 3)


 Dictionaries - hash tables or associative arrays
dicDictionary = {"Key 1": "Value 1", 2: 3, "pi": 3.14}
dicDictionary["pi"] = 3.15

Adding to dictionary:
default_data['item3'] = 3
default_data.update({'item4': 4, 'item5': 5})

Deleting item from dictionary
del dicDictionary["pi"]      # this will delete the "pi" key-value pair

dictionary can be intersected with list - see Lists section


Oct vs Int:
Python 2.6+
>>> oct(9)
'011'
>>> int(0o010)
8
>>> int(0o011)
9

Python 2.5-
>>> oct(9)
'011'
>>> int('010',8)
8
>>> int('011',8)
9


Umask and file creation
=============================
https://stackoverflow.com/questions/10291131/how-to-use-os-umask-in-python

os.stat
https://stackoverflow.com/questions/5337070/how-can-i-get-a-files-permission-mask
http://effbot.org/librarybook/stat.htm
 
https://stackoverflow.com/questions/5624359/write-file-with-specific-permissions-in-python

>>> import os
>>> os.getcwd()
'/home/chee/dev/Kyoto/tmp'
# Using default umask
>>> ff=open('pumask.txt','w')  
>>> ff.close()
>>> umask0002=os.umask(0066)
>>> print umask0002
2
# Using the 066 umask
>>> ff=open('pmask0066.txt','w')
>>> ff.close()
>>> umask0066=os.umask(umask0002)
>>> print umask0066
54
# using the reverted umask
>>> ff=open('pmask0002a.txt','w')
>>> ff.close()

-rw-rw-r--  1 chee chee       0 Dec 22 01:08 pumask.txt
-rw-------  1 chee chee       0 Dec 22 01:37 pmask0066.txt
-rw-rw-r--  1 chee chee       0 Dec 22 01:49 pmask0002a.txt


Or using os.open
------

import os
fd = os.open('/path/to/file', os.O_WRONLY, 0o600)
myFileObject = os.fdopen(fd)
myFileObject.write(...)
myFileObject.close()


Operators
==========
=  assignment
+=  add numbers or strings
-=  subtract
==  equality test
1 < a < 3 checks that a is both less than 3 and more than 1
del  delete|remove|clear|nullify variables, array elements or arrays


conditional operators:
==
!=
<>
>
<
>=
<=

Logical operators:
and
or
not = negate operator


 Variable Name assignment
Eg. 
Listt =[1,2]
fnVariable = len         # len is length function
print fnVariable(Listt)  # prints length of Listt



Strings 
=========
"blah" or 'blah' or "this 'is' blah" or 'this "is" blah'
''' this is 
multi-line string'''
"""this is 
multi-line string"""

unicode -> u"This is unicode string"


Find / Replace in Strings:
sout = "blah blah job_ID"
ii = sout.find("job_ID", beg=0, end=len(string))
-> returns index position of text found, or -1 if text not found.

sout.replace("$JOB_ID", jid_num)
-> replaces any "$JOB_ID" with string in jid_num


Value in Strings
eg. print "Name: %s\nNumber: %s\nString: %s" % (class.name, 3, 3 * "-")
Name: Poromenos
Number: 3
String: ---

Parse string:
In [8]: lhs, rhs = "2.7.0_bf4fda703454".split("_", 1)
In [9]: lhs
Out[9]: '2.7.0'
In [10]: rhs
Out[10]: 'bf4fda703454'


>>> "2.7.0_bf4fda703454".partition('_')
('2.7.0', '_', 'bf4fda703454')

>>> "shazam".partition("_")
('shazam', '', '')


"This is an example of \n\
   end of line string. But last one is \
   like that"

Substrings
   wordd = 'Hello World"
   wordd[0:2]        - 'Hel'
   wordd[:2]         - first two characters
   wordd[2:]         - from the first two to the last character.
   '<'+wordd*5+'>'   - concatenate with +, also repeat 5 times with *5.

   word = 'HelpA'
   word[-1]    'A'     - from the right end
   word[-2]    'p'
   word[-2:]   'pA'
   word[:-2]  'Hel'

Literal strings:   r'hello\world'   - treats the slash as a slash character, not something else.


Arguments in string:
sentence = "Hello {0}"
print sentence.format("Ted")    -> "Hello Ted"
print "Hello {0}".format("Ted")     -> "Hello Ted"
people="Ben"
place="Barn"
sentence = "I went .. {0} with {1}".format(people. placce)

String functions:
sentence.capitalize()
sentence.lower()
sentence.upper()
sentence.swapcase()
sentence.title()
sentence.count()
sentence.endswith()
sentence.isalnum()
sentence.isalpha()
sentence.isdigit()
sentence.islower()
sentence.isupper()
sentence.isspace()
sentence.lstrip()
sentence.rstrip()
sentence.strip()
sentence.replace()
sentence.split()
sentence.join()




Lists
======
a = ['spam', 'eggs', 100, 1234]
Indices start from 0, like strings
list.append()        - adds element to the end of list
list.pop()           - remove last element from list
list.extend()           - add last element to list

To have first in - first out, need deque:
    from collections import deque
the following is how to use the deque class.
myQ = deque([1,2,3])
myQ.append()         - adds element to the end of list
myQ.popleft()        - removes first element from the list.

mylist[-1]           - gives the last item on the list
mylist.index("blah") - returns index
"z" in my_list       - returns T/F to check if element is in a list
mylist[0:2]          - returns [0],[1] BUT NOT including [2] element
mylist.sort()
mylist.max()
mylist.min()
for item in mylist:  - looping over list elements

Prepend
a=['123','456']
a[:0] = ['abc']   This will Prepend 'abc' to a. Warning 'abc' MUST be in a list.

Intersection or Filtering both lists:
>>> adict = {"a": 1, "b":2, "c":3, "d":4, "e":5}    # define dictionary
>>> alist = ["a", "c", "z"]                         # define list
>>> [eee for eee in adict if eee in alist]          # results is the intersection of dictionary keys and list 
['a', 'c']
>>> for aaa in [eee for eee in adict if eee in alist]:
...    print aaa
...
a
c
#### results of looping over the intersection or list.

Flatten a list:
'\n'.join(myList)


Collections - defaultdict
===========================

>>> s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
>>> d = defaultdict(list)
>>> for k, v in s:
...     d[k].append(v)
...
>>> d.items()
[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]



Math, Array, builtin functions
================================


from the numpy library: import numpy
numpy.empty_like(a, dt, ...)    - make an empty array with same shape and size as a
numpy.ndarray                   - multidimensional homogenous array

Array(x:y) does not include y

mod and modulo (remainder) function
math.fmod(x,y)   -> x - n*y
a % b




Flow Control
==================
for .....:
 ...
elif:
   ....
else:
 ....

break      - break out of the for loop
pass       - do nothing
continue   - to skip remaining logic and go to the beginning next iteration


 
 For Loop using Range
lstRange = range(10)
for intNumber in lstRange:
    if intNumber in (3, 4, 7, 9):
        break
  elif intNumber == 5:
   continue
    else:
        pass # Do nothing
 
For loop using indices is specied using: range(1, n+1).
This will iterate from 1 until n. NOTE: the last integer is not iterated. Eg.
  for ii in range(1, numQuestions+1):

 If Loop
if lstRange[1] == 2:
    print "1 == 2"
elif lstRange[2] == 3:
    print "3 == 4"
else:
    print "Dunno"
 
 While Loop
while lstRange[1] == 1:
    pass


Functions, Modules, Append path
=====================================

 Normal Functions
# arg2 and arg3 are optional, they have default values
# if one is not passed (100 and "test", respectively).
def fnMyFunction(arg1, arg2 = 100, arg3 = "test"):
    return arg3, arg2, arg1
 
ret1, ret2, ret3 = fnMyFunction("Argument 1", arg3 = "Named argument")
 
- use "def" keyword to declare functions
- Optional arguments must have default values and listed after mandatory arguments
- Named argument is assigned a value
- can return a tuple of values


 Lambda Functions 
fnVariable = lambda x: x + 1
>>> print fnVariable(1)
2

a = ['spam', 'eggs', '100m', '1234']
b = ['m','s']
bFile = lambda x,y : x.endswith(y)
>>> bFile('spam', 'm')
True
>>> print map(lambda z: bFile('spam',z), b)
[True, False]
>>> print any(map(lambda z: bFile('spam',z), b))
True

Select elements of a list from another list
>>> rur_taskstats_keys = ["bkiowait", "etime", "minfault", "wcalls", "stime", "btime", "pgswapcnt", "abortinfo", "max_vm", "rcalls", "max_rss", "coremem", "majfault", "wchar", "utime", "exitcode", "rchar", "comm", "core", "ecode", "gid", "jid", "nice", "taskstats_nid", "pid", "pjid", "ppid", "prid", "rss", "sched", "uid","exitcode_signal"]
>>> rdef =["jid", "pid", "boof"]
>>> filter( (lambda x: x in rur_taskstats_keys), rdef)
['jid', 'pid']

Modules
- functions are placed *.py files to be able to be used elsewhere - these files are called modules.
- to use the functions defined in modules, say file is called func.py, then import using:
     import func
- python will search through the module search path.
- to determine the module search path, type:
    os.sys.path
- to append to the module search path:
     import sys,os,os.path
     sys.path.append(os.path.expanduser('~/Documents/My_Code'))
- NOT recommended to change PYTHONPATH.
- to automatically include the customised path, put the above sys.path.append(....) into a file like 00-start.py in this directory:
   ~user/ipython/profile_default/startup/00-start.py

>Arguments
============
THREE different forms of args are shown here:
def person(name, *args, **kwargs):
 print name
 for i in args:
  print i
 for key, val in kwargs.iteritems():      # each kwarg is a TUPLE (k,v), if no iteritems() then it will print just key
  print key, val

person("Jimmy", 1,2,3, gender="Male")
-- output--
Jimmy
1
2
3
gender Male
----------




Global vars
=============
To make use of global var, just need to declare that variable in all functions that use it, eg:
----------------------
globvar = 0

def set_globvar_to_one():
    global globvar    # Needed to modify global copy of globvar
    globvar = 1

def print_globvar():
    print globvar  



Classes 
=========
Eg. 
class MyClass:   #OLD STYLE Python 2.x
class MyClass(object):   #NEW STYLE Python 2.x
#http://docs.python.org/release/2.2.3/whatsnew/sect-rellinks.html
    def __init__(self):
        self.varMyVariable = 3
    def fnMyFunction(self, arg1, arg2):
        return self.varMyVariable
 
    # This is the class instantiation
>>> clsInstance = MyClass()
>>> clsInstance.fnMyFunction(1, 2)
3

>Inheritance
==============
# This class inherits from MyClass. 
class OtherClass(MyClass):
    def __init__(self, arg1):
        self.varMyVariable = 3
        print arg1
 
>>> clsInstance = OtherClass("hello")
hello
>>> clsInstance.fnMyFunction(1, 2)
3
# This class doesn't have a .test member, but
# we can add one to the instance anyway. Note
# that this will only be a member of clsInstance.
>>> clsInstance.test = 10
>>> clsInstance.test
10


- Multiple inheritance is declared as:  class OtherClass(MyClass1, MyClass2, MyClassN)
- Private variables and methods names have double underscore "__"
- Dynamically add new member to class instance; eg. clsInstance.test


To call on the the parent's methods:
--------
class ChildB(Base):
    def __init__(self):
        super(ChildB, self).__init__()


classmethod, staticmethod
=============================
Ref: https://stackoverflow.com/questions/12179271/python-classmethod-and-staticmethod-for-beginner#12179752
https://johnsofteng.wordpress.com/2010/01/19/staticmethod-vs-classmethod-in-python/

Say function a() is defined in Parent Class, while Sub Class extends Parent Class

If function a() has @staticmethod decorator, Sub.a() still refers to definition inside Parent Class. Whereas,

If function a() has @classmethod decorator, Sub.a() will points definition inside Sub Class.

Let’s talk about some definitions here:

@staticmethod function is nothing more than a function defined inside a class. It is callable without instantiating the class first. It’s definition is immutable via inheritance.

@classmethod function also callable without instantiating the class, but its definition follows Sub class, not Parent class, via inheritance. That’s because the first argument for @classmethod function must always be cls (class).

Example:
############
class Date(object):
    day = 0
    month = 0
    year = 0
    def __init__(self, day=0, month=0, year=0):
        self.day = day
        self.month = month
        self.year = year

@classmethod
    def from_string(cls, date_as_string):
        day, month, year = map(int, date_as_string.split('-'))
        date1 = cls(day, month, year)
        return date1
@staticmethod
    def is_date_valid(date_as_string):
        day, month, year = map(int, date_as_string.split('-'))
        return day <= 31 and month <= 12 and year <= 3999
############
    # usage:

day, month, year = map(int, string_date.split('-'))
date1 = Date(day, month, year)                # using basic way
date2 = Date.from_string('11-09-2012')        # using class method
is_date = Date.is_date_valid('11-09-2012')    # using static method



Exceptions
===========
def fnExcept():
    try:
        # Division by zero raises an exception
        10 / 0
    except ZeroDivisionError:
        print "Oops, invalid."


https://www.python.org/doc/essays/stdexceptions/
http://www.tutorialspoint.com/python/python_exceptions.htm
https://docs.python.org/2/library/errno.html


*** Principle is: if error is caught, then that is the end of the chain. 
To propagate the error, need to keep raising it in the exception.
---------------
import sys
def bye():
    sys.exit(40)                 # Crucial error: abort now!
try:
    bye()
except:
    print('got it')              # Oops--we ignored the exit
print('continuing...')

% python exiter.py
got it
continuing...
--------------

sys.exit(statuscode) is used to allow termination. This will raise a SystemExit exception. 

Do not use empty "except:" like above, because it will catch everything. Exceptions that are raised elsewhere will also be captured here. This may confuse the real cause of the problem.
The example above shows the system tries to exit, but that was caught by mistake and results in the program continuing.

To solve above problem, one can have:  
    except Exception:
the Exception is a superclass that does not include SystemExit so will not catch it. However other exceptions can still be caught unintentionally.

- these will let the program keep running
sys.exit(1)
sys._exit(1)
os.exit(1)
- this will stop the pogram immediately
os._exit(1)

sys.exit() is identical to "raise SystemExit()"
os._exit calls the C function _exit() and deos not do any clean up to flush buffers or clear handlers.

---


Defining Exceptions

class DeviceError(Exception):
 def __init__(self,errno,msg):
  self.args = (errno, msg)
  self.errno = errno
  self.errmsg = msg

# Raises an exception (multiple arguments)
raise DeviceError(1, 'Not Responding')



>Passing Exceptions to outer functions
=========================================

https://docs.python.org/2.7/tutorial/errors.html
>>> try:
...     raise NameError('HiThere')
... except NameError:
...     print 'An exception flew by!'
...     raise
...
An exception flew by!
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
NameError: HiThere


The last except clause may omit the exception name(s), to serve as a wildcard. Use this with extreme caution, since it is easy to mask a real programming error in this way! It can also be used to print an error message and then re-raise the exception (allowing a caller to handle the exception as well):

import sys
try:
    f = open('myfile.txt')
    s = f.readline()
    i = int(s.strip())
except IOError as e:
    print "I/O error({0}): {1}".format(e.errno, e.strerror)
except ValueError:
    print "Could not convert data to an integer."
except:
    print "Unexpected error:", sys.exc_info()[0]
    raise


>Exception v2 vs v3
====================
see section "Differences between Python 2.x vs Pyhon 3.x" -> ">Exceptions"





>Signals
=========
https://docs.python.org/2/library/signal.html
import signal, os

def handler(signum, frame):
    print 'Signal handler called with signal', signum
    raise IOError("Couldn't open device!")

# Set the signal handler and a 5-second alarm
signal.signal(signal.SIGALRM, handler)
signal.alarm(5)

# This open() may hang indefinitely
fd = os.open('/dev/ttyS0', os.O_RDWR)

signal.alarm(0)          # Disable the alarm


https://pymotw.com/2/signal/
$ python signal_getsignal.py
SIGHUP     ( 1): SIG_DFL
SIGINT     ( 2): &lt;built-in function default_int_handler&gt;
SIGQUIT    ( 3): SIG_DFL
SIGILL     ( 4): SIG_DFL
SIGTRAP    ( 5): SIG_DFL
SIGIOT     ( 6): SIG_DFL
SIGEMT     ( 7): SIG_DFL
SIGFPE     ( 8): SIG_DFL
SIGKILL    ( 9): None
SIGBUS     (10): SIG_DFL
SIGSEGV    (11): SIG_DFL
SIGSYS     (12): SIG_DFL
SIGPIPE    (13): SIG_IGN
SIGALRM    (14): &lt;function alarm_received at 0x7c3f0&gt;
SIGTERM    (15): SIG_DFL
SIGURG     (16): SIG_DFL
SIGSTOP    (17): None
SIGTSTP    (18): SIG_DFL
SIGCONT    (19): SIG_DFL
SIGCHLD    (20): SIG_DFL
SIGTTIN    (21): SIG_DFL
SIGTTOU    (22): SIG_DFL
SIGIO      (23): SIG_DFL
SIGXCPU    (24): SIG_DFL
SIGXFSZ    (25): SIG_IGN
SIGVTALRM  (26): SIG_DFL
SIGPROF    (27): SIG_DFL
SIGWINCH   (28): SIG_DFL
SIGINFO    (29): SIG_DFL
SIGUSR1    (30): SIG_DFL
SIGUSR2    (31): SIG_DFL


Differences between Python 2.x vs Pyhon 3.x
===============================================
http://sebastianraschka.com/Articles/2014_python_2_3_key_diff.html

https://docs.python.org/3/howto/pyporting.html

http://python-future.org/compatible_idioms.html


>Exceptions
====================
Python 2
print 'Python', python_version()
try:
    let_us_cause_a_NameError
except NameError, err:
    print err, '--> our error message'

result - Python 2.7.6
name 'let_us_cause_a_NameError' is not defined --> our error message


Python 3
print('Python', python_version())
try:
    let_us_cause_a_NameError
except NameError as err:
    print(err, '--> our error message')

result - Python 3.4.1
name 'let_us_cause_a_NameError' is not defined --> our error message





Importing 
===========
import random            # random library, to use do: random.<function name>
from time import clock   # time library, clock function
from numpy import *      # Dangerous since many libraries have some same function names
import matplotlib.pyplot as plt      # import and alias the function called pyplot

Random Numbers
=================
Python2.7
import random
random.seed()
random.randint(2, 99)   # generates random integers from 2 to 99


File I/O
=========
Eg. File I/O and serializing with "pickle" library

import pickle
lstList = ["This", "is", 4, 13327]
# Open the file C:\file.dat for writing. The letter r before the
# filename string is used to prevent backslash escaping.
flFile = file(r"C:\file.dat", "w")
pickle.dump(lstList, flFile)
flFile.close()
 
flFile = file(r"C:\file.txt", "w")
flFile.write("This is a sample string")
flFile.close()
 
flFile = file(r"C:\file.txt")
>>> print flFile.read()
'This is a sample string'
flFile.close()
 
# Open the file for reading.
flFile = file(r"C:\file.dat")
lstLoaded = pickle.load(flFile)
flFile.close()
>>> print lstLoaded
['This', 'is', 4, 13327]

# read all lines into a vector
flFile = open(r"tree_raw.txt", "r")
vlines = flFile.readlines()
oneLine = vlines[0].rstrip()    - removes the "\n" at the end of the word

# Functional Programming - to strip \n from words
manyWords = ['Applications\n', '-Browsers\n', '-Editors\n', 'Multimedia\n', '-Audio\n', '-Graphics\n', '-Video\n']
cleanWords = [line.rstrip() for line in manyWords]
result is ['Applications', '-Browsers', '-Editors', 'Multimedia', '-Audio', '-Graphics', '-Video']

# read contents of entire file
 f = open(file_name)
    try:
        return f.read()
    finally:
        f.close()

File modes: 
r rb r+ rb+      w wb w+ wb+          a ab a+ ab+ 
b      - for binary format
r,w,a  - for read, write, append
a      - if file exist, move pointer to end of file
w      - if file exist, overwrites
+      - for both reading and writing

Using 'with' keyword
- a construct that is similar to try-catch block, but used with unmanaged resources like file streams, and cleans-up even if there is exceptions
- example:
 with open("file.json") as json_file:
  # do some code - NOTE this is an INDENTED block

Appending a line to a file
 with open("_dollar.txt", "a") as outfile:
  outfile.write("$bin hello")

Opening files but checking first
------------------------------------
    if os.path.isfile(os.environ['PBS_CONF_FILE']):
        pbs_conf = open(os.environ['PBS_CONF_FILE'], 'r')
        for line in pbs_conf:
            os.environ[line.split("=")[0]] = line.split("=")[1].strip('\n')
        pbs_conf.close()
    else:
        print "Unable to find PBS_CONF_FILE ... " + os.environ['PBS_CONF_FILE']
        sys.exit(1)




Opening file with error catching:
-------------
try:
    f = open(my_file)

    try:
        do_stuff_that_fails()
    except EXPECTED_EXCEPTION_TYPES as e:
        do_stuff_when_it_doesnt_work()
    finally:
        f.close()

except (IOError, OSError) as e:
    do_other_stuff_when_it_we_have_file_IO_problems()
-------------
try:
    f = open('foo.txt')
except IOError:
    print('error')
else:
    with f:
        print f.readlines()




Open files using 'with':
-----------
with open(s_in, 'rw') as input_file:
 for line in input_file:
  count = count + 1
  #print str(count) + " " + line
  output = re.compile(r"^\s*[a-zA-Z0-9_]")   # searching line starting with non-whitesapce, non #
  result = output.search(line)    #result is a MatchObject or None
----------
try:
    with open(my_file) as f:
        do_stuff_that_fails()
except (IOError, OSError, Failure) as e:
    do_stuff_when_it_doesnt_work()
------------



Opening, Reading and Replacing lines in inline mode
--------
# Use the line number fond above, ADD a single line to the same input file.
count = 0
for line in fileinput.input(s_in, inplace=1):   #inplace=1 is having backup
 count = count+1
 if (count == lineNumberCount):
  print "WORKDIR"
 print line,
--------
this example prints line, but at the special line number, it prints and extra line with words "WORKDIR"


---from Alexis

import os
import stat

# Define file params
fname = '/tmp/myfile'
flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL  # Refer to "man 2 open".
mode = stat.S_IRUSR | stat.S_IWUSR  # This is 0o600 in octal and 384 in decimal.

# For security, remove file with potentially elevated mode
try:
    os.remove(fname)
except OSError:
    pass

# Open file descriptor
umask_original = os.umask(0)
try:
    fdesc = os.open(fname, flags, mode)
finally:
    os.umask(umask_original)

# Open file handle and write to file
with os.fdopen(fdesc, 'w') as fout:
    fout.write('something\n')


Writing CSV file
===================
Ref:
http://stackoverflow.com/questions/2982023/writing-header-with-dictwriter-from-pythons-csv-module

from collections import OrderedDict
ordered_fieldnames = OrderedDict([('field1',None),('field2',None)])
with open(outfile,'wb') as fou:
    dw = csv.DictWriter(fou, delimiter='\t', fieldnames=ordered_fieldnames)
    dw.writeheader()
    # continue on to write data

---------- 

with open(infile,'rb') as fin:
    dr = csv.DictReader(fin, delimiter='\t')

# dr.fieldnames contains values from first row of `f`.
with open(outfile,'wb') as fou:
    dw = csv.DictWriter(fou, delimiter='\t', fieldnames=dr.fieldnames)
    headers = {} 
    for n in dw.fieldnames:
        headers[n] = n
    dw.writerow(headers)
    for row in dr:
        dw.writerow(row)



Ref: https://pymotw.com/2/csv/

import csv
import sys

f = open(sys.argv[1], 'wt')
try:
    writer = csv.writer(f)
    writer.writerow( ('Title 1', 'Title 2', 'Title 3') )
    for i in range(10):
        writer.writerow( (i+1, chr(ord('a') + i), '08/%02d/07' % (i+1)) )
finally:
    f.close()

print open(sys.argv[1], 'rt').read()


JSON in Python
=================
 import json
 with open(file_poly) as json_file:
        json_data = json.load(json_file)
        json_file.close()

        if (iDebug==1):
            print(json_data)
            a_min = json_data["a"]["min"]
            a_max = json_data["a"]["max"]
            print a_min, a_max
    return json_data

The above code is for the JSON file below:
-----------------
{
    "Number_of_Questions" : 10,
    "a":{
        "min":"0",
        "max":"10"
    },
    "b":{
        "min":"1",
        "max":"9"
    },
    "c":{
        "min":"2",
        "max":"8"
    }
}
-----------------


(#https://docs.python.org/2/library/json.html)
To construct a JSON data structure from strings:
-----------
import json
text = \
'{ \
    "kind": "blogger#post", \
    "blog": { \
        "id": "8070105920543249955" \
    }, \
    "title": "A new post", \
    "content": "With <b>exciting</b> content..." \
}'
parsed = json.loads(text)
print json.dumps(parsed , indent=4, separators=(',', ': '))
-----------
NOTE: important to have the line continuation





OS FileSystem and shell commands
=====================================
import os           - package that enables shell commands.
os.getcwd()         - print current working directory
os.chdir('path')    - change directory
os.listdir()        - list contents of directory
os.path.basename(v) - return the base name of pathname of v
os.environ['HOME']  - get the value for env var HOME
os.environ.get(var) - get value for environment variable 'var'
os.path.isfile      - check if file exist
os.path.isdir      - check if directory exist
os.sys.path         - list the search path
os.chmod(path, 0444) - change permission of the file / directory in 'path'

Joining LIST of words as paths - using SPLATE '*' operator
>>> ldir
['', 'home1', 'chee', 'dev', 'Kyoto']
>>> os.path.join(*ldir)
'home1/chee/dev/Kyoto'
>>> ldir[1]='/home1'
>>> os.path.join(*ldir)
'/home1/chee/dev/Kyoto'



This will WALK the directory tree:

for (path, dirs, files) in os.walk(path):
    print path
    print dirs
    print files
    print "----"

Here are the results:

c:/python25
['DLLs', 'Doc', 'include', 'Lib', 'libs', 'tcl', 'Tools']
['LICENSE.txt', 'NEWS.txt', 'pylupdate4.exe', 'pyrcc4.exe', 'python.exe', 'pythonw.exe', 'pyuic4.bat', 'README.txt', 'temp.py', 'w9xpopen.exe']
----
c:/python25\DLLs
[]
['bz2.pyd', 'py.ico', 'pyc.ico', 'pyexpat.pyd', 'select.pyd', 'sqlite3.dll', 'tcl84.dll', 'tclpip84.dll', 'tk84.dll', 'unicodedata.pyd', 'winsound.pyd', '_bsddb.pyd', '_ctypes.pyd', '_ctypes_test.pyd', '_elementtree.pyd', '_hashlib.pyd', '_msi.pyd', '_socket.pyd', '_sqlite3.pyd', '_ssl.pyd', '_testcapi.pyd', '_tkinter.pyd']
----
c:/python25\Doc
[]
['Python25.chm']
----
c:/python25\include
[]
['abstract.h', 'asdl.h', 'ast.h', 'bitset.h', 'boolobject.h', 'bufferobject.h', 'cellobject.h', 'ceval.h', 'classobject.h', 'cobject.h', 'code.h', 'codecs.h', 'compile.h', 'complexobject.h', 'cStringIO.h', 'datetime.h', 'descrobject.h', 'dictobject.h', 'enumobject.h', 'errcode.h', 'eval.h', 'fileobject.h', 'floatobject.h', 'frameobject.h', 'funcobject.h', 'genobject.h', 'graminit.h', 'grammar.h', 'import.h', 'intobject.h', 'intrcheck.h', 'iterobject.h', 'listobject.h', 'longintrepr.h', 'longobject.h', 'marshal.h', 'metagrammar.h', 'methodobject.h', 'modsupport.h', 'moduleobject.h', 'node.h', 'object.h', 'objimpl.h', 'opcode.h', 'osdefs.h', 'parsetok.h', 'patchlevel.h', 'pgen.h', 'pgenheaders.h', 'pyarena.h', 'pyconfig.h', 'pydebug.h', 'pyerrors.h', 'pyexpat.h', 'pyfpe.h', 'pygetopt.h', 'pymactoolbox.h', 'pymem.h', 'pyport.h', 'pystate.h', 'pystrtod.h', 'Python-ast.h', 'Python.h', 'pythonrun.h', 'pythread.h', 'py_curses.h', 'rangeobject.h', 'setobject.h', 'sliceobject.h', 'stringobject.h', 'structmember.h', 'structseq.h', 'symtable.h', 'sysmodule.h', 'timefuncs.h', 'token.h', 'traceback.h', 'tupleobject.h', 'ucnhash.h', 'unicodeobject.h', 'weakrefobject.h']
----

To get only files (not directories) on the top level of a directory, say privateFullPath
onlyFiles = [f for f in os.listdir(privateFullPath) if os.path.isfile(os.path.join(privateFullPath, f))]



>subprocess
=============

In order to run a Linux piped command using python, such as ps -A | grep blah,
use the subprocess command two times with subprocess.PIPE to pipe the output to be the input of another

eg
ps = subprocess.Popen(('ps', '-A'), stdout=subprocess.PIPE)
output = subprocess.check_output(('grep', 'process_name'), stdin=ps.stdout)
ps.wait()

WARNING: arguments above like 'ps' and '-A' MUST NOT have spaces like 'ps ' or ' -A'

Security Issues:
-------------
01 import subprocess
02 import shlex
03  
04  
05 def unsafe(directory):
06     command = "du -sh {}".format(directory)
07     subprocess.call(command, shell=True)
08  
09  
10 def safe(directory):
11     sanitized_directory = shlex.quote(directory)
12     command = "du -sh {}".format(sanitized_directory)
13     subprocess.call(command, shell=True)
14  
15 if __name__ == "__main__":
16     directory = "/dev/null; ls -l /tmp"
17     unsafe(directory)
18     safe(directory)
-------------

in the unsafe() function, the input is not sanitized. The user can put anything.  In this case, the ; ends the du command, then the later command ls -l /tmp, allows people to see contents of /tmp


Another Example - using communicate to capture stdout, stderr
Warning - Use communicate() rather than .stdin.write, .stdout.read or .stderr.read to avoid deadlocks due to any of the other OS pipe buffers filling up and blocking the child process.
---
TEST_CMD = './qsub_kyoto.exe'
TEST_ARGS = ' not a-dash -u asdas dfh'
st1 = list()
st1.append(TEST_CMD)
st1.append(TEST_ARGS)
pr1 = subprocess.Popen(st1, stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
pout, perr =pr1.communicate()
print pout
print perr
---
The communicate function return a tuple representing stdout, stderr. these can be printed out.
*** Note The data read is buffered in memory, so do not use this method if the data size is large or unlimited.
*** Whenever subprocess.PIPE is used for the stdout and stderr, then need to use communicate(). 
Otherwise using wait() will cause deadlocks.
*** Don't use shell=True


Another Case:
I'm surprised you had any success running ls and rmdir without shell=True. ls and rmdir are not programs, they are internal commands within the shell program.
wget, however, is a program. You can use shell=True, but you don't need to. Using shell=False (the default) will save you from having to start a new shell process (which is an expensive operation). When you submit a command with shell=True, you submit the whole command as one string. But when you submit the command with shell=False, you must issue the command as a list of strings, with the command name in the first element of the list, the first argument in the next list element, etc. For example:

wgproc = subprocess.Popen(['wget', '-r', '--tries=10', 'http://fly.srk.fer.hr/', '-o', 'log'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
(standardout, junk) = wgproc.communicate()



Example - using stdout to capture output to a file:
ofile = open("/tmp/out.txt", "wb")
change=subprocess.Popen(["echo","0"], stdout=ofile)
ofile.close() 

--------------

If you need to execute a command with a timeout, use the timeout argument:
try:
    out_bytes = subprocess.check_output(['cmd','arg1','arg2'], timeout=5)
except subprocess.TimeoutExpired as e:
    ...
Normally, commands are executed without the assistance of an underlying shell (e.g., sh, bash, etc.). Instead, the list of strings supplied are given to a low-level system command, such as os.execve(). If you want the command to be interpreted by a shell, supply it using a simple string and give the shell=True argument. This is sometimes useful if you’re trying to get Python to execute a complicated shell command involving pipes, I/O redirection, and other features. 

For example:
out_bytes = subprocess.check_output('grep python | wc > out', shell=True)

Be aware that executing commands under the shell is a potential security risk if arguments are derived from user input. The shlex.quote() function can be used to properly quote arguments for inclusion in shell commands in this case.





Commands
===========
exec(open('<python file>').read())   - executes a python script file *.py
glob.glob(path/*.txt)                - grabs a list of filenames
interpreter = re.match('^#!(.*)', line).group(1)
        Matches the regular expression in the "line" string, then chooses the first group.
m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist")
m.group(0)       # The entire match
>>>'Isaac Newton'
m.group(1)       # The first parenthesized subgroup.
>>>'Isaac'
m.group(2)       # The second parenthesized subgroup.
>>>'Newton'
m.group(1, 2)    # Multiple arguments give us a tuple.
>>>('Isaac', 'Newton')

import sys; print sys.version      # version of python


"Be careful with exec and eval in Python"
http://lucumr.pocoo.org/2011/2/1/exec-in-python/


Command line Options
=======================
# Get the total number of args passed to the demo.py
total = len(sys.argv)
 
# Get the arguments list 
cmdargs = str(sys.argv)

# sys.argv values CAN BE CHANGED
print sys.argv[1]
sys.argv[1] = 'blah'
print sys.argv[1]


1. Using getopt
>>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'
>>> args = s.split()
>>> args
['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']
>>> optlist, args = getopt.getopt(args, 'x', [
...     'condition=', 'output-file=', 'testing'])
>>> optlist
[('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', '')]
>>> args
['a1', 'a2']
----
 try:
      opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
   except getopt.GetoptError:
      print 'test.py -i <inputfile> -o <outputfile>'
      sys.exit(2)
Note: The getopt assumes options are either single '-x' or long names '--xxxxxxx'
The notation for single is ":" and for long names is "="


2. Using optparse
Example:
----------------
import optparse
if __name__ == '__main__':
    entry = optparse.OptionParser(description="KyotoLSF to PBSPro Translator",
                                 prog="Converter", version="1.0",
    usage="python %prog.py -ug <group> -W <walltime>  -A <node-core specs> ")
#    entry.add_option("--source_files_path", "-s", default="")
    entry.add_option("-u", "--user_group",  type="string", action="store", dest="OPT_UG")
    options, arguments = entry.parse_args()
----------------



Octo.py
========
Ref: http://code.google.com/p/octopy/

MapReduce implementation in Python.

Put octo.py on all of the machines you want to use. 
On the machine you will use as a server (with ip address <ip>), also install triangular.py, and then execute:
     python octo.py server triangular.py &

On each of your clients, run
     python octo.py client <ip> &

You can try this out using the same machine to run the server process and one or more client processes, of course.


Unix Script
=============
Example Script:

<code>
#!/usr/bin/env python
import sys
import subprocess

sUsage = 'Usage: sudo <command> username'

print "**** users_du script"
print "**** prints the usages for each users"
print sUsage

# Check number of arguments
if len(sys.argv) == 1:                   # sys.argv[0] is the actual command
 print "Disk usage for all users"
 proc_ls = subprocess.Popen(['ls', '/home'], stdout=subprocess.PIPE)

 for line in proc_ls.stdout.readlines():
  sdir = '/home/' + line.rstrip()
  proc_du = subprocess.Popen(['sudo', 'du', '-s', sdir], stdout=subprocess.PIPE)
  for line_du in proc_du.stdout.readlines():
   print line_du
   stop

elif len(sys.argv) == 2:
 print "Disk usage for user ", sys.argv[1]
 sdir = '/home/' + sys.argv[1]
 proc_du = subprocess.Popen(['sudo', 'du', '-s', sdir], stdout=subprocess.PIPE)
 for line_du in proc_du.stdout.readlines():
  print line_du

else:
 sys.exit(0)


print "End of Script"
</code>


1. use header
#!/usr/bin/env python

2. Useful packages
import sys   # system commands            
import optparse     # allow script to have various options
import subprocess   # shell scripting features


print
======
Combininb strings and numbers
Eg.
 xx=3
 print ("hello" + str(xx))


Format specifications:
's'  String format. This is the default type for strings and may be omitted.
'b'  Binary format. Outputs the number in base 2.
'c'  Character. Converts the integer to the corresponding unicode character before printing.
'd'  Decimal Integer. Outputs the number in base 10.
'o'  Octal format. Outputs the number in base 8.
'x'  Hex format. Outputs the number in base 16, using lower- case letters for the digits above 9.
'X'  Hex format. Outputs the number in base 16, using upper- case letters for the digits above 9.
'n'  Number. This is the same as 'd', except that it uses the current locale setting to insert the appropriate number separator characters.
None  The same as 'd'.

'e'  Exponent notation. Prints the number in scientific notation using the letter ‘e’ to indicate the exponent. The default precision is 6.
'E'  Exponent notation. Same as 'e' except it uses an upper case ‘E’ as the separator character.
'f'  Fixed point. Displays the number as a fixed-point number. The default precision is 6.
'F'  Fixed point. Same as 'f'.
'g'  

General format. For a given precision p >= 1, this rounds the number to p significant digits and then formats the result in either fixed-point format or in scientific notation, depending on its magnitude.

The precise rules are as follows: suppose that the result formatted with presentation type 'e' and precision p-1 would have exponent exp. Then if -4 <= exp < p, the number is formatted with presentation type 'f' and precision p-1-exp. Otherwise, the number is formatted with presentation type 'e' and precision p-1. In both cases insignificant trailing zeros are removed from the significand, and the decimal point is also removed if there are no remaining digits following it.

Positive and negative infinity, positive and negative zero, and nans, are formatted as inf, -inf, 0, -0 and nan respectively, regardless of the precision.

A precision of 0 is treated as equivalent to a precision of 1. The default precision is 6.
'G'  General format. Same as 'g' except switches to 'E' if the number gets too large. The representations of infinity and NaN are uppercased, too.
'n'  Number. This is the same as 'g', except that it uses the current locale setting to insert the appropriate number separator characters.
'%'  Percentage. Multiplies the number by 100 and displays in fixed ('f') format, followed by a percent sign.
None  The same as 'g'.



Check Text Format Newline CLRF
================================
To check whether a text file has Windows /r/n or Unix /n or Mac /r line endings, do this:

f = open('myfile.txt', 'U')
f.readline()  # Reads a line
# The following now contains the newline ending of the first line:
# It can be "\r\n" (Windows), "\n" (Unix), "\r" (old Mac OS).
# If no newline is found, it contains None.
print repr(f.newlines)


WARNINGS
=========
A few things that Python are different to others and are counter-intuitive.
 
1. Array slicing, a[2:5] means a[2], a[3], a[4] but NOT a[5]. Better to think that indices are actually on the edge of number line, and contents in the middle.

2. Python "=" are NOT copy operators. For example
  a=7
  b=a
  b=3
  print a   .... gives 3, not 7

  To copy variable, use the copy method, eg
  b = a.copy()







Sourcing Script within a python file
======================================
I have a common script that needs to be called from every app definition’s start.py. What is the simple way of doing this. There are no functions inside this script, it is a plain script that reads some environment variables and outputs some information to the job directory before the job is completed.
Did a google search for this and most point to using os.system call to start a new python execution and run this script. Or define a function, use import statement to call the script and then call the function.
Is there a way to just source the script in the same python environment as start.py
I am trying to avoid duplicate code being used in multiple app definitions.


os.system is fine in simple cases. In more advanced cases (where you want more control programmatically) use subprocess.
<code>
    if os.path.exists(“/path/to/script"):
        command = “/path/to/script"
        script = subprocess.Popen(command, env=os.environ, shell=True, stdout=subprocess.PIPE)
        for line in script.stdout.readlines():
              print line
        script.wait()
</code>

In case you really want to “source” a script you can do things like
<code>
mpivars_script = os.environ['mpipath'] + '/bin64/mpivars.sh'
p = subprocess.Popen(". %s; env" % mpivars_script, stdout=subprocess.PIPE, shell=True)
stdout = p.communicate()[0]
env = dict((line.split("=", 1) for line in stdout.splitlines()))
os.environ.update(env)
</code>
But if you want to re-use your Python code the best is to write modules an import them with import.

 

Python openedx
=================
https://github.com/numerical-mooc/numerical-mooc


iPython /Jupyter
=================
to start:   ipython notebook

in markdown cell, to add a image to the notebook, need the "files/" to prepend to relative path. Eg.
![Image](files/figures/glider_forces.png)

 

 
Symbolic Math Python
=====================

import sympy        
from sympy.utilities.lambdify import lambdify

Declare a variable to be symbolic"
   x, nu = sympy.symbols('x nu')
WARNING - these symbols should not be variables that are intended to have a value assigned. Eg if nu=0.3 is defined, then it is a constant, NOT a symbolic variable.

Declare a maths expression or formula
   y = sympy(x)**3

Differentiate symbolically
   dydx = y.diff(x)

Lambdify - makes the symbolic expression into a function
- creating the lambda function
     yfunc = lambdify((x, ..), y)
- using the function
     yfunc(2.3)     --- evaluating y(x=2.3)



Printing Math Formula
=======================
from sympy import *
... has functions like: symbols, latex

ref: http://oneau.wordpress.com/2011/12/25/latex-sympy/
>>> x = Symbol("x")
>>> a =  1/( (x+2)*(x+1) )
>>> latex(a)
\frac{1}{\left(x + 1\right) \left(x + 2\right)}

PrettyPrint - to print latex equations onto png or dvi or terminal


from sympy.printing import printing
preview(str, outputTexFile='sample.tex')


   str = "line 1 \\\\" +" line 2\\\\ " + " line 3\\\\ "
 x,y=symbols('x,y')
   str = str + '$'+latex(x**2 + y)+'$'
   preview(str, outputTexFile='sample.tex')
This will produce the lines
    line 1
 line 2
 line 3
 x^2 + y 

preview(str, output='pdf', viewer='notepad')


Installing Packages
=====================
Instruction for windows
1. Download python packages such as numpy, etc
2. Unpack in any directory
3. Open up Command Prompt
4. Set the python path, such as: (may be different for your installation)
      set path=%path%;C:\python27
5. Change directory to where the packages like numpy, were unpacked.
6. To install the packages, typically (but may not be for all cases):
       python setup.py install
      

 
Linux package install

Numpy - 
Scipy - http://www.scipy.org/install.html
MPI4py - http://www.mpi4py.scipy.org/docs/usrman/tutorial.html# 


>Packages for Machine Learning
================================

scipy - http://www.scipy.org/install.html

numpy - mainly useful for its N-dimensional array objects

matplotlib - 2D plotting library producing publication quality figures

pandas - Python data analysis library, including structures such as dataframes
http://pandas.pydata.org/pandas-docs/stable/10min.html

sklearn - the machine learning algorithms used for data analysis and data mining tasks
http://scikit-learn.org/stable/install.html 
http://scikit-learn.org/stable/tutorial/basic/tutorial.html


Canopy - able to use Package Manager




Zipfile and Datetime
=====================
import os
import zipfile
import datetime

# Getting date and appending .zip
today = datetime.date.today()
zipfilename = str(today) + '.zip'   # str(today) format as "2015-02-16"
print zipfilename

# get files in directory
files = os.listdir('.')
#print files

# ZipFile is a context manager and can exit safely
with zipfile.ZipFile(zipfilename, 'w') as myzip:
 for ifile in files:           # looping through each file in the directory
  if ifile[-4:] == '.pdf':   # consider only *.pdf files
   print ifile
   myzip.write(ifile)      # add each *.pdf file into zip file.


A very good list of examples are in:
(http://www.pythonforbeginners.com/basics/python-datetime-time-examples/)
import time
import datetime

print "Time in seconds since the epoch: %s" %time.time()
print "Current date and time: " , datetime.datetime.now()
print "Or like this: " ,datetime.datetime.now().strftime("%y-%m-%d-%H-%M")

Formats fmt in datetime.date.today().strftime(fmt)
%Y Current year
%B Month of year: 
%W Week number of the year: "
%w Weekday of the week: 
%j Day of year: 
%d Day of the month : 
%A Day of week: 

for a time of 16:13:37
str(datetime.datetime.now()).split(" ")[1].split(".")[0].replace(":", "")
=> 161337

PyCharm Editor
=====================

How to disable auto-save:

    Go to File > Settings (Ctrl+Alt+S).
    Go to Appearance & Behavior > System Settings.
    Make sure the two are unchecked:
        Save files on frame deactivation
        Save files automatically if application is idle for x sec.
    Go to Editor > General > Editor Tabs
    Put a checkmark on "Mark modified files with asterisk"
    (Optional but recommended) Under "Tab Closing Policy", select "Close non-modified files first". You may also want to increase the number of allowed tabs.
    Click Apply > OK.

How to Change versions of Python Interpreter
- Click on the desired project on the left nav area
- Click File -> Settings. 
- In the Settings dialog, click: Project -> Project Interpreter
- On the main page of the dialog, click on the correct project to change
- In the Project Interpreter: select or add the correct Python version.


Error: Missing module sys - "import sys" module not found
Solution: File -> "Invalidate Caches / Restart"

To Show line numbers, go to:
File - Settings -Editor - General - Appearance


Python Logging module
==========================
Best shown with example below (from http://www.onlamp.com/pub/a/python/2005/06/02/logging.html)

-----------------------
#!/usr/bin/env python

import logging
import auxiliary_module

#create logger
logger = logging.getLogger("simple_example")
logger.setLevel(logging.DEBUG)
#create console handler and set level to error
ch = logging.StreamHandler()
ch.setLevel(logging.ERROR)
#create file handler and set level to debug
fh = logging.FileHandler("spam.log")
fh.setLevel(logging.DEBUG)
#create formatter
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - 
   %(message)s")
#add formatter to ch and fh
ch.setFormatter(formatter)
fh.setFormatter(formatter)
#add ch and fh to logger
logger.addHandler(ch)
logger.addHandler(fh)

#"application" code
logger.debug("debug message")
logger.info("info message")
logger.warn("warn message")
logger.error("error message")
logger.critical("critical message")

logger.info("creating an instance of auxiliary_module.Auxiliary")
a = auxiliary_module.Auxiliary()
logger.info("created an instance of auxiliary_module.Auxiliary")
logger.info("calling auxiliary_module.Auxiliary.do_something")
a.do_something()
-----------------------
#!/usr/bin/env python

import logging

#create logger
module_logger = logging.getLogger("spam_application.auxiliary")


class Auxiliary:
    def __init__(self):
        self.logger = logging.getLogger("spam_application.auxiliary.Auxiliary")
        self.logger.info("creating an instance of Auxiliary")
    def do_something(self):
        self.logger.info("doing something")
        a = 1 + 1
        self.logger.info("done doing something")

def some_function():
    module_logger.info("received a call to \"some_function\"")
------------------------

There two python code above shows two different things together
1) Multiple handlers for different error levels 
2) Multiple modules logging

The second code is a module (Auxilliary) called by the first code.

Naming the 3 loggers:
logger = logging.getLogger("simple_example")   # for main script
module_logger = logging.getLogger("spam_application.auxiliary")    # for module
self.logger = logging.getLogger("spam_application.auxiliary.Auxiliary") # for class

Two handlers
ch = logging.StreamHandler()
fh = logging.FileHandler("spam.log")

... which handle to log levels
ch.setLevel(logging.ERROR)
fh.setLevel(logging.DEBUG)


Installing a Second Python
============================
Ref: http://toomuchdata.com/2014/02/16/how-to-install-python-on-centos/

Install the following:
yum groupinstall "Development tools"
yum install zlib-devel 
yum install bzip2-devel 
yum install openssl-devel 
yum install ncurses-devel 
yum install sqlite-devel 
yum install readline-devel 
yum install tk-devel 
yum install gdbm-devel 
yum install db4-devel 
yum install libpcap-devel 
yum install xz-devel

Configure:
Use the following configure switches

Use unicode for extra compatibility.
2.7> --enable-unicode=ucs4 
3.2>--with-wide-unicode

Compile as shared library, as most python dist, otherwise not compatible with some code.
Add as configure flag:
LDFLAGS="-Wl,-rpath /usr/local/lib"

Alternatively edit /etc/ld.so.conf and add line: 
 /usr/local/lib.
Then make the dynamic linker aware:
 /sbin/ldconfig

Installing:
 make altinstall

Summary:
# Python 2.7.6:
wget http://python.org/ftp/python/2.7.6/Python-2.7.6.tar.xz
tar xf Python-2.7.6.tar.xz
cd Python-2.7.6
./configure --prefix=/usr/local --enable-unicode=ucs4 --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
make && make altinstall
 
# Python 3.3.5:
wget http://python.org/ftp/python/3.3.5/Python-3.3.5.tar.xz
tar xf Python-3.3.5.tar.xz
cd Python-3.3.5
./configure --prefix=/usr/local --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
make && make altinstall


Python Package Manager - Setuptools and pip
==============================================
pip is used to manage and install python packages for your installation.
Setuptools need to be installed first before using it to install pip.


# First get the setup script for Setuptools:
wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py
 
# Then install it for Python 2.7 and/or Python 3.3:
python2.7 ez_setup.py
python3.3 ez_setup.py
 
# Now install pip using the newly installed setuptools:
easy_install-2.7 pip
easy_install-3.3 pip
 
# With pip installed you can now do things like this:
pip2.7 install [packagename]
pip2.7 install --upgrade [packagename]
pip2.7 uninstall [packagename]




Isolate Python Environment using virtualenv
===============================================
Ref: http://toomuchdata.com/2014/02/16/how-to-install-python-on-centos/

# Install virtualenv for Python 2.7 
pip2.7 install virtualenv

# Python 3.3 already has pyvenv equivalent

#create a sandbox called my27project:
virtualenv-2.7 my27project
 
# create a sandbox called my33project:
pyvenv-3.3 my33project
 
# the system Python interpreter should still be default:
python --version
# Should show Python 2.6.6 for Centos 6
 
# to use my27project sandbox:
source my27project/bin/activate
python --version
# Should show Python 2.7.6
deactivate
 
# to use my33project sandbox :
source my33project/bin/activate
python --version
# Should show Python 3.3.5
deactivate


@ Declarators
===============
One out of many example showing the use of declarators:
----------------------
class myDecorator(object):

    def __init__(self, f):
        print "inside myDecorator.__init__()"
        f() # Prove that function definition has completed

    def __call__(self):
        print "inside myDecorator.__call__()"

@myDecorator
def aFunction():
    print "inside aFunction()"

print "Finished decorating aFunction()"

aFunction()
When you run this code, you see:

inside myDecorator.__init__()
inside aFunction()
Finished decorating aFunction()
inside myDecorator.__call__()
----------------------





Python uploading to Gdrive
=============================
See NotesPythonGoogleAPI


Python Writing Blog 
=====================
See NotesPythonGoogleAPI



Troubleshooting
=================

Message:
ImportError: No module named
Occurance:
Python programs using modules/packages, typically when programs have import statements.
Cause:
The module's package may have been installed but the path was not found.
If the package was not installed, then install using pip or some other tools.
Solution:
On linux, set the environment variable like below, in Bash:
export PYTHONPATH="/usr/local/lib/python2.7/site-packages"


Miscellaneous
=================
os.path.expanduser("~") - in Windows8, returns C:\Users\<username>
[''] and array of empty strings is counted as len 1. Mistake to assume this object has ZERO length
for ii in range (1,N) - this loops up to N-1 only.

To use New Python 2.6 features on Python 2.4:
from __future__ import with_statement


UnitTest
==========
http://pythontesting.net/start-here/
https://docs.python.org/2/library/unittest.html
http://docs.python-guide.org/en/latest/writing/tests/
http://pyunit.sourceforge.net/pyunit.html


Example:
<code>
import unittest

class TestAoption(unittest.TestCase):
    """ TestAoption

    """
    def test_dummy(self):
        """Dummytest

        :return:
        """
        self.assertEqual("hello", "hello")

if __name__ == '__main__':
    unittest.main()
</code>

To run:
1.  python test_Aoption_unittest.py
2. 


umask in python
==================
import os
import stat

# Define file params
fname = '/tmp/myfile'
flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL  # Refer to "man 2 open".
mode = stat.S_IRUSR | stat.S_IWUSR  # This is 0o600 in octal and 384 in decimal.

# For security, remove file with potentially elevated mode
try:
    os.remove(fname)
except OSError:
    pass

# Open file descriptor
umask_original = os.umask(0)
try:
    fdesc = os.open(fname, flags, mode)
finally:
    os.umask(umask_original)

# Open file handle and write to file
with os.fdopen(fdesc, 'w') as fout:
    fout.write('something\n')

If the desired mode is 0600, it can more clearly be specified as the octal number 0o600. Even better, just use the stat module.

Even though the old file is first deleted, a race condition is still possible. Including os.O_EXCL with os.O_CREAT in the flags will prevent the file from being created if it exists due to a race condition. This is a necessary secondary security measure to prevent opening a file that may already exist with a potentially elevated mode. In Python 3, FileExistsError with [Errno 17] is raised if the file exists.

Failing to first set the umask to 0 can lead to an incorrect mode (permission) being set by os.open. This is because the default umask is usually not 0, and it may be applied to the specified mode. For example, if my original umask is 2, and my specified mode is 0o222, if I fail to first set the umask to 0, the resulting file can instead have a mode of 0o220, which is not what I wanted. Per man 2 open, the mode of the created file is mode & ~umask.

The umask is restored to its original value as soon as possible. This getting and setting is not thread safe, and a threading.Lock must be used if necessary.

Coverage
===========
coverage.py
https://coverage.readthedocs.io/en/coverage-4.4.1/

- to install: pip install coverage
- create a program such as a.py that will take arguments, eg:
    python a.py hello
- run coverage: 
    coverage run a.py hello
- make report: (reports should appear in same directory)
    coverage report -m
    coverage html       (in ./htmlcov)
    coverage xml


Codacy
==========

- to install in env with python already: pip install codacy-coverage
- Go to Codacy website about setup test coverage, and get token.
- Set token on Windows, on the same CMD terminal as before,
    set CODACY_PROJECT_TOKEN=blah
- Perform coverage using codacy.
    python-codacy-coverage -r coverage.xml

Message:
(C:\Users\chee\Anaconda2) D:\My Box Files\workspace\gittest>set CODACY_PROJECT_TOKEN=blahhhhhhh

(C:\Users\chee\Anaconda2) D:\My Box Files\workspace\gittest>python-codacy-coverage -r coverage.xml
Traceback (most recent call last):
  File "c:\users\chee\anaconda2\lib\runpy.py", line 174, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "c:\users\chee\anaconda2\lib\runpy.py", line 72, in _run_code
    exec code in run_globals
  File "C:\Users\chee\Anaconda2\Scripts\python-codacy-coverage.exe\__main__.py", line 9, in <module>
  File "c:\users\chee\anaconda2\lib\site-packages\codacy\__init__.py", line 6, in main
    return reporter.run()
  File "c:\users\chee\anaconda2\lib\site-packages\codacy\reporter.py", line 198, in run
    args.commit = get_git_revision_hash()
  File "c:\users\chee\anaconda2\lib\site-packages\codacy\reporter.py", line 41, in get_git_revision_hash
    return subprocess.check_output(['git', 'rev-parse', 'HEAD']).decode("utf-8").strip()
  File "c:\users\chee\anaconda2\lib\subprocess.py", line 212, in check_output
    process = Popen(stdout=PIPE, *popenargs, **kwargs)
  File "c:\users\chee\anaconda2\lib\subprocess.py", line 390, in __init__
    errread, errwrite)
  File "c:\users\chee\anaconda2\lib\subprocess.py", line 640, in _execute_child
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified


Solution:
The error is because Codacy at this point does not run on Windows.
Tested Successful on Linux Centos6.4


Security - Bandit
===================
Bandit package
https://pypi.python.org/pypi/bandit/1.0.1


- install: pip install bandit
- to run:   bandit -r /path/to/code

1001 Most Idiotic Error Messages

Who this is for: For developer / programmers on the verge of pulling out their hair, due to incomprehensible or downright unhelpful error messages.

HOW TO USE ME: When you get a non-sensical COMPILATION or RUNTIME message, check to see if the problem & solution is recorded here. You are encouraged to "Google" your error messages first. Chances are Google will point you to this webpage if your error message has a solution on this webpage.

Your Responsibility: If you find a solution to a problem with an idiotic error message, please email to the address above (without the word ".earth").

The Idiotic Error Messages


Message:
`struct streampos' used where a `__off64_t
Occurance:
Compiling / Installing program
Cause:
gcc3.1 iostream - streambuf.h file
Solution:
1) Compile with gcc 2.95
2) Configure with
CC="/usr/local/gcc2/bin/gcc" CXX="/usr/local/gcc2/bin/g++" ./configure --prefix=/mydir
provided your old gcc2.95 is in /usr/local/gcc2/bin/


Message:
186 3 Formatted record insufficient in WRITE
Occurance:
runtime, NEC-SX5
Cause:
Too small buffer for writing records. This is a System problem.
Solution:
Edit the environment variable to enlarge buffer for stdout.
F_SYSLEN=300
EXPORT F_SYSLEN


Message:
Stack SEGV. PC 0x10014af4. segv (11). code 28, pc 0x10014af4 segv (11). code 13, pc 0x10015c20,
[Thread 0 (2252684)]: Stack SEGV. PC 0x10014af4.
Addr 0xfffae69e78. SZ 67108864
[ 0 (2252684)]: segv (11). code 28, pc 0x10014af4,
vaddr 0xfffae69e78, alt-sp 0x14cf7190
[Thread 1 (2299358)]: Stack SEGV. PC 0x10015c20.
Addr 0x14cf808c. SZ 16809984
[ 1 (2299358)]: segv (11). code 13, pc 0x10015c20,
vaddr 0x14cf808c, alt-sp 0x14cf7190

Occurance:
SGI Origin 2400 - Clare - MipsPro Fortran
Cause:
Stack size overflow
Solution:
Increase the system limit by:
ulimit -s 512000




Message:

Occurance:
Compile Time - programs suddenly cannot compile
Cause:
Existence of core file - previous run created MEGA core file
Solution:
rm core


Message:
Warning: Stack frame size (131855680) larger than system limit (67108864)
Occurance:
MIPSPro Compilers, SGI Origins
Cause:
Stack size to large
Solution:
Compile with f90 -static ....


Message:
The minimum size of partition 5 exceeds the partition size limit.
Occurance:
Compiling using xlf90 on IBM SP2
Cause:
When the highest level of optimization (-O5/O4) is requested, the compiler will perform inter procedural optimization (ipa). In the process, it needs temp storage and when the default amount is not sufficient, the above message results.
Solution:
To remedy this, add the following switch to your compile line:-qipa=partition=large See the "man page" on xlf for detail. (Type xlf at prompt, not man xlf)


Message:
1514-008 (W) Variable bpmort is misaligned. This may affect the efficiency of the code.
Occurance:
Compiling using xlf90 on IBM SP2
Cause:
The list of the variables in the COMMON block (in Fortran) does not enforce the need(alignment for reasons of performance) for arranging them by decreasing size of the number of bytes which is necessary to represent them in memory.
Solution:
Rearrange the variables listed in common so that the bigger sized variables come first. Example
real r4,v(99)
double precision r8
integer (8) i8
integer (4) i4
logical(2) log
common /test/log,v,r4,i4,r8,i8 ! NOT Good
common /test/r8,i8,v,r4,i4,log ! GOOD


Message:
Warning: Cannot send session cache limiter - headers already sent
Occurance:
PHP, HTML, webpages, using start_session() or headers()
Cause:
The PHP function such as headers(), used with start_session(), produces HTML headers. When the PHP fragments are placed in the body of the HTML, this causes errors because the PHP fragments are trying to put HTML headers within the HTML body.
Solution:
Put the PHP fragments before the HTML body tag.

Message:
xlf: 1501-230 Internal compiler error; please contact your Service Representative
Occurance:
On an IBM SP2, when trying to compile a simple MPI test program using either mpxlf, mpxlf90 or /usr/local/mpi/bin/mpif77
Cause:
There can may be several causes to this error message. But in this particular case, including the option "-qnolm" solved the problem.
Solution:
Include the compiling option "-qnolm" which is to "Disables license management."


Message:
f90 INTERNAL: Unable to open message system. *** Error code 2 (bu21)
Occurance:
Compiling in SGI using MipsPro Compiler f90 or CC.
Cause:
Incorrect variable LANG.
Solution:
export LANG=C or setenv LANG C


Message:
XArray.tem.cpp(78): error: expected an identifier T_index = item.T_index;
Occurance:
C++ programming
Cause:
In the example above, T_index is supposed to be a typename (eg int). This should be followed by a variable name such as: T_index var_name; instead of the equal sign.
Solution:
Make the statement into a declaration like: T_index varname;


Message:
initfutil.o(.text+0x117): undefined reference to `mpir_init_flog_'
initfutil.o(.text+0x11f): undefined reference to `mpir_init_fcm_'
/short/f01/chee/MPICH_SRC/mpich-1.2.5/lib/libmpich.a(initfutil.o): In function `MPIR_InitFortranDatatypes':
initfutil.o(.text+0x193): undefined reference to `mpir_get_fsize_'
make[4]: *** [overtake] Error 1
Occurance:
Compiling MPICH-1.2.5 using Intel Fortran and C/C++
Compile using: ifc -g -axW -xW -ipo -tpp7
Happens during MAKE process
Cause:
The function mpir_init_flog for example, is declared (prototyped) and also called from the c-code mpich-1.2.5/src/fortran/src/initfutil.c.
However the function mpir_init_flog is defined in the fortran-code mpich-1.2.5/src/fortran/src/initfcmn.f.
When compiled with ifc -ipo, the object file initfcmn.o is NOT READABLE by other compilers or linkers such as icc. Therefore during the "make" process, the reference was UNDEFINED.
Solution:
Avoid using -ipo with with ifc, if the object code needs to be linked with other programs which are not compiled with ifc.


Message:
/short/f01/chee/MPICH_SRC/mpich-1.2.5/bin/mpicc -O -DHAVE_ROMIOCONF_H -I/short/f01/chee/MPICH_SRC/mpich-1.2.5/include -I/short/f01/chee/MPICH_SRC/mpich-1.2.5/romio/adio/common/../include -I../include -c ad_fstype.c
/usr/include/bits/statfs.h(27): error: identifier "__SWORD_TYPE" is undefined __SWORD_TYPE f_type;
/usr/include/bits/statfs.h(28): error: identifier "__SWORD_TYPE" is undefined __SWORD_TYPE f_bsize;
/usr/include/bits/statfs.h(43): error: identifier "__SWORD_TYPE" is undefined __SWORD_TYPE f_namelen;
/usr/include/bits/statfs.h(44): error: identifier "__SWORD_TYPE" is undefined __SWORD_TYPE f_spare[6];
Occurance:
Compiling MPICH-1.2.5 using Intel Fortran and C/C++ on Linux Cluster
Happens during MAKE process
Cause:
Unable to find the system file where __SWORD_TYPE is defined.
Typically for linux systems, it is located in the file
/usr/include/bits/statfs.h and /usr/include/sys/statfs.h
Solution:
Add the following to the INCLUDE path in the makefile or configure option.
-I/usr/include

Message:
undefined reference to `iargc_'
or
undefined reference to `getarg_'
Occurance:
Compiling Fortran programs in using non-Unix Fortran compilers, including Intel Fortran
Cause:
The function iargc and the subroutine getarg are commonly available functions in Unix and is understood by the g77 compiler. However, they are not standard Fortran commands, and must be explicitly included during compilation.
Solution:
For Intel Fortran, add the flag during compilation:
ifc -Vaxlib .......


Message:
Error 372 : Automatic variable VARa declared in non-procedure testt
Occurance:
In Fortran programs, such as:
program testt
integer VARa(inum)
....
end program
Cause:
Cannot have dynamic arrays or even adjustable arrays in the main program. But can have this in sub-programs
Solution:
Transfer the section of code to a subroutine
program testt
integer inum
inum = CONSTNUM
call subA(inum)
end program
subroutine subA(ival)
integer VARa(iVal)
....
end subroutine


Message:
f90-700 f90: ERROR HPHI_INTER, File = Hphi.f, Line = 1163, Column = 10
No specific intrinsic exists for the intrinsic call "MAXVAL".
cf90: "explain cf90-message number" gives more information about each message
Occurance:
Compilation with SGI MIPSpro Fortran90 compiler.
The error will occur at the point where an intrinsic function is used, such as:
dimCN = maxval(Ntotala)
Cause:
This compilation error is due to the argument, Ntotala in the example above, that is used in the intrinsic function, has not been defined.
Solution:
Ensure that the argument is defined.


Message:
Yellow Zone stack overflow, thread 2
forrtl: error (76): IOT trap signal
Yellow Zone stack overflow, thread 3
forrtl: error (76): IOT trap signal
Yellow Zone stack overflow, thread 4
forrtl: error (76): IOT trap signal
0: __FINI_00_remove_gp_range [0x3ff81a1fec8]
1: __FINI_00_remove_gp_range [0x3ff81a29330]
2: __FINI_00_remove_gp_range [0x3ff800d5ba0]
3: __FINI_00_remove_gp_range [0x3ff8057d674]
4: __FINI_00_remove_gp_range [0x3ff80576450]
0: __FINI_00_remove_gp_range 5: __FINI_00_remove_gp_range [0x3ff8058d9e8]
[0x3ff81a1fec8]
6: __FINI_00_remove_gp_range [0x3ff8013bbd4]
1: __FINI_00_remove_gp_range [0x3ff81a29330]
7: __FINI_00_remove_gp_range [0x3ff801a2e10]
2: __FINI_00_remove_gp_range [0x3ff800d5ba0]
8: __FINI_00_remove_gp_range [0x3ff801c6110]
3: __FINI_00_remove_gp_range [0x3ff8057d674]
9: __FINI_00_remove_gp_range [0x3ff807e3bb0]
4: __FINI_00_remove_gp_range [0x3ff80576450]
10: __FINI_00_remove_gp_range [0x3ff807e25b4]
5: __FINI_00_remove_gp_range [0x3ff8058d9e8]
11: __FINI_00_remove_gp_range [0x3ff80582460]
6: __FINI_00_remove_gp_range [0x3ff8013bbd4]
12: __FINI_00_remove_gp_range [0x3ff805824dc]
7: __FINI_00_remove_gp_range [0x3ff801a2e10]
13: __FINI_00_remove_gp_range [0x3ff80594090]
8: __FINI_00_remove_gp_range [0x3ff801c6110]
14: __FINI_00_remove_gp_range [0x3ff8057d478]
9: __FINI_00_remove_gp_range [0x3ff807e3bb0]
15: _1258_hphi_interparam_ 0: __FINI_00_remove_gp_range [0x3ff81a1fec8]
1: __FINI_00_remove_gp_range [0x3ff81a29330]
2: __FINI_00_remove_gp_range [0x3ff800d5ba0]
3: __FINI_00_remove_gp_range [0x3ff8057d674]
4: __FINI_00_remove_gp_range [0x3ff80576450]
5: __FINI_00_remove_gp_range [0x3ff8058d9e8]
10: __FINI_00_remove_gp_range [0x3ff807e25b4]
6: __FINI_00_remove_gp_range [0x3ff8013bbd4]
11: __FINI_00_remove_gp_range [0x3ff80582460]
7: __FINI_00_remove_gp_range [0x3ff801a2e10]
8: __FINI_00_remove_gp_range [0x3ff801c6110]
12: __FINI_00_remove_gp_range [0x3ff805824dc]
9: __FINI_00_remove_gp_range [0x3ff807e3bb0]
13: __FINI_00_remove_gp_range [0x3ff80594090]
10: __FINI_00_remove_gp_range [0x3ff807e25b4]
14: __FINI_00_remove_gp_range [0x3ff8057d478]
11: __FINI_00_remove_gp_range [0x3ff80582460]
15: _1258_hphi_interparam_ 12: __FINI_00_remove_gp_range [0x3ff805824dc]
13: __FINI_00_remove_gp_range [0x3ff80594090]
14: __FINI_00_remove_gp_range [0x3ff8057d478]
15: _1258_hphi_interparam_ [Hphi.f: 1258, 0x12003f650]
16: __FINI_00_remove_gp_range [0x3ff81dbdba0]
17: __FINI_00_remove_gp_range [0x3ff81dbe194]
18: __FINI_00_remove_gp_range [0x3ff8058a4a8]
forrtl: severe (174): SIGSEGV, segmentation fault occurred
forrtl: severe (174): SIGSEGV, segmentation fault occurred
forrtl detected SIGSEGV loop - aborting, nonrecoverable error.
forrtl detected SIGSEGV loop - aborting, nonrecoverable error.
/var/spool/pbs/mom_priv/jobs/936116.sc0.SC: line 26: 12958396 IOT/Abort trap
test.exe
Occurance:
Using OpenMP with Fortran Compilers on
Compaq Alpha - Compaq Fortran (formerly Digital Fortran)
Linux Cluster - Intel Fortran Compiler
SGI - MIPSpro Fortran
Cause:
The stack size for child threads are overflowing. The main stack size for the program is changed by the ulimit command (in Bash shell) or limit command (in C shell). However this environment variable does not set the size for the child thread stack size. Thus the child thread stack overflow.
Solution:
Set the environment variables to increase the child thread stack size.
#for intel
export KMP_STACKSIZE=500000000
#for SGI
export MP_SLAVE_STACKSIZE=500000000
#for CompaqDEC
export MP_STACK_SIZE=500000000


Message:
OMP abort: stack overflow detected (address = 0x48439694) for OpenMP thread #1!
Occurance:
Using OpenMP with Fortran Compilers on
Compaq Alpha - Compaq Fortran (formerly Digital Fortran)
Linux Cluster - Intel Fortran Compiler
SGI - MIPSpro Fortran
Cause:
The stack size for child threads are overflowing. The main stack size for the program is changed by the ulimit command (in Bash shell) or limit command (in C shell). However this environment variable does not set the size for the child thread stack size. Thus the child thread stack overflow.
Solution:
Set the environment variables to increase the child thread stack size.
#for intel
export KMP_STACKSIZE=500000000
#for SGI
export MP_SLAVE_STACKSIZE=500000000
#for CompaqDEC
export MP_STACK_SIZE=500000000




Message:
[ 0 (4019240)]: segv (11). code 28, pc 0x100a0ae0, vaddr 0xfff93655d0, alt-sp 0x1b34fc20 /var/spool/pbs2.3.16-n32/mom_priv/jobs/17447.clare.SC: line 19: 4019240 Segmentation fault (core dumped) test.exe
Occurance:
Using OpenMP with Fortran Compilers on
Compaq Alpha - Compaq Fortran (formerly Digital Fortran)
Linux Cluster - Intel Fortran Compiler
SGI - MIPSpro Fortran
Cause:
The stack size for child threads are overflowing. The main stack size for the program is changed by the ulimit command (in Bash shell) or limit command (in C shell). However this environment variable does not set the size for the child thread stack size. Thus the child thread stack overflow.
Solution:
Set the environment variables to increase the child thread stack size.
#for intel
export KMP_STACKSIZE=500000000
#for SGI
export MP_SLAVE_STACKSIZE=500000000
#for CompaqDEC
export MP_STACK_SIZE=500000000



Message:
** Address error **
Occurance:
Produced by Intel Fortran compiled code whenever a segmentation fault occurs
Cause:
Could be any reason a segmentation fault occurs - eg accessing an element beyond an array bound.
Solution:
Try running the code in a debugger (gdb or idb) to determine where it is falling over. Try turning on bounds checking (-C option). If the code fails at high levels of optimisation but not low, try the "binary chop" method to isolate the code that causes the error.


Message:
Filename missing or blank - please enter name UNIT 23?
Occurance:
Fortran - with various compilers including Intel, MIPSpro
Cause:
When you have code like:
Case 1: open(23, file=' ')
Case 2: open(23, file='fort.23')
Solution:
Case 1: is legal for some compilers, although this is not Standard Fortran. To solve this, give as many arguments as there are code with "un-named" files, when running the program. Eg.
a.out file1 file2 file3
Case 2: This is an Intel Fortran bug which confuses "fort.23" as some sort of RESERVED name. To solve this, change the file name from "fort.23" to something other than "fort....". Eg.
open(23, file='solve.bug')


Message:
INTERNAL ERROR at line 31: mismatch in statements 'comma' 'rcomma'
Occurance:
SGI IRIX platform
C programs
cc MIPSpro Compilers: Version 7.3.1.3m
Cause:
Possible bug in the Compiler which causes the following code to produce the INTERNAL ERROR
#pragma omp for
for (int ii=1; ii<3; ii++)
Solution:
Solved by declaring the "int" outside the #pragma
int ii;
#pragma omp for
for (ii=1; ii<3; ii++)



Message:
cc-1491 CC: ERROR File = octave.gperf, Line = 96
A template argument cannot reference an unnamed type
Occurance:
SGI - MIPSpro C/C++ compiler
Note: This problem seems to be non-existant in other compiler/platforms.
Cause:
The problem comes from using an unnamed enumeration type. For example having the enumeration type:
enum {
TOTAL_KEYWORDS = 37,
MIN_WORD_LENGTH = 2,
MAX_WORD_LENGTH = 22,
MIN_HASH_VALUE = 2,
MAX_HASH_VALUE = 75
};

and the error will occur in a line such as:
if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH)
Solution:
Give the enum structure a name, eg:
enum enumOctGperf {
TOTAL_KEYWORDS = 37,
MIN_WORD_LENGTH = 2,
MAX_WORD_LENGTH = 22,
MIN_HASH_VALUE = 2,
MAX_HASH_VALUE = 75
};


Message:
LNK2001: unresolved external symbol _main"
"LNK2001: unresolved external symbol _WinMain

Cause:
This usually happens when you create the wrong type of project for you application.
In the first case, you chose "Win32 Application" when you should have selected
"Win32 Console Application". On the second case, well, it's the other way.

Solution:
(1) Create a new project of the correct type, and read all your files too it, or
(2) change the type of the current project. This can be easily accomplished.
Select Project -> Settings from the menu, and go to the Link tab. There, in the
edit control at the bottom, look for the /subsystem switch and change it to
"/subsystem:console" or "/subsystem:windows" as appropriate


Message:
"This application has failed to start because MSVCRTD.dll was not found. Re-installing the application may fix this problem."
Cause:
Solution:
DO NOT REINSTALL application - if you can create a new solution and run a new solution without crashing.


Message:
"fatal error LNK1181: cannot open input file '\naunsw002\cheec\Visual Studio Projects\ccTestPlatform\CBAtestPlatform\debug\cbatestplatform.lib'"
Cause:
ccTestPlatform Project Properties have selected the CBATestPlatform as a project dependency. Somehow this will not work. Note that CBATestPlatform.lib has already been added to the project via "Add Existing item".
Solution:
In the ccTestPlatform Project Properties, remove dependency to the CBATestPlatform project.

Message:
CBAtestPlatform "fatal error LNK1104: cannot open file 'CSIROmock.lib'"
Cause:
Within the Fortran code, there is a line which points to a dll,
eg. !dec$objcomment lib:'CSIROmock.lib'
However, the linker does not know where to find it even if the lib and dll files are in the projects "debug" sub-directory.
Solution:
Right click on the project -> Properties -> Linker -> General -> "Additional Library Directories"
-> then enter the directory path eg. "U:\Visual Studio Projects\CSIROmock\CSIROmock\Debug"


Message:
"CBAtestPlatform error LNK2019: unresolved external symbol _CDFINVPOISSON referenced in function _GeneratePoissonNumEvents
CBAtestPlatform fatal error LNK1120: 1 unresolved externals"
Cause:
Fail to recognize or find the function or subroutine called cdfinvpoisson
Solution:
1. Write an Fortran "interface" of the function (eg cdfinvpoisson) and put that interface within the code of the calling routine.
2. IN ADDITION, need to indicate where the *.lib file for the dll is. For example, given cdfinvpoisson is in the CsiroOpRiskLibrary.dll,
i) write this at the start of the source code for GeneratePoissonNumEvents:
!dec$objcomment lib:'CsiroOpRiskLibrary.lib'
OR
ii) in VS.Net, In the Solution Explorer, right click on the working project, select Project Dependencies, and
tick the project which contains CsiroOpRiskLibrary.


Message:
Error: prPromoteSym : Illegal KIND & CLASS mix [RISKDRIVER]
Cause:
A derived type has been defined in a module with the DLLEXPORT attribute.
The derived type cannot be initialised using the "structure constructor" technique.
Solution:
Remove the DLLExport attribute for the derived type in the module.



Message:
cctestplatform.exe: 0xC0000005: Access violation reading location 0x83ec8b55
Cause:
Tyring to call a subroutine in another module which is part of the same project, where the subroutine to be called has been DLL exported.
Solution:
Create a wrapper routine and put in the same module as the subroutine to be called. Then from the calling program, call this wrapper name.


Message:
Unhandled exception at 0x004952b2 in .........: 0xC0000005: Access violation reading location 0x41726570.
Cause:
Possibly initialized a variable with the wrong data. Eg.
integer*4 :: dDummy
dDummy = 0.0d0
Solution:
Check that all data variables are initialized with the correct data type.


Message:
Compilation Aborted (code 3)
Cause:
Could be memory allocation error. Or Not using new dll
Solution:
Copy new DLL into directory


Message:
Unhandled Exception at .....
Cause:
1. Memory Mismatch in arguments. Happens when a subroutine has explicitly sized arguments eg:
subroutine foo(arrA, iNum)
integer :: iNum
real :: arrA(iNum)
.....
If the caller puts in a wrond sized array or the wrong size value, then an error will occur. Eg:
allocate (arrB(10))
call foo(arrB, 15)
.......
This error cannot be found by looking at the size from within the subroutine because:
print *, size(arrA), iNUm
would print the same value because the array with the wrong size has been passed in.
Solution:
Use assumed shape arrays for dummy arguments rather than explicit size.


Message:
Error: The same named entity from different modules and/or program units cannot be referenced. [MCSINGLEEXPOSURE_VB]
Cause:
"Use " has been used recursively, typically in an interface section, the function interface
declares to use the same module resides in.
Solution:
In the interface of the function, the "use " can be specified to restrict the usage to certain data
structures using "only" keyword.
Example:
module mod_foo
type AAA
end type AAA
contains
function foo_A
...
end module
function OutF()
interface
function foo_A
use mod_foo, only: AAA
end function foo_A
end interface
.....
end funtion OutF


Message:
MKL ERROR : Parameter 2 was incorrect on entry to vslNewStre
Cause:
using MKL, VSL, VML routines from intel, and having directives like:
!dec$objcomment lib:'mkl_c_dll.lib'
!dec$objcomment lib:'mkl_ia32.lib'
are missing the path to the ...mkl\ia32\lib
Solution:
In VS.Net, within the dll/console project that uses them, add the path to the library files in:
Project -> Properties -> Linker -> General -> Additional Library Directories


Message:
Re: Error PRJ0019 : A tool returned an error code from "Performing registration"
Cause:
When compiling in VS.Net, and in the Post-Build-Events (in the Post-Build option), if it is designed
to copy the generated file to another directory automatically, then if the destination file is
being used by another user or it is over the network, then this error occurs.
Example is building a DLL file into the release directory, and having a Post-Build-event to copy the
DLL into windows\system32, and someone is actually using it. Or the destination to copy is in the I drive.
Solution:
Make sure no one else is using the file or remove this PostBuild rule.


Message:
The procedure entry point __powr8i4 could no be located in the dynamic link library libmmd.dll.
The procedure entry point __XERBLA could no be located in the dynamic link library imsl_dll.dll.
Cause:
An older version of the dll which has not got the function, is being used.
Solution:
Define the path with newest installation first.

Message:
Service Application Unavailable
Cause:
ASP.Net webpage not displaying because the web directory is located in a folder without proper
permissions.
Solution:
Grant permission to users. In my case the reason was a missing
\Users user. all I had to do to to give the ASPNET worker process
access to my web project (under WinXP / IIS 5.1):

browse to the web project directory
right-click and select properties
select the security tab
Click the Add button
Type "Users" into the "Enter the object names..." textbox and click "Check Names"
A user named [COMPUTER-NAME]\Users should be displayed.
Click OK.
The "ASP.NET Machine Account" should now be listed in the
"Group or user names:" textbox. By default, Read & Execute, List Folder
Contents and Read will be selected, however, you should be able to
run your site using only the Read permissions. This should be fine for the
permissions needed to run your site through IIS.


Message:
Checking if your computer has the latest version of Windows updating software for use with the website
Cause:
Something gone wrong during Windows Update or other causes and so causing subsequent Windows Update
to give the above message
Solution:
1. From MS site http://support.microsoft.com/kb/946413
**********
You receive error code 0x80244001 when you try to update the computer by visiting the Microsoft Update Web site or the Windows Update Web site
View products that this article applies to.
Article ID : 946413
Last Review : March 27, 2008
Revision : 1.0
On This Page

SYMPTOMS

CAUSE

RESOLUTION

Method 1: Register the Msxml3.dll file

Method 2: Register the Windows Update .dll files

Method 3: Close all instances of Internet Explorer

MORE INFORMATION
SYMPTOMS
When you try to connect to the Windows Update Web site or to the Microsoft Update Web site to search for updates, you receive error code 0x80244001.

Additionally, you may experience one or more of the following problems when you connect to the Windows Update Web site or to the Microsoft Update Web site to search for updates:• The Windows Update Web page may be blank.
• You are prompted to install an ActiveX control.
• The Windows Update Web site may appear to stop responding (hang) when you receive the following message:
Checking if your computer has the latest version of Windows updating software for use with the website
• You may receive an error message that resembles the following:
Files required to use Microsoft Update are no longer registered or installed on your computer

Back to the top

CAUSE
This problem may occur if a required system file is registered incorrectly.
Back to the top

RESOLUTION
To resolve this problem, use one of the following methods.
Back to the top

Method 1: Register the Msxml3.dll file
If you experience this problem on a computer that is running Windows XP or Windows Server 2003, register the Msxml3.dll file.

Note If you experience this problem on a computer that is running Microsoft Windows 2000-based, go to the next method.

To register the Msxml3.dll file, click Start, click Run, type regsvr32 msxml3.dll, and then click OK two times.
Back to the top

Method 2: Register the Windows Update .dll files
To resolve this issue, register the Windows Update .dll files and then try again to connect to the Windows Update Web site. To register the Windows Update .dll files, follow these steps:1. Click Start, click Run, type cmd, and then click OK.
2. At the command prompt, type regsvr32 wuapi.dll, and then press ENTER.
3. At the command prompt, type regsvr32 wuaueng.dll, and then press ENTER.
4. At the command prompt, type regsvr32 wuaueng1.dll, and then press ENTER.
5. At the command prompt, type regsvr32 wucltui.dll, and then press ENTER.
6. At the command prompt, type regsvr32 wups.dll, and then press ENTER.
7. At the command prompt, type regsvr32 wups2.dll, and then press ENTER.
8. At the command prompt, type regsvr32 wuweb.dll, and then press ENTER.
9. Try to connect to the Windows Update Web site again.

Back to the top

Method 3: Close all instances of Internet Explorer
To resolve this issue, close all the instances of Internet Explorer that are running on the computer. Then, try to connect to the Windows Update Web site or to the Microsoft Update Web site again. To connect to the Windows Update Web site or to the Microsoft Update Web site, click Start, and then click Windows Update or Microsoft Update.
Back to the top

MORE INFORMATION
You may be able to resolve the problem that is described in the "Symptoms" section by installing the Microsoft XML Parser (MSXML) 3.0 Service Pack 7 (SP7) update. For more information about how to download and install the MSXML 3.0 SP7 update, visit the following Microsoft Web site:
http://www.microsoft.com/downloads/details.aspx?familyid=28494391-052b-42ff-9674-f752bdca9582 (http://www.microsoft.com/downloads/details.aspx?familyid=28494391-052b-42ff-9674-f752bdca9582)
Back to the top
************

2. From http://www.archivum.info/microsoft.public.windowsupdate/2006-03/msg00164.html
When searching for available updates on the Windows Update site, you see the
0x8007043B error
This error may occur if the WUAUSERV and BITS services are not configured
properly in the registry.
http://v5.windowsupdate.microsoft.com/v5consumer/showarticle.aspx?articleid=19&ln=en

When searching for available updates on the Update site, you see the 0x8007043B
error
http://update.microsoft.com/windowsupdate/v6/showarticle.aspx?articleid=19&ln=en&IsMu=False

or


"Checking for updates, depending on your speed this process may take several
minutes"
Try registering the following:

Click Start, select Run and type (pressing enter after each one and wait for
the success message):

net.exe stop wuauserv
Press Ok

Repeat for the following:
regsvr32 wuapi.dll
regsvr32 wups.dll
regsvr32 wuaueng.dll
regsvr32 wuaueng1.dll
regsvr32 wuweb.dll
regsvr32 jscript.dll
regsvr32 wucltui.dll
regsvr32 msxml3.dll
regsvr32 softpub.dll
regsvr32 atl.dll

net.exe start wuauserv

3. From http://www.thetechguide.com/forum/lofiversion/index.php/t27833.html
Not sure if this will work, but can you try it please
Save these instructions too a text file then close down all browsers, including this one

Go to START>>RUN>>Type in, or copy and paste the following hitting OK after each command

net.exe stop wuauserv
regsvr32.exe wuapi.dll
regsvr32.exe wups.dll
regsvr32.exe wuaueng.dll
regsvr32.exe wucltui.dll
regsvr32.exe MSXML3.dll
net.exe start wuauserv

Afterwards go back to Windows updates



Message:
Not a message - Sticky keys in WinXP get activated by mistake.
Cause:
Due to Windows Accessibility Option which can be found under Control Panel.
Solution:
1. Try pressing both the left and right CTRL keys at the same time and
see if that releases CTRL.
2. Go to Start / Settings / Control Panel / Accessibility Options /
Keyboard Options.
Turn off CTRL lock if it's on.
If that solves it and to avoid future problems, press "Settings"
Turn off "Press modifier key twice to lock".
Turn on "Turn sticky keys off if 2 keys are pressed at once".
Or turn off "Use shortcut"
3. Clean your keyboard.



Message:
Not a message - Performance Monitor ( perfmon ) not working.
Cause:
There are several reasons to this. Rather than stating the numerous causes, a
brief description is given here about perfmon.
1. Perfmon is located in C:\windows\system32\perfmon.exe
2. A configuration or data file it uses is C:\windows\system32\perfmon.msc
3. It has some library files C:\windows\system32\perfX009.dat, where
X = 'c', 'd', 'h', 'i'
4. It has got various dlls C:\windows\system32\perf*.dll
5. It has also got various entries in the registry.
The corruption of any of these may cause perfmon to stop working.

Solution:
1. Quick and dirty way is to find a PC where perfmon is working and copy any necessary
perfmon files which are corrupted on your machine.
2. Correct the registry entries. This may be dangerous. Check:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Disable Performance Counters
3. See the links below for more info or
Google: "performance monitor" regedit disable.

http://www.dotnet247.com/247reference/msgs/51/255103.aspx

PRB: Performance Object Is Not Displayed in Performance Monitor
http://support.microsoft.com/kb/248993

Troubleshooting Performance Monitor Counter Problems
http://kbalertz.com/152513/Troubleshooting-Performance-Monitor-Counter-Problems.aspx

Q248993 - 248993 - PRB: Performance Object Is Not Displayed in Performance Monitor

Q249138 - 249138 - INFO: Controlling the Disabling of Performance Monitor Extensions

Q300956 - 300956 - How to Manually Rebuild Performance Counter Library Values




Message:
Unhandled Exception: System.IO.FileLoadException: Could not load file or assembl
y 'ConsoleDriver, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one
of its dependencies. Failed to grant minimum permission requests. (Exception fro
m HRESULT: 0x80131417)
File name: 'ConsoleDriver, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
' ---> System.Security.Policy.PolicyException: Required permissions cannot be ac
quired.
at System.Security.SecurityManager.ResolvePolicy(Evidence evidence, Permissio
nSet reqdPset, PermissionSet optPset, PermissionSet denyPset, PermissionSet& den
ied, Boolean checkExecutionPermission)
at System.Security.SecurityManager.ResolvePolicy(Evidence evidence, Permissio
nSet reqdPset, PermissionSet optPset, PermissionSet denyPset, PermissionSet& den
ied, Int32& securitySpecialFlags, Boolean checkExecutionPermission)

Cause:
The above message may be a general message that can be attributed to a number of different errors.
The list of causes below is therefore not limited, please send us other causes that you have found.
1. A managed code (eg C# application) is being run on a network drive (hence lack of permission).

Solution
1. Move the executable and related applications to run from a local drive.


Message:
Unhandled Exception: System.DllNotFoundException: Unable to load DLL 'xxxxx.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
Cause:
The above message may be a general message that can be attributed to a number of different errors.
The list of causes below is therefore not limited, please send us other causes that you have found
1. If you are deploying an application build / compiled on one machine, to another machine, then on that remote machine,
some files may be missing.
Solution:
1. Use Dependency Walker to scan your application's exe and / or the dlls that you build. From Dependency Walker, look for any
dependent dlls that are not found in the remote machine.
Example: this error may happen if msvcr80.dll or msvcr80d.dll are not in the remote machine.


Message:
OMP: Error #15: Initializing libguide.lib, but found libguide40.dll already initialized.
OMP: Hint: This may cause performance degradation and correctness issues. Set environment variable KMP_DUPLICATE_LIB_OK=TRUE to
ignore this problem and force the program to continue anyway.
Cause:
Occur when running within VS.Net and Intel Fortran. In particular it is because libraries, eg. IMSL_DLL.DLL is linked
into the application, but the application is compiled as static.
Solution:
Compile the program as using dynamic libraries by setting:
Project -> Properties -> Fortran -> Libraries -> Runtime Library -> Debug Multithreaded (/libs:dll /threads /dbglibs)


Message:
OMP: Warning #18: Setting environment variable "__KMP_REGISTERED_LIB_4504" failed:
OMP: System error #203: The system could not find the environment option that was entered
OMP: Error #110: Clearing __KMP_REGISTERED_LIB env var failed.
Cause:
Occur when running within VS.Net and Intel Fortran. In particular OpenMP as well as MKL is required in the code.
The conflict arise when using the threaded version of MKL with OpenMP dlls.
Solution:
To use threaded MKL, there is an option to use libguide40.lib (older) or libiomp5md.lib. To avoid this error, use
libguide40.lib


Message:
'CS0246: The type or namespace name 'type/namespace' could not be found (are you missing a using directive or an assembly reference?)'.
Cause:
The .NET framework enables a number of different build configurations. The first category choice is
{Release, Debug} and the second is processor dependent {x86, x64, anycpu}.
Occurs when updating code, particularly config files or settings, compile errors may occur or it may throw the
above error.
Solution:
It is recommended to clean the solution for all build configurations if these type of errors are encountered.
To do this, right click solution and select batch build. Then select all files and clean solution.
Then batch build solution.



****************
-no dll and no lib
-no lib
ccTestPlatform fatal error LNK1181: cannot open input file '\\naunsw002\cheec\Visual Studio Projects\ccTestPlatform\ccTestPlatform\Debug\CSIROmock.lib'




/nologo /Zi /Od /include:"\\naunsw002\cheec\Visual Studio Projects\ccTestPlatform\ccTestPlatform\\Header Files" /module:"$(INTDIR)/" /object:"$(INTDIR)/" /traceback /check:bounds /libs:static /dbglibs /c