def print_format_table():
"""
prints table of formatted text format options
"""
for style in range(8):
for fg in range(30,38):
s1 = ''
for bg in range(40,48):
format = ';'.join([str(style), str(fg), str(bg)])
s1 += '\x1b[%sm %s \x1b[0m' % (format, format)
print(s1)
print('\n')
print_format_table()
python3 log to file AND stdout
import logging
logging.basicConfig(handlers=[logging.FileHandler('/var/log/runner/process1.log'),logging.StreamHandler()],format='%(asctime)s %(levelname)s %(message)s',level=logging.INFO)
logging.info('foo')
Encore mieux pour supporter le logrotate sans copytruncate :
import logging.handlers
logging.basicConfig(handlers=[logging.handlers.WatchedFileHandler('/var/log/worker/worker1.log'),logging.StreamHandler()],format='%(asctime)s %(levelname)s %(message)s',level=logging.INFO)
/var/log/worker/*.log {
monthly
rotate 12
compress
delaycompress
missingok
notifempty
create 644 root root
}
Python 2:
import logging as loggingg
logging = loggingg.getLogger('simple_example')
logging.setLevel(loggingg.INFO)
formatter = loggingg.Formatter('%(asctime)s %(levelname)s %(message)s')
console_handler = loggingg.StreamHandler()
console_handler.setLevel(loggingg.INFO)
console_handler.setFormatter(formatter)
file_handler = loggingg.FileHandler('/var/log/worker/worker3.log')
file_handler.setLevel(loggingg.INFO)
file_handler.setFormatter(formatter)
logging.addHandler(console_handler)
logging.addHandler(file_handler)
for root, dirs, files in os.walk("folder"):
for file in files:
if file.endswith(".yml"):
print(os.path.join(root, file))
with open(os.path.join(root, file), "r") as sources:
lines = sources.readlines()
with open(os.path.join(root, file), "w") as sources:
for line in lines:
sources.write(re.sub(r'pattern', 'foo', line))
Add /json to pypi package url.. Magic!
subnets = ec2.subnets.all()
subnets_sorted = sorted(subnets, key=lambda k: k.tags[next(index for (index, d) in enumerate(k.tags) if d["Key"] == "Name")]['Value'])
Well, my python level is not good enough to clearly understand this but my Google level was largely enough to build this
Running Bottle with a different server
As said above, the standard server is perfectly suitable for development, personal use or a small group of people only using your application based on Bottle. For larger tasks, the standard server may become a bottleneck, as it is single-threaded, thus it can only serve one request at a time.
But Bottle has already various adapters to multi-threaded servers on board, which perform better on higher load. Bottle supports Cherrypy, Fapws3, Flup and Paste.
If you want to run for example Bottle with the Paste server, use the following code:
from bottle import PasteServer
...
run(server=PasteServer)
This works exactly the same way with FlupServer, CherryPyServer and FapwsServer.
import logging
logging.basicConfig(filename='log.txt', format=logging.BASIC_FORMAT)
logging.error('OH NO!')
try:
raise Exception('Foo')
except:
logging.exception("Oops:")
Un cours en français qui a l'air très bien. Via sametmax sur twitter
useful script to migrate redis server without downtime
Pratique pour avoir des stat rapidement dans ses scripts python (si on n'a pas de stack graphite sous la main)
How to pretty print a json string :
cat pref.json | python -m json.tool
for uuoc nazi ;) :
< pref.json python -m json.tool
or
python -m json.tool < pref.json
Pour le fun et le learning : faire du tcp avec python / scapy
via sametmax
mini web framework en python, dans le meme genre que bottle :
http://bottlepy.org/docs/dev/index.html