## %timeit?
## %magic
## %run kl_py_pelda.py
### Az elérhető mágikus parancsok listája
%lsmagic
Available line magics: %alias %alias_magic %autoawait %autocall %automagic %autosave %bookmark %cd %clear %cls %colors %conda %config %connect_info %copy %ddir %debug %dhist %dirs %doctest_mode %echo %ed %edit %env %gui %hist %history %killbgscripts %ldir %less %load %load_ext %loadpy %logoff %logon %logstart %logstate %logstop %ls %lsmagic %macro %magic %matplotlib %mkdir %more %notebook %page %pastebin %pdb %pdef %pdoc %pfile %pinfo %pinfo2 %pip %popd %pprint %precision %prun %psearch %psource %pushd %pwd %pycat %pylab %qtconsole %quickref %recall %rehashx %reload_ext %ren %rep %rerun %reset %reset_selective %rmdir %run %save %sc %set_env %store %sx %system %tb %time %timeit %unalias %unload_ext %who %who_ls %whos %xdel %xmode Available cell magics: %%! %%HTML %%SVG %%bash %%capture %%cmd %%debug %%file %%html %%javascript %%js %%latex %%markdown %%perl %%prun %%pypy %%python %%python2 %%python3 %%ruby %%script %%sh %%svg %%sx %%system %%time %%timeit %%writefile Automagic is ON, % prefix IS NOT needed for line magics.
## mággikus utasítás létrehozása
# modul betöltés
from IPython.core.magic import (register_line_magic, register_cell_magic)
@register_line_magic
def kl_hello(line):
if line == 'OK':
print("Ez egy szép nap!")
else:
print("Hello world!")
%kl_hello
print('------- Elválasztás')
%kl_hello OK
Hello world! ------- Elválasztás Ez egy szép nap!
## ellenőrzés a mágikus beírás működik DE a charset-re vigyázzni kell!!!
with open('test.txt', 'r') as f:
print(f.read())
''' mágikus paranccsal állománybaĂrás ''' Hello world! Itt a vĂ©ge fuss el vĂ©le. ----
%%writefile test.txt
''' A mágikus paranccsal állománybaírás
'''
Hello world!
Itt a vége
fuss el véle.
----
Overwriting test.txt
%%file mprun_demo.py
''' A mágikus paranccsal állománybaírás
'''
def sum_of_lists(N):
total = 0
for i in range(5):
L = [j ^ (j >> i) for j in range(N)]
total += sum(L)
del L # remove reference to L
return total
Overwriting mprun_demo.py
print(list(dir(int))[0:10]) ## int (egész tipusnak) mágikus metodusai közül az első 10 db
num=10
## num += 5
print('v1 :', num + 5)
print('v2 :', num.__add__(5))
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__'] v1 : 15 v2 : 15
a = 5
b = 9
i = a.__eq__(b)
print(i)
False
a = 5
b = 9
i = a.__ne__(b)
print(i)
True
a = 5
b = 9
i = a.__lt__(b)
print(i)
True
i = a.__gt__(b)
print(i)
False
i = a.__le__(b)
print(i)
True
i = a.__ge__(b)
print(i)
False
i = a.__add__(b)
print(i)
14
i = a.__sub__(b)
print(i)
-4
i = a.__mul__(b)
print(i)
45
i = a.__pow__(b)
print(i)
1953125
a = 'Lajoska'
i = a.__len__()
print(i)
7
%%writefile csvmagic.py
import pandas as pd
''' A mágikus parncs tartalom elmentése
'''
from io import StringIO # Python 3
def csv(line, cell):
sio = StringIO(cell)
return pd.read_csv(sio)
def load_ipython_extension(ipython):
"""Ezt a funkciót akkor hívják, amikor a kiterjesztés betöltődik.
Elfogad egy IPython InteractiveShell példányt.
Mágikus parancs regisztrálható a „register_magic_function” segítségével
a shell példány módszere."""
ipython.register_magic_function(csv, 'cell')
Overwriting csvmagic.py
## saját mágikus parancs betöltése
%load_ext csvmagic
%%csv ### használata
col1,col2,col3
0,1,2
3,4,5
7,8,9
col1 | col2 | col3 | |
---|---|---|---|
0 | 0 | 1 | 2 |
1 | 3 | 4 | 5 |
2 | 7 | 8 | 9 |
%%javascript
alert("Hi there") // ablkba kiírás
console.log("Hello Python") // logolás
var myVar = "Awesome" // értékadás
pystartlife.output.text(myVar)
%%javascript
// python változó JS módosítása
document.getElementById("textid").innerHTML = "a szám 101 lett";
from IPython.display import HTML
pystartlife = str(100)
HTML("<div id='textid'>" + pystartlife + "</div>")
%%HTML
<h2> heder 2</h2>
%%cmd
rem CommandCom ablak (DOS / Windows)
rem path
dir c:\tmp
Microsoft Windows [Version 10.0.18363.592] (c) 2019 Microsoft Corporation. Minden jog fenntartva. C:\Users\User\Documents\mintak\jupiter\kl\okt>rem CommandCom ablak (DOS / Windows) C:\Users\User\Documents\mintak\jupiter\kl\okt>rem path C:\Users\User\Documents\mintak\jupiter\kl\okt>dir c:\tmp Volume in drive C is W10 Volume Serial Number is 3EAD-BF3C Directory of c:\tmp 2020.01.12. 19:11 <DIR> . 2020.01.12. 19:11 <DIR> .. 2020.01.12. 19:11 61 example.csv 1 File(s) 61 bytes 2 Dir(s) 156˙839˙292˙928 bytes free C:\Users\User\Documents\mintak\jupiter\kl\okt>
## Operácios rendszer kernernek átadott parancs
!dir c:\tmp
!echo "hello world from windows"
!cd
## python változó kernernek átadása
message = "hello from Python"
!echo {message}
## Kerner változo pythonba
contents = !dir c:\tmp
print(contents)
Volume in drive C is W10 Volume Serial Number is 3EAD-BF3C Directory of c:\tmp 2020.01.12. 19:11 <DIR> . 2020.01.12. 19:11 <DIR> .. 2020.01.12. 19:11 61 example.csv 1 File(s) 61 bytes 2 Dir(s) 156˙839˙288˙832 bytes free "hello world from windows" C:\Users\User\Documents\mintak\jupiter\kl\okt hello from Python [' Volume in drive C is W10', ' Volume Serial Number is 3EAD-BF3C', '', ' Directory of c:\\tmp', '', '2020.01.12. 19:11 <DIR> .', '2020.01.12. 19:11 <DIR> ..', '2020.01.12. 19:11 61 example.csv', ' 1 File(s) 61 bytes', ' 2 Dir(s) 156˙839˙288˙832 bytes free']
import math
math.sin(2)
0.9092974268256817
math.cos(2)
-0.4161468365471424
# print('In (Eddigi bemeneti tagok listája : )',In)
# print('Out (Eddigi bemeneti tagok listája : )',Out)
print(In[37])
print(Out[37])
import math math.sin(2) 0.9092974268256817
print(_) ### print(Out[38]) erre mutat // Utolsó kimeneten megjelent érték újra elérése
-0.4161468365471424
print(__) ### print(Out[38]) erre mutat // dupla aláhúzással hozzáférhet a második-utolsó kimenethez
0.9092974268256817
print(___) ### print(Out[34]) erre mutat // hármas aláhúzással a harmadik-utolsó kimenetet
<IPython.core.display.HTML object>
print(_38) ## ugyan az minta print(Out[38])
-0.4161468365471424
## parancs lista cellatartomány megadásával
%history -n 40-44
40: print(_) ### print(Out[64]) erre mutat // Utolsó kimeneten megjelent érték újra elérése 41: print(__) ### print(Out[63]) erre mutat // dupla aláhúzással hozzáférhet a második-utolsó kimenethez 42: print(___) ### print(Out[64]) erre mutat // hármas aláhúzással a harmadik-utolsó kimenetet 43: print(_34) ## ugyan az minta print(Out[34]) 44: print(_38) ## ugyan az minta print(Out[34])
## futás ido elemzés timait mágikus paranccsal
%timeit L = [n ** 2 for n in range(1000)]
%timeit sum(range(100))
331 µs ± 4.34 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each) 1.21 µs ± 30.3 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
%%timeit
## futásidő elemzés cella érvényességgel
total = 0
for i in range(1000):
for j in range(1000):
total += i * (-1) ** j
424 ms ± 1.91 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
def sum_of_lists(N):
total = 0
for i in range(5):
L = [j ^ (j >> i) for j in range(N)]
total += sum(L)
return total
## futás elemzés
%prun sum_of_lists(1000000)
'''
14 function calls in 0.814 seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
5 0.663 0.133 0.663 0.133 <ipython-input-84-c4157532b4b8>:4(<listcomp>)
5 0.103 0.021 0.103 0.021 {built-in method builtins.sum}
1 0.037 0.037 0.803 0.803 <ipython-input-84-c4157532b4b8>:1(sum_of_lists)
1 0.011 0.011 0.814 0.814 <string>:1(<module>)
1 0.000 0.000 0.814 0.814 {built-in method builtins.exec}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
'''
## hibakeresés bekapcsolása + finomitás
%debug
%xmode Plain
%xmode Verbose
# debuger bekapcsolása
%pdb on
## AZ INTERKTIVITÁS BEÁLLÍTÁSA
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
### ~/.ipython/profile_default/ipython_config.py // itt jelenik mejd meg
%run ./kl_py_web_kep_letolt.ipynb
https://i.redd.it/t958mpar84d41.jpg https://i.redd.it/75u3xtvym4d41.jpg https://i.redd.it/7npazwk3c4d41.jpg https://i.redd.it/otyy4m8ox3d41.jpg https://i.redd.it/9ubwrdgkv5d41.jpg https://i.redd.it/nfde3tyk00d41.jpg https://i.imgur.com/FZeb2fn.jpg https://i.redd.it/cv1bxdquf3d41.jpg https://i.redd.it/fy5pngaor4d41.jpg https://i.redd.it/bgjd9wzbg4d41.jpg https://i.redd.it/26c7z44435d41.jpg https://i.redd.it/epr9z9fgi4d41.jpg https://i.redd.it/h5bmzln712d41.jpg https://i.redd.it/m60cirmdc5d41.jpg https://i.redd.it/bte0kbilb6d41.jpg https://i.redd.it/6hzp7uzr16d41.jpg https://i.redd.it/ujshybr9h3d41.jpg https://i.redd.it/0yvtpgojq4d41.jpg https://i.imgur.com/LZEyRER.jpg https://i.redd.it/raeh33ln16d41.jpg https://i.redd.it/drhrkiplh6d41.jpg https://i.redd.it/4ivv80r224d41.jpg https://i.redd.it/74bvdi37e6d41.jpg ---- KÉSZ ----
# %load ./first.py
## írás mágikus utasítással
def first(l):
return l[0]
def test_first():
assert first([1, 2, 3]) == 1
assert first([]) is None
%pycat ./first.py
one = "for the money"
two = "for the show"
three = "to get ready now go cat go"
%who str
### installálás
# !pip install -c r r-essentials
# !conda install -c r r-essentials