Skip to content

Instantly share code, notes, and snippets.

@yogggoy
Last active August 15, 2023 03:17
Show Gist options
  • Save yogggoy/ecd30bb78f80571756dd5872a927b273 to your computer and use it in GitHub Desktop.
Save yogggoy/ecd30bb78f80571756dd5872a927b273 to your computer and use it in GitHub Desktop.
Python hacks
little tricks on python::::::
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# https://stackoverflow.com/a/4990739/8747302
""" catch them all! """
import time, sys
''' catch all ecxeption info in sys.exc_info() '''
try:
time.sleep(5) # ^+C
raise Exception('some except')
except:
print "Unexpected error:", sys.exc_info()[0]
raise
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# https://stackoverflow.com/questions/3001138/python-decorators-and-inheritance
# https://habr.com/post/141501/
""" decorator vith args """
class Printer(object):
def write(self, s):
print s
def close(self):
print "closed"
class bar(object):
def __init__(self):
self.shape = 'box'
self.p = Printer()
def set_shape(self, shape):
self.val = str(shape)
@staticmethod
def decor(func):
def increment(self, *args, **kwargs):
self.p.write("start")
a = func(self, *args, **kwargs)
self.p.close()
return a
return increment
class foo(bar):
# def __init__(self):
# bar.__init__(self)
@bar.decor
def add(self, shape, color):
self.shape = shape
print color, self.shape
a = foo()
a.add('circle', color='Yellow')
#!/usr/bin/python
# -*- coding: utf-8 -*-
# https://stackoverflow.com/questions/14693701/how-can-i-remove-the-ansi-escape-sequences-from-a-string-in-python
import re
ansi_escape = re.compile(r'\x1B\[[0-?]*[ -/]*[@-~]')
ansi_escape.sub('', sometext)
sometext = 'ls\r\n\x1b[00m\x1b[01;31mexamplefile.zip\x1b[00m\r\n\x1b[01;31m'
ansi_escape.sub('', sometext)
# 'ls\r\nexamplefile.zip\r\n'
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# https://stackoverflow.com/questions/3941517/converting-list-to-args-when-calling-function
# https://docs.python.org/2/reference/expressions.html#calls
def a(*args):
for i in args:
print i
x = (1, 2, 3, 4)
y = ()
a(x)
# >> (1, 2, 3, 4)
a(y)
# >> ()
a(*x) # Symbhol *
# >> 1
#... 2
#... 3
#... 4
a(*y)
# >> nothing return

convert md to html with python

sudo pip install markdown --proxy="192.168.172.129:3128"
sudo pip install Pygments --proxy="192.168.172.129:3128"
pygmentize -S default -f html -a .codehilite > style.css
python -m markdown -x codehilite -x extra sbc_manual.md | ./add_style.py; firefox out.html

https://gist.github.com/andyferra/2554919 http://sebastianraschka.com/Articles/2014_markdown_syntax_color.html https://biscuitsandtea.herokuapp.com/posts/markdown https://python-markdown.github.io/ https://python-markdown.github.io/cli/ https://python-markdown.github.io/extensions/code_hilite/

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# https://stackoverflow.com/questions/323972/is-there-any-way-to-kill-a-thread-in-python
""" Stop Event on thread """
import threading
class StoppableThread(threading.Thread):
"""Thread class with a stop() method. The thread itself has to check
regularly for the stopped() condition."""
def __init__(self):
super(StoppableThread, self).__init__()
self._stop_event = threading.Event()
def stop(self):
self._stop_event.set()
def stopped(self):
return self._stop_event.is_set()
#!/usr/bin/python
# -*- coding: utf-8 -*-
# https://stackoverflow.com/questions/1557571/how-do-i-get-time-of-a-python-programs-execution
""" How long exec """
import time
def timer(f):
start_time = time.time()
f()
print("--- %s sec." % (time.time() - start_time))
@yogggoy
Copy link
Author

yogggoy commented Aug 15, 2023

замер времени выполнения

from timeit import timeit
print(round(timeit("s = '; '.join(str(x) for x in range(10 ** 7))", number=10), 3))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment