Tech C**P
15 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
Flask make_response():

Sometimes it is necessary to set additional headers in a view. Because
views do not have to return response objects but can return a value that
is converted into a response object by Flask itself, it becomes tricky to
add headers to it. This function can be called instead of using a return
and you will get a response object which you can use to attach headers.

If view looked like this and you want to add a new header::

def index():
return render_template('index.html', foo=42)

You can now do something like this::

def index():
response = make_response(render_template('index.html', foo=42))
response.headers['X-Parachutes'] = 'parachutes are cool'
return response

This function accepts the very same arguments you can return from a
view function. This for example creates a response with a 404 error
code::

response = make_response(render_template('not_found.html'), 404)

The other use case of this function is to force the return value of a
view function into a response which is helpful with view
decorators::

response = make_response(view_function())
response.headers['X-Parachutes'] = 'parachutes are cool'

Internally this function does the following things:

- if no arguments are passed, it creates a new response argument
- if one argument is passed, :meth:`flask.Flask.make_response`
is invoked with it.
- if more than one argument is passed, the arguments are passed
to the :meth:`flask.Flask.make_response` function as tuple.

#flask #make_response #helper #header #response
Disable the below message in gitlab if your are annoyed with it like me:
remote:
remote: To create a merge request for dev, visit:
remote: https://repo.alohi.ch/alohi/backend/affogato/merge_requests/new?merge_request%5Bsource_branch%5D=dev
remote:
Go to your project settings and uncheck:
Display link to create/view merge request on push

#gitlab #merge_request #push
Use set on a list to remove duplicates:
my_dup_list = [1,2,3,4,1,2,3,4]
print set(my_dup_list)

#set #python #duplicate #unique
Create an excel file using pandas in python:
users = [{
'user_id': 1
}, {
'user_id': 2
}]
df = pandas.DataFrame(users, columns=['user_id'])
filename = 'users_info_%s.xlsx' % random.randint(0, 100)
writer = pandas.ExcelWriter(filename, engine='xlsxwriter')
df.to_excel(writer, sheet_name='user information')

#pandas #python #excel #dataframe
If you want to implement Oauth2.0 read RFC below thoroughly:

https://tools.ietf.org/html/rfc6749

#oauth #rfc #rfc6749
tmux is a terminal multiplexer. It lets you switch easily between several programs in one terminal, detach them (they keep running in the background) and reattach them to a different terminal.
after installation you can list your sessions by:
tmux ls

Output would be something like this:
0: 1 windows (created Mon Aug  7 10:34:00 2017) [135x52] (attached)
2: 1 windows (created Mon Aug 7 13:56:10 2017) [126x25]

(attached) at the end of session 0 tells us that the session has been attached by a user.
You can attach a session by:
tmux attach -t 0
-t tells tmux which session should be attached.

Now let's say you want to detach a session you need to press ^b+d (CONTROL+b and then press d).

If you want to split your window to multiple panes press ^b+" (split horizontally) and/or ^b+% (split vertically).

^b+n : next window
^b+(up/down/left/right) : to move between panes
^b+? : help and shortcuts
^b+x : kill a pane

THE GREAT THING about tmux is that multiple users can attach the same session and work simultaneously!

I know you will get tired if I say more about tmux, so go and explore more yourself and see the real power of multiplexers.

#tmux #linux #sysadmin #terminal #multiplexer
You can add title to iOS push notification as of iOS 8.2:
A short string describing the purpose of the notification. Apple Watch displays this string as part of the notification interface. This string is displayed only briefly and should be crafted so that it can be understood quickly. This key was added in iOS 8.2.

#push #notification #ios
Emails need to have call to actions. It encourages users to pay more, to charge his account or to buy a product.

Display CTAs the right time to target users to increase ROI (return of investment)

#call_to_action #email #end_user #ROI
Tech C**P
You can add title to iOS push notification as of iOS 8.2: A short string describing the purpose of the notification. Apple Watch displays this string as part of the notification interface. This string is displayed only briefly and should be crafted so that…
{
"aps":{
"alert":{
"body":"The status of flight KL123 scheduled 20:30 to London Heathrow has changed to 'boarding'. Boarding at 20:05.",
"title":"Boarding at 20:05"
},
"category":"openFlightCategory"
},
}
As you all may know pip is a python package manager and everyone has worked with it to install python libs.

Some of the commands with pip that you may not know about it:

- freeze: output installed packages in requirements format. (you can grep and search for a specific package):


rply==0.7.4
rsa==3.2.3
sasl==0.2.1
scipy==0.13.0b1



- list: list installed packages:

tcpwatch (1.3.1)
telebot (0.0.3)
thrift (0.9.3)



- show: Show information about installed packages:

$ pip show pandas
Name: pandas
Version: 0.17.0
Summary: Powerful data structures for data analysis, time series,and statistics
Home-page: http://pandas.pydata.org
Author: The PyData Development Team
Author-email: pydata@googlegroups.com
License: BSD
Location: /Library/Python/2.7/site-packages
Requires: numpy, python-dateutil, pytz


Important note for devops: pip by default install a stable version of the package. If you need to install an unstable version of the package (latest version) use --pre flag to install pre-release package.

If you want to manage your custom python repository package versions (RC, Alpha, Beta, Stable, etc) add classifiers to your setup.py script like below:

setup(...,
classifiers=[
'Development Status :: 4 - Beta',
'Environment :: Console',
'Environment :: Web Environment',
'Intended Audience :: End Users/Desktop',
'Intended Audience :: Developers',
'Intended Audience :: System Administrators',
'License :: OSI Approved :: Python Software Foundation License',
'Operating System :: MacOS :: MacOS X',
'Operating System :: Microsoft :: Windows',
'Operating System :: POSIX',
'Programming Language :: Python',
'Topic :: Communications :: Email',
'Topic :: Office/Business',
'Topic :: Software Development :: Bug Tracking',
],
)



The important part is Development Status :: 4 - Beta, which for stable release you would use Development Status :: 5 - Production/Stable.

To see list of pypi Development Status values read from the below link:
https://pypi.python.org/pypi?%3Aaction=list_classifiers

Now you can give versioning like Django 2.0b1 (for pre release) and Django 1.11.6 for stable release.
To see different classifiers see the link Django link below to learn more:
https://pypi.python.org/pypi/Django

#django #pip #pre_release #freeze #package #python #devops
pip install from git repository:
pip install git+https://github.com/tangentlabs/django-oscar-paypal.git@issue/34/oscar-0.6

#git #repo #pip #install
put a key in dictionary into another key and remove it from dictionary:

Idiots way:
data = {
'username': 'alireza@gmail.com'
}
data['email'] = data['username']
del data['username']

Tech C**P way:
data = {
'username': 'alireza@gmail.com'
}
data['email'] = data.pop('username')

pop access the data and remove it from data payload. In case username is not in data, an error will be raised. To prevent such an error give it default value:
data['email'] = data.pop('username', 'NO_USERNAME')

#pop #python #dictionary #code_improvement
There is a mgic method in python called contains which should return true if item is in self, false otherwise.
The method signature of contains:
object.__contains__(self, item)

Samples:
>>> 'sample_output'.__contains__('sample')
True
>>> 'sample_output'.__contains__('ASL')
False

BUT DONT USE this magic methods this way, as it should be used in internal class operations.
Membership test operator for this is in. In case you want to test membership:
>>> 'sample' in 'sample_output'
True
>>> 'ASL' in 'sample_output'
False

Side note: methods which are started by _ (underscore) should not be called directly outside of class scope. They meant for internal class operations.

#python #membership #in #contain #exist