Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
May 08, 2015 Moving from Python to D | ||||
---|---|---|---|---|
| ||||
Hello, Sorry to bother you with something trivial, but I am having trouble translating a block of code I wrote in Python over to D. Everything else I've figured out so far. Could someone help me understand how to get this right? Here's the python: scene = scenes["title"] while 1 == 1: next_choice = None paths = scene["paths"] description = scene["description"] lines = string.split("\n") for line in lines: if len(line > 55): w = textwrap.TextWrapper(width=45, break_long_words=False) line = '\n'.join(w.wrap(line)) decription += line +"\n" print description #Shows the choices for i in range(0, len(paths)): path = paths[i] menu_item = i + 1 print "\t", menu_item, path["phrase"] print "\t(0 Quit)" #Get user selection prompt = "Make a selection ( 0 - %i): \n" % len(paths) while next_choice == None: try: choice = raw_input(prompt) menu_selection = int(choice) if menu_selection == 0: next_choice = "quit" else: index = menu_selection - 1 next_choice = paths[ index ] except(IndexError, ValueError): print choice, "is not a valid selection" if next_choice == "quit": print "Thanks for playing." sys.exit() else: scene = scenes[ next_choice["go_to"] ] print "You decided to:", next_choice["phrase"], "\n" if sys.platform == 'win32': os.system("cls") else: os.system("clear") I've got the very last piece set up, using consoleD to use clearScreen(), but The rest I'm not sure how to translate. Sorry for my incompetence in advance. |
May 08, 2015 Re: Moving from Python to D | ||||
---|---|---|---|---|
| ||||
Posted in reply to avarisclari | I'm not really sure exactly what parts are the issue, but I'll point out what I can: On 05/07/2015 11:24 PM, avarisclari wrote: > Hello, > > Sorry to bother you with something trivial, but I am having trouble > translating a block of code I wrote in Python over to D. Everything else > I've figured out so far. Could someone help me understand how to get > this right? > > Here's the python: > > scene = scenes["title"] It looks like scenes is a dictionary that stores dictionaries of strings? If so, then in D, scenes would be declared like this: string[string][string] scenes; Then the above line would be: auto scene = scenes["title"] > > while 1 == 1: In D, while(true) is probably prefered, but that's just a matter of style. 1==1 will work too. > next_choice = None > paths = scene["paths"] > description = scene["description"] > lines = string.split("\n") > Phobos (D's standard library) has a split: http://dlang.org/phobos/std_array.html#split Also one specifically for splitting lines: http://dlang.org/phobos/std_string.html#splitLines They're easier to use than it looks: auto lines = description.split("\n"); or better yet (also handles mac and win-style properly): auto lines = description.splitLines(); > for line in lines: Standard foreach: for(ref line; lines) { > if len(line > 55): > w = textwrap.TextWrapper(width=45, break_long_words=False) Exists in Phobos: http://dlang.org/phobos/std_string.html#wrap I've never actually used it myself though. > line = '\n'.join(w.wrap(line)) Join is easy: http://dlang.org/phobos/std_array.html#join result = (whatever array or range you want to join).join("\n") But really, I don't think you'll need this entire loop at all, though. I would just strip all the newlines and feed the result to Phobos's wrap(). Probably something like: auto description = scene["description"].replace("\n", " ").wrap(45); That should be all you need for the word wrapping. > decription += line +"\n" D uses ~ to concatenate strings instead of +. In D, + is just for mathematical addition. > print description > writeln(description); > > > #Shows the choices > for i in range(0, len(paths)): foreach(i; 0..paths.length) { > path = paths[i] > menu_item = i + 1 > print "\t", menu_item, path["phrase"] writeln("\t", menu_item, path["phrase"]); > > print "\t(0 Quit)" > > #Get user selection // Get user selection > > prompt = "Make a selection ( 0 - %i): \n" % len(paths) This is in phobos's std.conv: http://dlang.org/phobos/std_conv.html So, either: auto prompt = "Make a selection ( 0 - " ~ paths.length.to!string() ~ "): \n"; or: auto prompt = text("Make a selection ( 0 - ", paths.length, ~ "): \n") > > while next_choice == None: > try: > choice = raw_input(prompt) The basic equivalent to raw_input would be readln(): http://dlang.org/phobos/std_stdio.html#readln But, the library Scriptlike would make this super easy, thanks to a contributer: https://github.com/Abscissa/scriptlike Pardon the complete lack of styling in the docs (my fault): http://semitwist.com/scriptlike/interact.html So, it'd be: import scriptlike.interact; auto choice = userInput!int(prompt); > menu_selection = int(choice) > > if menu_selection == 0: > next_choice = "quit" > else: > index = menu_selection - 1 > next_choice = paths[ index ] > > except(IndexError, ValueError): > print choice, "is not a valid selection" > > if next_choice == "quit": > print "Thanks for playing." > sys.exit() I forget the function to exist a program, but if this is in your main() function, then you can just: return; > else: > scene = scenes[ next_choice["go_to"] ] > print "You decided to:", next_choice["phrase"], "\n" > if sys.platform == 'win32': > os.system("cls") > else: > os.system("clear") > > I've got the very last piece set up, using consoleD to use > clearScreen(), but The rest I'm not sure how to translate. Sorry for my > incompetence in advance. |
May 08, 2015 Re: Moving from Python to D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky | On 05/08/2015 12:06 AM, Nick Sabalausky wrote:
> On 05/07/2015 11:24 PM, avarisclari wrote:
>>
>> scene = scenes["title"]
>
> It looks like scenes is a dictionary that stores dictionaries of
> strings? If so, then in D, scenes would be declared like this:
>
> string[string][string] scenes;
>
> Then the above line would be:
>
> auto scene = scenes["title"]
>
BTW, D calls them "Associative Arrays" or "AA", instead of "dictionary". I *think* "dictionary" is what Python calls them, but I could be wrong.
|
May 08, 2015 Re: Moving from Python to D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky | On Friday, 8 May 2015 at 04:08:03 UTC, Nick Sabalausky wrote:
> On 05/08/2015 12:06 AM, Nick Sabalausky wrote:
>> On 05/07/2015 11:24 PM, avarisclari wrote:
>>>
>>> scene = scenes["title"]
>>
>> It looks like scenes is a dictionary that stores dictionaries of
>> strings? If so, then in D, scenes would be declared like this:
>>
>> string[string][string] scenes;
>>
>> Then the above line would be:
>>
>> auto scene = scenes["title"]
>>
>
> BTW, D calls them "Associative Arrays" or "AA", instead of "dictionary". I *think* "dictionary" is what Python calls them, but I could be wrong.
Yup, thank you for the help.
|
May 08, 2015 Re: Moving from Python to D | ||||
---|---|---|---|---|
| ||||
Posted in reply to avarisclari | "Moving from Python to D" always a good idea ;-) |
May 08, 2015 Re: Moving from Python to D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris | On Friday, 8 May 2015 at 14:13:43 UTC, Chris wrote: > "Moving from Python to D" > > always a good idea ;-) You might be interested in this: http://wiki.dlang.org/Programming_in_D_for_Python_Programmers http://d.readthedocs.org/en/latest/examples.html#plotting-with-matplotlib-python |
May 08, 2015 Re: Moving from Python to D | ||||
---|---|---|---|---|
| ||||
Posted in reply to avarisclari Attachments:
| On Fri, 2015-05-08 at 03:24 +0000, avarisclari via Digitalmars-d-learn wrote: > Hello, > > Sorry to bother you with something trivial, but I am having trouble translating a block of code I wrote in Python over to D. Everything else I've figured out so far. Could someone help me understand how to get this right? > > Here's the python: This is Python 2 not Python 3! Do you have a definition for scenes so I can run the code. Are there any tests of any sort? For me Step 1 is to make this Python 3 and much more idiomatic Python. Doing this will also make the route to a D implementation far more obvious. -- Russel. ============================================================================= Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder@ekiga.net 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel@winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder |
Copyright © 1999-2021 by the D Language Foundation