Udun's Labs

Python Notes


mylist = ['x', 3, 'b'] print '[%s]' % ', '.join(map(str, mylist))

Infinite generator: iter(int, 1) - try li=filter(None, iter(int, 1))

>>> def ii(): yield filter(None, iter(int, 1))
>>> ii
<function ii at 0x025FBA70>
>>> ii()
<generator object ii at 0x0262B788>
>>> ii()
<generator object ii at 0x0262BFD0>
>>> ii()[0]
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: 'generator' object has no attribute '__getitem__'
>>> ii().next()
# Infinite loop

I want to have a set of lists (as opposed to adding the list items to the set: s |= set(li))

Random refactoring puzzle:

# before - indexes better be some set (although just _micro_ optimization)
def SetChecked(self, indexes):
    for i in indexes:
        assert 0 <= i < self.Count, "Index (%s) out of range" % i
    for i in range(self.Count):
        self.Check(i, i in indexes)
# after ?
def SetChecked(self, indexes):
    for i in indexes: # will pick a random index from the set - may check
    # some items before assertion as opposed to before - important ?
        assert 0 <= i < self.Count, "Index (%s) out of range" % i
        self.Check(i, True)

Thou Shalt Not Modify A List During Iteration - for sets just use pop

My deleted: Override python list operators dynamically - see https://wiki.python.org/moin/FromFunctionToMethod - and non deleted Override list’s builtins dynamically in class scope and Access base class attribute in derived class - in ‘class scope’

To be expanded:

Interpreter facts
>>> def thatsSoUnlikeJava():
...     try:
...         raise Exception
...     finally:return True
>>> print thatsSoUnlikeJava()
>>> class C(object):
...     def __m(self): return 0
...     def m(self): return self.__m()
>>> class D(C):
...     def __m(self): return 1
>>> D().__m()
Traceback (most recent call last):
  File "<input>", line 1, in <module>
AttributeError: 'D' object has no attribute '__m'
>>> D().m()
>>> def wrap():
...     m=0
...     def wrapped():
...         m+=1
...         return m
...     return wrapped
>>> print wrap()()
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "<input>", line 4, in wrapped
UnboundLocalError: local variable 'm' referenced before assignment


Import never ceases to surprise me. For instance in the code I have:

    from win32com.shell import shell, shellcon

In Python27\Lib\site-packages\win32com there was no shell to be seen - then I noticed in Python27\Lib\site-packages\win32com\__init__.py:

__path__.append( win32api.GetFullPathName( __path__[0] + "\\..\\win32comext") )

And Python27\Lib\site-packages\win32comext\shell\__init__.py:

import win32com

And sure enough C:\_\Python27\Lib\site-packages\win32comext\shell\shell.pyd. What’s in there ? Centipydes probably. There’s shecllcon.py too. One can read there:


almost as in the program I am maintaining.

Python launcher for windows

Double click on a script will open with specified (by shebang) python version. Easiest to install: install python 3.3+ (see this for installer options). Gentle introduction and the PEP.

Mavenize an Eclipse Project

I had a simple eclipse java SE project and I wanted to start using maven (in 5 Minutes) for dependency management. What I did was (eclipse Luna, JEE pack - comes with m2e plugin []):

JBoss on Eclipse Luna

Windows - the Installing and starting JBoss AS on Windows page says I must have both maven and java installed - I really do not get why maven so I decided to proceed at my discretion.

  • Downloaded jboss-eap-6.2.0.zip from http://jbossas.jboss.org/downloads. A hell of a difficult site to locate. Btw the user friendliness of the JBoss site(s) is minimal - take a peek at the Getting Started Developing Applications Guide it says “This guide has move (sic) to http://www.jboss.org/jdf/quickstarts/jboss-as-quickstart/guide/Introduction/” - the latter link being broken. Plus the community servers are down.
  • Realized that this download is (probably - EDIT EDIT: JBoss 7.x or JBoss EAP 6.x) not what I wanted. The Installing and starting JBoss AS on Windows says something about jboss-7.x.x.x - scrolling further down in http://jbossas.jboss.org/downloads I located and downloaded JBoss AS 7.1.1.Final (jboss-as-7.1.1.Final.zip).
  • Unzipped jboss-as-7.1.1.Final.zip to my c:\dev
  • Fired eclipse up (Luna M7 Release (4.4.0M7) - EE pack) - located the update site of Jboss tools for Luna (installing them via the servers' view failed with dependency errors) - http://download.jboss.org/jbosstools/updates/development/luna/ - downloaded only JBossAS Tools 3.0.0.Beta1-v20140408-1231-B31, accepted, restarted, and the fun part started
  • Servers > New > JBoss AS 7.1 - profile Local (…), this server requires a runtime Next
  • JBoss 7.1 Runtime: download and install runtime, duh. let’s see
    • one of the runtimes (3) it offers to download is the jboss-as-7.1.1.Final.zip - great - go to Browse
    • c:\dev\jboss-as-7.1.1.Final, Execution environment JavaSE-1.6 (jdk done
  • import the project, make a new branch (jbossAS7)
  • run - oops:

    WARN [org.jboss.modules] (MSC service thread 1-6) Failed to define class x.y.Z in Module “deployment.DataCollectionServlet.war:main” from Service Module Loader: java.lang.UnsupportedClassVersionError: x/y/Z : Unsupported major.minor version 51.0

yep the project was 1.7 after all

  • deleted the server, created it anew, pointed the runtime of Jboss server to jdk 7 and it run - logging needs to be info though to print anything in eclipse’s console.
  • yes but I couldn’t hit the servlet - found the solution here: Binding JBoss AS 7 to all interfaces. In a nutshell I had to edit the server config at C:\dev\jboss-as-7.1.1.Final\standalone\configuration\standalone.xml - in there is the logging config too…

That was it - so basically zero modifications. Now to open shift…


Notes to zelf
  • Red exclamation mark - project won’t build - would not say why. Renamed the mains to main1 still no joy. Finally went to Build Path > Order and Export and ticked export on the JRE

        <?xml version="1.0" encoding="UTF-8"?>
           <classpathentry excluding="gr/uoa/di/mde515/engine/HeapFile.java" kind="src" path="src"/>
       -   <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
       +   <classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
           <classpathentry kind="output" path="bin"/>

    Lo and behold -> red ! became red x.
    Maybe a bug in caching ? (noted this after the Stack Overflows). I then unticked the JRE and still builds ok. See. Clean did not work and I had no missing libs either.

  • Server update sites:

      <?xml version="1.0" encoding="UTF-8"?>
         <site url="http://download.jboss.org/jbosstools/updates/development/luna/" selected="true" name="_jboss_tools"/>
         <site url="http://download.oracle.com/otn_software/oepe/luna/" selected="true" name="_oracle_glassfish"/>

    Unneeded are the maven ones (m2e-wtp had some uninstalled items though) :

      <site url="http://download.eclipse.org/m2e-wtp/milestones/luna/1.1.0" selected="true" name="_m2e-wtp"/>
      <site url="http://download.eclipse.org/technology/m2e/releases" selected="true" name="_m2eclipse"/>
  • Clean eclipse OSGi cache: Add -clean to eclipse.ini - first line, on its own - cleans mainly folder {eclipse}\configuration\org.eclipse.osgi. See How to run eclipse in clean mode? and what happens if we do so?. For workspace leaning see Clean out Eclipse workspace metadata - apparently -clean cleans the workspace too (?)

  • How to make xml file always be opened in text editor rather than xml editor

Java Memorabilia

Remember erasure ?

import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        List<String> strings = new ArrayList<>();
        ((List) strings).add(42); // compiler warning - ok at runtime
        // String s = strings.get(0); //  CCE - Compiler-generated cast
        System.out.println("strings: " + strings); // strings: [42]
        System.out.println(strings.contains(42)); // true

In other words:

    final HashSet<Integer> hashSet = new HashSet<>();
    // Set<String> cast = (Set<String>) hashSet; // error
    Set<String> cast = (Set) hashSet; // warning

From: here and here

Autohotkey Script for Cmd

Using windows without Autohotkey is using linux with no shell. No - I mean it.

For instance sooner or later you will use the cmd - this makes the xp bearable:


; Redefine only when the active window is a console window
#IfWinActive ahk_class ConsoleWindowClass

; Close Command Window with Ctrl+w
WinGetTitle sTitle
If (InStr(sTitle, "-")=0) {
        Send EXIT{Enter}
} else {
        Send ^w

; Ctrl+up / Down to scroll command window back and forward
Send {WheelUp}

Send {WheelDown}

; Paste in command window
; Spanish menu (Editar->Pegar, I suppose English version is the same, Edit->Paste)
Send !{Space}
Send {up}{up}{up}{up}{right}{down}{down}{enter}

; Mark in command window
Send !{Space}
Send {up}{up}{up}{up}{right}{enter}

; Select all in command window
Send !{Space}
Send {up}{up}{up}{up}{right}{down}{down}{down}{enter}



Some spanish guy holds the copyright I guess :D

Pythons (2) and Unicode

Repeat after me: Each unicode encoding (UTF-8, UTF-7, UTF-16, UTF-32, etc) maps different sequences of bytes to the unicode code points (therefore might as well map same sequences of bytes to different unicode code points). A code point is a number that maps to a particular abstract character (grapheme).


  • The unicode type stores an abstract sequence of code points.
  • str is for strings of bytes. These are very similar in nature to how strings are handled in C.

uni.encode(encoding): Unicode string to a Python byte string s.decode(encoding): convert a byte string to a Unicode string unicode(s, encoding): convert a byte string to a Unicode string

Important - Python 3. Important not only for guidelines but also for clarity. Moreover many “solutions” floating on the web are Python 3 (open() has both an encoding and newlines param in P3. ). Speaking of 2:

with open("file.txt", 'wb')as out:
with open("file2.txt", 'w')as out:
print(os.stat('file.txt').st_size)  # 1
print(os.stat('file2.txt').st_size)  # 2


Occasional C

When I happen to revisit this slim dinosaur that is C I always have to look up a couple things - here they are for your convenience and mine.

First things first - wrong:

int *ptr = malloc(10 * sizeof (int));


int *ptr = malloc(10 * sizeof *ptr);

NO exceptions. A, yes, sizeof is a unary operator. Are parentheses needed ? Well, I always add them when nobody’s looking.

Github Pages

Another 15 minutes fiddling with google so:




https://github.com/msysgit/msysgit.github.com/blob/master/index.html # com

Notice that https://wrye-bash.github.io/ gives a 404 - there should be an https://github.com/wrye-bash/wrye-bash.github.io/blob/master/index.html apparently.

Ah, yes - How do I clone a github wiki?:

Append .wiki.git to the repo name, i.e. if your repo name was foobar git clone git@github.com:myusername/foobar.git would be the path to clone your repo and git clone git@github.com:myusername/foobar.wiki.git would be the path to clone its wiki.

I ’m sure this post will grow.