Naked¶
A Python Command Line Application Framework¶
Naked (source: PyPI, GitHub) is a MIT licensed command line application framework that provides a variety of features for Python application developers.
Note
The QuickStart Guide demonstrates how to go from an empty directory to a PyPI push of your first application release using tools provided by the Naked Framework.
Here is a sample of the framework features:
New Projects¶
- New Project Generator : Create a complete project directory structure and project file stubs with the naked executable:
naked make
Command Line Parser¶
- Simple Command to Python Object Parser :
# positional strings in the command are command object attributes
# short- (e.g. '-p') and long-form (e.g. '--print') options are tested with a method
# user enters: <executable> hello world --print
c = Naked.commandline.Command(sys.argv[0], sys.argv[1:])
if c.cmd == 'hello' and c.cmd2 == "world":
if c.option('--print'):
print('Hello World!')
- Simple Command Argument Management :
# argument testing and assignment by command object methods
# user enters: <executable> -l Python --framework Naked
if c.option_with_arg('-l') and c.option_with_arg('--framework'):
language = c.arg('-l')
framework = c.arg('--framework')
print(framework + ' ' + language) # prints 'Naked Python' to standard out
- Simple Command Switch Management :
# switch testing by command object method
if c.option('-s'):
# do something
elif c.option('-l'):
# do something else
See the Command Line Parser documentation for details.
State Data¶
- Simple State Management :
# assign your own attributes to the command object for later use in your coding logic
if c.option('--spam'):
c.spam = True
c.eggs = False
if c.option('--eggs'):
c.spam = False
c.eggs = True
# other stuff
if c.spam:
print("yum")
elif c.eggs:
print("yum"*2)
See the Command Line Parser documentation for details.
- The StateObject : a compendium of automatically generated user state information. It includes data such as the Python interpreter version, operating system, user directory path, current working directory, date, time, and more.
from Naked.toolshed.state import StateObject
state = StateObject() # collects state information at time of instantiation
working_directory = state.cwd
if state.py2:
print("In the directory " + working_directory + " and using the Python 2 interpreter")
else:
print("In the directory " + working_directory + " and using the Python 3 interpreter")
See the Toolshed: state documentation for details.
Networking¶
- GET and POST requests are as simple as:
from Naked.toolshed.network import HTTP
http = HTTP("http://www.google.com")
if http.get_status_ok():
print(http.res.text)
http = HTTP("http://httpbin.org/post")
if http.post_status_ok():
print(http.res.text)
Text and binary file writes from GET and POST requests are just as easy. See the Toolshed: network documentation for details.
File I/O¶
- Supports Unicode (UTF-8) reads and writes by default:
from Naked.toolshed.file import FileReader, FileWriter
fr = FileReader('myfile.txt')
u_txt = fr.read()
fw = FileWriter('newfile.txt')
fw.write(u_txt)
There are a number of I/O methods in the FileReader
and FileWriter
classes. See the Toolshed: file documentation for details.
Execution of System Executables and Scripts¶
- System Command Execution :
from Naked.toolshed.shell import execute
execute('curl http://www.naked-py.com')
- Ruby Script Execution :
from Naked.toolshed.shell import execute_rb
execute_rb('ruby/testscript.rb')
- Node.js Script Execution :
from Naked.toolshed.shell import execute_js
execute_js('node/testscript.js')
See the Toolshed: shell documentation for more information, including documentation of exit status code checks & standard output and error stream handling from the Python side using Naked.toolshed.shell.muterun()
.
Explore Environment Variables¶
- Access Each String in the User PATH
from Naked.toolshed.shell import Environment
env = Environment()
if (env.is_var('PATH')):
for i in env.get_split_var_list('PATH'):
print(i)
See the Toolshed: shell documentation for details.
Function, Method, Class Extensions¶
The Naked toolshed types library includes extensions of commonly used Python types:
- XString extends the Python string
- XDict extends the Python dictionary
- XList extends the Python list
- XMaxHeap a max heap priority queue that extends the Python heapq
- XMinHeap a min heap priority queue that extends the Python heapq
- XSet extends the Python set
- XQueue extends the Python deque
Faster, compiled C versions of the library modules with an optional post-install compile for those who need a jetpack.
See the The Toolshed Library Overview documentation for an overview and links to the respective parts of the toolshed library documentation.
Text Templates¶
- The Ink Templating System - a lightweight, flexible text templating system that allows you to define the replacement tag syntax in your template documents. Available in the
Naked.toolshed.ink
library module.
from Naked.toolshed.ink import Template, Renderer
template_string = "I like {{food}} and {{drink}}"
template = Template(template_string)
template_key = {'food': 'fries', 'drink': 'beer'}
renderer = Renderer(template, template_key)
rendered_text = renderer.render()
print(rendered_text) # prints "I like fries and beer"
See the Toolshed: ink documentation for details.
Benchmarking¶
- Benchmarking decorators are available for your methods and functions. Insert a decorator above your function or method and get 10 trials of between 10 and 1 million repetitions of the code with comparison to a built-in test function. Comment it out and it’s gone.
from Naked.toolshed.benchmarking import timer_trials_benchmark
@timer_trials_benchmark
def your_function(arg1, arg2):
# your code
See the Toolshed: benchmarking documentation for details.
Profiling¶
- The
profiler.py
script is added to every project in the pathPROJECT/lib/profiler.py
. Insert your test code in the designated testing block and then runnaked profile
from any directory in your project. cProfile and pstats profiling is implemented with default report settings (which you can modify in theprofiler.py
file if you’d like).
Details are available in the naked executable profile documentation. An example is provided in the QuickStart Guide.
Testing¶
- Testing with the tox, nose, py.test, and the built-in Python unittest test runners can be run from any directory in your project with the
naked test
command. Use the included tests project directory for your unit test files.
Details are available in the naked executable test documentation. An example is provided in the QuickStart Guide.
Python Documentation¶
- Search the built-in Python documentation from the command line with the
pyh
naked executable command.
$ naked pyh dict
Help on class dict in module __builtin__:
class dict(object)
| dict() -> new empty dictionary
| dict(mapping) -> new dictionary initialized from a mapping object's
| (key, value) pairs
| dict(iterable) -> new dictionary initialized as if via:
| d = {}
| for k, v in iterable:
| d[k] = v
| dict(**kwargs) -> new dictionary initialized with the name=value pairs
| in the keyword argument list. For example: dict(one=1, two=2)
|
| Methods defined here:
|
| __cmp__(...)
| x.__cmp__(y) <==> cmp(x,y)
|
| __contains__(...)
| D.__contains__(k) -> True if D has a key k, else False
...
There is no need to enter the Python interactive interpreter.
Flexible and No Commitment¶
- Every component of the framework is 100% optional. You determine how much (if any) of the Naked source you need in your project. Building a project with the executable does not mandate use of the command parser, the automatically implemented help, usage, and version commands, or any part of the Naked toolshed library.
The goal is to help when you need it and get out of the way when you don’t.
Contents¶
- Naked Resources
- Install Guide
- Upgrade Guide
- Definitions
- QuickStart Guide
- Make Your Own Spam and Eggs
- Make a New Project with
naked make
- Test Your Application Version Command
- Inspect Your Project Files
- Create Your Application
- Handle Command Line Arguments for Your Application
- Create Your Help Text
- Create Your Usage Text
- Testing with
naked test
- Profiling with
naked profile
- Distribution to PyPI with
naked dist
- Naked Executable
- Naked Project Structure
- Help, Usage, and Version Commands
- Command Line Parser
- The Toolshed Library Overview
- Toolshed:
benchmarking
- Toolshed:
file
- Toolshed:
ink
- Toolshed:
network
- Toolshed:
python
- Toolshed:
shell
- Toolshed:
state
- Toolshed:
system
- Import System Module
- Import System C Module
- Description
- File and Directory Path Functions
- File Path Decorators
- File and Directory Testing Functions
- File Metadata Functions
- File Listings Functions
- Directory Write Function
- Symbolic Link Functions
- Data Stream Functions
- Application Exit Functions
- File and Directory Path Examples
- File Path Decorator Examples
- File and Directory Testing Examples
- File Metadata Examples
- File Listings Examples
- Directory Write Examples
- Data Stream Examples
- Application Exit Examples
- Toolshed:
types
:NakedObject
- Toolshed:
types
:XDict
- Toolshed:
types
:XList
- Toolshed:
types
:XMaxHeap
- Toolshed:
types
:XMinHeap
- Changes
- Licenses