Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #50904

Share Code Tips

Date 2013-07-19 09:51 -0400
From Devyn Collier Johnson <devyncjohnson@gmail.com>
Subject Share Code Tips
Newsgroups comp.lang.python
Message-ID <mailman.4868.1374241904.3114.python-list@python.org> (permalink)

Show all headers | View raw


Aloha Python Users!

I have some coding tips and interesting functions that I want to share 
with all of you. I want to give other programmers ideas and inspiration. 
It is all Python3; most of it should work in Python2. I am a Unix/Linux 
person, so some of these will only work on Unix systems. Sorry Microsuck 
users :-D ;-)

All of the below Python3 code came from Neobot v0.8dev. I host an 
artificial intelligence program on Launchpad (LP Username: 
devyncjohnson-d). I have not released my Python version yet. The current 
version of Neobot (v0.7a) is written in BASH and Python3.

To emulate the Linux shell's date command, use this Python

function def DATE(): print(time.strftime("%a %B %d %H:%M:%S %Z %Y"))

Want an easy way to clear the terminal screen? Then try this:

def clr(): os.system(['clear','cls'][os.name == 'nt'])

Here are two Linux-only functions:

def GETRAM(): print(linecache.getline('/proc/meminfo', 
1).replace('MemTotal:', '').strip()) #Get Total RAM in kilobytes#
def KDE_VERSION(): print(subprocess.getoutput('kded4 --version | awk -F: 
\'NR == 2 {print $2}\'').strip()) ##Get KDE version##

Need a case-insensitive if-statement? Check this out:

if 'YOUR_STRING'.lower() in SOMEVAR.lower():

Have a Python XML browser and want to add awesome tags? This code would 
see if the code to be parsed contains chess tags. If so, then they are 
replaced with chess symbols. I know, many people hate trolls, but trolls 
are my best friends. Try this:

if '<chess_'.lower() in PTRNPRS.lower(): DATA = 
re.sub('<chess_white_king/>', '♔', PTRNPRS, flags=re.I); DATA = 
re.sub('<chess_white_queen/>', '♕', DATA, flags=re.I); DATA = 
re.sub('<chess_white_castle/>', '♖', DATA, flags=re.I); DATA = 
re.sub('<chess_white_bishop/>', '♗', DATA, flags=re.I); DATA = 
re.sub('<chess_white_knight/>', '♘', DATA, flags=re.I); DATA = 
re.sub('<chess_white_pawn/>', '♙', DATA, flags=re.I); DATA = 
re.sub('<chess_black_king/>', '♚', DATA, flags=re.I); DATA = 
re.sub('<chess_black_queen/>', '♛', DATA, flags=re.I); DATA = 
re.sub('<chess_black_castle/>', '♜', DATA, flags=re.I); DATA = 
re.sub('<chess_black_bishop/>', '♝', DATA, flags=re.I); DATA = 
re.sub('<chess_black_knight/>', '♞', DATA, flags=re.I); PTRNPRS = 
re.sub('<chess_black_pawn/>', '♟', DATA, flags=re.I)

For those of you making scripts to be run in a terminal, try this for a 
fancy terminal prompt:

INPUTTEMP = input('User ≻≻≻')


I may share more code later. Tell me what you think of my coding style 
and tips.


Mahalo,

Devyn Collier Johnson
DevynCJohnson@Gmail.com

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Share Code Tips Devyn Collier Johnson <devyncjohnson@gmail.com> - 2013-07-19 09:51 -0400
  Re: Share Code Tips Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-19 17:59 +0000
    Re: Share Code Tips Devyn Collier Johnson <devyncjohnson@gmail.com> - 2013-07-19 18:08 -0400
      Re: Share Code Tips Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-20 03:18 +0000
        Re: Share Code Tips Devyn Collier Johnson <devyncjohnson@gmail.com> - 2013-07-20 06:07 -0400
    Re: Share Code Tips Chris Angelico <rosuav@gmail.com> - 2013-07-20 09:08 +1000
    Re: Share Code Tips Dave Angel <davea@davea.name> - 2013-07-19 19:09 -0400
    Re: Share Code Tips Devyn Collier Johnson <devyncjohnson@gmail.com> - 2013-07-19 21:04 -0400
      Re: Share Code Tips Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-20 03:44 +0000
        Re: Share Code Tips David Hutto <dwightdhutto@gmail.com> - 2013-07-20 00:15 -0400
        Re: Share Code Tips David Hutto <dwightdhutto@gmail.com> - 2013-07-20 00:22 -0400
        Re: Share Code Tips David Hutto <dwightdhutto@gmail.com> - 2013-07-20 00:26 -0400
        Re: Share Code Tips David Hutto <dwightdhutto@gmail.com> - 2013-07-20 00:27 -0400
        Re: Share Code Tips Devyn Collier Johnson <devyncjohnson@gmail.com> - 2013-07-20 06:10 -0400
        Re: Share Code Tips Devyn Collier Johnson <devyncjohnson@gmail.com> - 2013-07-20 08:36 -0400
    Re: Share Code Tips Chris Angelico <rosuav@gmail.com> - 2013-07-20 11:13 +1000
    Re: Share Code Tips Dave Angel <davea@davea.name> - 2013-07-19 21:51 -0400
    Re: Share Code Tips David Hutto <dwightdhutto@gmail.com> - 2013-07-19 23:42 -0400
    Re: Share Code Tips Devyn Collier Johnson <devyncjohnson@gmail.com> - 2013-07-20 06:06 -0400
    Re: Share Code Tips Devyn Collier Johnson <devyncjohnson@gmail.com> - 2013-07-20 08:20 -0400

csiph-web