
| Line: 1 to 1 | ||||||||
|---|---|---|---|---|---|---|---|---|
Using C# on Patas | ||||||||
| Added: | ||||||||
| > > | October 2009 : Mono upgraded to 2.4.2.3 on Patas. Thanks David! | |||||||
| C# is a powerful general-purpose programming language originally developed by Microsoft. After its approval as a standard by ECMA, it has been independently re-engineered as an open-source implementation which is available on many platforms, including Linux. It is installed and available for use on patas. Because the mono implementation is extremely compatible, Console-oriented C# programs developed on Windows machines (i.e. with Visual Studio) will typically run on mono without changing the source code. (It is beyond the scope of this document to discuss the compatibility of graphical programs.) | ||||||||
| Changed: | ||||||||
| < < | C# programs operate in a fully sandboxed runtime environment which provides garbage-collection. As in Java, Python, and other high-level general-purpose languages, disposal of unused memory objects is tracked and managed by the system, relieving an enormous burden from the application programmer. | |||||||
| > > | C# programs operate in a fully sandboxed runtime environment (the Common Language Infrastructure, or CLI) which provides garbage-collection. As in Java, Python, and other high-level general-purpose languages, disposal of unused memory objects is tracked and managed by the system, relieving an enormous burden from the application programmer. | |||||||
1. Sample Programusing System; using System.Text; | ||||||||
| Line: 81 to 83 | ||||||||
| ztring. | ||||||||
| Changed: | ||||||||
| < < | Internally, all strings in C# are Unicode, as is the Char data type. A rich set of functions is provided in the Class Library's 'Encoding' namespace for reading and writing the various 8-bit character sets as input and output. | |||||||
| > > | Internally, all strings in C# are Unicode, as is the Char data type. A rich set of functions is provided in the Class Library's 'Encoding' namespace for reading and writing the various 8-bit character sets as input and output. | |||||||
| Changed: | ||||||||
| < < | As in many other modern languages, the C# String type is immutable. This allows for significant internal optimizations in the runtime environment but can be inefficient when doing intensive mutation and other editing operations. For this reason, the CLR also implements the StringBuilder object, which allows a large amount of text to be gathered through appending. | |||||||
| > > | As in many other modern languages, the C# String type is immutable. This allows for significant internal optimizations in the runtime environment but can be inefficient when doing intensive mutation and other editing operations. For this reason, the CLR also implements the StringBuilder object, which allows a large amount of text to be gathered through appending. | |||||||
| Changed: | ||||||||
| < < | Strings can also be edited by converting them to an array of Char, as shown in the next example, "Reading and Writing Files." The example also shows how a String object can be created from an array of Char. | |||||||
| > > | Strings can also be edited by converting them to an array of Char, as shown in the next example, "Reading and Writing Files." The example also shows how a String object can be created from an array of Char. | |||||||
4. Reading and Writing Filesusing System; using System.IO; | ||||||||
| Line: 219 to 221 | ||||||||
| Result: | ||||||||
| Changed: | ||||||||
| < < | the[6] of[3] and[3] establish[2] for[2] states[2] to[2] united[2] perfect[1] promote[1] general[1] we[1] | |||||||
| > > | the[6] of[3] and[3] establish[2] for[2] states[2] to[2] united[2] perfect[1] promote[1] general[1] we[1] | |||||||
7. Access a Web Pageusing System; using System.IO; | ||||||||
| Line: 301 to 304 | ||||||||
| } } | ||||||||
| Added: | ||||||||
| > > | 10. Using the GroupBy LINQ operatorWe have found that, even with the new release 2.4.2 of Mono, the GroupBy operator is much slower than it is on .NET. It is to be expected that you will find some differences in performance between the Microsoft and Mono implementations. In the case of GroupBy, you can always break the operation in two steps, using a temporary dictionary, for example. Keeping this limitation in mind, here is the example I presented at the 2009 CLMA orientation talk on C#. This complete program reads an HTML file with the complete text of "Moby Dick," strips out HTML tags using a regular expression, and then prints a "graphical" Zipf distribution (word-frequency histogram) of the 35 most common words onto your console.using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
class MainClass
{
static void Main()
{
String text = new StreamReader("moby_dick.html").ReadToEnd();
text = Regex.Replace(text, "<(.|\n)*?>", String.Empty);
String[] words = text.Split(" \n\",.;-!?".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
| |||||||
| Changed: | ||||||||
| < < | -- Main.gslayden - 14 Nov 2008 | |||||||
| > > | var grp = words.GroupBy(w => w.ToLower()).ToArray();
var tallies = words
.GroupBy(w => w.ToLower())
.Select(g => new { g.Key, Tally = g.Count() })
.OrderByDescending(e => e.Tally);
int scale = tallies.First().Tally / 60;
foreach (var tally in tallies.Take(35))
WriteLine ("{0,6} {1}", tally.Key, new String('*', tally.Tally / scale));
}
};
11. More InformationA recording of the treehouse presentation "Using C# on Patas" can be found here: http://uweoconnect.extn.washington.edu/p30062745/. The slides from this talk are on patas at /opt/dropbox/09-10/orientation/Mono_on_Patas.pdf. Most of the code examples on this page, along with the "Moby Dick" text, can be found on patas in the following directory: /opt/dropbox/09-10/orientation/csharp-demos The best book on LINQ that I've found is "Pro LINQ: Language Integrated Query in C# 2008" by Joseph C. Rattz, Jr. (APress 1-59059-789-3). This book presupposes familiarity with C#. -- Main.gslayden - 28 Oct 200912. Discussion | |||||||
| If anybody has any info about how to get a good installation of mono onto ubuntu 8.0.x, I would really appreciate hearing about it. I put about 8 hours into trying to to install mono on Ubuntu, and that is about 6 more hours than I really had to spend. | ||||||||
| Line: 317 to 361 | ||||||||
| You could also experiment with some of the RPMs here, and see if any of them will install using "alien": http://download.opensuse.org/repositories/Mono/ Note that many of these are for mono 1.9.x. To get 2.x on patas I had to build it from source. -- brodbd - 30 Dec 2008 | ||||||||
| Added: | ||||||||
| > > | -----Original Message----- From: patas-announce-bounces@mailman2.u.washington.edu On Behalf Of brodb@u Sent: Thursday, October 15, 2009 3:34 PM To: patas-announce@u.washington.edu Subject: [patas-announce] Mono upgraded to 2.4.2.3. I've updated our local build of Mono, in /opt/mono/bin, to 2.4.2.3. Mono Debugger (mdb) version 2.4.2.1 is also installed there. The old version remains available in /opt/mono-2.0.1/bin, if you need it. | |||||||
| Line: 1 to 1 | ||||||||
|---|---|---|---|---|---|---|---|---|
Using C# on Patas | ||||||||
| Line: 8 to 8 | ||||||||
C# programs operate in a fully sandboxed runtime environment which provides garbage-collection. As in Java, Python, and other high-level general-purpose languages, disposal of unused memory objects is tracked and managed by the system, relieving an enormous burden from the application programmer.
1. Sample Program | ||||||||
| Changed: | ||||||||
| < < | using System;
using System.Text;
static class MainClass
{
static void Main(String[] args)
{
Console.WriteLine("hello world");
}
}
| |||||||
| > > | using System; using System.Text; static class MainClass { static void Main(String[] args) { Console.WriteLine("hello world"); } } | |||||||
| To compile and run on patas: | ||||||||
| Changed: | ||||||||
| < < | gmcs hello.cs mono hello.exe | |||||||
| > > | gmcs hello.cs mono hello.exe | |||||||
| The reason that this is a two-step process hints at the power of C# over interpreted languages; the first step "compiles" your source file into an intermediate byte-code called MSIL which is then processed by a runtime environment, called the Common Language Runtime, or CLR. This type of virtual instruction set is nothing new in computer science. But an innovation that Microsoft's CLR introduced was that this MSIL code is translated, on an as-needed basis, into actual native machine instructions for the target system. And it's retained in this optimal form as the program runs. This is called Just-in-time, or JIT compilation, and it means that your C# program runs with the performance of true native compilation. In fact, you can even use mono to execute, on patas, an MSIL binary produced by Microsoft Visual Studio 2008! Just copy the .exe file (in binary mode) to patas and run it like so: | ||||||||
| Changed: | ||||||||
| < < | mono compiled_by_msvc.exe | |||||||
| > > | mono compiled_by_msvc.exe | |||||||
| To invoke a mono program from a CONDOR script, you should specify the full path to the mono executable: | ||||||||
| Changed: | ||||||||
| < < | universe = vanilla getenv = true executable = /opt/mono/bin/mono arguments = "myprogram.exe -myarg1 -myarg2" input = mystdin.txt output = mystdout.txt error = mystderr.txt log = /tmp/myuwid.log transfer_executable = false queue | |||||||
| > > | universe = vanilla getenv = true executable = /opt/mono/bin/mono arguments = "myprogram.exe -myarg1 -myarg2" input = mystdin.txt output = mystdout.txt error = mystderr.txt log = /tmp/myuwid.log transfer_executable = false queue | |||||||
2. DocumentationMicrosoft's detailed commercial-quality documentation on C# is available freely on the web. Of primary interest will be the extensive CLR (".NET Framework") class libraries, which provide a wide array of system services and data structures. The mono project also offers a set of documentation. | ||||||||
| Line: 51 to 29 | ||||||||
The examples below highlight basic programming tasks which are relevant for computational linguists. All have been developed and tested with mono on patas.
3. String Manipulation | ||||||||
| Changed: | ||||||||
| < < | using System;
using System.Text;
static class MainClass
{
static void Main(String[] args)
{
String s = "1.\tThis is a string.";
String[] string_arr = s.Split('\t');
Char[] trim_chars = ".:;,".ToCharArray();
String ns = string_arr[0].Trim(trim_chars);
int i = Convert.ToInt32(ns);
Console.WriteLine(i);
foreach (String s2 in string_arr[1].Split())
Console.WriteLine(s2.Replace('s','z'));
}
}
| |||||||
| > > | using System; using System.Text; static class MainClass { static void Main(String[] args) { String s = "1.\tThis is a string."; String[] string_arr = s.Split('\t'); Char[] trim_chars = ".:;,".ToCharArray(); String ns = string_arr[0].Trim(trim_chars); int i = Convert.ToInt32(ns); Console.WriteLine(i); foreach (String s2 in string_arr[1].Split()) Console.WriteLine(s2.Replace('s','z')); } } | |||||||
| Result: | ||||||||
| Changed: | ||||||||
| < < | 1 Thiz iz a ztring. | |||||||
| > > | 1 Thiz iz a ztring. | |||||||
| Internally, all strings in C# are Unicode, as is the Char data type. A rich set of functions is provided in the Class Library's 'Encoding' namespace for reading and writing the various 8-bit character sets as input and output. | ||||||||
| Line: 87 to 40 | ||||||||
Strings can also be edited by converting them to an array of Char, as shown in the next example, "Reading and Writing Files." The example also shows how a String object can be created from an array of Char.
4. Reading and Writing Files | ||||||||
| Changed: | ||||||||
| < < | using System;
using System.IO;
using System.Linq;
using System.Text;
static class MainClass
{
static void Main(String[] args)
{
String my_filename = "the_file.txt";
String data = "Four score and seven years ago.";
// Write some data to the file
int i = 0;
using (FileStream fs = new FileStream(my_filename, FileMode.Create, FileAccess.Write, FileShare.None))
{
using (StreamWriter sr = new StreamWriter(fs, Encoding.GetEncoding(28591))) // Latin-1
{
foreach (String s in data.Split())
sr.WriteLine((++i).ToString() + ". " + new String(s.ToCharArray().Reverse ().ToArray()));
}
}
// Read data from the file
using (FileStream fs = File.Open(my_filename, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (StreamReader sr = new StreamReader(fs, Encoding.GetEncoding(28591)))
{
String s;
while (null != (s = sr.ReadLine()))
Console.WriteLine(s);
}
}
}
}
| |||||||
| > > | using System; using System.IO; using System.Linq; using System.Text; static class MainClass { static void Main(String[] args) { String my_filename = "the_file.txt"; String data = "Four score and seven years ago."; // Write some data to the file int i = 0; using (FileStream fs = new FileStream(my_filename, FileMode.Create, FileAccess.Write, FileShare.None)) { using (StreamWriter sr = new StreamWriter(fs, Encoding.GetEncoding(28591))) // Latin-1 { foreach (String s in data.Split()) sr.WriteLine((++i).ToString() + ". " + new String(s.ToCharArray().Reverse ().ToArray())); } } // Read data from the file using (FileStream fs = File.Open(my_filename, FileMode.Open, FileAccess.Read, FileShare.Read)) { using (StreamReader sr = new StreamReader(fs, Encoding.GetEncoding(28591))) { String s; while (null != (s = sr.ReadLine())) Console.WriteLine(s); } } } } | |||||||
| Result: | ||||||||
| Changed: | ||||||||
| < < | 1. ruoF 2. erocs 3. dna 4. neves 5. sraey 6. .oga | |||||||
| > > | 1. ruoF 2. erocs 3. dna 4. neves 5. sraey 6. .oga | |||||||
5. Hash Table of User-defined Objects | ||||||||
| Changed: | ||||||||
| < < | using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class MyObject
{
public Double d_x;
public Double d_y;
public Double d_z;
// constructor
public MyObject(Double x_arg, Double y_arg, Double z_arg)
{
d_x = x_arg; d_y = y_arg; d_z = z_arg;
}
};
static class MainClass
{
static void Main(String[] args)
{
Dictionary<String, MyObject> ht = new Dictionary<String, MyObject>();
ht.Add("object 1", new MyObject(3.0, 2.1, Math.PI));
ht.Add("object 2", new MyObject(Math.Sqrt(2.0), Math.Log(6.0,10.0), 3.2));
ht.Add("3rd object", new MyObject(2.1, 9.9, Double.NaN));
Console.WriteLine(ht["object 2"].d_x);
}
}
| |||||||
| > > | using System; using System.Collections.Generic; using System.Linq; using System.Text; public class MyObject { public Double d_x; public Double d_y; public Double d_z; // constructor public MyObject(Double x_arg, Double y_arg, Double z_arg) { d_x = x_arg; d_y = y_arg; d_z = z_arg; } }; static class MainClass { static void Main(String[] args) { Dictionary<String, MyObject> ht = new Dictionary<String, MyObject>(); ht.Add("object 1", new MyObject(3.0, 2.1, Math.PI)); ht.Add("object 2", new MyObject(Math.Sqrt(2.0), Math.Log(6.0,10.0), 3.2)); ht.Add("3rd object", new MyObject(2.1, 9.9, Double.NaN)); Console.WriteLine(ht["object 2"].d_x); } } | |||||||
6. Generic CollectionsC# has a number of powerful strongly-typed generic collection objects. For example: | ||||||||
| Line: 179 to 60 | ||||||||
| ||||||||
| Changed: | ||||||||
| < < | using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
static class MainClass
{
static void Main(String[] args)
{
String text = "We the People of the United States, in Order to " +
"form a more perfect Union, establish Justice, ensure domestic " +
"Tranquility, provide for the common defence, promote the general " +
"Welfare, and secure the Blessings of Liberty to ourselves and our " +
"Posterity, do ordain and establish this Constitution for the United " +
"States of America.";
Char[] split_chars = new Char[] { '.', ',', ' ' };
String[] words = text.ToLower().Split(split_chars,StringSplitOptions.RemoveEmptyEntries);
// use a hash table to tally word-types:
Dictionary<String, int> hash_tab = new Dictionary<String, int>();
foreach (String w in words)
{
int v;
if (hash_tab.TryGetValue(w,out v))
hash_tab[w] = v+1;
else
hash_tab.Add(w,1);
}
// demonstrate using LINQ to display 12 of the most common word-types plus their counts
KeyValuePair<String, int>[] kvp_arr = hash_tab.OrderByDescending(e => e.Value).Take (12).ToArray();
// this is similar to 'list comprehension' in Python
String s = kvp_arr.Aggregate(String.Empty,(av, e) => av + e.Key + "[" + e.Value + "] ");
Console.WriteLine(s);
}
}
| |||||||
| > > | using System; using System.Collections.Generic; using System.Linq; using System.Text; static class MainClass { static void Main(String[] args) { String text = "We the People of the United States, in Order to " + "form a more perfect Union, establish Justice, ensure domestic " + "Tranquility, provide for the common defence, promote the general " + "Welfare, and secure the Blessings of Liberty to ourselves and our " + "Posterity, do ordain and establish this Constitution for the United " + "States of America."; Char[] split_chars = new Char[] { '.', ',', ' ' }; String[] words = text.ToLower().Split(split_chars,StringSplitOptions.RemoveEmptyEntries); // use a hash table to tally word-types: Dictionary<String, int> hash_tab = new Dictionary<String, int>(); foreach (String w in words) { int v; if (hash_tab.TryGetValue(w,out v)) hash_tab[w] = v+1; else hash_tab.Add(w,1); } // demonstrate using LINQ to display 12 of the most common word-types plus their counts KeyValuePair<String, int>[] kvp_arr = hash_tab.OrderByDescending(e => e.Value).Take (12).ToArray(); // this is similar to 'list comprehension' in Python String s = kvp_arr.Aggregate(String.Empty,(av, e) => av + e.Key + "[" + e.Value + "] "); Console.WriteLine(s); } } | |||||||
Result:
the[6] of[3] and[3] establish[2] for[2] states[2] to[2] united[2] perfect[1] promote[1] general[1] we[1] | ||||||||
| Line: 220 to 64 | ||||||||
Result:
the[6] of[3] and[3] establish[2] for[2] states[2] to[2] united[2] perfect[1] promote[1] general[1] we[1] | ||||||||
| Added: | ||||||||
| > > | ||||||||
7. Access a Web Page | ||||||||
| Changed: | ||||||||
| < < | using System;
using System.IO;
using System.Text;
using System.Net;
static class MainClass
{
static void Main(String[] args)
{
String s;
using (WebClient wc = new WebClient())
{
using (Stream str = wc.OpenRead("http://www.compling.washington.edu/compling/"))
{
using (StreamReader sr = new StreamReader(str))
s = sr.ReadToEnd();
}
}
Console.WriteLine(s);
}
}
| |||||||
| > > | using System; using System.IO; using System.Text; using System.Net; static class MainClass { static void Main(String[] args) { String s; using (WebClient wc = new WebClient()) { using (Stream str = wc.OpenRead("http://www.compling.washington.edu/compling/")) { using (StreamReader sr = new StreamReader(str)) s = sr.ReadToEnd(); } } Console.WriteLine(s); } } | |||||||
8. LINQ OperationsOne of the exciting things about mono is that it includes support for one of the latest developments in Microsoft's C# 3.5, namely Language-Integrated Query (LINQ), and its supporting technologies (extension methods and lambda expressions). LINQ allows sophisticated and concise retrieval and manipulation operations to be executed on data collections via native C# language expressions. Categories of operations include aggregation, quantification, conversion, concatenation, retrieval, set (union, intersection, etc.), generation, grouping, join, ordering, projection, partitioning, and restriction (filtering). LINQ is an expansive topic in its own right. The following simple example gives a glimpse of what is possible. | ||||||||
| Changed: | ||||||||
| < < | using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
static class MainClass
{
static void Main(string[] args)
{
String[] items = { "cat", "pear", "apple", "cat", "banana", "pear", "pear", "apple" };
KeyValuePair[] tallies = items.GroupBy(k => k, e => 1)
.Select(f => new KeyValuePair<String, int>(f.Key.ToUpper(), f.Sum()))
.OrderBy(g => g.Key)
.ToArray();
foreach (KeyValuePair<String, int> kvp in tallies)
Console.WriteLine(kvp.Key + '\t' + kvp.Value);
}
}
| |||||||
| > > | using System; using System.Collections.Generic; using System.Linq; using System.Text; static class MainClass { static void Main(string[] args) { String[] items = { "cat", "pear", "apple", "cat", "banana", "pear", "pear", "apple" }; KeyValuePair[] tallies = items.GroupBy(k => k, e => 1) .Select(f => new KeyValuePair<String, int>(f.Key.ToUpper(), f.Sum())) .OrderBy(g => g.Key) .ToArray(); foreach (KeyValuePair<String, int> kvp in tallies) Console.WriteLine(kvp.Key + '\t' + kvp.Value); } } | |||||||
| Result: | ||||||||
| Changed: | ||||||||
| < < | APPLE 2 BANANA 1 CAT 2 PEAR 3 | |||||||
| > > | APPLE 2 BANANA 1 CAT 2 PEAR 3 | |||||||
9. Calling a Python Script as a Shell Process | ||||||||
| Changed: | ||||||||
| < < | using System;
using System.IO;
using System.Diagnostics;
class MainClass
{
public static void Main(string[] args)
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "python";
psi.Arguments = "test.py";
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
using (Process p = new Process())
{
p.StartInfo = psi;
p.Start();
Console.WriteLine(p.StandardOutput.ReadToEnd());
}
}
}
| |||||||
| > > | using System; using System.IO; using System.Diagnostics; class MainClass { public static void Main(string[] args) { ProcessStartInfo psi = new ProcessStartInfo(); psi.FileName = "python"; psi.Arguments = "test.py"; psi.RedirectStandardOutput = true; psi.UseShellExecute = false; using (Process p = new Process()) { p.StartInfo = psi; p.Start(); Console.WriteLine(p.StandardOutput.ReadToEnd()); } } } | |||||||
| -- Main.gslayden - 14 Nov 2008 | ||||||||
| Line: 309 to 87 | ||||||||
| My chances of adopting c# as my language of choice on Linux are pretty slim unless I can a good install of mono to run on my home version of Linux. -- Main.andyf - 24 Nov 2008 \ No newline at end of file | ||||||||
| Added: | ||||||||
| > > | Andy: Unfortunately, support for non-RPM-based distributions is pretty slim. Mono is a Novell project, so they focus mainly on SuSE, their own Linux distribution. There is a Debian package, though, and since Ubuntu is Debian-derived, it's possible you could get that to work. You could also experiment with some of the RPMs here, and see if any of them will install using "alien": http://download.opensuse.org/repositories/Mono/ Note that many of these are for mono 1.9.x. To get 2.x on patas I had to build it from source. -- brodbd - 30 Dec 2008 | |||||||
| Line: 1 to 1 | ||||||||
|---|---|---|---|---|---|---|---|---|
Using C# on Patas | ||||||||
| Line: 10 to 10 | ||||||||
1. Sample Programusing System; using System.Text; | ||||||||
| Added: | ||||||||
| > > | ||||||||
| static class MainClass { static void Main(String[] args) { WriteLine ("hello world"); } | ||||||||
| Changed: | ||||||||
| < < | } | |||||||
| > > | } | |||||||
| To compile and run on patas: | ||||||||
| Changed: | ||||||||
| < < | gmcs hello.cs mono hello.exe | |||||||
| > > | gmcs hello.cs mono hello.exe | |||||||
| The reason that this is a two-step process hints at the power of C# over interpreted languages; the first step "compiles" your source file into an intermediate byte-code called MSIL which is then processed by a runtime environment, called the Common Language Runtime, or CLR. This type of virtual instruction set is nothing new in computer science. But an innovation that Microsoft's CLR introduced was that this MSIL code is translated, on an as-needed basis, into actual native machine instructions for the target system. And it's retained in this optimal form as the program runs. This is called Just-in-time, or JIT compilation, and it means that your C# program runs with the performance of true native compilation. In fact, you can even use mono to execute, on patas, an MSIL binary produced by Microsoft Visual Studio 2008! Just copy the .exe file (in binary mode) to patas and run it like so: | ||||||||
| Changed: | ||||||||
| < < | mono compiled_by_msvc.exe | |||||||
| > > | mono compiled_by_msvc.exe | |||||||
To invoke a mono program from a CONDOR script, you should specify the full path to the mono executable:
universe = vanilla | ||||||||
| Line: 36 to 41 | ||||||||
| error = mystderr.txt log = /tmp/myuwid.log transfer_executable = false | ||||||||
| Changed: | ||||||||
| < < | queue | |||||||
| > > | queue | |||||||
2. DocumentationMicrosoft's detailed commercial-quality documentation on C# is available freely on the web. Of primary interest will be the extensive CLR (".NET Framework") class libraries, which provide a wide array of system services and data structures. The mono project also offers a set of documentation. | ||||||||
| Line: 48 to 53 | ||||||||
3. String Manipulationusing System; using System.Text; | ||||||||
| Changed: | ||||||||
| < < | static class MainClass {
| |||||||
| > > | static class MainClass { | |||||||
| static void Main(String[] args) { String s = "1.\tThis is a string."; String[] string_arr = s.Split('\t'); | ||||||||
| Added: | ||||||||
| > > | ||||||||
| Char[] trim_chars = ".:;,".ToCharArray(); String ns = string_arr[0].Trim(trim_chars); int i = ToInt32 (ns); | ||||||||
| Added: | ||||||||
| > > | ||||||||
| WriteLine (i); | ||||||||
| Added: | ||||||||
| > > | ||||||||
| foreach (String s2 in string_arr[1].Split()) WriteLine (s2.Replace('s','z')); } | ||||||||
| Changed: | ||||||||
| < < | } | |||||||
| > > | } | |||||||
| Result: | ||||||||
| Changed: | ||||||||
| < < | 1 Thiz iz a ztring. | |||||||
| > > | 1 Thiz iz a ztring. | |||||||
| Internally, all strings in C# are Unicode, as is the Char data type. A rich set of functions is provided in the Class Library's 'Encoding' namespace for reading and writing the various 8-bit character sets as input and output. | ||||||||
| Line: 75 to 91 | ||||||||
| using System.IO; using System.Linq; using System.Text; | ||||||||
| Changed: | ||||||||
| < < | static class MainClass { | |||||||
| > > | static class MainClass { | |||||||
| static void Main(String[] args) { String my_filename = "the_file.txt"; | ||||||||
| Added: | ||||||||
| > > | ||||||||
| String data = "Four score and seven years ago."; | ||||||||
| Changed: | ||||||||
| < < | // Write some data to the file int i = 0; | |||||||
| > > | // Write some data to the file int i = 0; | |||||||
| using (FileStream fs = new FileStream (my_filename, FileMode .Create, FileAccess .Write, FileShare .None)) | ||||||||
| Changed: | ||||||||
| < < | { using (StreamWriter sr = new StreamWriter (fs, GetEncoding (28591))) // Latin-1 | |||||||
| > > | { using (StreamWriter sr = new StreamWriter (fs, GetEncoding (28591))) // Latin-1 | |||||||
| { foreach (String s in data.Split()) sr.WriteLine((++i).ToString() + ". " + new String(s.ToCharArray().Reverse().ToArray())); } } | ||||||||
| Added: | ||||||||
| > > | ||||||||
| // Read data from the file using (FileStream fs = File.Open(my_filename, FileMode .Open, FileAccess .Read, FileShare .Read)) | ||||||||
| Changed: | ||||||||
| < < | { using (StreamReader sr = new StreamReader (fs, GetEncoding (28591))) | |||||||
| > > | { using (StreamReader sr = new StreamReader (fs, GetEncoding (28591))) | |||||||
| { String s; while (null = (s = sr.ReadLine())) | ||||||||
| Line: 99 to 122 | ||||||||
| } } } | ||||||||
| Changed: | ||||||||
| < < | } | |||||||
| > > | } | |||||||
| Result: | ||||||||
| Changed: | ||||||||
| < < | 1. ruoF 2. erocs 3. dna 4. neves 5. sraey 6. .oga | |||||||
| > > | 1. ruoF 2. erocs 3. dna 4. neves 5. sraey 6. .oga | |||||||
5. Hash Table of User-defined Objectsusing System; using System.Collections.Generic; using System.Linq; using System.Text; | ||||||||
| Added: | ||||||||
| > > | ||||||||
| public class MyObject { public Double d_x; public Double d_y; public Double d_z; | ||||||||
| Added: | ||||||||
| > > | ||||||||
| // constructor public MyObject (Double x_arg, Double y_arg, Double z_arg) { | ||||||||
| Changed: | ||||||||
| < < | d_x = x_arg; d_y = y_arg; d_z = z_arg; | |||||||
| > > | d_x = x_arg; d_y = y_arg; d_z = z_arg; | |||||||
| } }; | ||||||||
| Added: | ||||||||
| > > | ||||||||
| static class MainClass | ||||||||
| Changed: | ||||||||
| < < | {static void Main(String[] args) {Dictionary<String, MyObject> ht = new Dictionary<String, MyObject>(); ht.Add("object 1", new MyObject(3.0, 2.1, Math.PI)); ht.Add("object 2", new MyObject(Math.Sqrt(2.0), Math.Log(6.0,10.0), 3.2)); ht.Add("3rd object", new MyObject(2.1, 9.9, Double.NaN));
| |||||||
| > > | { static void Main(String[] args) { Dictionary<String, MyObject > ht = new Dictionary<String, MyObject >(); ht.Add("object 1", new MyObject (3.0, 2.1, Math.PI)); ht.Add("object 2", new MyObject (Math.Sqrt(2.0), Math.Log(6.0,10.0), 3.2)); ht.Add("3rd object", new MyObject (2.1, 9.9, NaN )); | |||||||
| WriteLine (ht["object 2"].d_x); } | ||||||||
| Changed: | ||||||||
| < < | } | |||||||
| > > | } | |||||||
6. Generic CollectionsC# has a number of powerful strongly-typed generic collection objects. For example: | ||||||||
| Line: 144 to 183 | ||||||||
| using System.Collections.Generic; using System.Linq; using System.Text; | ||||||||
| Added: | ||||||||
| > > | ||||||||
| static class MainClass { static void Main(String[] args) | ||||||||
| Line: 154 to 194 | ||||||||
| "Welfare, and secure the Blessings of Liberty to ourselves and our " + "Posterity, do ordain and establish this Constitution for the United " + "States of America."; | ||||||||
| Added: | ||||||||
| > > | ||||||||
| Char[] split_chars = new Char[] { '.', ',', ' ' }; String[] words = text.ToLower().Split(split_chars,StringSplitOptions.RemoveEmptyEntries); | ||||||||
| Added: | ||||||||
| > > | ||||||||
| // use a hash table to tally word-types: Dictionary<String, int> hash_tab = new Dictionary<String, int>(); foreach (String w in words) | ||||||||
| Line: 166 to 208 | ||||||||
| else hash_tab.Add(w,1); } | ||||||||
| Added: | ||||||||
| > > | ||||||||
| // demonstrate using LINQ to display 12 of the most common word-types plus their counts KeyValuePair <String, int>[] kvp_arr = hash_tab.OrderByDescending(e => e.Value).Take(12).ToArray(); // this is similar to 'list comprehension' in Python String s = kvp_arr.Aggregate(String.Empty,(av, e) => av + e.Key + "[" + e.Value + "] "); WriteLine (s); } | ||||||||
| Changed: | ||||||||
| < < | } | |||||||
| > > | } | |||||||
Result:
the[6] of[3] and[3] establish[2] for[2] states[2] to[2] united[2] perfect[1] promote[1] general[1] we[1] | ||||||||
| Line: 182 to 225 | ||||||||
| using System.IO; using System.Text; using System.Net; | ||||||||
| Added: | ||||||||
| > > | ||||||||
| static class MainClass { static void Main(String[] args) | ||||||||
| Line: 197 to 241 | ||||||||
| } WriteLine (s); } | ||||||||
| Changed: | ||||||||
| < < | } | |||||||
| > > | } | |||||||
8. LINQ OperationsOne of the exciting things about mono is that it includes support for one of the latest developments in Microsoft's C# 3.5, namely Language-Integrated Query (LINQ), and its supporting technologies (extension methods and lambda expressions). LINQ allows sophisticated and concise retrieval and manipulation operations to be executed on data collections via native C# language expressions. Categories of operations include aggregation, quantification, conversion, concatenation, retrieval, set (union, intersection, etc.), generation, grouping, join, ordering, projection, partitioning, and restriction (filtering). | ||||||||
| Line: 207 to 252 | ||||||||
| using System.Collections.Generic; using System.Linq; using System.Text; | ||||||||
| Added: | ||||||||
| > > | ||||||||
| static class MainClass { static void Main(string[] args) { String[] items = { "cat", "pear", "apple", "cat", "banana", "pear", "pear", "apple" }; | ||||||||
| Added: | ||||||||
| > > | ||||||||
| KeyValuePair [] tallies = items.GroupBy(k => k, e => 1) .Select(f => new KeyValuePair <String, int>(f.Key.ToUpper(), f.Sum())) .OrderBy(g => g.Key) .ToArray(); | ||||||||
| Added: | ||||||||
| > > | ||||||||
| foreach (KeyValuePair <String, int> kvp in tallies) WriteLine (kvp.Key + '\t' + kvp.Value); } | ||||||||
| Changed: | ||||||||
| < < | } | |||||||
| > > | } | |||||||
| Result: | ||||||||
| Changed: | ||||||||
| < < | APPLE 2 BANANA 1 CAT 2 PEAR 3 | |||||||
| > > | APPLE 2 BANANA 1 CAT 2 PEAR 3 | |||||||
9. Calling a Python Script as a Shell Processusing System; using System.IO; using System.Diagnostics; | ||||||||
| Added: | ||||||||
| > > | ||||||||
| class MainClass { public static void Main(string[] args) | ||||||||
| Line: 237 to 290 | ||||||||
| psi.Arguments = "test.py"; psi.RedirectStandardOutput = true; psi.UseShellExecute = false; | ||||||||
| Added: | ||||||||
| > > | ||||||||
| using (Process p = new Process()) { p.StartInfo = psi; p.Start(); | ||||||||
| Added: | ||||||||
| > > | ||||||||
| WriteLine (p.StandardOutput.ReadToEnd()); } } | ||||||||
| Changed: | ||||||||
| < < | } | |||||||
| > > | } | |||||||
| -- Main.gslayden - 14 Nov 2008 | ||||||||
| Line: 1 to 1 | ||||||||
|---|---|---|---|---|---|---|---|---|
Using C# on Patas | ||||||||
| Line: 10 to 10 | ||||||||
1. Sample Programusing System; using System.Text; | ||||||||
| Deleted: | ||||||||
| < < | ||||||||
| static class MainClass { static void Main(String[] args) { WriteLine ("hello world"); } | ||||||||
| Changed: | ||||||||
| < < | } | |||||||
| > > | } | |||||||
| To compile and run on patas: | ||||||||
| Changed: | ||||||||
| < < | gmcs hello.cs mono hello.exe | |||||||
| > > | gmcs hello.cs mono hello.exe | |||||||
| The reason that this is a two-step process hints at the power of C# over interpreted languages; the first step "compiles" your source file into an intermediate byte-code called MSIL which is then processed by a runtime environment, called the Common Language Runtime, or CLR. This type of virtual instruction set is nothing new in computer science. But an innovation that Microsoft's CLR introduced was that this MSIL code is translated, on an as-needed basis, into actual native machine instructions for the target system. And it's retained in this optimal form as the program runs. This is called Just-in-time, or JIT compilation, and it means that your C# program runs with the performance of true native compilation. In fact, you can even use mono to execute, on patas, an MSIL binary produced by Microsoft Visual Studio 2008! Just copy the .exe file (in binary mode) to patas and run it like so: | ||||||||
| Changed: | ||||||||
| < < | mono compiled_by_msvc.exe | |||||||
| > > | mono compiled_by_msvc.exe | |||||||
To invoke a mono program from a CONDOR script, you should specify the full path to the mono executable:
universe = vanilla | ||||||||
| Line: 41 to 36 | ||||||||
| error = mystderr.txt log = /tmp/myuwid.log transfer_executable = false | ||||||||
| Changed: | ||||||||
| < < | queue | |||||||
| > > | queue | |||||||
2. DocumentationMicrosoft's detailed commercial-quality documentation on C# is available freely on the web. Of primary interest will be the extensive CLR (".NET Framework") class libraries, which provide a wide array of system services and data structures. The mono project also offers a set of documentation. | ||||||||
| Line: 53 to 48 | ||||||||
3. String Manipulationusing System; using System.Text; | ||||||||
| Changed: | ||||||||
| < < | static class MainClass { | |||||||
| > > | static class MainClass {
| |||||||
| static void Main(String[] args) { String s = "1.\tThis is a string."; String[] string_arr = s.Split('\t'); | ||||||||
| Deleted: | ||||||||
| < < | ||||||||
| Char[] trim_chars = ".:;,".ToCharArray(); String ns = string_arr[0].Trim(trim_chars); int i = ToInt32 (ns); | ||||||||
| Deleted: | ||||||||
| < < | ||||||||
| WriteLine (i); | ||||||||
| Deleted: | ||||||||
| < < | ||||||||
| foreach (String s2 in string_arr[1].Split()) WriteLine (s2.Replace('s','z')); } | ||||||||
| Changed: | ||||||||
| < < | } | |||||||
| > > | } | |||||||
| Result: | ||||||||
| Changed: | ||||||||
| < < | 1 Thiz iz a ztring. | |||||||
| > > | 1 Thiz iz a ztring. | |||||||
| Internally, all strings in C# are Unicode, as is the Char data type. A rich set of functions is provided in the Class Library's 'Encoding' namespace for reading and writing the various 8-bit character sets as input and output. | ||||||||
| Line: 91 to 75 | ||||||||
| using System.IO; using System.Linq; using System.Text; | ||||||||
| Changed: | ||||||||
| < < | static class MainClass { | |||||||
| > > | static class MainClass { | |||||||
| static void Main(String[] args) { String my_filename = "the_file.txt"; | ||||||||
| Deleted: | ||||||||
| < < | ||||||||
| String data = "Four score and seven years ago."; | ||||||||
| Changed: | ||||||||
| < < | // Write some data to the file int i = 0; | |||||||
| > > | // Write some data to the file int i = 0; | |||||||
| using (FileStream fs = new FileStream (my_filename, FileMode .Create, FileAccess .Write, FileShare .None)) | ||||||||
| Changed: | ||||||||
| < < | { using (StreamWriter sr = new StreamWriter (fs, GetEncoding (28591))) // Latin-1 | |||||||
| > > | { using (StreamWriter sr = new StreamWriter (fs, GetEncoding (28591))) // Latin-1 | |||||||
| { foreach (String s in data.Split()) sr.WriteLine((++i).ToString() + ". " + new String(s.ToCharArray().Reverse().ToArray())); } } | ||||||||
| Deleted: | ||||||||
| < < | ||||||||
| // Read data from the file using (FileStream fs = File.Open(my_filename, FileMode .Open, FileAccess .Read, FileShare .Read)) | ||||||||
| Changed: | ||||||||
| < < | { using (StreamReader sr = new StreamReader (fs, GetEncoding (28591))) | |||||||
| > > | { using (StreamReader sr = new StreamReader (fs, GetEncoding (28591))) | |||||||
| { String s; while (null = (s = sr.ReadLine())) | ||||||||
| Line: 122 to 99 | ||||||||
| } } } | ||||||||
| Changed: | ||||||||
| < < | } | |||||||
| > > | } | |||||||
| Result: | ||||||||
| Changed: | ||||||||
| < < | 1. ruoF 2. erocs 3. dna 4. neves 5. sraey 6. .oga | |||||||
| > > | 1. ruoF 2. erocs 3. dna 4. neves 5. sraey 6. .oga | |||||||
5. Hash Table of User-defined Objectsusing System; using System.Collections.Generic; using System.Linq; using System.Text; | ||||||||
| Deleted: | ||||||||
| < < | ||||||||
| public class MyObject { public Double d_x; public Double d_y; public Double d_z; | ||||||||
| Deleted: | ||||||||
| < < | ||||||||
| // constructor public MyObject (Double x_arg, Double y_arg, Double z_arg) { | ||||||||
| Changed: | ||||||||
| < < | d_x = x_arg; d_y = y_arg; d_z = z_arg; | |||||||
| > > | d_x = x_arg; d_y = y_arg; d_z = z_arg; | |||||||
| } }; | ||||||||
| Deleted: | ||||||||
| < < | ||||||||
| static class MainClass | ||||||||
| Changed: | ||||||||
| < < | { static void Main(String[] args) { Dictionary<String, MyObject > ht = new Dictionary<String, MyObject >(); ht.Add("object 1", new MyObject (3.0, 2.1, Math.PI)); ht.Add("object 2", new MyObject (Math.Sqrt(2.0), Math.Log(6.0,10.0), 3.2)); ht.Add("3rd object", new MyObject (2.1, 9.9, NaN )); | |||||||
| > > | {static void Main(String[] args) {Dictionary<String, MyObject> ht = new Dictionary<String, MyObject>(); ht.Add("object 1", new MyObject(3.0, 2.1, Math.PI)); ht.Add("object 2", new MyObject(Math.Sqrt(2.0), Math.Log(6.0,10.0), 3.2)); ht.Add("3rd object", new MyObject(2.1, 9.9, Double.NaN));
| |||||||
| WriteLine (ht["object 2"].d_x); } | ||||||||
| Changed: | ||||||||
| < < | } | |||||||
| > > | } | |||||||
6. Generic CollectionsC# has a number of powerful strongly-typed generic collection objects. For example: | ||||||||
| Line: 183 to 144 | ||||||||
| using System.Collections.Generic; using System.Linq; using System.Text; | ||||||||
| Deleted: | ||||||||
| < < | ||||||||
| static class MainClass { static void Main(String[] args) | ||||||||
| Line: 194 to 154 | ||||||||
| "Welfare, and secure the Blessings of Liberty to ourselves and our " + "Posterity, do ordain and establish this Constitution for the United " + "States of America."; | ||||||||
| Deleted: | ||||||||
| < < | ||||||||
| Char[] split_chars = new Char[] { '.', ',', ' ' }; String[] words = text.ToLower().Split(split_chars,StringSplitOptions.RemoveEmptyEntries); | ||||||||
| Deleted: | ||||||||
| < < | ||||||||
| // use a hash table to tally word-types: Dictionary<String, int> hash_tab = new Dictionary<String, int>(); foreach (String w in words) | ||||||||
| Line: 208 to 166 | ||||||||
| else hash_tab.Add(w,1); } | ||||||||
| Deleted: | ||||||||
| < < | ||||||||
| // demonstrate using LINQ to display 12 of the most common word-types plus their counts KeyValuePair <String, int>[] kvp_arr = hash_tab.OrderByDescending(e => e.Value).Take(12).ToArray(); // this is similar to 'list comprehension' in Python String s = kvp_arr.Aggregate(String.Empty,(av, e) => av + e.Key + "[" + e.Value + "] "); WriteLine (s); } | ||||||||
| Changed: | ||||||||
| < < | } | |||||||
| > > | } | |||||||
Result:
the[6] of[3] and[3] establish[2] for[2] states[2] to[2] united[2] perfect[1] promote[1] general[1] we[1] | ||||||||
| Line: 225 to 182 | ||||||||
| using System.IO; using System.Text; using System.Net; | ||||||||
| Deleted: | ||||||||
| < < | ||||||||
| static class MainClass { static void Main(String[] args) | ||||||||
| Line: 241 to 197 | ||||||||
| } WriteLine (s); } | ||||||||
| Changed: | ||||||||
| < < | } | |||||||
| > > | } | |||||||
8. LINQ OperationsOne of the exciting things about mono is that it includes support for one of the latest developments in Microsoft's C# 3.5, namely Language-Integrated Query (LINQ), and its supporting technologies (extension methods and lambda expressions). LINQ allows sophisticated and concise retrieval and manipulation operations to be executed on data collections via native C# language expressions. Categories of operations include aggregation, quantification, conversion, concatenation, retrieval, set (union, intersection, etc.), generation, grouping, join, ordering, projection, partitioning, and restriction (filtering). | ||||||||
| Line: 252 to 207 | ||||||||
| using System.Collections.Generic; using System.Linq; using System.Text; | ||||||||
| Deleted: | ||||||||
| < < | ||||||||
| static class MainClass { static void Main(string[] args) { String[] items = { "cat", "pear", "apple", "cat", "banana", "pear", "pear", "apple" }; | ||||||||
| Deleted: | ||||||||
| < < | ||||||||
| KeyValuePair [] tallies = items.GroupBy(k => k, e => 1) .Select(f => new KeyValuePair <String, int>(f.Key.ToUpper(), f.Sum())) .OrderBy(g => g.Key) .ToArray(); | ||||||||
| Deleted: | ||||||||
| < < | ||||||||
| foreach (KeyValuePair <String, int> kvp in tallies) WriteLine (kvp.Key + '\t' + kvp.Value); } | ||||||||
| Changed: | ||||||||
| < < | } | |||||||
| > > | } | |||||||
| Result: | ||||||||
| Changed: | ||||||||
| < < | APPLE 2 BANANA 1 CAT 2 PEAR 3 | |||||||
| > > | APPLE 2 BANANA 1 CAT 2 PEAR 3 | |||||||
9. Calling a Python Script as a Shell Processusing System; using System.IO; using System.Diagnostics; | ||||||||
| Deleted: | ||||||||
| < < | ||||||||
| class MainClass { public static void Main(string[] args) | ||||||||
| Line: 290 to 237 | ||||||||
| psi.Arguments = "test.py"; psi.RedirectStandardOutput = true; psi.UseShellExecute = false; | ||||||||
| Deleted: | ||||||||
| < < | ||||||||
| using (Process p = new Process()) { p.StartInfo = psi; p.Start(); | ||||||||
| Deleted: | ||||||||
| < < | ||||||||
| WriteLine (p.StandardOutput.ReadToEnd()); } } | ||||||||
| Changed: | ||||||||
| < < | } | |||||||
| > > | } | |||||||
| -- Main.gslayden - 14 Nov 2008 \ No newline at end of file | ||||||||
| Added: | ||||||||
| > > | If anybody has any info about how to get a good installation of mono onto ubuntu 8.0.x, I would really appreciate hearing about it. I put about 8 hours into trying to to install mono on Ubuntu, and that is about 6 more hours than I really had to spend. My chances of adopting c# as my language of choice on Linux are pretty slim unless I can a good install of mono to run on my home version of Linux. -- Main.andyf - 24 Nov 2008 | |||||||
| Line: 1 to 1 | ||||||||
|---|---|---|---|---|---|---|---|---|
Using C# on Patas | ||||||||
| Line: 27 to 27 | ||||||||
| The reason that this is a two-step process hints at the power of C# over interpreted languages; the first step "compiles" your source file into an intermediate byte-code called MSIL which is then processed by a runtime environment, called the Common Language Runtime, or CLR. This type of virtual instruction set is nothing new in computer science. But an innovation that Microsoft's CLR introduced was that this MSIL code is translated, on an as-needed basis, into actual native machine instructions for the target system. And it's retained in this optimal form as the program runs. This is called Just-in-time, or JIT compilation, and it means that your C# program runs with the performance of true native compilation. | ||||||||
| Added: | ||||||||
| > > | In fact, you can even use mono to execute, on patas, an MSIL binary produced by Microsoft Visual Studio 2008! Just copy the .exe file (in binary mode) to patas and run it like so:
mono compiled_by_msvc.exe | |||||||
To invoke a mono program from a CONDOR script, you should specify the full path to the mono executable:
universe = vanilla getenv = true | ||||||||
| Line: 1 to 1 | ||||||||
|---|---|---|---|---|---|---|---|---|
Using C# on Patas | ||||||||
| Line: 44 to 44 | ||||||||
| Microsoft's detailed commercial-quality documentation on C# is available freely on the web. Of primary interest will be the extensive CLR (".NET Framework") class libraries, which provide a wide array of system services and data structures. The mono project also offers a set of documentation.
Wikipedia entry on C# mono - Documentation Library MSDN - C# Language Reference MSDN - .NET Framework Class Library | ||||||||
| Added: | ||||||||
| > > | The examples below highlight basic programming tasks which are relevant for computational linguists. All have been developed and tested with mono on patas. | |||||||
3. String Manipulationusing System; using System.Text; | ||||||||
| Line: 67 to 69 | ||||||||
| } | ||||||||
| Added: | ||||||||
| > > | Result:
1 Thiz iz a ztring. | |||||||
| Internally, all strings in C# are Unicode, as is the Char data type. A rich set of functions is provided in the Class Library's 'Encoding' namespace for reading and writing the various 8-bit character sets as input and output. As in many other modern languages, the C# String type is immutable. This allows for significant internal optimizations in the runtime environment but can be inefficient when doing intensive mutation and other editing operations. For this reason, the CLR also implements the StringBuilder object, which allows a large amount of text to be gathered through appending. | ||||||||
| Line: 197 to 207 | ||||||||
| // demonstrate using LINQ to display 12 of the most common word-types plus their counts KeyValuePair <String, int>[] kvp_arr = hash_tab.OrderByDescending(e => e.Value).Take(12).ToArray(); | ||||||||
| Added: | ||||||||
| > > | // this is similar to 'list comprehension' in Python | |||||||
| String s = kvp_arr.Aggregate(String.Empty,(av, e) => av + e.Key + "[" + e.Value + "] "); WriteLine (s); } | ||||||||
| Line: 261 to 272 | ||||||||
| CAT 2 PEAR 3 | ||||||||
| Added: | ||||||||
| > > | 9. Calling a Python Script as a Shell Process using System;
using System.IO;
using System.Diagnostics;
class MainClass
{
public static void Main(string[] args)
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "python";
psi.Arguments = "test.py";
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
using (Process p = new Process())
{
p.StartInfo = psi;
p.Start();
Console.WriteLine(p.StandardOutput.ReadToEnd());
}
}
}
| |||||||
| -- Main.gslayden - 14 Nov 2008 \ No newline at end of file | ||||||||
| Line: 1 to 1 | ||||||||
|---|---|---|---|---|---|---|---|---|
Using C# on Patas | ||||||||
| Line: 205 to 205 | ||||||||
Result:
the[6] of[3] and[3] establish[2] for[2] states[2] to[2] united[2] perfect[1] promote[1] general[1] we[1] | ||||||||
| Changed: | ||||||||
| < < | 7. LINQ Operations | |||||||
| > > | 7. Access a Web Page using System;
using System.IO;
using System.Text;
using System.Net;
static class MainClass
{
static void Main(String[] args)
{
String s;
using (WebClient wc = new WebClient())
{
using (Stream str = wc.OpenRead("http://www.compling.washington.edu/compling/"))
{
using (StreamReader sr = new StreamReader(str))
s = sr.ReadToEnd();
}
}
Console.WriteLine(s);
}
}
8. LINQ Operations | |||||||
| One of the exciting things about mono is that it includes support for one of the latest developments in Microsoft's C# 3.5, namely Language-Integrated Query (LINQ), and its supporting technologies (extension methods and lambda expressions). LINQ allows sophisticated and concise retrieval and manipulation operations to be executed on data collections via native C# language expressions. Categories of operations include aggregation, quantification, conversion, concatenation, retrieval, set (union, intersection, etc.), generation, grouping, join, ordering, projection, partitioning, and restriction (filtering). | ||||||||
| Line: 1 to 1 | ||||||||
|---|---|---|---|---|---|---|---|---|
Using C# on Patas | ||||||||
| Line: 26 to 26 | ||||||||
| The reason that this is a two-step process hints at the power of C# over interpreted languages; the first step "compiles" your source file into an intermediate byte-code called MSIL which is then processed by a runtime environment, called the Common Language Runtime, or CLR. This type of virtual instruction set is nothing new in computer science. But an innovation that Microsoft's CLR introduced was that this MSIL code is translated, on an as-needed basis, into actual native machine instructions for the target system. And it's retained in this optimal form as the program runs. This is called Just-in-time, or JIT compilation, and it means that your C# program runs with the performance of true native compilation. | ||||||||
| Added: | ||||||||
| > > |
To invoke a mono program from a CONDOR script, you should specify the full path to the mono executable:
universe = vanilla getenv = true executable = /opt/mono/bin/mono arguments = "myprogram.exe -myarg1 -myarg2" input = mystdin.txt output = mystdout.txt error = mystderr.txt log = /tmp/myuwid.log transfer_executable = false queue | |||||||
2. DocumentationMicrosoft's detailed commercial-quality documentation on C# is available freely on the web. Of primary interest will be the extensive CLR (".NET Framework") class libraries, which provide a wide array of system services and data structures. The mono project also offers a set of documentation. | ||||||||
| Line: 1 to 1 | |||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Using C# on Patas | |||||||||||||||||
| Line: 30 to 30 | |||||||||||||||||
| Microsoft's detailed commercial-quality documentation on C# is available freely on the web. Of primary interest will be the extensive CLR (".NET Framework") class libraries, which provide a wide array of system services and data structures. The mono project also offers a set of documentation. | |||||||||||||||||
| Changed: | |||||||||||||||||
| < < | mono - Documentation Library MSDN - C# Language Reference MSDN - .NET Framework Class Library | ||||||||||||||||
| > > | Wikipedia entry on C# mono - Documentation Library MSDN - C# Language Reference MSDN - .NET Framework Class Library | ||||||||||||||||
3. String Manipulationusing System; using System.Text; | |||||||||||||||||
| Line: 58 to 58 | |||||||||||||||||
| As in many other modern languages, the C# String type is immutable. This allows for significant internal optimizations in the runtime environment but can be inefficient when doing intensive mutation and other editing operations. For this reason, the CLR also implements the StringBuilder object, which allows a large amount of text to be gathered through appending. | |||||||||||||||||
| Changed: | |||||||||||||||||
| < < | Strings can also be edited by converting them to an array of Char, as shown in the next example. The example also shows how a String object can be created from an array of Char. | ||||||||||||||||
| > > | Strings can also be edited by converting them to an array of Char, as shown in the next example, "Reading and Writing Files." The example also shows how a String object can be created from an array of Char. | ||||||||||||||||
4. Reading and Writing Filesusing System; using System.IO; | |||||||||||||||||
| Line: 139 to 139 | |||||||||||||||||
| } } | |||||||||||||||||
| Changed: | |||||||||||||||||
| < < | 6. LINQ Operations | ||||||||||||||||
| > > | 6. Generic CollectionsC# has a number of powerful strongly-typed generic collection objects. For example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
static class MainClass
{
static void Main(String[] args)
{
String text = "We the People of the United States, in Order to " +
"form a more perfect Union, establish Justice, ensure domestic " +
"Tranquility, provide for the common defence, promote the general " +
"Welfare, and secure the Blessings of Liberty to ourselves and our " +
"Posterity, do ordain and establish this Constitution for the United " +
"States of America.";
Char[] split_chars = new Char[] { '.', ',', ' ' };
String[] words = text.ToLower().Split(split_chars,StringSplitOptions.RemoveEmptyEntries);
// use a hash table to tally word-types:
Dictionary<String, int> hash_tab = new Dictionary<String, int>();
foreach (String w in words)
{
int v;
if (hash_tab.TryGetValue(w,out v))
hash_tab[w] = v+1;
else
hash_tab.Add(w,1);
}
// demonstrate using LINQ to display 12 of the most common word-types plus their counts
KeyValuePair<String, int>[] kvp_arr = hash_tab.OrderByDescending(e => e.Value).Take(12).ToArray();
String s = kvp_arr.Aggregate(String.Empty,(av, e) => av + e.Key + "[" + e.Value + "] ");
Console.WriteLine(s);
}
}
Result:
the[6] of[3] and[3] establish[2] for[2] states[2] to[2] united[2] perfect[1] promote[1] general[1] we[1] 7. LINQ Operations | ||||||||||||||||
| One of the exciting things about mono is that it includes support for one of the latest developments in Microsoft's C# 3.5, namely Language-Integrated Query (LINQ), and its supporting technologies (extension methods and lambda expressions). LINQ allows sophisticated and concise retrieval and manipulation operations to be executed on data collections via native C# language expressions. Categories of operations include aggregation, quantification, conversion, concatenation, retrieval, set (union, intersection, etc.), generation, grouping, join, ordering, projection, partitioning, and restriction (filtering). | |||||||||||||||||
| Line: 1 to 1 | ||||||||
|---|---|---|---|---|---|---|---|---|
Using C# on Patas | ||||||||
| Line: 25 to 25 | ||||||||
| mono hello.exe | ||||||||
| Changed: | ||||||||
| < < | The reason that this is a two-step process hints at the power of C# over interpreted languages; the first step "compiles" your source file into an intermediate byte-code called MSIL which is later processed by a runtime environment, called the Common Language Runtime, or CLR. This type of virtual instruction set is nothing new in computer science. But an innovation that Microsoft's CLR introduced was that this MSIL code is translated, on an as-needed basis, into actual native machine instructions for the target system. And it's retained in this optimal form as the program runs. This is called Just-in-time, or JIT compilation, and it means that your C# program runs with the performance of true native compilation. | |||||||
| > > | The reason that this is a two-step process hints at the power of C# over interpreted languages; the first step "compiles" your source file into an intermediate byte-code called MSIL which is then processed by a runtime environment, called the Common Language Runtime, or CLR. This type of virtual instruction set is nothing new in computer science. But an innovation that Microsoft's CLR introduced was that this MSIL code is translated, on an as-needed basis, into actual native machine instructions for the target system. And it's retained in this optimal form as the program runs. This is called Just-in-time, or JIT compilation, and it means that your C# program runs with the performance of true native compilation. | |||||||
2. DocumentationMicrosoft's detailed commercial-quality documentation on C# is available freely on the web. Of primary interest will be the extensive CLR (".NET Framework") class libraries, which provide a wide array of system services and data structures. The mono project also offers a set of documentation. | ||||||||
| Line: 53 to 53 | ||||||||
| } } | ||||||||
| Added: | ||||||||
| > > | Internally, all strings in C# are Unicode, as is the Char data type. A rich set of functions is provided in the Class Library's 'Encoding' namespace for reading and writing the various 8-bit character sets as input and output. As in many other modern languages, the C# String type is immutable. This allows for significant internal optimizations in the runtime environment but can be inefficient when doing intensive mutation and other editing operations. For this reason, the CLR also implements the StringBuilder object, which allows a large amount of text to be gathered through appending. Strings can also be edited by converting them to an array of Char, as shown in the next example. The example also shows how a String object can be created from an array of Char. | |||||||
4. Reading and Writing Filesusing System; using System.IO; | ||||||||
| Line: 1 to 1 | ||||||||
|---|---|---|---|---|---|---|---|---|
Using C# on Patas | ||||||||
| Line: 20 to 20 | ||||||||
| } | ||||||||
| Changed: | ||||||||
| < < | To compile on patas: | |||||||
| > > | To compile and run on patas: | |||||||
gmcs hello.cs mono hello.exe | ||||||||
| Line: 91 to 91 | ||||||||
| } } | ||||||||
| Added: | ||||||||
| > > |
Result:
1. ruoF 2. erocs 3. dna 4. neves 5. sraey 6. .oga | |||||||
5. Hash Table of User-defined Objectsusing System; using System.Collections.Generic; | ||||||||
| Line: 1 to 1 | ||||||||
|---|---|---|---|---|---|---|---|---|
| Added: | ||||||||
| > > |
Using C# on PatasC# is a powerful general-purpose programming language originally developed by Microsoft. After its approval as a standard by ECMA, it has been independently re-engineered as an open-source implementation which is available on many platforms, including Linux. It is installed and available for use on patas. Because the mono implementation is extremely compatible, Console-oriented C# programs developed on Windows machines (i.e. with Visual Studio) will typically run on mono without changing the source code. (It is beyond the scope of this document to discuss the compatibility of graphical programs.) C# programs operate in a fully sandboxed runtime environment which provides garbage-collection. As in Java, Python, and other high-level general-purpose languages, disposal of unused memory objects is tracked and managed by the system, relieving an enormous burden from the application programmer.1. Sample Programusing System;
using System.Text;
static class MainClass
{
static void Main(String[] args)
{
Console.WriteLine("hello world");
}
}
To compile on patas:
gmcs hello.cs mono hello.exeThe reason that this is a two-step process hints at the power of C# over interpreted languages; the first step "compiles" your source file into an intermediate byte-code called MSIL which is later processed by a runtime environment, called the Common Language Runtime, or CLR. This type of virtual instruction set is nothing new in computer science. But an innovation that Microsoft's CLR introduced was that this MSIL code is translated, on an as-needed basis, into actual native machine instructions for the target system. And it's retained in this optimal form as the program runs. This is called Just-in-time, or JIT compilation, and it means that your C# program runs with the performance of true native compilation. 2. DocumentationMicrosoft's detailed commercial-quality documentation on C# is available freely on the web. Of primary interest will be the extensive CLR (".NET Framework") class libraries, which provide a wide array of system services and data structures. The mono project also offers a set of documentation. mono - Documentation LibraryMSDN - C# Language Reference MSDN - .NET Framework Class Library 3. String Manipulationusing System;
using System.Text;
static class MainClass
{
static void Main(String[] args)
{
String s = "1.\tThis is a string.";
String[] string_arr = s.Split('\t');
Char[] trim_chars = ".:;,".ToCharArray();
String ns = string_arr[0].Trim(trim_chars);
int i = Convert.ToInt32(ns);
Console.WriteLine(i);
foreach (String s2 in string_arr[1].Split())
Console.WriteLine(s2.Replace('s','z'));
}
}
4. Reading and Writing Filesusing System;
using System.IO;
using System.Linq;
using System.Text;
static class MainClass
{
static void Main(String[] args)
{
String my_filename = "the_file.txt";
String data = "Four score and seven years ago.";
// Write some data to the file
int i = 0;
using (FileStream fs = new FileStream(my_filename, FileMode.Create, FileAccess.Write, FileShare.None))
{
using (StreamWriter sr = new StreamWriter(fs, Encoding.GetEncoding(28591))) // Latin-1
{
foreach (String s in data.Split())
sr.WriteLine((++i).ToString() + ". " + new String(s.ToCharArray().Reverse().ToArray()));
}
}
// Read data from the file
using (FileStream fs = File.Open(my_filename, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (StreamReader sr = new StreamReader(fs, Encoding.GetEncoding(28591)))
{
String s;
while (null != (s = sr.ReadLine()))
Console.WriteLine(s);
}
}
}
}
5. Hash Table of User-defined Objectsusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class MyObject
{
public Double d_x;
public Double d_y;
public Double d_z;
// constructor
public MyObject(Double x_arg, Double y_arg, Double z_arg)
{
d_x = x_arg; d_y = y_arg; d_z = z_arg;
}
};
static class MainClass
{
static void Main(String[] args)
{
Dictionary<String, MyObject> ht = new Dictionary<String, MyObject>();
ht.Add("object 1", new MyObject(3.0, 2.1, Math.PI));
ht.Add("object 2", new MyObject(Math.Sqrt(2.0), Math.Log(6.0,10.0), 3.2));
ht.Add("3rd object", new MyObject(2.1, 9.9, Double.NaN));
Console.WriteLine(ht["object 2"].d_x);
}
}
6. LINQ OperationsOne of the exciting things about mono is that it includes support for one of the latest developments in Microsoft's C# 3.5, namely Language-Integrated Query (LINQ), and its supporting technologies (extension methods and lambda expressions). LINQ allows sophisticated and concise retrieval and manipulation operations to be executed on data collections via native C# language expressions. Categories of operations include aggregation, quantification, conversion, concatenation, retrieval, set (union, intersection, etc.), generation, grouping, join, ordering, projection, partitioning, and restriction (filtering). LINQ is an expansive topic in its own right. The following simple example gives a glimpse of what is possible.using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
static class MainClass
{
static void Main(string[] args)
{
String[] items = { "cat", "pear", "apple", "cat", "banana", "pear", "pear", "apple" };
KeyValuePair[] tallies = items.GroupBy(k => k, e => 1)
.Select(f => new KeyValuePair<String, int>(f.Key.ToUpper(), f.Sum()))
.OrderBy(g => g.Key)
.ToArray();
foreach (KeyValuePair<String, int> kvp in tallies)
Console.WriteLine(kvp.Key + '\t' + kvp.Value);
}
}
Result:
APPLE 2 BANANA 1 CAT 2 PEAR 3-- Main.gslayden - 14 Nov 2008 | |||||||