2015-05-04

Read file pattern (Python)

Here's a pattern that I've been using a lot to read files in Python 2.7:
import codecs
badrow_count = 0
goodrow_count = 0
rowcount = 0

filename = "somefile.txt"
encoding = "utf-8"
# see https://docs.python.org/2/library/codecs.html#standard-encodings
# for encodings

decode_error_handler = 'strict'
# this is the default
# see https://docs.python.org/2/library/codecs.html#codec-base-classes 
# for decoding error callbacks

f = codecs.open(filename=filename, mode='rU', encoding=encoding, 
    errors=decode_error_handler)
eof = False
while not eof:
    row = u''
    try:
        row = f.next()
    except UnicodeDecodeError as e:
        badrow_count += 1
        # do other things on this row
    except StopIteration:
        eof = True
    #except Exception as e:
        # handle other issues    
    else:
        goodrow_count += 1
        # do other stuff with row
    finally:
        if not eof:
            rowcounter += 1
        else:
            break
I prefer this to:
for row in f:
primarily in order to catch unicode decoder errors.

2015-02-27

postgresql unpivot

Given a monthly currency conversion table like:
iso4127_codeJanFebMarAprMayJunJulAugSepOctNovDec
JPY0.0096130.0097920.0097780.0097570.0098270.0097900.0098290.0097170.0093460.0092620.0086060.008375

I need to unpivot so that I get a table that is:
iso4127_codemonthrate
JPY10.009613
JPY20.009792
...
select iso4127_code, 
unnest(array[1,2,3,4,5,6,7,8,9,10,11,12]) as month,
unnest(array[jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec]) as rate
from table

Strip HTML tags using regex

While for proper Markup Language parsing one should actually use an XML parser, sometimes you just want to strip all the markup using regex:
s/<\/?[^>]+>//

Query AD using ldapsearch

(Useful for troubleshooting AD logons if you have cygwin:

http://jurjenbokma.com/ApprenticesNotes/ldapsearch_ad_query.html)

Now at: http://jurjenbokma.com/ApprenticesNotes/ldapsearch_ad_query.xhtml
to force TLS_REQCERT never:

LDAPTLS_REQCERT=never ldapsearch ...

2014-12-31

Dynamic/Metaprogramming in ABAP

http://scn.sap.com/thread/1374403#7695723 http://scn.sap.com/thread/699312

2014-10-28

Preferred SSLCipherSuite for mod_ssl

SSLCipherSuite DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:HIGH:!3DES:!ECDH:!SRP:!aNULL:!CAMELLIA:!PSK:!EXPORT:!eNULL

Provides:
DHE-RSA-AES128-GCM-SHA256 TLSv1.2 Kx=DH       Au=RSA  Enc=AESGCM(128) Mac=AEAD
DHE-RSA-AES256-GCM-SHA384 TLSv1.2 Kx=DH       Au=RSA  Enc=AESGCM(256) Mac=AEAD
DHE-RSA-AES128-SHA256   TLSv1.2 Kx=DH       Au=RSA  Enc=AES(128)  Mac=SHA256
DHE-RSA-AES256-SHA256   TLSv1.2 Kx=DH       Au=RSA  Enc=AES(256)  Mac=SHA256
DHE-RSA-AES128-SHA      SSLv3 Kx=DH       Au=RSA  Enc=AES(128)  Mac=SHA1
DHE-RSA-AES256-SHA      SSLv3 Kx=DH       Au=RSA  Enc=AES(256)  Mac=SHA1
AES128-GCM-SHA256       TLSv1.2 Kx=RSA      Au=RSA  Enc=AESGCM(128) Mac=AEAD
AES256-GCM-SHA384       TLSv1.2 Kx=RSA      Au=RSA  Enc=AESGCM(256) Mac=AEAD
AES128-SHA256           TLSv1.2 Kx=RSA      Au=RSA  Enc=AES(128)  Mac=SHA256
AES256-SHA256           TLSv1.2 Kx=RSA      Au=RSA  Enc=AES(256)  Mac=SHA256
AES128-SHA              SSLv3 Kx=RSA      Au=RSA  Enc=AES(128)  Mac=SHA1
AES256-SHA              SSLv3 Kx=RSA      Au=RSA  Enc=AES(256)  Mac=SHA1
DHE-DSS-AES256-GCM-SHA384 TLSv1.2 Kx=DH       Au=DSS  Enc=AESGCM(256) Mac=AEAD
DHE-DSS-AES256-SHA256   TLSv1.2 Kx=DH       Au=DSS  Enc=AES(256)  Mac=SHA256
DHE-DSS-AES256-SHA      SSLv3 Kx=DH       Au=DSS  Enc=AES(256)  Mac=SHA1
DHE-DSS-AES128-GCM-SHA256 TLSv1.2 Kx=DH       Au=DSS  Enc=AESGCM(128) Mac=AEAD
DHE-DSS-AES128-SHA256   TLSv1.2 Kx=DH       Au=DSS  Enc=AES(128)  Mac=SHA256
DHE-DSS-AES128-SHA      SSLv3 Kx=DH       Au=DSS  Enc=AES(128)  Mac=SHA1
This is a modification of the ciphersuite list from http://www.matthewgkeller.com/blog/2014/01/09/ecdhe-vs-dhe-in-the-new-world-order/comment-page-1/

Primarily we are looking to remove elliptical curve ciphers in favor of discrete log methods (Schneier, 2013) due to the uncertainty of NSA compromization of ECC. We also try to prioritize the remaining available ciphers by preferring GCM mode over CBC mode.

  1. AES128 has better key schedule than AES256 (Schneier, 2013).
  2. We support "SSLv3" ciphers because in OpenSSL, TLSv1.0 ciphers are classified as SSLv3 ciphers.

2014-10-27

SAP Internal Order Settlement Receiver Field

The value of this field depends on a business logic ruleset (thus, SAP GUI will report it as a structured field) when you inspect it from Internal Order display/Settlement rule tab. Here is (part of) the ruleset for determining the Settlement Receiver value: (Pseudo-SQL, NOT ABAP)
CASE 
   WHEN COBRB.KONTY = 'CTR'
      THEN COBRB.KOSTL
   WHEN COBRB.KONTY = 'FXA'
      THEN COBRB.ANLN1
END
(FXA is fixed asset settlement).

h/t (http://scn.sap.com/thread/3416443)