Tech C**P
14 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
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
In python when you open a file using open command the your can read the content of the file. read will read the whole content of the file at once, while readline reads the content of the file line by line.

NOTE: if file is huge, read() is definitely a bad idea, as it loads (without size parameter), whole file into memory.

NOTE: it is good practice to use the with keyword when dealing with file objects. The advantage is that the file is properly closed after its suite finishes, even if an exception is raised at some point. (We have reviewed with in depth a couple days ago)

NOTE: read function gets a size parameter that specifies the chunks read from a file. If the end of the file has been reached, f.read() will return an empty string.

For reading lines from a file, you can loop over the file object. This is memory efficient, fast, and leads to simple code:
>>>
>>> for line in f:
... print(line, end='')
...
This is the first line of the file.
Second line of the file

#python #file #read #readline #efficiency
One of the benefits of Django`'s `QuerySet is that you can apply filters on it, slice it and move it around your classes without actually hitting the database until you do so.

One of the ways that executes the query is using iteration:
for c in Course.objects.all():
print(c.title)
NOTE: the first time you iterate over the query, it hits the database and get the result NOT in each iteration.

In the below ways your query will be evaluated (executed):
- repr
- len
- list (e.g.: `list(Entry.objects.all())`)
- bool

#python #django #querySet #query_set #evaluation
If you are using python 3.5 or greater and want to merge to dictionaries:
>>> a = {'a': 1}
>>> b = {'b': 2}
>>> {**a, **b}
{'a': 1, 'b': 2}

That's it! You have merged 2 dictionaries in the most simplest way ever. :)

If you are using python 2.7 or python 3.4 or less, merge dictionaries as below:
def merge_two_dicts(a, b):
c = a.copy()
c.update(b)
return c

python #python3 #merge #dictionary
In normal git flow in case you have to checkout a file you would use:
git checkout -- your_file_name

In case you want to checkout multiple files you give other file names in front of checkout --. Sometimes there are bunch of modified files that you want to checkout all of them at once, so you need to go to the root of the project and:
git checkout -- .

It will checkout all files in your project.

You can also use:
git reset --hard

It will reset your working directory and replace all changes (including the index).

#git #checkout #reset #hard
If you want to comment a block of code out in Jinja2 template, use {# ... #}. It can also span into multiple lines:
{#
<tr class="">
<td colspan="2" style="font-family:Ubuntu,Helvetica,Arial,sans-serif; padding: 0px 0px 0px 20px;" class="">
Regards,
</td>
</tr>
<tr class="">
<td colspan="2" style="font-family:Ubuntu,Helvetica,Arial,sans-serif; padding: 0px 0px 30px 20px;" class="">
SYSTEM INFO
</td>
</tr>
#}

#python #jinja2 #comment #comment_out
It has been set to my routine task to copy folder/files from my local host or a remote linux server to another server or vise versa. scp linux command is used for this specific need.
The general command is:
scp YOUR_LOCAL_FILE USERNAME@YOUR_SERVER:~

The above command will copy a file from your local machine to a remote server. If you want to copy a folder to a remote machine user -r to recursively copy the whole folder into the remote.

You can also copy from your remote server to your local machine by changing the order in scp as below:
scp USERNAME@YOUR_SERVER:~/YOUR_REMOTE_FILE .

The above command will copy the remote file called YOUR_REMOTE_FILE from the home directory to your current path (.). Instead of dot you can provide your full path.

NOTE: use man scp and see tons of flags to master this command.

#linux #sysadmin #scp #copy
jinja2 has tons of filters for strings, lists, etc that can be used to make the process of, um well, filtering simpler.

To apply a filter you can use pipe symbol (|). In order to lower case`/`upper case a string:
{{"SOMETHING"|lower}}
{{"sOmThing"|upper}}

Or let's say getting the max number from a list:
{{ [1, 2, 3]|max }}

The filters are endless, see below for more:
http://jinja.pocoo.org/docs/2.10/templates/#filters

#python #jinja2 #template #filter #uppercase #lowercase #max
This media is not supported in your browser
VIEW IN TELEGRAM
📺 نسخه جدید تلگرام با امکانات جذابی مانند ارسال مجموعه عکس، فیلم به صورت یک مطلب و pin کردن پیام در کانال و... منتشر شد.
@Farsna
For enabling push notification on pushd server, you need to get a file with .p12 extension and .cer certificate file. For pushd to work you need to generate a .pem file and give its path in push configuration(`/etc/pushd/pushd.conf`).

We need to generate 2 pem files one called apns-cert.pem (generated from .cer file) and the other called apns-key.pem (generated from .p12 file).

To generate .pem file use openssl command, with the format below:
openssl pkcs12 -in YOUR_KEY.p12 -out apns-key.pem -nodes
NOTE: it may ask you for the password, enter the given password by whom that gave you the p12 file.

-in set your input file name and -out sets your output file name which will be generated.

And now generate the key pem file:
openssl x509 -in cert.cer -inform DER -outform PEM -out apns-cert.pem

Restart the pushd and check for any error in /var/log/pushd.

#pushd #openssl #p12 #cer #pem #push
lowercase/uppercase a function, variable or a string in PyCharm by using:
OS X: Command+Shift U

#pycharm #lowercase #uppercase
Download a static file from a remote URL by using requests in python:

# author -> Roman Podlinov
def download_file(url):
local_filename = url.split('/')[-1]
r = requests.get(url, stream=True)
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
return local_filename

NOTE: stream=True parameter is used for returning large files in chunk and save it into a file

#python #requests #stream #large_file #download
Modern_Linux_Administration.epub
2.3 MB
Modern Linux Administration
by Sam R. Alapati”

#ebook #epub #book #linux #sysadmin
To find all of users ids:

ps -eo uid


#linux sysadmin #ps #users #uid
Django tutorials will be published today or the day after, sorry for the delay. 🙈
In order to create a debian package from your shell script do as follow:

Let's consider our package name is dangling. We need to create 2 folders, first is called dangling and the second is DEBIAN which is inside of the dangling folder:
mkdir helloworld && mkdir helloworld/DEBIAN

You can copy the files into your package with the full paths on the destination filesystem. E.g. if you want to put a file in /usr/local/bin/ you put it in dangling/usr/local/bin/:
mkdir -p dangling/usr/local/bin
cp /usr/local/bin/dangling.sh dangling/usr/local/bin/

In your DEBIAN directory (created at the begining of the post), create a control file with the below content:
<span class="Apple-style-span" style="font-family: Consolas, Monaco, monospace; font-size: 12px; white-space: pre;">Package: dangling
</span><span class="Apple-style-span" style="font-family: Consolas, Monaco, monospace; font-size: 12px; white-space: pre;">Version: 0.1
</span><span class="Apple-style-span" style="font-family: Consolas, Monaco, monospace; font-size: 12px; white-space: pre;">Maintainer: Alireza Hoseini
</span><span class="Apple-style-span" style="font-family: Consolas, Monaco, monospace; font-size: 12px; white-space: pre;">Architecture: all
</span><span class="Apple-style-span" style="font-family: Consolas, Monaco, monospace; font-size: 12px; white-space: pre;">Description: would wipe all docker dangling volumes out

NOTE: These are the mandatory fields in the control file.

Now build the package using dpkg-deb:
dpkg-deb --build dangling

This creates a dangling.deb file, which you can now install on any Debian installation with following command:
dpkg -i dangling.deb

#debian #deb #create #package #dpkg #build
In case you want to add a custom font to OS X, you need to go to /Library/fonts and paste your custom fonts in this folder. No need to restart or logout.

reference: https://support.apple.com/en-us/HT201722

#apple #osx #font #fonts