Friday, October 30, 2015

Sometimes Python's multiple inheritance has me scratching my head...

I was trying to figure out and demonstrate python's Member Resolution Order (python2.7)... In particular, in the following, I suggest you pay attention to the member functions __new__ and __init__. Bear in mind that the following code (save for the calls to functions 'begin' & 'end') use only the minimum of code infrastructure to create subclass Class ABC from ClassAB and ClassAC (of ClassA) using multiple inheritance. If you wondered why this was so hard, then read on...
#!/usr/bin/env python
# -*- coding: utf-8 -*-
depth=0
def begin(*arg_l,**arg_d):
global depth
print(" "*depth+"BEGIN",arg_l,",".join("%s=%s"%item for item in arg_d.items()))
depth+=1
def end(*arg_l):
global depth
depth-=1
print(" "*depth+"END",arg_l)
class ClassA(object):
def __new__(SubClass,a,kwA="kwA",*arg_l,**arg_d):
begin("new.ClassA",SubClass,a,*arg_l,**arg_d)
out_instance=super().__new__(SubClass,*arg_l) # ,**arg_d) # IGNORE init kwabc
end("new.ClassA INSTANCE!",out_instance)
return out_instance
def __init__(self,a,kwa="kwa",*arg_l,**arg_d):
begin("init.ClassA",self,a,*arg_l,**arg_d)
none=super().__init__(*arg_l) # ,**arg_d) # IGNORE new kwABC
end("init.ClassA",none)
return none # Always None
class ClassAB(ClassA):
def __new__(SubClass,ab,ac,a,kwAB="kwAB",*arg_l,**arg_d):
begin("new.ClassAB",SubClass,ab,ac,a,*arg_l,**arg_d)
out_instance=super().__new__(SubClass,ac,a,*arg_l,**arg_d)
end("new.ClassAB INSTANCE!",out_instance)
return out_instance
def __init__(self,ab,ac,a,kwab="kwab",*arg_l,**arg_d):
begin("init.ClassAB",self,ab,ac,a,*arg_l,**arg_d)
none=super().__init__(ac,a,*arg_l,**arg_d)
end("init.ClassAB",none)
return none # Always None
class ClassAC(ClassA):
def __new__(SubClass,ac,a,kwAC="kwAC",*arg_l,**arg_d):
begin("new.ClassAC",SubClass,ac,a,*arg_l,**arg_d)
out_instance=super(ClassAC,SubClass).__new__(SubClass,a,*arg_l,**arg_d)
end("new.ClassAC INSTANCE!",out_instance)
return out_instance
def __init__(self,ac,a,kwac="kwac",*arg_l,**arg_d):
begin("init.ClassAC",self,ac,a,*arg_l,**arg_d)
none=super().__init__(a,*arg_l,**arg_d)
end("init.ClassAC",none)
return none # Always None
class ClassABC(ClassAB,ClassAC):
def __new__(SubClass,abc,ab,ac,a,kwABC="kwABC",*arg_l,**arg_d):
begin("new.ClassABC",SubClass,abc,ab,ac,a,*arg_l,**arg_d)
out_instance=super(ClassABC,SubClass).__new__(SubClass,ab,ac,a,*arg_l,**arg_d)
end("new.ClassABC INSTANCE!",out_instance)
return out_instance
def __init__(self,abc,ab,ac,a,kwabc="kwabc",*arg_l,**arg_d):
begin("init.ClassABC",self,abc,ab,ac,a,*arg_l,**arg_d)
none=super().__init__(ab,ac,a,*arg_l,**arg_d)
end("init.ClassABC",none)
return none # Always None
print(ClassABC(
"ABC","AB","AC","A",
kwABC="kwABC",kwAB="kwAB",kwAC="kwAC",kwA="kwA",
kwabc="kwabc",kwab="kwab",kwac="kwac",kwa="kwa"
))

Output:

 
BEGIN ('new.ClassABC', <class '__main__.ClassABC'>, 'ABC', 'AB', 'AC', 'A') kwAB=kwAB,kwAC=kwAC,kwA=kwA,kwabc=kwabc,kwab=kwab,kwac=kwac,kwa=kwa
BEGIN ('new.ClassAB', <class '__main__.ClassABC'>, 'AB', 'AC', 'A') kwAC=kwAC,kwA=kwA,kwabc=kwabc,kwab=kwab,kwac=kwac,kwa=kwa
BEGIN ('new.ClassAC', <class '__main__.ClassABC'>, 'AC', 'A') kwA=kwA,kwabc=kwabc,kwab=kwab,kwac=kwac,kwa=kwa
BEGIN ('new.ClassA', <class '__main__.ClassABC'>, 'A') kwabc=kwabc,kwab=kwab,kwac=kwac,kwa=kwa
END ('new.ClassA INSTANCE!', <__main__.ClassABC object at 0x7f8a829da190>)
END ('new.ClassAC INSTANCE!', <__main__.ClassABC object at 0x7f8a829da190>)
END ('new.ClassAB INSTANCE!', <__main__.ClassABC object at 0x7f8a829da190>)
END ('new.ClassABC INSTANCE!', <__main__.ClassABC object at 0x7f8a829da190>)
BEGIN ('init.ClassABC', <__main__.ClassABC object at 0x7f8a829da190>, 'ABC', 'AB', 'AC', 'A') kwABC=kwABC,kwAB=kwAB,kwAC=kwAC,kwA=kwA,kwab=kwab,kwac=kwac,kwa=kwa
BEGIN ('init.ClassAB', <__main__.ClassABC object at 0x7f8a829da190>, 'AB', 'AC', 'A') kwABC=kwABC,kwAB=kwAB,kwAC=kwAC,kwA=kwA,kwac=kwac,kwa=kwa
BEGIN ('init.ClassAC', <__main__.ClassABC object at 0x7f8a829da190>, 'AC', 'A') kwABC=kwABC,kwAB=kwAB,kwAC=kwAC,kwA=kwA,kwa=kwa
BEGIN ('init.ClassA', <__main__.ClassABC object at 0x7f8a829da190>, 'A') kwABC=kwABC,kwAB=kwAB,kwAC=kwAC,kwA=kwA
END ('init.ClassA', None)
END ('init.ClassAC', None)
END ('init.ClassAB', None)
END ('init.ClassABC', None)
<__main__.ClassABC object at 0x7f8a829da190>

Updated for Py3's super()

eg. none=super().__init__(*arg_l)

Conclusion:

Similar to a MS-Word Template document, Python classes appear temptingly easy, that is until you need to do multiple inheritance, and then there is copious amounts of syntactic-salt & syntactic-vinegar that the coder needs carefully balance to get the right taste. 


Note that arg_l is virtually unusable because of a clash with keyword arguments. And positional arguments need to be carefully counted and consumed, otherwise each class with "TypeError: __new__() got multiple values for keyword argument 'kwABC'". Even restricting yourself to keyword arguments only is problematic, in particular __new__ and __init__ of the same should "consume" exactly the same keyword argument, otherwise you will get the warnings: "DeprecationWarning: object.__init__() takes no parameters" etc.


However... maybe... with an appropriate argument naming convention the probability of a coder putting bullet hole in their own foot would be reduced.
Hints welcomed.

Thursday, September 10, 2015

Algol68 code samples, view on-line...

View... and then run on-line...

Maybe you want to hack your first "Hello, world!" program and run it on-line right NOW:
To see how easy Concurrent computing is, paste this specimen:
  PAR(
    print("Enjoy "),
    print("Rosetta "),
    print("Code ")
  )

See also: Have you ever wanted to test drive Algol68?... are ANY of the rumours true?

The Algol68 code specimen samples:

9

A

B

C

D

E

F

G

H

I

J

K

L

M

N

O

P

Q

R

S

T

U

V

W

Y

Z

Wednesday, September 9, 2015

Facebook hates Pythagoras: a²+b²=c²

And Wikipedia love/hates "a2+b2=c2" even more! = ☹

Are you a FaceBook user? And have you EVER tried to cut-and-past the Pythagorean-theorem of "a²+b²=c²?" [ I do it EVERY day :-^) ] ... But found your resulting paste gives the grotesque result of "a2+b2=c2?" ...

Now try to cut-and-paste this Facebook/Wikipedia version: "a2+b2=c2?"

Don't believe me? ... then try to cut and paste from either: Facebook: Pythagorean-theorem or Wikipedia: Pythagorean-theorem - Proof by rearrangement.

¢ Kind of typical of the 21ˢᵗ century... after 2½ thousand years of civilisation one cannot simply cut and paste Pythagoras's theorem of a²+b²=c² from wikipedia and share it with a friend on FaceBook! ☹ ¢

Fortunately WolframAlpha has no such problem, eg. click on: a²+b²=c²

Here is a useful table you from where you CAN cut-and-paste ˢᵘᵖᵉʳˢᶜʳᶦᵖᵗˢ and ₛᵤbₛcᵣᵢₚₜ... ¢ oops... Note the b & c, maybe we need to wait until the 22ⁿᵈ century for the entire alphabet to be in all browsers? ¢

Superscript and subscripts cut-and-paste
  0 1 2 3 4 5 6 7 8 9 A B C D E F
U+00Bx                          
U+207x x⁰ xⁱ     x⁴ x⁵ x⁶ x⁷ x⁸ x⁹ x⁺ x⁻ x⁼ x⁽ x⁾ xⁿ
U+208x x₀ x₁ x₂ x₃ x₄ x₅ x₆ x₇ x₈ x₉ x₊ x₋ x₌ x₍ x₎  
U+209x xₐ xₑ xₒ xₓ xₔ xₕ xₖ xₗ xₘ xₙ xₚ xₛ xₜ      

Ironically Wikipedia does detail how to achieve the alphabet in either ₛᵤbₛcᵣᵢₚₜ and ˢᵘᵖᵉʳˢᶜʳᶦᵖᵗˢ... Wikipedia: Unicode subscripts and superscripts.

But you can simply use this page: piliapp.com to: c̶r̶o̶s̶s̶ ̶o̶u̶t̶ ̶m̶i̶s̶t̶a̶k̶e̶s̶ or u͟n͟d͟e͟r͟l͟i͟n͟e͟ ͟i͟m͟p͟o͟r͟t͟a͟n͟t͟ ͟s͟t͟u͟f͟f AND THEN cut and paste it to share on FaceBook. Or use tiny-text to generate ˢᵘᵖᵉʳˢᶜʳᶦᵖᵗˢ.

And for even more find try: ₛᵤbₛcᵣᵢₚₜ @ panix.com

For the technically minded, here is a table of as many of the SI units that I could find, all ready for your cut-and-paste. ¢ Enjoy ¢

Wikipedia: SI Base Units

Named base SI base units.
Name Symbol Unicode Codepoint Quantity Typical Symbol for Variables
metre m

length l (lowercase L)
kilogram kg U+338F mass m
second s

time t
ampere A

electric current I (capital i)
kelvin K

thermodynamic temperature T
candela cd U+33C5 luminous intensity Iv (capital i with lowercase non-italicized v subscript)
mole mol U+33D6 amount of substance n

Wikipedia: Derived Units

Named units derived from SI base units

Symbol Unicode Expression in terms of other units Expression in terms of SI base units Quantity
hertz Hz ¹⁄s s⁻¹ frequency
radian rad m⁄m dimensionless angle
steradian sr ㎡⁄㎡ dimensionless solid angle
newton N
㎏⋅m⁄s² or ㎏⋅㎨ force, weight
pascal Pa N⁄㎡ ㎏⁄m⁄s² pressure, stress
joule J
N⋅m = C⋅V = W⋅s ㎏⋅㎡⁄s² energy, work, heat
watt W
J⁄s = V⋅A ㎏⋅㎡⁄s³ power, radiant flux
coulomb C
s⋅A s⋅A electric charge or quantity of electricity
volt V
W⁄A = J⁄C ㎏⋅㎡⁄s³⁄A voltage, electrical potential difference, electromotive force
farad F
C⁄V s⁴⋅A²⁄㎏⁄㎡ electric capacitance
ohm Ω
V⁄A ㎏⋅㎡⁄s³⁄A² electric resistance, impedance, reactance
siemens S
℧ = A⁄V s³⋅A²⁄㎏⁄㎡ electrical conductance
weber Wb J⁄A ㎏⋅㎡⁄s²⁄A magnetic flux
tesla T
V⋅s⁄㎡ = ㏝⁄㎡ = N⁄A⁄m ㎏⁄s²⁄A
magnetic field strength, magnetic flux density
henry H
V⋅s⁄A = ㏝⁄A ㎏⋅㎡⁄s²⁄A² inductance
deg Celsius °C K K temperature relative to 273.15 K
lumen lm ㏅⋅㏛ luminous flux
lux lx ㏐⁄㎡ ㏅⁄㎡ illuminance
becquerel Bq ¹⁄s s⁻¹ radioactivity (decays per unit time)
gray Gy J⁄㎏ ㎡⁄s² absorbed dose (of ionizing radiation)
sievert Sv J⁄㎏ ㎡⁄s² equivalent dose (of ionizing radiation)
katal kat
㏖⁄s ㏖⁄s catalytic activity

Wikipedia: The Metric Prefix

Unicode characters of SI derived units and other related non SI units.
Scale femto pico nano micro milli centi deci
hecto kilo Mega Giga Tera Other
Dimensionless

, ㏙, ㏗
Distance



㎞, ㏎
㍳, ㍶, ㏕, ㏌
Area


Volume
㎣, ㎖ ㎤,㏄ ㍹, ㎗ ㎥, ℓ
㎦, ㎘
Mass





㍲, ㍺, 🝲, 🝳, ℥
Pressure


, ㍴
Time

㏂, ㏘, 🝮, 🝱
Frequency


Potential





㍵, ㏞, ㋎
Current





Impedance related





Power





㏋, ㏈
Heat and Energy


, ℉, ㋍
Speed, acceleration and miscellaneous ㎧, ㎨, ㏏,㏚, ㏍
Radio Activity and light , , , , , ㎮, ㎯, , , , ㏆
Note: Struck through units are Unicode SI units already appearing in the first two tables.

Monday, August 10, 2015

Regarding the Colour of the New Zealand Flag...

(Alternative title: The ex-Empiree's New Clothes) #nzflag

Ironically the post WWII British Gov was only too happy (and in a big rush) to "grant" all "their" colonies "independence" cf. http://www.vice.com/en_uk/read/the-uk-government-are-opening-thousands-of-secret-files-to-the-public...

To achieve this the British Gov even had a special mobile hit squad of diplomats to speed up the "transition" and the British exit. Did you ever notice that when the "sun set" on the British Empire, it set WITHOUT the British Marines EVER loosing a single battle? Figure that one out! How is that a retreat?

It all culminated around 1970 when NZ (and others) became victim of combined effect of the Secret UK/US "Atlantic Charter" AND the UK/US manipulation of the Goldstandard... the net effect was the virtual crash of the NZ economy...

Meantime my grandparents quietly had their Irish/Scottish rights of descent "squished" by over eager "ex-colonies" asserting their newly found "sovereignty".

[ The ex-colonies "officials" were (in most cases) re-engineering the British "empire's" original bureaucracy to suit the pro-independence family dynasties that emerged ]

The end of the "British Empire", meant the end of the common currency, the end of border-less global travel, the imposition of ID numbers/travel-documents, the end of free trade, the increase in the ACTUAL income tax rate, and economies becoming centric on super-cities.

[ Ironically the common currency with free internal trade & travel of the US, EU & China (and even Australia) is what powers these economies. ]

If the ex-British colonies were truly smart, then they would have evicted England from the Empire and but kept the Empire. We then would have been had a chance to stand up against the emerging economic super powers and been be in a much better position to augment (and develop) the more vulnerable smaller economies.

So... just maybe... the colour of the flag is not the issue? Maybe the flag's colour is just another cleverly engineered distraction. Maybe the real question is... Who engineered this distraction? (And why?)