Did you know that you can apply
This is a simple class that sets a fake lesson in your class and insert a new lesson.
As you can see above we have used
#python #class #builtin #len #__len__
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 #SimpleHTTPServer #server
$ 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
Get your IP address from command line using
#linux #sysadmin #curl #ifconfig #ip #ip_address #ipv4
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
Usage in terminal is:
If you want it to use inside of your
#python #html2text #github #html #text
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
If you want to set a variable named
If you want to include a partial template (child template) into your parent template use
If one of your templates inherits from a main layout use
#python #jinja2 #extends #include #set #variable #set_variable #layout
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
For reading lines from a file, you can loop over the file object. This is memory efficient, fast, and leads to simple code:
#python #file #read #readline #efficiency
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
Tech C**P
with python compound statement in depth [ADVANCE]: with is ported into python in version 2.5. with is used to wrap the execution of a code block using methods defined by a context manager. The process of with statement: - The expression given in…
In case you have missed
with
tutorial read this post, which is related to the previous post.One of the benefits of
One of the ways that executes the query is using 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
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
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:
python #python3 #merge #dictionary
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
In case you want to
It will
You can also use:
It will reset your working directory and replace all changes (including the index).
#git #checkout #reset #hard
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
#python #jinja2 #comment #comment_out
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.
The general command is:
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
You can also copy from your remote server to your local machine by changing the order in scp as below:
The above command will copy the remote file called
#linux #sysadmin #scp #copy
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
Forwarded from خبرگزاری فارس
This media is not supported in your browser
VIEW IN TELEGRAM
📺 نسخه جدید تلگرام با امکانات جذابی مانند ارسال مجموعه عکس، فیلم به صورت یک مطلب و pin کردن پیام در کانال و... منتشر شد.
@Farsna
@Farsna
For enabling push notification on pushd server, you need to get a file with
We need to generate 2 pem files one called
To generate
And now generate the key pem file:
Restart the pushd and check for any error in
#pushd #openssl #p12 #cer #pem #push
.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