This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask | |
# initialize a new Flask app | |
app = Flask(__name__) | |
# add one routing | |
@app.route("/") | |
def index(): | |
return "Homepage routing works!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# string declaration | |
my_str1 = 'simple text' | |
my_str2 = "simple text 2" | |
my_multiline_str = """Hey, this | |
is a multiline | |
string in python""" | |
my_unicode_str = "This is a unicode string containing some special characters like:éáő" | |
print(my_str1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def parse_csv(text): | |
"""Parses a CSV format text, returns a dictionary, keys are the Column headers from the CSV and values are the data""" | |
result = {} | |
# first split by EOL | |
lines = text.split("\n") | |
if len(lines) > 0: | |
headers = [item.strip() for item in lines[0].split(",")] | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
greg@earth ~/Development/_freelancer/crud_operations_in_mongodb_using_python $ python main.py | |
Loading all items from database: | |
ID = 54953f0a8524880d021cd856 | Title = Wordpress website for Freelancers | Price = 250 | |
ID = 54953f408524880d0db9a8a1 | Title = Wordpress website for Freelancers | Price = 250 | |
ID = 54953f788524880d14d4e590 | Title = Wordpress website for Freelancers | Price = 250 | |
Saving new_project to database | |
new_project saved to database |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#creating a new dictionary with 3 keys : name, age and balance | |
d = { "name": "John Doe", "age": 44, "balance": 235.5 } | |
#printing out the values of the dictionary using the kyes | |
print(d["name"]) # will display "John Doe" | |
print(d["age"]) # will display 44 | |
print(d["balance"]) # will display 235.5 | |
#using dict to create dictionaries | |
my_dict_1 = dict(name="John Doe", age=44, balance=235.5) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
my_list_1 = [1, 3, 5, 7, 11, 13, 17] | |
my_list_2 = range(10, 21) # will result [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] | |
my_list_3 = range(10) | |
for nr in my_list_3: | |
print(nr) | |
# and maybe do some other stuff | |
#indexing - used for getting and setting values |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> from mro_type_error import Phablet | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
File "mro_type_error.py", line 30, in <module> | |
class Phablet(SmartPhone, Tablet): | |
TypeError: Cannot create a consistent method resolution | |
order (MRO) for bases Phone, Computer | |
>>> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Phone(object): | |
def __init__(self): | |
print("Phone constructor invoked.") | |
def call_number(self, phone_number): | |
print("Calling number {}".format(phone_number)) | |
class Computer(object): | |
def __init__(self): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> SmartPhone.mro() | |
[<class 'SmartPhone'>, <class 'Phone'>, <class 'Computer'>, <class 'object'>] | |
>>> SmartPhone.__mro__ | |
(<class 'SmartPhone'>, <class 'Phone'>, <class 'Computer'>, <class 'object'>) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Python 3.2.3 (default, Feb 27 2014, 21:31:18) | |
[GCC 4.6.3] on linux2 | |
Type "help", "copyright", "credits" or "license" for more information. | |
>>> from oop_multi import SmartPhone | |
>>> s = SmartPhone() | |
Phone constructor invoked. | |
Computer constructor invoked. | |
>>> |