Python code snippets

Reading a CSV of numbers to a list

import csv
with open(fileName, 'r') as read_obj:
	csv_reader = csv.reader(read_obj)
    list_of_csv = list(csv_reader)
# list_of_csv is a list[list[String]] where each list contains a row of the csv. 

Writing a list to a CSV

import csv
with open(fileName, 'w', newline='') as f:
  writer = csv.writer(f) # create the csv writer
  # write a row to the csv file
  write.writerow(fields)  # fileds is a list indicating column names or a string as a header
  writer.writerows(listOfRows) # each row is list/iterable List[List]
  # Or use writer.writerow(aRows) with a loop over a list/iterable of rows

Writing a File

 with open(filename, 'w') as f:
        for element in an_iterator:  # list, dict, etc...
        	f.write(rowString(element))  # rowString is a function or expression that 
          								 # makes string out of the content of element

Sorting a list of lists based on a specific index

z = [['a',2], ['v', 0.256]]
sorted(z, key = lambda item:  item[1])

Scala tips

Scala Option and Some

The absence of a value during rurtime should be somehow handled otherwise the will be a runtime error. The absent value is a null value. If we have a value of type D that may be absent (for example in method, function, class constructor), Scala uses an instance (object) of the (abstract) classOption[D] as its container. The class Option has two sub classes Some and None. Therefore, an instance of Option is an instance of either Some or None. For a variable x of type Option[D], the factory (of Option[D]) creates Some(x) if x != null or None if x == null .

The following are examples of situations to use Option:

1- Creating a type-Option val:

val optionalIntValue: Option[Int] = Some(1)
val optionalValue = Some("abc") // Scala infers the type
// or
// val optionalIntValue: Option[Int] = None  // type is needed

2- When a function may or may not return a value; we want to give it the option to return a value or not:

def myFunc: Option[String] = { ... }

3- When a parameter of a function can be present or absent, i.e being optional to be given:

def myFunc(param1: Option[D]): ...

case class MyClass(param1: Option[D], param2: String)

Keeping in mind that Option is just a type as a container for a value that can be absent, there are several ways to use an Option-type variable. Examples:

1- The most general way is to use an Option with a match. We can define the output of a method as an Option and handle the output. For example, a function that reads user input. User may just press Enter/Cancel and input nothing.

def readUserInput(): Option[processedUserInputType] = { ... }
readUserInput() match {
      case Some(outputOftheFunction) => // Do something with outputOftheFunction
      case None =>    // Do something when there is no output

This example is to show how to make a parameter of a function optional for the user. If not provided, it will be considered as a predefined value None.

def show(x: Option[String], y: Option[Int] = None) = {
  x match {
    case Some(s) => println(s)
    case None => println ("?")
  y match {
    case Some(number) => println( s"the number is ${number}")
    case None => println("no number")

val myInput1 = Some("hello bear")
show(myInput1) //  hello bear    no number
show(None) // ?       no number
show(None, y=Some(123)) // ?       the number is 123

2- Extracting the value of an Option using getOrElse method. A default value for the None case is mandatory.

val someValue1 =  Some(123)
val someValue2 : Option[Int] = None

someValue1.getOrElse(100) // 123
someValue2.getOrElse(100) // 100

Python common tips


Note that from myModule import * shouldn’t be used. If done, all the global variables defined in myModule will appear in the current namespace. Modulus should not pollute each other.

Lists and npArrays

1- Lists are (much) faster than npArrays when accessing or appending elements. However, npArrays are faster when doing linear algebra operations. It is more efficient to manipulate data in the form of lists and then convert them into npArrays when dealing with large data.


1- A set of sets: frozenset()

listOfSets1 = [{5},{1,2},{3,4},{5,7,9}]
listOfSets2 = [{11},{1,2},{12,16},{5,7,9},{7,23,13}]
setOfSets1 = {frozenset(eachSet) for eachSet in listOfSets1}
setOfSets2 = {frozenset(eachSet) for eachSet in listOfSets2}
Out[0]: {frozenset({5, 7, 9}), frozenset({1, 2})}


1- Positional vs keyword arguments

A function receives its arguments either in the form of positional or keyword arguments where order matters for positional arguments and argument’s name matters for keyword arguments. A keyword argument is recognized and used by its keyword/name in a function call. Examples are as follows.

def myFunction (arg1, arg2): # (position 1, position 2)
  # do something
# Equivalent function calls
myFunction (value1forArg1, value2forArg2) # position matters
myFunction (arg1 = value1forArg1, arg2 = value2forArg2)
myFunction (value1forArg1, arg2 = value2forArg2)
myFunction (arg2 = value2forArg2, arg1 = value1forArg1) # position does not matter

Keyword-only arguments

Some or all of the arguments of a function can be defined and forced to be keyword arguments; so, the function call is restricted to only use those arguments in the form of keyword arguments. To this ends, an * is typed in the function definition before the arguments to be as keyword arguments. In the following example, arg1 and arg2 are positional and argA, argB and argC are keyword-only arguments.

def myFunction (arg1, arg2, *, argA, argB, argC):
  # do something
# a function call
myFunction (valueForArg1, valueForArg2, argB = someVal1, argC = someVal2, argA = someVal3)

Note that keyword-only arguments only accept the keyword form of input. A function with all its arguments as keyword-only arguments is as follows.

def myFunction (*,argA, argB, argC, argD, argE):
  # do something

Positional-only arguments

To have all of the arguments of a function defined as positional, we use / in the function definition. For example

def myFunction (arg1, arg2, arg3, /):
  # do something

None of the arguments can be passed in the form of a keyword argument.

2- Type hinting

def myFunction(text: str, flag: bool, name: str = "Aristotle") -> str:


1- The results of different operators on objects. Objects (e.g. lists, dictionaries, other class objects, etc.) with the same value are usually stored at separate memory addresses.

L1 = [1,2,3]
L2 = [1,2,3]
print(L1 is L2)
print(L1 is not L2)
L1 = [1,2,3]
L2 = [6,7,8]
print(L1 != L2)
print(L1 is not L2)
# output

Useful Functions

zip() pair up iteratable and gives and iterator (

Prefix Operators * and **

1- Asterisks for unpacking into function call
The * operator can be used to unpack an iterable into the arguments in the function call.
The ** operator takes a dictionary of key-value pairs and unpack it into keyword arguments (or positional arguments but by their names) in a function call.


letters = ['a', 'b', 'c']
numbers = [2, 1, 3, 4, 7]
print(*numbers, *fruits)

# merging dictionaries
first_dict = {"A": 1, "B": 2}
second_dict = {"C": 3, "D": 4}
merged_dict = {**first_dict, **second_dict}

# unpacking into positional arguments by their names
def test_args_kwargs(name, surname, city):
    print(name + ' ' + surname + ' from ' + city)
info = {'name': 'J', 'surname': 'K', 'city': 'L'}

2- Asterisks for packing arguments given to function
Arbitrary positional arguments: when defining a function, we can use the * operator to capture an unlimited number of positional arguments given to the function. These arguments are captured into a tuple; so the function accepts a sequence of any number of (positional) arguments.

def f(*names):
  for name in names: print name
f('a', 'bb', 'ddh')

Arbitrary keyword arguments: the operator ** is used when defining a function to capture any number of keyword arguments. The keyword arguments will be captured into a dictionary.

def displayWord(word, **attributes):
    if 'color' in attributes:
        print(word + ' is ' + attributes['color'])
    if 'font' in attributes:
        print(word + ' is written in ' + attributes['font'])
displayWord('Jam', color='red', font='Times')

Note: Order of using formal args, *args and **kwargs is def funcName(args, *args, **kwargs)


Save an array to a text file.
Method 1: use np.savetxt()

Scala Fundamentals (1)

1- Block expression

The intermediate results (vals/vars) in a block expression are local. Example:

val firstName = "Polar"
def name: String = {
  println("This is a block")
  val title = "Mr."
  val lastName = "Bear"
  title + " " + firstName + " " + lastName

firstName: String = Polar
name: String
This is a block
Mr. Polar Bear
Error:(9, 17) not found: value title

Block expression are useful for evaluating intermediate expressions and assigning the result to a val/var.

2- Objects and classes

Everything is a kind of object in Scala; even primitive types, Int, Boolean, are objects. Also the program code to be run is an object and is defined in the form/pattern of an object. All objects can have methods and fields (attributes). Methods of an objects give functionality to the object to do task or computation on data. Fields are for storing data. Every object belongs to a class that have only one instance.

A class is an abstract template for creating objects that have similar methods and fields. A class also defines/create a Type (of objects) and therefore objects of the same class have the same Type. A class is not a value (like functions and objects) and it lives in its own namespace. Scala has two namespace: namespace of values, and namespace of types. Classes live in the type namespace.

The functionality of an object (if it has one) can be called like a function. A function is a first class value whereas a method belongs to an object. For example:

object addNumbers{
  def addEm (a: Int, b: Int) = println(a + b)

// compare with

object addNumbers{
  def apply (a: Int, b: Int) = println(a + b)

Function-like application of any object is available by using the apply method.

An executable Scala program is an object with an entry point, i.e. the main method. An object can be made executable (or an executable Scala code) by either adding a main method, or making the object extends the type App:

object MyProgram{  // this is a singleton objec
  import ... //import libraries
   def main(args : Array[String]) : Unit = {
     // Array[String] are used for the command line arguments
     // body of the main program to do stuff

// or

object MyProgram extends App {
   // body of the main program to do stuff

1- Singleton object

Is an objects which is unique and we cannot create other values of this object. Singleton object belongs to a class that have only one instance. As it can be seen the type of a singleton object defined as bellow is itself. The term Box$@3ae78a4c indicated the type @ a unique (reference) identifier of the object. All types of objects have unique identifiers.

object Box
res0: Box.type = Box$@3ae78a4c

2- Object (normal)

An object should belong to a predefined class. Example:

class Animal(val kind: String, val location: String) {
  def loc = println(kind + " lives in " + location)

val polarbear_1 = new Animal("Bear", "north pole")

// results
defined class Animal
polarbear_1: Animal = Animal@669cb6c7

Bear lives in north pole
res2: String = Bear

The class Animal creates a new type Animal that can be used like any other types. This is a type of an object.

3- Companion Objects

A companion object of a class is a singleton object with the same name as a class (and defined in the same file as the class’s file). The class is also called the companion class of the object. These two live in different namespaces, i.e. value and type namespaces. It is then important to note that the companion object is not an instant of its companion class. One application is to have the (singleton) companion object manage and implement the constructors of an instant (object) of the companion class. In this case the companion object is an auxiliary tool. For example:

class Line(val point_1:Double, val point_2:Double){
  def printPoints = println(point_1,point_2)
val line_1 = new Line(0.0, 1.0) // Line is the name of the class

// Compare with

class Line(val point_1:Double, val point_2:Double){
  def printPoints = println(point_1,point_2)

object Line{
  def apply(point_1:Double, point_2:Double): Line = new Line(point_1,point_2)

val line_1 = Line(0.0,1.0)  // Line is the name of the object 

4- Case classes

Case classes are useful shorthand for defining a class, its companion objects and several useful features at once. They are ideal for lightweight data-holding classes. Defining a case class is as of a class, however, with adding the literal case, and the val keyword for the constructors is optional. It also does not need a new keyword as there is a companion object with an apply method for the purpose of creating the case class. Example:

case class Animal (kind:String, location:String){
  def loc = println(kind + " lives in " + location)

// instantiate a case class using its class
val polarbear_1 = new Animal("polar bear", "north pole")

// instantiate a case class using its companion object automatically built
val polarbear_2 = Animal("polar bear", "north pole")

Features of case classes

1- Sensible equal and hashCode methods acting on the field values of the objects rather than the reference identity. Example:

polarbear_1 == polarbear_2
// result
res2: Boolean = true // but two different values

polarbear_1.eq(polarbear_2)  //eq method compares the reference ids
res2: Boolean = false

2- Copy method

val polarbear_3 = polarbear_1.copy()  // it is a shallow copy

3- Pattern matching using case classes. Pattern match is an extended if.

To match against the constructor values. Example:

case class Animal (kind:String, location:String){

object inspectAnimal{
  def inspect(anAnimal: Animal): String =
    anAnimal match {  // it reads: check if anAnimal matches a case and then output as insructed
      case Animal("bear", "north pole") => "this is a polar bear"
      case Animal("bear", "Banff") => "this is a Grizly or black bear"
      case Animal(_, "Banff") => "this might be a bear" 
      case Animal(its_kind,its_location) => s"this is a/an $its_kind living in $its_location"
		//binds its_kind, its_location to the constructor values    

val animal_1 = Animal("bear", "north pole")
val animal_2 = Animal("Marmot", "Banff")
val animal_3 = Animal("cat", "my house")

inspectAnimal.inspect(animal_1) // result: this is a polar bear
inspectAnimal.inspect(animal_2) // result: this might be a bear
inspectAnimal.inspect(animal_3) // result: this is a/an cat living in my house

To mach against the type. Example:

5- Abstract class

3- Tips with objects and classes

1- Overloading the methods (overloaded methods)

Instead of creating several methods with different names and doing the same task but receiving different number of arguments and/or different types of parameters, we can create methods with the same name but different parameter and let Scala decide on the proper version of the method. Overloading methods is applicable to classes and singleton objects. Example:

object doSomething{
  def sumNumbers(a:Int, b:Int): Unit = println(a + b)
  def sumNumbers(a:Double, b:Double): Unit = println(a + b)
  def sumNumbers(a:Int, b:Int, c:Int): Unit = println(a + b + c)

doSomething.sumNumbers(2,3) // 5
doSomething.sumNumbers(12.345,34.567) // 46.912
doSomething.sumNumbers(1,2,7) // 10

2- Auxiliary constructor

Other than the primary constructors of a class, auxiliary constructors can also be utilized. Auxiliary constructors overload the constructors. To do this, the following should be considered:

  • Auxiliary constructors are defined by def keyword.
  • Like Method Overloading, all auxiliary constructors should use the same name this.
  • each auxiliary constructor must have a different different parameters list.
  • each auxiliary constructor must first call a previously defined primary constructor(s) or/and an auxiliary constructor(s). This is performed by name this.

Nevertheless, constructor overloading can be implemented by setting default values for the primary constructors.

4- Traits (Subtyping)

Classes are abstraction over objects while traits are abstraction over classes; in other words, traits are template for classes. Traits allow us to define a super type for several classes which are the subtypes. This super type is is outside of the Any super type that all classes share. Classes under a trait can share the implementation of the same operation. A trait creates interface that any subtype, i.e. classes, must implement (comply with).

Compared with a class, a trait does not/cannot logically have a constructor. Classes (having constructors) can be created from a trait and objects from the classes. A trait can define abstract fields and methods that have names and type signature but no (concrete) implementation. An implementation is possible when defining an extending class under the trait. Implementing methods based on abstract methods in a trait is possible. Whatever abstract methods and fields are defined in a trait have to be then implemented in the extending class(es). Concrete (default) implementation of any method (or a field) in a trait is possible as well, however, it must be make sure that the method is valid in all the extending classes (subtypes). Otherwise, the method (or field) should be overridden in a subtype.

Abstract fields in a trait can be defined either using val or def. However, it is recommended to use def because def is a generalization of val in Scala. If an abstract field is defined by def, then val will be used for its implementation in the extending class.

Abstract methods (or fields) can have completely different implementations in different subtypes.

Example: Animals can be wild or pet. Instead of creating two separate non-connected (in terms of sharing fields and methods) classes, we create two extending classes under a trait.

trait Animal {
  def kind: String  // can also be val kind: String
  def location: String
  def inspect: Unit = println(s"$kind lives in $location")
  def id: Unit

case class Wild (kind: String, location: String, idNumber: Int) extends Animal {
  def id: Unit = print(s"The wild animal id is: $idNumber")

case class Pet (kind: String, location: String, idNumber: Int) extends Animal{
  def id: Unit = print(s"The pet id is: $idNumber")

val bear_1 = Wild("bear", "north pole", 1212123)
val cat_1 = Pet ("Cat", "my house", 123456)
defined trait Animal

defined class Wild

defined class Pet

bear_1: Wild = Wild(bear,north pole,1212123)

cat_1: Pet = Pet(Cat,my house,123456)

bear lives in north pole
The wild animal id is: 1212123
Cat lives in my house
The pet id is: 123456

Scala with IntelliJ IDEA

1- Defining a package object

  1. On the project side bar right click on Scala folder –> new –> package.
  2. name the package custom.methods.
  3. Right click on custom.methods –>new –> package object

The package object is now ready.

package custom
package object methods {
// define your custom classes, case classes, objects, methods here