Sometimes I find myself converting google doc spreadsheets (CSV) to JSON format. I would later on use the JSON string to insert it into a JavaScript file. So here is a simple script to convert a CSV file to JSON format.
A significant change from Python 2 to Python 3 is the way strings are dealt with. Python 3 doesnt always return a string when expected. For example, the return type of read() in version 2 has always been a string. But in version 3, its very often a "bytes" string. When you print a "bytes" string, you'll see every character in its byte format, special characters as escape secquences (newline as \n) and other unicode characters as escape sequences. This is because Python 3 differentiates between text (string) and data ("bytes" string) as oppossed to Unicode vs 8-bit string. (Text Vs. Data Instead Of Unicode Vs. 8-bit)
My localhost/index.html contains just this : <html><body><h1>It works!. stärke gläser</h1></body></html>
In Python 3, we need to need to explicitly convert it to string format via the str() function and specify the encoding-type. If you are getting errors using Python 3.0, you may want to update to atleast Python 3.0.1 - many have reported possible Unciode encoding/decoding bugs in 3.0.
Lets say I have an abstract class called Vehicle and 3 classes that descend from it are Bike, Car & Truck. I use a static variable in Vehicle called total to keep track of the total number of Vehicles. But I really don't want to keep track of the total number general Vehicles. What I really want is to keep track of the total number of Bikes, Cars & Trucks individually. This is easy - just declare total in Bike, Car & Truck classes.
Now arises a situation where we need a function println in Vehicle that accesses total. We'll also include a function called set in Vehicle to explicitly set the value of total (instead of creating 10 instances to prove a point).
There are two scenarios to this, both of which are not possible :
Declare a static variable total in Vehicle but different values persist in Bike::total, Car::total & Truck::total. This is impossible because total is Vehicle's static variable which is common to all. The following will output 2 2 2.
Declare the static variable total in each of Vehicle's subclasses, Bike, Car & Truck. But in this case, Vehicle's println needs to access the descendant class static variable like child::total which is not possible in most(all) languages. It is possible to overcome this problem by setting and getting the value in the subclasses and use $this->childMethod() in the parent class, Vehicle.
But the two methods, getChildValue and setChildValue must be defined properly in all subclasses. __CLASS__ returns the class in which its called from and get_class($this) returns the class of the current instance. Example : echo __CLASS__; in a method in Vehicle will always output Vehicle, but get_class($this) will output the classname of the object (In this case, Bike, Car or Truck). If we could do get_class($this)::$total (Bike::$total), then it could've been easily solved.
The last method is possible in Python in two ways - self.__class__.total & classmethod. Python has a way to access class member of the caller's class, not just the class members in which its being accessed. Here each of the subclasses have a static member called total which gets created and assigned in its parent, Vehicle
self.__class__.total points to the static member total of the object's (this) class and not of the class Vehicle. cls.total references the same thing as the first argument is actually the classname which is not passed in the parentheses, but by using the classname preceding the dot.