Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #50906
| References | <51E9445B.30509@Gmail.com> |
|---|---|
| Date | 2013-07-19 10:21 -0400 |
| Subject | Re: Share Code Tips |
| From | Joel Goldstick <joel.goldstick@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.4870.1374243674.3114.python-list@python.org> (permalink) |
[Multipart message — attachments visible in raw view] - view raw
On Fri, Jul 19, 2013 at 9:51 AM, Devyn Collier Johnson <
devyncjohnson@gmail.com> wrote:
> 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 <http://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
> --
> http://mail.python.org/**mailman/listinfo/python-list<http://mail.python.org/mailman/listinfo/python-list>
>
I'm guessing you may be posting with html. So all your code runs together.
--
Joel Goldstick
http://joelgoldstick.com
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Re: Share Code Tips Joel Goldstick <joel.goldstick@gmail.com> - 2013-07-19 10:21 -0400
Re: Share Code Tips Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-19 16:19 +0000
Re: Share Code Tips Devyn Collier Johnson <devyncjohnson@gmail.com> - 2013-07-19 18:10 -0400
Re: Share Code Tips Chris Angelico <rosuav@gmail.com> - 2013-07-20 09:04 +1000
csiph-web