[IronPython] How to convert between C# and IronPython

Jeff Hardy jdhardy at gmail.com
Thu Aug 5 04:37:17 CEST 2010


D'oh, forgot to remove some semicolons (Python will ignore them anyway):

>         from System.Security.Cryptography import RNGCryptoServiceProvider
>         from System import BitConverter, Random
>
>         rndC = RNGCryptoServiceProvider()
>         rndC.GetBytes(rndBytes)
>         seed = BitConverter.ToInt32(rndBytes, 0)
>         rand = Random(seed)
>
>         return rand.Next(min, max)

- Jeff

On Wed, Aug 4, 2010 at 8:36 PM, Jeff Hardy <jdhardy at gmail.com> wrote:
> Hi Andrew,
> Jimmy has already shown how to create a byte array, so I'll translate the rest:
>
>         from System.Security.Cryptography import RNGCryptoServiceProvider
>         from System import BitConverter, Random
>
>         rndC = RNGCryptoServiceProvider()
>         rndC.GetBytes(rndBytes)
>         seed = BitConverter.ToInt32(rndBytes, 0);
>         rand = Random(seed);
>
>         return rand.Next(min, max);
>
> In general, to convert C# to Python:
>
> drop semicolons ( ; )
> drop braces ( { } )
> drop 'new'
> drop types
> drop namespaces
>
> add 'def' to functions
> change foreach to for
> remove parens ( ( ) ) from if, for, while
> add colons ( : ) after class, def, if, while
>
> change 'using Foo' to 'from Foo import *' (bad style in Python, but it works)
>
> and a few other, more complicated steps
>
> Heck, writing a script to do it wouldn't be all that hard, and it
> shows just how much noise is in C#.
>
> - Jeff
>
> On Wed, Aug 4, 2010 at 5:02 PM, Andrew Evans <evans.d.andrew at gmail.com> wrote:
>> Hello
>>
>> I am trying to follow a tutorial written for silverlight and C# and convert
>> the code to Iron Python. However I am unsure about how to use this code as
>> IronPython
>>
>>         Byte[] rndBytes = new Byte[10];
>>
>>         RNGCryptoServiceProvider rndC = new RNGCryptoServiceProvider();
>>         rndC.GetBytes(rndBytes);
>>         int seed = BitConverter.ToInt32(rndBytes, 0);
>>         Random rand = new Random(seed);
>>
>>         return rand.Next(min, max);
>>
>> what modules do I need. How do I use Byte in IronPython etc
>>
>> Any ideas advice *cheers
>>
>>
>>
>> _______________________________________________
>> Users mailing list
>> Users at lists.ironpython.com
>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>>
>>
>



More information about the Ironpython-users mailing list