944,134 Members | Top Members by Rank

Ad:
  • Python Code Snippet
  • Views: 6576
  • Python RSS
1

A Taste of Boo

by on Nov 10th, 2005
Boo is new, well at least relatively new. If you are familiar with Python and C#, you can feel right at home. Most of Boo is written in C# by a Python devotee. Boo has static typing, but also mimics dynamic typing by inference. This way you are not in a complete straight-jacket. It runs in the NET or MONO environment, can be interpreted or compiled and a Python-like shell is available too. Boo is open source, developed enough to allow for serious experimentation. For those of you who have used the SharpDevelop IDE for C# there is a Boo plug-in. This code snippet will compile to a 10k executable file and can be distributed along with a 64k Boo.Lang.dll (runtime DLL) to other .NET or Mono computers. A utility to convert Boo to C#, or C# to Boo has been published.
Python Code Snippet (Toggle Plain Text)
  1. # Just a taste of Boo ...
  2. # Boo is a language that can be interpreted (with Booi.exe) or compiled
  3. # (with Booc.exe), there is even a shell similar to the Python shell (Booish.exe).
  4. # Boo works in the NET or MONO environment on Windows or Unix systems.
  5. # You may notice the Python like syntax and some concepts borrowed from C#, Perl
  6. # and Ruby. It is easy to translate Python to Boo, but there are differences!
  7. # Boo is open source and you can download it from: http://boo.codehaus.org/
  8. # You do need the Microsoft NET or open source MONO environment on your machine.
  9. # Most any programmer's editor will do, or download Booxw.exe from the above site.
  10. # The Boo plugin for the SharpDevelop IDE works well too.
  11. # tested with BOO_0.7.0 vegaseat 09nov2005
  12.  
  13. /*
  14. one line comments start with a Python # or a C //
  15. you are looking at an example of a multiline comment
  16. */
  17.  
  18. # any imports have to be done first ...
  19. import System.IO // for Directory
  20.  
  21. # next come all classes and functions ...
  22. # (at least at this point in time)
  23.  
  24. # the indented lines are part of the function
  25. # Boo uses the Python indented statement block
  26. # the function's argument types have to be declared
  27. # function return types can be inferred
  28.  
  29. def convertFahrenheit2Celsius(fahrenheit as double):
  30. """this would be the document string for the function"""
  31. celcius = 0.555 * (fahrenheit - 32)
  32. return celcius
  33.  
  34.  
  35. # simple stuff first ...
  36. print "Show a result the Python way ..."
  37. print "Simple math like 12345679*63 =", 12345679*63
  38.  
  39. print "\n... or using Boo string interpolation ..."
  40. print "Simple math like 12345679*63 = ${12345679*63}"
  41.  
  42. # print just an empty line
  43. print
  44.  
  45. print "Display numbers 0 to 9 ..."
  46. # the indentation makes the print statement part of the loop
  47. for number in range(10):
  48. print number
  49.  
  50. print
  51.  
  52. print "Display numbers 0 to 9 on one line ..."
  53. # print is a macro of System.Console.WriteLine() and adds a newline
  54. # use System.Console.Write() to stay on the same line
  55. # (using a comma to prevent the newline like in Python gives an error with Boo)
  56.  
  57. for number in range(10):
  58. //print number,
  59. System.Console.Write(number)
  60.  
  61. print; print
  62.  
  63. # just in case you think Boo has only for loops
  64. # there is a while loop too
  65. print "Count from 10 to 15, skip 13 ..."
  66. k = 10
  67. while k <= 15:
  68. unless k == 13:
  69. print k
  70. k++
  71.  
  72. # print 50 dashes
  73. print "-"*50
  74.  
  75. # a little more complex this time
  76. # Boo does have its roots in the C# language
  77. # so we are using C# format specifiers in the output
  78. # (the Python C-like format specifier % does not work!)
  79. print "Square root of integers 0 to 9 formatted as a float with 4 decimals:"
  80. for value in range(10):
  81. squareRoot = System.Math.Sqrt(value)
  82. System.Console.WriteLine("sqrt({0:D1}) = {1:F4}", value, squareRoot)
  83.  
  84. print
  85.  
  86. print "A not so typical for loop:"
  87. for food in "spam", "eggs", "cumquats":
  88. print "I love", food
  89.  
  90. print
  91.  
  92. # strings can be enclosed in " or '
  93. # (only " allows string interpolation)
  94. animal = "hippopotamus"
  95. print "this is the string = ", animal
  96. print "length of string = ", len(animal)
  97.  
  98. # a short intro to string slicing
  99. # a little cryptic at first blush, but very powerful
  100. # [begin : < end] note: end is exclusive
  101. # defaults are begin = 0, end = length
  102. # Python also has step, which is not implemented yet in Boo ...
  103. print "exclude first 3 char = ", animal[3: ]
  104. print "exclude last 4 char = ", animal[:-4]
  105.  
  106. print "reverse the string:"
  107. for c in reversed(animal):
  108. System.Console.Write(c)
  109.  
  110. print; print
  111.  
  112. print "reverse first name and last name:"
  113. name = "Ferdinand Porsche"
  114. # Boo has built-in regex support, \s is space
  115. firstname, lastname = @/\s/.Split(name)
  116. print lastname, firstname
  117.  
  118. # or you can do it the no-regex way ...
  119. firstname, lastname = name.Split() # default is whitespace
  120. print lastname, firstname
  121.  
  122. print
  123.  
  124. # Boo has lists similar to Python
  125. list1 = List(range(8))
  126. print list1.GetType() # Boo.Lang.List
  127. print list1 # [0, 1, 2, 3, 4, 5, 6, 7]
  128. print "number of items in list = ", len(list1)
  129.  
  130. # lists can be sliced too ...
  131. print "exclude first 3 items in list = ", list1[3:]
  132. print "first 3 items in list = ", list1[:3]
  133. print "exclude last 2 items in list = ", list1[:-2]
  134.  
  135. # create a list of all items above value 3 ...
  136. # (do behaves like a function)
  137. list2 = list1.Collect() do (item as int):
  138. return item > 3
  139. print "list1 = ", list1
  140. print "all items > 3 = ", list2
  141.  
  142. # add to the list with Add or Push ...
  143. list1.Add("any")
  144. list1.Push("type")
  145.  
  146. # add another list ...
  147. list1.Extend(["can", "be added", 3.1415])
  148.  
  149. print "added more items:", list1
  150.  
  151. # index is zero based
  152. print "'any' is at index", list1.IndexOf('any')
  153.  
  154. # find 'can' ...
  155. if 'can' in list1:
  156. print "'can' has been found"
  157. # or ...
  158. print "indeed 'can' has been found" if 'can' in list1
  159.  
  160. # use list comprehension ...
  161. list3 = [k for k in list1 if k isa string]
  162. print "just the string items: ", list3 # [any, type, can, be added]
  163.  
  164. # join the strings in list3 ...
  165. str1 = list3.Join(" ")
  166. print "join the strings: ", str1 # any type can be added
  167.  
  168. print
  169.  
  170. # Boo has arrays that are similar to Python tuples
  171. arr1 = array('ABCDE')
  172. print arr1 # System.Object[]
  173. print arr1.GetType() # System.Object[]
  174. print List(arr1) # [A, B, C, D, E]
  175. print arr1[1] # B
  176.  
  177. if arr1[1] isa string:
  178. print arr1[1]
  179. else:
  180. print arr1[1].GetType() # System.Char
  181.  
  182. for item in arr1:
  183. print item, cast(int, item) # prints char and ASCII number eg. A 65
  184.  
  185. print
  186.  
  187. print "Test boolean results ..."
  188. print "Is 3 > 5? Result =", 3 > 5 # False, yes False and not false as documented
  189. print "Is 3 < 5? Result =", 3 < 5 # True
  190.  
  191. print
  192.  
  193. /*
  194. Duck typing was coined by Dave Thomas in the Ruby community:
  195. "if it walks like a duck, and talks like a duck, then it is a duck".
  196. This is a way to fake being a dynamic language like Python.
  197. Useful, if you don't exactly know the type returned by an external call.
  198. */
  199.  
  200. d as duck // d accepts different types
  201.  
  202. d = 21 // sets d to an integer
  203. print d
  204. d += 12 // it can do everything an integer does
  205. print d
  206.  
  207. d = "hypocrite" // sets d to a string
  208. print d
  209. d = d.ToUpper() // now it can do everything a string does
  210. print d
  211.  
  212. print
  213.  
  214. # show files of a given extension from a given folder
  215. ftype = "*.boo"
  216. folder = "./" // current folder
  217. //folder = "D:/Boo/BooTest/" // or specified folder
  218.  
  219. // print "Show string the Python way ..."
  220. // print "All " + ftype + " files in folder " + folder + " :"
  221.  
  222. // ... or the Boo way ...
  223. print "All ${ftype} files in folder ${folder} :"
  224.  
  225. for fname in Directory.GetFiles(folder, ftype):
  226. print fname
  227.  
  228. print
  229.  
  230. # let's use the function we defined at the beginning
  231. # (make sure you define/create the function before you call it)
  232. print "A Fahrenheit to Celcius table:"
  233. # range is from -40 to < 220 in steps of 10
  234. for tf in range(-40, 220, 10):
  235. System.Console.WriteLine("{0:F1}F = {1:F1}C", tf, convertFahrenheit2Celsius(tf))
  236.  
  237. print
  238.  
  239. print "A more limited table:"
  240. # another variation of the for loop ...
  241. for tf in -40,0,32,98.6:
  242. System.Console.WriteLine("{0:F1}F = {1:F1}C", tf, convertFahrenheit2Celsius(tf))
  243.  
  244. prompt("Press enter to exit ...") // optional console wait
Comments on this Code Snippet
Nov 10th, 2005
0

Re: A Taste of Boo

Have to get the code field edited to accept Python syntax or the indentations are lost!!!!!
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Dec 11th, 2005
0

Re: A Taste of Boo

Actually Boo allows this more Python like construct:
print "{0:f1}F = {1:f1}C" % (tf, convertFahrenheit2Celsius(tf))
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Message:
Previous Thread in Python Forum Timeline: freevo uses python
Next Thread in Python Forum Timeline: A problem/Bug with libglade





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC