[Tutor] Is context manager the answer to synchronous function calls?

2015-09-16 Thread John Wong
Hi

Here is the code:

def create_vm(.):
# returns vm object

def modify_vm(.):
return new vm object

def get_vm_status(.):
return status
# Constraints:
# I cannot modify vm until vm is finished,
# I also cannot modify VM while the VM is being updated.
# Obviously it looks ugly and wrong to include the while
# in my functio. Those are user APIs.

# Straight-forward option
create_vm(...)
status = get_vm_status(...)
while status != "ready":
time.sleep(10)
status = get_vm_status(...)
modify_vm()
while status != "ready":
time.sleep(10)
status = get_vm_status(...)
print("done!")


I was thinking about context manager. But I feel like that's not the
right choice. If you can guess cloning a vm in my program will require
several more function calls. What's your recommendation to accomplish
this?

Thank you.

John
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Debugging in Python

2015-11-17 Thread John Wong
On Mon, Nov 16, 2015 at 12:48 PM, Alan Gauld 
wrote:
>
>
> The correct fix is to exit() from the python3 shell and start it again.
>>
>
> He's tried that and didn't find it satisfactory. That's why
> he wants a "better" workflow.
>
> Alternatively, add some main code at the end of your file and use
>> `python3 hlibert.py`:
>>
>> if __name__ == '__main__':
>>  hilbert(3)
>>
>
> That depends on the nature of his module and the type of testing he wants
> to do. He could, I agree, create a new test file that imports hilbert and
> then add his test code there but that loses the interactivity of a shell
> prompt which may be important.


Yeah, the only way you can actually keep trying is to write some kind of
test script. It doesn't have to be using any testing library. i.e. whatever
code you throw at the console for testing can be written down in another
python file. I call that a scratch pad. Obviously a proper test is
desirable. I too recommend investing time in that.

Doesn't matter whether you use a scratch pad or not, you can use PDB as
your debugger. I often use it to step into my code and hold on the
execution. I often do this when I need to inspect what an object instance
can do...

for example:

main.py

def main():
import pdb; pdb.set_trace()
file_object = some_complex_code_written_by_someone_else()
do_stuff_with_file_object(file_object)

main()


Now when I run main.py, I get an interpreter session inside the PDB
debugger (it's similar to GDB if you have written C/C++ code). I can now
type  dir(file_object), play with file_object, or step inside code to
inspect errors. Hope this helps.

Thanks.

John
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Noob: nested if-clauses

2016-01-29 Thread John Wong
On Fri, Jan 29, 2016 at 3:13 PM, Alan Gauld 
wrote:

>
> When I first heard that python was white-space sensitive I
> thought, oh no! But now I see it as a huge strength of the
> language. Once you get used to it you will find it helps
> far more than it hinders - just avoid tabs and/or map your
> tab key to spaces.
>
>
Just want to emphasize: use a tool that actually works for you and for the
programming tool. In general, any modern IDE is capable of doing the right
thing. NotePad++ is also capable, but avoid anything like WordPad or
NotePad, if they are still around...
Lastly, most projects I encounter do use 4-space, but some projects (and
notably Google projects internally) would use 2-space, so you almost never
have to worry about mixing 4-space and 2-space in your code. It just happen
that sometimes some people publish code under tab or 2 space and if you
just copy-paste you can run into issues, just FYI for the beginners.

Thanks.

John
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] wiped file

2016-07-14 Thread John Wong
Alan made really good points, standard practice, however, on Linux and
MacOSX, usually if you use editors like VIM, a temporary file is always
created. For Windows, I am not sure 100%. Check your editor, your editor
may have saved a cache file but marked as hidden file. For example,
Notepad++ has this feature:
http://superuser.com/questions/281776/does-notepad-backup-pre-edited-files-anywhere
.

I made my point because version control system (VCS) is an on-demand
software. Unless you instruct VCS to keep "version controlling" your code,
it will only keep the last copy. Furthermore, you'd have to store the VCS
repository somewhere else. For example, if you are using Git, you may use
BitBucket, GitHub, Gitlab to store a remote copy.


On Thu, Jul 14, 2016 at 11:13 AM, Alan Gauld via Tutor 
wrote:

> On 14/07/16 05:06, Noah Stickel wrote:
> > i open a python file in IDLE (python 3.5.1) and then my computer froze
> > after displaying an error 40 message (i have a DELL PC) and when i
> > restarted it i discovered that the file had been completely wiped clean.
> So
> > i still have the file but there is nothing in it. is there anyway i can
> > restore the contents of this python file?
>
> If you stored the file on the cloud (Dropbox, Google drive etc)
> you may be able to recover old versions. If you run Linux you
> may have a journalling file-system that can recover previous
> versions.
>
> Alternatively you may have committed the file to a version
> control system? In that case simply check out the old version.
>
> And of course if you regularly backup your disks then your
> backup programme can probably retrieve the last saved version.
>
> If none of the above applies then now you know why they should! :-(
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
> ___
> 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] Book recommendation

2016-08-01 Thread John Wong
On Sun, Jul 31, 2016 at 7:14 PM, D Wyatt  wrote:

> Hello.  I've been a member of this group for a very long time and have
> played with Python for years, but have never gotten past being a beginner
> for various reasons.  I just discovered a very good, free book, that is
> answering many of the questions I have had in the past that I never got
> answers I could understand from you all.  While I appreciate the time and
> effort you put in helping us out, most of you do not remember what you
> didn't used to know, and are often less than helpful because of this.  This
> is not meant as a criticism, but just my observation.
>
>
I think that's a constructive feedback. There are two problems. One is
often the question itself is not well-phrased. The other is remote
communication is a known challenge. I could spend a whole hour doing pair
programming or whiteboarding a solution with someone next to me physically,
but I can't do that over the Internet easily. The person asking the
question may not respond to my reply the next day (timezone difference).
It's a hard problem. I often ask my questions on IRC, and my experience is
also not quite as good as I would hope, although there are enough people to
chime in.

A lot of learning is practice, and then having a mentor available.


> Anyway, this book I found, Learning Python, is well-written and easy to
> understand.  Here is a url to it.https://www.packtpub.com/tech/python.
>
>
Great. I am glad you found one that's helping you. I share the view that
any book is a good book as long as the book is giving correct information.

A few other ones:
* Think Python: How to Think Like a Computer Scientist
* Learning Python by Mark Lutz - I started my journey with this, many years
ago
* Dive Into Python - quite good for beginnger
* Google's Python Class - very gentle introduction
* Python classes online (e.g. the ones available on MIT OCW) - pretty good
if you like slow, lecture-based.
* Python documentation
* Python Essential Reference (for intermediate -> advanced)
* Learn Python the Hard Way (not so much into this, just personal
preferences)

Just my 3.1415 cents.

John
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor