Skip to content

Commit

Permalink
[iotaledger#145] Reformatted examples for PEP-8.
Browse files Browse the repository at this point in the history
  • Loading branch information
todofixthis committed Jun 22, 2018
1 parent 8291c38 commit 1318039
Show file tree
Hide file tree
Showing 7 changed files with 504 additions and 506 deletions.
205 changes: 105 additions & 100 deletions examples/address_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,128 +4,133 @@
"""

from __future__ import absolute_import, division, print_function, \
unicode_literals
unicode_literals

from argparse import ArgumentParser
from getpass import getpass as secure_input
from sys import argv
from typing import Optional, Text

from iota import __version__, Iota
from six import binary_type, moves as compat, text_type

from iota import Iota, __version__
from iota.crypto.addresses import AddressGenerator
from iota.crypto.types import Seed
from six import binary_type, moves as compat, text_type


def main(uri, index, count, security, checksum):
# type: (Text, int, Optional[int], Optional[int], bool) -> None
seed = get_seed()
# type: (Text, int, Optional[int], Optional[int], bool) -> None
seed = get_seed()

# Create the API instance.
# Note: If ``seed`` is null, a random seed will be generated.
api = Iota(uri, seed)
# Create the API instance.
# Note: If ``seed`` is null, a random seed will be generated.
api = Iota(uri, seed)

# If we generated a random seed, then we need to display it to the
# user, or else they won't be able to use their new addresses!
if not seed:
print('A random seed has been generated. Press return to see it.')
output_seed(api.seed)
# If we generated a random seed, then we need to display it to the
# user, or else they won't be able to use their new addresses!
if not seed:
print('A random seed has been generated. Press return to see it.')
output_seed(api.seed)

print('Generating addresses. This may take a few minutes...')
print('')
print('Generating addresses. This may take a few minutes...')
print('')

# Here's where all the magic happens!
api_response = api.get_new_addresses(index, count, security, checksum)
for addy in api_response['addresses']:
print(binary_type(addy).decode('ascii'))
# Here's where all the magic happens!
api_response = api.get_new_addresses(index, count, security, checksum)
for addy in api_response['addresses']:
print(binary_type(addy).decode('ascii'))

print('')
print('')


def get_seed():
# type: () -> binary_type
"""
Prompts the user securely for their seed.
"""
print(
'Enter seed and press return (typing will not be shown). '
'If empty, a random seed will be generated and displayed on the screen.'
)
seed = secure_input('') # type: Text
return seed.encode('ascii')
# type: () -> binary_type
"""
Prompts the user securely for their seed.
"""
print(
'Enter seed and press return (typing will not be shown). '
'If empty, a random seed will be generated and displayed on the screen.'
)
seed = secure_input('') # type: Text
return seed.encode('ascii')


def output_seed(seed):
# type: (Seed) -> None
"""
Outputs the user's seed to stdout, along with lots of warnings
about security.
"""
print(
'WARNING: Anyone who has your seed can spend your IOTAs! '
'Clear the screen after recording your seed!'
)
compat.input('')
print('Your seed is:')
print('')
print(binary_type(seed).decode('ascii'))
print('')

print(
'Clear the screen to prevent shoulder surfing, '
'and press return to continue.'
)
print('https://en.wikipedia.org/wiki/Shoulder_surfing_(computer_security)')
compat.input('')
# type: (Seed) -> None
"""
Outputs the user's seed to stdout, along with lots of warnings
about security.
"""
print(
'WARNING: Anyone who has your seed can spend your IOTAs! '
'Clear the screen after recording your seed!'
)
compat.input('')
print('Your seed is:')
print('')
print(binary_type(seed).decode('ascii'))
print('')

print(
'Clear the screen to prevent shoulder surfing, '
'and press return to continue.'
)
print('https://en.wikipedia.org/wiki/Shoulder_surfing_(computer_security)')
compat.input('')


if __name__ == '__main__':
parser = ArgumentParser(
description = __doc__,
epilog = 'PyOTA v{version}'.format(version=__version__),
)

parser.add_argument(
'--uri',
type = text_type,
default = 'http://localhost:14265/',

help =
'URI of the node to connect to '
'(defaults to http://localhost:14265/).',
)

parser.add_argument(
'--index',
type = int,
default = 0,
help = 'Index of the key to generate.',
)

parser.add_argument(
'--count',
type = int,
default = None,

help =
'Number of addresses to generate. '
'If not specified, the first unused address will be returned.'
)

parser.add_argument(
'--security',
type = int,
default = AddressGenerator.DEFAULT_SECURITY_LEVEL,
help = 'Security level to be used for the private key / address. '
'Can be 1, 2 or 3',
)

parser.add_argument(
'--with-checksum',
action = 'store_true',
default = False,
dest = 'checksum',
help = 'List the address with the checksum.',
)

main(**vars(parser.parse_args(argv[1:])))
parser = ArgumentParser(
description=__doc__,
epilog='PyOTA v{version}'.format(version=__version__),
)

parser.add_argument(
'--uri',
type=text_type,
default='http://localhost:14265/',

help=(
'URI of the node to connect to '
'(defaults to http://localhost:14265/).'
),
)

parser.add_argument(
'--index',
type=int,
default=0,
help='Index of the key to generate.',
)

parser.add_argument(
'--count',
type=int,
default=None,

help=(
'Number of addresses to generate. '
'If not specified, the first unused address will be returned.'
),
)

parser.add_argument(
'--security',
type=int,
default=AddressGenerator.DEFAULT_SECURITY_LEVEL,
help=(
'Security level to be used for the private key / address. '
'Can be 1, 2 or 3'
),
)

parser.add_argument(
'--with-checksum',
action='store_true',
default=False,
dest='checksum',
help='List the address with the checksum.',
)

main(**vars(parser.parse_args(argv[1:])))
65 changes: 34 additions & 31 deletions examples/hello_world.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""

from __future__ import absolute_import, division, print_function, \
unicode_literals
unicode_literals

from argparse import ArgumentParser
from pprint import pprint
Expand All @@ -19,36 +19,39 @@


def main(uri):
# type: (Text) -> None
api = StrictIota(uri)

try:
node_info = api.get_node_info()
except ConnectionError as e:
print("Hm. {uri} isn't responding. Is the node running?".format(uri=uri))
print(e)
except BadApiResponse as e:
print("Looks like {uri} isn't very talkative today ):".format(uri=uri))
print(e)
else:
print('Hello {uri}!'.format(uri=uri))
pprint(node_info)
# type: (Text) -> None
api = StrictIota(uri)

try:
node_info = api.get_node_info()
except ConnectionError as e:
print(
"Hm. {uri} isn't responding; is the node running?".format(uri=uri)
)
print(e)
except BadApiResponse as e:
print("Looks like {uri} isn't very talkative today ):".format(uri=uri))
print(e)
else:
print('Hello {uri}!'.format(uri=uri))
pprint(node_info)


if __name__ == '__main__':
parser = ArgumentParser(
description = __doc__,
epilog = 'PyOTA v{version}'.format(version=__version__),
)

parser.add_argument(
'--uri',
type = text_type,
default = 'http://localhost:14265/',

help =
'URI of the node to connect to '
'(defaults to http://localhost:14265/).',
)

main(**vars(parser.parse_args(argv[1:])))
parser = ArgumentParser(
description=__doc__,
epilog='PyOTA v{version}'.format(version=__version__),
)

parser.add_argument(
'--uri',
type=text_type,
default='http://localhost:14265/',

help=(
'URI of the node to connect to '
'(defaults to http://localhost:14265/).'
),
)

main(**vars(parser.parse_args(argv[1:])))
Loading

0 comments on commit 1318039

Please sign in to comment.