Skip to content

Instantly share code, notes, and snippets.

Gergo Bogdan gergob

View GitHub Profile
View projects_repository.py
from pymongo import MongoClient
from bson.objectid import ObjectId
from project import Project
class ProjectsRepository(object):
""" Repository implementing CRUD operations on projects collection in MongoDB """
def __init__(self):
# initializing the MongoClient, this helps to
# access the MongoDB databases and collections
View project.py
from bson.objectid import ObjectId
class Project(object):
"""A class for storing Project related information"""
def __init__(self, project_id=None, title=None, description=None, price=0.0, assigned_to=None):
if project_id is None:
self._id = ObjectId()
else:
self._id = project_id
View dict_samples.py
#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)
View list_samples.py
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
@gergob
gergob / mro_type_error_console.py
Created Dec 9, 2014
MRO TypeError raised in command line
View mro_type_error_console.py
>>> 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
>>>
View mro_type_error.py
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):
@gergob
gergob / smartphone_mro.py
Created Dec 9, 2014
MRO of SmartPhone class
View smartphone_mro.py
>>> SmartPhone.mro()
[<class 'SmartPhone'>, <class 'Phone'>, <class 'Computer'>, <class 'object'>]
>>> SmartPhone.__mro__
(<class 'SmartPhone'>, <class 'Phone'>, <class 'Computer'>, <class 'object'>)
@gergob
gergob / smartphone_initialization.py
Created Dec 8, 2014
SmartPhone invokes two constructors
View smartphone_initialization.py
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.
>>>
@gergob
gergob / oop_multi.py
Created Dec 8, 2014
multiple_inheritance_manual_constructor
View oop_multi.py
class Phone(object):
def __init__(self):
print("Phone constructor invoked.")
def call_number(self, phone_number):
print("Calling number {}".format(phone_number))
@gergob
gergob / multiple_inheritance_in_action.py
Created Nov 26, 2014
Multiple Inheritance SmartPhone class in action
View multiple_inheritance_in_action.py
#
# will be discussed later why only the constructor of Phone class was invoked
#
>>> my_iphone = SmartPhone()
Phone constructor invoked.
>>> my_iphone.call_number("123456789")
Calling number 123456789
>>> my_iphone.install_software("python")
You can’t perform that action at this time.