python – Performs functions in Pandas DataFrame

I have a data frame like this:

date query
29-03-2019 SELECT * FROM tableN WHERE ..
30-03-2019 SELECT * FROM ... JOIN ... ON ... WHERE ..
.... ....
20-05-2019 CHOOSE ...

And I want to apply two functions (say, my functions are getTable and getCol) in this data frame to retrieve the tables and columns from the query.

The new data frame looks like this:

Date tableName attributeName
29-03-2019  [tableN]                      [attM]



30-03-2019  [tableA as A,tableC as C]     [A.attB, C.attD]



.... ... ...
20-05-2019  [tableF]                      [attG]

I want to do both functions together for each query. I tried that:

def getTableAttribute (dF):
df2 = dF.copy ()
df 2["tableName"] = df2["Query"].apply (getable)
df 2["attributeName"] = df2["Query"].apply (getCol)
df2.drop ("Query", Axis = 1, Inplace = True)
Print ("Done  n")
return df2

However, this feature runs very slowly on large amounts of data.
I need an opinion on what I've tried since my code is slow with large files.

Python – Why is not Pydot installed?

I work my way through Google's machine learning videos. To complete one of the modules, I had to install pydot pip, which was done successfully at the command prompt. I returned to my code and repeatedly got the error "name & # 39; pydot & # 39; is not defined", but I have made sure several times that I have installed it. I've even closed jupyter and reopened and get the same mistake. Any help would be appreciated.

Import numpy as np
import pydot
import load_iris from sklearn.datasets

iris = load_iris ()
test_idx = [0, 50, 100]

train_target = np.delete (iris.target, test_idx)
train_data = np.delete (iris.data, test_idx, axis = 0)

test_target = iris.target[test_idx]
test_data = iris.data[test_idx]

clf = tree.DecisionTreeClassifier ()
clf.fit (train_data, train_target)

print (test_target)
print (clf.predict (test_data))

import StringIO from sklearn.externals.six

dot_data = StringIO ()
tree.export_graphviz (clf,
out_file = dot_data,
feature_names = iris.feature_names,
class_names = iris.target_names,
filled = true, rounded = true,
Contamination = wrong)
graph = pydot.graph_from_dot_data (dot_data.getvalue ())
graph.write_pdf ("iris.pdf")

print (test_data[0]test_target[0])

html – Best way to implement a simple HTTP server for the user interface in Python?

I am doing a web scraping tool and would like to use the possibilities of HTML, CSS, JavaScript etc. I've been freelancing as a web developer before, and I'm much more comfortable creating a website than designing a graphical user interface.

So far, it looks like trying to implement my own simple HTTP server will be a mess of code and take longer than the actual scraper. Are there any special options for these types of things?

Python – case sensitive

The question can be found here https://leetcode.com/problems/letter-case-permutation

With a string S we can transform each letter individually
Uppercase or lowercase letters to create another string. Returns a list of all
possible strings that we could create.

My question is mainly about the pattern that I follow. I think I'm doing two versions of a function with many of these leetcode questions that I want to use recursively. One that acts as an "interface" is this case
letterCasePermutation and one who acts as a worker (letterCasePermutationAuxwhere the only difference is the arguments (I would normally have something like a counter and a vector reference to store my answers).

I choose this question as an example, but really every recursive question could play a role here.

ans = []

def letterCasePermutationAux (self, S, i = 0):

if i> = len (S) and len (S)> 0:
self.ans.append (S)
return

if s[i].isalpha ():
temp = list (s)
temp[i] = S[i].upper () if S[i].islower () else S[i].lower()
self.letterCasePermutationAux ("". join (temp), i + 1)

self.letterCasePermutationAux (S, i + 1)


def letterCasePermutation (self, S):
self.ans =[]
    self.letterCasePermutationAux (S)
return self.ans

My question is, is there a better way to get the job done? One that can only recursion with one function?

Integer field in Python with additional restrictions

I am trying to write an integer field in Python with the following attributes:

  • minimum
  • Exclusive minimum
  • Maximum
  • Exclusive maximum
  • Several of
  • value

Basically, given the constraints and the value, the class has a validation method that returns the errors as they occur. If the minimum is not zero, the exclusive minimum is explicitly set to zero to avoid redundancy. The same applies to maximum and exclusive maximum. All restrictions can be null. The restrictions work as follows:

  • The value must be greater than or equal to the minimum.
  • The value must be greater than the exclusive minimum.
  • The value must be less than or equal to the maximum.
  • The value must be less than the exclusive maximum.
  • The multiple of must be a factor of value.

Note: All restrictions are assumed to be integers, so no type checks are performed. This is because these values ​​come from a database, so the type is always an integer or null.

So far I have written the following class:

Class IntegerField:
def add_error (self, key, error):
self.errors.setdefault (key, [])
yourself. mistake[key].append (error)

def __init __ (self, minimum = None, exclusive_minimum = None,
maximum = None, exclusive_maximum = None, multiple_of = None):
self.minimum = minimum
self.exclusive_minimum = exclusive_minimum
self.maximum = maximum
self.exclusive_maximum = exclusive_maximum
self.multiple_of = multiple_of

if self.minimum:
self.exclusive_minimum = None
if self.maximum:
self.exclusive_maximum = None

self.value = none
self.errors = {}

if (self.minimum and self.maximum):
if self.minimum> self.maximum:
self.add_error
& # 39; Minimum & # 39 ;,
f & # 39; The minimum must be less than or equal to the maximum & # 39;
f & # 39; of {self.maximum}. & # 39;
)
self.add_error
& # 39; maximum & # 39 ;,
f & # 39; the maximum must be greater than or equal to & # 39;
f & # 39; minimum of {self.minimum}. & # 39;
)
elif (self.minimum and self.exclusive_maximum):
if self.minimum> = self.exclusive_maximum:
self.add_error
& # 39; Minimum & # 39 ;,
f & # 39; The minimum must be less than the exclusive maximum of & # 39;
f & # 39; {self.exclusive_maximum}. & # 39;
)
self.add_error
& # 39; # 39 ;, & exclusive_maximum
f & # 39; The exclusive maximum must be greater than the & # 39;
f & # 39; minimum of {self.minimum}. & # 39;
)
elif (self.exclusive_minimum and self.maximum):
if self.exclusive_minimum> = self.maximum:
self.add_error
& # 39; # 39 ;, & exclusive_minimum
f & # 39; The exclusive minimum must be less than the maximum of & # 39;
f & # 39; {self.maximum}. & # 39;
)
self.add_error
& # 39; maximum & # 39 ;,
f "The maximum must be greater than the exclusive minimum"
f & # 39; of {self.exclusive_minimum}. & # 39;
)
elif (self.exclusive_minimum and self.exclusive_maximum):
if self.exclusive_minimum> = (self.exclusive_maximum - 1):
self.add_error
& # 39; # 39 ;, & exclusive_minimum
f & # 39; the exclusive minimum must be less than one minus the & # 39;
f & # 39; exclusive maximum of {self.exclusive_maximum}. To & # 39;
eg if the exclusive maximum is set to 101, then
The exclusive minimum must be less than 100, ie 99 or
f & # 39; less. & # 39;
)
self.add_error
& # 39; # 39 ;, & exclusive_maximum
f & # 39; The exclusive maximum must be greater than one plus the & # 39;
f & # 39; exclusive minimum of {self.exclusive_minimum}. To & # 39;
eg if the exclusive minimum is set to 99, then
The exclusive maximum must be greater than 100, ie 101 & # 39;
for more. & # 39;
)

if bool (self.errors):
# Increase the errors

def set_value (self, value):
self.value = value

validate def (yourself):
if self.value:
print (& # 39; Please enter a value. & # 39;)
otherwise:
validation_errors = []

            if self.multiple_of:
if is_not_factor (self.value, self.multiple_of):
validation_errors.append (
f & # 39; The value must be a multiple of {self.multiple_of}. & # 39;
)

if self.minimum:
if self.value <self.minimum:
validation_errors.append (
f & # 39; The value must be greater than or equal to the minimum & # 39;
from {self.minimum}. & # 39;
)
return
elif self.exclusive_minimum:
if self.value <= self.exclusive_minimum:
                    validation_errors.append(
                        f'The value must be greater than the exclusive minimum of '
                        f'{self.exclusive_minimum}.'
                    )
                    return

            if self.maximum:
                if self.value > Selbst.Maximum:
validation_errors.append (
f & # 39; The value must be less than or equal to the maximum of & # 39 ;.
f & # 39; {self.maximum}. & # 39;
)
elif self.exclusive_maximum:
if self.value> = self.exclusive_maximum:
validation_errors.append (
f & # 39; The value must be less than the exclusive maximum of & # 39;
f & # 39; {self.exclusive_maximum}. & # 39;
)

Do I miss something in my class? In particular, I would like to know if the two methods (init and validate) work or not in every possible scenario. Does my current code also check for any single combination of constraints, or am I missing something?

base58 – Convert decimal numbers into base58check with Python 3.7

As part of the routine I used

ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"

def convertToBase58 (num):
sb = & # 39; & # 39;

while (num> 0):
r = num% 58 # divide by 58 and give the rest
sb = sb + ALPHABET[r]
    num = num / 58;
to return sb[::-1]

This comes back with an error stating that r must be an integer. However, using the% operator seems to define r as an integer. What did I miss, please?

html – How to use driver.current_url on a new tab that opens with .click () on Selenium for Python

I'm writing a Python script that uses BeautifulSoup to scrape the web and then Selenium to navigate websites. After navigating to a link to another site using .click (), I'd like to use .current_url to get the site URL to use for a nice soup. The problem is that the .click () opens the link in a new tab, so when I use current_url, I get the URL of the original site.

I tried with:

second_driver.find_element_by_tag_name (& # 39; body & # 39;). send_keys (Keys.CONTROL + Keys.TAB)

Although changing the tabs has no effect on the current_url function, it did not work.

Simplified Code:

second_driver = webdriver.Firefox ()
second_driver.get ()

Bedtime (1)

second_driver.find_element_by_class_name ().click()

Bedtime (1)

url = second_driver.current_url

So I do not want the URL of the new page after the click

Thank you and sorry if it's obvious that I'm a beginner.

Karatsuba's integer multiplication algorithm Python

This code does not pass all test cases. Can someone help? I only passed the direct test, then he loses precision.

Import math
to import unittest


Class IntegerMultiplier:

multiply def (self, x, y):
if x <10 or y <10:
return x * y

x = str (x)
y = str (y)

m_max = min (len (x), len (y))
x = x.rjust (m_max, & # 39; 0 & # 39;)
y = y.rjust (m_max, & # 39; 0 & # 39;)

m = math.floor (m_max / 2)

x_high = int (x[:m])
x_low = int (x[m:])

y_high = int (y[:m])
y_low = int (y[m:])

z1 = self.multiply (x_high, y_high)
z2 = self.multiply (x_low, y_low)
z3 = self.multiply ((x_low + x_high), (y_low + y_high))
z4 = z3 - z1 - z2

return z1 * (10 ** m_max) + z4 * (10 ** m) + z2


Class TestIntegerMultiplier (unittest.TestCase):

def test_normal_cases (self):
intergerMultiplier = IntegerMultiplier ()

case1 = intergerMultiplier.multiply (1234, 5678)
self.assertEqual (case1, 7006652)

if __name__ == & # 39; __ main __ & # 39 ;:
unittest.main ()
`` `

Rotate moving image in Python (Pygame)

I am currently developing a game in which a dice has a weapon (a simple black rectangle) and this weapon has to move with the dice, but has to spin and show where the mouse is. The end of the & # 39; weapon & # 39; It should also be fixed in the middle of the cube and go wherever the cube goes. I managed to make the "weapon" spin, but it turns around the middle and not the end. I also have no idea how to make the weapon stick to the cube when jumping. Any ideas?

I have 3 different files for this code, thought I should share them all to make it easier to find solutions!
main.py:

Import pygame as pg
import randomly
import from settings *
import from sprites *

Class game:
def __init __ (self):
# Initialize game window, etc
pg.init ()
pg.mixer.init ()
self.screen = pg.display.set_mode ((WIDTH, HEIGHT))
pg.display.set_caption (TITLE)
self.clock = pg.time.Clock ()
self.running = true

def new (self):
# Start a new game
self.all_sprites = pg.sprite.Group ()
self.platforms = pg.sprite.Group ()

self.player = player ()
self.all_sprites.add (self.player)

self.Gun = Gun ()
self.all_sprites.add (self.Gun)

p1 = Platform (0, HEIGHT - 40, WIDTH, 40)
self.all_sprites.add (p1)
self.platforms.add (p1)
listx = []
        listy = []
        for i in range (1,4):
Wide = random border (150,500)
listx.append (Wide)
High = random.randint (200,500)
ptf = platform (wide, high, 100,20)
self.all_sprites.add (ptf)
self.platforms.add (ptf)

self.run ()


def run (self):
# Game round
self.playing = Right
during self-play:
self.clock.tick (FPS)
self.events ()
self.update ()
self.draw ()

def update (self):
# Game Loop - Update
self.all_sprites.update ()
hits = pg.sprite.spritecollide (self.player, self.platforms, False)
if hits:
self.player.pos.y = hit[0].right
self.player.vel.y = 0

def events (self):
# Game Loop - Events
for event in pg.event.get ():
#check to close the window
if event.type == pg.QUIT:
if self-playing:
self.playing = false
self.running = false
if event.type == pg.KEYDOWN:
if event.key == pg.K_UP:
self.player.jump ()


Def Draw (self):
# Game Loop - drag
self.screen.fill (gray)
self.all_sprites.draw (self.screen)
# * AFTER * Draw everything
pg.display.flip ()

def show_start_screen (self):
# Game splash / Home screen
consist

def show_go_screen (self):
# Game over / on
consist

g = game ()
g.show_start_screen ()
while g.running:
g.new ()
g.show_go_screen ()

pg.quit ()

sprites.py:

Import pygame as pg
import from settings *
Import math
vec = pg.math.vector2

Class Player (pg.sprite.Sprite):
def __init __ (self):
pg.sprite.Sprite .__ init __ (self)
self.image = pg.Surface ((35, 35))
self.image.fill (yellow)
self.rect = self.image.get_rect ()
self.rect.center = (WIDTH / 2, HEIGHT / 2)
self.pos = vec (WIDTH / 2, HEIGHT / 2)
self.vel = vec (0, 0)
self.acc = vec (0, 0)

def jump (self):
self.vel.y = -10
self.KPRESS = 0


def update (self):
self.acc = vec (0, PLAYER_GRAV)
keys = pg.key.get_pressed ()
if key[pg.K_LEFT]:
self.acc.x = -PLAYER_ACC
if key[pg.K_RIGHT]:
self.acc.x = PLAYER_ACC

# Apply friction
self.acc.x + = self.vel.x * PLAYER_FRICTION
# Equations of motion
self.vel + = self.acc



self.pos + = self.vel + 0.5 * self.acc
Wrap # sides of the screen
if self.pos.x> WIDTH:
self.pos.x = 0
if self.pos.x <0:
self.pos.x = WIDTH


self.rect.midbottom = self.pos

Class Platform (pg.sprite.Sprite):
def __init __ (self, x, y, w, h):
pg.sprite.Sprite .__ init __ (self)
self.image = pg.Surface ((w, h))
self.image.fill (white)
self.rect = self.image.get_rect ()
self.rect.x = x
self.rect.y = y

Class Gun (pg.sprite.Sprite):
def __init __ (self):
pg.sprite.Sprite .__ init __ (self)
self.image = pg.Surface ((30,5), pg.SRCALPHA)
self.image.fill (black)
self.rect = self.image.get_rect ()
self.original_image = self.image
self.rect.center = (WIDTH / 2, HEIGHT / 2)
self.pos = vec (WIDTH / 2, HEIGHT / 2)

def update (self):
self.rotate ()
self.x = self.player.pos.x
self.y = self.player.pos.y

def turn (self):
mouse_x, mouse_y = pg.mouse.get_pos ()
rel_x, rel_y = mouse_x - self.x, mouse_y - self.y
angle = math.atan2 (rel_y, rel_x)
angle = (180 / math.pi) * -math.atan2 (rel_y, rel_x)
self.image = pg.transform.rotate (self.original_image, int (angle))
self.rect = self.image.get_rect (center = self.pos)

settings.py

Game Options / Settings
TITLE = "Jumpy!"
WIDTH = 800
HEIGHT = 600
FPS = 60
#Player features
PLAYER_ACC = 0.5
PLAYER_FRICTION = -0.12
PLAYER_GRAV = 0.5

Defining colors
gray = (60,60,60)
white = (255,255,255)
black = (0,0,0)
yellow = (255,255,0)

Algorithm – `set_includes` (String Subsequence Containment) in Python 2

I have implemented C ++ std :: includes Algorithm in Python, so that I can efficiently implement a scrabble "can I make this word" function:

def word_can_be_made_from_rack (word, rack):
return set_includes (sorted (rack), sorted (word))

Here is the implementation with some test cases:

def set_includes (haystack, needle):
j = 0
hn = len (haystack)
for c in needle:
while j! = hn and hay up[j] < c:
            j += 1
        if j == hn:
            return False
        if haystack[j] >    c:
return incorrectly
j + = 1
return true

assert set_includes (& # 39; abcdef & # 39 ;, & # 39; af & # 39;
assert set_includes (& # 39; abcdef & # 39 ;, & bgr; bce & # 39;
assert set_includes (& # 39; abcdef & # 39 ;, & # 39; abcdef & # 39;
assert set_includes (& # 39; aaaaa & # 39 ;, & # 39; a & # 39;
assert set_includes (& # 39; aaaaa & # 39 ;, & # 39; aa & # 39;
assert set_includes (& # 39; aaaaax & # 39 ;, & ax; & # 39;
assert set_includes (& # 39; abbbcxx & # 39 ;, & # 39; abc & # 39;

This is similar to searching if one list is a subsequence of another, except that it is assumed (and required) that the two input strings are sorted.

The manual management of the index j this code does not feel very pythonic. Am I missing an easier way to write this algorithm?

itertools one-liner become are accepted as answers, especially if they are more performant. 🙂