Skip to content

Commit

Permalink
v3.1 (#11)
Browse files Browse the repository at this point in the history
* Add option to "surprise" user by randomly selecting a generation method

* Add option to generate names to terminal output (stdout)

* Added documentation for both features

* Added CircleCI tests for both features
  • Loading branch information
BBaoVanC authored Mar 18, 2020
1 parent 2ab37cf commit f92836a
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 41 deletions.
6 changes: 6 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ jobs:
pip list
pynamegen debug=True file=classic.txt method=classic
pynamegen debug=True file=random.txt method=random
pynamegen debug=True file=surprise.txt method=surprise
pynamegen amt=1 debug=False file=stdout method=surprise
python37:
docker:
- image: circleci/python:3.7.7
Expand All @@ -25,6 +27,8 @@ jobs:
pip list
pynamegen debug=True file=classic.txt method=classic
pynamegen debug=True file=random.txt method=random
pynamegen debug=True file=surprise.txt method=surprise
pynamegen amt=1 debug=False file=stdout method=surprise
python38:
docker:
- image: circleci/python:3.8.2
Expand All @@ -38,6 +42,8 @@ jobs:
pip list
pynamegen debug=True file=classic.txt method=classic
pynamegen debug=True file=random.txt method=random
pynamegen debug=True file=surprise.txt method=surprise
pynamegen amt=1 debug=False file=stdout method=surprise
workflows:
version: 2
circleci_python3:
Expand Down
61 changes: 59 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,38 @@ Saving file...
Finished!
```

The default name generation method is classic, and looks like 'TheAssignmentanatorifier_90'.
You can also generates the names to terminal output by setting the filename to `stdout`. If you do so, make sure to set debug to False! This makes it easy to use the CLI as a way to send generated names to a program without using the Python API.

The generation method random looks like 'XaYyaknkCoH8'.
Generate 5 names with the random method and output to terminal:

``` plaintext
$ pynamegen amt=5 method=random debug=False file=stdout
Kc3HcV3pq_n0
ncwUV_Twbx7s
jYs56B1y_WxU
YN5_cU6fhwXc
SI46Rnp9skAo
```

The output for the above command will differ because the generated names will not be the same as in this example.

The following example generates a name with the classic method and pipes it to the `cowsay` command (which just outputs text of a cow and a message box). You can pipe the generated name to any command; this is just an example.

``` plaintext
$ pynamegen amt=1 method=classic debug=False file=stdout | cowsay
_______________
< Taleanator239 >
---------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
```

The default name generation method is `classic`, and looks like 'TheAssignmentanatorifier_90'.

The generation method `random` looks like 'XaYyaknkCoH8'.

You can change the generation method used by using the argument 'method=[method]' and replace [method] with the correct method.

Expand All @@ -152,6 +181,34 @@ Saving file...
Finished!
```

If you choose `surprise` as the generation method, a generation method will be randomly selected.

``` plaintext
$ pynamegen method=surprise
Randomly selected method: classic
Generating names...
[####################] 100% [100/100]...done
Preparing list to write to file...done
Opening file...
Writing names...
[####################] 100% [100/100]...done
Saving file...
Finished!
```

``` plaintext
$ pynamegen method=surprise
Randomly selected method: random
Generating names...
[####################] 100% [100/100]...done
Preparing list to write to file...done
Opening file...
Writing names...
[####################] 100% [100/100]...done
Saving file...
Finished!
```

---

## License
Expand Down
117 changes: 79 additions & 38 deletions scripts/pynamegen
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ PyNameGen CLI.
by BBaoVanC
PyNameGen is a CLI for libnamegen.`
PyNameGen is a CLI for libnamegen.
Documentation is located at:
https://github.com/BBaoVanC/PyNameGen
Expand All @@ -13,21 +13,52 @@ Copyright (C) 2020 BBaoVanC

# Imports
import sys
import random
import libprogress

allmethods = ["classic", "random"]


class ArgError(Exception): # custom error for invalid arguments
"""This error occurs when an argument passed to the program is invalid."""

pass


def parsemethod(methodname):
"""
Take a method name as input and output the library
Arguments:
methodname -- name of method in string form
Returns:
method -- variable containing the library class
"""
if methodname == "classic": # if method is 'classic'
import libnamegen.classic # import the classic method
method = libnamegen.classic # save this in argument list

elif methodname == "random": # if method is 'random'
import libnamegen.random # if method is the random gen
method = libnamegen.random # save this in argument list

else: # if none of the existing methods was selected
raise(ArgError("Arg for 'method' is invalid"))

return method


helptxt = """Usage:
pynamegen [options]
Options:
amt: Amount of names to generate
debug: Whether or not to output debug information
method: Which name generation method to use
file: file to output generated names to. Set to
"stdout" (without quotes) to output to terminal instead of a file.
If you use "stdout", it's a good idea to set debug to False!
method: Which name generation method to use. Set to
"surprise" (without quotes) to choose one at random
Example:
pynamegen amt=50 debug=True file=mynames.txt method=classic"""
# ^^^ this is the help menu text ^^^ #
Expand All @@ -40,6 +71,7 @@ args = {"amt": 100, # default amount of names to generate
} # default arguments
msel = False # saves if the method has been selected
gennames = True # saves if names should be generated
stdout = False # saves if the names should be outputted to stdout
for arg in cmdline: # this block converts 'cmdline' to dictionary 'args'
if arg in ["--help", "-h", "help"]: # if one of the help options is used
print(helptxt) # print the help menu text
Expand All @@ -60,59 +92,68 @@ for arg in cmdline: # this block converts 'cmdline' to dictionary 'args'

elif arg.startswith("file="): # if the selected argument is 'file'
args["file"] = str(arg.split("=")[1]) # save this in argument list
if args["file"] == "stdout": # if the files should be sent to terminal
stdout = True # stdout mode True

elif arg.startswith("method="): # if the selected argument is 'method'
msel = True # note that the method has been selected
b = arg.split("=")[1].lower() # convert argument to lowercase
if b == "classic": # if method is 'classic'
from libnamegen import classic # import the classic method
args["method"] = classic # save this in argument list
elif b == "random": # if method is 'random'
from libnamegen import random # if method is the random gen
args["method"] = random # save this in argument list
else: # if none of the existing methods was selected

if b == "surprise": # if the method should be picked randomly
rmethod = random.choice(allmethods) # nosec | pick random method
args["method"] = parsemethod(rmethod) # get method class
if args["debug"]: # if we should output debug information
print("Randomly selected method: " + rmethod)
# ^^^ tell what method was randomly selected
elif b not in allmethods:
raise(ArgError("Arg for 'method' is invalid"))
else:
args["method"] = parsemethod(b)

if gennames is True: # if we should generate the names
if not msel: # if the method hasn't been selected
from libnamegen import classic # import the classic method
args["method"] = classic # save the method in argument list
import libnamegen.classic # import the classic method
args["method"] = libnamegen.classic # save the method in argument list
if args["debug"]: # if we should output debug information
print("Generating names...") # log message
# use our function to generate names into the variable 'usernames'
# pylint: disable=no-member
usernames = args["method"].gen(count=args["amt"], debug=args["debug"])
# ^^^ generate the names with the selected method and options ^^^ #

if args["debug"]: # if we should output debug information
print("Preparing list to write to file", end="\r") # log message

# vvv prepare a list for writing directly to the file vvv #
usernames2 = list() # create new list named usernames2
for item in usernames: # for each item in the username list
# add newline character to each item in the usernames2 list...
item = item + "\n" # add a newline to the end of each name
usernames2.append(item) # ...
usernames2[-1] = usernames2[-1].strip() # remove newline from last item
if args["debug"]: # if we should output debug information
print("Preparing list to write to file...done") # log message
if not stdout: # if the names should be outputted to a file
if args["debug"]: # if we should output debug information
print("Preparing list to write to file", end="\r") # log message

# vvv prepare a list for writing directly to the file vvv #
usernames2 = list() # create new list named usernames2
for item in usernames: # for each item in the username list
# add newline character to each item in the usernames2 list...
item = item + "\n" # add a newline to the end of each name
usernames2.append(item) # ...
usernames2[-1] = usernames2[-1].strip() # remove \n from last name
if args["debug"]: # if we should output debug information
print("Preparing list to write to file...done") # log message

# vvv start making file vvv #
# vvv start making file vvv #
print("Opening file...")
# open the list of names. + means to create the file if it doesn't exist
file = open(args["file"], "w+") # open the file in overwrite mode (w+)

wtotal = len(usernames2) # save the total names in wtotal for progress log
if args["debug"]: # if debug is enabled
print("Writing names...")
for indx, item in enumerate(usernames2): # indx is the index
file.write(item) # write each username to the file
# open the list of names. '+' will create the file if it doesn't exist
file = open(args["file"], "w+") # open the file in overwrite mode (w+)

wtotal = len(usernames2) # save the total amount of names in wtotal
if args["debug"]: # if debug is enabled
print("Writing names...")
for indx, item in enumerate(usernames2): # indx is the index
file.write(item) # write each username to the file
if args["debug"]: # if we should output debug information
print(libprogress.genbar(curprg=indx+1, maxprg=wtotal),
end="\r") # ^^^ log message ^^^ #
if args["debug"]: # if we should output debug information
print(libprogress.genbar(curprg=indx+1, maxprg=wtotal), end="\r")
# ^^^ log message ^^^ #
if args["debug"]: # if we should output debug information
print(libprogress.genfullbar(prg=wtotal))
print("Saving file...") # log message
file.close() # close the file and apply our changes
print(libprogress.genfullbar(prg=wtotal))
print("Saving file...") # log message
file.close() # close the file and apply our changes
else:
for name in usernames:
print(name)
if args["debug"]: # if we should output debug information
print("Finished!") # log message
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="pynamegen",
version="3.0.1.post1",
version="3.1",
author="BBaoVanC",
author_email="bbaovanc@protonmail.com",
description="PyNameGen is a CLI for libnamegen",
Expand Down

0 comments on commit f92836a

Please sign in to comment.