Python UUID

>>> import uuid

>>> # make a UUID based on the host ID and current time
>>> uuid.uuid1()
UUID('a8098c1a-f86e-11da-bd1a-00112444be1e')

>>> # make a UUID using an MD5 hash of a namespace UUID and a name
>>> uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org')
UUID('6fa459ea-ee8a-3ca4-894e-db77e160355e')

>>> # make a random UUID
>>> uuid.uuid4()
UUID('16fd2706-8baf-433b-82eb-8c7fada847da')

>>> # make a UUID using a SHA-1 hash of a namespace UUID and a name
>>> uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org')
UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d')

>>> # make a UUID from a string of hex digits (braces and hyphens ignored)
>>> x = uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}')

>>> # convert a UUID to a string of hex digits in standard form
>>> str(x)
'00010203-0405-0607-0809-0a0b0c0d0e0f'

>>> # get the raw 16 bytes of the UUID
>>> x.bytes
b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f'

>>> # make a UUID from a 16-byte string
>>> uuid.UUID(bytes=x.bytes)
UUID('00010203-0405-0607-0809-0a0b0c0d0e0f')

References :
https://docs.python.org/3.5/library/uuid.html

Join bytes in Python

def run_server():
    HOST = ''
    PORT = 11000
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((HOST, PORT))
    s.listen(10)

    while True:
        conn, addr = s.accept()
        print("{0} - ".format(get_counter()) + 'Connection from : {0}'.format(addr))

        data_recv=conn.recv(1024)
        data=bytearray()

        conn.settimeout(1)

        while len(data_recv)>0:
            data.extend(data_recv)
            try:
                data_recv=conn.recv(1024)
            except socket.timeout:
                break

        data=str(data)
        if data=="1":
            message=get_message1()
            conn.sendall(message)
        conn.close()

References :

http://stackoverflow.com/questions/9099145/where-are-python-bytearrays-used

Bytes stream in Python

    @staticmethod
    def from_bin(dataB):
        reader=io.BytesIO(dataB)
        length1= reader.read(4)
        length2=reader.read(4)
        length3=reader.read(4)
        macs=reader.read(length1)
        uid=reader.read(length2)
        signature=reader.read(length3)
        reader.close()

        message=Message(macs,uid,signature)
        return message

References :
https://docs.python.org/3/library/io.html#io.BytesIO http://chimera.labs.oreilly.com/books/1230000000393/ch05.html#_solution_78
https://docs.python.org/2/library/io.html

Serialize memory objects to binary in Python

try:
    import cPickle as pickle
except:
    import pickle
import pprint

data1 = [ { 'a':'A', 'b':2, 'c':3.0 } ]
print 'BEFORE:',
pprint.pprint(data1)

data1_string = pickle.dumps(data1)

data2 = pickle.loads(data1_string)
print 'AFTER:',
pprint.pprint(data2)

print 'SAME?:', (data1 is data2)
print 'EQUAL?:', (data1 == data2)

References:

https://docs.python.org/3/library/pickle.html
https://docs.python.org/2/library/pickle.html
https://pymotw.com/2/pickle/

colorama – Python Package

Description:
Cross-platform colored terminal text

Available formatting constants are:

Fore: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET.
Back: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET.
Style: DIM, NORMAL, BRIGHT, RESET_ALL

Example :

from colorama import Fore, Back, Style
print(Fore.RED + 'some red text')
print(Back.GREEN + 'and with a green background')
print(Style.DIM + 'and in dim text')
print(Style.RESET_ALL)
print('back to normal now')

 

References :
https://pypi.python.org/pypi/colorama
http://stackoverflow.com/questions/287871/print-in-terminal-with-colors-using-python

AUTOCOMPLETE WITH PYGOBJECT3 IN PYCHARM

I recently started learning pygobject as part of my gsoc project and a major problem that I faced was not having autocomplete working with the gobject-introspection library in pycharm. No matter how much we deny it autocompletion helps us developers in not repetitively referring to documentation and guides and increases productivity at the same time.

Here is a solution for people faced with the same problem. First we need to edit the pyCharm idea.properties file:

  1. Navigate to the /bin folder.
  2. Locate the idea.properties file and open it for editing. (Chances are you will need to change the permissions.)
  3. Edit the line “idea.max.intellisense.filesize=1024” and make the value something like 10000.
  4. Save it!
  5. Restart pyCharm.

The above steps were done because all the modules in the Gobject-introspection library are binary modules and we need to generate stubs for it in order to have autocompletion. Gtk.py inside the library is too big for the initial buffer size of pyCharm that is 1024 and hence we increase it.

The next steps are fairly simple:

  1. Go to any existing pyGobject project or create a new python file and type or locate the following line:
    from gi.repository import Gtk
  2. Take your cursor to “Gtk” and press alt+enter and click on “Generate stubs for binary module”.
  3. Wait for the processes to finish.

Tada! You will have autocompletion working with Gobject-introspection library. This method can also be used for any other library with a similar problem.

References :

https://udayantandon.wordpress.com/2015/04/28/hello-world/