4259 links
  • Arnaud's links
  • Home
  • RSS Feed
  • Tag cloud
  • Login
search: tag:nomarkdown tag:python — newest first
  • Auto activate and deactivate python venv using zsh - DEV Community

    python_venv() {
    MYVENV=./venv
    # when you cd into a folder that contains $MYVENV
    [[ -d $MYVENV ]] && source $MYVENV/bin/activate > /dev/null 2>&1
    # when you cd into a folder that doesn't
    [[ ! -d $MYVENV ]] && deactivate > /dev/null 2>&1
    }
    autoload -U add-zsh-hook
    add-zsh-hook chpwd python_venv

    May 20, 2022 - permalink - https://dev.to/moniquelive/auto-activate-and-deactivate-python-venv-using-zsh-4dlm
    nomarkdown python venv zsh
  • python - How to search and replace text in a file? - Stack Overflow

    Sometime you cant use sed :(

    Read in the file

    with open('file.txt', 'r') as file :
    filedata = file.read()

    Replace the target string

    filedata = filedata.replace('ram', 'abcd')

    Write the file out again

    with open('file.txt', 'w') as file:
    file.write(filedata)

    July 7, 2021 - permalink - https://stackoverflow.com/questions/17140886/how-to-search-and-replace-text-in-a-file
    nomarkdown python replace
  • Note: some draft about monitoring beanstalk applications health with boto3 python script

    import pprint
    p = pprint.PrettyPrinter(indent=4)
    p.pprint(x)

    or

    import pprint
    pprint.pformat(x)

    import logging
    import pprint
    logging.info(pprint.pformat(dict))

    $ cat monitor_beanstalk.py

    !/bin/python

    import boto3
    import pprint
    pp = pprint.PrettyPrinter(indent=4)

    List all env and status and instances health

    client = boto3.client('elasticbeanstalk')

    envs = client.describe_environments()['Environments']

    pp.pprint(envs)

    for env in envs:
    print 'ApplicationName: {} EnvironmentName: {} Health: {} HealthStatus: {} Status: {}'.format(env['ApplicationName'].ljust(30),env['EnvironmentName'].ljust(30),env['Health'].ljust(10),env.get('HealthStatus', 'N/A').ljust(10),env['Status'].ljust(10))
    if (env['Health'] != 'Green') or (env.get('HealthStatus', 'N/A') != 'Ok' and env.get('HealthStatus', 'N/A') != 'N/A'):
    print '\nProblem'
    details = client.describe_environment_health(EnvironmentName=env['EnvironmentName'],AttributeNames=['All'])
    #pp.pprint(details)
    print details['Causes']
    print details['InstancesHealth']
    print '\n'

    June 15, 2017 - permalink - https://links.infomee.fr/shaare/eFFzMg
    aws beanstalk boto nomarkdown pprint python
  • Stupid Python Tricks: The KeyboardInterrupt Exception

    catching all exception in python
    import traceback

    for record in database:
    try:
    blabla
    except (KeyboardInterrupt, SystemExit):
    raise
    except Exception as e:
    # report error and proceed
    print(type(e).name)
    print(e)
    print(traceback.format_exc())

    Ne pas oublier de raise si on veut re-raise

    April 21, 2017 - permalink - http://effbot.org/zone/stupid-exceptions-keyboardinterrupt.htm
    exception nomarkdown python