Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ project and an extension to IPython and
[JupyterLab](https://github.com/deshaw/jupyterlab-pyflyby), that, among many
things, automatically inserts imports and tidies Python files and notebooks.


# What is pyflyby?
## What is pyflyby?

Pyflyby is a set of tools designed to improve interactive and non-interactive
workflows in Python. Pyflyby provides a number of utilities and extensions aimed
Expand Down Expand Up @@ -65,7 +64,6 @@ the `py` executable replacing python.
With the [jupyterlab-pyflyby](https://github.com/deshaw/jupyterlab-pyflyby) extension, imports will be executed and inserted in
the first cell of one's notebook:


![Fresh Jupyter session with two cells: the first imports Matplotlib, and the
second plots using both Matplotlib and NumPy. Upon execution, jupyterlab-pyflyby
automatically adds the missing NumPy import in the first cell, and then
Expand All @@ -83,7 +81,6 @@ format imports in Python files. This is similar to tools such as
[isort](https://pypi.org/project/isort/), but with different styling options and
with the ability to infer missing imports.


`tidy-import` includes the imports to pandas and matplotlib, as in the example
below, and queries whether to update the file:

Expand Down Expand Up @@ -114,11 +111,10 @@ IPython or to execute commands quickly from the developer's shell without the
need for imports. It supports a range of syntax options, allowing for quick
calculation and graph plotting.

- Without any parameters, `py` will start IPython with the pyflyby extension
activated.
- With space-separated arguments, `py` will attempt to interpret each argument
as a Python function call with the right imports:

- Without any parameters, `py` will start IPython with the pyflyby extension
activated.
- With space-separated arguments, `py` will attempt to interpret each argument
as a Python function call with the right imports:

```bash
$ py np.random.normal 0 1
Expand All @@ -139,45 +135,44 @@ $ py 'plot(scipy.stats.norm.pdf(linspace(-5, 5), 0, 1))'
```

![Bash shell where the py command is passed a string argument, which is code
that renders a Matplotlib plot using methods that were not imported. The plot
that renders a Matplotlib plot using methods that were not imported. The plot
successfully renders. The Python code used is printed, which contains the
automatically inferred imports and ends with the passed code.](/posts/better-interractive-jupyter-session-with-pyflyby/py-exec-matplotlib.png)

`find-import` , another utility available in pyflyby, can be deployed to find a
particular function across many libraries by returning the relevant import. For
particular function across many libraries by returning the relevant import. For
example:

```bash
$ find-import norm
from scipy.stats.distributions import norm
```

# Notes on pyflyby codebase
## Notes on pyflyby codebase

The [Pyflyby codebase](https://github.com/deshaw/pyflyby) provides a window into
advanced data structures and programming paradigms. Its use of modules and
programming concepts is unusual relative to those found in more classical data
science-focused libraries. For example:

- Pyflyby will conduct non-trivial manipulation of the Python Abstract Syntax
Tree (AST), which represents code written in tree form. Pyflyby uses AST to
find and insert missing imports, and it does so even as AST's exact
representation changes with almost every minor release.
- Pyflyby will conduct non-trivial manipulation of the Python Abstract Syntax
Tree (AST), which represents code written in tree form. Pyflyby uses AST to
find and insert missing imports, and it does so even as AST's exact
representation changes with almost every minor release.

- Pyflyby demonstrates the use of [Aspect-Oriented
programming](https://en.wikipedia.org/wiki/Aspect-oriented_programming),
which highlights the flexibility of the Python programming model.
- Pyflyby demonstrates the use of [Aspect-Oriented
programming](https://en.wikipedia.org/wiki/Aspect-oriented_programming),
which highlights the flexibility of the Python programming model.


# Conclusion
## Conclusion

Pyflyby's utilities are designed to improve developer efficiency and materially
reduce the impact of interruption on productivity. Its value, however, is
broader than that. Pyflyby expands what one is capable of doing within the Python ecosystem
and has helped identify a number of limitations and bugs in Python and IPython
over time.

# How to get pyflyby
## How to get pyflyby

Pyflyby is available on [GitHub](https://github.com/deshaw/pyflyby) or, for
terminal IPython users:
Expand All @@ -191,18 +186,7 @@ JupyterLab users can also install [the JupyterLab
Extension](https://github.com/deshaw/jupyterlab-pyflyby), which is
notebook-aware and enables even more features.


# Acknowledgements
## Acknowledgements

Pyflyby was created by Karl Chen and is supported by the [D. E. Shaw
group](https://www.deshaw.com/) in collaboration with [Quansight](https://www.quansight.com).










66 changes: 22 additions & 44 deletions apps/labs/posts/but-what-are-traitlets.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ hero:
You have probably seen Traitlets in applications, you likely even use it. The package has nearly 5 million downloads
on [conda-forge](https://anaconda.org/conda-forge/traitlets) alone.

# But, what is Traitlets ?
## But, what is Traitlets ?

In this post we'll answer this question along with where Traitlets came from, its applications, and a bit of history.

## `traitlets` 5.0
### `traitlets` 5.0

`traitlets` 5.0 has recently been released; 5 years after the earliest versions of
`traitlets` 4.0. The latest and greatest 5.0 release brings
Expand All @@ -34,25 +34,25 @@ Traitlets is [a library](https://pypi.org/project/traitlets) that provides objec
can expose individual configuration options in an intelligent way. They are used in almost all the Jupyter Projects.

Traitlets began as a pure Python implementation of the Enthought [Traits](https://pypi.org/project/traits/) library.
These libraries implement the object-oriented [trait pattern](https://en.wikipedia.org/wiki/Trait_(computer_programming)). Prior to 2015, `traitlets` was a part of the IPython (pre-jupyter) code base; then during ["The Big
These libraries implement the object-oriented [trait pattern](<https://en.wikipedia.org/wiki/Trait_(computer_programming)>). Prior to 2015, `traitlets` was a part of the IPython (pre-jupyter) code base; then during ["The Big
Split"](https://blog.jupyter.org/the-big-split-9d7b88a031a7) it was moved to their own reusable package.

Both `traitlets` and `traits` addressed the challenge of using typed code in interactive Python [REPL](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop "read-eval-print-loop")s. They offer type checking, coercion and validation at run time.
Both `traitlets` and `traits` addressed the challenge of using typed code in interactive Python [REPL](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop 'read-eval-print-loop')s. They offer type checking, coercion and validation at run time.

### Traitlets for development
#### Traitlets for development

The general idea when developing with Traitlets is:

1. The developer defines a class-level attribute,

2. This class level attribute will automatically be converted into
*a property-like* element with *runtime type and value checking*, *configurability* and
*observable events and changes*.
_a property-like_ element with _runtime type and value checking_, _configurability_ and
_observable events and changes_.

Traitlets minimizes boilerplate for Python applications. Traitlets maintains a uniform
naming convention and helps your users configure their applications.

## A Traitlets usage example
### A Traitlets usage example

We'll demonstrate the flexibility of Traitlets by configuring the `autocall`
traitlet/option of IPython from the command line interface, configuration
Expand Down Expand Up @@ -87,7 +87,6 @@ either `0`,`1`, or `2`. Traitlets provides a number of utilities to decide
whether assigning incorrect values should raise an exception; or coerce to one
of the valid ones. Here a wrong assignment will fail:


```
$ ipython --no-banner

Expand All @@ -101,7 +100,6 @@ In [3]: ip.autocall = 5
TraitError: The 'autocall' trait of a TerminalInteractiveShell instance expected any of [0, 1, 2], not the int 5.
```


While type – and value – checking at runtime is a nice feature, most of these
options are usually user preferences. Traitlets provides a way to automatically
create configuration files with help, as well as command line arguments
Expand All @@ -111,8 +109,7 @@ application on how to automatically generate configuration files, decide on
the option name and document it. No need for the developer to spend time on
deciding on a configuration parameter name.


### A generated configuration file.
#### A generated configuration file.

On a brand new machine with IPython installed you will find the following in
your default configuration file `~/.ipython/profile_default/ipython_config.py`,
Expand Down Expand Up @@ -171,7 +168,6 @@ to actually do the reformatting, the complete diff to add the options to the CLI
configuration file, and [automatic generation](https://github.com/ipython/traitlets/blob/3293530b6943b9522ae570e7ca29b30709a43567/traitlets/config/sphinxdoc.py#L1-L33) of [the option documentation](https://ipython.readthedocs.io/en/7.18.0/config/options/terminal.html#configtrait-TerminalInteractiveShell.autoformatter) in Sphinx
documentation is as follows:


```diff
@IPython/terminal/interactiveshell.py:98
Class TerminalInteractiveShell(InteractiveShell)
Expand All @@ -192,7 +188,7 @@ configuration options is decentralized and lives on the object being
configured. And there is no differences between adding a configuration option
in the configuration file or at the command line interface.

# Configure what you do not yet know about
## Configure what you do not yet know about

In an application with few parameters and only a couple of plugins it might be
relatively straightforward to provide options and CLI arguments; this becomes
Expand Down Expand Up @@ -241,10 +237,9 @@ Thus if you have an application or library with a number of plugins, and for
which configurability can be thought of as tree-like similar to a class
hierarchy, Traitlets can help you.

## Other functionalities

# Other functionalities

## Observability
### Observability

Beyond the configurability part is observability; as we already had a great
type system with hooks, and sometimes you may want to mutate configuration of
Expand Down Expand Up @@ -278,17 +273,15 @@ visualisations.
This can be useful to glue together models, with parameters needing to be
synchronized and models needing to react to changes.


## Dynamic defaults
### Dynamic defaults

Traitlets supports [dynamic defaults](https://traitlets.readthedocs.io/en/stable/api.html#dynamic-default-values),
i.e. your default values may depend on some context (OS, Python version).

Configurations are by default Python scripts (but can be JSON), so user
config files can be shared across machines and have dynamic values.


## Context base configuration
### Context base configuration

Object configuration can also look at the creator of the object at instantiation
time.
Expand All @@ -303,24 +296,24 @@ default values. This is for example used by [nbconvert](https://github.com/jupyt
to decide which options to activate depending on whether you do `--to=pdf`,
or `--to-html`

## Flags and aliases
### Flags and aliases

Long flag names like `--InteractiveShell.debug=True` can be cumbersome to type.
That is why traitlets provide aliases and flags, so that `ipython --debug` will
set `debug=True` to enable logging on all classes supporting it while still
allowing you to tweak the login level used on a per-class basis.

# Limitations
## Limitations

Of course all this power doesn't come completely for free:

- Your class and attribute names become part of your API
- As configuration is loaded/read before any potential plugins are loaded, it is
impossible to definitively detect typos or invalid configuration options.
- Traitlets relies heavily on metaclasses, so it can add a construction cost to
your objects and can be hard to debug.
- Your class and attribute names become part of your API
- As configuration is loaded/read before any potential plugins are loaded, it is
impossible to definitively detect typos or invalid configuration options.
- Traitlets relies heavily on metaclasses, so it can add a construction cost to
your objects and can be hard to debug.

# Conclusion
## Conclusion

This was a short introduction to Traitlets. I hope it made it a little clearer
how the Jupyter configuration system works, and we are looking forward to see
Expand All @@ -334,18 +327,3 @@ If you want to learn more, see [this JupyterCon 2018
talk](https://www.youtube.com/watch?v=_gYEVTaNuKU) and the [documentation](https://traitlets.readthedocs.io/).

Try the new 5.x version and let us know if you have questions.















10 changes: 5 additions & 5 deletions apps/labs/posts/code-generator-and-nebari.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ hero:

To the readers, I am Aryan Gupta(@[guptaaryan16](https://github.com/guptaaryan16/)), an EE Junior at IIT Roorkee, and this summer, I had a chance to work on PyTorch-Ignite’s Code-Generator project, a tailor-made web application to help machine learning researchers and enthusiasts and also keeping in mind the growing Kaggle community.

# The project itself
## The project itself

Let’s see the project itself. [Code-Generator](https://code-generator.pytorch-ignite.ai/) is a [Vue.js](https://vuejs.org/) application that streamlines the process of working on machine learning tasks. The app generates preconfigured code templates for tasks like vision classification, text classification, and other common themes in ML competitions.

Expand All @@ -24,7 +24,7 @@ This app was created with the great efforts of members of the PyTorch-Ignite com

![The Code-Generator App](/posts/code-generator-and-nebari/app.png)

# The issues I worked on
## The issues I worked on

My tasks were primarily to work on features that could improve the reproducibility-related features of the templates. Also, I had to contribute to new features and libraries which can be best for experiments' configuration management. My contributions to the project were mainly three-fold, and are detailed below.

Expand Down Expand Up @@ -113,7 +113,7 @@ But it can be challenging to manage, and we also need to ensure we satisfy the `

Some of the related PRs can be found here: [#283](https://github.com/pytorch-ignite/code-generator/pull/283) [#288](https://github.com/pytorch-ignite/code-generator/pull/288)

# The way forward for the project
## The way forward for the project

The Code-Generator project seems to be moving forward at a great pace and is expected to have many more additions in the future. Starting and maintaining a new project is challenging in open source and can have a very high risk/reward ratio. Still, good communities try their best to make and maintain projects that can be helpful for the maximum number of people and reduce the friction for new people entering the community. Here are some of the issues I suggest and may work on in the future.

Expand All @@ -131,7 +131,7 @@ We will add more templates and feature options in the future to the project. Tem

P.S. If you feel excited about the project, please feel free to suggest more changes and contribute to the project.

# Some lessons I learnt along the way
## Some lessons I learnt along the way

This internship experience increased my confidence in maintaining an open-source codebase and understanding how to test functions and debug changes. It also helped me understand the value of real-life design decisions and CI and how they can significantly influence the project for the good or the bad. Some of the best practices I learned in the open-source projects are listed below.

Expand All @@ -144,7 +144,7 @@ When we try to write new code or change the existing files, we often try to do i
While making a pull request(PR) seems very exciting on a project, you should know what and how much you want to accomplish in one pull request. Sometimes, we can make too many functional changes in one pull request, making testing and removing all the bugs difficult. This issue was evident in my time as an intern as some of my PRs were terribly formatted, and this led to me completely rebasing them and making 4-5 more PRs to the main project to remove many bugs in those changes. This was painful for me and my mentor, but I thank him for helping me with great suggestions and reviews.
Also, this experience increased my learning and helped me improve the quality of my pull requests. So don’t be afraid to make mistakes; learn from them and make your way in this incredible world of OSS!

# Acknowledgements
## Acknowledgements

In this section, I would like to thank my mentor, @[vfdev-5](https://github.com/vfdev-5), and other members of the PyTorch-Ignite community for helping me in this internship. Also, I would like to thank the people at [Quansight-Labs](https://quansight.com/) for providing me with such a great opportunity, especially @[rgommers](https://github.com/rgommers), @[trallard](https://github.com/trallard), @[melissawm](https://github.com/melissawm), and others for their invaluable guidance and time during this internship.

Expand Down
6 changes: 3 additions & 3 deletions apps/labs/posts/cxx-numba-interoperability.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ in foo(7)
130
```

# Summary
## Summary

In this post, we outlined a method of calling C++ library functions
from Python with an emphasis on their usage from Numba compiled
Expand All @@ -275,12 +275,12 @@ functions with minimal overhead. While the provided tool
with scalar inputs and return values, it can be easily extended to
support other C++ features as well.

# Acknowledgements
## Acknowledgements

I thank Breno Campos and Guilherme Leobas for discussions and for sharing
their expertise on the [LLVM Project](https://www.llvm.org/).

# Appendix: A list of Python/C/C++ connectivity tools
## Appendix: A list of Python/C/C++ connectivity tools

- [Boost.Python: C++ library with a IDL-like interface for binding C++ classes and functions to Python](https://www.boost.org/doc/libs/1_75_0/libs/python/doc/html/index.html)
- [cffi: C Foreign Function Interface for Python](https://cffi.readthedocs.io/en/latest/)
Expand Down
Loading