Re: [Tutor] Convert os.random output to a string

2014-03-06 Thread Jay Lozier

Alan

I am planning to store the passwords encrypted. This part is allow a 
user to generate and view the generated password.


Ben got me pointed to a snippet I can use. I was trying to do something 
the hard way.


Jay
On 03/06/2014 07:34 PM, Alan Gauld wrote:

On 06/03/14 23:32, Jay Lozier wrote:


I am try to generate random password strings of an arbitrary user
selected length. I read to generate a random string of a user supplied
length I can use os.urandom(n).


So far so good...

> I want to convert the output to a human readable string.

Define human readable?


I would like to store the string for later processing.


Depending on the application there may be laws/rules/guidance 
prohibiting the storage of passwords. Best practice only ever

stores the encrypted form not the original password (or more
properly nowadays pass-phrase since phrases are more secure
than words.)


Also, I would like to limit the characters to the US keyboard, so I
might be using the wrong function.


Even in the US not everyone uses a "US keyboard" especially nowadays 
with various tablets/phablets/phones in use. but if you stick to the 
printable subset of the old ASCII set you should be close enough...

But that probably means building your own random generator based
on the subrange of characters.


I am probably missing something very obvious.


Maybe, or you could be asking for more than the library gives?



--
Jay Lozier
jsloz...@gmail.com

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


Re: [Tutor] Convert os.random output to a string

2014-03-06 Thread Alan Gauld

On 06/03/14 23:32, Jay Lozier wrote:


I am try to generate random password strings of an arbitrary user
selected length. I read to generate a random string of a user supplied
length I can use os.urandom(n).


So far so good...

> I want to convert the output to a human readable string.

Define human readable?


I would like to store the string for later processing.


Depending on the application there may be laws/rules/guidance 
prohibiting the storage of passwords. Best practice only ever

stores the encrypted form not the original password (or more
properly nowadays pass-phrase since phrases are more secure
than words.)


Also, I would like to limit the characters to the US keyboard, so I
might be using the wrong function.


Even in the US not everyone uses a "US keyboard" especially nowadays 
with various tablets/phablets/phones in use. but if you stick to the 
printable subset of the old ASCII set you should be close enough...

But that probably means building your own random generator based
on the subrange of characters.


I am probably missing something very obvious.


Maybe, or you could be asking for more than the library gives?

--
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


Re: [Tutor] Convert os.random output to a string

2014-03-06 Thread Ben Finney
Jay Lozier  writes:

> I am try to generate random password strings of an arbitrary user
> selected length.

My first recommendation would be: Don't re-invent the wheel. Generating
password strings is a complex request, not because it's particularly
difficult but because the requirements are complex.

Research what other solutions have been published
https://duckduckgo.com/?q=generate+password+python> and see whether
you still need to write something different.

-- 
 \  “Any intelligent fool can make things bigger and more complex… |
  `\It takes a touch of genius – and a lot of courage – to move in |
_o__)the opposite direction.” —Albert Einstein |
Ben Finney

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


[Tutor] Convert os.random output to a string

2014-03-06 Thread Jay Lozier

Hi,

My first question

I am try to generate random password strings of an arbitrary user 
selected length. I read to generate a random string of a user supplied 
length I can use os.urandom(n). I want to convert the output to a human 
readable string. I would like to store the string for later processing.


Also, I would like to limit the characters to the US keyboard, so I 
might be using the wrong function.


The following is the output from my terminal - Python 3.3.4 on Manjaro 
Linux shows the generation of a random :


Python 3.3.4 (default, Feb 11 2014, 15:56:08)
[GCC 4.8.2 20140206 (prerelease)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from os import urandom
>>> a = urandom(16)
>>> type(a)

>>> a_str = a.decode()
Traceback (most recent call last):
  File "", line 1, in 
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb1 in position 0: 
invalid start byte

>>>

I am probably missing something very obvious.

Thanks in advance!

--
Jay Lozier
jsloz...@gmail.com

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


Re: [Tutor] How to determine which function code is being called from

2014-03-06 Thread Jerry Hill
On Thu, Mar 6, 2014 at 12:00 PM, Jignesh Sutar  wrote:
> Hi I'm trying to exclude a certain line of code if the function is called by
> another function, see illustration below:

As other have said, this is not often a good idea.  That said, it is
possible to inspect the call stack to see what called a particular
function, like this (python 3.3):

iimport inspect

def funcA():
caller = inspect.stack()[1][3]
print('funcA was called by ' + caller)
if caller == '':
print("running from funcA")# print only if running from funcA
if caller in ('', 'funcB'):
print("running from funcA or funcB") # print when running from
either function
if caller == 'funcB':
print("running from funcB") # print only when running from funcB

def funcB():
funcA()

print('- Calling funcA() directly -')
funcA()
print('- Calling funcB() -')
funcB()

Output:

>>>
- Calling funcA() directly -
funcA was called by 
running from funcA
running from funcA or funcB
- Calling funcB() -
funcA was called by funcB
running from funcA or funcB
running from funcB
>>>


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


Re: [Tutor] How to determine which function code is being called from

2014-03-06 Thread Danny Yoo
On Thu, Mar 6, 2014 at 9:00 AM, Jignesh Sutar  wrote:
> Hi I'm trying to exclude a certain line of code if the function is called by
> another function, see illustration below:
>
>
> def funcA():
> print "running from funcA" # print only if running from funcA
> print "running from funcA or funcB" #print when running from either
> function
> print "running from funcB" # print only when running from funcB
>
> def funcB():
> funcA()
>
> funcB()



Can you directly pass the state that funcA cares about as an explicit parameter?

I think we need to understand what you're trying to do in context.
Tell us more why you want to do what you're doing.  Maybe the approach
you're taking is correct, or maybe there's an easier way to accomplish
what you're trying to do.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to determine which function code is being called from

2014-03-06 Thread Dave Angel
 Jignesh Sutar  Wrote in message:
>
> 
 Hi I'm trying to exclude a certain line of code if the function is called by 
another function, see illustration below:

Your code example is all confused, so perhaps you are as well. 

Why should any of your code in function A care who calls it? The
 main point about making functions is to reuse code without caring
 who is calling it. If the function is going to have variable
 behavior,  control that by passing parameters.

Or perhaps this is testing code or debugging code.  In that case, 
 look at docs.python.org/2/library/inspect.org and look at
 function inspect.getouterframes ()

Can you give a nontrivial use example,  and without using recursion? 


-- 
DaveA

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


Re: [Tutor] How to determine which function code is being called from

2014-03-06 Thread spir

On 03/06/2014 06:00 PM, Jignesh Sutar wrote:

Hi I'm trying to exclude a certain line of code if the function is called
by another function, see illustration below:


def funcA():
 print "running from funcA" # print only if running from funcA
 print "running from funcA or funcB" #print when running from either
function
 print "running from funcB" # print only when running from funcB

def funcB():
 funcA()

funcB()


The simple way is to have a param telling about the caller. (But _probably_ you 
don't _really_ need that, it is instead an artifact of unproper design.)


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


Re: [Tutor] How to determine which function code is being called from

2014-03-06 Thread Emile van Sebille
While there are ways of getting at the caller using introspection, there 
are no reliable ways of doing so and you would do well to rethink the 
need and take an alternate course such as passing a parameter in.


Suppose the following:

funcC=funcB

what would you want to see?

Emile


On 3/6/2014 9:00 AM, Jignesh Sutar wrote:

Hi I'm trying to exclude a certain line of code if the function is
called by another function, see illustration below:


def funcA():
 print "running from funcA" # print only if running from funcA
 print "running from funcA or funcB" #print when running from either
function
 print "running from funcB" # print only when running from funcB

def funcB():
 funcA()

funcB()



___
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


[Tutor] How to determine which function code is being called from

2014-03-06 Thread Jignesh Sutar
Hi I'm trying to exclude a certain line of code if the function is called
by another function, see illustration below:


def funcA():
print "running from funcA" # print only if running from funcA
print "running from funcA or funcB" #print when running from either
function
print "running from funcB" # print only when running from funcB

def funcB():
funcA()

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