Python

===python noted===
- Tham chiếu ( Pass by Reference ) & Tham trị (pass by value): python list la tham chieu,
php array la tham tri

def func(list1):
list1[0]="tuan"

list1 = ["thu1","thu2"]
print(list1)
func(list1)
print(list1)

def change(x):
x = 3

x = 1
change(x)
print(x)

- Check a variable is a list or tuple or dictionary...
if type(a) is list:
   ...
- Loop with position index and value
for k,v in enumerate(list):
   ...

- Two type of make a new list
 newlist = ["tuan","thu"]
 newlist = list(("tuan","thu"))

- Two type of copy a list
lista = ["tuan","thu"]
listb = lista.copy()
listc = list(list)

- Two type of make a tuple
 newtuple = ("tuan","thu")

 newtuple = tuple(("tuan","thu"))

- Two type of make a new set
 newset = {"tuan","thu"}
 newset = set(("tuan","thu"))

- Two type of copy a set
seta = {"tuan","thu"}
setb = seta.copy()
setc = set(seta)

- Two type of make a new dict
 newdict = {"tuan":"thu","thu":"tuan}
 newdict = dict(tuan="thu",thu="tuan)

- Two type of copy a set
dicta = {"tuan":"thu"}
dictb = dicta.copy()
dictc = dict(dicta)

- Multi-inheritance

class A

class B

class C(A,B)

+ A is higher priority the B, if A and B have the same func then func of A will overide func of B

+ Just same name then will override(not want to care about the number of args)

+ Call to __init__ of parent
name_of_parent_class.__init__(self,param)

+ super function
Return a proxy object that delegates method calls to a parent or sibling class of type. This is useful for accessing inherited methods that have been overridden in a class

python2
class SubClass(MyParentClass):
 def __init__(self):
   super(SubClass, self).__init__()
python3

class SubClass(MyParentClass):
 def __init__(self):
   super().__init__()


#positional arguments, keyword argument
def add_more(a,b="tuan",c="du"):
    print(a)
    print(b)
    print(c)

add_more("du",b="thu",c="yeu thu")

# *arguments(arbitrary arguments)  =>tuple

# **keywords =>dictionary

def cheese(kind,*args,**kargs):
    print(kind)
    for i in args:
        print(i)
    for k in kargs:
        print(k,kargs[k])
 
cheese("mike","yeu thu",thu="du")

#Modules,Packages
#__init__.py
#__all__ = ["echo", "surround", "reverse"]

#Brief Tour of the Standard Library
#Logging
import logging
logging.debug('Debugging information')
logging.info('Informational message')
logging.warning('Warning:config file %s not found', 'server.conf')
logging.error('Error occurred')
logging.critical('Critical error -- shutting down')

#concatenate string

a= 1
b= "tuan"
c1= b + " " + str(a)
c1 = "{} haha{} ".format(a,b)

list1 = ["tuan","yeu","thu"]
abc = " ".join(list1)
print(abc)

str1 = "tuan yeu thu"
bcd = str1.split()
print(bcd)

Comparing dates in Python

Comparing dates is quite easy in Python. Dates can be easily compared using comparison operators (like <, >, <=, >=, != etc.)

import datetime
# date in yyyy/mm/dd format
d1 = datetime.datetime(2018, 5, 3)
d2 = datetime.datetime(2018, 6, 1)
# Comparing the dates will return
# either True or False
print("d1 is greater than d2 : ", d1 > d2)
print("d1 is less than d2 : ", d1 < d2)
print("d1 is not equal to d2 : ", d1 != d2)



===python command==============================


django-admin help


virtualenv -p python3.5 my_env

python3 -m venv tutorial-env


bug:>> http://stackoverflow.com/questions/14547631/python-locale-error-unsupported-locale-setting


source env/bin/activate


django-admin.py startproject tutorial


python manage.py startapp snippets == django-admin.py startapp tutorial
python manage.py help


python manage.py shell
python manage.py collectstatic


python manage.py createsuperuser


python manage.py runserver


django-admin compilemessages


rm -f tmp.db db.sqlite3
rm -r snippets/migrations
python manage.py makemigrations snippets
python manage.py migrate
python manage.py test polls


>>install mysqlclient
sudo apt-get install python-dev python3-dev
sudo apt-get install libmysqlclient-dev
pip install pymysql


pip install mysqlclient
pip install -r requirements.txt
pip freeze > requirements.txt


===elasticsearch command=======================


sudo -i service elasticsearch start
sudo -i service elasticsearch stop


sudo nano /etc/elasticsearch/elasticsearch.yml
structure : index(db)>>type with mapping(table)
check es run: curl -XGET 'localhost:9200/?pretty'
get index:
get mapping:curl -XGET 'localhost:9200/twitter/_mapping/tweet?pretty'


====================logstash=============================


config: sudo nano /etc/logstash/logstash.yml
fix bug for running: create a file >>/etc/logstash/conf.d/empty.conf
start: sudo systemctl start logstash.service
run mapping anaywhere: sudo /usr/share/logstash/bin/logstash --path.settings /etc/logstash -f es-test-input.conf
auto run config when place file config into folder “/etc/logstash/conf.d/”
log: tail -f /var/log/logstash/logstash-plain.log
Access: http://localhost:9200/

example file attach:
file: event-init.conf
input {
  jdbc {
  jdbc_connection_string => "jdbc:mysql://172.30.1.13:3306/nahi_sg_pro_20161018"
  jdbc_user => "root"
  jdbc_password => "123456"
  jdbc_driver_library => "/home/tuanvd02/Documents/projects/ES/mysql-connector-java-5.1.21.jar"
  jdbc_driver_class => "com.mysql.jdbc.Driver"
  statement => "select e.id, e.name as Event_Name,is_hot, e.address, e.image, e.description, es.sections, t.Tags_name,e.modified
from sg_events e
left join (
select es.event_id, GROUP_CONCAT(s.code SEPARATOR '; ') as sections
from sg_event_sections es 
left join sg_sections s on s.id = es.section_id
group by es.event_id
     ) as es on e.id = es.event_id
left join (
select te.event_id, GROUP_CONCAT(concat(t.id, '|', t.tname) SEPARATOR ';') as Tags_name
from sg_tag_events te 
left join tags t on t.id = te.tag_id
group by te.event_id
) as t on e.id = t.event_id
where e.active = 1 and e.approval ='Approval'and e.start_show <= now() and e.expiration_show >= now()"
  add_field => {"type" => "sg_events"}
}


}
output {
  elasticsearch {
  hosts => "localhost:9200"
  index => "es_event2"
  document_id => "%{id}"
  document_type => "%{type}"
}
}

file: event-update.conf
input {
  jdbc {
  jdbc_connection_string => "jdbc:mysql://172.30.1.13:3306/nahi_sg_pro_20161018"
  jdbc_user => "root"
  jdbc_password => "123456"
  jdbc_driver_library => "/home/tuanvd02/Documents/projects/ES/mysql-connector-java-5.1.21.jar"
  jdbc_driver_class => "com.mysql.jdbc.Driver"
  schedule => "* * * * *"
  statement => "select e.id, e.name as Event_Name,is_hot, e.address, e.image, e.description, es.sections, t.Tags_name,e.modified
from sg_events e
left join (
select es.event_id, GROUP_CONCAT(s.code SEPARATOR '; ') as sections
from sg_event_sections es 
left join sg_sections s on s.id = es.section_id
group by es.event_id
     ) as es on e.id = es.event_id
left join (
select te.event_id, GROUP_CONCAT(concat(t.id, '|', t.tname) SEPARATOR ';') as Tags_name
from sg_tag_events te 
left join tags t on t.id = te.tag_id
group by te.event_id
) as t on e.id = t.event_id
where e.active = 1 and e.approval ='Approval'and e.start_show <= now() and e.expiration_show >= now() and e.modified >= DATE_SUB(NOW(), INTERVAL 12000000000 MICROSECOND)"
  add_field => {"type" => "sg_events"}
}

}
output {
  elasticsearch {
  hosts => "localhost:9200"
  index => "es_event2"
  document_id => "%{id}"
  document_type => "%{type}"
}
}





=====================kibana================================


sudo -i service kibana start
sudo -i service kibana stop


config: /etc/kibana/kibana.yml




# DEPLOY----------------------------------
sudo apt-get update
sudo apt-get install  libapache2-mod-wsgi-py3


#Virtual host
<VirtualHost *:80>


       ServerName dev.core-django.com


       ServerAlias dev.core-django.com


       WSGIScriptAlias / /home/tuanvd02/Documents/projects/core-django/src/config/wsgi.py


       WSGIDaemonProcess dev.core-django.com python-path=/home/tuanvd02/Documents/projects/core-django/src python-home=/home/tuanvd02/Documents/projects/core-django/env/lib/python3.5/site-packages
       WSGIProcessGroup dev.core-django.com


       DocumentRoot /home/tuanvd02/Documents/projects/core-django/src


       ErrorLog /home/tuanvd02/Documents/projects/core-django/error.log


       Alias /static_cdn/ /home/tuanvd02/Documents/projects/core-django/static_cdn/
       Alias /media_cdn/ /home/tuanvd02/Documents/projects/core-django/media_cdn/


       <Directory /home/tuanvd02/Documents/projects/core-django/static_cdn>
               Require all granted
       </Directory>


       <Directory /home/tuanvd02/Documents/projects/core-django/media_cdn>
               Require all granted
       </Directory>


       <Directory /home/tuanvd02/Documents/projects/core-django/src/config>
               <Files wsgi.py>
                       Require all granted
               </Files>
       </Directory>
</VirtualHost>



======================python crawler project====================
https://askubuntu.com/questions/870530/how-to-install-geckodriver-in-ubuntu













Comments