SPLIT PYTHON LIST BY NTH ITEM
This has been one of the processes I’ve normally not solved in a clean or readable way.
In []: a = [1,2,3,4,5,6,7,8]
Taking the top output I want to return something similar to below.
Out[]: [(1, 2), (3, 4), (5, 6), (7, 8)]
Using a Pythonic approach this task isn’t to difficult, but there is some explaining to do.
First lets look at the code:
In []: a = iter([1,2,3,4,5,6,7,8])
In []: [ i for i in zip(a, a) ]
Out[]: [(1, 2), (3, 4), (5, 6), (7, 8)]
First we need to pass our list to the iter function, this turns our list into a iterator object:
LINUX /PROC/NET/ROUTE ADDRESSES UNREADABLE
So you may have looked at /proc/net/route before and thought how the heck am I suppose to read this. Well here is the low down.
This file uses endianness to store the addresses as hexadecimal, in reverse; for example 192 as hex is C0 :
In []: hex(192)
Out[]: '0xc0'
So lets take a look at our route file:
Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
eth0 00087F0A 00000000 0001 0 0 0 00FFFFFF 0 0 0
eth0 0000FEA9 00000000 0001 0 0 1002 0000FFFF 0 0 0
eth0 00000000 01087F0A 0003 0 0 0 00000000 0 0 0
Now the first entry has a destination of 00087F0A , lets go ahead and chunk these in to hex characters:
VEGAN SPAGHETTI WITH MEAT SAUCE
A dish we normally eat once a week is spaghetti and meat sauce; however, since we have been eating vegan we needed to substitute the meat sauce, as well as egg pasta.
The pasta wasn’t difficult at all, our local grocery store carried a brand right next to all the other pasta noodles.
As for the meat sauce, we decided to try Seitan (a wheat product).
The ground Seitan cooked up quite nicely, and even resembled the ground beef we were accustomed to.
STILL HAVE PIZZA AND BEER FOR DINNER!
Day 3 of eating vegan and we still have Pizza and Beer, eating vegan isn’t as hard as you may think!
VEGAN WAFFLE BREAKFAST
We are now on the third day of our vegan lifestyle change, and this morning we decided to give vegan waffles a try.
We’ve always made our waffles from scratch, but up till this point have used cows milk and eggs. This morning we tweaked that with almond milk to replace the cows milk, and apple sauce to replace the egg.
After a few minutes in the waffle iron our breakfast was ready, and you couldn’t tell the difference between the vegan and the non-vegan waffles by look.
1984 BY GEORGE ORWELL
I did not get a chance to read George Orwell’s 1984 while in high school, but have heard many times how great a book it is.It has always been on my list of to read books, but never got around to doing so until recently.
At time of this writing I’ve finished, and I have to say the book was fantastic!
I would highly recommend this book to anyone who enjoys reading fiction or dystopian style novels. It isn’t to long of a read, around 300 pages.
READING YUM REPOSITORY DATA
I’ve spent a lot of time working with RPM in the last couple years, and have had the pleasure of maintaining the IUS Community .
I wanted to share a small utility we use quite often called repodataParser , repodataParser is a Python class for working with RPM repositories, and used in a few of our Django applications .
The idea is all RPM repositories contain a XML file containing details about the package it contains. Lets take CentOS’s Vault for example:
RANDOM BOARD GAME SELECTION USING BOARD GAME GEEK
Using Board Game Geek’s API I wanted to create a simple Python tool for randomly picking a game to play. Below is some quick Python code to achieve my goal:
from urllib2 import urlopen
from lxml import etree
from random import choice
def get_xml():
req = urlopen('http://www.boardgamegeek.com/xmlapi/collection/flip387')
return req
def get_items():
xml = etree.parse(get_xml())
return xml.xpath('//item')
def get_thumbnail(item):
t = item.xpath('thumbnail')
if len(t) == 1:
return t[0].text
def get_name(item):
t = item.xpath('name')
if len(t) == 1:
return t[0].text
def get_stats(item):
t = item.xpath('stats')
if len(t) == 1:
return dict(t[0].items())
def get_minplayers(item):
return get_stats(item).get('minplayers')
def get_maxplayers(item):
return get_stats(item).get('maxplayers')
def get_playingtime(item):
return get_stats(item).get('playingtime')
def get_as_dict(item):
return dict(
name=get_name(item),
thumbnail=get_thumbnail(item),
minplayers=get_minplayers(item),
maxplayers=get_maxplayers(item),
playingtime=get_playingtime(item)
)
def get_games():
items = get_items()
return [get_as_dict(i) for i in items]
def get_random_game():
games = get_games()
return choice(games)
And using this would work like so:
USING GPG TO SIGN AND VERIFY A MESSAGE
First off GPG stands for GNU Privacy Guard , and is a open source implementation of PGP.
So what can you do with GPG , and why should we care?
One common use of is to sign your messages, this way the receiver can verify it did in fact come from you, and that it hasn’t been altered.
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: SKS 1.1.4
Com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public key we can verify a signed message I wrote.