...
Just my blog

Blog about everything, mostly about tech stuff I made. Here is the list of stuff I'm using at my blog. Feel free to ask me about implementations.

Soft I recommend
Py lib I recommend

I'm using these libraries so you can ask me about them.

"Output" за год Python

Фиг знает, когда там принято делать выводы за год, я сделаю их сегодня. Screenshot_13 Ровно год назад, я поменял проект с простого сапорта виндовых серверов, на какой-то мудреный девелопмент-недопрограмминг. Для общего понимания читателю, если он читает - я ничего не смыслил в таких словах из программирования как: функция, переменная, стринги, интеджеры. То есть я как-бы основные понятия знал, даже что-то ковырял и делал на уровне сайта и 'php' (прости господи), но если я слышал, например слово "декоратор функции" или "лямбда функция" - я просто делал вид, что думал о чем-то более важном. Так вот, поставил я себе задачу, выучить этот ваш "Python" - задачу все еще выполняю, но мне кажется, я заслужил  на то, чтобы отметить один пройденный этап. Не смотря на все злоключения и лень поганую, честно признаться, на само изучение я потратил не больше двух месяцев, первый мой прорыв был еще в мае, когда я попытался сделать …

Read...

For list of dicts: merge same key values, sum another diff. values, count each merge iteration +1 Python

If anybody can help me with some task? I have list of dicts(or tuples), where: if tuples:

 comment_id, user_id,     comment_date, comment_time, comment_likes
('51799',   '112801710', '2015-12-07',  '00:03:21',   '0'),
('51761',   '112801710', '2015-12-06',  '19:31:46',   '3'),
('51764',   '112801710', '2015-12-06',  '19:54:19',   '0'),
('51741',   '112801710', '2015-12-06',  '14:17:34',   '2'),
('51768',   '52879933',  '2015-12-06',  '20:03:34',   '0'),
('51766',   '52879933',  '2015-12-06',  '21:33:34',   '0'),

or can be converted to dict like:

{'comm_count': 1, 'user_id': '217407103', 'likes': 0},
  • comment_id - is always unique and cannot meet twice in this list,
  • user_id - is not unique for this list, it can be there as much times as comments were left in the set of posts (naturally I wanted to use this as counter)
  • comment_date and comment_time - can be ignored, needed to sel from db,
  • comment_likes - how much likes each comment has.

The task - make one list of tuples or dictionaries where I have just one 'user_id' unique, next how much 'likes' …

Read...

Шпаргалки для Python 3

Сохранил себе, даю и вам. Всякие мудаки в интернете по линкам на эти гайды делают редирект на свои грязные сайты.

 python_shpora

 

PDF - внизу: memento python 3 - russian memento python 3 english KeyMap for Pycharm PyCharm_ReferenceCard  

Read...

Django static files

How to find where def. static stores?

[root@test smm_py]# python3 manage.py findstatic css/base.css admin/js/core.js
No matching file found for 'css/base.css'.
Found 'admin/js/core.js' here:
  /usr/local/lib/python3.4/site-packages/django/contrib/admin/static/admin/js/core.js

Then move it into the folder you need. [su_note]Don't forget to chown and chmod[/su_note] Then update django's.conf file:

Alias /static/ "/var/www/smm_tools/site/"

And check the settings.py:

103 # Static files (CSS, JavaScript, Images)
104 # https://docs.djangoproject.com/en/1.8/howto/static-files/
105
106 STATIC_URL = '/static/'

https://docs.djangoproject.com/en/1.8/howto/static-files/  

Read...

Django + Python3.4 + Mysql(MariaDB)

Just hint for future me: [su_note]How to install django with mysqld - log[/su_note]

[root@test smm_py]# vim settings.py 
[root@test smm_py]# python manage.py makemigrations
                    import MySQLdb as Database
                    ImportError: No module named 'MySQLdb'
                    django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb'
[root@test smm_py]# pip install mysql-python3
                    Collecting mysql-python
                    Downloading MySQL-python-1.2.5.zip (108kB)
                    100% || 110kB 812kB/s 
                    ImportError: No module named 'ConfigParser'
                    ----------------------------------------
                    Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-t64nwwtb/mysql-python
[root@test smm_py]# easy_install mysql-python
                    ImportError: No module named 'ConfigParser'
                    ImportError: No module named 'ConfigParser'
[root@test smm_py]# pip3 install ConfigParser
                    Collecting ConfigParser
                    _KEYCRE = re.compile(ur"%\(([^)]+)\)s")
                    SyntaxError: invalid syntax
                    ----------------------------------------
[root@test smm_py]# pip3.4 install --allow-external mysql-connector-python mysql-connector-python
                    Collecting mysql-connector-python
                    Downloading http://cdn.mysql.com/Downloads/Connector-Python/mysql-connector-python-2.0.4.zip (277kB)
                    100% || 278kB 3.7MB/s 
                    Successfully built mysql-connector-python
                    Successfully installed mysql-connector-python-2.0.4
[root@test smm_py]# vim /smm_py/settings.py 
                     76 
                     77 DATABASES = {
                     78     'default': {
                     79 'ENGINE': 'django.db.backends.mysql',
[root@test smm_py]# python3 manage.py makemigrations
                    django.core.exceptions.ImproperlyConfigured: 'mysql.connector.django' isn't an available database backend.
                    Try using …

Read...

Django + apache2 + mod_wsgi

This is conf file to allow apache run Django applications.

# Web site at /var/www/smm_tools
# Python scripts at /var/www/smm_tools/smm_py

#LoadModule wsgi_module /usr/lib64/httpd/modules/mod_wsgi.so

<VirtualHost *:80>
    ServerName smm.www.trianglesis.org.ua
    ServerAlias www.smm.www.trianglesis.org.ua
    ServerAdmin it@www.trianglesis.org.ua
    DocumentRoot /var/www/smm_tools/

    ErrorLog "|/usr/sbin/rotatelogs /var/log/smm_tools/cargo.error.%Y-%m-%d.log 86400"
    CustomLog "|/usr/sbin/rotatelogs /var/log/smm_tools/cargo.access.%Y-%m-%d.log 86400" combined
    ServerSignature On

    Alias /css/         "/var/www/smm_tools/site/css"
    Alias /js/          "/var/www/smm_tools/site/js/"
    Alias /templates/   "var/www/smm_tools/site/templates/"
    Alias /fonts/       "/var/www/smm_tools/fonts/"

    #WSGIScriptAlias / /var/www/smm_tools/smm_tools.wsgi
    #WSGIPythonPath /var/www/smm_tools

    <Directory /var/www/smm_tools/smm_py/smm_py>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>
    WSGIDaemonProcess smm_tools python-path=/var/www/smm_tools/smm_py:/usr/local/lib/python3.4/site-packages/
    WSGIProcessGroup smm_tools
    WSGIScriptAlias / /var/www/smm_tools/smm_py/smm_py/wsgi.py

</VirtualHost>

Use these links:

  • https://www.digitalocean.com/community/tutorials/how-to-run-django-with-mod_wsgi-and-apache-with-a-virtualenv-python-environment-on-a-debian-vps
  • https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-ubuntu-14-04
  • https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/

Don't forget to use correct IPs - domains, and addresses. Especially if proxy pass is used.    

Read...

Allow Apache run python scripts

This is a simple ex. to allow apache run .py

# Python site at /var/www/py_site

LoadModule wsgi_module /usr/lib64/httpd/modules/mod_wsgi.so

<VirtualHost *:80>
 ServerName test.www.trianglesis.org.ua
 ServerAlias www.www.trianglesis.org.ua
 ServerAdmin test@test.com
 DocumentRoot /var/www/py_site/

 <Directory /var/www/py_site>
 Order allow,deny
 Require all granted
 Allow from all
 </Directory>

 <Directory /var/www/py_site/cgi-bin>
 Options +ExecCGI
 Require all granted
 Allow from all
 Options +ExecCGI
 AddHandler cgi-script .cgi .pl .py
 AddHandler cgi-script .py
 </Directory>

</VirtualHost>

   

Read...

PostgreSQL first run

This is the most common issue for PostgreSQL first run in Linux env: configure the server to allow TCP/IP connections postgresql server it can produce PostgeSQL cannot connect to database host Here I'll show how to fix it before you struggle with firewall and ports: I've found ot here: 17.3. Starting the Database Server and How Do I Enable remote access to PostgreSQL database server? My situation:

vi /var/lib/pgsql/data/pg_hba.conf

add the string:

host all all 10.10.29.0/24 trust

Postgresql_start with my range and next config:

vi /var/lib/pgsql/data/postgresql.conf

add the string:

listen_addresses='*'

Postgresql_start_ipall After that restart the service:

systemctl restart postgresql.service

also you can use an old 'service postgresql restart'

[root@centst010 ~]# service postgresql restart
Redirecting to /bin/systemctl restart  postgresql.service

And here it is:

[root@centst010 ~]# netstat -anp | grep 5432
tcp        0      0 0.0.0.0:5432            0.0.0.0:*               LISTEN
tcp        0      0 192.168.1.40:5432       192.168.1.22:65369      ESTABLISHED
tcp6       0      0 :::5432                 :::*                    LISTEN

   

Read...

Вначале ты...

Вначале, ты решаешь поучить Python, начинаешь немного углубляться... ... и приходишь в себя в тот момент, когда читаешь о фреймворках.

Read...

Going deeper with Python or HTMLParser and Vkontakte randomizer comes back!

Hello, for anybody who read this blog.py_vk Last time I'm trying to parse saved HTML page to get Vkontakte ids and randomly select one of them each time: here and here. Now I'll try to go deeper and use different way to extract data from life webpage without sawing it to the folder with python script. For my opinion, using some googling I should use this:

  • http://docs.python-guide.org/en/latest/scenarios/scrape/
  • http://stackoverflow.com/questions/2081586/web-scraping-with-python
  • later I will add some more KB

The small plan:

  • Add URL of parsed page:
    • to txt file - and them get it from file to python
    • to console, after python request to user
  • Get all found ids and save it list to file ids.csv
    • optionally with Name+id
    • or just id if names will produce many encoding errors
  • Get one id from the list by random choice
    • optionally save it to winner.txt
    • or just show in console result

Some inconvenience with this way:

  • Additional …

Read...