http://stackoverflow.com/questions/38987/how-can-i-merge-two-python-dictionaries-in-a-single-expression
Wednesday, May 25, 2016
Tuesday, May 24, 2016
python timeout script
http://stackoverflow.com/questions/2281850/timeout-function-if-it-takes-too-long-to-finish
# 1
# 2
import signal
from time import sleep
class TimeoutError(Exception):
pass
class timeout:
def __init__(self, seconds=1, error_message='Error Message: Timeout - The process is taking longer than expected.'):
self.seconds = seconds
self.error_message = error_message
def handle_timeout(self, signum, frame):
#print 'Time Out'
raise TimeoutError(self.error_message)
def __enter__(self):
signal.signal(signal.SIGALRM, self.handle_timeout)
signal.alarm(self.seconds)
def __exit__(self, type, value, traceback):
signal.alarm(0)
with timeout(seconds=10):
a = 50
while a > 0:
print 'OK'
sleep(5)
# 1
# 2
import signal
from time import sleep
class TimeoutError(Exception):
pass
class timeout:
def __init__(self, seconds=1, error_message='Error Message: Timeout - The process is taking longer than expected.'):
self.seconds = seconds
self.error_message = error_message
def handle_timeout(self, signum, frame):
#print 'Time Out'
raise TimeoutError(self.error_message)
def __enter__(self):
signal.signal(signal.SIGALRM, self.handle_timeout)
signal.alarm(self.seconds)
def __exit__(self, type, value, traceback):
signal.alarm(0)
with timeout(seconds=10):
a = 50
while a > 0:
print 'OK'
sleep(5)
Tuesday, May 17, 2016
Wednesday, May 11, 2016
python xml
https://docs.python.org/2/library/xml.etree.elementtree.html
http://stackoverflow.com/questions/15643094/python-version-2-7-xml-elementtree-how-to-iterate-through-certain-elements-of
http://stackoverflow.com/questions/9177360/updating-xml-elements-and-attribute-values-using-python-etree
http://stackoverflow.com/questions/14440375/how-to-add-an-element-to-xml-file-by-using-elementtree
# Node name = '.tag'
# Node attribute = '.attrib'
# Node value = '.text'
#
Element.findall() finds only elements with a tag which are direct children of the current element. Element.find() finds the first child with a particular tag, and Element.text accesses the element’s text content. Element.get() accesses the element’s attributes:
# XML Handling
[To write an XML file using Etree: tree.write("/tmp/" + executionID +".xml") ]
import xml.etree.ElementTree as ET
# Get element object:
# First get root element
# From File:
tree = ET.parse('country_data.xml')
root = tree.getroot()
# From xml string
xml_str = '''
<project>
<publishers>
...
...
</publishers>
<project>
root = ET.fromstring(xml_str)
updated_xml_str = ET.tostring(root)
====
# Get desired element object to set using XPath: text or attribute
DESCRIPTION_PATH = './publishers/hudson.plugins.descriptionsetter.DescriptionSetterPublisher/description'
DESCRIPTION_PATH is path relative to the root element (in case of jenkins, the root element is project)
root.find(DESCRIPTION_PATH).text = 'Updated Text'
# Create new element:
# create new child object:
new_element_object = 'ET.Element("new_element")
# Add the new_element_object to the existing element you want;
for root:
root.append(new_element_object)
OR (a one-liner):
root.find(DESCRIPTION_PATH).append('new_element_object')
OR (use Subelement);
ET.SubElement(root.find(DESCRIPTION_PATH), tag='new_element')
ET.SubElement(root.find(DESCRIPTION_PATH), 'new_element') # Without tag variable
OR (with text setting)
ET.SubElement(root.find(DESCRIPTION_PATH), 'new_element').text = 'HELLO MAN JEE!'
# element.findall(path) : gives a list of all elements at the given path relative to the element it is operated on
it gives list of all the element: to find the text, you need to use the method 'find'
y = root.findall('builders/hudson.tasks.Shell')
for x in y:
print x.find('command').text
http://stackoverflow.com/questions/15643094/python-version-2-7-xml-elementtree-how-to-iterate-through-certain-elements-of
http://stackoverflow.com/questions/9177360/updating-xml-elements-and-attribute-values-using-python-etree
http://stackoverflow.com/questions/14440375/how-to-add-an-element-to-xml-file-by-using-elementtree
# Node name = '.tag'
# Node attribute = '.attrib'
# Node value = '.text'
#
Element.findall() finds only elements with a tag which are direct children of the current element. Element.find() finds the first child with a particular tag, and Element.text accesses the element’s text content. Element.get() accesses the element’s attributes:
# XML Handling
[To write an XML file using Etree: tree.write("/tmp/" + executionID +".xml") ]
import xml.etree.ElementTree as ET
# Get element object:
# First get root element
# From File:
tree = ET.parse('country_data.xml')
root = tree.getroot()
# From xml string
xml_str = '''
<project>
<publishers>
...
...
</publishers>
<project>
root = ET.fromstring(xml_str)
updated_xml_str = ET.tostring(root)
====
# Get desired element object to set using XPath: text or attribute
DESCRIPTION_PATH = './publishers/hudson.plugins.descriptionsetter.DescriptionSetterPublisher/description'
DESCRIPTION_PATH is path relative to the root element (in case of jenkins, the root element is project)
root.find(DESCRIPTION_PATH).text = 'Updated Text'
# Create new element:
# create new child object:
new_element_object = 'ET.Element("new_element")
# Add the new_element_object to the existing element you want;
for root:
root.append(new_element_object)
OR (a one-liner):
root.find(DESCRIPTION_PATH).append('new_element_object')
OR (use Subelement);
ET.SubElement(root.find(DESCRIPTION_PATH), tag='new_element')
ET.SubElement(root.find(DESCRIPTION_PATH), 'new_element') # Without tag variable
OR (with text setting)
ET.SubElement(root.find(DESCRIPTION_PATH), 'new_element').text = 'HELLO MAN JEE!'
# element.findall(path) : gives a list of all elements at the given path relative to the element it is operated on
it gives list of all the element: to find the text, you need to use the method 'find'
y = root.findall('builders/hudson.tasks.Shell')
for x in y:
print x.find('command').text
Saturday, May 7, 2016
get all POST args in python flask
http://stackoverflow.com/questions/25410944/print-all-post-request-parameters-without-knowing-their-names
The code below works for URLs with parameters added,like:
request.args returns a MultiDict. It can have multiple values for each key. In order to print all parameters, you can try:The code below works for URLs with parameters added,like:
http://www.webservice.my/rest?extraKey=extraValue
multi_dict = request.args
for key in multi_dict:
print multi_dict.get(key)
print multi_dict.getlist(key)
For parameters embedded within the POST request as a form:dict = request.form
for key in dict:
print 'form key '+dict[key]
See the example here and you will have a good idea.Thursday, May 5, 2016
flask on openshift
Cloning into 'myflaskapp'...
The authenticity of host 'myflaskapp-rainbeats.rhcloud.com (54.175.133.175)' can't be established.
RSA key fingerprint is cf:ee:77:cb:0e:fc:02:d7:72:7e:ae:80:c0:90:88:a7.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'myflaskapp-rainbeats.rhcloud.com,54.175.133.175' (RSA) to the list of known hosts.
Your application 'myflaskapp' is now available.
URL: http://myflaskapp-rainbeats.rhcloud.com/
SSH to: 572bf48189f5cfab9f000079@myflaskapp-rainbeats.rhcloud.com
Git remote: ssh://572bf48189f5cfab9f000079@myflaskapp-rainbeats.rhcloud.com/~/git/myflaskapp.git/
Cloned to: /root/myflaskapp
Run 'rhc show-app myflaskapp' for more details about your app.
#
https://blog.openshift.com/getting-started-with-mongodb-shell-on-openshift/
# Mongo not accessible
http://stackoverflow.com/questions/25898744/rhc-and-mongo-command-not-found-in-openshift-cartridge
#
Please make note of these MongoDB credentials:
RockMongo User: admin
RockMongo Password: lGguBhDpSp4Z
URL: https://python-rainbeats.rhcloud.com/rockmongo/
The authenticity of host 'myflaskapp-rainbeats.rhcloud.com (54.175.133.175)' can't be established.
RSA key fingerprint is cf:ee:77:cb:0e:fc:02:d7:72:7e:ae:80:c0:90:88:a7.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'myflaskapp-rainbeats.rhcloud.com,54.175.133.175' (RSA) to the list of known hosts.
Your application 'myflaskapp' is now available.
URL: http://myflaskapp-rainbeats.rhcloud.com/
SSH to: 572bf48189f5cfab9f000079@myflaskapp-rainbeats.rhcloud.com
Git remote: ssh://572bf48189f5cfab9f000079@myflaskapp-rainbeats.rhcloud.com/~/git/myflaskapp.git/
Cloned to: /root/myflaskapp
Run 'rhc show-app myflaskapp' for more details about your app.
#
https://blog.openshift.com/getting-started-with-mongodb-shell-on-openshift/
# Mongo not accessible
http://stackoverflow.com/questions/25898744/rhc-and-mongo-command-not-found-in-openshift-cartridge
#
Please make note of these MongoDB credentials:
RockMongo User: admin
RockMongo Password: lGguBhDpSp4Z
URL: https://python-rainbeats.rhcloud.com/rockmongo/
Subscribe to:
Posts (Atom)