Sebastian Thomschke
IT Freelancer – Java, J2EE, IBM WebSphere Portal, Lotus Notes/Domino-
Leveraging PyDev’s auto completion for indirectly created objects
This is just a quick tip how to enable auto completion in PyDev for indirectly created objects.
By default, PyDev has no problems in presenting you the possible object attributes and methods on a variable if that variable gets a new object instance assigned directly in the code.
For example, when you first type …
mybook = Book()
… and in the next line you enter …
mybook.
… PyDev will dutifully present you the statically declared features for the Book class in a popup.
If you however get that Book instance as the result of another method, e.g. …
mybook = bookstore.findBook("foobar")… PyDev currently seems to be helpless.
Because of Python’s dynamic nature it is of course a bit harder for an IDE to figure out what kind of objects may a method. But PyDev could for example honor the @return / @rtype PyDoc annotations (which you can add to the findBook’s method declaration) but currently it just does not.
To still have autocompletion you have two options:
- Temporarily instantiate a Book object in the code.
mybook = Book() mybook = bookstore.findBook("foobar") mybook. #--> now you have auto completionThis has the drawback, that if you forget to remove the first line before you finish coding an unneccessary Book instance will be created on every execution during runtime.
- Use an assertion to ensure that the mybook variable contains an object of type Book
mybook = bookstore.findBook("foobar") assert isinstance(mybook, Book) mybook. #--> now you have auto completion
- Temporarily instantiate a Book object in the code.
-
OVal 1.40 released
I am happy to announce the immediate availability of Version 1.40 OVal the Object Validation Framework for Java. This release fixes some minor issues and provides the following two new features:
1. Enabling/disabling constraints based on the object state:
Using the newly introduced when attribute for constraints it is possible to specify under which circumstances the constraint will be considered during object validation. The when attribute holds a formula in one of the supported scripting languages. If the formula returns true, the constraint will be activated for the current validation cycle otherwise it is ignored. In the following example the alias attribute must have a value only when the name attribute is not set.public class User { private String name; @NotNull(when = "groovy:_this.name == null") private String alias; // . . . }2. Fine grain control over how constraints are applied on arrays, maps, collections and their elements
The newly introduced attribute appliesTo allows you to specify if and how a constraint declared for a map, a collection or an array is applied to their elements as well or exclusively. See the following example how this works:public class BusinessObject { // the ids field may be null, but if it is not null // none of it's items must be null @NotNull(appliesTo = { ConstraintTarget.VALUES }) private String[] ids; // only the field itself must not be null, // the list can contain null values @NotNull(appliesTo = { ConstraintTarget.CONTAINER }) private List items; // the field must not be null as well as all keys // and values of the map @NotNull(appliesTo = { ConstraintTarget.CONTAINER, ConstraintTarget.KEYS, ConstraintTarget.VALUES }) private Map itemsByGroup; // . . . }Download
You can get the lates binaries and source files of OVal from here: http://sourceforge.net/projects/oval/files/ -
Installing WebSphere Portal in a local network
Recently I had to setup some WebSphere Portal 6.1 installations in VMWares within a private network. Unfortunately the portal installer aborted with the following message “EJPIC0067E: Portal requires a fully qualified host name that is recoganized by the DNS Server.” Even when adding the fully qualified hostname to the hosts file the installer may still fail with the same message.
The workaround is to disable the hostname check by invoking the portal installer with the following parameter specified: -W nodeHost.active=”False”. You should however only do this for test or development installations. -
Comparing version numbers in Jython / Python
Here comes a function to compare version numbers of e.g. Maven artifacts in Jython / Python.
import re def cmpver(vA, vB): """ Compares two version number strings @param vA: first version string to compare @param vB: second version string to compare @author Sebastian Thomschke @return negative if vA < vB, zero if vA == vB, positive if vA > vB. Examples: >>> cmpver("0", "1") -1 >>> cmpver("1", "0") 1 >>> cmpver("1", "1") 0 >>> cmpver("1.0", "1.0") 0 >>> cmpver("1.0", "1") 0 >>> cmpver("1", "1.0") 0 >>> cmpver("1.1.0", "1.0.1") 1 >>> cmpver("1.0.1", "1.1.1") -1 >>> cmpver("0.3-SNAPSHOT", "0.3") -1 >>> cmpver("0.3", "0.3-SNAPSHOT") 1 >>> cmpver("1.3b", "1.3c") -1 >>> cmpver("1.14b", "1.3c") 1 """ if vA == vB: return 0 def num(s): if s.isdigit(): return int(s) return s seqA = map(num, re.findall('\d+|\w+', vA.replace('-SNAPSHOT', ''))) seqB = map(num, re.findall('\d+|\w+', vB.replace('-SNAPSHOT', ''))) # this is to ensure that 1.0 == 1.0.0 in cmp(..) lenA, lenB = len(seqA), len(seqB) for i in range(lenA, lenB): seqA += (0,) for i in range(lenB, lenA): seqB += (0,) rc = cmp(seqA, seqB) if rc == 0: if vA.endswith('-SNAPSHOT'): return -1 if vB.endswith('-SNAPSHOT'): return 1 return rcTheoretically lines 43 – 49 could be written in a more compact way but using the short circuit evaluations (as below) lead to wrong results – at least in Jython 2.1:
seqa = map(lambda s: s.isdigit() and int(s) or s, re.findall('\d+|\w+', vA.replace('-SNAPSHOT', ''))) seqb = map(lambda s: s.isdigit() and int(s) or s, re.findall('\d+|\w+', vB.replace('-SNAPSHOT', ''))) -
Running TomCat 6 in Debug Mode under Windows
While tracing some problems in one of my grails applications I had the need to do step debugging on a remote Tomcat server. Eventually I came up with the following lines to launch TomCat in debug mode:
@echo off set JPDA_TRANSPORT="dt_socket" set JPDA_ADDRESS="8000" set JPDA_SUSPEND="y" catalina.bat jpda start
Simply create a debug.bat file in TomCat’s bin directory and add these lines.
-
Determine the user who logged on via SSH
Today we had the need to determine the initial id of a user who logged onto a Linux box via SSH and executed the su command. When the su command is issued the effective user is changed and whoami or id commands will report that new user id instead.
For anyone who is interested, that is what we came up to put the initial user id into a variable named ${LOGIN_USER}
LOGIN_USER=`who -m`; LOGIN_USER=${LOGIN_USER%% *}or alternatively
LOGIN_USER=`who -m | cut -d' ' -f1`


Looking for a high performer for your next IT project?

English
Deutsch
Recent Comments