#!/usr/bin/env python # coding: utf-8 # In[12]: from traitlets import default, HasTraits, Integer, Unicode from traitlets.config import Configurable, Config # In[16]: class MyClass(Configurable): s = Unicode('default', config=True, help="my s") i = Integer(5, config=True, help="an integer") notconfigurable = Float(1.5, help="This isn't configurable") # In[17]: print(MyClass.class_config_rst_doc()) # In[19]: c = MyClass(s='x') c.s # In[7]: class MyClass2: def __init__(self, s='default', i=5): self.s = s self.i = 5 # In[20]: import socket class MyClass3(Configurable): hostname = Unicode('something', help="my hostname") @default('hostname') def _hostname_default(self): return socket.gethostname() # In[21]: MyClass3().hostname # In[5]: c.s = "some string" c.s # In[6]: c.s = 5 # In[28]: import argparse parser = argparse.ArgumentParser() parser.add_argument('-v', dest='verbose', action='store_true') parser.add_argument('file', type=str, help="a file") parser.parse_args() # In[25]: sys.argv # In[30]: parser.parse_args(['myfile']) # In[31]: parser.parse_args(["-v", 'myfile']) # In[35]: get_ipython().system('python /Users/benjaminrk/dev/ip/scripts/showargs.py -v myfile has spaces') # In[34]: get_ipython().system('python /Users/benjaminrk/dev/ip/scripts/showargs.py -v "myfile has spaces"') # In[36]: sys.argv # In[40]: def main(argv=None): print(parser.parse_args(argv)) # In[42]: main() # In[45]: def find_six(seq): for i, item in enumerate(seq): if item == 6: print(f"Found 6 at index {i}") break else: print("Never found 6!") find_six([1, 2, 6, 5]) # In[44]: find_six([1, 2, 3]) # In[50]: from subprocess import Popen, PIPE, STDOUT p = Popen(['echo', 'hi']) #, stdout=PIPE) p.wait() type(p.stdout) # In[ ]: