Tech C**P
15 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
Shift time in python using datetime & timedelta:
import datetime
from datetime import timedelta

date1 = datetime.datetime.utcnow()
# output of date1: datetime.datetime(2017, 11, 8, 10, 24, 25, 19492)
date2 = date1 + timedelta(days=1)
# output of date2: datetime.datetime(2017, 11, 9, 10, 24, 25, 19492)

To get your new date in EPOCH:
at_epoch = (date2 - datetime.datetime(1970, 1, 1)).total_seconds()
# output of subtract: 1510223065.019492

To turn EPOCH to datetime:
import time

from_epoch = time.localtime(1510223065.019492)
# output: time.struct_time(tm_year=2017, tm_mon=11, tm_mday=9, tm_hour=13, tm_min=54, tm_sec=25, tm_wday=3, tm_yday=313, tm_isdst=0)

formatted_date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1510223065.019492))
# output: 2017-11-09 13:54:25

#python #epoch #datetime #time #timedelta #strftime
Customize pushd server to add a new field like subtitle or title for iOS push notification payload. As you may know ios payload is like below:
{
"aps":{
"alert":{
"body":"New flight is booked",
"title":"Boarding at 20:05",
"subtitle": ""Yay!
},
"category":"openFlightCategory"
},
}

pushd by default does not support subtitle in the payload, to add this do the following:

- open /usr/local/pushdcustomized/lib/payload.coffee file
- add @subtitle = {} to line 14
- add when 'subtitle' then @subtitle.default = value to line 28
- add the below function to line 51:
localizedMessage: (lang) ->
@localized('subtitle', lang)

Ok done in payload. Push now accept subtitle for iOS, but there is one step left. You need to send subtitle to iOS device.

Open /usr/local/pushdcustomized/lib/pushservices/apns.coffee file and go to line note.alert = alert (line 44), and change it to:
note.alert = {'title':payload.title.default, 'body':payload.msg.default, 'subtitle': payload.subtitle.default}

OK DONE :)

Restart your pushd server and enjoy title and subtitle in your iOS push notifications.

#pushd #ios #title #subtitle #coffee_script #push #notification
One the most useful commands in vim is to delete or change inside of a block like {} or inside of a charater like "SOME THING".

In case you want to just delete inside of something and you don't want to go into INSERT mode just press di keyboard buttons in order, otherwise press ci keyboard buttons to go in INSERT mode and change something.

Let's give an example. Suppose we have lines like below and we have opened it in vim:
user_id = "43dd94e5d79ffeb2ffffabd112d5e945"
name = "Alireza"
dob = "SECRET :)"

Place the cursor between double quotes somewhere in the SECRET :) and press d then press c keys and finally press double quote ("). The vim will search for the surrounding double quotes and will remove everything inside of it. Our output is:
user_id = "43dd94e5d79ffeb2ffffabd112d5e945"
name = "Alireza"
dob = ""

Now let's assume we have a block of code like below:
users_data = {
}

Go inside of the block of curly braces (`{}`) and put your cursor inside of the block. Now press c on your keyboard then press i afterward, and at the end press { on your keyboard. It will delete everything inside of {} for you and put your vim mode in INSERT mode. So you would have the following output:
users_data = {
}

You can do the same with every block of code and every character like (), [], '', etc.

ci stands for Change Inside
di stands for Delete Inside

#vim #tricks #commands #change #delete #ci #di
A quick way to comment/uncomment lines in vim:

Put your cursor on the first # character, press Ctrl+V, and go down until the last commented line and press x, that will delete all the # characters vertically.


For commenting a block of text is almost the same:

- First, go to the first line you want to comment, press Ctrl+V. This will put the editor in the VISUAL BLOCK mode.

- Then using the arrow key and select until the last line.

- Now press Shift+I, which will put the editor in INSERT mode and then press #. This will add a hash to the first line.

- Then press Esc (give it a second), and it will insert a # character on all other selected lines.

#vim #comment #comment_out #visual_block
Stay tuned for the Django video tutorials for the weekend. Yay!!!

Happy coding 💥💫
این رازِ پُر از عذاب پیشت باشد
تصویر من خراب پیشت باشد

شاید که اربعینت نبودم آقا !
این اشک علی الحساب پیشت باشد
😭😭😭😭
سائلم ؛ آب و دانه میخواهم
رحمت" مادرانه" میخواهم

آی "بی بی" گدا نمیخواهی!؟
پسر بی وفا نمیخواهی!؟😭😭😭

کاش میشد ز من سوال کنی
پسرم کربلا نمیخواهی!؟

پسر بی وفا نمیخواهی 😭😭😭
Media is too big
VIEW IN TELEGRAM
Routes implementation in `Django` and how to wire routes & views

#python #django #route #view #django_part8
In ls you can list files by using regex pattern as below:
ls data.subtitle_*

* is gready and list all the files that start with data.subtitle_.

Now lets say you want to get count of files which is listed by ls command. For that you can use wc -l as follow:
ls data.subtitle_* | wc -l

Now you shoud the count of files listed by the first command. This method of using 2 commands is piping commands, which is in fact separated by | (pipe)

#linux #sysadmin #ls #wc #command #bash #terminal
tr command in Linux can be used to trim special characters from a string in bash script. Let's say we want to remove underscore (_) from the string _hello_:
$ echo "_hello_" | tr -d "_"
hello
As you can see the output is hello.

-d flag causes to delete _ from the whole string.

In case you want to delete multiple special characters, that's easy as pie:
$ echo "_sal-am_" | tr -d "_-"
salam

We have removed - and _ from the string above.

Making lower case to uppercase:
tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ
asdasd
ASDASD

OR:
tr [:lower:] [:upper:]
asdasd
ASDASD

Turn {} into ():
$ echo '{hello}' | tr '{}' '()'
(hello)

#linux #tr #translate #trim #bash #sysadmin
Preventing installation of devDependencies node modules:

npm install in the package directory installs the dependencies in the local node_modules folder.

In global mode (ie, with -g or --global appended to the command), it installs the current package context (ie, the current working directory) as a global package.

By default, npm install will install all modules listed as dependencies in package.json.

With the --production flag (or when the NODE_ENV environment variable is set to production`), `npm will not install modules listed in devDependencies.

#node #npm #package_json #devDependencies #NODE_ENV
Use jq to process JSON in linux terminal. In order to parse JSON in linux you can use sed, awk and grep, but with jq it is far more easier.

https://stedolan.github.io/jq/

#jq #json #terminal #linux #sysadmin
Did you know that you can apply len python function on your classes? Let's say you have a Lessons class and when a use use len() function on your class it should return number of lessons inside of your class. For our sample let's say we have the below class:
class Lessons(object):
def __init__(self, lessons=0):
self.lessons = lessons

def insert(self, data):
# let's assume we have save data in database
self.lessons += 1

def __len__(self):
return self.lessons

This is a simple class that sets a fake lesson in your class and insert a new lesson.
std_lessons = Lessons(1)
print len(std_lessons) # output: 1

std_lessons.insert({})
print len(std_lessons) # output: 2

As you can see above we have used len on our class itself. In case you don't implemet __len__ on your class and use len() on it, there will be an AttributeError:
len(std_no_len)
Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: Lessons instance has no attribute '__len__'

__len__ is built-in class and can be implemented in different scenarios. There are tons of built-in functions that you can master and use to improve code quality and prove that you are professional.

#python #class #builtin #len #__len__
Did you know that the most easiest way to start a simple HTTP server using python is just a command?
$ python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...

SimpleHTTPServer is a python module that serve all the data that is present inside of the current directory. Install this package and enjoying playing around :)

#python #SimpleHTTPServer #server
Sometimes to know your IP address, you have to open a web browser and open a site that gives you the ip address of your system. But what if you are a sysadmin and you don't have access to a browser on Linux server? What if you don't wanna leave your terminal?

Get your IP address from command line using curl:
$ curl ifconfig.co
80.171.524.14

#linux #sysadmin #curl #ifconfig #ip #ip_address #ipv4
In order to extract only text from an HTML website in the most robust way without using regex or urlib or so, use the python library below:
https://github.com/aaronsw/html2text

Usage in terminal is:
Usage: html2text.py [(filename|url) [encoding]]

If you want it to use inside of your python code:
import html2text
print html2text.html2text("<p>Hello, world.</p>")

#python #html2text #github #html #text
More about jinja2:

If you want to set a variable named button:
{% set button = 'Login' %}

If you want to include a partial template (child template) into your parent template use include:
{% include 'fa.button.tpl' %}

BE CAREFUL that we have used {% NOT {{ for the code block.

If one of your templates inherits from a main layout use extends directive:
{% extends "layout.tpl" %}

#python #jinja2 #extends #include #set #variable #set_variable #layout