I am interviewing for a Python developer position. I always find my knowledge gap or shortage. I will not stop learning.
Django
With Django, you can take Web applications from concept to launch in a matter of hours.
part 1
source activate py3
conda install django
python -m django —version # 1.11.4
django-admin startproject mysite # initailize a project by creating manage.py and mysite folder with starting codes
cd mysite
python manage.py runserver # create db.sqlite3
http://127.0.0.1:8000/
python manage.py startapp polls # create polls folder
cd polls
# change text in vews.py
touch urls.py # create file and change text
cd ..
touch urls.py # create file and change text
part 2
python manage.py migrate # install apps
cd polls
subl models.py # add models
cd ../mysite
subl setting.py # add 'polls.apps.PollsConfig' to apps
python manage.py makemigrations polls
python manage.py sqlmigrate polls 0001
python manage.py migrate
python manage.py shell
# play with API
python manage.py createsuperuser
Username: admin
password: j*89*****
cd polls
subl admin.py # make poll app modifiable in the admin
Difference between function and generator
star sign before a variable name?
def foo(x,*y, **z):
print(x)
print(y)
print(z)
foo(1,2,3, a=4,b=5)
lis = [2,3]
dic = {'a':4, 'b':5}
foo(1,*lis,**dic)
Inside a function header:
* collects all the positional arguments in a tuple** collects all the keyword arguments in a dictionary
In a function call:
* unpacks an list or tuple into position arguments** unpacks an dictionary into keyword argumentsextract lines begin with “error: “
result = [l for l in open(“input.txt”) if l.startswith(“error: “)]
try/execept handling
x = 1
y = 0
z = 0
result = 2
try:
z= z+1 # 1
result = x/y
except ZeroDivisionError:
z = z + result #3
else:
z= z+1 # if no error occurs
finally:
z = z+1 #4
z = z+1 #5
print(z)
try:
fuck
except NameError as e: # or Exception
print("watch your language")
finally:
print("go home")
sort
a = [-1,-4,2]
a.sort() # inplace
sorted(a, key = abs)
separate string into partitioned tuple
"hello".partition("e")Python generator vs python function
generator save state at
yield, so the result will keep going for next call unless it is exhausted.import math
def get_primes(number):
while True:
if is_prime(number):
yield number
number += 1
def is_prime(number):
if number > 1:
if number == 2:
return True
if number % 2 == 0:
return False
for current in range(3, int(math.sqrt(number) + 1), 2):
#print(current)
if number % current == 0:
return False
return True
return False
a = get_primes(2)
a.__next__()
split vs rsplit
rsplit is from the right
class method
class vehicles:
count = 0
def __init__(self,value):
self.value = value
vehicles.count +=1
def getval(self):
return self.value
def getcount(cls):
return vehicles.count
counter = classmethod(getcount)
type1 = vehicles("car")
type2 = vehicles("Bus")
type3 = vehicles("bike")
print(type1.getval(),type2.getval(),vehicles.counter(),type2.getcount())
# car Bus 3 3
function decorator
Decorator is a handy way to wrap a function so it temporaly becomes an embedded function.
class Foo():
@classmethod
def class_foo(cls):
print("class: %s" % cls)
Foo.class_foo()
# equals to
class Foo():
def class_foo(cls):
print("class: %s" % cls)
class_foo = classmethod(class_foo)
Foo.class_foo()
another example
def my_decorator(some_function):
def wrapper():
print("before some_function() is called.")
some_function()
print("after some_function() is called.")
return wrapper
@my_decorator
def just_some_function():
print("Wheee!")
just_some_function()
zip()
The syntax of zip() is:
zip(*iterables)
example
coordinate = ['x', 'y', 'z']
value = [3, 4, 5, 0, 9]
result = zip(coordinate, value)
resultList = list(result)
print(resultList)
c, v = zip(*resultList)
print('c =', c)
print('v =', v)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.