Skip to content

Instantly share code, notes, and snippets.

View thigm85's full-sized avatar

Thiago G. Martins thigm85

View GitHub Profile
@thigm85
thigm85 / mininal-maven-pom.xml
Created February 17, 2016 21:50 — forked from torgeir/minimal-maven-pom.xml
A minimal maven pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>gd.wa</groupId>
<artifactId>minimal-pom</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
@thigm85
thigm85 / python_date_time.py
Last active March 14, 2016 22:23
Sample code showing how to work with date and time in python
import time
current_time = time.time() # current time in seconds since the Epoch
print current_time
date = "201602251430"
time_tuple = time.strptime(date, "%Y%m%d%H%M") # parse date string using a specific format
print time_tuple
date_in_seconds = time.mktime(time_tuple) # convert time tuple to seconds from Epoch
@thigm85
thigm85 / python_unittest.py
Created March 2, 2016 14:42
Code sample for writing python unit tests with the unittest module.
import unittest
# Subclass TestCase class
class TestUM(unittest.TestCase):
# Execute code prior to unit tests being run
def setUp(self):
pass
# create unit tests using assert methods
<?xml version="1.0"?>
<data>
<country name="Liechtenstein">
<rank>1</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
<country name="Singapore">
import xml.etree.ElementTree as ET
# parse XML from a file
tree = ET.parse(file_path) # parse the file and obtain ElementTree obj
root = tree.getroot() # get the root Element obj
# parse XML in a string format
root = ET.fromstring(country_data_as_string)
root = ET.XML(country_data_as_string) # equivalent to above
@thigm85
thigm85 / git_stash_example.sh
Created March 4, 2016 14:27
git stash examples.
# list previous stashes
git stash list
# drop most recent stash
git stash drop
# apply most recent stash
git stash apply
# create a list
squares = [1, 4, 9, 16, 25]
# indexing
squares[0] # it is zero-based index
squares[-1]
# slicing
squares[0:2] # [inclusive, exclusive], first and second element
squares[-2:] # last two elements
# Syntax:
# '.': Selects the current node. This is mostly useful at the
# beginning of the path, to indicate that it’s a relative path.
# Example: Find all elements named 'property' that are children
# of the current node.
root.findall("./property")
# Syntax:
# '[tag='text']': Selects all elements that have a child
import sys
try:
f = open('myfile.txt')
s = f.readline()
i = int(s.strip())
except IOError as e:
print "I/O error({0}): {1}".format(e.errno, e.strerror)
except ValueError:
print "Could not convert data to an integer."
@thigm85
thigm85 / try_except_else_example.py
Last active March 8, 2016 13:28
Example of using else clause after try ... except clauses in python. Reference: https://docs.python.org/2/tutorial/errors.html#handling-exceptions
for arg in sys.argv[1:]:
try:
f = open(arg, 'r')
except IOError:
print 'cannot open', arg
else:
print arg, 'has', len(f.readlines()), 'lines'
f.close()