Last active
December 17, 2015 12:49
-
-
Save xfire/5612339 to your computer and use it in GitHub Desktop.
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
printf '\33]50;%s%d\007' "xft:Terminus:pixelsize=" 20 | |
- python properties ----------------------------------------------------------------------- | |
>>> props.py >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | |
| def python_properties(): | |
| with open("/home/fire/python.txt", "r") as f: | |
| print(f.read()) | |
| | |
| props = [ "has a dynamic and strong type system" | |
| , "is multi-paradigm" | |
| , "can do object oriented, imperative, functional, procedural and reflective paradigms" | |
| , "has a readable, indention based syntax" | |
| , "has a VM, which can be embedded into other languages" | |
| , "is often used as scripting language" | |
| , "has batteries included" | |
| ] | |
| | |
| for prop in props: | |
| print(prop) | |
| | |
| python_properties() | |
# embedding / scripting | |
# jython (jvm) | |
# ironpython (.net clr) | |
# Pyjamas (translates python into Javascript) | |
# boost python (embed python into c++ applications) | |
# batteries included | |
# data types | |
# text processing | |
# I/O | |
# internet protocols and support | |
# --- third party | |
# numpy + scipy | |
# google app engine | |
# django, flask, web2py, ... | |
# databases | |
- data types ------------------------------------------------------------------------------ | |
>>> ipython >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | |
# lets define a list of builtin data types | |
x = ['foo', "foo", """foo | |
bar""", 23, 23.42, True, [1,2,3,4], ("blub", 42), {"key": "value"}, {1,2,2,3}] | |
x | |
# ok, can we somehow get the type of a value? | |
? type | |
type(23) | |
# so, lets print our types | |
for v in x: | |
print(v, type(v)) | |
# this can be done more functional by creating a new list based on the existing one | |
? map | |
map(str, [1,2,3]) | |
list(map(str, [1,2,3])) | |
list(map(lambda i: i+1, [1,2,3])) | |
list(map(lambda v: (v, type(v)), x)) | |
# list comprehensions, another nice way to express operations on iterables | |
[i*i for i in [1,2,3]] | |
[(v, type(v)) for v in x] | |
# list slicing | |
l = list(range(1, 11)) | |
l | |
# get the first 5 elements | |
l[0:5] | |
# get every second element | |
l[::2] | |
# reverse a list | |
l[::-1] | |
# get the last element | |
l[-1] | |
# dicts | |
d = {"a": 23, "b": 42} | |
d["a"] | |
d["b"] | |
d["c"] | |
# sets | |
s = {1,2,3} | |
s.add(1) | |
s | |
s.add(23) | |
s | |
- control flow ---------------------------------------------------------------------------- | |
>>> ipython >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | |
if 23 == 23: | |
print("yeah, python rocks!") | |
else: | |
print("noooo!") | |
for i in [1,2,3]: | |
print(i) | |
# while loops | |
i = 3 | |
while i > 0: | |
print(i) | |
i -= 1 | |
# exceptions | |
try: | |
raise NotImplementedError("OhOh, something is wrong here") | |
except NotImplementedError as e: | |
print(e, type(e)) | |
finally: | |
print("finally over") | |
- classes --------------------------------------------------------------------------------- | |
>>> classes.py + ipython >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | |
# how object oriented programming is done in python | |
| class Person: | |
| """ | |
| This is my first fancy super duper hardcore complex python class. | |
| """ | |
| | |
| def __init__(self, name, age): | |
| """Constructor""" | |
| self.name = name # public member | |
| self._age = age # protected member sortof | |
| self.__private = False # private member | |
| | |
| def getName(self): | |
| """Return the name of the person""" | |
| return self.name | |
| | |
| def getAge(self): | |
| """Return the age of the person""" | |
| return self._age | |
| | |
| def __str__(self): | |
| """Returns a human readable representation""" | |
| return "Person [name = '{0}', age = '{1}', private = '{2}']".format(self.name, self._age, self.__private) | |
%run person.py | |
# create a new instance of a person | |
p = Person("rico", 33) | |
p.getName() | |
p.name | |
p.getAge() | |
p._age | |
p.__private | |
str(p) | |
# deriving | |
class Student(Person): | |
pass | |
s = Student("foo", 23) | |
s.getName() | |
# docstrings | |
help(Person) | |
print(Person.__doc__) | |
print(Person.getName.__doc__) | |
- meta programming ------------------------------------------------------------------------ | |
>>> ipython >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | |
# classes are simple dictionaries with some fancy syntactic sugar | |
Person.__dict__.keys() | |
Person.__dict__["getName"] | |
Person.__dict__["getName"]() | |
# one argument? ahhh, self | |
Person.__dict__["getName"](p) | |
# class instances too! | |
p.__dict__ | |
# meta programming magic | |
# we can change everything everytime we want! | |
Person.field = 23 | |
p.field | |
Person.method = lambda self: self.name | |
p.method() | |
- meta classes ---------------------------------------------------------------------------- | |
>>> meta.py + ipython >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | |
# meta classes are factories for classes so we can generate/modify classes at runtime | |
# for every getter we want to automagically generate a getType() method | |
| class Meta(type): | |
| def __init__(cls, name, bases, nsp): | |
| super(Meta, cls).__init__(name, bases, nsp) | |
| | |
| for getterName in filter(lambda v: v.startswith("get"), nsp.keys()): | |
| def getType(): | |
| getter = getattr(cls, getterName) | |
| def nested(self): | |
| return type(getter(self)) | |
| return nested | |
| setattr(cls, getterName + "Type", getType()) | |
class Simple(metaclass = Meta): | |
def getFoo(self): return 23 | |
def getBar(self): return "test" | |
s = Simple() | |
s.getFoo() | |
s.getFooType() | |
s.getBar() | |
s.getBarType() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment