Posted by Derek@TheDailyLinux in
News »
Add Comment »
After many failed attempts at removing some bad code somewhere in one of my wordpress blogs, I decided to delete the entire wordpress installation and start with a fresh one. This time I’m using some security themed plugins and being much more selective on the themes that I include. Please bear with me as I try to get the site back to its original state.
We should be pretty much good to go now. I’m confident things will be smoother from here on out. By the way, if any of you have any suggestions for beefing up security on a wordpress blog, please let me know in the comments!
Best Regards,
Derek
Posted by Derek@TheDailyLinux in
News »
Add Comment »
There was a major hardware failure at the hosting facilities over Thanksgiving weekend which took down (and wiped clean) everything on this site. Luckily, the staff were able to salvage the hard drive from the old server and transfer everything to the new hardware. This does take some time to transfer. We’re hoping that by tomorrow, things will be fully transferred and back to normal. In the meantime, please forgive any oddities or speed issues as you browse. They should be cleared up soon. If you do find something that you need immediately, please feel free to use the comment form below.
Best Regards,
Derek
Posted by Derek@TheDailyLinux in
Uncategorized »
Add Comment »
Posted by Derek@TheDailyLinux in
Programming »
Add Comment »
The following sections give quick, heavily commented, and ready-to-run examples that will give you a head start in creating a unit test framework in Python (pyUnit). First, we’ll take a look at a standalone module with several simple examples of unit tests. Then, we’ll take a look at a more complicated version that uses modules and test suites.
Here’s the first example. As long as you are running Python version 2.1 or greater, you can copy/paste this code, save it to unittest.py, and run it using python unittest.py.
'''This is a simple program to demonstrate how to create a unittest in
Python. For more information and documentation, please see the official
documentation page here: http://docs.python.org/library/unittest.html'''
import unittest #Include the pyUnit unittest framework
def add(a,b,c=0):
'''A simple adding function to demo unittest'''
return a+b+c
# The following is the class in which all functions will be ran by unittest
class AddTest(unittest.TestCase):
''' Main class for add testing; Can be added to a suite'''
# The function "setUp" will always be ran in order to setup the
# test environment before all the tests have run.
def setUp(self):
'''Verify environment is setup properly''' # Printed if test fails
pass
# The function "tearDown" will always be ran in order to cleanup the
# test environment after all the tests have run.
def tearDown(self):
'''Verify environment is tore down properly''' # Printed if test fails
pass
# Functions beginning with "test" will be ran as a unit test.
def test_positive_add(self):
'''Verify that adding positive numbers works''' # Printed if test fails
self.assertEqual(add(10,23,22), 55) # Test will fail if "false" (boolean)
self.assertEqual(add(11,23), 34)
self.assertEqual(add(1,1,17), 19)
# Functions beginning with "test" will be ran as a unit test.
# @unittest.skip("demonstrating skipping") # Skip this test (Python >= 2.7)
def test_negative_add(self):
'''Verify that adding negative numbers works''' # Printed if test fails
self.assertEqual(add(-12,23), 11)
# Functions beginning with "test" will be ran as a unit test.
# In this case, this test will be skipped.
#@unittest.skip("demonstrating skipping")
def test_negative_add_skip(self):
'''Verify that adding negative numbers works''' # Printed if test fails
self.assertEqual(add(-12,23), 11)
if __name__=='__main__':
unittest.main()
For more complicated test suites and modularity, please take a look at the git repository I’ve setup on bitbucket.org. Download the source, read the README file, and you’ll be able to have a working example of a testsuite (run with python pyunit_intro/suite/test/unittest_run_all.py).
https://bitbucket.org/dhildreth/pyunit_intro
Source: Much of the code from above was done first in this great introductory presentation given by Chander K. Ganesan at PyCon 2010.
Posted by Derek@TheDailyLinux in
Tips and Tricks »
Add Comment »
Here’s a quickie that will help you find lines longer than 80 characters (or any number of characters) within VIM. Open vim, and copy/paste the following:
:/.{80,}
You can use the regex anywhere, actually, but this is a good one to store in the useful command list. Or, create an alias for it in your .vimrc file for quick access.
Posted by Derek@TheDailyLinux in
General »
Add Comment »
For many, Labor Day marks the end of summer and the beginning of fall and winter. Anybody else looking forward to this again?
Be safe. Have fun. Oh, and get outside while you still can!
Best Regards,
Derek
Posted by Derek@TheDailyLinux in
Tips and Tricks »
Add Comment »
Here’s how to setup an array and use it in a for loop. Open up the terminal and let’s get started…
Step 1: Create an array
This is easily done by surround space separated items in parenthesis. For example:
files=( dir1/file1.php dir2/file2.php dir1/subdir/file3.php )
Note: Be aware of any spaces within the array items. If you have this, you’ll need to surround it in quotes. See appendix at the end of this post.
Step 2: Figure out what you want to do to each of the items in the array
The next part is figuring out what you want to do to each of the items in the array. Keep in mind, these items can be variables, filenames, text, numbers, whatever. For example, I want to see how large each of these files are along with other information. For this, I would run ls -lh filename.php
Unix~ ls -lh dir1/file1.php
-rw-r--r-- 1 user group 2.7K May 4 16:47 dir1/file1.php
Step 3: Iterate through the list of array items
Finally, let’s perform the action from step 2 on all of the items in our array. For this, the key is to use ${array[@]}, which says run through all (@ll) of the items in the array called “array”. Going along with the contrived, simple example set in the previous steps, the final command is:
for file in ${files[@]}; do ls -lh $file; done
The output from the above command is:
Unix~ for file in ${files[@]}; do ls -lh $file; done
-rw-r--r-- 1 user group 2.7K May 4 16:47 dir1/file1.php
-rw-rw-r-- 1 user group 2.8K Aug 25 15:23 dir2/file2.php
-rw-r--r-- 1 user group 2.4K Mar 7 19:57 dir1/subdir/file3.php
APPENDIX: Working with spaces in array items
If you have spaces within your array items, be sure to surround them with quotes like such:
test=( one "two 2" "three 3 tree" )
On top of that, you’ll need to surround ${test[@]} in quotes like such:
for i in "${test[@]}"; do echo $i; done
Here’s what you’ll get:
Unix~ for i in "${test[@]}"; do echo "==> $i"; done
==> one
==> two 2
==> three 3 tree
If you don’t use quotes, you’ll get this instead (BAD!):
for i in ${test[@]}; do echo "==> $i"; done
==> one
==> two
==> 2
==> three
==> 3
==> tree
Posted by Derek@TheDailyLinux in
Tips and Tricks »
1 Comment »
If you have an extra folder or file within a tarball (.tar) file, you can remove it without extracting the entire tarball first. This can be really handy when you have a massive file that you don’t want to spend a lot of time extracting and re-archiving. Open up a terminal shell and follow along with the example below.
We’ll first see what’s in the compressed tarball named sandbox.tar.gz by using the --list option of tar:
thelinuxdaily$ tar --list --file=sandbox.tar.gz
sandbox/
sandbox/delete_me/
sandbox/delete_me/hello.txt
sandbox/hello.txt
sandbox/hello2.txt
sandbox/hello3.txt
sandbox/save_me/
sandbox/save_me/hello.txt
Let’s try to delete the folder called sandbox/delete_me from the compressed tarball:
thelinuxdaily$ tar --delete --file=sandbox.tar.gz sandbox/delete_me
tar: Cannot update compressed archives
tar: Error is not recoverable: exiting now
See what happened? That means we need to uncompress it. In this case, it’s a .gz compression type, so we’ll use gunzip (if you were using bz2, you’d use bunzip2:
thelinuxdaily$ gunzip sandbox.tar.gz
Let’s try to delete the folder sandbox/delete_me again:
thelinuxdaily$ tar --delete --file=sandbox.tar sandbox/delete_me
We didn’t get an error message, so that’s good. Let’s see if it’s gone by using --list again:
thelinuxdaily$ tar --list --file=sandbox.tar
sandbox/
sandbox/hello.txt
sandbox/hello2.txt
sandbox/hello3.txt
sandbox/save_me/
sandbox/save_me/hello.txt
thelinuxdaily$ gzip sandbox.tar
Excellent! That’s what we needed to see. If you have any other tips, feel free to use the comments below.
Posted by Derek@TheDailyLinux in
News »
Add Comment »
It all started with an email to a mailing list about an operating system that wasn’t going to be “big and professional like gnu”. So, happy birthday to Linux as of 25 Aug 91 20:57:08 GMT. Thanks for the good times so far!
Be sure to check out the special 20th anniversary page on The Linux Foundation’s page here:
http://www.linuxfoundation.org/20th/
Posted by Derek@TheDailyLinux in
Tutorials and Guides »
Add Comment »
Most of the guides I came across were just wildly all over the place, so I figured a short and simple guide that gets right to the point would be useful. Here’s how to set up Fedora 15 to build (compile might be another word) a .tex file into a .pdf file from the command line. It can be applied to other Linux distributions as well. There are GUIs to help with this, but it’s best to start with the basics. So, fire up a terminal and let’s get started.
Step 1: Install the TexLive Package
The first thing we need to do is install the texlive package.
su -c 'yum install -y texlive'
Step 2: Prepare a .tex Document
Next, we’ll create a “hello world” type .tex file. Fire up your favorite text editor and copy/paste the following (I enjoy using vim since I can stay in the command line while building the LaTeX file). When you’re finished, save the file (I’ve saved it as “hello.tex”).
documentclass{book}
usepackage{lipsum}
begin{document}
chapter{Sample}
lipsum[1-4]
end{document}
Step 3: Build the .tex Document Into a PDF File
Finally, we’ll build (compile) the .tex source into a pretty PDF document using the command below:
pdflatex hello.tex
You’ll see something similar to this:
[dhildreth@drh hello_world]$ pdflatex hello.tex
This is pdfTeXk, Version 3.141592-1.40.3 (Web2C 7.5.6)
%&-line parsing enabled.
entering extended mode
(./hello.tex
LaTeX2e <2005/12/01>
Babel <v3.8h> and hyphenation patterns for english, usenglishmax, dumylang, noh
yphenation, arabic, basque, bulgarian, coptic, welsh, czech, slovak, german, ng
erman, danish, esperanto, spanish, catalan, galician, estonian, farsi, finnish,
french, greek, monogreek, ancientgreek, croatian, hungarian, interlingua, ibyc
us, indonesian, icelandic, italian, latin, mongolian, dutch, norsk, polish, por
tuguese, pinyin, romanian, russian, slovenian, uppersorbian, serbian, swedish,
turkish, ukenglish, ukrainian, loaded.
(/usr/share/texmf/tex/latex/base/book.cls
Document Class: book 2005/09/16 v1.4f Standard LaTeX document class
(/usr/share/texmf/tex/latex/base/bk10.clo))
(/usr/share/texmf/tex/latex/lipsum/lipsum.sty)
No file hello.aux.
Chapter 1.
[1{/usr/share/texmf/fonts/map/pdftex/updmap/pdftex.map}] [2] (./hello.aux) )</u
sr/share/texmf/fonts/type1/bluesky/cm/cmbx12.pfb></usr/share/texmf/fonts/type1/
bluesky/cm/cmr10.pfb></usr/share/texmf/fonts/type1/bluesky/cm/cmsl10.pfb>
Output written on hello.pdf (2 pages, 22507 bytes).
Transcript written on hello.log.
[dhildreth@drh hello_world]$
Notice the text “Output written on hello.pdf (2 pages, 22507 bytes).”. That means it worked.
Step 4: View the New PDF File
Now, all you need to do is view the generated PDF document. Run the following command:
evince hello.pdf
Note, if you’re like me, you constantly flip between the source and the pdf. If that’s the case, evince will automatically update the pdf every time you re-build using pdflatex. Simply add “&” on the end of the command above:
evince hello.pdf &
This is what you should be seeing when completed:
Alternative: Rubber
I’d like to make a special note about the ‘rubber’ utility. It can take a lot of the guess work out of building LaTeX files and I strongly recommend using it over pdftex or pdflatex as mentioned above. You can install and run the ‘rubber’ utility with the following commands:
su -c 'yum install -y rubber'
rubber --pdf hello.tex
Conclusions
I hope this helps you get started moving quickly in the right direction. Comments and feedback are welcome.
A few difficulties I had when doing this for the first time were:
- “Where’s the ‘latex’ package??”
- Turns out, texlive is the package to install these days.
- Undefined control sequence message:
[dhildreth@drh hello_world]$ pdftex small2e.tex
This is pdfTeXk, Version 3.141592-1.40.3 (Web2C 7.5.6)
%&-line parsing enabled.
entering extended mode
(./small2e.tex
! Undefined control sequence.
l.11 documentclass
{article} % Your input file must contain these two...
?
- Apparently, there’s a difference between pdflatex and pdftex. Understand this when moving forward. I would recommend using the command
rubber --pdf hello.tex instead since it takes care of the decision for you (as well as offers many other benefits — man rubber for more info).