Monday, July 10, 2017

Summer of code 2017: Python, Day 23 Files



As explained in my Summer of code 2017: Python post I decided to pick up Python

This is officially day 23. today I will take a look at files

First lets take a look at the open() function, you need to pass in the path, everything else is optional

You should always pass in the mode for clarity

Here are all the open() modes

Character Meaning
'r' open for reading (default)
'w' open for writing, truncating the file first
'x' open for exclusive creation, failing if the file already exists
'a' open for writing, appending to the end of the file if it exists
'b' binary mode
't' text mode (default)
'+' open a disk file for updating (reading and writing)
'U' universal newlines mode (deprecated)


Writing to a file in Python

Here is an example of how to write to a file

= open('boo.txt', mode ='wt', encoding='utf=8')
f.write('the first line')
f.write('line 2\n')
f.write('line 3')
f.write('line 4')
f.close()

Here is what the file looks like after running the example above


As you can see if you don't supply line breaks, there won't be any line breaks in the file, this is why there is only 1 line break in the file

If you run the same example again, the same exact file will be created, this is because mode w will truncate the file first

If you change the mode from wt to at, then the text will be appended

= open('boo.txt', mode ='at', encoding='utf=8')
f.write('the first line')
f.write('line 2\n')
f.write('line 3')
f.write('line 4')


Reading a file in Python

Reading files is similar to writing files, you still use open and specify mode and encoding
To read the first 10 characters, you can specify read(10)
If you want to read the whole file, you can just specify read(), if you specify read() again, you will get back an empty string. To start reading from the beginning again, you can use seek(0)
To read a line at a time, use readline()

Here is how all this above looks inside the REPL

>>> f = open('boo.txt', mode ='rt', encoding='utf=8')
>>> f.read(10)
'the first '
>>> f.read()
'lineline 2\nline 3line 4the first lineline 2\nline 3line 4the first lineline 2\nline 3line 4'
>>> f.read()
''
>>> f.seek(0)
0
>>> f.readline()
'the first lineline 2\n'
>>> f.readline()
'line 3line 4the first lineline 2\n'
>>> f.readline()
'line 3line 4the first lineline 2\n'
>>> f.readline()
'line 3line 4'
>>> f.readline()
'' 


That is all for today,  short and sweet....

Friday, July 7, 2017

Summer of code 2017: Python, Day 20 Classes


As explained in my Summer of code 2017: Python post I decided to pick up Python

This is officially day 20. today I will take a look at classes

All types in Python have a class
Class instances are created by calling the class as if it were a function


Here is the simplest form of a class

class Test:
    """description of class"""
 
   
 
    def val(self, val):
        return val

If we now instantiate the class and pass in the string "denis' to the val method, the string is returned


>>> f = Test()
>>> print(f.val("denis"))
denis
>>> 

In Python self is similar to this in Java or c#

Implementation details are denoted by a leading underscore, in Python there are no public, protected or private access modifiers like in Java or c#



Here is an example of the class where we added __init__ and we are also returning _val instead of val

class Test:
    """description of class"""
 
    def __init__(self, val):
        self._val = val
 
    def val(self):
        return self._val

Here is what the output looks like now

>>> f = Test("denis")
>>> print(f.val())
denis
>>> f = Test("yo")
>>> print(f.val())
yo
>>> 

As you can see it is all pretty simple stuff at this point

To do inheritance, you add the base class name in parentheses after the class name. So if a class SomeTest inherits from Test, you would specify it like this:  class SomeTest(Test)

Here is what the code looks like for both classes

class Test:
    """description of class"""
 
    def __init__(self, val):
        self._val = val
 
    def val(self):
        return self._val
 
 
class SomeTest(Test):
    """description of class"""
 
 
    def someVal(self):
        return self._val


Now we can call both the val and someVal methods from the SomeTest class because the val method exists in the test class from which the SomeTest class inherits

Here is the output

>>> f = Test("yo")
>>> print(f.val())
yo
>>> g = SomeTest("Den")
>>> print(g.val())
Den
>>> print(g.someVal())
Den
>>> 


I am sure I will be revisiting and adding to my notes when I start messing around with classes more

That is it.. a short post for a short Holiday week in the US


Wednesday, July 5, 2017

Summer of code 2017: Python, Day 18 Iterables


As explained in my Summer of code 2017: Python post I decided to pick up Python

This is officially day 18. today I will take a look at iterables


Comprehensions

A comprehension is a list, a set or a dictionary that is computed via a set of looping and filtering instructions. The comprehension consists of a single expression followed by at least one for clause and zero or more for or if clauses

List Comprehensions

The basic syntax for list comprehensions looks like this

[expr(item) for item in iterable]

Here is an example in code, the code below returns the length for each item in the list

>>> words= "A comprehension is a list, a set or a dictionary that is computed via a set
 of looping and filtering instructions".split()
>>> words
['A', 'comprehension', 'is', 'a', 'list,', 'a', 'set', 'or', 'a', 'dictionary', 'that',
 'is', 'computed', 'via', 'a', 'set', 'of', 'looping', 'and', 'filtering', 'instructions']
>>> [len(word) for word in words]
[1, 13, 2, 1, 5, 1, 3, 2, 1, 10, 4, 2, 8, 3, 1, 3, 2, 7, 3, 9, 12]
>>> 

Pretty simple stuff, nothing too complicated here


Set Comprehensions

The basic syntax for set comprehensions looks like this

{expr(item) for item in iterable}

Here is an example in code, the code below returns the length as well as the max character for each item of the set

>>> x = {'abc','def','vvv','nnn'}
>>> type(x)
<class 'set'>
>>> {len(str(x)) for x in x}
{3}
>>> {max(str(x)) for x in x}
{'f', 'v', 'n', 'c'}
>>> 

As you can see the set is distinct, the number 3 is only returned once. Also the set is unordered



Dictionary Comprehensions


The basic syntax for dictionary comprehensions looks like this

{ key_expr:value_expr  for item in iterable }

Here is an example in code, the code below will swap the key value pair

>>> stocks ={
'TGT' :  '51.33', 
'AAPL': '147.48',
'MSFT':  '71.49',
'F'   :  '11.12', 
'INTC':  '34.37'}
>>> stocks
{'TGT': '51.33', 'AAPL': '147.48', 'MSFT': '71.49', 'F': '11.12', 'INTC': '34.37'}
>>> price_to_stock = {ticker: price for price, ticker in stocks.items()}
>>> price_to_stock
{'51.33': 'TGT', '147.48': 'AAPL', '71.49': 'MSFT', '11.12': 'F', '34.37': 'INTC'}
>>> 


We can make it more interesting by using a condition and only return the key value pair if the price is the max price for that ticker. I added a price of 1000 for AAPL and a price of 5000 for F. As you can see in the code below, I have used a filtering predicate if max(price)

>>> stocks ={
'TGT' :  '51.33', 
'AAPL': '147.48',
'AAPL': '1000',
'MSFT':  '71.49',
'F'   :  '11.12', 
'F'   :  '5000',
'INTC':  '34.37'}
>>> price_to_stock = {price: ticker for ticker, price in stocks.items() if max(price)}
>>> price_to_stock
{'51.33': 'TGT', '1000': 'AAPL', '71.49': 'MSFT', '5000': 'F', '34.37': 'INTC'}
>>> 

As you can see for Apple (AAPL), we return the pair with the price of 1000, for F (Ford), we return the pair with the price of 5000, the other prices for AAPL and F are not returned

Generators

Iterator

An object representing a stream of data. Repeated calls to the iterator’s __next__() method (or passing it to the built-in function next()) return successive items in the stream.

>>> i = ['a', 'b', 'c', 'd' ]
>>> iterator =iter(i)
>>> next(iterator)
'a'
>>> next(iterator)
'b'
>>> next(iterator)
'c'
>>> 

When no more data is available a StopIteration exception is raised instead. At this point, the iterator object is exhausted and any further calls to its __next__() method just raise StopIteration again.

Here is what that looks like

>>> next(iterator)
'd'
>>> next(iterator)
Traceback (most recent call last):
  File "", line 1, in 
    next(iterator)
StopIteration
>>> 

We can trap this by checking for the StopIteration exception, here is an example

>>> try:
 next(iterator)
except(StopIteration):
    print ('max value reached')

    
max value reached
>>> 



I also looked at itertools, itertools  implements a number of iterator building blocks inspired by constructs from APL, Haskell, and SML. Each has been recast in a form suitable for Python.

Here are some of these iterators

count()
cycle()
repeat()

accumulate()
chain()
chain.from_iterable()
compress()
dropwhile()
filterfalse()
groupby()
islice()
starmap()
takewhile()
tee()
zip_longest()


That was all for today, next I will look at classes

Monday, July 3, 2017

Summer of code 2017: Python, Day 16 Exceptions


As explained in my Summer of code 2017: Python post I decided to pick up Python

This is officially day 16. I took a couple of days off but today I looked at exceptions in Python, here are my notes


A couple of things to keep in mind when getting into exceptions



  • You can raise an exception to interrupt the flow of your program
  • You can handle an exception to resume control instead of the program being terminated
  • If you do not handle exceptions, unhandled exceptions will terminate the program.
  • Information about exception events will be contained in exception objects



Here is a simple function that converts an input to an integer, there is no exception handling done

>>> def converter(s):


    x = int(s)
    return x


>>> converter("33")
33
>>> converter("A")
Traceback (most recent call last):
  File "", line 1, in 
    converter("A")
  File "", line 4, in converter
    x = int(s)
ValueError: invalid literal for int() with base 10: 'A'
>>> 


As you can see it blows up with a ValueError exception

To catch an exception, you wrap a try except block around your statements, here is an example


>>> def converter(s):
 try:
  x = int(s)
 except ValueError:
  x = -1
 return x

>>> converter("33")
33
>>> converter("A")
-1
>>> 


As you can see no exceptions are displayed

You can also check for more than one exceptions like shown in the code below

>>> def converter(s):
    '''Convert to an integer.'''
    x = -1
    try:
        x = int(s)
        print("Conversion successful! x =", x)
    except ValueError:
        print("Conversion doomed!")
    except TypeError:
        print("Conversion doomed!")
    return x

>>> converter(33)
Conversion succesful! x = 33
33
>>> converter("a")
Conversion doomed!
-1
>>> converter(1.5555555)
Conversion succesful! x = 1
1
>>> converter([1,2])
Conversion doomed!
-1
>>>


We can simplify the code above by combining two exceptions into one check, you can see that in the code below

>>> 
def converter(s):
    '''Convert to an integer.'''
    x = -1
    try:
        x = int(s)
        print("Conversion successful! x =", x)
    except (ValueError, TypeError):
        print("Conversion doomed!")
    return x

>>> converter(33)
Conversion succesful! x = 33
33
>>> converter("a")
Conversion doomed!
-1
>>> converter([1,2])
Conversion doomed!
-1
>>> 


You need to have a statement after except, if you were to do the following, you will get a syntax error

>>> def converter(s):
    '''Convert to an integer.'''
    x = -1
    try:
        x = int(s)
        print("Conversion succesful! x =", x)
    except (ValueError, TypeError):
    return x
SyntaxError: expected an indented block


In Python to deal with a need for placeholder statement a pass statement can be used. Pass is a null operation, when it is executed, nothing happens. It is useful as a placeholder when a statement is required syntactically, but no code needs to be executed, for example:


>>> def converter(s):
    '''Convert to an integer.'''
    x = -1
    try:
        x = int(s)
        print("Conversion succesful! x =", x)
    except (ValueError, TypeError):
        pass
    return x

>>> 


These 3 errors should be handled during development and not at runtime

  • IndentationError
  • SyntaxError
  • NameError



If we want to print the exception that has been caught, you can assign it to a variable

Here is an example, e will hold the exception and then a print with a conversion to a string and format is used to print the exception to the console

>>> def converter(s):
    '''Convert to an integer.'''
    x = -1
    try:
        x = int(s)
        print("Conversion succesful! x =", x)
    except (ValueError, TypeError) as e:
        print("Conversion error: {}"\
        .format(str(e)),
        file=sys.stderr)
    return x

>>> import sys
>>> converter(10)
Conversion succesful! x = 10
10
>>> converter('a')
Conversion error: invalid literal for int() with base 10: 'a'
-1
>>> converter([1,2])
Conversion error: int() argument must be a string, a bytes-like object or a number, not 'list'
-1
>>> 


If you want to print the statement to the console but also raise the exception, you can use raise. Raise re-raises the last exception that was active in the current scope. Here is an example

def converter(s):
    '''Convert to an integer.'''
    x = -1
    try:
        x = int(s)
        print("Conversion succesful! x =", x)
    except (ValueError, TypeError) as e:
        print("Conversion error: {}"\
        .format(str(e)),
        file=sys.stderr)
        raise
    return x

>>> import sys
>>> converter(1)
Conversion succesful! x = 1
1
>>> converter('a')
Conversion error: invalid literal for int() with base 10: 'a'
Traceback (most recent call last):
  File "", line 1, in 
    converter('a')
  File "", line 5, in converter
    x = int(s)
ValueError: invalid literal for int() with base 10: 'a'
>>> 




If you have code that you want to run no matter what happens for example to 'cleanup' anything, you can use the finally keyword. Keep in mind that if you have multiple return statements, the one on the finally block is the one that will be returned.

Here is an example

>>> def foo():
 try:
  return 'trying...'
 finally:
  return 'finally'

 
>>> foo()
'finally'

That is all for today, I will continue with iterables next