Tech C**P
14 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
In couchDB you have to plan for views beforehand. As you go along, data will grow and it would be a cumbersome task to change views and wait for data reindexing.

In order to implement a view in couchDB you write JS code. For writing view you have to at least have a map function (reduce can be ignored).

An example of a view to get users by their corresponding uid:

function(doc) {
if (doc.type == "user")
emit(doc.user_id, doc);
}

Here we pass doc which is database documents to our function and check if type of document is user (type field is added it is not related to couchDB view), we will emit (let's say return) user document. It's key is user_id and it's value will be user document.

#view #couchdb
You can create different hosts on your ssh config (~/.ssh/config) in order to prevent typing username and password all time and speedup working with remote servers.

A sample host on ssh config:
Host your_server_name
HostName 192.168.12.182
User my_user_name_on_remote_server
Port 22
IdentityFile ~/.ssh/id_rsa

Now save the config file and exit. You can now ssh to server as below:
ssh your_server_name

INSTEAD OF USING:
ssh my_user_name_on_remote_server@192.168.12.182

#linux #sysadmin #ssh
InfluxDB is time series database that can store huge amount of data for different metrics like CPU, RAM, Storage usage. If you have Grafana in place, one of the databases that would work with it is InfluxDB.

If you setup InfluxDB you can type influx and hit enter to see InfluxDB console (like MySQL):
infladmin@my_host:~$ influx
Connected to http://localhost:8086 version 1.2.2
InfluxDB shell version: 1.2.2

Databases in InfluxDB are called well Database :) and tables are called Measurement. To see list of all databases and make a database active do as below:
> SHOW DATABASES
name: databases
name
----
_internal
my_db
monitoring
some_other_db

> USE my_db
Using database my_db

You are now on my_db database. To display list of measurements (table) and query it:
> SHOW MEASUREMENTS
name: measurements
name
----
cpu_stat
mem_stat

> SELECT * FROM cpu_stat LIMIT 2;
name: cpu_stat
time my_tag metric value
---- ----------- ------ -----
1503999476000000000 CPU_CORE3 CORES 0
1503999476000000000 total 16

Yes, it is almost similar to MySQL. The main point of such a database is to use monitoring tool in place and push metrics into influxDB, then make graphs by using Grafana.

#influxdb #grafana #measurement #time_series_db
Display line numbers in vim. If a file is open with vim press ESC (if you are in insert mode) and then press colon (:) and type:

set number


The above command will print line numbers in front of each line.

Now if you want to hide line numbers you just need to press colon again and type:

set nonumber


#vim #tricks #line_number #nonumber #number
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