annotation and reflection
@Retention(RetentionPolicy.RUNTIME)
public @interface DataContract {
String testToCall();
}
@DataContract(testToCall="Test")
public enum Cat {
Red("r"),
Blue("B"),
Yellow("Y");
public void Test(int id){System.out.println("test called "+id);}
}
main(String[] args) {
Cat c3= Cat.Red;
DataContract a= c3.getClass().getAnnotation(DataContract.class);
Method m = c3.getClass().getMethod(a.testToCall(),int.class);
m.invoke(c3,3); }
File IO:
PrintWriter w= new PrintWriter(new FileWriter("c:\\..."));
w.println("line 1");
w.close();
FileReader r= new FileReader("c:\\...");
BufferedReader br= new BufferedReader(r);
CapReader cr = new CapReader(br);
System.out.println(cr.readLine());
cr.close();
Path p=Paths.get(uri);
Path p1=Paths.get(uri+"1");
Path p2=Paths.get(uri);
Files.copy(p, p1);
Files.deleteIfExists(p);
Files.move(p1, p2);
Collections
List<Integer> list2=Arrays.asList(1,2,3,4);
Set<Integer> s= new HashSet<Integer>();
Queue<String> q= new LinkedList<String>();
q.offer("a"); q.poll(); q.peek();
\Map<Integer,Dog<Sex>> dogMap= new HashMap<Integer,Dog<Sex>>();
dogMap.put(1, new Dog<Sex>(null));
dogMap.get(1).Get();
Cat c= Cat.Red; //enum is class and has collection
for(Cat c1 : Cat.values()) System.out.println(c1);
Threading --by extends
Task t= new Task(); t.start(); // run()=sync start=async.
for (int i=0;i<100;i++) println("Main Thread run "+Thread.currentThread().getId());
public class Task extends Thread {
@Override public void run() {super.run();
for (int i=0;i<100;i++) Thread.sleep(1000);
println(Thread.currentThread().getId()+" not blocking main thread "+i);
Threading by Runnable
public class Task implements Runnable { @Override public void run(){same code}}
Task t= new Task(); Thread th= new Thread(t); th.start(); // explicit new thread
Threading by Executor
ExecutorService exec= Executors.newFixedThreadPool(3);
ExecutorService exec= Executors.newSingleThreadExecutor();
exec.submit(tRunnable);
exec.shutdown();
Synchronization at Method level
public class HW implements Runnable { static int counter=0;
public static void main(String[] args) {
ExecutorService exec= Executors.newFixedThreadPool(100);
HW h=new HW(); for(int i=0;i<10;i++) exec.submit(h);
exec.shutdown(); }
@Override
public synchronized void run() { counter++;
println("Thread Sync "+Thread.currentThread().getId()+" "+counter);}
maven mvn clean compile packge JAVA_HOME MAVEN_HOME
Thursday, March 31, 2016
Java First Braindump
Wednesday, March 30, 2016
Think Design Pattern in psuedo-code
Design pattern are abstract and hard to explain in words. But code seems simple
Visitor -- IAccept IVisitor
public interface IVisitor { Visit(Stock s);Visit(Option o);}
public interface IAccpet { void Accept(IVisitor v);}
public class Option : IAccpet {
public void Accept(IVisitor v) { v.Visit(this);}
}
public class Stock : IAccpet {
public void Accept(IVisitor v) {v.Visit(this); }
}
public class PnLVisitor : IVisitor {
void Visit(Option o) { } void Visit(Stock s) { } }
public class Trader
{
List<IAccpet> _holdings = new List<IAccpet>() { new Stock(), new Option() };
void CalcPnl() { foreach (var h in _holdings) h.Accept(new PnLVisitor()); }
}
Decorator---avoid sub-classing by composition/Wrapper
// eg. BufferStream(NetwokStream) since NS no buf
abstract class BaseComponent { }
class Decorator
{
protected BaseComponent _base;
// need to pass in concrete one
public Decorator(BaseComponent b){_base = b;}
}
Strategy --extend not modify
// new Strat extend for the same ctx
// avoid switch statement, call svc varying strat
public interface IStrategy { double Calculate();}
public class OptoinStrategy : IStrategy {
public OptoinStrategy(Context ctx) {}
public double Calculate(){return strategyValue;}
}
public class Context { }
public class Service {
IStrategy _strat;
public Service(IStrategy strat){ _strat = strat;}
public double Calc(){return _strat.Calculate();}
}
Think threading in pseudo-code
Threading primitives, Algorithm are better described in code
PLinq=AsParallel+With options
static void p_linq_as_parallel()
{
var rng = Enumerable.Range(3, 1000000000 - 3).AsParallel();
var primes = rng.Where(r => Enumerable.Range(2, (int) Math.Sqrt(r)).AsParallel().All(i => r%i > 0));
}
CountDown Rendezvous
static CountdownEvent _ce= new CountdownEvent(2);
static void count_down_2()
{
new Thread((d) =>
{
Thread.Sleep(3000);
Console.WriteLine("signal/wait after 3 sec, Id={0}",
Thread.CurrentThread.ManagedThreadId);
_ce.Signal();
_ce.Wait();
}).Start(); //immediate start no more sleep
Console.WriteLine("immed signal but have to wait,Id={0}",
Thread.CurrentThread.ManagedThreadId);
_ce.Signal();
_ce.Wait();
Console.WriteLine("all signaled so proceed");
}
Reader/Writer Lock = Monitor +upgrade+policy
// ReaderWriterLockSlim is 2x slower than lock
private static List<int> _shared_data = new List<int>();
private static ReaderWriterLockSlim _rw = new ReaderWriterLockSlim();
static void reader_writer()
{
//multi- reader and writer
new Thread(read_intermittent_continously).Start(); //muti-reader
new Thread(read_intermittent_continously).Start();
new Thread(write_occationa_continously).Start(); //muti-reader
new Thread(write_occationa_continously).Start();
}
static void read_intermittent_continously()
{
while (true)
{
_rw.EnterReadLock();
foreach (var i in _shared_data) { Thread.Sleep(10);}
_rw.ExitReadLock();
}
}
static void write_occationa_continously()
{
while (true)
{
int i = (new Random()).Next();
if (!_shared_data.Contains(i))
{
_rw.EnterWriteLock();
_shared_data.Add(i);
_rw.ExitWriteLock();
}
// need to read before upd=upgradeable
_rw.EnterUpgradeableReadLock();
int ii = _shared_data[0];
_rw.EnterWriteLock();
// do some updates
_rw.ExitWriteLock();
_rw.ExitUpgradeableReadLock();
Thread.Sleep(300);
}
}
static void nested_lock()
{
//var rw = new ReaderWriterLockSlim(); runtime exception, need policy
var rw = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
rw.EnterReadLock();
rw.EnterReadLock();
rw.ExitReadLock();
rw.ExitReadLock();
}
Barrier=Rendezvous= Countdown repeatedly
static Barrier _barrier = new Barrier(3);
static Action _act = () => {
for (int i = 0; i < 5; i++)
{
Console.Write(i + " ");
_barrier.SignalAndWait();
}
};
static void setup_rendezvous()
{
// output 000111222333444 so block till all 3 get there
new Thread(()=> { _act(); } );
new Thread(() => { _act(); });
new Thread(() => { _act(); });
}
Tuesday, March 29, 2016
Self host Nuget Server or Gallery and Nuget tools
There are 3-ways to setup Nuget, dir, WebApp, Nuget Gallery Repository== IIS Server or Local Dirtory or Nuget Gallary ASP.Net empty WebApp, install-package nuget.server => ur own nugget local Nuget Galary=nuget.org open source download Runs as WebSite+Sql Express. Nuget Gallery Web Page=NugetExplorer e.g. upload NugetExplorer--Win Desktop App GitHub build need 7.1 SDK, CodePlex has binary Create .nupkg file--NugetExplorer->Content->Meta->Tag- --> Dependency-->need to know ur own Dependencies rest dependencies pickup by Nuget Server NuGet.exe command Line tool--regular cmd not PS1 NugetExplorer--Open->"Nuget Command Line tools"--Download open nupkg to get NugGet.exe saved to c:\tools nugget.exe setApiKey nugget.exe publish mypackage.1.0.0.0.nupkg nuget push m.1.0.nupkg -s http://localhost:4222/ MYKEY PS1 Get-Package -ListAvailable copy package localfeed -updates LinqPad see Nuget Package install-package uninstall-package get-help nuget install-package -examples get-package -filter jq -project prj1
Set up Custom Control for Theme and Prefix AutoGen
Theme vs Skin --- Window build in Aero2.NormalColor.xaml, Classic.Xaml
Skin need code or xaml to load from xmal file to a scope
<Window.Resources>
<ResourceDictionary Source=""></ResourceDictionary>
</Window.Resources>
Style in Custom Control theme must be referenced by Component Res key
This style only applied inside Control never outside so this is not skinning
Classic.xaml/Areo.NormalColor.xaml under Theme dir
<Style TargetType="{x:Type Border}"
x:Key="{ComponentResourceKey
TypeInTargetAssembly={x:Type loc:CustomControl1},
ResourceId={x:Type Border}}">
<Setter Property="Background" Value="Red"></Setter>
</Style>
Generic.xaml use the style by Compo key
<Border Style="{DynamicResource {ComponentResourceKey
TypeInTargetAssembly={x:Type local:CustomControl1},
ResourceId={x:Type Border}}}">
</Border>
Note Microsoft build-in Theme applied to OS elements, Window Chrome, etc, not WPF
so no key needed. Windows theme only affect WPF Aero, Areo2, Classic, Lunar...
Control Panel..Personalization theme change
need Assembly Attribute to locate theme
[assembly:ThemeInfo(
ResourceDictionaryLocation.SourceAssembly, // theme
ResourceDictionaryLocation.SourceAssembly // generic
)]
design drag from Toolbox auto gene prefix
[assembly:XmlnsPrefix("http://jqd/","wpf")]
[assembly: XmlnsDefinition("http://jqd/", "WpfCustomControlLibrary1")]
to avoid verbose Component key use static class
public static class ShareResources {
static ComponentResourceKey _style1Key = new ComponentResourceKey
(typeof(CustomControl1), "style1");
public static ComponentResourceKey Style1Key
{ get { return _style1Key; }}}
<Border Style="{x:Static local:ShareResources.Style1Key}" />
Monday, March 28, 2016
Xaml Triggers and VisualState
DataTemplate/ControlTemplate/Style has triggers
Tigger can do setter, Enter exit action
MultiDataTrigger and MultiTrigger both have .Conditions
StoreBoard.Target=BackGround.(SolidBrush.Color)
This breaks the rule that code should not know xaml view
Xaml can only define Vstate need code behind to move
each state can have storeboard for transition effect
<ControlTemplate.Triggers>
<Trigger Property="" >
<Setter />
</Trigger>
<EventTrigger>
<BeginStoryboard>
<Storyboard></Storyboard>
</BeginStoryboard>
<EventTrigger.EnterActions>
</EventTrigger.EnterActions>
<EventTrigger.ExitActions>
<StopStoryboard>
</StopStoryboard>
</EventTrigger.ExitActions>
</EventTrigger>
<DataTrigger>
<DataTrigger.EnterActions>
<BeginStoryboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<StopStoryboard></StopStoryboard>
</DataTrigger.ExitActions>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<DataTemplate>
<DataTemplate.Triggers>
</DataTemplate.Triggers>
</DataTemplate>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
</MultiDataTrigger.Conditions>
</MultiDataTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
</MultiTrigger.Conditions>
</MultiTrigger>
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Name="PART_border">
<ContentPresenter></ContentPresenter>
<VisualStateManager.VisualStateGroups>
<VisualState Name="normal"/>
<VisualState Name="MouseOver" >
<Storyboard Storyboard.TargetProperty="" Storyboard.TargetName="" />
<VisualState>
</VisualStateManager.VisualStateGroups>
</Border>
VisualStateManager.GoToState(this, "normal", true);
WCF custom control--inject into WPF and MVVM Command System
Execute/CanExecute=ICommand=RoutedCmd
route only, no app code, need to find cmdbinding up VTree
Decoupled Design. DelegateCommand, ApplicationCommand implements ICmd
<Button Command="AppCmds.Cut" /> <Window.CmdBindings> has app code
if(..) e.CanExec=true
<CmdBdg Executed="codebhd_appCode" CanExec="cdbh"/> note Execute(d).
routing without xaml VTree
--- target Binding, could be element outside up-VTree
<Button Command="Cut" CmdTarget={Binding Elem=_tb1}
--- or OnApplyTemplate() inject code
btn.Cliked +=(s,e)=>{ApplicationCommands.Cut.Execute(null,__tb1);}
Custom Routed Command --need to set CmdBinding somewhere
static class CmdHost { static RoutedCmd CustCmd;}
public CustomControl1() {
CommandBindins.Add(new CommandBinding(CmdHost.CustCmd, Exec,CanExec));
now CustCmd is injected into WPF Command System to enable the following:
(1) user <Button Command="CmdHost.CustCmd" Target="_ctl1"/>
(2) user Code Behind {Cmd.Host.CustCmd.Exec("Text RontedCmd(param,tgt");}
(3) ctr itself Exec(sender, e) { _ctl1 is updated here}
Custom Control must Implements ICommandSource to support MVVM
propdep snippet, manual modify ICommandSource
install-package relaycommand bring down source
install-package netfx-system.windows.input.delegatecommand
DelegateCommand have ()=>true for CanExec
[TemplatePart(Name = "PART_BDR", Type = typeof(TextBlock))]
public class CustomControl1 : ContentControl, ICommandSource
{
#region Minimum MVVM support---DP Cmd, OnApplyTemp hookup and
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
Border b = GetTemplateChild("PART_border") as Border;
b.MouseLeftButtonDown += (s, e) => {
if (Command is RoutedCommand) (Command as RoutedCommand).Execute(CommandParameter, CommandTarget);
else Command.Execute(CommandParameter);
};
}
public static readonly DependencyProperty CommandProperty =
DependencyProperty.Register("Command", typeof(ICommand),
typeof(CustomControl1), new PropertyMetadata((ICommand)null,
new PropertyChangedCallback(OnCommandChanged)));
[TypeConverter(typeof(CommandConverter))]
public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
#endregion
#region Only for CanExecute =IsEnabled from VM DelegateCommand
private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var cc = d as CustomControl1;
if(cc!= null)
{
cc.OnCommandChanged((ICommand)e.OldValue,(ICommand)e.NewValue);
}
}
EventHandler _persistHandleToAvoidGC;
protected virtual void OnCommandChanged(ICommand oldCmd, ICommand newCmd) // future extension point
{
HookUnhookCommand(oldCmd, newCmd); //hook/unhook for mem clean up
}
private void HookUnhookCommand(ICommand oldCmd, ICommand newCmd)
{
_persistHandleToAvoidGC = new EventHandler((s,e)=> {
// finnally, disable ctl through cmd.CanExe to VM DelegateCmd ()=>T/F
IsEnabled = Command.CanExecute(CommandParameter);
});
if(newCmd!=null) newCmd.CanExecuteChanged += _persistHandleToAvoidGC;
if (oldCmd != null) oldCmd.CanExecuteChanged -= _persistHandleToAvoidGC;
}
#endregion
#region CmdParam/Target are related to WPF Routed Cmd, not MVVM
public object CommandParameter
{
get { return (object)GetValue(CommandParameterProperty); }
set { SetValue(CommandParameterProperty, value); }
}
public static readonly DependencyProperty CommandParameterProperty =
DependencyProperty.Register("CommandParameter", typeof(object), typeof(CustomControl1), new PropertyMetadata(null));
public IInputElement CommandTarget
{
get { return (IInputElement)GetValue(CommandTargetProperty); }
set { SetValue(CommandTargetProperty, value); }
}
public static readonly DependencyProperty CommandTargetProperty =
DependencyProperty.Register("CommandTarget", typeof(IInputElement), typeof(CustomControl1), new PropertyMetadata(null));
#endregion
}
}
<cc:CustomControl1 Command="{Binding TestCommand}" Background="Transparent" />
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
Loaded += (s,e) => // INPC need to set in loaded
{
TestCommand = new DelegateCommand(() =>
{ MessageBox.Show("Command Triggered"); },
() => false); //true=CanExecute
};
}
public event PropertyChangedEventHandler PropertyChanged;
private DelegateCommand _testCommand;
public DelegateCommand TestCommand
{
get { return _testCommand; }
set
{
_testCommand = value;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("TestCommand"));
}
}
}
Sunday, March 27, 2016
WPF Custom Control RoutedEvent --Inject into Event System
WPF Event system is called RoutedEvent: direct, bubble up, tunnel(preview) How WPF set up its event system everyup the visual tree see Button_click event and set e.Handled=true. to Ignore handled=true, Win Ctor() { AddHandler(Button.ClcikEvent, new RountedEvent(Win_cliked), true) true means ignore e.handled=true anywhere along the tree Use Snippet for Building Custom Routed event C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC#\Snippets\1033\Visual C# and C:\Users\jimmy\Documents\Visual Studio 2015\Code Snippets are snippet storage do not edit them,use VS2015->Tools->Snippt Mgr->add/import from HomeLab16\tools store download copy paste xml in HomeLab16\tools change shortcut in xml to proprevt Snippet will put static RE,add/remove EventMgr,Bubble onto CustomControl proected virtual RaiseMyEventFromCtl() { //to allow future override RaiseEvent(new RountedEventArgs(CustCtl.MyEvent));} Custom eventarg--eventarg additional param delegate void CustRoutedEvenHander(object,CustRoutedEventArgs); class CustRoutedEventArgs: RoutedEventArgs ctor(RoutedEvent re, param):base(re) Usage <myCC CustEvent="" /> // now CustEvt is walk up/down trees
Python Braindump
from math import * import random #Python 3.4.1/IDLE
while int(num)>10: # : is code block
print("too big") # clean code by indentation
num=input()
# code block end by indentation, not space
message="see {0} and {1}" print(message.format(1,random.randint(1,100)))
def load_data(): return "1" load_data() #important (): ()
if: elseif: else: for c in w: while True: break try: except: finally:
list c=["Red",'red',"Blue"] c.sort() reverse() append("R") count("R") len(c)
w="_"*6 for c in w: print(c," ",end="") str_as_list=list("cheese"),lst=[]
for i in range(1,100): "K" in "Kol" "this".[:1] is t .[2:]
tuple vs list () vs [] --multi type c=[1,'cat'] t= 1, "cat" t=(1,"cat")
append,c[0]=2 vs. readonly unpacking i,str=t def get_t(): return i, str
[] help () -- c,i=get_cmd_item_tuple() c=line[:1] i=line[2:] return i,c
if c=="d" and i in c.remove(i) # tuple faster, list more functionality
[],(),{},{:} --list,tuple,set()=unordered list no index, no append but add
dict() key index d[k]=v no add/append, list append not add. d={} is dict not set
s1^s2 s1&s2 s1-s2 s1|s2 len() in works for []/()/{}/{:} in key not val.
del lst[0] del d[0] s={1,2,3,"str"} s.pop() is remove s.remove("str")
class Person: def run(self): p=Person() p.run() def __init__(self,n=""):
self.dynamic_name=n p.dynamic_name="allen" >>p1 mem hex id(p1) int p1 is p2
self._p is private naming convention only raw representation=toString() type(c)
def __repr__(self):return "{0} {1}".format(Person,self.__dict__) __dict__ prop
file structure --- main.py \package1\module1.py must have __init__.py init pkg
import package1 p=package1.module1.Person() # avoid long name???
>>__name__ shows '__main__' print(__name__) shows '__package1.module1__'
from hr import person p=person.Person() # * could be bad practice
from pkg import (m1,m2) from urllib.request import urlopen #symbol=def
from urllib.request import urlopen # one way to avoid long name
with urlopen("http://www.cnn.com") as story: #symbol used without pkg.mod
for line in story:
print(line.decode('utf8'))
def main(): """docstring seen in help def and module level"""
if __name__=="__main__": main() #module used as main.py so define entry point
sys.argv[0] is main.py argv[1] is 1st param #!/usr/bin/env python3 #Shabang
__file__ sys.argv=[sys.argv[0],0] IDLE command line except (ValueError, TypeE):
try: import msvcrt def f(): except: print("import err") alt-3/4 comment
except (ValueErr,ArithmeticError) as e: print(e) print(str(e))
raise pass print("{}".format(str(e)), file=stderr) raise ValueError()
p=os.getcwd() os.chdir(p) os.mkdir("test")
r=@ in C# r"c:\working\bash" list("test it")=>[t,e,s,t,' ',i,t]
str(b'\345')=>b'\\xes" '\345' is UTF-8
network pass along b'data' need .encode/.decode('utf-8')
value vs id=ptr == vs is p==q T same content p is q F bcoz id(p)!=id(q)
optional param eval only once def f(m=[]) m.append('a')=>'aaa' on f(),f()
f(c=ctime) same problem so m=None instead.
LEGB rule --- gc=0 def set() gc=3 not change gc since it is local scope.
def set2() global gc gc=3 will change gc
Help system --- import words dir(words)=>'__fetch_words',... dir(itertools)
everything is object including module and symbols
t=(39) is int t=(39,) is tuple, unpacking (a,(b,c))=(1,(2,3))
p=1,2,3 type(p) shows tuple not []
join vs split ---';'.join(['a','b','c']).split(';')
"x of y of z".partition('of')=>('x','of','y of z') only split by 1st
Python Iterable and Iterator
Python has similar concept to C# IEnumerable/IEnumerator + some RX
Comprehension--[expr(i) for i in iterable] words="words make a sentence."
words.split() {[len(w) for w in words]} {factorial(x) for x in range(10)}
d={k:v} d_flip={v:k for k:v in d.items()} from math import factorial
from pprint import pprint as p p(d_flip) [w[0]:x for w in words]
{os.path.readpath(p):os.stat(p).st_size for p in glob.glob("*.py")
sum(x for x in range(1,step=1,stop_exclusive=100) if is_prime(x))
itr=itr(itra) itm =next(itr) def first(itra): return next(itr(itra))
except StopIteration return None is null first(set()) empty set None
def first(itra): try: return next(iter(itra)) return None
from math import sqrt
def is_prime(x):
if x<2:
return False
for i in range(2,int(sqrt(x))+2):
if x%i==0:
return False
return True
from itertools import islice, count
p1000=[x for x in range(1,1000) if is_prime(x)]
i=list(islice(p1000,5,10)) # islice=skip+take
count() infinite lazy squence, carefuly with memory overrun by [
slice_inf= islice((x for x in count() if is_prime(x)),1000) # (tuple is fine
from pprint import pprint as pp does not help need list to convert
print(list(slice_inf)) print(sum(slice_inf)) #fast efficient
print(any(is_prime(x) for x in range(4495,4500)))
print(all( n==n.title() for n in ["London","Boston Ma"])) # ma=false
from itertools import chain
s1=[1,2,3,4] s2=[5,6,7,8] print(list(zip(s1,s2,s1)))
print(list(chain(s1,s2,s1))) print(list([ max(x) for x in zip(s1,s2,s1)]))
Fibonacci in Python -- lazy yield does not cost memory
def f(x):
yield 1
t1=1
t2=1
while t1<x:
yield t1
t1,t2=t1+t2,t1
Generator = Lazy comprehension Iterable
g=(x for x in range(1000)) #must use () not [] as tuple RO efficient
l=list(g) realization cost 45M Ram list(g) is single use list(g) empty after
sum( x for x in ...) low memory cost re-run l=list(g) is not emoty cost Ram
statefull generator -- counter or seen=set() in memory durin iteration.
def take(n,itra) counter=0 for .. if(counter==n) return counter++ yield itm
def distinct(itra) seen=set() for .. if i in seen: continue yield itm seen.add(itm)
pipeline for i in take(10,distinc(itra)
Bash Fibonacci Recursive Function
echo works better than return + $?, since consecutive call f $(( a-1 )), f $(( a-2 )) does not set $? correctly.
the result of echo can be captured at the final step.
#!/bin/bash
f(){
if [[ $1 -eq 1 || $1 -eq 2 ]]; then # int differ from string compare
echo 1
else
a=$1
echo $(( $( f $((a-1)) ) + $( f $(( a-2))) )) # $(( )) substitute var/func
fi
}
f $1
v=$(( $(f $1) )) # capture echo into variable
echo $v
Saturday, March 26, 2016
Linux Posix Thread
Using Eclipse Mars C/C++ IDE project,
properties->Setting->C/C++->Linker->Libraries->pthread will set linker option -pthread
pthread_create, _join _mutext_lock _unlock _t _exit, clean C-code
#include <pthread.h>
#include <stdio.h>
extern void safe_api();
static int shared;
void *func(void *arg)
{
printf("p thread executing...%\s\n",(char*)arg);
pthread_exit((void*) 99);
}
// properties settings c/c++ linker libraries add pthread -pthread
int main()
{
pthread_t h,t1,t2;
int exit_code;
pthread_create(&h, NULL, func, "pass in data");
printf("%s","main thread\n");
pthread_join(h, (void **)&exit_code);
//getchar();
pthread_create(&t1,NULL,safe_api,NULL);
pthread_create(&t2,NULL,safe_api,NULL);
pthread_join(t1,NULL);
pthread_join(t2,NULL);
printf("%d",shared);
}
pthread_mutex_t lock;
void safe_api()
{
int i;
//for(i =0;i<1000000;i++) shared++; no local fine
for(i =0;i<1000000;i++)
{
pthread_mutex_lock(&lock);
int a=shared;
a++; // thread local ++ need mutex
shared=a;
pthread_mutex_unlock(&lock);
}
}
Friday, March 25, 2016
WPF Custom Control TemplateBinding and Property Declaration
Custom Ctl has a cs and template xaml, no code behind setup.
Need to think about Inject into Trigger system,2-way DataBinding
mark arbitrary child attachment for layout/count etc.
// TemplateBinding does not support two way, generic.xaml
so have to use full binding point to TemplateParent
Text={TemplateBinding Text} //control.Text is DP
Text ={Binding Text RelateiveSource={RelativeSource TemplateParent}}
[TemplatePart(Name="PART_TB",Type=typeof(TB)] // for designer only
TB {get;set { TextChaned-=; TextChange+= avoid Memory Leak}
OnApplyTemplate() {
TB=GetTemplateChild("PART_TB") as TB //connect generic.xaml <TB/> to prop TB
}
UIElem->FrameworkElem->Control->ContentCtl->HeadedCC->ItemCtl->Selector->RangeBase->Buildin
propdepreg propa
http://www.midity.com/content/visual-studio-2010-dependencyproperty-code-snippets-0
[Framework]PropertyMetaData([FrameworkPropertyMetaDataOptions.BindingTwoWayByDefault],
new PropDataChgedCB(cb), new CoerceValueCB(cb) //coerce ctl.val, VM no chg even twoWay
Read Only Dependency Prop, private set by key, inject into trigger system
generic.xaml <ControlTemplate.Triggers><Trigger prop="Going" value=true>
<Setter prop="Background" Value="Green" />
private static readonly DPKey GoingDPkey=DP.RegisterRO("Going",...);
public static readonly DP GoingDP=GoingDPKey.DependencyProperty; // connect key to DPval
public bool Going {return (bool) GetValue(GoingDP);} // regular prop
// private set using key.
OnApplyTemplate() { GetTemplateChild("PART_btn").Click+=(s,e)=>{SetValue(GoingDPKey,true);
Custom ctl uses attached Prop set on runtime child to count etc.
public static readonly DP RowProperty=DP.RegisterAttached("Row", ...);
public static int GetRow(DependencyObject obj) {return obj.GetVal(RowProp);}
public static void SetRow(DependencyObject obj,int i){obj.SetVal(RowProp,i);}
OnInit() { foreach(c in Panel.Children) {c.GetRow(c);...} // unknown content ahead of time.}
How generic Generic.xaml can be --- restricted by Presenter type only
<ControlTemplate><ContentPresenter> or <ItemPresenter> <BuildInCtrl />
then any other control injected by user of this control.
<ContentPresenter/> require CC: ContentControl --<cc>anything</cc>
<ItemsPresneter/> require CC: ItemsControl <cc> multi type multi things</cc>
Avoid Collection Type Singleton.
public static RO DP ItemsProp typeof(IList) default=ObsCol=> singleton
should set in ctor, OnApplyTemp, OnInit,
Generic.xaml can be <ListBox ItemsSource="{TemplateBinding Items}"/>
Thursday, March 24, 2016
Connect View.DataContext to ViewModel: Prism vs BlackWidow vs. Fusion
BlackWidow requires Presenter<V,VM> follows naming convention to set DataCtx=VM. Fusion is
more explicit. Developer can select V,VM and Presenter during Asset Manager Design.
Prism is most explicit in that it either code or config V to VM setup. Since it does not
requires Presenter, View Composition requires register with Region to actually show View
after V DataContext set to VM.
(1) ViewModelLocator --- Wire up using PropertyMetadata Event in DependProp
//just as normal Dependency property declaration
public static readonly DependencyProperty AutoWireViewModelProperty =
DependencyProperty.Register( "AutoWireViewModel", typeof(bool),
typeof(VMLocators), new PropertyMetadata(false,OnChanged));
// then create ViewModel as DataContext
void OnChanged(DependencyObj d,PropChangedEventArgs e) {
var vmType=d.GetType().FullName+"Model" //convention
(FrameworkElement)d.DataContext = Activator.GetInstance(vmType);
}
usage in xmal
<UserControl ... local:VMLocators.AutoWireViewModel="true" >
(2) IVew/IViewModel -- Wire up through code-behind and DepInj
Base Inteface
IView { IVM ViewModel {get;set;} IViewModel {IV View {get;set;}}
Derived Interface same code to allow Container.Reg different type
IMWView : IView IMWViewModel : IViewModel
Implementation
MWView :IMWView {
public IViewModel { get { return (IMWViewModel) DataContext;}
set { DataContext =value;} }
MWViewModel: IMWViewModel {
public IView { get;set;}
ctor(IMWView view) {View=view;
View.ViewModel=this;//DI for VM ctor vs. Code-Behind V ctor
(3) IVew/IViewModel --- Wire up Code-Behind,View 1st
Same as (2), except VM not see V, do not DI View into VM.
(4) IModule:Init() --- Trigger DI and View Composition
_container.RegisterType(IMWView,MWView);
_container.RegisterType(IMWViewModel,MWViewModel);
_container.RegisterType(ToolbarView);
_container.RegisterType(MWView);
// replacing BlackWidow Activity by Discovery, Add in code anywhere
_regMgr.RegisterViewWithRegion("mwReg",typeof(MWView));//Discovery
vm1=_container.Resolve<IMWView>();
vm2=_container.Resolve<IMWView>(); // more than 1 instance of VM.
_regMgr.Regions["mwReg"].Add(vm1.View);
IRegion r=_regMrg.Regions[""];
r.Deactive(vm1.View); // swap out views
regs.Add(vm2.View);
(5) MEF Wire-up
Mef takes away the need to do Container.Reg/ResolveType so Add looks like
_regMgr.Regions["mwReg"].Add(ServiceLocator.Current.GetInstanace<MWView>());
[Export] public class MWView : UserControl //DI by attribute
ServiceLocator.Current.GetInstanace<MWView> triggers [Export]
[ModuleExport(typeof(ModuleAModule),WhenAvailable)]
public class ModuleAModule: IModule {
[ImportingConstructor] ctor(IRegionManager) //MEF DI Mgr
IModule:Init() similar for _regMgr.Region[""].Add or IRegion.Add Deactivate
But need to use ServiceLocator.Current.GetInstanace<T> in ServiceLocator.dll
[Export][ModuleExport][ImportingCtor] are in System.CompMod.Composition
Wednesday, March 23, 2016
Prism bootstraping Shell, Module, Region vs Fusion Bootstrapping Designer
Prism has multiple extension points for injecting Shell, Module, Region with boiler template code. Fusion replaces these with Bootstrapper Extension where pre-configure shell pop up. It can bootstrap anything including module and regions through GUI asset manager instead of code. It also abstract away Data pipe as Data Provider, though data still can come through directly such as in Raw Socket Connection. But it only makes sense if avoid Data Provider pattern has other benefit such as performance or less code
Here is how Prism bootstrap
Shell: <ContentControl prism:RegionManager.RegionName="R1" />
Module: (Separate Wpf User Control Project since classLib gives winForm ctx menu)
public class ModuleA : IModule::Init(){ _regMgr.RegisterViewWithRegion("R1",typeof(MWView));}
Unity/MefBootstrapper:
(0) OnStartup() {
bootstrapper.Run();
//System.ComponentModelComposition.dll,MSMefExtension.dll, unity.dll
}
(1) CreateShell()
{
return Container.Resolve<Shell>();
return Container.GetExportedValue<Shell>(); }
[Export] public partial class Shell : Window
(2) InitializeShell()
{
App.Current.MainWindow=(Window) Shell;
App.Current.MainWindow.Show();
}
(3) ConfigureAggregateCalalog --- MEF Bootstrapping
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(Bootstrapper).Assembly));
(4) ConfigureModuleCatalog --load module in code
{
Type t=typeof(ModuleAModule) // add ref to solution;
ModuleCatalog.AddModule( new ModuleInfo(){ ModuleName=t.Name,
ModuleType=t.AssemblyQualifiedName,
InitializatinMod=WhenAvailable});
}
(5) ConfigureAggregateCalalog --load MEF module in code as MEF in (3)
AggregateCatalog.Catalogs.Add(typeof(ModuleAModule).Assembly);
[ModuleExport(typeof(ModuleAModule),InitializationMode=] public ModuleAModule
(6) CreateModuleCatalog --- load by directory
return new DirectoryModuleCatalog() {ModulePath=".\Modules"};
[Moldule(ModuleName="ModuleA",OnDemand=True)] public ModuleAModule
(7) CreateModuleCatalog --- load by resource.xaml
return ModuleCatalog.CreateFromXaml(new Uri("/Proj;component/m.xaml",UriKind.Abs));
<Modulelarity:ModuleCatalog>
<Modularity:ModuleInfo Ref="file://ModuleA.dll" ModuleName="ModuleA"
ModuleType="ModuleA.ModuleAModule, ModuleA, Version=1.0.0.0" InitMod />
(8) CreateModuleCatalog --- load by app.config
return new ConfigurationModuleCatalog();
<configSections> <section name="modules" type=ModulesConfigurationSection />
<modules>
<module assemblyFile="Modules/ModuleA.dll" moduleName="ModuleA"
moduleType="ModuleA.ModuleAModule, ModuleA, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null" startupLoaded="true" />
(9) IModule.Initialize() --- all your code Reg type, sub services, compose view
Sunday, March 20, 2016
Performance Diagnostics Starting Points
(1) vm map, CLR profiler, Process Explorer
--- Managed vs Native Heap, Image loaded
--- related to WPA for stack,private, page,mapped
--- vmmap is static shot, profiler is compare of multi
--- procexp tell what DLL get loaded, really needed?
(2) VSP CPU sampling and instrumentation Profiler
--- chase donw hotpath to high % function calls.
--- .net Memory analysis, profile=muti dump over time
--- low cpu=>blocking, high cpu=>element count>>1
(3) Concurrency Visualizer
--- Main Thread UI Proccing %
--- check % for execution, synchronization, Memory mgt
(4) DotTrace
---Thread tree chase specific calls
--- Hot spot may tell where to look.
--- if you know where to look, then drill into code.
(5) WPF Performance Suite Win7 and patch for Win10
--- Perforator, FR, DRAR, LiveView, Large Element Count
---- Render %, low FR+Hight HW IRT (buf-compose)=>problem
--- Win7 SDK or download with patch for Win10
https://www.microsoft.com/en-us/download/confirmation.aspx?id=21042
http://download.microsoft.com/download/A/6/A/A6AC035D-DA3F-4F0C-ADA4-37C8E5D34E3D/setup/WinSDKPerformanceToolKit_amd64/wpt_x64.msi
(6) WPA Win7,8,10, gpedit.msc sid in secSettings\UsrRights\Profile Perf
--- Win8+ ADK
--- Thread events (CtxSW, 3 states Ready, Running Waiting), Thread Starvation
--- Thread Stack Column, Chase down UI Delay->CPU precise, blocking func calls
--- Resident Set Analysis, MMList(active/modified),
---- Heap/VirtualAlloc usage, Commit Stack (load symbol)
--- Heap trace need to set single process trace: reg add HKLM/Software/Microsoft/
Windows NT/CurrentVersion/Image File Execution Options/myapp.exe
/v TracingFlags /t REG_DWORD /d 1 /f
reg delete HKLM/...... /v TracingFlags /f
(7) command line tool wpr, xperf
--- where wpr, wpr -provider ,-profiles -help -start CPU.verbose.memory
--- where xperf ... could be already on user PC.
Saturday, March 19, 2016
Linux C Tcp Server with Windows Python and C# client
telnet localhost 107 ^] =exit (as tcp client)
pgrep -a my_tcp_sever pgrep my_tcp_server | xargs kill
Tool Eclipse Mars C/C++
fork() allow concurrent processing of client socket.
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <netdb.h>
#define SERVER_PORT 1067
struct sockaddr_in create_server()
{
struct sockaddr_in server;
server.sin_family=AF_INET;
server.sin_addr.s_addr=htonl(INADDR_ANY);
server.sin_port=htons(SERVER_PORT);
return server;
}
void pass_through(int in, int out)
{
unsigned char buf[1024];
int c;
while( (c=read(in,buf,1024))>0)
{
write(out,buf,c);
}
}
int main(void) {
struct sockaddr_in svr=create_server();
int sok=socket(AF_INET,SOCK_STREAM,0);
bind(sok,(struct sockaddr *)&svr,sizeof(svr));
listen(sok,5); // Listener queue
puts("Listening at 1067.45..\n");
struct sockaddr_in clnt;
int clnt_len;
int fd; //connection descriptor
while(1)
{
clnt_len= sizeof(clnt);
fd=accept(sok,(struct sockaddr *)&clnt,&clnt_len);
puts("client connected\n");
if(fork()==0){
close(sok); // inside child process.
pass_through(fd,fd);
}
else
close(fd);// limit # descriptor can run out, close it
}
return EXIT_SUCCESS;
}
a simple python client
#!/usr/bin/python3
from socket import *
s=socket(family=AF_INET,type=SOCK_STREAM);
s.connect(("192.168.0.108",1067));
m="this is a test"
s.send(m.encode('ascii'));
resp=s.recv(len(m))
print(resp.decode())
s.close()
C# client
TcpClient tcp_c = new TcpClient();
tcp_c.Connect("192.168.0.108", 1067);
string msg = "message from C#";
tcp_c.Client.Send(Encoding.ASCII.GetBytes(msg));
byte[] buf = new byte[msg.Length];
tcp_c.Client.Receive(buf);
Console.WriteLine(Encoding.ASCII.GetString(buf));
Useful netowork command and tools
ipconfig /displaydns w/o real dns for local development: any.localdev.us. nslookup any.localdev.us nslookup > set type=NS/MX/CNAME/AAAA (Ipv6 A-Record lookup) pathping tracert microsoft.com https://www.whatismyip.com/ NAT rout to different PC by port telnet cnn.com 80 netstat -ano nslookup set type=mx telnet ..._smtp..gmail 25 nmap -v cnn.com port scan open source WireShark, Fiddler lsof -i TCP sudo lsof -i |grep tftp wget cnn.com telnet localhost 107 ^] =exit (as tcp client) pgrep -a my_tcp_sever pgrep my_tcp_server | xargs kill
Thursday, March 17, 2016
REST Backing store for user setting configuration persistence
Microsoft Pattern and Practice Configuration Block has been removed because Everyone can easily roll their own.
So here are some basics of Configuration interface with REST backing store.
(1) REST backstorage has very standard CRUD interface
public class RESTConfigurationStorage : IConfigurationStorage
{
public void Save(IConfigurationSectionHandlerForStorage configurationSection, string configurationId)
public bool Load(IConfigurationSectionHandlerForStorage configurationSection, string configurationId)
public void Create(IConfigurationSectionHandlerForStorage configurationSection, string configurationId, string parent)
public void Delete(IConfigurationSectionHandlerForStorage configurationSection, string configurationId)
}
Note that there are interfaces for CRUD, Sectional data. While this follows original config xml format. the
real purpose is to allow C# code get/set user configuration data methodically.
public interface IConfigurationStorage
{
void Save(IConfigurationSectionHandlerForStorage configurationSection, string configurationId);
bool Load(IConfigurationSectionHandlerForStorage configurationSection, string configurationId);
void Create(IConfigurationSectionHandlerForStorage configurationSection, string configurationId, string parent);
void Delete(IConfigurationSectionHandlerForStorage configurationSection, string configurationId);
}
public interface IConfigurationSectionHandlerForStorage : IConfigurationSection, INotifyPropertyChanged
{
IEnumerable<string> ValuePropertyNames { get; }
IEnumerable<string> DefaultPropertyNames { get; }
void SetDefaultValue(string propertyName, string value);
string GetDefinedDefaultValue(string propertyName);
void RemoveDefaultValue(string propertyName);
void ResetDirtyFlag();
}
public interface IConfigurationSection : INotifyPropertyChanged
{
string this[string propertyName] { get; set; }
IEnumerable<string> PropertyNames { get; }
IEnumerable<string> ValuePropertyNames { get; }
event Action DirtyFlagChanged;
event Action PropertyDirtyFlagChanged;
T GetProperty<T>(string propertyName);
T GetProperty<T>(string propertyName, T defaultValue);
void SetProperty<T>(string propertyName, T value);
IConfigurationSection SubConfiguration(string prefix); // sub is recursive
}
(2) in a WPF app, Unity Container makes a user setting singleton and allow every VM or presenter
get and set user level setting all the way to the backing store.
Note the real implementation is to Save using REST calls serializing all changes on UserSetting object
public interface IConfiguration : IConfigurationSection, INotifyPropertyChanged
{
void SaveConfigurationSection(string configurationSectionId);
void SaveAllConfigurationSections(); // this will go to Storage services
}
private UnityContainer _container;
IConfigurationSection s;
var userSettings = _container.Resolve<IUserSettings>();
// All storage actions are related to UserSetting persistence.
var config = new ConfigurationService(userSettings, "Core");
// So all Section are refering to UserSetting in DI.
_container.RegisterInstance<IConfigurationSection>(userSettings.Configuration);
_container.RegisterInstance<IConfigurationService>(config);
// Have a name will allow different instance of the same services.
_container.RegisterInstance<IConfigurationService>("Core", config);
}
}
public interface IConfigurationService
{
}
public class ConfigurationService : IConfigurationService
{
private readonly IConfiguration _configuration;
public ConfigurationService(IUserSettings userSettings, String propertyNamespace)
{
}
public void Save()
{
_configuration.SaveConfigurationSection("id");
}
}
Wednesday, March 16, 2016
.Net Stream Programming
.Net view programmable data byte[] as follows:
(1) Backing store, disk, memory, network socket
(2) Base Stream: FileStream, MemoryStream, NetworkStream, IO.PipeStream
(3) Decorator Stream: GZipStream, CryptoStream, BufferedStream, DeflateStream
(4) StreamAdaptor: Stream, Binary, Xml Reader/Write,
(5) Network Stream does not support Buffering, so a decorator BufferStream is used to wrap around it to support streaming -- typical Decorator Pattern.
As conformed to general networking concept, it provide pipes, chunking, Request/Response pattern
and raw network socket.
var server = new NamedPipeServerStream("p1");
var client = new NamedPipeClientStream("p1");
server.WaitForConnection(); // wait a client to connect.
server.WriteByte(123);
client.Connect();
client.ReadByte();
var svr = new NamedPipeServerStream("p1", PipeDirection.InOut, 1, PipeTransmissionMode.Message);
var clnt = new NamedPipeClientStream("p1");
clnt.Connect();
var msg = Encoding.UTF8.GetBytes("This is a message");
clnt.Write(msg, 0, msg.Length);
// server side chunk reading
StringBuilder sb = new StringBuilder();
var buf2 = new byte[10];
svr.WaitForConnection();
do
{
svr.Read(buf2, 0, buf2.Length);
sb.Append(Encoding.UTF8.GetString(buf2));
Array.Clear(buf2, 0, buf2.Length);
} while (!svr.IsMessageComplete);
// Web Stream
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://myserver/1.aspx");
var requestStream = request.GetRequestStream();
requestStream.Write((new ASCIIEncoding).GetBytes("param1=greeting&p2=9"));
requestStream.Close();
var response=request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
//Web Client cannot read bc have to close stream to send
WebClient wClnt = new WebClient();
wClnt.OpenWrite("http://..").Close(); // has to call close to send
Stream s3 = wClnt.OpenRead("http://.."); // must re-open to get response
Tcp Raw Socket -- Cannot support UDP since it is not Connection Oriented
var ip = IPAddress.Parse("127.0.0.1");
TcpListener listener = new TcpListener(ip, 5200);
listener.Start();
Socket socket=listener.AcceptSocket(); // block
NetworkStream s = new NetworkStream(socket);
int byteRead = 0;
byte[] buf = new byte[100];
do
{
byteRead= s.Read(buf, 0, buf.Length);
} while (byteRead!=0);
socket.Close();
TcpClient client = new TcpClient("localhost", 5200);
NetworkStream ns=client.GetStream();
byte[] data = new byte[100];
(new Random()).NextBytes(data);
ns.Write(data,0,data.Length);
ns.Close();
BufferedStream bs = new BufferedStream(ns);
bs.Write(data, 0, data.Length);
using HttpClient for async streaming
async static void Get()
{
var client = new HttpClient();
var resp = await client.GetAsync("http://www.cnn.com");
resp.EnsureSuccessStatusCode();
Console.Write(await resp.Content.ReadAsStringAsync());
}
Raw byte[] chunk reading
static byte[] ReadBytes(Stream s)
{
var s_threadSafe = Stream.Synchronized(s);
byte[] buf = new byte[s_threadSafe.Length];
int totalRead = 0; //already read.
int chunk = 1;
while (totalRead < s_threadSafe.Length && chunk>0)
{
chunk = s_threadSafe.Read(buf, totalRead,buf.Length-totalRead);
totalRead += chunk;
}
return buf;
}
a few more things about Stream adaptor
--- Wrap streams: new BinaryReader(s); StreamWriter(fs)
--- Need to sync up: StreamWriter.Flush(), StreamReader.DiscardBufferedData();
--- Data Layout match BinaryReader.ReadInt32 vs ReadInt16
--- For SSD, FileOptions.WriteThrough turnoff OS CachManager Caching, but not recommend otherwise.
WPF PropertyMetadata Change Event to inject code
PropertyMetadata has the following signature
new PropertyMetadata (false,OnChanged)
private void OnChanged(DependencyObject d, DependencyPropertyChangedEventArgs e){}
It can be uses to inject code
(1) ViewModelLocator --- Wire up View DataContext to a ViewModel.
//just as normal Dependency property declaration
public static readonly DependencyProperty AutoWireViewModelProperty =
DependencyProperty.Register( "AutoWireViewModel", typeof(bool),
typeof(VMLocators), new PropertyMetadata(false,OnChanged));
// then create ViewModel as DataContext
void OnChanged(DependencyObj d,PropChangedEventArgs e) {
var vmType=d.GetType().FullName+"Model" //convention
(FrameworkElement)d.DataContext = Activator.GetInstance(vmType);
}
usage in xmal
<UserControl ... local:VMLocators.AutoWireViewModel="true" >
(2) Similarly as in (1) Loaded Event can trigger method calls through attached property
Similarly for Attached Property Behavior
<UserControl loc:LoadedMethodName="GetDataAsync" />
var f=d as FrameworkElement;
f.Loaded+=() =>{
MethodInfo mi=d.GetType.GetMethod(e.NewValue);
mi.Invoke(vm,null);
}
ViewModel has async void GetDataAsync() { .. var data= await GetDataAsync(); ...}
(3) WPF regular behavior also has PropertyMeta ChangedEvent and can pop Notification
ShowNotificationMsgBehevior : Behavior
declare dependency property Message as string
void OnMsg(d,e) {
var b=d as ShowNotificationMsgBehevior ;
b.AssocObj.Content=e.NewValue}
<ContentControl>
<interaction.ShowNotificationMsgBehevior Message={Binding NotifMsg} />
Sunday, March 13, 2016
Windows Form bits
Windows Form MVP:
View ---MainForm/UserControl/CustomControl, IMainFormView, IViewWithEventCommand.
IViewWithEventCommand {
public event EventHandler playButtonClicked {add {play.Clicked+=value;} remove}
public string prop1...
public SetCommands([]cmds); }
IMainFormView { public IView1, IView2}
Model/Mgr/Services ---- business Entity,its operations and Services calls to outside data sources.
Presenter --- Ctor MainFormPresenterNoDI(IMainformView) MainFormPresenterDI(IMainFormView, IMod1, IModService1).
CommandBase : ICommand : INPC {Execute(); CanExecute(), CanExecuteChanged}
Boostrapping: Program::Main() { new IView, MainForm, IPresenter, IModel, IService}
PlayView: IView ::SetCommands([]cmds) {INPC--no databinding PropChanged+={ btn.Enabled=cmd.CanExec();}; btn.Click+=cmd.exec
IEventAggregator { Publish(T msg); Subscribe(action); Unsubscribe(action);}
EventAggregator: IEventAggragator singleton, no need for boostraping.
PlayCommand : CommandBase ctor(IPlayer) override Exec() {_player.Play()}
Useful links
http://dockpanelsuite.com/
viperneo.github.io/winforms-newui/
windowsribbon.codeplex.com
explorer style
Infragistics Windows Form control
pinvoke.net
Saturday, March 12, 2016
.Net Braindump
Virtual Memory State:free,reserved, committed. Large Heap object >85K
ephemeral generation =0,1 on ephemeral sequment.
GC phases: Mark Live->Upd ref to compact-> pause/barrierr on ->Compact/Collect unreachable->barrier off.
Freachable Queue, GCSettings tweaks LargeObj compaction/Latency/IsServerGC Mode.
GCSettings.LantencyModel=batch/interactive Server/UI, LowLatency/Sustained --short 0,1 only, long blowup
Server vs. Workstation GC: Non-Concurrent/Background vs. Concurrent/Non-Concurent <gcServer/> <gcConcurrent/> muti-gc-thread high priority vs. gc=user thread
Server GC -- 1 thread+heap per core, larget ctx switching, use wkgc+non-concurent for 100+ inst of a single app
Concurrent gc only for gen2. Fx 4.5 Background replaced concurrent and not restrict alloc during GC.
wkstn vs Svr GC--User Thread GC vs 1 per core GC,Fx4.0+ Background GC even one-core, single core
can be concurrent by swithing
C++/CLI Windows Form int APIENTRY WinMain(HINSTANCE,HINST,LPSTR,in) { Application::Run(gcnew Form1());
WF Databinding: PriceTB.DataBinding.Add("Text,_QuoteList,"Price") CurrencyManager, IList, IBindingList
BindingSource.DataSource=_QuoteList, LB.DataSource=BindingSource, bs.Add() .Remove().
C++/CLI ViewModel INPC Hand coded.
TaskScheduler.FromCurrentSyncCtx, Task work-stealing all core buzy.
lambda= class displayClass1 :: b_0(), delegate(b_0) Closure=lambda+public envVar passByRef Shared not stack e.g Window <>4__this
TaskClosure pass env by param since it will run later: Task.Factory.StartNew((env)=>{},state:env);
TaskCompletionSource ctor(IAsyncResult) gone TCS<T> wraps Task with Workflow in Continuation(set result/Cxl/Excp).
So should have been called TaskContinuationSource.
All blocks: t.Result, Task.WaitAny/All vs Task.WhenAny/All
TaskException=AggregateException .InnerException (list, ae.Flatten()), CLR ret-throw ae
Handling ae coding pattern: WaitAll(); if(t.Excption !=null) or try-catch.
App start catch-all: TaskScheduler.UnObservedTaskException+=EventH<Unobs..EventArgs>(H(){e.SetObserved(););
CancellationTokenSource cts, cts.Token token.ThrowIfCxlRequested, need task-excep code(OperationCxlExcp)
ParentTask.StartNew( child(TaskCreationOptions.AttachToParent);
Parallel.For(0,10,(i)=>{}) (0,10,()=>threadlocal init,(i,lstate,accuRet)=>{}, (fin)=>interlock(ref g,fin)
.For(item, (i,loopState)=>ls.Stop/Break all itr)
pie=Enumerable.Range().AsParallel.Select(), pie.ForAll(),pie1.Unordered.Join(pie2,p2=>p2.id,p1=>p1.id, {p1,p2}).
pie.Aggregate(0, (subTot)=>{thread local},(total)=>,(avg)=>{calc})
Parallel.Invokve(Action[]);
AsyncCache: await Task this[..] indexer cache miss=> Task.Run Get awaitable
Timeout: await .WhenAny(Donwload, Task.Delay(3000ms);
TaskWhenAll: await .TakeWhenAll( from addr in mailinglist select Blast(addr))
Task.FromResult(42): await GetCache(out v)?Task.FromResult(v=42):GetAfterCachMiss();
AsyncProducerConsumer: DataQueue+TCSQueue, Take() .FromRes(DataQ.DeQ) or TCS.EnQ return TCS.Task; Add() DataQ.EnQ, TCS.DeQ.SetResult.
RetryOnFault(10) for10 try catch await F().ConfigureAwait(false);
NeedOnlyOnce data=await .WhenAny([]F(cxlTok)); cxlTok.Cancel(); return data.
WhenAllOr1stException([]tasks) --CountDownEvtn(tasks.Count) ContinueWith( if(ce.Signal()) tcs.SetResult if Excep) tcs.setExcp, return tcs.Task;(cannot .WhenAll since run through all)
[CallerMemberName] INPC
statuc void NotifyPropChanged(this PropertyChangedEventHandler h, object s,[CallerMemberName] string m=null)
{
h(s,new PropertyChangeEventArgs(m);}
INPC PropertyChange.NotifyPropChanged(this)
INPC PropertyChange.NotifyPropChanged(this,nameof(Age));
order?.customer?.name=="jon"
using static Math;
public Age => DateTime.Now -BirthDate;
public DOB { get;}=new DateTime(2000,1,1)
Clicked?.Invoke(this,Empty); EvnH check null
$"{Age} birday comming";
3 pattern of WhenAny
Throttling reduTaskList, while(count>0) t=await WhenAny(), remove t, add from OrgList to reduList
Early Bail out of one task: pass in Cxltok ct tcs=new TCS(false) using(ct.Register(tcs.Set(true)) await WhenAny(task,tcs.task).
Redundancy: 1stTask=await WhenAny([]), cxlToken.Cxl;
Rx Rundown
Package Rx-Main, Rx-WPF( bring in System.Reactive.Windows.Threading)
Scheduler.Immediate(immediate sync no queue run) .CurrentThread (Queued, single threaded), .NewThread(many thread), DispatcherSheduler,
new EventLoopScheduler() (a separate thread as Message Pump, not static property as above)
Thread/TaskPoolScheduler=TaskScheduler.Default (Two background Thread, all other are foreground)
SubscribeOn/ObserveOn(scheduler.NewThread)--SubOn ignore NewThread or ThreadPool just pick one,ObseOn multi and in order.
WPF/WF SubscribeOn to compute, ObserveOn to update
Setup
var q = from t in Enumerable.Range(1, 100) select t;
//Queryable -- need observer
var qab = q.AsQueryable(); //System.Linq.IQueryable
qab.Subscribe(Observer.Create(F), Scheduler.Immediate);
//Observable
var obs=q.ToObservable(Scheduler.ThreadPool); //System.IObservable.
obs.ObserveOn(DispatcherSynchronizationContext.Current).Subscribe(F); // SyncCtx obsolete
Observable.Interval(TimeSpan.FromSeconds(1)).Subscribe(i=>F((int)i));
// ObservableExtensions in Core.dll, DispatcherScheduler in Rx-Wpf System.Reactive.Windows.Threading
obs.ObserveOn(DispatcherScheduler.Current).Subscribe(F);
Observable.Using(()=>new StreamReader(""), sr => sr.ReadToEnd().ToObservable());// using need input IDis
Observable.Using(() => Disposable.Empty, d => Observable.Range(1, 100)); //fool using by Empty
.Finally(()=>{manualresetevent.Set()}; //block to join bg threadpool. .Catch .Timer .TimeStamp .Timeout need Catch, .Throttle=Buffer
.Scan=Aggregate but running finite .Buffer vs. .Window= List vs.IObs .Do .Count() is obs
Observable.Create(obsr => { obsr.OnNext(1); obsr.OnCompleted(); return () => { }; }); //Disposable.Empty
Observable.Generate(init,state=>CanContinue,MoveNext,Getresult,ThreadPool,
.Publish .Connect .RefCount() hot-cold-auto cleanup.
.FromEventPatten(=>Ink,prop.Changed+=h,Changed=-h).Where(..)
Observable.FromAsyncPattern(sm.BeginRead, sm.EndRead); obsolete use await task
Rx Hot/Cold obs--- Interval is cold since each sub see different, But if Obs.publish then all sub
next Obs.connect will have all Subs see same data => hot
Friday, March 11, 2016
TAP interleaving using build-in WhenAny and Custom TCS Combinator
For a list of Task, Interleaving is to have a list of data independent Task workflow.
(1) await always block when doing Task.Run. So there are no interleaving here.
(2) This changed in a Parallel loop, since await is threadlocal so won't block all of the task.
(3) TCS Parallel is similar to WhenAny in term of error rate and speed. So Parallel is not effective
on repeated 7 url to 392. It seems interleaving does not optimize or could be too much network contention.
class InterLeaving
{
public int _processingCount;
static IEnumerable<Task<T>> Interleaved<T>(IEnumerable<Task<T>> tasks)
{
var inputTasks = tasks.ToList();
var sources = (from _ in Enumerable.Range(0, inputTasks.Count)
select new TaskCompletionSource<T>()).ToList();
int nextTaskIndex = -1;
foreach (var inputTask in inputTasks)
{
inputTask.ContinueWith(completed =>
{
var source = sources[Interlocked.Increment(ref nextTaskIndex)];
if (completed.IsFaulted)
source.TrySetException(completed.Exception.InnerExceptions);
else if (completed.IsCanceled)
source.TrySetCanceled();
else
source.TrySetResult(completed.Result);
}, CancellationToken.None,
TaskContinuationOptions.ExecuteSynchronously,
TaskScheduler.Default);
}
return from source in sources
select source.Task;
}
public void TCSCombinator()
{
var urls = GetListOfUrl();
List<Task<string>> taskList = (from _ in urls select WebDownloadDataTask(_)).ToList();
// TCS interleaved by running in parallel
Interleaved(taskList).AsParallel().ForAll(async t => // async lambda
{
string s = await t;
Console.WriteLine("Interleaved in Parallel: " + s.Length);
if(s!="") Interlocked.Increment(ref _processingCount);
});
//foreach( var t in Interleaved(taskList))
//{
// string s = await t; // await will block include foreach
// Console.WriteLine("Interleaved: "+s.Length);
//}
Console.WriteLine("TCS Combinator Totally {0} processed", _processingCount);
}
public async void WhenAny()
{
var urls = GetListOfUrl();
List<Task<string>> taskList = (from _ in urls select WebDownloadDataTask(_)).ToList();
// sequencial interleaving since await block while-loop
while (taskList.Count > 0)
{
// WhenAny return Task<CompletedTask>>
Task<string> t = await Task.WhenAny(taskList); // await trigger Task.Run then blocks while-loop
taskList.Remove(t);
string s = await t; // this is the same as t.Result, just to make sure to complete
Console.WriteLine("WhenAny interleaving process data: " + s.Length);
if (t.Result != "") _processingCount++;
}
Console.WriteLine("WhenAny Totally {0} processed", _processingCount);
}
private Task<string> WebDownloadDataTask(string u)
{
return Task<string>.Run(() =>
{
string data = "";
try
{
WebClient wc = new WebClient();
data = wc.DownloadString(u);
Console.WriteLine("Done: " + u);
}
catch
{
Console.WriteLine("Exception");
}
return data;
});
}
List<string> GetListOfUrl()
{
var list = new List<string>
{
"http://www.cnn.com",
"http://www.yahoo.com",
"http://www.nfl.com",
"http://www.nba.com",
"http://www.cnbc.com",
"http://www.msdn.com",
"http://www.usatoday.com"
};
return list.Concat(list).Concat(list).Concat(list).Concat(list).Concat(list).
...
Concat(list).Concat(list).Concat(list).Concat(list).Concat(list).ToList();
}
}
Thursday, March 10, 2016
Running UI on difference dispatcher thread
It may be beneficial to run separate UI event loop, though performance testing is warranted.
private void button_Click(object sender, RoutedEventArgs e)
{
this.Title = System.Threading.Thread.CurrentThread.ManagedThreadId.ToString();
Thread th = new Thread(new ThreadStart(show));
th.SetApartmentState(ApartmentState.STA);
th.IsBackground = false; // true=> just a child window
th.Start();
}
void show()
{
Window1 w = new Window1() { Title = System.Threading.Thread.CurrentThread.ManagedThreadId.ToString() };
w.Show();
w.Closed += (sender2, e2) =>
w.Dispatcher.InvokeShutdown();
System.Windows.Threading.Dispatcher.Run(); //must run to start event-loop
}
Wednesday, March 9, 2016
C# blast
winddbg
sxe ld clrjit .loadby sos clr !procinfo g go k call stack native !name2ee memory structure EEClass method table
!dumpmd !U decompile k=native stack !dumpil [addr] !clrstack ~0s main thread !dumpheap -stat
!dumpheap -stat System.Byte[] !do [address] !dumpmt -md (object vs. method table)
!dso stack obj (.prefer_dml 1 will has link to address ease of use) k native callstack !dumheap -type DynamicMethod
F6, Break !gcroot [addr] who is holding obj
compiler tools
dumpbin corflags.exe csc /platform:x86 => corflags corflags /32bitreq- or + will change exe header
csc /r:a=foo.dll /o+ (extern alias a; a::foo.f();)
ildasm/ilspy
ldstr,ldarg.0,stloc.0, ldfld, brtrue.s,callvirt, switch,<>_dictionary,WinForm/WPF single thread custom add/Remove event no CAS,
cache delegate inst vs c_displayClass for closure, heap allocated. lambda/expression tree delegate instance caching
expression node: const, lambdad node, BiOp node param shared ref vs. delegate instance Func'1<int32>
f.Compile() will jit efficient code home-iconicity Enumerable<T> vs Queryable<T>, Delegate vs. Expression tree, Where/Select.
ldtoken method valuetype refelection getMethodFromHandle
Language Extension
dynamic d=new myD() class myD : DynamincObject {override bool TrySetMemeber(binder,v){_dict[binder.Name]=v;
git.com has AsDynamic make obj dynamic by reflection
await button1 public static class Ext { btnAwaiter GetAwaiter(this Buttont b) { return new btnAwaiter(btn);}}
class btnAwaiter :INotifyCompletion { IsCompleted; GetResult(); OnCompleted(action continuation) {_b.Clicked=continuation();}
IAsyncStateMachine.MoveNext(), Stack Splilling, f1()+f2()+ await f3(), need to have splill stack onto heap due to await
List<T> !dumpheap -stat
!dumpmt -md [addr] will trck down List'1[int32, string, reftype] !dumpclass [EEClass Addr] __Cannon ref type
u clr!ArrayStoreCheck native stack covariant check start from clr code clr!... covariant IRead<out T> contravariant IWrite<in T>
C++ Export Symbol
win32 C++ project check export sysmbol will InterOp COM, csc /link=Embed InterOp, PIA as dev tool, no pIA deployment.
[DllImport("test.dll",CallingConvention=Cdecl)] public static extern "C" void com_function() convention= Stack mgt.
safe pointer:
struct IntPtr { unsafe void* m_value} struct TypeReference { IntPtr type; IntPtr value;}
int x=42; TypeReference r=__makeref(x); //local only, cannot return __refvalue(r,int)= -42; Type t=__reftype(r);
var boxing =TypeReference.ToObject(r); int* p=&x; is unsafe. CAS uses __makeref to be performance native code
Syste.ValueType is System.Object, Heap has MT method table.
__arglist only usefull c# interOp c++/CLI
mySafe::tryout_arglist^ u = gcnew Unsafe::tryout_arglist();
u->Test(1, 2, 3, "y");
public class tryout_arglist {
public void Test(__arglist){
var itr = new ArgIterator(__arglist);
for(int i=0;i<itr.GetRemainingCount();i++){
var r = itr.GetNextArg();
var v = TypedReference.ToObject(r);
Iterator:
yield return is a statemachine, ILSpy _state=-1; switch(_state) case 0: _state=1
local var is on heap, so must set var=null if eager GC. GC.GetTotalMemory(fullgccollection:true) shows big
IEnumerable cannot have out/ref since stackframe is on heap, danger.
IEnumerable<T> and IEnumerator<T> both yield return but former need GetEnumerator() and check Env.GetManagedThreadId
INPC using expression tree---Notify(()=>Bid0)
VMBase::Nofity<T>(<Expression<Func<T>> s) { PropertyChangeEventHandler h=this.PropertyChanged;
var pi=(s as MemberExpression).Member).Body as PropertyInfo; h(this,pi.Name);
_state=-2 vs _state=-1 start in ILSpy.
Inject into LINQ expression for debugging
Console.WriteLine(info) is effective to see value as we loop through. But a LINQ expression or even its function lambda equivalent cannot use Console directly. So the following Injector extension is very useful.
Inject into LINQ
public static class Injector
{
public static IEnumerable<T> Where<T>(this IEnumerable<T> source,Func<T,string> debug)
{
foreach (T t in source)
{
string info = debug(t);
Console.WriteLine(info);
yield return t;
}
}
}
var q = from i in new[]{ 1, 5, 7, 2, 9, 3 }
where "where %2 "+i
where i % 2 == 0
where "select " + i
select i;
q.ToList();
Tuesday, March 8, 2016
Google Protocol Buffer in C#
It claims to be much efficient on the wire format than .Net Binary Serializer. So the follow summary how to use it in Visual Studio
Nuget Package Console: install-package Google.ProtocolBuffers. This will bring in Dll and tools like ProtoGen.exe to convert .proto file into C# class.
using packages\..\tools\ProtoGen.exe as follows will generate a class to be used in serialization.
ProtoGen -public_classes=true, -generate_private_ctor=false address.proto
this class does not allow constructor and setter, instead must use builder and parser.
.proto file and C# code to build, serialize/deserialize as file on disk
syntax = "proto2";
package tryout;
message Person {
optional string name = 1;
required int32 id = 2;
optional string email = 3;
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
message PhoneNumber {
optional string number = 1;
optional PhoneType type = 2;
}
repeated PhoneNumber phones = 4;
}
message AddressBook {
repeated Person people = 1;
}
static void Main(string[] args)
{
var bdr = Person.CreateBuilder();
bdr.Id = 1;
bdr.Name = "John";
bdr.Email = "john@yahoo.com";
Person john = bdr.Build();
using (var output = File.Create("john.dat"))
{
john.WriteTo(output);
}
var john2= Person.ParseFrom(File.OpenRead("john.dat"));
}
Monday, March 7, 2016
bash admin command braindump
sudo apt-get update sudo apt-get install openssh-server sudo ufw allow 22 mv f1 f2 rm -f dir file dir structure: /etc group pwd config ---/cron.hourly /apt ( 3rd party) /var ---- /log /www /dev device usb, disk network interfaces. /usr vs. /bin vs. /sbin user, systen , admin /lib kernal model shared lib /sys /proc resources sudo groupadd /groupdel jqd-group sudo chown root:jqd-group /var/jqd ls -l /var | grep jqd sudo useradd -m tony sudo passwd tony sudo usermod -aG jqd-group tony grep jqd-group /etc/group chmod r,g o -- owner/group/other r,w,x,=0,4,2,1 chmod g-r chmod o+x chmod 777, 770, 664(no one can x) ps aux |grep root pstree -p top kill 123 killall php-cgi top i=mem t=task l=load/uptime uptime tail -f /var/log/syslog & jobs fg 1 bg 1 tar czf archive.tar.gz /var/* nohup sudo apt-get update sudo apt-get install apache2 sudo service apache2 status/stop/start lsblk ls /etc/apt/source.list ls /var/www/html cd /var/log zcat syslog.2.gz cron 7 fields 5 20 * * 1-5 root /usr/bin/backup.sh min0-59 hr0-23 dom0-31 moy1-12 dow0-7 why 0? usr cron 6 fields */5 8-16 * * 1 echo "test" ~/working/1.txt every 5min 8am-4pm any day m Monday system cron /etc/crontab my_cron with 7 fields no extension cron.d cron.daily .hourly .weekly .monthly user cron job crontab -e -r -l # edit remove list cannot add file must use editor /var/spool/cron/crontab
TAP is almost the same as TPL for ComputeBound task
my previous post showed that logging to disk using async vs, buffer (Blocking collection, ring buffer) has significant impact on UI Processing. i.e. main thread spend 80% doing UI Processing vs 1-40%.
The tool is VS 2013/2015 Concurrency Visualizer.
But if the task is Compute Bound (Fabonacci) then TAP (task-based async pattern) vs TPL (Task Parallel Library) show no difference ---- Visualizer show both have main thread doing UI process 100%
here is the code in a WPF code-behind with a button and a Label L, offloading compute to TAP or TPL.
private void Button_Click(object sender, RoutedEventArgs e)
{
//TPL_ComputeBound();
TAP_ComputeBound();
}
void TPL_ComputeBound()
{
// will return from thread pool in ContinueWith
long ret = 0;
var task = Task.Run(() => ret = Fibonacci(n))
.ContinueWith((t) => L.Content = ret, TaskScheduler.FromCurrentSynchronizationContext());
}
async void TAP_ComputeBound() //asyn void breaks TPL/TAP workflow and should avoid
{
long ret = await ComputTask();
L.Content = ret; // above blocks and return to UI main thread.
}
Task<long> ComputTask()
{
return Task.Run(() =>
{
return Fibonacci(n);
}).ContinueWith((t) =>
{
return t.Result;
});
}
long Fibonacci(long n)
{
unchecked
{
if (n == 1 || n == 2) return 1;
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
}
}
Sunday, March 6, 2016
Visibility into SQL Server Parallelism and search indexing
(1) IO Completion, Asyn Network tells if SAN configuration would impact data retrival. (2) CXPACKET and MAXDOP option would trial-error how many CPU usage would be efficient. (3) Execution plan is a good view into what SQL server is doing: scan vs. seeking index, Loop join to walk over clustered to non-clustered. (4) Indexed View not just assist as index for other select, itself is faster -- OS related select wait_type,* from sys.dm_os_wait_stats where wait_type like '%IO_Completion%' or wait_type like '%Packet%' or wait_type like '%async_Network%' select * from Sales.SalesOrderDetail option(MAXDOP 1) set statistics io on --logical, physical reads exec sys.sp_configure N'Show advanced Options',1 reconfigure exec sys.sp_configure N'cost threshold for parallelism', 11 reconfigure -- Index--one clustered, size big as data. slow when insert use tempdb drop table dbo.test truncate table test2 create table test ( id int, firstname varchar(100), lastname varchar(100) ) create table test2 ( id int, firstname varchar(100), lastname varchar(100) ) create nonclustered index IX_name_test on test ( firstname asc, lastname asc ) on [primary] insert into test (id,firstname,lastname) select top 100000 ROW_NUMBER() over (order by a.name) rowId, 'Bob', case when ROW_NUMBER() over (order by a.name)%2=0 then 'Smith' else 'Brown' end name from sys.all_objects a cross join sys.all_objects b insert into test2 (id,firstname,lastname) select top 100000 ROW_NUMBER() over (order by a.name) rowId, 'Bob', case when ROW_NUMBER() over (order by a.name)%2=0 then 'Smith' else 'Brown' end name from sys.all_objects a cross join sys.all_objects b -- execution plan shows cost equal 50%=50% or index worse. -- all table scan, no seek index, not even use non-clustered idx -- table storage size: no-index=cluster index small, non-cluster index big as data select * from test select * from test2 select * from test with(index(IX_name_test)) -- when force index => worse cost 96% use AdventureWorks2012 -- nested loop join (inner join) even for a simple slect -- it actually join from non-clusered index to clustered, totally waste select * from HumanResources.Employee where NationalIDNumber=295847284 create nonclustered index IX_hum on HumanResources.Employee ( NationalIDNumber asc, hiredate ) -- non-clustered index must be column inclusive to work select NationalIDNumber,hiredate from HumanResources.Employee where NationalIDNumber=295847284 -- still seek predicate in execution plan has implicit_conv NationIDNum is nvar not int --index view, schemabinding restrict table changes create view sd_v with schemabinding as select sd.SalesOrderID,sd.OrderQty,sd.ProductID from Sales.SalesOrderDetail sd inner join sales.SalesOrderHeader h on h.SalesOrderID=sd.SalesOrderID --list all index select i.name, t.name, i.type_desc from sys.tables t inner join sys.indexes i on i.object_id=t.object_id where t.name like '%cust%' sp_helpindex 'PK_Customer_CustomerID';
Saturday, March 5, 2016
Write Qt Desktop App using Python
Qt is cross platform Desktop framework with IDE Qt Creator. But its code behind is C++.
To replace C++ with Python, we need Python 3.4, PyQt4.11 and Qt4.8.7 download and installed properly
The IDE is Jetbrain PyCharm Community Edition. It can compile/run test GUI but no design surface to work with.
Here is the Python+PyQt+Qt code and Simulate a browser
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
from PyQt4.QtWebKit import QWebView
class BlogBrowser(QDialog):
def __init__(self):
QDialog.__init__(self)
layout=QGridLayout()
self.setLayout(layout)
self.url_ledt=QLineEdit("http://homelab16.blogspot.com")
go_btn=QPushButton("Go")
self.browser=QWebView(self)
self.browser.setUrl(QUrl(self.url_ledt.text()))
layout.addWidget(self.url_ledt,0,0)
layout.addWidget(go_btn,0,1)
layout.addWidget(self.browser,1,0,1,2)
go_btn.clicked.connect(self.navigate)
def navigate(self, txt):
txt=self.url_ledt.text()
if "http://" not in txt:
txt="http://"+txt
self.browser.setUrl(QUrl(txt))
app = QApplication(sys.argv)
bb=BlogBrowser()
bb.show()
app.exec_()
Html5 Test: it scores 240 vs. IE Edge 421 vs. FF 478 vs. Chrome 521 out of max 555
Subscribe to:
Comments (Atom)