Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Tuesday, January 17, 2017

write graph dot networkx

write graph to dot in NetworkX:
 
nx.write_dot(G,'graph.dot')
 
than convert to svg (png doesn't work, don't know why) 
 
dot -Tsvg graph.dot > graph.svg

Tuesday, August 18, 2015

Mac OS X ipython notebook install

IPython is extended development environment for python with more usable shell, architecture for parallel and multiuser computing, and web-based environment (Ipython notebooks) for modern interactive web-based shell.

Unfortunately, python related staff on Mac requires more effort than on Linux.

Way to install Ipython Notebooks on mac is there:

1) Download anaconda package manager from there:
http://continuum.io/downloads

you need:

Mac OS X — 64-bit
Python 2.7
Graphical Installer 

2) Install anaconda by double click on downloaded package and following instructions in installer,

3) Start new bash shell (Mac OS X Terminal)

4) in terminal (confirm where required):
# for  updating conda
conda update conda
# for creating ipython environment
conda create --name ipython_learn python=2.7 ipython ipyhon-notebook
# list environments, here should be ipython_learn

conda env list 
# for activating ipython_learn environment
source activate ipython_learn
# for starting ipython notebooks environment
ipython notebook

Ok, there your ipython environment started.
Upload or just use your notebooks there.




Friday, March 7, 2008

Freemind map <--> lisp (perl, python) list convertion (I,II)


Intro:


I am using Freemind on regular basis, for storing ideas, notes, and planning.It is an ideal tool for the manipulating tree like structures as simple, as it can be done with plain text, and even simpler, due to the Freemind intuitive shortcuts.

In fact, file in Freemind .mm can be processed as XML (if ignore some details, like header, and html that can present inside nodes, maybe something another can be also) using XSLT transform, or in any programming language with access to XML parsing.

I want lists structures being presented by Freemind, and back,
Freemind trees being presented in list-like structures, like following: ((1 2 ) 3 (4 (5 6)) 7 8).

Part I. list to freemind converter.



I have simply modified one of previously presented programs, there : nested list implementation

Instead of writing "[" I wrote <node> tag with empty TEXT attribute, simply for anonymous fork inside Freemind:
$ret .= "<node ID=\"Perl_GEN".$id."\" TEXT=\"".$listp."\" />\n";


Also beginning and header of freemind map written in corresponding places of program.
That is
<map version="0.9.0_Beta_8">
and closing of the tag.

For the list:
$al = [1, [2, [3], 4], 5];


I have obtained:


Part II. Freemind to plain list converter.


Lets start from presentation of Freemind tree nodes in one not nested lisp list (no round brackets, '(' and ')' inside list):

Lets look inside of Freemind map:
$ cat map.mm | head
First too lines describe program and format.
Than <node> tags follow.
We need only the text field from node.

So XSLT transformer of mm to plain list, ignoring tree structure can look like following:
xslt simple.

$ xsltproc map_lisp_list_s.xsl ~/map.mm | perl -e "<>; while(<>){print;}"
(
"map"
"check"
"check1"
"check2"
"check3"
"ch3ch1"
"node"
"subnode1"
"subnode2"
"trnode" )

Perl was simply used to strip one line from beginning.

Part III,

that is Freemind to tree list will be described further.

Monday, February 25, 2008

Links: differences in syntax: Perl, tcl, Shell, C++, Python, Java, Javascript, Lisp

I have already mentioned in my post on comparing numbers and strings in shell, about problems of simultaneous use of different programming languages.

To conclude, having reference cards with description one language to another differences can be useful.

Listed is set of resources, intended for migration from one language to another.

Recommended (short,self-descriptive, useful):
languages comparison:
http://merd.sourceforge.net/pixel/language-study/syntax-across-languages/


Recommended, but not short:

Wikipedia page:
http://en.wikipedia.org/wiki/Comparison_of_programming_languages

Open directory listing for comparisons:
http://www.dmoz.org/Computers/Programming/Languages/Comparison_and_Review/

PLEAC - Programming Language Examples Alike Cookbook
Comparison of productivity of writing in different programming languages:
page.mi.fu-berlin.de/~prechelt/Biblio/jccpprtTR.pdf

Useful:
Java for c++ Programmers:
http://pages.cs.wisc.edu/~hasti/cs368/JavaTutorial/

http://triton.towson.edu/~mzimand/os/Lect2-java-tutorial.html
Lisp to javascript converter, descriptive.
http://javascript.crockford.com/little.html
Another one Lisp to Javascript converter written in javascript.( You can look into source to look into the code)
http://www.joeganley.com/code/jslisp.html

Comparison Python with Java, Lisp i.t.c.

http://wiki.python.org/moin/LanguageComparisons


Three scripting concurrents:
http://mjtsai.com/blog/2002/11/25/perl_vs_python_vs_ruby/

Accumulator generator in different languages:
http://www.paulgraham.com/accgen.html

Tcl vs. Python, with nice short examples

http://homepages.cwi.nl/~sjoerd/PythonVsTcl-old.html


This thread describes differences between bash and perl.
http://www.perlmonks.org/?node_id=661859

And at the end resource with language comparison in action (memory, speed, size).
http://shootout.alioth.debian.org/

Sunday, February 17, 2008

Perl nested lists vs. Python and Lisp.

It is no nested lists in Perl. However, Python and Lisp have this ability.

Description of implementing in Perl equivalent structure to nested Python and Lisp lists given.

Used:

$ python --version
Python 2.5.1
$ perl -v | head -2

This is perl, v5.8.8 built for i486-linux-gnu-thread-multi
$ clisp --version | head -1
GNU CLISP 2.41 (2006-10-13) (built 3371977993) (memory 3401903509)


Consider
'(1 (2 (3) 4) 5)
lisp list as the example of data structure we work with:

$ clisp
...
[1]> (setq alist `(1 (2 (3) 4) 5))
(1 (2 (3) 4) 5)
[2]> (elt alist 0)
1
[3]> (elt alist 2)
5
[4]> (elt alist 1)
(2 (3) 4)
[5]>

Corresponding interactive session for Python:

>>> a = [1, [2, [3], 4], 5]
>>> print a
[1, [2, [3], 4], 5]
>>> print a[0]
1
>>> print a[1]
[2, [3], 4]
>>> print a[2]
5



Python in list processing is very like Lisp. The difference for defining list body is only using commas and square brackets in python, and you don't need put quote sign before it. (As done in Lisp to interpret it as list, not executing as function).

Perl is different in this point. Perl instead of interpreting list as given, removes all parens inside.


$ perl -e '@al = (1, (2, (3), 4), 5); \
print "\@al[0]=@al[0] , \@al[1]=@al[1] , \@al[2]=@al[2] ,\
\@al[3]=@al[3] , \@al[4]=@al[4] \n"; \
print @al; print "\n"; '
@al[0]=1 , @al[1]=2 , @al[2]=3 , @al[3]=4 , @al[4]=5
12345


Lisp has very convenient data model, all data in lisp are pointers.
And the solution of implementing nested lists in Perl is pointer based.

Pointer to list in perl uses square brackets ([]), like lists syntax in Python.
So our structure look is:

$al = [1, [2, [3], 4], 5];


Following procedure prints elements, emulating output in Python session and Common Lisp REPL:


sub slistp($)
{
my ($listp) = @_;
my $i;
my $ret = "";
if (ref($listp) eq 'ARRAY')
{
$ret = "[";
$ret .= slistp(@$listp[0]);
for ($i=1;$i<(scalar @$listp);$i++)
{
$ret .= ",";
$ret .= slistp(@$listp[$i]);
};
$ret .= "]";
}
else
{
$ret .= $listp;
}
return($ret);
}


Usage for our list:
$al = [1, [2, [3], 4], 5];
print "\@al[0]=".@$al[0].", \@al[1]=@$al[1] , \@al[2]=@$al[2] \n";
print slistp($al);
print "\n";
print "\@al[0]=".slistp(@$al[0])."\n\@al[1]=".slistp(@$al[1]);
print "\n\@al[2]=".slistp(@$al[2])."\n";

result of complete code execution:
$ perl nestlist.pl
@al[0]=1, @al[1]=ARRAY(0x8152b44) , @al[2]=5
[1,[2,[3],4],5]
@al[0]=1
@al[1]=[2,[3],4]
@al[2]=5


So for Perl lisp-based tree structures, list references have to be used instead of lists.

Additional fees for dereferencing array pointers applied in Perl syntax.
It is no REPL integrated session in Perl, and examining complex structures based on lists require usage of additional modules, or writing own functions. Therefore Python, and especially Lisp are more preferable for list processing tasks.

Sunday, February 10, 2008

Playing python: syntax tree of python expressions

Done: Working version of program for python expressions parsing.

Source is available on my page.

Below is example of how it works, and tree generated/presented.

Good explanation of python's slowness.

Marked blue - is input in terminal

------------------%<-------------------------------
./parse_python.py
2 + 2

[257, [266, [267, [268, [269, [326, [303, [304, [305, [306, [307, [309, [310, [311, [312, [313, [314, [315, [316, [317, [2, '2']]]]], [14, '+'], [314, [315, [316, [317, [2, '2']]]]]]]]]]]]]]]]]], [4, '']]], [0, '']]
['symbol=file_input', ['symbol=stmt', ['symbol=simple_stmt', ['symbol=small_stmt', ['symbol=expr_stmt', ['symbol=testlist', ['symbol=test', ['symbol=or_test', ['symbol=and_test', ['symbol=not_test', ['symbol=comparison', ['symbol=expr', ['symbol=xor_expr', ['symbol=and_expr', ['symbol=shift_expr', ['symbol=arith_expr', ['symbol=term', ['symbol=factor', ['symbol=power', ['symbol=atom', ['token=NUMBER', 'value=2']]]]], ['token=PLUS', 'value=+'], ['symbol=term', ['symbol=factor', ['symbol=power', ['symbol=atom', ['token=NUMBER', 'value=2']]]]]]]]]]]]]]]]]], ['token=NEWLINE', 'value=']]], ['token=ENDMARKER', 'value=']]
------------------>%-------------------------------

Friday, February 8, 2008

Common Lisp vs Python: code size

Just joke :)

I know, it is not enough statistics for conclusions :)

We have compared reduce statement in Common lisp and python with one fellow.
I wrote in Common lisp, he in Python.

Resulting "code" sizes for same construct, just translated from one language to another:


$ echo "reduce(lambda x,y: (x,y,x), (1,2,3,4))" | wc
1 4 39
$ echo "(reduce (lambda(x y)(list x y x)) (list 1 2 3 4))" | wc
1 11 50
Python : 39 symbols long
Common Lisp : 50 symbols.


And the winner is Python :)
Python win about 20 or 25 percents of code size.