Split Video From Command Line Using FFmpeg
I have Vlc installed, so I enabled to view the advanced controls and recorded the required portion.
This is the video splitted with Vlc.
It is the primary time standard by which the world regulates clocks and time. To get current UTC time in Python, we can use datetime
module.
In [5]: import datetime In [6]: datetime.datetime.now(datetime.timezone.utc) Out[6]: datetime.datetime(2014, 11, 22, 14, 42, 21, 34435, tzinfo=datetime.timezone.utc)
It is a system for describing instants in time, defined as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970, not counting leap seconds. To get Unix timestamp, we can use time
module.
In [8]: import time In [9]: time.time() Out[9]: 1416667432.5664258 In [10]: int(time.time()) Out[10]: 1416667766
With Pyhon 3.4, we can directly get timestamp from UTC.
In [13]: datetime.datetime.utcnow().timestamp() Out[13]: 1416649608.58369
To convert Unix timestamp to UTC we can use utcfromtimestamp
function.
In [21]: datetime.datetime.utcfromtimestamp(1416668401) Out[21]: datetime.datetime(2014, 11, 22, 15, 0, 1)
To convert UTC time object to Unix time, we can use strftime
function.
In [38]: dt = datetime.datetime.now() In [39]: dt.strftime("%s") Out[39]: '1416668938'
Alternatively, we can use calendar.timegen
function.
In [46]: import calendar In [47]: dt = datetime.datetime.utcnow() In [48]: calendar.timegm(dt.utctimetuple()) Out[48]: 1416669150
When using iPython, users can import required modules to test them. After importing them, if they get modified either by user or some other process, users have to reload it for futher usage.
Depending on the Python version, appropriate reload
function can reload modules.
# Python 2.x In [15]: imp.reload(module) # Python 3.0–3.3 In [15]: imp.reload(module) # Python 3.4+ In [15]: importlib.reload(module)
Instead of manually reloading, ipython has autoreload
extention which can auto reload modules. For that, load the extention and activate it.
In [15]: %load_ext autoreload In [16]: %autoreload 2
This can be added to ipython config file so that autoreload gets activated, whenver it starts.
$ ipython profile create
This creates a default config file. Open config file which is present at ~/.ipython/profile_default/ipython_config.py
and add these two lines to it.
c.InteractiveShellApp.extensions = ['autoreload'] c.InteractiveShellApp.exec_lines = ['%autoreload 2']
Note that it won't reloads c extensions automatically.
I was writing custom template tags for one of my Django package. I came across a situation where I had lists in string format. I need to convert them into lists.
temp_str = '[345, 3, 456, 45]' required_obj = [345, 3, 456, 45]
There are two methods to get this job done.
This can be used to evaluate strings containing Python values from untrusted sources without parsing values. import ast ast.literal_eval(temp_str)
This is used to deserialize a string to python object using a conversion table.
import json json.loads(temp_str) These two functions come in handy whenever you want to convert a list of python objects to their correct types. For example, if you have list of python objects like this obj_list = ['hello', '3', '3.64', '-1']
You can convert them to their corresponding types using these functions.
def converter(l): for i in l: try: yield json.loads(i) except ValueError: yield i def converter(l): for i in l: try: yield ast.literal_eval(i) except ValueError: yield i
They yield a objects of corresponding types new_obj_list = ['hello', 3, 3.64, -1]
from celery import Celery
app = Celery('tasks', backend='amqp',
broker='amqp://<user>:<password>@<ip>/<vhost>')
@app.task
def add(x, y):
return x + y
add.apply_async(args=[1,2])
celery worker -A my_app -l info
# add new user
sudo rabbitmqctl add_user <user> <password>
# add new virtual host
sudo rabbitmqctl add_vhost <vhost_name>
# set permissions for user on vhost
sudo rabbitmqctl set_permissions -p <vhost_name> <user> ".*" ".*" ".*"
# restart rabbit
sudo rabbitmqctl restart
from celery import Celery
app = Celery('tasks', backend='amqp',
broker='amqp://<user>:<password>@<ip>/<vhost>')
def add(x, y):
return x + y
In [1]: from remote import add
In [2]: add.delay(1, 2)
Out[2]: <AsyncResult: 3eb96a11-aa61-46d3-9b9d-e0e1703438d0>
In [3]: b.delay(2, 3)
Out[3]: <AsyncResult: ec40db1a-a43c-4486-9530-0a3153fe1380>
In [4]: b.delay(3, 4)
Out[4]: <AsyncResult: ca53a4c7-061b-408e-82ee-86c2d43d21a0>
celery worker -l info -A remote
[2014-11-09 00:01:19,168: INFO/MainProcess] Received task: remote.add[c2d2bb27-ff5f-47da-b2b9-6fb11669ee1a]
[2014-11-09 00:01:19,170: INFO/MainProcess] Received task: remote.add[8daa1a5c-17d0-46dc-9c93-faf7fbeccdd9]
[2014-11-09 00:01:19,172: INFO/MainProcess] Received task: remote.add[79603d15-24f1-43f8-b8b7-525b7cd4b9a2]
[2014-11-09 00:01:19,401: INFO/MainProcess] Task remote.add[8daa1a5c-17d0-46dc-9c93-faf7fbeccdd9] succeeded in 0.226168102003s: 3
[2014-11-09 00:01:19,462: INFO/MainProcess] Task remote.add[c2d2bb27-ff5f-47da-b2b9-6fb11669ee1a] succeeded in 0.286503815001s: 5
[2014-11-09 00:01:19,464: INFO/MainProcess] Task remote.add[79603d15-24f1-43f8-b8b7-525b7cd4b9a2] succeeded in 0.288741396998s: 7
sudo apt-get install pandoc
pandoc readme.md --from markdown --to rst -s -o readme.rst
pip install pypandoc
import pypandoc
output = pypandoc.convert('somefile.md', 'rst')
Developers and hackers prefer using terminal and spend a lot of time on it. Instead of typing long commands over and over, they can be aliased to shortnames. The shell builtin alias allows users to set aliases.
One of the most used command while setting up development environment is pip install -r requirements.txt
This can be aliased to pir
.
alias pir='pip install -r requirements.txt
Now to install requirements, type pir
and pressing enter. Here are some other aliases related to python which will be useful on a daily basis.
alias py='python' alias ipy='ipython' alias py3='python3' alias ipy3='ipython3' alias jn='jupyter notebook' alias wo='workon' alias pf='pip freeze | sort' alias pi='pip install' alias pun='pip uninstall' alias dj="python manage.py" alias drs="python manage.py runserver" alias drp="python manage.py runserverplus" alias drps="python manage.py runserverplus --print-sql" alias dsh="python manage.py shell" alias dsp="python manage.py shell_plus" alias dsps="python manage.py shell_plus --print-sql" alias dsm="python manage.py schemamigration" alias dm="python manage.py migrate" alias dmm="python manage.py makemigrations" alias ddd="python manage.py dumpdata" alias dld="python manage.py loaddata" alias dt="python manage.py test"
Just add the above aliases to your ~/.bashrc
or ~/.zshrc
. That's it. Hpy alsng!