Wednesday, September 26, 2012
Python best free resources
Here I found some python cool tutorials and books for free. It is keeping me awake late night.
http://pythonbooks.revolunet.com/
You may share yours
Posted by
Abu Zahed Jony
at
4:54 AM
0
comments
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Labels:
python resources
Monday, September 17, 2012
Design pattern in python: Abstract factory pattern
class Shop(object):
def __init__(self, item_factory = None):
self.item_factory=item_factory
def get_item_description(self):
item=self.item_factory.get_item()
print "Item model ",self.item_factory.model()
print "Item name ",item.name()
print "Item color ",self.item_factory.color()
class Shoes(object):
def name(self):
return "Shoes"
def __str__(self):
return "Shoes"
class Mobile(object):
def name(self):
return "Nokia"
def __str__(self):
return "Nokia"
class ShoesFactory(object):
def get_item(self):
return Shoes()
def model(self):
return "MEN101"
def color(self):
return "Black"
class MobileFactory(object):
def get_item(self):
return Mobile()
def model(self):
return "NOK404"
def color(self):
return "Black deep"
shopShoes=Shop(ShoesFactory())
shopShoes.get_item_description()
print "*"*30
shopMobile=Shop(MobileFactory())
shopMobile.get_item_description()
Output:
Item model MEN101 Item name Shoes Item color Black ****************************** Item model NOK404 Item name Nokia Item color Black deep
Posted by
Abu Zahed Jony
at
7:57 AM
1 comments
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Labels:
design pattern
Design pattern in python: Singleton pattern
class MyClass: def __init__(self): self.numA = 5 self.numB = 10 def getNumA(self): return self.numA def getNumB(self): return self.numB def setNumA(self, v): self.numA = v def setNumA(self, v): self.numB = v def toString(self): return "A:",self.numA,"B:",self.numB _singleton = None def get_instance(cls = MyClass): global _singleton if _singleton is None: _singleton = cls() return _singleton if __name__=='__main__': print "Main output" a = get_instance() print a.toString() a.setNumA(15) print a.toString() b = get_instance() print b.toString()
Output:
Main output
('A:', 5, 'B:', 10)
('A:', 5, 'B:', 15)
('A:', 5, 'B:', 15)
Posted by
Abu Zahed Jony
at
6:02 AM
0
comments
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Labels:
design pattern
Friday, September 7, 2012
Python Markdown module
Markdownld install pip command (pip install markdown)
import markdown md = markdown.Markdown(safe_mode=True) html = md.convert(content) #'html' is converted html data
Here very nice documentation of markdown. You may read it.
Posted by
Abu Zahed Jony
at
10:20 PM
0
comments
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Labels:
Markdown
Python jsonpickle: object serialization and deserialization
import jsonpickle
class Subclass(object):
subclass_value=10
def my_fun(self):
print "this is myfun from Subclass"
class MainClass(object):
mainclass_data='This is main class data'
mainclass_value=5
sc=Subclass()
obj=MainClass()
pickle=jsonpickle.encode(obj,max_depth=1) #Here I set max_depth, so it will not recurse
unpickleObject=jsonpickle.decode(pickle) #deeper than 'max_depth' steps into the object
unpickleObject.sc.my_fun()
print unpickleObject.mainclass_data
Output:
this is myfun from Subclass This is main class data
Posted by
Abu Zahed Jony
at
1:11 PM
0
comments
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Labels:
jsonpickle
Install pip on windows
Pip is very usefull for installing python packages. Lets see the installing procedure of pip on windows
Now you have pip. Go to the command prompt and run pip help and you will find pip command details.
For example I want to install jsonpickle using pip now comand is pip install jsonpickle
- Make sure that your python path is added to environment variable
- download easy installer for windows & install it by clicking on the exe file or download run ez_setup.py from here
- dowaload pip pip & uncompress it.
- cd to the uncompress directoy and run command python setup.py install
Posted by
Abu Zahed Jony
at
12:20 PM
0
comments
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Labels:
pip
Tuesday, September 4, 2012
python subclass list(reflection)
class MyClass(object): pass class SubClassA(MyClass): pass class SubClassB(MyClass): pass class SubClassC(MyClass): pass print([cls.__name__ for cls in vars()['MyClass'].__subclasses__()])
Output:
['SubClassA', 'SubClassB', 'SubClassC']
Posted by
Abu Zahed Jony
at
1:44 AM
0
comments
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Labels:
reflection
Subscribe to:
Posts (Atom)
