Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #50904
| Path | csiph.com!usenet.pasdenom.info!gegeweb.org!newsfeed0.kamp.net!newsfeed.kamp.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <devyncjohnson@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.005 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'scripts': 0.03; ';-)': 0.03; 'yet.': 0.04; "'',": 0.07; 'python3': 0.07; 'bash': 0.09; 'collier': 0.09; 'emulate': 0.09; 'launchpad': 0.09; 'parsed': 0.09; 'python': 0.11; 'def': 0.12; 'systems.': 0.12; ':-d': 0.16; 'command,': 0.16; 'fancy': 0.16; 'python3.': 0.16; 'tags.': 0.16; 'skip:p 40': 0.19; 'written': 0.21; 'coding': 0.22; 'header:User- Agent:1': 0.23; 'subject:Code': 0.24; 'unix/linux': 0.24; 'this:': 0.26; 'function': 0.29; 'host': 0.29; 'skip:p 30': 0.29; 'unix': 0.29; 'xml': 0.29; 'code': 0.31; 'received:10.0.0': 0.31; 'run': 0.32; 'linux': 0.33; 'programmers': 0.33; 'style': 0.33; 'johnson': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'version': 0.36; 'data,': 0.36; 'ram': 0.36; 'received:10.0': 0.36; 'should': 0.36; 'so,': 0.37; 'two': 0.37; 'received:10': 0.37; 'clear': 0.37; 'awesome': 0.38; 'to:addr :python-list': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'users': 0.40; 'easy': 0.60; 'most': 0.60; 'tell': 0.60; 'skip:o 30': 0.61; 'tips': 0.61; 'browser': 0.61; 'you.': 0.62; 'making': 0.63; 'email addr:gmail.com': 0.63; 'more': 0.64; 'total': 0.65; 'skip:1 20': 0.65; 'here': 0.66; 'person,': 0.68; 'skip:$ 10': 0.81; 'functions:': 0.84; 'hate': 0.91 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject :content-type:content-transfer-encoding; bh=4PsAAPFT91JKv32KvLN+Ohbp17MPM9t1OYllAWK6eD0=; b=B5z0iYSk/bz4JR/RiOUQXipKCrikVH1VHLfj5Nr4ak+tzcmZWgBEceNRcGJHn7tnB1 LnhMZ1chFN9GMvgiicx5MkT8wCp+HqWpViixwA2LABfLZm0gs74KcZil7jqdvagShnXf 0oV+RtpJNVYFlcApv8vlM8NWNpBzIE8Rmevtx0s6e4K3OTo44cw6cP0dneedP8KHS7Cf KMTVRrvbDQNE32cWWkxJ6FDX3kNepruwZeix9zGnUnYT9nFduGORfSFS96iPWWUiBdfL 3Ny0TQlFiFRPHHLCPlg9DZ5PU3JD9lMcCq69m722ihoqv+KJXQpk927Ch3qo7zM9TubS cvaQ== |
| X-Received | by 10.42.52.6 with SMTP id h6mr10534590icg.5.1374241895726; Fri, 19 Jul 2013 06:51:35 -0700 (PDT) |
| Date | Fri, 19 Jul 2013 09:51:23 -0400 |
| From | Devyn Collier Johnson <devyncjohnson@gmail.com> |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130623 Thunderbird/17.0.7 |
| MIME-Version | 1.0 |
| To | Python Mailing List <python-list@python.org> |
| Subject | Share Code Tips |
| Content-Type | text/plain; charset=UTF-8; format=flowed |
| Content-Transfer-Encoding | 8bit |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.4868.1374241904.3114.python-list@python.org> (permalink) |
| Lines | 65 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1374241904 news.xs4all.nl 15908 [2001:888:2000:d::a6]:39924 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:50904 |
Show key headers only | 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 | Next — Next in thread | Find similar | Unroll 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