Skip to content

Instantly share code, notes, and snippets.

@lgray
Created December 29, 2019 16:08
Show Gist options
  • Save lgray/97d7e88972b57891585698d96eeb0ee4 to your computer and use it in GitHub Desktop.
Save lgray/97d7e88972b57891585698d96eeb0ee4 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def awkwardql_doc(func):\n",
" def to_string():\n",
" return func.__doc__.strip()\n",
" return to_string\n",
"\n",
"def awkwardql_inspect(func):\n",
" import inspect\n",
" def to_string():\n",
" return inspect.getsource(func)\n",
" return to_string"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Writing test1.py\n"
]
}
],
"source": [
"%%writefile test1.py\n",
"\n",
"def test1():\n",
" leptons = electrons union muons\n",
"\n",
" cut count(leptons) >= 3 named \"three_leptons\" {\n",
"\n",
" Z = electrons as (lep1, lep2) union muons as (lep1, lep2)\n",
" where lep1.charge != lep2.charge\n",
" min by abs(mass(lep1, lep2) - 91.2)\n",
"\n",
" third = leptons except [Z.lep1, Z.lep2] max by pt\n",
"\n",
" hist third.pt by regular(100, 0, 250) named \"third_pt\" titled \"Leading other lepton pT\"\n",
" }"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "invalid syntax (test1.py, line 3)",
"output_type": "error",
"traceback": [
"Traceback \u001b[0;36m(most recent call last)\u001b[0m:\n",
" File \u001b[1;32m\"/home/lgray/miniconda3/lib/python3.7/site-packages/IPython/core/interactiveshell.py\"\u001b[0m, line \u001b[1;32m3319\u001b[0m, in \u001b[1;35mrun_code\u001b[0m\n exec(code_obj, self.user_global_ns, self.user_ns)\n",
"\u001b[0;36m File \u001b[0;32m\"<ipython-input-4-90a5eaac168a>\"\u001b[0;36m, line \u001b[0;32m1\u001b[0;36m, in \u001b[0;35m<module>\u001b[0;36m\u001b[0m\n\u001b[0;31m import test1\u001b[0m\n",
"\u001b[0;36m File \u001b[0;32m\"/home/lgray/test1.py\"\u001b[0;36m, line \u001b[0;32m3\u001b[0m\n\u001b[0;31m leptons = electrons union muons\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
]
}
],
"source": [
"import test1\n",
"\n",
"test1_wrapped = awkwardql_inspect(test1.test1)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Writing test2.py\n"
]
}
],
"source": [
"%%writefile test2.py\n",
"\n",
"def test2():\n",
" r\"\"\"\n",
" leptons = electrons union muons\n",
"\n",
" cut count(leptons) >= 3 named \"three_leptons\" {\n",
"\n",
" Z = electrons as (lep1, lep2) union muons as (lep1, lep2)\n",
" where lep1.charge != lep2.charge\n",
" min by abs(mass(lep1, lep2) - 91.2)\n",
"\n",
" third = leptons except [Z.lep1, Z.lep2] max by pt\n",
"\n",
" hist third.pt by regular(100, 0, 250) named \"third_pt\" titled \"Leading other lepton pT\"\n",
" }\n",
" \"\"\""
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"import test2\n",
"\n",
"test2_wrapped = awkwardql_doc(test2.test2)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'leptons = electrons union muons\\n\\n cut count(leptons) >= 3 named \"three_leptons\" {\\n\\n Z = electrons as (lep1, lep2) union muons as (lep1, lep2)\\n where lep1.charge != lep2.charge\\n min by abs(mass(lep1, lep2) - 91.2)\\n\\n third = leptons except [Z.lep1, Z.lep2] max by pt\\n\\n hist third.pt by regular(100, 0, 250) named \"third_pt\" titled \"Leading other lepton pT\"\\n }'"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"test2_wrapped()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment