Skip to content

python -c "from collections import OrderedDict; import os; print(':'.join(list(OrderedDict.fromkeys(os.environ['PATH'].split(':')))))"

import os

os.chdir("/dev/shm")

os.getcwd()

Python - to sort

https://stackoverflow.com/questions/23384230/how-to-post-multiple-value-with-same-key-in-python-requests

requests.post(url, data=[('interests', 'football'), ('interests', 'basketball')])
requests.post(url, data={'interests': ['football', 'basketball']})

$ export FLASK_APP=hello.py $ python -m flask run --host=0.0.0.0

Conda:

conda init
conda config --set auto_activate_base false
source ~/.bashrc

https://stackoverflow.com/questions/43580/how-to-find-the-mime-type-of-a-file-in-python

from mimetypes import MimeTypes
import urllib
mime = MimeTypes()
url = urllib.pathname2url('Upload.xml')
mime_type = mime.guess_type(url)
print mime_type

mime.from_buffer(file_contents)

https://stackoverflow.com/questions/3002085/python-to-print-out-status-bar-and-percentage

# list outdated python packages
pip3 list --outdated

# auto update all outdated python packages
pip3 list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1  | xargs -n1 pip3 install -U
def single_char_xor(input_bytes, char_value):
    output_bytes = b''
    for byte in input_bytes:
        output_bytes += bytes([byte ^ char_value])
    return output_bytes, char_value
import binascii

x = "100001101101011010000011011010100101011000011001101110001000100101100010100111010000100000110101101001011100111010001001001110100100001101101000001101011110010111001011"

def splitCount(s, count):
    return [''.join(x) for x in zip(*[list(s[z::count]) for z in range(count)])]

http://urwid.org/ - Console user interface library for Python

https://towardsdatascience.com/progress-bars-in-python-4b44e8a4c482

https://www.quora.com/How-can-I-delete-the-last-printed-line-in-Python-language

CURSOR_UP_ONE = '\x1b[1A'
ERASE_LINE = '\x1b[2K'

import sys
sys.stdout.write(CURSOR_UP_ONE)
sys.stdout.write(ERASE_LINE)


def delete_last_lines(n=1):
    for _ in range(n):
        sys.stdout.write(CURSOR_UP_ONE)
        sys.stdout.write(ERASE_LINE)

https://stackoverflow.com/questions/5419389/how-to-overwrite-the-previous-print-to-stdout-in-python * http://en.wikipedia.org/wiki/ANSI_escape_code * https://www.youtube.com/watch?v=WAitSilLDUA

print('\x1b[2K\r'),

pip3 install flake8 atom: * python-indent * busy-signal * intentions * linter * linter-flake8 * linter-ui-default * sort-lines

https://stackoverflow.com/questions/3002085/python-to-print-out-status-bar-and-percentage

import progressbar
from time import sleep

bar = progressbar.ProgressBar(maxval=20,
                              widgets=[progressbar.Bar('=', '[', ']'), ' ',
                                       progressbar.Percentage()],
                              term_width=80)

bar.start()

for i in range(20):
    bar.update(i + 1)
    sleep(0.1)

bar.finish()
from time import sleep
import sys

CURSOR_UP_ONE = '\x1b[1A'
ERASE_LINE = '\x1b[2K'

for i in range(41):
    sys.stdout.write('\r')
    print(f"testing: {i}", end="")
    if i in (5, 15, 33):
        sys.stdout.write('\r')
        print(f"found one!: {i}")
    sleep(0.15)
    sys.stdout.write('\r')
    sleep(0.15)
    sys.stdout.write(ERASE_LINE)

Pretty Print

#!/usr/bin/env python3

from termcolor import cprint
import time

output = ""
progress = ""


def beautify_print_try(value):
    global output
    print("\033c")
    cprint(output, 'green', attrs=['bold'])
    cprint('[*] Try: ' + value, 'red', attrs=['bold'])


def beautify_print():
    global output
    print("\033c")
    cprint(output, 'green', attrs=['bold'])


def doit():
    global output
    for i in range(100):
        if i in (10, 40, 80):
            output = output + "\n" + str(i)
        beautify_print_try(str(i))
        time.sleep(0.01)


doit()
beautify_print()
doit()
beautify_print()

ipython - History:

%history [-n] [-o] [-p] [-t] [-f FILENAME] [-g [PATTERN [PATTERN ...]]]
             [-l [LIMIT]] [-u]
             [range [range ...]]

Print input history (_i<n> variables), with most recent last.

By default, input history is printed without line numbers so it can be
directly pasted into an editor. Use -n to show them.

By default, all input history from the current session is displayed.
Ranges of history can be indicated using the syntax:

``4``
  Line 4, current session
``4-6``
  Lines 4-6, current session
``243/1-5``
  Lines 1-5, session 243
``~2/7``
  Line 7, session 2 before current
``~8/1-~6/5``
  From the first line of 8 sessions ago, to the fifth line of 6
  sessions ago.

Multiple ranges can be entered, separated by spaces

The same syntax is used by %macro, %save, %edit, %rerun

Examples
--------
::

In [6]: %history -n 4-6
4:a = 12
5:print a**2
6:%history -n 4-6

python -m pyftpdlib -p 21 -w -d pwd -i 10.10.14.25