woensdag 26 oktober 2016

Gebruik van eigen gemaakte Python modules en Package

Hoe gebruik ik een eigen gemaakte module

bijv in H:\python\mymodules  maak ik 2 pythonfiles aan testmod1.py en testmod2.py

#-------------------------------------------------------------------------------
# Name:       testmod1
# Purpose:
#-------------------------------------------------------------------------------

def main():
    pass

if __name__ == '__main__':
    main()


def testmod1():
    print "Ik ben testmod1"


Als ik deze wil gebruiken in een python editor dan moet de module bekendgemaakt worden

One way is to add a .pth to your site-packages location. Ga naar site-packages in 
C:\Python27\Lib\site-packages

Maak een .pth file aan bijv my_custom_python_modules.pth

Zet hierin paden naar eigen gemaakte moduless
>>  H:\python\mymodules



Package

Als je van meerdere modules een package wil maken dan doe je als volgt

bijv Maak een package MYMODUlLES met alle daaronder zich bevindende modules

Dus  H:\python\mymodules  bevat meerde .py files: testmod1.py en testmod2.py

stap 1. Maak in H:\python\mymodules een file aan
 __init__.pyc

stap 2  Vul deze __init__.pyc met alle modules available
from testmod1 import testmod1
from testmod2 import testmod2

Voeg in de  my_custom_python_modules.pth ook het pad H:\Python toe



Python links

http://www.pythonforbeginners.com/lists/python-lists-cheat-sheet


Python Strings en argumenten

Printing

Print on the same line

print('One', end=' ')
print('Two', end=' ')
print('Three')
==> One Two Three

>>> print('One', 'Two', 'Three', sep='*') One*Two*Three
>>>

concatenation

print('Enter the amount of ' + \
'sales for each day and ' + \
'press Enter.')

string comparing

if name1 == name2:
      print('The names are the same.')
else:
      print('The names are NOT the same.')

Functions

Functions (value returning)


def function_name():
   statement
   statement
   etc.
return expression


- Value can be string or number

- value can be boolean:  True or False
- value can be



Arguments by value

The form of argument passing that is used in Python, where a function cannot change the
value of an argument that was passed to it, is commonly called pass by value. This is a way
that one function can communicate with another function. The communication channel
works in only one direction, however. The calling function can communicate with the called
function, but the called function cannot use the argument to communicate with the calling
function

Datums bepalen adhv begin en einddatum in Dataframe

Voorbeeld op losse velden  ####################################################################### # import necessary packages from datetime...