...
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.

Python HTMLParser and Vkontakte randomizer

Finally I've finish my first "program" on Python.py_vk The task is to parse people's id from web page where reposter's id stores. Main problems were:

  • web-page code is loading dynamically so there is no simple way to get ids from it, the best solution was - save section where id stores in .html file
  • I wanted to catch id + nickname but list of pairs was not a good decision when random works
  • I can't create a list which stores all found ids, it wiped every iteration
  • I have some unsupported chars in nicknames and they'd broke iteration
  • I've get a lot of junk while scan .html so I used regex to avoid them
  • I can't add various ids in list without adding one id to list recursively - and guess what? Yes, it's broke the iteration

What I've learned: Here will be a huge list of different things for indexing for further …

Read...

Python HTMLParser

py_vk How to spent two days if you know nothing about Python:

  • need parse HTML page code, where VK id and username of every person who shared post stores

 

with open('test.html', 'r', encoding='utf-8') as content_file:
    read_data = content_file.read()

from html.parser import HTMLParser
import re

class MyHTMLParser(HTMLParser):
    def handle_starttag(self, tag, attrs):
        vk_id = str(attrs)
        for line in vk_id:
            vk = re.findall('/\S+$', vk_id)
        vk_fnd = str(vk)
        if re.search('/\w+\'\)\]', vk_fnd):
            global vk_read
            vk_read = vk_fnd
            for ch in ['/', ')', '[', ']', '"', "'"]:
                if ch in vk_read:
                    vk_read = vk_read.replace(ch, "")
    def handle_data(self, data):
        global vk_name
        vk_name = str(data)
        assert isinstance(data, object)
        for line in vk_name:
            if re.match('\S+\s+\S+$', vk_name):
                print("@{0} - {1}".format(vk_read, vk_name))
                break


parser = MyHTMLParser()
parser.feed(read_data)

Now I know more. First bug and first fix: UnicodeEncodeError: 'charmap' codec can't encode character '\u0406' in position 15: character maps to <undefined>

with open('test.html', 'r', encoding='utf-8') as content_file:
    read_data = content_file.read()
'''
1. …

Read...

Plone installation

Will try to use Plone CMS for test cases, interesting to see what and how CMS on Python worked. Start here: https://plone.org/documentation/manual/installing-plone/installation-quick-guide For Python: Guide and I've use only one part from it here. I've done everything as guided above.

[root@web Python-2.7.6]# ls -ltr /usr/bin/python*
-rwxr-xr-x. 2 root root 4864 Jan 22  2014 /usr/bin/python2.6
-rwxr-xr-x. 2 root root 4864 Jan 22  2014 /usr/bin/python
lrwxrwxrwx. 1 root root    6 Nov  2 15:46 /usr/bin/python2 -> python
[root@web Python-2.7.6]# ls -ltr /usr/local/bin/python*
-rwxr-xr-x. 1 root root 6214493 Apr 20 11:44 /usr/local/bin/python2.7
-rwxr-xr-x. 1 root root    1674 Apr 20 11:48 /usr/local/bin/python2.7-config
[root@web Python-2.7.6]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@web Python-2.7.6]# which python
/usr/bin/python
[root@web Python-2.7.6]# sudo sh
sh-4.1# which python
/usr/bin/python
sh-4.1# python
Python 2.6.6 (r266:84292, Jan 22 2014, 09:42:36)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2

And Python lives here! Can have some troubles, like:

[root@web plone]# ./install.sh standalone

which: no python2.7 in (/sbin:/bin:/usr/sbin:/usr/bin) …

Read...

Заводим питончика дома или установка Python на Centos 6

Хороший гайд pythonздесь: https://github.com/h2oai/h2o/wiki/Installing-python-2.7-on-centos-6.3.-Follow-this-sequence-exactly-for-centos-machine-only

How to install Python 2.7.6 on CentOS 6.3 (6.2 and 6.4 okay too, probably others)

stolen from Daniel Eriksson. Thanks Daniel! http://toomuchdata.com/2012/06/25/how-to-install-python-2-7-3-on-centos-6-2/ (modified a little) CentOS 6.2 ships with Python 2.6.6 and depends on that specific version. Be careful not to replace it or bad things will happen. If you need access to a newer version of Python you must compile it yourself and install it side-by-side with the system version. Here are the steps necessary to install Python 2.7.6. Execute all the commands below as root. Either log in as root temporarily or use sudo.

Install development tools

In order to compile Python you must first install the development tools:

yum groupinstall "Development tools"

You also need a few extra libs installed before compiling Python or else you will run into problems later when trying to install various packages:

yum install zlib-devel
yum install bzip2-devel …

Read...

Python array

Начинаю учить Пайтон, буду набивать шпаргалки.

lst = ['spam', 'drums', 100, 1234]
print(lst[2])
100

Это мой первый массив, детка. Спустя время, это может показаться для меня смешным, но сейчас я очень доволен собой.

Read...