Skip to content

Instantly share code, notes, and snippets.

@yydai
Last active May 26, 2019
Embed
What would you like to do?

Python Code snippets

1. File

1. Reading specific lines only (Python)

https://stackoverflow.com/questions/2081836/reading-specific-lines-only-python

with open("file") as fp:
    for i, line in enumerate(fp):
        if i == 25:
            # 26th line
        elif i == 29:
            # 30th line
        elif i > 29:
            break

2. Decorators

1. logs

import logger
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

def log(func):
    def wrapper(*args, **kwargs):
        start_time = time.time()
        logging.debug('start {}'.format(func.__name__))
        ret = func(*args, **kwargs)
        end_time = time.time()
        logging.debug('end {}, cost {} seconds'.format(
            func.__name__, end_time - start_time))
        return ret
    return wrapper

@log
def test():
    print ("test")

3. Dict

Sorted

import operator
a = {"a": 10, "b": 1, "c": 3, "d": 4}
b = sorted(a.iteritems(), key=operator.itemgetter(1), reverse=True)

4. Set logger

From: https://docs.python-guide.org/writing/logging/ First create a config file: logging_config.ini

[loggers]
keys=root

[handlers]
keys=stream_handler

[formatters]
keys=formatter

[logger_root]
level=DEBUG
handlers=stream_handler

[handler_stream_handler]
class=StreamHandler
level=DEBUG
formatter=formatter
args=(sys.stderr,)

[formatter_formatter]
format=%(asctime)s %(name)-12s %(levelname)-8s %(message)s

In python file, called it like this:

import logging
from logging.config import fileConfig

fileConfig('logging_config.ini')
logger = logging.getLogger()
logger.debug('often makes a very good meal of %s', 'visiting tourists')

Above is in the main file, if in other modules just import logging is enough:

import logging
logger = logging.getLogger()

with

example code:

   @contextmanager
   def get_ftp_client(self):
       ftp = FTP()
       ftp.connect(self.config['host'], self.config['port'])
       ftp.login(self.config['username'], self.config['password'])
       yield ftp
       ftp.close()

   def _is_dir(self, full_file_path):
       with self.get_ftp_client() as ftp:
           try:
               ftp.cwd(full_file_path)
               result = True
           except ftplib.error_perm:
               result = False
       return result

this usage can auto close the resource that used. Look at https://docs.python.org/3/library/contextlib.html for more about this.

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