Архив метки: Python

Python pyodbc postgres

import pyodbc
conn_str = (
    "DRIVER={PostgreSQL Unicode};"
    "DATABASE=postgres;"
    "UID=postgres;"
    "PWD=whatever;"
    "SERVER=localhost;"
    "PORT=5432;"
    )
conn = pyodbc.connect(conn_str)
crsr = conn.execute("SELECT 123 AS n")
row = crsr.fetchone()
print(row)
crsr.close()
conn.close()

колёсико мышки в pygame

import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
def main():
   while True:
      for event in pygame.event.get():
            if event.type == QUIT:
               pygame.quit()
               return
            elif event.type == MOUSEWHEEL:
               print(event)
               print(event.x, event.y)
               print(event.flipped)
               print(event.which)
      clock.tick(60)
main()

Подключение .NET в программу на Python

Python .NET
PS C:\Users\user> C:\Users\user\AppData\Local\Programs\Python\Python38\python.exe  -m pip install --trusted-host pypi.org --trusted-host files.pythonhosted.org pythonnet
Collecting pythonnet
  Downloading pythonnet-2.5.2-cp38-cp38-win_amd64.whl (81 kB)
     |████████████████████████████████| 81 kB 39 kB/s
Requirement already satisfied: pycparser in c:\users\user\appdata\local\programs\python\python38\lib\site-packages (from pythonnet) (2.20)
Installing collected packages: pythonnet
Successfully installed pythonnet-2.5.2
PS C:\Users\user> C:\Users\user\AppData\Local\Programs\Python\Python38\python.exe
Python 3.8.7 (tags/v3.8.7:6503f05, Dec 21 2020, 17:59:51) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import clr
>>> from System import String

>>> from System import Math
>>> Math.Round(1.2346, 3)
1.235
>>>

python pdb — список доступных команд

(Pdb) h
Documented commands (type help <topic>):
========================================
EOF    c          d        h         list      q        rv       undisplay
a      cl         debug    help      ll        quit     s        unt
alias  clear      disable  ignore    longlist  r        source   until
args   commands   display  interact  n         restart  step     up
b      condition  down     j         next      return   tbreak   w
break  cont       enable   jump      p         retval   u        whatis
bt     continue   exit     l         pp        run      unalias  where
Miscellaneous help topics:
==========================
exec  pdb
(Pdb)
python3 -m pdb myscript.py

Python: построчное формирование pandas dataframe

>>> import pandas as pd
>>> from numpy.random import randint
>>> df = pd.DataFrame(columns=['lib', 'qty1', 'qty2'])
>>> for i in range(5):
>>>     df.loc[i] = ['name' + str(i)] + list(randint(10, size=2))
>>> df
     lib qty1 qty2
0  name0    3    3
1  name1    2    4
2  name2    2    8
3  name3    2    1
4  name4    9    6