[Tutor] How do I do this in python?

2009-06-11 Thread Robert Lummis
I want to write a function that I can use for debugging purposes that
prints the values of whatever list of object references it is given as
arguments, without knowing in advance how many object references it
will be called with or what they might be. For example, the call:
show(a,b,c) would output the values of the arguments like this:

a = 3
b = 'john'
c = 'Monday'

while show (x) would output (for example):

x = 3.14

of course displaying whatever the actual current values are. For a
collection object it would make sense to output just the first few
values.

So within the 'show' function definition I have to somehow get a list
of the literal argument names it was called with and then use the
names as globals to get the values. How do I do that?

If this can't be done but there is a completely different way to
achieve a similar result, what is it?

I'm trying to learn python but it's a struggle because the
documentation is so dispersed. If there is a solution to this
question, what documentation could I have looked at to find it on my
own?

BTW I'm using python 3.01 if it matters.

-- 
Robert Lummis
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How do I do this in python?

2009-06-11 Thread Robert Lummis
On Thu, Jun 11, 2009 at 11:58 AM, spir wrote:
> Le Thu, 11 Jun 2009 10:46:26 -0400,
> Kent Johnson  s'exprima ainsi:
>
>> On Wed, Jun 10, 2009 at 9:43 PM, Robert Lummis
>> wrote:
>> > I want to write a function that I can use for debugging purposes that
>> > prints the values of whatever list of object references it is given as
>> > arguments, without knowing in advance how many object references it
>> > will be called with or what they might be. For example, the call:
>> > show(a,b,c) would output the values of the arguments like this:
>> >
>> >    a = 3
>> >    b = 'john'
>> >    c = 'Monday'
>> >
>> > while show (x) would output (for example):
>> >
>> >    x = 3.14
>>
>> Here is a pretty clean solution. It passes names rather than values,
>> then looks the values up in the caller's stack frame. Written for
>> Python 2.x but should work for 3.x if you change the prints.
>>
>> In [1]: import sys
>> In [11]: def show(names):
>>    :     frame = sys._getframe(1)
>>    :     for name in names.split():
>>    :         if name in frame.f_locals:
>>    :             print name, '=', frame.f_locals[name]
>>    :         else:
>>    :             print name, 'not found'
>>
> [...]
>>
>> Kent
>
> Waow, great!
>
> Denis
> --
> la vita e estrany
> ___
> Tutor maillist  -  tu...@python.org
> http://mail.python.org/mailman/listinfo/tutor
>

Thanks for all the intelligent and thoughtful replies to my newB
question. It looks like this tutor mailing list is going to be a big
help!  It's going to take me some time and trial and error to digest
all the replies so I understand them fully but it will be instructive.

I guess one thing I learned already, even though I didn't ask about
it, is that using python3 isn't the best way to start out.

Again, thank you all for taking the time to reply.
-- 
Robert Lummis
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] how to manage an encrypted file?

2009-06-19 Thread Robert Lummis
Could you recommend a module or methods I should use to manage an
encrypted text file? I want to store passwords and associated contact
information in a file and feel confident that if the file is stolen
the information couldn't be read.

-- 
Robert Lummis
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to manage an encrypted file?

2009-06-22 Thread Robert Lummis
Very good suggestion! Thanks! I knew about TrueCrypt before (it's
really excellent) but I didn't think of it for this purpose because it
didn't show up when I did a Synaptic search. I'll see if I can get it
installed on ubuntu and used with Python. Actually, the URL you give
doesn't work (at least for me). But I found the following by googling
the URL:

http://blog.bjrn.se/2008/02/truecrypt-explained-truecrypt-5-update.html

On Mon, Jun 22, 2009 at 3:46 AM, Daniele wrote:
>> From: Wayne 
>
>> If you want the most basic encryption you could simply XOR the file. It's 
>> fairly easy to break, though, because the same character patterns will be 
>> present as with your original file.
>
> Actually if you do it properly this kind of encryption is unbreakable,
> but you'd have to:
> 1. generate a random key long enough to cover your data
> 2. keep the key secure and in a separate place
> 3. DON'T use the key twice
>
> there must be a Security Now episode that explains the thing pretty
> well (could be this one http://www.grc.com/sn/sn-011.htm).
> Some arguments may rise against the randomness of the key, but I don't
> think its the case to go into that.
>
> You could also use TrueCrypt which is an extremely powerful
> cryptographic software (open source), I found this article that can
> help you linking it with python:
> http://blog.bjrn.se/2008/01/truecrypt-explained.html
>



-- 
Robert Lummis
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] does python have something like "#include" in C?

2009-06-29 Thread Robert Lummis
... or any pre-processing at all?

I'm looking for a way to get boiler plate code into the main program
file. Of course I could copy and paste it with an editor but I was
hoping for something more pythonic. I know about import but that's not
the same.

-- 
Robert Lummis
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] does python have something like "#include" in C?

2009-06-29 Thread Robert Lummis
Here's an example that seems not possible in python. I'm probably
missing something so please enlighten me. I only tried doing this as
an exercise to show myself how name references work. I'm not saying
it's needed or that it's good practice.

I can write the following as a single file and it works as expected:

===snip===
#!/usr/bin/python

def show(*args):
print
for arg in args:
print arg + ':',
exec('print ' + arg)

a=15
b='hello'
x=['bob',3]

show('a')
show('a','b')
show('a','b','x')
===snip===

The calls to 'show' output lines like "a: 15" which could be useful
for debugging or some such purpose.

However, it seems that I can't put the function definition in a file
and import it because I can't find a way to refer to an object in the
main program file from within a module file. I understand that it's a
good thing to contol which namespaces are referenced by which code but
isn't there sometimes a need for code in a module to access the main
program file's namespace? My example may be a little contrived but
isn't this ability legitimately needed at times?

On Mon, Jun 29, 2009 at 11:22 AM, A.T.Hofkamp wrote:
> Robert Lummis wrote:
>>
>> ... or any pre-processing at all?
>>
>> I'm looking for a way to get boiler plate code into the main program
>> file. Of course I could copy and paste it with an editor but I was
>> hoping for something more pythonic. I know about import but that's not
>> the same.
>
> Python is very good at eliminating boilerplating, so import should be enough
> normally.
>
> Could you give an example of what you want to include that cannot be done
> with import?
>
> Albert
>
>



-- 
Robert Lummis
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] does python have something like "#include" in C?

2009-06-29 Thread Robert Lummis
Thanks very much for all your responses. It's pretty clear now that
what I thought I could somehow do is not "pythonic" and for good
reason.

Kent Johnson says it well: "Module dependencies should be one-way" I
can buy that.

On Mon, Jun 29, 2009 at 2:47 PM, Kent Johnson wrote:
> On Mon, Jun 29, 2009 at 1:23 PM, Robert Lummis wrote:
>> Here's an example that seems not possible in python. I'm probably
>> missing something so please enlighten me. I only tried doing this as
>> an exercise to show myself how name references work. I'm not saying
>> it's needed or that it's good practice.
>>
>> I can write the following as a single file and it works as expected:
>>
>> ===snip===
>> #!/usr/bin/python
>>
>> def show(*args):
>>    print
>>    for arg in args:
>>            print arg + ':',
>>            exec('print ' + arg)
>>
>> a=15
>> b='hello'
>> x=['bob',3]
>>
>> show('a')
>> show('a','b')
>> show('a','b','x')
>> ===snip===
>>
>> The calls to 'show' output lines like "a: 15" which could be useful
>> for debugging or some such purpose.
>>
>> However, it seems that I can't put the function definition in a file
>> and import it because I can't find a way to refer to an object in the
>> main program file from within a module file.
>
> Right. We did recently discuss ways to implement this function:
> http://www.mail-archive.com/tutor@python.org/msg35873.html
>
>> I understand that it's a
>> good thing to contol which namespaces are referenced by which code but
>> isn't there sometimes a need for code in a module to access the main
>> program file's namespace? My example may be a little contrived but
>> isn't this ability legitimately needed at times?
>
> Generally no, that would be a design smell. Module dependencies should
> be one-way; if main needs module foo, then foo should not have to know
> about main.
>
> Kent
>



-- 
Robert Lummis
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] mnemonics to better learn Python

2009-07-31 Thread Robert Lummis
Understand that "if A or B" is short for the phrase "if either A or B
is true", or more fully "if A is true or B is true". When I add "is
true" then the logic seems obvious to me without memorizing anything.

Likewise, "if A and B" means "if A and B are both true", which is the
same as "if A is true and B is true".

On Fri, Jul 31, 2009 at 12:09 PM, Alan Gauld wrote:
>
> "Eduardo Vieira"  wrote
>
>> Hello, would anybody have a good memorization technique for boolean
>> results? Like when using 'or'/'and' what it returns when both are
>> false, the last is false, etc?
>
> Hmm, I don't try to remember those, I just work it out based on the meaning.
> A and B is true only if both A and B are True
> A or B is true if either A or B is True.
>
> Thats it really, what's to remember?
>
> I guess for a non native English speaker it might be more difficult because
> you need to translate and/or into the native language?
> But I assume every language has an equivalent for both of those concepts?
>
> In hardware engineering its more complex because you have nand and nor gates
> to deal with too, but they don't apply in software - at least not directly.
>
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
> ___
> Tutor maillist  -  tu...@python.org
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Robert Lummis
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Bounces

2009-10-08 Thread Robert Lummis
It probably means that you used "reply all" and one of the addresses
was not valid, possibly because it got garbled somehow on an earlier
"reply all".

On Thu, Oct 8, 2009 at 2:15 AM, Luke Paireepinart
 wrote:
> I keep getting Mail Delivery Subsystem notices from tutor saying my mail is
> undeliverable.  Is that just me or are other people getting this as well?
> I'm realizing now that a lot of my replies are not getting through.  Anyone
> know why this may be? (even if you don't, someone please reply so I know at
> least this one got through!)
>
> ___
> Tutor maillist  -  tu...@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>



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


[Tutor] Accessing variables in main from functions in a module

2009-10-30 Thread Robert Lummis
I want to move some functions from my "main" program file to a module
file because the main file is getting too big for convenience. The
functions access arrays (lists of lists) that are defined and
initialised in the main file. How do I reference the main file arrays
from statements within the module file?

The overall program solves a game by a recursive method. The arrays
are the game data. Functions in the main file and also functions in
the module need to read and write them. The program works when all the
functions are in the main file but now I want to develop it further
and the presence of a bunch of unchanging housekeeping functions in
the main file makes it cumbersome to read and edit.

With everything in the main file a function like the following works
as expected:

def functionA (row, col, x):
 grid[row][col] = 

where grid[] is initialised outside of any functions (i.e. in the main).

So how do I write that function when I move it to a module file? I
thought I read somewhere that the main routine has the name "__main__"
so I tried:

 __main__.grid[row][col] = <...>

 but that gives "NameError: name '__main__' is not defined".

I understand that it is best to minimize cross-file references but I
feel it is warranted in this case. Even if you think it isn't good
practice I would like to understand how to do it (or else why it can't
be done). Is there some other way to keep files from getting too big?

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


Re: [Tutor] Accessing variables in main from functions in a module

2009-10-31 Thread Robert Lummis
These replies are great! Thanks to everyone who took the trouble to
respond and explain. I'll need some time to digest what you say and
put it to use but I'm sure your suggestions will be a huge help.

On Fri, Oct 30, 2009 at 7:55 PM, Alan Gauld  wrote:
> "Robert Lummis"  wrote
>
>> I want to move some functions from my "main" program file to a module
>> file because the main file is getting too big for convenience.
>
> Good idea!
>
>> functions access arrays (lists of lists) that are defined and
>> initialised in the main file. How do I reference the main file arrays
>> from statements within the module file?
>
> In  general you don't. You pass the data into the function.
> Thats the only (sane) way to make the functions reusable,
> by passing in all the data they need to do their job. Its also
> the only sane way to avoid the debugging nightmare of
> global variables changing under your feet due to hidden
> function side effects.
>
> So when you move the functions, rewrite them from
>
> def f(...)
>   # stuff
> to
>
> def f(...grid)
>   #stuff
>   return grid
>
> And when you call them do
>
> grid = mymod.f(...,grid)
>
> That keeps the functions independant and avoids any
> mysteries about where your main function variables are
> getting changed.
>
>> The overall program solves a game by a recursive method.
>
> In that case even more reason to pass the data in. Recursive
> functions opertating on global variables are one of the hardest
> things to debug in the programming universe, only beaten by
> multiple threads modifying unsynchronised global variables!
>
>> and the presence of a bunch of unchanging housekeeping functions in
>> the main file makes it cumbersome to read and edit.
>
> Yes thats one reason to move functions out. But an even better
> reason is to logically partition the code into whats reusable
> and what is specific to the problem. So consider whether its
> really the housekeeping that wants moving or the core algorithms...
>
>> def functionA (row, col, x):
>>    grid[row][col] = 
>>
>> where grid[] is initialised outside of any functions (i.e. in the main).
>>
>> So how do I write that function when I move it to a module file? I
>
> As above
>
> def functionA (row, col, x, grid):
>    grid[row][col] = 
>    return grid
>
> and call it with
>
> grid = module.function(row,col,x, grid)
>
>> thought I read somewhere that the main routine has the name "__main__"
>
> A module that is being run directly has the name __main__ not a function.
>
>> I understand that it is best to minimize cross-file references but I
>> feel it is warranted in this case.
>
> I doubt it.
>
>> Even if you think it isn't good practice I would like to understand how
>> to do it (or else why it can't be done).
>
> You can probably do it by inspecting the call stack using the traceback
> module or somesuch, but there'sd really no need.
>
>> Is there some other way to keep files from getting too big?
>
> You can use classes. You could have a Grid class with all the functions
> that modify the grid as methods. Then you can create grid objects and
> operate on them and each will have its own internal grid. You can then
> easily move the class into a separate module as required.
>
> HTH,
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
> ___
> Tutor maillist  -  tu...@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



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