Re: [Tutor] Project suggestions
On 13 March 2014 02:29, Scott W Dunning wrote: > Hey Everyone, > > I just got through doing a Guess-the-number script and was looking for > something else to practice on. Do any of you have any suggestions on some > things I could work on? Keep in mind I am not only extremely new to python > I am new to programming. Thanks for any suggestions!!! > > Scott > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > There's a great list of Beginner Programming Challenges on Ubuntuforums that are a good way to get you thinking. http://ubuntuforums.org/showthread.php?t=1714324 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Project suggestions :p:
On 03/12/2014 10:29 PM, Scott W Dunning wrote: Hey Everyone, I just got through doing a Guess-the-number script and was looking for something else to practice on. Do any of you have any suggestions on some things I could work on? Keep in mind I am not only extremely new to python I am new to programming. Thanks for any suggestions!!! Scott ___ I find that in conjunction with a good tutorial I like to have some problems to solve. I have been enjoying the puzzles on the checkio.org site recently. There is a great beginners area called "The Library" with very simple tasks, then other more challenging tasks in other levels. As you solve problems you gain points which unlock more challenging levels. thomas ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] c++ on python
On Wed, 2014-03-12 at 22:05 +, Alan Gauld wrote: > On 12/03/14 16:49, Stefan Behnel wrote: > > Alan Gauld, 12.03.2014 10:11: > >> If it were a library then you would have to call > >> the individual C++ functions directly using > >> something like ctypes, which is usually more > >> complex. > > > > ctypes won't talk to C++, but Cython can do it quite easily. > > I thought it would work provided the interface functions > were declared as C functions? That might involve > writing a wrapper around it but that is usually > trivial if you have to compile the source anyway. ctypes (and CFFI) talks quite happily to C++ functions as long as they are declared with C linkage (so as to avoid any "name mangling"): export "C" x f(…){…} makes f accessible via ctypes if f is in a shared object/dynamic link library. -- Russel. = Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.win...@ekiga.net 41 Buckmaster Roadm: +44 7770 465 077 xmpp: rus...@winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder signature.asc Description: This is a digitally signed message part ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] c++ on python
On 7 March 2014 14:29, Gabriele Brambilla wrote: > Hi, > in the next days I will receive a c++ code that I would like to run in > python (http://docs.python.org/2/extending/index.html). > It should be self consistent (no extraroutines). > I want to be ready to use it... Has someone some C++ code examples available > that I can try to run easily before getting that code? Hi Gabriele, I recommend either Alan's suggestion of running the C++ code as an external application (with e.g. subprocess) or using cython to connect the C++ code to Python. Which is appropriate depends on what you need to do and how much flexibility you need from within the Python code. I learned C and C++ before Python and consider myself reasonably proficient in all 3 languages and capable of understanding (dis-)assembly and linkage and so on and I still find ctypes to be a bit of handful (I have less experience of cffi). Using Cython or just compiling the C++ code with an external compiler is much better for the error checking and so on. Using ctypes you get very little error-checking, just seg faults and corruption. Oscar ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Loop Issue
On 03/13/2014 12:40 AM, Danny Yoo wrote: The context is the beginning of the thread: https://mail.python.org/pipermail/tutor/2014-March/100543.html with the loop: ### while health != 0: ... ### The point, and reason why this loop was (potentially) infinite, is that the condition was false. Should be <0 instead ('cause health points are not removed one by one, so that a health value of 0 exactly can be jumped over). d ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Project suggestions
On 03/13/2014 03:29 AM, Scott W Dunning wrote: Hey Everyone, I just got through doing a Guess-the-number script and was looking for something else to practice on. Do any of you have any suggestions on some things I could work on? Keep in mind I am not only extremely new to python I am new to programming. Thanks for any suggestions!!! Scott There are many variants you can introduce in this game, small or big, that may drive to learn or practice notions of python and programming: * have the out-of-interval hint depend on actual present interval (rather than initial) * have the player choose the initial interval; or have random, in a sensible way * reverse the game: the human choose the secret number, and the machine plays * have both the 'chooser' and the 'guesser' be played by the computer * make the game (the game logic and rule) defined in code, instead of spread as a number of apparently unrelated data (the interval, the secret number...) and functions (the guesser guesses, the whole playing loop...) Technically, the latter point is sometimes called "reification" (literally thingification). We constantly do that in programming, especially (OO) object-oriented. It is argually the core of preperly structuring an app. In this case, it is not necessary, since the game (the gaming machine) is the whole app. However, you can do it as if it where a component or dimension of a bigger software system. As if your whole present code were embedded in: guess_the_number = { ... } and all top elements defined in your code are defining properties of the game. (Am I clear?) d ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Project suggestions
On 03/13/2014 07:17 AM, Ben Finney wrote: Scott W Dunning writes: I just got through doing a Guess-the-number script and was looking for something else to practice on. Do any of you have any suggestions on some things I could work on? Keep in mind I am not only extremely new to python I am new to programming. Thanks for any suggestions!!! You've received the suggestion, but I'll recommend it again: Newcomers to Python should work through the Python Tutorial http://docs.python.org/3/tutorial/>. Run each example yourself, experiment at the interactive Python prompt to understand it, before continuing through the tutorial. Personly, I don't find this tutorial good at all. It is good enough for already programmers, especially ones knowing which share many *implicit* principles and notions with python; it was certainly firstly made for C/Unix hackers, and AFAIK hasn't much changed. d ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Project suggestions
On 03/13/2014 03:29 AM, Scott W Dunning wrote: Hey Everyone, I just got through doing a Guess-the-number script and was looking for something else to practice on. Do any of you have any suggestions on some things I could work on? Keep in mind I am not only extremely new to python I am new to programming. Thanks for any suggestions!!! Scott Look at all games in the online tutrial "invent with python" (which I already mentionned): http://inventwithpython.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Loop Issue
spir Wrote in message: > On 03/13/2014 12:40 AM, Danny Yoo wrote: >> The context is the beginning of the thread: >> >> https://mail.python.org/pipermail/tutor/2014-March/100543.html >> >> with the loop: >> >> >> ### >> while health != 0: >> ... >> ### > > The point, and reason why this loop was (potentially) infinite, is that the > condition was false. Should be <0 instead ('cause health points are not > removed > one by one, so that a health value of 0 exactly can be jumped over). > Well unless I misremember, the value of health goes down. So you'd want while health > 0 -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] c++ on python
Alan Gauld, 12.03.2014 23:05: > On 12/03/14 16:49, Stefan Behnel wrote: >> Alan Gauld, 12.03.2014 10:11: >>> If it were a library then you would have to call >>> the individual C++ functions directly using >>> something like ctypes, which is usually more >>> complex. >> >> ctypes won't talk to C++, but Cython can do it quite easily. > > I thought it would work provided the interface functions > were declared as C functions? That might involve > writing a wrapper around it but that is usually > trivial if you have to compile the source anyway. The thing is: if you have to write your own wrapper anyway (trivial or not), then why not write it in Cython right away and avoid the intermediate plain C level? It's usually much nicer to work with object oriented code on both sides (assuming you understand the languages on both sides), than to try to squeeze them through a C-ish API bottleneck in the middle. Stefan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] c++ on python
On Thu, 2014-03-13 at 16:57 +0100, Stefan Behnel wrote: […] > The thing is: if you have to write your own wrapper anyway (trivial or > not), then why not write it in Cython right away and avoid the intermediate > plain C level? If the task is two write an adapter (aka wrapper) then perhaps use SWIG whcih is easier for this task than writing Cython code. > It's usually much nicer to work with object oriented code on both sides > (assuming you understand the languages on both sides), than to try to > squeeze them through a C-ish API bottleneck in the middle. It could be that "object oriented" is a red herring. Without details (*) of what it is about the C++ code that is the connection between Python and C++, it is difficult to generalize. ctypes can be a real pain when trying to call C++ from Python using argument values that are not primitive types. CFFI solves (currently much, soon most) of this problem by addressing the adapter between Python and C++ in a different way to that employed by ctypes. In both cases, both are a lot easier than writing Cython code. (*) I may have just missed this detail in which case apologies. -- Russel. = Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.win...@ekiga.net 41 Buckmaster Roadm: +44 7770 465 077 xmpp: rus...@winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder signature.asc Description: This is a digitally signed message part ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] c++ on python
Perhaps I should look into Cython as I'm currently working on a project that utilises a C API. I've been finding that getting the data types to be exactly what the C API is expecting to be the hardest part. With the original question in mind, here's an example calling into a C++ external C API: (Works if compiled is VisualStudio. The DLL produced by MinGW-G++ didn't work). --- // main.h #ifndef __MAIN_H__ #define __MAIN_H__ #include #define DLL_EXPORT __declspec(dllexport) #ifdef __cplusplus extern "C" { #endif int DLL_EXPORT add(int a, int b); #ifdef __cplusplus } #endif #endif // __MAIN_H__ --- --- //main.cpp #include "main.h" // a sample exported function int DLL_EXPORT add(int a, int b) { return(a + b); } extern "C" DLL_EXPORT BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) { switch (fdwReason) { case DLL_PROCESS_ATTACH: // attach to process // return FALSE to fail DLL load break; case DLL_PROCESS_DETACH: // detach from process break; case DLL_THREAD_ATTACH: // attach to thread break; case DLL_THREAD_DETACH: // detach from thread break; } return TRUE; // succesful } --- --- # -*- coding: utf-8 -*- # dll.py import ctypes class DllInterface(object): dll_handle = None def __init__(self, dll_file): self.dll_handle = ctypes.WinDLL(dll_file) def add_a_and_b(self, a=0, b=0): return self.dll_handle.add(a, b) if __name__ == '__main__': dll_file = 'PythonDLL.dll' external_lib = DllInterface(dll_file) int_a = ctypes.c_int(1) int_b = ctypes.c_int(2) result = external_lib.add_a_and_b(int_a, int_b) print(result) --- -- James -- James On 13 March 2014 15:57, Stefan Behnel wrote: > Alan Gauld, 12.03.2014 23:05: >> On 12/03/14 16:49, Stefan Behnel wrote: >>> Alan Gauld, 12.03.2014 10:11: If it were a library then you would have to call the individual C++ functions directly using something like ctypes, which is usually more complex. >>> >>> ctypes won't talk to C++, but Cython can do it quite easily. >> >> I thought it would work provided the interface functions >> were declared as C functions? That might involve >> writing a wrapper around it but that is usually >> trivial if you have to compile the source anyway. > > The thing is: if you have to write your own wrapper anyway (trivial or > not), then why not write it in Cython right away and avoid the intermediate > plain C level? > > It's usually much nicer to work with object oriented code on both sides > (assuming you understand the languages on both sides), than to try to > squeeze them through a C-ish API bottleneck in the middle. > > Stefan > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] c++ on python
James Chapman, 13.03.2014 17:35: > Perhaps I should look into Cython as I'm currently working on a > project that utilises a C API. > > I've been finding that getting the data types to be exactly what the C > API is expecting to be the hardest part. > > With the original question in mind, here's an example calling into a > C++ external C API: > > (Works if compiled is VisualStudio. The DLL produced by MinGW-G++ didn't > work). > > --- > // main.h > > #ifndef __MAIN_H__ > #define __MAIN_H__ > > #include > > #define DLL_EXPORT __declspec(dllexport) > > #ifdef __cplusplus > extern "C" > { > #endif > > int DLL_EXPORT add(int a, int b); > > #ifdef __cplusplus > } > #endif > > #endif // __MAIN_H__ > --- > > --- > //main.cpp > > #include "main.h" > > // a sample exported function > int DLL_EXPORT add(int a, int b) > { > return(a + b); > } > > extern "C" DLL_EXPORT BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD > fdwReason, LPVOID lpvReserved) > { > switch (fdwReason) > { > case DLL_PROCESS_ATTACH: > // attach to process > // return FALSE to fail DLL load > break; > > case DLL_PROCESS_DETACH: > // detach from process > break; > > case DLL_THREAD_ATTACH: > // attach to thread > break; > > case DLL_THREAD_DETACH: > // detach from thread > break; > } > return TRUE; // succesful > } > --- > > --- > # -*- coding: utf-8 -*- > # dll.py > > import ctypes > > > class DllInterface(object): > > dll_handle = None > > def __init__(self, dll_file): > self.dll_handle = ctypes.WinDLL(dll_file) > > def add_a_and_b(self, a=0, b=0): > return self.dll_handle.add(a, b) > > > if __name__ == '__main__': > dll_file = 'PythonDLL.dll' > external_lib = DllInterface(dll_file) > int_a = ctypes.c_int(1) > int_b = ctypes.c_int(2) > result = external_lib.add_a_and_b(int_a, int_b) > print(result) In Cython, that would essentially be cdef extern from "main.h": int add(int a, int b) print(add(1, 2)) It compiles down to C(++), i.e. it interfaces at the API level, not the ABI level, as ctypes would. Stefan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] c++ on python
Russel Winder, 13.03.2014 17:29: > On Thu, 2014-03-13 at 16:57 +0100, Stefan Behnel wrote: > […] >> The thing is: if you have to write your own wrapper anyway (trivial or >> not), then why not write it in Cython right away and avoid the intermediate >> plain C level? > > If the task is two write an adapter (aka wrapper) then perhaps use SWIG > whcih is easier for this task than writing Cython code. Depends. SWIG is nice if you have a large API that a) you want to wrap quickly all at once and b) that matches the tool well. Once you're beyond the "matches the tool well" spot, however, you'll start having an increasingly hard time pushing the tool into matching your API. Cython has a higher learning curve to get started (it's a programming language, not a wrapper generator by itself, use something like XDress for that), but is unlimited in what it allows you to do (because it's a programming language). So things won't suddenly become harder (let alone impossible) afterwards. >> It's usually much nicer to work with object oriented code on both sides >> (assuming you understand the languages on both sides), than to try to >> squeeze them through a C-ish API bottleneck in the middle. > > It could be that "object oriented" is a red herring. Without details (*) > of what it is about the C++ code that is the connection between Python > and C++, it is difficult to generalize. Sure. I've seen both good and bad API designs in C++, as in any other language. > ctypes can be a real pain when trying to call C++ from Python using > argument values that are not primitive types. CFFI solves (currently > much, soon most) of this problem by addressing the adapter between > Python and C++ in a different way to that employed by ctypes. In both > cases, both are a lot easier than writing Cython code. Now, that's a bit overly generalising, wouldn't you say? Even in the cases where cffi is as simple as Cython, I'd still prefer the portability and simplicity advantage of having statically compiled (and tested) wrapper code over a mix of a hand written C++-to-C wrapper and some dynamically generated glue code with its own set of runtime dependencies. But I can certainly accept that tools like ctypes and cffi have their niche, too. If you're comfortable with them, and they fit your needs, then sure, use them. There isn't one tool that caters for everyone. Stefan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Beginner - list not returning correct variable value
Hello Tutor, I am a self-taught Python script beginner and I do it from the Michael Dawson Book. In attached script, can somebody tell me why the values of the variables strenght_points, health_points, wisdom_points and dexterity_points stay at 0 value when 'printing' their value from the list attributes[] when the 'for' loop at the bottom of the script runs. I have commented out the script to highlight what causes me problem. I hope this is clear enough a question. Thanks for your help, Marc #Character role play #you have 4 attributes and can assign/remove points to/from it #use short story scripting to task with no MENU tree #ignore calculus logic limitations #set variables strength_points = 0 health_points = 0 wisdom_points = 0 dexterity_points = 0 pool = 30 att_choice = None attributes = [('strength',strength_points), ('health',health_points), ('wisdom',wisdom_points), ('dexterity',dexterity_points)] print('Hello gladiator,\n') print('you have the following attributes with no points:\n') for i in attributes: att_name, points = i print(att_name,'\t',points) input('\nPress ENTER to continue') print('\nYou are now entrusted with a pool of 30 points to spend on them as you whish') input('Press ENTER to continue') print('\nUsing negative points will move them back to your pool of points') input('Press ENTER to continue') print('\nChose an attribute and add/remove points at your will: ') while att_choice != '0': #here proof that variables values are changing as expexted: print('here proof that variables values are changing as expected:') print(strength_points,health_points,wisdom_points,dexterity_points,pool) print('\n[1] stength [2] health [3] wisdom [4] dexterity [0] EXIT') att_choice = input('ENTER number here: ') if att_choice == '1': print('\nHow many strenght points you want to add (+) or remove (-) ?') points = int(input('Enter points here: ')) strength_points += points pool -= points elif att_choice == '2': print('\nHow many health points you want to add (+) or remove (-) ?') points = int(input('Enter points here: ')) health_points += points pool -= points elif att_choice == '3': print('\nHow many wisdom points you want to add (+) or remove (-) ?') points = int(input('Enter points here: ')) wisdom_points += points pool -= points elif att_choice == '4': print('\nHow many dexterity points you want to add (+) or remove (-) ?') points = int(input('Enter points here: ')) dexterity_points += points pool -= points elif att_choice == '0': print('Good Bye Gladiator!') break else: print('Sorry I don\'t understand your selection') input('Press ENTER to try again') print('\nYou have now the following attributes and points:\n') #here the loop does not return variable value as expected: for i in attributes: att_name, point = i print(att_name,'\t',point) input('\nPress ENTER to continue\n') input('\nPress ENTER to exit') ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Loop Issue
OK, thank you. I knew the loop was infinite but usuyally when I did infinite loops before, I had them print out a message, so it was obvious. Thanks for the clarification. On Thu, Mar 13, 2014 at 9:38 AM, spir wrote: > On 03/13/2014 12:40 AM, Danny Yoo wrote: > >> The context is the beginning of the thread: >> >> https://mail.python.org/pipermail/tutor/2014-March/100543.html >> >> with the loop: >> >> >> ### >> while health != 0: >> ... >> ### >> > > The point, and reason why this loop was (potentially) infinite, is that > the condition was false. Should be <0 instead ('cause health points are not > removed one by one, so that a health value of 0 exactly can be jumped over). > > d > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Beginner - list not returning correct variable value
On 13/03/14 16:12, Marc Eymard wrote: In attached script, can somebody tell me why the values of the variables strenght_points, health_points, wisdom_points and dexterity_points stay at 0 value when 'printing' their value from the list attributes[] Because when you create the list you use the *values* of those variables at the time you created the list. They do not track the values of the variables after that so changing the variables has no effect on the list. But, looking at your list, it is a name and corresponding value. That is the definition of a dictionary so, rather than having a set of variables and trying to maintain a list reflecting those, you should just use a dictionary. So your code would start with: #set variables pool = 30 att_choice = None attributes = { 'strength':0, 'health':0, 'wisdom':0, 'dexterity',0 } Then instead of using the variables use: while att_choice != '0': print('here proof that variables values are changing as expected:') print(attributes['strength'],attributes['health'], attributes['wisdom'], attributes['dexterity'],pool) and if att_choice == '1': print('\nHow many strength points to add or remove?') points = int(input('Enter points here: ')) attributes['strength'] += points pool -= points and so on. Then finally to output the values: print('\nYou have now the following attributes and points:\n') for i in attributes: print(i,'\t',attributes[i]) HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor