====== Python ===== You can make a python file run by adding this to the script: #!/usr/bin/python This tells the command line that it is a python script. There is a distinction between function, and instances. Like for instance, a file called: test_file = open("test.txt", "wb") This file has instances you can acces: test_file.mode Then there are also functions you can access, and to access them you use parenthesis. ===== Python Version ===== Ok so theres a very big distinction between python 2 and 3. I already ran into issues looking at tutorials and just using the print statement, which apparently got changed from a statement to a function in version 3. In my version of Ubuntu 16.04, the default python is 2, and to use 3 i have to do python3 ===== Python Types ===== Numbers, strings, lists, tuples, dictionary. ==== Strings ==== Multiple ways of printing strings. * single quotes * double quotes * triple quotes ==== Numbers ==== Integers by default. In python you do not predeclare variables. It is dynamically typed. You don't have to worry about types. In python you don't worry about references or pointers. But you do worry about mutable vs immutable data. Integers are an immutable type. Strings are also immutable. Lists are mutable. To make a list you do: x = [0 , 1] Python puts values in memory and then it creates labels to it. Thats a concept of variables. Ok cool, so a big ditinction is that data is stored in memory, vs allocated for by datatypes. ==== Booleans ==== 0 evaluates to ''%%False%%''. 1 or any other number evaluates to ''%%True%%''. ==== Lists ==== Lists are one of the fundamental datatypes. You can create lists by doing the following: this_is_a_list = ['Cup', 'Watch', 'Scissors', 'Iphone'] You can index the list: print(this_is_a_list[0]) # This prints everything including 0 up to but not including 3. print(this_is_a_list[0:3]) You can have lists inside of lists. There is a crazy amount of stuff with can do with lists, and python is really flexible and easy to use. Sort, add, combine lists, what have you. Super quick and easy. ==== Tuple ==== Unlike a list, you can't change a tuple after you create it. Declared with parenthesis. this_is_a_tuple(3,1,4,1,5,9) ==== Dictionaries ==== Values that have a unique key for each value that you are storing. Very similar to lists, but you can't join like you can with a plus sign. ===== Operators ===== 7 different operators. + - * / % ** // ==== Logical Operators ==== if else elif == != > >= < <== You can combine conditionals with ''%%or%%'' and ''%%not%%''. ===== Loops ===== ==== for loops ==== for x in range(0,10) for x in this_is_list ==== while loops ==== while(x != 0): do stuff! ===== Functions ===== Functions allow us to reuse and make more readable code. In python you don't have to specify return type. Woah! def addNumber(fNum, lNum): sumNum = fNum + lNum return seNum ===== Strings ===== Python has really strong string support. You can index characters within strings using square brackets string_thing(0:2) Theres a bunch of stuff you can with the % operator, the string formatting operator each string kinda becomes an object that has lots of functions associated with it * ''%%isalpha()%%'' * ''%%isalnum()%%'' * ''%%strip()%%'' * ''%%split()%%'' ==== Formatting ==== If you want to print a neat line of data use the [[https://docs.python.org/2/library/string.html#format-string-syntax|Format String Syntax]]. For example: print("[ FAIL ] {: <20} Channel: {: <20} Statistics: {:>6.4f} hz".format(description, chan, stats)) This sets a fix width for each field and aligns the test either left ''%%<%%'' or right ''%%>%%'' ===== Mutability ===== In python pretty much everything is an object, and these objects are either mutable or immutable. Mutable means the object and its contants can change and immutable means that it cannot. Lots of basic data types are immutable, such as int, float, and string. The crazy thing is that python creates an object to represent that value that a variable such as an int is set to and then uses the variable as a tag that points to that object. Immutable ojects: * bool * int * float * tuple * str Mutable objects: * list * set * dict ===== File IO ===== Yay, we access files! It is super easy. Just use the ''%%open%%'', ''%%close%%'', ''%%read%%'', ''%%write%%'' file functions! Create a variable that is a file, then open it, read it, write it, delete it using an os function, whatever you want man! ===== OOP ===== Objects have attributes and abilities. We define these objects inside classes that contain attributes and abilities. Define a class using class ''%%Name:%%'' Private attributes are preceeded by two underscores. There is the ''%%self%%'' instance that refers to the object instance. Just like the ''%%this%%''. There are also constructors which you call with ''%%__init__%%'' There is method overloading, which you do by setting a parameter to ''%%None%%''. This makes it optional. Then you can check inside the method if that parameter is None. There is polymorphism, which allows you to make functions that take in a parent class, and then you can pass in child objects of that parent class. See the ''%%classes.py%%'' in the learn directory for examples!! ===== Web App ===== Did this [[https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-iii-web-forms|python - flask tutorial]] I used SQLAlchemy python plugin to interface with an SQLite db and also used a migration app to automatically handle db changes. ===== Debugging ===== {{::screenshot_2019-01-10_10-25-29.png?400|}} To debug, you can use PuDB, which is a nice graphical terminal program. ===== Virtual Environments ===== A virtual environment creates a python environment stored in a folder of your choosing. This allows you to install all kinds of crap not to your system, but to that virtual environment in that folder. Virutal environments where included in python3 and to make one you run: python3 -m venv /path/to/virtualenv/folder ===== Plane Notes ===== Did a bunch of review, and now gonna skim through the book! Fundamental data types: ints, floats, strings, Learning how to build strings with single quotes, double quotes and tripple single/double quotes. Learning how to build lists and index it and slice them with ranges [0:4]. Only works in legal ranges. A slice will always return a list. an access will return an element. Learning about ranges. Learned about pop, and how you can index a pop, by doing list_whatever.pop(index) and how theres no push in python because there is an append, and python wants to be all pure and shit and not have duplicate functions. You can assign lists uisng text parallel assignment firstname, lastname = ["Bilbo", "Baggins"] God python is so wierd with shit, like, there's all these nice little features everywhere. Haha. Dictionaries have keys and they are defined by curly braces {}. They are unordered. You can make a dictionary using a sequence of keys. Learning about the format functions on strings. God damn dude you can do anything to strings. You use it by calling it on a string and using {0}, {1} ... positionals to insert formatted string values. For instance to insert a decimal number from a calc: strNum = "Hi this a the square root of 2: {0:.{1}f}".format(math.sqrt(2),5) Omg reg exes. Ok so you can create a pattern by using pattern = re.compile('whater|whatever|'), then you use a function in the pattern to split a string. COOOL. pattern = re.compile(",|:|;") pattern.split("Hi,I'm:a;string") # this will print out ["Hi",'I'm','a','string'] You can do so much funky shit with strings. Every string object has a list of useful functions. OK tuples time and I'm fuggin DONE. Tuples are like lists, but they're assigned with parenthesis and they're immutable so you can't append or pop. Apparently they're faster. They can only be changed by replacement. So you could do something like: t = (1,2,3) l = list(t) l.append("apples") t = tuple(l) You can index tuples by using square braces. Tuples of 1 are not created as tuples, but as objects of their respective datatype. Empty types are still tuples. ().__class__ == tuple Tuples can be embedded! ===== Methods ===== To make a function you simply write: def my_function(a, b) return a + b PS: this is how you document a method. ## # @brief This function does this # # @return It returns nothing # You can create a method that is in the class scope by adding a self parameter. Methods that don't return anything return ''%%None%%''. You can have method with default arguments: def method_a(a,b,c=3): return a + b + c Functions without self arg are global, even if they are defined in a class. Empty functions need a pass function but lines after the pass filler still get called. A string placed at the top of a function definition is a documentation string that can be accessed with the ''%%__doc__%%'' attribute. ===== Classes ===== It's very easy to make classes in python. class Rabbit: def name(self): return "Muccho" def _status(self): return "dead" def __password(self): return 'password' ===== Control Statements ===== The ''%%continue%%'' statement is fun. It sends the execution to the start of the while loop without running any code following the continue.